diff --git a/crates/ui-egui/src/canvas/marks/tests.rs b/crates/ui-egui/src/canvas/marks/tests.rs index d2d7bfb4a3..33784cf250 100644 --- a/crates/ui-egui/src/canvas/marks/tests.rs +++ b/crates/ui-egui/src/canvas/marks/tests.rs @@ -187,6 +187,7 @@ fn auto_spell_check_underlines_the_unknown_words() { /// An edit, Ignore All and Add to Dictionary each change the segments of the next pass. #[test] fn auto_spell_check_follows_edits_ignore_all_and_the_dictionary() { + let _dictionary = crate::testing::custom_dictionary(); let (mut app, id) = spelling_doc("Wqzxvtr and Pzxqvrt"); assert_eq!(segments(&mut app), 2); app.session.execute("text.setText", &json!({"id": id.0, "text": "Wqzxvtr and Pzxqvrt or Qxzvwtr"})).unwrap(); diff --git a/crates/ui-egui/src/canvas/tests.rs b/crates/ui-egui/src/canvas/tests.rs index 2a683a07df..54648d2c61 100644 --- a/crates/ui-egui/src/canvas/tests.rs +++ b/crates/ui-egui/src/canvas/tests.rs @@ -681,9 +681,11 @@ fn convert8(t: &craft::color_ui::display::DisplayTransform, rgb: [u8; 3]) -> [u8 [px[0], px[1], px[2]] } -/// Run a test that compares two renders with the color settings that are active when it starts. -/// A test on another thread can change the process's active settings; this thread keeps its own. +/// Run a test that compares two renders with the color settings that are active when it starts, +/// and the default proof view. A test on another thread can change the process's active settings; +/// this thread keeps its own. The proof view is the whole process's, so it is held still. fn keeping_color_settings(test: fn()) { + let _view = crate::testing::DefaultProofView::new(); vectorcraft_color::cms::with_active(vectorcraft_color::cms::active(), test); } diff --git a/crates/ui-egui/src/control/tests.rs b/crates/ui-egui/src/control/tests.rs index fec7898e19..fdda88b384 100644 --- a/crates/ui-egui/src/control/tests.rs +++ b/crates/ui-egui/src/control/tests.rs @@ -1068,6 +1068,8 @@ fn menu_dialogs_open_after_a_merge_on_another_thread() { /// leave the view off, so they apply it by command id without giving the documents new revisions. #[test] fn panels_that_redraw_apply_a_merge_on_another_thread_first() { + // The proof view the checkboxes leave off is the whole process's. + let _view = crate::testing::DefaultProofView::new(); let cases = [ ("view.separationsPreview", json!({"on": false})), ("view.overprintPreview", json!({"on": false})), diff --git a/crates/ui-egui/src/dialogs/check_spelling.rs b/crates/ui-egui/src/dialogs/check_spelling.rs index dba6619638..4c4d575a6f 100644 --- a/crates/ui-egui/src/dialogs/check_spelling.rs +++ b/crates/ui-egui/src/dialogs/check_spelling.rs @@ -595,6 +595,7 @@ mod tests { #[test] fn ignore_goes_on_through_the_rest_of_the_same_type_object() { + let _dictionary = crate::testing::custom_dictionary(); let mut app = app(); let id = text(&mut app, 50.0, &format!("{WORD_B} one {WORD_B} two {WORD_B}")); let other = text(&mut app, 100.0, &format!("then {WORD_A}")); @@ -618,6 +619,7 @@ mod tests { #[test] fn ignore_and_change_all_reach_every_word_of_a_selected_groups_type() { + let _dictionary = crate::testing::custom_dictionary(); let mut app = app(); let a = text(&mut app, 50.0, &format!("{WORD_B} {WORD_D} {WORD_B}")); let b = text(&mut app, 100.0, &format!("{WORD_D} {WORD_B}")); @@ -650,6 +652,7 @@ mod tests { #[test] fn change_all_reaches_a_target_past_a_full_check_of_other_words() { + let _dictionary = crate::testing::custom_dictionary(); let mut app = app(); // The second WORD_A comes after a whole check's worth of other unknown words. PAGE.with(|p| p.set(2)); @@ -670,6 +673,7 @@ mod tests { #[test] fn change_all_reaches_a_target_past_the_checks_limit_in_one_object() { + let _dictionary = crate::testing::custom_dictionary(); use vectorcraft_engine::cmd::spelling::MAX_WORDS; let mut app = app(); // A target, then the most other words one check lists, then a second target, all in one @@ -686,6 +690,7 @@ mod tests { #[test] fn text_changed_under_the_dialog_is_checked_again_from_its_start() { + let _dictionary = crate::testing::custom_dictionary(); // A word of its own: Add to Dictionary puts it in the custom dictionary the process shares. const STALE: &str = "zqvstaleword"; for f in steps() { @@ -704,6 +709,7 @@ mod tests { #[test] fn a_restarted_check_still_skips_the_ignored_occurrences() { + let _dictionary = crate::testing::custom_dictionary(); let mut app = app(); let id = text(&mut app, 50.0, &format!("{WORD_A} {WORD_B} {WORD_A}")); begin(&mut app); @@ -717,6 +723,7 @@ mod tests { #[test] fn an_unknown_word_brought_back_before_the_shown_one_is_shown_again() { + let _dictionary = crate::testing::custom_dictionary(); // Words of their own: Add to Dictionary puts one in the custom dictionary the process shares. // FIRST has as many bytes as "different", so the second word stays at the bytes shown. const FIRST: &str = "zqvbckone"; @@ -741,6 +748,7 @@ mod tests { #[test] fn a_word_taken_out_of_the_custom_dictionary_is_shown_again() { + let _dictionary = crate::testing::custom_dictionary(); // Words of their own: the custom dictionary is shared by the whole process. const FIRST: &str = "zqvdictone"; const SECOND: &str = "zqvdicttwo"; @@ -757,6 +765,7 @@ mod tests { #[test] fn a_shown_object_that_leaves_the_scope_starts_the_scope_again() { + let _dictionary = crate::testing::custom_dictionary(); let mut app = app(); let first = text(&mut app, 50.0, &format!("{WORD_A} here")); let second = text(&mut app, 100.0, &format!("{WORD_B} there")); @@ -770,6 +779,7 @@ mod tests { #[test] fn a_selected_object_deleted_under_the_dialog_leaves_the_scope() { + let _dictionary = crate::testing::custom_dictionary(); let mut app = app(); let first = text(&mut app, 50.0, &format!("{WORD_A} here")); let second = text(&mut app, 100.0, &format!("{WORD_B} there")); @@ -797,6 +807,7 @@ mod tests { #[test] fn ignore_reaches_the_last_of_more_unknown_words_than_one_check_lists() { + let _dictionary = crate::testing::custom_dictionary(); use vectorcraft_engine::cmd::spelling::MAX_WORDS; let mut app = app(); let id = text(&mut app, 50.0, &vec![WORD_B; MAX_WORDS + 1].join(" ")); @@ -813,6 +824,7 @@ mod tests { #[test] fn ignore_and_change_all_reach_a_selected_groups_type_past_the_checks_limit() { + let _dictionary = crate::testing::custom_dictionary(); use vectorcraft_engine::cmd::spelling::MAX_WORDS; let mut app = app(); let a = text(&mut app, 50.0, &vec![WORD_B; MAX_WORDS].join(" ")); @@ -841,6 +853,7 @@ mod tests { #[test] fn ignore_all_and_add_to_dictionary_go_on_past_each_check() { + let _dictionary = crate::testing::custom_dictionary(); PAGE.with(|p| p.set(2)); let mut app = app(); let id = text(&mut app, 50.0, &format!("{WORD_A} {WORD_B} {WORD_A} {WORD_D} {WORD_A}")); @@ -874,6 +887,7 @@ mod tests { #[test] fn change_to_the_same_word_changes_nothing_and_goes_on() { + let _dictionary = crate::testing::custom_dictionary(); type To = fn(&mut VectorcraftApp, &mut SpellingWalk, &str) -> Result<(), String>; for f in [super::change as To, super::change_all] { let mut app = app(); @@ -890,6 +904,7 @@ mod tests { #[test] fn a_word_moved_by_another_command_is_not_taken_for_an_ignored_one() { + let _dictionary = crate::testing::custom_dictionary(); let mut app = app(); let id = text(&mut app, 50.0, &format!("{WORD_A} {WORD_B} {WORD_A}")); begin(&mut app); @@ -904,6 +919,7 @@ mod tests { #[test] fn change_all_changes_a_whole_checks_worth_of_occurrences_in_one_edit() { + let _dictionary = crate::testing::custom_dictionary(); use vectorcraft_engine::cmd::spelling::MAX_WORDS; let mut app = app(); let id = text(&mut app, 50.0, &vec![WORD_A; MAX_WORDS + 1].join(" ")); @@ -925,6 +941,7 @@ mod tests { #[test] fn a_check_with_all_layers_off_stays_in_the_shown_object() { + let _dictionary = crate::testing::custom_dictionary(); let mut app = app(); let a = text(&mut app, 50.0, &format!("{WORD_A} x")); let b = text(&mut app, 100.0, &format!("{WORD_A} y")); @@ -1012,6 +1029,7 @@ mod tests { #[test] fn check_spelling_word_replacement_and_undo_redo() { + let _dictionary = crate::testing::custom_dictionary(); let mut app = app(); let id = text(&mut app, 50.0, &format!("Prefix {WORD_A} suffix")); @@ -1028,6 +1046,7 @@ mod tests { #[test] fn check_spelling_change_all_in_one_undo_step() { + let _dictionary = crate::testing::custom_dictionary(); let mut app = app(); let a = text(&mut app, 50.0, &format!("First {WORD_B} here and {WORD_B} again")); let b = text(&mut app, 100.0, &format!("Second {WORD_B} line")); @@ -1046,6 +1065,7 @@ mod tests { #[test] fn check_spelling_dictionary_addition() { + let _dictionary = crate::testing::custom_dictionary(); let mut app = app(); forget(&mut app, &[WORD_C]); text(&mut app, 50.0, &format!("Look at {WORD_C} here")); @@ -1086,6 +1106,7 @@ mod tests { #[test] fn check_spelling_refused_on_document_switch() { + let _dictionary = crate::testing::custom_dictionary(); let mut app = app(); text(&mut app, 50.0, &format!("Text {WORD_A} here")); begin(&mut app); @@ -1100,6 +1121,7 @@ mod tests { #[test] fn check_spelling_ignores_uneditable_text() { + let _dictionary = crate::testing::custom_dictionary(); let mut app = app(); let editable_id = text(&mut app, 50.0, &format!("Prefix {WORD_A} suffix")); let locked_id = text(&mut app, 100.0, &format!("Prefix {WORD_B} suffix")); @@ -1120,6 +1142,7 @@ mod tests { #[test] fn check_spelling_change_all_containing_target_word_terminates() { + let _dictionary = crate::testing::custom_dictionary(); let mut app = app(); let t1 = text(&mut app, 50.0, &format!("Prefix {WORD_A} suffix")); diff --git a/crates/ui-egui/src/dialogs/custom_dictionary.rs b/crates/ui-egui/src/dialogs/custom_dictionary.rs index 13b8d8393a..d06fa4640a 100644 --- a/crates/ui-egui/src/dialogs/custom_dictionary.rs +++ b/crates/ui-egui/src/dialogs/custom_dictionary.rs @@ -198,6 +198,7 @@ mod tests { /// applies the list once. #[test] fn add_and_delete_apply_on_done() { + let _dictionary = crate::testing::custom_dictionary(); let mut app = app(); forget(&mut app, &[A, B, C]); app.run("text.spelling.addWord", json!({ "word": A })).unwrap(); @@ -238,6 +239,7 @@ mod tests { /// dictionary as it was. #[test] fn a_done_that_cant_be_saved_changes_nothing() { + let _dictionary = crate::testing::custom_dictionary(); const P: &str = "zqvdictp"; const Q: &str = "zqvdictq"; let mut app = app(); diff --git a/crates/ui-egui/src/dialogs/proof_setup.rs b/crates/ui-egui/src/dialogs/proof_setup.rs index 4ff53c7cba..782f8cbe09 100644 --- a/crates/ui-egui/src/dialogs/proof_setup.rs +++ b/crates/ui-egui/src/dialogs/proof_setup.rs @@ -494,30 +494,12 @@ pub fn saved_items() -> Vec { #[cfg(test)] mod tests { use super::*; + use crate::testing::DefaultProofView; use craft::host::ScriptedFiles; use craft::ui::dialog::DialogHost; use std::sync::Arc; use vectorcraft_engine::Session; - /// Proof views are process-wide: these tests take turns, and each leaves the default view. - static VIEW: std::sync::Mutex<()> = std::sync::Mutex::new(()); - - struct DefaultView<'a>(#[allow(dead_code)] std::sync::MutexGuard<'a, ()>); - - impl DefaultView<'_> { - fn new() -> Self { - let g = VIEW.lock().unwrap_or_else(|e| e.into_inner()); - proof::set_view(Default::default()); - DefaultView(g) - } - } - - impl Drop for DefaultView<'_> { - fn drop(&mut self) { - proof::set_view(Default::default()); - } - } - fn app() -> (VectorcraftApp, egui::Context) { let mut app = VectorcraftApp::new(Session::new(), Default::default()); app.run("file.new", json!({})).unwrap(); @@ -592,7 +574,7 @@ mod tests { /// device, Preserve Numbers, the intent and Simulate Black Ink, and turns Proof Colors on. #[test] fn customize_sets_the_proof_setup_and_turns_proof_colors_on() { - let _v = DefaultView::new(); + let _v = DefaultProofView::new(); let (mut app, ctx) = app(); let item = crate::menus::menu_entries(&app).into_iter().find(|e| e.path == ["View", "Proof Setup"] && e.label == "Customize…").unwrap(); app.run(item.command.as_deref().unwrap(), item.params.clone()).unwrap(); @@ -621,7 +603,7 @@ mod tests { /// Cancel (or turning Preview off) puts the earlier proof view back. #[test] fn preview_shows_the_settings_and_cancel_puts_the_view_back() { - let _v = DefaultView::new(); + let _v = DefaultProofView::new(); let (mut app, ctx) = app(); app.run("view.proofSetup", json!({"target": "protanopia"})).unwrap(); let before = proof::view(); @@ -646,7 +628,7 @@ mod tests { /// set, and redraw the documents. #[test] fn cancel_puts_back_a_view_whose_profile_was_removed() { - let _v = DefaultView::new(); + let _v = DefaultProofView::new(); let (mut app, ctx) = app(); let name = "Proof Setup Removed RGB"; cms::register_icc(&cms::icc_bytes(cms::DISPLAY_P3).unwrap(), Some(name.into())).unwrap(); @@ -670,7 +652,7 @@ mod tests { /// puts back the view it found the first time. #[test] fn running_proof_setup_again_keeps_the_open_dialog() { - let _v = DefaultView::new(); + let _v = DefaultProofView::new(); let (mut app, ctx) = app(); let before = proof::view(); app.run("ui.proofSetupDialog", json!({})).unwrap(); @@ -686,7 +668,7 @@ mod tests { /// the dialog found comes back, and Overprint Preview turned on during the preview stays on. #[test] fn another_dialog_in_its_place_puts_the_view_back() { - let _v = DefaultView::new(); + let _v = DefaultProofView::new(); let (mut app, ctx) = app(); let before = proof::view(); app.run("ui.proofSetupDialog", json!({})).unwrap(); @@ -714,7 +696,7 @@ mod tests { /// view it found. #[test] fn back_from_under_another_dialog_it_previews_again() { - let _v = DefaultView::new(); + let _v = DefaultProofView::new(); let (mut app, ctx) = app(); let before = proof::view(); app.run("ui.proofSetupDialog", json!({})).unwrap(); @@ -734,7 +716,7 @@ mod tests { /// Preview on shows the dialog's values again, and Cancel puts back the command's view. #[test] fn a_view_set_while_the_dialog_previews_stays() { - let _v = DefaultView::new(); + let _v = DefaultProofView::new(); let (mut app, ctx) = app(); app.run("ui.proofSetupDialog", json!({})).unwrap(); set(&mut app, &ctx, "device", json!(rgb())); @@ -752,7 +734,7 @@ mod tests { /// stays open with the proof view as it was. #[test] fn ok_with_an_unknown_device_keeps_the_dialog_open() { - let _v = DefaultView::new(); + let _v = DefaultProofView::new(); let (mut app, ctx) = app(); let name = "Proof Setup Gone RGB"; cms::register_icc(&cms::icc_bytes(cms::DISPLAY_P3).unwrap(), Some(name.into())).unwrap(); @@ -774,7 +756,7 @@ mod tests { /// applies the setup with Proof Colors on. #[test] fn save_and_load_keep_a_custom_proof_setup() { - let _v = DefaultView::new(); + let _v = DefaultProofView::new(); let (mut app, ctx) = app(); let dir = vectorcraft_testkit::TempDir::new("proof-setup-dialog"); let folder = dir.join("Proof Setups"); @@ -833,7 +815,7 @@ mod tests { /// folder stays while the picker is open and when a file is saved in it. #[test] fn a_canceled_save_leaves_no_folder() { - let _v = DefaultView::new(); + let _v = DefaultProofView::new(); let (mut app, ctx) = app(); let dir = vectorcraft_testkit::TempDir::new("proof-setup-dialog-cancel"); let folder = dir.join("Proof Setups"); @@ -869,7 +851,7 @@ mod tests { /// folder before; Load… does not make the folder. #[test] fn load_starts_in_the_folder_once_it_exists() { - let _v = DefaultView::new(); + let _v = DefaultProofView::new(); let (mut app, ctx) = app(); let dir = vectorcraft_testkit::TempDir::new("proof-setup-dialog-load-folder"); let folder = dir.join("Proof Setups"); diff --git a/crates/ui-egui/src/palette.rs b/crates/ui-egui/src/palette.rs index 1a329e6fcb..bf08e37abb 100644 --- a/crates/ui-egui/src/palette.rs +++ b/crates/ui-egui/src/palette.rs @@ -2464,53 +2464,40 @@ mod tests { #[test] fn the_entries_are_built_again_only_when_the_catalog_or_the_language_changes() { // The process-wide parts of the catalog's generation, which tests running alongside move - // (fonts, plug-ins, scripts): the count is read on a run they held still through. + // (fonts, plug-ins, scripts), as each step began and as it ended. let shared = || (crate::menus::plugin_revision(), crate::scripts_menu::revision(), vectorcraft_text::FontDb::global().generation()); - for _ in 0..20 { - let before = shared(); - let (seen, english) = palette_builds_over_opens_and_a_language_change(); - if shared() != before { - continue; - } - assert_ne!(seen.last().map(|(key, _)| *key), english, "the language moves the catalog's generation"); - let builds = seen.windows(2).filter(|p| !Arc::ptr_eq(&p[0].1, &p[1].1)).count(); - assert!(builds >= 1 && builds < seen.len() - 1, "{builds} builds over {} opens and edits", seen.len()); - return; - } - panic!("the fonts, plug-ins or scripts kept changing"); - } - - /// The palette's build key and index after each step of the trial, and the key in English. - fn palette_builds_over_opens_and_a_language_change() -> (Vec<(BuildKey, Arc)>, Option) { let mut window = Window::new(); assert!(window.app.canvas.palette.is_none(), "nothing is built while the palette is closed"); - let built = |w: &Window| w.app.canvas.palette.as_ref().map(|(key, b)| (*key, b.index.clone())).expect("the open palette is built"); - let mut seen = vec![]; + let mut seen: Vec<(BuildKey, Arc, _, _)> = vec![]; + let mut step = |w: &mut Window, frames: Vec>| { + let began = shared(); + for events in frames { + w.frame(events); + } + let (key, b) = w.app.canvas.palette.as_ref().expect("the open palette is built"); + seen.push((*key, b.index.clone(), began, shared())); + }; window.app.ui.palette_open = true; - window.frame(vec![]); - seen.push(built(&window)); - window.frame(vec![egui::Event::Text("group".into())]); - seen.push(built(&window)); - window.frame(key(egui::Key::Enter)); - window.frame(vec![]); - seen.push(built(&window)); + step(&mut window, vec![vec![]]); + step(&mut window, vec![vec![egui::Event::Text("group".into())]]); + step(&mut window, vec![key(egui::Key::Enter), vec![]]); window.app.run("select.none", json!({})).unwrap(); window.app.ui.palette_open = true; - window.frame(vec![]); - seen.push(built(&window)); - let english = seen.last().map(|(key, _)| *key); + step(&mut window, vec![vec![]]); // The language as the app takes it: each frame's tick sets the drawing language from the preference. window.app.session.prefs.interface_language = "de".into(); - window.frame(vec![]); - window.frame(vec![]); - seen.push(built(&window)); + step(&mut window, vec![vec![], vec![]]); crate::i18n::set_current(Lang::EN); - // Another test may install a plug-in meanwhile, which moves the key too: the rule is a - // build exactly when the key moves. - for pair in seen.windows(2) { - let [(k0, i0), (k1, i1)] = pair else { continue }; + for (i, pair) in seen.windows(2).enumerate() { + let [(k0, i0, began, _), (k1, i1, _, ended)] = pair else { continue }; + // A build exactly when the key moves. assert_eq!(!Arc::ptr_eq(i0, i1), k0 != k1, "{k0:?} → {k1:?}"); + if i + 2 == seen.len() { + assert_ne!(k0, k1, "the language moves the catalog's generation"); + } else if began == ended { + // Typing, a pick, a close, an open and a state the catalog does not read. + assert_eq!(k0, k1, "step {}: nothing the catalog reads changed", i + 1); + } } - (seen, english) } } diff --git a/crates/ui-egui/src/shortcut_editor.rs b/crates/ui-egui/src/shortcut_editor.rs index 545aa48a65..5ccce9988c 100644 --- a/crates/ui-egui/src/shortcut_editor.rs +++ b/crates/ui-egui/src/shortcut_editor.rs @@ -1027,9 +1027,8 @@ pub(crate) fn show(app: &mut VectorcraftApp, ui: &mut egui::Ui) { // A plug-in installed or removed while the dialog is open changes the window's commands: the dialog takes them, // keeping its edits. if let Some(window) = commands::keymap(ui.ctx()).filter(|w| current.window.as_ref().is_none_or(|was| !Arc::ptr_eq(was, w))) { - // shortcut: the commands are compared by count; a swap of one plug-in for another within a frame needs ids. let fresh = match current.keymap.take() { - Some(edited) if edited.catalog().len() == window.catalog().len() => edited, + Some(edited) if edited.catalog() == window.catalog() => edited, edited => { let mut fresh = (*window).clone(); if let Some(edited) = edited { diff --git a/crates/ui-egui/src/testing.rs b/crates/ui-egui/src/testing.rs index 6f2f16cdf5..fcc438c353 100644 --- a/crates/ui-egui/src/testing.rs +++ b/crates/ui-egui/src/testing.rs @@ -61,6 +61,36 @@ pub(crate) fn part_frame( out } +/// Held by a test that changes the custom dictionary, which the whole process shares, or that needs it to hold still: +/// every change moves the dictionary's generation, and a Check Spelling walk that sees it move checks its scope again +/// from the start. +pub(crate) fn custom_dictionary() -> std::sync::MutexGuard<'static, ()> { + static HELD: std::sync::Mutex<()> = std::sync::Mutex::new(()); + HELD.lock().unwrap_or_else(std::sync::PoisonError::into_inner) +} + +/// The default proof view, held: View › Proof Colors, Overprint Preview and Separations Preview are the whole process's +/// and the canvas draws through them, so a test that sets them, or that compares the canvas's renders, holds this. It +/// leaves the default view when it is dropped. +pub(crate) struct DefaultProofView { + _held: std::sync::MutexGuard<'static, ()>, +} + +impl DefaultProofView { + pub(crate) fn new() -> Self { + static HELD: std::sync::Mutex<()> = std::sync::Mutex::new(()); + let held = HELD.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + vectorcraft_render::proof::set_view(Default::default()); + DefaultProofView { _held: held } + } +} + +impl Drop for DefaultProofView { + fn drop(&mut self) { + vectorcraft_render::proof::set_view(Default::default()); + } +} + /// Gives `fixture` the app's input for its next frame as the hosts give it (`embed::before_input`): the commands the /// frames since the last call fired run first. pub(crate) fn fixture_input(app: &mut crate::VectorcraftApp, fixture: &mut craft::ui::testing::Fixture) { diff --git a/crates/ui-egui/src/tests/plugin_runs.rs b/crates/ui-egui/src/tests/plugin_runs.rs index 728bb1c958..0ebc06643d 100644 --- a/crates/ui-egui/src/tests/plugin_runs.rs +++ b/crates/ui-egui/src/tests/plugin_runs.rs @@ -3,8 +3,10 @@ //! done, unless the document changed meanwhile. Cancel stops it; its progress window shows it. A //! light filter that reports its progress runs there too. +use std::sync::Arc; use std::time::{Duration, Instant}; +use craft::host::{Host, ManualJobs}; use craft::ui::dialog::DialogId; use craft::ui::patterns::form_dialogs::HostedParamDialog; use egui_kittest::Harness; @@ -24,6 +26,16 @@ fn app() -> VectorcraftApp { app } +/// The window of an [`app`] whose background work waits until the test runs it: a run that OK +/// started is still running when the frame that started it ends. On threads a fast run could end +/// before a later pass of that frame, which applies it. +fn held_window() -> (Harness<'static, VectorcraftApp>, Arc) { + let mut app = app(); + let jobs = Arc::new(ManualJobs::new()); + app.host = Arc::new(Host::builder(app.host.app().clone()).i18n(crate::i18n::shared()).jobs(jobs.clone()).build()); + (window(app), jobs) +} + /// A heavy test filter `id` (named "Test `id`") with `params` that counts `steps` down, then adds /// a triangle. fn heavy(id: &str, steps: u64, params: &str) -> Vec { @@ -125,7 +137,7 @@ fn installing_a_heavy_filter_asks_first() { /// saying so, and its result is one undo step named after it, selected. #[test] fn a_heavy_filter_runs_off_the_ui_thread_and_applies_as_one_undo_step() { - let mut h = window(app()); + let (mut h, jobs) = held_window(); let id = "test.ui.heavy.run"; install(h.state_mut(), id, 1000, r#"{"size":{"type":"number","min":1,"max":9,"default":3}}"#); h.state_mut().run("plugin.dialog", json!({"id": id})).unwrap(); @@ -138,7 +150,7 @@ fn a_heavy_filter_runs_off_the_ui_thread_and_applies_as_one_undo_step() { assert_eq!(h.state().plugin_runs.running.len(), 1); assert_eq!(h.state().ui.status, format!("Running “Test {id}”…")); let app = h.state_mut(); - wait(app); + finish_held(app, &jobs); let st = app.session.doc().unwrap(); assert_eq!(st.history.undo.len(), undo + 1, "one undo step"); assert_eq!(st.history.undo.last().unwrap().label, format!("Test {id}")); @@ -258,6 +270,12 @@ fn finish_runs(app: &mut VectorcraftApp) { } } +/// Runs the jobs [`held_window`] held, then applies the runs' results as the frames would. +fn finish_held(app: &mut VectorcraftApp, jobs: &ManualJobs) { + jobs.run_all(); + finish_runs(app); +} + /// A light filter that may report its progress runs off the UI thread when it runs at once, as a /// heavy one does: the status bar says so, and its result is one undo step named after it. #[test] @@ -281,7 +299,7 @@ fn a_light_filter_that_reports_progress_runs_off_the_ui_thread() { /// run, as for any light filter. #[test] fn ok_runs_a_light_progress_filter_in_the_background_without_a_preview() { - let mut h = window(app()); + let (mut h, jobs) = held_window(); let id = "test.ui.light.progress.dialog"; install_light(h.state_mut(), id, true, r#"{"size":{"type":"number","min":1,"max":9,"default":3}}"#, ""); let undo = h.state().session.doc().unwrap().history.undo.len(); @@ -292,7 +310,7 @@ fn ok_runs_a_light_progress_filter_in_the_background_without_a_preview() { answer(&mut h, true); assert!(none_open(h.state())); assert_eq!(h.state().plugin_runs.running.len(), 1, "running in the background"); - finish_runs(h.state_mut()); + finish_held(h.state_mut(), &jobs); assert_eq!(h.state().session.doc().unwrap().history.undo.len(), undo + 1); // Preview on: OK keeps the previewed run, and nothing runs in the background. h.state_mut().run("plugin.dialog", json!({"id": id})).unwrap(); @@ -310,7 +328,7 @@ fn ok_runs_a_light_progress_filter_in_the_background_without_a_preview() { assert!(!h.state().session.in_interaction(), "the preview was rolled back"); answer(&mut h, true); assert_eq!(h.state().plugin_runs.running.len(), 1, "running in the background"); - finish_runs(h.state_mut()); + finish_held(h.state_mut(), &jobs); assert_eq!(h.state().session.doc().unwrap().history.undo.len(), undo + 3); } @@ -318,7 +336,7 @@ fn ok_runs_a_light_progress_filter_in_the_background_without_a_preview() { /// rolls it back and runs the filter in the background instead of again on the UI thread. #[test] fn ok_after_a_failed_preview_runs_a_light_progress_filter_in_the_background() { - let mut h = window(app()); + let (mut h, jobs) = held_window(); let id = "test.ui.light.progress.fails"; install_light(h.state_mut(), id, true, r#"{"size":{"type":"number","min":1,"max":9,"default":3}}"#, "(unreachable)"); let doc = h.state().session.doc().unwrap().doc.clone(); @@ -330,7 +348,7 @@ fn ok_after_a_failed_preview_runs_a_light_progress_filter_in_the_background() { // Only a refused preview is not kept; a kept one would have committed and left no run. assert!(!h.state().session.in_interaction(), "the failed preview was rolled back"); assert_eq!(h.state().plugin_runs.running.len(), 1, "running in the background"); - finish_runs(h.state_mut()); + finish_held(h.state_mut(), &jobs); assert_eq!(h.state().session.doc().unwrap().doc, doc); }