Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 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 Down Expand Up @@ -147,6 +150,9 @@ impl<'a, State> Harness<'a, State> {
step_dt,
wait_for_pending_images,
queued_events: Default::default(),

#[cfg(feature = "snapshot")]
default_snapshot_options: SnapshotOptions::default(),
};
// Run the harness until it is stable, ensuring that all Areas are shown and animations are done
harness.run_ok();
Expand Down
23 changes: 22 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,21 @@ 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.
pub fn options(&self) -> &SnapshotOptions {
&self.default_snapshot_options
}

/// Set the default options used for subsequent snapshot tests.
pub fn set_options(&mut self, options: SnapshotOptions) {
self.default_snapshot_options = 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 [`Self::set_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 +599,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 [`Self::set_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 +613,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 [`Self::set_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 +647,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 [`Self::set_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
Loading