diff --git a/crates/egui_kittest/src/lib.rs b/crates/egui_kittest/src/lib.rs index 60d689c1..ebeba65a 100644 --- a/crates/egui_kittest/src/lib.rs +++ b/crates/egui_kittest/src/lib.rs @@ -460,11 +460,15 @@ impl<'a, State> Harness<'a, State> { &mut self.state } - fn event(&self, event: egui::Event) { + /// Queue an event to be processed in the next frame. + pub fn event(&self, event: egui::Event) { self.queued_events.lock().push(EventType::Event(event)); } - fn event_modifiers(&self, event: egui::Event, modifiers: Modifiers) { + /// Queue an event with modifiers. + /// + /// Queues the modifiers to be pressed, then the event, then the modifiers to be released. + pub fn event_modifiers(&self, event: egui::Event, modifiers: Modifiers) { let mut queue = self.queued_events.lock(); queue.push(EventType::Modifiers(modifiers)); queue.push(EventType::Event(event)); @@ -584,6 +588,16 @@ impl<'a, State> Harness<'a, State> { self.key_combination_modifiers(modifiers, &[key]); } + /// Remove the cursor from the screen. + /// + /// Will fire a [`egui::Event::PointerGone`] event. + /// + /// If you click a button and then take a snapshot, the button will be shown as hovered. + /// If you don't want that, you can call this method after clicking. + pub fn remove_cursor(&self) { + self.event(egui::Event::PointerGone); + } + /// Mask something. Useful for snapshot tests. /// /// Call this _after_ [`Self::run`] and before [`Self::snapshot`]. diff --git a/crates/egui_kittest/tests/tests.rs b/crates/egui_kittest/tests/tests.rs index 04f02ba8..81f86145 100644 --- a/crates/egui_kittest/tests/tests.rs +++ b/crates/egui_kittest/tests/tests.rs @@ -181,3 +181,36 @@ fn test_masking() { harness.snapshot("test_masking"); } + +#[test] +fn test_remove_cursor() { + let hovered = false; + let mut harness = Harness::new_ui_state( + |ui, state| { + let response = ui.button("Click me"); + *state = response.hovered(); + }, + hovered, + ); + + harness.fit_contents(); + + harness.get_by_label("Click me").click(); + harness.run(); + + assert!(harness.state(), "The button should be hovered"); + let hovered_button_snapshot = harness.render().expect("Failed to render"); + + harness.remove_cursor(); + harness.run(); + assert!( + !harness.state(), + "The button should not be hovered after removing cursor" + ); + + let non_hovered_button_snapshot = harness.render().expect("Failed to render"); + assert_ne!( + hovered_button_snapshot, non_hovered_button_snapshot, + "The button appearance should change" + ); +}