Skip to content

Commit ccd0c5c

Browse files
fix(ios): clear a long text value wherever the caret lands (#3908)
* fix(ios): clear a long text value wherever the caret lands (#3851) * fix(ios): keep a clear's retries on the input it started on The clear's handle can be index-bound, so an input that replaced the cleared one, such as the next screen's after the delete navigated, read through it and got the next select-all and delete. * refactor(ios): name the shared clear loop for what it does, and keep re-reads of an input read without a snapshot * refactor(ios): post and read a clear through the text entry reader resolveTextEntryInput already owns which input a target may reach, so the clear re-reads through the caller's reader instead of a local identity check. * fix(ios): fail an append repair whose clear lost the input
1 parent 59ec1a1 commit ccd0c5c

6 files changed

Lines changed: 193 additions & 30 deletions

File tree

‎apple/runner/AgentDeviceRunner/AgentDeviceRunner/AgentDeviceRunnerApp.m‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -539,14 +539,16 @@ - (void)agentDeviceTextEntryDidChange:(UITextField *)textField {
539539
}
540540
// An auto-submitting one-time-code field: the last digit navigates away, to a screen whose own
541541
// input takes the code field's place or to one without an input. `replace-after-input` is the
542-
// same navigation landing before the code is complete.
542+
// same navigation landing before the code is complete, and `populated-successor` the same on any
543+
// edit, clearing included, to a screen whose input already holds a value.
543544
NSArray<NSString *> *arguments = NSProcessInfo.processInfo.arguments;
544545
BOOL codeComplete = textField.text.length >= AgentDeviceTextEntryAutoSubmitLength;
545546
if (textField.superview == nil) {
546547
return;
547548
}
548549
if ((codeComplete && [arguments containsObject:@"--agent-device-text-entry-auto-submit"]) ||
549-
(textField.text.length > 0 && [arguments containsObject:@"--agent-device-text-entry-replace-after-input"])) {
550+
(textField.text.length > 0 && [arguments containsObject:@"--agent-device-text-entry-replace-after-input"]) ||
551+
[arguments containsObject:@"--agent-device-text-entry-populated-successor"]) {
550552
[self replaceTextEntryFieldWithNextScreenInput:textField];
551553
} else if (codeComplete &&
552554
[arguments containsObject:@"--agent-device-text-entry-auto-submit-without-successor"]) {
@@ -559,6 +561,9 @@ - (void)replaceTextEntryFieldWithNextScreenInput:(UITextField *)textField {
559561
if (![NSProcessInfo.processInfo.arguments containsObject:@"--agent-device-text-entry-unnamed-input"]) {
560562
nextScreenField.accessibilityIdentifier = @"agent-device-auto-submit-next-screen-input";
561563
}
564+
if ([NSProcessInfo.processInfo.arguments containsObject:@"--agent-device-text-entry-populated-successor"]) {
565+
nextScreenField.text = @"next screen value";
566+
}
562567
nextScreenField.borderStyle = UITextBorderStyleRoundedRect;
563568
[textField.superview addSubview:nextScreenField];
564569
[textField removeFromSuperview];
@@ -762,6 +767,10 @@ - (void)viewDidLoad {
762767
if (![NSProcessInfo.processInfo.arguments containsObject:@"--agent-device-text-entry-unnamed-input"]) {
763768
textField.accessibilityIdentifier = @"agent-device-hardware-keyboard-input";
764769
}
770+
if ([NSProcessInfo.processInfo.arguments containsObject:@"--agent-device-text-entry-long-prefill"]) {
771+
// Longer than one clear burst and far wider than the field, so its end is scrolled out of view.
772+
textField.text = [@"" stringByPaddingToLength:300 withString:@"stale long value " startingAtIndex:0];
773+
}
765774
textField.borderStyle = UITextBorderStyleRoundedRect;
766775
// An empty input view keeps the software keyboard down, which is the hardware-keyboard responder
767776
// these routes are addressed to. `--agent-device-text-entry-soft-keyboard` leaves the real input

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,15 +251,16 @@ extension RunnerTests {
251251
case cleared
252252
/// The field never exposes its value, so it is typed into unverified, as the commit wait leaves it.
253253
case unverified
254-
/// The field still held text after the last pass, or could not be read. Nothing is typed.
254+
/// The field still held text after the last pass, or could not be read. The synthesized post
255+
/// types nothing; the element route's clear falls back to delete bursts.
255256
case notCleared
256257
/// A clear post stopped the plan.
257258
case stopped
258259
}
259260

260261
/// Clears until the field reads empty, at most `maxClearPassCount` passes. A read that cannot
261262
/// answer ends the clear, so the post never types over text it could not see removed.
262-
static func clearForSynthesizedReplacement(
263+
static func clearUntilEmpty(
263264
clearOnce: () -> SynthesizedStepDispatch,
264265
read: () -> TextValueRead
265266
) -> SynthesizedClearOutcome {
@@ -378,7 +379,7 @@ extension RunnerTests {
378379
}
379380
if step.replacesExistingText {
380381
var passes = 0
381-
let outcome = Self.clearForSynthesizedReplacement(
382+
let outcome = Self.clearUntilEmpty(
382383
clearOnce: {
383384
passes += 1
384385
return enter(XCUIKeyboardKey.delete.rawValue, replacingExistingText: true)

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

Lines changed: 91 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -361,27 +361,109 @@ extension RunnerTests {
361361
}
362362
}
363363

364-
/// Clears `input`, which must have been read with nothing dispatched since.
365-
func clearTextInput(app: XCUIApplication, _ input: ResolvedTextEntryInput) {
366-
// Skip the clear (delete burst + moveCaretToEnd edge-tap) ONLY when we can confirm the
364+
/// Clears the reader's input. Every post goes through `reader.dispatch` and every read back through
365+
/// `reader.input()`, so a bound target's clear stops at an input that took its place. On iOS a
366+
/// readable value is selected whole and deleted, wherever the caret is. Otherwise, or when text is
367+
/// left, delete bursts run from the caret, and a value longer than one burst is read again after
368+
/// each burst until it reads empty, stops shortening, or has had at least one burst more than it
369+
/// needs.
370+
func clearTextInput(app: XCUIApplication, reader: inout TextEntryReader) {
371+
guard let input = reader.input() else {
372+
return
373+
}
374+
// Skip the clear (edge-tap, select all, delete bursts) ONLY when we can confirm the
367375
// field is empty. Why skip: the edge-tap computes a point from the element frame, which can
368376
// be stale after the field repositions on focus (e.g. the Settings search bar jumps
369377
// bottom->top and reveals a "Suggestions" list) — tapping there navigates away instead of
370378
// clearing; and replacing into an already-empty field is a no-op anyway.
371379
// editableTextValue returns nil for secure (and unknown) fields, where we CANNOT confirm
372380
// emptiness — those must still be cleared, or replace would concatenate stale + new text.
373381
// So distinguish nil (clear) from "" (skip).
374-
if let existing = editableTextValue(for: input.attributes, treatingPlaceholderAsEmpty: true),
375-
existing.isEmpty {
382+
let existing = editableTextValue(for: input.attributes, treatingPlaceholderAsEmpty: true)
383+
if existing?.isEmpty == true {
376384
return
377385
}
378386
// Read before the caret tap, which dispatches to the app.
379-
let count = estimatedDeleteCount(for: input.attributes)
387+
var valueLength = normalizedElementText(input.attributes.value).count
388+
// The tap also focuses the field, which a clear can reach with focus unconfirmed.
380389
#if !os(tvOS)
381-
moveCaretToEnd(app: app, input: input)
390+
reader.dispatch { moveCaretToEnd(app: app, input: input) }
391+
#endif
392+
// An input that stops answering once focused (#3060) cannot be read back, so it gets one burst.
393+
guard reader.input() != nil else {
394+
let burst = Self.textClearBurst(valueLength: valueLength)
395+
reader.dispatch { input.element.typeText(String(repeating: XCUIKeyboardKey.delete.rawValue, count: burst)) }
396+
return
397+
}
398+
#if os(iOS)
399+
// The tap aims at the end of what the field shows, which a long value has scrolled past, so
400+
// bursts from there reach only what is before the caret (#3851). Select all reaches all of it, but
401+
// only a read can show it took: a masked field can ignore it, so an unreadable value keeps the
402+
// bursts alone.
403+
if existing != nil {
404+
var passes = 0
405+
let outcome = Self.clearUntilEmpty(
406+
clearOnce: {
407+
guard let input = reader.input() else {
408+
return .stop
409+
}
410+
passes += 1
411+
reader.dispatch {
412+
input.element.typeKey("a", modifierFlags: .command)
413+
input.element.typeText(XCUIKeyboardKey.delete.rawValue)
414+
}
415+
return .posted
416+
},
417+
read: {
418+
textValueRead(for: reader.input()?.attributes, treatingPlaceholderAsEmpty: true)
419+
}
420+
)
421+
NSLog("AGENT_DEVICE_RUNNER_TEXT_ENTRY_CLEAR route=element passes=%d outcome=%@", passes, String(describing: outcome))
422+
// Text left after every pass goes to the bursts; an input that no longer resolves is not typed into.
423+
guard outcome == .notCleared,
424+
let remaining = reader.input().flatMap({ editableTextValue(for: $0.attributes, treatingPlaceholderAsEmpty: true) })
425+
else {
426+
return
427+
}
428+
valueLength = remaining.count
429+
}
382430
#endif
383-
let deletes = String(repeating: XCUIKeyboardKey.delete.rawValue, count: count)
384-
input.element.typeText(deletes)
431+
for _ in 0...(valueLength / Self.maxTextClearBurst + 1) {
432+
guard let input = reader.input() else {
433+
return
434+
}
435+
let burst = Self.textClearBurst(valueLength: valueLength)
436+
reader.dispatch { input.element.typeText(String(repeating: XCUIKeyboardKey.delete.rawValue, count: burst)) }
437+
// A burst that covers the value needs no read.
438+
guard burst < valueLength + Self.textClearSlack,
439+
let remaining = reader.input().flatMap({ editableTextValue(for: $0.attributes, treatingPlaceholderAsEmpty: true) }),
440+
Self.textClearContinues(clearedLength: valueLength, remainingLength: remaining.count)
441+
else {
442+
return
443+
}
444+
valueLength = remaining.count
445+
}
446+
}
447+
448+
/// Extra deletes past a value's length, for whitespace its normalized read trimmed.
449+
private static let textClearSlack = 8
450+
451+
/// The most deletes one clear burst sends. A longer value takes more bursts.
452+
private static let maxTextClearBurst = 120
453+
454+
/// Deletes for one clear burst on a value of `valueLength` characters, at least 24 for a value
455+
/// that reads empty but may not be.
456+
static func textClearBurst(valueLength: Int) -> Int {
457+
let base = valueLength == 0 ? 24 : valueLength + textClearSlack
458+
return max(24, min(maxTextClearBurst, base))
459+
}
460+
461+
/// Whether a clear sends another burst after one sized from `clearedLength` characters left
462+
/// `remainingLength`. Each burst must shorten the value, so a field that ignores deletes or
463+
/// rewrites its value ends the clear instead of looping; the fill's verification reports what
464+
/// remains.
465+
static func textClearContinues(clearedLength: Int, remainingLength: Int) -> Bool {
466+
remainingLength > 0 && remainingLength < clearedLength
385467
}
386468

387469
func isTextEntryElement(_ element: XCUIElement) -> Bool {
@@ -535,12 +617,6 @@ extension RunnerTests {
535617
#endif
536618
}
537619

538-
private func estimatedDeleteCount(for element: any XCUIElementAttributes) -> Int {
539-
let valueText = normalizedElementText(element.value)
540-
let base = valueText.isEmpty ? 24 : (valueText.count + 8)
541-
return max(24, min(120, base))
542-
}
543-
544620
private func normalizedElementText(_ value: Any?) -> String {
545621
String(describing: value ?? "")
546622
.trimmingCharacters(in: .whitespacesAndNewlines)

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ extension RunnerTests {
2323
// place while the response reported "typed". Append (`type`) and unrepaired entry stay
2424
// no-ops, which is what an empty payload means for them.
2525
if repairMode == .replacement {
26-
guard let clearTarget = reader.input() else {
26+
guard reader.input() != nil else {
2727
// No resolvable input means no clear happened — including the synthesized
2828
// first-responder route, whose target carries no element. Success here would claim a
2929
// clear the runner never performed.
@@ -36,7 +36,7 @@ extension RunnerTests {
3636
failure: .notFocused
3737
)
3838
}
39-
reader.dispatch { clearTextInput(app: app, clearTarget) }
39+
clearTextInput(app: app, reader: &reader)
4040
// nil means the value is unreadable (secure fields), not "not empty": leave `verified`
4141
// nil there rather than reporting a mismatch the runner cannot actually observe.
4242
let observed = editableTextValue(for: reader.input()?.attributes, treatingPlaceholderAsEmpty: true)
@@ -119,13 +119,13 @@ extension RunnerTests {
119119

120120
var entryBaseline: TextEntryObservation?
121121
if repairMode == .replacement {
122-
guard let replacementTarget = initialTarget else {
122+
guard initialTarget != nil else {
123123
logTextEntryPhase(commandId: commandId, phase: "total", startedAt: totalStartedAt, chars: text.count, mode: repairMode)
124124
return TextEntryResult(verified: nil, repaired: false, expectedText: expectedText, observedText: nil)
125125
}
126126
if currentText == nil || currentText?.isEmpty == false {
127127
let clearStartedAt = Date()
128-
reader.dispatch { clearTextInput(app: app, replacementTarget) }
128+
clearTextInput(app: app, reader: &reader)
129129
logTextEntryPhase(
130130
commandId: commandId,
131131
phase: "clear",
@@ -477,10 +477,18 @@ extension RunnerTests {
477477
expectedText.count,
478478
observedText.count
479479
)
480-
reader.dispatch {
481-
clearTextInput(app: app, repairTarget)
482-
repairTarget.element.typeText(expectedText)
480+
clearTextInput(app: app, reader: &reader)
481+
// An input the clear removed or let another take the place of has nowhere for the repair to go.
482+
guard let input = reader.input() else {
483+
return TextEntryResult(
484+
verified: nil,
485+
repaired: false,
486+
expectedText: expectedText,
487+
observedText: nil,
488+
failure: .commitNotObserved
489+
)
483490
}
491+
reader.dispatch { input.element.typeText(expectedText) }
484492
return verifyTextEntry(
485493
reader: &reader,
486494
expectedText: expectedText,

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,24 @@ extension RunnerTests {
123123
}
124124
}
125125

126+
// A clear burst covers a short value whole and caps a long one, which the clear then
127+
// finishes in more bursts (#3851).
128+
func testTextClearBurstCoversShortValuesAndCapsLongOnes() {
129+
XCTAssertEqual(Self.textClearBurst(valueLength: 0), 24)
130+
XCTAssertEqual(Self.textClearBurst(valueLength: 10), 24)
131+
XCTAssertEqual(Self.textClearBurst(valueLength: 100), 108)
132+
XCTAssertEqual(Self.textClearBurst(valueLength: 501), 120)
133+
}
134+
135+
// A clear goes on only while each burst shortens the value, so a field that ignores deletes or
136+
// rewrites its value cannot keep it looping.
137+
func testTextClearContinuesOnlyWhileTheValueShortens() {
138+
XCTAssertTrue(Self.textClearContinues(clearedLength: 501, remainingLength: 381))
139+
XCTAssertFalse(Self.textClearContinues(clearedLength: 501, remainingLength: 0))
140+
XCTAssertFalse(Self.textClearContinues(clearedLength: 501, remainingLength: 501))
141+
XCTAssertFalse(Self.textClearContinues(clearedLength: 501, remainingLength: 640))
142+
}
143+
126144
// One read owns readability for every route: a secure field is unreadable whatever it reports,
127145
// a plain field is unreadable only while its value is a mask, and anything that is not a text
128146
// input has no text to read.
@@ -156,7 +174,7 @@ extension RunnerTests {
156174
// ending the clear as a field that could not be read.
157175
func testSynthesizedClearLeavesAMaskedFieldUnverified() {
158176
var passes = 0
159-
let outcome = Self.clearForSynthesizedReplacement(
177+
let outcome = Self.clearUntilEmpty(
160178
clearOnce: {
161179
passes += 1
162180
return .posted
@@ -459,7 +477,7 @@ extension RunnerTests {
459477
for testCase in cases {
460478
var passes = 0
461479
var reads = testCase.reads[...]
462-
let outcome = Self.clearForSynthesizedReplacement(
480+
let outcome = Self.clearUntilEmpty(
463481
clearOnce: {
464482
passes += 1
465483
return .posted
@@ -470,7 +488,7 @@ extension RunnerTests {
470488
XCTAssertEqual(passes, testCase.passes, "\(testCase.reads)")
471489
}
472490
XCTAssertEqual(
473-
Self.clearForSynthesizedReplacement(clearOnce: { .stop }, read: { .text("") }),
491+
Self.clearUntilEmpty(clearOnce: { .stop }, read: { .text("") }),
474492
.stopped
475493
)
476494
}

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,57 @@ extension RunnerTests {
176176
XCTAssertEqual(editableTextValue(for: nextScreenField, treatingPlaceholderAsEmpty: true), "")
177177
}
178178

179+
// A value longer than one clear burst and wider than its field (#3851): the caret edge-tap lands
180+
// mid-text and a burst deletes at most 120 characters, so neither alone can reach all of it.
181+
@MainActor
182+
func testFillReplacesAValueLongerThanOneClearBurst() throws {
183+
let textField = try launchRemovableInputFixture("--agent-device-text-entry-long-prefill")
184+
185+
let response = executeTypeCommand(
186+
activeApp: app,
187+
command: try fillCommandFixture(commandId: "fill-long-prefill", text: "Ada Lovelace", at: textField)
188+
)
189+
190+
XCTAssertTrue(response.ok, String(describing: response.error))
191+
XCTAssertEqual(response.data?.textEntryRoute, "xctest-element")
192+
XCTAssertEqual(editableTextValue(for: textField), "Ada Lovelace")
193+
}
194+
195+
@MainActor
196+
func testFillClearsAValueLongerThanOneClearBurst() throws {
197+
let textField = try launchRemovableInputFixture("--agent-device-text-entry-long-prefill")
198+
199+
let response = executeTypeCommand(
200+
activeApp: app,
201+
command: try fillCommandFixture(commandId: "clear-long-prefill", text: "", at: textField)
202+
)
203+
204+
XCTAssertTrue(response.ok, String(describing: response.error))
205+
XCTAssertEqual(editableTextValue(for: textField, treatingPlaceholderAsEmpty: true), "")
206+
}
207+
208+
// The clear's first delete navigates to a screen whose input, already holding a value, takes the
209+
// cleared one's place under the same handle. The clear keeps going only on the input it started on.
210+
@MainActor
211+
func testFillClearDoesNotEraseTheInputThatReplacedItsTarget() throws {
212+
let textField = try launchRemovableInputFixture(
213+
"--agent-device-text-entry-long-prefill",
214+
"--agent-device-text-entry-populated-successor"
215+
)
216+
217+
let failureCountBefore = currentXCTestFailureCount()
218+
_ = executeTypeCommand(
219+
activeApp: app,
220+
command: try fillCommandFixture(commandId: "fill-clear-replaced-input", text: "", at: textField)
221+
)
222+
223+
XCTAssertFalse(didRecordXCTestFailure(since: failureCountBefore))
224+
XCTAssertFalse(textField.exists)
225+
let nextScreenField = app.textFields["agent-device-auto-submit-next-screen-input"]
226+
XCTAssertTrue(nextScreenField.exists)
227+
XCTAssertEqual(editableTextValue(for: nextScreenField), "next screen value")
228+
}
229+
179230
// The reported shape: the last digit navigates to a screen without an input.
180231
@MainActor
181232
func testFillSucceedsWhenAppRemovesInputAfterLastCharacter() throws {

0 commit comments

Comments
 (0)