hex/ios/HEXMobileTests/FullKeyboardEngineTests.swift
Ryan Vogel bb5875cea8 iOS: Norwegian, Spanish and German full-keyboard layouts
Testers asked for the Norwegian layout and, with it, Spanish and
German. The layout follows the dictation language unless set.

What changed:
- KeyboardLanguage holds each layout's letter rows, long-press accents
  and spelling dictionary: Norsk (a-ring ending row 1, o-slash and ae
  row 2), Espanol (n-tilde ending row 2), Deutsch (QWERTZ, u/o/a-umlaut,
  sharp s on a held S), as Apple lays them out. Adding a language is a
  new case with those three filled in.
- KeyboardLayout takes the language. Rows of 11 keys narrow only the
  character keys; shift, delete and the bottom row keep their English
  (10-key) sizes, and row 3's letters sit centred between shift and
  delete. letterWidth stays the 10-key width; characterWidth is new.
- Experiments > Full keyboard > Keyboard layout: Automatic, English,
  Norsk, Espanol, Deutsch (settings.keyboardLayout). Automatic follows
  the dictation language (no layout yet for Hebrew and others: English),
  else the first iPhone keyboard or preferred language with a layout. A
  pick in the keyboard's language switch changes the layout at once.
- Autocorrect uses the layout's UITextChecker dictionary (nb, es, de);
  without one it offers nothing but your text replacements instead of
  correcting into English. The English contractions and typo list
  (i -> I) apply only to English; neighbouring-key costs follow the
  layout. LanguageSettings.effectiveDictationLanguage now prefers what
  Automatic picks over the last language dictation ran in.
- Typing logs record the layout's language; the snapshot stage takes
  --keyboard-layout, follows --dictation-language and --accent-key.
- TypingLogTests pins English, since FullKeyboardView now reads the
  App Group's dictation language.

Verified: xcodebuild test on the iPhone 18 Pro simulator (iOS 27.2):
206 tests, 0 failures, 1 skipped (the Apple Speech clip test), with
layout tests for every language at 320-440 pt and landscape (rows,
11-key widths, hit-frame tiling, balloons, accent menus) and autocorrect
tests; Release generic-iOS build with CODE_SIGNING_ALLOWED=NO succeeds;
light and dark screenshots of each layout, its accents, shift, the
settings row and the dictation bar's re-shot trigger checked by eye. The
simulator has dictionaries for nb, es and de. Accent lists are from
Apple's layouts as remembered, not checked on a device; the 123 and #+=
pages are English's in every language.
2026-11-20 07:52:48 -05:00

298 lines
10 KiB
Swift

