import Testing @testable import ParcelAnimation @MainActor private final class TestReduceMotion: ReduceMotionSource { var isEnabled = false var callback: (@MainActor () -> Void)? var isObserving: Bool { callback != nil } func start(changed: @escaping @MainActor () -> Void) { callback = changed } func stop() { callback = nil } func change(to enabled: Bool) { isEnabled = enabled callback?() } } @MainActor private final class TestMotionClock { var timestamp = 0.0 } @Suite @MainActor struct DriverIntegrationTests { @Test func predictedFrameTimeDoesNotRejectCurrentClockCommands() throws { let source = TestDisplayLink() let clock = TestMotionClock() let driver = DisplayLinkDriver( source: source, clock: { clock.timestamp }, onFrames: { _ in }, onFailure: { _ in Issue.record("Unexpected failure") }) try driver.schedule([request()]) source.fire(at: 0.016) clock.timestamp = 0.008 try driver.schedule([request(target: rect(700))]) try driver.updatePolicy(MotionPolicy()) try driver.updateSettings(MotionSettings(speedMultiplier: 2)) #expect(driver.animator.timestamp == 0.016) #expect(driver.isRunning) } @Test func initialSystemSettingIsInjectedBeforeFirstFrame() throws { let source = TestDisplayLink() let system = TestReduceMotion() system.isEnabled = true var frames: [MotionFrame] = [] let driver = DisplayLinkDriver( source: source, reduceMotionSource: system, clock: { 0 }, onFrames: { frames += $0 }, onFailure: { _ in Issue.record("Unexpected failure") }) try driver.schedule([request()]) source.fire(at: 0) #expect(frames.count == 1 && frames[0].isFinal) #expect(driver.animator.policy.reduceMotion) } @Test func systemChangeReplansActiveMotion() throws { let source = TestDisplayLink() let system = TestReduceMotion() let clock = TestMotionClock() var frames: [MotionFrame] = [] let driver = DisplayLinkDriver( source: source, reduceMotionSource: system, clock: { clock.timestamp }, onFrames: { frames += $0 }, onFailure: { _ in Issue.record("Unexpected failure") }) try driver.schedule([request()]) clock.timestamp = 0.04 system.change(to: true) source.fire(at: clock.timestamp) #expect(frames.last?.isFinal == true && !driver.isRunning) clock.timestamp = 1 system.change(to: false) try driver.schedule([request()]) source.fire(at: clock.timestamp) #expect(frames.last?.isFinal == false && driver.isRunning) } @Test func explicitReduceMotionCannotOverrideSystemRequirement() throws { let source = TestDisplayLink() let system = TestReduceMotion() system.isEnabled = true let driver = DisplayLinkDriver( source: source, reduceMotionSource: system, clock: { 0 }, onFrames: { _ in }, onFailure: { _ in Issue.record("Unexpected failure") }) try driver.updatePolicy(MotionPolicy(reduceMotion: false)) #expect(driver.animator.policy.reduceMotion) try driver.updatePolicy(MotionPolicy(reduceMotion: true)) system.change(to: false) #expect(driver.animator.policy.reduceMotion) } @Test func disabledPolicyStillOverridesSystemFade() throws { let source = TestDisplayLink() let system = TestReduceMotion() system.isEnabled = true var frames: [MotionFrame] = [] let driver = DisplayLinkDriver( source: source, reduceMotionSource: system, clock: { 0 }, onFrames: { frames += $0 }, onFailure: { _ in Issue.record("Unexpected failure") }) try driver.updatePolicy(MotionPolicy(enabled: false)) try driver.schedule([request(surface: .overlay, token: .pop, startOpacity: 0)]) source.fire(at: 0) #expect(frames.first?.isFinal == true && frames.first?.opacity == 1) } @Test func destructionRemovesAccessibilityObservation() { let source = TestDisplayLink() let system = TestReduceMotion() var driver: DisplayLinkDriver? = DisplayLinkDriver( source: source, reduceMotionSource: system, onFrames: { _ in }, onFailure: { _ in Issue.record("Unexpected failure") }) #expect(driver != nil && system.isObserving) driver = nil #expect(!system.isObserving) } }