Skip to content

Commit 2864217

Browse files
thymikeeclaude
andcommitted
fix(ios): aim the back fallback at one header point on every route
AX health picks the route but no longer the target: the fallback taps 0.1h (56-96) on a resolved window and on the sole screen alike, the leading control of a standard bar (y 62-106 on a 402x874 iPhone). A rejected query skips the windows query only where the sole screen answers, so an iPad still asks for its windows as before. Tests cover a sole-screen tap that changes nothing and the iPad windows route after a rejected query. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent bd1d534 commit 2864217

3 files changed

Lines changed: 109 additions & 47 deletions

File tree

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+Navigation.swift‎

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -179,19 +179,15 @@ extension RunnerTests {
179179
return candidate.midY >= window.minY && candidate.midY <= maxY
180180
}
181181

182-
static func topLeadingNavigationFallbackPoint(
183-
in frame: CGRect,
184-
windowResolved: Bool = true
185-
) -> CGPoint? {
182+
static func topLeadingNavigationFallbackPoint(in frame: CGRect) -> CGPoint? {
186183
guard SnapshotGeometry.isPositiveFinite(frame) else {
187184
return nil
188185
}
189186
// Aim at the standard leading navigation slot, bounded for compact and tablet widths.
190187
let xOffset = min(max(frame.width * 0.08, 28), 44)
191-
// Sit below the status/dynamic-island region and inside common custom RN search headers.
192-
let yOffset = windowResolved
193-
? min(max(frame.height * 0.155, 56), 132)
194-
: min(max(frame.height * 0.1, 56), 96)
188+
// Sit below the status/dynamic-island region, inside the bar's leading control: on a 402x874
189+
// iPhone the bar spans y 62-116 and its back control 62-106.
190+
let yOffset = min(max(frame.height * 0.1, 56), 96)
195191
return CGPoint(x: frame.minX + xOffset, y: frame.minY + yOffset)
196192
}
197193

@@ -205,10 +201,7 @@ extension RunnerTests {
205201
app: app,
206202
policy: synthesizedGesturePolicy(.coordinateTap),
207203
windowQueryRejected: windowQueryRejected
208-
), let point = Self.topLeadingNavigationFallbackPoint(
209-
in: context.referenceFrame,
210-
windowResolved: context.resolvedWindow != nil
211-
) else {
204+
), let point = Self.topLeadingNavigationFallbackPoint(in: context.referenceFrame) else {
212205
return .unavailable
213206
}
214207
let before = captureNavigationFallbackVisualState(app: app, context: context)

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+SynthesizedInteraction.swift‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,10 +391,13 @@ extension RunnerTests {
391391
var health = mainOwned.accessibilityHealth
392392
let window: XCUIElement?
393393
let referenceFrame: CGRect
394-
if !windowQueryRejected, let resolved = resolveRunnerWindowElement(app: app) {
394+
// A tree that just rejected a query skips the windows query only where the sole screen answers
395+
// without it; anywhere else that query is still the only route.
396+
let rejectedTreeSoleScreenFrame = windowQueryRejected ? soleScreenReferenceFrame(app: app) : nil
397+
if rejectedTreeSoleScreenFrame == nil, let resolved = resolveRunnerWindowElement(app: app) {
395398
window = resolved.window
396399
referenceFrame = resolved.frame
397-
} else if let soleScreenFrame = soleScreenReferenceFrame(app: app) {
400+
} else if let soleScreenFrame = rejectedTreeSoleScreenFrame ?? soleScreenReferenceFrame(app: app) {
398401
window = nil
399402
referenceFrame = soleScreenFrame
400403
// The windows query just failed on this tree, so this gesture's keyboard probes and XCTest

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/UnitTests/RunnerTests+NavigationTests.swift‎

Lines changed: 99 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,101 @@ extension RunnerTests {
1919

2020
@MainActor
2121
func testInAppBackVerifiesSoleScreenWhenXCTestCannotReadFramesOrEnumerate() throws {
22-
app.launchArguments = ["--agent-device-back-regression"]
22+
let status = try launchBackFixture(["--agent-device-back-regression"])
23+
defer { endBackFixture() }
24+
25+
let outcome = try withRejectedAppTree(framesReadable: false) { tapInAppBackControl(app: app) }
26+
27+
guard showsAppsOnlyFullScreen else {
28+
guard case .unavailable = outcome else {
29+
return XCTFail("without a window or a sole screen there is no route to tap")
30+
}
31+
return
32+
}
33+
guard case .performed = outcome else {
34+
return XCTFail("a window-less back must synthesize and verify against the sole display")
35+
}
36+
XCTAssertEqual(status.label, "Back performed")
37+
}
38+
39+
/// The sole-screen route compares real captures: a tap that lands on nothing leaves the screen
40+
/// unchanged, so back is unavailable rather than performed.
41+
@MainActor
42+
func testInAppBackOnTheSoleScreenReportsATapThatChangedNothingAsUnavailable() throws {
43+
_ = try launchBackFixture([])
44+
defer { endBackFixture() }
45+
46+
let outcome = try withRejectedAppTree(framesReadable: false) { tapInAppBackControl(app: app) }
47+
48+
guard case .unavailable = outcome else {
49+
return XCTFail("a top-leading tap on an app without a back control must not read as performed")
50+
}
51+
}
52+
53+
/// Only an iPhone with one screen can skip the windows query after the tree rejected one; anywhere
54+
/// else the windows query is still asked, and its window carries the tap.
55+
@MainActor
56+
func testInAppBackStillAsksForWindowsWhereNoSoleScreenAnswers() throws {
57+
let status = try launchBackFixture(["--agent-device-back-regression"])
58+
defer { endBackFixture() }
59+
60+
let outcome = try withRejectedAppTree(framesReadable: true, rejections: 1) {
61+
tapInAppBackControl(app: app)
62+
}
63+
64+
guard case .performed = outcome else {
65+
return XCTFail("back must reach the fixture's top-leading control through a window or the sole screen")
66+
}
67+
XCTAssertEqual(status.label, "Back performed")
68+
}
69+
70+
private var showsAppsOnlyFullScreen: Bool {
71+
UIDevice.current.userInterfaceIdiom == .phone && XCUIScreen.screens.count == 1
72+
}
73+
74+
/// Launches the runner app; with `--agent-device-back-regression` it shows a top-leading back
75+
/// control whose press sets the returned status to "Back performed", and without it nothing there.
76+
@MainActor
77+
private func launchBackFixture(_ arguments: [String]) throws -> XCUIElement {
78+
app.launchArguments = arguments
2379
app.launch()
2480
mainOwned.app = app
2581
mainOwned.bundleId = "com.callstack.agentdevice.runner"
2682
clearSnapshotXCTestChannelPenalty(reason: "navigation-test")
27-
defer {
28-
invalidateCachedTarget(reason: "unit_test_cleanup")
29-
app.terminate()
30-
}
31-
let status = app.staticTexts["agent-device-back-status"]
83+
let status = arguments.contains("--agent-device-back-regression")
84+
? app.staticTexts["agent-device-back-status"]
85+
: app.staticTexts.element(matching: NSPredicate(format: "label == %@", "Agent Device Runner"))
3286
XCTAssertTrue(status.waitForFixtureExistence(timeout: appExistenceTimeout))
87+
return status
88+
}
89+
90+
@MainActor
91+
private func endBackFixture() {
92+
invalidateCachedTarget(reason: "unit_test_cleanup")
93+
app.terminate()
94+
}
95+
96+
/// Stands in for XCTest on a tree it cannot serialize while `body` runs: the first `rejections`
97+
/// queries record the rejection the runner suppresses and find nothing, and unless
98+
/// `framesReadable`, every element frame reads empty.
99+
@MainActor
100+
private func withRejectedAppTree<T>(
101+
framesReadable: Bool,
102+
rejections: Int = .max,
103+
_ body: () -> T
104+
) throws -> T {
33105
var restorations: [() -> Void] = []
34106
defer { restorations.reversed().forEach { $0() } }
35-
let query = try XCTUnwrap(class_getInstanceMethod(XCUIElementQuery.self, NSSelectorFromString("allElementsBoundByIndex")))
107+
let selector = NSSelectorFromString("allElementsBoundByIndex")
108+
let query = try XCTUnwrap(class_getInstanceMethod(XCUIElementQuery.self, selector))
36109
let originalQuery = method_getImplementation(query)
37-
let rejectedQuery = imp_implementationWithBlock({ (_: AnyObject) -> NSArray in
110+
var remainingRejections = rejections
111+
let rejectedQuery = imp_implementationWithBlock({ (target: AnyObject) -> NSArray in
112+
guard remainingRejections > 0 else {
113+
typealias Query = @convention(c) (AnyObject, Selector) -> NSArray
114+
return unsafeBitCast(originalQuery, to: Query.self)(target, selector)
115+
}
116+
remainingRejections -= 1
38117
self.record(XCTIssue(type: .assertionFailure, compactDescription: "Failed to resolve query: kAXErrorIllegalArgument"))
39118
return []
40119
} as @convention(block) (AnyObject) -> NSArray)
@@ -43,22 +122,17 @@ extension RunnerTests {
43122
method_setImplementation(query, originalQuery)
44123
imp_removeBlock(rejectedQuery)
45124
}
46-
let frame = try XCTUnwrap(class_getInstanceMethod(XCUIElement.self, NSSelectorFromString("frame")))
47-
let originalFrame = method_getImplementation(frame)
48-
let unavailableFrame = imp_implementationWithBlock({ (_: AnyObject) -> CGRect in .zero } as @convention(block) (AnyObject) -> CGRect)
49-
method_setImplementation(frame, unavailableFrame)
50-
restorations.append {
51-
method_setImplementation(frame, originalFrame)
52-
imp_removeBlock(unavailableFrame)
53-
}
54-
55-
let outcome = tapInAppBackControl(app: app)
56-
restorations.reversed().forEach { $0() }
57-
restorations.removeAll()
58-
guard case .performed = outcome else {
59-
return XCTFail("a window-less back must synthesize and verify against the sole display")
125+
if !framesReadable {
126+
let frame = try XCTUnwrap(class_getInstanceMethod(XCUIElement.self, NSSelectorFromString("frame")))
127+
let originalFrame = method_getImplementation(frame)
128+
let unavailableFrame = imp_implementationWithBlock({ (_: AnyObject) -> CGRect in .zero } as @convention(block) (AnyObject) -> CGRect)
129+
method_setImplementation(frame, unavailableFrame)
130+
restorations.append {
131+
method_setImplementation(frame, originalFrame)
132+
imp_removeBlock(unavailableFrame)
133+
}
60134
}
61-
XCTAssertEqual(status.label, "Back performed")
135+
return body()
62136
}
63137

64138
@MainActor
@@ -106,22 +180,14 @@ extension RunnerTests {
106180
}
107181
#endif
108182

183+
/// A 402x874 iPhone's navigation bar spans y 62-116 and its back control 62-106.
109184
func testTopLeadingNavigationFallbackPointTargetsHeaderControlBand() throws {
110185
let point = try XCTUnwrap(
111186
Self.topLeadingNavigationFallbackPoint(
112-
in: CGRect(x: 0, y: 0, width: 430, height: 932)
187+
in: CGRect(x: 0, y: 0, width: 402, height: 874)
113188
)
114189
)
115190

116-
XCTAssertEqual(point.x, 34.4, accuracy: 0.01)
117-
XCTAssertEqual(point.y, 132, accuracy: 0.01)
118-
}
119-
120-
func testSoleScreenNavigationFallbackPointTargetsCompactHeader() throws {
121-
let point = try XCTUnwrap(Self.topLeadingNavigationFallbackPoint(
122-
in: CGRect(x: 0, y: 0, width: 402, height: 874),
123-
windowResolved: false
124-
))
125191
XCTAssertEqual(point.x, 32.16, accuracy: 0.01)
126192
XCTAssertEqual(point.y, 87.4, accuracy: 0.01)
127193
}

0 commit comments

Comments
 (0)