hex/ios/HEXMobileTests/HexKeyGestureTests.swift
Ryan Vogel 39a3d8b710 iOS: choose per dictation whether Translate what I say pastes translated
A Spanish-speaking TestFlight tester liked Translate what I say but always
speaks Spanish and only sometimes wants the English. Until now translation
was all or nothing, set in Experiments.

- DictationTranslationSettings.pastesTranslation (App Group key
  experiments.translateDictation.pastesTranslation, default true, so
  existing users still get translations) is a switch that keeps the last
  choice. translateIfEnabled skips translation while it says as spoken, in
  the app (warm session, handoff, Action Button) and in in-keyboard
  dictation alike.
- TranslationPasteChoice names both sides in English, like the rest of
  Hex's copy: "Paste in English" / "Paste in Spanish" ("Paste as spoken"
  when the dictation language is unknown, "Norwegian" without "Bokmål").
  It exists only while the experiment is on, the keyboard has Full Access,
  and you dictate in a language other than the target.
- Full keyboard: the Hex key menu gets two rows under Language, each with a
  checkmark, and the space bar reads "Español → English" while dictations
  paste translated.
- Dictation bar: the language tray gets a "Paste in English" chip, lit
  while translating, and the language label reads "Español → English". The
  label now has layout priority so it isn't cut to "Español → E…".
- Settings: a "Paste in English" toggle under Insert in mirrors the switch.
  Turning the experiment on resets it to translated.
- Simulator fixtures: --translate-to <code> and --paste-as-spoken.

Verified: new TranslationPasteSwitchTests (titles and visibility, the
remembered choice, as spoken skipping translation, the model's label, and
the Hex key menu's rows, their checkmarks and their callback). Full
xcodebuild test: 230 tests, 0 failures, 1 skipped (AppleSpeechClipTests).
Rendered on an iPhone 18 Pro simulator (iOS 27.2): full-keyboard-style in
light and dark, translated and as spoken, without translation (unchanged),
full-keyboard-idle, keyboard-language, keyboard-warm, keyboard-recording and
experiments. The simulator can't actually translate (Apple's
LanguageAvailability says es -> en is unsupported there), so a translated
paste is unverified on device.
2026-11-20 09:38:45 -05:00

139 lines
6 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)
gesture.cancel()
XCTAssertEqual(gesture.state, .idle)
XCTAssertEqual(gesture.holdElapsed(), .ignored)
XCTAssertEqual(gesture.touchMoved(to: CGPoint(x: 50, y: 0)), .ignored)
}
}
/// Translate what I say's switch where you dictate: "Paste in English" or as spoken, in the
/// full keyboard's Hex key menu and the dictation bar, remembered for the next dictations.
@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 testTheHexKeyMenuOffersBothAndReportsThePick() {
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("Paste") }, "no rows while translation is off")
menu.pasteChoice = TranslationPasteChoice(target: "en", source: "es-ES")
menu.pastesTranslation = false
var picked: Bool?
menu.onPasteTranslation = { picked = $0 }
let rows = controls(in: menu).filter { $0.accessibilityLabel?.hasPrefix("Paste") == true }
XCTAssertEqual(rows.map(\.accessibilityLabel), ["Paste in English", "Paste in Spanish"])
XCTAssertEqual(rows.map { $0.accessibilityTraits.contains(.selected) }, [false, true])
rows[0].sendActions(for: .touchUpInside)
XCTAssertEqual(picked, true)
}
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)
}
}