mirror of
https://github.com/emilk/egui.git
synced 2026-08-29 04:40:03 -04:00
Add ImageLoader::has_pending and wait_for_pending_images (#7030)
With kittest it was difficult to wait for images to be loaded before taking a snapshot test. This PR adds `Harness::with_wait_for_pending_images` (true by default) which will cause `Harness::run` to sleep until all images are loaded (or `HarnessBuilder::with_max_steps` is exceeded). It also adds a new ImageLoader::has_pending and BytesLoader::has_pending, which should be implemented if things are loaded / decoded asynchronously. It reverts https://github.com/emilk/egui/pull/6901 which was my previous attempt to fix this (but this didn't work since only the tested crate is compiled with cfg(test) and not it's dependencies)
This commit is contained in:
@@ -31,6 +31,7 @@ pub use renderer::*;
|
||||
use egui::{Modifiers, Pos2, Rect, RepaintCause, Vec2, ViewportId};
|
||||
use kittest::{Node, Queryable};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ExceededMaxStepsError {
|
||||
pub max_steps: u64,
|
||||
pub repaint_causes: Vec<RepaintCause>,
|
||||
@@ -66,6 +67,7 @@ pub struct Harness<'a, State = ()> {
|
||||
renderer: Box<dyn TestRenderer>,
|
||||
max_steps: u64,
|
||||
step_dt: f32,
|
||||
wait_for_pending_images: bool,
|
||||
}
|
||||
|
||||
impl<State> Debug for Harness<'_, State> {
|
||||
@@ -88,6 +90,7 @@ impl<'a, State> Harness<'a, State> {
|
||||
step_dt,
|
||||
state: _,
|
||||
mut renderer,
|
||||
wait_for_pending_images,
|
||||
} = builder;
|
||||
let ctx = ctx.unwrap_or_default();
|
||||
ctx.enable_accesskit();
|
||||
@@ -128,6 +131,7 @@ impl<'a, State> Harness<'a, State> {
|
||||
renderer,
|
||||
max_steps,
|
||||
step_dt,
|
||||
wait_for_pending_images,
|
||||
};
|
||||
// Run the harness until it is stable, ensuring that all Areas are shown and animations are done
|
||||
harness.run_ok();
|
||||
@@ -293,10 +297,13 @@ impl<'a, State> Harness<'a, State> {
|
||||
loop {
|
||||
steps += 1;
|
||||
self.step();
|
||||
|
||||
let wait_for_images = self.wait_for_pending_images && self.ctx.has_pending_images();
|
||||
|
||||
// We only care about immediate repaints
|
||||
if self.root_viewport_output().repaint_delay != Duration::ZERO {
|
||||
if self.root_viewport_output().repaint_delay != Duration::ZERO && !wait_for_images {
|
||||
break;
|
||||
} else if sleep {
|
||||
} else if sleep || wait_for_images {
|
||||
std::thread::sleep(Duration::from_secs_f32(self.step_dt));
|
||||
}
|
||||
if steps > self.max_steps {
|
||||
|
||||
Reference in New Issue
Block a user