From 6e4b57b9e4e002aeeab786ebda78a47ddf665fe5 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 18 Nov 2026 19:22:11 -0500 Subject: [PATCH] test compile fix 2 (all) (swarm/tfix2); committed by the lead --- Cargo.lock | 1 + apps/pdfcraft/Cargo.toml | 2 + apps/pdfcraft/src/tests.rs | 14 +++- crates/ui-egui/tests/it/comments.rs | 33 +++++--- crates/ui-egui/tests/it/content.rs | 27 +++++-- crates/ui-egui/tests/it/control.rs | 31 +++++--- crates/ui-egui/tests/it/editing.rs | 97 +++++++++++++++-------- crates/ui-egui/tests/it/javascript.rs | 7 +- crates/ui-egui/tests/it/preferences.rs | 16 +++- crates/ui-egui/tests/it/prepare.rs | 24 ++++-- crates/ui-egui/tests/it/print.rs | 18 +++-- crates/ui-egui/tests/it/reading_colors.rs | 2 +- crates/ui-egui/tests/it/recovery.rs | 7 +- crates/ui-egui/tests/it/reload.rs | 18 +++-- crates/ui-egui/tests/it/scan_clean.rs | 30 ++++--- crates/ui-egui/tests/it/support/mod.rs | 11 +++ crates/ui-egui/tests/it/view.rs | 2 +- crates/ui-egui/tests/it/viewer.rs | 2 +- crates/ui-egui/tests/perf.rs | 5 +- 19 files changed, 232 insertions(+), 115 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c87f21ff92..83460a99de 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6021,6 +6021,7 @@ dependencies = [ "craft-update", "eframe", "egui", + "egui_kittest", "fmv-macos-events", "log", "pdfcraft-engine", diff --git a/apps/pdfcraft/Cargo.toml b/apps/pdfcraft/Cargo.toml index 68fd2c5cd3..0e6299f6fa 100644 --- a/apps/pdfcraft/Cargo.toml +++ b/apps/pdfcraft/Cargo.toml @@ -46,6 +46,8 @@ raw-window-handle = "0.6" [dev-dependencies] # Waits for wgpu's adapter, device and error-scope futures in tests (already in the tree through wgpu). pollster = "1.0" +# Runs the app for a frame in tests, so what it shows (its notices) can be read (already in the tree through the UI crate's tests). +egui_kittest = { workspace = true, features = ["eframe"] } [target.'cfg(target_os = "linux")'.dev-dependencies] # Exercise the vendored Wayland scroll policy with the actual winit event type. diff --git a/apps/pdfcraft/src/tests.rs b/apps/pdfcraft/src/tests.rs index 3ad7137db9..284dd03b83 100644 --- a/apps/pdfcraft/src/tests.rs +++ b/apps/pdfcraft/src/tests.rs @@ -59,15 +59,23 @@ fn the_window_and_windows_taskbar_icons_load_from_the_bundled_png() { #[test] fn a_software_renderer_is_shown_to_the_user() { use eframe::wgpu::DeviceType; + // The notices the window shows after one frame of `app`. + fn shown(app: pdfcraft_ui_egui::PdfCraftApp) -> Vec { + let mut h = egui_kittest::Harness::builder().build_eframe(move |_cc| app); + h.run_steps(2); + let cards = craft::ui::runtime::toast::cards(&h.ctx, &craft::ui::runtime::env::WindowKey::main()); + cards.iter().map(|card| card.toast.message.as_str().to_owned()).collect() + } // Issue #519: WARP or llvmpipe is announced in the app, not just in pdfcraft.log. let mut app = crate::startup::test_app(); super::notify_software_renderer(&mut app, DeviceType::Cpu); - let notice = app.toast.as_ref().map(|(m, _)| m.as_str()).unwrap_or_default(); - assert!(notice.contains("without a graphics processor"), "{notice:?}"); + let notices = shown(app); + assert!(notices.iter().any(|n| n.contains("without a graphics processor")), "{notices:?}"); for gpu in [DeviceType::IntegratedGpu, DeviceType::DiscreteGpu, DeviceType::VirtualGpu, DeviceType::Other] { let mut app = crate::startup::test_app(); super::notify_software_renderer(&mut app, gpu); - assert!(app.toast.is_none(), "{gpu:?}: {:?}", app.toast); + let notices = shown(app); + assert!(notices.is_empty(), "{gpu:?}: {notices:?}"); } } diff --git a/crates/ui-egui/tests/it/comments.rs b/crates/ui-egui/tests/it/comments.rs index d961eee28c..2cdbf7aa4e 100644 --- a/crates/ui-egui/tests/it/comments.rs +++ b/crates/ui-egui/tests/it/comments.rs @@ -644,11 +644,11 @@ fn shapes_and_ink_are_drawn_by_dragging() { assert!((r[0] - 40.0).abs() < 3.0 && (r[1] - 40.0).abs() < 3.0 && (r[2] - 140.0).abs() < 3.0 && (r[3] - 100.0).abs() < 3.0, "{r:?}"); } -/// Press, drag and release a stroke, running only one frame after the release. Finished -/// renders are held back from the view (`hold_rasters`), so the raster the commit asks for can't -/// arrive before the test looks, however fast it renders; a test lets it through by clearing it. +/// Press, drag and release a stroke, running only one frame after the release. On a +/// [`pen_harness`] the render jobs wait for the test, so the raster the commit asks for can't +/// arrive before the test looks, however fast it renders; a test lets it through with +/// `support::settle_held`. fn stroke_without_settling(h: &mut Harness<'static, PdfCraftApp>, a: (f32, f32), b: (f32, f32)) { - h.state_mut().views[0].hold_rasters = true; let (from, to) = (at(h, a.0, a.1), at(h, b.0, b.1)); h.hover_at(from); h.run_steps(1); @@ -667,13 +667,24 @@ fn stroke_without_settling(h: &mut Harness<'static, PdfCraftApp>, a: (f32, f32), const PEN_COLOUR: [f64; 3] = [0.2, 0.4, 0.6]; const PEN_WIDTH: f64 = 7.0; -/// A harness with the Ink tool armed in the pen colour and width above. -fn pen_harness() -> Harness<'static, PdfCraftApp> { - harness(|app| { +/// A harness with the Ink tool armed in the pen colour and width above, whose jobs (the page +/// renders among them) wait until the test runs them; the pages are rendered once before it returns. +fn pen_harness() -> (Harness<'static, PdfCraftApp>, std::sync::Arc) { + let (app, jobs) = crate::control::app_with_manual_jobs(); + let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |_cc| { + let mut app = app; + app.set_option(&egui::Context::default(), "language", "en").unwrap(); + app.open_bytes(&egui::Context::default(), "text.pdf", None, TEXT_FIXTURE.to_vec()).expect("opens"); + app.set_option(&egui::Context::default(), "left", "closed").unwrap(); + app.set_option(&egui::Context::default(), "author", "Tester").unwrap(); app.comment_prefs.set_color(pdfcraft_ui_egui::comments::CommentTool::Ink, PEN_COLOUR); app.comment_prefs.set_width(pdfcraft_ui_egui::comments::CommentTool::Ink, PEN_WIDTH); app.set_option(&egui::Context::default(), "quick", "ink").unwrap(); - }) + app + }); + h.run_steps(4); + support::settle_held(&mut h, &jobs); + (h, jobs) } /// How many path shapes of the last frame are painted in the pen's colour and width. @@ -699,20 +710,20 @@ fn pen_paths(h: &Harness<'static, PdfCraftApp>) -> usize { /// raster that draws it arrives later. The stroke stays painted from its points meanwhile. #[test] fn a_released_pen_stroke_stays_drawn_until_the_page_raster_has_it() { - let mut h = pen_harness(); + let (mut h, jobs) = pen_harness(); assert_eq!(pen_paths(&h), 0, "nothing in the pen's colour before the stroke"); stroke_without_settling(&mut h, (40.0, 100.0), (140.0, 60.0)); assert_eq!(comments(&h).len(), 1, "the stroke is committed"); assert!(pen_paths(&h) > 0, "still drawn from its points"); h.run_steps(5); assert!(pen_paths(&h) > 0, "drawn from its points for as long as its raster takes"); - h.state_mut().views[0].hold_rasters = false; + support::settle_held(&mut h, &jobs); assert!(support::step_until(&mut h, |h| pen_paths(h) == 0), "the raster draws it now and the painted stroke goes"); } #[test] fn undoing_a_pen_stroke_before_its_raster_leaves_no_ghost() { - let mut h = pen_harness(); + let (mut h, _jobs) = pen_harness(); stroke_without_settling(&mut h, (40.0, 100.0), (140.0, 60.0)); assert!(pen_paths(&h) > 0, "the released stroke is painted"); undo(&mut h); diff --git a/crates/ui-egui/tests/it/content.rs b/crates/ui-egui/tests/it/content.rs index ea73c5868d..fedc37528c 100644 --- a/crates/ui-egui/tests/it/content.rs +++ b/crates/ui-egui/tests/it/content.rs @@ -34,6 +34,21 @@ fn harness_bytes_with_step_dt(bytes: Vec, step_dt: f32) -> Harness<'static, h } +/// [`harness`] whose jobs, the page renders among them, wait until the test runs them; the pages are rendered once +/// before it returns. +fn held_harness() -> (Harness<'static, PdfCraftApp>, std::sync::Arc) { + let (app, jobs) = crate::control::app_with_manual_jobs(); + let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).with_step_dt(0.25).build_eframe(move |_cc| { + let mut app = app; + app.set_option(&egui::Context::default(), "language", "en").unwrap(); + app.open_bytes(&egui::Context::default(), "form.pdf", None, include_bytes!("../data/form.pdf").to_vec()).unwrap(); + app.set_option(&egui::Context::default(), "zoom", "150").unwrap(); + app + }); + crate::support::settle_held(&mut h, &jobs); + (h, jobs) +} + fn at(h: &Harness<'static, PdfCraftApp>, x: f32, y: f32) -> Pos2 { let r = h.state().views[0].page_screen_rect(0).expect("on screen"); let k = r.width() / 300.0; @@ -890,16 +905,17 @@ fn red(pixels: &image::RgbaImage, r: egui::Rect) -> usize { #[test] fn a_resized_added_picture_shows_only_at_its_new_size() { let _gpu = gpu(); - let mut h = harness(); + let (mut h, jobs) = held_harness(); assert!(h.execute("edit.edit_text")); h.run_steps(2); let img = craft_image_codecs::Image::from_u8(60, 60, craft_image_codecs::ChannelLayout::Rgb, [200u8, 30, 30].repeat(3600)).unwrap(); let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap(); let ctx_h = h.ctx.clone(); h.state_mut().add_image(&ctx_h, "red.png".into(), png); + jobs.run_all(); h.run_steps(3); assert_eq!(h.state().views[0].session.content.selected, Some((0, 0)), "the picture is selected"); - crate::support::settle(&mut h); + crate::support::settle_held(&mut h, &jobs); let rect = added(&h)[0].content.rect(); let r = item_on_screen(&h, rect); // The lower right corner to the middle: half the size, the upper left corner kept. @@ -914,12 +930,12 @@ fn a_resized_added_picture_shows_only_at_its_new_size() { h.hover_at(grip + (to - grip) * (k as f32 / 4.0)); h.run_steps(1); } - crate::support::settle(&mut h); + crate::support::settle_held(&mut h, &jobs); let pixels = h.render().unwrap(); assert!(red(&pixels, kept) > 100, "while dragged, the picture is drawn at its new size"); assert_eq!(red(&pixels, gone), 0, "while dragged, the picture shows at its old size"); assert_eq!(added(&h)[0].content.rect(), rect, "nothing is applied before release"); - h.state_mut().views[0].hold_rasters = true; + // From here the renders wait: the frames below see the page as it was before the release. h.drop_at(to); for frame in 0..4 { h.step(); @@ -928,8 +944,7 @@ fn a_resized_added_picture_shows_only_at_its_new_size() { assert_eq!(red(&pixels, gone), 0, "frame {frame} after the release: the picture shows at its old size"); } assert_ne!(added(&h)[0].content.rect(), rect, "the resize is applied"); - h.state_mut().views[0].hold_rasters = false; - crate::support::settle(&mut h); + crate::support::settle_held(&mut h, &jobs); h.run_steps(2); let pixels = h.render().unwrap(); assert!(red(&pixels, kept) > 100 && red(&pixels, gone) == 0, "the page raster has the picture at its new size"); diff --git a/crates/ui-egui/tests/it/control.rs b/crates/ui-egui/tests/it/control.rs index 553e3ccfd6..b9c4d7c102 100644 --- a/crates/ui-egui/tests/it/control.rs +++ b/crates/ui-egui/tests/it/control.rs @@ -603,9 +603,9 @@ fn moving_before_middle_button_release_keeps_scrolling_without_starting_page_too let released = h.state().views[0].page_screen_rect(0).unwrap().top(); h.run_steps(8); assert_eq!(h.state().views[0].page_screen_rect(0).unwrap().top(), released, "no drift after releasing a held middle drag"); - assert!(h.state().views[0].crop_drag.is_none(), "the Crop tool must not receive a wheel drag"); - assert!(!crate::support::dialog_open(&h)); - assert!(!h.state().session.get(h.state().views[0].id).unwrap().dirty); + // A drag the Crop tool received ends in a crop edit or opens Set Page Boxes. + assert!(!crate::support::dialog_open(&h), "the Crop tool must not receive a wheel drag"); + assert!(!h.state().session.get(h.state().views[0].id).unwrap().dirty, "the Crop tool must not receive a wheel drag"); // Small movement within the dead zone before release still latches auto-scroll on. ok(&mut h, &c, "ui.drag", json!({ "from": [p.x, p.y], "to": [p.x, p.y + 10.0], "steps": 4, "button": "middle" })); @@ -613,9 +613,11 @@ fn moving_before_middle_button_release_keeps_scrolling_without_starting_page_too ok(&mut h, &c, "ui.move", json!({ "x": p.x, "y": p.y + 60.0 })); h.run_steps(8); assert!(h.state().views[0].page_screen_rect(0).unwrap().top() < released - 30.0, "scrolling continues with no button held"); - assert!(h.state().views[0].crop_drag.is_none(), "the Crop tool must not receive a wheel drag"); ok(&mut h, &c, "ui.click", json!({ "x": p.x, "y": p.y + 60.0, "button": "middle" })); assert!(!h.state().views[0].auto_scrolling(), "the next middle click toggles scrolling off"); + h.run_steps(2); + assert!(!crate::support::dialog_open(&h), "the Crop tool must not receive the latched gesture"); + assert!(!h.state().session.get(h.state().views[0].id).unwrap().dirty, "the Crop tool must not receive the latched gesture"); } #[cfg(target_os = "linux")] @@ -862,7 +864,8 @@ fn japanese_controls_and_search_keep_command_ids() { let hits = ok(&mut h, &c, "ui.inspect", json!({ "query": translated })); assert!(hits["count"].as_u64().unwrap() > 0, "{query}: {hits}"); ok(&mut h, &c, "ui.key", json!({ "key": "Escape" })); - h.state_mut().palette_query.clear(); + // Escape cleared the query; on the empty query it closes the palette. + ok(&mut h, &c, "ui.key", json!({ "key": "Escape" })); } assert_eq!(ok(&mut h, &c, "ui.state", json!({}))["documents"][0]["name"], "doc.pdf"); ok(&mut h, &c, "ui.set", json!({ "key": "language", "value": "en" })); @@ -903,7 +906,8 @@ fn german_preferences_and_search_keep_command_ids() { let hits = ok(&mut h, &c, "ui.inspect", json!({ "query": "Dokument teilen…" })); assert!(hits["count"].as_u64().unwrap() > 0, "{query}: {hits}"); ok(&mut h, &c, "ui.key", json!({ "key": "Escape" })); - h.state_mut().palette_query.clear(); + // Escape cleared the query; on the empty query it closes the palette. + ok(&mut h, &c, "ui.key", json!({ "key": "Escape" })); } assert_eq!(ok(&mut h, &c, "ui.state", json!({}))["documents"][0]["name"], "doc.pdf"); } @@ -931,7 +935,8 @@ fn ukrainian_preferences_and_search_keep_command_ids() { let hits = ok(&mut h, &c, "ui.inspect", json!({ "query": "Розділити документ…" })); assert!(hits["count"].as_u64().unwrap() > 0, "{query}: {hits}"); ok(&mut h, &c, "ui.key", json!({ "key": "Escape" })); - h.state_mut().palette_query.clear(); + // Escape cleared the query; on the empty query it closes the palette. + ok(&mut h, &c, "ui.key", json!({ "key": "Escape" })); } for (dialog, label) in [("properties", "Властивості документа"), ("protect", "Захистити паролем"), ("about", "Учасники")] { @@ -1192,10 +1197,10 @@ fn organize_overview_is_reachable_through_the_control_channel() { #[test] fn saved_signature_can_be_changed_through_the_control_channel() { let (mut h, c) = harness(); - h.state_mut().signature = Some(pdfcraft_ui_egui::fill_sign::SavedSig::Typed("Ada Lovelace".into())); + h.state_mut().signature = Some(craft::review::SavedSignature::Typed("Ada Lovelace".into())); ok(&mut h, &c, "ui.command", json!({ "id": "sign.fill.signature.change" })); h.run_steps(3); - assert_eq!(h.state().signature_draft.text, "Ada Lovelace"); + // The pad opens on the saved text: its text field shows it. let fields = ok(&mut h, &c, "ui.inspect", json!({ "role": "TextInput" })); let field = fields["widgets"].as_array().unwrap().iter().find(|w| w["value"] == "Ada Lovelace").expect("the signature text field"); let r = field["rect"].as_array().unwrap(); @@ -1204,11 +1209,11 @@ fn saved_signature_can_be_changed_through_the_control_channel() { ok(&mut h, &c, "ui.key", json!({ "key": "A", "modifiers": ["command"] })); ok(&mut h, &c, "ui.type", json!({ "text": "Grace Hopper" })); ok(&mut h, &c, "ui.click", json!({ "label": "Apply" })); - assert_eq!(h.state().signature, Some(pdfcraft_ui_egui::fill_sign::SavedSig::Typed("Grace Hopper".into()))); + assert_eq!(h.state().signature, Some(craft::review::SavedSignature::Typed("Grace Hopper".into()))); assert_eq!(ok(&mut h, &c, "ui.state", json!({}))["quick_tool"], "fill-signature"); ok(&mut h, &c, "ui.command", json!({ "id": "sign.fill.signature.remove" })); assert_eq!(h.state().signature, None); - h.state_mut().initials = Some(pdfcraft_ui_egui::fill_sign::SavedSig::Typed("GH".into())); + h.state_mut().initials = Some(craft::review::SavedSignature::Typed("GH".into())); ok(&mut h, &c, "ui.command", json!({ "id": "sign.fill.initials.remove" })); assert_eq!(h.state().initials, None); } @@ -1760,10 +1765,10 @@ fn initial_zoom_option_is_applied_before_the_first_viewport_layout() { let mut app = new_app(); app.open_bytes(&egui::Context::default(), "initial-zoom.pdf", None, fixture(3)).unwrap(); app.set_option(&egui::Context::default(), "zoom", &percent.to_string()).unwrap(); - assert_eq!(app.views[0].zoom(), percent as f32 / 100.0, "initial options must work before the viewport exists"); + assert_eq!(app.views[0].zoom(), f64::from(percent) / 100.0, "initial options must work before the viewport exists"); app }); - assert_eq!(h.state().views[0].zoom(), percent as f32 / 100.0); + assert_eq!(h.state().views[0].zoom(), f64::from(percent) / 100.0); } } diff --git a/crates/ui-egui/tests/it/editing.rs b/crates/ui-egui/tests/it/editing.rs index 28ecf119c7..04f4d54ced 100644 --- a/crates/ui-egui/tests/it/editing.rs +++ b/crates/ui-egui/tests/it/editing.rs @@ -3012,8 +3012,11 @@ fn extract_options_and_rotate_pages_dialog() { // Rotate Pages: odd page numbers only. h.state_mut().run_command(&egui::Context::default(), "page.rotate_dialog"); h.run_steps(2); - h.state_mut().rotate_draft.which = 0; - h.state_mut().rotate_draft.parity = pdfcraft_engine::PageParity::Odd; + let odd = craft::pages::PageRange { scope: craft::pages::PageScope::All, subset: craft::pages::Subset::Odd, ..Default::default() }; + let ctx = h.ctx.clone(); + let rotate = craft::ui::dialog::dialog_id("page.rotate_dialog"); + h.state_mut().dialogs.set_json_field(&ctx, rotate, "pages", serde_json::to_value(odd).unwrap()).unwrap(); + h.run_steps(1); h.get_by_label("OK").click(); h.run_steps(3); let s = h.state(); @@ -3031,8 +3034,11 @@ fn rotate_pages_keeps_a_typed_last_page() { let mut h = organize(20); h.state_mut().run_command(&egui::Context::default(), "page.rotate_dialog"); h.run_steps(2); - let d = &mut h.state_mut().rotate_draft; - (d.which, d.from, d.to) = (2, 5, 5); + // Pages 5 to 5, as the range's From and To show them. + let range = craft::pages::PageRange { scope: craft::pages::PageScope::Range, from: 5, to: Some(5), ..Default::default() }; + let ctx = h.ctx.clone(); + let rotate = craft::ui::dialog::dialog_id("page.rotate_dialog"); + h.state_mut().dialogs.set_json_field(&ctx, rotate, "pages", serde_json::to_value(range).unwrap()).unwrap(); h.run_steps(1); h.get_by_role_and_label(Role::SpinButton, "to").click(); h.run_steps(2); @@ -3569,13 +3575,16 @@ fn ink(pixels: &image::RgbaImage, r: egui::Rect) -> usize { fn an_applied_paragraph_keeps_its_edited_look_until_the_page_is_redrawn() { let _gpu = gpu(); let bytes = source_font_fixture_with_text("BT /F1 24 Tf 20 220 Td (Topmost) Tj ET BT /F2 24 Tf 20 120 Td (Bottom) Tj ET"); + // The jobs, the page renders among them, wait until the test runs them. + let (app, jobs) = crate::control::app_with_manual_jobs(); let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |_cc| { - let mut app = crate::control::new_app(); + let mut app = app; app.set_option(&egui::Context::default(), "language", "en").unwrap(); app.open_bytes(&egui::Context::default(), "boxes.pdf", None, bytes).expect("opens"); app }); h.run_steps(4); + support::settle_held(&mut h, &jobs); assert!(h.execute("edit.edit_text")); h.run_steps(2); let r = h.state().views[0].page_screen_rect(0).expect("on screen"); @@ -3593,10 +3602,11 @@ fn an_applied_paragraph_keeps_its_edited_look_until_the_page_is_redrawn() { let head = egui::Rect::from_min_max(top.min, egui::pos2(top.left() + top.width() * 0.12, top.bottom())); let tail = egui::Rect::from_min_max(egui::pos2(top.left() + top.width() * 0.3, top.top()), top.max).shrink(2.0); click_at(&mut h, top.center()); - support::wait_until(&mut h, "the editor's textless page", |app| !app.render_pending()); + // The editor's textless page. + support::settle_held(&mut h, &jobs); h.state_mut().views[0].session.line_editor.as_mut().expect("the top box is open").text = "T".into(); h.run_steps(2); - h.state_mut().views[0].hold_rasters = true; + // From here the renders wait: the frames below see the page as it was before applying. h.key_press_modifiers(Modifiers::COMMAND, Key::Enter); for frame in 0..4 { h.step(); @@ -3605,8 +3615,8 @@ fn an_applied_paragraph_keeps_its_edited_look_until_the_page_is_redrawn() { assert!(ink(&pixels, head) > 20, "frame {frame} after applying shows no text"); assert_eq!(ink(&pixels, tail), 0, "frame {frame} after applying shows the old text again"); } - h.state_mut().views[0].hold_rasters = false; - support::wait_until(&mut h, "the page drawn with the edit", |app| !app.render_pending()); + // The page drawn with the edit. + support::settle_held(&mut h, &jobs); h.run_steps(2); let pixels = h.render().unwrap(); assert!(ink(&pixels, head) > 20, "the page draws the applied text"); @@ -5367,7 +5377,7 @@ fn paragraph_drag_fixture(content: &str, rotation: i64, unit: i64) -> Vec { fn paragraph_drag_harness(bytes: Vec, view_rotation: u16) -> Harness<'static, PdfCraftApp> { let mut h = Harness::builder().with_size(egui::vec2(1400.0, 1100.0)).build_eframe(move |_cc| { - let mut app = crate::control::new_app(); + let mut app = scripted_app(); app.session = pdfcraft_engine::Session::new().with_clock(|| 1_700_000_000); app.set_option(&egui::Context::default(), "language", "en").unwrap(); app.open_bytes(&egui::Context::default(), "paragraphs.pdf", None, bytes).unwrap(); @@ -5450,14 +5460,13 @@ fn paragraph_drag_content(h: &Harness<'static, PdfCraftApp>) -> String { fn save_paragraph_drag(h: &mut Harness<'static, PdfCraftApp>, name: &str, original: &[u8]) -> Vec { let out = temp_path(name); let id = h.state().views[0].id; - h.state_mut().save_override = Some(out.to_string_lossy().into_owned()); + queue_save(&out); h.key_press_modifiers(Modifiers::COMMAND, Key::S); h.run_steps(4); - let saved = std::fs::read(&out).unwrap(); + let saved = saved_file(&out); assert!(saved.starts_with(original), "Save keeps the opened/saved revisions"); assert!(!h.state().session.get(id).unwrap().dirty); assert_eq!(h.state().session.get(id).unwrap().bytes.as_slice(), saved.as_slice()); - std::fs::remove_file(out).unwrap(); saved } @@ -6014,18 +6023,42 @@ fn resized_pages_panel_thumbnails_reorder_multiple_pages_and_undo() { #[test] fn a_thumbnail_drag_that_ends_off_the_panel_or_outlives_the_pages_is_dropped() { let mut h = harness(4, |app| app.set_option(&egui::Context::default(), "panel", "pages").unwrap()); - let before = page_texts(h.state()); - // A drag left over (the panel closed mid-drag, the button released elsewhere) moves nothing. - h.state_mut().views[0].panel_drag = Some(vec![2]); h.run_steps(2); - assert!(h.state().views[0].panel_drag.is_none()); - assert_eq!(page_texts(h.state()), before); - // A change to the document drops page indexes taken before it. - h.state_mut().views[0].panel_drag = Some(vec![3]); + let before = page_texts(h.state()); + // Presses a thumbnail and drags it halfway towards the first one, leaving the button down. + let start_drag = |h: &mut Harness<'static, PdfCraftApp>, label: &str| -> egui::Pos2 { + let (from, to) = (panel_thumb(h, label), panel_thumb(h, "Page 1")); + h.hover_at(from); + h.run_steps(1); + h.drag_at(from); + h.run_steps(1); + for k in 1..=3 { + h.hover_at(from + (to - from) * (k as f32 / 6.0)); + h.run_steps(1); + } + to + }; + // The panel closed mid-drag: the release, over where the first thumbnail was, moves nothing. + let to = start_drag(&mut h, "Page 3"); + h.set_option("panel", "none").unwrap(); + h.run_steps(2); + h.hover_at(to); + h.run_steps(1); + h.drop_at(to); + h.run_steps(2); + assert_eq!(page_texts(h.state()), before, "a drag that outlived its panel moves nothing"); + // A change to the document drops page indexes taken before it: the release moves nothing. + h.set_option("panel", "pages").unwrap(); + h.run_steps(3); + let to = start_drag(&mut h, "Page 4"); let id = h.state().views[0].id; let info = h.state().session.get(id).unwrap().info.clone(); h.state_mut().views[0].document_changed(&egui::Context::default(), &info); - assert!(h.state().views[0].panel_drag.is_none()); + h.hover_at(to); + h.run_steps(1); + h.drop_at(to); + h.run_steps(2); + assert_eq!(page_texts(h.state()), before, "a drag that outlived the pages it picked moves nothing"); } /// A Pages panel thumbnail (the rightmost node with that label). @@ -6100,11 +6133,10 @@ fn host_dirty_hears_when_unsaved_work_appears_and_goes() { assert_eq!(reports.lock().unwrap().last(), Some(&true), "an edit is unsaved work"); let out = temp_path("host-dirty.pdf"); - h.state_mut().save_override = Some(out.to_string_lossy().into_owned()); + queue_save(&out); h.key_press_modifiers(Modifiers::COMMAND, Key::S); h.run_steps(3); assert_eq!(reports.lock().unwrap().last(), Some(&false), "saving clears it"); - let _ = std::fs::remove_file(out); } #[test] @@ -6864,8 +6896,10 @@ fn a_resized_image_keeps_its_new_size_until_the_page_is_redrawn() { }) .unwrap(); let bytes = write_incremental(&doc, &Default::default()).unwrap(); + // The jobs, the page renders among them, wait until the test runs them. + let (app, jobs) = crate::control::app_with_manual_jobs(); let mut h = Harness::builder().with_size(egui::vec2(1400.0, 1100.0)).build_eframe(move |_cc| { - let mut app = crate::control::new_app(); + let mut app = app; app.set_option(&egui::Context::default(), "language", "en").unwrap(); app.open_bytes(&egui::Context::default(), "picture.pdf", None, bytes).unwrap(); app @@ -6874,12 +6908,13 @@ fn a_resized_image_keeps_its_new_size_until_the_page_is_redrawn() { assert!(h.execute("edit.edit_text")); h.state_mut().views[0].view.zoom = 1.2; h.run_steps(4); - support::wait_until(&mut h, "the page raster", |app| !app.render_pending()); + // The page raster. + support::settle_held(&mut h, &jobs); let page = h.state().views[0].page_screen_rect(0).unwrap(); let at = |x: f32, y: f32| egui::pos2(page.left() + x / 200.0 * page.width(), page.top() + (300.0 - y) / 300.0 * page.height()); click_at(&mut h, at(100.0, 80.0)); assert!(h.state().views[0].session.image_selection.is_some(), "the picture is selected"); - support::wait_until(&mut h, "the page raster", |app| !app.render_pending()); + support::settle_held(&mut h, &jobs); // From 60..140 × 40..120 to half its size, keeping its lower left corner. let (grip, to) = (at(140.0, 120.0), at(100.0, 80.0)); let kept = egui::Rect::from_two_pos(at(62.0, 42.0), at(98.0, 78.0)); @@ -6898,7 +6933,7 @@ fn a_resized_image_keeps_its_new_size_until_the_page_is_redrawn() { matches!(&shape.shape, egui::Shape::Rect(r) if r.stroke.width == 0.75 && (r.rect.min - old.min).length() < 2.0 && (r.rect.max - old.max).length() < 2.0) }); assert!(!trailing, "the dragged picture's outline trails behind it"); - h.state_mut().views[0].hold_rasters = true; + // From here the renders wait: the frames below see the page as it was before the release. h.drop_at(to); for frame in 0..4 { h.step(); @@ -6925,8 +6960,8 @@ fn a_resized_image_keeps_its_new_size_until_the_page_is_redrawn() { h.run_steps(2); let pixels = h.render().unwrap(); assert_eq!(red(&pixels, gone), 0, "after the second release"); - h.state_mut().views[0].hold_rasters = false; - support::wait_until(&mut h, "the page drawn with the resized picture", |app| !app.render_pending()); + // The page drawn with the resized picture. + support::settle_held(&mut h, &jobs); h.run_steps(2); let pixels = h.render().unwrap(); assert!(red(&pixels, again) > 20 && red(&pixels, gone) == 0, "the page raster has the picture at its new size"); @@ -7675,7 +7710,7 @@ fn delete_in_the_page_grid_deletes_the_page_and_not_the_hidden_picture() { /// can't see, and Enter applies no crop. #[test] fn a_selected_pictures_keys_wait_for_its_page_to_be_on_screen() { - use pdfcraft_ui_egui::canvas::PageLayout; + use pdfcraft_ui_egui::canvas::PageDisplay as PageLayout; for (crop, key) in [(false, egui::Key::Delete), (true, egui::Key::Enter)] { let mut h = picture_pages(crop); let id = h.state().views[0].id; @@ -8396,7 +8431,7 @@ fn stale_clipboard_plain_text_does_not_paste_image() { h.run_steps(2); // can_paste_image should now be false because the clipboard has text, not an image! - assert!(!h.state().can_paste_image(), "cannot paste image when the clipboard has text"); + assert!(!h.state_mut().can_paste_image(), "cannot paste image when the clipboard has text"); // paste_image should fail and return false without pasting let ctx_h = h.ctx.clone(); diff --git a/crates/ui-egui/tests/it/javascript.rs b/crates/ui-egui/tests/it/javascript.rs index 0b47862fef..d13adced4f 100644 --- a/crates/ui-egui/tests/it/javascript.rs +++ b/crates/ui-egui/tests/it/javascript.rs @@ -251,7 +251,7 @@ fn merge_data_files_into_a_spreadsheet() { let files = craft::host::ScriptedFiles::answering([craft::host::Picked::Paths(vec![out.clone()])]); let host = crate::support::host_with_files(files); let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |_cc| { - let mut app = PdfCraftApp::with_host(host); + let mut app = crate::support::test_app(host); app.set_option(&egui::Context::default(), "language", "en").unwrap(); app }); @@ -616,11 +616,12 @@ fn clicking_duplicate_image_buttons_imports_only_into_the_clicked_field() { let img = craft_image_codecs::Image::from_u8(2, 2, craft_image_codecs::ChannelLayout::Rgb, vec![120u8; 12]).unwrap(); let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap(); std::fs::write(&path, png).unwrap(); + // The image picker answers with the PNG. + let files = std::sync::Arc::new(craft::host::ScriptedFiles::answering([craft::host::Picked::Paths(vec![path.clone()])])); let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(|_cc| { - let mut app = crate::control::new_app(); + let mut app = crate::control::app_with_files(files); app.set_option(&egui::Context::default(), "language", "en").unwrap(); app.open_bytes(&egui::Context::default(), "images.pdf", None, bytes).unwrap(); - app.save_override = Some(path.to_string_lossy().into_owned()); app }); h.run_steps(6); diff --git a/crates/ui-egui/tests/it/preferences.rs b/crates/ui-egui/tests/it/preferences.rs index c62ee044a2..49dee2c551 100644 --- a/crates/ui-egui/tests/it/preferences.rs +++ b/crates/ui-egui/tests/it/preferences.rs @@ -5,7 +5,7 @@ use egui_kittest::kittest::Queryable; // PLAN: assumed public `pdfcraft_ui_egui::{Mode, units::Unit}` and `craft::theme::selection::MotionChoice` for tests use craft::theme::selection::MotionChoice; use pdfcraft_ui_egui::units::Unit; -use pdfcraft_ui_egui::{Mode, PdfCraftApp}; +use pdfcraft_ui_egui::{Mode, PdfCraftApp, side_panels}; const PDF: &[u8] = include_bytes!("../data/form.pdf"); @@ -36,6 +36,11 @@ fn harness() -> H { h } +/// Whether the Preferences window is on screen: its page list names the first page while it is open. +fn preferences_shown(h: &H) -> bool { + h.query_by_label("General & Interface").is_some() +} + fn click(h: &mut H, label: &str) { h.get_by_label(label).click(); h.run_steps(3); @@ -77,7 +82,10 @@ fn cases() -> Vec { h.get_all_by_label("Left").nth(1).unwrap().click(); h.run_steps(3); }, - applied: |a| a.side_panels.tools_on_right() && !a.side_panels.rail_on_right(), + applied: |a| { + let dock = side_panels::state(a); + dock["tools_side"] == "right" && dock["rail_side"] == "left" + }, }, Case { name: "animation effects", @@ -123,13 +131,13 @@ fn preference_rows_apply_on_ok_and_nothing_changes_on_cancel() { assert!(!(case.applied)(h.state()), "{}: a row edits the draft, not the app", case.name); click(&mut h, "OK"); h.run_steps(3); - assert!(h.state().preferences.is_none(), "{}: OK closes the window", case.name); + assert!(!preferences_shown(&h), "{}: OK closes the window", case.name); assert!((case.applied)(h.state()), "{}: OK applies the draft", case.name); let mut h = edited(&case); click(&mut h, "Cancel"); h.run_steps(3); - assert!(h.state().preferences.is_none(), "{}: Cancel closes the window", case.name); + assert!(!preferences_shown(&h), "{}: Cancel closes the window", case.name); assert!(!(case.applied)(h.state()), "{}: Cancel leaves the app as it was", case.name); } } diff --git a/crates/ui-egui/tests/it/prepare.rs b/crates/ui-egui/tests/it/prepare.rs index f7b3e1a06a..de3d15ffc3 100644 --- a/crates/ui-egui/tests/it/prepare.rs +++ b/crates/ui-egui/tests/it/prepare.rs @@ -18,9 +18,18 @@ fn harness() -> Harness<'static, PdfCraftApp> { } fn harness_with(name: &'static str, bytes: Vec) -> Harness<'static, PdfCraftApp> { + harness_on(crate::control::new_app(), name, bytes) +} + +/// The form fixture in an app whose pickers answer from `files`. +fn harness_with_files(files: std::sync::Arc) -> Harness<'static, PdfCraftApp> { + harness_on(crate::control::app_with_files(files), "form.pdf", include_bytes!("../data/form.pdf").to_vec()) +} + +fn harness_on(app: PdfCraftApp, name: &'static str, bytes: Vec) -> Harness<'static, PdfCraftApp> { let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| { cc.egui_ctx.options_mut(|options| options.warn_on_id_clash = true); - let mut app = crate::control::new_app(); + let mut app = app; app.set_option(&egui::Context::default(), "language", "en").unwrap(); app.open_bytes(&egui::Context::default(), name, None, bytes).unwrap(); app.set_option(&egui::Context::default(), "zoom", "150").unwrap(); @@ -128,7 +137,8 @@ fn notices(h: &Harness<'static, PdfCraftApp>) -> Vec { /// The row of the calculation order page for the field `name`, whatever its position and value. fn order_row<'h>(h: &'h Harness<'static, PdfCraftApp>, name: &str) -> egui_kittest::Node<'h> { - h.get_by_label_contains(&format!(". {name}:")) + let pattern = format!(". {name}:"); + h.get_by(move |node| node.label().is_some_and(|label| label.contains(&pattern))) } fn text_center_near(h: &Harness<'static, PdfCraftApp>, label: &str, expected: Pos2) -> Option { @@ -1078,16 +1088,16 @@ fn preview_fills_the_form_and_locked_fields_keep_their_properties() { #[test] fn image_fields_take_a_picture_when_clicked() { - let mut h = harness(); + // A picture to choose: the picker answers with it. + let path = std::env::temp_dir().join(format!("pdfcraft-image-field-{}.png", std::process::id())); + image::RgbImage::from_pixel(8, 4, image::Rgb([200, 30, 30])).save(&path).unwrap(); + let files = std::sync::Arc::new(craft::host::ScriptedFiles::answering([craft::host::Picked::Paths(vec![path])])); + let mut h = harness_with_files(files); assert!(h.execute("form.add.image")); h.run_steps(2); let (a, b) = (at(&h, 40.0, 220.0), at(&h, 140.0, 290.0)); drag(&mut h, a, b); assert_eq!(names(&h).last().map(String::as_str), Some("Image1")); - // A picture to choose (the picker is bypassed in tests). - let path = std::env::temp_dir().join(format!("pdfcraft-image-field-{}.png", std::process::id())); - image::RgbImage::from_pixel(8, 4, image::Rgb([200, 30, 30])).save(&path).unwrap(); - h.state_mut().save_override = Some(path.to_string_lossy().into_owned()); // Fill it in as a reader would. h.get_by_label("Preview").click(); h.run_steps(2); diff --git a/crates/ui-egui/tests/it/print.rs b/crates/ui-egui/tests/it/print.rs index 79d0eb62db..09c0ee00f1 100644 --- a/crates/ui-egui/tests/it/print.rs +++ b/crates/ui-egui/tests/it/print.rs @@ -864,13 +864,14 @@ fn a_paper_picked_from_the_menu_while_in_label_is_kept() { /// Properties… reads the printer's driver options off the GUI thread, as a job of the host: the /// dialog keeps drawing while the read is pending, and a read that dies leaves the panel with -/// nothing rather than waiting (#1818). The request is made directly, so the test runs where -/// Properties… opens the driver's own window instead (Windows). -#[cfg(not(target_arch = "wasm32"))] +/// nothing rather than waiting (#1818). On Windows Properties… opens the driver's own window +/// (`HAS_PRINTER_PREFERENCES`), so there is no read to test. +#[cfg(not(any(target_arch = "wasm32", windows)))] #[test] fn printer_options_are_read_off_the_gui_thread_and_a_dead_read_offers_nothing() { use pdfcraft_engine::print::spool::PrinterOption; let mut h = harness(); + list_printers(&mut h, &["Office", "Dead"]); h.state_mut().options_source = Some(Arc::new(move |printer: &str| { assert_ne!(printer, "Dead", "the read died"); vec![PrinterOption { @@ -883,12 +884,12 @@ fn printer_options_are_read_off_the_gui_thread_and_a_dead_read_offers_nothing() })); h.open(); h.settle(); + // Properties… for the chosen printer, with the panel closed, as a user opens it. let ask = |h: &mut Rig, printer: &str| { - let d = &mut h.state_mut().print_draft; - d.driver_options = None; - d.driver_options_loading = Some(printer.into()); - d.show_driver_options = true; - h.state_mut().read_options_in_background(printer.into()); + h.pick(Some(printer)); + h.settle(); + h.get_by_label("Properties…").click(); + h.run_steps(1); }; ask(&mut h, "Office"); h.run_steps(3); @@ -902,6 +903,7 @@ fn printer_options_are_read_off_the_gui_thread_and_a_dead_read_offers_nothing() assert_eq!(options[0].key, "InputSlot"); h.get_by_label("Paper tray"); + h.action("driver.done"); ask(&mut h, "Dead"); h.settle(); assert_eq!(h.state().print_draft.driver_options, Some(("Dead".into(), Vec::new())), "a dead read offers nothing, and the panel says so"); diff --git a/crates/ui-egui/tests/it/reading_colors.rs b/crates/ui-egui/tests/it/reading_colors.rs index 15a14cdba7..7c385f787c 100644 --- a/crates/ui-egui/tests/it/reading_colors.rs +++ b/crates/ui-egui/tests/it/reading_colors.rs @@ -41,7 +41,7 @@ fn pending_pages_use_document_reading_background() { let mut output = ctx.run_ui(RawInput { screen_rect: Some(Rect::from_min_size(pos2(0.0, 0.0), vec2(900.0, 600.0))), ..Default::default() }, |ui| { let rect = ui.max_rect(); - textures = pdfcraft_ui_egui::canvas::document_area_at(&mut app, 0, rect, ui).textures.entries; + textures = pdfcraft_ui_egui::canvas::document_area_at(&mut app, 0, rect, ui).expect("the page view runs").textures.entries; }); output.textures_delta.clear(); let view = &app.views[0]; diff --git a/crates/ui-egui/tests/it/recovery.rs b/crates/ui-egui/tests/it/recovery.rs index 3f22a7b976..74237a5a26 100644 --- a/crates/ui-egui/tests/it/recovery.rs +++ b/crates/ui-egui/tests/it/recovery.rs @@ -71,14 +71,13 @@ fn new_app(dir: &Path) -> (PdfCraftApp, Rig) { let info = AppInfo { id: "pdfcraft", name: "PdfCraft", version: env!("CARGO_PKG_VERSION") }; let host = Host::builder(info).jobs(jobs.clone()).files(files.clone()).build(); let wake = Wake::none(); - // PLAN: assumed `pdfcraft_ui_egui::{Clipboard, FileIntake}` and `Clipboard::new(SystemClipboard::new())` as in `AppServices::system` (pdf-hub-w1) - let clipboard = pdfcraft_ui_egui::Clipboard::new(craft::platform::clipboard::SystemClipboard::new()); - let (intake, _) = pdfcraft_ui_egui::FileIntake::new(wake.clone()); + let clipboard = craft::platform::clipboard::Clipboard::new(craft::platform::clipboard::SystemClipboard::new()); + let (intake, _) = craft::platform::open::FileIntake::new(wake.clone()); let watch = craft::platform::watch::FileWatch::new(wake.clone()); + // A recovery folder turns autosave and recovery on. let services = AppServices::new(wake, clipboard, Some(dir.to_path_buf()), None, intake, watch); let mut app = PdfCraftApp::with_host(Arc::new(host), services); app.set_option(&egui::Context::default(), "language", "en").unwrap(); - app.enable_recovery(); (app, Rig { files, jobs }) } diff --git a/crates/ui-egui/tests/it/reload.rs b/crates/ui-egui/tests/it/reload.rs index 4834d6921a..4674fd6984 100644 --- a/crates/ui-egui/tests/it/reload.rs +++ b/crates/ui-egui/tests/it/reload.rs @@ -14,7 +14,12 @@ use pdfcraft_ui_egui::{PdfCraftApp, SaveTarget}; /// An `n`-page document with a proper xref table; page `i` shows "Page i+1", and with `notes` /// each page has a sticky note. fn fixture_with(n: usize, notes: bool) -> Vec { - let mut objs: Vec = vec!["<< /Type /Catalog /Pages 2 0 R >>".into()]; + fixture_catalog(n, notes, "") +} + +/// [`fixture_with`] whose catalog also holds `catalog` (entries such as `/PageMode /FullScreen`). +fn fixture_catalog(n: usize, notes: bool, catalog: &str) -> Vec { + let mut objs: Vec = vec![format!("<< /Type /Catalog /Pages 2 0 R {catalog}>>")]; let kids: Vec = (0..n).map(|i| format!("{} 0 R", 4 + 2 * i)).collect(); objs.push(format!("<< /Type /Pages /Kids [{}] /Count {n} /MediaBox [0 0 200 300] >>", kids.join(" "))); objs.push("<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>".into()); @@ -325,7 +330,7 @@ fn a_shorter_new_version_drops_what_pointed_into_the_old_one() { assert_eq!(pages(&h), 1); assert_eq!(view.current(), 0, "the page is clamped"); // PLAN: assumed `comments.primary()` reads the selected comment (pdf-comments) - assert!(view.selected.is_empty() && view.session.comments.primary().is_none(), "no selection points past the new version"); + assert!(view.selected.items.is_empty() && view.session.comments.primary().is_none(), "no selection points past the new version"); } /// Save asks before writing over a version of the file the document did not come from (#2166): @@ -346,7 +351,7 @@ fn saving_over_a_file_changed_by_something_else_asks_first() { h.get_by_label("Cancel").click(); h.run_steps(2); assert_eq!(std::fs::read(&file.path).unwrap(), fixture(4), "Cancel keeps the newer file"); - assert!(dirty(&h) && h.state().pending_overwrite.is_none()); + assert!(dirty(&h) && h.query_by_label("Save over the changed file?").is_none()); let ctx_h = h.ctx.clone(); assert!(!h.state_mut().save_active(&ctx_h, SaveTarget::InPlace)); h.run_steps(2); @@ -455,12 +460,11 @@ fn a_newer_version_found_as_the_dialog_closes_replaces_the_held_one() { #[test] fn a_file_rebuilt_under_the_full_screen_prompt_reloads_once_it_is_answered() { // #3917 review: the full-screen prompt is about the document's current version. - let file = TempPdf::new("under-full-screen", &fixture(2)); + // A document that asks for full screen opens with the prompt, as from a user's open. + let file = TempPdf::new("under-full-screen", &fixture_catalog(2, false, "/PageMode /FullScreen ")); let mut h = harness(&file.path); - let id = h.state().views[0].id; - // PLAN: assumed `ask_full_screen(id)` takes the document id (pdf-dialogs-w4) - h.state_mut().ask_full_screen(id); h.run_steps(2); + h.get_by_label("Open in Full Screen mode?"); file.rebuild(&fixture(4)); h.run_steps(4); assert_eq!(pages(&h), 2, "not replaced under the prompt"); diff --git a/crates/ui-egui/tests/it/scan_clean.rs b/crates/ui-egui/tests/it/scan_clean.rs index 30eac0c400..4b97d3b1cf 100644 --- a/crates/ui-egui/tests/it/scan_clean.rs +++ b/crates/ui-egui/tests/it/scan_clean.rs @@ -1,7 +1,10 @@ //! Scan & OCR ▸ Enhance scanned file in the real shell (egui_kittest): the dialog, the run and //! its report, and that the page keeps its comment while its scan is cleaned in place. +use std::sync::Arc; + use crate::support::AppHarness as _; +use craft::host::ManualJobs; use craft::pages::ui::PageRangeOptions; use craft::pages::{PageRange, PageScope}; use craft::ui::dialog::dialog_id; @@ -29,27 +32,28 @@ fn scan() -> Vec { s.save_bytes(id).unwrap().to_vec() } -fn harness() -> Harness<'static, PdfCraftApp> { - let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(|_cc| { - let mut app = crate::control::new_app(); +/// The scan open in an app whose jobs wait until `run_jobs` runs them. +fn harness() -> (Harness<'static, PdfCraftApp>, Arc) { + let (app, jobs) = crate::control::app_with_manual_jobs(); + let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |_cc| { + let mut app = app; app.set_option(&egui::Context::default(), "language", "en").unwrap(); app.open_bytes(&egui::Context::default(), "scan.pdf", None, scan()).unwrap(); app }); h.run_steps(4); - h + (h, jobs) } /// Runs the host's queued jobs, then lets the app poll them. -// PLAN: assumed `PdfCraftApp::manual_jobs() -> &ManualJobs` (pdf-shell S-7). -fn run_jobs(h: &mut Harness<'static, PdfCraftApp>) { - h.state().manual_jobs().run_all(); +fn run_jobs(h: &mut Harness<'static, PdfCraftApp>, jobs: &ManualJobs) { + jobs.run_all(); h.run_steps(3); } #[test] fn enhance_scanned_file_cleans_the_chosen_pages_and_says_what_it_did() { - let mut h = harness(); + let (mut h, jobs) = harness(); let id = h.state().active_ids().unwrap().1; assert!(h.state().session.get(id).unwrap().comment_props(0, 0).is_some()); assert!(h.execute("ocr.enhance")); @@ -61,10 +65,10 @@ fn enhance_scanned_file_cleans_the_chosen_pages_and_says_what_it_did() { h.run_steps(1); assert!(h.state_mut().dialogs.confirm::(&ctx)); h.run_steps(2); - run_jobs(&mut h); + run_jobs(&mut h, &jobs); + assert!(crate::support::shown_notice(&h).is_some(), "a notice says what was done"); let app = h.state(); assert!(app.scan_clean_run.is_none() && !app.dialogs.is_open::(), "finished and closed"); - assert!(app.toast.is_some(), "a notice says what was done"); let doc = app.session.get(id).unwrap(); assert!(doc.can_undo().is_some(), "one undo step restores the page"); assert_eq!(doc.info.pages.len(), 2); @@ -73,14 +77,14 @@ fn enhance_scanned_file_cleans_the_chosen_pages_and_says_what_it_did() { #[test] fn nothing_to_enhance_is_said_not_done() { - let mut h = harness(); + let (mut h, jobs) = harness(); let id = h.state().active_ids().unwrap().1; h.state_mut().scan_clean_draft.settings = pdfcraft_engine::scan_clean::ScanCleanSettings { level: 0, edge_shadows: false, despeckle: false }; // With every filter off the dialog's OK is disabled; starting anyway changes nothing. let ctx_h = h.ctx.clone(); h.state_mut().start_scan_clean(&ctx_h); - run_jobs(&mut h); + run_jobs(&mut h, &jobs); + assert!(crate::support::shown_notice(&h).is_some(), "a notice says nothing was cleaned"); let app = h.state(); - assert!(app.toast.is_some(), "a notice says nothing was cleaned"); assert_eq!(app.session.get(id).unwrap().can_undo(), None); } diff --git a/crates/ui-egui/tests/it/support/mod.rs b/crates/ui-egui/tests/it/support/mod.rs index 6a44498719..04e914c5b6 100644 --- a/crates/ui-egui/tests/it/support/mod.rs +++ b/crates/ui-egui/tests/it/support/mod.rs @@ -50,6 +50,17 @@ pub fn settle(h: &mut Harness<'static, PdfCraftApp>) { assert!(settled, "pages rendered"); } +/// [`settle`] for an app whose jobs wait in `jobs`: runs them each frame until nothing is waiting to be rendered. +/// Between calls the page pictures are held back, as a slow render would hold them. +pub fn settle_held(h: &mut Harness<'static, PdfCraftApp>, jobs: &ManualJobs) { + let settled = step_until(h, |h| { + jobs.run_all(); + h.run_steps(1); + !h.state().render_pending() + }); + assert!(settled, "pages rendered"); +} + pub fn wait_until(h: &mut Harness<'static, PdfCraftApp>, what: &str, ready: impl Fn(&PdfCraftApp) -> bool) { let held = step_until(h, |h| ready(h.state())); assert!( diff --git a/crates/ui-egui/tests/it/view.rs b/crates/ui-egui/tests/it/view.rs index f9735a467a..add905a24f 100644 --- a/crates/ui-egui/tests/it/view.rs +++ b/crates/ui-egui/tests/it/view.rs @@ -2755,7 +2755,7 @@ fn a_page_whose_content_was_cut_is_named_in_a_notice() { let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |_cc| { let mut app = crate::control::new_app(); app.set_option(&egui::Context::default(), "language", "en").unwrap(); - app.attach_control(channel, None); + app.attach_control(craft::automation::ControlChannel::new(channel), None); app.open_bytes(&egui::Context::default(), "cut.pdf", None, pdf).expect("opens"); app }); diff --git a/crates/ui-egui/tests/it/viewer.rs b/crates/ui-egui/tests/it/viewer.rs index 68e8ccc52c..336daef9a9 100644 --- a/crates/ui-egui/tests/it/viewer.rs +++ b/crates/ui-egui/tests/it/viewer.rs @@ -1843,7 +1843,7 @@ fn right_scrolls_to_a_spreads_second_page_off_screen_before_turning() { app.open_bytes(&egui::Context::default(), "spread.pdf", None, sized_fixture(&[(600, 800); 4])).unwrap(); app.set_option(&egui::Context::default(), "layout", "two-page").unwrap(); // Before the first layout: zoomed about the content's top left, scrolled to it. - app.views[0].view.zoom = zoom; + app.views[0].view.zoom = f64::from(zoom); app }); crate::support::settle(&mut h); diff --git a/crates/ui-egui/tests/perf.rs b/crates/ui-egui/tests/perf.rs index 82d67fd2a5..b9cd42f9ba 100644 --- a/crates/ui-egui/tests/perf.rs +++ b/crates/ui-egui/tests/perf.rs @@ -61,13 +61,14 @@ fn named_comments(n: usize) -> Vec { } /// The app with `bytes` open and each of `options` set, stepped at 60 frames a second. -fn harness(bytes: Vec, options: &'static [(&'static str, &'static str)]) -> Harness<'static, PdfCraftApp> { +fn harness(bytes: Vec, options: &[(&str, &str)]) -> Harness<'static, PdfCraftApp> { + let options: Vec<(String, String)> = options.iter().map(|(key, value)| (key.to_string(), value.to_string())).collect(); let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).with_step_dt(1.0 / 60.0).build_eframe(move |_cc| { let mut app = new_app(); app.set_option(&egui::Context::default(), "language", "en").unwrap(); app.open_bytes(&egui::Context::default(), "work.pdf", None, bytes).expect("opens"); for (key, value) in options { - app.set_option(&egui::Context::default(), key, value).unwrap(); + app.set_option(&egui::Context::default(), &key, &value).unwrap(); } app });