From e60c5e3fa26478ebff55753aa1ad7eeb6b47ca0c Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 25 Nov 2026 18:20:17 -0800 Subject: [PATCH] Tests: the panel harness holds the app-window panel lookup `tests::panel_harness::floated` shows a panel docked or floating in the whole app window and returns its body and named nodes, with `Shown::past`, `past_named` and `middles`. The panel-row tests and the Gradient panel tests both use it instead of their own copies. --- crates/ui-egui/src/panels/gradient/tests.rs | 100 ++--------------- crates/ui-egui/src/panels/tests_panel_rows.rs | 58 +--------- crates/ui-egui/src/tests/panel_harness.rs | 104 +++++++++++++++++- 3 files changed, 118 insertions(+), 144 deletions(-) diff --git a/crates/ui-egui/src/panels/gradient/tests.rs b/crates/ui-egui/src/panels/gradient/tests.rs index 83ab5a2ba2..f4b57b8c8b 100644 --- a/crates/ui-egui/src/panels/gradient/tests.rs +++ b/crates/ui-egui/src/panels/gradient/tests.rs @@ -6,6 +6,7 @@ use egui::{Key, Modifiers, Pos2, pos2, vec2}; use vectorcraft_engine::Session; use super::*; +use crate::tests::panel_harness::{Place, Shown, floated}; #[test] fn dropped_colours_recolour_or_add_an_opaque_stop() { @@ -366,103 +367,20 @@ fn escape_ends_the_stop_eyedropper_and_returns_to_the_gradient_tool() { // ---------- the panel in the app window ---------- -/// Where the panel is shown in the app window. -#[derive(Clone, Copy, Debug)] -enum Place { - /// Docked in its column. - Docked, - /// Floating, this many points wide. - Floating(f32), -} - -/// The Gradient panel as one frame of the app window drew it. -struct Shown { - /// The panel's body. - body: egui::Rect, - /// The name and rect of every named node in the body. - nodes: Vec<(String, egui::Rect)>, - /// Each catalog string asked for, as the panel's language shows it. - names: Vec<(&'static str, String)>, -} - -impl Shown { - /// The rects of the nodes named `name` (a catalog string asked for). - fn rects(&self, name: &str) -> Vec { - let shown = self.names.iter().find(|(asked, _)| *asked == name).map_or(name, |(_, shown)| shown.as_str()); - let found: Vec = self.nodes.iter().filter(|(label, _)| label == shown).map(|(_, r)| *r).collect(); - assert!(!found.is_empty(), "the panel shows no {shown:?}"); - found - } - - /// The nodes that reach past either side of the body, each with its rect. - fn past(&self, nodes: impl IntoIterator) -> Vec { - let outside = |r: &egui::Rect| r.left() < self.body.left() - 0.5 || r.right() > self.body.right() + 0.5; - nodes.into_iter().filter(|(_, r)| outside(r)).map(|(name, r)| format!("{name} at {r:?}")).collect() - } - - /// The vertical middles of the nodes named `names`. - fn middles(&self, names: &[&str]) -> Vec { - names.iter().flat_map(|name| self.rects(name)).map(|r| r.center().y).collect() - } -} - /// A selected gradient stop: the panel shows its Color row. fn select_a_stop(app: &mut VectorcraftApp) { app.run("paint.setFill", json!({"gradient": {}})).unwrap(); app.run("gradient.selectStop", json!({"index": 1})).unwrap(); } -/// The Gradient panel shown at `place` in the whole app window (1440 by 900 pt) over a selected rectangle that -/// `setup` paints, drawn in `lang`, with the catalog strings `names` as `lang` shows them. -fn shown(lang: &str, place: Place, setup: fn(&mut VectorcraftApp), names: &[&'static str]) -> Shown { - use egui::accesskit::{Node, NodeId, Role}; +/// The Gradient panel shown at `place` in the whole app window over a selected rectangle that `setup` paints, drawn +/// in `lang`, with the catalog strings `names` as `lang` shows them. The Gradient Fill box is always asked for, so a +/// docked panel is found by it. +fn shown(lang: &str, place: Place, setup: fn(&mut VectorcraftApp), names: &[&str]) -> Shown { let mut app = super::super::tests_appearance::app_with_rect(); setup(&mut app); - app.session.prefs.interface_language = lang.into(); - app.run("window.panel", json!({ "panel": "gradient" })).unwrap(); - match place { - Place::Docked => app.run("window.panel.dock", json!({ "panel": "gradient" })).unwrap(), - Place::Floating(width) => { - app.run("window.panel.float", json!({ "panel": "gradient", "x": 520, "y": 120, "width": width, "height": 700 })).unwrap() - } - }; - app.run("window.panel.activate", json!({ "panel": "gradient" })).unwrap(); - let ctx = crate::testing::ui_context(); - ctx.enable_accesskit(); - let screen = egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(1440.0, 900.0)); - let mut nodes: Vec<(NodeId, Node)> = Vec::new(); - for at in 1..=8 { - let input = egui::RawInput { time: Some(f64::from(at) * 0.1), screen_rect: Some(screen), ..Default::default() }; - let out = crate::testing::app_frame(&mut app, &ctx, input); - nodes = out.platform_output.accesskit_update.map_or_else(Vec::new, |update| update.nodes); - } - // The frames set the language from the preference; the names are looked up in it before English is put back. - let names: Vec<(&'static str, String)> = names.iter().map(|name| (*name, crate::i18n::t(name).to_owned())).collect(); - let fill = crate::i18n::t("Gradient Fill").to_owned(); - crate::i18n::set_current(crate::i18n::Lang::EN); - let rect = - |node: &Node| node.bounds().map(|b| egui::Rect::from_min_max(egui::pos2(b.x0 as f32, b.y0 as f32), egui::pos2(b.x1 as f32, b.y1 as f32))); - let by_id: std::collections::HashMap = nodes.iter().map(|(id, n)| (*id, n)).collect(); - let under = |root: NodeId| { - let mut found = Vec::new(); - let mut todo = vec![root]; - while let Some(id) = todo.pop() { - let Some(node) = by_id.get(&id) else { continue }; - if let (Some(label), Some(r)) = (node.label(), rect(node)) { - found.push((label.to_owned(), r)); - } - todo.extend(node.children().iter().copied()); - } - found - }; - // The panel's body: the tab panel that holds the Gradient Fill box. - let (body, nodes) = nodes - .iter() - .filter(|(_, n)| n.role() == Role::TabPanel) - .filter_map(|(id, n)| Some((rect(n)?, under(*id)))) - .find(|(_, named)| named.iter().any(|(l, _)| *l == fill)) - .unwrap_or_else(|| panic!("no Gradient panel at {place:?} in {lang}")); - Shown { body, nodes, names } + let asked: Vec<&str> = names.iter().copied().chain(["Gradient Fill"]).collect(); + floated(app, "gradient", lang, place, &asked) } /// Docked, every control of the panel stays inside its column, with a solid fill (the panel shows its notice) and @@ -500,9 +418,9 @@ fn the_gradient_rows_wrap_in_a_narrow_panel() { "Delete Stop", ]; for lang in ["en", "fr"] { - let asked: Vec<&'static str> = names.iter().copied().chain(["Freeform Gradient"]).collect(); + let asked: Vec<&str> = names.iter().copied().chain(["Freeform Gradient"]).collect(); let panel = shown(lang, Place::Floating(crate::panel_docking::PANEL_MIN_WIDTH), select_a_stop, &asked); - let past = panel.past(names.iter().flat_map(|name| panel.rects(name).into_iter().map(|r| (name.to_string(), r)))); + let past = panel.past_named(&names); assert!(past.is_empty(), "past the panel in {lang}: {past:?}"); for freeform in panel.rects("Freeform Gradient") { let inside = freeform.left() >= panel.body.left() - 0.5 && freeform.right() <= panel.body.right() + TYPE_OVERFLOW + 0.5; diff --git a/crates/ui-egui/src/panels/tests_panel_rows.rs b/crates/ui-egui/src/panels/tests_panel_rows.rs index 15a8dace13..e249c1ea95 100644 --- a/crates/ui-egui/src/panels/tests_panel_rows.rs +++ b/crates/ui-egui/src/panels/tests_panel_rows.rs @@ -1,12 +1,9 @@ //! Headless frames of panel rows that the framework lays out: controls side by side stay inside the panel's width. -use serde_json::json; - use super::tests_appearance::app_with_rect; use super::*; -use crate::i18n::Lang; use crate::panel_docking::PANEL_MIN_WIDTH; -use crate::tests::panel_harness::{Panel, ROOM}; +use crate::tests::panel_harness::{Panel, Place, ROOM, floated}; #[test] fn the_arrowhead_row_stays_inside_the_stroke_panel() { @@ -30,62 +27,19 @@ fn the_color_themes_hex_row_stays_inside_a_narrow_panel() { } } -/// Panel `panel` floated `width` wide in the whole app window, drawn in `lang`: the rect of its body, and the rects of -/// the controls named each of `names` (catalog strings, shown in `lang`). -fn floated(panel: &str, lang: &str, width: f32, names: &[&str]) -> (egui::Rect, Vec<(String, Vec)>) { - use egui::accesskit::Role; - const AT: f32 = 520.0; - let mut app = app_with_rect(); - app.session.prefs.interface_language = lang.into(); - app.run("window.panel", json!({ "panel": panel })).unwrap(); - app.run("window.panel.float", json!({ "panel": panel, "x": AT, "y": 120, "width": width, "height": 700 })).unwrap(); - let ctx = crate::testing::ui_context(); - ctx.enable_accesskit(); - let screen = egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(1440.0, 900.0)); - let mut nodes = Vec::new(); - for at in 1..=8 { - let input = egui::RawInput { time: Some(f64::from(at) * 0.1), screen_rect: Some(screen), ..Default::default() }; - let out = crate::testing::app_frame(&mut app, &ctx, input); - nodes = out.platform_output.accesskit_update.map_or_else(Vec::new, |update| update.nodes); - } - // The frames set the language from the preference; the names are looked up in it before English is put back. - let shown: Vec<&str> = names.iter().map(|name| crate::i18n::t(name)).collect(); - crate::i18n::set_current(Lang::EN); - let rect = |node: &egui::accesskit::Node| { - node.bounds().map(|b| egui::Rect::from_min_max(egui::pos2(b.x0 as f32, b.y0 as f32), egui::pos2(b.x1 as f32, b.y1 as f32))) - }; - // The floating panel's body: the tab panel at the left edge the float asked for. - let body = nodes - .iter() - .filter(|(_, n)| n.role() == Role::TabPanel) - .find_map(|(_, n)| rect(n).filter(|r| (r.left() - AT).abs() < 1.0)) - .unwrap_or_else(|| panic!("{panel} does not float at x {AT}")); - assert!(body.width() <= width + 1.0, "{panel}'s body {body:?} is wider than the {width} the float asked for"); - let controls = shown - .into_iter() - .map(|name| { - let found: Vec = nodes.iter().filter(|(_, n)| n.label().is_some_and(|l| l == name)).filter_map(|(_, n)| rect(n)).collect(); - assert!(!found.is_empty(), "{panel} shows no {name:?}"); - (name.to_owned(), found) - }) - .collect(); - (body, controls) -} - /// The controls named `names` (catalog strings, shown in `lang`) that reach past either side of panel `panel`, /// floated at the narrowest panel width in the whole app window, each with its rect. fn past_a_narrow_panel(panel: &str, lang: &str, names: &[&str]) -> Vec { - let (body, controls) = floated(panel, lang, PANEL_MIN_WIDTH, names); - let outside = |r: &egui::Rect| r.left() < body.left() - 0.5 || r.right() > body.right() + 0.5; - controls.iter().flat_map(|(name, found)| found.iter().filter(|r| outside(r)).map(move |r| format!("{name} at {r:?}"))).collect() + floated(app_with_rect(), panel, lang, Place::Floating(PANEL_MIN_WIDTH), names).past_named(names) } #[test] fn the_profile_row_stays_on_one_line_in_a_wide_stroke_panel() { - let (_, controls) = floated("stroke", "en", 360.0, &["Profile", "Flip Along", "Flip Across"]); + let names = ["Profile", "Flip Along", "Flip Across"]; + let panel = floated(app_with_rect(), "stroke", "en", Place::Floating(360.0), &names); // The field, its caption, the list and the two buttons share one line, so their middles are level. - let middles: Vec = controls.iter().flat_map(|(_, found)| found.iter().map(|r| r.center().y)).collect(); - assert!(middles.windows(2).all(|m| (m[0] - m[1]).abs() < 1.0), "{controls:?}"); + let middles = panel.middles(&names); + assert!(middles.windows(2).all(|m| (m[0] - m[1]).abs() < 1.0), "{middles:?}"); } #[test] diff --git a/crates/ui-egui/src/tests/panel_harness.rs b/crates/ui-egui/src/tests/panel_harness.rs index 0b36cec615..9c61500431 100644 --- a/crates/ui-egui/src/tests/panel_harness.rs +++ b/crates/ui-egui/src/tests/panel_harness.rs @@ -6,6 +6,7 @@ use std::{ cell::{RefCell, RefMut}, + collections::HashMap, rc::Rc, }; @@ -13,7 +14,8 @@ use craft::behavior::PartRef; use craft::ui::runtime::frame::take_output; use craft::ui::testing::pointer::part_rect_of_node; use craft::ui::testing::{Fixture, Target}; -use egui::{Event, Key, Modifiers, PointerButton, Pos2, Rect, Ui, Vec2, vec2}; +use egui::{Event, Key, Modifiers, PointerButton, Pos2, Rect, Ui, Vec2, pos2, vec2}; +use serde_json::json; use crate::VectorcraftApp; @@ -361,3 +363,103 @@ impl Panel { self.fixture.tree().focused_key() == self.look(name).node } } + +/// Where [`floated`] shows a panel in the app window. +#[derive(Clone, Copy, Debug)] +pub(crate) enum Place { + /// Docked in its column. + Docked, + /// Floating at x 520, this many points wide. + Floating(f32), +} + +/// A panel as one frame of the whole app window drew it. +pub(crate) struct Shown { + /// The panel's body. + pub(crate) body: Rect, + /// The name and rect of every named node in the body. + pub(crate) nodes: Vec<(String, Rect)>, + /// Each catalog string asked for, with the text the panel's language shows for it. + names: Vec<(String, String)>, +} + +impl Shown { + /// The rects of the nodes named `name` (a catalog string asked for). + pub(crate) fn rects(&self, name: &str) -> Vec { + let shown = self.names.iter().find(|(asked, _)| asked == name).map_or(name, |(_, shown)| shown.as_str()); + let found: Vec = self.nodes.iter().filter(|(label, _)| label == shown).map(|(_, r)| *r).collect(); + assert!(!found.is_empty(), "the panel shows no {shown:?}"); + found + } + + /// The nodes of `nodes` that reach past either side of the body, each with its rect. + pub(crate) fn past(&self, nodes: impl IntoIterator) -> Vec { + let outside = |r: &Rect| r.left() < self.body.left() - 0.5 || r.right() > self.body.right() + 0.5; + nodes.into_iter().filter(|(_, r)| outside(r)).map(|(name, r)| format!("{name} at {r:?}")).collect() + } + + /// The nodes named each of `names` (catalog strings asked for) that reach past either side of the body, each with + /// its rect. + pub(crate) fn past_named(&self, names: &[&str]) -> Vec { + self.past(names.iter().flat_map(|name| self.rects(name).into_iter().map(|r| ((*name).to_owned(), r)))) + } + + /// The vertical middles of the nodes named `names`, in order. + pub(crate) fn middles(&self, names: &[&str]) -> Vec { + names.iter().flat_map(|name| self.rects(name)).map(|r| r.center().y).collect() + } +} + +/// Panel `panel` of `app` shown at `place` in the whole app window (1440 by 900 pt), drawn in `lang`, with the catalog +/// strings `names` as `lang` shows them. The test sets `app` up before the call. A floating panel's body is the tab +/// panel at the left edge the float asked for; a docked panel's body is the tab panel that shows every one of `names`. +pub(crate) fn floated(mut app: VectorcraftApp, panel: &str, lang: &str, place: Place, names: &[&str]) -> Shown { + use egui::accesskit::{Node, NodeId, Role}; + const AT: f32 = 520.0; + app.session.prefs.interface_language = lang.into(); + app.run("window.panel", json!({ "panel": panel })).unwrap(); + match place { + Place::Docked => app.run("window.panel.dock", json!({ "panel": panel })).unwrap(), + Place::Floating(width) => app.run("window.panel.float", json!({ "panel": panel, "x": AT, "y": 120, "width": width, "height": 700 })).unwrap(), + }; + app.run("window.panel.activate", json!({ "panel": panel })).unwrap(); + let ctx = crate::testing::ui_context(); + ctx.enable_accesskit(); + let screen = Rect::from_min_size(Pos2::ZERO, vec2(1440.0, 900.0)); + let mut nodes: Vec<(NodeId, Node)> = Vec::new(); + for at in 1..=8 { + let input = egui::RawInput { time: Some(f64::from(at) * 0.1), screen_rect: Some(screen), ..Default::default() }; + let out = crate::testing::app_frame(&mut app, &ctx, input); + nodes = out.platform_output.accesskit_update.map_or_else(Vec::new, |update| update.nodes); + } + // The frames set the language from the preference; the names are looked up in it before English is put back. + let names: Vec<(String, String)> = names.iter().map(|name| ((*name).to_owned(), crate::i18n::t(name).to_owned())).collect(); + crate::i18n::set_current(crate::i18n::Lang::EN); + let rect = |node: &Node| node.bounds().map(|b| Rect::from_min_max(pos2(b.x0 as f32, b.y0 as f32), pos2(b.x1 as f32, b.y1 as f32))); + let by_id: HashMap = nodes.iter().map(|(id, n)| (*id, n)).collect(); + let under = |root: NodeId| { + let mut found = Vec::new(); + let mut todo = vec![root]; + while let Some(id) = todo.pop() { + let Some(node) = by_id.get(&id) else { continue }; + if let (Some(label), Some(r)) = (node.label(), rect(node)) { + found.push((label.to_owned(), r)); + } + todo.extend(node.children().iter().copied()); + } + found + }; + let mut bodies = nodes.iter().filter(|(_, n)| n.role() == Role::TabPanel).filter_map(|(id, n)| Some((rect(n)?, under(*id)))); + let found = match place { + Place::Docked => { + assert!(!names.is_empty(), "a docked panel is found by the names it shows"); + bodies.find(|(_, named)| names.iter().all(|(_, shown)| named.iter().any(|(label, _)| label == shown))) + } + Place::Floating(_) => bodies.find(|(r, _)| (r.left() - AT).abs() < 1.0), + }; + let (body, nodes) = found.unwrap_or_else(|| panic!("no {panel} panel at {place:?} in {lang}")); + if let Place::Floating(width) = place { + assert!(body.width() <= width + 1.0, "{panel}'s body {body:?} is wider than the {width} the float asked for"); + } + Shown { body, nodes, names } +}