|
| 1 | +import 'dart:async'; |
| 2 | + |
| 3 | +import 'package:auto_route/auto_route.dart'; |
| 4 | +import 'package:flutter_test/flutter_test.dart'; |
| 5 | +import 'package:material_ui/material_ui.dart'; |
| 6 | + |
| 7 | +import '../back_test_utils.dart'; |
| 8 | + |
| 9 | +// https://github.com/flutter/flutter/issues/188018 |
| 10 | +// Setup: three tabs A, B and C, each with its own stack. |
| 11 | +// Steps: push two pages in tab A, switch to C and push one page, switch to B |
| 12 | +// at its first page, make Android's predictive back gesture twice, then |
| 13 | +// switch to A and go back twice. |
| 14 | +// Should: the gestures on B leave the hidden stacks of A and C alone; back |
| 15 | +// on A then pops A3, then A2, each disposed as it pops, A3 first. |
| 16 | +void main() { |
| 17 | + testWidgets('Predictive back on a tab leaves the hidden tabs and disposes in order', (tester) async { |
| 18 | + final disposed = <String>[]; |
| 19 | + final router = RootStackRouter.build( |
| 20 | + routes: [ |
| 21 | + AutoRoute( |
| 22 | + path: '/', |
| 23 | + page: PageInfo( |
| 24 | + 'HomeRoute', |
| 25 | + builder: (_) => const AutoTabsRouter(routes: [_Route('A'), _Route('B'), _Route('C')]), |
| 26 | + ), |
| 27 | + children: [ |
| 28 | + for (final tab in ['A', 'B', 'C']) |
| 29 | + AutoRoute( |
| 30 | + path: tab.toLowerCase(), |
| 31 | + page: EmptyShellRoute(tab), |
| 32 | + children: [ |
| 33 | + for (var i = 1; i <= 3; i++) AutoRoute(path: i == 1 ? '' : '$i', page: _page('$tab$i', disposed)), |
| 34 | + ], |
| 35 | + ), |
| 36 | + ], |
| 37 | + ), |
| 38 | + ], |
| 39 | + ); |
| 40 | + await tester.pumpWidget(MaterialApp.router(theme: androidTheme, routerConfig: router.config())); |
| 41 | + addTearDown(router.dispose); |
| 42 | + await tester.pumpAndSettle(); |
| 43 | + final tabs = router.innerRouterOf<TabsRouter>('HomeRoute')!; |
| 44 | + List<String?> stackOf(int index) => [for (final page in tabs.stackRouterOfIndex(index)!.stack) page.name]; |
| 45 | + |
| 46 | + Future<void> showTab(int index) async { |
| 47 | + tabs.setActiveIndex(index); |
| 48 | + await tester.pumpAndSettle(); |
| 49 | + } |
| 50 | + |
| 51 | + Future<void> pushInTab(int index, String name) async { |
| 52 | + unawaited(tabs.stackRouterOfIndex(index)!.push(_Route(name))); |
| 53 | + await tester.pumpAndSettle(); |
| 54 | + } |
| 55 | + |
| 56 | + await pushInTab(0, 'A2'); |
| 57 | + await pushInTab(0, 'A3'); |
| 58 | + await showTab(2); |
| 59 | + await pushInTab(2, 'C2'); |
| 60 | + await showTab(1); |
| 61 | + |
| 62 | + await predictiveBack(tester); |
| 63 | + await predictiveBack(tester); |
| 64 | + expect(tester.takeException(), isNull); |
| 65 | + expect(stackOf(0), ['A1', 'A2', 'A3']); |
| 66 | + expect(stackOf(1), ['B1']); |
| 67 | + expect(stackOf(2), ['C1', 'C2']); |
| 68 | + expect(disposed, isEmpty); |
| 69 | + |
| 70 | + await showTab(0); |
| 71 | + expect(find.text('A3'), findsOneWidget); |
| 72 | + await predictiveBack(tester); |
| 73 | + expect(find.text('A2'), findsOneWidget); |
| 74 | + await predictiveBack(tester); |
| 75 | + expect(find.text('A1'), findsOneWidget); |
| 76 | + expect(disposed, ['A3', 'A2']); |
| 77 | + expect(stackOf(2), ['C1', 'C2']); |
| 78 | + }); |
| 79 | +} |
| 80 | + |
| 81 | +PageInfo _page(String name, List<String> disposed) => |
| 82 | + .new(name, builder: (_) => _DisposeRecorder(() => disposed.add(name), child: Text(name))); |
| 83 | + |
| 84 | +class _DisposeRecorder extends StatefulWidget { |
| 85 | + const _DisposeRecorder(this.onDispose, {required this.child}); |
| 86 | + |
| 87 | + final VoidCallback onDispose; |
| 88 | + final Widget child; |
| 89 | + |
| 90 | + @override |
| 91 | + State<_DisposeRecorder> createState() => _DisposeRecorderState(); |
| 92 | +} |
| 93 | + |
| 94 | +class _DisposeRecorderState extends State<_DisposeRecorder> { |
| 95 | + @override |
| 96 | + void dispose() { |
| 97 | + widget.onDispose(); |
| 98 | + super.dispose(); |
| 99 | + } |
| 100 | + |
| 101 | + @override |
| 102 | + Widget build(BuildContext context) => Scaffold(body: widget.child); |
| 103 | +} |
| 104 | + |
| 105 | +class _Route extends PageRouteInfo<void> { |
| 106 | + const _Route(super.name); |
| 107 | +} |
0 commit comments