import UIKit import XCTest @testable import HEX /// The strip's Paste chip: one read per paste, and where to stop iOS's Allow Paste. @MainActor final class KeyboardClipboardTests: XCTestCase { private var suiteName = "" private var defaults: UserDefaults! override func setUp() async throws { suiteName = "hex.tests.\(UUID().uuidString)" defaults = UserDefaults(suiteName: suiteName)! } override func tearDown() async throws { defaults.removePersistentDomain(forName: suiteName) } func testTheChipReadsTextOrALinkOnce() { let pasteboard = UIPasteboard.withUniqueName() defer { UIPasteboard.remove(withName: pasteboard.name) } XCTAssertNil(KeyboardClipboardTracker.read(pasteboard)) pasteboard.url = URL(string: "https://hex.example/a") XCTAssertEqual(KeyboardClipboardTracker.read(pasteboard), "https://hex.example/a") pasteboard.string = "See you at 7" XCTAssertEqual(KeyboardClipboardTracker.read(pasteboard), "See you at 7") } func testAPasteThatAskedPointsToTheSettingAFewTimes() { let tracker = KeyboardClipboardTracker(defaults: defaults) XCTAssertFalse(tracker.needsAllowPasteSetting) XCTAssertFalse(tracker.shouldPointToAllowPaste(readDuration: 0.01, refused: false), "Allow: iOS didn't ask") XCTAssertFalse(tracker.needsAllowPasteSetting) for _ in 0.. KeyboardClipboardOffer? { tracker.offer( textContentType: .oneTimeCode, keyboardType: .numberPad, autocorrectionType: .default, fieldIsEmpty: true, hasFullAccess: true, appleKeyboardRoute: { _ in appleKeyboard ? .next : nil }, pasteboard: pasteboard ) } XCTAssertEqual(offer(appleKeyboard: true), .code(canPaste: false)) XCTAssertNil(offer(appleKeyboard: false), "Hex and Emoji only: the next keyboard gets no code (TestFlight build 56)") pasteboard.string = "482913" XCTAssertEqual(offer(appleKeyboard: true), .code(canPaste: true)) XCTAssertEqual(offer(appleKeyboard: false), .paste, "a copied code still pastes") XCTAssertEqual( tracker.offer( textContentType: nil, keyboardType: .default, autocorrectionType: .default, fieldIsEmpty: true, hasFullAccess: true, appleKeyboardRoute: { _ in XCTFail("asked outside a code field"); return .next }, pasteboard: pasteboard ), .paste ) } /// Where Apple's keyboard is split (the Duo unfolded and upright) Code from Messages opens /// iOS's keyboard list instead of switching: the switch to the next keyboard can end the host /// app there (TestFlight build 121), and the list's pick doesn't. func testCodeFromMessagesOpensTheListWhereApplesKeyboardSplits() { let pasteboard = UIPasteboard.withUniqueName() defer { UIPasteboard.remove(withName: pasteboard.name) } let tracker = KeyboardClipboardTracker(defaults: defaults) func offer(canSwitch: Bool) -> KeyboardClipboardOffer? { tracker.offer( textContentType: .oneTimeCode, keyboardType: .numberPad, autocorrectionType: .default, fieldIsEmpty: true, hasFullAccess: true, canSwitchKeyboard: canSwitch, appleKeyboardRoute: { _ in .next }, pasteboard: pasteboard ) } XCTAssertEqual(offer(canSwitch: true), .code(canPaste: false)) XCTAssertEqual(offer(canSwitch: false), .code(canPaste: false, opensList: true)) pasteboard.string = "482913" XCTAssertEqual(offer(canSwitch: true), .code(canPaste: true)) XCTAssertEqual(offer(canSwitch: false), .code(canPaste: true, opensList: true), "a copied code still pastes") XCTAssertEqual(FullKeyboardView(model: KeyboardViewModel(), target: FixtureTextTarget()).currentPosture, .phone, "an iPhone knows at once") } /// TestFlight build 56: ChatGPT's and Capy's message boxes kept Paste and Code from Messages /// in place of suggestions. A `.oneTimeCode` box with letters and default autocorrection is /// a chat box; Paste shows only while the field is empty, as Apple's keyboard offers a copy. func testChipsGiveWayToSuggestionsInAChatBox() { let pasteboard = UIPasteboard.withUniqueName() defer { UIPasteboard.remove(withName: pasteboard.name) } let tracker = KeyboardClipboardTracker(defaults: defaults) func offer( _ keyboardType: UIKeyboardType, _ autocorrection: UITextAutocorrectionType, empty: Bool = true ) -> KeyboardClipboardOffer? { tracker.offer( textContentType: .oneTimeCode, keyboardType: keyboardType, autocorrectionType: autocorrection, fieldIsEmpty: empty, hasFullAccess: true, appleKeyboardRoute: { _ in .next }, pasteboard: pasteboard ) } XCTAssertNil(offer(.default, .default), "a chat box with nothing copied: suggestions") XCTAssertEqual(offer(.default, .yes), .code(canPaste: false), "Safari's autocomplete=one-time-code") XCTAssertEqual(offer(.default, .no), .code(canPaste: false)) XCTAssertEqual(offer(.asciiCapableNumberPad, .default), .code(canPaste: false)) XCTAssertNil(offer(.numberPad, .default, empty: false), "typing a code by hand") pasteboard.string = "See you at 7" XCTAssertEqual(offer(.default, .default), .paste) XCTAssertNil(offer(.default, .default, empty: false), "after a word the strip suggests") XCTAssertEqual(offer(.numberPad, .default), .code(canPaste: true)) } /// TestFlight build 147: a username field kept Hex, where nothing offers a saved login (iOS /// offers them only in Apple's keyboard's bar). An empty username or password field shows /// Passwords, which reaches Apple's keyboard by Code from Messages' route; never with only /// Hex and Emoji, never once something is typed, and never in an email field, which a /// keyboard can't tell from one outside a login form. Deciding reads only whether the field /// is empty, as Paste always did. func testPasswordsChipLeadsLoginFieldsToApplesKeyboard() { let pasteboard = UIPasteboard.withUniqueName() defer { UIPasteboard.remove(withName: pasteboard.name) } let tracker = KeyboardClipboardTracker(defaults: defaults) func offer( _ type: UITextContentType?, empty: Bool = true, route: KeyboardInputModes.Route? = .next, canSwitch: Bool = true ) -> KeyboardClipboardOffer? { tracker.offer( textContentType: type, keyboardType: .default, autocorrectionType: .no, fieldIsEmpty: empty, hasFullAccess: true, canSwitchKeyboard: canSwitch, appleKeyboardRoute: { numberPad in XCTAssertFalse(numberPad); return route }, pasteboard: pasteboard ) } XCTAssertEqual(offer(.username), .passwords(canPaste: false)) XCTAssertEqual(offer(.password), .passwords(canPaste: false)) XCTAssertEqual(offer(.newPassword), .passwords(canPaste: false)) XCTAssertEqual(offer(.username, route: .list), .passwords(canPaste: false, opensList: true), "Emoji or another keyboard on") XCTAssertEqual(offer(.username, canSwitch: false), .passwords(canPaste: false, opensList: true), "the Duo split") XCTAssertNil(offer(.username, route: nil), "only Hex: nothing to switch to") XCTAssertNil(offer(.username, empty: false)) XCTAssertNil(offer(.emailAddress)) XCTAssertNil(offer(nil)) pasteboard.string = "ryan@example.com" XCTAssertEqual(offer(.username), .passwords(canPaste: true)) XCTAssertEqual(offer(.username, route: nil), .paste) let strip = KeyboardStripView() strip.update(suggestions: [], showsUndo: false, statusText: nil, clipboard: .passwords(canPaste: false)) let chip = strip.subviews.compactMap { $0 as? UIButton }.first { $0.configuration?.title == "Passwords" } XCTAssertEqual(chip?.isHidden, false) XCTAssertEqual(chip?.accessibilityHint, "Switches to the next keyboard, where iOS offers your saved passwords") } func testApplesKeyboardIsNotEmojiOrAnExtension() { typealias Mode = KeyboardInputModes.Mode let english = Mode(primaryLanguage: "en-US", className: "UIKeyboardInputMode") let hex = Mode(primaryLanguage: "en-US", className: "UIKeyboardExtensionInputMode") let emoji = Mode(primaryLanguage: "emoji", className: "UIKeyboardInputMode") XCTAssertTrue(KeyboardInputModes.hasAppleKeyboard([hex, emoji, english])) XCTAssertFalse(KeyboardInputModes.hasAppleKeyboard([hex, emoji])) XCTAssertFalse(KeyboardInputModes.hasAppleKeyboard([hex])) XCTAssertFalse(KeyboardInputModes.hasAppleKeyboard([hex, hex, emoji]), "another third-party keyboard gets no code either") } /// TestFlight build 126: in Safari's one-time-code field the switch landed on Emoji or another /// third-party keyboard about half the time; on a number pad it always reached Apple's. Where /// it could miss, the chip opens iOS's keyboard list instead. func testCodeFromMessagesOpensTheListWhereTheSwitchCouldMissApplesKeyboard() { typealias Mode = KeyboardInputModes.Mode let english = Mode(primaryLanguage: "en-US", className: "UIKeyboardInputMode") let french = Mode(primaryLanguage: "fr-FR", className: "UIKeyboardInputMode") let hex = Mode(primaryLanguage: "en-US", className: "UIKeyboardExtensionInputMode") let other = Mode(primaryLanguage: "en-US", className: "UIKeyboardExtensionInputMode") let emoji = Mode(primaryLanguage: "emoji", className: "UIKeyboardInputMode") for modes in [[english, hex], [hex, english], [english, french, hex]] { XCTAssertEqual(KeyboardInputModes.route(modes, numberPad: false), .next) XCTAssertEqual(KeyboardInputModes.route(modes, numberPad: true), .next) } for modes in [[english, emoji, hex], [hex, emoji, english], [hex, other, english], [english, hex, other]] { XCTAssertEqual(KeyboardInputModes.route(modes, numberPad: false), .list) XCTAssertEqual(KeyboardInputModes.route(modes, numberPad: true), .next) } XCTAssertNil(KeyboardInputModes.route([hex, emoji], numberPad: true)) let pasteboard = UIPasteboard.withUniqueName() defer { UIPasteboard.remove(withName: pasteboard.name) } let tracker = KeyboardClipboardTracker(defaults: defaults) func offer(_ keyboardType: UIKeyboardType, fullAccess: Bool = true) -> KeyboardClipboardOffer? { tracker.offer( textContentType: .oneTimeCode, keyboardType: keyboardType, autocorrectionType: .yes, fieldIsEmpty: true, hasFullAccess: fullAccess, appleKeyboardRoute: { numberPad in KeyboardInputModes.route([english, emoji, hex], numberPad: numberPad) }, pasteboard: pasteboard ) } XCTAssertEqual(offer(.default), .code(canPaste: false, opensList: true), "Safari's field with Emoji on") XCTAssertEqual(offer(.numberPad), .code(canPaste: false)) XCTAssertEqual(offer(.default, fullAccess: false), .code(canPaste: false, opensList: true), "needs no Full Access") pasteboard.string = "482913" XCTAssertEqual(offer(.default), .code(canPaste: true, opensList: true)) XCTAssertEqual(offer(.numberPad, fullAccess: false), .code(canPaste: false), "no Paste without Full Access") } }