Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

test(cua-driver-e2e): settle the front app before macOS foreground hotkey checks (#5948)

cua-agents[bot]cda95b1864ec
diff --git a/libs/cua-driver/rust/crates/cua-driver-e2e/tests/installed_app_launch_macos_test.rs b/libs/cua-driver/rust/crates/cua-driver-e2e/tests/installed_app_launch_macos_test.rs
index 525ff4a1d5..9d02aafea1 100644
--- a/libs/cua-driver/rust/crates/cua-driver-e2e/tests/installed_app_launch_macos_test.rs
+++ b/libs/cua-driver/rust/crates/cua-driver-e2e/tests/installed_app_launch_macos_test.rs
@@ -51,26 +51,28 @@ fn activate_bundle(bundle_id: &str) {
let _ = osascript(&format!(r#"tell application id "{bundle_id}" to activate"#));
}
-fn wait_for_frontmost(bundle_id: &str, timeout: Duration) -> bool {
- let deadline = Instant::now() + timeout;
- while Instant::now() < deadline {
- if frontmost_bundle_id().as_deref() == Some(bundle_id) {
- return true;
- }
- sleep(Duration::from_millis(100));
- }
- false
-}
-
+/// Make Finder frontmost and keep it there for a settle window. Hosted macOS
+/// runners can hand the front to another process just after `activate`
+/// succeeded, so one sample is a race: re-activate with a bounded deadline
+/// until Finder has held the front for `FRONT_SETTLE` in a row.
fn ensure_finder_frontmost() {
- for _ in 0..3 {
- activate_bundle(FINDER_BUNDLE);
- if wait_for_frontmost(FINDER_BUNDLE, Duration::from_secs(2)) {
- return;
+ const FRONT_SETTLE: Duration = Duration::from_millis(1500);
+ let deadline = Instant::now() + Duration::from_secs(30);
+ let mut stable_since: Option<Instant> = None;
+ while Instant::now() < deadline {
+ if frontmost_bundle_id().as_deref() == Some(FINDER_BUNDLE) {
+ let since = *stable_since.get_or_insert_with(Instant::now);
+ if since.elapsed() >= FRONT_SETTLE {
+ return;
+ }
+ sleep(Duration::from_millis(100));
+ } else {
+ stable_since = None;
+ activate_bundle(FINDER_BUNDLE);
+ sleep(Duration::from_millis(300));
}
- sleep(Duration::from_millis(500));
}
- panic!("could not make Finder frontmost for installed-app launch validation");
+ panic!("could not make Finder stay frontmost for installed-app launch validation");
}
fn kill_app_process(process_name: &str) {
diff --git a/libs/cua-driver/rust/crates/cua-driver-e2e/tests/installed_app_textedit_macos_test.rs b/libs/cua-driver/rust/crates/cua-driver-e2e/tests/installed_app_textedit_macos_test.rs
index f8fc449efe..f502faa952 100644
--- a/libs/cua-driver/rust/crates/cua-driver-e2e/tests/installed_app_textedit_macos_test.rs
+++ b/libs/cua-driver/rust/crates/cua-driver-e2e/tests/installed_app_textedit_macos_test.rs
@@ -183,15 +183,33 @@ fn wait_for_frontmost(bundle_id: &str) -> bool {
false
}
+/// Make `bundle_id` the frontmost app and keep it there for a settle window.
+///
+/// On hosted macOS runners Finder (and other login-session helpers) can take
+/// the front a moment after an `activate` already reported success, so a single
+/// "is it frontmost right now" sample is a race. Re-activate with a bounded
+/// deadline until the app has held the front for `FRONT_SETTLE` in a row.
fn ensure_frontmost(bundle_id: &str) {
- for _ in 0..3 {
- osascript(&format!(r#"tell application id "{bundle_id}" to activate"#));
- if wait_for_frontmost(bundle_id) {
- return;
+ const FRONT_SETTLE: Duration = Duration::from_millis(1500);
+ let deadline = Instant::now() + Duration::from_secs(30);
+ let mut stable_since: Option<Instant> = None;
+ while Instant::now() < deadline {
+ if frontmost_bundle_id() == bundle_id {
+ let since = *stable_since.get_or_insert_with(Instant::now);
+ if since.elapsed() >= FRONT_SETTLE {
+ return;
+ }
+ std::thread::sleep(Duration::from_millis(100));
+ } else {
+ stable_since = None;
+ osascript(&format!(r#"tell application id "{bundle_id}" to activate"#));
+ std::thread::sleep(Duration::from_millis(300));
}
- std::thread::sleep(Duration::from_millis(500));
}
- panic!("could not make {bundle_id} frontmost for the TextEdit foreground check");
+ panic!(
+ "could not make {bundle_id} stay frontmost for the TextEdit foreground check; frontmost is {}",
+ frontmost_bundle_id()
+ );
}
fn focused_textedit_window_title() -> String {
@@ -224,6 +242,14 @@ fn open_sibling_document(text: &str) {
);
std::thread::sleep(Duration::from_millis(200));
}
+ // Re-settle: the front must still be TextEdit (and the sibling focused)
+ // right before the chord is sent, not just at some earlier sample.
+ ensure_frontmost(TEXTEDIT_BUNDLE);
+ assert_eq!(
+ focused_textedit_window_title(),
+ sibling_title,
+ "the sibling document lost the keyboard focus while the front settled"
+ );
std::fs::remove_file(path).expect("remove sibling document fixture");
}