import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/ui/motion/container_transform_flight.dart'; import 'package:pipelines_app/ui/motion/container_transform_route.dart'; import 'package:pipelines_app/ui/motion/container_transform_source.dart'; const _screen = Size(390, 844); const _rowColor = Color(0xFFE3F2FD); /// A list of three rows; tapping one pushes its page, grown from that row. class _List extends StatelessWidget { const _List({required this.sources}); final ContainerTransformSources sources; @override Widget build(BuildContext context) => ContainerTransformScope( sources: sources, child: ListView( padding: const EdgeInsets.only(top: 200), children: [ for (final id in ['a', 'b', 'c']) ContainerTransformSource( id: id, color: _rowColor, marker: const Color(0xFF1565C0), markerWidth: 3, child: GestureDetector( key: Key('row-$id'), onTap: () => Navigator.of(context).push( ContainerTransformPageRoute( link: sources.link(() => id), builder: (_) => _Page(id: id), ), ), child: SizedBox(height: 72, child: Text('row $id')), ), ), ], ), ); } class _Page extends StatelessWidget { const _Page({required this.id}); final String id; @override Widget build(BuildContext context) => Scaffold( appBar: AppBar(), body: Center(child: Text('page $id')), ); } Future _pump( WidgetTester tester, { bool reduced = false, }) async { tester.view.physicalSize = _screen; tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); final sources = ContainerTransformSources(); final home = _List(sources: sources); Widget media(BuildContext context, Widget? child) => MediaQuery( data: MediaQuery.of(context).copyWith(disableAnimations: reduced), child: child!, ); await tester.pumpWidget(MaterialApp(builder: media, home: home)); return sources; } Rect _surface(WidgetTester tester) => tester.getRect(find.byKey(containerTransformSurfaceKey)); double _opacityAbove(WidgetTester tester, Finder finder) => tester .widget( find.ancestor(of: finder, matching: find.byType(Opacity)).first, ) .opacity; /// Strictly between the row and the full screen on every edge it moves. void _expectBetween(Rect rect, Rect row) { expect(rect.top, lessThan(row.top)); expect(rect.top, greaterThan(0)); expect(rect.height, greaterThan(row.height)); expect(rect.height, lessThan(_screen.height)); } Future _backGesture(String method, [Map? args]) => TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger .handlePlatformMessage( 'flutter/backgesture', const StandardMethodCodec().encodeMethodCall( MethodCall(method, args), ), (_) {}, ); void main() { testWidgets('the row grows into the page and Back shrinks it back', ( tester, ) async { final sources = await _pump(tester); final row = tester.getRect(find.byKey(const Key('row-b'))); expect(sources.originOf('b')!.rect, row); await tester.tap(find.byKey(const Key('row-b'))); await tester.pump(); await tester.pump(const Duration(milliseconds: 16)); // The first frame starts on the row, in the row's colour, with the page // content still hidden. expect(_surface(tester).top, closeTo(row.top, 16)); expect(_opacityAbove(tester, find.text('page b')), 0); await tester.pump(const Duration(milliseconds: 120)); _expectBetween(_surface(tester), row); // The list behind stays where it was. expect(tester.getRect(find.byKey(const Key('row-b'))), row); await tester.pumpAndSettle(); expect(_surface(tester), Offset.zero & _screen); expect(_opacityAbove(tester, find.text('page b')), 1); await tester.pageBack(); await tester.pump(); await tester.pump(const Duration(milliseconds: 110)); _expectBetween(_surface(tester), row); // The page content goes early in the close. expect(_opacityAbove(tester, find.text('page b')), lessThan(1)); await tester.pumpAndSettle(); expect(find.text('page b'), findsNothing); expect(find.byKey(containerTransformSurfaceKey), findsNothing); expect(tester.getRect(find.byKey(const Key('row-b'))), row); }); testWidgets('reduced motion fades the page in place', (tester) async { await _pump(tester, reduced: true); await tester.tap(find.byKey(const Key('row-b'))); await tester.pump(); await tester.pump(const Duration(milliseconds: 60)); expect(_surface(tester), Offset.zero & _screen); final fading = tester .widget( find .ancestor( of: find.byKey(containerTransformSurfaceKey), matching: find.byType(Opacity), ) .first, ) .opacity; expect(fading, inExclusiveRange(0, 1)); await tester.pumpAndSettle(); expect(find.text('page b'), findsOneWidget); await tester.pageBack(); await tester.pump(); await tester.pump(const Duration(milliseconds: 60)); expect(_surface(tester), Offset.zero & _screen); await tester.pumpAndSettle(); expect(find.text('page b'), findsNothing); }); testWidgets('a row that is gone fades the page instead', (tester) async { final sources = await _pump(tester); expect(sources.originOf('missing'), isNull); final navigator = tester.state(find.byType(Navigator)); navigator.push( ContainerTransformPageRoute( link: sources.link(() => 'missing'), builder: (_) => const _Page(id: 'x'), ), ); await tester.pump(); await tester.pump(const Duration(milliseconds: 100)); expect(_surface(tester), Offset.zero & _screen); await tester.pumpAndSettle(); expect(find.text('page x'), findsOneWidget); }); testWidgets('Android predictive back drives the shrink, then commits', ( tester, ) async { await _pump(tester); final row = tester.getRect(find.byKey(const Key('row-b'))); await tester.tap(find.byKey(const Key('row-b'))); await tester.pumpAndSettle(); await _backGesture('startBackGesture', { 'touchOffset': [5.0, 400.0], 'progress': 0.0, 'swipeEdge': 0, }); await tester.pump(); await _backGesture('updateBackGestureProgress', { 'x': 120.0, 'y': 400.0, 'progress': 0.5, 'swipeEdge': 0, }); await tester.pump(); _expectBetween(_surface(tester), row); await _backGesture('cancelBackGesture'); await tester.pumpAndSettle(); expect(find.text('page b'), findsOneWidget); expect(_surface(tester), Offset.zero & _screen); await _backGesture('startBackGesture', { 'touchOffset': [5.0, 400.0], 'progress': 0.0, 'swipeEdge': 0, }); await tester.pump(); await _backGesture('updateBackGestureProgress', { 'x': 200.0, 'y': 400.0, 'progress': 0.7, 'swipeEdge': 0, }); await tester.pump(); await _backGesture('commitBackGesture'); await tester.pumpAndSettle(); expect(find.text('page b'), findsNothing); expect(tester.getRect(find.byKey(const Key('row-b'))), row); }); }