Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions crates/egui_kittest/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ pub struct HarnessBuilder<State = ()> {
pub(crate) state: PhantomData<State>,
pub(crate) renderer: Box<dyn TestRenderer>,
pub(crate) wait_for_pending_images: bool,

#[cfg(feature = "snapshot")]
pub(crate) default_snapshot_options: crate::SnapshotOptions,
}

impl<State> Default for HarnessBuilder<State> {
Expand All @@ -28,6 +31,9 @@ impl<State> Default for HarnessBuilder<State> {
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(),
}
}
}
Expand Down Expand Up @@ -56,6 +62,12 @@ impl<State> HarnessBuilder<State> {
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,
Expand Down
9 changes: 9 additions & 0 deletions crates/egui_kittest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<State> Debug for Harness<'_, State> {
Expand All @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
19 changes: 18 additions & 1 deletion crates/egui_kittest/src/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -556,9 +557,17 @@ pub fn image_snapshot(current: &image::RgbaImage, name: impl Into<String>) {

#[cfg(any(feature = "wgpu", feature = "snapshot"))]
impl<State> 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.
Expand Down Expand Up @@ -586,6 +595,9 @@ impl<State> 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`.
Expand All @@ -597,12 +609,14 @@ impl<State> 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.
Expand All @@ -629,6 +643,9 @@ impl<State> 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`.
Expand Down