Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246import Testing
@testable import ParcelAnimation
@Suite struct AnimatorTests { @Test func newAnimatorIsIdle() { let animator = Animator() #expect(animator.isIdle && animator.activeWindowIDs.isEmpty) #expect(animator.timestamp == nil) }
@Test func scriptedTimelineHasGoldenOutput() throws { var animator = Animator() try animator.schedule([request(start: rect(0), target: rect(100))], at: 0) var origins: [Double] = [] var finalFlags: [Bool] = [] for timestamp in [0, 0.05, 0.1, 0.15, 0.2] { let output = try #require(animator.tick(at: timestamp).first) origins.append(output.rect.originX) finalFlags.append(output.isFinal) } #expect(origins == [0, 68, 94, 100, 100]) #expect(finalFlags == [false, false, false, false, true]) #expect(animator.isIdle) #expect(try animator.tick(at: 1).isEmpty) }
@Test func tickPartitionDoesNotChangePresentation() throws { var dense = Animator() var sparse = Animator() let motion = request(surface: .overlay, token: .drop, velocity: MotionVelocity(horizontal: 700)) try dense.schedule([motion], at: 0) try sparse.schedule([motion], at: 0) for index in 0..<100 { _ = try dense.tick(at: Double(index) / 1000) } #expect(try dense.tick(at: 0.1) == sparse.tick(at: 0.1)) }
@Test func outputsAreSortedByWindowID() throws { var animator = Animator() try animator.schedule([request(window: 9), request(window: 2), request(window: 4)], at: 0) #expect(try animator.tick(at: 0).map(\.windowID) == [2, 4, 9].map { MotionWindowID(rawValue: $0) }) }
@Test func repeatedTickAtSameTimestampDoesNotDuplicateFrame() throws { var animator = Animator() try animator.schedule([request()], at: 0) #expect(try animator.tick(at: 0).count == 1) #expect(try animator.tick(at: 0).isEmpty) }
@Test func retargetKeepsPositionAndVelocity() throws { var animator = Animator() try animator.schedule([request(start: rect(100), target: rect(500))], at: 0) let before = try #require(try animator.presentation(for: testWindow, at: 0.04)) try animator.schedule([request(start: rect(0), target: rect(50))], at: 0.04) let after = try #require(try animator.presentation(for: testWindow, at: 0.04)) #expect(after.frame.rect == before.frame.rect) #expect(near(after.velocity.horizontal, before.velocity.horizontal)) let justAfter = try #require(try animator.presentation(for: testWindow, at: 0.040_001)) #expect(justAfter.velocity.horizontal > 0) #expect(near(justAfter.velocity.horizontal, before.velocity.horizontal, tolerance: 1)) #expect(try animator.tick(at: 0.24).first?.rect.originX == 50) }
@Test func overlayRetargetCarriesSpringVelocity() throws { var animator = Animator() try animator.schedule([request(surface: .overlay, token: .drop)], at: 0) let before = try #require(try animator.presentation(for: testWindow, at: 0.06)) try animator.schedule([request(target: rect(100, 50, 200, 100), surface: .overlay, token: .drop)], at: 0.06) let after = try #require(try animator.presentation(for: testWindow, at: 0.06)) #expect(after.frame.rect == before.frame.rect) #expect(near(after.velocity.horizontal, before.velocity.horizontal)) #expect(near(after.velocity.width, before.velocity.width)) }
@Test func retargetUsesUnroundedInternalPosition() throws { var animator = Animator() try animator.schedule([request(start: rect(0), target: rect(100))], at: 0) let before = try #require(try animator.presentation(for: testWindow, at: 0.01)) try animator.schedule([request(start: before.frame.rect, target: rect(200))], at: 0.01) let plan = try #require(try animator.presentation(for: testWindow, at: 0.010_001)) #expect(near(plan.velocity.horizontal, before.velocity.horizontal, tolerance: 1)) #expect(plan.frame.rect.originX == before.frame.rect.originX) }
@Test func retargetKeepsCurrentSizeUntilItsFinalFrame() throws { var animator = Animator() try animator.schedule([request()], at: 0) try animator.schedule([request(target: rect(600, 400, 500, 300))], at: 0.05) let output = try #require(animator.tick(at: 0.05).first) #expect(output.rect.size == request().start.size) #expect(try animator.tick(at: 1).first?.rect.size == MotionSize(width: 500, height: 300)) }
@Test func retargetRevisionWinsAtSameTick() throws { var animator = Animator() try animator.schedule([request()], at: 0) let old = try #require(animator.tick(at: 0).first) try animator.schedule([request(target: rect(700))], at: 0) let new = try #require(animator.tick(at: 0).first) #expect(new.revision > old.revision) var coalescer = FrameCoalescer() try coalescer.submit(new) #expect(try !coalescer.submit(old)) }
@Test func staggeredOverlayWaitsUntilItsStart() throws { var animator = Animator() try animator.schedule([request(surface: .overlay, token: .shuffle, index: 3)], at: 0) #expect(try animator.tick(at: 0).isEmpty) #expect(try animator.tick(at: 0.035).isEmpty) #expect(try animator.tick(at: MotionToken.shuffle.spec().delay(for: 3)).count == 1) #expect(!animator.isIdle) }
@Test func glanceEmitsAtMostTenRedrawsIncludingFinal() throws { var animator = Animator() try animator.schedule([request(surface: .miniMap, token: .glance)], at: 0) var frames: [MotionFrame] = [] for step in 0...200 { frames += try animator.tick(at: Double(step) / 1000) } #expect(frames.count <= 10) #expect(frames.last?.isFinal == true) }
@Test func disablingMidFlightFlushesFinalFrame() throws { var animator = Animator() try animator.schedule([request(), request(window: 2, surface: .overlay, token: .pop)], at: 0) _ = try animator.tick(at: 0.04) try animator.updatePolicy(MotionPolicy(enabled: false), at: 0.04) let output = try animator.tick(at: 0.04) #expect(output.count == 2 && output.allSatisfy(\.isFinal)) #expect(animator.isIdle) }
@Test func reduceMotionChangeSnapsGeometryAndFadesOverlay() throws { var animator = Animator() try animator.schedule([request(), request(window: 2, surface: .overlay, token: .pop, startOpacity: 0)], at: 0) try animator.updatePolicy(MotionPolicy(reduceMotion: true), at: 0.05) let output = try animator.tick(at: 0.05) #expect(output[0].isFinal) #expect(output[1].rect == request().target) #expect(output[1].scale == 1 && output[1].offsetHorizontal == 0) #expect(try animator.tick(at: 0.17).allSatisfy(\.isFinal)) #expect(animator.isIdle) }
@Test func speedChangeKeepsMomentumAndReplansDeadline() throws { var animator = Animator() try animator.schedule([request()], at: 0) let before = try #require(try animator.presentation(for: testWindow, at: 0.05)) try animator.updateSettings(MotionSettings(speedMultiplier: 2), at: 0.05) let after = try #require(try animator.presentation(for: testWindow, at: 0.05)) #expect(after.frame.rect == before.frame.rect) #expect(near(after.velocity.horizontal, before.velocity.horizontal)) #expect(try animator.tick(at: 0.151).first?.isFinal == true) }
@Test func calmChangeSnapsActiveWindows() throws { var animator = Animator() try animator.schedule([request()], at: 0) try animator.updateSettings(MotionSettings(level: .calm), at: 0.05) #expect(try animator.tick(at: 0.05).first?.isFinal == true) }
@Test func cancelRemovesOnlyChosenWindow() throws { var animator = Animator() try animator.schedule([request(), request(window: 2)], at: 0) animator.cancel(testWindow) #expect(animator.activeWindowIDs == [MotionWindowID(rawValue: 2)]) #expect(try animator.presentation(for: testWindow, at: 0) == nil) }
@Test func cancelAllReturnsToIdle() throws { var animator = Animator() try animator.schedule([request()], at: 0) animator.cancelAll() #expect(animator.isIdle) #expect(try animator.tick(at: 0).isEmpty) }
@Test func badRequestBatchDoesNotPartiallySchedule() throws { var animator = Animator() #expect(throws: MotionError.invalidGeometry) { try animator.schedule([request(), request(window: 2, start: rect(.nan))], at: 0) } #expect(animator.isIdle && animator.timestamp == nil) }
@Test func duplicateWindowBatchIsRejected() { var animator = Animator() #expect(throws: MotionError.duplicateWindow(testWindow)) { try animator.schedule([request(), request()], at: 0) } #expect(animator.isIdle) }
@Test func timestampsMustBeMonotonic() throws { var animator = Animator() try animator.schedule([request()], at: 1) #expect(throws: MotionError.timestampWentBackwards) { try animator.tick(at: 0) } #expect(throws: MotionError.timestampWentBackwards) { try animator.schedule([request()], at: 0) } #expect(throws: MotionError.timestampWentBackwards) { try animator.updatePolicy(MotionPolicy(enabled: false), at: 0) } #expect(animator.policy.enabled) }
@Test func nonfiniteTimestampDoesNotMutateAnimator() { var animator = Animator() #expect(throws: MotionError.invalidTimestamp) { try animator.tick(at: .nan) } #expect(animator.timestamp == nil) }
@Test func delayedFinalTickStillCompletesExactly() throws { var animator = Animator() try animator.schedule([request()], at: 0) let output = try #require(animator.tick(at: 100).first) #expect(output.rect == request().target && output.isFinal) #expect(animator.isIdle) }
@Test func offPolicyEmitsOnlyOneFramePerWindow() throws { var animator = Animator(policy: MotionPolicy(enabled: false)) try animator.schedule([request(), request(window: 2, surface: .overlay, token: .shuffle, index: 10)], at: 0) #expect(try animator.tick(at: 0).count == 2) #expect(try animator.tick(at: 1).isEmpty) }
@Test func animatorAndCoalescerHaveDeterministicScriptedOutput() throws { var animator = Animator() var coalescer = FrameCoalescer(fullFrameRate: 60) try animator.schedule([request(window: 2), request(window: 1)], at: 0) var batches: [FrameBatch] = [] for timestamp in [0, 0.05, 0.1, 0.2] { for output in try animator.tick(at: timestamp) { try coalescer.submit(output) } let batch = try #require(coalescer.takeReady(at: timestamp).first) batches.append(batch) try coalescer.complete(batch, duration: 0.001, at: timestamp + 0.001) } #expect(batches.count == 4) #expect(batches.allSatisfy { $0.frames.count == 2 }) #expect(batches.last?.frames.allSatisfy(\.isFinal) == true) #expect(animator.isIdle && coalescer.pendingCount == 0 && coalescer.inFlightCount == 0) }}