From 841edbbf4232b79cb40b8b5b94b1c4fc7178b7e8 Mon Sep 17 00:00:00 2001 From: Lucas Meurer Date: Sun, 29 Dec 2024 19:37:52 +0100 Subject: [PATCH] Small fixes and improvements --- crates/eframe/src/web/web_painter_wgpu.rs | 2 +- crates/egui-wgpu/src/lib.rs | 5 +++- crates/egui_demo_app/tests/test_demo_app.rs | 4 +-- .../src/demo/demo_app_windows.rs | 2 +- crates/egui_demo_lib/src/demo/modals.rs | 8 +++--- .../egui_demo_lib/src/demo/widget_gallery.rs | 2 +- crates/egui_demo_lib/src/rendering_test.rs | 2 +- crates/egui_kittest/README.md | 4 +-- crates/egui_kittest/src/app_kind.rs | 4 +-- crates/egui_kittest/src/builder.rs | 23 +++++++-------- crates/egui_kittest/src/lib.rs | 8 ++---- crates/egui_kittest/src/renderer.rs | 12 +++----- crates/egui_kittest/src/snapshot.rs | 28 +++++++++---------- crates/egui_kittest/src/wgpu.rs | 12 ++++---- crates/egui_kittest/tests/tests.rs | 2 +- 15 files changed, 57 insertions(+), 61 deletions(-) diff --git a/crates/eframe/src/web/web_painter_wgpu.rs b/crates/eframe/src/web/web_painter_wgpu.rs index 591d4224d3bd..5e217180ce3e 100644 --- a/crates/eframe/src/web/web_painter_wgpu.rs +++ b/crates/eframe/src/web/web_painter_wgpu.rs @@ -115,7 +115,7 @@ impl WebPainterWgpu { let render_state = RenderState::create( &options.wgpu_options, &instance, - &surface, + Some(&surface), depth_format, 1, options.dithering, diff --git a/crates/egui-wgpu/src/lib.rs b/crates/egui-wgpu/src/lib.rs index f10426827235..6eb81e8b0b37 100644 --- a/crates/egui-wgpu/src/lib.rs +++ b/crates/egui-wgpu/src/lib.rs @@ -188,7 +188,10 @@ impl RenderState { let capabilities = { profiling::scope!("get_capabilities"); - surface.map_or_else(|| vec![TextureFormat::Rgba8Unorm], |s| s.get_capabilities(&adapter).formats) + surface.map_or_else( + || vec![TextureFormat::Rgba8Unorm], + |s| s.get_capabilities(&adapter).formats, + ) }; let target_format = crate::preferred_framebuffer_format(&capabilities)?; diff --git a/crates/egui_demo_app/tests/test_demo_app.rs b/crates/egui_demo_app/tests/test_demo_app.rs index 0c513dd91522..b242dad43166 100644 --- a/crates/egui_demo_app/tests/test_demo_app.rs +++ b/crates/egui_demo_app/tests/test_demo_app.rs @@ -1,9 +1,7 @@ -use eframe::egui_wgpu::WgpuSetup; use egui::accesskit::Role; use egui::Vec2; use egui_demo_app::{Anchor, WrapApp}; use egui_kittest::kittest::Queryable; -use std::sync::Arc; #[test] fn test_demo_app() { @@ -69,7 +67,7 @@ fn test_demo_app() { harness.run(); - if let Err(e) = harness.try_wgpu_snapshot(&anchor.to_string()) { + if let Err(e) = harness.try_snapshot(&anchor.to_string()) { results.push(e); } } 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 7e4891e1f78c..9cf01e9eac6c 100644 --- a/crates/egui_demo_lib/src/demo/demo_app_windows.rs +++ b/crates/egui_demo_lib/src/demo/demo_app_windows.rs @@ -405,7 +405,7 @@ mod tests { options.threshold = 2.1; } - let result = harness.try_wgpu_snapshot_options(&format!("demos/{name}"), &options); + let result = harness.try_snapshot_options(&format!("demos/{name}"), &options); if let Err(err) = result { errors.push(err.to_string()); } diff --git a/crates/egui_demo_lib/src/demo/modals.rs b/crates/egui_demo_lib/src/demo/modals.rs index 989101b4d751..8d22c0cc0089 100644 --- a/crates/egui_demo_lib/src/demo/modals.rs +++ b/crates/egui_demo_lib/src/demo/modals.rs @@ -235,21 +235,21 @@ mod tests { let mut results = Vec::new(); harness.run(); - results.push(harness.try_wgpu_snapshot("modals_1")); + results.push(harness.try_snapshot("modals_1")); harness.get_by_label("Save").click(); // TODO(lucasmerlin): Remove these extra runs once run checks for repaint requests harness.run(); harness.run(); harness.run(); - results.push(harness.try_wgpu_snapshot("modals_2")); + results.push(harness.try_snapshot("modals_2")); harness.get_by_label("Yes Please").click(); // TODO(lucasmerlin): Remove these extra runs once run checks for repaint requests harness.run(); harness.run(); harness.run(); - results.push(harness.try_wgpu_snapshot("modals_3")); + results.push(harness.try_snapshot("modals_3")); for result in results { result.unwrap(); @@ -282,6 +282,6 @@ mod tests { harness.run(); // This snapshots should show the progress bar modal on top of the save modal. - harness.wgpu_snapshot("modals_backdrop_should_prevent_focusing_lower_area"); + harness.snapshot("modals_backdrop_should_prevent_focusing_lower_area"); } } diff --git a/crates/egui_demo_lib/src/demo/widget_gallery.rs b/crates/egui_demo_lib/src/demo/widget_gallery.rs index d473d4b9d0e3..cfe746899cb8 100644 --- a/crates/egui_demo_lib/src/demo/widget_gallery.rs +++ b/crates/egui_demo_lib/src/demo/widget_gallery.rs @@ -307,6 +307,6 @@ mod tests { harness.fit_contents(); - harness.wgpu_snapshot("widget_gallery"); + harness.snapshot("widget_gallery"); } } diff --git a/crates/egui_demo_lib/src/rendering_test.rs b/crates/egui_demo_lib/src/rendering_test.rs index 011ca4736231..9d4c7985f2de 100644 --- a/crates/egui_demo_lib/src/rendering_test.rs +++ b/crates/egui_demo_lib/src/rendering_test.rs @@ -703,7 +703,7 @@ mod tests { harness.fit_contents(); - let result = harness.try_wgpu_snapshot(&format!("rendering_test/dpi_{dpi:.2}")); + let result = harness.try_snapshot(&format!("rendering_test/dpi_{dpi:.2}")); if let Err(err) = result { errors.push(err); } diff --git a/crates/egui_kittest/README.md b/crates/egui_kittest/README.md index c124fac3a622..99fe9be6543b 100644 --- a/crates/egui_kittest/README.md +++ b/crates/egui_kittest/README.md @@ -29,13 +29,13 @@ fn main() { // You can even render the ui and do image snapshot tests #[cfg(all(feature = "wgpu", feature = "snapshot"))] - harness.wgpu_snapshot("readme_example"); + harness.snapshot("readme_example"); } ``` ## Snapshot testing There is a snapshot testing feature. To create snapshot tests, enable the `snapshot` and `wgpu` features. -Once enabled, you can call `Harness::wgpu_snapshot` to render the ui and save the image to the `tests/snapshots` directory. +Once enabled, you can call `Harness::snapshot` to render the ui and save the image to the `tests/snapshots` directory. To update the snapshots, run your tests with `UPDATE_SNAPSHOTS=true`, so e.g. `UPDATE_SNAPSHOTS=true cargo test`. Running with `UPDATE_SNAPSHOTS=true` will still cause the tests to fail, but on the next run, the tests should pass. diff --git a/crates/egui_kittest/src/app_kind.rs b/crates/egui_kittest/src/app_kind.rs index a854f45c6f0a..3b5cf9e734c6 100644 --- a/crates/egui_kittest/src/app_kind.rs +++ b/crates/egui_kittest/src/app_kind.rs @@ -5,10 +5,10 @@ type AppKindUiState<'a, State> = Box; type AppKindContext<'a> = Box; type AppKindUi<'a> = Box; -/// In order to access the [eframe::App] trait from the generic [State], we store a function pointer +/// In order to access the [`eframe::App`] trait from the generic `State`, we store a function pointer /// here that will return the dyn trait from the struct. In the builder we have the correct where /// clause to be able to create this. -/// Later we can use it anywhere to get the [eframe::App] from the [State]. +/// Later we can use it anywhere to get the [`eframe::App`] from the `State`. #[cfg(feature = "eframe")] type AppKindEframe<'a, State> = (fn(&mut State) -> &mut dyn eframe::App, eframe::Frame); diff --git a/crates/egui_kittest/src/builder.rs b/crates/egui_kittest/src/builder.rs index 68f1c8c11fb2..d0c219bb156a 100644 --- a/crates/egui_kittest/src/builder.rs +++ b/crates/egui_kittest/src/builder.rs @@ -1,8 +1,8 @@ use crate::app_kind::AppKind; +use crate::wgpu::WgpuTestRenderer; use crate::{Harness, LazyRenderer, TestRenderer}; use egui::{Pos2, Rect, Vec2}; use std::marker::PhantomData; -use crate::wgpu::WgpuTestRenderer; /// Builder for [`Harness`]. pub struct HarnessBuilder { @@ -40,20 +40,26 @@ impl HarnessBuilder { self } + /// Set the [`TestRenderer`] to use for rendering. + /// + /// By default, a [`LazyRenderer`] is used. + #[inline] pub fn renderer(mut self, renderer: impl TestRenderer + 'static) -> Self { self.renderer = Box::new(renderer); self } /// Enable wgpu rendering with a default setup suitable for testing. + /// + /// This sets up a [`WgpuTestRenderer`] with the default setup. #[cfg(feature = "wgpu")] - pub fn wgpu(mut self) -> Self { + pub fn wgpu(self) -> Self { self.renderer(WgpuTestRenderer::default()) } /// Enable wgpu rendering with the given setup. #[cfg(feature = "wgpu")] - pub fn wgpu_setup(mut self, setup: egui_wgpu::WgpuSetup) -> Self { + pub fn wgpu_setup(self, setup: egui_wgpu::WgpuSetup) -> Self { self.renderer(WgpuTestRenderer::from_setup(setup)) } @@ -86,12 +92,7 @@ impl HarnessBuilder { app: impl FnMut(&egui::Context, &mut State) + 'a, state: State, ) -> Harness<'a, State> { - Harness::from_builder( - self, - AppKind::ContextState(Box::new(app)), - state, - None, - ) + Harness::from_builder(self, AppKind::ContextState(Box::new(app)), state, None) } /// Create a new Harness with the given ui closure and a state. @@ -124,10 +125,10 @@ impl HarnessBuilder { } /// Create a new [Harness] from the given eframe creation closure. - /// The app can be accessed via the [Harness::state] / [Harness::state_mut] methods. + /// The app can be accessed via the [`Harness::state`] / [`Harness::state_mut`] methods. #[cfg(feature = "eframe")] pub fn build_eframe<'a>( - mut self, + self, build: impl FnOnce(&mut eframe::CreationContext<'a>) -> State, ) -> Harness<'a, State> where diff --git a/crates/egui_kittest/src/lib.rs b/crates/egui_kittest/src/lib.rs index acc1d198d975..5725e867ef31 100644 --- a/crates/egui_kittest/src/lib.rs +++ b/crates/egui_kittest/src/lib.rs @@ -12,22 +12,20 @@ mod snapshot; pub use snapshot::*; use std::fmt::{Debug, Formatter}; mod app_kind; +mod renderer; #[cfg(feature = "wgpu")] mod texture_to_image; #[cfg(feature = "wgpu")] pub mod wgpu; -mod renderer; pub use kittest; -use std::mem; use crate::app_kind::AppKind; use crate::event::EventState; pub use builder::*; -pub use renderer::*; -use egui::{Modifiers, Pos2, Rect, TexturesDelta, Vec2, ViewportId}; +use egui::{Modifiers, Pos2, Rect, Vec2, ViewportId}; use kittest::{Node, Queryable}; -use egui_wgpu::RenderState; +pub use renderer::*; /// The test Harness. This contains everything needed to run the test. /// Create a new Harness using [`Harness::new`] or [`Harness::builder`]. diff --git a/crates/egui_kittest/src/renderer.rs b/crates/egui_kittest/src/renderer.rs index 14c786159bf0..7bcaace581cb 100644 --- a/crates/egui_kittest/src/renderer.rs +++ b/crates/egui_kittest/src/renderer.rs @@ -2,22 +2,18 @@ use egui::{Context, FullOutput, TexturesDelta}; use image::RgbaImage; pub trait TestRenderer { - /// We use this to pass the glow / wgpu render state to [eframe::Frame]. + /// We use this to pass the glow / wgpu render state to [`eframe::Frame`]. #[cfg(feature = "eframe")] - fn setup_eframe(&self, cc: &mut eframe::CreationContext<'_>, frame: &mut eframe::Frame) {} + fn setup_eframe(&self, _cc: &mut eframe::CreationContext<'_>, _frame: &mut eframe::Frame) {} /// Handle a [`TexturesDelta`] by updating the renderer's textures. fn handle_delta(&mut self, delta: &TexturesDelta); - /// Render the [`Harness`] and return the resulting image. + /// Render the [`crate::Harness`] and return the resulting image. /// /// # Errors /// Returns an error if the rendering fails. - fn render( - &mut self, - ctx: &egui::Context, - output: &egui::FullOutput, - ) -> Result; + fn render(&mut self, ctx: &Context, output: &FullOutput) -> Result; } pub enum LazyRenderer { diff --git a/crates/egui_kittest/src/snapshot.rs b/crates/egui_kittest/src/snapshot.rs index 6ef1f2893b77..9682a0db776e 100644 --- a/crates/egui_kittest/src/snapshot.rs +++ b/crates/egui_kittest/src/snapshot.rs @@ -319,7 +319,7 @@ pub fn image_snapshot(current: &image::RgbaImage, name: &str) { #[cfg(feature = "wgpu")] impl Harness<'_, State> { - /// Render a image using a default [`crate::wgpu::TestRenderer`] and compare it to the snapshot + /// Render a image using the setup [`crate::TestRenderer`] and compare it to the snapshot /// with custom options. /// /// If you want to change the default options for your whole project, you could create an @@ -327,7 +327,7 @@ impl Harness<'_, State> { /// new `my_image_snapshot` function on the Harness that calls this function with the desired options. /// You could additionally use the /// [disallowed_methods](https://rust-lang.github.io/rust-clippy/master/#disallowed_methods) - /// lint to disable use of the [`Harness::wgpu_snapshot`] to prevent accidentally using the wrong defaults. + /// lint to disable use of the [`Harness::snapshot`] to prevent accidentally using the wrong defaults. /// /// The snapshot files will be saved under [`SnapshotOptions::output_path`]. /// The snapshot will be saved under `{output_path}/{name}.png`. @@ -335,9 +335,9 @@ impl Harness<'_, State> { /// If new image didn't match the snapshot, a diff image will be saved under `{output_path}/{name}.diff.png`. /// /// # Errors - /// Returns a [`SnapshotError`] if the image does not match the snapshot, if there was an + /// Returns a [`SnapshotError`] if the image does not match the snapshot, if there was an /// error reading or writing the snapshot, if the rendering fails or if no default renderer is available. - pub fn try_wgpu_snapshot_options( + pub fn try_snapshot_options( &mut self, name: &str, options: &SnapshotOptions, @@ -348,22 +348,22 @@ impl Harness<'_, State> { try_image_snapshot_options(&image, name, options) } - /// Render a image using a default [`crate::wgpu::TestRenderer`] and compare it to the snapshot. + /// Render a image using the setup [`crate::TestRenderer`] and compare it to the snapshot. /// 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 new image didn't match the snapshot, a diff image will be saved under `tests/snapshots/{name}.diff.png`. /// /// # Errors - /// Returns a [`SnapshotError`] if the image does not match the snapshot, if there was an + /// Returns a [`SnapshotError`] if the image does not match the snapshot, if there was an /// error reading or writing the snapshot, if the rendering fails or if no default renderer is available. - pub fn try_wgpu_snapshot(&mut self, name: &str) -> Result<(), SnapshotError> { + pub fn try_snapshot(&mut self, name: &str) -> Result<(), SnapshotError> { let image = self .render() .map_err(|err| SnapshotError::RenderError { err })?; try_image_snapshot(&image, name) } - /// Render a image using a default [`crate::wgpu::TestRenderer`] and compare it to the snapshot + /// Render a image using the setup [`crate::TestRenderer`] and compare it to the snapshot /// with custom options. /// /// If you want to change the default options for your whole project, you could create an @@ -371,7 +371,7 @@ impl Harness<'_, State> { /// new `my_image_snapshot` function on the Harness that calls this function with the desired options. /// You could additionally use the /// [disallowed_methods](https://rust-lang.github.io/rust-clippy/master/#disallowed_methods) - /// lint to disable use of the [`Harness::wgpu_snapshot`] to prevent accidentally using the wrong defaults. + /// lint to disable use of the [`Harness::snapshot`] to prevent accidentally using the wrong defaults. /// /// The snapshot files will be saved under [`SnapshotOptions::output_path`]. /// The snapshot will be saved under `{output_path}/{name}.png`. @@ -382,8 +382,8 @@ impl Harness<'_, State> { /// Panics if the image does not match the snapshot, if there was an error reading or writing the /// snapshot, if the rendering fails or if no default renderer is available. #[track_caller] - pub fn wgpu_snapshot_options(&mut self, name: &str, options: &SnapshotOptions) { - match self.try_wgpu_snapshot_options(name, options) { + pub fn snapshot_options(&mut self, name: &str, options: &SnapshotOptions) { + match self.try_snapshot_options(name, options) { Ok(_) => {} Err(err) => { panic!("{}", err); @@ -391,7 +391,7 @@ impl Harness<'_, State> { } } - /// Render a image using a default [`crate::TestRenderer`] and compare it to the snapshot. + /// Render a image using the setup [`crate::TestRenderer`] and compare it to the snapshot. /// 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 new image didn't match the snapshot, a diff image will be saved under `tests/snapshots/{name}.diff.png`. @@ -400,8 +400,8 @@ impl Harness<'_, State> { /// Panics if the image does not match the snapshot, if there was an error reading or writing the /// snapshot, if the rendering fails or if no default renderer is available. #[track_caller] - pub fn wgpu_snapshot(&mut self, name: &str) { - match self.try_wgpu_snapshot(name) { + pub fn snapshot(&mut self, name: &str) { + match self.try_snapshot(name) { Ok(_) => {} Err(err) => { panic!("{}", err); diff --git a/crates/egui_kittest/src/wgpu.rs b/crates/egui_kittest/src/wgpu.rs index 3003c3fe204e..4f1481cc5361 100644 --- a/crates/egui_kittest/src/wgpu.rs +++ b/crates/egui_kittest/src/wgpu.rs @@ -1,17 +1,17 @@ use crate::texture_to_image::texture_to_image; +use egui::TexturesDelta; use egui_wgpu::wgpu::{Backends, StoreOp, TextureFormat}; use egui_wgpu::{wgpu, RenderState, ScreenDescriptor, WgpuSetup}; use image::RgbaImage; use std::iter::once; use std::sync::Arc; use wgpu::Maintain; -use egui::TexturesDelta; -// TODO: Replace this with the setup from https://github.com/emilk/egui/pull/5506 +// TODO(#5506): Replace this with the setup from https://github.com/emilk/egui/pull/5506 pub fn default_wgpu_setup() -> egui_wgpu::WgpuSetup { egui_wgpu::WgpuSetup::CreateNew { supported_backends: Backends::all(), - device_descriptor: Arc::new(|a| wgpu::DeviceDescriptor::default()), + device_descriptor: Arc::new(|_| wgpu::DeviceDescriptor::default()), power_preference: wgpu::PowerPreference::default(), } } @@ -19,7 +19,7 @@ pub fn default_wgpu_setup() -> egui_wgpu::WgpuSetup { pub(crate) fn create_render_state(setup: WgpuSetup) -> egui_wgpu::RenderState { let instance = match &setup { WgpuSetup::Existing { instance, .. } => instance.clone(), - _ => Default::default(), + WgpuSetup::CreateNew { .. } => Default::default(), }; pollster::block_on(egui_wgpu::RenderState::create( @@ -36,7 +36,7 @@ pub(crate) fn create_render_state(setup: WgpuSetup) -> egui_wgpu::RenderState { .expect("Failed to create render state") } -/// Utility to render snapshots from a [`Harness`] using [`egui_wgpu`]. +/// Utility to render snapshots from a [`crate::Harness`] using [`egui_wgpu`]. pub struct WgpuTestRenderer { render_state: RenderState, } @@ -82,7 +82,7 @@ impl crate::TestRenderer for WgpuTestRenderer { } } - /// Render the [`Harness`] and return the resulting image. + /// Render the [`crate::Harness`] and return the resulting image. fn render( &mut self, ctx: &egui::Context, diff --git a/crates/egui_kittest/tests/tests.rs b/crates/egui_kittest/tests/tests.rs index 6799b9a35675..bf7d0bdb2206 100644 --- a/crates/egui_kittest/tests/tests.rs +++ b/crates/egui_kittest/tests/tests.rs @@ -11,5 +11,5 @@ fn test_shrink() { harness.fit_contents(); #[cfg(all(feature = "snapshot", feature = "wgpu"))] - harness.wgpu_snapshot("test_shrink"); + harness.snapshot("test_shrink"); }