use std::rc::Rc;
use std::task::Waker;
use super::{Counter, Harness, capture_logs};
use crate::{
AccessEvent, AccessRole, Accessibility, Button, FloatingWindow, Icon, Key, KeyEvent, Lock,
Modifiers, Output, Row, SessionLock, Size, Slider, Spacer, Style, Text, TextInput, Window,
};
fn tab() -> KeyEvent {
KeyEvent {
key: Key::Tab,
text: None,
modifiers: Modifiers::default(),
repeat: false,
}
}
fn harness() -> (Harness, Rc<Accessibility>) {
let harness = Harness::new();
let access = harness.cx.provide(Accessibility::default());
(harness, access)
}
fn counter(harness: &mut Harness, clicks: &Counter) -> Window {
let count = harness.cx.signal(0);
let clicks = clicks.clone();
let panel = harness.panel().child(
Row::new()
.child(
Button::new(Text::new(move || format!("clicked {} times", count.get()))).on_click(
move |_| {
clicks.hit();
count.update(|n| *n += 1);
},
),
)
.child(Spacer::new())
.child(Text::new("hello")),
);
harness.window(panel, 300.0, 30.0)
}
fn take(access: &Accessibility) -> Vec<AccessEvent> {
access.take_events(Waker::noop())
}
#[test]
fn a_counter_bar_shows_its_button_by_role_and_name() {
let (mut harness, access) = harness();
let clicks = Counter::default();
let _window = counter(&mut harness, &clicks);
let windows = access.windows();
assert_eq!(windows.len(), 1);
let frame = access.node(windows[0]).expect("the window");
assert_eq!(frame.role, AccessRole::Frame);
assert_eq!(frame.name, "crabshell");
assert_eq!(frame.parent, None);
let row = access.node(frame.children[0]).expect("the row");
assert_eq!(row.role, AccessRole::Filler);
assert_eq!(row.parent, Some(windows[0]));
let roles: Vec<_> = row
.children
.iter()
.map(|&id| access.node(id).expect("a child").role)
.collect();
assert_eq!(
roles,
[AccessRole::Button, AccessRole::Filler, AccessRole::Label]
);
let button = access.node(row.children[0]).expect("the button");
assert_eq!(button.name, "clicked 0 times");
assert_eq!(button.index, 0);
assert!(button.focusable && button.enabled && button.shown && button.clickable);
assert!(!button.focused);
assert!(button.rect.width > 0.0);
let label = access.node(row.children[2]).expect("the label");
assert_eq!((label.name.as_str(), label.index), ("hello", 2));
assert!(!label.clickable && !label.focusable);
}
#[test]
fn accessible_name_wins_over_the_text_and_follows_its_closure() {
let (mut harness, access) = harness();
let name = harness.cx.signal(String::from("volume"));
let panel = harness.panel().child(
Row::new()
.child(Button::new(Icon::new("audio")).accessible_name(move || name.get()))
.child(Button::new(Text::new("text")).accessible_name("named")),
);
let window = harness.window(panel, 300.0, 30.0);
let frame = access.node(access.windows()[0]).expect("the window");
let row = access.node(frame.children[0]).expect("the row");
let names = |access: &Accessibility| -> Vec<String> {
let row = access.node(row.children[0]).expect("a row").parent;
let row = access.node(row.expect("a parent")).expect("the row");
row
.children
.iter()
.map(|&id| access.node(id).expect("a button").name)
.collect()
};
assert_eq!(names(&access), ["volume", "named"]);
assert!(take(&access).is_empty());
access.listen(true);
name.set(String::from("muted"));
harness.frame(&window);
assert_eq!(names(&access), ["muted", "named"]);
assert_eq!(take(&access), [AccessEvent::NameChanged(row.children[0])]);
}
#[test]
fn a_label_change_renames_the_button_around_it() {
let (mut harness, access) = harness();
let clicks = Counter::default();
let window = counter(&mut harness, &clicks);
access.listen(true);
let frame = access.node(access.windows()[0]).expect("the window");
let row = access.node(frame.children[0]).expect("the row");
let button = row.children[0];
let label = access.node(button).expect("the button").children[0];
assert!(access.click(button));
harness.frame(&window);
assert_eq!(clicks.get(), 1);
assert_eq!(
access.node(button).expect("the button").name,
"clicked 1 times"
);
assert_eq!(
take(&access),
[
AccessEvent::NameChanged(label),
AccessEvent::NameChanged(button)
]
);
}
#[test]
fn a_click_needs_an_enabled_shown_button() {
let (mut harness, access) = harness();
let clicks = Counter::default();
let (on, shown) = (harness.cx.signal(true), harness.cx.signal(true));
let hit = clicks.clone();
let panel = harness.panel().child(
Row::new()
.child(
Button::new(Text::new("a"))
.enabled(move || on.get())
.visible(move || shown.get())
.on_click(move |_| hit.hit()),
)
.child(Text::new("b")),
);
let window = harness.window(panel, 300.0, 30.0);
let frame = access.node(access.windows()[0]).expect("the window");
let row = access.node(frame.children[0]).expect("the row");
let (button, text) = (row.children[0], row.children[1]);
assert!(!access.click(text), "a text has no action");
on.set(false);
harness.frame(&window);
let state = access.node(button).expect("the button");
assert!(!state.enabled && !state.focusable);
assert!(!access.click(button));
on.set(true);
shown.set(false);
harness.frame(&window);
assert!(!access.click(button));
let parent = access.node(text).expect("the text").parent;
let row = access.node(parent.expect("a parent")).expect("the row");
assert_eq!(row.children, [button, text]);
assert!(!access.node(button).expect("the button").shown);
assert_eq!(clicks.get(), 0);
}
#[test]
fn showing_and_turning_a_button_on_are_heard() {
let (mut harness, access) = harness();
let (on, shown, label) = (
harness.cx.signal(true),
harness.cx.signal(true),
harness.cx.signal(true),
);
let panel = harness.panel().child(
Row::new().child(
Button::new(
Row::new()
.child(Text::new("mute"))
.child(Text::new("now").visible(move || label.get())),
)
.enabled(move || on.get())
.visible(move || shown.get()),
),
);
let window = harness.window(panel, 300.0, 30.0);
let row = access
.node(access.windows()[0])
.expect("the window")
.children[0];
let button = access.node(row).expect("the row").children[0];
assert_eq!(access.node(button).expect("the button").name, "mute now");
access.listen(true);
on.set(false);
harness.frame(&window);
shown.set(false);
harness.frame(&window);
shown.set(true);
harness.frame(&window);
label.set(false);
harness.frame(&window);
let inner = access.node(button).expect("the button").children[0];
let now = access.node(inner).expect("the labels").children[1];
assert_eq!(
take(&access),
[
AccessEvent::Enabled(button, false),
AccessEvent::Shown(button, false),
AccessEvent::Shown(button, true),
AccessEvent::Shown(now, false),
AccessEvent::NameChanged(button),
]
);
assert_eq!(access.node(button).expect("the button").name, "mute");
}
#[test]
fn keyed_children_coming_and_going_are_heard_once_laid_out() {
let (mut harness, access) = harness();
let items = harness.cx.signal(vec![1, 2]);
let panel = harness.panel().child(Row::new().children_keyed(
move || items.get(),
|n| *n,
|_, n| Text::new(move || n.get().to_string()),
));
access.listen(true);
let window = harness.window(panel, 300.0, 30.0);
assert_eq!(take(&access), [AccessEvent::WindowsChanged]);
let row = access
.node(access.windows()[0])
.expect("the window")
.children[0];
let before = access.node(row).expect("the row").children;
items.set(vec![2, 3]);
harness.frame(&window);
let after = access.node(row).expect("the row").children;
assert_eq!(after[0], before[1]);
assert_eq!(
take(&access),
[
AccessEvent::Children {
parent: row,
child: before[0],
index: 0,
added: false
},
AccessEvent::Children {
parent: row,
child: after[1],
index: 1,
added: true
},
]
);
drop(window);
assert_eq!(take(&access), [AccessEvent::WindowsChanged]);
}
#[test]
fn a_floating_window_title_and_a_placeholder_rename_them() {
let (mut harness, access) = harness();
let (title, shown) = (
harness.cx.signal(String::from("About")),
harness.cx.signal(true),
);
let placeholder = harness.cx.signal(String::from("search"));
let window = Window::floating(
&harness.cx,
FloatingWindow::new()
.title(move || title.get())
.visible(move || shown.get())
.child(
TextInput::new(harness.cx.signal(String::new())).placeholder(move || placeholder.get()),
),
);
window.set_size(Size::new(200.0, 100.0));
harness.frame(&window);
let root = access.windows()[0];
let field = access.node(root).expect("the window").children[0];
assert_eq!(access.node(root).expect("the window").name, "About");
access.listen(true);
title.set(String::from("Settings"));
placeholder.set(String::from("find"));
harness.frame(&window);
assert_eq!(
take(&access),
[
AccessEvent::NameChanged(root),
AccessEvent::NameChanged(field)
]
);
assert_eq!(access.node(field).expect("the field").name, "find");
shown.set(false);
harness.frame(&window);
assert_eq!(take(&access), [AccessEvent::WindowsChanged]);
assert!(access.windows().is_empty());
assert!(access.node(root).is_none() && access.node(field).is_none());
shown.set(true);
harness.frame(&window);
assert_eq!(access.windows(), [root]);
}
#[test]
fn while_locked_only_the_lock_is_read_or_clicked() {
let (mut harness, access) = harness();
let clicks = Counter::default();
let window = counter(&mut harness, &clicks);
let panel = access.windows()[0];
let button = access
.node(access.node(panel).expect("the panel").children[0])
.expect("the row")
.children[0];
access.listen(true);
let lock = Lock::new(
&harness.cx,
SessionLock::new().surface(|_, _| Text::new("locked")),
);
let surface = lock.surface(&Output::new("DP-1")).expect("the lock lives");
surface.set_size(Size::new(300.0, 30.0));
harness.frame(&surface);
assert_eq!(take(&access), [AccessEvent::WindowsChanged]);
let listed = access.windows();
assert_eq!(listed.len(), 1);
assert_eq!(
access.node(listed[0]).expect("the lock").role,
AccessRole::Frame
);
assert!(access.node(panel).is_none() && access.node(button).is_none());
assert!(!access.click(button), "clicked behind the lock");
harness.frame(&window);
assert_eq!(clicks.get(), 0);
drop(surface);
assert_eq!(take(&access), [AccessEvent::WindowsChanged]);
assert_eq!(access.windows(), [panel]);
assert!(access.click(button));
assert_eq!(clicks.get(), 1);
}
#[test]
fn tab_and_the_keyboard_coming_and_going_are_heard() {
let (mut harness, access) = harness();
let clicks = Counter::default();
let window = counter(&mut harness, &clicks);
let root = access.windows()[0];
let button = access
.node(access.node(root).expect("the window").children[0])
.expect("the row")
.children[0];
window.keyboard_enter();
window.key(&tab());
assert!(take(&access).is_empty());
window.keyboard_leave();
access.listen(true);
window.keyboard_enter();
assert_eq!(
take(&access),
[
AccessEvent::Active(root, true),
AccessEvent::Focused(button, true)
]
);
assert!(access.node(button).expect("the button").focused);
assert!(access.node(root).expect("the window").active);
window.key(&tab());
assert!(take(&access).is_empty(), "Tab stays on the only button");
window.keyboard_leave();
assert_eq!(
take(&access),
[
AccessEvent::Focused(button, false),
AccessEvent::Active(root, false)
]
);
window.keyboard_enter();
access.listen(false);
assert!(take(&access).is_empty());
}
#[test]
fn a_slider_is_one_object_that_takes_the_focus() {
let (mut harness, access) = harness();
let value = harness.cx.signal(0.5);
let panel = harness.panel().child(
Row::new()
.child(Slider::new(value).accessible_name("brightness"))
.child(TextInput::new(harness.cx.signal(String::new())).placeholder("search")),
);
let window = harness.window(panel, 300.0, 30.0);
access.listen(true);
let row = access
.node(
access
.node(access.windows()[0])
.expect("the window")
.children[0],
)
.expect("the row");
let slider = access.node(row.children[0]).expect("the slider");
assert_eq!(slider.role, AccessRole::Slider);
assert_eq!(slider.name, "brightness");
assert!(slider.children.is_empty() && slider.focusable);
let field = access.node(row.children[1]).expect("the field");
assert_eq!(
(field.role, field.name.as_str()),
(AccessRole::Entry, "search")
);
assert!(field.editable && !field.multi_line);
window.keyboard_enter();
take(&access);
window.key(&tab());
assert_eq!(take(&access), [AccessEvent::Focused(row.children[0], true)]);
}
#[test]
fn a_window_leaves_the_list_when_dropped() {
let (mut harness, access) = harness();
let clicks = Counter::default();
let window = counter(&mut harness, &clicks);
let root = access.windows()[0];
drop(window);
assert!(access.windows().is_empty());
assert!(access.node(root).is_none());
assert!(!access.click(root));
}
#[test]
fn a_panicking_accessible_name_is_logged_with_its_path() {
let (mut harness, _access) = harness();
let logs = capture_logs(|| {
let panel = harness.panel().child(
Row::new().child(
Button::new(Text::new("ok"))
.style(Style::new())
.accessible_name(|| -> String { panic!("no name") }),
),
);
let _window = harness.window(panel, 300.0, 30.0);
});
assert!(
logs.contains(
"PanelWindow[DP-1]/Row/Button[0].accessible_name panicked and is disabled: no name"
),
"{logs}"
);
}