import AppKit import ParcelConfig import ParcelCore import Testing @testable import ParcelUI struct StackControlsTests { @Test func activeSegmentsAndControlsHaveIndependentExpectedGeometry() throws { let group = try #require(StackChips.groups(in: LearningScene.state(cap: 1)).first) #expect(group.active) #expect(group.visible == WindowID(rawValue: 1)) #expect(group.frame == CGRect(x: 20, y: 0, width: 486, height: 20)) #expect( group.chips.map(\.frame) == [ CGRect(x: 22, y: 0, width: 140, height: 20), CGRect(x: 162, y: 0, width: 140, height: 20), CGRect(x: 302, y: 0, width: 140, height: 20), ]) #expect( group.chips.map(\.indicatorFrame) == [ CGRect(x: 80, y: 1, width: 24, height: 3), CGRect(x: 227, y: 1, width: 10, height: 3), CGRect(x: 367, y: 1, width: 10, height: 3), ]) #expect(group.frame(for: .previous) == CGRect(x: 0, y: 0, width: 22, height: 20)) #expect(group.frame(for: .next) == CGRect(x: 442, y: 0, width: 22, height: 20)) #expect(group.frame(for: .move) == CGRect(x: 464, y: 0, width: 22, height: 20)) #expect(group.hit(at: CGPoint(x: 23, y: 10)) == .chip(WindowID(rawValue: 1))) #expect(group.hit(at: CGPoint(x: 1, y: 10)) == .control(.previous)) #expect(group.hit(at: CGPoint(x: 443, y: 10)) == .control(.next)) #expect(group.hit(at: CGPoint(x: 465, y: 10)) == .control(.move)) #expect(group.hit(at: CGPoint(x: -1, y: 10)) == nil) #expect(group.hit(at: CGPoint(x: 200, y: 21)) == nil) #expect(group.hit(at: CGPoint(x: CGFloat.nan, y: 10)) == nil) } @Test func inactiveGripsStayCompactAndEverySegmentTracksSelection() throws { var state = LearningScene.state(cap: 1) state.focusedWindow = nil let inactive = try #require(StackChips.groups(in: state).first) #expect(!inactive.active) #expect(inactive.frame == CGRect(x: 20, y: 0, width: 90, height: 20)) #expect(inactive.countFrame == CGRect(x: 66, y: 0, width: 24, height: 20)) #expect( inactive.chips.map(\.indicatorFrame) == [ CGRect(x: 5, y: 7.5, width: 12, height: 5), CGRect(x: 27, y: 8.5, width: 12, height: 3), CGRect(x: 49, y: 8.5, width: 12, height: 3), ]) #expect(inactive.frame(for: .previous) == .zero) #expect(inactive.hit(at: CGPoint(x: 1, y: 10)) == .chip(WindowID(rawValue: 1))) state = reduce(state, .focusChanged(WindowID(rawValue: 2))).0 let active = try #require(StackChips.groups(in: state).first) #expect(active.active) #expect(active.chips.map { $0.indicatorFrame.width } == [10, 24, 10]) for chip in active.chips { #expect(chip.frame.contains(chip.indicatorFrame)) } } @Test func inactiveAccordionStackKeepsItsGripsAndCountInsideTheNarrowSafeGap() throws { let state = LearningScene.state(count: 4, layout: .accordion, inner: 20, outer: 20, cap: 2) let group = try #require(StackChips.groups(in: state).first) #expect(group.windows == [WindowID(rawValue: 2), WindowID(rawValue: 3), WindowID(rawValue: 4)]) #expect(!group.active) #expect(state.frames[group.visible] == Rect(x: 698, y: 20, width: 82, height: 560)) #expect(group.frame == CGRect(x: 698, y: 0, width: 82, height: 20)) #expect(group.countFrame == CGRect(x: 58, y: 0, width: 24, height: 20)) let width = 58.0 / 3 #expect( group.chips.map(\.frame) == [ CGRect(x: 0, y: 0, width: width, height: 20), CGRect(x: width, y: 0, width: width, height: 20), CGRect(x: 2 * width, y: 0, width: width, height: 20), ]) } @Test func crowdedStacksNeverProduceOverlappingControlHitAreas() { #expect(StackChips.groups(in: LearningScene.state(count: 100, cap: 1)).isEmpty) } @Test func dropTargetsAreSettledActiveUnobstructedTilesIncludingSingletons() throws { var state = LearningScene.state(cap: 2) let targets = StackChips.dropTargets(in: state) #expect(targets.count == 2) #expect( targets.map(\.windows).flatMap { $0 }.sorted() == [ WindowID(rawValue: 1), WindowID(rawValue: 2), WindowID(rawValue: 3), ]) let target = try #require(targets.first) #expect(target.accessibilityLabel == "Move to tile: Window 1") state.observedFrames[target.window]?.origin.xCoordinate += 1 #expect(!StackChips.dropTargets(in: state).contains { $0.window == target.window }) state.observedFrames = [:] #expect(StackChips.dropTargets(in: state).isEmpty) state = LearningScene.state(cap: 2) state.activeSpaces = [:] #expect(StackChips.dropTargets(in: state).isEmpty) } @Test(arguments: ["50% %@", "東京 — fenêtre", "Tile: %1$@ / %2$@", ""]) func moveDestinationInterpolatesTheWholeTitleWithoutTreatingItAsAFormat(title: String) { let target = StackDropTarget( window: WindowID(rawValue: 1), windows: [WindowID(rawValue: 1)], workspace: LearningScene.key, frame: CGRect(x: 20, y: 20, width: 300, height: 560), title: title) #expect(target.accessibilityLabel == "Move to tile: \(title)") } @Test func floatingWindowsAndFocusChangesCannotOfferAnUnsafeDrop() throws { var state = LearningScene.state(cap: 2) let target = try #require(StackChips.dropTargets(in: state).first) let floating = WindowID(rawValue: 99) state = reduce( state, .windowAppeared( ManagedWindow(id: floating, app: AppID(pid: 11, bundleID: "fixture"), isFloating: true), workspace: LearningScene.key, frame: Rect(x: target.frame.minX, y: target.frame.minY, width: 20, height: 20)) ).0 state.observedFrames[floating] = Rect(x: target.frame.minX, y: target.frame.minY, width: 20, height: 20) #expect(!StackChips.dropTargets(in: state).contains { $0.window == target.window }) } @Test func preciseScrollCoalescesOneActionPerGestureAndIgnoresMomentum() { var scroll = StackScroll() #expect(scroll.receive(deltaX: 0, deltaY: -6, precise: true, phase: .began, at: 0) == nil) for time in [0.01, 0.02] { #expect(scroll.receive(deltaX: 0, deltaY: -6, precise: true, phase: .changed, at: time) == nil) } #expect(scroll.receive(deltaX: 0, deltaY: -6, precise: true, phase: .changed, at: 0.03) == .next) for index in 1...100 { #expect(scroll.receive(deltaX: 0, deltaY: -80, precise: true, phase: .changed, at: Double(index)) == nil) } #expect(scroll.receive(deltaX: 0, deltaY: -200, precise: true, phase: .ended, at: 101) == nil) #expect(scroll.receive(deltaX: 0, deltaY: -200, precise: true, phase: .momentum, at: 102) == nil) #expect(scroll.receive(deltaX: 24, deltaY: 0, precise: true, phase: .began, at: 103) == .previous) } @Test func wheelCooldownAndDirectionReversalAvoidPerEventActions() { var scroll = StackScroll() #expect(scroll.receive(deltaX: 0, deltaY: -1, precise: false, phase: .none, at: 0) == .next) #expect(scroll.receive(deltaX: 0, deltaY: -1, precise: false, phase: .none, at: 0.05) == nil) #expect(scroll.receive(deltaX: 0, deltaY: 1, precise: false, phase: .none, at: 0.12) == .previous) #expect(scroll.receive(deltaX: .nan, deltaY: -1, precise: false, phase: .none, at: 0.3) == nil) #expect(scroll.receive(deltaX: 0, deltaY: -1, precise: false, phase: .none, at: .infinity) == nil) #expect(scroll.receive(deltaX: 0, deltaY: -20, precise: true, phase: .began, at: 1) == nil) #expect(scroll.receive(deltaX: 0, deltaY: 20, precise: true, phase: .changed, at: 1.1) == nil) #expect(scroll.receive(deltaX: 0, deltaY: 4, precise: true, phase: .changed, at: 1.2) == .previous) } @Test func dragRevalidatesTargetsAndCancelsWithoutSelectingOrMoving() { let source = WindowID(rawValue: 1) let selfTile = StackDropTarget( window: source, windows: [source, WindowID(rawValue: 2)], workspace: LearningScene.key, frame: CGRect(x: 20, y: 20, width: 300, height: 560), title: "Source") let target = StackDropTarget( window: WindowID(rawValue: 10), windows: [WindowID(rawValue: 10)], workspace: LearningScene.key, frame: CGRect(x: 400, y: 20, width: 300, height: 560), title: "Target") var drag = StackChipDrag() drag.begin(source: source, at: CGPoint(x: 40, y: 10)) drag.update(at: CGPoint(x: 43, y: 10), targets: [selfTile, target]) #expect(!drag.hasDragged && drag.target == nil) drag.update(at: CGPoint(x: 100, y: 100), targets: [selfTile, target]) #expect(drag.hasDragged && drag.target == nil) drag.update(at: CGPoint(x: 500, y: 100), targets: [selfTile, target]) #expect(drag.target == target) #expect(drag.release(at: CGPoint(x: 500, y: 100), targets: []) == nil) #expect(drag.source == nil) drag.begin(source: source, at: CGPoint(x: 40, y: 10)) #expect( drag.release(at: CGPoint(x: 500, y: 100), targets: [target]) == StackMove(source: source, target: target.window)) drag.begin(source: source, at: CGPoint(x: 40, y: 10)) drag.update(at: CGPoint(x: 500, y: 100), targets: [target]) drag.cancel() #expect(drag.release(at: CGPoint(x: 500, y: 100), targets: [target]) == nil) drag.begin(source: source, at: CGPoint(x: 40, y: 10)) #expect(drag.release(at: CGPoint(x: CGFloat.nan, y: 100), targets: [target]) == nil) } } extension AppKitTestSuite { @MainActor struct StackControlsViewTests { init() { _ = NSApplication.shared } @Test func contextOnlyTabDragSurvivesGeometryRefreshWithoutRenderingLegacyChips() throws { var moves: [StackMove] = [] var transitions: [Bool] = [] let controller = StackChipController( focus: { _ in Issue.record("Dragging a tab must not select it") }, close: { _ in }, move: { moves.append(StackMove(source: $0, target: $1)) }, orderPanels: false) defer { controller.stop() } controller.onDragStateChanged = { transitions.append($0) } let state = LearningScene.state(count: 3, inner: 0, outer: 0, cap: 2) controller.updateDragContext(state: state, configuration: .init(enabled: false)) let sourceTile = try #require(controller.dropTargets.first { $0.windows.count > 1 }) let source = try #require(sourceTile.windows.last) let target = try #require(controller.dropTargets.first { !$0.windows.contains(source) }) let end = CGPoint(x: target.frame.midX, y: target.frame.midY) #expect(controller.groups.isEmpty && controller.panels.isEmpty) controller.beginDrag(source, at: CGPoint(x: sourceTile.frame.midX, y: sourceTile.frame.midY)) controller.updateDrag(at: end) #expect(controller.isDragging && transitions == [true]) var unknown = state unknown.observedFrames[target.window] = nil controller.updateDragContext(state: unknown, configuration: .init(enabled: false)) #expect(controller.isDragging) #expect(controller.dropTargets.isEmpty) #expect(controller.groups.isEmpty && controller.panels.isEmpty) controller.updateDragContext(state: state, configuration: .init(enabled: false)) #expect(controller.isDragging && transitions == [true]) controller.endDrag(at: end) #expect(moves == [StackMove(source: source, target: target.window)]) #expect(!controller.isDragging && transitions == [true, false]) #expect(controller.groups.isEmpty && controller.panels.isEmpty) } @Test(arguments: ["cancel", "hidden", "removed", "reused"]) func contextOnlyTabDragRejectsInvalidationAndLateRelease(reason: String) throws { var moves: [StackMove] = [] var transitions: [Bool] = [] let controller = StackChipController( focus: { _ in Issue.record("A cancelled drag must not select a tab") }, close: { _ in }, move: { moves.append(StackMove(source: $0, target: $1)) }, orderPanels: false) defer { controller.stop() } controller.onDragStateChanged = { transitions.append($0) } var state = LearningScene.state(count: 3, inner: 0, outer: 0, cap: 2) controller.updateDragContext(state: state, configuration: .init(enabled: false)) let sourceTile = try #require(controller.dropTargets.first { $0.windows.count > 1 }) let source = try #require(sourceTile.windows.last) let target = try #require(controller.dropTargets.first { !$0.windows.contains(source) }) let end = CGPoint(x: target.frame.midX, y: target.frame.midY) controller.beginDrag(source, at: CGPoint(x: sourceTile.frame.midX, y: sourceTile.frame.midY)) controller.updateDrag(at: end) #expect(controller.isDragging) switch reason { case "cancel": controller.cancelDrag() case "hidden": state.activeSpaces = [:] case "removed": state = reduce(state, .windowDisappeared(source)).0 case "reused": state.windows[source] = ManagedWindow( id: source, app: AppID(pid: 999, bundleID: "fixture.reused")) default: Issue.record("Unknown drag invalidation") } controller.updateDragContext(state: state, configuration: .init(enabled: false)) #expect(!controller.isDragging && transitions == [true, false]) controller.endDrag(at: end) #expect(moves.isEmpty) #expect(transitions == [true, false]) #expect(controller.groups.isEmpty && controller.panels.isEmpty) } @Test func arrowsAndKeyboardMoveMenuExposeNamedActionsWithoutCustomChords() throws { let state = reduce(LearningScene.state(cap: 2), .focusChanged(WindowID(rawValue: 2))).0 let group = try #require(StackChips.groups(in: state).first) let source = try #require(group.chips.last) let target = try #require(StackChips.dropTargets(in: state).first { !$0.windows.contains(source.window) }) var cycles: [CycleDirection] = [] var moved: [StackMove] = [] var closed: [WindowID] = [] let view = StackChipView( group: group, focus: { _ in }, close: { closed.append($0) }, cycle: { window, direction in #expect(window == group.visible) cycles.append(direction) }, move: { moved.append(StackMove(source: $0, target: $1)) }) view.update(group, targets: StackChips.dropTargets(in: state)) let controls = view.subviews.compactMap { $0 as? NSButton }.filter { $0.tag < 0 } #expect( controls.map { $0.accessibilityLabel() } == [ "Previous stacked window", "Next stacked window", "Move Window to Tile…", ]) #expect( controls.allSatisfy { $0.keyEquivalent.isEmpty && !$0.needsPanelToBecomeKey && $0.nextKeyView != nil }) #expect(controls.allSatisfy { $0.acceptsFirstMouse(for: nil) }) controls[0].performClick(nil) controls[1].performClick(nil) #expect(cycles == [.previous, .next]) let tab = try #require((view.accessibilityTabs() as? [NSButton])?.last) #expect(tab.accessibilityCustomActions()?.map(\.name) == ["Close Window", "Move Window to Tile…"]) let menu = try #require(tab.menu?.items.last?.submenu) #expect(menu.items.map(\.title) == [target.accessibilityLabel]) #expect(menu.items.allSatisfy { $0.keyEquivalent.isEmpty }) menu.performActionForItem(at: 0) #expect(moved == [StackMove(source: source.window, target: target.window)]) #expect(closed.isEmpty) tab.menu?.performActionForItem(at: 0) #expect(closed == [source.window]) view.update(group, targets: []) #expect(!controls[2].isEnabled) #expect(tab.accessibilityCustomActions()?.map(\.name) == ["Close Window"]) } @Test func nativeDragSignalsEscapeOwnershipAndCancellationRejectsLateReleaseWithoutTakingFocus() throws { var moved: [StackMove] = [] var focused: [WindowID] = [] var dragStates: [Bool] = [] let existingWindows = Set(NSApp.windows.map(ObjectIdentifier.init)) let controller = StackChipController( focus: { focused.append($0) }, close: { _ in }, move: { moved.append(StackMove(source: $0, target: $1)) }) defer { controller.stop() } controller.onDragStateChanged = { dragStates.append($0) } controller.update(state: LearningScene.state(cap: 2), configuration: .init(enabled: false)) let group = try #require(controller.groups.first) let source = try #require(group.chips.last) let target = try #require(controller.dropTargets.first { !$0.windows.contains(source.window) }) let panel = try #require( NSApp.windows.first { !existingWindows.contains(ObjectIdentifier($0)) && $0.isVisible && ($0.contentView as? StackChipView)?.windows == group.windows }) let button = try #require( panel.contentView?.subviews.compactMap { $0 as? NSButton }.first { $0.tag == Int(source.window.rawValue) }) let start = try stackButtonPoint(button, panel: panel) let end = CGPoint(x: target.frame.midX, y: target.frame.midY) let keyWindow = NSApp.keyWindow let responder = panel.firstResponder let activeApp = NSWorkspace.shared.frontmostApplication?.processIdentifier #expect(panel.styleMask.contains(.nonactivatingPanel)) #expect(panel.canBecomeKey && !panel.canBecomeMain) #expect((panel as? NSPanel)?.becomesKeyOnlyIfNeeded == true) #expect(!button.needsPanelToBecomeKey && button.acceptsFirstMouse(for: nil)) button.mouseDown(with: try stackMouseEvent(.leftMouseDown, point: start, panel: panel)) button.mouseDragged( with: try stackMouseEvent( .leftMouseDragged, point: CGPoint(x: start.x + 3, y: start.y), panel: panel)) #expect(!controller.isDragging && dragStates.isEmpty) #expect(!panel.isKeyWindow) button.mouseDragged(with: try stackMouseEvent(.leftMouseDragged, point: end, panel: panel)) #expect(controller.isDragging && dragStates == [true]) #expect(!panel.isKeyWindow && NSApp.keyWindow === keyWindow) #expect(panel.firstResponder === responder) #expect(NSWorkspace.shared.frontmostApplication?.processIdentifier == activeApp) let preview = try #require(NSApp.windows.first { $0.isVisible && $0.title == "Move here" }) #expect(preview.ignoresMouseEvents) #expect(preview.contentView?.layer?.animationKeys()?.isEmpty != false) controller.cancelDrag() controller.cancelDrag() #expect(!controller.isDragging && dragStates == [true, false]) #expect(!preview.isVisible) button.mouseDragged(with: try stackMouseEvent(.leftMouseDragged, point: end, panel: panel)) button.mouseUp(with: try stackMouseEvent(.leftMouseUp, point: start, panel: panel)) #expect(moved.isEmpty && focused.isEmpty) #expect(!controller.isDragging && dragStates == [true, false]) button.mouseDown(with: try stackMouseEvent(.leftMouseDown, point: start, panel: panel)) button.mouseDragged(with: try stackMouseEvent(.leftMouseDragged, point: end, panel: panel)) button.mouseUp(with: try stackMouseEvent(.leftMouseUp, point: end, panel: panel)) #expect(moved == [StackMove(source: source.window, target: target.window)]) #expect(focused.isEmpty) #expect(!controller.isDragging && dragStates == [true, false, true, false]) #expect(!panel.isKeyWindow && NSApp.keyWindow === keyWindow) #expect(panel.firstResponder === responder) #expect(NSWorkspace.shared.frontmostApplication?.processIdentifier == activeApp) #expect(!NSApp.windows.contains { $0.isVisible && $0.title == "Move here" }) } @Test func plainChipClicksAndDragsPreserveTheExistingKeyboardResponderWithoutActivation() throws { let group = try #require(StackChips.groups(in: LearningScene.state(cap: 1)).first) var focused: [WindowID] = [] var cancelled = 0 let panel = LearningPanel(title: group.accessibilityLabel, interactive: true) defer { panel.close() } let view = StackChipView( group: group, focus: { focused.append($0) }, close: { _ in }, dragCancelled: { cancelled += 1 }) panel.contentView = view panel.place(group.frame) panel.orderFront(nil) let tabs = try #require(view.accessibilityTabs() as? [NSButton]) let button = try #require(tabs.first) let keyboardResponder = try #require(tabs.last) try #require(panel.makeFirstResponder(keyboardResponder)) let keyWindow = NSApp.keyWindow let activeApp = NSWorkspace.shared.frontmostApplication?.processIdentifier let start = try stackButtonPoint(button, panel: panel) let down = try stackMouseEvent(.leftMouseDown, point: start, panel: panel) #expect(button.bounds.contains(button.convert(down.locationInWindow, from: nil))) button.mouseDown(with: down) button.mouseUp(with: try stackMouseEvent(.leftMouseUp, point: start, panel: panel)) #expect(focused == [WindowID(rawValue: 1)] && cancelled == 1) #expect(!panel.isKeyWindow && NSApp.keyWindow === keyWindow) #expect(panel.firstResponder === keyboardResponder) button.mouseDown(with: down) let end = CGPoint(x: start.x + 20, y: start.y) button.mouseDragged(with: try stackMouseEvent(.leftMouseDragged, point: end, panel: panel)) button.cancelOperation(nil) button.cancelOperation(nil) button.mouseUp(with: try stackMouseEvent(.leftMouseUp, point: start, panel: panel)) #expect(cancelled == 2 && focused == [WindowID(rawValue: 1)]) #expect(!panel.isKeyWindow && NSApp.keyWindow === keyWindow) #expect(panel.firstResponder === keyboardResponder) #expect(NSWorkspace.shared.frontmostApplication?.processIdentifier == activeApp) } @Test func stopClearsGestureBeforeReentrantNotificationAndLateReleaseCannotSelectOrMove() throws { var moved: [StackMove] = [] var focused: [WindowID] = [] var dragStates: [Bool] = [] let existingWindows = Set(NSApp.windows.map(ObjectIdentifier.init)) let controller = StackChipController( focus: { focused.append($0) }, close: { _ in }, move: { moved.append(StackMove(source: $0, target: $1)) }) defer { controller.stop() } controller.onDragStateChanged = { [weak controller] dragging in #expect(controller?.isDragging == dragging) dragStates.append(dragging) if !dragging { controller?.cancelDrag() } } controller.update(state: LearningScene.state(cap: 2), configuration: .init(enabled: false)) let group = try #require(controller.groups.first) let source = try #require(group.chips.last) let target = try #require(controller.dropTargets.first { !$0.windows.contains(source.window) }) let panel = try #require( NSApp.windows.first { !existingWindows.contains(ObjectIdentifier($0)) && $0.isVisible && ($0.contentView as? StackChipView)?.windows == group.windows }) let button = try #require( panel.contentView?.subviews.compactMap { $0 as? NSButton }.first { $0.tag == Int(source.window.rawValue) }) let start = try stackButtonPoint(button, panel: panel) let end = CGPoint(x: target.frame.midX, y: target.frame.midY) button.mouseDown(with: try stackMouseEvent(.leftMouseDown, point: start, panel: panel)) button.mouseDragged(with: try stackMouseEvent(.leftMouseDragged, point: end, panel: panel)) #expect(controller.isDragging && dragStates == [true]) controller.stop() controller.stop() controller.cancelDrag() #expect(!controller.isDragging && dragStates == [true, false]) button.mouseUp(with: try stackMouseEvent(.leftMouseUp, point: start, panel: panel)) #expect(moved.isEmpty && focused.isEmpty) #expect(controller.groups.isEmpty && controller.dropTargets.isEmpty) #expect(!NSApp.windows.contains { $0.isVisible && $0.title == "Move here" }) } @Test func synchronousGestureRejectionCannotRecreatePreviewOrCommitALateRelease() throws { var dragStates: [Bool] = [] var moved: [StackMove] = [] var focused: [WindowID] = [] let existingWindows = Set(NSApp.windows.map(ObjectIdentifier.init)) let controller = StackChipController( focus: { focused.append($0) }, close: { _ in }, move: { moved.append(StackMove(source: $0, target: $1)) }) defer { controller.stop() } controller.onDragStateChanged = { [weak controller] dragging in #expect(controller?.isDragging == dragging) dragStates.append(dragging) if dragging { controller?.cancelDrag() } } controller.update(state: LearningScene.state(cap: 2), configuration: .init(enabled: false)) let group = try #require(controller.groups.first) let source = try #require(group.chips.last) let target = try #require(controller.dropTargets.first { !$0.windows.contains(source.window) }) let panel = try #require( NSApp.windows.first { !existingWindows.contains(ObjectIdentifier($0)) && $0.isVisible && ($0.contentView as? StackChipView)?.windows == group.windows }) let button = try #require( panel.contentView?.subviews.compactMap { $0 as? NSButton }.first { $0.tag == Int(source.window.rawValue) }) let start = try stackButtonPoint(button, panel: panel) let end = CGPoint(x: target.frame.midX, y: target.frame.midY) button.mouseDown(with: try stackMouseEvent(.leftMouseDown, point: start, panel: panel)) button.mouseDragged(with: try stackMouseEvent(.leftMouseDragged, point: end, panel: panel)) #expect(!controller.isDragging && dragStates == [true, false]) #expect(!NSApp.windows.contains { $0.isVisible && $0.title == "Move here" }) button.mouseDragged(with: try stackMouseEvent(.leftMouseDragged, point: end, panel: panel)) button.mouseUp(with: try stackMouseEvent(.leftMouseUp, point: start, panel: panel)) #expect(dragStates == [true, false]) #expect(moved.isEmpty && focused.isEmpty) #expect(!panel.isKeyWindow) } @Test func disappearingTargetOrStoppingCannotCommitAnOldDrag() throws { var moved = 0 var dragStates: [Bool] = [] let existingWindows = Set(NSApp.windows.map(ObjectIdentifier.init)) let controller = StackChipController( focus: { _ in Issue.record("An invalidated drag selected a chip") }, close: { _ in }, move: { _, _ in moved += 1 }) defer { controller.stop() } controller.onDragStateChanged = { dragStates.append($0) } let state = LearningScene.state(cap: 2) controller.update(state: state, configuration: .init(enabled: false)) let group = try #require(controller.groups.first) let source = try #require(group.chips.last) let target = try #require(controller.dropTargets.first { !$0.windows.contains(source.window) }) let panel = try #require( NSApp.windows.first { !existingWindows.contains(ObjectIdentifier($0)) && $0.isVisible && ($0.contentView as? StackChipView)?.windows == group.windows }) let button = try #require( panel.contentView?.subviews.compactMap { $0 as? NSButton }.first { $0.tag == Int(source.window.rawValue) }) let start = try stackButtonPoint(button, panel: panel) let end = CGPoint(x: target.frame.midX, y: target.frame.midY) button.mouseDown(with: try stackMouseEvent(.leftMouseDown, point: start, panel: panel)) button.mouseDragged(with: try stackMouseEvent(.leftMouseDragged, point: end, panel: panel)) var unknown = state unknown.observedFrames = [:] controller.update(state: unknown, configuration: .init(enabled: false)) button.mouseUp(with: try stackMouseEvent(.leftMouseUp, point: start, panel: panel)) #expect(moved == 0) #expect(!controller.isDragging && dragStates == [true, false]) #expect(!NSApp.windows.contains { $0.isVisible && $0.title == "Move here" }) controller.stop() #expect(controller.dropTargets.isEmpty) } } } @MainActor private func stackButtonPoint(_ button: NSButton, panel: NSWindow) throws -> CGPoint { let screen = try #require(NSScreen.screens.first) let center = button.convert(CGPoint(x: button.bounds.midX, y: button.bounds.midY), to: nil) let physical = panel.convertPoint(toScreen: center) return CGPoint(x: physical.x, y: screen.frame.maxY - physical.y) } @MainActor private func stackMouseEvent(_ type: NSEvent.EventType, point: CGPoint, panel: NSWindow) throws -> NSEvent { let screen = CGPoint(x: point.x, y: (NSScreen.screens.first?.frame.maxY ?? 0) - point.y) return try #require( NSEvent.mouseEvent( with: type, location: panel.convertPoint(fromScreen: screen), modifierFlags: [], timestamp: 1, windowNumber: panel.windowNumber, context: nil, eventNumber: 0, clickCount: 1, pressure: type == .leftMouseUp ? 0 : 1)) }