use taim_accessibility::focus::Focus;
use taim_accessibility::tree::Tree as SemanticTree;
use taim_accessibility::{Dispatch, Router};
use taim::{Size, Theme};
use taim_core::semantics::{Actions, Role, State, Value};
use taim_core::widget;
use taim_core::{Layout, layout};
use taim_renderer::Renderer;
use taim_widget::Element;
use taim_widget::{button, checkbox, radio, slider, text, text_input, toggler};
type Message = ();
const BOUNDS: Size = Size {
width: 200.0,
height: 40.0,
};
fn headless_renderer() -> Renderer {
taim_futures::futures::executor::block_on(<Renderer as taim_core::renderer::Headless>::new(
taim_core::renderer::Settings::default(),
Some("tiny-skia"),
))
.expect("tiny-skia renderer must be available")
}
fn semantics_of(
element: Element<'_, Message, Theme, Renderer>,
) -> Option<taim_core::semantics::Node> {
let mut element = element;
let renderer = headless_renderer();
let mut tree = widget::Tree::new(element.as_widget());
tree.diff(element.as_widget_mut());
let node = element.as_widget_mut().layout(
&mut tree,
&renderer,
&layout::Limits::new(Size::ZERO, BOUNDS),
);
element.as_widget().semantics(&tree, Layout::new(&node))
}
#[test]
fn a_real_button_reports_an_activatable_focusable_node() {
let node = semantics_of(button(text("Save")).on_press(()).into())
.expect("a button must report semantics");
assert_eq!(node.role, Role::Button);
assert!(node.actions.contains(Actions::ACTIVATE));
assert!(node.actions.contains(Actions::FOCUS));
assert!(node.state.contains(State::FOCUSABLE));
assert!(!node.state.contains(State::DISABLED));
assert!(node.bounds.width > 0.0, "a node must carry real bounds");
}
#[test]
fn a_button_with_no_handler_is_disabled_and_offers_nothing() {
let node = semantics_of(button(text("Save")).into()).expect("still reports a node");
assert_eq!(node.role, Role::Button);
assert!(node.state.contains(State::DISABLED));
assert!(
node.actions.is_empty(),
"a disabled button must not advertise an action that does nothing"
);
}
#[test]
fn a_real_checkbox_reports_its_checked_state_and_label() {
let unchecked = semantics_of(
checkbox(false)
.label("Remember me")
.on_toggle(|_| ())
.into(),
)
.expect("a checkbox must report semantics");
let checked =
semantics_of(checkbox(true).label("Remember me").on_toggle(|_| ()).into()).unwrap();
assert_eq!(unchecked.role, Role::Checkbox);
assert!(!unchecked.state.contains(State::CHECKED));
assert!(checked.state.contains(State::CHECKED));
assert_eq!(
checked.name.as_deref(),
Some("Remember me"),
"the visible label is the accessible name"
);
}
#[test]
fn a_checkbox_with_no_handler_is_disabled() {
let node = semantics_of(checkbox(true).label("Locked").into()).unwrap();
assert!(node.state.contains(State::DISABLED));
assert!(node.actions.is_empty());
assert!(node.state.contains(State::CHECKED));
}
#[test]
fn a_real_radio_reports_selection_and_its_label() {
#[derive(Clone, Copy, PartialEq, Eq)]
enum Choice {
A,
B,
}
let selected = semantics_of(radio("Option A", Choice::A, Some(Choice::A), |_| ()).into())
.expect("a radio must report semantics");
let unselected =
semantics_of(radio("Option B", Choice::B, Some(Choice::A), |_| ()).into()).unwrap();
assert_eq!(selected.role, Role::RadioButton);
assert!(selected.state.contains(State::CHECKED));
assert!(
selected.state.contains(State::SELECTED),
"a chosen radio is both checked and the selected member of its group"
);
assert!(!unselected.state.contains(State::CHECKED));
assert_eq!(selected.name.as_deref(), Some("Option A"));
}
#[test]
fn a_real_toggler_reports_as_a_two_state_control() {
let on = semantics_of(toggler(true).label("Wi-Fi").on_toggle(|_| ()).into())
.expect("a toggler must report semantics");
assert_eq!(on.role, Role::Checkbox);
assert!(on.state.contains(State::CHECKED));
assert_eq!(on.name.as_deref(), Some("Wi-Fi"));
}
#[test]
fn a_real_slider_reports_its_value_with_its_range() {
let node = semantics_of(slider(0.0..=100.0, 70.0, |_| ()).into())
.expect("a slider must report semantics");
assert_eq!(node.role, Role::Slider);
assert_eq!(
node.value(),
Some(&Value::Number {
current: 70.0,
min: 0.0,
max: 100.0,
})
);
assert!(node.actions.contains(Actions::INCREMENT));
assert!(node.actions.contains(Actions::DECREMENT));
assert!(node.actions.contains(Actions::SET_VALUE));
}
#[test]
fn a_real_text_input_reports_its_value() {
let node = semantics_of(text_input("Name", "Ada").on_input(|_| ()).into())
.expect("a text input must report semantics");
assert_eq!(node.role, Role::TextField);
assert_eq!(node.value(), Some(&Value::Text("Ada".into())));
assert!(node.actions.contains(Actions::SET_VALUE));
}
#[test]
fn a_read_only_text_input_does_not_offer_set_value() {
let node = semantics_of(text_input("Name", "Ada").into()).unwrap();
assert!(
!node.actions.contains(Actions::SET_VALUE),
"a field with no on_input must not claim it can be edited"
);
}
#[test]
fn a_real_secure_text_input_never_discloses_its_value() {
let node = semantics_of(
text_input("Password", "hunter2")
.secure(true)
.on_input(|_| ())
.into(),
)
.expect("a secure field still reports a node");
assert_eq!(node.role, Role::SecureField);
assert_eq!(
node.value(),
None,
"a secure field must never carry a semantic value"
);
let formatted = format!("{node:?}");
assert!(
!formatted.contains("hunter2"),
"the secret leaked through the node's debug output: {formatted}"
);
}
#[test]
fn a_secure_field_is_still_focusable_and_editable() {
let node = semantics_of(
text_input("Password", "hunter2")
.secure(true)
.on_input(|_| ())
.into(),
)
.unwrap();
assert!(node.actions.contains(Actions::FOCUS));
assert!(node.actions.contains(Actions::SET_VALUE));
assert!(node.state.contains(State::FOCUSABLE));
}
#[test]
fn a_real_control_flows_into_the_accessibility_tree_and_is_focusable() {
let node = semantics_of(button(text("Save")).on_press(()).into()).unwrap();
let id = node.id.clone();
let tree = SemanticTree::from_forest(&[node]);
assert_eq!(tree.len(), 1);
let mut focus = Focus::new();
assert!(
focus.focus(&tree, &id),
"a real button must be focusable through the real engine"
);
assert_eq!(focus.focused(), Some(&id));
}
#[test]
fn a_real_controls_action_routes_through_the_real_router() {
let node = semantics_of(button(text("Save")).on_press(()).into()).unwrap();
let id = node.id.clone();
let tree = SemanticTree::from_forest(&[node]);
assert_eq!(
Router::new().route(
&tree,
&Dispatch {
target: id.clone(),
action: Actions::ACTIVATE,
},
),
Ok(id)
);
}
#[test]
fn a_disabled_real_control_refuses_the_action_through_the_router() {
let node = semantics_of(button(text("Save")).into()).unwrap();
let id = node.id.clone();
let tree = SemanticTree::from_forest(&[node]);
assert!(
Router::new()
.route(
&tree,
&Dispatch {
target: id,
action: Actions::ACTIVATE,
},
)
.is_err(),
"a disabled button must not be activatable through assistive technology"
);
}
#[test]
fn a_disabled_real_control_is_skipped_by_focus_traversal() {
let live = semantics_of(button(text("Save")).on_press(()).into()).unwrap();
let dead = semantics_of(button(text("Locked")).into()).unwrap();
let live_id = live.id.clone();
let dead_id = dead.id.clone();
let tree = SemanticTree::from_forest(&[live, dead]);
let mut focus = Focus::new();
assert!(focus.next(&tree));
assert_eq!(focus.focused(), Some(&live_id));
let _ = focus.next(&tree);
assert_eq!(
focus.focused(),
Some(&live_id),
"traversal must skip a disabled control entirely"
);
assert!(!focus.focus(&tree, &dead_id));
}