From 27414eae8a39183eda2b1d5f72cf5b1df6e4b053 Mon Sep 17 00:00:00 2001 From: Lucas Meurer Date: Sat, 12 Oct 2024 13:53:33 +0200 Subject: [PATCH] Add comment about threshold and for the SnapshotError variants --- crates/egui_kittest/src/lib.rs | 2 ++ crates/egui_kittest/src/snapshot.rs | 28 ++++++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/crates/egui_kittest/src/lib.rs b/crates/egui_kittest/src/lib.rs index fb5b4f7fb9a..f65dcecc031 100644 --- a/crates/egui_kittest/src/lib.rs +++ b/crates/egui_kittest/src/lib.rs @@ -56,6 +56,8 @@ impl<'a> Harness<'a> { let viewport = input.viewports.get_mut(&ViewportId::ROOT).unwrap(); viewport.native_pixels_per_point = Some(builder.dpi); + // We need to run egui for a single frame so that the AccessKit state can be initialized + // and users can immediately start querying for widgets. let mut output = ctx.run(input.clone(), &mut app); Self { diff --git a/crates/egui_kittest/src/snapshot.rs b/crates/egui_kittest/src/snapshot.rs index be061c05dda..9f7cf2a2637 100644 --- a/crates/egui_kittest/src/snapshot.rs +++ b/crates/egui_kittest/src/snapshot.rs @@ -5,16 +5,26 @@ use std::path::{Path, PathBuf}; #[derive(Debug)] pub enum SnapshotError { + /// Image did not match snapshot Diff { + /// Count of pixels that were different diff: i32, + /// Path where the diff image was saved diff_path: PathBuf, }, + /// Error opening the existing snapshot (it probably doesn't exist, check the + /// [`ImageError`] for more information) OpenSnapshot { + /// Path where the snapshot was expected to be path: PathBuf, - err: image::ImageError, + /// The error that occurred + err: ImageError, }, + /// The size of the image did not match the snapshot SizeMismatch { + /// Expected size expected: (u32, u32), + /// Actual size actual: (u32, u32), }, } @@ -82,7 +92,21 @@ pub fn try_image_snapshot(current: &image::RgbaImage, name: &str) -> Result<(), }); } - let result = dify::diff::get_results(previous, current.clone(), 0.1, true, None, &None, &None); + // Looking at dify's source code, the threshold is based on the distance between two colors in + // YIQ color space. + // The default is 0.1, but we'll try 0.0 because ideally the output should not change at all. + // We might have to increase the threshold if there are minor differences when running tests + // on different gpus or different backends. + let threshold = 0.0; + let result = dify::diff::get_results( + previous, + current.clone(), + threshold, + true, + None, + &None, + &None, + ); if let Some((diff, result_image)) = result { result_image.save(diff_path.clone()).unwrap();