From 4463e4e2973b80264448d91168118bc60e7e5284 Mon Sep 17 00:00:00 2001 From: Lucas Meurer Date: Thu, 26 Sep 2024 00:24:24 +0200 Subject: [PATCH] Add snapshot tests for all demos --- .../src/demo/demo_app_windows.rs | 55 +++++++++++++++++++ .../tests/snapshots/B\303\251zier Curve.png" | 3 + .../tests/snapshots/Code Editor.png | 3 + .../tests/snapshots/Code Example.png | 3 + .../tests/snapshots/Context Menus.png | 3 + .../tests/snapshots/Dancing Strings.png | 3 + .../tests/snapshots/Drag and Drop.png | 3 + .../tests/snapshots/Extra Viewport.png | 3 + .../tests/snapshots/Font Book.png | 3 + .../egui_demo_lib/tests/snapshots/Frame.png | 3 + .../tests/snapshots/Highlighting.png | 3 + .../tests/snapshots/Interactive Container.png | 3 + .../tests/snapshots/Misc Demos.png | 3 + .../tests/snapshots/Multi Touch.png | 3 + .../tests/snapshots/Painting.png | 3 + .../tests/snapshots/Pan Zoom.png | 3 + .../egui_demo_lib/tests/snapshots/Panels.png | 3 + .../tests/snapshots/Scrolling.png | 3 + .../egui_demo_lib/tests/snapshots/Sliders.png | 3 + .../egui_demo_lib/tests/snapshots/Strip.png | 3 + .../egui_demo_lib/tests/snapshots/Table.png | 3 + .../tests/snapshots/Text Layout.png | 3 + .../tests/snapshots/TextEdit.png | 3 + .../tests/snapshots/Tooltips.png | 3 + .../tests/snapshots/Undo Redo.png | 3 + .../tests/snapshots/Widget Gallery.png | 3 + .../tests/snapshots/Window Options.png | 3 + crates/egui_kittest/src/lib.rs | 2 + crates/egui_kittest/src/snapshot.rs | 40 +++++++++++--- 29 files changed, 168 insertions(+), 7 deletions(-) create mode 100644 "crates/egui_demo_lib/tests/snapshots/B\303\251zier Curve.png" create mode 100644 crates/egui_demo_lib/tests/snapshots/Code Editor.png create mode 100644 crates/egui_demo_lib/tests/snapshots/Code Example.png create mode 100644 crates/egui_demo_lib/tests/snapshots/Context Menus.png create mode 100644 crates/egui_demo_lib/tests/snapshots/Dancing Strings.png create mode 100644 crates/egui_demo_lib/tests/snapshots/Drag and Drop.png create mode 100644 crates/egui_demo_lib/tests/snapshots/Extra Viewport.png create mode 100644 crates/egui_demo_lib/tests/snapshots/Font Book.png create mode 100644 crates/egui_demo_lib/tests/snapshots/Frame.png create mode 100644 crates/egui_demo_lib/tests/snapshots/Highlighting.png create mode 100644 crates/egui_demo_lib/tests/snapshots/Interactive Container.png create mode 100644 crates/egui_demo_lib/tests/snapshots/Misc Demos.png create mode 100644 crates/egui_demo_lib/tests/snapshots/Multi Touch.png create mode 100644 crates/egui_demo_lib/tests/snapshots/Painting.png create mode 100644 crates/egui_demo_lib/tests/snapshots/Pan Zoom.png create mode 100644 crates/egui_demo_lib/tests/snapshots/Panels.png create mode 100644 crates/egui_demo_lib/tests/snapshots/Scrolling.png create mode 100644 crates/egui_demo_lib/tests/snapshots/Sliders.png create mode 100644 crates/egui_demo_lib/tests/snapshots/Strip.png create mode 100644 crates/egui_demo_lib/tests/snapshots/Table.png create mode 100644 crates/egui_demo_lib/tests/snapshots/Text Layout.png create mode 100644 crates/egui_demo_lib/tests/snapshots/TextEdit.png create mode 100644 crates/egui_demo_lib/tests/snapshots/Tooltips.png create mode 100644 crates/egui_demo_lib/tests/snapshots/Undo Redo.png create mode 100644 crates/egui_demo_lib/tests/snapshots/Widget Gallery.png create mode 100644 crates/egui_demo_lib/tests/snapshots/Window Options.png diff --git a/crates/egui_demo_lib/src/demo/demo_app_windows.rs b/crates/egui_demo_lib/src/demo/demo_app_windows.rs index d55cc6aff3f..0b1a74ce8a0 100644 --- a/crates/egui_demo_lib/src/demo/demo_app_windows.rs +++ b/crates/egui_demo_lib/src/demo/demo_app_windows.rs @@ -377,3 +377,58 @@ fn file_menu_button(ui: &mut Ui) { } }); } + +#[cfg(test)] +mod tests { + use crate::demo::demo_app_windows::Demos; + use egui::Vec2; + use egui_kittest::kittest::Queryable; + use egui_kittest::snapshot::try_image_snapshot; + use egui_kittest::wgpu::TestRenderer; + use egui_kittest::Harness; + + #[test] + fn demos_should_match_snapshot() { + let demos = Demos::default(); + + let mut errors = Vec::new(); + + for mut demo in demos.demos { + let name = demo + .name() + .split_once(" ") + .map(|(_, name)| name) + .unwrap_or(demo.name()); + + let mut harness = Harness::new(|ctx| { + demo.show(ctx, &mut true); + }); + + // We need to run the app for multiple frames before all windows have the right size + harness.run(); + harness.run(); + harness.run(); + + let window = harness.node().children().next().unwrap(); + // TODO: Windows should probably have a label? + //let window = harness.get_by_name(name); + + let size = window.raw_bounds().expect("window bounds").size(); + harness = harness.with_size(Vec2::new(size.width as f32, size.height as f32)); + + // We need to run the app for some more frames... + harness.run(); + harness.run(); + + let image = TestRenderer::new().render(&harness); + let result = try_image_snapshot(image, &format!("{}", name)); + if let Err(err) = result { + errors.push(err); + } + } + + if !errors.is_empty() { + panic!("Errors: {:#?}", errors); + } + } +} diff --git "a/crates/egui_demo_lib/tests/snapshots/B\303\251zier Curve.png" "b/crates/egui_demo_lib/tests/snapshots/B\303\251zier Curve.png" new file mode 100644 index 00000000000..73a3fd5afe6 --- /dev/null +++ "b/crates/egui_demo_lib/tests/snapshots/B\303\251zier Curve.png" @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:baa595e6d1f67e09bb9303b925ae7e8bfcb1095869dedd15980f2da553286a51 +size 153229 diff --git a/crates/egui_demo_lib/tests/snapshots/Code Editor.png b/crates/egui_demo_lib/tests/snapshots/Code Editor.png new file mode 100644 index 00000000000..a472ee0ded8 --- /dev/null +++ b/crates/egui_demo_lib/tests/snapshots/Code Editor.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4116ee3996fab18e591e8f82023f9a3aa6c7c8f4081cf33b34596f952f637e4c +size 88807 diff --git a/crates/egui_demo_lib/tests/snapshots/Code Example.png b/crates/egui_demo_lib/tests/snapshots/Code Example.png new file mode 100644 index 00000000000..23eaa8fcf42 --- /dev/null +++ b/crates/egui_demo_lib/tests/snapshots/Code Example.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9a2a0b05229f926cf60dea21a37a9980af4a8aac8fba32a948143d5d78e55ed3 +size 97752 diff --git a/crates/egui_demo_lib/tests/snapshots/Context Menus.png b/crates/egui_demo_lib/tests/snapshots/Context Menus.png new file mode 100644 index 00000000000..54dd2634a4a --- /dev/null +++ b/crates/egui_demo_lib/tests/snapshots/Context Menus.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d2077c0047e00e61983090268768a298bda1a6d3ae12312f58bdbe401725d03c +size 25151 diff --git a/crates/egui_demo_lib/tests/snapshots/Dancing Strings.png b/crates/egui_demo_lib/tests/snapshots/Dancing Strings.png new file mode 100644 index 00000000000..048bbdcfe4c --- /dev/null +++ b/crates/egui_demo_lib/tests/snapshots/Dancing Strings.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d6dad3634f4792d04351e2146e17f6aec3736d2ca11a6d3f524acba501114fe0 +size 144784 diff --git a/crates/egui_demo_lib/tests/snapshots/Drag and Drop.png b/crates/egui_demo_lib/tests/snapshots/Drag and Drop.png new file mode 100644 index 00000000000..c78be16618c --- /dev/null +++ b/crates/egui_demo_lib/tests/snapshots/Drag and Drop.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d9466eaad8b642daf4b1ec07e9311294aacd8384c2ea657c24157b4a5efa59e4 +size 57546 diff --git a/crates/egui_demo_lib/tests/snapshots/Extra Viewport.png b/crates/egui_demo_lib/tests/snapshots/Extra Viewport.png new file mode 100644 index 00000000000..53f5dfe1896 --- /dev/null +++ b/crates/egui_demo_lib/tests/snapshots/Extra Viewport.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:dbd2677a131cc8b1d4b2ebac547bcd907375dcdf90d26245be8a2f121bf5d74f +size 20333 diff --git a/crates/egui_demo_lib/tests/snapshots/Font Book.png b/crates/egui_demo_lib/tests/snapshots/Font Book.png new file mode 100644 index 00000000000..97b9f11c229 --- /dev/null +++ b/crates/egui_demo_lib/tests/snapshots/Font Book.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9e8731835a1ece9f3e5a97ba40b297c68a0d9a4061319e67878e7b6fe200ad24 +size 153264 diff --git a/crates/egui_demo_lib/tests/snapshots/Frame.png b/crates/egui_demo_lib/tests/snapshots/Frame.png new file mode 100644 index 00000000000..12277d7e5ba --- /dev/null +++ b/crates/egui_demo_lib/tests/snapshots/Frame.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cdae677c8366af17eeb38445b5babee544bfc5070abaa7cfb48e42f41824fa56 +size 51884 diff --git a/crates/egui_demo_lib/tests/snapshots/Highlighting.png b/crates/egui_demo_lib/tests/snapshots/Highlighting.png new file mode 100644 index 00000000000..2e3df16ed60 --- /dev/null +++ b/crates/egui_demo_lib/tests/snapshots/Highlighting.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5534494c0b8372639e9d24acc5f5c1e15ef5987a0820ca1e244c22e74e0f18bf +size 31102 diff --git a/crates/egui_demo_lib/tests/snapshots/Interactive Container.png b/crates/egui_demo_lib/tests/snapshots/Interactive Container.png new file mode 100644 index 00000000000..fa4fda5c871 --- /dev/null +++ b/crates/egui_demo_lib/tests/snapshots/Interactive Container.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bfc85be4e113fb986157435d7acfb3381a56b21921317224abe649b4da36e798 +size 36819 diff --git a/crates/egui_demo_lib/tests/snapshots/Misc Demos.png b/crates/egui_demo_lib/tests/snapshots/Misc Demos.png new file mode 100644 index 00000000000..39074b780bd --- /dev/null +++ b/crates/egui_demo_lib/tests/snapshots/Misc Demos.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e5ecc6c377e16bec5228859c839b661f456db224b4b329f4bae932ffff1624f7 +size 75710 diff --git a/crates/egui_demo_lib/tests/snapshots/Multi Touch.png b/crates/egui_demo_lib/tests/snapshots/Multi Touch.png new file mode 100644 index 00000000000..1c47a0c41c1 --- /dev/null +++ b/crates/egui_demo_lib/tests/snapshots/Multi Touch.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9b74dcf0b119e03b231a4a288639c035021324bb9d8014c1f5228956d14f58b1 +size 324143 diff --git a/crates/egui_demo_lib/tests/snapshots/Painting.png b/crates/egui_demo_lib/tests/snapshots/Painting.png new file mode 100644 index 00000000000..0f1c6d36246 --- /dev/null +++ b/crates/egui_demo_lib/tests/snapshots/Painting.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:912b1d3c06864261d3a084de6e7cee9464841b6d54fd74699fb1541a39298bba +size 323025 diff --git a/crates/egui_demo_lib/tests/snapshots/Pan Zoom.png b/crates/egui_demo_lib/tests/snapshots/Pan Zoom.png new file mode 100644 index 00000000000..84a8362c4de --- /dev/null +++ b/crates/egui_demo_lib/tests/snapshots/Pan Zoom.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8ebe157e8a32125aaa6691d7fb231b1c557f2b30c261205579a605c25720bb73 +size 39762 diff --git a/crates/egui_demo_lib/tests/snapshots/Panels.png b/crates/egui_demo_lib/tests/snapshots/Panels.png new file mode 100644 index 00000000000..6e6d658970c --- /dev/null +++ b/crates/egui_demo_lib/tests/snapshots/Panels.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:394837e1331cf3b0640d21201a121080fa78f3290d8f1a942104e63fee68fe4e +size 456632 diff --git a/crates/egui_demo_lib/tests/snapshots/Scrolling.png b/crates/egui_demo_lib/tests/snapshots/Scrolling.png new file mode 100644 index 00000000000..28154c31919 --- /dev/null +++ b/crates/egui_demo_lib/tests/snapshots/Scrolling.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:05e081870e934880dd1efcd479cfb5a07d5ccc40b4c773e1458b88c50b654a01 +size 210058 diff --git a/crates/egui_demo_lib/tests/snapshots/Sliders.png b/crates/egui_demo_lib/tests/snapshots/Sliders.png new file mode 100644 index 00000000000..240fac5b5aa --- /dev/null +++ b/crates/egui_demo_lib/tests/snapshots/Sliders.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:78a8fbe91614e4ea481cf2b773a7e881a3b64becdcad162ed3383af2d8a30960 +size 138692 diff --git a/crates/egui_demo_lib/tests/snapshots/Strip.png b/crates/egui_demo_lib/tests/snapshots/Strip.png new file mode 100644 index 00000000000..8edd42a1fff --- /dev/null +++ b/crates/egui_demo_lib/tests/snapshots/Strip.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3eb0943dd207a3ae8c20f503a5046351d1268d612e8a2230a199c70971ec0b58 +size 123211 diff --git a/crates/egui_demo_lib/tests/snapshots/Table.png b/crates/egui_demo_lib/tests/snapshots/Table.png new file mode 100644 index 00000000000..194d0a33a88 --- /dev/null +++ b/crates/egui_demo_lib/tests/snapshots/Table.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7268ac64bfd6128f8f4ee5aee1d80a2612536550f3158f895d39af39b5f07cb0 +size 95381 diff --git a/crates/egui_demo_lib/tests/snapshots/Text Layout.png b/crates/egui_demo_lib/tests/snapshots/Text Layout.png new file mode 100644 index 00000000000..d0875f7db84 --- /dev/null +++ b/crates/egui_demo_lib/tests/snapshots/Text Layout.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4cd97103a9b536779fe7b86e940e82f0574bf774f6883f7d38c38dc880b4cddf +size 83206 diff --git a/crates/egui_demo_lib/tests/snapshots/TextEdit.png b/crates/egui_demo_lib/tests/snapshots/TextEdit.png new file mode 100644 index 00000000000..ceeb5082b3d --- /dev/null +++ b/crates/egui_demo_lib/tests/snapshots/TextEdit.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7bd317d11a0f7e7b548736b93e24f421d18061394f1dcacfa0ab2774551109b7 +size 53625 diff --git a/crates/egui_demo_lib/tests/snapshots/Tooltips.png b/crates/egui_demo_lib/tests/snapshots/Tooltips.png new file mode 100644 index 00000000000..1749b834d1f --- /dev/null +++ b/crates/egui_demo_lib/tests/snapshots/Tooltips.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b12673d58ff153449777baf01d183ab73a311c74f82c79c6c292f05752b72b92 +size 195954 diff --git a/crates/egui_demo_lib/tests/snapshots/Undo Redo.png b/crates/egui_demo_lib/tests/snapshots/Undo Redo.png new file mode 100644 index 00000000000..c441c93d104 --- /dev/null +++ b/crates/egui_demo_lib/tests/snapshots/Undo Redo.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:96f65efbb8a5a4a33454a7bf5f2a679cce078df9405c18207dddbcaace5a7553 +size 31041 diff --git a/crates/egui_demo_lib/tests/snapshots/Widget Gallery.png b/crates/egui_demo_lib/tests/snapshots/Widget Gallery.png new file mode 100644 index 00000000000..f1b29751af8 --- /dev/null +++ b/crates/egui_demo_lib/tests/snapshots/Widget Gallery.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8e376aaa97574d8c0fca3b331a39dcaadbc463ef06026512e71e93d559539d9d +size 101513 diff --git a/crates/egui_demo_lib/tests/snapshots/Window Options.png b/crates/egui_demo_lib/tests/snapshots/Window Options.png new file mode 100644 index 00000000000..c37be4216ed --- /dev/null +++ b/crates/egui_demo_lib/tests/snapshots/Window Options.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:71d56fa2d6ee4a7cfba57d1a186e1496046c4cb7509d7bb0d20d6446360bc93c +size 59316 diff --git a/crates/egui_kittest/src/lib.rs b/crates/egui_kittest/src/lib.rs index a29cb9924db..19aeb31a69d 100644 --- a/crates/egui_kittest/src/lib.rs +++ b/crates/egui_kittest/src/lib.rs @@ -6,6 +6,8 @@ mod utils; #[cfg(feature = "wgpu")] pub mod wgpu; +pub use kittest; + use crate::utils::egui_vec2; pub use accesskit_consumer; use egui::accesskit::NodeId; diff --git a/crates/egui_kittest/src/snapshot.rs b/crates/egui_kittest/src/snapshot.rs index ff76fe69c33..289cdca1f4d 100644 --- a/crates/egui_kittest/src/snapshot.rs +++ b/crates/egui_kittest/src/snapshot.rs @@ -1,11 +1,23 @@ -pub fn image_snapshot(current: image::RgbaImage, name: &str) { +use std::path::{Path, PathBuf}; + +#[derive(Debug)] +pub struct SnapshotError { + pub diff: i32, + pub diff_path: PathBuf, +} + +pub fn try_image_snapshot(current: image::RgbaImage, name: &str) -> Result<(), SnapshotError> { let current = dify_image::RgbaImage::from_raw(current.width(), current.height(), current.into_raw()) .unwrap(); - let path = format!("tests/snapshots/{name}.png"); - let diff_path = format!("tests/snapshots/{name}.diff.png"); - let current_path = format!("tests/snapshots/{name}.new.png"); + let snapshots_path = Path::new("tests/snapshots"); + + let path = snapshots_path.join(format!("{name}.png")); + std::fs::create_dir_all(path.parent().unwrap()).ok(); + + let diff_path = snapshots_path.join(format!("{name}.diff.png")); + let current_path = snapshots_path.join(format!("{name}.new.png")); std::fs::create_dir_all("tests/snapshots").ok(); @@ -15,7 +27,7 @@ pub fn image_snapshot(current: image::RgbaImage, name: &str) { Ok(image) => image.to_rgba8(), Err(err) => { println!("Error opening image: {err}"); - println!("Saving current image as {path}"); + println!("Saving current image as {path:?}"); current.save(&path).unwrap(); current.clone() @@ -29,12 +41,26 @@ pub fn image_snapshot(current: image::RgbaImage, name: &str) { if std::env::var("UPDATE_SNAPSHOTS").is_ok() { current.save(&path).unwrap(); - println!("Updated snapshot: {path}"); + println!("Updated snapshot: {path:?}"); } else { - panic!("Image did not match snapshot. Diff: {diff}, {diff_path}"); + return Err(SnapshotError { diff, diff_path }); } } else { // Delete old diff if it exists std::fs::remove_file(diff_path).ok(); } + + Ok(()) +} + +pub fn image_snapshot(current: image::RgbaImage, name: &str) { + match try_image_snapshot(current, name) { + Ok(_) => {} + Err(err) => { + panic!( + "{name} failed. Image did not match snapshot. Diff: {}, {:?}", + err.diff, err.diff_path + ); + } + } }