Merge pull request #8939 from dotCooCoo/fix/followup-8463-preview-test

Document Setup: the grid preview test edits the open dialog in one context
This commit is contained in:
BFlat authored and GitHub committed 2026-11-27 00:53:11 +07:00
commit 59f241a3cd
2 files changed
+86 -2

No files matched your search

@@ -1123,4 +1123,76 @@ mod tests {
let preset = fields(json!({"gridColors": "blue"}));
assert_eq!(super::preview_grid(&preset, &setup), (vectorcraft_doc::GridSize::Large, [hex("#ffffff"), hex("#c4dcff")]));
}
/// 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: &mut VectorcraftApp,
ctx: &egui::Context,
tiles: &mut std::collections::HashMap<egui::TextureId, Vec<egui::Color32>>,
) -> (Rect, egui::Vec2, Vec<egui::Color32>) {
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 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;
if img.size == [2, 2] {
tiles.insert(*id, img.pixels.clone());
}
}
}
out.textures_delta.clear();
preview = out
.shapes
.iter()
.filter_map(|c| match &c.shape {
egui::Shape::Rect(r) => {
r.brush.as_ref().filter(|b| tiles.contains_key(&b.fill_texture_id)).map(|b| (r.rect, b.uv, b.fill_texture_id))
}
_ => None,
})
.max_by(|a, b| a.0.area().total_cmp(&b.0.area()));
}
let (rect, uv, texture) = preview.unwrap_or_else(|| panic!("no grid preview"));
let pixels = tiles.get(&texture).cloned().unwrap_or_else(|| panic!("no tile for {texture:?}"));
(rect, uv.size(), pixels)
}
/// The tile's pixels for the grid colors `a` and `b`: `a` in the top-left and bottom-right cells.
fn tile(a: &str, b: &str) -> Vec<egui::Color32> {
let c = |h| crate::panels::c32(&hex(h));
vec![c(a), c(b), c(b), c(a)]
}
/// 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, ctx) = setup_open();
let mut tiles = std::collections::HashMap::new();
set(&app, &ctx, "tab", json!("Transparency"));
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(&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(&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(&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)));
assert_eq!(undo_steps(&app), 0);
}
}
+14 -2
View File
@@ -50,6 +50,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,
@@ -58,9 +71,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
}