Allow masking widgets in kittest snapshots (#7467)

* Closes <https://github.com/emilk/egui/issues/THE_RELEVANT_ISSUE>
* [ ] I have followed the instructions in the PR template
This commit is contained in:
Lucas Meurer 2025-08-21 17:46:36 +02:00 committed by GitHub
parent bf981b8d3e
commit 42c2fc58c9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 38 additions and 1 deletions

View File

@ -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

View File

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:cdb0c0b955e3d3773a00afa61d4c44999de447aa71dd737181563101f09f5ac9
size 5444

View File

@ -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");
}