Skip to content

Commit

Permalink
Add Harness::run_ok and use it instead of try_run().ok()
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmerlin committed Jan 6, 2025
1 parent c841e08 commit 6013a23
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
2 changes: 1 addition & 1 deletion crates/egui_demo_lib/src/demo/demo_app_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ mod tests {
harness.set_size(Vec2::new(size.width as f32, size.height as f32));

// Run the app for some more frames...
harness.try_run().ok();
harness.run_ok();

let mut options = SnapshotOptions::default();
// The Bézier Curve demo needs a threshold of 2.1 to pass on linux
Expand Down
13 changes: 7 additions & 6 deletions crates/egui_demo_lib/src/demo/modals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,13 @@ mod tests {

harness.get_by_role(Role::ComboBox).click();

harness.try_run().ok();
// Harness::run would fail because we keep requesting repaints to simulate progress.
harness.run_ok();
assert!(harness.ctx.memory(|mem| mem.any_popup_open()));
assert!(harness.state().user_modal_open);

harness.press_key(Key::Escape);
harness.try_run().ok();
harness.run_ok();
assert!(!harness.ctx.memory(|mem| mem.any_popup_open()));
assert!(harness.state().user_modal_open);
}
Expand Down Expand Up @@ -238,11 +239,11 @@ mod tests {
results.push(harness.try_snapshot("modals_1"));

harness.get_by_label("Save").click();
harness.run_steps(3);
harness.run_ok();
results.push(harness.try_snapshot("modals_2"));

harness.get_by_label("Yes Please").click();
harness.run_steps(3);
harness.run_ok();
results.push(harness.try_snapshot("modals_3"));

for result in results {
Expand All @@ -266,11 +267,11 @@ mod tests {
initial_state,
);

harness.run_steps(3);
harness.run_ok();

harness.get_by_label("Yes Please").simulate_click();

harness.run_steps(2);
harness.run_ok();

// This snapshots should show the progress bar modal on top of the save modal.
harness.snapshot("modals_backdrop_should_prevent_focusing_lower_area");
Expand Down
24 changes: 21 additions & 3 deletions crates/egui_kittest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl<'a, State> Harness<'a, State> {
step_dt,
};
// Run the harness until it is stable, ensuring that all Areas are shown and animations are done
harness.try_run().ok();
harness.run_ok();
harness
}

Expand Down Expand Up @@ -259,7 +259,7 @@ impl<'a, State> Harness<'a, State> {
if let Some(response) = &self.response {
self.set_size(response.rect.size());
}
self.try_run().ok();
self.run_ok();
}

/// Run until
Expand All @@ -274,6 +274,7 @@ impl<'a, State> Harness<'a, State> {
///
/// See also:
/// - [`Harness::try_run`].
/// - [`Harness::run_ok`].
/// - [`Harness::step`].
/// - [`Harness::run_steps`].
#[track_caller]
Expand All @@ -289,7 +290,7 @@ impl<'a, State> Harness<'a, State> {
/// Run until
/// - all animations are done
/// - no more repaints are requested
/// - the maximum number of steps is reached
/// - the maximum number of steps is reached (See [`HarnessBuilder::with_max_steps`])
///
/// Returns the number of steps that were run.
///
Expand All @@ -298,6 +299,7 @@ impl<'a, State> Harness<'a, State> {
///
/// See also:
/// - [`Harness::run`].
/// - [`Harness::run_ok`].
/// - [`Harness::step`].
/// - [`Harness::run_steps`].
pub fn try_run(&mut self) -> Result<u64, ExceededMaxStepsError> {
Expand All @@ -323,6 +325,22 @@ impl<'a, State> Harness<'a, State> {
Ok(steps)
}

/// Run until
/// - all animations are done
/// - no more repaints are requested
/// - the maximum number of steps is reached (See [`HarnessBuilder::with_max_steps`])
///
/// Returns the number of steps that were run, or None if the maximum number of steps was exceeded.
///
/// See also:
/// - [`Harness::run`].
/// - [`Harness::try_run`].
/// - [`Harness::step`].
/// - [`Harness::run_steps`].
pub fn run_ok(&mut self) -> Option<u64> {
self.try_run().ok()
}

/// Run a number of steps.
/// Equivalent to calling [`Harness::step`] x times.
pub fn run_steps(&mut self, steps: usize) {
Expand Down

0 comments on commit 6013a23

Please sign in to comment.