Skip to content

Commit

Permalink
Add more convenient snapshot api, allow reusing the TestRenderer and …
Browse files Browse the repository at this point in the history
…disable dithering
  • Loading branch information
lucasmerlin committed Oct 12, 2024
1 parent c9d1bea commit 2b1fafa
Show file tree
Hide file tree
Showing 31 changed files with 112 additions and 81 deletions.
12 changes: 2 additions & 10 deletions crates/egui_demo_lib/src/demo/demo_app_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,6 @@ mod tests {
use crate::demo::demo_app_windows::Demos;
use egui::Vec2;
use egui_kittest::kittest::Queryable;
use egui_kittest::try_image_snapshot;
use egui_kittest::wgpu::TestRenderer;
use egui_kittest::Harness;

#[test]
Expand All @@ -409,23 +407,17 @@ mod tests {
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();

let window = harness.node().children().next().unwrap();
// TODO(lucasmerlin): Windows should probably have a label?
//let window = harness.get_by_name(name);

let size = window.raw_bounds().expect("window bounds").size();
harness.set_size(Vec2::new(size.width as f32, size.height as f32));

// We need to run the app for some more frames...
harness.run();
// Run the app for some more frames...
harness.run();

let image = TestRenderer::new().render(&harness);
let result = try_image_snapshot(&image, &format!("demos/{name}"));
let result = harness.try_wgpu_snapshot(&format!("demos/{name}"));
if let Err(err) = result {
errors.push(err);
}
Expand Down
11 changes: 2 additions & 9 deletions crates/egui_demo_lib/src/demo/widget_gallery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,6 @@ mod tests {
use super::*;
use crate::View;
use egui::{CentralPanel, Context, Vec2};
use egui_kittest::image_snapshot;
use egui_kittest::wgpu::TestRenderer;
use egui_kittest::Harness;

#[test]
Expand All @@ -307,16 +305,11 @@ mod tests {
demo.ui(ui);
});
};
let mut harness = Harness::builder()
let harness = Harness::builder()
.with_size(Vec2::new(380.0, 550.0))
.with_dpi(2.0)
.build(app);

// The first and second frames are slightly different, so we take the second frame
harness.run();

let image = TestRenderer::new().render(&harness);

image_snapshot(&image, "widget_gallery");
harness.wgpu_snapshot("widget_gallery");
}
}
4 changes: 2 additions & 2 deletions crates/egui_demo_lib/tests/snapshots/demos/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.
4 changes: 2 additions & 2 deletions crates/egui_demo_lib/tests/snapshots/demos/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.
4 changes: 2 additions & 2 deletions crates/egui_demo_lib/tests/snapshots/demos/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.
4 changes: 2 additions & 2 deletions crates/egui_demo_lib/tests/snapshots/demos/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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions crates/egui_demo_lib/tests/snapshots/demos/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.
4 changes: 2 additions & 2 deletions crates/egui_demo_lib/tests/snapshots/demos/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.
4 changes: 2 additions & 2 deletions crates/egui_demo_lib/tests/snapshots/demos/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.
4 changes: 2 additions & 2 deletions crates/egui_demo_lib/tests/snapshots/demos/Frame.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions crates/egui_demo_lib/tests/snapshots/demos/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.
4 changes: 2 additions & 2 deletions crates/egui_demo_lib/tests/snapshots/demos/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.
4 changes: 2 additions & 2 deletions crates/egui_demo_lib/tests/snapshots/demos/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.
4 changes: 2 additions & 2 deletions crates/egui_demo_lib/tests/snapshots/demos/Painting.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions crates/egui_demo_lib/tests/snapshots/demos/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.
4 changes: 2 additions & 2 deletions crates/egui_demo_lib/tests/snapshots/demos/Panels.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions crates/egui_demo_lib/tests/snapshots/demos/Scrolling.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions crates/egui_demo_lib/tests/snapshots/demos/Sliders.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions crates/egui_demo_lib/tests/snapshots/demos/Strip.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions crates/egui_demo_lib/tests/snapshots/demos/Table.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions crates/egui_demo_lib/tests/snapshots/demos/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.
4 changes: 2 additions & 2 deletions crates/egui_demo_lib/tests/snapshots/demos/TextEdit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions crates/egui_demo_lib/tests/snapshots/demos/Tooltips.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions crates/egui_demo_lib/tests/snapshots/demos/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.
4 changes: 2 additions & 2 deletions crates/egui_demo_lib/tests/snapshots/demos/Window Options.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions crates/egui_demo_lib/tests/snapshots/widget_gallery.png
2 changes: 1 addition & 1 deletion crates/egui_kittest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ fn main() {

// You can even render the ui and do image snapshot tests
#[cfg(all(feature = "wgpu", feature = "snapshot"))]
egui_kittest::image_snapshot(&egui_kittest::wgpu::TestRenderer::new().render(&harness), "readme_example");
harness.wgpu_snapshot("readme_example");
}
```
36 changes: 34 additions & 2 deletions crates/egui_kittest/src/snapshot.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::Harness;
use image::ImageError;
use std::fmt::Display;
use std::io::ErrorKind;
Expand Down Expand Up @@ -74,7 +75,8 @@ impl Display for SnapshotError {
/// Image snapshot test.
///
/// # Errors
/// Returns a [`SnapshotError`] if the image does not match the snapshot.
/// Returns a [`SnapshotError`] if the image does not match the snapshot or if there was an error
/// reading or writing the snapshot.
pub fn try_image_snapshot(current: &image::RgbaImage, name: &str) -> Result<(), SnapshotError> {
let snapshots_path = Path::new("tests/snapshots");

Expand Down Expand Up @@ -163,7 +165,8 @@ fn maybe_update_snapshot(
/// Image snapshot test.
///
/// # Panics
/// Panics if the image does not match the snapshot.
/// Panics if the image does not match the snapshot or if there was an error reading or writing the
/// snapshot.
#[track_caller]
pub fn image_snapshot(current: &image::RgbaImage, name: &str) {
match try_image_snapshot(current, name) {
Expand All @@ -173,3 +176,32 @@ pub fn image_snapshot(current: &image::RgbaImage, name: &str) {
}
}
}

#[cfg(feature = "wgpu")]
impl Harness<'_> {
/// Render a image using a default [`crate::wgpu::TestRenderer`] and compare it to the snapshot.
///
/// # Errors
/// Returns a [`SnapshotError`] if the image does not match the snapshot or if there was an error
/// reading or writing the snapshot.
#[track_caller]
pub fn try_wgpu_snapshot(&self, name: &str) -> Result<(), SnapshotError> {
let image = crate::wgpu::TestRenderer::new().render(self);
try_image_snapshot(&image, name)
}

/// Render a image using a default [`crate::wgpu::TestRenderer`] and compare it to the snapshot.
///
/// # Panics
/// Panics if the image does not match the snapshot or if there was an error reading or writing the
/// snapshot.
#[track_caller]
pub fn wgpu_snapshot(&self, name: &str) {
match self.try_wgpu_snapshot(name) {
Ok(_) => {}
Err(err) => {
panic!("{}", err);
}
}
}
}
28 changes: 21 additions & 7 deletions crates/egui_kittest/src/wgpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use std::iter::once;
use wgpu::Maintain;

pub struct TestRenderer {
renderer: egui_wgpu::Renderer,
device: wgpu::Device,
queue: wgpu::Queue,
dithering: bool,
}

impl Default for TestRenderer {
Expand All @@ -36,20 +36,34 @@ impl TestRenderer {
))
.expect("Failed to create device");

let renderer = egui_wgpu::Renderer::new(&device, TextureFormat::Rgba8Unorm, None, 1, true);
Self::create(device, queue)
}

pub fn create(device: wgpu::Device, queue: wgpu::Queue) -> Self {
Self {
renderer,
device,
queue,
dithering: false,
}
}

pub fn with_dithering(mut self, dithering: bool) -> Self {
self.dithering = dithering;
self
}

pub fn render(&mut self, harness: &Harness<'_>) -> RgbaImage {
let mut renderer = egui_wgpu::Renderer::new(
&self.device,
TextureFormat::Rgba8Unorm,
None,
1,
self.dithering,
);

for delta in &harness.texture_deltas {
for (id, image_delta) in &delta.set {
self.renderer
.update_texture(&self.device, &self.queue, *id, image_delta);
renderer.update_texture(&self.device, &self.queue, *id, image_delta);
}
}

Expand All @@ -70,7 +84,7 @@ impl TestRenderer {
harness.ctx.pixels_per_point(),
);

let user_buffers = self.renderer.update_buffers(
let user_buffers = renderer.update_buffers(
&self.device,
&self.queue,
&mut encoder,
Expand Down Expand Up @@ -113,7 +127,7 @@ impl TestRenderer {
})
.forget_lifetime();

self.renderer.render(&mut pass, &tessellated, &screen);
renderer.render(&mut pass, &tessellated, &screen);
}

self.queue
Expand Down

0 comments on commit 2b1fafa

Please sign in to comment.