diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index 7e77cbc6ca..a71701ab3d 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -276,6 +276,8 @@ impl VectorcraftApp { #[cfg(test)] now_override: None, #[cfg(test)] + clock_origin: craft::ui::runtime::motion::now(), + #[cfg(test)] fail_next_tool_choice: None, held_tool: Vec::new(), } @@ -341,6 +343,17 @@ impl VectorcraftApp { now_secs() } + /// The app's clock as an instant, which the control channel times requests and captures with: + /// [`motion::now`](craft::ui::runtime::motion::now), or `now_override` seconds after the app was + /// made when a test set it. + pub(crate) fn instant(&self) -> craft::ui::runtime::motion::Instant { + #[cfg(test)] + if let Some(now) = self.now_override { + return self.clock_origin + std::time::Duration::from_secs_f64(now.max(0.0)); + } + craft::ui::runtime::motion::now() + } + /// 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) { diff --git a/crates/ui-egui/src/app/control.rs b/crates/ui-egui/src/app/control.rs index 845a5498b3..2389373b4c 100644 --- a/crates/ui-egui/src/app/control.rs +++ b/crates/ui-egui/src/app/control.rs @@ -1,6 +1,5 @@ use craft::automation::ControlChannel; use craft::automation::screenshot::{DocumentId, Finished, settle_ms}; -use craft::ui::runtime::motion; use craft::ui::runtime::windows::WindowHost; use serde_json::json; use vectorcraft_engine::file_access::AutomationRoots; @@ -38,15 +37,16 @@ impl VectorcraftApp { } let host = WindowHost::of(ctx); let document = self.document_identity(); + let now = self.instant(); let finished = self.channel.as_mut().map_or_else(Vec::new, |channel| { channel.captures.note_windows(&host, document); - channel.captures.drain(motion::now(), document) + channel.captures.drain(now, document) }); self.finish_captures(finished); let settle = settle_ms(ctx.global_style().animation_time); while let Some(req) = self.deferred_control.take().or_else(|| self.channel.as_mut().and_then(ControlChannel::next_request)) { // A request held past its deadline is answered as one still queued then. - if req.deadline.is_some_and(|deadline| motion::now() >= deadline) { + if req.deadline.is_some_and(|deadline| self.instant() >= deadline) { let _ = req.reply.send(json!({"ok": false, "error": craft::automation::control::expired_error()})); continue; } @@ -79,8 +79,9 @@ impl VectorcraftApp { } }; let document = self.document_identity(); + let now = self.instant(); if let Some(channel) = self.channel.as_mut() { - channel.settle(req, reply, document, settle); + channel.settle_at(req, reply, document, settle, now); } } // A capture is issued only after passes settle it, and an idle window runs none; a hidden @@ -99,9 +100,10 @@ impl VectorcraftApp { /// due, answering those that expired or lost their window meanwhile. pub(crate) fn issue_captures(&mut self, ctx: &egui::Context) { let document = self.document_identity(); + let now = self.instant(); let finished = self.channel.as_mut().map_or_else(Vec::new, |channel| { channel.captures.note_pass(); - channel.captures.issue(&WindowHost::of(ctx), motion::now(), document) + channel.captures.issue(&WindowHost::of(ctx), now, document) }); self.finish_captures(finished); } diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 240a24edb2..90e1ab5fa0 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -359,6 +359,10 @@ pub struct VectorcraftApp { /// leaves this `None`). #[cfg(test)] pub(crate) now_override: Option, + /// The instant `now_override`'s zero stands for, so [`Self::instant`] reads the same test clock + /// as an [`craft::ui::runtime::motion::Instant`]. + #[cfg(test)] + pub(crate) clock_origin: craft::ui::runtime::motion::Instant, /// An error the next tool choice returns before the engine sees it ([`Self::try_select_tool`]), /// so tests can drive a failed switch (production has no such field). #[cfg(test)] diff --git a/crates/ui-egui/src/tests/automation.rs b/crates/ui-egui/src/tests/automation.rs index d8cc9f9d04..ac619939fe 100644 --- a/crates/ui-egui/src/tests/automation.rs +++ b/crates/ui-egui/src/tests/automation.rs @@ -1105,10 +1105,17 @@ fn a_screenshot_reply_hides_paths_like_any_reply() { /// `ui.screenshot` is answered from the app's own frames: once the request settles, a kept pass sends /// the window's screenshot command, and the image the window gives back is written where the request -/// asked. +/// asked. The settle delay and the capture's deadline count on the app's clock, which the test steps +/// one frame at a time, so a slow machine does not reach the deadline. #[test] fn a_screenshot_is_issued_by_the_apps_frames_and_written() { + use craft::automation::screenshot::DEADLINE; let (mut app, tx, base) = confined("screenshot-frames"); + // The app's clock starts past the wall clock by twice the deadline: a capture timed on the wall + // clock anywhere on its path is answered as expired, or never comes due. + let start = 2.0 * DEADLINE.as_secs_f64(); + let frame = 1.0 / 60.0; + app.now_override = Some(start); let ctx = app.ui_ctx.clone().unwrap(); let size = egui::vec2(800.0, 600.0); let raw = || { @@ -1121,11 +1128,13 @@ fn a_screenshot_is_issued_by_the_apps_frames_and_written() { let path = base.join("out/shot.png"); let (req, reply) = ControlRequest::new("ui.screenshot", json!({"path": s(path.clone()), "focus": false})); tx.send(req).unwrap(); - // The settle delay runs on the wall clock: frames come until it is over, as an idle window's do. The - // request's own deadline (`craft::automation::screenshot::DEADLINE`) answers it with an error when no - // frame issues it in time, so the loop ends on the reply. + // Frames come until the settle delay is over, as an idle window's do, each a frame's time later on + // the app's clock. The capture's deadline is never reached on that clock while it is issued in time. let callback = loop { + let now = app.now_override.unwrap_or(start); + assert!(now - start < DEADLINE.as_secs_f64(), "no frame issued the screenshot before its deadline"); let out = crate::testing::app_frame(&mut app, &ctx, raw()); + app.now_override = Some(now + frame); let sent = out.viewport_output.get(&egui::ViewportId::ROOT).and_then(|root| { root.commands.iter().find_map(|c| if let egui::ViewportCommand::Screenshot(callback) = c { Some(callback.clone()) } else { None }) });