Skip to content

Commit a96c7c8

Browse files
committed
fix(ios): clear a long text value wherever the caret lands (callstack#3851)
1 parent 6ea9f81 commit a96c7c8

5 files changed

Lines changed: 128 additions & 14 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,10 @@ - (void)viewDidLoad {
762762
if (![NSProcessInfo.processInfo.arguments containsObject:@"--agent-device-text-entry-unnamed-input"]) {
763763
textField.accessibilityIdentifier = @"agent-device-hardware-keyboard-input";
764764
}
765+
if ([NSProcessInfo.processInfo.arguments containsObject:@"--agent-device-text-entry-long-prefill"]) {
766+
// Longer than one clear burst and far wider than the field, so its end is scrolled out of view.
767+
textField.text = [@"" stringByPaddingToLength:300 withString:@"stale long value " startingAtIndex:0];
768+
}
765769
textField.borderStyle = UITextBorderStyleRoundedRect;
766770
// An empty input view keeps the software keyboard down, which is the hardware-keyboard responder
767771
// these routes are addressed to. `--agent-device-text-entry-soft-keyboard` leaves the real input

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,8 @@ 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

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

Lines changed: 75 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -355,27 +355,95 @@ extension RunnerTests {
355355
}
356356
}
357357

358-
/// Clears `input`, which must have been read with nothing dispatched since.
358+
/// Clears `input`, which must have been read with nothing dispatched since. On iOS a readable
359+
/// value is selected whole and deleted, wherever the caret is. Otherwise, or when text is left,
360+
/// delete bursts run from the caret, and a value longer than one burst is read again after each
361+
/// burst until it reads empty, stops shortening, or has had at least one burst more than it needs.
359362
func clearTextInput(app: XCUIApplication, _ input: ResolvedTextEntryInput) {
360-
// Skip the clear (delete burst + moveCaretToEnd edge-tap) ONLY when we can confirm the
363+
// Skip the clear (edge-tap, select all, delete bursts) ONLY when we can confirm the
361364
// field is empty. Why skip: the edge-tap computes a point from the element frame, which can
362365
// be stale after the field repositions on focus (e.g. the Settings search bar jumps
363366
// bottom->top and reveals a "Suggestions" list) — tapping there navigates away instead of
364367
// clearing; and replacing into an already-empty field is a no-op anyway.
365368
// editableTextValue returns nil for secure (and unknown) fields, where we CANNOT confirm
366369
// emptiness — those must still be cleared, or replace would concatenate stale + new text.
367370
// So distinguish nil (clear) from "" (skip).
368-
if let existing = editableTextValue(for: input.attributes, treatingPlaceholderAsEmpty: true),
369-
existing.isEmpty {
371+
let existing = editableTextValue(for: input.attributes, treatingPlaceholderAsEmpty: true)
372+
if existing?.isEmpty == true {
370373
return
371374
}
372375
// Read before the caret tap, which dispatches to the app.
373-
let count = estimatedDeleteCount(for: input.attributes)
376+
var valueLength = normalizedElementText(input.attributes.value).count
377+
// The tap also focuses the field, which a clear can reach with focus unconfirmed.
374378
#if !os(tvOS)
375379
moveCaretToEnd(app: app, input: input)
376380
#endif
377-
let deletes = String(repeating: XCUIKeyboardKey.delete.rawValue, count: count)
378-
input.element.typeText(deletes)
381+
#if os(iOS)
382+
// The tap aims at the end of what the field shows, which a long value has scrolled past, so
383+
// bursts from there reach only what is before the caret (#3851). Select all reaches all of it, but
384+
// only a read can show it took: a masked field can ignore it, so an unreadable value keeps the
385+
// bursts alone.
386+
if existing != nil {
387+
var passes = 0
388+
let outcome = Self.clearForSynthesizedReplacement(
389+
clearOnce: {
390+
passes += 1
391+
input.element.typeKey("a", modifierFlags: .command)
392+
input.element.typeText(XCUIKeyboardKey.delete.rawValue)
393+
return .posted
394+
},
395+
read: {
396+
textValueRead(
397+
for: ResolvedTextEntryInput(reading: input.element)?.attributes,
398+
treatingPlaceholderAsEmpty: true
399+
)
400+
}
401+
)
402+
NSLog("AGENT_DEVICE_RUNNER_TEXT_ENTRY_CLEAR route=element passes=%d outcome=%@", passes, String(describing: outcome))
403+
// Text left after every pass goes to the bursts; a field that stopped resolving is not typed into.
404+
guard outcome == .notCleared,
405+
let remaining = ResolvedTextEntryInput(reading: input.element)
406+
.flatMap({ editableTextValue(for: $0.attributes, treatingPlaceholderAsEmpty: true) })
407+
else {
408+
return
409+
}
410+
valueLength = remaining.count
411+
}
412+
#endif
413+
for _ in 0...(valueLength / Self.maxTextClearBurst + 1) {
414+
let burst = Self.textClearBurst(valueLength: valueLength)
415+
input.element.typeText(String(repeating: XCUIKeyboardKey.delete.rawValue, count: burst))
416+
// A burst that covers the value needs no read.
417+
guard burst < valueLength + Self.textClearSlack,
418+
let remaining = ResolvedTextEntryInput(reading: input.element)
419+
.flatMap({ editableTextValue(for: $0.attributes, treatingPlaceholderAsEmpty: true) }),
420+
Self.textClearContinues(clearedLength: valueLength, remainingLength: remaining.count)
421+
else {
422+
return
423+
}
424+
valueLength = remaining.count
425+
}
426+
}
427+
428+
/// Extra deletes past a value's length, for whitespace its normalized read trimmed.
429+
private static let textClearSlack = 8
430+
431+
/// The most deletes one clear burst sends. A longer value takes more bursts.
432+
private static let maxTextClearBurst = 120
433+
434+
/// Deletes for one clear burst on a value of `valueLength` characters, at least 24 for a value
435+
/// that reads empty but may not be.
436+
static func textClearBurst(valueLength: Int) -> Int {
437+
let base = valueLength == 0 ? 24 : valueLength + textClearSlack
438+
return max(24, min(maxTextClearBurst, base))
439+
}
440+
441+
/// Whether a clear sends another burst after one sized from `clearedLength` characters left
442+
/// `remainingLength`. Each burst must shorten the value, so a field that ignores deletes or
443+
/// rewrites its value ends the clear instead of looping; the fill's verification reports what
444+
/// remains.
445+
static func textClearContinues(clearedLength: Int, remainingLength: Int) -> Bool {
446+
remainingLength > 0 && remainingLength < clearedLength
379447
}
380448

381449
func isTextEntryElement(_ element: XCUIElement) -> Bool {
@@ -529,12 +597,6 @@ extension RunnerTests {
529597
#endif
530598
}
531599

532-
private func estimatedDeleteCount(for element: any XCUIElementAttributes) -> Int {
533-
let valueText = normalizedElementText(element.value)
534-
let base = valueText.isEmpty ? 24 : (valueText.count + 8)
535-
return max(24, min(120, base))
536-
}
537-
538600
private func normalizedElementText(_ value: Any?) -> String {
539601
String(describing: value ?? "")
540602
.trimmingCharacters(in: .whitespacesAndNewlines)

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

Lines changed: 18 additions & 0 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.

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,35 @@ 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+
179208
// The reported shape: the last digit navigates to a screen without an input.
180209
@MainActor
181210
func testFillSucceedsWhenAppRemovesInputAfterLastCharacter() throws {

0 commit comments

Comments
 (0)