SPB Git

Repository Public

KeyboardSurfaceTests.swift 9.5 KB 216 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : SP Keyboard7// File             : Tests/AppTests/KeyboardSurfaceTests.swift8// Module           : Tests9// Repository       : Private repository (SPB Git)10// Project Website  : Not specified11// Created          : 2026-12-0312// Last Modified    : 2026-12-0313//14// Author           : Simon-Pierre Boucher15// Position         : Professor of Finance16// Institution      : Université du Québec en Outaouais (UQO)17// Organization     : SP Boucher18// Website          : https://spboucher.ai19// Git              : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development      : dev@spboucher.ai23// Support          : support@spboucher.ai24// Security/Admin   : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License          : None specified (no license file in this repository)33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import XCTest42import UIKit43import AIEngine44import KeyboardCore45import ThemeEngine46@testable import SPKeyboard4748final class TouchModelTests: XCTestCase {49    private let cell = CGSize(width: 40, height: 54)50    private let w = TouchModel.Candidate(id: "w", center: CGPoint(x: 60, y: 27))51    private let e = TouchModel.Candidate(id: "e", center: CGPoint(x: 100, y: 27))5253    private func resolve(x: CGFloat, y: CGFloat = 27, weights: [String: Double]) -> String {54        TouchModel.resolve(point: CGPoint(x: x, y: y), base: x < 80 ? w : e, cell: cell, candidates: [w, e], weights: weights)55    }5657    func testWithoutWeightsTheKeyUnderTheFingerWins() {58        XCTAssertEqual(resolve(x: 79, weights: [:]), "w")59        XCTAssertEqual(resolve(x: 81, weights: [:]), "e")60    }6162    func testCoreOfAKeyIsNeverReassigned() {63        let weights = ["e": 0.99, "w": 0.0001]64        XCTAssertEqual(resolve(x: 60, weights: weights), "w")65        XCTAssertEqual(resolve(x: 60 + cell.width * 0.39, weights: weights), "w", "inside the core, whatever the word suggests")66    }6768    func testBorderTouchGoesToTheLikelyLetter() {69        let weights = ["e": 0.6, "w": 0.01]70        XCTAssertEqual(resolve(x: 78, weights: weights), "e", "two points on the wrong side of the border")71        XCTAssertEqual(resolve(x: 82, weights: weights), "e")72        XCTAssertEqual(resolve(x: 78, weights: ["e": 0.01, "w": 0.6]), "w")73    }7475    func testEvenLikelihoodsLeaveGeometryAlone() {76        let weights = ["e": 0.3, "w": 0.3]77        XCTAssertEqual(resolve(x: 78, weights: weights), "w")78        XCTAssertEqual(resolve(x: 82, weights: weights), "e")79    }8081    func testAnUnlikelyKeyCanStillBeTyped() {82        // On the cap of w, with a word that only wants e: w is what the finger is on.83        XCTAssertEqual(resolve(x: 60 + cell.width * 0.37, weights: ["e": 1]), "w")84        // And on the far rim, an even mildly plausible w still wins on its own side.85        XCTAssertEqual(resolve(x: 60 + cell.width * 0.42, weights: ["e": 0.3, "w": 0.1]), "w")86    }87}8889@MainActor90final class KeyboardControllerTwoTests: XCTestCase {91    private var document: PreviewDocument!92    private var container: KeyboardPreviewContainer!93    private var window: UIWindow!9495    override func setUp() async throws {96        document = PreviewDocument()97        container = KeyboardPreviewContainer(document: document, environment: .live())98        window = UIWindow(frame: CGRect(x: 0, y: 0, width: 393, height: 320))99        container.frame = window.bounds100        window.addSubview(container)101        window.isHidden = false102    }103104    override func tearDown() async throws {105        window.isHidden = true106        window = nil107        container = nil108        document = nil109    }110111    private func inject(_ settings: KeyboardSettings = KeyboardSettings(), theme: Theme = BuiltInThemes.default, dark: Theme? = nil) {112        container.controller.inject(settings: settings, dictionary: UserDictionary(), theme: theme, darkTheme: dark, aiSettings: AISettings())113        container.setNeedsLayout()114        container.layoutIfNeeded()115        container.controller.rootView.layoutIfNeeded()116        container.controller.rootView.surface.layoutIfNeeded()117    }118119    /// Traces `word` until the lexicon is loaded and the decoder has answered.120    private func glide(_ word: String, expecting text: String, file: StaticString = #filePath, line: UInt = #line) async {121        for _ in 0..<60 {122            container.controller.debugGlide(word)123            await container.controller.debugGlideTask?.value124            if document.text == text { return }125            guard document.text.count < text.count else { break }126            try? await Task.sleep(nanoseconds: 100_000_000)127        }128        XCTAssertEqual(document.text, text, file: file, line: line)129    }130131    func testGlideTypesWordsEndToEnd() async {132        inject()133        await glide("bonjour", expecting: "Bonjour ")134        await glide("monde", expecting: "Bonjour monde ")135        container.controller.debugTap("backspace")136        XCTAssertEqual(document.text, "Bonjour ", "one backspace takes the whole traced word back")137        await glide("merci", expecting: "Bonjour merci ")138        container.controller.debugTap(".")139        XCTAssertEqual(document.text, "Bonjour merci.", "punctuation takes the automatic space")140    }141142    func testGlideIsOffWhenTheSettingIsOffAndInAddressFields() async {143        var settings = KeyboardSettings()144        settings.glideTyping = false145        inject(settings)146        XCTAssertFalse(container.controller.rootView.surface.glideEnabled)147        settings.glideTyping = true148        inject(settings)149        XCTAssertTrue(container.controller.rootView.surface.glideEnabled)150        container.host.traits.variant = .url151        container.controller.textDidChange()152        XCTAssertFalse(container.controller.rootView.surface.glideEnabled)153    }154155    func testNumberRowAddsARowAndKeepsTheKeyboardCompact() {156        inject()157        let surface = container.controller.rootView.surface158        XCTAssertEqual(surface.layout.rows.count, 4)159        XCTAssertEqual(surface.layout.rows[0].first?.hint, "1")160        let plain = container.controller.preferredHeight161        var settings = KeyboardSettings()162        settings.numberRow = true163        inject(settings)164        XCTAssertEqual(surface.layout.rows.count, 5)165        XCTAssertNil(surface.layout.rows[1].first?.hint)166        let tall = container.controller.preferredHeight167        XCTAssertGreaterThan(tall, plain)168        XCTAssertLessThan(tall - plain, 54, "less than one full extra row")169        container.controller.debugTap("7")170        XCTAssertEqual(document.text, "7")171    }172173    func testDarkVariantFollowsTheAppearance() {174        window.overrideUserInterfaceStyle = .light175        inject(theme: BuiltInThemes.atelierClair, dark: BuiltInThemes.atelierNuit)176        XCTAssertFalse(container.controller.debugDrawsDark)177        window.overrideUserInterfaceStyle = .dark178        window.updateTraitsIfNeeded()179        container.controller.rootView.updateTraitsIfNeeded()180        XCTAssertEqual(container.controller.rootView.traitCollection.userInterfaceStyle, .dark)181        container.controller.viewWillAppear()182        XCTAssertTrue(container.controller.debugDrawsDark)183        // Without a pair, the single theme is drawn in both appearances.184        inject(theme: BuiltInThemes.craie)185        XCTAssertEqual(container.controller.darkTheme, nil)186    }187188    func testAdaptiveTouchOnlyBendsTheBorder() {189        inject()190        let surface = container.controller.rootView.surface191        guard let w = surface.frameOfKey(id: "w"), let e = surface.frameOfKey(id: "e") else { return XCTFail("letters are laid out") }192        let border = CGPoint(x: (w.maxX + e.minX) / 2 - 1.5, y: w.midY)193        XCTAssertEqual(surface.debugKeyID(at: border, adaptive: true), "w", "no word in progress: plain geometry")194        surface.keyWeights = ["e": 0.7, "w": 0.01]195        XCTAssertEqual(surface.debugKeyID(at: border, adaptive: true), "e")196        XCTAssertEqual(surface.debugKeyID(at: border, adaptive: false), "w")197        XCTAssertEqual(surface.debugKeyID(at: CGPoint(x: w.midX, y: w.midY), adaptive: true), "w")198    }199200    func testEmojiKeyOpensThePanelAndReturnKeyWaitsForText() {201        inject()202        container.host.traits.enablesReturnKeyAutomatically = true203        container.controller.textDidChange()204        let surface = container.controller.rootView.surface205        XCTAssertEqual(surface.layout.keys.first { $0.id == "return" }?.isEnabled, false)206        container.controller.debugTap("return")207        XCTAssertEqual(document.text, "", "a waiting return key does nothing")208        container.controller.debugTap("a")209        XCTAssertEqual(surface.layout.keys.first { $0.id == "return" }?.isEnabled, true)210        container.controller.debugTap("backspace")211        XCTAssertEqual(surface.layout.keys.first { $0.id == "return" }?.isEnabled, false)212        container.controller.debugTap("emoji")213        XCTAssertEqual(container.controller.mode, .emoji)214    }215}216
Files main
Menu

Theme