Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import "AgentDeviceRunnerApp.h"
#import <UIKit/UIKit.h>
#import <UserNotifications/UserNotifications.h>
#import <WebKit/WebKit.h>
#import "UnitTests/AgentDeviceTextEntryFields.h"

// Mirrors the App Tracking Transparency prompt's real button order: the affirmative button is
Expand Down Expand Up @@ -988,14 +989,29 @@ - (void)viewDidLoad {
self.keyboardBusyAnswer = answer;
}

// A field whose return key reads "Done" and submits, which `keyboard dismiss` must never press. The
// optional `accessory` argument adds a Done toolbar above the keys, the one control it may press.
NSArray<NSString *> *launchArguments = NSProcessInfo.processInfo.arguments;
if ([launchArguments containsObject:@"--agent-device-keyboard-webview"]) {
WKWebView *webView = [[WKWebView alloc] init];
webView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:webView];
UILayoutGuide *safeArea = self.view.safeAreaLayoutGuide;
[NSLayoutConstraint activateConstraints:@[
[webView.topAnchor constraintEqualToAnchor:safeArea.topAnchor],
[webView.leadingAnchor constraintEqualToAnchor:safeArea.leadingAnchor],
[webView.trailingAnchor constraintEqualToAnchor:safeArea.trailingAnchor],
[webView.bottomAnchor constraintEqualToAnchor:safeArea.bottomAnchor],
]];
[webView loadHTMLString:@"<meta name='viewport' content='width=device-width'>"
"<input aria-label='Web Search' enterkeyhint='search'>"
"<p id='result'>No Return</p>"
"<script>let count = 0; const input = document.querySelector('input');"
"input.onkeydown = (event) => { if (event.key === 'Enter') {"
"document.querySelector('#result').textContent = 'Return ' + ++count; } };</script>"
baseURL:nil];
}
NSUInteger doneReturnFlag = [launchArguments indexOfObject:@"--agent-device-keyboard-done-return"];
if (doneReturnFlag != NSNotFound) {
NSString *accessoryShape = doneReturnFlag + 1 < launchArguments.count ? launchArguments[doneReturnFlag + 1] : @"";
BOOL plainAccessory = [accessoryShape isEqualToString:@"plain-accessory"];
BOOL withAccessory = plainAccessory || [accessoryShape isEqualToString:@"accessory"];
UITextField *field = [[UITextField alloc] init];
field.accessibilityIdentifier = @"agent-device-keyboard-done-return-input";
field.placeholder = @"Return key is Done";
Expand All @@ -1011,22 +1027,7 @@ - (void)viewDidLoad {
[weakField resignFirstResponder];
}]
forControlEvents:UIControlEventEditingDidEndOnExit];
if (plainAccessory) {
// An accessory that is no toolbar: a plain view holding a button, as a custom or React Native
// input accessory is.
UIView *accessory = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
accessory.backgroundColor = UIColor.systemGray5Color;
UIButton *accessoryDone = [UIButton buttonWithType:UIButtonTypeSystem];
[accessoryDone setTitle:@"Done" forState:UIControlStateNormal];
accessoryDone.frame = CGRectMake(240, 0, 70, 44);
[accessoryDone addAction:[UIAction actionWithHandler:^(__kindof UIAction *action) {
weakAnswer.text = @"Accessory";
[weakField resignFirstResponder];
}]
forControlEvents:UIControlEventTouchUpInside];
[accessory addSubview:accessoryDone];
field.inputAccessoryView = accessory;
} else if (withAccessory) {
if ([accessoryShape isEqualToString:@"accessory"]) {
UIToolbar *accessory = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
accessory.items = @[
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
Expand All @@ -1044,19 +1045,19 @@ - (void)viewDidLoad {
field.inputAccessoryView = accessory;
}
AgentDeviceAddKeyboardFieldWithAnswer(self.view, field, answer, label);
if (withAccessory) {
// A screen-level Done with the toolbar Done's label and no identifier, far from the keyboard, as a
// navigation bar's would be: dismiss must still press the toolbar's, never this one.
if ([accessoryShape isEqualToString:@"adjacent-app-button"]) {
UIButton *screenDone = [UIButton buttonWithType:UIButtonTypeSystem];
screenDone.accessibilityIdentifier = @"agent-device-adjacent-done";
[screenDone setTitle:@"Done" forState:UIControlStateNormal];
[screenDone addAction:[UIAction actionWithHandler:^(__kindof UIAction *action) {
weakAnswer.text = @"Screen";
[weakField resignFirstResponder];
}]
forControlEvents:UIControlEventTouchUpInside];
screenDone.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:screenDone];
[NSLayoutConstraint activateConstraints:@[
[screenDone.topAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.topAnchor constant:8],
[screenDone.bottomAnchor constraintEqualToAnchor:self.view.keyboardLayoutGuide.topAnchor constant:-6],
[screenDone.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor constant:-16],
]];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,6 @@
import Foundation

/// What the keyboard's return key offers, read from the key's own label or identifier.
///
/// The key is the app's declaration of what submitting the focused field does, and only the app
/// can answer that: `UITextInput.returnKeyType` reaches the keyboard as one of Apple's return-key
/// types, whose English wording is what XCTest exposes. This maps that wording to one stable kind.
///
/// Exact, case-insensitive matches only. An unlisted label stays `.unknown` rather than being
/// guessed: iOS localizes these keys unevenly, so a prefix or fuzzy rule would report a localized
/// "Done" as something else. Absence is not a kind either — only a control this module cannot read
/// is `.unknown`; a keyboard with no return control is reported by the caller that looked for one.
///
/// Three of Apple's return-key types are deliberately unmapped and answer `.unknown`: `Google` and
/// `Yahoo` are vendor-branded keys whose wording names a vendor rather than a submit action, and
/// `Emergency Call` places a call, which no caller of this should ever report as a submit. Every
/// other Apple type has a case above.
///
/// `probeLabels` is the order `findKeyboardReturnControl(app:)` presses in. It is the
/// runner's own vocabulary, so it is not derived from the cases: it carries the two spellings of the
/// plain return key, in the order that found a hittable control before this rule existed. The
/// `keyboard status` read does not use it: it reads the keyboard's own key elements, so it needs no
/// English to find the return key.
/// The known identifiers of system Return controls. The displayed label can be localized.
enum RunnerKeyboardReturnKey: String {
case go
case search
Expand All @@ -33,13 +13,9 @@ enum RunnerKeyboardReturnKey: String {
case enter
case unknown

/// The keyboard control labels `findKeyboardReturnControl(app:)` tries, in order.
static let probeLabels = ["return", "Return", "Enter", "Go", "Search", "Next", "Done", "Send", "Join"]

/// The kind an accessibility label or identifier names. Surrounding whitespace is trimmed
/// because the accessibility tree carries it; nothing else is normalized.
static func kind(of text: String) -> RunnerKeyboardReturnKey {
switch text.trimmingCharacters(in: .whitespacesAndNewlines).lowercased() {
/// Classify an observed accessibility identifier for status metadata.
static func kind(forIdentifier identifier: String) -> RunnerKeyboardReturnKey {
switch identifier.trimmingCharacters(in: .whitespacesAndNewlines).lowercased() {
case "go": return .go
case "search": return .search
case "send": return .send
Expand Down
Loading
Loading