|
| 1 | +import Combine |
| 2 | +import Defaults |
| 3 | +import XCTest |
| 4 | + |
| 5 | +@testable import Leader_Key |
| 6 | + |
| 7 | +class KeyboardLayoutTests: XCTestCase { |
| 8 | + var controller: Controller! |
| 9 | + var cancellables: Set<AnyCancellable>! |
| 10 | + var userState: UserState! |
| 11 | + var userConfig: UserConfig! |
| 12 | + |
| 13 | + override func setUp() { |
| 14 | + super.setUp() |
| 15 | + cancellables = Set<AnyCancellable>() |
| 16 | + |
| 17 | + // Create test instances |
| 18 | + userConfig = UserConfig() |
| 19 | + userState = UserState(userConfig: userConfig) |
| 20 | + controller = Controller(userState: userState, userConfig: userConfig) |
| 21 | + |
| 22 | + // Reset to default state |
| 23 | + Defaults[.forceEnglishKeyboardLayout] = false |
| 24 | + } |
| 25 | + |
| 26 | + override func tearDown() { |
| 27 | + cancellables = nil |
| 28 | + controller = nil |
| 29 | + userState = nil |
| 30 | + userConfig = nil |
| 31 | + super.tearDown() |
| 32 | + } |
| 33 | + |
| 34 | + // Helper to create fake NSEvent for testing |
| 35 | + private func fakeEvent( |
| 36 | + keyCode: UInt16, characters: String, charactersIgnoringModifiers: String, |
| 37 | + modifierFlags: NSEvent.ModifierFlags = [] |
| 38 | + ) -> NSEvent { |
| 39 | + // This is a simplified mock - in real implementation we'd need to create a proper NSEvent |
| 40 | + // For now, we'll test the logic indirectly through Controller methods |
| 41 | + return NSEvent.keyEvent( |
| 42 | + with: .keyDown, |
| 43 | + location: NSPoint.zero, |
| 44 | + modifierFlags: modifierFlags, |
| 45 | + timestamp: 0, |
| 46 | + windowNumber: 0, |
| 47 | + context: nil, |
| 48 | + characters: characters, |
| 49 | + charactersIgnoringModifiers: charactersIgnoringModifiers, |
| 50 | + isARepeat: false, |
| 51 | + keyCode: keyCode |
| 52 | + )! |
| 53 | + } |
| 54 | + |
| 55 | + func testAZERTYLayoutWithForceEnglishDisabled() { |
| 56 | + Defaults[.forceEnglishKeyboardLayout] = false |
| 57 | + |
| 58 | + // Physical A key on AZERTY keyboard produces "q" |
| 59 | + let azertyAKey = fakeEvent(keyCode: 0x00, characters: "q", charactersIgnoringModifiers: "q") |
| 60 | + let result = controller.charForEvent(azertyAKey) |
| 61 | + |
| 62 | + XCTAssertEqual(result, "q", "Should respect AZERTY layout and return 'q' for physical A key") |
| 63 | + } |
| 64 | + |
| 65 | + func testAZERTYLayoutWithForceEnglishEnabled() { |
| 66 | + Defaults[.forceEnglishKeyboardLayout] = true |
| 67 | + |
| 68 | + // Physical A key on AZERTY keyboard - should force to English "a" |
| 69 | + let azertyAKey = fakeEvent(keyCode: 0x00, characters: "q", charactersIgnoringModifiers: "q") |
| 70 | + let result = controller.charForEvent(azertyAKey) |
| 71 | + |
| 72 | + XCTAssertEqual(result, "a", "Should force English layout and return 'a' for physical A key") |
| 73 | + } |
| 74 | + |
| 75 | + func testColemakLayoutWithForceEnglishDisabled() { |
| 76 | + Defaults[.forceEnglishKeyboardLayout] = false |
| 77 | + |
| 78 | + // Physical S key on Colemak produces "r" |
| 79 | + let colemakSKey = fakeEvent(keyCode: 0x01, characters: "r", charactersIgnoringModifiers: "r") |
| 80 | + let result = controller.charForEvent(colemakSKey) |
| 81 | + |
| 82 | + XCTAssertEqual(result, "r", "Should respect Colemak layout and return 'r' for physical S key") |
| 83 | + } |
| 84 | + |
| 85 | + func testColemakLayoutWithForceEnglishEnabled() { |
| 86 | + Defaults[.forceEnglishKeyboardLayout] = true |
| 87 | + |
| 88 | + // Physical S key on Colemak - should force to English "s" |
| 89 | + let colemakSKey = fakeEvent(keyCode: 0x01, characters: "r", charactersIgnoringModifiers: "r") |
| 90 | + let result = controller.charForEvent(colemakSKey) |
| 91 | + |
| 92 | + XCTAssertEqual(result, "s", "Should force English layout and return 's' for physical S key") |
| 93 | + } |
| 94 | + |
| 95 | + func testCaseSensitivityWithLayout() { |
| 96 | + Defaults[.forceEnglishKeyboardLayout] = false |
| 97 | + |
| 98 | + // Test lowercase |
| 99 | + let lowerR = fakeEvent(keyCode: 0x0F, characters: "r", charactersIgnoringModifiers: "r") |
| 100 | + let lowerResult = controller.charForEvent(lowerR) |
| 101 | + XCTAssertEqual(lowerResult, "r", "Should return lowercase 'r'") |
| 102 | + |
| 103 | + // Test uppercase with shift |
| 104 | + let upperR = fakeEvent( |
| 105 | + keyCode: 0x0F, characters: "R", charactersIgnoringModifiers: "R", modifierFlags: .shift) |
| 106 | + let upperResult = controller.charForEvent(upperR) |
| 107 | + XCTAssertEqual(upperResult, "R", "Should return uppercase 'R' with shift") |
| 108 | + |
| 109 | + XCTAssertNotEqual(lowerResult, upperResult, "Lowercase and uppercase should be different") |
| 110 | + } |
| 111 | + |
| 112 | + func testCaseSensitivityWithForceEnglish() { |
| 113 | + Defaults[.forceEnglishKeyboardLayout] = true |
| 114 | + |
| 115 | + // Test lowercase |
| 116 | + let lowerR = fakeEvent(keyCode: 0x0F, characters: "r", charactersIgnoringModifiers: "r") |
| 117 | + let lowerResult = controller.charForEvent(lowerR) |
| 118 | + XCTAssertEqual(lowerResult, "r", "Should return lowercase 'r' in force English mode") |
| 119 | + |
| 120 | + // Test uppercase with shift |
| 121 | + let upperR = fakeEvent( |
| 122 | + keyCode: 0x0F, characters: "R", charactersIgnoringModifiers: "R", modifierFlags: .shift) |
| 123 | + let upperResult = controller.charForEvent(upperR) |
| 124 | + XCTAssertEqual(upperResult, "R", "Should return uppercase 'R' with shift in force English mode") |
| 125 | + |
| 126 | + XCTAssertNotEqual( |
| 127 | + lowerResult, upperResult, "Lowercase and uppercase should be different in force English mode") |
| 128 | + } |
| 129 | + |
| 130 | + func testSpecialKeysAlwaysUseKeyMaps() { |
| 131 | + Defaults[.forceEnglishKeyboardLayout] = false |
| 132 | + |
| 133 | + // Arrow keys should always use KeyMaps regardless of layout setting |
| 134 | + let leftArrow = fakeEvent(keyCode: 0x7B, characters: "", charactersIgnoringModifiers: "") |
| 135 | + let result = controller.charForEvent(leftArrow) |
| 136 | + |
| 137 | + // Should return the KeyMaps entry for left arrow |
| 138 | + XCTAssertEqual(result, "←", "Special keys should always use KeyMaps") |
| 139 | + } |
| 140 | +} |
0 commit comments