import CoreGraphics import ParcelConfig import ParcelEngine import ParcelPlatform import Testing @testable import ParcelCore private func mouseState(layout: Layout = .columns, count: UInt32 = 3, gap: Double = 0, cap: Int = 3) -> State { let ids = (1...count).map { WindowID(rawValue: $0) } var state = engineInitialState() state.workspaces[engineWorkspace] = Workspace( key: engineWorkspace, layout: layout, gaps: Gaps(inner: gap, outer: Insets()), windowCap: cap, order: ids, focus: ids.first) state.windows = Dictionary(uniqueKeysWithValues: ids.map { ($0, engineWindow($0.rawValue)) }) state.focusedWindow = ids.first return reduce(state, .relayout).0 } private func mouseStackedState() -> State { var state = mouseState(count: 4, cap: 2) let ids = (1...4).map { WindowID(rawValue: UInt32($0)) } state.workspaces[engineWorkspace]?.tree = .split( axis: .horizontal, fraction: 0.5, first: .tile(WindowStack(ids[0], others: [ids[2]])), second: .tile(WindowStack(ids[1], others: [ids[3]]))) return reduce(state, .relayout).0 } private struct MouseSequence { var model = MouseModel() var state: State var configuration = MouseConfiguration() var effects: [MouseEffect] = [] var observedFrames: [WindowID: Rect] = [:] var currentModifiers = HotKey.Modifiers() mutating func move(_ window: UInt32, to point: Point) throws { let id = WindowID(rawValue: window) var frame = try #require(state.frames[id]) frame.origin = point observedFrames[id] = frame } @discardableResult mutating func send( _ kind: DragSample.Kind, at point: Point, modifiers: HotKey.Modifiers? = nil, window: UInt32? = nil, clickCount: Int = 1, timestamp: Double = 0 ) -> [MouseEffect] { if let modifiers { currentModifiers = modifiers } let next = model.reduce( DragSample( kind: kind, location: CGPoint(x: point.xCoordinate, y: point.yCoordinate), modifiers: currentModifiers, clickCount: clickCount, timestamp: timestamp), windowAtStart: window.map { DragWindow(pid: engineApp.pid, windowNumber: $0) }, state: state, configuration: configuration, observedFrames: observedFrames) effects += next for effect in next { if case .event(let event) = effect { state = reduce(state, event).0 } } return next } } @Suite("Pure mouse gestures") struct MouseModelTests { @Test(arguments: [false, true], [false, true]) func nativeDropKeepsItsOccupantsWhenFocusChanges( stacked: Bool, focusChangesBeforePreview: Bool ) throws { let source = WindowID(rawValue: 2) let target = WindowID(rawValue: 1) let initial = reduce( stacked ? mouseStackedState() : mouseState(count: 2, cap: 2), .focusChanged(source) ).0 let destination = try #require(initial.frames[target]) var sequence = MouseSequence(state: initial) sequence.send(.down, at: try #require(initial.frames[source]).center, window: source.rawValue) try sequence.move(source.rawValue, to: destination.origin) let focusChanges: [Event] = [ .focusChanged(stacked ? WindowID(rawValue: 4) : source), .focusChanged(stacked ? WindowID(rawValue: 3) : target), ] if focusChangesBeforePreview { sequence.state = reduce(sequence.state, focusChanges).0 } let feedback = sequence.send(.dragged, at: destination.center) let preview = try #require( feedback.compactMap { effect -> DragLayoutPreview? in if case .feedback(.layoutPreview(let preview)) = effect { return preview } return nil }.last) #expect(preview.selectedTarget == 1) sequence.state = reduce(sequence.state, focusChanges).0 let release = sequence.send(.up, at: destination.center) let stacks = try #require(initial.workspaces[engineWorkspace]?.tree?.stacks) #expect(release.contains(.event(.gestureCommitted(.swapTiles(source: stacks[1], target: stacks[0]))))) #expect(sequence.state.frames[source] == initial.frames[target]) #expect(sequence.state.frames[target] == initial.frames[source]) #expect(sequence.state.workspaces[engineWorkspace]?.tree?.stacks.map(\.visible) == [source, target]) if stacked { for id in [WindowID(rawValue: 3), WindowID(rawValue: 4)] { let peer = id == WindowID(rawValue: 3) ? source : target #expect(sequence.state.frames[id] == initial.frames[peer]) } } #expect(sequence.state.undoHistory?.count(for: source) == 1) #expect(sequence.state.undoHistory?.count(for: target) == 1) let restored = reduce(sequence.state, .commandIssued(.undo)).0 #expect(restored.frames == initial.frames) #expect( restored.workspaces[engineWorkspace]?.tree?.stacks.map(\.windows) == initial.workspaces[engineWorkspace]?.tree?.stacks.map(\.windows)) #expect(restored.undoHistory?.count(for: source) == 0) } @Test func newlyLearnedMinimumRevalidatesTheDropInsteadOfCancellingTheGesture() throws { let initial = mouseStackedState() let source = WindowID(rawValue: 1) let target = WindowID(rawValue: 2) let destination = try #require(initial.frames[target]).center var sequence = MouseSequence(state: initial) sequence.send(.down, at: try #require(initial.frames[source]).center, window: source.rawValue) sequence.state.windows[WindowID(rawValue: 3)]?.minimum = LearnedMinimumSize(size: Size(width: 840, height: 300)) sequence.state = reduce(sequence.state, .relayout).0 sequence.observedFrames[source] = Rect(origin: destination, size: try #require(initial.frames[source]).size) let feedback = sequence.send(.dragged, at: destination) let preview = try #require( feedback.compactMap { effect -> DragLayoutPreview? in if case .feedback(.layoutPreview(let preview)) = effect { return preview } return nil }.last) #expect(preview.selectedTarget == 2) sequence.send(.up, at: destination) #expect(sequence.state.workspaces[engineWorkspace]?.tree?.stacks.first?.visible == target) #expect(sequence.state.frames[source]?.size.width == 840) #expect(preview.frames.allSatisfy { sequence.state.frames[$0.window] == $0.frame }) } @Test func nativeHitOwnsTheDragBeforeItsFocusNotificationArrives() throws { let initial = mouseStackedState() let source = WindowID(rawValue: 3) let target = WindowID(rawValue: 2) #expect(!initial.visibleWindows.contains(source)) let destination = try #require(initial.frames[target]).center var sequence = MouseSequence(state: initial) let down = sequence.send(.down, at: try #require(initial.frames[source]).center, window: source.rawValue) #expect(down == [.suspend(source)]) try sequence.move(source.rawValue, to: destination) sequence.send(.dragged, at: destination) sequence.send(.up, at: destination) #expect(sequence.state.frames[source] == initial.frames[target]) #expect(sequence.state.workspaces[engineWorkspace]?.tree?.stacks.map(\.visible) == [target, source]) #expect( sequence.state.workspaces[engineWorkspace]?.tree?.stacks.last?.windows == [WindowID(rawValue: 1), source]) } @Test func escapeAfterStackSelectionChangesDoesNotCommitTheDrop() throws { let initial = reduce(mouseStackedState(), .focusChanged(WindowID(rawValue: 2))).0 var sequence = MouseSequence(state: initial) let source = WindowID(rawValue: 2) let destination = try #require(initial.frames[WindowID(rawValue: 1)]) sequence.send(.down, at: try #require(initial.frames[source]).center, window: source.rawValue) try sequence.move(source.rawValue, to: destination.origin) sequence.send(.dragged, at: destination.center) sequence.state = reduce(sequence.state, .focusChanged(WindowID(rawValue: 3))).0 let beforeCancel = sequence.state sequence.send(.cancel, at: destination.center) let release = sequence.send(.up, at: destination.center) #expect(!release.contains { if case .event = $0 { true } else { false } }) #expect(sequence.state.workspaces == beforeCancel.workspaces) #expect(sequence.state.frames == initial.frames) #expect(sequence.state.undoHistory?.windows.isEmpty != false) } @Test func contentDragWithoutNativeFrameChangeDoesNotRearrange() throws { let initial = mouseState() var sequence = MouseSequence(state: initial) let start = try #require(initial.frames[WindowID(rawValue: 1)]).center let target = try #require(initial.frames[WindowID(rawValue: 3)]).center #expect(sequence.send(.down, at: start, window: 1) == [.suspend(WindowID(rawValue: 1))]) sequence.send(.dragged, at: target) let end = sequence.send(.up, at: target) #expect(end.contains(.resume(WindowID(rawValue: 1), dropping: true))) #expect(sequence.state == initial) #expect(!sequence.effects.contains { if case .event = $0 { true } else { false } }) #expect(!sequence.model.isActive) } @Test func plainNativeGroupSwapAdaptsWidthsToHiddenMemberMinimums() throws { let source = WindowID(rawValue: 1) let peer = WindowID(rawValue: 3) let target = WindowID(rawValue: 2) var initial = mouseStackedState() initial.displays[engineDisplay] = Display(id: engineDisplay, frame: Rect(width: 1488, height: 920)) initial.windows[peer]?.minimum = LearnedMinimumSize(size: Size(width: 840, height: 620)) initial.windows[target]?.minimum = LearnedMinimumSize(size: Size(width: 574, height: 400)) initial = reduce(initial, .relayout).0 let before = try #require(initial.workspaces[engineWorkspace]?.tree?.stacks) #expect(initial.frames[source]?.size.width == 840) #expect(initial.frames[target]?.size.width == 648) var sequence = MouseSequence(state: initial) let destination = try #require(initial.frames[target]).center sequence.send(.down, at: try #require(initial.frames[source]).center, window: source.rawValue) try sequence.move(source.rawValue, to: destination) let effects = sequence.send(.dragged, at: destination) let preview = try #require( effects.compactMap { effect -> DragLayoutPreview? in if case .feedback(.layoutPreview(let preview)) = effect { return preview } return nil }.last) #expect(preview.selectedTarget == 2) #expect(preview.frames.first { $0.window == source }?.frame.size.width == 840) #expect(preview.frames.first { $0.window == source }?.frame.minX == 648) let outline = try #require( effects.compactMap { effect -> WindowFrame? in if case .feedback(.targetChanged(let frame)) = effect { return frame } return nil }.last) #expect(outline.window == target) #expect(outline.frame == preview.frames.first { $0.window == source }?.frame) #expect(outline.frame != initial.frames[target]) #expect(sequence.state.workspaces == initial.workspaces) sequence.send(.up, at: destination) #expect(sequence.state.workspaces[engineWorkspace]?.tree?.stacks == [before[1], before[0]]) #expect(preview.frames.allSatisfy { sequence.state.frames[$0.window] == $0.frame }) #expect(sequence.state.frames[source] == sequence.state.frames[peer]) #expect(sequence.state.undoHistory?.count(for: peer) == 1) let restored = reduce(sequence.state, .commandIssued(.undo)).0 #expect(restored.frames == initial.frames) #expect(restored.workspaces[engineWorkspace]?.tree == initial.workspaces[engineWorkspace]?.tree) } @Test func plainNativeDragPreviewsThenCommitsExactlyOneSwap() throws { let initial = mouseState() var sequence = MouseSequence(state: initial) let source = WindowID(rawValue: 1) let target = WindowID(rawValue: 3) let end = try #require(initial.frames[target]).center sequence.send(.down, at: try #require(initial.frames[source]).center, window: 1) try sequence.move(1, to: end) let effects = sequence.send(.dragged, at: end) let preview = try #require( effects.compactMap { effect -> DragLayoutPreview? in if case .feedback(.layoutPreview(let value)) = effect { return value } return nil }.first) #expect(preview.targets.filter { if case .tile = $0.kind { true } else { false } }.count == 3) #expect(preview.selectedTarget == 3) #expect(sequence.state == initial) sequence.send(.up, at: end) #expect(preview.frames.allSatisfy { sequence.state.frames[$0.window] == $0.frame }) #expect(sequence.state.frames[source] == initial.frames[target]) #expect(sequence.state.undoHistory?.count(for: source) == 1) let restored = reduce(sequence.state, .commandIssued(.undo)).0 #expect(restored.frames == initial.frames) } @Test(arguments: [false, true]) func nativeResizeIsProvisionalAndCornerClampsAnUnavailableAxis(corner: Bool) throws { let initial = mouseState(count: 2) var sequence = MouseSequence(state: initial) let source = WindowID(rawValue: 1) let start = try #require(initial.frames[source]) sequence.send(.down, at: start.center, window: 1) sequence.observedFrames[source] = Rect( x: start.minX, y: start.minY, width: start.size.width + 40, height: start.size.height - (corner ? 40 : 0)) let end = Point(x: start.center.xCoordinate + 40, y: start.center.yCoordinate) let effects = sequence.send(.dragged, at: end) #expect(sequence.state == initial) #expect(!effects.contains { if case .writeFrame = $0 { true } else { false } }) sequence.send(.up, at: end) #expect(sequence.state.frames[source]?.size.width == start.size.width + 40) #expect(sequence.state.frames[source]?.size.height == start.size.height) #expect(sequence.state.undoHistory?.count(for: source) == 1) #expect(reduce(sequence.state, .commandIssued(.undo)).0.frames == initial.frames) } @Test func nativeCornerResizeAdjustsBothAvailableAxesInOneTransaction() throws { var initial = mouseState(layout: .grid, count: 4) let ids = (1...4).map { WindowID(rawValue: UInt32($0)) } initial.workspaces[engineWorkspace] = Workspace( key: engineWorkspace, layout: .grid, windowCap: 4, order: ids, focus: ids.first) initial = reduce(initial, .relayout).0 var sequence = MouseSequence(state: initial) let source = WindowID(rawValue: 4) let start = try #require(initial.frames[source]) let end = Point(x: start.maxX - 30, y: start.maxY - 40) sequence.send(.down, at: Point(x: start.maxX - 1, y: start.maxY - 1), window: 4) sequence.observedFrames[source] = Rect( x: start.minX, y: start.minY, width: start.size.width - 30, height: start.size.height - 40) let feedback = sequence.send(.dragged, at: end) #expect(sequence.state == initial) let preview = try #require( feedback.compactMap { effect -> DragLayoutPreview? in if case .feedback(.layoutPreview(let preview)) = effect { return preview } return nil }.first) let expected = try #require(preview.frames.first { $0.window == source }) #expect(expected.frame.size.width == start.size.width - 30) #expect(expected.frame.size.height == start.size.height - 40) sequence.send(.up, at: end) #expect(sequence.state.frames[source] == expected.frame) #expect(sequence.state.undoHistory?.count(for: source) == 1) #expect(reduce(sequence.state, .commandIssued(.undo)).0.frames == initial.frames) } @Test func releasingOutsideTheShownDestinationDoesNotCommitAStaleDrop() throws { let initial = mouseState() var sequence = MouseSequence(state: initial) let source = WindowID(rawValue: 1) let target = try #require(initial.frames[WindowID(rawValue: 3)]).center sequence.send(.down, at: try #require(initial.frames[source]).center, window: 1) try sequence.move(1, to: target) sequence.send(.dragged, at: target) sequence.send(.up, at: Point(x: -50, y: -50)) #expect(sequence.state == initial) } @Test(arguments: [DragSample.Kind.cancel, .interrupted]) func nativePreviewCancellationNeverCommits(kind: DragSample.Kind) throws { let initial = mouseState() var sequence = MouseSequence(state: initial) let end = try #require(initial.frames[WindowID(rawValue: 3)]).center sequence.send(.down, at: try #require(initial.frames[WindowID(rawValue: 1)]).center, window: 1) try sequence.move(1, to: end) sequence.send(.dragged, at: end) #expect(sequence.send(kind, at: end).contains(.feedback(.layoutPreview(nil)))) sequence.send(.up, at: end) #expect(sequence.state == initial) } @Test func changedTopologyInvalidatesStoredPreview() throws { var sequence = MouseSequence(state: mouseState()) let source = WindowID(rawValue: 1) let target = WindowID(rawValue: 3) let end = try #require(sequence.state.frames[target]).center sequence.send(.down, at: try #require(sequence.state.frames[source]).center, window: 1) try sequence.move(1, to: end) sequence.send(.dragged, at: end) sequence.state = reduce(sequence.state, .commandIssued(.setLayout(.rows))).0 let changed = sequence.state #expect(sequence.send(.up, at: end).contains(.feedback(.layoutPreview(nil)))) #expect(sequence.state == changed) } @Test func nativeResizeBeyondMinimumCannotCommitAStaleFeasiblePreview() throws { var initial = mouseState(count: 2) let source = WindowID(rawValue: 1) let neighbor = WindowID(rawValue: 2) initial.windows[neighbor]?.minimum = LearnedMinimumSize(size: Size(width: 400, height: 100)) initial = reduce(initial, .relayout).0 var sequence = MouseSequence(state: initial) let start = try #require(initial.frames[source]) sequence.send(.down, at: start.center, window: 1) for delta in [20.0, 1000.0] { sequence.observedFrames[source] = Rect( x: start.minX, y: start.minY, width: start.size.width + delta, height: start.size.height) sequence.send(.dragged, at: Point(x: start.center.xCoordinate + delta, y: start.center.yCoordinate)) } sequence.send(.up, at: Point(x: start.center.xCoordinate + 1000, y: start.center.yCoordinate)) #expect(sequence.state == initial) } @Test func fullHeightPreviewCommitsAtomicallyAndUndoRestoresLayout() throws { let initial = mouseState(layout: .grid, count: 4, cap: 4) var sequence = MouseSequence(state: initial) let source = WindowID(rawValue: 4) let screen = try #require(initial.displays[engineDisplay]).frame let end = Point(x: screen.center.xCoordinate, y: screen.minY + 58) sequence.send(.down, at: try #require(initial.frames[source]).center, window: 4) try sequence.move(4, to: end) let effects = sequence.send(.dragged, at: end) let preview = try #require( effects.compactMap { effect -> DragLayoutPreview? in if case .feedback(.layoutPreview(let value)) = effect { return value } return nil }.first) #expect(preview.targets.first { $0.id == preview.selectedTarget }?.kind == .fullHeight) #expect(sequence.state == initial) sequence.send(.up, at: end) #expect(sequence.state.workspaces[engineWorkspace]?.layout == .side) #expect(sequence.state.workspaces[engineWorkspace]?.tree?.tileCount == 3) #expect(sequence.state.frames[source]?.size.height == screen.size.height) #expect(preview.frames.allSatisfy { sequence.state.frames[$0.window] == $0.frame }) #expect(reduce(sequence.state, .relayout).0.frames == sequence.state.frames) #expect(sequence.state.undoHistory?.count(for: source) == 1) #expect(reduce(sequence.state, .commandIssued(.undo)).0.frames == initial.frames) } @Test(arguments: [false, true]) func swapAndStackUseChosenNonAdjacentTile(stacking: Bool) throws { let initial = mouseState() var sequence = MouseSequence(state: initial) let source = WindowID(rawValue: 1) let target = WindowID(rawValue: 3) let start = try #require(initial.frames[source]).center let end = try #require(initial.frames[target]).center let modifiers: HotKey.Modifiers = stacking ? [.command, .option] : .command sequence.send(.down, at: start, modifiers: modifiers, window: 1) try sequence.move(1, to: end) let changed = sequence.send(.dragged, at: end, timestamp: 1) #expect( changed.contains( .feedback(.targetChanged(WindowFrame(window: target, frame: try #require(initial.frames[target])))))) sequence.send(.up, at: end, timestamp: 2) #expect(sequence.effects.contains(.event(.windowDropped(source: source, target: target, stacking: stacking)))) #expect(sequence.effects.contains(.feedback(.dropped(velocity: Point())))) if stacking { let stack = try #require( sequence.state.workspaces[engineWorkspace]?.tree?.stacks.first { $0.windows.contains(source) }) #expect(stack.windows == [target, source]) #expect(stack.visible == source) #expect(sequence.state.frames[source] == sequence.state.frames[target]) } else { #expect(sequence.state.frames[source] == initial.frames[target]) #expect(sequence.state.frames[target] == initial.frames[source]) #expect(sequence.state.frames[WindowID(rawValue: 2)] == initial.frames[WindowID(rawValue: 2)]) } } @Test func targetFollowsCursorAndClearsOutsideWorkspace() throws { let initial = mouseState() var sequence = MouseSequence(state: initial) let start = try #require(initial.frames[WindowID(rawValue: 1)]).center sequence.send(.down, at: start, modifiers: .command, window: 1) try sequence.move(1, to: Point(x: 300, y: 100)) for id: UInt32 in [3, 2] { let frame = try #require(initial.frames[WindowID(rawValue: id)]) #expect( sequence.send(.dragged, at: frame.center).contains( .feedback(.targetChanged(WindowFrame(window: WindowID(rawValue: id), frame: frame))))) } #expect(sequence.send(.dragged, at: Point(x: -50, y: 100)).contains(.feedback(.targetChanged(nil)))) sequence.send(.up, at: Point(x: -50, y: 100)) #expect(sequence.state == initial) } @Test func configurableSwapModifiersAndPlainDragKeepTheSameTarget() throws { let initial = mouseState() var sequence = MouseSequence(state: initial) sequence.configuration = MouseConfiguration(swapModifiers: .control, stackModifiers: [.control, .shift]) let start = try #require(initial.frames[WindowID(rawValue: 1)]).center let target = try #require(initial.frames[WindowID(rawValue: 3)]).center sequence.send(.down, at: start, modifiers: [], window: 1) try sequence.move(1, to: target) sequence.send(.dragged, at: target, modifiers: .control) #expect( sequence.effects.contains( .feedback( .targetChanged( WindowFrame( window: WindowID(rawValue: 3), frame: try #require(initial.frames[WindowID(rawValue: 3)])))) )) #expect( sequence.send(.modifiersChanged, at: target, modifiers: []).contains { if case .feedback(.layoutPreview(let preview)) = $0 { return preview?.selectedTarget == 3 } return false }) #expect( sequence.send(.modifiersChanged, at: target, modifiers: .control).contains( .feedback( .targetChanged( WindowFrame( window: WindowID(rawValue: 3), frame: try #require(initial.frames[WindowID(rawValue: 3)])))) )) sequence.send(.up, at: target, modifiers: .control) #expect( sequence.effects.contains( .event(.windowDropped(source: WindowID(rawValue: 1), target: WindowID(rawValue: 3), stacking: false)))) } @Test(arguments: [HotKey.Modifiers(), [.command, .control], [.command, .function]]) func unmatchedModifiersStayPlain(modifiers: HotKey.Modifiers) throws { let initial = mouseState() var sequence = MouseSequence(state: initial) sequence.send( .down, at: try #require(initial.frames[WindowID(rawValue: 1)]).center, modifiers: modifiers, window: 1) let point = try #require(initial.frames[WindowID(rawValue: 3)]).center try sequence.move(1, to: point) sequence.send(.dragged, at: point) sequence.send(.up, at: point) #expect(sequence.state.frames[WindowID(rawValue: 1)] == initial.frames[WindowID(rawValue: 3)]) } @Test func clicksAndSubthresholdMovementDoNotDrop() throws { var sequence = MouseSequence(state: mouseState()) let point = try #require(sequence.state.frames[WindowID(rawValue: 1)]).center sequence.send(.down, at: point, modifiers: .command, window: 1) sequence.send(.dragged, at: Point(x: point.xCoordinate + 2, y: point.yCoordinate + 2)) #expect(sequence.send(.up, at: point).contains(.resume(WindowID(rawValue: 1), dropping: false))) #expect(!sequence.effects.contains { if case .event = $0 { true } else { false } }) } @Test func escapeCancelsAndMouseUpCannotCommit() throws { let initial = mouseState() var sequence = MouseSequence(state: initial) sequence.send( .down, at: try #require(initial.frames[WindowID(rawValue: 1)]).center, modifiers: .command, window: 1) let target = try #require(initial.frames[WindowID(rawValue: 3)]).center sequence.send(.dragged, at: target) #expect(sequence.send(.cancel, at: target).contains(.feedback(.targetChanged(nil)))) #expect(sequence.model.isActive) #expect(sequence.send(.dragged, at: target).isEmpty) #expect(sequence.send(.up, at: target).contains(.resume(WindowID(rawValue: 1), dropping: true))) #expect(!sequence.model.isActive) #expect(sequence.state == initial) } @Test func disabledModelStillSuspendsPlainWindowDrags() throws { let initial = mouseState() var sequence = MouseSequence(state: initial) let start = try #require(initial.frames[WindowID(rawValue: 1)]).center sequence.send(.down, at: start, modifiers: .command, window: 1) sequence.configuration.enabled = false #expect(sequence.send(.dragged, at: Point(x: 1000, y: 100)).contains(.feedback(.targetChanged(nil)))) #expect(sequence.model.isActive) sequence.send(.up, at: Point(x: 1000, y: 100)) #expect( sequence.send(.down, at: start, modifiers: .command, window: 1).contains(.suspend(WindowID(rawValue: 1)))) #expect(sequence.state == initial) } @Test func missingInactiveAndWrongOwnersAreIgnoredWhileFloatingCanSnap() throws { var state = mouseState() let point = try #require(state.frames[WindowID(rawValue: 1)]).center var model = MouseModel() let sample = DragSample( kind: .down, location: CGPoint(x: point.xCoordinate, y: point.yCoordinate), modifiers: .command) #expect(model.reduce(sample, state: state, configuration: MouseConfiguration()).isEmpty) #expect( model.reduce( sample, windowAtStart: DragWindow(pid: 999, windowNumber: 1), state: state, configuration: MouseConfiguration() ).isEmpty) state.windows[WindowID(rawValue: 1)]?.isFloating = true #expect( model.reduce( sample, windowAtStart: DragWindow(pid: engineApp.pid, windowNumber: 1), state: state, configuration: MouseConfiguration() ).contains(.suspend(WindowID(rawValue: 1)))) _ = model.cancel(state: state, awaitingMouseUp: false) state.windows[WindowID(rawValue: 1)]?.isFloating = false state.activeSpaces[engineDisplay] = SpaceID(rawValue: 99) #expect( model.reduce( sample, windowAtStart: DragWindow(pid: engineApp.pid, windowNumber: 1), state: state, configuration: MouseConfiguration() ).isEmpty) } @Test(arguments: [Layout.columns, .rows]) func sharedEdgesResizeLiveAndEscapeRestores(layout: Layout) throws { let initial = mouseState(layout: layout, count: 2, gap: 16) var sequence = MouseSequence(state: initial) let edge = try #require(initial.tileEdges.first) let start = edge.frame.center #expect(sequence.send(.down, at: start).contains(.beginResize(edge.affectedWindows))) let end = Point( x: start.xCoordinate + (edge.axis == .horizontal ? 80 : 0), y: start.yCoordinate + (edge.axis == .vertical ? 80 : 0)) let changed = sequence.send(.dragged, at: end) #expect(changed.contains(.event(.tileEdgeResized(edge, delta: 80)))) #expect(sequence.state.frames[WindowID(rawValue: 1)] != initial.frames[WindowID(rawValue: 1)]) #expect(sequence.state.frames[WindowID(rawValue: 2)] != initial.frames[WindowID(rawValue: 2)]) sequence.send(.cancel, at: end) #expect(sequence.state.workspaces != initial.workspaces) #expect(sequence.send(.dragged, at: end).isEmpty) #expect(sequence.send(.up, at: end).contains(.endResize)) #expect(sequence.state.workspaces == initial.workspaces) #expect(sequence.state.frames == initial.frames) #expect(sequence.send(.up, at: end).isEmpty) } @Test func edgeReleasePersistsAndDoubleClickResets() throws { let initial = mouseState(count: 2, gap: 16) var sequence = MouseSequence(state: initial) let edge = try #require(initial.tileEdges.first) let end = Point(x: edge.position + 100, y: edge.frame.center.yCoordinate) sequence.send(.down, at: edge.frame.center) sequence.send(.dragged, at: end) sequence.send(.up, at: end) let resized = try #require(sequence.state.tileEdges.first) #expect(resized.fraction != edge.fraction) sequence.send(.down, at: resized.frame.center, clickCount: 2) #expect(sequence.state.workspaces == initial.workspaces) #expect(sequence.state.frames == initial.frames) #expect(!sequence.model.isActive) } @Test func minimumRefusalNudgesWithoutChangingRatio() throws { var initial = mouseState(count: 2, gap: 8) initial.windows[WindowID(rawValue: 2)]?.minimum = LearnedMinimumSize(size: Size(width: 600, height: 100)) initial = reduce(initial, .relayout).0 var sequence = MouseSequence(state: initial) let edge = try #require(initial.tileEdges.first) sequence.send(.down, at: edge.frame.center) let effects = sequence.send(.dragged, at: Point(x: edge.position + 100, y: 400)) #expect(effects.contains(.feedback(.refused(.right)))) #expect(sequence.state == initial) #expect(!effects.contains { if case .event = $0 { true } else { false } }) } @Test func sourceStackAndFloatingTargetsAreNotDropTargets() throws { var initial = mouseState() initial = reduce( initial, .windowDropped(source: WindowID(rawValue: 2), target: WindowID(rawValue: 1), stacking: true) ).0 var sequence = MouseSequence(state: initial) let start = try #require(initial.frames[WindowID(rawValue: 2)]).center sequence.send(.down, at: start, modifiers: .command, window: 2) try sequence.move(2, to: Point(x: start.xCoordinate + 10, y: start.yCoordinate)) #expect( sequence.send(.dragged, at: Point(x: start.xCoordinate + 10, y: start.yCoordinate)).contains( .feedback(.targetChanged(nil)))) sequence.send(.up, at: start) #expect(sequence.state == initial) } @Test func gapClaimsRespectUnmanagedAndFloatingTopmostWindows() throws { var state = mouseState(count: 2, gap: 8) let edge = try #require(state.tileEdges.first) let sample = DragSample(kind: .down, location: CGPoint(x: edge.position, y: 400)) var model = MouseModel() #expect( model.reduce( sample, windowAtStart: DragWindow(pid: 999, windowNumber: 99), state: state, configuration: MouseConfiguration() ).isEmpty) #expect(!model.isActive) state.windows[WindowID(rawValue: 1)]?.isFloating = true let floating = model.reduce( sample, windowAtStart: DragWindow(pid: engineApp.pid, windowNumber: 1), state: state, configuration: MouseConfiguration()) #expect(!floating.contains(.beginResize(edge.affectedWindows))) #expect(floating.contains(.suspend(WindowID(rawValue: 1)))) } @Test func dividerHoverAgreesWithJunctionDragAndRejectsObservedContent() throws { let state = mouseState(layout: .side, count: 3, gap: 8) let root = try #require(state.tileEdges.first { $0.path.isEmpty }) let branch = try #require(state.tileEdges.first { !$0.path.isEmpty }) for (point, expected) in [ (Point(x: root.position, y: branch.position), root), (Point(x: branch.frame.minX, y: branch.position), branch), ] { let hovered = try #require(MouseModel.divider(at: point, topmost: nil, state: state)) #expect(hovered.identity == expected.identity) var model = MouseModel() let effects = model.reduce( DragSample(kind: .down, location: CGPoint(x: point.xCoordinate, y: point.yCoordinate)), state: state, configuration: MouseConfiguration()) #expect(effects.contains(.beginResize(hovered.affectedWindows))) } let content = try #require(state.frames[WindowID(rawValue: 1)]) let inside = Point(x: content.maxX - 1, y: content.center.yCoordinate) #expect(MouseModel.divider(at: inside, topmost: nil, state: state) == nil) let occluder = DragWindow( pid: 999, windowNumber: 999, bounds: CGRect( x: root.frame.minX, y: root.frame.minY, width: root.frame.size.width, height: root.frame.size.height)) #expect(MouseModel.divider(at: root.frame.center, topmost: occluder, state: state) == nil) let observed = [WindowID(rawValue: 1): Rect(width: 1200, height: 800)] #expect(MouseModel.divider(at: root.frame.center, topmost: nil, state: state, observedFrames: observed) == nil) } @Test func junctionClaimsTheActualGapRatherThanAnExtendedNeighborBand() throws { let state = mouseState(layout: .side, count: 3, gap: 8) let through = try #require(state.tileEdges.first { $0.path.isEmpty }) let branch = try #require(state.tileEdges.first { !$0.path.isEmpty }) let throughGap = Point(x: through.position, y: branch.position) var model = MouseModel() let throughEffects = model.reduce( DragSample(kind: .down, location: CGPoint(x: throughGap.xCoordinate, y: throughGap.yCoordinate)), state: state, configuration: MouseConfiguration()) #expect(throughEffects.contains(.beginResize(through.affectedWindows))) _ = model.cancel(state: state, awaitingMouseUp: false) let branchGap = Point(x: branch.frame.minX, y: branch.position) #expect(through.contains(branchGap, tolerance: 5)) #expect(branch.contains(branchGap, tolerance: 5)) let branchEffects = model.reduce( DragSample(kind: .down, location: CGPoint(x: branchGap.xCoordinate, y: branchGap.yCoordinate)), state: state, configuration: MouseConfiguration()) #expect(branchEffects.contains(.beginResize(branch.affectedWindows))) #expect(!branchEffects.contains(.beginResize(through.affectedWindows))) } @Test func releasingTheModifierBeforeUpUsesThePlainStructuralDrop() throws { let initial = mouseState() var sequence = MouseSequence(state: initial) let start = try #require(initial.frames[WindowID(rawValue: 1)]).center let target = try #require(initial.frames[WindowID(rawValue: 3)]).center sequence.send(.down, at: start, modifiers: .command, window: 1) try sequence.move(1, to: target) sequence.send(.dragged, at: target) sequence.send(.up, at: target, modifiers: []) #expect(sequence.state.frames[WindowID(rawValue: 1)] == initial.frames[WindowID(rawValue: 3)]) #expect(sequence.state.frames[WindowID(rawValue: 3)] == initial.frames[WindowID(rawValue: 1)]) #expect( sequence.effects.contains { if case .event(.gestureCommitted(.swapTiles)) = $0 { true } else { false } }) } @Test func crossDisplayIntentReportsOnceInsteadOfSilentlySettling() throws { var initial = mouseState() initial = reduce(initial, .displaySetChanged(Array(engineInitialState(twoDisplays: true).displays.values))).0 var sequence = MouseSequence(state: initial) let source = WindowID(rawValue: 1) let start = try #require(initial.frames[source]).center let outside = Point(x: 1500, y: 400) sequence.send(.down, at: start, modifiers: .command, window: 1) try sequence.move(1, to: outside) #expect(sequence.send(.dragged, at: outside).contains(.report(.invalidDropTarget(source)))) #expect(!sequence.send(.dragged, at: outside).contains(.report(.invalidDropTarget(source)))) sequence.send(.up, at: outside) #expect(sequence.state == initial) } @Test func floatingTargetIsExcluded() throws { var state = mouseState() state.windows[WindowID(rawValue: 3)]?.isFloating = true var sequence = MouseSequence(state: state) sequence.send( .down, at: try #require(state.frames[WindowID(rawValue: 1)]).center, modifiers: .command, window: 1) let target = try #require(state.frames[WindowID(rawValue: 3)]).center try sequence.move(1, to: target) #expect(sequence.send(.dragged, at: target).contains(.feedback(.targetChanged(nil)))) sequence.send(.up, at: target) #expect(sequence.state == state) } @Test(arguments: [Int32(20), 101]) func positiveLayerTopmostNeverClaimsAnEdge(layer: Int32) throws { let state = mouseState(count: 2, gap: 8) let edge = try #require(state.tileEdges.first) let sample = DragSample(kind: .down, location: CGPoint(x: edge.position, y: 400)) var model = MouseModel() let effects = model.reduce( sample, windowAtStart: DragWindow(pid: engineApp.pid, windowNumber: 1, layer: layer), state: state, configuration: MouseConfiguration()) #expect(effects.isEmpty) #expect(!model.isActive) #expect(!model.consumes(sample, state: state, observedFrames: [:])) } @Test(arguments: [1, 2]) func zeroGapDoesNotReservePlainClicks(clickCount: Int) throws { let state = mouseState(count: 2) let edge = try #require(state.tileEdges.first) let sample = DragSample(kind: .down, location: CGPoint(x: edge.position, y: 400), clickCount: clickCount) var model = MouseModel() #expect(model.reduce(sample, state: state, configuration: MouseConfiguration()).isEmpty) #expect(!model.isActive) } @Test( arguments: [1.0, 2.0, 3.0], [Layout.columns, .rows].flatMap { layout in [2.0, 12.0, 16.0, 20.0].map { (layout, $0) } }) func ownedWindowClaimsOnlyTheStrictTwoPointFrameBand(distance: Double, arrangement: (Layout, Double)) throws { let state = mouseState(layout: arrangement.0, count: 2, gap: arrangement.1) let window = WindowID(rawValue: 1) let frame = try #require(state.frames[window]) let edge = try #require(state.tileEdges.first) let location = edge.axis == .horizontal ? CGPoint(x: frame.maxX - distance, y: edge.frame.center.yCoordinate) : CGPoint(x: edge.frame.center.xCoordinate, y: frame.maxY - distance) let sample = DragSample(kind: .down, location: location) var model = MouseModel() let effects = model.reduce( sample, windowAtStart: DragWindow(pid: engineApp.pid, windowNumber: 1), state: state, configuration: MouseConfiguration(), observedFrames: [window: frame]) #expect(effects.contains(.beginResize(try #require(state.tileEdges.first).affectedWindows)) == (distance <= 2)) #expect(model.consumes(sample, state: state, observedFrames: [window: frame]) == (distance <= 2)) } @Test func observedBoundsControlTheBandAndPreventResetInsideContent() throws { let state = mouseState(count: 2, gap: 2) let window = WindowID(rawValue: 1) let edge = try #require(state.tileEdges.first) let original = try #require(state.frames[window]) var model = MouseModel() let narrowed = Rect( x: original.minX, y: original.minY, width: original.size.width - 3, height: original.size.height) let inOldFrame = DragSample(kind: .down, location: CGPoint(x: original.maxX - 2, y: 400)) let narrowEffects = model.reduce( inOldFrame, windowAtStart: DragWindow(pid: engineApp.pid, windowNumber: 1), state: state, configuration: MouseConfiguration(), observedFrames: [window: narrowed]) #expect(!narrowEffects.contains(.beginResize(edge.affectedWindows))) _ = model.cancel(state: state, awaitingMouseUp: false) let expanded = Rect( x: original.minX, y: original.minY, width: original.size.width + 2, height: original.size.height) let doubleClick = DragSample(kind: .down, location: CGPoint(x: edge.position, y: 400), clickCount: 2) let expandedEffects = model.reduce( doubleClick, windowAtStart: DragWindow(pid: engineApp.pid, windowNumber: 1), state: state, configuration: MouseConfiguration(), observedFrames: [window: expanded]) #expect(!expandedEffects.contains(.event(.tileEdgeReset(edge)))) #expect(!model.consumes(doubleClick, state: state, observedFrames: [window: expanded])) } @Test func directReleaseRequestsReadbackCommitInsteadOfPublishingTheRequestedFrame() throws { let initial = mouseState(count: 2) var sequence = MouseSequence(state: initial) let window = WindowID(rawValue: 1) let start = try #require(initial.frames[window]) sequence.send(.down, at: start.center, modifiers: [.control, .option, .command], window: 1) let end = Point(x: start.center.xCoordinate + 80, y: start.center.yCoordinate + 50) let effects = sequence.send(.up, at: end) #expect( effects.contains( .commitDirect( window: window, workspace: engineWorkspace, frame: Rect( x: start.minX + 80, y: start.minY + 50, width: start.size.width, height: start.size.height), dropVelocity: nil))) #expect(!effects.contains { if case .event = $0 { true } else { false } }) #expect(sequence.state == initial) } @Test func invalidCoordinatesSurfaceAnErrorAndCancel() throws { var sequence = MouseSequence(state: mouseState()) sequence.send( .down, at: try #require(sequence.state.frames[WindowID(rawValue: 1)]).center, modifiers: .command, window: 1 ) #expect(sequence.send(.dragged, at: Point(x: .nan, y: 100)).contains(.report(.invalidGeometry))) #expect(sequence.model.isActive) sequence.send(.up, at: Point(x: 100, y: 100)) #expect(!sequence.model.isActive) } }