Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ @interface AgentDeviceRunnerFixtureViewController () <UNUserNotificationCenterDe
@property(nonatomic, strong) UILabel *navigationBackStatus;
@property(nonatomic, strong) UILabel *maskedToggleStatus;
@property(nonatomic, strong) UILabel *maskedDeliveryStatus;
@property(nonatomic, strong) UILabel *maskedTextLengthStatus;
@property(nonatomic, assign) NSUInteger maskedTogglePresses;
@property(nonatomic, strong) UILabel *alertActionStatus;
@property(nonatomic, strong) UILabel *alertActivationBusyAnswer;
Expand Down Expand Up @@ -409,6 +410,9 @@ - (void)agentDeviceTextEntryDidChange:(UITextField *)textField {
if (self.maskedDeliveryStatus != nil) {
self.maskedDeliveryStatus.text = [textField.text hasSuffix:@"not-a-secret-123"] ? @"delivered" : @"pending";
}
if (self.maskedTextLengthStatus != nil) {
self.maskedTextLengthStatus.text = [NSString stringWithFormat:@"length=%lu", (unsigned long)textField.text.length];
}
// A field whose app owns its value, the way a controlled React Native `TextInput` does. A burst
// typed faster than the app renders loses the characters that arrived while a render was in
// flight, and the field settles stable short of the request.
Expand Down Expand Up @@ -658,11 +662,19 @@ - (void)viewDidLoad {
[NSProcessInfo.processInfo.arguments containsObject:@"--agent-device-text-entry-submit-clears"];
BOOL maskedValue =
[NSProcessInfo.processInfo.arguments containsObject:@"--agent-device-text-entry-masked-value"];
// A native secure field, which iOS clears on the first delete after it takes focus.
BOOL secureEntry =
[NSProcessInfo.processInfo.arguments containsObject:@"--agent-device-text-entry-secure-entry"];
BOOL bulletPlaceholder =
[NSProcessInfo.processInfo.arguments containsObject:@"--agent-device-text-entry-bullet-placeholder"];
BOOL refusesFirstFocus =
[NSProcessInfo.processInfo.arguments containsObject:@"--agent-device-text-entry-refuses-first-focus"];
BOOL countsValueReads =
[NSProcessInfo.processInfo.arguments containsObject:@"--agent-device-text-entry-counts-value-reads"];
BOOL witnessesReplacement =
[NSProcessInfo.processInfo.arguments containsObject:@"--agent-device-text-entry-witness"];
UITextField *textField;
AgentDeviceSecureEntryField *secureField = nil;
if (digitCountValue) {
textField = [[AgentDeviceDigitCountTextField alloc] init];
} else if (digitGroupingValue) {
Expand All @@ -674,6 +686,12 @@ - (void)viewDidLoad {
textField = [[AgentDeviceSubmitClearingTextField alloc] init];
} else if (maskedValue) {
textField = [[AgentDeviceMaskedTextField alloc] init];
} else if (secureEntry) {
secureField = [[AgentDeviceSecureEntryField alloc] init];
secureField.secureTextEntry = YES;
secureField.reportsPlaceholderAsValue = bulletPlaceholder;
secureField.refusesFirstFocus = refusesFirstFocus;
textField = secureField;
} else if (witnessesReplacement) {
textField = [[AgentDeviceTextEntryWitnessField alloc] init];
} else if (countsValueReads) {
Expand Down Expand Up @@ -819,10 +837,14 @@ - (void)viewDidLoad {
[neighbour.heightAnchor constraintEqualToConstant:AgentDeviceTextEntryFieldHeight],
]];
}
if (maskedValue) {
if (maskedValue || secureEntry) {
if ([NSProcessInfo.processInfo.arguments containsObject:@"--agent-device-text-entry-prefilled"]) {
textField.text = @"stale-secret";
}
// While empty, the field then reads the same as one holding eight characters.
if (bulletPlaceholder) {
textField.placeholder = @"\u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022";
}
// The show/hide toggle sits over the field's right edge, where the runner's caret edge-tap
// lands. Counting its presses tells the test whether a repair reached for the field again.
UIButton *toggle = [UIButton buttonWithType:UIButtonTypeSystem];
Expand Down Expand Up @@ -853,6 +875,28 @@ - (void)viewDidLoad {
[self.maskedDeliveryStatus.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],
[self.maskedDeliveryStatus.topAnchor constraintEqualToAnchor:self.maskedToggleStatus.bottomAnchor constant:8],
]];
// The text's length, which a mask hides: an empty field can show as many bullets as a full one.
self.maskedTextLengthStatus = [[UILabel alloc] init];
self.maskedTextLengthStatus.accessibilityIdentifier = @"agent-device-masked-text-length";
self.maskedTextLengthStatus.text = [NSString stringWithFormat:@"length=%lu", (unsigned long)textField.text.length];
self.maskedTextLengthStatus.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:self.maskedTextLengthStatus];
[NSLayoutConstraint activateConstraints:@[
[self.maskedTextLengthStatus.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],
[self.maskedTextLengthStatus.topAnchor constraintEqualToAnchor:self.maskedDeliveryStatus.bottomAnchor constant:8],
]];
if (secureField) {
UILabel *counts = [[UILabel alloc] init];
counts.accessibilityIdentifier = @"agent-device-secure-entry-counts";
counts.text = @"deletes=0 taps=0";
counts.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:counts];
[NSLayoutConstraint activateConstraints:@[
[counts.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],
[counts.topAnchor constraintEqualToAnchor:self.maskedTextLengthStatus.bottomAnchor constant:8],
]];
secureField.countLabel = counts;
}
[self updateMaskedToggleStatus];
}
if ([NSProcessInfo.processInfo.arguments containsObject:@"--agent-device-text-entry-app-owned-value"]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
@interface AgentDeviceValueReadCountingTextField : AgentDeviceMaskedTextField
@end

@interface AgentDeviceSecureEntryField : UITextField <UIGestureRecognizerDelegate>
// Shows how many deletes and taps the field received.
@property(nonatomic, weak) UILabel *countLabel;
// Refuses the first focus, as a field whose first tap the app consumes does.
@property(nonatomic, assign) BOOL refusesFirstFocus;
// Reports the placeholder as the value while empty, as a React Native secure input does.
@property(nonatomic, assign) BOOL reportsPlaceholderAsValue;
@end

@interface AgentDeviceValueReadCountLabel : UILabel
@property(nonatomic, weak) UITextField *countedField;
@end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,59 @@ - (NSString *)accessibilityValue {
}
@end

@implementation AgentDeviceSecureEntryField {
NSUInteger _deletes;
NSUInteger _taps;
}
- (instancetype)init {
self = [super init];
if (self) {
UITapGestureRecognizer *tapCounter = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(countTap:)];
tapCounter.cancelsTouchesInView = NO;
tapCounter.delegate = self;
[self addGestureRecognizer:tapCounter];
}
return self;
}

