Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210import CoreGraphicsimport Foundationimport ParcelCoreimport ParcelPlatformimport Testing
@testable import ParcelEngine
@MainActor @Suite("Confirmed Activity placements")struct ActivityPlacementTests { private func withHarness(glide: Bool = false, _ body: (EngineHarness, ActivityCollector) async throws -> Void) async throws { let directory = FileManager.default.temporaryDirectory.appendingPathComponent( "parcel-activity-placement-\(UUID())") defer { if FileManager.default.fileExists(atPath: directory.path) { do { try FileManager.default.removeItem(at: directory) } catch { Issue.record(error) } } } let harness = try EngineHarness(configuration: engineConfiguration(glide: glide)) let collector = ActivityCollector( store: ActivityStore(fileURL: directory.appendingPathComponent("activity.json")), scheduler: harness.scheduler, clock: harness.clock) collector.configure(enabled: true) harness.engine.activity = collector try await harness.start() do { try await body(harness, collector) } catch { await harness.engine.stop() collector.flushOnQuit() throw error } await harness.engine.stop() collector.flushOnQuit() }
private func enqueue(_ task: Task<EngineCommandResult, any Error>, harness: EngineHarness) async throws { for _ in 0..<1000 where harness.scheduler.readyCount == 0 { await Task.yield() } try #require(harness.scheduler.readyCount > 0, "Command did not enqueue") try harness.scheduler.runReady() }
@Test(arguments: [false, true], ActivitySource.allCases) func placementsWaitForFinalReadbackAndCountOnce(glide: Bool, source: ActivitySource) async throws { try await withHarness(glide: glide) { harness, collector in try harness.add([1, 2]) try harness.finishMotion() try collector.reset() harness.engine.setAutomaticTilingEnabled(false) harness.world.frames[WindowID(rawValue: 1)] = Rect(x: 70, y: 80, width: 300, height: 400) let before = harness.world.frames harness.world.deferWrites = true let task = Task { try await harness.engine.perform(.retile, activitySource: source) } try await enqueue(task, harness: harness) #expect(collector.ledger.allTime.placements == 0) try harness.finishMotion() #expect(collector.ledger.allTime.placements == 0) while harness.world.pendingWriteCount > 0 { try harness.world.completeNextWrite() } try harness.finishMotion() let result = try await task.value let changed = harness.world.frames.filter { before[$0.key] != $0.value }.count #expect(changed > 0) #expect(result.isAccepted) #expect(collector.ledger.allTime.placements == changed) switch source { case .keyboard: #expect(collector.ledger.allTime.keyboard == changed) case .mouse: #expect(collector.ledger.allTime.mouse == changed) case .other: #expect(collector.ledger.allTime.other == changed) } harness.world.deferWrites = false let unchanged = Task { try await harness.engine.perform(.retile, activitySource: source) } try await enqueue(unchanged, harness: harness) try harness.finishMotion() #expect(try await unchanged.value.reports == [.retiled(moved: 0)]) #expect(collector.ledger.allTime.placements == changed) } }
@Test func automaticTilingCountsActualChangedWindowsNotTheNumberPlanned() async throws { try await withHarness { harness, collector in try harness.add([1, 2]) try harness.finishMotion() let expected = harness.world.frames.values.filter { $0 != initialEngineFrame }.count #expect(collector.ledger.allTime.other == expected) #expect(collector.ledger.allTime.keyboard == 0) #expect(collector.ledger.usedFeatures.isEmpty) } }
@Test(arguments: [false, true]) func refusalsAndMissingReadbacksNeverInventPlacements(timeout: Bool) async throws { try await withHarness { harness, collector in try harness.add([1, 2]) try harness.finishMotion() try collector.reset() harness.world.frames[WindowID(rawValue: 1)] = Rect(x: 70, y: 80, width: 300, height: 400) harness.engine.setAutomaticTilingEnabled(false) harness.world.behaviors[engineApp] = .init(timesOut: timeout, refusesFrames: !timeout) let task = Task { try await harness.engine.perform(.retile, activitySource: .keyboard) } try await enqueue(task, harness: harness) try harness.finishMotion() _ = try await task.value #expect(collector.ledger.allTime.placements == 0) } }
@Test func supportedQuantizedFramesCountAcceptedMovement() async throws { try await withHarness { harness, collector in try harness.add([1, 2]) try harness.finishMotion() try collector.reset() harness.engine.setAutomaticTilingEnabled(false) harness.world.frames[WindowID(rawValue: 1)] = Rect(x: 70, y: 80, width: 300, height: 400) harness.world.behaviors[engineApp] = .init(widthMultiple: 128) let before = harness.world.frames let task = Task { try await harness.engine.perform(.retile, activitySource: .keyboard) } try await enqueue(task, harness: harness) try harness.finishMotion() _ = try await task.value #expect( collector.ledger.allTime.keyboard == harness.world.frames.filter { before[$0.key] != $0.value }.count) } }
@Test(arguments: [false, true]) func resetAndOffExcludeReceiptsAlreadyInFlight(turnOff: Bool) async throws { try await withHarness { harness, collector in try harness.add([1, 2]) try harness.finishMotion() try collector.reset() harness.world.deferWrites = true let task = Task { try await harness.engine.perform(.setLayout(.rows), activitySource: .keyboard) } try await enqueue(task, harness: harness) try harness.finishMotion() if turnOff { collector.configure(enabled: false); collector.configure(enabled: true) } else { try collector.reset() } while harness.world.pendingWriteCount > 0 { try harness.world.completeNextWrite() } _ = try await task.value #expect(collector.ledger.allTime.placements == 0) } }
@Test(arguments: [false, true]) func edgeResizeCountsOnePlacementPerChangedWindowOnlyAfterRelease(cancel: Bool) async throws { try await withHarness { harness, collector in var configuration = harness.engine.configuration configuration.layout.global.gaps = .init(inner: 8, outer: 0) try harness.engine.applyConfiguration(configuration) try harness.add([1, 2]) try harness.finishMotion() try collector.reset() let before = harness.world.frames let edge = try #require(harness.engine.snapshot.state.tileEdges.first) func sample(_ kind: DragSample.Kind, offset: Double) -> DragSample { DragSample(kind: kind, location: CGPoint(x: edge.position + offset, y: edge.frame.center.yCoordinate)) } #expect( harness.engine.handleMouse( DragSample( kind: .down, location: CGPoint(x: edge.frame.center.xCoordinate, y: edge.frame.center.yCoordinate)))) for index in 1...4 { try harness.scheduler.advance(by: 0.02) _ = harness.engine.handleMouse(sample(.dragged, offset: Double(index * 20))) #expect(collector.ledger.allTime.placements == 0) } if cancel { _ = harness.engine.handleMouse(sample(.cancel, offset: 80)) } _ = harness.engine.handleMouse(sample(.up, offset: 80)) try harness.finishMotion() let changed = harness.world.frames.filter { before[$0.key] != $0.value }.count #expect(collector.ledger.allTime.mouse == changed) #expect(collector.ledger.allTime.keyboard == 0) if !cancel { #expect(changed > 0) } } }
@Test func mouseStackingMarksTheFeatureWithoutInventingKeyboardUse() async throws { try await withHarness { harness, collector in try harness.add([1, 2]) try harness.finishMotion() try collector.reset() harness.engine.send( .windowDropped(source: WindowID(rawValue: 1), target: WindowID(rawValue: 2), stacking: true), activitySource: .mouse) try harness.scheduler.runReady() try harness.finishMotion() #expect(collector.ledger.allTime.mouse > 0) #expect(collector.ledger.allTime.keyboard == 0) #expect(collector.ledger.usedFeatures.contains(.stack)) } }
@Test func earlyReadbacksCountChangedTargetsButNotRefusalsOrNoOps() async throws { try await withHarness { harness, collector in let initial = Rect(x: 0, y: 0, width: 100, height: 100) let target = Rect(x: 200, y: 100, width: 200, height: 100) harness.world.emit(.placementReadBack(initial: initial, target: target, final: initial)) harness.world.emit(.placementReadBack(initial: target, target: target, final: target)) harness.world.emit(.placementReadBack(initial: initial, target: target, final: target)) let adjusted = Rect( x: target.minX, y: target.minY, width: target.size.width - 10, height: target.size.height) harness.world.emit(.placementReadBack(initial: initial, target: target, final: adjusted)) #expect(collector.ledger.allTime == ActivityTotals(other: 2)) } }}