import 'package:cutdex_core/timeline/model.dart'; import 'package:cutdex_timeline_ui/cutdex_timeline_ui.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:timeline_ui_example/main.dart'; void main() { const second = TimelineClock.ticksPerSecond; final toggle = find.byKey(const Key('playback-toggle')); TimelineViewController controller(WidgetTester tester) => tester .widget(find.byKey(const Key('timeline-surface'))) .controller; testWidgets('play, pause and resume advance only presentation time', ( tester, ) async { await tester.pumpWidget(const TimelineExampleApp()); final c = controller(tester); final original = c.version; c.seekTo(2 * second); await tester.tap(toggle); await tester.pump(); await tester.pump(const Duration(seconds: 2)); expect(c.playheadTick, 4 * second); expect(find.byTooltip('暂停 / Pause'), findsOneWidget); await tester.tap(toggle); await tester.pump(const Duration(seconds: 3)); expect(c.playheadTick, 4 * second); expect(find.byTooltip('播放 / Play'), findsOneWidget); await tester.tap(toggle); await tester.pump(); await tester.pump(const Duration(milliseconds: 500)); expect(c.playheadTick, 4 * second + second ~/ 2); expect(c.version, same(original)); expect(c.canUndo, isFalse); await tester.pumpWidget(const SizedBox()); expect(tester.takeException(), isNull); }); testWidgets('seek rebases playback; end pauses and play restarts', ( tester, ) async { await tester.pumpWidget(const TimelineExampleApp()); final c = controller(tester); final end = c.version.kernelVersion.query().storylineSpan(c.storyline); await tester.tap(toggle); await tester.pump(); await tester.pump(const Duration(seconds: 1)); c.seekTo(10 * second); await tester.pump(const Duration(seconds: 1)); expect(c.playheadTick, 11 * second); c.seekTo(end - second ~/ 2); await tester.pump(const Duration(seconds: 1)); expect(c.playheadTick, end); expect(find.byTooltip('播放 / Play'), findsOneWidget); await tester.pump(const Duration(seconds: 2)); expect(c.playheadTick, end); await tester.tap(toggle); await tester.pump(); expect(c.playheadTick, 0); await tester.pump(const Duration(seconds: 1)); expect(c.playheadTick, second); await tester.pumpWidget(const SizedBox()); }); testWidgets('scene switch pauses the previous playhead', (tester) async { await tester.pumpWidget(const TimelineExampleApp()); final short = controller(tester); await tester.tap(toggle); await tester.pump(); await tester.pump(const Duration(seconds: 1)); await tester.tap(find.byTooltip('切换场景 / Switch scene')); await tester.pump(); await tester.pump(const Duration(milliseconds: 300)); await tester.tap(find.text('3 小时 / 3 hours')); await tester.pump(); final stopped = short.playheadTick; await tester.pumpAndSettle(); expect(controller(tester), isNot(same(short))); expect(find.byTooltip('播放 / Play'), findsOneWidget); await tester.pump(const Duration(seconds: 2)); expect(short.playheadTick, stopped); expect(controller(tester).playheadTick, 0); }); testWidgets('backgrounding pauses playback', (tester) async { await tester.pumpWidget(const TimelineExampleApp()); final c = controller(tester); await tester.tap(toggle); await tester.pump(); await tester.pump(const Duration(seconds: 1)); tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive); await tester.pump(const Duration(seconds: 2)); expect(c.playheadTick, second); expect(find.byTooltip('播放 / Play'), findsOneWidget); tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed); await tester.pump(const Duration(seconds: 1)); expect(c.playheadTick, second); }); }