From aadcdb774a7d594a9ff76f50f8716d9aaa40d4de Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 24 Nov 2026 18:45:30 -0800 Subject: [PATCH] Tests: the progress window's delay counts on the app's clock (now_override in tests), which the background-job tests hold still, and the heavy filter shortcut test keeps its run on a manual job queue, so neither depends on how fast a loaded machine runs --- crates/ui-egui/src/app.rs | 14 ++++++++---- crates/ui-egui/src/background.rs | 14 ++---------- crates/ui-egui/src/export_progress.rs | 13 ++++++----- crates/ui-egui/src/lib.rs | 5 +++-- crates/ui-egui/src/opening.rs | 2 +- crates/ui-egui/src/opening/tests.rs | 2 ++ crates/ui-egui/src/placing.rs | 3 ++- crates/ui-egui/src/placing/tests.rs | 2 ++ crates/ui-egui/src/plugin_runs.rs | 4 ++-- crates/ui-egui/src/shortcuts/tests.rs | 4 ++++ crates/ui-egui/src/tests/plugin_runs.rs | 30 +++++++++++++++++++++++-- crates/ui-egui/src/tests/trace_runs.rs | 4 +++- crates/ui-egui/src/trace_runs.rs | 2 +- 13 files changed, 68 insertions(+), 31 deletions(-) diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index 225be05ca7..fdf92146d2 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -308,6 +308,15 @@ impl VectorcraftApp { /// eframe isn't a dependency of this crate (the host owns the event loop); these entry points are /// called from the host's `eframe::App` impl through `embed`'s hooks. impl VectorcraftApp { + /// Seconds on the app's clock: the monotonic clock, or the time a test set in `now_override`. + pub(crate) fn now(&self) -> f64 { + #[cfg(test)] + if let Some(now) = self.now_override { + return now; + } + now_secs() + } + /// Applies the work that finished off the UI thread. It stays pending while a script runs, since /// every change to the documents ends in a busy error until the script ends. pub(crate) fn poll_finished_work(&mut self) { @@ -415,10 +424,7 @@ impl VectorcraftApp { // Monotonic host time, never egui's `InputState::time`: a hidden window's `logic` replays // the last shown pass's time, freezing recovery, clipboard polling and deadlines. Tests // inject a clock to drive idle scheduling deterministically. - #[cfg(test)] - let now = self.now_override.unwrap_or_else(now_secs); - #[cfg(not(test))] - let now = now_secs(); + let now = self.now(); self.perf.fps = self.frame_rate.frame(now, self.perf.frame_ms / 1000.0); self.sync_views(); self.refresh_replace_font_warning(false); diff --git a/crates/ui-egui/src/background.rs b/crates/ui-egui/src/background.rs index 779f6dc59c..57a9bf1086 100644 --- a/crates/ui-egui/src/background.rs +++ b/crates/ui-egui/src/background.rs @@ -263,6 +263,7 @@ fn start( let id = app.background.next_id; let jobs = app.jobs(); let (sets, status) = (app.ui.status_sets, if label.is_empty() { String::new() } else { format!("{label}…") }); + let started = export_progress::now(app); let bg = &mut app.background; bg.next_id += 1; let tx = bg.results.get_or_insert_with(channel).0.clone(); @@ -280,18 +281,7 @@ fn start( }), ); let line = status.clone(); - bg.jobs.push(Job { - id, - kind, - label: label.clone(), - doc, - progress, - hidden: false, - started: export_progress::now(), - sets, - line, - then: Box::new(then), - }); + bg.jobs.push(Job { id, kind, label: label.clone(), doc, progress, hidden: false, started, sets, line, then: Box::new(then) }); if label.is_empty() { return Ok(json!({ "background": true })); } diff --git a/crates/ui-egui/src/export_progress.rs b/crates/ui-egui/src/export_progress.rs index bfef9a1f61..c8170460a3 100644 --- a/crates/ui-egui/src/export_progress.rs +++ b/crates/ui-egui/src/export_progress.rs @@ -39,9 +39,10 @@ pub(crate) struct Shown { pub hide: Option<&'static str>, } -/// Seconds on the clock the jobs record their start with. -pub(crate) fn now() -> f64 { - craft::platform::clock::monotonic_ms() / 1000.0 +/// Seconds on the clock the jobs record their start with: the app's clock, which a test can hold +/// still. +pub(crate) fn now(app: &VectorcraftApp) -> f64 { + app.now() } /// The window of the first table that has a job it shows. Opening and placing wait for the tables @@ -58,7 +59,7 @@ fn first(app: &VectorcraftApp) -> Option { /// no other dialog is open, and runs the command of the button pressed. pub(crate) fn show(app: &mut VectorcraftApp, ui: &mut egui::Ui) { let Some(shown) = first(app) else { return }; - let age = now() - shown.started; + let age = now(app) - shown.started; if age < DELAY { // Nothing else wakes the frame that opens the window. ui.ctx().request_repaint_after(Duration::from_secs_f64(DELAY - age)); @@ -99,7 +100,8 @@ mod tests { use super::DELAY; use crate::{Services, VectorcraftApp, background}; - /// An app whose background writer waits until `go` is set. + /// An app whose background writer waits until `go` is set. Its clock stands still: a job is only + /// old enough for its window after [`age`]. fn app(go: Arc) -> VectorcraftApp { let services = Services { write_shared: Some(Arc::new(move |_: &str, _: &[u8]| { @@ -112,6 +114,7 @@ mod tests { }; let mut app = VectorcraftApp::new(Session::new(), services); crate::tests_background::threaded(&mut app); + app.now_override = Some(0.0); app } diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 59aa12e06c..4fc00a0a78 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -340,8 +340,9 @@ pub struct VectorcraftApp { pub(crate) pen_end: stylus::PenEnd, /// The window pass whose [`Self::tick`] ran last: work started in that pass, before its end, waits for the next. pub(crate) ticked_pass: Option, - /// A test's clock for [`Self::tick`], replacing the monotonic clock so idle scheduling can be - /// driven one simulated frame at a time (production always leaves this `None`). + /// A test's clock for [`Self::tick`] and the progress window's delay, replacing the monotonic + /// clock so idle scheduling can be driven one simulated frame at a time (production always + /// leaves this `None`). #[cfg(test)] pub(crate) now_override: Option, /// An error the next tool choice returns before the engine sees it ([`Self::try_select_tool`]), diff --git a/crates/ui-egui/src/opening.rs b/crates/ui-egui/src/opening.rs index 31e8ce20f2..15235131dd 100644 --- a/crates/ui-egui/src/opening.rs +++ b/crates/ui-egui/src/opening.rs @@ -129,7 +129,7 @@ pub(crate) fn finish_answered(app: &mut VectorcraftApp, path: &str, file: ReadFi /// Lists the file being read for `path` and tells the status bar → `{background: true, status}`. fn track(app: &mut VectorcraftApp, path: &str, done: Receiver>, scope: ScopeHandle, token: CancelToken) -> Value { let label = format!("Opening {}", fileio::file_name(path)); - let started = crate::export_progress::now(); + let started = crate::export_progress::now(app); app.opening.pending.push(Pending { path: path.to_string(), label: label.clone(), done, scope, token, hidden: false, started }); let status = format!("{label}…"); app.status(status.clone()); diff --git a/crates/ui-egui/src/opening/tests.rs b/crates/ui-egui/src/opening/tests.rs index 609f3c18a8..2466381a3d 100644 --- a/crates/ui-egui/src/opening/tests.rs +++ b/crates/ui-egui/src/opening/tests.rs @@ -526,6 +526,8 @@ fn bytes(title: &str) -> Vec { #[test] fn a_file_that_takes_a_while_to_open_shows_a_progress_window() { let mut app = VectorcraftApp::new(Session::new(), Services::default()); + // The app's clock stands still: a read is only old enough for its window after `age`. + app.now_override = Some(0.0); let ctx = crate::testing::ui_context(); assert_eq!(progress(&mut app, &ctx), "", "nothing opening"); start(&mut app, "/art/Big.vectorcraft", bytes("Big")); diff --git a/crates/ui-egui/src/placing.rs b/crates/ui-egui/src/placing.rs index d70caa18ae..6be029beba 100644 --- a/crates/ui-egui/src/placing.rs +++ b/crates/ui-egui/src/placing.rs @@ -363,6 +363,7 @@ fn spawn_labeled( }); let jobs = app.jobs(); app.placing.lane.push(&*jobs, "vectorcraft-place", job); + let started = crate::export_progress::now(app); app.placing.pending.push(Pending { name, label: label.clone(), @@ -375,7 +376,7 @@ fn spawn_labeled( scope: completion, token, hidden: false, - started: crate::export_progress::now(), + started, }); let status = format!("{label}…"); app.status(status.clone()); diff --git a/crates/ui-egui/src/placing/tests.rs b/crates/ui-egui/src/placing/tests.rs index 385600ab5a..990e449e88 100644 --- a/crates/ui-egui/src/placing/tests.rs +++ b/crates/ui-egui/src/placing/tests.rs @@ -2017,6 +2017,8 @@ fn a_file_that_takes_a_while_to_place_shows_a_progress_window() { let mut app = VectorcraftApp::new(Session::new(), services); crate::tests_background::threaded(&mut app); app.placing.off_the_ui_thread = true; + // The app's clock stands still: a read is only old enough for its window after `age`. + app.now_override = Some(0.0); app.run("file.new", json!({"width": 400, "height": 300})).unwrap(); let ctx = crate::testing::ui_context(); diff --git a/crates/ui-egui/src/plugin_runs.rs b/crates/ui-egui/src/plugin_runs.rs index 1f9eae3978..f7c579337c 100644 --- a/crates/ui-egui/src/plugin_runs.rs +++ b/crates/ui-egui/src/plugin_runs.rs @@ -43,7 +43,7 @@ pub struct Running { pub progress: Arc, /// Continue in Background: the progress window no longer shows it. pub hidden: bool, - /// When it started, in seconds of the monotonic clock: the progress window's age. + /// [`export_progress::now`] when it started: the progress window's age. started: f64, done: Receiver, } @@ -89,7 +89,7 @@ pub fn start(app: &mut VectorcraftApp, p: Value) -> Result { }; app.host.jobs().spawn("vectorcraft-plugin", Box::new(job)); let label = format!("Running {who}…"); - let started = craft::platform::clock::monotonic_ms() / 1000.0; + let started = export_progress::now(app); let runs = &mut app.plugin_runs; runs.running.push(Running { label: label.clone(), who, params, progress: shown, hidden: false, started, done }); app.status(label.clone()); diff --git a/crates/ui-egui/src/shortcuts/tests.rs b/crates/ui-egui/src/shortcuts/tests.rs index 0f302c1551..4dc3ffaaa4 100644 --- a/crates/ui-egui/src/shortcuts/tests.rs +++ b/crates/ui-egui/src/shortcuts/tests.rs @@ -1717,6 +1717,10 @@ fn a_filter_plugin_keeps_its_shortcut_when_replaced_or_reloaded() { fn a_heavy_filter_plugin_runs_from_its_shortcut_as_from_its_menu_item() { use vectorcraft_plugins::wat as tpl; let mut app = new_app(); + // The background work waits until the test runs it, which it never does: the run started from + // the key is still running when Cancel stops it, however long the frames take. + let jobs = std::sync::Arc::new(craft::host::ManualJobs::new()); + app.host = std::sync::Arc::new(craft::host::Host::builder(app.host.app().clone()).i18n(crate::i18n::shared()).jobs(jobs).build()); app.session.allow_plugin_grants_from_requests(); app.session.execute("shape.rectangle", &json!({"x": 10, "y": 10, "width": 20, "height": 20})).unwrap(); let (at_once, asks) = ("test.UI.heavy_filter-at-once", "test.UI.heavy_filter-asks"); diff --git a/crates/ui-egui/src/tests/plugin_runs.rs b/crates/ui-egui/src/tests/plugin_runs.rs index 0ebc06643d..ed161e797f 100644 --- a/crates/ui-egui/src/tests/plugin_runs.rs +++ b/crates/ui-egui/src/tests/plugin_runs.rs @@ -16,7 +16,7 @@ use vectorcraft_plugins::{registry, wat as tpl}; use super::plugins::{approve, window}; use crate::dialogs::plugin::InstallPrompt; -use crate::{VectorcraftApp, dialogs, plugin_runs}; +use crate::{VectorcraftApp, dialogs, export_progress, plugin_runs}; /// An app with a new 200 × 200 pt document. fn app() -> VectorcraftApp { @@ -28,9 +28,11 @@ fn app() -> VectorcraftApp { /// 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. +/// before a later pass of that frame, which applies it. The app's clock stands still, so the +/// progress window, which would hold the results back, opens only when a test moves the clock. fn held_window() -> (Harness<'static, VectorcraftApp>, Arc) { let mut app = app(); + app.now_override = Some(0.0); 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) @@ -158,6 +160,30 @@ fn a_heavy_filter_runs_off_the_ui_thread_and_applies_as_one_undo_step() { assert!(app.ui.status.is_empty(), "{}", app.ui.status); } +/// The progress window opens once the run is [`export_progress::DELAY`] old on the app's clock, +/// however long the frames take; after Continue in Background closes it the result applies. +#[test] +fn the_progress_window_opens_by_the_app_clock() { + use egui_kittest::kittest::Queryable; + let (mut h, jobs) = held_window(); + let id = "test.ui.heavy.clock"; + install(h.state_mut(), id, 1, "{}"); + let undo = h.state().session.doc().unwrap().history.undo.len(); + assert_eq!(h.state_mut().run("plugin.dialog", json!({"id": id})).unwrap()["background"], true); + // Only the progress window shows its step. + let step = "The plug-in is computing…"; + frame(&mut h); + assert!(h.query_by_label(step).is_none(), "not before the delay"); + h.state_mut().now_override = Some(export_progress::DELAY); + frame(&mut h); + assert!(h.query_by_label(step).is_some(), "the progress window, after the delay"); + assert_eq!(h.state_mut().run("plugin.hideRuns", json!({})).unwrap(), json!({"hidden": 1})); + frame(&mut h); + assert!(h.query_by_label(step).is_none(), "Continue in Background closed it"); + finish_held(h.state_mut(), &jobs); + assert_eq!(h.state().session.doc().unwrap().history.undo.len(), undo + 1); +} + /// A document edited while the filter runs doesn't take its result: nothing changes, and the status /// bar says to run it again. #[test] diff --git a/crates/ui-egui/src/tests/trace_runs.rs b/crates/ui-egui/src/tests/trace_runs.rs index 5de44dbc68..17a92f9785 100644 --- a/crates/ui-egui/src/tests/trace_runs.rs +++ b/crates/ui-egui/src/tests/trace_runs.rs @@ -282,10 +282,12 @@ fn a_trace_started_before_a_revert_does_not_apply() { #[test] fn the_progress_window_shows_a_trace() { let mut app = app(); + // The app's clock stands still: the trace is only old enough for its window when made so. + app.now_override = Some(0.0); let ctx = crate::testing::ui_context(); crate::menus::invoke(&mut app, "imageTrace.make", json!({"preset": "Default"})); assert!(!progress(&mut app, &ctx, 0.0).contains("Tracing the image"), "not before the delay"); - // The delay counts on the monotonic clock: the trace started that long ago. + // The delay counts on the app's clock: the trace started that long ago. app.trace_runs.running[0].started -= DELAY; progress(&mut app, &ctx, DELAY + 0.05); // a new window measures itself unseen first let shown = progress(&mut app, &ctx, DELAY + 0.1); diff --git a/crates/ui-egui/src/trace_runs.rs b/crates/ui-egui/src/trace_runs.rs index 4f7b4f0dae..341c0c4113 100644 --- a/crates/ui-egui/src/trace_runs.rs +++ b/crates/ui-egui/src/trace_runs.rs @@ -125,7 +125,7 @@ pub fn start(app: &mut VectorcraftApp, command: &str, p: Value, from_panel: bool let _ = tx.send((run, traced)); }) .map_err(|e| e.to_string())?; - let started = crate::export_progress::now(); + let started = crate::export_progress::now(app); app.trace_runs.running.push(Running { started, doc, target, token, superseded: false, steps, from_panel, hidden: false, done }); // This frame may have asked for its next one before the trace started (`tick`): ask now. if let Some(ctx) = &app.ui_ctx {