Show the place tests' app in a window and draw every frame on its context

The Place dialog and the import options open on the window's dialog host, so an app without a window
had nowhere to show them.
This commit is contained in:
bflatastic
2026-11-22 21:18:57 +07:00
parent d733b5de3c
commit 0d9ee13eac
+32 -27
View File
@@ -60,14 +60,19 @@ fn png(w: u32, h: u32, ppi: f64) -> Vec<u8> {
vectorcraft_engine::cmd::fileio::ppi::with_png_resolution(&out, (ppi, ppi))
}
/// An app reading files from disk, with a 400×300 document.
/// An app reading files from disk, in a window, with a 400×300 document.
fn app() -> VectorcraftApp {
let services = crate::Services { read: Some(Box::new(|p: &str| std::fs::read(p).map_err(|e| e.to_string()))), ..Default::default() };
let mut app = VectorcraftApp::new(Session::new(), services);
let mut app = crate::testing::windowed(VectorcraftApp::new(Session::new(), services));
app.run("file.new", json!({"width": 400, "height": 300})).unwrap();
app
}
/// The context of `app`'s window, which every frame of a test runs on.
fn window(app: &VectorcraftApp) -> egui::Context {
app.ui_ctx.clone().expect("the app has a window")
}
/// File → Place… with `paths` chosen in the file picker.
fn place_picked_all(app: &mut VectorcraftApp, ctx: &egui::Context, paths: &[&str]) {
let picker = scripted(app);
@@ -205,7 +210,7 @@ fn laid_out(app: &mut VectorcraftApp, ctx: &egui::Context) -> egui::Rect {
#[test]
fn a_drop_with_a_position_places_on_the_canvas_and_opens_off_it() {
let mut app = app();
let ctx = crate::testing::ui_context();
let ctx = window(&app);
let rect = laid_out(&mut app, &ctx);
let pos = rect.center() + vec2(60.0, -40.0);
let want = app.canvas_view().unwrap().to_doc(pos);
@@ -225,7 +230,7 @@ fn a_drop_with_a_position_places_on_the_canvas_and_opens_off_it() {
#[test]
fn a_drop_without_a_position_opens_documents_and_places_pictures() {
let mut app = app();
let ctx = crate::testing::ui_context();
let ctx = window(&app);
laid_out(&mut app, &ctx);
let center = app.view().unwrap().center;
let uid = app.session.active().unwrap().uid;
@@ -264,7 +269,7 @@ fn a_dropped_file_of_each_library_kind_opens_in_its_destination() {
let path = dir.file(name, &bytes);
for route in 0..4 {
let mut app = app();
let ctx = crate::testing::ui_context();
let ctx = window(&app);
let rect = laid_out(&mut app, &ctx);
match route {
0 => frame(&mut app, &ctx, over(rect), &[&path], false),
@@ -307,7 +312,7 @@ fn a_dropped_file_of_each_library_kind_opens_in_its_destination() {
#[test]
fn a_dropped_png_is_placed_where_it_was_dropped() {
let mut app = app();
let ctx = crate::testing::ui_context();
let ctx = window(&app);
let rect = laid_out(&mut app, &ctx);
let dir = Folder::new("ui-place-png");
let path = dir.file("drop.png", &png(20, 10, 72.0));
@@ -330,7 +335,7 @@ fn a_dropped_png_is_placed_where_it_was_dropped() {
#[test]
fn a_dropped_document_opens_as_a_tab_of_its_own() {
let mut app = app();
let ctx = crate::testing::ui_context();
let ctx = window(&app);
laid_out(&mut app, &ctx);
let dir = Folder::new("ui-place-tab");
let svg = dir.file("dropped.svg", SVG.as_bytes());
@@ -350,7 +355,7 @@ fn a_native_svg_drop_uses_file_open_even_with_a_path_only_handle() {
let dir = Folder::new("ui-place-path-only");
let path = dir.file("path-only.svg", SVG.as_bytes());
let mut dropped = app();
let ctx = crate::testing::ui_context();
let ctx = window(&dropped);
laid_out(&mut dropped, &ctx);
let handle = Arc::new(MockDrop { path: path.clone().into(), bytes: Err("only a path".into()) }) as egui::DroppedFileHandle;
frame_files(&mut dropped, &ctx, vec![], vec![handle], false);
@@ -376,7 +381,7 @@ fn a_native_svg_drop_uses_file_open_even_with_a_path_only_handle() {
#[test]
fn an_svg_drop_without_a_path_is_detected_from_its_bytes() {
let mut app = app();
let ctx = crate::testing::ui_context();
let ctx = window(&app);
let rect = laid_out(&mut app, &ctx);
let svg = Arc::new(MockDrop { path: Default::default(), bytes: Ok(SVG.as_bytes().to_vec()) }) as egui::DroppedFileHandle;
frame_files(&mut app, &ctx, vec![], vec![svg], false);
@@ -393,7 +398,7 @@ fn an_svg_drop_without_a_path_is_detected_from_its_bytes() {
#[test]
fn multiple_svg_drops_open_individually() {
let mut app = app();
let ctx = crate::testing::ui_context();
let ctx = window(&app);
laid_out(&mut app, &ctx);
let dir = Folder::new("ui-place-several-svg");
let a = dir.file("first.svg", SVG.as_bytes());
@@ -407,7 +412,7 @@ fn multiple_svg_drops_open_individually() {
#[test]
fn unsupported_and_unreadable_drops_leave_the_document_open() {
let mut app = app();
let ctx = crate::testing::ui_context();
let ctx = window(&app);
let rect = laid_out(&mut app, &ctx);
let dir = Folder::new("ui-place-unreadable");
// Over the canvas the first is a place; the SVGs are dropped off it, so they open.
@@ -429,8 +434,8 @@ fn unsupported_and_unreadable_drops_leave_the_document_open() {
#[test]
fn files_dropped_with_no_document_open_open_and_are_recent() {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
let ctx = crate::testing::ui_context();
let mut app = crate::testing::windowed(VectorcraftApp::new(Session::new(), Default::default()));
let ctx = window(&app);
let dir = Folder::new("ui-place-no-document");
let path = dir.file("open-me.png", &png(8, 8, 72.0));
frame(&mut app, &ctx, vec![], &[], false);
@@ -453,7 +458,7 @@ fn a_file_read_after_another_document_became_active_lands_where_it_was_dropped()
let mut app = app();
let inbox = crate::place::PlaceInbox::default();
app.services.place_inbox = Some(inbox.clone());
let ctx = crate::testing::ui_context();
let ctx = window(&app);
let rect = laid_out(&mut app, &ctx);
let want = app.canvas_view().unwrap().to_doc(rect.center() + vec2(60.0, -40.0));
let drop_on_active = |app: &VectorcraftApp| match app.drop_target("late.png", DropSpot::Canvas(want), false) {
@@ -485,7 +490,7 @@ fn a_file_read_after_another_document_became_active_lands_where_it_was_dropped()
#[test]
fn the_place_dialog_lists_the_files_and_loads_the_cursor_with_several() {
let mut app = app();
let ctx = crate::testing::ui_context();
let ctx = window(&app);
let dir = Folder::new("ui-place-dialog");
let a = dir.file("a.png", &png(300, 150, 300.0));
let b = dir.file("b.png", &png(10, 10, 72.0));
@@ -506,7 +511,7 @@ fn the_place_dialog_lists_the_files_and_loads_the_cursor_with_several() {
#[test]
fn one_file_places_centred_in_the_view_and_replace_swaps_the_selection() {
let mut app = app();
let ctx = crate::testing::ui_context();
let ctx = window(&app);
app.view_mut().unwrap().center = vectorcraft_geom::Point::new(120.0, 80.0);
let dir = Folder::new("ui-place-replace");
place_picked(&mut app, &ctx, &dir.file("one.png", &png(300, 150, 300.0)));
@@ -530,7 +535,7 @@ fn one_file_places_centred_in_the_view_and_replace_swaps_the_selection() {
fn the_control_bar_shows_the_image_file_link_colour_mode_and_ppi() {
use crate::tests_removeanchors::{control_frame, has};
let mut app = app();
let ctx = crate::testing::ui_context();
let ctx = window(&app);
let dir = Folder::new("ui-place-control-bar");
let path = dir.file("photo.png", &png(30, 30, 300.0));
app.run("file.place", json!({"path": path})).unwrap();
@@ -592,7 +597,7 @@ fn the_control_bars_file_name_relinks_the_image() {
let data = vectorcraft_format::base64_encode(&png(30, 30, 72.0));
app.run("file.place", json!({"name": "red.png", "dataBase64": data, "link": false})).unwrap();
let before = selected_image(&app).geometric_bounds().unwrap();
let ctx = crate::testing::ui_context();
let ctx = window(&app);
click_label(&mut app, &ctx, "red.png");
for s in ["Replace…", "Go To Link", "Edit Original", "Update Link", "Placement Options…"] {
item(&mut app, &ctx, s);
@@ -643,7 +648,7 @@ fn the_control_bar_and_properties_trace_a_selected_image() {
let mut app = app();
let data = vectorcraft_format::base64_encode(&png(30, 30, 72.0));
app.run("file.place", json!({"name": "red.png", "dataBase64": data, "link": false})).unwrap();
let ctx = crate::testing::ui_context();
let ctx = window(&app);
let bar = control_frame(&mut app, &ctx, vec![]);
for s in ["Embedded", "Unembed…", "Image Trace", "Mask", "Crop Image", "Opacity:"] {
assert!(has(&bar, s), "{s}");
@@ -690,7 +695,7 @@ fn the_control_bars_mask_clips_the_image_and_selects_the_clipping_path() {
let mut app = app();
let data = vectorcraft_format::base64_encode(&png(30, 30, 72.0));
let img = app.run("file.place", json!({"name": "red.png", "dataBase64": data, "link": false})).unwrap()["ids"][0].as_u64().unwrap();
let ctx = crate::testing::ui_context();
let ctx = window(&app);
click_label(&mut app, &ctx, "Mask");
let st = app.session.active().unwrap();
let group = &st.doc.layers[0].children().unwrap()[0];
@@ -702,7 +707,7 @@ fn the_control_bars_mask_clips_the_image_and_selects_the_clipping_path() {
#[test]
fn a_vectorcraft_document_places_linked_and_edit_original_opens_it() {
let mut app = app();
let ctx = crate::testing::ui_context();
let ctx = window(&app);
// A one-artboard document with a rectangle.
let mut src = Session::new();
src.execute("file.new", &json!({"width": 100, "height": 50})).unwrap();
@@ -734,7 +739,7 @@ fn a_vectorcraft_document_places_linked_and_edit_original_opens_it() {
#[test]
fn the_place_dialog_links_a_pdf_page_and_embeds_it_with_link_off() {
let mut app = app();
let ctx = crate::testing::ui_context();
let ctx = window(&app);
let mut src = Session::new();
src.execute("file.new", &json!({"width": 100, "height": 50})).unwrap();
src.execute("shape.rectangle", &json!({"x": 0, "y": 0, "width": 100, "height": 50})).unwrap();
@@ -758,7 +763,7 @@ fn the_place_dialog_links_a_pdf_page_and_embeds_it_with_link_off() {
#[test]
fn the_place_dialog_offers_a_postscript_ai_and_an_eps_file_as_a_locked_object() {
let mut app = app();
let ctx = crate::testing::ui_context();
let ctx = window(&app);
let ps = b"%!PS-Adobe-3.0\n%%BoundingBox: 0 0 100 100\n%%EndComments\n1 0 0 setrgbcolor 10 10 50 50 rectfill\nshowpage\n%%EOF\n";
let dir = Folder::new("ui-place-postscript");
for name in ["legacy.ai", "legacy.eps", "legacy.ps"] {
@@ -837,7 +842,7 @@ fn click(app: &mut VectorcraftApp) {
#[test]
fn holding_escape_with_the_place_cursor_loaded_discards_one_file() {
let mut app = app();
let ctx = crate::testing::ui_context();
let ctx = window(&app);
frame(&mut app, &ctx, vec![], &[], false);
app.run("shortcuts.set", json!({"id": "select.none", "shortcut": "Escape", "force": true})).unwrap();
let id = app.run("shape.rectangle", json!({"x": 10, "y": 10, "width": 20, "height": 20})).unwrap()["id"].clone();
@@ -866,7 +871,7 @@ fn holding_escape_with_the_place_cursor_loaded_discards_one_file() {
#[test]
fn holding_a_chord_bound_to_a_command_repeats_it_with_the_place_cursor_loaded() {
let mut app = app();
let ctx = crate::testing::ui_context();
let ctx = window(&app);
frame(&mut app, &ctx, vec![], &[], false);
app.run("shortcuts.set", json!({"id": "view.zoomIn", "shortcut": "Shift+Escape", "force": true})).unwrap();
let dir = Folder::new("ui-place-held-chord");
@@ -1343,7 +1348,7 @@ fn a_file_placed_while_idle_follows_its_changes() {
// worker during the idle frames asks for a repaint.
app.canvas.worker_started = true;
app.run("prefs.set", json!({"key": "updateLinks", "value": "automatically"})).unwrap();
let ctx = crate::testing::ui_context();
let ctx = window(&app);
let mut time = 0.0;
idle(&mut app, &ctx, &mut time, 3.0, &|_| false);
// A look finds the new document without links: no wakes of its own for it.
@@ -1382,7 +1387,7 @@ fn a_look_whose_worker_stopped_does_not_stop_later_looks() {
let path = dir.file("stopped-watch.png", &png(40, 30, 72.0));
app.run("file.place", json!({"path": path})).unwrap();
let id = app.session.active().unwrap().selection.objects[0];
let ctx = crate::testing::ui_context();
let ctx = window(&app);
let mut time = 0.0;
idle(&mut app, &ctx, &mut time, 3.0, &|_| false);
// A look that is due starts in this frame and is applied in the next one, at the same time.