Skip to content

Commit 9d6012c

Browse files
authored
fix(ios): stop a fill's post-focus reads from recording an XCTest failure (#3238) (#3329)
* fix(ios): stop a fill's post-focus reads from recording an XCTest failure (#3238) #3237 settled this on the tap route; the type/fill focus path kept two reads of the element it resolved BEFORE its own focus tap. `textEntryRefreshPoint(for:)` read `element.frame` after that tap to build the target's refresh point, and `moveCaretToEnd(element:)` read it before the caret edge-tap reached from `clearTextInput` on a replacement fill. A field that stops answering the query that resolved it once it takes focus — the #3060 Flutter shape — turns either read into a recorded "No matches found for …", which the dispatch path converts into XCTEST_RECORDED_FAILURE plus an invalidated target for a focus the gesture had already delivered. The report's `fill @e6` row measured 4.5 s at this seam. Both frames now come from `textEntrySnapshotFrame`, which reads through `snapshot()` — the throwing channel that records nothing, already how `probeTextEntryInput` and `withElement` reach an element. Where the frame cannot be answered the call sites degrade per the route's own rules: the refresh point drops to NO point rather than the pre-focus one, because this route's first resolve is not yet bound to an identity and a field that went unqueryable on focus may have moved the layout so the old point names another input, so the fill fails closed with a typed reason instead of typing into that input; the caret edge-tap skips to the caller's point-free delete burst rather than dispatching a point from a frame the field may have moved. Runner-only, no CLI/daemon/API change. Reads that distinguish absence from failure stay on `probeTextEntryInput`. A fill whose field genuinely did not take focus still fails typed, and a genuinely recorded failure still invalidates and surfaces. The regression test drives `fill` through `execute(command:)` on the unqueryable-on-focus fixture: red before the change at the refresh-point read, green after with the app-side focus witness proving the gesture landed. * fix(ios): dispatch the caret edge-tap app-relative instead of through the handle (#3329 review) A successful `snapshot()` proves the handle answered its query when the frame was read, not that it still answers when the tap dispatches — and an element-anchored `coordinate(withNormalizedOffset:).tap()` re-runs the resolving query to place its anchor when the action executes, the same #3060 recorder one call later. Both branches now leave the handle: the edge-tap point is computed from the snapshot frame and dispatched through `tapAt`, the app-relative coordinate route that resolves no element at all — the shape `waitForTextEntryReadinessAfterTap` settled on in #3237 — and the empty-frame `element.tap()` (how a departed handle reads) is folded into the same fail-closed guard, degrading to the point-free delete burst rather than tapping a handle that may not resolve. `clearTextInput` threads `app` to reach `tapAt`; its three call sites already hold it. No CLI/daemon/API change.
1 parent 5511ada commit 9d6012c

4 files changed

Lines changed: 111 additions & 18 deletions

File tree

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

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ extension RunnerTests {
243243
}
244244
}
245245

