mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
* iOS keyboard: touches at the screen's side edges reach the keys at once
What: KeyboardViewController.viewDidAppear turns off delaysTouchesBegan on
the keyboard window's gesture recognizers, iOS's system-gesture gate.
Why: a TestFlight tester (build 25) said they miss keys more on Hex's full
keyboard than on Apple's. Record typing logs from an iPhone show touches
within 20 pt of the side edges reaching the keys 104 ms (median, n=8) after
the finger went down, against 21 ms everywhere else (n=1,258): iOS holds
them back while it checks for an edge gesture. q, a, p, l, shift and delete
lagged and could land after the next key. Apple's keyboard has no such delay.
Verified: full HEXMobileTests suite on the iPhone 18 Pro simulator (iOS 27.2):
280 tests, 0 failures, 1 skipped (as before). The simulator never delays edge
touches (the key balloon shows at x = 1 pt while held), so whether this removes
the delay on an iPhone is unverified until the next typing log.
* iOS keyboard: a tap just above row 1 types its key, not a suggestion
What: row 1's touch targets also take the strip's bottom 4 pt, below its pills
and chips (KeyboardLayout.stripTouchBand; touchAreaFrame is the key area plus
that band and is what the key plane accepts). The Hex key's target in the strip
now stops where row 1's starts. Every other target is unchanged: gaps still
split down the middle and the outer keys still reach the screen's edges, in
every layout (Russian and Serbian included).
Why: in the live keyboard (AXe taps on the iPhone 18 Pro simulator) a tap 1 to
7 pt above a row-1 keycap accepted a suggestion. Apple's keyboard on the same
simulator types the key in the ~8 pt above its keycaps (at x = 110 it types e
from y = 590; its keycap starts at 598, the predictive bar ends at 589).
Verified: KeyboardLayoutTests now check that the hit frames tile the touch area
with no gaps or overlaps and that 2 pt above every row-1 keycap types it, in
every language, width and orientation; full suite 280 tests, 0 failures.
* iOS keyboard: the likely next letter claims up to 5 pt of its neighbour's target
What: FullKeyboardEngine.key(at:) picks the key a touch presses: the one under
it, or a neighbouring letter within letterReach (5 pt) of the touch whose odds
of coming next in the word beat the touched letter's by 0.8 nats for every point
it reaches past its own target. The odds are the word list's frequency mass
after what is typed (TypingDecoderModel.continuations; accented letters without
a key of their own count toward their base). It applies only on the letters page
of a text field, with autocorrect on and allowed by the field, outside caps lock,
for English and Spanish, whose Spatial autocorrect models FullKeyboardView now
maps whether or not that experiment is on (other layouts keep fixed targets). A
touch a letter claimed keeps it while a rolling finger stays within reach, so a
point of jitter doesn't slide it back. Record typing logs' began line names the
key pressed, with `under` when the odds moved it (docs/typing-logs.md). The
ios/README.md gains a Touch targets section for this and the two previous commits.
Why: Apple's keyboard does this (AXe on the iPhone 18 Pro simulator: after "th"
a tap 10 pt inside d types e; with nothing typed its row borders sit within about
2 pt of the gaps' middles), and Hex's targets were fixed. A fixed upward offset
was measured and left out: moving each of 643 labelled logged taps up by 2 to
8 pt landed no more of them on the meant key (586 at 0, 583 to 562 shifted).
Verified with a benchmark harness (kept outside the repo) that types taps
through FullKeyboardEngine; 5 pt and 0.8 nats/pt chosen on dev sets, checked on
held-out sets:
- held-out English (2,000 Tatoeba sentences typed with a touch model fitted to
real typing logs, 13,870 words): words mistyped before autocorrect 3,814 ->
2,504 (names aside); output word error rate with default autocorrect 13.2% ->
10.4%, with Spatial 8.5% -> 7.9%. Taps the odds moved broke 98 words that were
typed right and fixed 1,561 (8 pt: 112 and 1,921).
- held-out Spanish (13,301 words): default 22.9% -> 19.1%, Spatial 10.4% ->
10.2%; broke 46, fixed 1,451.
- fat-finger sentences (180, 1,614 words): default 15.3% -> 12.9%, Spatial
6.2% -> 5.3%. Real logged words (172): default 10.5% -> 9.3%, Spatial 4.1%
both.
- the tester's sentence, 30 tap draws: 51 of 360 slipped words now typed right
before autocorrect; Spatial 6.7% -> 6.3%.
New FullKeyboardEngineTests: the e/d border after "th" and "an", past the reach,
with autocorrect off or disallowed, a rolled finger; and the tester's sentence
from "but", tapped, through Spatial autocorrect. Full suite: 280 tests, 0
failures.
* iOS keyboard: Spatial autocorrect keeps names typed in lowercase, capitalised
What: when Spatial autocorrect reads a word typed all in lowercase that the
dictionary knows only capitalised (a name, day or place: "mary", "monday",
"boston"), it counts as a word (the 7-nat margin) and, unless a reading beats it
by that, is capitalised as the dictionary path does ("Mary").
Why: the models' word lists are lowercase only (SCOWL), so a lowercase name was
an unknown word with a 2-nat margin. On the held-out English benchmark Spatial
changed 287 of 1,488 correctly typed names, against 85 for the default engine:
"mary" -> "may" 98 times, "boston" -> "boson", "monday" -> "money". This is the
main thing that stood against turning Spatial autocorrect on by default.
Verified (benchmark harness, held-out sets, Spatial autocorrect without and with
the next-letter odds): English correctly typed names changed 287 -> 82 (default
engine 85), output word error rate 8.5% -> 7.1% (6.5% with the odds); Spanish
names 182 -> 5 of 732, 10.4% -> 9.2% (8.8% with the odds); fat-finger sentences
6.2% -> 6.0%; the tester's sentence unchanged (6.8%). New
AutocorrectEngineTests case: "see mary on monday in boston" typed at the keys'
centres comes out "see Mary on Monday in Boston". Full suite: 280 tests, 0
failures.
498 lines
28 KiB
Swift
498 lines
28 KiB
Swift
import CoreGraphics
|
||
import XCTest
|
||
@testable import HEX
|
||
|
||
final class KeyboardLayoutTests: XCTestCase {
|
||
private let portraitWidths: [CGFloat] = [320, 375, 393, 402, 430, 440]
|
||
private let layers: [KeyboardLayer] = [.letters, .numbers, .symbols]
|
||
private let fields: [KeyboardFieldKind] = [.text, .url, .email]
|
||
|
||
private func everyPortraitLayout() -> [KeyboardLayout] {
|
||
var layouts: [KeyboardLayout] = []
|
||
for width in portraitWidths {
|
||
for layer in layers {
|
||
for field in fields {
|
||
for globe in [false, true] {
|
||
layouts.append(KeyboardLayout(width: width, layer: layer, field: field, showsGlobe: globe))
|
||
}
|
||
}
|
||
}
|
||
// Only the letters differ between languages.
|
||
for language in KeyboardLanguage.allCases where language != .english {
|
||
for globe in [false, true] {
|
||
layouts.append(KeyboardLayout(width: width, showsGlobe: globe, language: language))
|
||
}
|
||
layouts.append(KeyboardLayout(width: width, field: .email, language: language))
|
||
}
|
||
}
|
||
return layouts
|
||
}
|
||
|
||
func testPortraitIs262PointsAndLandscapeIs176Points() {
|
||
for width in portraitWidths {
|
||
XCTAssertEqual(KeyboardLayout(width: width).totalHeight, 262, "width \(width)")
|
||
}
|
||
XCTAssertEqual(KeyboardLayout.height(for: .portrait), 262)
|
||
XCTAssertEqual(KeyboardLayout.height(for: .landscape), 176)
|
||
}
|
||
|
||
func testPortraitMetricsMatchAppleIOS26() {
|
||
let proMax = KeyboardLayout(width: 440)
|
||
XCTAssertEqual(proMax.letterWidth, (440 - 13 - 54) / 10, accuracy: 0.001)
|
||
let q = proMax.key(.character("q"))!
|
||
XCTAssertEqual(q.frame.minX, 6.5, accuracy: 0.001)
|
||
XCTAssertEqual(q.frame.minY, 44, accuracy: 0.001)
|
||
XCTAssertEqual(q.frame.height, 45)
|
||
let shift = proMax.key(.shift)!
|
||
XCTAssertEqual(shift.frame.width, proMax.letterWidth * 1.34, accuracy: 0.001)
|
||
}
|
||
|
||
func testHitTargetsTileTheKeyAreaWithNoDeadZonesOrOverlaps() {
|
||
var generator = SystemRandomNumberGenerator()
|
||
for layout in everyPortraitLayout() + landscapeLayouts() {
|
||
let area = layout.touchAreaFrame
|
||
// Row 1 also takes the strip's bottom band, under its pills, as Apple's keys reach
|
||
// above their keycaps; the Hex key in the strip stops where it starts.
|
||
XCTAssertEqual(area.minY, layout.keysAreaFrame.minY - KeyboardLayout.stripTouchBand, describe(layout))
|
||
if let hex = layout.key(.hex), hex.row < 0 {
|
||
XCTAssertLessThanOrEqual(hex.hitFrame.maxY, area.minY, describe(layout))
|
||
}
|
||
for key in layout.keys where key.row == 0 {
|
||
let justAbove = CGPoint(x: key.frame.midX, y: key.frame.minY - 2)
|
||
XCTAssertEqual(layout.hitTest(justAbove)?.kind, key.kind, "\(describe(layout)) \(key.label)")
|
||
}
|
||
var rowTop = area.minY
|
||
for row in 0..<4 {
|
||
let targets = layout.keys.filter { $0.row == row }.map(\.hitFrame).sorted { $0.minX < $1.minX }
|
||
XCTAssertEqual(targets.first?.minX, 0, describe(layout))
|
||
XCTAssertEqual(targets.last?.maxX, layout.width, describe(layout))
|
||
for target in targets {
|
||
XCTAssertEqual(target.minY, rowTop, accuracy: 0.0001, "\(describe(layout)) row \(row)")
|
||
}
|
||
for (a, b) in zip(targets, targets.dropFirst()) {
|
||
XCTAssertEqual(a.maxX, b.minX, accuracy: 0.0001, "\(describe(layout)) row \(row)")
|
||
}
|
||
rowTop = targets[0].maxY
|
||
}
|
||
XCTAssertEqual(rowTop, area.maxY, accuracy: 0.0001, describe(layout))
|
||
|
||
for _ in 0..<400 {
|
||
let point = CGPoint(
|
||
x: CGFloat.random(in: 0..<layout.width, using: &generator),
|
||
y: CGFloat.random(in: area.minY..<area.maxY, using: &generator)
|
||
)
|
||
let hits = layout.keys.filter { $0.hitFrame.contains(point) }
|
||
XCTAssertEqual(hits.count, 1, "\(describe(layout)) at \(point): \(hits.map(\.label))")
|
||
}
|
||
XCTAssertNil(layout.hitTest(CGPoint(x: 20, y: layout.stripFrame.midY)), "strip is not a key")
|
||
}
|
||
}
|
||
|
||
func testKeycapsSitInsideTheirTargetsAndNeverOverlap() {
|
||
for layout in everyPortraitLayout() + landscapeLayouts() {
|
||
let bounds = CGRect(x: 0, y: 0, width: layout.width, height: layout.totalHeight)
|
||
for key in layout.keys {
|
||
XCTAssertTrue(key.hitFrame.contains(key.frame), "\(describe(layout)) \(key.label)")
|
||
XCTAssertTrue(bounds.contains(key.frame), "\(describe(layout)) \(key.label)")
|
||
XCTAssertGreaterThan(key.frame.width, 20, "\(describe(layout)) \(key.label)")
|
||
}
|
||
for (i, a) in layout.keys.enumerated() {
|
||
for b in layout.keys[(i + 1)...] {
|
||
XCTAssertFalse(a.frame.intersects(b.frame), "\(describe(layout)) \(a.label) × \(b.label)")
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
func testBalloonsStayInsideThe262PointKeyboard() {
|
||
for layout in everyPortraitLayout() {
|
||
let bounds = CGRect(x: 0, y: 0, width: layout.width, height: layout.totalHeight)
|
||
for key in layout.keys where key.character != nil {
|
||
guard let balloon = layout.balloon(for: key) else {
|
||
return XCTFail("\(describe(layout)) \(key.label) has no balloon")
|
||
}
|
||
XCTAssertTrue(
|
||
bounds.insetBy(dx: -0.001, dy: -0.001).contains(balloon.bounds),
|
||
"\(describe(layout)) \(key.label): \(balloon.bubble)"
|
||
)
|
||
XCTAssertGreaterThanOrEqual(balloon.bubble.width, key.frame.width)
|
||
}
|
||
}
|
||
}
|
||
|
||
func testAccentMenusClampInwardAcrossEveryWidth() {
|
||
for width in portraitWidths {
|
||
let layout = KeyboardLayout(width: width)
|
||
let bounds = CGRect(x: 0, y: 0, width: layout.width, height: layout.totalHeight)
|
||
for ch in ["e", "a", "i", "o", "u", "n", "c", "s", "z"] {
|
||
let key = layout.key(.character(ch))!
|
||
let options = AccentCatalog.options(for: ch, uppercase: false)!
|
||
let menu = AccentMenu.build(for: key.frame, rawOptions: options, in: layout.width)
|
||
XCTAssertTrue(
|
||
bounds.insetBy(dx: -0.01, dy: -0.01).contains(menu.bubbleFrame),
|
||
"width \(width) key \(ch): \(menu.bubbleFrame)"
|
||
)
|
||
XCTAssertEqual(menu.selectedOption, ch)
|
||
}
|
||
}
|
||
}
|
||
|
||
func testEdgeBalloonsArePushedInward() {
|
||
let layout = KeyboardLayout(width: 375)
|
||
let q = layout.balloon(for: layout.key(.character("q"))!)!
|
||
let p = layout.balloon(for: layout.key(.character("p"))!)!
|
||
let g = layout.balloon(for: layout.key(.character("g"))!)!
|
||
XCTAssertGreaterThan(q.offsetX, 0)
|
||
XCTAssertLessThan(p.offsetX, 0)
|
||
XCTAssertEqual(g.offsetX, 0, accuracy: 0.001)
|
||
XCTAssertEqual(q.bubble.minY, 0, accuracy: 0.001)
|
||
}
|
||
|
||
func testNoBalloonsForModifiersOrInLandscape() {
|
||
let portrait = KeyboardLayout(width: 393)
|
||
XCTAssertNil(portrait.balloon(for: portrait.key(.space)!))
|
||
XCTAssertNil(portrait.balloon(for: portrait.key(.shift)!))
|
||
let landscape = KeyboardLayout(width: 852, orientation: .landscape)
|
||
XCTAssertNil(landscape.balloon(for: landscape.key(.character("a"))!))
|
||
}
|
||
|
||
func testBottomRowMatchesIOS26EmailAndURLRightOfSpace() {
|
||
// The Hex key sits at the strip's trailing edge, so the bottom row is Apple's.
|
||
XCTAssertEqual(bottomRow(KeyboardLayout(width: 393)), [.layerSwitch(.numbers), .space, .returnKey])
|
||
XCTAssertEqual(
|
||
bottomRow(KeyboardLayout(width: 375, showsGlobe: true)),
|
||
[.layerSwitch(.numbers), .globe, .space, .returnKey]
|
||
)
|
||
XCTAssertEqual(
|
||
bottomRow(KeyboardLayout(width: 393, layer: .numbers, field: .email)),
|
||
[.layerSwitch(.letters), .space, .character("@"), .character("."), .returnKey]
|
||
)
|
||
XCTAssertEqual(
|
||
bottomRow(KeyboardLayout(width: 393, field: .url)),
|
||
[.layerSwitch(.numbers), .space, .character("."), .character("/"), .returnKey]
|
||
)
|
||
}
|
||
|
||
func testHexKeySitsAtTheStripsTrailingEdge() {
|
||
let layout = KeyboardLayout(width: 402)
|
||
let hex = layout.key(.hex)!
|
||
XCTAssertEqual(hex.row, -1)
|
||
XCTAssertEqual(hex.frame.maxX, 402 - layout.metrics.side, accuracy: 0.001)
|
||
XCTAssertEqual(hex.frame.midY, layout.metrics.stripHeight / 2, accuracy: 0.001)
|
||
XCTAssertLessThanOrEqual(layout.stripFrame.maxX, hex.frame.minX)
|
||
}
|
||
|
||
func testRowThreeModifiersFollowTheLayer() {
|
||
XCTAssertNotNil(KeyboardLayout(width: 393).key(.shift))
|
||
XCTAssertEqual(KeyboardLayout(width: 393, layer: .numbers).key(.layerSwitch(.symbols))?.row, 2)
|
||
XCTAssertEqual(KeyboardLayout(width: 393, layer: .symbols).key(.layerSwitch(.numbers))?.row, 2)
|
||
let numbers = KeyboardLayout(width: 393, layer: .numbers)
|
||
XCTAssertEqual(numbers.keys.filter { $0.row == 2 }.compactMap(\.character), [".", ",", "?", "!", "'"])
|
||
}
|
||
|
||
func testRowTwoIsCentred() {
|
||
let layout = KeyboardLayout(width: 402)
|
||
let a = layout.key(.character("a"))!.frame
|
||
let l = layout.key(.character("l"))!.frame
|
||
XCTAssertEqual(a.minX, layout.width - l.maxX, accuracy: 0.001)
|
||
XCTAssertEqual(layout.key(.character("a"))!.hitFrame.minX, 0)
|
||
XCTAssertEqual(layout.key(.character("l"))!.hitFrame.maxX, layout.width)
|
||
}
|
||
|
||
func testLandscapeKeepsInsetsAndHeight() {
|
||
let layout = KeyboardLayout(width: 932, orientation: .landscape, horizontalInset: 60)
|
||
XCTAssertEqual(layout.totalHeight, 176)
|
||
XCTAssertEqual(layout.key(.character("q"))!.frame.minX, 64, accuracy: 0.001)
|
||
XCTAssertEqual(layout.key(.character("q"))!.hitFrame.minX, 0)
|
||
}
|
||
|
||
// MARK: - Languages
|
||
|
||
func testLetterRowsMatchApplesLayouts() {
|
||
let rows: [KeyboardLanguage: [String]] = [
|
||
.english: ["qwertyuiop", "asdfghjkl", "zxcvbnm"],
|
||
.norwegian: ["qwertyuiopå", "asdfghjkløæ", "zxcvbnm"],
|
||
.spanish: ["qwertyuiop", "asdfghjklñ", "zxcvbnm"],
|
||
.german: ["qwertzuiopü", "asdfghjklöä", "yxcvbnm"],
|
||
.italian: ["qwertyuiop", "asdfghjkl", "zxcvbnm"],
|
||
.french: ["qwertyuiop", "asdfghjkl", "zxcvbnm"],
|
||
.portuguese: ["qwertyuiop", "asdfghjkl", "zxcvbnm"],
|
||
.russian: ["йцукенгшщзх", "фывапролджэ", "ячсмитьбю"],
|
||
.serbianLatin: ["qwertzuiop", "asdfghjkl", "yxcvbnm"],
|
||
]
|
||
for (language, expected) in rows {
|
||
let layout = KeyboardLayout(width: 402, language: language)
|
||
for row in 0..<3 {
|
||
XCTAssertEqual(letters(layout, row: row).joined(), expected[row], "\(language) row \(row + 1)")
|
||
}
|
||
}
|
||
}
|
||
|
||
func testElevenKeyRowsFitEveryIPhoneWidthAndLandscape() {
|
||
let cases: [(CGFloat, KeyboardOrientation, CGFloat)] = [
|
||
(375, .portrait, 0), (402, .portrait, 0), (440, .portrait, 0),
|
||
(667, .landscape, 0), (874, .landscape, 0), (956, .landscape, 60),
|
||
]
|
||
for language in [KeyboardLanguage.norwegian, .german] {
|
||
for (width, orientation, inset) in cases {
|
||
let layout = KeyboardLayout(width: width, orientation: orientation, horizontalInset: inset, language: language)
|
||
let english = KeyboardLayout(width: width, orientation: orientation, horizontalInset: inset)
|
||
let m = layout.metrics
|
||
let label = "\(language) \(width)pt \(orientation)"
|
||
let inner = width - 2 * inset
|
||
XCTAssertEqual(layout.characterWidth, (inner - 2 * m.side - 10 * m.gap) / 11, accuracy: 0.001, label)
|
||
XCTAssertGreaterThan(layout.characterWidth, 26, label)
|
||
for row in 0..<2 {
|
||
let keys = layout.keys.filter { $0.row == row }.sorted { $0.frame.minX < $1.frame.minX }
|
||
XCTAssertEqual(keys.count, 11, label)
|
||
// Rows 1 and 2 run edge to edge, like English row 1.
|
||
XCTAssertEqual(keys.first!.frame.minX, inset + m.side, accuracy: 0.001, label)
|
||
XCTAssertEqual(keys.last!.frame.maxX, width - inset - m.side, accuracy: 0.001, label)
|
||
}
|
||
// Row 3's letters are as wide as the others and centred between shift and delete,
|
||
// which keep their English size, as does the whole bottom row.
|
||
let row3 = layout.keys.filter { $0.row == 2 && $0.character != nil }.sorted { $0.frame.minX < $1.frame.minX }
|
||
XCTAssertEqual(row3.first!.frame.width, layout.characterWidth, accuracy: 0.001, label)
|
||
XCTAssertEqual(row3.first!.frame.minX - inset, width - inset - row3.last!.frame.maxX, accuracy: 0.001, label)
|
||
XCTAssertEqual(layout.key(.shift)!.frame, english.key(.shift)!.frame, label)
|
||
XCTAssertEqual(layout.key(.delete)!.frame, english.key(.delete)!.frame, label)
|
||
XCTAssertLessThan(layout.key(.shift)!.frame.maxX, row3.first!.frame.minX, label)
|
||
XCTAssertEqual(
|
||
layout.keys.filter { $0.row == 3 }.map(\.frame),
|
||
english.keys.filter { $0.row == 3 }.map(\.frame),
|
||
label
|
||
)
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Russian's nine-letter third row keeps its letters as wide as rows 1 and 2; shift and
|
||
/// delete narrow to fit beside them, as on Apple's Russian keyboard.
|
||
func testRussiansNineLetterRowFitsBetweenNarrowerShiftAndDelete() {
|
||
let cases: [(CGFloat, KeyboardOrientation, CGFloat)] = [
|
||
(320, .portrait, 0), (375, .portrait, 0), (402, .portrait, 0), (440, .portrait, 0),
|
||
(667, .landscape, 0), (956, .landscape, 60),
|
||
]
|
||
for (width, orientation, inset) in cases {
|
||
let layout = KeyboardLayout(width: width, orientation: orientation, horizontalInset: inset, language: .russian)
|
||
let english = KeyboardLayout(width: width, orientation: orientation, horizontalInset: inset)
|
||
let label = "\(width)pt \(orientation)"
|
||
let row3 = layout.keys.filter { $0.row == 2 && $0.character != nil }.sorted { $0.frame.minX < $1.frame.minX }
|
||
XCTAssertEqual(row3.count, 9, label)
|
||
XCTAssertEqual(row3.first!.frame.width, layout.characterWidth, accuracy: 0.001, label)
|
||
let shift = layout.key(.shift)!.frame
|
||
let delete = layout.key(.delete)!.frame
|
||
XCTAssertEqual(row3.first!.frame.minX - shift.maxX, layout.metrics.gap, accuracy: 0.001, label)
|
||
XCTAssertEqual(delete.minX - row3.last!.frame.maxX, layout.metrics.gap, accuracy: 0.001, label)
|
||
XCTAssertEqual(shift.width, delete.width, accuracy: 0.001, label)
|
||
XCTAssertLessThan(shift.width, english.key(.shift)!.frame.width, label)
|
||
XCTAssertGreaterThanOrEqual(shift.width, layout.characterWidth - 0.001, label)
|
||
XCTAssertEqual(
|
||
layout.keys.filter { $0.row == 3 }.map(\.frame),
|
||
english.keys.filter { $0.row == 3 }.map(\.frame),
|
||
label
|
||
)
|
||
}
|
||
// The numbers and symbols pages are English's.
|
||
for layer in [KeyboardLayer.numbers, .symbols] {
|
||
XCTAssertEqual(
|
||
KeyboardLayout(width: 402, layer: layer, language: .russian).keys,
|
||
KeyboardLayout(width: 402, layer: layer).keys
|
||
)
|
||
}
|
||
}
|
||
|
||
func testSpanishKeepsTenKeysWithNOnRowTwo() {
|
||
for width: CGFloat in [375, 402, 440] {
|
||
let layout = KeyboardLayout(width: width, language: .spanish)
|
||
XCTAssertEqual(layout.characterWidth, layout.letterWidth, accuracy: 0.001)
|
||
let n = layout.key(.character("ñ"))!
|
||
XCTAssertEqual(n.frame.maxX, width - layout.metrics.side, accuracy: 0.001)
|
||
XCTAssertEqual(n.hitFrame.maxX, width)
|
||
XCTAssertEqual(layout.key(.character("a"))!.frame.minX, layout.metrics.side, accuracy: 0.001)
|
||
}
|
||
}
|
||
|
||
func testAccentsMatchEachLanguage() {
|
||
XCTAssertEqual(AccentCatalog.options(for: "ø", uppercase: false, language: .norwegian), ["ø", "ö"])
|
||
XCTAssertEqual(AccentCatalog.options(for: "æ", uppercase: true, language: .norwegian), ["Æ", "Ä"])
|
||
XCTAssertNil(AccentCatalog.options(for: "å", uppercase: false, language: .norwegian))
|
||
let norwegianA = AccentCatalog.options(for: "a", uppercase: false, language: .norwegian)!
|
||
XCTAssertFalse(norwegianA.contains("å") || norwegianA.contains("æ"), "they have their own keys")
|
||
XCTAssertTrue(AccentCatalog.options(for: "s", uppercase: false, language: .german)!.contains("ß"))
|
||
XCTAssertFalse(AccentCatalog.options(for: "u", uppercase: false, language: .german)!.contains("ü"))
|
||
XCTAssertEqual(AccentCatalog.options(for: "e", uppercase: false, language: .spanish)?.dropFirst().first, "é")
|
||
XCTAssertEqual(AccentCatalog.options(for: "n", uppercase: true, language: .spanish), ["N", "Ñ", "Ń"])
|
||
XCTAssertEqual(
|
||
AccentCatalog.options(for: "a", uppercase: false),
|
||
AccentCatalog.options(for: "a", uppercase: false, language: .english),
|
||
"English is unchanged"
|
||
)
|
||
// Italian's accents come first on each vowel: à è ì ò ù, and é right after è.
|
||
let italian = ["a", "e", "i", "o", "u"].map {
|
||
AccentCatalog.options(for: $0, uppercase: false, language: .italian)!.dropFirst().first
|
||
}
|
||
XCTAssertEqual(italian, ["à", "è", "ì", "ò", "ù"])
|
||
XCTAssertEqual(AccentCatalog.options(for: "e", uppercase: false, language: .italian)?.dropFirst(2).first, "é")
|
||
XCTAssertEqual(AccentCatalog.options(for: "o", uppercase: true, language: .italian)?.prefix(2), ["O", "Ò"])
|
||
XCTAssertEqual(AccentCatalog.options(for: "e", uppercase: false, language: .french)?.dropFirst().first, "é")
|
||
XCTAssertEqual(AccentCatalog.options(for: "c", uppercase: false, language: .french)?.dropFirst().first, "ç")
|
||
XCTAssertEqual(AccentCatalog.options(for: "a", uppercase: false, language: .portuguese)?.prefix(3), ["a", "á", "ã"])
|
||
// Russian: ё on е and ъ on ь, as on Apple's; Serbian (Latin): č ć on c, đ on d, š on s, ž on z.
|
||
XCTAssertEqual(AccentCatalog.options(for: "е", uppercase: false, language: .russian)?.prefix(2), ["е", "ё"])
|
||
XCTAssertEqual(AccentCatalog.options(for: "Е", uppercase: true, language: .russian)?.prefix(2), ["Е", "Ё"])
|
||
XCTAssertEqual(AccentCatalog.options(for: "ь", uppercase: false, language: .russian)?.prefix(2), ["ь", "ъ"])
|
||
XCTAssertEqual(AccentCatalog.options(for: "ь", uppercase: true, language: .russian)?.prefix(2), ["Ь", "Ъ"])
|
||
XCTAssertNil(AccentCatalog.options(for: "б", uppercase: false, language: .russian))
|
||
XCTAssertEqual(AccentCatalog.options(for: "c", uppercase: false, language: .serbianLatin)?.prefix(3), ["c", "č", "ć"])
|
||
XCTAssertEqual(AccentCatalog.options(for: "d", uppercase: true, language: .serbianLatin), ["D", "Đ"])
|
||
XCTAssertEqual(AccentCatalog.options(for: "s", uppercase: false, language: .serbianLatin)?.prefix(2), ["s", "š"])
|
||
XCTAssertEqual(AccentCatalog.options(for: "z", uppercase: false, language: .serbianLatin)?.prefix(2), ["z", "ž"])
|
||
}
|
||
|
||
func testAccentMenusOnTheNewKeysStayInside() {
|
||
let keys: [KeyboardLanguage: [String]] = [
|
||
.norwegian: ["å", "ø", "æ", "a", "o"],
|
||
.spanish: ["ñ", "a", "o", "n"],
|
||
.german: ["ü", "ö", "ä", "s", "a", "u"],
|
||
.russian: ["й", "е", "х", "ж", "э", "ь", "ю", "я"],
|
||
.serbianLatin: ["c", "d", "s", "z"],
|
||
]
|
||
for width in portraitWidths {
|
||
for (language, characters) in keys {
|
||
let layout = KeyboardLayout(width: width, language: language)
|
||
let bounds = CGRect(x: 0, y: 0, width: layout.width, height: layout.totalHeight)
|
||
for ch in characters {
|
||
let key = layout.key(.character(ch))!
|
||
guard let options = AccentCatalog.options(for: ch, uppercase: false, language: language) else { continue }
|
||
let menu = AccentMenu.build(for: key.frame, rawOptions: options, in: layout.width)
|
||
XCTAssertTrue(
|
||
bounds.insetBy(dx: -0.01, dy: -0.01).contains(menu.bubbleFrame),
|
||
"\(language) \(width) \(ch): \(menu.bubbleFrame)"
|
||
)
|
||
XCTAssertEqual(menu.selectedOption, ch)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
func testTheLayoutFollowsTheDictationLanguage() {
|
||
func automatic(_ dictation: String?, modes: [String] = [], preferred: [String] = []) -> KeyboardLanguage {
|
||
KeyboardLanguage.automatic(dictationLanguage: dictation, inputModes: modes, preferredLanguages: preferred)
|
||
}
|
||
XCTAssertEqual(automatic("nb-NO"), .norwegian)
|
||
XCTAssertEqual(automatic("es-MX"), .spanish)
|
||
XCTAssertEqual(automatic("de-AT"), .german)
|
||
XCTAssertEqual(automatic("en-GB", modes: ["nb-NO"]), .english, "the dictation language wins")
|
||
XCTAssertEqual(automatic("he-IL", preferred: ["he-IL", "de-DE"]), .english, "Hebrew has no layout yet")
|
||
// Italian, French and Portuguese type in their own language, never English.
|
||
XCTAssertEqual(automatic("it-IT", modes: ["it-IT", "en-US"]), .italian)
|
||
XCTAssertEqual(automatic("it-CH"), .italian)
|
||
XCTAssertEqual(automatic("fr-CA"), .french)
|
||
XCTAssertEqual(automatic("pt-BR"), .portuguese)
|
||
XCTAssertEqual(automatic("en-US", modes: ["it-IT"]), .english, "the dictation language still wins")
|
||
// Russian, and Serbian in Latin script (Hex has no Cyrillic Serbian layout).
|
||
XCTAssertEqual(automatic("ru-RU", modes: ["en-US"]), .russian)
|
||
XCTAssertEqual(automatic("sr-Latn-RS"), .serbianLatin)
|
||
XCTAssertEqual(automatic("sr_Latn"), .serbianLatin)
|
||
XCTAssertEqual(automatic("sr-RS"), .english, "Cyrillic Serbian has no layout")
|
||
XCTAssertEqual(automatic("en-US", modes: ["ru-RU", "sr-Latn"]), .english, "the dictation language still wins")
|
||
// Before Hex knows the dictation language: the first keyboard, then language, with a layout.
|
||
XCTAssertEqual(automatic(nil, modes: ["emoji", "nb-NO", "en-US"]), .norwegian)
|
||
XCTAssertEqual(automatic(nil, preferred: ["nn-NO"]), .norwegian)
|
||
XCTAssertEqual(automatic(nil, modes: ["emoji", "it-IT"], preferred: ["en-US"]), .italian)
|
||
XCTAssertEqual(automatic(nil, preferred: ["it-IT", "en-US"]), .italian)
|
||
XCTAssertEqual(automatic(nil, preferred: ["fr-FR"]), .french)
|
||
XCTAssertEqual(automatic(nil, preferred: ["ru-RU"]), .russian)
|
||
XCTAssertEqual(automatic(nil, modes: ["emoji", "sr-Latn"], preferred: ["en-US"]), .serbianLatin)
|
||
XCTAssertEqual(automatic(nil, modes: ["sr"], preferred: ["en-US"]), .english, "Apple's Cyrillic Serbian keyboard")
|
||
XCTAssertEqual(automatic(nil, preferred: ["el-GR"]), .english)
|
||
}
|
||
|
||
func testSpellCheckUsesTheLayoutsLanguageOrNone() {
|
||
XCTAssertEqual(KeyboardLanguage.norwegian.spellCheckLanguage(available: ["en_US", "nb"]), "nb")
|
||
XCTAssertNil(KeyboardLanguage.norwegian.spellCheckLanguage(available: ["en_US", "da"]))
|
||
XCTAssertEqual(KeyboardLanguage.german.spellCheckLanguage(available: ["de"]), "de")
|
||
XCTAssertEqual(KeyboardLanguage.italian.spellCheckLanguage(available: ["en_US", "it_IT"]), "it_IT")
|
||
XCTAssertEqual(KeyboardLanguage.french.spellCheckLanguage(available: ["en_US", "fr_FR"]), "fr_FR")
|
||
XCTAssertEqual(KeyboardLanguage.portuguese.spellCheckLanguage(available: ["pt_PT", "pt_BR"]), "pt_BR")
|
||
XCTAssertNil(KeyboardLanguage.italian.spellCheckLanguage(available: ["en_US", "es"]), "never English")
|
||
XCTAssertEqual(KeyboardLanguage.russian.spellCheckLanguage(available: ["en_US", "ru_RU"]), "ru_RU")
|
||
XCTAssertNil(KeyboardLanguage.russian.spellCheckLanguage(available: ["en_US", "uk_UA"]), "never English")
|
||
XCTAssertEqual(KeyboardLanguage.serbianLatin.spellCheckLanguage(available: ["en_US", "sr_Latn"]), "sr_Latn")
|
||
XCTAssertNil(KeyboardLanguage.serbianLatin.spellCheckLanguage(available: ["en_US", "sr", "hr_HR"]), "no Cyrillic or Croatian")
|
||
}
|
||
|
||
// MARK: - Helpers
|
||
|
||
private func landscapeLayouts() -> [KeyboardLayout] {
|
||
[
|
||
KeyboardLayout(width: 667, orientation: .landscape),
|
||
KeyboardLayout(width: 852, orientation: .landscape, layer: .numbers),
|
||
KeyboardLayout(width: 956, orientation: .landscape, field: .email, horizontalInset: 60),
|
||
] + KeyboardLanguage.allCases.flatMap { language in
|
||
[
|
||
KeyboardLayout(width: 667, orientation: .landscape, language: language),
|
||
KeyboardLayout(width: 956, orientation: .landscape, showsGlobe: true, horizontalInset: 60, language: language),
|
||
]
|
||
}
|
||
}
|
||
|
||
private func letters(_ layout: KeyboardLayout, row: Int) -> [String] {
|
||
layout.keys.filter { $0.row == row && $0.character != nil }
|
||
.sorted { $0.frame.minX < $1.frame.minX }
|
||
.compactMap(\.character)
|
||
}
|
||
|
||
private func bottomRow(_ layout: KeyboardLayout) -> [KeyKind] {
|
||
layout.keys.filter { $0.row == 3 }.sorted { $0.frame.minX < $1.frame.minX }.map(\.kind)
|
||
}
|
||
|
||
private func describe(_ layout: KeyboardLayout) -> String {
|
||
"\(layout.width)pt \(layout.orientation) \(layout.layer) \(layout.field) globe=\(layout.showsGlobe) \(layout.language)"
|
||
}
|
||
}
|
||
|
||
@MainActor
|
||
final class FullKeyboardSettingsTests: XCTestCase {
|
||
func testFullKeyboardAndAutocorrectAreOnByDefault() throws {
|
||
let suite = "FullKeyboardSettingsTests-\(UUID().uuidString)"
|
||
let defaults = try XCTUnwrap(UserDefaults(suiteName: suite))
|
||
defer { defaults.removePersistentDomain(forName: suite) }
|
||
let settings = FullKeyboardSettings(defaults: defaults)
|
||
|
||
XCTAssertTrue(settings.isEnabled)
|
||
XCTAssertTrue(settings.autocorrect)
|
||
|
||
settings.applyLaunchArguments(["HEX"])
|
||
XCTAssertTrue(settings.isEnabled)
|
||
settings.applyLaunchArguments(["HEX", FullKeyboardSettings.keyboardLaunchArgument, "dictation-bar"])
|
||
XCTAssertFalse(settings.isEnabled)
|
||
settings.applyLaunchArguments(["HEX", FullKeyboardSettings.launchArgument])
|
||
XCTAssertTrue(settings.isEnabled)
|
||
|
||
settings.autocorrect = false
|
||
XCTAssertFalse(FullKeyboardSettings(defaults: defaults).autocorrect)
|
||
}
|
||
|
||
/// Testers who turned the full keyboard experiment off chose the dictation bar and keep
|
||
/// it; those who never touched it (unset) get the full keyboard.
|
||
func testExplicitlyOffKeepsTheDictationBar() throws {
|
||
let suite = "FullKeyboardSettingsTests-\(UUID().uuidString)"
|
||
let defaults = try XCTUnwrap(UserDefaults(suiteName: suite))
|
||
defer { defaults.removePersistentDomain(forName: suite) }
|
||
|
||
defaults.set(false, forKey: "experiments.fullKeyboard")
|
||
XCTAssertFalse(FullKeyboardSettings(defaults: defaults).isEnabled)
|
||
defaults.set(true, forKey: "experiments.fullKeyboard")
|
||
XCTAssertTrue(FullKeyboardSettings(defaults: defaults).isEnabled)
|
||
|
||
FullKeyboardSettings(defaults: defaults).isEnabled = false
|
||
XCTAssertEqual(defaults.object(forKey: "experiments.fullKeyboard") as? Bool, false)
|
||
}
|
||
|
||
/// Without Full Access the keyboard reads no App Group: the full keyboard, which types.
|
||
func testNoAppGroupReadsAsFullKeyboard() {
|
||
XCTAssertTrue(FullKeyboardSettings(defaults: nil).isEnabled)
|
||
}
|
||
}
|