import CoreGraphics import ParcelConfig import ParcelCore import ParcelPlatform import Testing @testable import ParcelEngine @Suite("Mouse review regressions") @MainActor struct MouseReviewRegressionTests { @Test(arguments: [false, true]) func liveClampingFramesNeverLearnOrConsumeCorrections(roundsToGrid: Bool) async throws { var configuration = edgeConfiguration() configuration.layout.global.gaps = LayoutGaps(inner: 10, outer: 0) let harness = try EngineHarness(configuration: configuration) try await harness.start() try harness.add([1]) try harness.add([2], app: secondEngineApp) try harness.finishMotion(after: 0.03) let source = WindowID(rawValue: 2) let initial = harness.engine.snapshot.state let edge = try #require(initial.tileEdges.first) harness.world.behaviors[secondEngineApp] = roundsToGrid ? .init(widthMultiple: 19) : .init(minimumSize: Size(width: 650, height: 100)) harness.engine.handleMouse(sample(.down, edge.frame.center)) for index in 1...20 { try harness.scheduler.advance(by: 0.02) harness.engine.handleMouse(sample(.dragged, Point(x: edge.position + Double(index * 7), y: 400))) #expect(harness.engine.snapshot.state.windows[source]?.minimum == nil) #expect(harness.engine.minimumCandidates[source] == nil) #expect(harness.engine.correctiveWrites[source] == nil) #expect(harness.engine.frameAdjustments[source] == nil) #expect(!harness.engine.blockedWindows.contains(source)) } #expect(harness.world.writes(for: source).count >= 10) #expect(!harness.log.reports.contains(.invalidGeometry)) let finalPoint = Point(x: edge.position + 140, y: 400) let beforeRelease = harness.world.writes(for: source).count harness.engine.handleMouse(sample(.up, finalPoint)) try harness.finishMotion() #expect(harness.world.writes(for: source).count > beforeRelease) #expect(harness.engine.frameAdjustments[source] != nil) #expect(harness.engine.correctiveWrites[source]?.count == (roundsToGrid ? nil : 1)) #expect(harness.engine.minimumCandidates[source]?.confirmations == (roundsToGrid ? nil : 1)) if roundsToGrid { #expect(harness.world.writes(for: source).last?.frame.size.width == 455) #expect(harness.world.frames[source]?.size.width == 437) } #expect(!harness.engine.blockedWindows.contains(source)) #expect(!harness.log.platformFailures.contains(.correctiveWriteLimit(source, limit: 6))) await harness.engine.stop() } @Test func liveClampNotificationsDoNotResendTheSameSample() async throws { let harness = try EngineHarness(configuration: edgeConfiguration()) try await harness.start() try harness.add([1]) try harness.add([2], app: secondEngineApp) try harness.finishMotion(after: 0.03) let window = WindowID(rawValue: 2) let edge = try #require(harness.engine.snapshot.state.tileEdges.first) harness.world.behaviors[secondEngineApp] = .init(minimumSize: Size(width: 650, height: 100)) harness.engine.handleMouse(sample(.down, edge.frame.center)) try harness.scheduler.advance(by: 0.02) harness.engine.handleMouse(sample(.dragged, Point(x: edge.position + 80, y: 400))) let accepted = try #require(harness.world.frames[window]) let writes = harness.world.writes(for: window).count for _ in 0..<10 { harness.world.userFrame(window, frame: accepted, resized: true) try harness.scheduler.advance(by: 0.03) #expect(harness.world.writes(for: window).count == writes) } harness.engine.handleMouse(sample(.dragged, Point(x: edge.position + 100, y: 400))) #expect(harness.world.writes(for: window).count == writes + 1) harness.engine.handleMouse(sample(.up, Point(x: edge.position + 100, y: 400))) try harness.finishMotion() #expect(harness.engine.correctiveWrites[window]?.count == 1) await harness.engine.stop() } @Test func minimumUpdatesWaitUntilTheCapturedEdgeIsReleased() async throws { let harness = try EngineHarness(configuration: edgeConfiguration()) try await harness.start() try harness.add([1, 2]) let window = WindowID(rawValue: 2) let edge = try #require(harness.engine.snapshot.state.tileEdges.first) harness.engine.handleMouse(sample(.down, edge.frame.center)) let learned = LearnedMinimumSize(size: Size(width: 700, height: 100)) harness.engine.send(.minimumSizeChanged(window, minimum: learned)) try harness.scheduler.runReady() #expect(harness.engine.snapshot.state.windows[window]?.minimum == nil) harness.engine.handleMouse(sample(.dragged, Point(x: edge.position + 80, y: 400))) #expect(!harness.log.reports.contains(.invalidGeometry)) #expect(harness.engine.snapshot.state.tileEdges.first?.fraction != edge.fraction) harness.engine.handleMouse(sample(.up, Point(x: edge.position + 80, y: 400))) #expect(harness.engine.snapshot.state.windows[window]?.minimum == learned) #expect(harness.engine.deferredResizeMinimums.isEmpty) await harness.engine.stop() } @Test func slowLiveResizeCoalescesToTheLatestSampleAndReconcilesAfterUp() async throws { let harness = try EngineHarness(configuration: edgeConfiguration()) try await harness.start() try harness.add([1, 2]) let edge = try #require(harness.engine.snapshot.state.tileEdges.first) harness.world.behaviors[engineApp] = .init(duration: 0.1) harness.world.deferWrites = true harness.engine.handleMouse(sample(.down, edge.frame.center)) try harness.scheduler.advance(by: 0.02) harness.engine.handleMouse(sample(.dragged, Point(x: edge.position + 10, y: 400))) let writes = harness.world.writes.count #expect(harness.world.pendingWriteCount == 1) for index in 2...20 { try harness.scheduler.advance(by: 0.001) harness.engine.handleMouse(sample(.dragged, Point(x: edge.position + Double(index * 5), y: 400))) } #expect(harness.world.writes.count == writes) #expect(harness.engine.pendingFrames.count == 2) harness.engine.handleMouse(sample(.up, Point(x: edge.position + 100, y: 400))) let final = harness.engine.snapshot.state.frames try harness.world.completeNextWrite() #expect(harness.world.pendingWriteCount == 0) try harness.scheduler.advance(by: 0.2) #expect(harness.world.pendingWriteCount == 1) #expect(harness.world.writes.count == writes + 1) let finalWrites = try #require(harness.world.writes.last) #expect(finalWrites.writes.allSatisfy { $0.frame == final[$0.window] }) try harness.world.completeNextWrite() #expect(harness.world.frames == final) #expect(harness.engine.finalResizeWindows.isEmpty) await harness.engine.stop() } @Test func completedSlowWritesAreThrottledByMeasuredLatency() async throws { let harness = try EngineHarness(configuration: edgeConfiguration()) try await harness.start() try harness.add([1, 2]) let edge = try #require(harness.engine.snapshot.state.tileEdges.first) harness.world.behaviors[engineApp] = .init(duration: 0.1) harness.engine.handleMouse(sample(.down, edge.frame.center)) for index in 1...40 { try harness.scheduler.advance(by: 0.005) harness.engine.handleMouse(sample(.dragged, Point(x: edge.position + Double(index * 2), y: 400))) } let liveWrites = harness.world.writes.filter { $0.at > 10 } #expect(liveWrites.count < 10) #expect(liveWrites.count >= 2) for (first, second) in zip(liveWrites.dropFirst(2), liveWrites.dropFirst(3)) { #expect(second.at - first.at >= 0.02) } harness.engine.handleMouse(sample(.up, Point(x: edge.position + 80, y: 400))) try harness.finishMotion() #expect(harness.world.frames == harness.engine.snapshot.state.frames) await harness.engine.stop() } @Test func escapeIgnoresContinuedWindowMovementWithoutWritingUntilRealUp() async throws { let harness = try EngineHarness(configuration: engineConfiguration(glide: true)) try await harness.start() try harness.add([1, 2]) try harness.finishMotion() let initial = harness.engine.snapshot.state let source = WindowID(rawValue: 1) let start = try #require(initial.frames[source]).center let target = try #require(initial.frames[WindowID(rawValue: 2)]).center var feedback: [EngineMouseFeedback] = [] harness.engine.onMouseFeedback = { feedback.append($0) } harness.engine.handleMouse(sample(.down, start, modifiers: .command), windowAtStart: identity(1)) harness.world.userFrame(source, frame: Rect(x: 700, y: 50, width: 600, height: 800)) harness.engine.handleMouse(sample(.dragged, target, modifiers: .command)) #expect(feedback.last != .targetChanged(nil)) harness.engine.handleMouse(sample(.cancel, target)) #expect(feedback.last == .layoutPreview(nil)) let writes = harness.world.writes(for: source).count for index in 1...5 { harness.world.userFrame(source, frame: Rect(x: 700 + Double(index * 20), y: 50, width: 600, height: 800)) harness.engine.handleMouse(sample(.dragged, target, modifiers: .command)) try harness.finishMotion(after: 0.1) #expect(harness.world.writes(for: source).count == writes) #expect(harness.engine.dragging.contains(source)) #expect(harness.engine.pointerIsDown) } harness.engine.handleMouse(sample(.up, target, modifiers: .command)) try harness.finishMotion() #expect(harness.world.frames[source] == initial.frames[source]) #expect(harness.engine.snapshot.state.workspaces == initial.workspaces) #expect(!feedback.contains { if case .dropped = $0 { true } else { false } }) await harness.engine.stop() } @Test func cancelledEdgeRestoresOnlyOnTheRealMouseUp() async throws { let harness = try EngineHarness(configuration: edgeConfiguration()) try await harness.start() try harness.add([1, 2]) let initial = harness.engine.snapshot.state let edge = try #require(initial.tileEdges.first) let end = Point(x: edge.position + 80, y: 400) harness.engine.handleMouse(sample(.down, edge.frame.center)) harness.engine.handleMouse(sample(.dragged, end)) try harness.scheduler.advance(by: 0.03) let changed = harness.world.frames harness.engine.handleMouse(sample(.cancel, end)) let writes = harness.world.writes.count harness.engine.handleMouse(sample(.dragged, Point(x: edge.position + 160, y: 400))) try harness.finishMotion(after: 0.5) #expect(harness.world.frames == changed) #expect(harness.world.writes.count == writes) #expect(!harness.engine.liveResizeWindows.isEmpty) harness.engine.handleMouse(sample(.up, end)) try harness.finishMotion() #expect(harness.engine.snapshot.state.workspaces == initial.workspaces) #expect(harness.world.frames == initial.frames) await harness.engine.stop() } @Test(arguments: [false, true]) func contentDragNeverSwapsWithoutWindowMovement(stacking: Bool) async throws { let harness = try EngineHarness() try await harness.start() try harness.add([1, 2]) let initial = harness.engine.snapshot.state let source = WindowID(rawValue: 1) let start = try #require(initial.frames[source]).center let end = try #require(initial.frames[WindowID(rawValue: 2)]).center let modifiers: HotKey.Modifiers = stacking ? [.command, .option] : .command var feedback: [EngineMouseFeedback] = [] harness.engine.onMouseFeedback = { feedback.append($0) } harness.engine.handleMouse(sample(.down, start, modifiers: modifiers), windowAtStart: identity(1)) harness.engine.handleMouse(sample(.dragged, end, modifiers: modifiers)) harness.engine.handleMouse(sample(.up, end, modifiers: modifiers)) #expect(harness.engine.snapshot.state.workspaces == initial.workspaces) #expect(feedback.allSatisfy { $0 == .targetChanged(nil) || $0 == .layoutPreview(nil) }) await harness.engine.stop() } @Test(arguments: [false, true]) func disabledMouseStillSuspendsUserDrags(samples: Bool) async throws { var configuration = engineConfiguration() configuration.mouse.enabled = false let harness = try EngineHarness(configuration: configuration) try await harness.start() try harness.add([1, 2]) let source = WindowID(rawValue: 1) let initial = harness.engine.snapshot.state if samples { harness.engine.handleMouse( sample(.down, try #require(initial.frames[source]).center, modifiers: .command), windowAtStart: identity(1)) } else { harness.world.emit(.pointerButton(isDown: true)) } let count = harness.world.allWrites.count harness.world.userFrame(source, frame: Rect(x: 700, y: 50, width: 600, height: 800)) try harness.scheduler.runReady() #expect(harness.world.allWrites.count == count) if samples { harness.engine.handleMouse(sample(.up, Point(x: 900, y: 400), modifiers: .command)) } else { harness.world.emit(.pointerButton(isDown: false)) } try harness.finishMotion() if samples { #expect(harness.world.frames[source] == initial.frames[WindowID(rawValue: 2)]) #expect(harness.world.frames[WindowID(rawValue: 2)] == initial.frames[source]) #expect(harness.engine.snapshot.state.windows[source]?.isFloating == false) } else { #expect(harness.world.frames[source] == initial.frames[source]) #expect(harness.engine.snapshot.state.workspaces == initial.workspaces) } await harness.engine.stop() } @Test func refusalFeedbackReportsOnlyWhenEnteringARefusedPeriod() async throws { let harness = try EngineHarness(configuration: edgeConfiguration()) try await harness.start() try harness.add([1, 2]) harness.engine.send( .minimumSizeChanged(WindowID(rawValue: 2), minimum: LearnedMinimumSize(size: Size(width: 600, height: 100))) ) try harness.scheduler.runReady() let edge = try #require(harness.engine.snapshot.state.tileEdges.first) var feedback: [EngineMouseFeedback] = [] harness.engine.onMouseFeedback = { feedback.append($0) } harness.engine.handleMouse(sample(.down, edge.frame.center)) let reports = harness.log.reports.count for offset in 1...20 { harness.engine.handleMouse(sample(.dragged, Point(x: edge.position + Double(offset * 5), y: 400))) } #expect(feedback.filter { $0 == .refused(.right) }.count == 1) #expect(harness.log.reports.count == reports + 1) harness.engine.handleMouse(sample(.dragged, Point(x: edge.position - 20, y: 400))) harness.engine.handleMouse(sample(.dragged, Point(x: edge.position + 50, y: 400))) #expect(feedback.filter { $0 == .refused(.right) }.count == 2) #expect(harness.log.reports.count == reports + 2) harness.engine.handleMouse(sample(.up, Point(x: edge.position + 50, y: 400))) await harness.engine.stop() } private func edgeConfiguration() -> ParcelConfiguration { var configuration = engineConfiguration() configuration.layout.global.gaps = LayoutGaps(inner: 8, outer: 0) return configuration } private func sample(_ kind: DragSample.Kind, _ point: Point, modifiers: HotKey.Modifiers = []) -> DragSample { DragSample(kind: kind, location: CGPoint(x: point.xCoordinate, y: point.yCoordinate), modifiers: modifiers) } private func identity(_ number: UInt32) -> DragWindow { DragWindow(pid: engineApp.pid, windowNumber: number) } }