Skip to content

Commit d9065c5

Browse files
thymikeeclaude
andcommitted
perf(ios): carry the focus-confirming read in its evidence
The tapped input's read rides in TextEntryFocusEvidence.inputReportsKeyboardFocus, so the evidence and the post-focus read cannot disagree. One read per poll serves both the input's own focus check and the app-wide focus query's expected snapshot. The keyboard-raising fill test skips before it asserts and pins the fixture's value-read count at 18 (17 measured, 19 when the entry resolves the input again). Device timings move from comments to the PR. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 3244fec commit d9065c5

4 files changed

Lines changed: 69 additions & 42 deletions

File tree

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

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -312,26 +312,35 @@ extension RunnerTests {
312312
}
313313

314314
/// What confirmed that a focus tap's input took focus.
315-
enum TextEntryFocusEvidence: String {
315+
enum TextEntryFocusEvidence {
316316
/// tvOS moves focus with the focus engine, so there is nothing to wait for.
317-
case focusEngine = "focus-engine"
317+
case focusEngine
318318
/// The app-wide focus query named a text input (not iOS).
319-
case focusedInput = "focused-input"
320-
/// The tapped input's own snapshot reports keyboard focus.
321-
case inputReportsKeyboardFocus = "input-reports-keyboard-focus"
319+
case focusedInput
320+
/// The tapped input's own read reports keyboard focus. That read is the focus step's post-focus
321+
/// read.
322+
case inputReportsKeyboardFocus(ResolvedTextEntryInput)
322323
/// A software keyboard appeared after the tap, so some input took focus.
323-
case keyboardAppeared = "keyboard-appeared"
324+
case keyboardAppeared
324325
/// The app-wide keyboard-focus query answered with the tapped input.
325-
case focusQueryMatchesInput = "focus-query-matches-input"
326+
case focusQueryMatchesInput
327+
328+
/// The name `runner.log` records.
329+
var name: String {
330+
switch self {
331+
case .focusEngine: return "focus-engine"
332+
case .focusedInput: return "focused-input"
333+
case .inputReportsKeyboardFocus: return "input-reports-keyboard-focus"
334+
case .keyboardAppeared: return "keyboard-appeared"
335+
case .focusQueryMatchesInput: return "focus-query-matches-input"
336+
}
337+
}
326338
}
327339

328340
struct TextEntryStabilization {
329341
let element: XCUIElement?
330342
/// Nil when nothing confirmed focus before the focus timeout.
331343
let evidence: TextEntryFocusEvidence?
332-
/// The read of `element` that confirmed focus, when the input's own read did. It serves as the
333-
/// focus step's post-focus read.
334-
var focusRead: ResolvedTextEntryInput? = nil
335344

336345
var focusConfirmed: Bool { evidence != nil }
337346
}

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,12 @@ extension RunnerTests {
7474
}
7575
#if os(iOS)
7676
// The tapped input's own read comes before the keyboard. While a software keyboard is
77-
// presented, the keyboard query blocks and then finds none: after a fill's focus tap it took
78-
// 0.68 s on an iPhone 17 Pro simulator (iOS 27.0) and 0.57–3.77 s on CI. An input that
79-
// reports keyboard focus is stronger evidence than the keyboard's arrival, which shows only
80-
// that some input took focus.
81-
if let target, let read = ResolvedTextEntryInput(reading: target), let snapshot = read.snapshot,
82-
snapshotHasKeyboardFocus(snapshot)
83-
{
84-
return TextEntryStabilization(element: target, evidence: .inputReportsKeyboardFocus, focusRead: read)
77+
// presented, the keyboard query blocks and then finds none. An input that reports keyboard
78+
// focus is stronger evidence than the keyboard's arrival, which shows only that some input
79+
// took focus.
80+
let read = target.flatMap(ResolvedTextEntryInput.init(reading:))
81+
if let read, let snapshot = read.snapshot, snapshotHasKeyboardFocus(snapshot) {
82+
return TextEntryStabilization(element: read.element, evidence: .inputReportsKeyboardFocus(read))
8583
}
8684
#endif
8785
// focusedTextInput is intentionally nil on iOS; the keyboard transitioning to visible after
@@ -90,7 +88,8 @@ extension RunnerTests {
9088
return TextEntryStabilization(element: target, evidence: .keyboardAppeared)
9189
}
9290
#if os(iOS)
93-
if let target, keyboardFocusConfirmed(app: app, element: target) {
91+
// The read came before the keyboard query, so it is not handed on as the post-focus read.
92+
if let read, keyboardFocusConfirmed(app: app, read: read) {
9493
return TextEntryStabilization(element: target, evidence: .focusQueryMatchesInput)
9594
}
9695
#endif
@@ -149,8 +148,8 @@ extension RunnerTests {
149148
target: target,
150149
keyboardVisibleBeforeTap: keyboardVisibleBeforeTap
151150
)
152-
NSLog("AGENT_DEVICE_RUNNER_TEXT_ENTRY_FOCUS evidence=%@", stabilized.evidence?.rawValue ?? "none")
153-
if let read = stabilized.focusRead {
151+
NSLog("AGENT_DEVICE_RUNNER_TEXT_ENTRY_FOCUS evidence=%@", stabilized.evidence?.name ?? "none")
152+
if case .inputReportsKeyboardFocus(let read) = stabilized.evidence {
154153
return focusedTextEntryTarget(read.element, read: read, requestedPoint: requestedPoint)
155154
}
156155
let readyTarget = TextEntryTarget(

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,22 @@ extension RunnerTests {
129129
/// fallback at all.
130130
func keyboardFocusConfirmed(app: XCUIApplication, element: XCUIElement) -> Bool {
131131
#if os(iOS)
132+
return ResolvedTextEntryInput(reading: element).map { keyboardFocusConfirmed(app: app, read: $0) } ?? false
133+
#else
134+
return false
135+
#endif
136+
}
137+
138+
#if os(iOS)
139+
/// `keyboardFocusConfirmed(app:element:)` against a read of the element already taken.
140+
func keyboardFocusConfirmed(app: XCUIApplication, read: ResolvedTextEntryInput) -> Bool {
141+
// An element that no longer resolves reads as identifier "" and frame `.zero`, which would
142+
// match any focused element that also reports an empty frame. Require a real frame first,
143+
// so a dead handle cannot corroborate anything.
144+
guard let expected = read.snapshot, !expected.frame.isEmpty else {
145+
return false
146+
}
132147
return safely("TEXT_ENTRY_FOCUS_CONFIRMED", false) {
133-
// An element that no longer resolves reads as identifier "" and frame `.zero`, which would
134-
// match any focused element that also reports an empty frame. Require a real frame first,
135-
// so a dead handle cannot corroborate anything.
136-
guard element.exists, let expected = try? element.snapshot(), !expected.frame.isEmpty else {
137-
return false
138-
}
139148
let focused = app
140149
.descendants(matching: .any)
141150
.matching(NSPredicate(format: "hasKeyboardFocus == 1"))
@@ -145,10 +154,8 @@ extension RunnerTests {
145154
}
146155
return observed.identifier == expected.identifier && observed.frame == expected.frame
147156
}
148-
#else
149-
return false
150-
#endif
151157
}
158+
#endif
152159

153160
private func keyboardElementExists(app: XCUIApplication) -> Bool {
154161
#if os(iOS)

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

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,8 @@ extension RunnerTests {
9292

9393
/// After a focus tap raises the software keyboard, stabilization takes focus from the tapped
9494
/// input's own read before it asks for the keyboard. While the keyboard is presented, the keyboard
95-
/// query blocks and then finds none: after a fill's focus tap it took 0.68 s on the test app
96-
/// (iPhone 17 Pro, iOS 27.0) and 0.57–3.77 s on CI. The keyboard is already up here, so asking it
97-
/// first confirms focus from the keyboard's arrival instead.
95+
/// query blocks and then finds none. The keyboard is already up here, so asking it first confirms
96+
/// focus from the keyboard's arrival instead.
9897
@MainActor
9998
func testFocusStabilizationAsksTheTappedInputBeforeTheKeyboard() throws {
10099
app.launchArguments = ["--agent-device-text-entry-regression", "--agent-device-text-entry-soft-keyboard"]
@@ -111,35 +110,48 @@ extension RunnerTests {
111110

112111
let stabilized = stabilizeTextInputBeforeTyping(app: app, target: field, keyboardVisibleBeforeTap: false)
113112

114-
XCTAssertEqual(stabilized.evidence, .inputReportsKeyboardFocus)
115-
XCTAssertEqual(stabilized.focusRead?.snapshot?.frame, field.frame)
113+
guard case .inputReportsKeyboardFocus(let read) = stabilized.evidence else {
114+
return XCTFail("focus confirmed by \(stabilized.evidence?.name ?? "nothing"), not by the tapped input")
115+
}
116+
XCTAssertEqual(read.snapshot?.frame, field.frame)
116117
}
117118

118-
/// A fill whose focus tap raises the software keyboard types and reads its text back when focus is
119-
/// confirmed from the input rather than from the keyboard's arrival.
119+
/// A fill whose focus tap raises the software keyboard hands the read that confirmed focus to the
120+
/// entry as its first read. The fixture counts queries as in
121+
/// `testFillReadsEachResolvedFieldThroughOneSnapshot`: this fill made 17, and 19 when the entry
122+
/// resolved the input again. The one read of headroom covers a keyboard that arrives before the
123+
/// field reports focus.
120124
@MainActor
121-
func testFillThatRaisesTheSoftwareKeyboardTypesAndReadsBack() throws {
122-
app.launchArguments = ["--agent-device-text-entry-regression", "--agent-device-text-entry-soft-keyboard"]
125+
func testFillThatRaisesTheSoftwareKeyboardReusesTheReadThatConfirmedFocus() throws {
126+
app.launchArguments = [
127+
"--agent-device-text-entry-regression",
128+
"--agent-device-text-entry-soft-keyboard",
129+
"--agent-device-text-entry-counts-value-reads",
130+
]
123131
app.launch()
124132
defer { invalidateCachedTarget(reason: "unit_test_cleanup"); app.terminate() }
125133
XCTAssertTrue(app.waitForFixtureExistence(timeout: appExistenceTimeout))
126134
let field = app.textFields["agent-device-hardware-keyboard-input"]
127135
XCTAssertTrue(field.waitForFixtureExistence(timeout: appExistenceTimeout))
128136
XCTAssertFalse(isKeyboardVisible(app: app))
137+
let valueReads = app.staticTexts["agent-device-text-entry-value-reads"]
129138
let frame = field.frame
130139
let fill = try runnerCommandFixture(
131140
#"{"command":"type","commandId":"fill-raises-keyboard","text":"hello","textEntryMode":"replace","x":\#(frame.midX),"y":\#(frame.midY)}"#
132141
)
133142

143+
let readsBefore = try XCTUnwrap(Int(valueReads.label))
134144
let response = executeTypeCommand(activeApp: app, command: fill)
145+
let readsAfter = try XCTUnwrap(Int(valueReads.label))
135146

136-
XCTAssertTrue(response.ok, String(describing: response.error))
137-
XCTAssertEqual(response.data?.textEntryRoute, "xctest-element")
138-
XCTAssertEqual(field.value as? String, "hello")
139147
try XCTSkipUnless(
140148
app.keyboards.firstMatch.waitForFixtureExistence(timeout: appExistenceTimeout),
141149
"software keyboard is down: this simulator did not raise the keyboard the focus tap asked for"
142150
)
151+
XCTAssertTrue(response.ok, String(describing: response.error))
152+
XCTAssertEqual(response.data?.textEntryRoute, "xctest-element")
153+
XCTAssertEqual(valueReads.value as? String, "hello")
154+
XCTAssertLessThanOrEqual(readsAfter - readsBefore, 18)
143155
}
144156
#endif
145157
}

0 commit comments

Comments
 (0)