246-
func clearTextInput(_ element: XCUIElement) {
246+
func clearTextInput(app: XCUIApplication, _ element: XCUIElement) {
247247
// Skip the clear (delete burst + moveCaretToEnd edge-tap) ONLY when we can confirm the
248248
// field is empty. Why skip: the edge-tap computes a point from the element frame, which can
249249
// be stale after the field repositions on focus (e.g. the Settings search bar jumps
@@ -257,7 +257,7 @@ extension RunnerTests {
257257
return
258258
}
259259
#if !os(tvOS)
260-
moveCaretToEnd(element: element)
260+
moveCaretToEnd(app: app, element: element)
261261
#endif
262262
let count = estimatedDeleteCount(for: element)
263263
let deletes = String(repeating: XCUIKeyboardKey.delete.rawValue, count: count)
@@ -357,6 +357,21 @@ extension RunnerTests {
357357
return app.descendants(matching: identity.elementType).matching(identifier: identity.identifier).element
358358
}
359359

360+
/// Reads the frame a post-focus point is computed from, through the channel that records nothing.
361+
/// A tap holds a handle bound to the query that resolved its element, and asking that handle for
362+
/// `element.frame` re-runs the query: an element that stopped answering it once it took focus — a
363+
/// Flutter password field whose two accessibility channels disagree when focused (#3060) — makes
364+
/// XCTest record "No matches found for …", which ends the runner session for work the dispatch had
365+
/// already landed. `snapshot()` answers the same query through the throwing channel and records
366+
/// nothing, which is already how `probeTextEntryInput` and `withElement` reach an element. Nil means
367+
/// no trustworthy frame: callers must degrade to a route that needs no point rather than dispatch
368+
/// one computed from where the element used to be.
369+
func textEntrySnapshotFrame(_ element: XCUIElement) -> CGRect? {
370+
// Skipped when the input is already gone, so a removed field does not pay the snapshot's
371+
// ~2-second wait for a match before its point degrades (the same guard `withElement` makes).
372+
safely("TEXT_ENTRY_SNAPSHOT_FRAME", { element.exists ? try? element.snapshot() : nil })?.frame
373+
}
374+
360375
/// Snapshots one candidate: its identity, a proven no-match, or a failure that proves nothing.
361376
func probeTextEntryInput(_ element: XCUIElement) -> TextEntryInputProbe {
362377
var probe = TextEntryInputProbe.unavailable
@@ -395,20 +410,29 @@ extension RunnerTests {
395410
return nil
396411
}
397412

398-
private func moveCaretToEnd(element: XCUIElement) {
413+
private func moveCaretToEnd(app: XCUIApplication, element: XCUIElement) {
399414
#if os(tvOS)
400415
return
401416
#else
402-
let frame = element.frame
403-
guard !frame.isEmpty else {
404-
element.tap()
417+
// Both the point AND its dispatch must stay off the handle: this runs AFTER the command's own
418+
// focus tap, and an input that stopped answering its resolving query on focus (#3060) makes
419+
// every post-focus handle touch a recorder — `element.frame` records directly, and an
420+
// element-anchored `coordinate(...).tap()` re-runs that same query to resolve its anchor when
421+
// the action executes. A fresh `snapshot()` proves the handle answered moments earlier, not
422+
// that it still answers at tap-dispatch time. So the point comes from the snapshot frame and
423+
// goes out app-relative through `tapAt`, which resolves no element at all — the shape
424+
// `waitForTextEntryReadinessAfterTap` settled on in #3237. With no trustworthy frame, or a
425+
// zero-size one (how a departed handle reads), there is no point to dispatch — an edge-tap
426+
// from a frame the field moved after records a failure or navigates away, the shape
427+
// `clearTextInput`'s comment refuses — so degrade to the caller's point-free delete burst.
428+
guard let frame = textEntrySnapshotFrame(element), !frame.isEmpty else {
405429
return
406430
}
407-
let origin = element.coordinate(withNormalizedOffset: CGVector(dx: 0, dy: 0))
408-
let target = origin.withOffset(
409-
CGVector(dx: max(2, frame.width - 4), dy: max(2, frame.height / 2))
431+
_ = tapAt(
432+
app: app,
433+
x: frame.minX + max(2, frame.width - 4),
434+
y: frame.minY + max(2, frame.height / 2)
410435
)
411-
target.tap()
412436
#endif
413437
}
414438

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ extension RunnerTests {
137137
if keyboardVisibleBeforeTap, let target {
138138
return TextEntryTarget(
139139
element: target,
140-
refreshPoint: textEntryRefreshPoint(for: target) ?? requestedPoint,
140+
refreshPoint: textEntryRefreshPoint(for: target, fallbackTo: requestedPoint),
141141
prefersFocusedElement: false
142142
)
143143
}
@@ -157,18 +157,32 @@ extension RunnerTests {
157157
: (waitForTextEntryReadiness(app: app, target: readyTarget) ?? stabilized.element ?? target)
158158
return TextEntryTarget(
159159
element: element,
160-
refreshPoint: textEntryRefreshPoint(for: element) ?? requestedPoint,
160+
refreshPoint: textEntryRefreshPoint(for: element, fallbackTo: requestedPoint),
161161
prefersFocusedElement: false
162162
)
163163
}
164164

165-
private func textEntryRefreshPoint(for element: XCUIElement?) -> CGPoint? {
165+
/// The field's current center for a refresh point that survives a focus move, or `requestedPoint`
166+
/// when the field reads with an empty frame. Read through `textEntrySnapshotFrame`, not
167+
/// `element.frame`: this runs after the focus tap, and a field that stopped answering its
168+
/// resolving query on focus (#3060) turns that read into a recorded failure which ends the runner
169+
/// session for a focus the dispatch had already landed. When the read could not answer at all,
170+
/// the target gets NO refresh point rather than the pre-focus one: this route's first resolve is
171+
/// not yet bound to an identity, and a field that went unqueryable on focus may have moved the
172+
/// layout so the old point names a different input, so fail closed with a typed reason instead of
173+
/// typing into that other field.
174+
private func textEntryRefreshPoint(
175+
for element: XCUIElement?,
176+
fallbackTo requestedPoint: CGPoint?
177+
) -> CGPoint? {
166178
guard let element else {
179+
return requestedPoint
180+
}
181+
guard let frame = textEntrySnapshotFrame(element) else {
167182
return nil
168183
}
169-
let frame = element.frame
170184
guard !frame.isEmpty else {
171-
return nil
185+
return requestedPoint
172186
}
173187
return CGPoint(x: frame.midX, y: frame.midY)
174188
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ extension RunnerTests {
3434
failure: .notFocused
3535
)
3636
}
37-
clearTextInput(clearTarget)
37+
clearTextInput(app: app, clearTarget)
3838
// nil means the value is unreadable (secure fields), not "not empty": leave `verified`
3939
// nil there rather than reporting a mismatch the runner cannot actually observe.
4040
let observed = editableTextValue(for: clearTarget, treatingPlaceholderAsEmpty: true)
@@ -125,7 +125,7 @@ extension RunnerTests {
125125
}
126126
if currentText == nil || currentText?.isEmpty == false {
127127
let clearStartedAt = Date()
128-
clearTextInput(replacementTarget)
128+
clearTextInput(app: app, replacementTarget)
129129
activeTarget = activeTarget.withElement(replacementTarget)
130130
logTextEntryPhase(
131131
commandId: commandId,
@@ -465,7 +465,7 @@ extension RunnerTests {
465465
expectedText.count,
466466
observedText.count
467467
)
468-
clearTextInput(repairTarget)
468+
clearTextInput(app: app, repairTarget)
469469
repairTarget.typeText(expectedText)
470470
let repairedTarget = target.withElement(repairTarget)
471471
let repairedResult = verifyTextEntry(

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,61 @@ extension RunnerTests {
214214
)
215215
XCTAssertNotNil(textEntryTapWitness)
216216
}
217+
218+
/// #3238: the report's `fill @e6` row. A coordinate `fill` resolves the input, taps it for focus,
219+
/// and then reads that handle's frame to build the target's refresh point — after the tap. A field
220+
/// that stopped answering its resolving query on focus turns that read into a recorded
221+
/// "No matches found for …", which the dispatch path turns into `XCTEST_RECORDED_FAILURE` plus an
222+
/// invalidated target for a focus the gesture had already delivered. The fill must still terminate
223+
/// with a typed reason — this fixture's field leaves the accessibility tree entirely, so the
224+
/// runner cannot resolve it to type into and refuses rather than typing app-wide — and it must
225+
/// record nothing on the way there.
226+
@MainActor
227+
func testCoordinateFillOnInputThatLeavesTheTreeOnFocusFailsTypedAndRecordsNothing() throws {
228+
app.launchArguments = [
229+
"--agent-device-text-entry-regression",
230+
"--agent-device-text-entry-unqueryable-on-focus",
231+
]
232+
app.launch()
233+
defer {
234+
invalidateCachedTarget(reason: "unit_test_cleanup")
235+
app.terminate()
236+
}
237+
XCTAssertTrue(app.waitForExistence(timeout: appExistenceTimeout))
238+
let field = app.textFields["agent-device-hardware-keyboard-input"]
239+
XCTAssertTrue(field.waitForExistence(timeout: appExistenceTimeout))
240+
// The fixture keeps its keyboard down with an empty inputView, but a simulator with the software
241+
// keyboard forced on takes the fill's keyboard-visible branch, which has different resolution.
242+
// That is an environment fact, so the test that pins the hidden-keyboard branch skips on it.
243+
try XCTSkipIf(
244+
isKeyboardVisible(app: app),
245+
"software keyboard is up: this simulator cannot exercise the hidden-keyboard replacement path"
246+
)
247+
let frame = field.frame
248+
mainOwned.app = app
249+
mainOwned.bundleId = "com.callstack.agentdevice.runner"
250+
mainOwned.processIdentifier = try XCTUnwrap(Self.processIdentifier(of: app))
251+
252+
let failures = currentXCTestFailureCount()
253+
let fill = try runnerCommandFixture(
254+
#"{"appBundleId":"com.callstack.agentdevice.runner","command":"type","commandId":"fill-unqueryable-on-focus","text":"fresh","textEntryMode":"replace","x":\#(frame.midX),"y":\#(frame.midY)}"#
255+
)
256+
let response = try execute(command: fill)
257+
258+
XCTAssertFalse(
259+
didRecordXCTestFailure(since: failures),
260+
String(describing: response.error)
261+
)
262+
XCTAssertFalse(response.ok, String(describing: response.data))
263+
XCTAssertEqual(response.error?.code, "TEXT_INPUT_NOT_FOCUSED")
264+
// The app's own delegate callback: the gesture landed and took focus; what the old seam lost was
265+
// the session, not the tap. Without this, a fill that never reached the field would pass too.
266+
XCTAssertEqual(
267+
app.staticTexts["agent-device-text-entry-focus"].label,
268+
"focus",
269+
"the fill's focus tap must have reached the field"
270+
)
271+
}
217272
#endif
218273
}
219274
#endif

0 commit comments

Comments
 (0)