|
| 1 | +//! Tests the accesskit accessibility output of egui. |
| 2 | +
|
| 3 | +use accesskit::Role; |
| 4 | +use egui::{Context, RawInput}; |
| 5 | + |
| 6 | +/// Baseline test that asserts there are no spurious nodes in the |
| 7 | +/// accesskit output when the ui is empty. |
| 8 | +/// |
| 9 | +/// This gives reasonable certainty that any nodes appearing in the other accesskit outputs |
| 10 | +/// are put there because of the widgets rendered. |
| 11 | +#[test] |
| 12 | +fn empty_ui_should_return_tree_with_only_root_window() { |
| 13 | + let ctx = Context::default(); |
| 14 | + ctx.enable_accesskit(); |
| 15 | + |
| 16 | + let output = ctx.run(RawInput::default(), |ctx| { |
| 17 | + egui::CentralPanel::default().show(ctx, |_| {}); |
| 18 | + }); |
| 19 | + |
| 20 | + let tree_update = output |
| 21 | + .platform_output |
| 22 | + .accesskit_update |
| 23 | + .expect("Missing accesskit update"); |
| 24 | + |
| 25 | + let tree = tree_update.tree.unwrap(); |
| 26 | + |
| 27 | + assert_eq!( |
| 28 | + tree_update.nodes.len(), |
| 29 | + 1, |
| 30 | + "Empty ui should produce only the root window." |
| 31 | + ); |
| 32 | + let (id, root) = &tree_update.nodes[0]; |
| 33 | + |
| 34 | + assert_eq!(*id, tree.root); |
| 35 | + assert_eq!(root.role(), Role::Window); |
| 36 | +} |
| 37 | + |
| 38 | +#[test] |
| 39 | +fn button_text() { |
| 40 | + let button_text = "This is a test button!"; |
| 41 | + |
| 42 | + let ctx = Context::default(); |
| 43 | + ctx.enable_accesskit(); |
| 44 | + |
| 45 | + let output = ctx.run(RawInput::default(), |ctx| { |
| 46 | + egui::CentralPanel::default().show(ctx, |ui| ui.button(button_text)); |
| 47 | + }); |
| 48 | + |
| 49 | + let nodes = output |
| 50 | + .platform_output |
| 51 | + .accesskit_update |
| 52 | + .expect("Missing accesskit update") |
| 53 | + .nodes; |
| 54 | + |
| 55 | + assert_eq!( |
| 56 | + nodes.len(), |
| 57 | + 2, |
| 58 | + "Expected only the root node and the button." |
| 59 | + ); |
| 60 | + |
| 61 | + nodes |
| 62 | + .iter() |
| 63 | + .find(|(_, node)| node.role() == Role::Button && node.name() == Some(button_text)) |
| 64 | + .expect("Button should exist in the accesskit output"); |
| 65 | +} |
| 66 | + |
| 67 | +#[test] |
| 68 | +fn toggle_button_text() { |
| 69 | + let button_text = "A toggle button"; |
| 70 | + |
| 71 | + let ctx = Context::default(); |
| 72 | + ctx.enable_accesskit(); |
| 73 | + |
| 74 | + let mut selected = false; |
| 75 | + let output = ctx.run(RawInput::default(), |ctx| { |
| 76 | + egui::CentralPanel::default().show(ctx, |ui| ui.toggle_value(&mut selected, button_text)); |
| 77 | + }); |
| 78 | + |
| 79 | + let nodes = output |
| 80 | + .platform_output |
| 81 | + .accesskit_update |
| 82 | + .expect("Missing accesskit update") |
| 83 | + .nodes; |
| 84 | + |
| 85 | + assert_eq!( |
| 86 | + nodes.len(), |
| 87 | + 2, |
| 88 | + "Expected only the root node and the button." |
| 89 | + ); |
| 90 | + |
| 91 | + nodes |
| 92 | + .iter() |
| 93 | + .find(|(_, node)| node.role() == Role::ToggleButton && node.name() == Some(button_text)) |
| 94 | + .expect("Toggle button should exist in the accesskit output"); |
| 95 | +} |
0 commit comments