#[cfg(test)]
mod tests {
use crate::integ as h;
use xa11y::*;
fn ensure_checkbox_linux(app: &App, want_on: bool) {
let chk = app
.locator("check_box")
.element()
.expect("check_box not found");
let is_on = chk.states.checked == Some(Toggled::On);
if is_on != want_on {
chk.provider().press(&chk).expect("press failed");
std::thread::sleep(std::time::Duration::from_millis(150));
}
}
#[test]
#[ignore]
fn event_try_recv_returns_none_when_idle_linux() {
use std::time::Duration;
let app = h::app_root();
let sub = app.subscribe().expect("subscribe");
std::thread::sleep(Duration::from_millis(300));
while sub.try_recv().is_some() {}
assert!(sub.try_recv().is_none());
}
#[test]
#[ignore]
fn event_recv_times_out_when_idle_linux() {
use std::time::Duration;
let app = h::app_root();
let sub = app.subscribe().expect("subscribe");
std::thread::sleep(Duration::from_millis(300));
while sub.try_recv().is_some() {}
let r = sub.recv(Duration::from_millis(300));
assert!(
matches!(r, Err(Error::Timeout { .. })),
"expected Timeout, got {:?}",
r
);
}
#[test]
#[ignore]
fn event_drop_unsubscribes_cleanly_linux() {
let app = h::app_root();
{
let _sub = app.subscribe().expect("subscribe");
}
let _sub2 = app.subscribe().expect("re-subscribe");
}
#[test]
#[ignore]
fn event_metadata_populated_linux() {
use std::time::Duration;
let app = h::app_root();
let expected_pid = app.pid;
let slider = app.locator(r#"[role="slider"]"#).element().expect("slider");
let target_val = if slider.numeric_value == Some(42.0) {
17.0
} else {
42.0
};
let sub = app.subscribe().expect("subscribe");
slider
.provider()
.set_numeric_value(&slider, target_val)
.expect("set_numeric_value");
let event = sub
.wait_for(|_| true, Duration::from_secs(3))
.expect("at least one event must arrive within 3s");
if let Some(pid) = expected_pid {
assert_eq!(event.app_pid, pid, "app_pid must match subscribed app");
}
assert!(!event.app_name.is_empty(), "app_name must be populated");
}
#[test]
#[ignore]
fn event_focus_changed_linux() {
use std::time::Duration;
let app = h::app_root();
let target = app
.locator(r#"button[name="New"]"#)
.element()
.expect("New button not found");
let sub = app.subscribe().expect("subscribe");
target.provider().focus(&target).expect("focus failed");
let event = sub
.wait_for(
|e| e.kind == EventKind::FocusChanged,
Duration::from_secs(3),
)
.expect("FocusChanged must be delivered within 3s");
assert_eq!(event.kind, EventKind::FocusChanged);
let tgt = event.target.expect("FocusChanged target must be populated");
assert_eq!(
tgt.role,
Role::Button,
"expected Button, got {:?}",
tgt.role
);
}
#[test]
#[ignore]
fn event_value_changed_linux() {
use std::time::Duration;
let app = h::app_root();
let slider = app
.locator(r#"[role="slider"]"#)
.element()
.expect("slider not found");
let sub = app.subscribe().expect("subscribe");
slider
.provider()
.set_numeric_value(&slider, 73.0)
.expect("set_numeric_value failed");
let event = sub
.wait_for(
|e| e.kind == EventKind::ValueChanged,
Duration::from_secs(3),
)
.expect("ValueChanged must be delivered within 3s");
assert_eq!(event.kind, EventKind::ValueChanged);
let tgt = event.target.expect("target");
assert_eq!(tgt.role, Role::Slider);
}
#[test]
#[ignore]
fn event_name_changed_linux() {
use std::time::Duration;
let app = h::app_root();
ensure_checkbox_linux(&app, false);
let app = h::app_root();
let submit = h::named(&app, "Submit");
submit
.provider()
.press(&submit)
.expect("prime press failed");
std::thread::sleep(Duration::from_millis(200));
let app = h::app_root();
ensure_checkbox_linux(&app, true);
std::thread::sleep(Duration::from_millis(200));
let app = h::app_root();
let sub = app.subscribe().expect("subscribe");
let submit = h::named(&app, "Submit");
submit
.provider()
.press(&submit)
.expect("trigger press failed");
let event = sub
.wait_for(|e| e.kind == EventKind::NameChanged, Duration::from_secs(3))
.expect("NameChanged must be delivered within 3s");
assert_eq!(event.kind, EventKind::NameChanged);
drop(sub);
let app = h::app_root();
ensure_checkbox_linux(&app, false);
}
#[test]
#[ignore]
fn event_announcement_linux() {
use std::time::Duration;
let app = h::app_root();
let announce = app
.locator(r#"button[name="Announce"]"#)
.element()
.expect("Announce button not found");
let sub = app.subscribe().expect("subscribe");
announce.provider().press(&announce).expect("press failed");
let event = sub
.wait_for(
|e| e.kind == EventKind::Announcement,
Duration::from_secs(3),
)
.expect("Announcement must be delivered within 3s");
assert_eq!(event.kind, EventKind::Announcement);
}
#[test]
#[ignore]
fn event_state_changed_checked_linux() {
use std::time::Duration;
let app = h::app_root();
let chk = app
.locator("check_box")
.element()
.expect("check_box not found");
let was_on = chk.states.checked == Some(Toggled::On);
let sub = app.subscribe().expect("subscribe");
chk.provider().press(&chk).expect("press failed");
let event = sub
.wait_for(
|e| {
matches!(
e.kind,
EventKind::StateChanged {
flag: StateFlag::Checked,
..
}
)
},
Duration::from_secs(3),
)
.expect("StateChanged{Checked} must be delivered within 3s");
match event.kind {
EventKind::StateChanged {
flag: StateFlag::Checked,
value,
} => {
assert_eq!(value, !was_on, "checked flag must flip");
}
other => panic!("unexpected kind: {:?}", other),
}
let app = h::app_root();
let chk = app
.locator("check_box")
.element()
.expect("check_box not found (post-test)");
if (chk.states.checked == Some(Toggled::On)) != was_on {
chk.provider()
.press(&chk)
.expect("post-test press to restore checkbox");
}
}
}