hex/ios/HEXMobileTests/KeyboardLayoutTests.swift
Ryan Vogel 359b7c4d9e
iOS: dictate from the keyboard without opening Hex, plus suggestions, autocorrect, and Paste (#1999)
* feat(ios): match iOS 26 Liquid Glass keyboard geometry, palette, and Hex key controls

* feat(ios): add 3-slot suggestion strip, cautious autocorrect, and typing gestures

* test(ios): cover iOS 26 keyboard metrics, autocorrect, gestures, and hosted stage

* iOS: move Hex for iPhone to Anomaly's Apple team

(cherry picked from commit 09061502ff)

* Cut Hex Mobile TestFlight betas with a dedicated release tag

Connect the Anomaly app record and pin the mobile package graph. Cut ios-beta prereleases from dev at the scheduled windows or on dispatch, skipping unchanged mobile inputs instead of creating desktop releases or allocating build numbers locally. Prepare the generated project for the Cloud archive after clone.

Verified actionlint, duplicate and desktop-only skips, strict SwiftPM resolution, and an unsigned Release device archive containing the app, keyboard, and widgets. First Xcode Cloud onboarding and signed distribution are still pending.

* Dictate inside the iOS keyboard without opening Hex (probe)

When no warm session is running, the keyboard now records and transcribes on its own instead of opening Hex. iOS refuses to record from a keyboard extension directly (audiomxd: extension lacks the record-audio entitlement), so capture runs through getUserMedia in a hidden WKWebView, whose media process records under Hex's microphone permission. PCM is streamed to Swift, written to a WAV file, and transcribed with on-device Apple Speech (SpeechTranscriber), downloading its assets from the keyboard on first use. Steps and memory are logged to the App Group.

Replaces the cold-start handoff that opened Hex and returned to the host app. Not gated yet: needs an Experiments toggle before it ships. Verified on an iPhone 17 Pro (iOS 27.2) with Hex quit: dictation in Mail/Notes inserted text; the keyboard's footprint was about 58 MB with 118 MB headroom. Unsigned simulator tests were not run.

* Regenerate the keyboard Info.plist with its microphone and speech descriptions

XcodeGen writes these from project.yml; the previous commit changed project.yml without the generated plist. Verified by building for the device.

* Start in-keyboard dictation from the full keyboard's Hex key

Holding the Hex key without a warm session opened Hex through the cold-start handoff; it now records and transcribes inside the keyboard when that path is available, and only falls back to opening Hex otherwise. Verified it builds and installs on the iPhone 17 Pro.

* Dictate with a tap on the Hex key and the bar's Hex mark

The full keyboard's Hex key ignored taps and needed a 0.35 s hold; a tap now dictates too (sliding still opens the style menu). The dictation bar's Hex mark opened Hex through the cold-start handoff; with no warm session it now starts in-keyboard dictation instead. Removes the stray-tap-does-nothing guard because Ryan wants the key to act like a dictation button.

Verified HexKeyGestureTests (5 passed) in the iPhone 18 Pro simulator and installed on the iPhone 17 Pro.

* Offer Paste and one-time-code help in the full keyboard's strip

When something was copied in the last 3 minutes and not yet pasted from the keyboard, the empty strip shows the system Paste button (UIPasteControl), which hands the clipboard over only on tap, so iOS never shows its paste prompt; nothing reads the clipboard before then. In one-time-code fields the strip also offers switching keyboards, because iOS gives codes from Messages and Mail only to its own keyboard. Copies seen and pasted are tracked by changeCount in the App Group. UIPasteControl's configuration is immutable, so a palette change replaces the control (setting colors on it raised NSInternalInconsistencyException in FullKeyboardEngineTests).

Verified the full HEXMobileTests suite passes on the iPhone 18 Pro simulator and the build installs on the iPhone 17 Pro; the Paste button itself is not yet tried on the device.

* Describe in-keyboard dictation in the iOS README

The status paragraph said the keyboard never touches the microphone; it now records through WebKit and transcribes with Apple Speech when no warm session runs. The cold-start handoff remains only before iOS 26.

---------

Co-authored-by: Kit Langton <kit.langton@gmail.com>
2026-11-19 12:20:12 -05:00

224 lines
10 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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))
}
}
}
}
return layouts
}
func testPortraitIs253PointsAndLandscapeIs176Points() {
for width in portraitWidths {
XCTAssertEqual(KeyboardLayout(width: width).totalHeight, 253, "width \(width)")
}
XCTAssertEqual(KeyboardLayout.height(for: .portrait), 253)
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, 35, 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.keysAreaFrame
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 testBalloonsStayInsideThe253PointKeyboard() {
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() {
XCTAssertEqual(bottomRow(KeyboardLayout(width: 393)), [.layerSwitch(.numbers), .space, .hex, .returnKey])
XCTAssertEqual(
bottomRow(KeyboardLayout(width: 375, showsGlobe: true)),
[.layerSwitch(.numbers), .globe, .space, .hex, .returnKey]
)
XCTAssertEqual(
bottomRow(KeyboardLayout(width: 393, layer: .numbers, field: .email)),
[.layerSwitch(.letters), .space, .character("@"), .character("."), .hex, .returnKey]
)
XCTAssertEqual(
bottomRow(KeyboardLayout(width: 393, field: .url)),
[.layerSwitch(.numbers), .space, .character("."), .character("/"), .hex, .returnKey]
)
}
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: - 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),
]
}
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)"
}
}
@MainActor
final class FullKeyboardSettingsTests: XCTestCase {
func testFullKeyboardIsOffAndAutocorrectOnByDefault() throws {
let suite = "FullKeyboardSettingsTests-\(UUID().uuidString)"
let defaults = try XCTUnwrap(UserDefaults(suiteName: suite))
defer { defaults.removePersistentDomain(forName: suite) }
let settings = FullKeyboardSettings(defaults: defaults)
XCTAssertFalse(settings.isEnabled)
XCTAssertTrue(settings.autocorrect)
settings.applyLaunchArguments(["HEX"])
XCTAssertFalse(settings.isEnabled)
settings.applyLaunchArguments(["HEX", FullKeyboardSettings.launchArgument])
XCTAssertTrue(settings.isEnabled)
settings.autocorrect = false
XCTAssertFalse(FullKeyboardSettings(defaults: defaults).autocorrect)
}
func testNoAppGroupReadsAsOff() {
XCTAssertFalse(FullKeyboardSettings(defaults: nil).isEnabled)
}
}