| index 56f3c61..8f3a1d7 100644 |
| --- a/native/apple/Tests/SheetsKitTests/FirstKeystrokeTests.swift |
| +++ b/native/apple/Tests/SheetsKitTests/FirstKeystrokeTests.swift |
| @@ -1,5 +1,6 @@ |
| #if os(macOS) |
| import AppKit |
| +import Carbon |
| import SwiftUI |
| import XCTest |
| import RastrilloApple |
| @@ -212,6 +213,46 @@ final class FirstKeystrokeTests: XCTestCase { |
| XCTAssertEqual(server.value(100, 0), "x") |
| } |
| |
| + /// Review #72, finding 4: a layout's own dead key (Spanish ´, German ^) |
| + /// needs no Option and its key-down may carry no characters at all. The |
| + /// layouts are read with UCKeyTranslate, not selected, so the machine's |
| + /// keyboard is left alone: find each layout's unmodified dead keys, check |
| + /// they compose, and check such a key-down starts typing whatever AppKit |
| + /// puts in its character fields. |
| + @MainActor func testLayoutDeadKeysWithoutOptionStartTyping() throws { |
| + for id in ["com.apple.keylayout.Spanish-ISO", "com.apple.keylayout.German"] { |
| + let filter = [kTISPropertyInputSourceID as String: id] as CFDictionary |
| + guard let list = TISCreateInputSourceList(filter, true)?.takeRetainedValue() as? [TISInputSource], |
| + let source = list.first, |
| + let raw = TISGetInputSourceProperty(source, kTISPropertyUnicodeKeyLayoutData) else { |
| + XCTFail("\(id) is not installed"); continue |
| + } |
| + let data = Unmanaged<CFData>.fromOpaque(raw).takeUnretainedValue() as Data |
| + var deadKeys: [(UInt16, String)] = [] |
| + data.withUnsafeBytes { buffer in |
| + let layout = buffer.bindMemory(to: UCKeyboardLayout.self).baseAddress! |
| + func translate(_ code: UInt16, _ state: inout UInt32) -> String { |
| + var chars = [UniChar](repeating: 0, count: 4); var length = 0 |
| + UCKeyTranslate(layout, code, UInt16(kUCKeyActionDown), 0, UInt32(LMGetKbdType()), 0, &state, 4, &length, &chars) |
| + return String(utf16CodeUnits: chars, count: length) |
| + } |
| + for code in UInt16(0)..<UInt16(51) { |
| + var state: UInt32 = 0 |
| + guard translate(code, &state).isEmpty, state != 0 else { continue } |
| + deadKeys.append((code, translate(14, &state))) // then e |
| + } |
| + } |
| + XCTAssertFalse(deadKeys.isEmpty, "\(id) has an unmodified dead key") |
| + XCTAssertTrue(deadKeys.contains { ["é", "ê", "è"].contains($0.1) }, "\(id): a dead key then e composes (\(deadKeys))") |
| + for (code, _) in deadKeys { |
| + for ignoring in ["", "´", "^"] { |
| + let event = NSEvent.keyEvent(with: .keyDown, location: .zero, modifierFlags: [], timestamp: 0, windowNumber: 0, context: nil, characters: "", charactersIgnoringModifiers: ignoring, isARepeat: false, keyCode: code)! |
| + XCTAssertTrue(SheetTypeAhead.startsTyping(event), "\(id) dead key \(code) with ignoring \"\(ignoring)\"") |
| + } |
| + } |
| + } |
| + } |
| + |
| @MainActor func testWhichKeysStartTyping() { |
| func event(_ chars: String, _ ignoring: String, _ code: UInt16, _ flags: NSEvent.ModifierFlags = []) -> NSEvent { |
| NSEvent.keyEvent(with: .keyDown, location: .zero, modifierFlags: flags, timestamp: 0, windowNumber: 0, context: nil, characters: chars, charactersIgnoringModifiers: ignoring, isARepeat: false, keyCode: code)! |