import 'package:flutter/cupertino.dart'; 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, required this.cupertino}); final ContainerTransformSources sources; final bool cupertino; @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), cupertino: cupertino, builder: (_) => _Page(id: id, cupertino: cupertino), ), ), child: SizedBox(height: 72, child: Text('row $id')), ), ), ], ), ); } class _Page extends StatelessWidget { const _Page({required this.id, required this.cupertino}); final String id; final bool cupertino; @override Widget build(BuildContext context) => cupertino ? CupertinoPageScaffold( navigationBar: const CupertinoNavigationBar(), child: Center(child: Text('page $id')), ) : Scaffold( appBar: AppBar(), body: Center(child: Text('page $id')), ); } Future _pump( WidgetTester tester, { bool cupertino = false, bool reduced = false, }) async { tester.view.physicalSize = _screen; tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); final sources = ContainerTransformSources(); final home = _List(sources: sources, cupertino: cupertino); Widget media(BuildContext context, Widget? child) => MediaQuery( data: MediaQuery.of(context).copyWith(disableAnimations: reduced), child: child!, ); await tester.pumpWidget( cupertino ? CupertinoApp(builder: media, home: home) : 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'), cupertino: false, builder: (_) => const _Page(id: 'x', cupertino: false), ), ); 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('a Cupertino swipe back shrinks the page with the finger', ( tester, ) async { await _pump(tester, cupertino: true); final row = tester.getRect(find.byKey(const Key('row-b'))); await tester.tap(find.byKey(const Key('row-b'))); await tester.pumpAndSettle(); expect(_surface(tester), Offset.zero & _screen); final gesture = await tester.startGesture(const Offset(5, 400)); await gesture.moveBy(const Offset(20, 0)); await gesture.moveBy(const Offset(100, 0)); await tester.pump(); final early = _surface(tester); _expectBetween(early, row); await gesture.moveBy(const Offset(100, 0)); await tester.pump(); final later = _surface(tester); expect(later.height, lessThan(early.height), reason: 'follows the finger'); // Back toward the start edge, the page grows again. await gesture.moveBy(const Offset(-150, 0)); await tester.pump(); expect(_surface(tester).height, greaterThan(later.height)); await gesture.moveBy(const Offset(200, 0)); await gesture.up(); await tester.pumpAndSettle(); expect(find.text('page b'), findsNothing); expect(find.byKey(const Key('row-b')), findsOneWidget); }); testWidgets('a short Cupertino swipe released early stays on the page', ( tester, ) async { await _pump(tester, cupertino: true); await tester.tap(find.byKey(const Key('row-b'))); await tester.pumpAndSettle(); final gesture = await tester.startGesture(const Offset(5, 400)); await gesture.moveBy(const Offset(20, 0)); await gesture.moveBy(const Offset(40, 0)); await tester.pump(); await gesture.up(); await tester.pumpAndSettle(); expect(find.text('page b'), findsOneWidget); expect(_surface(tester), Offset.zero & _screen); }); 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); }); }