diff --git a/crates/egui_kittest/src/builder.rs b/crates/egui_kittest/src/builder.rs index 75ebd54b215..cc686914ff1 100644 --- a/crates/egui_kittest/src/builder.rs +++ b/crates/egui_kittest/src/builder.rs @@ -14,6 +14,9 @@ pub struct HarnessBuilder { pub(crate) state: PhantomData, pub(crate) renderer: Box, pub(crate) wait_for_pending_images: bool, + + #[cfg(feature = "snapshot")] + pub(crate) default_snapshot_options: crate::SnapshotOptions, } impl Default for HarnessBuilder { @@ -28,6 +31,9 @@ impl Default for HarnessBuilder { step_dt: 1.0 / 4.0, wait_for_pending_images: true, os: egui::os::OperatingSystem::Nix, + + #[cfg(feature = "snapshot")] + default_snapshot_options: crate::SnapshotOptions::default(), } } } @@ -56,6 +62,12 @@ impl HarnessBuilder { self } + /// Set the default options used for snapshot tests on this harness. + #[cfg(feature = "snapshot")] + pub fn with_options(&mut self, options: crate::SnapshotOptions) { + self.default_snapshot_options = options; + } + /// Override the [`egui::os::OperatingSystem`] reported to egui. /// /// This affects e.g. the way shortcuts are displayed. So for snapshot tests, diff --git a/crates/egui_kittest/src/lib.rs b/crates/egui_kittest/src/lib.rs index e331119e329..c8112f47bbc 100644 --- a/crates/egui_kittest/src/lib.rs +++ b/crates/egui_kittest/src/lib.rs @@ -75,6 +75,9 @@ pub struct Harness<'a, State = ()> { step_dt: f32, wait_for_pending_images: bool, queued_events: EventQueue, + + #[cfg(feature = "snapshot")] + default_snapshot_options: SnapshotOptions, } impl Debug for Harness<'_, State> { @@ -100,6 +103,9 @@ impl<'a, State> Harness<'a, State> { state: _, mut renderer, wait_for_pending_images, + + #[cfg(feature = "snapshot")] + default_snapshot_options, } = builder; let ctx = ctx.unwrap_or_default(); ctx.set_theme(theme); @@ -147,6 +153,9 @@ impl<'a, State> Harness<'a, State> { step_dt, wait_for_pending_images, queued_events: Default::default(), + + #[cfg(feature = "snapshot")] + default_snapshot_options, }; // Run the harness until it is stable, ensuring that all Areas are shown and animations are done harness.run_ok(); diff --git a/crates/egui_kittest/src/snapshot.rs b/crates/egui_kittest/src/snapshot.rs index c115332060b..f6511c451a0 100644 --- a/crates/egui_kittest/src/snapshot.rs +++ b/crates/egui_kittest/src/snapshot.rs @@ -7,6 +7,7 @@ use std::path::PathBuf; pub type SnapshotResult = Result<(), SnapshotError>; #[non_exhaustive] +#[derive(Clone, Debug)] pub struct SnapshotOptions { /// The threshold for the image comparison. /// The default is `0.6` (which is enough for most egui tests to pass across different @@ -556,9 +557,17 @@ pub fn image_snapshot(current: &image::RgbaImage, name: impl Into) { #[cfg(any(feature = "wgpu", feature = "snapshot"))] impl Harness<'_, State> { + /// The default options used for snapshot tests. + /// set by [`crate::HarnessBuilder::with_options`]. + pub fn options(&self) -> &SnapshotOptions { + &self.default_snapshot_options + } + /// Render an image using the setup [`crate::TestRenderer`] and compare it to the snapshot /// with custom options. /// + /// These options will override the ones set by [`crate::HarnessBuilder::with_options`]. + /// /// If you want to change the default options for your whole project, you could create an /// [extension trait](http://xion.io/post/code/rust-extension-traits.html) to create a /// new `my_image_snapshot` function on the Harness that calls this function with the desired options. @@ -586,6 +595,9 @@ impl Harness<'_, State> { } /// Render an image using the setup [`crate::TestRenderer`] and compare it to the snapshot. + /// + /// This is like [`Self::try_snapshot_options`] but will use the options set by [`crate::HarnessBuilder::with_options`]. + /// /// The snapshot will be saved under `tests/snapshots/{name}.png`. /// The new image from the last test run will be saved under `tests/snapshots/{name}.new.png`. /// If the new image didn't match the snapshot, a diff image will be saved under `tests/snapshots/{name}.diff.png`. @@ -597,12 +609,14 @@ impl Harness<'_, State> { let image = self .render() .map_err(|err| SnapshotError::RenderError { err })?; - try_image_snapshot(&image, name) + try_image_snapshot_options(&image, name.into(), &self.default_snapshot_options) } /// Render an image using the setup [`crate::TestRenderer`] and compare it to the snapshot /// with custom options. /// + /// These options will override the ones set by [`crate::HarnessBuilder::with_options`]. + /// /// If you want to change the default options for your whole project, you could create an /// [extension trait](http://xion.io/post/code/rust-extension-traits.html) to create a /// new `my_image_snapshot` function on the Harness that calls this function with the desired options. @@ -629,6 +643,9 @@ impl Harness<'_, State> { } /// Render an image using the setup [`crate::TestRenderer`] and compare it to the snapshot. + /// + /// This is like [`Self::snapshot_options`] but will use the options set by [`crate::HarnessBuilder::with_options`]. + /// /// The snapshot will be saved under `tests/snapshots/{name}.png`. /// The new image from the last test run will be saved under `tests/snapshots/{name}.new.png`. /// If the new image didn't match the snapshot, a diff image will be saved under `tests/snapshots/{name}.diff.png`.