diff --git a/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+AlertObservation.swift b/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+AlertObservation.swift index 11397272f5..9ed0e068d6 100644 --- a/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+AlertObservation.swift +++ b/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+AlertObservation.swift @@ -54,7 +54,6 @@ extension RunnerTests { viewport: CGRect ) -> [XCUIElementSnapshot] { let frame = snapshot.frame - let hostWindow = snapshot.elementType == .window && SnapshotGeometry.isPositiveFinite(frame) ? frame : viewport let matches: Bool switch source { case .blockingSystemModal: @@ -66,8 +65,10 @@ extension RunnerTests { case .dismissPopup: matches = snapshot.elementType == .window && containsDismissPopupMarker(snapshot) } - if matches && Self.isPlaced(frame, in: hostWindow) { return [snapshot] } - return snapshot.children.flatMap { alertSnapshots(in: $0, source: source, viewport: hostWindow) } + if matches && Self.isPlaced(frame, in: viewport) { return [snapshot] } + // A window is placed in the box it sits in, and is itself the box of what it hosts. + let childViewport = snapshot.elementType == .window && SnapshotGeometry.isPositiveFinite(frame) ? frame : viewport + return snapshot.children.flatMap { alertSnapshots(in: $0, source: source, viewport: childViewport) } } private func containsDismissPopupMarker(_ snapshot: XCUIElementSnapshot) -> Bool { diff --git a/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/UnitTests/RunnerTests+AlertObservationTests.swift b/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/UnitTests/RunnerTests+AlertObservationTests.swift index ac603efae3..a8d5311a40 100644 --- a/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/UnitTests/RunnerTests+AlertObservationTests.swift +++ b/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/UnitTests/RunnerTests+AlertObservationTests.swift @@ -104,6 +104,39 @@ extension RunnerTests { XCTAssertEqual(app.staticTexts["agent-device-alert-actions"].label, "First actions: 0; replacement actions: 0") } + @MainActor + func testAlertActivationRefusesAnUnreadableHostWindowWithoutTapping() throws { + app.launchArguments = ["--agent-device-alert-replacement-regression"] + app.launch() + defer { + invalidateCachedTarget(reason: "unit_test_cleanup") + app.terminate() + } + XCTAssertTrue(app.alerts.firstMatch.waitForFixtureExistence(timeout: appExistenceTimeout)) + let resolved = try resolveAlertBeforeTheCommand() + let alert = RunnerAlert( + root: resolved.root, + ownerApp: resolved.ownerApp, + buttons: resolved.buttons, + source: resolved.source, + window: app.windows.matching(identifier: "agent-device-missing-window").firstMatch + ) + let button = try XCTUnwrap(alert.buttons.first { $0.label == "OK" }) + + let frame = button.frame + let options = XCTExpectedFailure.Options() + options.issueMatcher = { $0.compactDescription.contains("agent-device-missing-window") } + var outcome: RunnerInteractionOutcome? + XCTExpectFailure("The unreadable window is the condition under test", options: options) { + outcome = activateAlertButton(alert, button: button, action: "accept", frame: frame, deadline: .distantFuture) + } + + guard case .unsupported? = outcome else { + return XCTFail("an unreadable host window must refuse the tap, got \(String(describing: outcome))") + } + XCTAssertEqual(app.staticTexts["agent-device-alert-actions"].label, "First actions: 0; replacement actions: 0") + } + @MainActor func testAlertDeadlineDuringSynthesisPreparationLeavesTheOriginalUntouched() throws { app.launchArguments = ["--agent-device-alert-replacement-regression"] @@ -307,5 +340,25 @@ extension RunnerTests { ) XCTAssertEqual(alertSnapshots(in: window, source: .appAlert, viewport: window.frame).map(\.elementType), [.alert]) } + + @MainActor + func testDismissPopupWindowIsPlacedInTheBoxItSitsIn() { + let viewport = CGRect(x: 0, y: 0, width: 402, height: 874) + let popup = { (x: CGFloat) in + ElementAttributesFixture(.window, CGRect(x: x, y: 0, width: 402, height: 874), children: [ + ElementAttributesFixture(.other, CGRect(x: x, y: 0, width: 402, height: 874), label: "dismiss popup") + ]) + } + XCTAssertEqual(alertSnapshots(in: popup(0), source: .dismissPopup, viewport: viewport).count, 1) + XCTAssertTrue(alertSnapshots(in: popup(1000), source: .dismissPopup, viewport: viewport).isEmpty) + } + + @MainActor + func testAnInnerPanelAlertIsPlacedInItsWindowNotTheApplicationFrame() { + let alert = ElementAttributesFixture(.alert, CGRect(x: 341, y: 250, width: 270, height: 170)) + let innerPanel = ElementAttributesFixture(.window, CGRect(x: 0, y: 0, width: 951, height: 669), children: [alert]) + let root = ElementAttributesFixture(.application, CGRect(x: 0, y: 0, width: 466, height: 678), children: [innerPanel]) + XCTAssertEqual(alertSnapshots(in: root, source: .appAlert, viewport: root.frame).map(\.elementType), [.alert]) + } #endif }