mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
* iOS keyboard: choose several languages and switch them on the space bar
A TestFlight tester asked for more than one keyboard language. Settings ›
Keyboard › Keyboard layout (one menu: Automatic or one layout) becomes
Languages, a page where you tick one or more languages and drag them into
order. Automatic stays, and stays the default: unset, it still follows the
dictation language. The iPhone's own keyboards' and preferred languages'
layouts are offered first ("On this iPhone"), then the rest. Ticking a
language from Automatic keeps Automatic's language beside it; unticking the
last goes back to Automatic.
With two or more languages the space bar names the current one, and a
quick swipe along it switches (left the next, right the one before),
changing the letters (QWERTY, AZERTY, ЙЦУКЕН), accents and autocorrect
language. Apple and Gboard switch with a long press on the globe, but
Face ID iPhones have no globe in a third-party keyboard and Hex's space-bar
slide is already its cursor trackpad, so this follows SwiftKey: with two or
more languages the cursor slides once the space bar was held
(trackpadHoldDelay, 0.4 s, the hold #2100 added, as Apple's), a slide
before that is a language swipe, and one shorter than 24 pt still types a
space. The hold timer firing during a slow swipe leaves it a swipe. With
one language nothing changes. The globe key is untouched
(handleInputModeList: next keyboard, list on hold).
The current language persists (settings.keyboardLanguages.current), so the
keyboard comes back in it. Optional Switch dictation too (off by default)
sets the dictation language to the keyboard's on each switch, in the
iPhone's region where Apple Speech has it (en-GB on a British iPhone).
Storage: settings.keyboardLanguages (array). It reads the old
settings.keyboardLayout until first written and writes its first language
there for an older build. "Keyboard layout" string removed; new strings
have zh-Hans entries.
Verified: xcodebuild test FullKeyboardEngineTests + KeyboardLayoutTests on
an iOS 27.2 simulator (54 tests, 0 failures), with new cases for the swipe
vs hold, switching in a FullKeyboardView, resolution, migration and the
dictation language pick.
* iOS keyboard: bilingual autocorrect for two Latin-script languages
With two or more keyboard languages, autocorrect and the strip also check
the first other chosen language with Latin letters and an iOS dictionary,
on the current keys, as Apple's "Français et English" keyboard does:
French with English beside it, English with French; Russian alone (Cyrillic)
and no partner for Serbian (no iOS dictionary) or between two French
layouts (one dictionary).
AutocorrectEngine gets a partner engine with its own UITextChecker language
and, where one is bundled (en, es), Spatial autocorrect's model reading the
same taps on the current layout. It shares your words (learned words, text
replacements, Learn how I type). Rules:
- a word either language knows as typed is never changed (only your text
replacements still apply), so French "dont" never becomes "don't" and
English "the" never becomes "thé";
- a typo only one language can fix gets that fix ("teh" -> "the" while
French types);
- when both can, the language of the last few words only one dictionary
knows wins, else the clearly cheaper slip on these keys, else the
keyboard's own language;
- the strip interleaves both languages' fixes and completions, the leaning
language first; spelling guesses only for a word neither knows;
- one-letter words stay with the keyboard's own language (UITextChecker
takes every letter as a word, so English "i" still becomes "I").
Slip costs now use the layout you type on (layoutLanguage), so the partner
prices AZERTY neighbours on the French keys.
Measured in the iOS 27.2 simulator (testABilingualKeyboardKeepsBothLanguagesWords,
iOS dictionaries plus the English Spatial model, each word in an empty field):
the English keyboard alone changed 34 of 77 common French words (demain ->
remain, dont -> don't, que -> we, cette -> centre...) and the French keyboard
alone 28 of 80 common English words (was -> sas, then -> the, people ->
peuple...); with English and French chosen, 0 of either, typing in either
layout, and neither language's own words changed.
Verified: xcodebuild test FullKeyboardEngineTests, AutocorrectEngineTests
and KeyboardLayoutTests (91 tests, 0 failures), with new cases for the
rules on stub dictionaries, the real-dictionary sentence "Bonjour merci the
meeting est dont teh tomorow demian" -> "... the tomorrow demain" on the
French keyboard, and the pairing rule.
* iOS settings: drag handles for newly ticked languages, French's region
Keyboard › Languages: a language ticked while the page was open showed no
drag handle (SwiftUI gives a row added in edit mode no reorder control), so
it couldn't be reordered until the page was opened again. The chosen
section's rows are now rebuilt whenever the list changes (.id of the
list). Français (France)'s line in the iPhone's language now reads its
region too ("French (France)", "法语(法国)"), like the other two French
layouts.
Verified in the iOS 27.2 simulator: unticking and re-ticking Français
shows its handle; dragging it above Русский reorders the list and the App
Group (settings.keyboardLanguages en, fr, ru); screenshots in English and
Chinese.
* iOS docs: keyboard languages, the space-bar swipe and bilingual autocorrect
README: Settings › Keyboard › Languages (Automatic or chosen languages in
order), the space-bar swipe and why it beats a long press on the globe here,
the hold-then-slide trackpad with two or more languages, the persisted
current language, Switch dictation too, bilingual autocorrect's rules and
measured break counts, the space bar naming the keyboard's language while
it switches, the tests that cover them, and the simulator memory figures
(the second language costs about 1 MB after a sentence: Français 28.7-28.8
alone vs 29.7-29.9 MB with English; English 28.9-29.9 vs 29.5-29.9 MB with
Français). typing-logs.md: the language_swipe and keyboard_language ui
lines.
Docs only; the figures come from the iOS 27.2 simulator runs on this branch.
* iOS docs: bilingual break count on the rebased keyboard
After rebasing onto #2100/#2101 (tap targets near the strip, learned-word
erasing), testABilingualKeyboardKeepsBothLanguagesWords reports the French
keyboard alone changing 27 of 80 common English words, not 28 (one word
fewer); the English keyboard alone still changes 34 of 77 French words and
English + French none of either. README updated to the current figure.
Verified: full xcodebuild test on the rebased branch, iOS 27.2 simulator,
327 tests, 0 failures, 1 skipped (AppleSpeechClipTests, no clips).
784 lines
39 KiB
Swift
784 lines
39 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 testDeleteWhatHexLearnedForgetsLearnedWords() {
|
||
let store = LearnedWordsStore(defaults: defaults)
|
||
store.learn("teh")
|
||
XCTAssertTrue(store.contains("teh"))
|
||
|
||
TypingJournal.delete(in: nil, settings: TypingJournalSettings(defaults: defaults))
|
||
|
||
XCTAssertFalse(store.contains("teh"), "a keyboard that loaded the word drops it")
|
||
XCTAssertNil(defaults.data(forKey: LearnedWordsStore.defaultsKey))
|
||
XCTAssertFalse(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 ")
|
||
}
|
||
|
||
/// An Italian iPhone types in Italian: its words stay ("Domani" never becomes "Domain")
|
||
/// and its typos get Italian fixes, accents included.
|
||
func testItalianCorrectsInItalianNeverEnglish() {
|
||
engine.language = .italian
|
||
XCTAssertEqual(engine.autocorrect.language, .italian)
|
||
XCTAssertTrue(engine.autocorrect.hasDictionary)
|
||
for word in ["Domani", "andiano", "al", "mare", "con", "i", "bambibi", "perche", "Sono", "molto", "stai"] {
|
||
typeWord(word)
|
||
tap(.space)
|
||
}
|
||
XCTAssertEqual(target.text, "Domani andiamo al mare con i bambini perché Sono molto stai ")
|
||
}
|
||
|
||
/// Russian types in Russian: its words stay and its typos get Russian fixes, never English.
|
||
func testRussianCorrectsInRussianNeverEnglish() {
|
||
engine.language = .russian
|
||
XCTAssertEqual(engine.autocorrect.language, .russian)
|
||
XCTAssertTrue(engine.autocorrect.hasDictionary)
|
||
for word in ["Привет", "опаздываю", "на", "десчть", "минут", "буду", "дома", "в", "восемь", "хорошл", "спасибл"] {
|
||
typeWord(word)
|
||
tap(.space)
|
||
}
|
||
XCTAssertEqual(target.text, "Привет опаздываю на десять минут буду дома в восемь хорошо спасибо ")
|
||
}
|
||
|
||
/// Serbian (Latin) has no dictionary on iOS: nothing is "corrected", into English or anything else.
|
||
func testSerbianLatinWithoutADictionaryCorrectsNothing() {
|
||
engine.language = .serbianLatin
|
||
XCTAssertEqual(engine.autocorrect.language, .serbianLatin)
|
||
XCTAssertFalse(engine.autocorrect.hasDictionary)
|
||
for word in ["Kasnim", "deset", "minuta", "hvala", "sta", "radis", "Sutra", "dolazim", "the", "teh"] {
|
||
typeWord(word)
|
||
tap(.space)
|
||
}
|
||
XCTAssertEqual(target.text, "Kasnim deset minuta hvala sta radis Sutra dolazim the teh ")
|
||
}
|
||
|
||
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))
|
||
// AZERTY: M sits under P, A beside Z, and W under D.
|
||
XCTAssertTrue(AutocorrectEngine.areAdjacentKeys("m", "p", language: .french))
|
||
XCTAssertFalse(AutocorrectEngine.areAdjacentKeys("m", "p", language: .english))
|
||
XCTAssertTrue(AutocorrectEngine.areAdjacentKeys("a", "z", language: .french))
|
||
XCTAssertTrue(AutocorrectEngine.areAdjacentKeys("w", "d", language: .french))
|
||
XCTAssertFalse(AutocorrectEngine.areAdjacentKeys("w", "q", language: .french))
|
||
XCTAssertTrue(AutocorrectEngine.areAdjacentKeys("a", "q", language: .frenchCanadian))
|
||
}
|
||
|
||
// MARK: - Split and punctuated guesses
|
||
|
||
/// A deterministic dictionary: `UITextChecker`'s answers vary between iOS versions.
|
||
private final class StubChecker: TextChecking {
|
||
var valid: Set<String> = []
|
||
var guessTable: [String: [String]] = [:]
|
||
var completionTable: [String: [String]] = [:]
|
||
func isWordValid(_ word: String) -> Bool { valid.contains(word.lowercased()) }
|
||
func guesses(for word: String) -> [String] { guessTable[word.lowercased()] ?? [] }
|
||
func completions(for prefix: String) -> [String] { completionTable[prefix.lowercased()] ?? [] }
|
||
}
|
||
|
||
private func stubbed(_ configure: (StubChecker) -> Void) -> AutocorrectEngine {
|
||
let checker = StubChecker()
|
||
configure(checker)
|
||
return AutocorrectEngine(checker: checker, learnedWords: LearnedWordsStore(defaults: defaults))
|
||
}
|
||
|
||
private func fix(_ engine: AutocorrectEngine, _ typed: String) -> String? {
|
||
engine.replacementOnSpace(beforeInput: typed, field: .text, autocorrectionType: .default)?.replacement
|
||
}
|
||
|
||
func testAMissedSpaceBecomesTwoWordsNeverAHyphen() {
|
||
let engine = stubbed {
|
||
$0.valid = ["up", "to", "a", "lot"]
|
||
// UITextChecker lists the hyphenated form first and the spaced one later.
|
||
$0.guessTable = ["upto": ["up-to", "updo", "unto", "up to"], "alot": ["a-lot", "lot", "a lot"]]
|
||
}
|
||
XCTAssertEqual(fix(engine, "upto"), "up to")
|
||
XCTAssertEqual(fix(engine, "Upto"), "Up to")
|
||
XCTAssertEqual(fix(engine, "alot"), "a lot", "English \"a\" is a word")
|
||
let slots = engine.suggestions(beforeInput: "upto", field: .text, autocorrectionType: .default)
|
||
XCTAssertEqual(slots.dropFirst().first?.replacement, "up to")
|
||
XCTAssertFalse(slots.contains { $0.replacement.contains("-") }, "never offers a hyphen you didn't type")
|
||
}
|
||
|
||
func testASplitNeedsTwoWordsYourLettersAndTheSpacedForm() {
|
||
let engine = stubbed {
|
||
$0.valid = ["ab", "cd", "e", "mail", "xy"]
|
||
$0.guessTable = [
|
||
// Only the hyphenated form: the checker doesn't offer the two words.
|
||
"abcd": ["ab-cd"],
|
||
// "zq" isn't a word.
|
||
"xyzq": ["xy-zq", "xy zq"],
|
||
// One letter that isn't "a" or "I" (UITextChecker accepts every letter).
|
||
"bmail": ["b-mail", "b mail"],
|
||
// Not a missed space: the letters changed.
|
||
"emial": ["e-mail", "e mail"],
|
||
]
|
||
}
|
||
for typed in ["abcd", "xyzq", "bmail", "emial"] {
|
||
XCTAssertNil(fix(engine, typed), "\(typed) stays as typed")
|
||
}
|
||
}
|
||
|
||
func testAGuessNeverAddsPunctuationYouDidntType() {
|
||
let engine = stubbed {
|
||
$0.valid = ["ok"]
|
||
$0.guessTable = ["tbq": ["t.b.q", "tb-q", "tbq."], "wnot": ["w.not", "won't"]]
|
||
}
|
||
XCTAssertNil(fix(engine, "tbq"))
|
||
XCTAssertEqual(fix(engine, "wnot"), "won't", "a contraction's apostrophe is part of the word")
|
||
let slots = engine.suggestions(beforeInput: "tbq", field: .text, autocorrectionType: .default)
|
||
XCTAssertFalse(slots.contains { $0.replacement.contains(".") || $0.replacement.contains("-") })
|
||
}
|
||
|
||
// MARK: - Bilingual autocorrect
|
||
|
||
private func fix(_ engine: AutocorrectEngine, before: String) -> String? {
|
||
engine.replacementOnSpace(beforeInput: before, field: .text, autocorrectionType: .default)?.replacement
|
||
}
|
||
|
||
/// French and English on one keyboard (Apple's "Français et English"): a word of either is
|
||
/// never "corrected" into the other, a typo only one can fix gets its fix, and when both
|
||
/// can, the language of the words before wins, else the keyboard's own.
|
||
func testBilingualAutocorrectNeverCorrectsOneLanguageIntoTheOther() {
|
||
let available = ["fr_FR", "en_US"]
|
||
let english = StubChecker()
|
||
english.valid = ["the", "see", "you", "meeting", "at", "noon"]
|
||
english.guessTable = ["mercu": ["mercy"], "est": ["east", "set"], "meetign": ["meeting"], "chst": ["chat"]]
|
||
english.completionTable = ["mer": ["merry", "mercy"]]
|
||
let french = stubbed {
|
||
$0.valid = ["je", "pense", "que", "dont", "est", "le", "chat", "merci"]
|
||
$0.guessTable = ["mercu": ["merci"], "the": ["thé"], "chst": ["chat"], "dont": ["don't"]]
|
||
$0.completionTable = ["mer": ["merci", "mercredi"]]
|
||
}
|
||
french.setLanguage(.french, available: available)
|
||
french.setPartner(.english, available: available, checker: english)
|
||
XCTAssertEqual(french.partner?.language, .english)
|
||
|
||
XCTAssertNil(fix(french, before: "the"), "an English word stays on the French keyboard")
|
||
XCTAssertNil(fix(french, before: "dont"), "French \"dont\" never becomes \"don't\"")
|
||
XCTAssertEqual(fix(french, before: "teh"), "the", "an English typo is fixed while French types")
|
||
XCTAssertEqual(fix(french, before: "meetign"), "meeting")
|
||
XCTAssertEqual(fix(french, before: "chst"), "chat")
|
||
XCTAssertEqual(fix(french, before: "mercu"), "merci", "both can fix it: the keyboard's own language")
|
||
XCTAssertEqual(fix(french, before: "je pense que mercu"), "merci")
|
||
XCTAssertEqual(fix(french, before: "see you mercu"), "mercy", "the words before are English")
|
||
XCTAssertEqual(Array(strip(french, "mercu").dropFirst()), ["merci", "mercy"], "both languages' fixes in the strip")
|
||
XCTAssertEqual(Array(strip(french, "mer").dropFirst()), ["merci", "merry"], "and both languages' completions")
|
||
|
||
// English typing with French beside it: French words stay, "i" is still "I".
|
||
let englishFirst = stubbed { $0.valid = english.valid; $0.guessTable = english.guessTable }
|
||
englishFirst.setLanguage(.english, available: available)
|
||
let frenchChecker = StubChecker()
|
||
frenchChecker.valid = ["dont", "est", "merci"]
|
||
englishFirst.setPartner(.french, available: available, checker: frenchChecker)
|
||
XCTAssertNil(fix(englishFirst, before: "est"))
|
||
XCTAssertNil(fix(englishFirst, before: "dont"), "French \"dont\" stays")
|
||
XCTAssertEqual(fix(englishFirst, before: "i"), "I")
|
||
|
||
// No partner without a dictionary for it, or for the keyboard's own language.
|
||
french.setPartner(.german, available: available)
|
||
XCTAssertNil(french.partner)
|
||
french.setPartner(.english, available: available, checker: english)
|
||
french.setLanguage(.serbianLatin, available: available)
|
||
XCTAssertNil(french.partner)
|
||
}
|
||
|
||
/// How often one language's keyboard changes the other's words, against iOS's own
|
||
/// dictionaries and English's Spatial autocorrect model: common French words typed on the
|
||
/// English keyboard and common English words on the French one (taps where the layout has
|
||
/// the letters, else the word put in as it is), each in an empty field. With both
|
||
/// languages chosen, none changes. Prints `BILINGUAL_BREAKS` lines.
|
||
func testABilingualKeyboardKeepsBothLanguagesWords() throws {
|
||
let frenchWords = [
|
||
"bonjour", "merci", "demain", "toujours", "dont", "est", "pour", "sur", "pas", "plus",
|
||
"tout", "mais", "elle", "les", "des", "une", "que", "qui", "dans", "avec", "fait",
|
||
"être", "très", "où", "ça", "même", "bien", "aussi", "comme", "nous", "vous", "ils",
|
||
"sont", "été", "après", "encore", "chez", "parce", "rien", "peu", "beaucoup",
|
||
"jamais", "cette", "notre", "votre", "leur", "quand", "alors", "deux", "trois",
|
||
"jour", "temps", "voir", "faire", "dire", "aller", "veux", "peut", "suis", "oui",
|
||
"non", "soir", "matin", "maison", "travail", "ami", "amis", "envie", "vraiment",
|
||
"bisous", "salut", "réunion", "semaine", "rendez", "arrive", "attends", "train",
|
||
]
|
||
let englishWords = [
|
||
"the", "and", "you", "that", "was", "for", "are", "with", "his", "they", "this",
|
||
"have", "from", "one", "had", "word", "but", "not", "what", "all", "were", "when",
|
||
"your", "can", "said", "there", "use", "each", "which", "she", "how", "their",
|
||
"will", "other", "about", "out", "many", "then", "them", "these", "some", "her",
|
||
"would", "make", "like", "him", "into", "time", "has", "look", "two", "more",
|
||
"write", "see", "number", "way", "could", "people", "than", "first", "water",
|
||
"been", "call", "who", "now", "find", "long", "down", "day", "did", "get", "come",
|
||
"made", "may", "part", "tomorrow", "meeting", "thanks", "weekend", "tonight",
|
||
]
|
||
func typed(_ words: [String], on language: KeyboardLanguage, partner: KeyboardLanguage?) -> [String] {
|
||
engine.language = language
|
||
engine.partnerLanguage = partner
|
||
engine.autocorrect.decoder = TypingDecoderModel.bundled(language.rawValue).map { TypingDecoder(model: $0) }
|
||
engine.autocorrect.partner?.decoder = partner.flatMap { TypingDecoderModel.bundled($0.rawValue) }.map { TypingDecoder(model: $0) }
|
||
var changed: [String] = []
|
||
for word in words {
|
||
while target.hasText { target.deleteBackward() }
|
||
engine.textDidChange()
|
||
let keys = word.map { engine.layout.key(.character(String($0))) }
|
||
if keys.allSatisfy({ $0 != nil }) {
|
||
for ch in word { tap(.character(String(ch))) }
|
||
} else {
|
||
target.insertText(word)
|
||
engine.textDidChange()
|
||
}
|
||
tap(.space)
|
||
if target.text != word + " " { changed.append("\(word)→\(target.text.trimmingCharacters(in: .whitespaces))") }
|
||
}
|
||
return changed
|
||
}
|
||
let englishAlone = typed(frenchWords, on: .english, partner: nil)
|
||
let frenchAlone = typed(englishWords, on: .french, partner: nil)
|
||
let englishWithFrench = typed(frenchWords, on: .english, partner: .french)
|
||
let frenchWithEnglish = typed(englishWords, on: .french, partner: .english)
|
||
let englishKeepsEnglish = typed(englishWords, on: .english, partner: .french)
|
||
let frenchKeepsFrench = typed(frenchWords, on: .french, partner: .english)
|
||
print("BILINGUAL_BREAKS English alone changed \(englishAlone.count) of \(frenchWords.count) French words: \(englishAlone)")
|
||
print("BILINGUAL_BREAKS Français alone changed \(frenchAlone.count) of \(englishWords.count) English words: \(frenchAlone)")
|
||
print("BILINGUAL_BREAKS English + Français, English typing, changed \(englishWithFrench.count) French words: \(englishWithFrench)")
|
||
print("BILINGUAL_BREAKS English + Français, Français typing, changed \(frenchWithEnglish.count) English words: \(frenchWithEnglish)")
|
||
print("BILINGUAL_BREAKS own words changed: English \(englishKeepsEnglish), Français \(frenchKeepsFrench)")
|
||
XCTAssertEqual(englishWithFrench, [])
|
||
XCTAssertEqual(frenchWithEnglish, [])
|
||
XCTAssertEqual(englishKeepsEnglish, [])
|
||
XCTAssertEqual(frenchKeepsFrench, [])
|
||
}
|
||
|
||
/// The iPhone's own dictionaries: English typos are fixed on the French keyboard while
|
||
/// French and English words stay as typed.
|
||
func testTheFrenchKeyboardFixesEnglishTyposWithEnglishBesideIt() {
|
||
engine.language = .french
|
||
engine.partnerLanguage = .english
|
||
XCTAssertEqual(engine.autocorrect.partner?.language, .english)
|
||
for word in ["Bonjour", "merci", "the", "meeting", "est", "dont", "teh", "tomorow", "demian"] {
|
||
typeWord(word)
|
||
tap(.space)
|
||
}
|
||
XCTAssertEqual(target.text, "Bonjour merci the meeting est dont the tomorrow demain ")
|
||
}
|
||
|
||
func testTheSystemDictionarySplitsAMissedSpace() {
|
||
// iOS 27's UITextChecker guesses "never mind" and "never-mind" for this.
|
||
XCTAssertEqual(fix(engine.autocorrect, "nevermind"), "never mind")
|
||
XCTAssertNotEqual(fix(engine.autocorrect, "eachother")?.contains("-"), true)
|
||
}
|
||
|
||
// MARK: - Completions
|
||
|
||
private func strip(_ engine: AutocorrectEngine, _ typed: String) -> [String] {
|
||
engine.suggestions(beforeInput: typed, field: .text, autocorrectionType: .default).map(\.replacement)
|
||
}
|
||
|
||
func testCompletionsKeepTheDictionarysFrequencyOrderAheadOfYourContacts() {
|
||
let engine = stubbed {
|
||
$0.valid = ["zo"]
|
||
// Most probable first, as UITextChecker lists them; not by length or alphabet.
|
||
$0.completionTable = ["zo": ["zone", "zoo", "zombie", "zones"]]
|
||
}
|
||
engine.loadLexiconEntries([("Zorbin", "Zorbin"), ("Zoltrax", "Zoltrax"), ("zoeticket", "zoeticket")])
|
||
XCTAssertEqual(strip(engine, "zo"), ["zo", "zone", "zoo"])
|
||
XCTAssertEqual(strip(engine, "Zo"), ["Zo", "Zone", "Zoo"], "a capital at the start of a sentence still means words first")
|
||
}
|
||
|
||
func testNamesComeLastUnlessYouTypedACapital() {
|
||
let engine = stubbed {
|
||
$0.valid = ["qu"]
|
||
$0.completionTable = ["qu": ["Quilla", "quip", "quiz"]]
|
||
}
|
||
engine.loadLexiconEntries([("Quorvent", "Quorvent"), ("qubitly", "qubitly")])
|
||
XCTAssertEqual(strip(engine, "qu"), ["qu", "quip", "quiz"])
|
||
XCTAssertEqual(strip(engine, "Qu"), ["Qu", "Quilla", "Quip"], "you typed a capital: names keep their place")
|
||
|
||
// With fewer dictionary words, your own lowercase word comes before a name.
|
||
let few = stubbed {
|
||
$0.valid = ["qu"]
|
||
$0.completionTable = ["qu": ["Quilla", "quip"]]
|
||
}
|
||
few.loadLexiconEntries([("Quorvent", "Quorvent"), ("qubitly", "qubitly")])
|
||
XCTAssertEqual(strip(few, "qu"), ["qu", "quip", "qubitly"])
|
||
XCTAssertEqual(strip(few, "Qu"), ["Qu", "Quilla", "Quip"])
|
||
// Nothing else: a contact's name is still offered.
|
||
let names = stubbed { $0.valid = ["qu"] }
|
||
names.loadLexiconEntries([("Quorvent", "Quorvent")])
|
||
XCTAssertEqual(strip(names, "qu"), ["qu", "Quorvent"])
|
||
}
|
||
|
||
func testTheStripNeverRepeatsWhatYouTyped() {
|
||
let engine = stubbed {
|
||
$0.valid = ["k", "kay"]
|
||
$0.completionTable = ["k": ["k", "know", "keep"], "kay": ["kay", "kayak"]]
|
||
}
|
||
XCTAssertEqual(strip(engine, "k"), ["k", "know", "keep"], "one letter gets completions too")
|
||
XCTAssertEqual(strip(engine, "kay"), ["kay", "kayak"])
|
||
XCTAssertEqual(strip(engine, "Kay"), ["Kay", "Kayak"])
|
||
XCTAssertEqual(strip(engine, "kq"), ["kq"], "nothing to offer: only the left slot, not a second copy")
|
||
XCTAssertEqual(
|
||
engine.suggestions(beforeInput: "kq", field: .text, autocorrectionType: .default).map(\.kind),
|
||
[.keepLiteral]
|
||
)
|
||
}
|
||
|
||
func testTheSystemDictionaryRanksCommonWordsFirst() {
|
||
// iOS 27's UITextChecker lists "b" as but, be, because… and "th" as the, they, that's….
|
||
XCTAssertEqual(Array(strip(engine.autocorrect, "b").dropFirst()), ["but", "be"])
|
||
XCTAssertEqual(strip(engine.autocorrect, "th").dropFirst().first, "the")
|
||
XCTAssertEqual(strip(engine.autocorrect, "Th").dropFirst().first, "The")
|
||
}
|
||
|
||
// MARK: - Spatial autocorrect (the bundled models)
|
||
|
||
private func useDecoder(_ language: KeyboardLanguage = .english) throws {
|
||
if language != engine.language { engine.language = language }
|
||
let model = try XCTUnwrap(TypingDecoderModel.bundled(language.rawValue), "the \(language) model is bundled")
|
||
engine.autocorrect.decoder = TypingDecoder(model: model)
|
||
}
|
||
|
||
private func tap(at point: CGPoint) {
|
||
engine.touchBegan(id: 1, at: point)
|
||
engine.touchEnded(id: 1, at: point)
|
||
now += 0.2
|
||
}
|
||
|
||
/// Types `word` at the keys' centres, except an "o" written as "^" lands low, just inside
|
||
/// the k key (where these logs' o taps go astray), then a space.
|
||
private func typeTaps(_ word: String) {
|
||
for ch in word {
|
||
if ch == "^" {
|
||
let o = engine.layout.key(.character("o"))!.frame
|
||
let k = engine.layout.key(.character("k"))!.frame
|
||
let point = CGPoint(x: o.midX - 8, y: k.minY + 4)
|
||
XCTAssertEqual(engine.layout.hitTest(point)?.kind, .character("k"))
|
||
tap(at: point)
|
||
} else {
|
||
let key = engine.layout.key(.character(String(ch)))!.frame
|
||
tap(at: CGPoint(x: key.midX, y: key.midY))
|
||
}
|
||
}
|
||
tap(.space)
|
||
}
|
||
|
||
func testSpatialAutocorrectReadsTapsWithTheWordBefore() throws {
|
||
try useDecoder()
|
||
for word in ["we", "need", "t^"] { typeTaps(word) }
|
||
XCTAssertEqual(target.text, "we need to ", "a two-letter slip after \"need\"")
|
||
for word in ["and", "seeh^w"] { typeTaps(word) }
|
||
XCTAssertEqual(target.text, "we need to and see how ", "a missed space with a slip inside")
|
||
}
|
||
|
||
func testSpatialAutocorrectLeavesWordsTypedRight() throws {
|
||
try useDecoder()
|
||
for word in ["come", "and", "miss", "the", "quiz"] { typeTaps(word) }
|
||
XCTAssertEqual(target.text, "come and miss the quiz ")
|
||
}
|
||
|
||
/// The models' word lists are lowercase: a name or day typed in lowercase is still a
|
||
/// word, capitalised as the dictionary path does, never "may" or "money".
|
||
func testSpatialAutocorrectCapitalisesANameTypedInLowercase() throws {
|
||
try useDecoder()
|
||
for word in ["see", "mary", "on", "monday", "in", "boston"] { typeTaps(word) }
|
||
XCTAssertEqual(target.text, "see Mary on Monday in Boston ")
|
||
}
|
||
|
||
func testSpatialAutocorrectAddsSpanishAccents() throws {
|
||
try useDecoder(.spanish)
|
||
for word in ["esta", "aqui", "manana"] { typeTaps(word) }
|
||
XCTAssertEqual(target.text, "esta aquí mañana ", "\"esta\" is a word too, so it stays")
|
||
}
|
||
|
||
func testSpatialAutocorrectKnowsYourOwnWords() throws {
|
||
try useDecoder()
|
||
engine.autocorrect.loadLexiconEntries([("Zorbix", "Zorbix")])
|
||
typeTaps("zorbiz")
|
||
typeTaps("zorbix")
|
||
XCTAssertEqual(target.text, "Zorbix zorbix ", "a slip in a name from Contacts, and the name typed right")
|
||
}
|
||
|
||
/// "th", then the e aimed high 80 ms later, landing on the strip's lower half under its
|
||
/// left slot. Record typing logs showed that picked what was typed and a space ("th "),
|
||
/// and kept "th" from being corrected for the rest of the session. The keys take the
|
||
/// tap now, on row 1's top edge, and Spatial autocorrect keeps the "the" it spells.
|
||
func testSpatialAutocorrectReadsAnETappedHighOnTheStripAsThe() throws {
|
||
try useDecoder()
|
||
typeTaps("on")
|
||
for letter in ["t", "h"] {
|
||
let key = engine.layout.key(.character(letter))!.frame
|
||
engine.touchBegan(id: 1, at: CGPoint(x: key.midX, y: key.midY))
|
||
engine.touchEnded(id: 1, at: CGPoint(x: key.midX, y: key.midY))
|
||
now += 0.1
|
||
}
|
||
let e = engine.layout.key(.character("e"))!.frame
|
||
let aimedHigh = CGPoint(x: e.midX, y: engine.layout.stripFrame.maxY - 6)
|
||
XCTAssertTrue(engine.takesStripTouch(at: aimedHigh))
|
||
tap(at: aimedHigh)
|
||
tap(.space)
|
||
XCTAssertEqual(target.text, "on the ")
|
||
}
|
||
|
||
// MARK: - Offensive words
|
||
|
||
func testTheBundledListMarksSlursAndSwearWordsAndLeavesAnatomyAlone() {
|
||
let en = OffensiveWords.bundled("en")
|
||
XCTAssertEqual(en.kind(of: "nigger"), .slur)
|
||
XCTAssertEqual(en.kind(of: "Faggots"), .slur, "inflections, any case")
|
||
XCTAssertEqual(en.kind(of: "chink"), .slur, "slurs LDNOOBW lacks")
|
||
XCTAssertEqual(en.kind(of: "fuck"), .swear)
|
||
XCTAssertEqual(en.kind(of: "damn"), .swear)
|
||
for word in ["penis", "vagina", "anus", "sex", "suck", "rape", "escort", "scat", "xx"] {
|
||
XCTAssertNil(en.kind(of: word), "\(word) is an everyday, anatomy, medical or reporting word")
|
||
}
|
||
XCTAssertTrue(en.blocks("blow job", allowSwearing: false), "an expression")
|
||
XCTAssertTrue(en.blocks("job", allowSwearing: false, after: "blow"), "an expression's second word after its first")
|
||
XCTAssertFalse(en.blocks("blow", allowSwearing: false), "an expression's words alone are fine")
|
||
XCTAssertFalse(en.blocks("job", allowSwearing: false, after: "a"))
|
||
XCTAssertTrue(en.blocks("the shit", allowSwearing: false), "a listed word inside a split")
|
||
XCTAssertFalse(en.blocks("the shit", allowSwearing: true))
|
||
XCTAssertTrue(en.blocks("the nigger", allowSwearing: true))
|
||
let es = OffensiveWords.bundled("es")
|
||
XCTAssertEqual(es.kind(of: "maricón"), .slur)
|
||
XCTAssertEqual(es.kind(of: "mierda"), .swear)
|
||
XCTAssertNil(es.kind(of: "negro"), "Spanish for black")
|
||
XCTAssertNil(es.kind(of: "cono"), "accents are kept: coño is listed, cono (cone) isn't")
|
||
XCTAssertEqual(OffensiveWords.bundled("de").kind(of: "scheisse"), .swear)
|
||
XCTAssertNil(OffensiveWords.bundled("de").kind(of: "dick"), "German for thick")
|
||
XCTAssertEqual(OffensiveWords.bundled("nb").kind(of: "faen"), .swear)
|
||
XCTAssertNil(OffensiveWords.bundled("nb").kind(of: "fag"), "Norwegian for a trade")
|
||
let it = OffensiveWords.bundled("it")
|
||
XCTAssertEqual(it.kind(of: "nigger"), .slur, "every layout shares the loanwords")
|
||
XCTAssertEqual(it.kind(of: "fuck"), .swear)
|
||
XCTAssertNil(it.kind(of: "chink"))
|
||
}
|
||
|
||
func testAnExpressionIsBlockedOnlyAfterItsFirstWord() {
|
||
let autocorrect = stubbed {
|
||
$0.valid = ["blow", "a", "jo", "job", "joke"]
|
||
$0.completionTable = ["jo": ["job", "joke"]]
|
||
}
|
||
XCTAssertEqual(Array(strip(autocorrect, "blow jo").dropFirst()), ["joke"])
|
||
XCTAssertEqual(Array(strip(autocorrect, "a jo").dropFirst()), ["job", "joke"])
|
||
autocorrect.allowsSwearing = true
|
||
XCTAssertEqual(Array(strip(autocorrect, "blow jo").dropFirst()), ["job", "joke"])
|
||
}
|
||
|
||
func testSwearWordsAreNeverGuessedOrCompletedUnlessTurnedOn() {
|
||
let autocorrect = stubbed {
|
||
$0.valid = ["ship", "duck", "fuchsia"]
|
||
$0.guessTable = ["shiy": ["shit", "ship"]]
|
||
$0.completionTable = ["fuc": ["fuck", "fucking", "fuchsia"]]
|
||
}
|
||
XCTAssertEqual(fix(autocorrect, "shiy"), "ship", "shit is a closer key, but never offered")
|
||
XCTAssertFalse(strip(autocorrect, "shiy").contains("shit"))
|
||
XCTAssertEqual(Array(strip(autocorrect, "fuc").dropFirst()), ["fuchsia"])
|
||
|
||
autocorrect.allowsSwearing = true
|
||
XCTAssertEqual(fix(autocorrect, "shiy"), "shit")
|
||
XCTAssertEqual(Array(strip(autocorrect, "fuc").dropFirst()), ["fuck", "fucking"])
|
||
}
|
||
|
||
func testSlursAreNeverOfferedEvenWithSwearWordsOn() {
|
||
let autocorrect = stubbed {
|
||
$0.guessTable = ["niggr": ["nigger", "nigga"], "fagot": ["faggot", "fagots"]]
|
||
$0.completionTable = ["nig": ["nigger", "night", "nigh"]]
|
||
$0.valid = ["night", "nigh", "fagots"]
|
||
}
|
||
autocorrect.allowsSwearing = true
|
||
XCTAssertNil(fix(autocorrect, "niggr"))
|
||
XCTAssertEqual(strip(autocorrect, "niggr"), ["niggr"])
|
||
XCTAssertEqual(fix(autocorrect, "fagot"), "fagots")
|
||
XCTAssertEqual(Array(strip(autocorrect, "nig").dropFirst()), ["night", "nigh"])
|
||
}
|
||
|
||
func testAnOffensiveWordTypedInFullIsNeverCorrected() {
|
||
let autocorrect = stubbed {
|
||
$0.guessTable = ["fuck": ["duck", "luck"], "nigger": ["bigger", "niggle"]]
|
||
}
|
||
XCTAssertNil(fix(autocorrect, "fuck"), "UITextChecker may not know it, but you typed it")
|
||
XCTAssertNil(fix(autocorrect, "Nigger"))
|
||
XCTAssertFalse(autocorrect.suggestions(beforeInput: "fuck", field: .text, autocorrectionType: .default)
|
||
.contains(where: \.willAutoReplace))
|
||
autocorrect.allowsSwearing = true
|
||
XCTAssertEqual(fix(autocorrect, "fuck"), "duck", "with swear words on, it's an ordinary word, as on Android")
|
||
XCTAssertNil(fix(autocorrect, "Nigger"), "a slur typed in full still stays")
|
||
}
|
||
|
||
func testYourOwnContactsAndReplacementsAreStillOffered() {
|
||
let autocorrect = stubbed { $0.completionTable = ["dic": ["dick", "dictionary"]] }
|
||
autocorrect.loadLexiconEntries([("Dick", "Dick"), ("wtf", "what the fuck")])
|
||
XCTAssertEqual(Array(strip(autocorrect, "Dic").dropFirst()), ["Dick", "Dictionary"])
|
||
XCTAssertEqual(fix(autocorrect, "wtf"), "what the fuck")
|
||
}
|
||
|
||
/// What iOS's own dictionary does with near-misses of swear words and slurs (printed for
|
||
/// the record), and that the strip and space never pass them on.
|
||
func testTheSystemDictionarysOffensiveGuessesNeverReachTheStrip() {
|
||
let checker = SystemTextChecker()
|
||
let words = OffensiveWords.bundled("en")
|
||
for typed in ["fuc", "fuvk", "fucm", "shi", "shiy", "shjt", "bitc", "bithc", "cun", "cunr", "dic", "dicj",
|
||
"nig", "nigg", "niggr", "niggee", "fagg", "faggit", "kik", "spi", "coo", "pus", "pussu", "ass", "asshol"] {
|
||
let guesses = checker.guesses(for: typed)
|
||
let completions = checker.completions(for: typed)
|
||
let flagged = (guesses + completions).filter { words.kind(of: $0) != nil }
|
||
print("UITextChecker \(typed): valid=\(checker.isWordValid(typed)) guesses=\(guesses.prefix(6)) completions=\(completions.prefix(6)) offensive=\(flagged)")
|
||
for allow in [false, true] {
|
||
engine.autocorrect.allowsSwearing = allow
|
||
for item in strip(engine.autocorrect, typed).dropFirst() {
|
||
XCTAssertFalse(words.blocks(item, allowSwearing: allow), "\(typed) offered \(item), swear words \(allow)")
|
||
}
|
||
if let replacement = fix(engine.autocorrect, typed) {
|
||
XCTAssertFalse(words.blocks(replacement, allowSwearing: allow), "\(typed) corrected to \(replacement)")
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Types `word` at the keys' centres, no space, and returns the strip's words.
|
||
private func stripAfterTaps(_ word: String, after before: [String] = []) -> [String] {
|
||
for previous in before { typeTaps(previous) }
|
||
for ch in word {
|
||
let key = engine.layout.key(.character(String(ch)))!.frame
|
||
tap(at: CGPoint(x: key.midX, y: key.midY))
|
||
}
|
||
return engine.suggestions.dropFirst().map(\.replacement)
|
||
}
|
||
|
||
func testSpatialAutocorrectNeverReadsTapsAsAnOffensiveWord() throws {
|
||
try useDecoder()
|
||
let words = OffensiveWords.bundled("en")
|
||
engine.autocorrect.allowsSwearing = true
|
||
XCTAssertTrue(stripAfterTaps("fucj", after: ["what", "the"]).contains("fuck"), "with swear words on, the model offers it")
|
||
tap(.space)
|
||
let probes = ["fucj", "fuxk", "shjt", "shiy", "bitcg", "cunr", "dicj", "pussu", "nigher", "niggee", "faggit", "kikw", "cocj"]
|
||
for allow in [false, true] {
|
||
engine.autocorrect.allowsSwearing = allow
|
||
for probe in probes {
|
||
let strip = stripAfterTaps(probe, after: ["what", "a"])
|
||
for item in strip {
|
||
XCTAssertFalse(words.blocks(item, allowSwearing: allow), "\(probe) offered \(item), swear words \(allow)")
|
||
}
|
||
tap(.space)
|
||
let typed = target.text.split(separator: " ").last.map(String.init) ?? ""
|
||
XCTAssertFalse(words.blocks(typed, allowSwearing: allow), "\(probe) became \(typed), swear words \(allow)")
|
||
}
|
||
}
|
||
}
|
||
|
||
func testSpatialAutocorrectKeepsAnOffensiveWordTypedInFull() throws {
|
||
try useDecoder()
|
||
for word in ["what", "the", "fuck", "you", "bitch"] { typeTaps(word) }
|
||
XCTAssertEqual(target.text, "what the fuck you bitch ")
|
||
}
|
||
|
||
func testTurningAutocorrectSettingOffKeepsSuggestionsWithoutAutoReplacing() {
|
||
engine.autocorrect.isEnabled = false
|
||
typeWord("dont")
|
||
XCTAssertFalse(engine.suggestions.isEmpty)
|
||
XCTAssertFalse(engine.suggestions[1].willAutoReplace)
|
||
tap(.space)
|
||
XCTAssertEqual(target.text, "dont ")
|
||
}
|
||
}
|