- (void)countTap:(UITapGestureRecognizer *)recognizer {
(void)recognizer;
_taps += 1;
[self showCounts];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
(void)gestureRecognizer;
(void)otherGestureRecognizer;
return YES;
}

- (void)showCounts {
self.countLabel.text = [NSString stringWithFormat:@"deletes=%lu taps=%lu", (unsigned long)_deletes, (unsigned long)_taps];
}

- (BOOL)becomeFirstResponder {
if (self.refusesFirstFocus) {
self.refusesFirstFocus = NO;
return NO;
}
return [super becomeFirstResponder];
}

- (void)deleteBackward {
_deletes += 1;
[self showCounts];
[super deleteBackward];
}

- (NSString *)accessibilityValue {
if (self.reportsPlaceholderAsValue && self.text.length == 0) {
return self.placeholder;
}
return [super accessibilityValue];
}
@end

static NSUInteger AgentDeviceTextEntryValueReads = 0;

@implementation AgentDeviceValueReadCountingTextField
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,10 +395,10 @@ extension RunnerTests {

/// Clears the reader's input. Every post goes through `reader.dispatch` and every read back through
/// `reader.input()`, so a bound target's clear stops at an input that took its place. On iOS a
/// readable value is selected whole and deleted, wherever the caret is. Otherwise, or when text is
/// left, delete bursts run from the caret, and a value longer than one burst is read again after
/// each burst until it reads empty, stops shortening, or has had at least one burst more than it
/// needs.
/// readable value is selected whole and deleted, wherever the caret is, and a masked value gets
/// deletes before any caret tap. Otherwise, or when text is left, delete bursts run from the caret,
/// and a value longer than one burst is read again after each burst until it reads empty, stops
/// shortening, or has had at least one burst more than it needs.
func clearTextInput(app: XCUIApplication, reader: inout TextEntryReader) throws {
guard let input = try reader.input() else {
return
Expand All @@ -408,15 +408,44 @@ extension RunnerTests {
// be stale after the field repositions on focus (e.g. the Settings search bar jumps
// bottom->top and reveals a "Suggestions" list) — tapping there navigates away instead of
// clearing; and replacing into an already-empty field is a no-op anyway.
// editableTextValue returns nil for secure (and unknown) fields, where we CANNOT confirm
// emptiness — those must still be cleared, or replace would concatenate stale + new text.
// So distinguish nil (clear) from "" (skip).
let existing = editableTextValue(for: input.attributes, treatingPlaceholderAsEmpty: true)
// textValueForClear returns nil for secure (and unknown) fields, and for a placeholder that is
// itself a mask, where we CANNOT confirm emptiness — those must still be cleared, or
// replace would concatenate stale + new text. So distinguish nil (clear) from "" (skip). On iOS
// the masked route below can still prove a masked field empty.
let existing = textValueForClear(input.attributes)
if existing?.isEmpty == true {
return
}
let defersCaretTap = reader.target.reusesCurrentFocus
var valueLength = normalizedElementText(input.attributes.value).count
#if os(iOS)
// A masked value cannot aim the caret tap, which lands at the field's right edge, where an app
// often draws a show/hide control: pressing it swaps the input (tester-army/e2e#1631). iOS clears
// a secure field on its first delete after the field takes focus, and deletes from a caret left
// at the end reach the rest, so deletes go first and the caret moves only for masked text left.
if textValueRead(for: input.attributes) == .unreadable {
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
// Deletes or text sent to a field without focus record an XCTest failure, which tears down the
// runner. The focus step can leave focus unconfirmed, so the field gets the readiness repeat at
// its center, clear of a control at the edge, even when it is empty and needs no clear.
if input.snapshot.map({ snapshotHasKeyboardFocus($0) }) != true {
try reader.dispatch { try retapAt(read: input, app: app, element: input.element) }
}
if maskedTextShown(by: input) == .empty {
NSLog("AGENT_DEVICE_RUNNER_TEXT_ENTRY_CLEAR route=masked-skip shown=empty")
return
}
let burst = Self.textClearBurst(valueLength: valueLength)
reader.dispatch { input.element.typeText(String(repeating: XCUIKeyboardKey.delete.rawValue, count: burst)) }
// An input that stops answering (#3060) goes on to the caret tap and burst below.
if let cleared = try reader.input() {
let shown = maskedTextShown(by: cleared)
NSLog("AGENT_DEVICE_RUNNER_TEXT_ENTRY_CLEAR route=masked-deletes shown=%@", String(describing: shown))
if shown != .text {
return
}
}
}
#endif
// The tap also focuses the field, which a clear can reach with focus unconfirmed.
#if !os(tvOS)
if !defersCaretTap {
Expand Down Expand Up @@ -513,6 +542,33 @@ extension RunnerTests {
remainingLength > 0 && remainingLength < clearedLength
}

/// What a masked field shows about its text.
enum MaskedTextShown: Equatable {
/// No text, or a placeholder that is not itself a mask: the field is empty.
case empty
/// A placeholder that is itself a mask, which text of the same length shows too.
case maskPlaceholder
/// Anything else, such as a mask other than the placeholder: text may be in the field.
case text
}

static func maskedTextShown(value: String, showsPlaceholder: Bool) -> MaskedTextShown {
let shown = value.trimmingCharacters(in: .whitespacesAndNewlines)
if shown.isEmpty {
return .empty
}
if showsPlaceholder {
return isMaskedTextValue(shown) ? .maskPlaceholder : .empty
}
return .text
}

/// `maskedTextShown(value:showsPlaceholder:)` for `input`, with the placeholder rule the shared read uses.
func maskedTextShown(by input: ResolvedTextEntryInput) -> MaskedTextShown {
let value = normalizedElementText(input.attributes.value)
return Self.maskedTextShown(value: value, showsPlaceholder: isPlaceholderValue(value, for: input.attributes))
}

func isTextEntryElement(_ element: XCUIElement) -> Bool {
switch element.elementType {
case .textField, .secureTextField, .searchField, .textView:
Expand Down Expand Up @@ -749,6 +805,20 @@ extension RunnerTests {
return value
}

/// The text a clear can trust in the input `attributes` describe, with a placeholder read as empty;
/// nil when unreadable. A placeholder that is itself a mask is nil too, since text of its length shows
/// it as well, so that field is cleared instead of skipped. Only the clear reads it this way: the
/// shared read keeps such a placeholder empty for verification, as a PIN field that shows its digits needs.
func textValueForClear(_ attributes: (any XCUIElementAttributes)?) -> String? {
guard let value = editableTextValue(for: attributes, treatingPlaceholderAsEmpty: true) else {
return nil
}
if value.isEmpty, Self.isMaskedTextValue(normalizedElementText(attributes?.value)) {
return nil
}
return value
}

/// Whether an accessibility value is a masked rendering of a secure field: one or more
/// password bullets and nothing else. The bullet count need not match the typed length, so
/// such a value carries no information about what the field holds. A field whose real text is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ extension RunnerTests {
logTextEntryPhase(commandId: commandId, phase: "total", startedAt: totalStartedAt, chars: text.count, mode: repairMode)
return TextEntryResult(verified: nil, repaired: false, expectedText: expectedText, observedText: nil)
}
if currentText == nil || currentText?.isEmpty == false {
if textValueForClear(initialTarget?.attributes)?.isEmpty != true {
let clearStartedAt = Date()
try clearTextInput(app: app, reader: &reader)
logTextEntryPhase(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,18 @@ extension RunnerTests {
XCTAssertFalse(Self.textClearContinues(clearedLength: 501, remainingLength: 640))
}

// A secure field shows its placeholder when empty and a mask otherwise. A placeholder that is not a
// mask proves it empty; one that is itself a mask proves nothing, since text of its length shows it
// too (tester-army/e2e#1631).
func testMaskedTextShownTellsAnEmptyFieldFromAMaskPlaceholder() {
let bullets = { (count: Int) in String(repeating: "\u{2022}", count: count) }
XCTAssertEqual(Self.maskedTextShown(value: "", showsPlaceholder: false), .empty)
XCTAssertEqual(Self.maskedTextShown(value: "Password", showsPlaceholder: true), .empty)
XCTAssertEqual(Self.maskedTextShown(value: bullets(8), showsPlaceholder: true), .maskPlaceholder)
XCTAssertEqual(Self.maskedTextShown(value: bullets(3), showsPlaceholder: false), .text)
XCTAssertEqual(Self.maskedTextShown(value: "hunter2", showsPlaceholder: false), .text)
}

// One read owns readability for every route: a secure field is unreadable whatever it reports,
// a plain field is unreadable only while its value is a mask, and anything that is not a text
// input has no text to read.
Expand Down
Loading
Loading