mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
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.
222 lines
8.5 KiB
Swift
222 lines
8.5 KiB
Swift
import UIKit
|
||
import XCTest
|
||
@testable import HEX
|
||
|
||
@MainActor
|
||
final class AutocorrectEngineTests: XCTestCase {
|
||
private var suiteName: String!
|
||
private var defaults: UserDefaults!
|
||
private var target: FixtureTextTarget!
|
||
private var engine: FullKeyboardEngine!
|
||
private var now: TimeInterval = 200
|
||
|
||
override func setUp() async throws {
|
||
suiteName = "AutocorrectEngineTests-\(UUID().uuidString)"
|
||
defaults = UserDefaults(suiteName: suiteName)!
|
||
target = FixtureTextTarget()
|
||
target.autocapitalizationType = UITextAutocapitalizationType.none
|
||
let store = LearnedWordsStore(defaults: defaults)
|
||
let autocorrect = AutocorrectEngine(checker: SystemTextChecker(), learnedWords: store)
|
||
engine = FullKeyboardEngine(target: target, autocorrect: autocorrect)
|
||
engine.width = 393
|
||
engine.clock = { [unowned self] in self.now }
|
||
engine.configureForField()
|
||
}
|
||
|
||
override func tearDown() async throws {
|
||
defaults.removePersistentDomain(forName: suiteName)
|
||
}
|
||
|
||
private func tap(_ kind: KeyKind) {
|
||
let key = engine.layout.key(kind)!
|
||
let center = CGPoint(x: key.frame.midX, y: key.frame.midY)
|
||
engine.touchBegan(id: 1, at: center)
|
||
engine.touchEnded(id: 1, at: center)
|
||
now += 0.45
|
||
}
|
||
|
||
private func typeWord(_ word: String) {
|
||
for ch in word {
|
||
if ch.isUppercase {
|
||
tap(.shift)
|
||
tap(.character(ch.lowercased()))
|
||
} else if ch == " " {
|
||
tap(.space)
|
||
} else {
|
||
tap(.character(String(ch)))
|
||
}
|
||
}
|
||
}
|
||
|
||
func testFiftyWordAndContextCases() {
|
||
engine.autocorrect.loadLexiconEntries([
|
||
("omw", "On my way!"),
|
||
("addr", "123 Market St"),
|
||
("Kit", "Kit"),
|
||
])
|
||
|
||
// 1–25: Contractions, transpositions, shortcuts, and casing that MUST auto-replace on space.
|
||
let replacements: [(String, String)] = [
|
||
("i", "I"),
|
||
("im", "I'm"),
|
||
("ive", "I've"),
|
||
("dont", "don't"),
|
||
("Dont", "Don't"),
|
||
("DONT", "DON'T"),
|
||
("cant", "can't"),
|
||
("wont", "won't"),
|
||
("didnt", "didn't"),
|
||
("doesnt", "doesn't"),
|
||
("isnt", "isn't"),
|
||
("youre", "you're"),
|
||
("theyre", "they're"),
|
||
("thats", "that's"),
|
||
("oclock", "o'clock"),
|
||
("teh", "the"),
|
||
("Teh", "The"),
|
||
("TEH", "THE"),
|
||
("taht", "that"),
|
||
("adn", "and"),
|
||
("wiht", "with"),
|
||
("recieve", "receive"),
|
||
("keybaord", "keyboard"),
|
||
("iphnoe", "iPhone"),
|
||
("omw", "On my way!"),
|
||
]
|
||
for (typed, expected) in replacements {
|
||
let actual = engine.autocorrect.replacementOnSpace(
|
||
beforeInput: typed,
|
||
field: .text,
|
||
autocorrectionType: .default
|
||
)
|
||
XCTAssertEqual(actual?.replacement, expected, "expected \(typed) -> \(expected)")
|
||
}
|
||
|
||
// 26–52: Valid English words, ambiguous words, acronyms, PascalCase, shorthand, and non-prose contexts that MUST stay untouched.
|
||
let untouched: [(String, KeyboardFieldKind, UITextAutocorrectionType)] = [
|
||
("the", .text, .default),
|
||
("from", .text, .default),
|
||
("form", .text, .default),
|
||
("their", .text, .default),
|
||
("there", .text, .default),
|
||
("were", .text, .default),
|
||
("well", .text, .default),
|
||
("ill", .text, .default),
|
||
("its", .text, .default),
|
||
("lets", .text, .default),
|
||
("running", .text, .default),
|
||
("late", .text, .default),
|
||
("tonight", .text, .default),
|
||
("thx", .text, .default),
|
||
("pls", .text, .default),
|
||
("tmrw", .text, .default),
|
||
("tbh", .text, .default),
|
||
("idk", .text, .default),
|
||
("lmao", .text, .default),
|
||
("ios", .text, .default),
|
||
("hex", .text, .default),
|
||
("API", .text, .default),
|
||
("NYC", .text, .default),
|
||
("HexKey", .text, .default),
|
||
("@teh", .text, .default),
|
||
("https://anoma.ly/teh", .text, .default),
|
||
("teh", .email, .default),
|
||
("teh", .url, .default),
|
||
("teh", .text, .no),
|
||
]
|
||
for (context, field, mode) in untouched {
|
||
let actual = engine.autocorrect.replacementOnSpace(
|
||
beforeInput: context,
|
||
field: field,
|
||
autocorrectionType: mode
|
||
)
|
||
XCTAssertNil(actual, "expected \(context) (\(field)) to stay untouched, got \(String(describing: actual))")
|
||
}
|
||
}
|
||
|
||
func testAmbiguousContractionsSuggestWithoutAutoReplacing() {
|
||
let slots = engine.autocorrect.suggestions(
|
||
beforeInput: "well",
|
||
field: .text,
|
||
autocorrectionType: .default
|
||
)
|
||
XCTAssertEqual(slots.first?.displayText, "“well”")
|
||
XCTAssertEqual(slots.dropFirst().first?.replacement, "we'll")
|
||
XCTAssertEqual(slots.dropFirst().first?.willAutoReplace, false)
|
||
}
|
||
|
||
func testSpaceDeleteSpaceKeepLoopAndLearnedWordsPersistence() {
|
||
typeWord("teh")
|
||
XCTAssertEqual(engine.suggestions.count >= 2, true)
|
||
XCTAssertEqual(engine.suggestions[0].kind, .keepLiteral)
|
||
XCTAssertEqual(engine.suggestions[1].replacement, "the")
|
||
XCTAssertTrue(engine.suggestions[1].willAutoReplace)
|
||
|
||
// 1. Space applies cautious autocorrect (`teh` -> `the `).
|
||
tap(.space)
|
||
XCTAssertEqual(target.text, "the ")
|
||
|
||
// 2. Single Backspace reverts `the ` -> `teh` and suppresses `"teh"` for this session.
|
||
tap(.delete)
|
||
XCTAssertEqual(target.text, "teh")
|
||
|
||
// 3. Pressing Space again keeps `teh ` untouched!
|
||
tap(.space)
|
||
XCTAssertEqual(target.text, "teh ")
|
||
|
||
// 4. Keeping `"teh"` a second time via Slot 0 persists it in App Group `learnedWords`.
|
||
typeWord("teh")
|
||
engine.acceptSuggestion(engine.suggestions[0])
|
||
XCTAssertEqual(target.text, "teh teh ")
|
||
XCTAssertTrue(LearnedWordsStore(defaults: defaults).contains("teh"))
|
||
}
|
||
|
||
func testQwertyAdjacencyPrefersPhysicalNeighbor() {
|
||
let near = AutocorrectEngine.singleEditKeyboardCost(from: "tge", to: "the")!
|
||
let far = AutocorrectEngine.singleEditKeyboardCost(from: "tpe", to: "the")!
|
||
XCTAssertLessThan(near, far, "g is adjacent to h; p is across the keyboard")
|
||
XCTAssertNil(AutocorrectEngine.singleEditKeyboardCost(from: "abcdef", to: "abxyef"), "two substitutions are rejected")
|
||
}
|
||
|
||
func testALayoutWithoutADictionaryNeverCorrectsIntoEnglish() {
|
||
engine.autocorrect.loadLexiconEntries([("omw", "On my way!")])
|
||
engine.autocorrect.setLanguage(.norwegian, available: ["en_US", "en"])
|
||
XCTAssertFalse(engine.autocorrect.hasDictionary)
|
||
typeWord("teh")
|
||
XCTAssertTrue(engine.suggestions.isEmpty, "no English suggestions for Norwegian")
|
||
tap(.space)
|
||
XCTAssertEqual(target.text, "teh ")
|
||
typeWord("omw")
|
||
tap(.space)
|
||
XCTAssertEqual(target.text, "teh On my way! ", "your own text replacements still apply")
|
||
}
|
||
|
||
func testEnglishFixesStayOffInOtherLanguages() {
|
||
engine.autocorrect.setLanguage(.norwegian, available: ["nb", "en_US"])
|
||
XCTAssertTrue(engine.autocorrect.hasDictionary)
|
||
typeWord("i")
|
||
tap(.space)
|
||
XCTAssertEqual(target.text, "i ", "Norwegian \"i\" means \"in\"")
|
||
engine.autocorrect.setLanguage(.english, available: ["en_US"])
|
||
typeWord("i")
|
||
tap(.space)
|
||
XCTAssertEqual(target.text, "i I ")
|
||
}
|
||
|
||
func testAdjacencyFollowsTheLayout() {
|
||
// German swaps Y and Z: on QWERTZ, Z sits beside T.
|
||
XCTAssertTrue(AutocorrectEngine.areAdjacentKeys("z", "t", language: .german))
|
||
XCTAssertFalse(AutocorrectEngine.areAdjacentKeys("z", "t", language: .english))
|
||
XCTAssertTrue(AutocorrectEngine.areAdjacentKeys("ø", "l", language: .norwegian))
|
||
XCTAssertTrue(AutocorrectEngine.areAdjacentKeys("ñ", "l", language: .spanish))
|
||
}
|
||
|
||
func testTurningAutocorrectSettingOffKeepsSuggestionsWithoutAutoReplacing() {
|
||
engine.autocorrect.isEnabled = false
|
||
typeWord("dont")
|
||
XCTAssertFalse(engine.suggestions.isEmpty)
|
||
XCTAssertFalse(engine.suggestions[1].willAutoReplace)
|
||
tap(.space)
|
||
XCTAssertEqual(target.text, "dont ")
|
||
}
|
||
}
|