Skip to content

Commit cee9941

Browse files
thymikeeclaude
andcommitted
fix(ios): confirm a back button pop only from bar and back button labels and counts
A tap that navigates nowhere can still move the back button, such as by a large-title collapse, so its frame no longer confirms a pop. Frames stay in the still check before the tap and in matching the button to press. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent d1ffd66 commit cee9941

6 files changed

Lines changed: 80 additions & 33 deletions

File tree

‎apple/runner/AgentDeviceRunner/AgentDeviceRunner/AgentDeviceRunnerAppFixtures.m‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ @interface AgentDeviceRunnerFixtureViewController : AgentDeviceRunnerViewControl
4242
// A real navigation stack for `back`. `--agent-device-back-navigation-bar` pushes a detail screen,
4343
// so the bar shows UIKit's system back button; the interactive pop is off, so only that button can
4444
// pop it. `--blocked-back` keeps that button but replaces its action, so pressing it navigates
45-
// nowhere. `--ticking-title` counts the detail screen's title down every 0.2 s, as a timer does,
45+
// nowhere. `--shifting-back` makes that press move the bar down instead, so the back button moves
46+
// without any navigation. `--ticking-title` counts the detail screen's title down every 0.2 s, as a timer does,
4647
// so its bar changes without any navigation. `--custom-back-item` instead puts a bar button titled
4748
// "Back" on the root, which is not the system back button and must never be pressed. The root's
4849
// label reports what happened.
@@ -79,7 +80,11 @@ - (instancetype)initWithCustomBackItem:(BOOL)customBackItem blockedBack:(BOOL)bl
7980
detail.view.backgroundColor = UIColor.systemTealColor;
8081
if (blockedBack) {
8182
if (@available(iOS 16.0, *)) {
82-
detail.navigationItem.backAction = [UIAction actionWithHandler:^(__kindof UIAction *action) {}];
83+
BOOL shifting = [NSProcessInfo.processInfo.arguments containsObject:@"--shifting-back"];
84+
__weak UINavigationController *weakSelf = self;
85+
detail.navigationItem.backAction = [UIAction actionWithHandler:^(__kindof UIAction *action) {
86+
if (shifting) weakSelf.additionalSafeAreaInsets = UIEdgeInsetsMake(30, 0, 0, 0);
87+
}];
8388
}
8489
}
8590
if ([NSProcessInfo.processInfo.arguments containsObject:@"--ticking-title"]) {

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

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -275,17 +275,22 @@ extension RunnerTests {
275275
}
276276
}
277277

278-
/// The label and frame of each navigation bar's system back buttons, one entry per bar in bar
279-
/// order, so the bar count counts too. A pop removes, moves, or relabels the pressed button, or
280-
/// removes its bar, so a change of this state is the evidence that the tap navigated. Bar titles
281-
/// and identifiers are left out: a bar can retitle itself, such as a countdown, without any
282-
/// navigation. A pop that leaves the back button as it was, such as one between two screens whose
283-
/// back buttons have the same title, answers unverified.
278+
/// The labels of each navigation bar's system back buttons, one list per bar in bar order, and
279+
/// the frames of those buttons. Both must agree for a still bar before the tap. Bar titles and
280+
/// identifiers are left out: a bar can retitle itself, such as a countdown, without any navigation.
284281
struct NavigationBarBackState: Equatable {
285-
let entries: [String]
282+
let backButtonLabels: [[String]]
286283
let backButtonFrames: [CGRect]
287284
}
288285

