Medlar/crates/app/tests/shell.rs
astar76 a87272a230 S003: window, tokens, theme, motion, fonts and icons
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 00:28:08 -06:00

80 lines
2.9 KiB
Rust

//! The window's frame: idle draws nothing, settings and the desktop's colours change it while it runs.
use eframe::egui::{Ui, vec2};
use egui_kittest::Harness;
use medlar_app::app::App;
use medlar_app::desktop::Sources;
use medlar_app::motion::spring;
use medlar_app::theme::hex;
use medlar_app::tokens::spring::DEFAULT;
use medlar_core::settings::{Motion, Settings};
use std::time::{Duration, Instant};
struct State {
app: Option<App>,
target: f32,
seen: f32,
}
fn harness(settings: Settings, sources: Sources) -> Harness<'static, State> {
let draw = move |ui: &mut Ui, s: &mut State| {
let app = s.app.get_or_insert_with(|| App::new(ui.ctx(), settings.clone(), sources.clone()));
let (target, seen) = (s.target, &mut s.seen);
app.frame(ui, |_, _| *seen = spring("shell test", target, DEFAULT));
};
let mut h = Harness::builder().with_size(vec2(480.0, 360.0)).build_ui_state(draw, State { app: None, target: 0.0, seen: 0.0 });
for _ in 0..30 {
h.step();
}
h
}
fn nowhere() -> Sources {
Sources { caelestia: "/nonexistent".into(), kde: "/nonexistent".into() }
}
#[test]
fn frames_are_drawn_only_while_something_moves() {
let mut h = harness(Settings { motion: Motion::Normal, ..Settings::default() }, nowhere());
assert!(!h.ctx.has_requested_repaint(), "idle");
h.state_mut().target = 100.0;
h.step();
assert!(h.ctx.has_requested_repaint() && h.state().seen > 0.0 && h.state().seen < 100.0);
for _ in 0..240 {
h.step();
}
assert_eq!(h.state().seen, 100.0);
assert!(!h.ctx.has_requested_repaint(), "settled");
}
#[test]
fn motion_off_jumps_and_takes_effect_at_once() {
let mut h = harness(Settings::default(), nowhere());
h.state_mut().app.as_mut().unwrap().settings.motion = Motion::Off;
h.state_mut().target = 100.0;
h.step();
assert_eq!(h.state().seen, 100.0);
}
#[test]
fn a_new_desktop_colour_shows_without_a_restart() {
let dir = tempfile::tempdir().unwrap();
let caelestia = dir.path().join("scheme.json");
let write = |c: &str| std::fs::write(&caelestia, format!(r#"{{"mode":"dark","colours":{{"primary":"{c}"}}}}"#)).unwrap();
write("f5b2e1");
let mut h = harness(Settings::default(), Sources { caelestia: caelestia.clone(), ..nowhere() });
let before = h.state().app.as_ref().unwrap().theme.clone();
write("2b7fd4");
let deadline = Instant::now() + Duration::from_secs(10);
loop {
std::thread::sleep(Duration::from_millis(100));
h.step();
let now = &h.state().app.as_ref().unwrap().theme;
if *now != before {
assert_eq!(now.primary, medlar_app::theme::Theme::from_seed(
hex("2b7fd4").unwrap(), true, false, Settings::default().scheme, Settings::default().contrast).primary);
return;
}
assert!(Instant::now() < deadline, "the new colour was never noticed");
}
}