From 6209f84bcfa7a0bf9b9eb201c45dca6a01d4ebf1 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 20 Nov 2026 20:03:49 -0800 Subject: [PATCH 1/2] Document Setup: the grid preview test edits the open dialog in one context The grid preview tests drew each edit in a new egui context, so they would pass if an open dialog kept drawing the old colors' texture. A new test draws one context through a color edit, a size edit and a preset edit, and checks the pixels of the texture the preview's mesh draws. --- crates/ui-egui/src/dialogs/document_setup.rs | 51 +++++++++++++++++--- 1 file changed, 45 insertions(+), 6 deletions(-) diff --git a/crates/ui-egui/src/dialogs/document_setup.rs b/crates/ui-egui/src/dialogs/document_setup.rs index 575603e926..a27e6cae40 100644 --- a/crates/ui-egui/src/dialogs/document_setup.rs +++ b/crates/ui-egui/src/dialogs/document_setup.rs @@ -1153,32 +1153,50 @@ mod tests { /// The grid preview Document Setup draws, in a fresh context: its rect, the texture /// coordinates of its bottom-right corner (two cells per unit) and the tile's four pixels. fn grid_preview(app: &mut VectorcraftApp) -> (Rect, egui::Vec2, Vec) { + grid_preview_in(app, &preview_context(), &mut Default::default()) + } + + /// A new context with the app's fonts and theme, for drawing the grid preview. + fn preview_context() -> egui::Context { let ctx = egui::Context::default(); crate::theme::install_fonts(&ctx); crate::theme::apply(&ctx, Default::default()); - let (mut mesh, mut pixels) = (None, None); + ctx + } + + /// The grid preview Document Setup draws in `ctx`: its rect, the texture coordinates of its + /// bottom-right corner (two cells per unit) and the four pixels of the texture its mesh draws. + /// `tiles` holds the pixels of each grid texture uploaded so far in `ctx`, so every frame drawn + /// in `ctx` goes through this function. + fn grid_preview_in( + app: &mut VectorcraftApp, + ctx: &egui::Context, + tiles: &mut std::collections::HashMap>, + ) -> (Rect, egui::Vec2, Vec) { + let mut mesh = None; for _ in 0..3 { let raw = egui::RawInput { screen_rect: Some(Rect::from_min_size(Pos2::ZERO, vec2(1200.0, 900.0))), ..Default::default() }; let mut out = ctx.run_ui(raw, |ui| crate::dialogs::show(app, ui.ctx())); - let tiles: Vec = + let grids: Vec = ctx.tex_manager().read().allocated().filter(|(_, m)| m.name == crate::canvas::TRANSPARENCY_GRID).map(|(id, _)| *id).collect(); for (id, deltas) in &out.textures_delta.set { - if tiles.contains(id) + if grids.contains(id) && let Some(d) = deltas.last() { let egui::ImageData::Color(img) = &d.image; - pixels = Some(img.pixels.clone()); + tiles.insert(*id, img.pixels.clone()); } } out.textures_delta.clear(); mesh = out.shapes.iter().find_map(|c| match &c.shape { - egui::Shape::Mesh(m) if tiles.contains(&m.texture_id) => Some(m.clone()), + egui::Shape::Mesh(m) if grids.contains(&m.texture_id) => Some(m.clone()), _ => None, }); } let mesh = mesh.unwrap_or_else(|| panic!("no grid preview")); let uv = mesh.vertices.iter().fold(vec2(0.0, 0.0), |a, v| a.max(v.uv.to_vec2())); - (mesh.calc_bounds(), uv, pixels.unwrap_or_else(|| panic!("no tile"))) + let pixels = tiles.get(&mesh.texture_id).cloned().unwrap_or_else(|| panic!("no tile for {:?}", mesh.texture_id)); + (mesh.calc_bounds(), uv, pixels) } /// The tile's pixels for the grid colors `a` and `b`: `a` in the top-left and bottom-right cells. @@ -1219,6 +1237,27 @@ mod tests { assert_eq!(undo_steps(&app), 0); } + /// In an open dialog, the preview follows each edit of Grid Colors and Grid Size: after a + /// color edit it draws a tile in the new colors, and after a size edit it draws the new cells. + #[test] + fn the_grid_preview_follows_edits_in_the_open_dialog() { + let (mut app, _) = transparency_tab(); + let (ctx, mut tiles) = (preview_context(), Default::default()); + let (rect, uv, pixels) = grid_preview_in(&mut app, &ctx, &mut tiles); + assert_eq!((uv, pixels), (rect.size() / 16.0, tile(LIGHT[0], LIGHT[1])), "Medium, Light"); + app.ui.dialog.as_mut().unwrap().fields.insert("gridColors".into(), json!(["#ff0000", "#0000ff"])); + let (rect, uv, pixels) = grid_preview_in(&mut app, &ctx, &mut tiles); + assert_eq!((uv, pixels), (rect.size() / 16.0, tile("#ff0000", "#0000ff")), "color edit"); + app.ui.dialog.as_mut().unwrap().fields.insert("gridSize".into(), json!("large")); + let (rect, uv, pixels) = grid_preview_in(&mut app, &ctx, &mut tiles); + assert_eq!((uv, pixels), (rect.size() / 32.0, tile("#ff0000", "#0000ff")), "size edit"); + app.ui.dialog.as_mut().unwrap().fields.insert("gridColors".into(), json!("Dark")); + let (_, _, pixels) = grid_preview_in(&mut app, &ctx, &mut tiles); + assert_eq!(pixels, tile("#666666", "#4d4d4d"), "preset edit"); + let st = app.session.active().unwrap(); + assert_eq!((st.doc.setup.grid_size, st.doc.setup.grid_colors), (vectorcraft_doc::GridSize::Medium, LIGHT.map(hex))); + } + /// The grid preview sits beside Grid Size, Grid Colors and the color boxes, inside the dialog, /// and covers none of the tab's text or color boxes. #[test] From 16710bf8db44632ffa039dfc0ca11e5f205f30cb Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 25 Nov 2026 18:59:48 -0800 Subject: [PATCH 2/2] Tests: the grid preview test draws through the shared frame helper drawn_preview now runs each frame with testing::part_frame_uploads instead of its own ctx.run_ui loop. part_frame_uploads is part_frame with the frame's texture uploads kept in the returned FullOutput; part_frame calls it and clears them as before, so its callers see no change. --- crates/ui-egui/src/dialogs/document_setup.rs | 22 ++++++++++++-------- crates/ui-egui/src/testing.rs | 16 ++++++++++++-- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/crates/ui-egui/src/dialogs/document_setup.rs b/crates/ui-egui/src/dialogs/document_setup.rs index 6d8b492104..4c9a6ae4c7 100644 --- a/crates/ui-egui/src/dialogs/document_setup.rs +++ b/crates/ui-egui/src/dialogs/document_setup.rs @@ -1117,20 +1117,24 @@ mod tests { assert_eq!(super::preview_grid(&preset, &setup), (vectorcraft_doc::GridSize::Large, [hex("#ffffff"), hex("#c4dcff")])); } - /// The grid preview the open dialog draws in `ctx`: its rect, the span of its texture + /// The grid preview the open dialog draws in `ctx`, in frames run as the hosts run them + /// ([`crate::testing::part_frame_uploads`]): its rect, the span of its texture /// coordinates (two cells per unit) and the four pixels of the texture it draws. `tiles` holds /// the pixels of each 2 × 2 texture uploaded so far in `ctx`, so every frame drawn in `ctx` /// goes through this function. The preview is the largest rect drawn with such a texture. fn drawn_preview( - app: &VectorcraftApp, + app: &mut VectorcraftApp, ctx: &egui::Context, tiles: &mut std::collections::HashMap>, ) -> (Rect, egui::Vec2, Vec) { let mut preview = None; for _ in 0..4 { let raw = egui::RawInput { screen_rect: Some(Rect::from_min_size(Pos2::ZERO, vec2(1200.0, 900.0))), ..Default::default() }; - let dialogs = app.dialogs().unwrap(); - let mut out = ctx.run_ui(raw, |ui| dialogs.with(|h| h.run(ui))); + let mut out = crate::testing::part_frame_uploads(app, ctx, raw, |app, ui| { + if let Some(dialogs) = app.dialogs() { + dialogs.with(|h| h.run(ui)); + } + }); for (id, deltas) in &out.textures_delta.set { if let Some(d) = deltas.last() { let egui::ImageData::Color(img) = &d.image; @@ -1166,19 +1170,19 @@ mod tests { /// color edit it draws a tile in the new colors, and after a size edit it draws the new cells. #[test] fn the_grid_preview_follows_edits_in_the_open_dialog() { - let (app, ctx) = setup_open(); + let (mut app, ctx) = setup_open(); let mut tiles = std::collections::HashMap::new(); set(&app, &ctx, "tab", json!("Transparency")); - let (rect, uv, pixels) = drawn_preview(&app, &ctx, &mut tiles); + let (rect, uv, pixels) = drawn_preview(&mut app, &ctx, &mut tiles); assert_eq!((uv, pixels), (rect.size() / 16.0, tile(LIGHT[0], LIGHT[1])), "Medium, Light"); set(&app, &ctx, "gridColors", json!(["#ff0000", "#0000ff"])); - let (rect, uv, pixels) = drawn_preview(&app, &ctx, &mut tiles); + let (rect, uv, pixels) = drawn_preview(&mut app, &ctx, &mut tiles); assert_eq!((uv, pixels), (rect.size() / 16.0, tile("#ff0000", "#0000ff")), "color edit"); set(&app, &ctx, "gridSize", json!("large")); - let (rect, uv, pixels) = drawn_preview(&app, &ctx, &mut tiles); + let (rect, uv, pixels) = drawn_preview(&mut app, &ctx, &mut tiles); assert_eq!((uv, pixels), (rect.size() / 32.0, tile("#ff0000", "#0000ff")), "size edit"); set(&app, &ctx, "gridColors", json!("Dark")); - let (_, _, pixels) = drawn_preview(&app, &ctx, &mut tiles); + let (_, _, pixels) = drawn_preview(&mut app, &ctx, &mut tiles); assert_eq!(pixels, tile("#666666", "#4d4d4d"), "preset edit"); let st = app.session.active().unwrap(); assert_eq!((st.doc.setup.grid_size, st.doc.setup.grid_colors), (vectorcraft_doc::GridSize::Medium, LIGHT.map(hex))); diff --git a/crates/ui-egui/src/testing.rs b/crates/ui-egui/src/testing.rs index ac732ae644..a065c31ef8 100644 --- a/crates/ui-egui/src/testing.rs +++ b/crates/ui-egui/src/testing.rs @@ -48,6 +48,19 @@ pub(crate) fn app_frame(app: &mut crate::VectorcraftApp, ctx: &egui::Context, ra /// [`app_frame`] with `draw` showing part of the window in place of the whole app. pub(crate) fn part_frame( + app: &mut crate::VectorcraftApp, + ctx: &egui::Context, + raw: egui::RawInput, + draw: impl FnMut(&mut crate::VectorcraftApp, &mut egui::Ui), +) -> egui::FullOutput { + let mut out = part_frame_uploads(app, ctx, raw, draw); + out.textures_delta.clear(); + out +} + +/// [`part_frame`] with the frame's texture uploads kept in the output, for a test that reads them. The test clears +/// `textures_delta` once it has read it. +pub(crate) fn part_frame_uploads( app: &mut crate::VectorcraftApp, ctx: &egui::Context, mut raw: egui::RawInput, @@ -56,9 +69,8 @@ pub(crate) fn part_frame( app.adopt_context(ctx); app.raw_input(ctx, &mut raw); embed::before_input(ctx, |out| app.frame_input(out)); - let mut out = ctx.run_ui(raw, |ui| draw(app, ui)); + let out = ctx.run_ui(raw, |ui| draw(app, ui)); run_fired(app, ctx); - out.textures_delta.clear(); out }