286+
/// Whether the bar after the tap shows a pop: the bar count, the back button count, or a back
287+
/// button label changed. Frames are left out: a tap that navigates nowhere can still move the
288+
/// button, such as by a large-title collapse or a bar button that appears. A pop that keeps the
289+
/// same back button labels and bar count answers unverified.
290+
static func navigationBarBackPopped(from before: NavigationBarBackState, to after: NavigationBarBackState) -> Bool {
291+
after.backButtonLabels != before.backButtonLabels
292+
}
293+
289294
/// One read of the bar state, with the time the read started.
290295
struct TimedNavigationBarBackState {
291296
let state: NavigationBarBackState
@@ -415,7 +420,7 @@ extension RunnerTests {
415420
) -> NavigationBarBackConfirmation {
416421
while true {
417422
switch readNavigationBarBackState(app: app, phase: .confirmation, deadlineMs: deadlineMs) {
418-
case .read(let after) where after != before: return .changed
423+
case .read(let after) where Self.navigationBarBackPopped(from: before, to: after): return .changed
419424
case .hung: return .hung
420425
case .read, .unreadable: break
421426
}
@@ -491,30 +496,28 @@ extension RunnerTests {
491496
) -> NavigationBarBackRead<NavigationBarBackState> {
492497
heldNavigationBarRead(phase) {
493498
guard let bars = try? elementQueries(app: app, matching: .navigationBar) else { return nil }
494-
var entries: [String] = []
499+
var backButtonLabels: [[String]] = []
495500
var backButtonFrames: [CGRect] = []
496501
for bar in bars.flatMap({ $0.allElementsBoundByIndex }) {
497502
guard currentUptimeMs() < deadlineMs, let snapshot = try? bar.snapshot() else { return nil }
498-
let entry = Self.navigationBarBackEntry(snapshot)
499-
entries.append(entry.entry)
500-
backButtonFrames.append(contentsOf: entry.backButtonFrames)
503+
let buttons = Self.navigationBarBackButtons(snapshot)
504+
backButtonLabels.append(buttons.map(\.label))
505+
backButtonFrames.append(contentsOf: buttons.map(\.frame))
501506
}
502-
return NavigationBarBackState(entries: entries, backButtonFrames: backButtonFrames)
507+
return NavigationBarBackState(backButtonLabels: backButtonLabels, backButtonFrames: backButtonFrames)
503508
}
504509
}
505510

506-
static func navigationBarBackEntry(_ bar: XCUIElementSnapshot) -> (entry: String, backButtonFrames: [CGRect]) {
507-
var buttons: [String] = []
508-
var frames: [CGRect] = []
511+
static func navigationBarBackButtons(_ bar: XCUIElementSnapshot) -> [(label: String, frame: CGRect)] {
512+
var buttons: [(label: String, frame: CGRect)] = []
509513
var pending = bar.children
510514
while let element = pending.popLast() {
511515
if element.identifier == navigationBarBackButtonIdentifier {
512-
buttons.append("\(element.label)\u{1F}\(NSCoder.string(for: element.frame))")
513-
frames.append(element.frame)
516+
buttons.append((element.label, element.frame))
514517
}
515518
pending.append(contentsOf: element.children)
516519
}
517-
return (buttons.joined(separator: "\u{1E}"), frames)
520+
return buttons
518521
}
519522

520523
@MainActor

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

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,25 @@ extension RunnerTests {
106106
XCTAssertEqual(app.navigationBars.buttons.matching(identifier: Self.navigationBarBackButtonIdentifier).count, 1)
107107
}
108108

109+
/// A back button that only moves after the tap is not a navigation: a tap that navigates nowhere
110+
/// but shifts the bar, as a large-title collapse does, reports an unknown outcome.
111+
@MainActor
112+
func testInAppBackDoesNotCreditAMovedBackButtonToAPressedBackButton() throws {
113+
guard #available(iOS 16.0, *) else { throw XCTSkip("UINavigationItem.backAction needs iOS 16") }
114+
_ = try launchBackNavigationFixture(["--agent-device-back-navigation-bar", "--blocked-back", "--shifting-back"])
115+
defer { endBackFixture() }
116+
let backButton = app.navigationBars.buttons.matching(identifier: Self.navigationBarBackButtonIdentifier).firstMatch
117+
let frameBefore = backButton.frame
118+
119+
guard case .unconfirmed(let payload) = try performInAppBack(app: app) else {
120+
return XCTFail("a back button that only moved must not confirm a tap that navigated nowhere")
121+
}
122+
XCTAssertEqual(payload.code, RunnerWireErrorCode.backSwipeUnverified)
123+
XCTAssertEqual(lastNavigationBarBackTraceForTesting?.pressed, true)
124+
XCTAssertTrue(app.navigationBars["Detail"].exists)
125+
XCTAssertNotEqual(backButton.frame.minY, frameBefore.minY, "the fixture must move the back button")
126+
}
127+
109128
/// An XCTest issue while the back button is looked up, such as a button gone mid-transition, means
110129
/// no button: back answers from the swipe path. An issue that escaped the read would fail this
111130
/// test as it fails the command with a recorded failure. The swipe's own outcome is not asserted:
@@ -651,7 +670,7 @@ extension RunnerTests {
651670
/// screen origin.
652671
func testNavigationBarBackStateHoldsTheLiveButtonOfAPlacedWindow() {
653672
let snapshotButton = CGRect(x: 8, y: 54, width: 44, height: 44)
654-
let state = NavigationBarBackState(entries: ["Detail"], backButtonFrames: [snapshotButton])
673+
let state = NavigationBarBackState(backButtonLabels: [["Root"]], backButtonFrames: [snapshotButton])
655674
let placed = SurfaceGeometry(
656675
windowOnScreen: SurfaceRect(CGRect(x: 224, y: 128, width: 386, height: 954)),
657676
panelNativeSize: nil,
@@ -674,7 +693,7 @@ extension RunnerTests {
674693
/// fractional frame values do not defeat the match; a button a point away is another button.
675694
func testNavigationBarBackStateHoldsALiveButtonWithinHalfAPointPerEdge() {
676695
let state = NavigationBarBackState(
677-
entries: ["Root"],
696+
backButtonLabels: [["Root"]],
678697
backButtonFrames: [CGRect(x: 8.33, y: 54.67, width: 44.33, height: 44)]
679698
)
680699
let placed = SurfaceGeometry(
@@ -692,8 +711,8 @@ extension RunnerTests {
692711
/// Two agreeing reads of the bar show a still bar only when they started at least one read
693712
/// interval apart: two reads drawn in one animation frame of a push agree, but prove nothing.
694713
func testNavigationBarBackStateHeldStillOnlyAcrossOneReadInterval() {
695-
let bar = NavigationBarBackState(entries: ["Root"], backButtonFrames: [CGRect(x: 8, y: 54, width: 44, height: 44)])
696-
let moved = NavigationBarBackState(entries: ["Root"], backButtonFrames: [CGRect(x: 40, y: 54, width: 44, height: 44)])
714+
let bar = NavigationBarBackState(backButtonLabels: [["Root"]], backButtonFrames: [CGRect(x: 8, y: 54, width: 44, height: 44)])
715+
let moved = NavigationBarBackState(backButtonLabels: [["Root"]], backButtonFrames: [CGRect(x: 40, y: 54, width: 44, height: 44)])
697716
let intervalMs = Self.navigationBarBackReadInterval * 1000
698717
let first = TimedNavigationBarBackState(state: bar, startedMs: 1000)
699718

@@ -704,6 +723,22 @@ extension RunnerTests {
704723
let movedLater = TimedNavigationBarBackState(state: moved, startedMs: 1000 + intervalMs)
705724
XCTAssertFalse(Self.navigationBarBackStateHeldStill(from: first, to: movedLater))
706725
}
726+
727+
/// After the tap, only a change of the bar count, the back button count, or a back button label
728+
/// shows a pop. A back button that only moved, such as by a large-title collapse, shows none.
729+
func testNavigationBarBackPoppedOnlyOnABarOrBackButtonChange() {
730+
let button = CGRect(x: 8, y: 54, width: 44, height: 44)
731+
let detail = NavigationBarBackState(backButtonLabels: [["Root"]], backButtonFrames: [button])
732+
733+
let moved = NavigationBarBackState(backButtonLabels: [["Root"]], backButtonFrames: [button.offsetBy(dx: 0, dy: 30)])
734+
XCTAssertFalse(Self.navigationBarBackPopped(from: detail, to: moved))
735+
let relabeled = NavigationBarBackState(backButtonLabels: [["Settings"]], backButtonFrames: [button])
736+
XCTAssertTrue(Self.navigationBarBackPopped(from: detail, to: relabeled))
737+
let atRoot = NavigationBarBackState(backButtonLabels: [[]], backButtonFrames: [])
738+
XCTAssertTrue(Self.navigationBarBackPopped(from: detail, to: atRoot))
739+
let barGone = NavigationBarBackState(backButtonLabels: [], backButtonFrames: [])
740+
XCTAssertTrue(Self.navigationBarBackPopped(from: atRoot, to: barGone))
741+
}
707742
#endif
708743

709744
/// A still screen settles before the budget runs out at any capture time: the settle loop's

‎apple/runner/RUNNER_PROTOCOL.md‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,16 @@ candidate is found in a readable tree, and two reads in a row of the bar state,
142142
places the snapshot frames at the window's screen origin, because a placed iPad window reports them
143143
from its own top-left, and allows 0.5 pt per edge for fractional frame values. No other button
144144
counts, whatever its label.
145-
The bar state is the label and frame of each navigation bar's `BackButton` children, one entry per
146-
bar in bar order, so the bar count counts too; one XCTest snapshot per bar. Bar titles and
147-
identifiers are left out, because a bar can retitle itself (a countdown) without navigating. After
148-
the tap it reads that state again with a 0.05 s pause between reads. A change from the state read
149-
before the tap answers `ok`. A pop that leaves the back button as it was, such as one between two
150-
screens whose back buttons have the same title, answers `BACK_SWIPE_UNVERIFIED`; so can a pop on a
145+
The bar state is the labels of each navigation bar's `BackButton` children, one list per bar in
146+
bar order, and the frames of those buttons; one XCTest snapshot per bar. The still check before the
147+
tap compares labels and frames. Bar titles and identifiers are left out, because a bar can retitle
148+
itself (a countdown) without navigating. After the tap it reads that state again with a 0.05 s
149+
pause between reads. Only these changes from the state read before the tap answer `ok`: the number
150+
of bars, the number of `BackButton` children (such as none at the root), or a `BackButton` label.
151+
Frames are left out after the tap, because a tap that navigates nowhere can still move the button
152+
(a large-title collapse, a bar button that appears). A pop that keeps the same `BackButton` labels
153+
and bar count, such as one between two screens whose back buttons have the same title, answers
154+
`BACK_SWIPE_UNVERIFIED`; so can a pop on a
151155
back button whose title updates itself, misread as a navigation. No change, or reads that fail, answer `BACK_SWIPE_UNVERIFIED`; a delivered tap is
152156
never followed by the swipe. Before and after the tap, reads start for up to 1 s each, checked
153157
before each bar's snapshot; a query in flight finishes or ends at XCTest's own query timeout.

‎packages/command-surface/src/system/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ export const SYSTEM_COMMAND_OUTPUT_SCHEMAS = {
286286
const appStateCommandDescription =
287287
'Show foreground app/activity (Android and HarmonyOS; iOS and macOS answer per session)';
288288
const backCommandDescription =
289-
'Navigate back in the app or through system navigation. The in-app mode targets the app navigation stack; system targets platform back behavior. On iOS both modes press the navigation bar system back button (identifier BackButton) only when exactly one hittable one is found in a readable accessibility tree, and succeed only when the bar count or a back button then changes (a bar title alone never counts); with no such button, two candidates, or an unreadable tree, they swipe from the left edge instead (on an iPad whose app window cannot be resolved, only while the app fills the screen) and succeed only when the app screen changes. Reason back_swipe_no_change: the swipe was sent and the screen did not change; a visible back or close control is an alternative. Reason back_swipe_unverified: the button was pressed but the navigation bar did not change within 1 s, or the swipe may or may not have navigated, because the screen kept changing before it or could not be sampled after it; the resulting screen is unconfirmed. Reason back_swipe_not_dispatched, or a screen that cannot be sampled before the swipe, means nothing was sent. An app with a left-edge drawer may open the drawer instead. A visible back or close control can be targeted directly by its ref.';
289+
'Navigate back in the app or through system navigation. The in-app mode targets the app navigation stack; system targets platform back behavior. On iOS both modes press the navigation bar system back button (identifier BackButton) only when exactly one hittable one is found in a readable accessibility tree, and succeed only when the number of navigation bars, the number of back buttons, or a back button title then changes (a bar title or a moved back button alone never counts, so a pop that keeps the same back button title and bar count reports back_swipe_unverified); with no such button, two candidates, or an unreadable tree, they swipe from the left edge instead (on an iPad whose app window cannot be resolved, only while the app fills the screen) and succeed only when the app screen changes. Reason back_swipe_no_change: the swipe was sent and the screen did not change; a visible back or close control is an alternative. Reason back_swipe_unverified: the button was pressed but the navigation bar did not change within 1 s, or the swipe may or may not have navigated, because the screen kept changing before it or could not be sampled after it; the resulting screen is unconfirmed. Reason back_swipe_not_dispatched, or a screen that cannot be sampled before the swipe, means nothing was sent. An app with a left-edge drawer may open the drawer instead. A visible back or close control can be targeted directly by its ref.';
290290
const homeCommandDescription =
291291
'Send the selected device to its home screen. This leaves the app session open but moves the foreground away from the app.';
292292
const orientationCommandDescription =

0 commit comments

Comments
 (0)