Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303import Foundationimport ParcelConfigimport ParcelEngineimport ParcelUIimport Testing
@testable import ParcelApp@testable import ParcelPlatform
@MainActor @Suite struct AppShortcutCaptureTests { @Test func startupDefersMenuReadsUntilTheEngineOwnsApplications() async throws { @MainActor final class Readiness { var ready = false var reads = 0 } let readiness = Readiness() let symbolic = SymbolicHotKey(id: 79, isEnabled: true, hotKey: nil) let environment = AppShortcutEnvironment( frontmostPID: 42, hasPermission: { true }, voiceOverEnabled: { false }, readSymbolicHotKeys: { [symbolic] }, readMenuShortcuts: { _ in readiness.reads += 1; return [] }, canReadMenus: { readiness.ready }) #expect(try await environment.read().symbolicHotKeys == [symbolic]) #expect(readiness.reads == 0) readiness.ready = true _ = try await environment.read() #expect(readiness.reads == 1) }
@Test func registeredShortcutGoesToRecorderWithoutExecutingOrRepeating() { let chord = KeyChord(modifiers: [.ctrl, .alt], key: .left) let key = SettingsShortcutChecker.hotkey(chord) let capture = AppShortcutCapture() var recorded: [KeyChord] = [] var recording = true let record: (KeyChord) -> Bool = { chord in guard recording else { return false } recorded.append(chord) recording = false return true } #expect(capture.consume(HotKeyPress(hotKey: key, isRepeat: false), chord: chord, record: record)) #expect(recorded == [chord]) #expect(capture.consume(HotKeyPress(hotKey: key, isRepeat: true), chord: chord, record: record)) #expect(recorded == [chord]) #expect(!capture.consume(HotKeyPress(hotKey: key, isRepeat: false), chord: chord, record: record)) }
@Test func shortcutEnvironmentKeepsSuccessfulChecksAndVoiceOverState() async throws { let symbolic = SymbolicHotKey(id: 79, isEnabled: true, hotKey: HotKey(keyCode: 123, modifiers: .control)) let menu = ParcelPlatform.MenuShortcut( pid: 42, path: ["File", "Open"], character: "o", hotKey: HotKey(keyCode: 31, modifiers: .command)) let environment = makeEnvironment( voiceOver: true, symbolic: { [symbolic] }, menus: { pid in #expect(pid == 42) return [menu] }) let result = try await environment.read() #expect(result.symbolicHotKeys == [symbolic]) #expect(result.menuShortcuts == [menu]) #expect(result.voiceOverEnabled) #expect(result.symbolicHotKeysChecked) #expect(result.limitations.isEmpty) }
@Test func missingAccessibilityIsGuidanceNotAMenuRead() async throws { var reads = 0 let environment = makeEnvironment( permission: false, menus: { _ in reads += 1; return [] }) await #expect(throws: InputError.accessibilityRequired) { try await environment.read() } #expect(reads == 0) }
@Test func noRunningAppsIsSilentAndDoesNotReadMenus() async throws { var reads = 0 let environment = makeEnvironment( pid: nil, menus: { _ in reads += 1; return [] }) let result = try await environment.read() #expect(result.menuShortcuts.isEmpty) #expect(result.limitations.isEmpty) #expect(reads == 0) }
@Test func malformedMenuDataIsSkippedWithoutLosingSystemShortcuts() async throws { let symbolic = SymbolicHotKey(id: 79, isEnabled: true, hotKey: nil) let environment = makeEnvironment( symbolic: { [symbolic] }, menus: { _ in throw InputError.unexpectedAccessibilityValue("AXMenuItemCmdVirtualKey") }) let result = try await environment.read() #expect(result.symbolicHotKeys == [symbolic]) #expect(result.menuShortcuts.isEmpty) #expect(result.limitations.isEmpty) }
@Test func malformedSystemShortcutDoesNotPreventTheMenuCheck() async throws { var reads = 0 let environment = makeEnvironment( symbolic: { throw InputError.malformedSymbolicHotKey("raw-secret") }, menus: { _ in reads += 1 return [] }) let result = try await environment.read() #expect(reads == 1) #expect(!result.symbolicHotKeysChecked) #expect( result.limitations == ["System shortcuts could not be checked: A system shortcut contains invalid data."]) }
@Test func aChangedOnlyAppIsSkippedRatherThanCancellingTheCheck() async throws { let environment = makeEnvironment(menus: { _ in throw EngineMenuShortcutError.applicationChanged }) let result = try await environment.read() #expect(result.menuShortcuts.isEmpty) #expect(result.limitations.isEmpty) }
@Test func aChangedAppDoesNotPreventCheckingOtherApps() async throws { var reads: [Int32] = [] let environment = makeEnvironment( menus: { pid in reads.append(pid) if pid == 42 { throw EngineMenuShortcutError.applicationChanged } return [] }, applications: { [.init(pid: 42, name: "Changed"), .init(pid: 43, name: "Readable")] }) let result = try await environment.read() #expect(reads == [42, 43]) #expect(result.limitations.isEmpty) }
@Test( arguments: [nil, .unexpectedAccessibilityValue("AXTitle"), .menuTraversalLimit] as [InputError?], [false, true]) func frontmostRaceCancelsSuccessfulAndFailedReadsEvenAfterReturningToTheSameApp( failure: InputError?, returnsToSameApp: Bool ) async { weak var subject: AppShortcutEnvironment? let environment = makeEnvironment(menus: { _ in let subject = try #require(subject) subject.applicationActivated(pid: 43) if returnsToSameApp { subject.applicationActivated(pid: 42) } if let failure { throw failure } return [] }) subject = environment await #expect(throws: CancellationError.self) { try await environment.read() } #expect(environment.applicationPID == (returnsToSameApp ? 42 : 43)) }
@Test(arguments: [false, true]) func cancellationFromEitherReaderPropagatesWithoutGuidance(fromMenu: Bool) async { let environment = makeEnvironment( symbolic: { if !fromMenu { throw CancellationError() } return [] }, menus: { _ in throw CancellationError() }) await #expect(throws: CancellationError.self) { try await environment.read() } }
@Test func alreadyCancelledReadDoesNotStartEitherReader() async { var symbolicReads = 0 var menuReads = 0 let environment = makeEnvironment( symbolic: { symbolicReads += 1; return [] }, menus: { _ in menuReads += 1; return [] }) let task = Task { try await environment.read() } task.cancel() await #expect(throws: CancellationError.self) { try await task.value } #expect(symbolicReads == 0) #expect(menuReads == 0) }
@Test func appActivationRetainsTheLastExternalAppAndNotifiesRefresh() { let environment = makeEnvironment() var changes = 0 environment.onApplicationChanged = { changes += 1 } environment.applicationActivated(pid: ProcessInfo.processInfo.processIdentifier) #expect(environment.applicationPID == 42) #expect(changes == 0) environment.applicationActivated(pid: 43) #expect(environment.applicationPID == 43) #expect(changes == 1) }
private func makeEnvironment( pid: Int32? = 42, permission: Bool = true, voiceOver: Bool = false, symbolic: @escaping @MainActor () async throws -> [SymbolicHotKey] = { [] }, menus: @escaping @MainActor (Int32) async throws -> [ParcelPlatform.MenuShortcut] = { _ in [] }, applications: (@MainActor () -> [AppShortcutEnvironment.Application])? = nil ) -> AppShortcutEnvironment { AppShortcutEnvironment( frontmostPID: pid, hasPermission: { permission }, voiceOverEnabled: { voiceOver }, readSymbolicHotKeys: symbolic, readMenuShortcuts: menus, applications: applications) }
@Test(arguments: [ InputError.accessibility(pid: 42, attribute: "AXChildren", cause: .other(-25200)), .accessibility(pid: 42, attribute: "AXChildren", cause: .timedOut), .unexpectedAccessibilityValue("AXChildren"), .menuTraversalLimit, ]) func unreadableAppDoesNotHideLaterConflicts(failure: InputError) async throws { var reads: [Int32] = [] let symbolic = SymbolicHotKey(id: 79, isEnabled: true, hotKey: nil) let chord = try #require(ParcelConfiguration.defaults.keybindings[.swapLeft]) let menu = ParcelPlatform.MenuShortcut( pid: 43, path: ["Previous"], hotKey: SettingsShortcutChecker.hotkey(chord)) let environment = makeEnvironment( symbolic: { [symbolic] }, menus: { pid in reads.append(pid) if pid == 42 { throw failure } return [menu] }, applications: { [.init(pid: 42, name: "Finder"), .init(pid: 43, name: "Readable")] }) let result = try await environment.read() #expect(reads == [42, 43]) #expect(result.menuShortcuts == [menu]) #expect(result.symbolicHotKeys == [symbolic]) #expect(result.limitations.isEmpty) let check = TourShortcutCheck(configuration: .defaults, environment: result) #expect(check.limitations.isEmpty) #expect(check.warnings.contains { $0.chord == chord }) }
@Test func menuFreeReadableAppIsStillACompletedCheckAndDuplicatePIDsAreSkipped() async throws { var reads: [Int32] = [] let environment = makeEnvironment( pid: nil, menus: { pid in reads.append(pid); return [] }, applications: { [.init(pid: 43, name: "App"), .init(pid: 43, name: "App")] }) #expect(try await environment.read().limitations.isEmpty) #expect(reads == [43]) }
@Test func everyUnreadableAppIsSilentlySkipped() async throws { var reads: [Int32] = [] let environment = makeEnvironment( menus: { pid in reads.append(pid); throw InputError.menuTraversalLimit }, applications: { [.init(pid: 42, name: "First"), .init(pid: 43, name: "Second")] }) let result = try await environment.read() #expect(result.menuShortcuts.isEmpty) #expect(result.limitations.isEmpty) #expect(TourShortcutCheck(configuration: .defaults, environment: result).warnings.isEmpty) #expect(reads == [42, 43]) }
@Test(arguments: [ InputError.accessibilityRequired, .accessibility(pid: 42, attribute: "AXChildren", cause: .notPermitted), ]) func permissionFailuresStopTheScanRatherThanSkipAnApp(failure: InputError) async { var reads: [Int32] = [] let environment = makeEnvironment( menus: { pid in reads.append(pid); throw failure }, applications: { [.init(pid: 42, name: "First"), .init(pid: 43, name: "Second")] }) await #expect(throws: InputError.accessibilityRequired) { try await environment.read() } #expect(reads == [42]) }
@Test func permissionLostAfterAReadableAppFailsTheCheck() async { var permission = true let environment = AppShortcutEnvironment( frontmostPID: 42, hasPermission: { permission }, voiceOverEnabled: { false }, readSymbolicHotKeys: { [] }, readMenuShortcuts: { _ in permission = false; return [] }) await #expect(throws: InputError.accessibilityRequired) { try await environment.read() } }
@Test func differentKeyRepeatsAreNotSuppressed() { let capture = AppShortcutCapture() let first = KeyChord(modifiers: .cmd, key: .a) let next = KeyChord(modifiers: .cmd, key: .b) #expect( capture.consume( HotKeyPress(hotKey: SettingsShortcutChecker.hotkey(first), isRepeat: false), chord: first, record: { _ in true })) #expect( !capture.consume( HotKeyPress(hotKey: SettingsShortcutChecker.hotkey(next), isRepeat: true), chord: next, record: { _ in false })) }}