import 'dart:async'; import 'package:flutter/scheduler.dart'; import 'package:flutter/widgets.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/ui/motion/animation_speed.dart'; import 'package:pipelines_app/ui/motion/motion.dart'; class _Store implements AnimationSpeedStore { double? value; bool fail = false; Completer? pending; @override Future load() async => pending == null ? value : pending!.future; @override Future save(double value) async { this.value = value; return !fail; } } void main() { tearDown(() => timeDilation = 1); test( 'device preference restores speed, clamps values, resets and reports save failure', () async { final store = _Store()..value = 2; final controller = AnimationSpeedController(store: store); await controller.restore(); expect(controller.speed, 2); expect(timeDilation, 0.5); controller.setSpeed(0.1); expect(controller.speed, 0.5); expect(timeDilation, 2); await controller.save(); expect(store.value, 0.5); store.fail = true; await controller.save(); expect(controller.saveFailed, isTrue); store.fail = false; await controller.reset(); expect(timeDilation, 1); expect(store.value, 1); expect(controller.saveFailed, isFalse); controller.dispose(); }, ); test( 'late restore cannot overwrite a newer choice and invalid values are ignored', () async { final store = _Store()..pending = Completer(); final controller = AnimationSpeedController(store: store); final restore = controller.restore(); controller.setSpeed(1.5); store.pending!.complete(0.5); await restore; expect(controller.speed, 1.5); controller.setSpeed(double.nan); expect(controller.speed, 1.5); controller.dispose(); }, ); testWidgets( 'animation clock changes ticker speed while wall timers and reduced motion stay independent', (tester) async { final controller = AnimationSpeedController(store: _Store()); await tester.pumpWidget(const SizedBox()); final animation = AnimationController( vsync: tester, duration: const Duration(seconds: 1), ); controller.setSpeed(2); animation.forward(); await tester.pump(); var elapsed = false; final timer = Timer( const Duration(milliseconds: 250), () => elapsed = true, ); await tester.pump(const Duration(milliseconds: 250)); expect(animation.value, closeTo(0.5, 0.02)); expect(elapsed, isTrue); timer.cancel(); await tester.pumpWidget( MediaQuery( data: const MediaQueryData(disableAnimations: true), child: Builder( builder: (context) { expect(Motion.of(context, Motion.standard), Duration.zero); return const SizedBox(); }, ), ), ); animation.dispose(); controller.dispose(); timeDilation = 1; }, ); }