Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic test for egui accesskit output. #4716

Merged
merged 2 commits into from
Jun 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions crates/egui/tests/accesskit.rs
Original file line number Diff line number Diff line change
@@ -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");
}
Loading