Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304import Testing
@testable import ParcelAnimation
@Suite struct FramePlannerTests { @Test func windowDefersGrowthUntilFinalFrame() throws { let plan = try FramePlanner.plan(request(), at: 0) let initial = try plan.sample(at: 0) #expect(initial.frame.rect == request().start) #expect(try plan.sample(at: plan.endTime.nextDown).frame.rect.size == request().start.size) #expect(try plan.sample(at: plan.endTime).frame.rect.size == request().target.size) #expect(initial.velocity.width == 0 && initial.velocity.height == 0) }
@Test func glideIsFrontLoaded() throws { let plan = try FramePlanner.plan(request(start: rect(0), target: rect(100)), at: 0) let first = try plan.sample(at: 1.0 / 60).frame.rect.originX #expect(first >= 25 && first <= 40) #expect(try plan.sample(at: 0.12).frame.rect.originX >= 90) }
@Test func finalFrameReachesExactTarget() throws { let plan = try FramePlanner.plan(request(), at: 10) let sample = try plan.sample(at: 10.2) #expect(sample.frame.rect == request().target) #expect(sample.frame.isFinal) #expect(sample.velocity == .zero) }
@Test func allFrameCoordinatesAreWholePoints() throws { let plan = try FramePlanner.plan( request(start: rect(2.6, 3.8, 70.1, 90.8), target: rect(455.2, 233.9, 190.7, 150.2)), at: 0) for step in 0...100 { let sample = try plan.sample(at: Double(step) / 500).frame.rect #expect(sample.originX == sample.originX.rounded()) #expect(sample.originY == sample.originY.rounded()) #expect(sample.width == sample.width.rounded()) #expect(sample.height == sample.height.rounded()) } }
@Test func minimumSizeIsRoundedUp() throws { let plan = try FramePlanner.plan( request(target: rect(500, 300, 2, 3), minimum: MotionSize(width: 120.1, height: 80.8)), at: 0) for step in 0...50 { let sample = try plan.sample(at: Double(step) / 100).frame.rect #expect(sample.width >= 121 && sample.height >= 81) } }
@Test func overlayNeverShrinksBelowMinimumDuringOvershoot() throws { let plan = try FramePlanner.plan( request( start: rect(50, 50, 800, 600), target: rect(60, 60, 100, 80), minimum: MotionSize(width: 120, height: 100), surface: .overlay, token: .drop), at: 0) for step in 0...100 { let sample = try plan.sample(at: Double(step) / 300).frame.rect #expect(sample.width >= 120 && sample.height >= 100) } }
@Test func overshootCannotCrossAnyScreenEdge() throws { let screen = rect(-800, -500, 800, 500) let plan = try FramePlanner.plan( request( start: rect(-700, -400), target: rect(-20, -20, 600, 400), screen: screen, surface: .overlay, token: .drop, velocity: MotionVelocity(horizontal: 50_000, vertical: 50_000)), at: 0) for step in 0...100 { let sample = try plan.sample(at: Double(step) / 300).frame.rect #expect(sample.originX >= screen.originX && sample.originY >= screen.originY) #expect(sample.maxX <= screen.maxX && sample.maxY <= screen.maxY) } }
@Test func oversizedTargetFitsWithinScreen() throws { let plan = try FramePlanner.plan(request(target: rect(-100, -100, 10_000, 10_000)), at: 0) #expect(try plan.sample(at: 0.2).frame.rect == testScreen) }
@Test func fractionalScreenEdgesRoundInward() throws { let screen = rect(-20.8, -10.2, 100.7, 80.8) let plan = try FramePlanner.plan(request(target: rect(-100, -100, 500, 500), screen: screen), at: 0) #expect(try plan.sample(at: 1).frame.rect == rect(-20, -10, 99, 80)) }
@Test func impossibleMinimumSurfacesError() { #expect(throws: MotionError.minimumSizeExceedsScreen) { try FramePlanner.plan(request(minimum: MotionSize(width: 1921, height: 1)), at: 0) } }
@Test func inwardRoundingCanMakeMinimumImpossible() { #expect(throws: MotionError.minimumSizeExceedsScreen) { try FramePlanner.plan( request(screen: rect(0.1, 0.1, 10, 10), minimum: MotionSize(width: 9.1, height: 9.1)), at: 0) } }
@Test func invalidStartGeometryIsRejected() { #expect(throws: MotionError.invalidGeometry) { try FramePlanner.plan(request(start: rect(.nan)), at: 0) } }
@Test func negativeSizeIsRejected() { #expect(throws: MotionError.invalidGeometry) { try FramePlanner.plan(request(target: rect(0, 0, -1, 2)), at: 0) } }
@Test func invalidScreenIsRejected() { #expect(throws: MotionError.invalidGeometry) { try FramePlanner.plan(request(screen: rect(.infinity)), at: 0) } }
@Test func invalidMinimumIsRejected() { #expect(throws: MotionError.invalidGeometry) { try FramePlanner.plan(request(minimum: MotionSize(width: .nan, height: 0)), at: 0) } }
@Test func invalidTimestampIsRejected() { #expect(throws: MotionError.invalidTimestamp) { try FramePlanner.plan(request(), at: .nan) } }
@Test func invalidSampleTimeIsRejected() throws { let plan = try FramePlanner.plan(request(), at: 0) #expect(throws: MotionError.invalidTimestamp) { try plan.sample(at: .infinity) } }
@Test func invalidOpacityIsRejected() { #expect(throws: MotionError.invalidOpacity) { try FramePlanner.plan(request(startOpacity: 1.1), at: 0) } }
@Test func invalidVelocityIsRejected() { #expect(throws: MotionError.invalidVelocity) { try FramePlanner.plan(request(velocity: MotionVelocity(horizontal: .infinity)), at: 0) } }
@Test func negativeStaggerIndexIsRejected() { #expect(throws: MotionError.invalidGeometry) { try FramePlanner.plan(request(index: -1), at: 0) } }
@Test(arguments: MotionToken.allCases.filter { $0 != .drop }) func routineWindowPlacementNeverBouncesEvenWithOverlayToken(_ token: MotionToken) throws { let plan = try FramePlanner.plan(request(start: rect(100), target: rect(500), token: token, index: 5), at: 0) #expect(plan.startTime == 0) #expect(plan.spec.bounce == 0) var previous = 100.0 for step in 0...100 { let sample = try plan.sample(at: Double(step) / 500).frame #expect(sample.rect.originX >= previous && sample.rect.originX <= 500) #expect(sample.scale == 1 && sample.offsetHorizontal == 0) previous = sample.rect.originX } }
@Test func intentionalWindowSettlementHasOneSmallPositionBounceWithoutResizing() throws { let plan = try FramePlanner.plan( request(start: rect(100, 100), target: rect(500, 100, 400, 200), token: .drop), at: 0) var samples: [Double] = [] for step in 0..<300 { let state = plan.state(at: Double(step) / 1000) samples.append(state.rect.originX) #expect(state.rect.originX >= 100 && state.rect.originX <= 502) #expect(state.rect.originY == 100) #expect(state.rect.size == MotionSize(width: 200, height: 100)) #expect(state.velocity.width == 0 && state.velocity.height == 0) } #expect(samples.contains { $0 > 500.5 }) #expect(samples.firstIndex { $0 > 500 } != nil) #expect(plan.state(at: plan.endTime).rect == rect(500, 100, 400, 200)) let before = plan.state(at: plan.endTime - 1e-6) #expect(near(before.rect.originX, 500, tolerance: 1e-6)) #expect(near(before.velocity.horizontal, 0, tolerance: 0.001)) }
@Test(arguments: [MotionPolicy(enabled: false), MotionPolicy(reduceMotion: true)]) func intentionalWindowSettlementHonorsMotionPolicy(_ policy: MotionPolicy) throws { let plan = try FramePlanner.plan(request(token: .drop), at: 0, policy: policy) #expect(plan.duration == 0) #expect(try plan.sample(at: 0).frame.rect == request().target) #expect(try plan.sample(at: 0).velocity == .zero) }
@Test func windowVelocityCannotCauseOvershoot() throws { let plan = try FramePlanner.plan( request(start: rect(0), target: rect(100), velocity: MotionVelocity(horizontal: 50_000)), at: 0) for step in 0...100 { #expect(try plan.sample(at: Double(step) / 500).frame.rect.originX <= 100) } }
@Test func overlayOvershootIsClampedByToken() throws { let plan = try FramePlanner.plan( request( start: rect(100), target: rect(500), surface: .overlay, token: .drop, velocity: MotionVelocity(horizontal: 50_000)), at: 0) let limit = 400 * plan.spec.overshootLimit for step in 0...100 { let sample = try plan.sample(at: Double(step) / 300).frame.rect.originX #expect(sample >= 100 - limit && sample <= 500 + limit) } }
@Test func dropCarriesReleaseVelocity() throws { let moving = try FramePlanner.plan( request(surface: .overlay, token: .drop, velocity: MotionVelocity(horizontal: 1000)), at: 0) let still = try FramePlanner.plan(request(surface: .overlay, token: .drop), at: 0) #expect(try moving.sample(at: 0).velocity.horizontal == 1000) #expect(try moving.sample(at: 0.01).frame.rect.originX > still.sample(at: 0.01).frame.rect.originX) }
@Test func popAccentReturnsToIdentity() throws { let plan = try FramePlanner.plan(request(surface: .overlay, token: .pop), at: 0) #expect(try plan.sample(at: 0).frame.scale == 0.98) #expect(try plan.sample(at: 0.25).frame.scale == 1) }
@Test func nudgeIsLimitedToFourPoints() throws { let plan = try FramePlanner.plan(request(surface: .overlay, token: .nudge), at: 0) #expect(try plan.sample(at: 0).frame.offsetHorizontal == 4) for step in 0...100 { #expect(try abs(plan.sample(at: Double(step) / 500).frame.offsetHorizontal) <= 4) } #expect(try plan.sample(at: 0.18).frame.offsetHorizontal == 0) }
@Test func stackOffsetIsPerLayer() throws { let plan = try FramePlanner.plan(request(surface: .overlay, token: .stack, index: 3), at: 0) #expect(try plan.sample(at: 0).frame.offsetHorizontal == 6) #expect(try plan.sample(at: 0.28).frame.offsetHorizontal == 0) }
@Test func shuffleDelaysAreCapped() throws { let plan = try FramePlanner.plan(request(surface: .overlay, token: .shuffle, index: 20), at: 0) #expect(plan.startTime == 0.048) #expect(try plan.sample(at: 0).frame.rect == request().start) }
@Test func motionOffEndsEverythingInOneFrame() throws { let plan = try FramePlanner.plan( request(surface: .overlay, token: .shuffle, index: 20, targetOpacity: 0), at: 0, policy: MotionPolicy(enabled: false)) let sample = try plan.sample(at: 0) #expect(sample.frame.rect == request().target) #expect(sample.frame.opacity == 0) #expect(sample.frame.isFinal) #expect(sample.frame.scale == 1 && sample.frame.offsetHorizontal == 0) }
@Test func reduceMotionSnapsWindowsImmediately() throws { let plan = try FramePlanner.plan(request(), at: 0, policy: MotionPolicy(reduceMotion: true)) #expect(try plan.sample(at: 0).frame.isFinal) #expect(try plan.sample(at: 0).frame.rect == request().target) }
@Test func reduceMotionUsesGentleOpacityOnly() throws { let plan = try FramePlanner.plan( request(surface: .overlay, token: .shuffle, index: 10, startOpacity: 0), at: 0, policy: MotionPolicy(reduceMotion: true), settings: MotionSettings(level: .extra, speedMultiplier: 0.1)) #expect(plan.duration == 0.12 && plan.startTime == 0) for step in 0...12 { let sample = try plan.sample(at: Double(step) / 100) #expect(sample.frame.rect == request().target) #expect(sample.frame.scale == 1 && sample.frame.offsetHorizontal == 0) #expect((0...1).contains(sample.frame.opacity)) #expect(sample.velocity == .zero) } #expect(try near(plan.sample(at: 0.06).frame.opacity, 0.75)) }
@Test func normalFadeAlsoOnlyChangesOpacity() throws { let plan = try FramePlanner.plan(request(surface: .overlay, token: .fade, startOpacity: 0), at: 0) #expect(try plan.sample(at: 0.06).frame.rect == request().target) #expect(try near(plan.sample(at: 0.06).frame.opacity, 0.75)) }
@Test func slowAppSnapsButItsOverlayStillMoves() throws { let metrics = try measured(0.041) let window = try FramePlanner.plan(request(metrics: metrics), at: 0) let overlay = try FramePlanner.plan(request(surface: .overlay, token: .pop, metrics: metrics), at: 0) #expect(window.duration == 0) #expect(overlay.duration == 0.25) }
@Test func recentlyTimedOutAppSnaps() throws { let plan = try FramePlanner.plan(request(metrics: measured(0.001, timeout: true, at: 10)), at: 14.9) #expect(plan.duration == 0) }
@Test func recentlyLaunchedAppSnaps() throws { let metrics = AppWriteMetrics(launchedAt: 10) #expect(try FramePlanner.plan(request(metrics: metrics), at: 11).duration == 0) #expect(try FramePlanner.plan(request(metrics: metrics), at: 11.5).duration == 0.2) }
@Test func calmWindowsSnapAndOverlaySpringsAreQuiet() throws { let settings = MotionSettings(level: .calm) let window = try FramePlanner.plan(request(), at: 0, settings: settings) let overlay = try FramePlanner.plan(request(surface: .overlay, token: .nudge), at: 0, settings: settings) #expect(window.duration == 0) #expect(overlay.spec.bounce == 0 && overlay.spec.accent == .none) }}