From 779312ac0c438fdf6e5a1fd546415879b3b09869 Mon Sep 17 00:00:00 2001 From: Wybe Westra Date: Fri, 28 Jun 2024 13:03:31 +0200 Subject: [PATCH] Add basic test for egui accesskit output. (#4716) For a project I am currently attempting to use the acceskit output of egui to preform end-to-end integration tests of my application. However, the output is currently quite basic (buttons for example don't report that they are disabled). Before adding new features to the acceskit integration, I thought it would be a good idea to add some testing to egui's accesskit output. I have started with a simple test for button texts, to get feedback on whether this is the correct direction to go in. --------- Co-authored-by: Wybe Westra --- crates/egui/tests/accesskit.rs | 95 ++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 crates/egui/tests/accesskit.rs diff --git a/crates/egui/tests/accesskit.rs b/crates/egui/tests/accesskit.rs new file mode 100644 index 00000000000..f68e50b437a --- /dev/null +++ b/crates/egui/tests/accesskit.rs @@ -0,0 +1,95 @@ +//! Tests the accesskit accessibility output of egui. + +use accesskit::Role; +use egui::{Context, RawInput}; + +/// Baseline test that asserts there are no spurious nodes in the +/// accesskit output when the ui is empty. +/// +/// This gives reasonable certainty that any nodes appearing in the other accesskit outputs +/// are put there because of the widgets rendered. +#[test] +fn empty_ui_should_return_tree_with_only_root_window() { + let ctx = Context::default(); + ctx.enable_accesskit(); + + let output = ctx.run(RawInput::default(), |ctx| { + egui::CentralPanel::default().show(ctx, |_| {}); + }); + + let tree_update = output + .platform_output + .accesskit_update + .expect("Missing accesskit update"); + + let tree = tree_update.tree.unwrap(); + + assert_eq!( + tree_update.nodes.len(), + 1, + "Empty ui should produce only the root window." + ); + let (id, root) = &tree_update.nodes[0]; + + assert_eq!(*id, tree.root); + assert_eq!(root.role(), Role::Window); +} + +#[test] +fn button_text() { + let button_text = "This is a test button!"; + + let ctx = Context::default(); + ctx.enable_accesskit(); + + let output = ctx.run(RawInput::default(), |ctx| { + egui::CentralPanel::default().show(ctx, |ui| ui.button(button_text)); + }); + + let nodes = output + .platform_output + .accesskit_update + .expect("Missing accesskit update") + .nodes; + + assert_eq!( + nodes.len(), + 2, + "Expected only the root node and the button." + ); + + nodes + .iter() + .find(|(_, node)| node.role() == Role::Button && node.name() == Some(button_text)) + .expect("Button should exist in the accesskit output"); +} + +#[test] +fn toggle_button_text() { + let button_text = "A toggle button"; + + let ctx = Context::default(); + ctx.enable_accesskit(); + + let mut selected = false; + let output = ctx.run(RawInput::default(), |ctx| { + egui::CentralPanel::default().show(ctx, |ui| ui.toggle_value(&mut selected, button_text)); + }); + + let nodes = output + .platform_output + .accesskit_update + .expect("Missing accesskit update") + .nodes; + + assert_eq!( + nodes.len(), + 2, + "Expected only the root node and the button." + ); + + nodes + .iter() + .find(|(_, node)| node.role() == Role::ToggleButton && node.name() == Some(button_text)) + .expect("Toggle button should exist in the accesskit output"); +}