Skip to content

Commit a81fa24

Browse files
committed
fix: harden iOS XCTest recovery paths
1 parent 9dabe5b commit a81fa24

7 files changed

Lines changed: 470 additions & 147 deletions

File tree

‎apple-runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+Interaction.swift‎

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -51,28 +51,6 @@ extension RunnerTests {
5151
let usedNonHittableFallback: Bool
5252
}
5353

54-
// MARK: - Navigation Gestures
55-
56-
func tapInAppBackControl(app: XCUIApplication) -> Bool {
57-
#if os(macOS)
58-
if let back = macOSNavigationBackElement(app: app) {
59-
tapElementCenter(app: app, element: back)
60-
return true
61-
}
62-
return false
63-
#elseif os(tvOS)
64-
_ = pressTvRemote(.menu)
65-
return true
66-
#else
67-
let buttons = app.navigationBars.buttons.allElementsBoundByIndex
68-
if let back = buttons.first(where: { $0.isHittable }) {
69-
back.tap()
70-
return true
71-
}
72-
return false
73-
#endif
74-
}
75-
7654
func performBackGesture(app: XCUIApplication) {
7755
if pressTvRemote(.menu) {
7856
return
@@ -1375,27 +1353,6 @@ extension RunnerTests {
13751353
}
13761354
#endif
13771355

1378-
private func tapElementCenter(app: XCUIApplication, element: XCUIElement) {
1379-
let frame = element.frame
1380-
if !frame.isEmpty {
1381-
_ = tapAt(app: app, x: frame.midX, y: frame.midY)
1382-
return
1383-
}
1384-
#if !os(tvOS)
1385-
element.tap()
1386-
#endif
1387-
}
1388-
1389-
private func macOSNavigationBackElement(app: XCUIApplication) -> XCUIElement? {
1390-
let predicate = NSPredicate(
1391-
format: "identifier == %@ OR label == %@",
1392-
"go back",
1393-
"Back"
1394-
)
1395-
let element = app.descendants(matching: .any).matching(predicate).firstMatch
1396-
return element.exists ? element : nil
1397-
}
1398-
13991356
#if AGENT_DEVICE_RUNNER_UNIT_TESTS
14001357
// Identity in portrait/unknown, 90° per landscape, 180° upside-down.
14011358
func testNativeSynthesizedPointRotatesByInterfaceOrientation() {
Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
import XCTest
2+
3+
extension RunnerTests {
4+
static let navigationBackKeywords = ["back", "close", "cancel"]
5+
static let navigationFallbackVerificationDelay: TimeInterval = 0.25
6+
7+
func tapInAppBackControl(app: XCUIApplication) -> Bool {
8+
#if os(macOS)
9+
if let back = macOSNavigationBackElement(app: app) {
10+
tapElementCenter(app: app, element: back)
11+
return true
12+
}
13+
return false
14+
#elseif os(tvOS)
15+
_ = pressTvRemote(.menu)
16+
return true
17+
#else
18+
let buttons = app.navigationBars.buttons.allElementsBoundByIndex
19+
if let back = buttons.first(where: { $0.isHittable }) {
20+
back.tap()
21+
return true
22+
}
23+
if isSnapshotXCTestChannelPenalized(bundleId: currentBundleId) {
24+
NSLog("AGENT_DEVICE_RUNNER_IN_APP_BACK_SKIPPED_XCTEST_ENUMERATION bundle=%@", currentBundleId ?? "")
25+
} else if let back = topNavigationBackElement(app: app) {
26+
tapElementCenter(app: app, element: back)
27+
return true
28+
}
29+
return tapTopLeadingNavigationFallback(app: app)
30+
#endif
31+
}
32+
33+
private func tapElementCenter(app: XCUIApplication, element: XCUIElement) {
34+
let frame = element.frame
35+
if !frame.isEmpty {
36+
_ = tapAt(app: app, x: frame.midX, y: frame.midY)
37+
return
38+
}
39+
#if !os(tvOS)
40+
element.tap()
41+
#endif
42+
}
43+
44+
private func topNavigationBackElement(app: XCUIApplication) -> XCUIElement? {
45+
#if os(iOS)
46+
let frame = onScreenWindowFrame(app: app)
47+
let candidates = app.buttons.matching(Self.navigationBackPredicate()).allElementsBoundByIndex.compactMap {
48+
element -> (XCUIElement, Int)? in
49+
guard element.exists, element.isHittable else { return nil }
50+
guard Self.isTopNavigationControlFrame(element.frame, in: frame) else { return nil }
51+
guard let rank = Self.navigationBackControlRank(
52+
label: element.label,
53+
identifier: element.identifier
54+
) else {
55+
return nil
56+
}
57+
return (element, rank)
58+
}
59+
return candidates.sorted { lhs, rhs in
60+
if lhs.1 != rhs.1 { return lhs.1 < rhs.1 }
61+
let leftFrame = lhs.0.frame
62+
let rightFrame = rhs.0.frame
63+
if leftFrame.minY != rightFrame.minY { return leftFrame.minY < rightFrame.minY }
64+
return leftFrame.minX < rightFrame.minX
65+
}.first?.0
66+
#else
67+
return nil
68+
#endif
69+
}
70+
71+
static func navigationBackPredicate() -> NSPredicate {
72+
let clauses = navigationBackKeywords.flatMap { _ in
73+
["label CONTAINS[c] %@", "identifier CONTAINS[c] %@"]
74+
}.joined(separator: " OR ")
75+
let arguments = navigationBackKeywords.flatMap { [$0, $0] }
76+
return NSPredicate(format: clauses, argumentArray: arguments)
77+
}
78+
79+
static func navigationBackControlRank(label: String, identifier: String) -> Int? {
80+
let text = "\(label) \(identifier)".lowercased()
81+
return navigationBackKeywords.firstIndex { text.contains($0) }
82+
}
83+
84+
static func isTopNavigationControlFrame(_ candidate: CGRect, in window: CGRect) -> Bool {
85+
guard
86+
candidate.width.isFinite,
87+
candidate.height.isFinite,
88+
window.width.isFinite,
89+
window.height.isFinite,
90+
candidate.width > 0,
91+
candidate.height > 0,
92+
window.width > 0,
93+
window.height > 0
94+
else {
95+
return false
96+
}
97+
// Accept the compact navigation/search header band without matching deep content controls.
98+
let maxY = window.minY + min(max(window.height * 0.22, 96), 180)
99+
return candidate.midY >= window.minY && candidate.midY <= maxY
100+
}
101+
102+
static func topLeadingNavigationFallbackPoint(in frame: CGRect) -> CGPoint? {
103+
guard frame.width.isFinite, frame.height.isFinite, frame.width > 0, frame.height > 0 else {
104+
return nil
105+
}
106+
// Aim at the standard leading navigation slot, bounded for compact and tablet widths.
107+
let xOffset = min(max(frame.width * 0.08, 28), 44)
108+
// Sit below the status/dynamic-island region and inside common custom RN search headers.
109+
let yOffset = min(max(frame.height * 0.155, 56), 132)
110+
return CGPoint(x: frame.minX + xOffset, y: frame.minY + yOffset)
111+
}
112+
113+
private func tapTopLeadingNavigationFallback(app: XCUIApplication) -> Bool {
114+
#if os(iOS)
115+
let frame = onScreenWindowFrame(app: app)
116+
guard let point = Self.topLeadingNavigationFallbackPoint(in: frame) else {
117+
return false
118+
}
119+
let before = captureNavigationFallbackVisualState()
120+
let context = synthesizedCoordinateContext(
121+
policy: synthesizedGesturePolicy(.coordinateTap)
122+
)?.withReferenceFrame(frame)
123+
let synthesized = performGesture(app, idleTimeout: false) {
124+
synthesizedTapAt(app: app, x: point.x, y: point.y, context: context)
125+
}
126+
if case .performed = synthesized.outcome {
127+
return didNavigationFallbackChangeVisualState(before: before)
128+
}
129+
let fallback = performGesture(app) {
130+
tapAt(app: app, x: point.x, y: point.y)
131+
}
132+
if case .performed = fallback.outcome {
133+
return didNavigationFallbackChangeVisualState(before: before)
134+
}
135+
#endif
136+
return false
137+
}
138+
139+
private func captureNavigationFallbackVisualState() -> Data? {
140+
#if os(iOS)
141+
runnerPngData(for: XCUIScreen.main.screenshot().image)
142+
#else
143+
return nil
144+
#endif
145+
}
146+
147+
private func didNavigationFallbackChangeVisualState(before: Data?) -> Bool {
148+
sleepFor(Self.navigationFallbackVerificationDelay)
149+
let after = captureNavigationFallbackVisualState()
150+
let changed = Self.didNavigationFallbackChangeVisualState(before: before, after: after)
151+
if !changed {
152+
NSLog("AGENT_DEVICE_RUNNER_IN_APP_BACK_FALLBACK_NO_STATE_CHANGE")
153+
}
154+
return changed
155+
}
156+
157+
static func didNavigationFallbackChangeVisualState(before: Data?, after: Data?) -> Bool {
158+
guard let before, let after else { return false }
159+
return before != after
160+
}
161+
162+
private func macOSNavigationBackElement(app: XCUIApplication) -> XCUIElement? {
163+
let predicate = NSPredicate(
164+
format: "identifier == %@ OR label == %@",
165+
"go back",
166+
"Back"
167+
)
168+
let element = app.descendants(matching: .any).matching(predicate).firstMatch
169+
return element.exists ? element : nil
170+
}
171+
172+
#if AGENT_DEVICE_RUNNER_UNIT_TESTS
173+
func testTopLeadingNavigationFallbackPointTargetsHeaderControlBand() throws {
174+
let point = try XCTUnwrap(
175+
Self.topLeadingNavigationFallbackPoint(
176+
in: CGRect(x: 0, y: 0, width: 430, height: 932)
177+
)
178+
)
179+
180+
XCTAssertEqual(point.x, 34.4, accuracy: 0.01)
181+
XCTAssertEqual(point.y, 132, accuracy: 0.01)
182+
}
183+
184+
func testTopLeadingNavigationFallbackPointRejectsInvalidFrame() {
185+
XCTAssertNil(Self.topLeadingNavigationFallbackPoint(in: .infinite))
186+
XCTAssertNil(Self.topLeadingNavigationFallbackPoint(in: .zero))
187+
}
188+
189+
func testNavigationBackControlRankPrefersBackThenCloseThenCancel() {
190+
XCTAssertEqual(Self.navigationBackControlRank(label: "Back", identifier: ""), 0)
191+
XCTAssertEqual(Self.navigationBackControlRank(label: "Close", identifier: ""), 1)
192+
XCTAssertEqual(Self.navigationBackControlRank(label: "Cancel search", identifier: ""), 2)
193+
XCTAssertNil(Self.navigationBackControlRank(label: "Search for more feeds", identifier: ""))
194+
}
195+
196+
func testNavigationBackPredicateUsesTheSharedKeywordTable() {
197+
let predicate = Self.navigationBackPredicate()
198+
199+
XCTAssertTrue(predicate.evaluate(with: ["label": "Back", "identifier": ""]))
200+
XCTAssertTrue(predicate.evaluate(with: ["label": "", "identifier": "close-button"]))
201+
XCTAssertFalse(predicate.evaluate(with: ["label": "Search for more feeds", "identifier": ""]))
202+
}
203+
204+
func testTopNavigationControlFrameAcceptsOnlyHeaderBand() {
205+
let window = CGRect(x: 0, y: 0, width: 430, height: 932)
206+
207+
XCTAssertTrue(
208+
Self.isTopNavigationControlFrame(
209+
CGRect(x: 340, y: 84, width: 72, height: 44),
210+
in: window
211+
)
212+
)
213+
XCTAssertFalse(
214+
Self.isTopNavigationControlFrame(
215+
CGRect(x: 20, y: 760, width: 72, height: 44),
216+
in: window
217+
)
218+
)
219+
XCTAssertFalse(Self.isTopNavigationControlFrame(.infinite, in: window))
220+
}
221+
222+
func testNavigationFallbackRequiresObservedVisualChange() {
223+
XCTAssertTrue(
224+
Self.didNavigationFallbackChangeVisualState(
225+
before: Data([1, 2, 3]),
226+
after: Data([1, 2, 4])
227+
)
228+
)
229+
XCTAssertFalse(
230+
Self.didNavigationFallbackChangeVisualState(
231+
before: Data([1, 2, 3]),
232+
after: Data([1, 2, 3])
233+
)
234+
)
235+
XCTAssertFalse(Self.didNavigationFallbackChangeVisualState(before: nil, after: Data([1])))
236+
XCTAssertFalse(Self.didNavigationFallbackChangeVisualState(before: Data([1]), after: nil))
237+
}
238+
#endif
239+
}

‎apple-runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+Snapshot.swift‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,8 @@ extension RunnerTests {
571571
func makeSnapshotTraversalContext(
572572
app: XCUIApplication,
573573
options: SnapshotOptions,
574-
captureDeadline: Date = .distantFuture
574+
captureDeadline: Date = .distantFuture,
575+
treeCaptureSliceBudgetOverride: TimeInterval? = nil
575576
) throws -> SnapshotTraversalContext? {
576577
let (viewport, queryRoot) = try runMainThreadWork(
577578
command: nil,
@@ -584,7 +585,8 @@ extension RunnerTests {
584585
)
585586
}
586587

587-
let slice = min(treeCaptureSliceBudget, max(0.5, captureDeadline.timeIntervalSinceNow))
588+
let treeSliceBudget = treeCaptureSliceBudgetOverride ?? treeCaptureSliceBudget
589+
let slice = min(treeSliceBudget, max(0.5, captureDeadline.timeIntervalSinceNow))
588590
guard let rootSnapshot = try captureSnapshotRootBounded(queryRoot, sliceSeconds: slice) else {
589591
return nil
590592
}
@@ -600,7 +602,7 @@ extension RunnerTests {
600602
)
601603
}
602604

603-
static let treeCaptureTimeoutCode = "IOS_TREE_CAPTURE_TIMEOUT"
605+
static let xCTestSnapshotTimeoutCode = "IOS_TREE_CAPTURE_TIMEOUT"
604606

605607
func hasAbandonedTreeCapture() -> Bool {
606608
treeCaptureLock.lock()
@@ -611,7 +613,8 @@ extension RunnerTests {
611613
/// Runs the blocking tree-snapshot XPC on the main thread bounded by `sliceSeconds`. On
612614
/// timeout the XPC keeps running on main (it cannot be cancelled); the capture is marked
613615
/// abandoned so plans avoid XCTest-backed tiers until it drains, the tree backend is penalized
614-
/// for this bundle, and the plan moves on to the private AX backend (#1105/#1122).
616+
/// for this bundle, and the plan moves to the platform's independent recovery tier when one
617+
/// exists (#1105/#1122).
615618
private func captureSnapshotRootBounded(
616619
_ element: XCUIElement,
617620
sliceSeconds: TimeInterval
@@ -628,7 +631,7 @@ extension RunnerTests {
628631
self.abandonedTreeCaptureCount += 1
629632
self.treeCaptureLock.unlock()
630633
NSLog("AGENT_DEVICE_RUNNER_TREE_CAPTURE_SLICE_TIMEOUT slice=%.1f", sliceSeconds)
631-
self.penalizeSnapshotTreeBackend(
634+
self.penalizeSnapshotXCTestChannel(
632635
bundleId: self.currentBundleId,
633636
reason: "tree_capture_slice_timeout"
634637
)
@@ -647,17 +650,17 @@ extension RunnerTests {
647650
private func treeCaptureTimeoutError(sliceSeconds: TimeInterval) -> () -> Error {
648651
{
649652
SnapshotCaptureFailure(
650-
code: Self.treeCaptureTimeoutCode,
653+
code: Self.xCTestSnapshotTimeoutCode,
651654
message: "the XCTest tree capture exceeded its \(Int(sliceSeconds))s time slice",
652-
hint: "The capture plan recovers through the private AX backend on this screen."
655+
hint: "The capture plan will avoid or tightly bound XCTest-backed snapshot tiers on this screen."
653656
)
654657
}
655658
}
656659

657660
func snapshotMainThreadTimeoutError(_ operation: String) -> () -> Error {
658661
{
659662
SnapshotCaptureFailure(
660-
code: Self.treeCaptureTimeoutCode,
663+
code: Self.xCTestSnapshotTimeoutCode,
661664
message: "timed out while \(operation) on the XCTest main thread",
662665
hint: "The capture plan will skip XCTest-backed snapshot tiers while the previous main-thread work drains."
663666
)

0 commit comments

Comments
 (0)