Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion crates/egui_kittest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions crates/egui_kittest/tests/snapshots/test_masking.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions crates/egui_kittest/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Loading