hex/ios/HEXMobileTests/KeyboardClipboardTests.swift
Ryan Vogel 3f1b9af152
iOS keyboard: Code from Messages opens the keyboard list where Apple's keyboard is split, instead of offering nothing (#2473)
TestFlight build 147, iPhone Duo (iOS 27.2.1): "enable the Apple Auto SMS ... the
OTP in the keyboard". iOS gives SMS codes only to Apple's keyboard, so Hex
offers Code from Messages in an empty one-time-code field. Where Apple's
keyboard is split (the Duo unfolded and upright) it offered nothing at all
(#2349): its switch to the next keyboard can end the host there, so the chip
was dropped and only the globe was left, with nothing pointing to it.

KeyboardClipboardTracker.offer now keeps the chip there and makes it open iOS's
keyboard list (opensList), the path #2340 and #2417 made safe for the globe:
the chip's touch already goes through InputModeListTouch with every tap held
back, so the list opens and the person picks Apple's keyboard. Everywhere else
the chip is unchanged (a switch where only Apple's keyboard follows, the list
where Emoji or another keyboard could). Replaced: "only Paste is offered" on
the split posture; a copied code still offers Paste beside the chip.

KeyboardClipboardTests: testNoCodeFromMessagesWhereApplesKeyboardSplits becomes
testCodeFromMessagesOpensTheListWhereApplesKeyboardSplits (the split posture
now gets .code(opensList: true), with and without a copied code).

Verified on the Mac Studio, iPhone Duo simulator (iOS 27.2, 24B91016s),
unfolded and upright, Apple's split keyboard on, real touches on the inner
screen: 10 fresh hosts (5 native .oneTimeCode fields with autocorrection off,
5 Safari autocomplete="one-time-code" fields): the chip showed 10/10, a tap
opened the keyboard list 10/10, picking English (US) brought Apple's split
keyboard 10/10, no host ended, no crash reports. Build 147 showed no chip in the
same two fields.
2026-11-23 00:27:29 -05:00

209 lines
12 KiB
Swift

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..<KeyboardClipboardTracker.allowPasteHintLimit {
XCTAssertTrue(tracker.shouldPointToAllowPaste(readDuration: 1.2, refused: false))
XCTAssertTrue(tracker.needsAllowPasteSetting)
}
XCTAssertFalse(
KeyboardClipboardTracker(defaults: defaults).shouldPointToAllowPaste(readDuration: 1.2, refused: false),
"after a few, Ask is left alone: someone may want it"
)
XCTAssertTrue(tracker.needsAllowPasteSetting, "Hex's app page still remembers iOS prompted")
XCTAssertTrue(tracker.shouldPointToAllowPaste(readDuration: 0.01, refused: true), "refused: the chip did nothing, always say why")
}
func testSilentCrossAppPasteClearsAllowPasteSettingButOwnCopyDoesNot() {
let pasteboard = UIPasteboard.withUniqueName()
defer { UIPasteboard.remove(withName: pasteboard.name) }
let tracker = KeyboardClipboardTracker(defaults: defaults)
XCTAssertTrue(tracker.shouldPointToAllowPaste(readDuration: 1.1, refused: false))
XCTAssertTrue(tracker.needsAllowPasteSetting)
// Hex's own copy never prompts iOS, so pasting it fast doesn't prove Allow is on.
pasteboard.string = "from hex"
tracker.markOwnedCopy(pasteboard: pasteboard)
tracker.recordSilentPaste(pasteboard: pasteboard)
XCTAssertTrue(tracker.needsAllowPasteSetting)
// A copy from another app pasted without prompting proves Paste from Other Apps is Allow.
pasteboard.string = "from opencode"
tracker.recordSilentPaste(pasteboard: pasteboard)
XCTAssertFalse(tracker.needsAllowPasteSetting)
// Dismissing hides the app banner until iOS prompts again.
XCTAssertTrue(tracker.shouldPointToAllowPaste(readDuration: 0.01, refused: true))
XCTAssertTrue(tracker.needsAllowPasteSetting)
tracker.dismissAllowPasteSetting()
XCTAssertFalse(tracker.needsAllowPasteSetting)
}
@MainActor
func testAllowPasteStripNoticeOpensPasteSettings() {
let model = KeyboardViewModel()
var opened: [URL] = []
model.onOpenURL = { opened.append($0) }
let view = FullKeyboardView(model: model, target: FixtureTextTarget())
XCTAssertNil(view.allowPasteNotice)
view.showAllowPasteHint()
XCTAssertEqual(view.allowPasteNotice, KeyboardClipboardTracker.allowPasteNotice)
view.stripView.onOpenNotice?()
XCTAssertNil(view.allowPasteNotice, "tapping Settings clears the strip notice")
XCTAssertEqual(opened, [KeyboardLaunchRequest.pasteSettings.url])
}
func testCodeFromMessagesNeedsApplesKeyboard() {
let pasteboard = UIPasteboard.withUniqueName()
defer { UIPasteboard.remove(withName: pasteboard.name) }
let tracker = KeyboardClipboardTracker(defaults: defaults)
func offer(appleKeyboard: Bool) -> 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))
}
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")
}
}