|
| 1 | +// Copyright 2013 The Flutter Authors |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:flutter/foundation.dart'; |
| 6 | +import 'package:flutter_test/flutter_test.dart'; |
| 7 | +import 'package:go_router/go_router.dart'; |
| 8 | +import 'package:material_ui/material_ui.dart'; |
| 9 | + |
| 10 | +import 'test_helpers.dart'; |
| 11 | + |
| 12 | +/// Reads the clip behavior of the [Navigator] identified by [navigatorKey]. |
| 13 | +/// |
| 14 | +/// Offstage widgets are included so that the Navigators of inactive |
| 15 | +/// [StatefulShellBranch]es can be inspected too. |
| 16 | +Clip clipBehaviorOf(WidgetTester tester, GlobalKey<NavigatorState> navigatorKey) => |
| 17 | + tester.widget<Navigator>(find.byKey(navigatorKey, skipOffstage: false)).clipBehavior; |
| 18 | + |
| 19 | +class _ShellRouteData extends ShellRouteData { |
| 20 | + const _ShellRouteData(); |
| 21 | + |
| 22 | + @override |
| 23 | + Widget builder(BuildContext context, GoRouterState state, Widget navigator) => navigator; |
| 24 | +} |
| 25 | + |
| 26 | +/// Reads the description of the `clipBehavior` diagnostics property of the |
| 27 | +/// given [route], or null if the property is hidden at its default value. |
| 28 | +String? clipBehaviorDiagnosticOf(ShellRoute route) { |
| 29 | + final builder = DiagnosticPropertiesBuilder(); |
| 30 | + route.debugFillProperties(builder); |
| 31 | + return builder.properties |
| 32 | + .where((DiagnosticsNode node) => !node.isFiltered(DiagnosticLevel.info)) |
| 33 | + .where((DiagnosticsNode node) => node.name == 'clipBehavior') |
| 34 | + .map((DiagnosticsNode node) => node.toDescription()) |
| 35 | + .firstOrNull; |
| 36 | +} |
| 37 | + |
| 38 | +void main() { |
| 39 | + group('ShellRoute', () { |
| 40 | + test('includes a non-default clipBehavior in debugFillProperties', () { |
| 41 | + ShellRoute shellRoute({Clip clipBehavior = Clip.hardEdge}) => ShellRoute( |
| 42 | + clipBehavior: clipBehavior, |
| 43 | + builder: (_, _, Widget child) => child, |
| 44 | + routes: <RouteBase>[GoRoute(path: '/', builder: (_, _) => const Text('Home'))], |
| 45 | + ); |
| 46 | + |
| 47 | + expect(clipBehaviorDiagnosticOf(shellRoute()), isNull); |
| 48 | + expect(clipBehaviorDiagnosticOf(shellRoute(clipBehavior: Clip.none)), 'none'); |
| 49 | + }); |
| 50 | + |
| 51 | + testWidgets('clips the nested Navigator by default', (WidgetTester tester) async { |
| 52 | + final navigatorKey = GlobalKey<NavigatorState>(debugLabel: 'shell'); |
| 53 | + await createRouter(<RouteBase>[ |
| 54 | + ShellRoute( |
| 55 | + navigatorKey: navigatorKey, |
| 56 | + builder: (_, _, Widget child) => child, |
| 57 | + routes: <RouteBase>[GoRoute(path: '/', builder: (_, _) => const Text('Home'))], |
| 58 | + ), |
| 59 | + ], tester); |
| 60 | + |
| 61 | + expect(clipBehaviorOf(tester, navigatorKey), Clip.hardEdge); |
| 62 | + }); |
| 63 | + |
| 64 | + testWidgets('forwards clipBehavior to the nested Navigator', (WidgetTester tester) async { |
| 65 | + final navigatorKey = GlobalKey<NavigatorState>(debugLabel: 'shell'); |
| 66 | + await createRouter(<RouteBase>[ |
| 67 | + ShellRoute( |
| 68 | + navigatorKey: navigatorKey, |
| 69 | + clipBehavior: Clip.none, |
| 70 | + builder: (_, _, Widget child) => child, |
| 71 | + routes: <RouteBase>[GoRoute(path: '/', builder: (_, _) => const Text('Home'))], |
| 72 | + ), |
| 73 | + ], tester); |
| 74 | + |
| 75 | + expect(clipBehaviorOf(tester, navigatorKey), Clip.none); |
| 76 | + }); |
| 77 | + |
| 78 | + testWidgets('forwards clipBehavior to the nested Navigator when using pageBuilder', ( |
| 79 | + WidgetTester tester, |
| 80 | + ) async { |
| 81 | + final navigatorKey = GlobalKey<NavigatorState>(debugLabel: 'shell'); |
| 82 | + await createRouter(<RouteBase>[ |
| 83 | + ShellRoute( |
| 84 | + navigatorKey: navigatorKey, |
| 85 | + clipBehavior: Clip.antiAlias, |
| 86 | + pageBuilder: (_, _, Widget child) => MaterialPage<void>(child: child), |
| 87 | + routes: <RouteBase>[GoRoute(path: '/', builder: (_, _) => const Text('Home'))], |
| 88 | + ), |
| 89 | + ], tester); |
| 90 | + |
| 91 | + expect(clipBehaviorOf(tester, navigatorKey), Clip.antiAlias); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + group('StatefulShellBranch', () { |
| 96 | + testWidgets('clips the branch Navigator by default', (WidgetTester tester) async { |
| 97 | + final navigatorKey = GlobalKey<NavigatorState>(debugLabel: 'branch'); |
| 98 | + await createRouter(<RouteBase>[ |
| 99 | + StatefulShellRoute.indexedStack( |
| 100 | + builder: (_, _, StatefulNavigationShell shell) => shell, |
| 101 | + branches: <StatefulShellBranch>[ |
| 102 | + StatefulShellBranch( |
| 103 | + navigatorKey: navigatorKey, |
| 104 | + routes: <RouteBase>[GoRoute(path: '/', builder: (_, _) => const Text('A'))], |
| 105 | + ), |
| 106 | + ], |
| 107 | + ), |
| 108 | + ], tester); |
| 109 | + |
| 110 | + expect(clipBehaviorOf(tester, navigatorKey), Clip.hardEdge); |
| 111 | + }); |
| 112 | + |
| 113 | + testWidgets('forwards clipBehavior per branch', (WidgetTester tester) async { |
| 114 | + final keyA = GlobalKey<NavigatorState>(debugLabel: 'a'); |
| 115 | + final keyB = GlobalKey<NavigatorState>(debugLabel: 'b'); |
| 116 | + final root = GlobalKey<NavigatorState>(debugLabel: 'root'); |
| 117 | + await createRouter( |
| 118 | + <RouteBase>[ |
| 119 | + StatefulShellRoute.indexedStack( |
| 120 | + builder: (_, _, StatefulNavigationShell shell) => shell, |
| 121 | + branches: <StatefulShellBranch>[ |
| 122 | + StatefulShellBranch( |
| 123 | + navigatorKey: keyA, |
| 124 | + clipBehavior: Clip.none, |
| 125 | + routes: <RouteBase>[GoRoute(path: '/a', builder: (_, _) => const Text('A'))], |
| 126 | + ), |
| 127 | + StatefulShellBranch( |
| 128 | + navigatorKey: keyB, |
| 129 | + routes: <RouteBase>[GoRoute(path: '/b', builder: (_, _) => const Text('B'))], |
| 130 | + ), |
| 131 | + ], |
| 132 | + ), |
| 133 | + ], |
| 134 | + tester, |
| 135 | + navigatorKey: root, |
| 136 | + initialLocation: '/a', |
| 137 | + ); |
| 138 | + |
| 139 | + expect(clipBehaviorOf(tester, keyA), Clip.none); |
| 140 | + |
| 141 | + root.currentContext!.go('/b'); |
| 142 | + await tester.pumpAndSettle(); |
| 143 | + |
| 144 | + // Each branch keeps its own clip behavior; the loaded branches stay in |
| 145 | + // the tree because StatefulShellRoute preserves their state. |
| 146 | + expect(clipBehaviorOf(tester, keyA), Clip.none); |
| 147 | + expect(clipBehaviorOf(tester, keyB), Clip.hardEdge); |
| 148 | + }); |
| 149 | + |
| 150 | + testWidgets('forwards clipBehavior to preloaded branches', (WidgetTester tester) async { |
| 151 | + final keyA = GlobalKey<NavigatorState>(debugLabel: 'a'); |
| 152 | + final keyB = GlobalKey<NavigatorState>(debugLabel: 'b'); |
| 153 | + await createRouter( |
| 154 | + <RouteBase>[ |
| 155 | + StatefulShellRoute.indexedStack( |
| 156 | + builder: (_, _, StatefulNavigationShell shell) => shell, |
| 157 | + branches: <StatefulShellBranch>[ |
| 158 | + StatefulShellBranch( |
| 159 | + navigatorKey: keyA, |
| 160 | + routes: <RouteBase>[GoRoute(path: '/a', builder: (_, _) => const Text('A'))], |
| 161 | + ), |
| 162 | + StatefulShellBranch( |
| 163 | + navigatorKey: keyB, |
| 164 | + preload: true, |
| 165 | + clipBehavior: Clip.none, |
| 166 | + routes: <RouteBase>[GoRoute(path: '/b', builder: (_, _) => const Text('B'))], |
| 167 | + ), |
| 168 | + ], |
| 169 | + ), |
| 170 | + ], |
| 171 | + tester, |
| 172 | + initialLocation: '/a', |
| 173 | + ); |
| 174 | + await tester.pumpAndSettle(); |
| 175 | + |
| 176 | + expect(clipBehaviorOf(tester, keyB), Clip.none); |
| 177 | + }); |
| 178 | + }); |
| 179 | + |
| 180 | + group('typed routes', () { |
| 181 | + test(r'ShellRouteData.$route forwards clipBehavior', () { |
| 182 | + final routes = <RouteBase>[GoRoute(path: '/', builder: (_, _) => const Text('Home'))]; |
| 183 | + |
| 184 | + expect( |
| 185 | + ShellRouteData.$route(factory: (_) => const _ShellRouteData(), routes: routes).clipBehavior, |
| 186 | + Clip.hardEdge, |
| 187 | + ); |
| 188 | + expect( |
| 189 | + ShellRouteData.$route( |
| 190 | + factory: (_) => const _ShellRouteData(), |
| 191 | + clipBehavior: Clip.none, |
| 192 | + routes: routes, |
| 193 | + ).clipBehavior, |
| 194 | + Clip.none, |
| 195 | + ); |
| 196 | + }); |
| 197 | + |
| 198 | + test(r'StatefulShellBranchData.$branch forwards clipBehavior', () { |
| 199 | + final routes = <RouteBase>[GoRoute(path: '/', builder: (_, _) => const Text('Home'))]; |
| 200 | + |
| 201 | + expect(StatefulShellBranchData.$branch(routes: routes).clipBehavior, Clip.hardEdge); |
| 202 | + expect( |
| 203 | + StatefulShellBranchData.$branch(routes: routes, clipBehavior: Clip.none).clipBehavior, |
| 204 | + Clip.none, |
| 205 | + ); |
| 206 | + }); |
| 207 | + }); |
| 208 | +} |
0 commit comments