use super::open;
use crate::paint_probe::frame::Window;
use crate::settings::Settings;
use crate::test_support::window::pane;
use crate::ui::settings::rows::PINNED;
use crate::ui::settings::search::{NOTHING, OPEN, hits};
use crate::ui::settings::tree;
use crate::ui::{Model, Page};
use egui_kittest::kittest::Queryable;
fn control_of(window: &Window, model: &mut Model, label: &str) -> egui::Pos2 {
let at = crate::paint_probe::frame::locate_in(window, label, |ctx| {
crate::ui::render(ctx, model);
})
.expect("the label is on the glass");
let inside = window
.ctx()
.data(|d| d.get_temp::<egui::Rect>(egui::Id::new(crate::ui::overlay::BOX)))
.expect("the box");
egui::pos2(inside.right() - crate::ui::theme::space::L, at.y)
}
#[test]
fn a_check_at_the_rows_right_end_writes_the_field_and_the_pin_the_bar() {
let window = Window::new();
let mut model = open(Page::Seat(crate::settings::Page::Look));
window.frame(Vec::new(), |ctx| crate::ui::render(ctx, &mut model));
let at = control_of(&window, &mut model, "bubbles");
crate::paint_probe::frame::click(&window, at, |ctx| crate::ui::render(ctx, &mut model));
assert!(
!model.settings.on(crate::settings::Toggle::Bubbles),
"the check flipped the field"
);
let mut model = open(Page::Seat(crate::settings::Page::Bar));
window.frame(Vec::new(), |ctx| crate::ui::render(ctx, &mut model));
let at = control_of(&window, &mut model, PINNED);
crate::paint_probe::frame::click(&window, at, |ctx| crate::ui::render(ctx, &mut model));
assert!(model.bar.pinned, "the pin's row is the bar's own fact");
}
#[test]
fn a_slider_moves_on_the_arrows_and_writes_its_field() {
let window = Window::new();
let mut model = open(Page::Seat(crate::settings::Page::Input));
window.frame(Vec::new(), |ctx| crate::ui::render(ctx, &mut model));
let shown = format!("{} pt", crate::settings::WHEEL);
let value = crate::paint_probe::frame::locate_in(&window, &shown, |ctx| {
crate::ui::render(ctx, &mut model);
})
.expect("the wheel's number is on the glass");
let trough = egui::pos2(value.x - 60.0, value.y);
crate::paint_probe::frame::click(&window, trough, |ctx| crate::ui::render(ctx, &mut model));
let pressed = model.settings.wheel;
assert!(
(pressed - crate::settings::WHEEL).abs() > f32::EPSILON,
"{pressed}"
);
let right = egui::Event::Key {
key: egui::Key::ArrowRight,
physical_key: None,
pressed: true,
repeat: false,
modifiers: egui::Modifiers::NONE,
};
window.frame(vec![right], |ctx| crate::ui::render(ctx, &mut model));
assert!(
(model.settings.wheel - pressed).abs() > f32::EPSILON,
"{pressed} -> {}",
model.settings.wheel
);
}
#[test]
fn a_row_whose_kind_and_value_disagree_paints_nothing() {
let lying = crate::settings::Knob {
key: "lying",
page: crate::settings::Page::Look,
label: "a knob that lies",
help: "its kind says a toggle and its value is a number",
kind: crate::settings::Kind::Toggle,
get: |_| crate::settings::Knobbed::At(1.0),
set: |_, _| {},
};
let mut settings = Settings::default();
let mut jump = None;
let painted = pane(|ui| crate::ui::settings::rows::knob(ui, &mut settings, &mut jump, &lying));
assert!(painted.trim().is_empty(), "{painted}");
}
#[test]
fn the_search_finds_rows_across_every_page() {
let found = hits("wheel");
assert_eq!(found.len(), 2, "{found:?}");
assert_eq!(found[0].key, Some("wheel"));
assert_eq!(
found[1].page,
Page::Seat(crate::settings::Page::Input),
"the page by its help"
);
let found = hits("pinned");
assert_eq!(found.len(), 1);
assert_eq!(found[0].label, PINNED);
assert!(hits("roles").iter().any(|hit| hit.page == Page::Roles));
assert!(hits("nothing says this").is_empty());
}
#[test]
fn a_hit_turns_the_overlay_to_its_page_and_jumps_to_the_row() {
let mut model = open(Page::Seat(crate::settings::Page::Look));
"bar linger".clone_into(&mut model.seeking.text);
let mut harness = crate::snapshot::seat(model, 1400.0, 900.0);
assert!(harness.query_all_by_label("bar linger").next().is_some());
assert!(
harness.query_all_by_label("scale").next().is_none(),
"the page's rows stand down under the hits"
);
harness.get_by_label(OPEN).click();
harness.run();
assert_eq!(
harness.state().page(),
Some(Page::Seat(crate::settings::Page::Bar))
);
assert!(harness.state().seeking.text.is_empty());
assert_eq!(
harness.state().seeking.jump,
None,
"spent on the row's frame"
);
"no such row".clone_into(&mut harness.state_mut().seeking.text);
harness.run();
assert!(harness.query_all_by_label(NOTHING).next().is_some());
}
#[test]
fn at_the_matrix_size_the_tree_is_a_select_and_the_rows_stack() {
let look = Page::default();
let mut harness = crate::snapshot::seat(open(look), 400.0, 700.0);
assert!(
harness
.query_all_by_label(Page::Roles.label())
.next()
.is_none(),
"no column"
);
for knob in crate::settings::KNOBS
.iter()
.filter(|knob| Page::Seat(knob.page) == look)
{
assert!(
harness.query_all_by_label(knob.label).next().is_some(),
"{} stacked on the page",
knob.key
);
}
harness.get_by_label(&tree::path(look)).click();
harness.run();
harness.get_by_label(&tree::path(Page::Clients)).click();
harness.run();
assert_eq!(harness.state().page(), Some(Page::Clients));
}