diff --git a/crates/egui_kittest/examples/require_button_labels_plugin.rs b/crates/egui_kittest/examples/require_button_labels_plugin.rs new file mode 100644 index 000000000..8d28a256c --- /dev/null +++ b/crates/egui_kittest/examples/require_button_labels_plugin.rs @@ -0,0 +1,50 @@ +//! Example that shows how to create a `egui_kittest` Plugin that ensures each button has a label. + +use egui_kittest::kittest::{NodeT as _, Queryable as _}; +use egui_kittest::{Harness, Plugin}; + +/// Plugin that panics if any visible button in the current UI lacks a non-empty label. +pub struct RequireButtonLabels; + +impl Plugin for RequireButtonLabels { + fn after_step(&mut self, harness: &mut Harness<'_, S>) { + for button in harness.query_all_by_role(egui::accesskit::Role::Button) { + let node = button.accesskit_node(); + + match node.label().as_deref() { + Some(label) if !label.is_empty() => {} + _ => panic!( + "Button at {:?} has no accessible label. \ + Every button must be labelled so screen readers and kittest queries \ + can address it.", + node.bounding_box(), + ), + } + } + } +} + +fn main() { + // Check tests below for usages +} + +#[test] +fn test_has_label() { + let mut harness = Harness::builder() + .with_plugin(RequireButtonLabels) + .build_ui(|ui| { + let _ = ui.button("Test"); + }); + harness.run(); // this is fine +} + +#[test] +#[should_panic] +fn test_no_label() { + let mut harness = Harness::builder() + .with_plugin(RequireButtonLabels) + .build_ui(|ui| { + let _ = ui.button(()); + }); + harness.run(); // BOOM +}