Medlar/crates/app/tests/widgets.rs
2026-11-25 07:46:35 -06:00

697 lines
25 KiB
Rust

//! Controls answer the pointer and the keyboard, popups stay in the window, dialogs hold and return the focus.
use eframe::egui::accesskit::Role;
use eframe::egui::{Event, Id, Key, Modifiers, PointerButton, Pos2, Rect, Ui, pos2, vec2};
use egui_kittest::Harness;
use egui_kittest::kittest::{NodeT, Queryable};
use medlar_app::app::App;
use medlar_app::desktop::Sources;
use medlar_app::theme::Theme;
use medlar_app::ui::dialog::{Answer, Dialog, sheet};
use medlar_app::ui::field::{Combo, Field, Number, search};
use medlar_app::ui::menu::{self, Item};
use medlar_app::ui::toast::Toasts;
use medlar_app::ui::widgets::{Button, Chip, Choice, Slider, fab_menu, group, range, segmented, split};
use medlar_core::settings::{Motion, Settings};
/// What a test changes and looks at.
struct S {
app: Option<App>,
motion: Motion,
open: bool,
flag: bool,
n: usize,
value: f64,
other: f64,
text: String,
said: Vec<String>,
rects: Vec<Rect>,
toasts: Toasts<&'static str>,
}
type H = Harness<'static, S>;
fn harness(draw: impl Fn(&mut Ui, &Theme, &mut S) + 'static) -> H {
let state = S {
app: None,
motion: Motion::Normal,
open: false,
flag: false,
n: 0,
value: 50.0,
other: 80.0,
text: String::new(),
said: vec![],
rects: vec![],
toasts: Toasts::default(),
};
let frame = move |ui: &mut Ui, s: &mut S| {
let nowhere = Sources { caelestia: "/nonexistent".into(), kde: "/nonexistent".into() };
let Some(mut app) = s.app.take() else {
s.app = Some(App::new(ui.ctx(), Settings::default(), nowhere));
ui.ctx().request_repaint();
return;
};
app.settings.motion = s.motion;
app.frame(ui, |app, ui| {
s.rects.clear();
draw(ui, &app.theme.clone(), s);
});
s.app = Some(app);
};
let mut h = Harness::builder().with_size(vec2(480.0, 360.0)).with_step_dt(1.0 / 60.0).build_ui_state(frame, state);
steps(&mut h, 10);
h
}
fn steps(h: &mut H, n: usize) {
for _ in 0..n {
h.step();
}
}
fn move_to(h: &mut H, at: Pos2) {
h.input_mut().events.push(Event::PointerMoved(at));
h.step();
}
fn button(h: &mut H, at: Pos2, button: PointerButton, pressed: bool) {
h.input_mut().events.push(Event::PointerButton { pos: at, button, pressed, modifiers: Modifiers::NONE });
h.step();
}
fn click(h: &mut H, at: Pos2) {
move_to(h, at);
button(h, at, PointerButton::Primary, true);
button(h, at, PointerButton::Primary, false);
steps(h, 40);
}
fn press(h: &mut H, key: Key) {
with(h, Modifiers::NONE, key);
}
fn with(h: &mut H, modifiers: Modifiers, key: Key) {
for pressed in [true, false] {
h.input_mut().events.push(Event::ModifiersChanged(modifiers));
h.input_mut().events.push(Event::Key { key, physical_key: None, pressed, repeat: false, modifiers });
h.step();
}
h.input_mut().events.push(Event::ModifiersChanged(Modifiers::NONE));
steps(h, 40);
}
fn type_text(h: &mut H, text: &str) {
h.input_mut().events.push(Event::Text(text.into()));
steps(h, 3);
}
fn rect(h: &H, label: &str) -> Rect {
// The words above a field have its name too; the control comes after them.
h.get_all_by_label(label).last().unwrap().rect()
}
fn at(h: &H, label: &str) -> Pos2 {
rect(h, label).center()
}
fn focused(h: &H) -> Option<String> {
h.query_all(egui_kittest::kittest::By::new().predicate(|n| n.is_focused())).next().and_then(|n| n.accesskit_node().label())
}
#[test]
fn hover_and_press_tint_squeeze_and_go_back_even_when_the_pointer_left() {
use medlar_app::ui::widgets::feel;
let mut h = harness(|ui, t, s| {
let r = Button::new("Go").show(ui, t);
let f = feel(&r, true);
(s.value, s.other) = (f.tint as f64, f.press as f64);
if r.clicked() {
s.n += 1;
}
});
let mid = at(&h, "Go");
move_to(&mut h, mid);
assert!(h.state().value < 0.08, "eased in, not there at once");
steps(&mut h, 30);
assert!((h.state().value - 0.08).abs() < 0.001);
button(&mut h, mid, PointerButton::Primary, true);
steps(&mut h, 30);
assert!((h.state().value - 0.12).abs() < 0.001 && (h.state().other - 1.0).abs() < 0.01);
move_to(&mut h, pos2(400.0, 300.0));
button(&mut h, pos2(400.0, 300.0), PointerButton::Primary, false);
steps(&mut h, 60);
assert_eq!((h.state().value, h.state().other, h.state().n), (0.0, 0.0, 0));
assert!(!h.ctx.has_requested_repaint(), "idle");
click(&mut h, mid);
assert_eq!(h.state().n, 1);
}
#[test]
fn disabled_and_busy_buttons_do_nothing_and_keep_their_place() {
let mut h = harness(|ui, t, s| {
let r = Button::new("Save file").enabled(!s.flag).busy(s.open).show(ui, t);
s.rects.push(r.rect);
s.n += usize::from(r.clicked());
});
let before = h.state().rects[0];
for (disabled, busy) in [(true, false), (false, true)] {
(h.state_mut().flag, h.state_mut().open) = (disabled, busy);
steps(&mut h, 60);
let mid = at(&h, "Save file");
click(&mut h, mid);
press(&mut h, Key::Tab);
press(&mut h, Key::Enter);
assert_eq!((h.state().n, h.state().rects[0]), (0, before));
assert!(h.get_by_label("Save file").accesskit_node().is_disabled());
}
}
#[test]
fn loading_shows_after_300_ms_and_not_before() {
use medlar_app::ui::widgets::waiting;
let mut h = harness(|ui, _, s| s.flag = waiting(ui.ctx(), Id::unique("wait"), s.open));
h.state_mut().open = true;
steps(&mut h, 17);
assert!(!h.state().flag);
steps(&mut h, 4);
assert!(h.state().flag);
h.state_mut().open = false;
h.step();
assert!(!h.state().flag);
}
#[test]
fn every_control_is_reached_and_used_with_the_keyboard() {
let mut h = harness(|ui, t, s| {
s.n += usize::from(Button::icon("pause", "Pause").show(ui, t).clicked());
Choice::new("Night").switch(ui, t, &mut s.flag);
Choice::new("Some").mixed(!s.open).checkbox(ui, t, &mut s.open);
if Chip::new("Video").filter(s.text == "v").show(ui, t).response.clicked() {
s.text = "v".into();
}
Slider::new("Limit", 0.0..=100.0, 5.0).width(200.0).show(ui, t, &mut s.value);
});
press(&mut h, Key::Tab);
assert_eq!(focused(&h).as_deref(), Some("Pause"));
press(&mut h, Key::Space);
press(&mut h, Key::Tab);
press(&mut h, Key::Enter);
press(&mut h, Key::Tab);
press(&mut h, Key::Space);
press(&mut h, Key::Tab);
press(&mut h, Key::Space);
assert_eq!((h.state().n, h.state().flag, h.state().open, h.state().text.as_str()), (1, true, true, "v"));
press(&mut h, Key::Tab);
press(&mut h, Key::ArrowRight);
press(&mut h, Key::ArrowUp);
assert_eq!((h.state().value, focused(&h).as_deref()), (60.0, Some("Limit")), "arrows move the value, not the focus");
press(&mut h, Key::End);
press(&mut h, Key::ArrowRight);
assert_eq!(h.state().value, 100.0);
press(&mut h, Key::Home);
assert_eq!(h.state().value, 0.0);
with(&mut h, Modifiers::SHIFT, Key::Tab);
assert_eq!(focused(&h).as_deref(), Some("Video"));
}
#[test]
fn the_grabbed_point_of_a_slider_stays_under_the_pointer() {
let mut h = harness(|ui, t, s| {
s.rects.push(Slider::new("Limit", 0.0..=100.0, 0.0).width(204.0).show(ui, t, &mut s.value).rect);
});
let r = h.state().rects[0];
let handle = pos2(r.left() + 102.0, r.center().y);
let grab = handle + vec2(8.0, 0.0);
move_to(&mut h, grab);
button(&mut h, grab, PointerButton::Primary, true);
steps(&mut h, 3);
assert_eq!(h.state().value, 50.0, "a press on the handle doesn't move it");
move_to(&mut h, grab + vec2(20.0, 0.0));
move_to(&mut h, grab + vec2(40.0, 0.0));
assert!((h.state().value - 70.0).abs() < 0.01, "{}", h.state().value);
button(&mut h, grab + vec2(40.0, 0.0), PointerButton::Primary, false);
steps(&mut h, 20);
click(&mut h, pos2(r.left() + 22.0, r.center().y));
assert!((h.state().value - 10.0).abs() < 0.01, "a click on the bar goes there: {}", h.state().value);
}
#[test]
fn the_handles_of_a_range_never_cross() {
let mut h = harness(|ui, t, s| {
let slider = Slider::new("Hours", 0.0..=100.0, 1.0).width(204.0);
s.rects.push(Rect::from_min_size(ui.cursor().min, vec2(204.0, 40.0)));
range(ui, t, slider, &mut s.value, &mut s.other);
});
let r = h.state().rects[0];
let low = pos2(r.left() + 102.0, r.center().y);
move_to(&mut h, low);
button(&mut h, low, PointerButton::Primary, true);
for x in [120.0, 160.0, 200.0, 400.0] {
move_to(&mut h, pos2(r.left() + x, r.center().y));
}
assert_eq!((h.state().value, h.state().other), (80.0, 80.0));
}
#[test]
fn a_pressed_button_of_a_group_grows_and_the_whole_keeps_its_width() {
let mut h = harness(|ui, t, s| {
ui.horizontal(|ui| {
group(ui, t, "Show", &[("", "All"), ("", "Loading"), ("", "Done")], &mut s.n);
s.rects.push(Button::new("Next").show(ui, t).rect);
});
});
let next = h.state().rects[0];
let mid = at(&h, "Loading");
move_to(&mut h, mid);
button(&mut h, mid, PointerButton::Primary, true);
for _ in 0..20 {
h.step();
assert_eq!(h.state().rects[0], next, "what is beside the group stays");
}
button(&mut h, mid, PointerButton::Primary, false);
assert_eq!(h.state().n, 1);
let mut h = harness(|ui, t, s| {
segmented(ui, t, "Look", &[("", "Light"), ("", "Dark")], &mut s.n);
});
let dark = at(&h, "Dark");
click(&mut h, dark);
assert_eq!(h.state().n, 1);
}
fn items() -> Vec<Item<&'static str>> {
let many = (0..40).map(|i| Item::new(format!("Queue {i}"), "queue")).collect();
vec![
Item::new("Off", "off").enabled(false),
Item::new("Open", "open").keys("Enter"),
Item::line(),
Item::sub("Priority", vec![Item::new("High", "high"), Item::new("Low", "low")]),
Item::sub("Move to", many),
Item::new("Remove", "remove").danger(),
]
}
fn menu_harness(place: Pos2) -> H {
harness(move |ui, t, s| {
let at = Rect::from_min_size(place, vec2(80.0, 40.0));
let r = ui.scope_builder(eframe::egui::UiBuilder::new().max_rect(at), |ui| Button::new("Menu").width(80.0).show(ui, t)).inner;
if let Some(a) = menu::below(ui, t, &r, items).or_else(|| menu::context(ui, t, &r, items)) {
s.said.push(a.into());
}
s.flag = menu::any_open(ui.ctx());
})
}
#[test]
fn menus_and_submenus_stay_inside_the_window() {
for place in [pos2(0.0, 0.0), pos2(400.0, 0.0), pos2(0.0, 320.0), pos2(400.0, 320.0), pos2(200.0, 160.0)] {
let mut h = menu_harness(place);
click(&mut h, place + vec2(40.0, 20.0));
assert!(h.state().flag);
let move_to_row = at(&h, "Move to");
click(&mut h, move_to_row);
let window = h.ctx.content_rect();
for node in h.query_all_by_role(Role::Menu) {
assert!(window.contains_rect(node.rect()), "{place:?}: {:?}", node.rect());
}
assert_eq!(h.query_all_by_role(Role::Menu).count(), 2);
for node in ["Open", "Remove"] {
assert!(window.contains_rect(rect(&h, node)));
}
}
}
#[test]
fn a_menu_is_clicked_toggled_and_closed_from_outside() {
let mut h = menu_harness(pos2(20.0, 20.0));
let open = pos2(60.0, 40.0);
click(&mut h, open);
assert!(h.query_by_label("Open").is_some());
click(&mut h, open);
assert!(h.query_by_label("Open").is_none(), "the button closes it again");
click(&mut h, open);
let off = at(&h, "Off");
click(&mut h, off);
assert!(h.state().flag && h.state().said.is_empty(), "a disabled row does nothing");
let row = at(&h, "Open");
click(&mut h, row);
assert!(!h.state().flag && h.state().said == ["open"]);
click(&mut h, open);
click(&mut h, pos2(470.0, 350.0));
assert!(!h.state().flag);
move_to(&mut h, open);
button(&mut h, open, PointerButton::Secondary, true);
button(&mut h, open, PointerButton::Secondary, false);
steps(&mut h, 30);
assert!(h.state().flag, "a right click opens the context menu");
let menu = h.get_by_role(Role::Menu).rect();
assert!((menu.min - open).length() < 12.0, "at the pointer: {menu:?}");
}
#[test]
fn a_submenu_opens_by_resting_on_its_row_and_survives_a_diagonal() {
let mut h = menu_harness(pos2(20.0, 20.0));
click(&mut h, pos2(60.0, 40.0));
let row = at(&h, "Priority");
move_to(&mut h, row);
steps(&mut h, 4);
assert!(h.query_by_label("High").is_none(), "not at once");
steps(&mut h, 30);
let low = at(&h, "Low");
// Over the row below for a moment, on the way to the submenu.
let below = at(&h, "Move to");
move_to(&mut h, below);
steps(&mut h, 3);
move_to(&mut h, low);
steps(&mut h, 30);
click(&mut h, low);
assert_eq!(h.state().said, ["low"]);
}
#[test]
fn a_menu_is_used_with_the_keyboard_alone_and_gives_the_focus_back() {
let mut h = menu_harness(pos2(20.0, 20.0));
press(&mut h, Key::Tab);
press(&mut h, Key::Enter);
assert!(h.state().flag && h.state().said.is_empty(), "the key that opens it chooses nothing");
press(&mut h, Key::ArrowDown);
press(&mut h, Key::ArrowRight);
assert!(h.query_by_label("High").is_some());
press(&mut h, Key::ArrowLeft);
assert!(h.query_by_label("High").is_none());
press(&mut h, Key::ArrowRight);
press(&mut h, Key::ArrowDown);
press(&mut h, Key::Enter);
assert_eq!(h.state().said, ["low"]);
assert!(!h.state().flag);
assert_eq!(focused(&h).as_deref(), Some("Menu"));
press(&mut h, Key::Space);
type_text(&mut h, "r");
press(&mut h, Key::Enter);
assert_eq!(h.state().said, ["low", "remove"], "a letter goes to its row");
with(&mut h, Modifiers::SHIFT, Key::F10);
assert!(h.state().flag);
press(&mut h, Key::ArrowUp);
press(&mut h, Key::Escape);
assert!(!h.state().flag);
assert_eq!(focused(&h).as_deref(), Some("Menu"));
}
#[test]
fn a_combo_and_a_split_button_choose() {
let mut h = harness(|ui, t, s| {
let options = ["One", "Two", "Three"].map(String::from);
Combo::new("Queue", &options, 200.0).show(ui, t, &mut s.n);
let (main, more) = split(ui, t, "download", "Download", "More", false);
if main.clicked() {
s.said.push("main".into());
}
if let Some(a) = menu::below(ui, t, &more, || vec![Item::new("Later", "later")]) {
s.said.push(a.into());
}
});
press(&mut h, Key::Tab);
press(&mut h, Key::ArrowDown);
press(&mut h, Key::ArrowDown);
press(&mut h, Key::Enter);
assert_eq!(h.state().n, 1);
let main = at(&h, "Download");
click(&mut h, main);
let more = at(&h, "More");
click(&mut h, more);
let later = at(&h, "Later");
click(&mut h, later);
assert_eq!(h.state().said, ["main", "later"]);
}
fn dialog_harness() -> H {
harness(|ui, t, s| {
s.n += usize::from(Button::new("Behind").show(ui, t).clicked());
if Button::new("Open").show(ui, t).clicked() {
s.open = true;
}
let d = Dialog::new("test", "Add a download", "Download");
let answer = d.show(ui.ctx(), t, s.open, |ui| {
Field::new("Link").clear().show(ui, t, &mut s.text);
let options = ["One", "Two"].map(String::from);
Combo::new("Queue", &options, 200.0).show(ui, t, &mut 0);
});
if let Some(a) = answer {
s.open = false;
s.said.push(format!("{a:?}"));
}
})
}
#[test]
fn a_dialog_traps_the_focus_and_gives_it_back() {
let mut h = dialog_harness();
press(&mut h, Key::Tab);
press(&mut h, Key::Tab);
assert_eq!(focused(&h).as_deref(), Some("Open"));
press(&mut h, Key::Enter);
assert!(h.state().open && h.state().said.is_empty(), "the key that opens it answers nothing");
assert_eq!(focused(&h).as_deref(), Some("Link"));
let mut seen = vec![];
for _ in 0..5 {
seen.push(focused(&h).unwrap_or_default());
press(&mut h, Key::Tab);
}
assert_eq!(seen, ["Link", "Queue", "Download", "Cancel", "Link"]);
for key in [Key::ArrowUp, Key::ArrowUp, Key::ArrowLeft, Key::ArrowUp, Key::ArrowDown] {
press(&mut h, key);
assert!(!["Behind", "Open"].contains(&focused(&h).unwrap_or_default().as_str()), "{key:?} left the dialog");
if h.query_by_label("Two").is_some() {
press(&mut h, Key::Escape);
}
}
assert!(h.state().open);
press(&mut h, Key::Escape);
assert_eq!(h.state().said, ["Cancel"]);
assert_eq!(focused(&h).as_deref(), Some("Open"));
assert!(h.query_by_label("Link").is_none(), "gone");
assert!(!h.ctx.has_requested_repaint(), "idle");
}
#[test]
fn enter_does_the_main_action_unless_a_button_has_it() {
let mut h = dialog_harness();
let open = at(&h, "Open");
click(&mut h, open);
type_text(&mut h, "https://example.org/a");
press(&mut h, Key::Enter);
assert_eq!((h.state().said.as_slice(), h.state().text.as_str()), (["Main".to_string()].as_slice(), "https://example.org/a"));
click(&mut h, open);
with(&mut h, Modifiers::SHIFT, Key::Tab);
assert_eq!(focused(&h).as_deref(), Some("Cancel"));
press(&mut h, Key::Enter);
assert_eq!(h.state().said, ["Main", "Cancel"]);
// Escape closes the list of a combo first, then the dialog.
click(&mut h, open);
let queue = at(&h, "Queue");
click(&mut h, queue);
assert!(h.query_by_label("Two").is_some());
press(&mut h, Key::Escape);
assert!(h.state().open && h.query_by_label("Two").is_none());
press(&mut h, Key::Escape);
assert!(!h.state().open);
assert_eq!(h.state().n, 0);
}
#[test]
fn what_is_behind_a_dialog_cannot_be_clicked_and_the_dialog_fits() {
let mut h = dialog_harness();
let (open, behind) = (at(&h, "Open"), at(&h, "Behind"));
click(&mut h, open);
click(&mut h, behind);
assert!(h.state().n == 0 && h.state().open, "a click beside it loses nothing");
let window = h.ctx.content_rect();
for label in ["Link", "Queue", "Cancel", "Download"] {
assert!(window.contains_rect(rect(&h, label)), "{label}");
}
}
#[test]
fn a_dangerous_question_starts_on_cancel() {
let mut h = harness(|ui, t, s| {
let d = Dialog::confirm("ask", "Delete the file?", "It can't be taken back.", "Delete", true);
if let Some(a) = d.show(ui.ctx(), t, s.open, |_| {}) {
s.open = false;
s.said.push(format!("{a:?}"));
}
});
h.state_mut().open = true;
steps(&mut h, 40);
assert_eq!(focused(&h).as_deref(), Some("Cancel"));
press(&mut h, Key::Enter);
assert_eq!(h.state().said, [format!("{:?}", Answer::Cancel)]);
h.state_mut().open = true;
steps(&mut h, 40);
let delete = at(&h, "Delete");
click(&mut h, delete);
assert_eq!(h.state().said, ["Cancel", "Main"]);
}
#[test]
fn a_dialog_works_with_motion_off() {
let mut h = dialog_harness();
h.state_mut().motion = Motion::Off;
let open = at(&h, "Open");
click(&mut h, open);
assert_eq!(focused(&h).as_deref(), Some("Link"));
press(&mut h, Key::Escape);
assert!(h.query_by_label("Link").is_none());
}
#[test]
fn a_sheet_closes_by_button_escape_and_scrim() {
let mut h = harness(|ui, t, s| {
if sheet(ui.ctx(), t, "test", "Details", s.open, |ui| {
Field::new("Name").show(ui, t, &mut s.text);
}) {
s.open = false;
s.n += 1;
}
});
for i in 0..3 {
h.state_mut().open = true;
steps(&mut h, 60);
assert_eq!(focused(&h).as_deref(), Some("Name"));
assert!(h.ctx.content_rect().contains_rect(rect(&h, "Close")));
match i {
0 => press(&mut h, Key::Escape),
1 => click(&mut h, pos2(20.0, 180.0)),
_ => {
let close = at(&h, "Close");
click(&mut h, close);
}
}
assert!(!h.state().open && h.state().n == i + 1, "way {i}");
steps(&mut h, 60);
}
}
#[test]
fn fields_clear_step_and_refuse() {
let mut h = harness(|ui, t, s| {
search(ui, t, "Search", &mut s.text, 200.0);
Number { label: "Limit", unit: "KB/s", range: 0.0..=100.0, step: 10.0, enabled: true, width: 240.0, bare: false }.show(ui, t, &mut s.value);
});
press(&mut h, Key::Tab);
type_text(&mut h, "iso");
assert_eq!(h.state().text, "iso");
press(&mut h, Key::Escape);
assert_eq!((h.state().text.as_str(), focused(&h).as_deref()), ("", Some("Search")));
type_text(&mut h, "deb");
let clear = at(&h, "Clear");
click(&mut h, clear);
assert_eq!((h.state().text.as_str(), focused(&h).as_deref()), ("", Some("Search")));
let more = at(&h, "More");
click(&mut h, more);
assert_eq!(h.state().value, 60.0);
let limit = at(&h, "Limit");
click(&mut h, limit);
press(&mut h, Key::ArrowDown);
assert_eq!(h.state().value, 50.0);
let height = rect(&h, "Limit");
with(&mut h, Modifiers::COMMAND, Key::A);
type_text(&mut h, "900");
assert_eq!(h.state().value, 50.0, "out of range is not taken");
assert!(h.query_by_label("From 0 to 100").is_some());
assert_eq!(rect(&h, "Limit"), height, "the error moves nothing");
press(&mut h, Key::Tab);
assert!(h.query_by_label("From 0 to 100").is_none(), "and dropped when the field is left");
click(&mut h, limit);
with(&mut h, Modifiers::COMMAND, Key::A);
type_text(&mut h, "12,5");
assert_eq!(h.state().value, 12.5);
}
#[test]
fn toasts_stack_wait_under_the_pointer_and_undo() {
let mut h = harness(|ui, t, s| {
if let Some(a) = s.toasts.show(ui.ctx(), t, 360.0) {
s.said.push(a.into());
}
});
h.state_mut().toasts.info("info", "First");
h.state_mut().toasts.undo("delete", "Second", "undo");
steps(&mut h, 60);
let (first, second) = (rect(&h, "First"), rect(&h, "Second"));
assert!(first.bottom() < second.top() && h.ctx.content_rect().contains_rect(second));
move_to(&mut h, second.center());
steps(&mut h, 60 * 8);
assert!(h.query_by_label("First").is_some(), "they wait while the pointer is there");
move_to(&mut h, pos2(5.0, 5.0));
steps(&mut h, 60 * 5);
assert!(h.query_by_label("First").is_none() && h.query_by_label("Second").is_some());
let undo = at(&h, "Undo");
click(&mut h, undo);
assert_eq!(h.state().said, ["undo"]);
steps(&mut h, 60);
assert!(h.state().toasts.is_empty() && !h.ctx.has_requested_repaint());
for i in 0..7 {
h.state_mut().toasts.info("info", format!("Toast {i}"));
}
steps(&mut h, 60);
assert!(h.query_by_label("Toast 2").is_none() && h.query_by_label("Toast 3").is_some(), "four at most");
assert!(rect(&h, "Toast 3").top() > 0.0);
}
#[test]
fn a_fab_menu_opens_chooses_and_closes() {
let mut h = harness(|ui, t, s| {
let items = [("link", "From a link"), ("file_open", "From a file")];
if let Some(i) = fab_menu(ui.ctx(), t, Id::unique("fab"), pos2(464.0, 344.0), ("Add", "Close"), &mut s.open, &items) {
s.said.push(items[i].1.into());
}
});
let fab = at(&h, "Add");
click(&mut h, fab);
assert!(h.state().open);
let item = at(&h, "From a file");
click(&mut h, item);
assert!(!h.state().open && h.state().said == ["From a file"]);
press(&mut h, Key::Tab);
press(&mut h, Key::Enter);
assert_eq!(focused(&h).as_deref(), Some("From a link"));
press(&mut h, Key::Escape);
assert!(!h.state().open);
assert_eq!(focused(&h).as_deref(), Some("Add"));
click(&mut h, fab);
click(&mut h, pos2(20.0, 20.0));
assert!(!h.state().open);
}
#[test]
fn a_tooltip_shows_the_shortcut_and_stays_inside() {
let mut h = harness(|ui, t, _| {
let at = Rect::from_min_size(pos2(440.0, 320.0), vec2(40.0, 40.0));
ui.scope_builder(eframe::egui::UiBuilder::new().max_rect(at), |ui| Button::icon("delete", "Remove from the list").keys("Del").show(ui, t));
});
move_to(&mut h, pos2(460.0, 340.0));
steps(&mut h, 90);
let window = h.ctx.content_rect();
let tips: Vec<Rect> = h.get_all_by_label("Remove from the list").map(|n| n.rect()).collect();
assert_eq!(tips.len(), 2, "the button and its tooltip");
assert!(tips.iter().all(|r| window.contains_rect(*r)), "{tips:?}");
}
#[test]
fn every_page_of_the_gallery_draws_at_the_smallest_size() {
use medlar_app::ui::gallery;
for page in 0..gallery::PAGES.len() {
let mut app = None;
let mut g = Harness::builder().with_size(vec2(480.0, 360.0)).build_ui(move |ui| {
let nowhere = Sources { caelestia: "/nonexistent".into(), kde: "/nonexistent".into() };
if app.is_none() {
app = Some(App::new(ui.ctx(), Settings::default(), nowhere));
return;
}
app.as_mut().unwrap().frame(ui, |app, ui| gallery::page(app, ui, page));
});
for _ in 0..5 {
g.step();
}
}
}