Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

test(ui): wait for the text layer by state, not for a fixed second (#1277) (#2327)

mtlcl4108356665d7b4b
diff --git a/crates/ui-egui/tests/ui_group/column_select.rs b/crates/ui-egui/tests/ui_group/column_select.rs
index 30ef0a13c..017851a89 100644
--- a/crates/ui-egui/tests/ui_group/column_select.rs
+++ b/crates/ui-egui/tests/ui_group/column_select.rs
@@ -37,14 +37,9 @@ fn harness(setup: impl FnOnce(&mut PdfCraftApp) + 'static) -> Harness<'static, P
setup(&mut app);
app
});
- for _ in 0..200 {
- h.run_steps(2);
- if !h.state().render_pending() {
- break;
- }
- std::thread::sleep(std::time::Duration::from_millis(5));
- }
- assert!(h.state().views[0].glyph_screen_pos(0, 0).is_some(), "text layer loaded");
+ // Wait for the text layer itself (#1277): under load the first render takes longer than a
+ // fixed second, and nothing may be pending yet before the text layer is asked for.
+ crate::support::wait_until(&mut h, "the page's text layer", |app| app.views[0].glyph_screen_pos(0, 0).is_some() && !app.render_pending());
h
}
diff --git a/crates/ui-egui/tests/ui_group/identity.rs b/crates/ui-egui/tests/ui_group/identity.rs
index 52581fe6a..ccfba78d7 100644
--- a/crates/ui-egui/tests/ui_group/identity.rs
+++ b/crates/ui-egui/tests/ui_group/identity.rs
@@ -41,13 +41,7 @@ fn the_identity_name_signs_new_comments_and_is_remembered() {
// New comments carry it. Wait for the page (and its text) to finish loading first: selecting
// text that hasn't loaded selects nothing, and no highlight is made (seen on slow CI runners).
h.state_mut().set_option("dialog", "none").unwrap();
- for _ in 0..200 {
- h.run_steps(2);
- if !h.state().render_pending() {
- break;
- }
- std::thread::sleep(std::time::Duration::from_millis(5));
- }
+ crate::support::wait_until(&mut h, "the page's text layer", |app| app.views[0].glyph_screen_pos(0, 8).is_some() && !app.render_pending());
h.state_mut().views[0].select_text(0, 4, 8);
assert!(h.state_mut().execute("comment.highlight"));
// Wait for the edit to land in the document (it applies on a later frame).
@@ -55,13 +49,7 @@ fn the_identity_name_signs_new_comments_and_is_remembered() {
let doc = h.state().session.get(h.state().views[0].id).unwrap();
doc.info.annotations.first().and_then(|a| a.author.clone())
};
- for _ in 0..100 {
- if author(&h).is_some() {
- break;
- }
- h.run_steps(1);
- std::thread::sleep(std::time::Duration::from_millis(5));
- }
+ crate::support::wait_until(&mut h, "the highlight", |app| app.session.get(app.views[0].id).is_some_and(|d| !d.info.annotations.is_empty()));
assert_eq!(author(&h).as_deref(), Some("Grace Hopper"));
// It survives a restart; an empty or missing name keeps the default, and a huge one is cut.
let mut restored = PdfCraftApp::new();
diff --git a/crates/ui-egui/tests/ui_group/main.rs b/crates/ui-egui/tests/ui_group/main.rs
index 4c59ca93a..5f0215dd4 100644
--- a/crates/ui-egui/tests/ui_group/main.rs
+++ b/crates/ui-egui/tests/ui_group/main.rs
@@ -3,6 +3,9 @@
//! write screenshots, use process-ID temporary paths or probe installed fonts stay standalone
//! in `tests/`. A file in this folder is only built if it is listed here.
+#[path = "../support/mod.rs"]
+mod support;
+
mod animate;
mod bulk_fields;
mod column_select;
diff --git a/crates/ui-egui/tests/ui_group/text_select.rs b/crates/ui-egui/tests/ui_group/text_select.rs
index eb380f261..4b9013a57 100644
--- a/crates/ui-egui/tests/ui_group/text_select.rs
+++ b/crates/ui-egui/tests/ui_group/text_select.rs
@@ -41,13 +41,8 @@ fn harness_with(pdf: Vec<u8>, setup: impl FnOnce(&mut PdfCraftApp) + 'static) ->
setup(&mut app);
app
});
- for _ in 0..200 {
- h.run_steps(2);
- if !h.state().render_pending() {
- break;
- }
- std::thread::sleep(std::time::Duration::from_millis(5));
- }
+ // Wait for the text layer itself (#1277), not for a fixed second.
+ crate::support::wait_until(&mut h, "the page's text layer", |app| app.views[0].glyph_screen_pos(0, 5).is_some() && !app.render_pending());
let at = h.state().views[0].glyph_screen_pos(0, 5).expect("text layer loaded");
h.hover_at(at);
h.run_steps(1);
@@ -94,16 +89,12 @@ fn quick_clicks_with_the_highlighter_mark_only_the_word() {
}
}
h.run_steps(3);
- // Under load the mark can land a few frames later; wait for it (egui's clock is fixed per
- // frame, so waiting can't turn the clicks into a different run).
- for _ in 0..200 {
- let app = h.state();
- if app.session.get(app.views[0].id).is_some_and(|d| !d.info.annotations.is_empty()) && !app.render_pending() {
- break;
- }
- std::thread::sleep(std::time::Duration::from_millis(5));
- h.run_steps(1);
- }
+ // Under load the mark can land later; wait for it (egui's clock is fixed per frame, so
+ // waiting can't turn the clicks into a different run). By state, not for a fixed second
+ // (#1277).
+ crate::support::wait_until(&mut h, "the highlight and the page after it", |app| {
+ app.session.get(app.views[0].id).is_some_and(|d| !d.info.annotations.is_empty()) && !app.render_pending()
+ });
let app = h.state();
let doc = app.session.get(app.views[0].id).unwrap();
let marks: Vec<(&str, usize)> = doc.info.annotations.iter().map(|a| (a.subtype.as_str(), a.quads.len())).collect();
diff --git a/crates/ui-egui/tests/ui_group/updates.rs b/crates/ui-egui/tests/ui_group/updates.rs
index c06bd8b2c..a8ecb31c5 100644
--- a/crates/ui-egui/tests/ui_group/updates.rs
+++ b/crates/ui-egui/tests/ui_group/updates.rs
@@ -21,13 +21,17 @@ fn harness(answer: Result<&str, &str>) -> Harness<'static, PdfCraftApp> {
})
}
-/// Run frames until the background check has reported (or give up).
+/// Run frames until the background check has reported. By what the dialog shows, with the
+/// same 30-second deadline as `support::wait_until`, not for a fixed second: under load the
+/// check's thread can take longer (#1277).
fn settle(h: &mut Harness<'static, PdfCraftApp>) {
- for _ in 0..200 {
+ let deadline = std::time::Instant::now() + std::time::Duration::from_secs(30);
+ loop {
h.run_steps(2);
if h.query_by_label_contains("Checking for a newer version").is_none() {
break;
}
+ assert!(std::time::Instant::now() < deadline, "timed out waiting for the update check");
std::thread::sleep(std::time::Duration::from_millis(5));
}
h.run_steps(2);