Skip to content

Commit

Permalink
Add snapshot tests for all demos
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmerlin committed Sep 25, 2024
1 parent 233ee9f commit 4463e4e
Show file tree
Hide file tree
Showing 29 changed files with 168 additions and 7 deletions.
55 changes: 55 additions & 0 deletions crates/egui_demo_lib/src/demo/demo_app_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
3 changes: 3 additions & 0 deletions crates/egui_demo_lib/tests/snapshots/Bézier Curve.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions crates/egui_demo_lib/tests/snapshots/Code Editor.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions crates/egui_demo_lib/tests/snapshots/Code Example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions crates/egui_demo_lib/tests/snapshots/Context Menus.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions crates/egui_demo_lib/tests/snapshots/Dancing Strings.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions crates/egui_demo_lib/tests/snapshots/Drag and Drop.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions crates/egui_demo_lib/tests/snapshots/Extra Viewport.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions crates/egui_demo_lib/tests/snapshots/Font Book.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions crates/egui_demo_lib/tests/snapshots/Frame.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions crates/egui_demo_lib/tests/snapshots/Highlighting.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions crates/egui_demo_lib/tests/snapshots/Misc Demos.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions crates/egui_demo_lib/tests/snapshots/Multi Touch.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions crates/egui_demo_lib/tests/snapshots/Painting.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions crates/egui_demo_lib/tests/snapshots/Pan Zoom.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions crates/egui_demo_lib/tests/snapshots/Panels.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions crates/egui_demo_lib/tests/snapshots/Scrolling.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions crates/egui_demo_lib/tests/snapshots/Sliders.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions crates/egui_demo_lib/tests/snapshots/Strip.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions crates/egui_demo_lib/tests/snapshots/Table.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions crates/egui_demo_lib/tests/snapshots/Text Layout.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions crates/egui_demo_lib/tests/snapshots/TextEdit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions crates/egui_demo_lib/tests/snapshots/Tooltips.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions crates/egui_demo_lib/tests/snapshots/Undo Redo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions crates/egui_demo_lib/tests/snapshots/Widget Gallery.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions crates/egui_demo_lib/tests/snapshots/Window Options.png
2 changes: 2 additions & 0 deletions crates/egui_kittest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
40 changes: 33 additions & 7 deletions crates/egui_kittest/src/snapshot.rs
Original file line number Diff line number Diff line change
@@ -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();

Expand All @@ -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()
Expand All @@ -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
);
}
}
}

0 comments on commit 4463e4e

Please sign in to comment.