Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179import ParcelCoreimport ParcelPlatformimport Testing
@testable import ParcelEngine
@MainActor @Suite struct NativeMenuShortcutTests { @Test func focusedExcludedWindowsStayInspectableOnTheOwnerThread() async throws { let harness = NativeHarness() var window = engineWindow(1) window.facts = WindowFacts(bundleID: engineApp.bundleID, role: "AXWindow", isFullScreen: true) harness.first.snapshots = [NativeWindowSnapshot(window: window, frame: initialEngineFrame, excluded: true)] try await harness.start() let reads = harness.first.focusReads let focused = try await harness.world.focusedInspectionWindow(pid: engineApp.pid) #expect(focused?.id == window.id) #expect(focused?.facts?.isFullScreen == true) #expect(harness.first.focusReads == reads + 1) #expect(harness.world.inspectionWindow(window.id)?.facts?.isFullScreen == true) #expect(try await harness.world.focusedInspectionWindow(pid: -1) == nil) harness.world.stop() #expect(harness.world.inspectionWindow(window.id) == nil) }
@Test func attachedApplicationUsesItsExistingOwner() async throws { let harness = NativeHarness() try await harness.start() defer { harness.world.stop() } let expected = MenuShortcut( pid: engineApp.pid, path: ["File", "Open"], hotKey: HotKey(keyCode: 31, modifiers: .command)) harness.first.shortcuts = [expected] #expect(try await harness.world.menuShortcuts(pid: engineApp.pid) == [expected]) #expect(harness.first.menuReads == 1) #expect(harness.backend.applicationRequests.filter { $0 == engineApp }.count == 1) }
@Test(arguments: [false, true]) func startupMenuReadNeverCompetesWithEngineOwnership(ownerBusy: Bool) async throws { let backend = FakeNativeBackend() let application = FakeNativeApplication() backend.apps[engineApp] = application if ownerBusy { backend.applicationError = AppThreadFailure.alreadyOwned(engineApp.pid) } let world = NativeEngineWorld( backend: backend, scheduler: ManualEngineScheduler(clock: ManualEngineClock()), readUnmanagedMenuShortcuts: { _ in Issue.record("A managed app must not create a temporary menu owner during startup") return [] }) try world.start { _ in } defer { world.stop() } if ownerBusy { await #expect(throws: EngineMenuShortcutError.unavailable) { try await world.menuShortcuts(pid: engineApp.pid) } } else { #expect(try await world.menuShortcuts(pid: engineApp.pid).isEmpty) #expect(application.menuReads == 1) } #expect(backend.applicationRequests == [engineApp]) }
@Test func readErrorsAndStoppedWorldAreSurfaced() async throws { let harness = NativeHarness() try await harness.start() harness.first.error = InputError.menuTraversalLimit await #expect(throws: InputError.menuTraversalLimit) { try await harness.world.menuShortcuts(pid: engineApp.pid) } harness.world.stop() await #expect(throws: EngineMenuShortcutError.unavailable) { try await harness.world.menuShortcuts(pid: engineApp.pid) } }
@Test func oneApplicationFailureDoesNotAffectAnotherOwner() async throws { let harness = NativeHarness() try await harness.start() defer { harness.world.stop() } harness.first.error = InputError.menuTraversalLimit let expected = MenuShortcut( pid: secondEngineApp.pid, path: ["File", "Open"], hotKey: HotKey(keyCode: 31, modifiers: .command)) harness.second.shortcuts = [expected] await #expect(throws: InputError.menuTraversalLimit) { try await harness.world.menuShortcuts(pid: engineApp.pid) } #expect(try await harness.world.menuShortcuts(pid: secondEngineApp.pid) == [expected]) #expect(harness.first.menuReads == 1) #expect(harness.second.menuReads == 1) }
@Test func setupCanReadMenusBeforeTheWorldStarts() async throws { let backend = FakeNativeBackend() let scheduler = ManualEngineScheduler(clock: ManualEngineClock()) var reads: [Int32] = [] let expected = MenuShortcut(pid: 42, path: ["Open"], hotKey: HotKey(keyCode: 31)) let world = NativeEngineWorld( backend: backend, scheduler: scheduler, readUnmanagedMenuShortcuts: { pid in reads.append(pid) return [expected] }) #expect(try await world.menuShortcuts(pid: 42) == [expected]) #expect(reads == [42]) #expect(backend.applicationRequests.isEmpty) world.stop() await #expect(throws: EngineMenuShortcutError.unavailable) { try await world.menuShortcuts(pid: 42) } #expect(reads == [42]) }
@Test func onePreStartFailureDoesNotPreventOtherMenuReads() async throws { let world = NativeEngineWorld( backend: FakeNativeBackend(), scheduler: ManualEngineScheduler(clock: ManualEngineClock()), readUnmanagedMenuShortcuts: { pid in if pid == 42 { throw InputError.menuTraversalLimit } return [MenuShortcut(pid: pid, path: ["Open"], hotKey: HotKey(keyCode: 31))] }) defer { world.stop() } await #expect(throws: InputError.menuTraversalLimit) { try await world.menuShortcuts(pid: 42) } #expect(try await world.menuShortcuts(pid: 43).map(\.pid) == [43]) }
@Test(arguments: [false, true]) func preStartReadCannotCrossAWorldLifetimeChange(start: Bool) async throws { var changeLifetime: (@MainActor () throws -> Void)? let world = NativeEngineWorld( backend: FakeNativeBackend(), scheduler: ManualEngineScheduler(clock: ManualEngineClock()), readUnmanagedMenuShortcuts: { _ in try changeLifetime?() return [] }) defer { world.stop() } changeLifetime = { if start { try world.start { _ in } } else { world.stop() } } await #expect(throws: EngineMenuShortcutError.applicationChanged) { try await world.menuShortcuts(pid: 42) } }
@Test func cancelledMenuRequestDoesNotReachTheReader() async { var reads = 0 let world = NativeEngineWorld( backend: FakeNativeBackend(), scheduler: ManualEngineScheduler(clock: ManualEngineClock()), readUnmanagedMenuShortcuts: { _ in reads += 1 return [] }) defer { world.stop() } let request = Task { try await world.menuShortcuts(pid: 42) } request.cancel() await #expect(throws: CancellationError.self) { try await request.value } #expect(reads == 0) }
@Test func worldStopInvalidatesAnAttachedOwnerRead() async throws { let harness = NativeHarness() try await harness.start() harness.first.onMenuRead = { harness.world.stop() } await #expect(throws: EngineMenuShortcutError.applicationChanged) { try await harness.world.menuShortcuts(pid: engineApp.pid) } }
@Test func applicationExitInvalidatesInFlightRead() async throws { let harness = NativeHarness() try await harness.start() defer { harness.world.stop() } harness.first.onMenuRead = { harness.backend.emit(.terminated(engineApp)) do { try await harness.drain() } catch { Issue.record(error) } } await #expect(throws: EngineMenuShortcutError.applicationChanged) { try await harness.world.menuShortcuts(pid: engineApp.pid) } }}