Skip to content

Commit bd88d65

Browse files
committed
fix(ios): clear a long text value wherever the caret lands (#3851)
1 parent d217300 commit bd88d65

5 files changed

Lines changed: 127 additions & 13 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: 74 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,10 @@ extension RunnerTests {
326326
}
327327
}
328328

329-
/// Clears `input`, which must have been read with nothing dispatched since.
329+
/// Clears `input`, which must have been read with nothing dispatched since. On iOS a readable
330+
/// value is selected whole and deleted, wherever the caret is. Otherwise, or when text is left,
331+
/// delete bursts run from the caret, and a value longer than one burst is read again after each
332+
/// burst until it reads empty, stops shortening, or has had at least one burst more than it needs.
330333
func clearTextInput(app: XCUIApplication, _ input: ResolvedTextEntryInput) {
331334
// Skip the clear (delete burst + moveCaretToEnd edge-tap) ONLY when we can confirm the
332335
// field is empty. Why skip: the edge-tap computes a point from the element frame, which can
@@ -336,17 +339,82 @@ extension RunnerTests {
336339
// editableTextValue returns nil for secure (and unknown) fields, where we CANNOT confirm
337340
// emptiness — those must still be cleared, or replace would concatenate stale + new text.
338341
// So distinguish nil (clear) from "" (skip).
339-
if let existing = editableTextValue(for: input.attributes, treatingPlaceholderAsEmpty: true),
340-
existing.isEmpty {
342+
let existing = editableTextValue(for: input.attributes, treatingPlaceholderAsEmpty: true)
343+
if existing?.isEmpty == true {
341344
return
342345
}
343346
// Read before the caret tap, which dispatches to the app.
344-
let count = estimatedDeleteCount(for: input.attributes)
347+
var valueLength = normalizedElementText(input.attributes.value).count
348+
// The tap also focuses the field: a clear of an input found under a point arrives unfocused.
345349
#if !os(tvOS)
346350
moveCaretToEnd(app: app, input: input)
347351
#endif
348-
let deletes = String(repeating: XCUIKeyboardKey.delete.rawValue, count: count)
349-
input.element.typeText(deletes)
352+
#if os(iOS)
353+
// The tap aims at the end of what the field shows, which a long value has scrolled past, so
354+
// bursts from there leave the start of the value (#3851). Select all reaches all of it, but
355+
// only a read can show it took: a masked field can ignore it, so an unreadable value keeps the
356+
// bursts alone.
357+
if existing != nil {
358+
var passes = 0
359+
let outcome = Self.clearForSynthesizedReplacement(
360+
clearOnce: {
361+
passes += 1
362+
input.element.typeKey("a", modifierFlags: .command)
363+
input.element.typeText(XCUIKeyboardKey.delete.rawValue)
364+
return .posted
365+
},
366+
read: {
367+
textValueRead(
368+
for: ResolvedTextEntryInput(reading: input.element)?.attributes,
369+
treatingPlaceholderAsEmpty: true
370+
)
371+
}
372+
)
373+
NSLog("AGENT_DEVICE_RUNNER_TEXT_ENTRY_CLEAR route=element passes=%d outcome=%@", passes, String(describing: outcome))
374+
// Text left after every pass goes to the bursts; a field that stopped resolving is not typed into.
375+
guard outcome == .notCleared,
376+
let remaining = ResolvedTextEntryInput(reading: input.element)
377+
.flatMap({ editableTextValue(for: $0.attributes, treatingPlaceholderAsEmpty: true) })
378+
else {
379+
return
380+
}
381+
valueLength = remaining.count
382+
}
383+
#endif
384+
for _ in 0...(valueLength / Self.maxTextClearBurst + 1) {
385+
let burst = Self.textClearBurst(valueLength: valueLength)
386+
input.element.typeText(String(repeating: XCUIKeyboardKey.delete.rawValue, count: burst))
387+
// A burst that covers the value needs no read.
388+
guard burst < valueLength + Self.textClearSlack,
389+
let remaining = ResolvedTextEntryInput(reading: input.element)
390+
.flatMap({ editableTextValue(for: $0.attributes, treatingPlaceholderAsEmpty: true) }),
391+
Self.textClearContinues(clearedLength: valueLength, remainingLength: remaining.count)
392+
else {
393+
return
394+
}
395+
valueLength = remaining.count
396+
}
397+
}
398+
399+
/// Extra deletes past a value's length, for whitespace its normalized read trimmed.
400+
private static let textClearSlack = 8
401+
402+
/// The most deletes one clear burst sends. A longer value takes more bursts.
403+
private static let maxTextClearBurst = 120
404+
405+
/// Deletes for one clear burst on a value of `valueLength` characters, at least 24 for a value
406+
/// that reads empty but may not be.
407+
static func textClearBurst(valueLength: Int) -> Int {
408+
let base = valueLength == 0 ? 24 : valueLength + textClearSlack
409+
return max(24, min(maxTextClearBurst, base))
410+
}
411+
412+
/// Whether a clear sends another burst after one sized from `clearedLength` characters left
413+
/// `remainingLength`. Each burst must shorten the value, so a field that ignores deletes or
414+
/// rewrites its value ends the clear instead of looping; the fill's verification reports what
415+
/// remains.
416+
static func textClearContinues(clearedLength: Int, remainingLength: Int) -> Bool {
417+
remainingLength > 0 && remainingLength < clearedLength
350418
}
351419

352420
func isTextEntryElement(_ element: XCUIElement) -> Bool {
@@ -500,12 +568,6 @@ extension RunnerTests {
500568
#endif
501569
}
502570

503-
private func estimatedDeleteCount(for element: any XCUIElementAttributes) -> Int {
504-
let valueText = normalizedElementText(element.value)
505-
let base = valueText.isEmpty ? 24 : (valueText.count + 8)
506-
return max(24, min(120, base))
507-
}
508-
509571
private func normalizedElementText(_ value: Any?) -> String {
510572
String(describing: value ?? "")
511573
.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)