import UIKit
import XCTest
@testable import HEX
@MainActor
final class FullKeyboardEngineTests: XCTestCase {
private var target: FixtureTextTarget!
private var engine: FullKeyboardEngine!
private var now: TimeInterval = 100
private var suiteName: String!
private var defaults: UserDefaults!
override func setUp() async throws {
suiteName = "FullKeyboardEngineTests-\(UUID().uuidString)"
defaults = UserDefaults(suiteName: suiteName)!
target = FixtureTextTarget()
target.autocapitalizationType = UITextAutocapitalizationType.none
let ac = AutocorrectEngine(learnedWords: LearnedWordsStore(defaults: defaults))
engine = FullKeyboardEngine(target: target, autocorrect: ac)
engine.width = 393
engine.clock = { [unowned self] in self.now }
engine.configureForField()
}
override func tearDown() async throws {
defaults.removePersistentDomain(forName: suiteName)
}
private func point(_ kind: KeyKind) -> CGPoint {
let key = engine.layout.key(kind)!
return CGPoint(x: key.frame.midX, y: key.frame.midY)
}
private func down(_ id: Int, _ kind: KeyKind) { engine.touchBegan(id: id, at: point(kind)) }
private func up(_ id: Int, _ kind: KeyKind) { engine.touchEnded(id: id, at: point(kind)) }
private func tap(_ kind: KeyKind) {
let at = point(kind)
engine.touchBegan(id: 1, at: at)
engine.touchEnded(id: 1, at: at)
}
private func type(_ text: String) {
for ch in text {
tap(ch == " " ? .space : .character(String(ch)))
now += 0.5
}
}
func testRolloverKeepsOrder() {
down(1, .character("t"))
down(2, .character("h"))
up(1, .character("t"))
up(2, .character("h"))
XCTAssertEqual(target.text, "th")
}
func testRolloverWhenTheSecondKeyLiftsFirst() {
down(1, .character("t"))
down(2, .character("h"))
up(2, .character("h"))
up(1, .character("t"))
XCTAssertEqual(target.text, "th")
}
func testSlidingToAnotherKeyTypesWhereTheFingerLifts() {
down(1, .character("f"))
engine.touchMoved(id: 1, to: point(.character("g")))
XCTAssertEqual(engine.previewKey?.kind, .character("g"))
up(1, .character("g"))
XCTAssertEqual(target.text, "g")
}
func testShiftChordAndOneShot() {
down(1, .shift)
down(2, .character("a"))
up(2, .character("a"))
down(3, .character("b"))
up(3, .character("b"))
up(1, .shift)
tap(.character("c"))
XCTAssertEqual(target.text, "ABc")
tap(.shift)
tap(.character("d"))
tap(.character("e"))
XCTAssertEqual(target.text, "ABcDe")
}
func testDoubleTapShiftLocks() {
tap(.shift)
now += 0.1
tap(.shift)
type("hi")
XCTAssertEqual(target.text, "HI")
XCTAssertEqual(engine.shift.state, .locked)
}
func testHoldNumbersSlideToADigitAndLetGo() {
down(1, .layerSwitch(.numbers))
XCTAssertEqual(engine.layout.layer, .numbers)
engine.touchMoved(id: 1, to: point(.character("7")))
engine.touchEnded(id: 1, at: point(.character("7")))
XCTAssertEqual(target.text, "7")
XCTAssertEqual(engine.layout.layer, .letters)
}
func testNumbersStayUntilSpace() {
tap(.layerSwitch(.numbers))
XCTAssertEqual(engine.layout.layer, .numbers)
tap(.character("4"))
tap(.character("2"))
XCTAssertEqual(engine.layout.layer, .numbers)
tap(.space)
XCTAssertEqual(engine.layout.layer, .letters)
XCTAssertEqual(target.text, "42 ")
}
func testDoubleSpaceTypesAPeriod() {
type("ok")
tap(.space)
now += 0.2
tap(.space)
XCTAssertEqual(target.text, "ok. ")
now += 0.2
tap(.space)
XCTAssertEqual(target.text, "ok. ", "a third space is just a space")
}
func testSlowDoubleSpaceStaysTwoSpaces() {
type("ok")
tap(.space)
now += 0.5
tap(.space)
XCTAssertEqual(target.text, "ok ")
}
func testSentenceAutoCapitalization() {
target.autocapitalizationType = .sentences
engine.configureForField()
XCTAssertEqual(engine.shift.state, .on)
type("hi")
tap(.space)
now += 0.2
tap(.space)
type("there")
XCTAssertEqual(target.text, "Hi. There")
}
func testAcceleratingDeleteRampAndWordDelete() {
type("hello world")
down(1, .delete)
XCTAssertEqual(target.text, "hello worl")
// Initial repeat deletes 1 char.
engine.repeatDelete(heldFor: FullKeyboardEngine.repeatDelay)
XCTAssertEqual(target.text, "hello wor")
// Accelerated repeat deletes 2 chars per tick.
engine.repeatDelete(heldFor: FullKeyboardEngine.fastRepeatAfter)
XCTAssertEqual(target.text, "hello w")
// After 1.4 s, removes whole words backward.
engine.repeatDelete(heldFor: FullKeyboardEngine.wordDeleteAfter)
XCTAssertEqual(target.text, "hello ")
engine.repeatDelete(heldFor: FullKeyboardEngine.wordDeleteAfter)
XCTAssertEqual(target.text, "")
up(1, .delete)
}
func testLongPressAccentPickerSelectsAndUppercaseWorks() {
tap(.shift)
down(1, .character("e"))
XCTAssertTrue(engine.openAccentMenuIfEligible(forTouch: 1))
let menu = try! XCTUnwrap(engine.activeAccentMenu)
XCTAssertNil(engine.previewKey, "single-key balloon hides while accent picker is open")
XCTAssertEqual(menu.options.first, "E")
// Slide to index 2 ("É").
let cell = menu.cellFrames[2]
engine.touchMoved(id: 1, to: CGPoint(x: cell.midX, y: cell.midY))
engine.touchEnded(id: 1, at: CGPoint(x: cell.midX, y: cell.midY))
XCTAssertNil(engine.activeAccentMenu)
XCTAssertEqual(target.text, "É")
XCTAssertEqual(engine.shift.state, .off)
}
func testSpacebarTrackpadMovesCaretWithoutInsertingSpace() {
type("helo")
let spaceStart = point(.space)
engine.touchBegan(id: 1, at: spaceStart)
// Enter trackpad mode (-12 pt) then drag left by 16 pt (-2 chars: before "lo").
let enterPoint = CGPoint(x: spaceStart.x - 12, y: spaceStart.y)
engine.touchMoved(id: 1, to: enterPoint)
XCTAssertTrue(engine.isTrackpadActive)
engine.touchMoved(id: 1, to: CGPoint(x: enterPoint.x - 16, y: spaceStart.y))
engine.touchEnded(id: 1, at: CGPoint(x: enterPoint.x - 16, y: spaceStart.y))
XCTAssertFalse(engine.isTrackpadActive)
XCTAssertEqual(target.cursorIndex, 2)
tap(.character("l"))
XCTAssertEqual(target.text, "hello")
}
func testEnablesReturnKeyAutomaticallyBlocksEmptyField() {
target.enablesReturnKeyAutomatically = true
tap(.returnKey)
XCTAssertEqual(target.text, "")
type("ok")
tap(.returnKey)
XCTAssertEqual(target.text, "ok\n")
}
func testRecordingPanelAllocatesLazilyAndTearsDownOnIdle() {
let model = KeyboardViewModel()
let view = FullKeyboardView(model: model, target: target)
view.frame = CGRect(x: 0, y: 0, width: 393, height: 254)
view.layoutIfNeeded()
XCTAssertFalse(view.hasAllocatedRecordingPanel, "no Metal scope allocated while typing")
let t: TimeInterval = 1_000
model.applyBridgeState(
hasFullAccess: true,
snapshot: KeyboardSnapshot(state: .recording, heartbeat: t, expiresAt: t + 60, recordingStartedAt: t - 1, jobID: "j1"),
isModelInstalled: true,
style: .polished,
returnAppID: nil,
now: t,
clearPending: false,
animated: false
)
RunLoop.main.run(until: Date().addingTimeInterval(0.05))
XCTAssertTrue(view.hasAllocatedRecordingPanel)
model.applyBridgeState(
hasFullAccess: true,
snapshot: KeyboardSnapshot(state: .ready, heartbeat: t + 1, expiresAt: t + 60),
isModelInstalled: true,
style: .polished,
returnAppID: nil,
now: t + 1,
clearPending: true,
animated: false
)
RunLoop.main.run(until: Date().addingTimeInterval(0.05))
XCTAssertFalse(view.hasAllocatedRecordingPanel, "Metal scope torn down once recording ends")
}
func testSpaceBarNamesTheDictationLanguage() {
let model = KeyboardViewModel()
let view = FullKeyboardView(model: model, target: target)
view.frame = CGRect(x: 0, y: 0, width: 393, height: 262)
view.layoutIfNeeded()
XCTAssertNil(view.plane.spaceLabel, "no label until the language is known")
model.automaticLanguage = "en-US"
model.languageChoices = ["nb-NO", "en-US"]
RunLoop.main.run(until: Date().addingTimeInterval(0.05))
XCTAssertEqual(view.plane.spaceLabel, "English")
model.dictationLanguage = "nb-NO"
RunLoop.main.run(until: Date().addingTimeInterval(0.05))
XCTAssertEqual(view.plane.spaceLabel, "Norsk")
model.hasFullAccess = false
RunLoop.main.run(until: Date().addingTimeInterval(0.05))
XCTAssertNil(view.plane.spaceLabel, "without Full Access the keyboard can't dictate")
}
func testTypesOnTheNorwegianLayout() {
engine.language = .norwegian
XCTAssertNotNil(engine.layout.key(.character("å")))
XCTAssertEqual(engine.autocorrect.language, .norwegian)
tap(.shift)
tap(.character("ø"))
tap(.character("l"))
XCTAssertEqual(target.text, "Øl")
let o = engine.layout.key(.character("ø"))!
engine.touchBegan(id: 7, at: CGPoint(x: o.frame.midX, y: o.frame.midY))
XCTAssertTrue(engine.openAccentMenuIfEligible(forTouch: 7))
XCTAssertEqual(Set(engine.activeAccentMenu?.options ?? []), ["ø", "ö"])
engine.touchCancelled(id: 7)
engine.language = .german
XCTAssertNil(engine.layout.key(.character("å")))
XCTAssertNotNil(engine.layout.key(.character("ü")))
}
func testEmailFieldShowsAtAndDot() {
target.keyboardType = .emailAddress
engine.configureForField()
tap(.character("@"))
tap(.character("."))
XCTAssertEqual(target.text, "@.")
}
func testNumberPadOpensOnNumbers() {
target.keyboardType = .numberPad
engine.configureForField()
XCTAssertEqual(engine.layout.layer, .numbers)
}
}