import Foundation import ParcelConfig import ParcelCore import ParcelEngine import ParcelIPC import Testing @Suite("Automatic tiling") @MainActor struct AutomaticTilingTests { @Test(arguments: [Command.setLayout(.rows), .retile, .resetSizes, .snap(.almostMaximize)], [false, true]) func explicitCommandsArrangeOnlyTheirWindowsWhileAutomaticTilingIsOff(command: Command, animated: Bool) async throws { let harness = try EngineHarness( configuration: engineConfiguration(glide: animated), initialState: engineInitialState(twoDisplays: true)) try await harness.start() try harness.add([1, 2]) let other = WorkspaceKey(display: secondEngineDisplay, space: SpaceID(rawValue: 2)) try harness.add([3], app: secondEngineApp, workspace: other) let focused = WindowID(rawValue: 1) harness.engine.send(.focusChanged(focused)) try harness.scheduler.runReady() try harness.finishMotion() harness.engine.setAutomaticTilingEnabled(false) #expect(harness.engine.snapshot.focusedWindow == focused) let disturbed = Rect(x: 70, y: 80, width: 410, height: 310) harness.world.userFrame(focused, frame: disturbed) harness.world.userFrame(WindowID(rawValue: 3), frame: disturbed) try harness.scheduler.runReady() let writes = harness.world.allWrites.count harness.world.appear(engineWindow(4, app: secondEngineApp), workspace: other) harness.engine.send(.focusChanged(focused)) harness.engine.send(.commandIssued(command)) try harness.scheduler.runReady() try harness.finishMotion() #expect(!harness.engine.isAutomaticTilingEnabled) #expect(harness.world.frames[focused] == harness.engine.snapshot.state.frames[focused]) #expect(harness.world.frames[focused] != disturbed) #expect(harness.world.frames[WindowID(rawValue: 3)] == disturbed) #expect(harness.world.frames[WindowID(rawValue: 4)] == initialEngineFrame) #expect(!harness.world.allWrites.dropFirst(writes).contains { [3, 4].contains($0.window.rawValue) }) let settledWrites = harness.world.allWrites.count harness.world.appear(engineWindow(5)) try harness.scheduler.runReady() try harness.finishMotion() #expect(harness.world.allWrites.count == settledWrites) #expect(harness.world.frames[WindowID(rawValue: 5)] == initialEngineFrame) await harness.engine.stop() } @Test(arguments: [Command.retile, .setLayout(.rows), .resetSizes], [false, true]) func explicitCommandsRepairMissedFrameNotificationsWithoutArrangingOtherDisplays(command: Command, animated: Bool) async throws { let harness = try EngineHarness( configuration: engineConfiguration(glide: animated), initialState: engineInitialState(twoDisplays: true)) try await harness.start() try harness.add([1, 2]) let other = WorkspaceKey(display: secondEngineDisplay, space: SpaceID(rawValue: 2)) try harness.add([3], app: secondEngineApp, workspace: other) let focused = WindowID(rawValue: 1) harness.engine.send(.focusChanged(focused)) harness.engine.send(.commandIssued(.setLayout(.rows))) try harness.scheduler.runReady() try harness.finishMotion() let arranged = try #require(harness.world.frames[focused]) harness.engine.setAutomaticTilingEnabled(false) let disturbed = Rect(x: 180, y: 200, width: 520, height: 360) harness.world.frames[focused] = disturbed harness.world.frames[WindowID(rawValue: 3)] = disturbed try harness.scheduler.runReady() try harness.finishMotion() #expect(harness.world.frames[focused] == disturbed) harness.engine.send(.commandIssued(command)) try harness.scheduler.runReady() try harness.finishMotion() #expect(harness.world.frames[focused] == arranged) #expect(harness.world.frames[WindowID(rawValue: 3)] == disturbed) #expect(!harness.engine.isAutomaticTilingEnabled) await harness.engine.stop() } @Test func workspaceMenuCommandDoesNotArrangeAnUnrelatedPendingArrivalWhileAutomaticTilingIsOff() async throws { let harness = try EngineHarness(initialState: engineInitialState(twoDisplays: true)) try await harness.start() try harness.add([1, 2]) harness.engine.setAutomaticTilingEnabled(false) let other = WorkspaceKey(display: secondEngineDisplay, space: SpaceID(rawValue: 2)) harness.world.appear(engineWindow(3, app: secondEngineApp), workspace: other) let writes = harness.world.allWrites.count harness.engine.send(.workspaceCommandIssued(.setLayout(.rows), workspace: engineWorkspace)) try harness.scheduler.runReady() try harness.finishMotion() #expect(harness.engine.snapshot.state.workspaces[engineWorkspace]?.layout == .rows) for id in [WindowID(rawValue: 1), WindowID(rawValue: 2)] { #expect(harness.world.frames[id] == harness.engine.snapshot.state.frames[id]) } #expect(harness.world.frames[WindowID(rawValue: 3)] == initialEngineFrame) #expect(!harness.world.allWrites.dropFirst(writes).contains { $0.window == WindowID(rawValue: 3) }) try await harness.engine.awaitWorkspaceSettled(in: engineWorkspace) #expect(!harness.engine.isAutomaticTilingEnabled) await harness.engine.stop() } @Test func engineIPCQueriesAutomaticTilingButLeavesItsSettingToTheAppShell() async throws { let harness = try EngineHarness() let handler = EngineIPCHandler(engine: harness.engine) try await harness.start() for enabled in [true, false] { harness.engine.setAutomaticTilingEnabled(enabled) guard case .object(let values) = try await handler.query(.config) else { Issue.record("Expected configuration object") return } #expect(values["automatic_tiling"] == .bool(enabled)) #expect(values["paused"] == nil) for action in [ { try await handler.run(command: "toggle-automatic-tiling", args: []) }, { _ = try await harness.engine.performConfigured(.toggleAutomaticTiling); return JSONValue.null }, ] { do { _ = try await action() Issue.record("Engine bypassed the app-owned Automatic tiling setting") } catch let error as IPCError { #expect(error.code == "shell_command") } #expect(harness.engine.isAutomaticTilingEnabled == enabled) } } await #expect(throws: IPCError.self) { try await handler.run(command: "toggle-automatic-tiling", args: ["extra"]) } await #expect(throws: IPCError.self) { try await handler.run(command: "pause-tiling", args: []) } await harness.engine.stop() } @Test func systemConflictNoticeClearsOnStopAndUpdatesAfterRestart() async throws { let harness = try EngineHarness() var notices: [SystemWindowConflict?] = [] harness.engine.onSystemWindowConflictChanged = { notices.append($0) } try await harness.start() harness.world.emit(.systemWindowConflictChanged(.stageManager)) #expect(notices == [.stageManager]) await harness.engine.stop() #expect(notices == [.stageManager, nil]) #expect(harness.engine.systemWindowConflict == nil) try await harness.engine.start() harness.world.emit(.systemWindowConflictChanged(.nativeTiling)) #expect(notices == [.stageManager, nil, .nativeTiling]) await harness.engine.stop() } @Test func displaySuspensionBlocksOnlyThatDisplayAndRespectsAutomaticTilingOff() async throws { let harness = try EngineHarness(initialState: engineInitialState(twoDisplays: true)) try await harness.start() harness.world.emit(.suspendedDisplaysChanged([engineDisplay])) try harness.add([1]) #expect(harness.world.allWrites.isEmpty) #expect(harness.world.focused.isEmpty) let other = WorkspaceKey(display: secondEngineDisplay, space: SpaceID(rawValue: 2)) try harness.add([2], app: secondEngineApp, workspace: other) #expect(harness.world.allWrites.map(\.window) == [WindowID(rawValue: 2)]) #expect(harness.engine.suspendedDisplays == [engineDisplay]) harness.engine.setAutomaticTilingEnabled(false) harness.world.emit(.suspendedDisplaysChanged([])) try harness.scheduler.runReady() #expect(harness.world.allWrites.map(\.window) == [WindowID(rawValue: 2)]) #expect(!harness.engine.isAutomaticTilingEnabled) harness.engine.setAutomaticTilingEnabled(true) try harness.scheduler.runReady() try harness.finishMotion() #expect( harness.world.frames[WindowID(rawValue: 1)] == harness.engine.snapshot.state.frames[WindowID(rawValue: 1)]) await harness.engine.stop() } @Test(arguments: [false, true]) func displaySuspensionCancelsMotionAndDrainsInFlightWritesRegardlessOfAutomaticTiling(automatic: Bool) async throws { let harness = try EngineHarness(configuration: engineConfiguration(glide: true)) try await harness.start() try harness.add([1, 2]) try harness.finishMotion() harness.engine.setAutomaticTilingEnabled(automatic) harness.world.deferWrites = true harness.engine.send(.workspaceCommandIssued(.setLayout(.rows), workspace: engineWorkspace)) try harness.scheduler.advance(by: 0.05) harness.links.tickAll(at: harness.clock.now) #expect(harness.world.pendingWriteCount > 0) let suspension = DisplaySuspension( display: engineDisplay, reason: .stageManager, since: Date(timeIntervalSinceReferenceDate: 0)) harness.world.emit(.displaySuspensionsChanged([suspension])) let writes = harness.world.allWrites.count while harness.world.pendingWriteCount > 0 { try harness.world.completeNextWrite() } try harness.finishMotion() #expect(harness.world.allWrites.count == writes) #expect(harness.engine.snapshot.displaySuspensions == [suspension]) #expect(harness.engine.suspendedDisplays == [engineDisplay]) #expect(harness.links.runningCount == 0) #expect(harness.engine.isIdle) await harness.engine.stop() } @Test func suspensionLimitsManualCommandsToOtherDisplaysAndDoesNotAutomaticallyArrangeOnResume() async throws { let harness = try EngineHarness(initialState: engineInitialState(twoDisplays: true)) try await harness.start() try harness.add([1, 2]) let other = WorkspaceKey(display: secondEngineDisplay, space: SpaceID(rawValue: 2)) try harness.add([3, 4], app: secondEngineApp, workspace: other) try harness.finishMotion() harness.engine.setAutomaticTilingEnabled(false) let suspension = DisplaySuspension( display: engineDisplay, reason: .spaceTransition, since: Date(timeIntervalSinceReferenceDate: 0)) harness.world.emit(.displaySuspensionsChanged([suspension])) let firstFrames = harness.world.frames.filter { [1, 2].contains($0.key.rawValue) } let writes = harness.world.allWrites.count harness.engine.send(.workspaceCommandIssued(.setLayout(.rows), workspace: engineWorkspace)) harness.engine.send(.workspaceCommandIssued(.setLayout(.rows), workspace: other)) try harness.scheduler.runReady() try harness.finishMotion() #expect(harness.world.frames.filter { [1, 2].contains($0.key.rawValue) } == firstFrames) #expect(harness.world.allWrites.dropFirst(writes).allSatisfy { [3, 4].contains($0.window.rawValue) }) for id in [WindowID(rawValue: 3), WindowID(rawValue: 4)] { #expect(harness.world.frames[id] == harness.engine.snapshot.state.frames[id]) } #expect(harness.engine.snapshot.displaySuspensions == [suspension]) let beforeResume = harness.world.allWrites.count harness.world.emit(.displaySuspensionsChanged([])) try harness.scheduler.runReady() try harness.finishMotion() #expect(harness.world.allWrites.count == beforeResume) #expect(harness.engine.snapshot.displaySuspensions.isEmpty) harness.engine.send(.workspaceCommandIssued(.retile, workspace: engineWorkspace)) try harness.scheduler.runReady() try harness.finishMotion() for id in [WindowID(rawValue: 1), WindowID(rawValue: 2)] { #expect(harness.world.frames[id] == harness.engine.snapshot.state.frames[id]) } #expect(!harness.engine.isAutomaticTilingEnabled) await harness.engine.stop() } @Test func automaticTilingOffKeepsObservationsAndConfigurationThenEnablesWithSnap() async throws { let harness = try EngineHarness(configuration: engineConfiguration(glide: true)) try await harness.start() try harness.add([1, 2]) harness.engine.setAutomaticTilingEnabled(false) #expect(!harness.engine.isAutomaticTilingEnabled) #expect(harness.links.runningCount == 0) let count = harness.world.allWrites.count let window = WindowID(rawValue: 2) let moved = Rect(x: 220, y: 150, width: 560, height: 700) harness.world.userFrame(window, frame: moved) var configuration = harness.engine.configuration configuration.layout.global.defaultLayout = .rows try harness.engine.applyConfiguration(configuration) try harness.scheduler.runReady() try harness.finishMotion() #expect(harness.world.allWrites.count == count) #expect(harness.engine.snapshot.state.observedFrames[window] == moved) #expect(harness.engine.configuration == configuration) #expect(harness.engine.isIdle) harness.engine.setAutomaticTilingEnabled(true) try harness.scheduler.runReady() #expect(harness.engine.isAutomaticTilingEnabled) #expect(harness.links.runningCount == 0) #expect(harness.world.frames == harness.engine.snapshot.state.frames) #expect(harness.engine.isIdle) harness.engine.send(.commandIssued(.setLayout(.side))) try harness.scheduler.runReady() #expect(harness.links.runningCount == 1) try harness.finishMotion() await harness.engine.stop() } @Test func explicitAnimatedResumeGlidesFromObservedFrames() async throws { let harness = try EngineHarness(configuration: engineConfiguration(glide: true)) harness.engine.setAutomaticTilingEnabled(false) try await harness.start() try harness.add([1, 2]) #expect(harness.world.allWrites.isEmpty) harness.engine.setAutomaticTilingEnabled(true, animatedResume: true) try harness.scheduler.runReady() #expect(harness.engine.isAutomaticTilingEnabled) #expect(harness.links.runningCount == 1) #expect(harness.world.frames != harness.engine.snapshot.state.frames) try harness.finishMotion() #expect(harness.world.frames == harness.engine.snapshot.state.frames) #expect(harness.engine.isIdle) await harness.engine.stop() } @Test func animatedResumeStillRespectsDisabledMotion() async throws { var configuration = engineConfiguration(glide: true) configuration.motion.enabled = false let harness = try EngineHarness(configuration: configuration) harness.engine.setAutomaticTilingEnabled(false) try await harness.start() try harness.add([1, 2]) harness.engine.setAutomaticTilingEnabled(true, animatedResume: true) try harness.scheduler.runReady() #expect(harness.links.runningCount == 0) #expect(harness.world.frames == harness.engine.snapshot.state.frames) await harness.engine.stop() } @Test func disablingAutomaticTilingDrainsDispatchedWritesButNeverSendsQueuedMotion() async throws { let harness = try EngineHarness(configuration: engineConfiguration(glide: true)) harness.world.deferWrites = true try await harness.start() try harness.add([1, 2]) try harness.scheduler.advance(by: 0.05) harness.links.tickAll(at: harness.clock.now) harness.engine.setAutomaticTilingEnabled(false) #expect(!harness.engine.isIdle) let writes = harness.world.allWrites.count try harness.world.completeNextWrite() try harness.finishMotion() #expect(harness.world.allWrites.count == writes) #expect(harness.world.pendingWriteCount == 0) #expect(harness.scheduler.pendingCount == 0) #expect(harness.engine.isIdle) harness.engine.setAutomaticTilingEnabled(true) try harness.scheduler.runReady() try harness.world.completeNextWrite() #expect(harness.world.frames == harness.engine.snapshot.state.frames) #expect(harness.engine.isIdle) await harness.engine.stop() } @Test func disablingAutomaticTilingCancelsRateLimitedFinalWrite() async throws { let harness = try EngineHarness() try await harness.start() try harness.add([1, 2]) harness.engine.send(.commandIssued(.setLayout(.rows))) try harness.scheduler.runReady() #expect(harness.scheduler.pendingCount == 1) harness.engine.setAutomaticTilingEnabled(false) let writes = harness.world.allWrites.count try harness.scheduler.advance(by: 1) #expect(harness.world.allWrites.count == writes) #expect(harness.scheduler.pendingCount == 0) #expect(harness.engine.isIdle) await harness.engine.stop() } @Test func automaticTilingSwitchIsIdempotentAndCanBeSetBeforeStart() async throws { let harness = try EngineHarness() harness.engine.setAutomaticTilingEnabled(false) harness.engine.setAutomaticTilingEnabled(false) try await harness.start() try harness.add([1, 2]) #expect(harness.world.allWrites.isEmpty) harness.engine.setAutomaticTilingEnabled(true) let scheduled = harness.scheduler.scheduledCount harness.engine.setAutomaticTilingEnabled(true) #expect(harness.scheduler.scheduledCount == scheduled) try harness.scheduler.runReady() #expect(harness.world.frames == harness.engine.snapshot.state.frames) #expect(harness.engine.isIdle) await harness.engine.stop() } }