import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/ui/motion/fade_through_stack.dart'; import 'package:pipelines_app/ui/motion/motion.dart'; class _Page extends StatefulWidget { const _Page(this.name); final String name; @override State<_Page> createState() => _PageState(); } class _PageState extends State<_Page> { final controller = TextEditingController(); @override void dispose() { controller.dispose(); super.dispose(); } @override Widget build(BuildContext context) => Column( children: [ Text('page ${widget.name}'), TextField(key: Key('field ${widget.name}'), controller: controller), ElevatedButton( key: Key('button ${widget.name}'), onPressed: () => tapped.add(widget.name), child: Text('tap ${widget.name}'), ), ], ); } final tapped = []; Widget _host(int index, {bool reduced = false, bool lazy = false}) => MaterialApp( home: MediaQuery( data: MediaQueryData(disableAnimations: reduced), child: Scaffold( body: FadeThroughStack( index: index, lazy: lazy, children: const [_Page('a'), _Page('b'), _Page('c')], ), ), ), ); double _opacityOf(WidgetTester tester, String name) => tester .widget( find.ancestor( of: find.text('page $name'), matching: find.byType(Opacity), ), ) .opacity; bool _onstage(WidgetTester tester, String name) => !tester .widget( find .ancestor( of: find.text('page $name', skipOffstage: false), matching: find.byType(Offstage, skipOffstage: false), ) .first, ) .offstage; void main() { setUp(tapped.clear); testWidgets('fades the old page out, then the new page in', (tester) async { await tester.pumpWidget(_host(0)); await tester.pumpWidget(_host(1)); final total = Motion.tabFadeThrough; await tester.pump(total * 0.2); expect(_opacityOf(tester, 'a'), inExclusiveRange(0.0, 1.0)); expect(_opacityOf(tester, 'b'), 0.0); await tester.pump(total * 0.5); expect(_opacityOf(tester, 'a'), 0.0); expect(_opacityOf(tester, 'b'), inExclusiveRange(0.0, 1.0)); await tester.pump(total); expect(_opacityOf(tester, 'b'), 1.0); expect(_onstage(tester, 'a'), isFalse); }); testWidgets('a page keeps its state across switches', (tester) async { await tester.pumpWidget(_host(0)); await tester.enterText(find.byKey(const Key('field a')), 'draft'); final before = tester.state(find.byType(_Page).first); await tester.pumpWidget(_host(1)); await tester.pump(Motion.tabFadeThrough * 2); await tester.pumpWidget(_host(0)); await tester.pump(Motion.tabFadeThrough * 2); expect(tester.state(find.byType(_Page).first), same(before)); expect(find.text('draft'), findsOneWidget); }); testWidgets('only the active page takes taps', (tester) async { await tester.pumpWidget(_host(0)); await tester.pumpWidget(_host(1)); await tester.pump(Motion.tabFadeThrough * 0.2); // The outgoing page is still visible but must not take the tap; it // reaches the active page's button underneath instead. await tester.tap(find.byKey(const Key('button a')), warnIfMissed: false); expect(tapped, isNot(contains('a'))); expect(tapped, ['b']); }); testWidgets('rapid switching ends with only the last page shown', ( tester, ) async { await tester.pumpWidget(_host(0)); await tester.pumpWidget(_host(1)); await tester.pump(const Duration(milliseconds: 30)); await tester.pumpWidget(_host(2)); await tester.pump(const Duration(milliseconds: 30)); await tester.pumpWidget(_host(0)); await tester.pump(Motion.tabFadeThrough * 2); expect(_onstage(tester, 'a'), isTrue); expect(_onstage(tester, 'b'), isFalse); expect(_onstage(tester, 'c'), isFalse); expect(_opacityOf(tester, 'a'), 1.0); }); testWidgets('reduced motion switches instantly', (tester) async { await tester.pumpWidget(_host(0, reduced: true)); await tester.pumpWidget(_host(1, reduced: true)); await tester.pump(); expect(_opacityOf(tester, 'b'), 1.0); }); testWidgets('a lazy stack builds the shown page, then the rest when idle', ( tester, ) async { await tester.pumpWidget(_host(0, lazy: true)); expect(find.text('page a'), findsOneWidget); expect(find.text('page b', skipOffstage: false), findsNothing); expect(find.text('page c', skipOffstage: false), findsNothing); // One page per idle frame: b first, then c in a later frame. var frames = 0; Future frameUntil(String page) async { while (find.text(page, skipOffstage: false).evaluate().isEmpty) { expect(frames++, lessThan(20)); await tester.pump(const Duration(milliseconds: 16)); } } await frameUntil('page b'); expect(find.text('page c', skipOffstage: false), findsNothing); await frameUntil('page c'); expect(_onstage(tester, 'b'), isFalse); expect(_onstage(tester, 'c'), isFalse); }); testWidgets('a lazy stack keeps each page and its state', (tester) async { await tester.pumpWidget(_host(0, lazy: true)); await tester.enterText(find.byKey(const Key('field a')), 'draft'); final first = tester.state(find.byType(_Page).first); await tester.pumpWidget(_host(2, lazy: true)); await tester.pump(Motion.tabFadeThrough * 2); expect(find.text('page c'), findsOneWidget); expect(_opacityOf(tester, 'c'), 1.0); await tester.pumpWidget(_host(0, lazy: true)); await tester.pumpAndSettle(); expect(tester.state(find.byType(_Page).first), same(first)); expect(find.text('draft'), findsOneWidget); }); }