import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import '../native_back.dart'; import 'package:pipelines_app/application/app_router.dart'; import 'package:pipelines_app/core/transport/pipelines_client.dart'; import '../core/transport/pipelines_client_test.dart' show FakeChannel; import '../features/pipelines/fixtures.dart'; import 'chat_registration_test.dart' as chat; import 'pipelines_app_test.dart' as app; import 'pipelines_replies.dart' as pipelines; Future backGesture(WidgetTester tester, String method) async { await tester.binding.defaultBinaryMessenger.handlePlatformMessage( 'flutter/backgesture', const StandardMethodCodec().encodeMethodCall( MethodCall( method, method == 'startBackGesture' ? { 'touchOffset': [5, 300], 'progress': 0.0, 'swipeEdge': 0, } : null, ), ), (_) {}, ); await tester.pump(); } void main() { for (final predictive in [false, true]) { for (final destination in ['pipelines', 'runs', 'chat']) { testWidgets( '$destination: ${predictive ? 'gesture' : 'button'} Back pops dialog, detail, then leaves to the platform', (tester) async { tester.view.physicalSize = const Size(390, 844); tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); final exits = []; tester.binding.defaultBinaryMessenger.setMockMethodCallHandler( SystemChannels.platform, (call) async { if (call.method == 'SystemNavigator.pop') exits.add(call.method); return null; }, ); addTearDown( () => tester.binding.defaultBinaryMessenger .setMockMethodCallHandler(SystemChannels.platform, null), ); final channel = FakeChannel()..readyCompleter.complete(); final provider = PlatformRouteInformationProvider( initialRouteInformation: RouteInformation( uri: Uri.parse('/$destination'), ), ); addTearDown(provider.dispose); await app.pumpApp( tester, channel: channel, client: PipelinesClient(channelFactory: (_) => channel), provider: provider, ); final fixture = PipelineFixture(); final String title; switch (destination) { case 'pipelines': title = 'Alpha review'; await pipelines.replyPipelines(tester, channel, fixture); case 'runs': title = 'Navigation run'; await app.replyList( tester, channel, runs: [ {'id': 1, 'title': title, 'status': 'succeeded'}, ], ); default: title = 'Wire chat'; await chat.replySessions(tester, channel); } await tester.pumpAndSettle(); await tester.tap(find.text(title).first); await tester.pump(); switch (destination) { case 'pipelines': await pipelines.replyPipelineDetail(tester, channel, fixture); case 'runs': await app.replyDetail(tester, channel, 1); default: await chat.replySubscribe(tester, channel); await chat.replyHistory(tester, channel); } await tester.pumpAndSettle(); final detailPath = provider.value.uri.path; expect(detailPath, isNot('/$destination')); final navigator = tester.state( find.byType(Navigator), ); expect(navigator.canPop(), isTrue); if (predictive) { await backGesture(tester, 'startBackGesture'); expect(navigator.userGestureInProgress, isTrue); await backGesture(tester, 'cancelBackGesture'); await tester.pumpAndSettle(); expect(provider.value.uri.path, detailPath); expect(navigator.canPop(), isTrue); } // A real route on top of the detail must consume Back first. showDialog( context: navigator.overlay!.context, builder: (_) => const AlertDialog(title: Text('Navigation dialog')), ); await tester.pumpAndSettle(); await tester.binding.handlePopRoute(); await tester.pumpAndSettle(); expect(find.text('Navigation dialog'), findsNothing); expect(provider.value.uri.path, detailPath); expect(findNativeBack(), findsOneWidget); if (predictive) { await backGesture(tester, 'startBackGesture'); await backGesture(tester, 'commitBackGesture'); } else { await tester.binding.handlePopRoute(); } await tester.pumpAndSettle(); expect(provider.value.uri.path, '/$destination'); expect(find.text(title), findsWidgets); expect(navigator.canPop(), isFalse); expect(exits, isEmpty); // At the main screen Back is the platform's own: one press leaves, // with no exit prompt. await tester.binding.handlePopRoute(); await tester.pumpAndSettle(); expect(find.text('Press back again to close'), findsNothing); expect(exits, ['SystemNavigator.pop']); expect(find.text('Close Pipelines?'), findsNothing); final router = Router.of(navigator.context).routerDelegate as AppRouterDelegate; expect(router.route.path, '/$destination'); }, ); } } }