hex/ios/HEXMobileTests/HexKeyGestureTests.swift
Ryan Vogel 05849318bd
iOS: fewer paste prompts, and a translate switch in the keyboard (#2134)
* iOS: Translate to <language> switch in the Hex key menu, with a badge on the Hex key

A tester asked for a translate button where they dictate, not an app setting.
Translate what I say could only be turned on in Settings; the keyboards only
had a "Paste in English / Paste in Spanish" pair while it was already on.

- Full keyboard Hex key menu: "Translate to English" switch (checkmark while
  on) and "Translation language ›", a page of up to six targets (current,
  English, dictation and preferred languages, then common ones; only those
  Apple's Translation lists once it has answered). Picking one sets the target
  and turns translation on.
- Dictation bar language tray: the same switch as a chip; the tray also opens
  when there's no language choice but translation is available.
- While dictations paste translated, the Hex key wears the target's code
  ("EN") in its corner, and the dictation bar shows "→ EN" beside the Hex mark
  (its language label drops the "→ English" it now repeats).
- Same App Group keys as Settings: the switch writes
  experiments.translateDictation (on also sets pastesTranslation to
  translated, as Settings' switch does); Settings' row re-reads everything on
  appear and on returning to the foreground.
- Replaced: the menu's "Paste in English / Paste in Spanish" rows and the bar's
  "Paste in English" chip (and onPasteTranslationSelected), now one switch.
  Settings keeps its Paste in English toggle.
- The keyboard target now includes the app's String Catalog, so the Hex key
  menu (and the shared strings it already had) follow a Chinese iPhone; new
  zh-Hans strings: Language, Translate to %@, Translating to %@, Translation
  language. Menu's Language/Automatic are localized now.

Verified on the iOS 27.2 simulator: HEXMobileTests 391 run, 0 failures
(1 skipped); in the real keyboard in Messages, the menu switch and target page
wrote experiments.translateDictation / .target (en -> ja, then off) and the
badge followed; snapshot scenes in light, dark and zh-Hans.

* iOS: Paste chip reads once and points to Paste from Other Apps › Allow

A tester had to tap Allow Paste for every paste from the keyboard. iOS asks for
each programmatic read while Settings › Apps › Hex › Paste from Other Apps is
Ask (the default). No read path avoids it inside a keyboard: UIPasteControl
draws blank there (TestFlight build 4) and detectPatterns can't return the
text. But that one setting covers the keyboard, so the fix is to point to it.

Checked on the iOS 27.2 simulator: pasted resolves the keyboard's identity to
anomaly.hex.mobile (TCCGetIdentityForCredential), a keyboard read alone (Hex
never launched) made "Paste from Other Apps: Ask" appear under Hex, and with
Allow the chip pasted in Messages and Safari with only iOS's "Hex pasted
from..." banner.

- One read per paste: text if hasStrings, else the URL. Before, a link was read
  as string then url, which could ask twice.
- The read blocks while iOS asks, so a read that took 0.3 s or more asked: the
  strip then shows "Settings › Apps › Hex › Paste from Other Apps › Allow" for
  8 s (VoiceOver gets a sentence), the first 3 times only. A refused read
  (Don't Allow, or the setting is Deny, while the clipboard has text) always
  shows it, since the chip did nothing. Typing takes the strip back; while it
  shows, the pasted text's suggestions wait.
- zh-Hans strings for both. The chip, its slot and offering only a fresh copy
  are unchanged.

Verified: HEXMobileTests 393 run, 0 failures (1 skipped), including two new
KeyboardClipboardTests; on the simulator, Ask -> hint in Messages and Safari
(the simulator can't show the prompt, so its read is refused), Allow -> pasted
with no prompt and no hint. A real iPhone must confirm the prompt's timing
(>= 0.3 s) and that Allow stops it there too.
2026-11-21 03:24:58 +00:00

247 lines
11 KiB
Swift

import CoreGraphics
import UIKit
import XCTest
@testable import HEX
final class HexKeyGestureTests: XCTestCase {
func testTapDictates() {
var gesture = HexKeyGesture()
gesture.touchDown(at: .zero)
XCTAssertEqual(gesture.touchMoved(to: CGPoint(x: 3, y: 2)), .ignored, "a jitter is still a tap")
XCTAssertEqual(gesture.touchUp(), .tap)
XCTAssertEqual(gesture.holdElapsed(), .ignored, "a timer left over from the tap")
XCTAssertEqual(gesture.state, .idle)
}
func testStillHoldFiresOnceAndReleaseFinishes() {
var gesture = HexKeyGesture()
gesture.touchDown(at: .zero)
XCTAssertEqual(gesture.holdElapsed(), .hold)
XCTAssertEqual(gesture.holdElapsed(), .ignored)
XCTAssertEqual(gesture.touchUp(), .release, "push to talk: lifting finishes")
XCTAssertEqual(gesture.touchUp(), .ignored)
}
func testMoveBeforeHoldOpensMenu() {
var gesture = HexKeyGesture()
gesture.touchDown(at: CGPoint(x: 20, y: 20))
XCTAssertEqual(gesture.touchMoved(to: CGPoint(x: 20, y: 9)), .openMenu)
XCTAssertEqual(gesture.holdElapsed(), .ignored, "the menu won, no dictation")
XCTAssertEqual(gesture.touchMoved(to: CGPoint(x: 0, y: -40)), .dragMenu)
XCTAssertEqual(gesture.touchUp(), .releaseMenu)
XCTAssertEqual(gesture.state, .idle)
}
func testMoveAfterHoldDoesNothing() {
var gesture = HexKeyGesture()
gesture.touchDown(at: .zero)
XCTAssertEqual(gesture.holdElapsed(), .hold)
XCTAssertEqual(gesture.touchMoved(to: CGPoint(x: 0, y: -80)), .ignored, "no menu once dictating")
XCTAssertEqual(gesture.touchUp(), .release)
}
func testCancelResets() {
var gesture = HexKeyGesture()
gesture.touchDown(at: .zero)
XCTAssertEqual(gesture.cancel(), .abandon, "a press that was neither tap nor hold: drop its capture")
XCTAssertEqual(gesture.state, .idle)
XCTAssertEqual(gesture.holdElapsed(), .ignored)
XCTAssertEqual(gesture.touchMoved(to: CGPoint(x: 50, y: 0)), .ignored)
}
func testCancelAfterHoldKeepsDictating() {
var gesture = HexKeyGesture()
gesture.touchDown(at: .zero)
XCTAssertEqual(gesture.holdElapsed(), .hold)
XCTAssertEqual(gesture.cancel(), .ignored, "the hold already dictates; Done finishes it")
XCTAssertEqual(gesture.cancel(), .ignored)
}
/// In-keyboard dictation listens from touch-down: the key reports the press at once, and
/// abandons it only when the touch is cancelled before it is a tap or a hold.
@MainActor
func testHexKeyReportsThePressAndAbandonsOnlyACancelledOne() {
let key = HexKeyControl(frame: CGRect(x: 0, y: 0, width: 44, height: 44))
var presses = 0, abandoned = 0, dictations = 0
key.onPress = { _ in presses += 1 }
key.onPressAbandoned = { abandoned += 1 }
key.onHold = { dictations += 1 }
_ = key.beginTracking(UITouch(), with: nil)
XCTAssertEqual(presses, 1, "touch-down, before the lift")
XCTAssertEqual(dictations, 0)
key.endTracking(UITouch(), with: nil)
XCTAssertEqual(dictations, 1, "a tap keeps the capture")
XCTAssertEqual(abandoned, 0)
_ = key.beginTracking(UITouch(), with: nil)
key.cancelTracking(with: nil)
XCTAssertEqual(presses, 2)
XCTAssertEqual(abandoned, 1, "the system took the touch: drop it")
XCTAssertEqual(dictations, 1)
}
}
/// Translate what I say where you dictate: the Translate to <language> switch and its targets
/// in the full keyboard's Hex key menu and the dictation bar, and the paste switch Settings keeps.
@MainActor
final class TranslationPasteSwitchTests: 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 testTheChoiceNamesBothLanguagesAndShowsOnlyWhileTranslating() {
let settings = DictationTranslationSettings(defaults: defaults)
XCTAssertNil(settings.pasteChoice(source: "es-MX"), "off: no switch")
settings.isEnabled = true
settings.targetLanguage = "en"
let choice = settings.pasteChoice(source: "es-MX")
XCTAssertEqual(choice?.translatedTitle, "Paste in English")
XCTAssertEqual(choice?.spokenTitle, "Paste in Spanish")
XCTAssertNil(settings.pasteChoice(source: "en-GB"), "dictating in the target: nothing to choose")
XCTAssertEqual(settings.pasteChoice(source: nil)?.spokenTitle, "Paste as spoken")
settings.targetLanguage = "nb"
XCTAssertEqual(settings.pasteChoice(source: "de-DE")?.translatedTitle, "Paste in Norwegian")
}
func testTheLastChoiceIsRememberedAndAsSpokenSkipsTranslation() async {
let settings = DictationTranslationSettings(defaults: defaults)
settings.isEnabled = true
settings.targetLanguage = "en"
XCTAssertTrue(settings.pastesTranslation, "translated until you pick otherwise")
settings.pastesTranslation = false
XCTAssertFalse(DictationTranslationSettings(defaults: defaults).pastesTranslation)
let translated = await DictationTranslator.translateIfEnabled(
"Nos vemos mañana.",
source: Locale.Language(identifier: "es-ES"),
settings: settings
)
XCTAssertNil(translated, "as spoken pastes the transcript itself")
}
func testTheKeyboardShowsWhereDictationsGo() {
let model = KeyboardViewModel()
model.dictationLanguage = "es-ES"
model.languageChoices = ["es-ES", "en-US"]
XCTAssertNil(model.pasteChoice)
XCTAssertEqual(model.languageSummary, "Español")
model.translationTarget = "en"
XCTAssertEqual(model.pasteChoice?.translatedTitle, "Paste in English")
XCTAssertEqual(model.languageSummary, "Español → English")
model.pastesTranslation = false
XCTAssertEqual(model.languageSummary, "Español", "as spoken")
model.hasFullAccess = false
XCTAssertNil(model.pasteChoice, "no Full Access, no translation")
}
func testTheHexKeyMenuSwitchesTranslationAndPicksItsTarget() {
let menu = HexStyleMenuView(frame: CGRect(x: 0, y: 0, width: 400, height: 300))
menu.languageChoices = ["es-ES", "en-US"]
menu.dictationLanguage = "es-ES"
XCTAssertFalse(labels(in: menu).contains { $0.hasPrefix("Translat") }, "no rows without a target (before iOS 26)")
// Off: the switch names the target, and the second row opens the targets.
menu.translationTarget = "en"
menu.translationTargets = ["en", "ja", "es"]
var toggled: Bool?
var target: String?
menu.onTranslationToggle = { toggled = $0 }
menu.onTranslationTarget = { target = $0 }
let toggle = controls(in: menu).first { $0.accessibilityLabel == "Translate to English" }
XCTAssertEqual(toggle?.accessibilityValue, "Off")
XCTAssertEqual(toggle?.accessibilityTraits.contains(.selected), false)
toggle?.sendActions(for: .touchUpInside)
XCTAssertEqual(toggled, true, "a tap turns it on")
XCTAssertTrue(labels(in: menu).contains("Translation language"))
menu.isTranslating = true
let on = controls(in: menu).first { $0.accessibilityLabel == "Translate to English" }
XCTAssertEqual(on?.accessibilityValue, "On")
on?.sendActions(for: .touchUpInside)
XCTAssertEqual(toggled, false, "and off again")
menu.show(page: .translation)
XCTAssertEqual(labels(in: menu), ["English", "Japanese", "Spanish"])
controls(in: menu)[1].sendActions(for: .touchUpInside)
XCTAssertEqual(target, "ja")
// One target: nothing to pick, so only the switch.
menu.show(page: .styles)
menu.translationTargets = ["en"]
XCTAssertFalse(labels(in: menu).contains("Translation language"))
}
func testTheSwitchIsSettingsSwitchAndTurningItOnTranslates() {
let settings = DictationTranslationSettings(defaults: defaults)
XCTAssertFalse(settings.isTranslating)
settings.pastesTranslation = false
settings.isTranslating = true
XCTAssertTrue(settings.isEnabled, "the same App Group key as Settings' switch")
XCTAssertTrue(settings.pastesTranslation, "on means translated, whatever the paste switch said")
settings.pastesTranslation = false
XCTAssertFalse(settings.isTranslating, "Settings' Paste in English off: not translating")
settings.isTranslating = true
settings.isTranslating = false
XCTAssertFalse(settings.isEnabled)
}
func testTheMenuOffersTargetsAppleTranslatesInto() {
let choices = DictationTranslationSettings.targetChoices(
current: "ja",
dictation: ["zh-CN", "en-US", "nb-NO"],
preferred: ["zh-Hans-CN", "de-DE", "fr-FR", "it-IT", "ko-KR", "pt-BR"],
supported: ["en", "ja", "zh", "de", "fr", "it", "ko", "pt"]
)
XCTAssertEqual(choices, ["ja", "en", "zh", "de", "fr", "it"], "one per language, no Norwegian, six at most")
XCTAssertEqual(
DictationTranslationSettings.targetChoices(current: "nb", dictation: [], preferred: [], supported: ["en", "ja"]),
["nb", "en", "ja"],
"the current target stays even when Apple can't translate into it"
)
XCTAssertEqual(
DictationTranslationSettings.targetChoices(current: "en", dictation: ["en-US"], preferred: ["en-US"], supported: nil),
["en", "es", "zh", "fr", "de", "ja"],
"an English-only iPhone still gets languages to pick, before Apple's list arrives too"
)
}
func testTheHexKeyWearsTheTargetWhileTranslating() {
let model = KeyboardViewModel()
model.translationLanguage = "en"
XCTAssertTrue(model.showsTranslationSwitch)
XCTAssertNil(model.translatingTo, "off: no badge")
model.translationTarget = "en"
XCTAssertEqual(model.translatingTo, "en")
XCTAssertEqual(TranslationPasteChoice.badge(target: "en"), "EN")
XCTAssertEqual(TranslationPasteChoice.badge(target: "zh-TW"), "ZH")
model.pastesTranslation = false
XCTAssertNil(model.translatingTo, "pasting as spoken: no badge")
model.pastesTranslation = true
model.hasFullAccess = false
XCTAssertNil(model.translatingTo)
XCTAssertFalse(model.showsTranslationSwitch)
let key = HexKeyControl()
key.isWarm = true
key.translatingTo = "en"
XCTAssertEqual(key.accessibilityLabel, "Dictate with Hex, Translating to English")
}
private func controls(in view: UIView) -> [UIControl] {
view.subviews.flatMap { ($0 as? UIControl).map { [$0] } ?? [] + controls(in: $0) }
}
private func labels(in view: UIView) -> [String] {
controls(in: view).compactMap(\.accessibilityLabel)
}
}