diff --git a/crates/egui_kittest/src/lib.rs b/crates/egui_kittest/src/lib.rs index 6adefe53..ae4d42f4 100644 --- a/crates/egui_kittest/src/lib.rs +++ b/crates/egui_kittest/src/lib.rs @@ -28,8 +28,9 @@ pub use builder::*; pub use node::*; pub use renderer::*; +use egui::epaint::{ClippedShape, RectShape}; use egui::style::ScrollAnimation; -use egui::{Key, Modifiers, Pos2, Rect, RepaintCause, Vec2, ViewportId}; +use egui::{Color32, Key, Modifiers, Pos2, Rect, RepaintCause, Shape, Vec2, ViewportId}; use kittest::Queryable; #[derive(Debug, Clone)] @@ -556,6 +557,18 @@ impl<'a, State> Harness<'a, State> { self.key_combination_modifiers(modifiers, &[key]); } + /// Mask something. Useful for snapshot tests. + /// + /// Call this _after_ [`Self::run`] and before [`Self::snapshot`]. + /// This will add a [`RectShape`] to the output shapes, for the current frame. + /// Will be overwritten on the next call to [`Self::run`]. + pub fn mask(&mut self, rect: Rect) { + self.output.shapes.push(ClippedShape { + clip_rect: Rect::EVERYTHING, + shape: Shape::Rect(RectShape::filled(rect, 0.0, Color32::MAGENTA)), + }); + } + /// Render the last output to an image. /// /// # Errors diff --git a/crates/egui_kittest/tests/snapshots/test_masking.png b/crates/egui_kittest/tests/snapshots/test_masking.png new file mode 100644 index 00000000..932e9d5e --- /dev/null +++ b/crates/egui_kittest/tests/snapshots/test_masking.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cdb0c0b955e3d3773a00afa61d4c44999de447aa71dd737181563101f09f5ac9 +size 5444 diff --git a/crates/egui_kittest/tests/tests.rs b/crates/egui_kittest/tests/tests.rs index 8ff6584a..e18e2176 100644 --- a/crates/egui_kittest/tests/tests.rs +++ b/crates/egui_kittest/tests/tests.rs @@ -138,3 +138,24 @@ fn test_scroll_down() { "The button was not clicked after scrolling down. (Probably not scrolled enough / at all)" ); } + +#[test] +fn test_masking() { + let mut harness = Harness::new_ui(|ui| { + let timestamp = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_millis(); + + ui.label("I should not be masked."); + ui.label(format!("Timestamp: {timestamp}")); + ui.label("I should also not be masked."); + }); + + harness.fit_contents(); + + let to_be_masked = harness.get_by_label_contains("Timestamp: "); + harness.mask(to_be_masked.rect()); + + harness.snapshot("test_masking"); +}