import Foundation import Testing @testable import ParcelCore struct FocusModeTests { private let display = DisplayID(rawValue: "focus-display") private var key: WorkspaceKey { WorkspaceKey(display: display, space: SpaceID(rawValue: 1)) } private func id(_ number: UInt32) -> WindowID { WindowID(rawValue: number) } private func populated(cap: Int = 3, count: UInt32 = 3) -> State { let screen = Display(id: display, frame: Rect(x: 100, y: 50, width: 1200, height: 800)) var state = State(displays: [screen], activeSpaces: [display: key.space]) state.workspaces[key] = Workspace( key: key, layout: .side, ratio: .twoThirds, gaps: Gaps(inner: 8, outer: Insets(12)), reservedEdges: Insets(top: 20, left: 30), windowCap: cap) for number in 1...count { let window = ManagedWindow(id: id(number), app: AppID(pid: 42, bundleID: "org.example.fixture")) state = reduce(state, .windowAppeared(window, workspace: key, frame: Rect())).0 } return reduce(state, .focusChanged(id(1))).0 } private func twoWorkspaces(acrossDisplays: Bool, sourceActive: Bool = true) -> (State, WorkspaceKey) { var state = populated() let destinationDisplay = acrossDisplays ? DisplayID(rawValue: "destination-focus-display") : display let destination = WorkspaceKey(display: destinationDisplay, space: SpaceID(rawValue: 2)) if acrossDisplays { state.displays[destinationDisplay] = Display( id: destinationDisplay, frame: Rect(x: 1300, y: 50, width: 1200, height: 800)) state.activeSpaces[destinationDisplay] = destination.space } if !sourceActive { state.activeSpaces[display] = acrossDisplays ? SpaceID(rawValue: 3) : destination.space state.activeDisplay = destinationDisplay state.focusedWindow = id(10) } state.workspaces[destination] = Workspace( key: destination, layout: .columns, gaps: Gaps(inner: 10, outer: Insets(24)), order: [id(10), id(11)], focus: id(10)) for number: UInt32 in [10, 11] { state.windows[id(number)] = ManagedWindow( id: id(number), app: AppID(pid: 43, bundleID: "org.example.other")) } state.focusModes = [ key: FocusModes(focusedWindow: id(1), noPeek: true, cleanSpace: true), destination: FocusModes(focusedWindow: id(10), noPeek: true, cleanSpace: true), ] return (reduce(state, .relayout).0, destination) } @Test func focusFillsUsableAreaAndToggleRestoresExactTreeAndFrames() { let original = populated() let (focused, effects) = reduce(original, .commandIssued(.toggleFocus)) #expect(focused.activeFocusModes.focusedWindow == id(1)) #expect(focused.frames[id(1)] == Rect(x: 142, y: 82, width: 1146, height: 756)) #expect(focused.frames[id(2)] == original.frames[id(2)]) #expect(focused.workspaces == original.workspaces) #expect(focused.tileEdges.isEmpty) #expect(effects.contains(.raiseWindow(id(1)))) #expect(effects.contains(.announce(.focusModeChanged(.focus, enabled: true)))) let (restored, exits) = reduce(focused, .commandIssued(.toggleFocus)) #expect(restored.frames == original.frames) #expect(restored.workspaces == original.workspaces) #expect(restored.activeFocusModes == FocusModes()) #expect(restored.tileEdges == original.tileEdges) #expect(exits.contains(.announce(.focusModeChanged(.focus, enabled: false)))) } @Test(arguments: [ Command.setLayout(.columns), .cycleLayout, .setRatio(.half), .nudgeRatio(0.05), .resetSizes, .retile, .setWindowCap(2), .split(.horizontal), .balance, .rotate, .mirror(.vertical), .makePrimary, .swap(.right), .move(.right), .stack(.right), .join(.right), .unstack, .toggleFloat, .snap(.leftHalf), .moveToDisplay(.right), .toggleFullscreen, ]) func layoutAndWindowCommandsEndFocus(command: Command) { let state = reduce(populated(), .commandIssued(.toggleFocus)).0 let (result, effects) = reduce(state, .commandIssued(command)) #expect(result.activeFocusModes.focusedWindow == nil) #expect(effects.contains(.announce(.focusModeChanged(.focus, enabled: false)))) } @Test(arguments: [ Event.focusChanged(WindowID(rawValue: 2)), .windowMovedByUser(WindowID(rawValue: 1), frame: Rect(x: 200, y: 100, width: 500, height: 400)), .windowResizedByUser(WindowID(rawValue: 1), frame: Rect(width: 500, height: 400)), .windowDragBegan(WindowID(rawValue: 1)), .windowDisappeared(WindowID(rawValue: 1)), .windowDropped(source: WindowID(rawValue: 1), target: WindowID(rawValue: 2), stacking: false), ]) func observationTriggersEndFocus(event: Event) { let state = reduce(populated(), .commandIssued(.toggleFocus)).0 let result = reduce(state, event) #expect(result.0.activeFocusModes.focusedWindow == nil) #expect(result.1.contains(.announce(.focusModeChanged(.focus, enabled: false)))) } @Test func transientKeyboardFocusLossDoesNotUndoTheExpandedArrangement() { let state = reduce(populated(), .commandIssued(.toggleFocus)).0 let (unfocused, effects) = reduce(state, .focusChanged(nil)) #expect(unfocused.focusedWindow == nil) #expect(unfocused.activeFocusModes.focusedWindow == state.activeFocusModes.focusedWindow) #expect(unfocused.frames == state.frames) #expect(!effects.contains(.announce(.focusModeChanged(.focus, enabled: false)))) let resumed = reduce(unfocused, .focusChanged(id(1))).0 #expect(resumed.frames == state.frames) #expect(resumed.activeFocusModes.focusedWindow == id(1)) #expect(reduce(resumed, .focusChanged(id(2))).0.activeFocusModes.focusedWindow == nil) } @Test(arguments: [ WindowFacts(bundleID: "org.example.fixture", role: "AXSheet"), WindowFacts(bundleID: "org.example.fixture", role: "AXWindow", subrole: "AXDialog"), WindowFacts(bundleID: "org.example.fixture", role: "AXWindow", subrole: "AXFloatingWindow"), WindowFacts(bundleID: "org.example.fixture", role: "AXWindow", isModal: true), WindowFacts(bundleID: "org.example.fixture", role: "AXWindow", hasParent: true), ]) func auxiliaryKeyboardFocusKeepsTheExpandedDocumentAndAccordionSelection(facts: WindowFacts) throws { let accordion = reduce(populated(), .commandIssued(.setLayout(.accordion))).0 let selected = reduce(accordion, .focusChanged(id(2))).0 let original = reduce(selected, .commandIssued(.toggleFocus)).0 let app = try #require(original.windows[id(2)]?.app) let auxiliary = ManagedWindow( id: id(4), app: app, role: facts.role ?? "", subrole: facts.subrole, isFloating: true, facts: facts) let frame = Rect(x: 400, y: 150, width: 420, height: 180) let moved = Rect(x: 405, y: 160, width: 425, height: 185) let events: [(Event, WindowID?)] = [ (.windowAppeared(auxiliary, workspace: key, frame: frame), id(2)), (.focusChanged(auxiliary.id), auxiliary.id), (.windowFrameEdited(auxiliary.id, frame: moved), auxiliary.id), (.focusChanged(id(2)), id(2)), (.focusChanged(auxiliary.id), auxiliary.id), (.windowDisappeared(auxiliary.id), nil), (.focusChanged(id(2)), id(2)), ] var state = original for (event, keyboardFocus) in events { let (next, effects) = reduce(state, event) #expect(next.focusedWindow == keyboardFocus) #expect(next.activeFocusModes.focusedWindow == id(2)) #expect(next.workspaces[key]?.focus == id(2)) #expect(next.workspaces[key]?.tree == original.workspaces[key]?.tree) for (window, frame) in original.frames { #expect(next.frames[window] == frame) } #expect(frameWrites(effects).isEmpty) #expect(!effects.contains(.announce(.focusModeChanged(.focus, enabled: false)))) #expect(!effects.contains(.raiseWindow(auxiliary.id))) #expect(!effects.contains { if case .focusWindow = $0 { true } else { false } }) state = next } #expect(state.workspaces == original.workspaces) #expect(state.frames == original.frames) } @Test func auxiliaryFocusEndsExpansionWhenTheUserSwitchesApplicationsOrDocuments() throws { let state = reduce(populated(), .commandIssued(.toggleFocus)).0 let app = try #require(state.windows[id(1)]?.app) for owner in [app, AppID(pid: 7, bundleID: "org.example.other")] { let auxiliary = ManagedWindow(id: id(4), app: owner, role: "AXSheet", isFloating: true) let discovered = reduce(state, .windowAppeared(auxiliary, workspace: key, frame: Rect())).0 #expect(discovered.activeFocusModes.focusedWindow == id(1)) let active = reduce(discovered, .focusChanged(auxiliary.id)).0 #expect(active.activeFocusModes.focusedWindow == (owner == app ? id(1) : nil)) let switched = reduce(active, .focusChanged(id(2))).0 #expect(switched.activeFocusModes.focusedWindow == nil) #expect(switched.workspaces[key]?.focus == id(2)) } } @Test func repeatedFocusAndReconciliationDoNotEndFocusButNewWindowDoes() { let state = reduce(populated(), .commandIssued(.toggleFocus)).0 #expect(reduce(state, .focusChanged(id(1))).0.activeFocusModes.focusedWindow == id(1)) let existing = state.windows[id(2)]! #expect( reduce(state, .windowAppeared(existing, workspace: key, frame: state.frames[id(2)]!)).0.activeFocusModes .focusedWindow == id(1)) #expect(reduce(state, .relayout).0.activeFocusModes.focusedWindow == id(1)) let newcomer = ManagedWindow(id: id(4), app: existing.app, isFloating: true) let result = reduce(state, .windowAppeared(newcomer, workspace: key, frame: Rect(width: 400, height: 300))) #expect(result.0.activeFocusModes.focusedWindow == nil) #expect(result.0.frames[id(1)] == populated().frames[id(1)]) } @Test func refreshingAnExplicitlyTiledWindowUsesItsEffectiveDispositionForFocus() throws { let float = reduce(populated(), .commandIssued(.toggleFloat)).0 let tiled = reduce(float, .commandIssued(.toggleFloat)).0 let expanded = reduce(tiled, .commandIssued(.toggleFocus)).0 var observed = try #require(expanded.windows[id(1)]) observed.isFloating = true observed.managementReason = .automatic observed.subrole = "AXFloatingWindow" let refreshed = reduce(expanded, .windowAppeared(observed, workspace: key, frame: Rect())).0 #expect(refreshed.windows[id(1)]?.isFloating == false) #expect(refreshed.activeFocusModes.focusedWindow == id(1)) #expect(refreshed.frames == expanded.frames) observed.managementReason = .rule let reclassified = reduce(refreshed, .windowAppeared(observed, workspace: key, frame: Rect())).0 #expect(reclassified.windows[id(1)]?.isFloating == true) #expect(reclassified.activeFocusModes.focusedWindow == nil) #expect(reclassified.workspaces[key]?.tree?.contains(id(1)) == false) } @Test func nativeSpaceSwitchPreservesModesAndHiddenStackSelectionOnReturn() { var state = populated(cap: 1) state = reduce(state, .commandIssued(.toggleFocus)).0 state = reduce(state, .commandIssued(.toggleNoPeek)).0 state = reduce(state, .commandIssued(.toggleCleanSpace)).0 let other = WorkspaceKey(display: display, space: SpaceID(rawValue: 2)) state = reduce(state, .activeSpaceChanged(display: display, space: other.space)).0 #expect(state.activeFocusModes == FocusModes()) let newWindow = ManagedWindow(id: id(10), app: AppID(pid: 43, bundleID: "org.example.other")) state = reduce(state, .windowAppeared(newWindow, workspace: other, frame: Rect())).0 #expect(state.modes(for: key) == FocusModes(focusedWindow: id(1), noPeek: true, cleanSpace: true)) state = reduce(state, .activeSpaceChanged(display: display, space: key.space)).0 #expect(state.activeFocusModes == FocusModes(focusedWindow: id(1), noPeek: true, cleanSpace: true)) #expect(state.focusedWindow == id(1)) #expect(state.workspaces[key]?.tree?.visibleWindows == [id(1)]) #expect(state.frames[id(1)] == Rect(x: 142, y: 82, width: 1146, height: 756)) state = reduce(state, .commandIssued(.focusNextInStack)).0 #expect(state.activeFocusModes.focusedWindow == nil) } @Test func privacyTogglesAreIndependentOfFocusAndLayoutChanges() { var state = populated() for command in [Command.toggleFocus, .toggleNoPeek, .toggleCleanSpace] { state = reduce(state, .commandIssued(command)).0 } #expect(state.activeFocusModes == FocusModes(focusedWindow: id(1), noPeek: true, cleanSpace: true)) state = reduce(state, .commandIssued(.setLayout(.rows))).0 #expect(state.activeFocusModes == FocusModes(noPeek: true, cleanSpace: true)) state = reduce(state, .commandIssued(.toggleNoPeek)).0 #expect(state.activeFocusModes == FocusModes(cleanSpace: true)) state = reduce(state, .commandIssued(.toggleCleanSpace)).0 #expect(state.activeFocusModes == FocusModes()) } @Test func suspensionClearsCleanSpaceAcrossInactiveSpacesWithoutClearingNoPeek() { var state = populated() state = reduce(state, .commandIssued(.toggleCleanSpace)).0 state = reduce(state, .commandIssued(.toggleNoPeek)).0 let other = WorkspaceKey(display: display, space: SpaceID(rawValue: 2)) state.focusModes?[other] = FocusModes(noPeek: true, cleanSpace: true) let result = reduce(state, .sessionSuspended([display])) #expect(result.0.activeFocusModes == FocusModes(noPeek: true)) #expect(result.0.modes(for: other) == FocusModes(noPeek: true)) #expect(result.1.filter { $0 == .announce(.focusModeChanged(.cleanSpace, enabled: false)) }.count == 2) } @Test func disconnectClearsOnlyDisconnectedDisplayModes() { var state = populated() state = reduce(state, .commandIssued(.toggleFocus)).0 state = reduce(state, .commandIssued(.toggleCleanSpace)).0 let otherDisplay = DisplayID(rawValue: "other") let other = WorkspaceKey(display: otherDisplay, space: SpaceID(rawValue: 9)) state.focusModes?[other] = FocusModes(cleanSpace: true) let result = reduce(state, .displaySetChanged([])).0 #expect(result.modes(for: key) == FocusModes()) #expect(result.modes(for: other).cleanSpace) } @Test func invalidObservationDoesNotEndFocus() { let state = reduce(populated(), .commandIssued(.toggleFocus)).0 let result = reduce(state, .windowMovedByUser(id(1), frame: Rect(x: .nan))) #expect(result.0 == state) #expect(result.1 == [.report(.invalidGeometry)]) } @Test func focusRejectsFloatingMissingFocusAndOversizedWindows() { let state = populated() let floating = reduce(state, .commandIssued(.toggleFloat)).0 #expect(reduce(floating, .commandIssued(.toggleFocus)).1 == [.report(.notTiled(id(1)))]) var missing = state missing.focusedWindow = nil #expect(reduce(missing, .commandIssued(.toggleFocus)).1 == [.report(.noFocusedWindow)]) #expect(reduce(State(), .commandIssued(.toggleFocus)).1 == [.report(.noActiveWorkspace)]) var oversized = state oversized.windows[id(1)]?.minimum = LearnedMinimumSize(size: Size(width: 2000, height: 2000)) #expect(reduce(oversized, .commandIssued(.toggleFocus)).1.contains(.report(.cannotFit([id(1)])))) #expect(reduce(oversized, .commandIssued(.toggleFocus)).0.activeFocusModes.focusedWindow == nil) } @Test func switchingWindowsAcrossDisplaysEndsOnlyThePreviousFocus() { var state = populated() let second = DisplayID(rawValue: "second-focus-display") let secondKey = WorkspaceKey(display: second, space: SpaceID(rawValue: 2)) state.displays[second] = Display(id: second, frame: Rect(x: 1300, y: 50, width: 1200, height: 800)) state.activeSpaces[second] = secondKey.space let window = ManagedWindow(id: id(10), app: AppID(pid: 50, bundleID: "org.example.other")) state = reduce(state, .windowAppeared(window, workspace: secondKey, frame: Rect())).0 state = reduce(state, .focusChanged(id(1))).0 state = reduce(state, .commandIssued(.toggleFocus)).0 state = reduce(state, .commandIssued(.toggleNoPeek)).0 state = reduce(state, .focusChanged(id(10))).0 #expect(state.modes(for: key).focusedWindow == nil) #expect(state.modes(for: key).noPeek) #expect(state.activeFocusModes == FocusModes()) } @Test func edgeGestureRestoresFocusBeforeResizing() throws { let original = populated() let edge = try #require(original.tileEdges.first) let focused = reduce(original, .commandIssued(.toggleFocus)).0 let resized = reduce(focused, .tileEdgeResized(edge, delta: 20)).0 #expect(resized.activeFocusModes.focusedWindow == nil) #expect(resized.frames == reduce(original, .tileEdgeResized(edge, delta: 20)).0.frames) let reset = reduce(focused, .tileEdgeReset(edge)).0 #expect(reset.activeFocusModes.focusedWindow == nil) } @Test(arguments: [Command.toggleNoPeek, .toggleCleanSpace]) func privacyModesRejectMissingWorkspaceOrFocusButCanAlwaysTurnOff(command: Command) { #expect(reduce(State(), .commandIssued(command)).1 == [.report(.noActiveWorkspace)]) let missing = reduce(populated(), .focusChanged(nil)).0 #expect(reduce(missing, .commandIssued(command)).1 == [.report(.noFocusedWindow)]) var enabled = reduce(populated(), .commandIssued(command)).0 enabled = reduce(enabled, .focusChanged(nil)).0 #expect(reduce(enabled, .commandIssued(command)).0.activeFocusModes == FocusModes()) } @Test func modesRoundTripAndOldStateDecodesWithModesOff() throws { var state = populated() state = reduce(state, .commandIssued(.toggleFocus)).0 state = reduce(state, .commandIssued(.toggleNoPeek)).0 state = reduce(state, .commandIssued(.toggleCleanSpace)).0 #expect(try JSONDecoder().decode(State.self, from: JSONEncoder().encode(state)) == state) state.focusModes = nil let data = try JSONEncoder().encode(state) #expect(try JSONDecoder().decode(State.self, from: data).activeFocusModes == FocusModes()) } @Test(arguments: [false, true], [false, true]) func reappearingWindowEndsSourceAndDestinationFocus(acrossDisplays: Bool, sourceActive: Bool) throws { for number: UInt32 in [1, 2] { let (state, destination) = twoWorkspaces(acrossDisplays: acrossDisplays, sourceActive: sourceActive) let window = try #require(state.windows[id(number)]) let frame = Rect(x: 1400, y: 150, width: 400, height: 300) let event = Event.windowAppeared(window, workspace: destination, frame: frame) var unfocused = state unfocused.focusModes?[key]?.focusedWindow = nil unfocused.focusModes?[destination]?.focusedWindow = nil let expected = reduce(unfocused, event).0 let (result, effects) = reduce(state, event) #expect(result.workspace(containing: window.id) == destination) #expect(result.workspaces[key]?.order.contains(window.id) == false) #expect(result.modes(for: key) == FocusModes(noPeek: true, cleanSpace: true)) #expect(result.modes(for: destination) == FocusModes(noPeek: true, cleanSpace: true)) #expect(result.workspaces == expected.workspaces) #expect(result.frames == expected.frames) #expect(result.tileEdges == expected.tileEdges) #expect(effects.filter { $0 == .announce(.focusModeChanged(.focus, enabled: false)) }.count == 2) if !result.isActive(destination) { #expect(result.frames[window.id] == frame) #expect(!frameWrites(effects).contains { $0.window == window.id }) } } } @Test func movingToInactiveDisplayDoesNotWriteTheSourceFocusFrame() throws { var (state, destination) = twoWorkspaces(acrossDisplays: true) state.activeSpaces[destination.display] = SpaceID(rawValue: 3) state = reduce(state, .relayout).0 let window = try #require(state.windows[id(1)]) let frame = Rect(x: 1500, y: 200, width: 400, height: 300) let (result, effects) = reduce(state, .windowAppeared(window, workspace: destination, frame: frame)) #expect(result.modes(for: key).focusedWindow == nil) #expect(result.modes(for: destination).focusedWindow == nil) #expect(result.frames[window.id] == frame) #expect(result.layoutFrames().frames[window.id] == nil) #expect(!frameWrites(effects).contains { $0.window == window.id }) #expect(effects.filter { $0 == .announce(.focusModeChanged(.focus, enabled: false)) }.count == 2) } @Test(arguments: [false, true]) func transferEndsBothWorkspaceFocusModes(acrossDisplays: Bool) { var (state, destination) = twoWorkspaces(acrossDisplays: acrossDisplays) var effects: [Effect] = [] state.transfer(id(1), to: destination, effects: &effects) #expect(state.workspace(containing: id(1)) == destination) #expect(state.modes(for: key) == FocusModes(noPeek: true, cleanSpace: true)) #expect(state.modes(for: destination) == FocusModes(noPeek: true, cleanSpace: true)) #expect(effects.filter { $0 == .announce(.focusModeChanged(.focus, enabled: false)) }.count == 2) #expect(!reduce(state, .relayout).1.contains(.announce(.focusModeChanged(.focus, enabled: false)))) } @Test func transferToSameWorkspaceLeavesFocusUnchanged() { let original = reduce(populated(), .commandIssued(.toggleFocus)).0 var state = original var effects: [Effect] = [] state.transfer(id(1), to: key, effects: &effects) #expect(state == original) #expect(effects.isEmpty) } @Test(arguments: [Command.moveToDisplay(.right), .move(.right), .swap(.right), .stack(.right)]) func crossDisplayCommandsEndBothAffectedFocusModes(command: Command) { var (state, destination) = twoWorkspaces(acrossDisplays: true) state = reduce(state, .focusChanged(id(3))).0 state = reduce(state, .commandIssued(.toggleFocus)).0 let (result, effects) = reduce(state, .commandIssued(command)) #expect(result.workspace(containing: id(3)) == destination) #expect(result.modes(for: key) == FocusModes(noPeek: true, cleanSpace: true)) #expect(result.modes(for: destination) == FocusModes(noPeek: true, cleanSpace: true)) #expect(effects.filter { $0 == .announce(.focusModeChanged(.focus, enabled: false)) }.count == 2) } @Test func workspaceMovePreservesUnrelatedInactiveFocusModes() throws { var (state, destination) = twoWorkspaces(acrossDisplays: true) let unrelated = WorkspaceKey(display: display, space: SpaceID(rawValue: 9)) let window = ManagedWindow(id: id(20), app: AppID(pid: 44, bundleID: "org.example.third")) state.windows[window.id] = window state.workspaces[unrelated] = Workspace(key: unrelated, order: [window.id], focus: window.id) let modes = FocusModes(focusedWindow: window.id, noPeek: true, cleanSpace: true) state.focusModes?[unrelated] = modes let moved = try #require(state.windows[id(1)]) let result = reduce(state, .windowAppeared(moved, workspace: destination, frame: Rect())).0 #expect(result.modes(for: key).focusedWindow == nil) #expect(result.modes(for: destination).focusedWindow == nil) #expect(result.modes(for: unrelated) == modes) } @Test func rejectedWorkspaceMovePreservesBothFocusModes() throws { let (state, destination) = twoWorkspaces(acrossDisplays: true) let window = try #require(state.windows[id(1)]) let (result, effects) = reduce(state, .windowAppeared(window, workspace: destination, frame: Rect(x: .nan))) #expect(result == state) #expect(effects == [.report(.invalidGeometry)]) } @Test(arguments: [false, true]) func staleFocusForWindowInAnotherWorkspaceDoesNotOverrideItsLayout(acrossDisplays: Bool) throws { var (state, destination) = twoWorkspaces(acrossDisplays: acrossDisplays) state.workspaces[key]?.remove(id(1)) var receiving = try #require(state.workspaces[destination]) let screen = try #require(state.displays[destination.display]?.frame) receiving.insert(id(1), floating: false, in: screen, windows: state.windows) state.workspaces[destination] = receiving state.focusModes?[destination]?.focusedWindow = nil state.windows[id(1)]?.minimum = LearnedMinimumSize(size: Size(width: 2000, height: 1000)) var unfocused = state unfocused.focusModes?[key]?.focusedWindow = nil #expect(state.layoutFrames() == unfocused.layoutFrames()) if !state.isActive(destination) { #expect(state.layoutFrames().frames[id(1)] == nil) } let (result, effects) = reduce(state, .relayout) #expect(result.modes(for: key) == FocusModes(noPeek: true, cleanSpace: true)) let repairedUnfocused = reduce(unfocused, .relayout).0 #expect(result.tileEdges == repairedUnfocused.tileEdges) #expect(result.frames == repairedUnfocused.frames) #expect(effects.contains(.announce(.focusModeChanged(.focus, enabled: false)))) } @Test(arguments: [false, true]) func staleFocusForUnassignedWindowDoesNotEmitFrame(hasWindowRecord: Bool) { var state = populated() let unassigned = id(99) if hasWindowRecord { state.windows[unassigned] = ManagedWindow( id: unassigned, app: AppID(pid: 42, bundleID: "org.example.fixture")) } state.focusModes = [key: FocusModes(focusedWindow: unassigned, noPeek: true, cleanSpace: true)] var unfocused = state unfocused.focusModes?[key]?.focusedWindow = nil #expect(state.layoutFrames() == unfocused.layoutFrames()) #expect(state.layoutFrames().frames[unassigned] == nil) let (result, effects) = reduce(state, .relayout) #expect(result.modes(for: key) == FocusModes(noPeek: true, cleanSpace: true)) #expect(result.tileEdges == unfocused.tileEdges) #expect(!frameWrites(effects).contains { $0.window == unassigned }) } @Test func staleFocusForMissingManagedWindowDoesNotEmitFrame() { var state = populated() state.windows[id(1)] = nil state.focusModes = [key: FocusModes(focusedWindow: id(1))] #expect(state.layoutFrames().frames[id(1)] == nil) #expect(reduce(state, .relayout).0.modes(for: key).focusedWindow == nil) } @Test(arguments: [false, true]) func staleFocusForFloatingWindowNeverEmitsFocusGeometry(hasStoredFrame: Bool) { var state = populated() state.windows[id(1)]?.isFloating = true state.workspaces[key]?.removeTile(id(1)) state.frames[id(1)] = hasStoredFrame ? Rect(x: 300, y: 200, width: 400, height: 300) : nil state.windows[id(1)]?.minimum = LearnedMinimumSize(size: Size(width: 2000, height: 1000)) state.focusModes = [key: FocusModes(focusedWindow: id(1), noPeek: true, cleanSpace: true)] var unfocused = state unfocused.focusModes?[key]?.focusedWindow = nil #expect(state.layoutFrames() == unfocused.layoutFrames()) #expect(state.layoutFrames().frames[id(1)] == state.frames[id(1)]) let (result, effects) = reduce(state, .relayout) #expect(result.modes(for: key) == FocusModes(noPeek: true, cleanSpace: true)) #expect(result.frames[id(1)] == state.frames[id(1)]) #expect(result.tileEdges == unfocused.tileEdges) #expect(effects.filter { $0 == .announce(.focusModeChanged(.focus, enabled: false)) }.count == 1) } @Test(arguments: [false, true]) func restoringFloatingStateClearsFocusWithoutStaleEdgeSuppression(sourceActive: Bool) throws { let frame = Rect(x: 300, y: 200, width: 400, height: 300) let floating = reduce( reduce(populated(), .commandIssued(.toggleFloat)).0, .windowMovedByUser(id(1), frame: frame) ).0 var state = reduce(floating, .commandIssued(.tileWindow(id(1)))).0 for command in [Command.toggleFocus, .toggleNoPeek, .toggleCleanSpace] { state = reduce(state, .commandIssued(command)).0 } if !sourceActive { state = reduce(state, .activeSpaceChanged(display: display, space: SpaceID(rawValue: 2))).0 } let restoredWindow = try #require(floating.windows[id(1)]) let restoredWorkspace = try #require(floating.workspaces[key]) state.windows[id(1)] = restoredWindow state.workspaces[key] = restoredWorkspace state.frames[id(1)] = frame var unfocused = state unfocused.focusModes?[key]?.focusedWindow = nil let (result, effects) = reduce(state, [.relayout, .relayout]) #expect(result.modes(for: key) == FocusModes(noPeek: true, cleanSpace: true)) #expect(result.windows[id(1)]?.isFloating == true) #expect(result.frames[id(1)] == frame) #expect(result.tileEdges == unfocused.tileEdges) #expect(effects[0].filter { $0 == .announce(.focusModeChanged(.focus, enabled: false)) }.count == 1) #expect(!effects[1].contains(.announce(.focusModeChanged(.focus, enabled: false)))) #expect(!reduce(result, .relayout).1.contains(.announce(.focusModeChanged(.focus, enabled: false)))) } @Test func snapRestoreToFloatingClearsFocusAndConsumesHistory() { let frame = Rect(x: 300, y: 200, width: 400, height: 300) var state = reduce(populated(), .commandIssued(.toggleFloat)).0 state = reduce(state, .windowMovedByUser(id(1), frame: frame)).0 state = reduce(state, .commandIssued(.snap(.leftHalf))).0 state = reduce(state, .commandIssued(.tileWindow(id(1)))).0 for command in [Command.toggleFocus, .toggleNoPeek, .toggleCleanSpace] { state = reduce(state, .commandIssued(command)).0 } #expect(state.snapHistory?[id(1)]?.previousFrame == frame) #expect(state.modes(for: key).focusedWindow == id(1)) let (result, effects) = reduce(state, .commandIssued(.snap(.restore))) #expect(result.modes(for: key) == FocusModes(noPeek: true, cleanSpace: true)) #expect(result.windows[id(1)]?.isFloating == true) #expect(result.frames[id(1)] == frame) #expect(result.snapHistory?[id(1)] == nil) #expect(result.workspaces[key]?.tree?.windows.contains(id(1)) == false) #expect(!result.tileEdges.isEmpty) #expect(effects.filter { $0 == .announce(.focusModeChanged(.focus, enabled: false)) }.count == 1) #expect(effects.contains(.announce(.floatingChanged(id(1), isFloating: true)))) #expect(frameWrites(effects).contains(WindowFrame(window: id(1), frame: frame))) } @Test func undoWithoutHistoryLeavesValidFocusUnchanged() { let state = reduce(populated(), .commandIssued(.toggleFocus)).0 let (result, effects) = reduce(state, .commandIssued(.undo)) #expect(result == state) #expect(effects == [.report(.noUndoHistory)]) } @Test func batchWorkspaceMoveRestoresLayoutOnceAndKeepsAnnouncementsOnItsEvent() throws { let (state, destination) = twoWorkspaces(acrossDisplays: true) let window = try #require(state.windows[id(1)]) let (result, effects) = reduce( state, [.windowAppeared(window, workspace: destination, frame: Rect()), .relayout]) #expect(result.modes(for: key).focusedWindow == nil) #expect(result.modes(for: destination).focusedWindow == nil) #expect(effects[0].filter { $0 == .announce(.focusModeChanged(.focus, enabled: false)) }.count == 2) #expect(!effects[1].contains(.announce(.focusModeChanged(.focus, enabled: false)))) #expect(effects.flatMap { $0 }.filter { if case .setFrames = $0 { true } else { false } }.count == 1) } @Test func batchExitAndAppearanceComputeOneRestoredLayout() { let state = reduce(populated(), .commandIssued(.toggleFocus)).0 let newcomer = ManagedWindow(id: id(4), app: AppID(pid: 42, bundleID: "org.example.fixture")) let result = reduce(state, [.windowAppeared(newcomer, workspace: key, frame: Rect()), .relayout]) #expect(result.0.activeFocusModes.focusedWindow == nil) #expect(result.1.flatMap { $0 }.filter { if case .setFrames = $0 { true } else { false } }.count == 1) } }