Repository navigation
refactor(apple-runner): extract the keyboard return control lookup #4589
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerKeyboardReturnKey.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| 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. | ||
| enum RunnerKeyboardReturnKey: String { | ||
| case go | ||
| case search | ||
| case send | ||
| case next | ||
| case done | ||
| case join | ||
| case route | ||
| case `continue` | ||
| 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() { | ||
| case "go": return .go | ||
| case "search": return .search | ||
| case "send": return .send | ||
| case "next": return .next | ||
| case "done": return .done | ||
| case "join": return .join | ||
| case "route": return .route | ||
| case "continue": return .continue | ||
| case "return", "enter": return .enter | ||
| default: return .unknown | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
...tDeviceRunner/AgentDeviceRunnerUITests/UnitTests/RunnerTests+KeyboardReturnKeyTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import XCTest | ||
|
|
||
| #if AGENT_DEVICE_RUNNER_UNIT_TESTS | ||
| extension RunnerTests { | ||
| /// Every Apple return-key type this rule maps: the key's English wording, and the wire value the | ||
| /// host publishes for it (design Feature C: `keyboard status` reports `returnKey`). The raw value | ||
| /// is the contract, so these assert the pair, not the case alone. `Google`, `Yahoo` and | ||
| /// `Emergency Call` are Apple types this rule leaves unmapped on purpose; the test below pins that. | ||
| func testKeyboardReturnKeyKindNamesEachReturnKeyType() { | ||
| let cases: [(label: String, expected: String)] = [ | ||
| ("Go", "go"), | ||
| ("Search", "search"), | ||
| ("Send", "send"), | ||
| ("Next", "next"), | ||
| ("Done", "done"), | ||
| ("Join", "join"), | ||
| ("Route", "route"), | ||
| ("Continue", "continue"), | ||
| ("Return", "enter"), | ||
| ("Enter", "enter"), | ||
| ] | ||
| for (label, expected) in cases { | ||
| XCTAssertEqual(RunnerKeyboardReturnKey.kind(of: label).rawValue, expected, label) | ||
| } | ||
| } | ||
|
|
||
| /// Keyboards and iOS versions differ in the label's casing and padding; the kind it names does not. | ||
| func testKeyboardReturnKeyKindIgnoresCasingAndPadding() { | ||
| XCTAssertEqual(RunnerKeyboardReturnKey.kind(of: "done"), .done) | ||
| XCTAssertEqual(RunnerKeyboardReturnKey.kind(of: "DONE"), .done) | ||
| XCTAssertEqual(RunnerKeyboardReturnKey.kind(of: " Done"), .done) | ||
| XCTAssertEqual(RunnerKeyboardReturnKey.kind(of: "\nSend\t"), .send) | ||
| } | ||
|
|
||
| /// The three Apple return-key types this rule leaves unmapped on purpose. `Google` and `Yahoo` | ||
| /// name a vendor rather than a submit action, and `Emergency Call` places a call: reporting any of | ||
| /// them as a submit kind would tell a caller to submit a field whose key does something else. | ||
| func testKeyboardReturnKeyKindLeavesBrandedAndEmergencyAppleTypesUnknown() { | ||
| for label in ["Google", "Yahoo", "Emergency Call"] { | ||
| XCTAssertEqual(RunnerKeyboardReturnKey.kind(of: label), .unknown, label) | ||
| } | ||
| } | ||
|
|
||
| /// Closest negatives: a label that CONTAINS a return-key word, or is a localized or custom key's | ||
| /// wording, is not that key. A prefix or fuzzy rule would report a "Send Message" key as `send`, | ||
| /// or a localized "Retour" key as `enter`; both would tell a caller to submit a field whose key | ||
| /// does something else. | ||
| func testKeyboardReturnKeyKindLeavesAmbiguousLabelsUnknown() { | ||
| for label in [ | ||
| "Send Message", | ||
| "Search Messages", | ||
| "Done Sending", | ||
| "Next slide", | ||
| "Go to Settings", | ||
| "Continued", | ||
| "Return key", | ||
| "Retour", | ||
| "完了", | ||
| "", | ||
| " ", | ||
| ] { | ||
| XCTAssertEqual(RunnerKeyboardReturnKey.kind(of: label), .unknown, label) | ||
| } | ||
| } | ||
|
|
||
| /// The two owners of the key's wording stay aligned: every label the press path probes names a | ||
| /// kind a status read can report, so a lookup that finds a control is never a status read that | ||
| /// answers `unknown`. | ||
| func testEveryKeyboardReturnProbeLabelMapsToAReportedKind() { | ||
| let expected: [String: RunnerKeyboardReturnKey] = [ | ||
| "return": .enter, | ||
| "Return": .enter, | ||
| "Enter": .enter, | ||
| "Go": .go, | ||
| "Search": .search, | ||
| "Next": .next, | ||
| "Done": .done, | ||
| "Send": .send, | ||
| "Join": .join, | ||
| ] | ||
| for label in RunnerKeyboardReturnKey.probeLabels { | ||
| XCTAssertEqual(RunnerKeyboardReturnKey.kind(of: label), expected[label], label) | ||
| } | ||
| } | ||
| } | ||
| #endif | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.