tab_test.dart 10.2 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/cupertino.dart';
6
import 'package:flutter/services.dart';
7 8 9
import 'package:flutter_test/flutter_test.dart';

void main() {
10
  testWidgets('Use home', (WidgetTester tester) async {
11
    await tester.pumpWidget(
12 13
      CupertinoApp(
        home: CupertinoTabView(
xster's avatar
xster committed
14 15
          builder: (BuildContext context) => const Text('home'),
        ),
16 17 18 19 20 21
      ),
    );

    expect(find.text('home'), findsOneWidget);
  });

22
  testWidgets('Use routes', (WidgetTester tester) async {
23
    await tester.pumpWidget(
24 25
      CupertinoApp(
        home: CupertinoTabView(
xster's avatar
xster committed
26 27 28 29
          routes: <String, WidgetBuilder>{
            '/': (BuildContext context) => const Text('first route'),
          },
        ),
30 31 32 33 34 35
      ),
    );

    expect(find.text('first route'), findsOneWidget);
  });

36
  testWidgets('Use home and named routes', (WidgetTester tester) async {
37
    await tester.pumpWidget(
38 39
      CupertinoApp(
        home: CupertinoTabView(
xster's avatar
xster committed
40
          builder: (BuildContext context) {
41
            return CupertinoButton(
xster's avatar
xster committed
42 43
              child: const Text('go to second page'),
              onPressed: () {
44
                Navigator.of(context).pushNamed('/2');
xster's avatar
xster committed
45 46 47 48 49 50 51
              },
            );
          },
          routes: <String, WidgetBuilder>{
            '/2': (BuildContext context) => const Text('second named route'),
          },
        ),
52 53 54 55 56 57 58 59 60 61
      ),
    );

    expect(find.text('go to second page'), findsOneWidget);
    await tester.tap(find.text('go to second page'));
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 300));
    expect(find.text('second named route'), findsOneWidget);
  });

62
  testWidgets('Use onGenerateRoute', (WidgetTester tester) async {
63
    await tester.pumpWidget(
64 65
      CupertinoApp(
        home: CupertinoTabView(
xster's avatar
xster committed
66 67
          onGenerateRoute: (RouteSettings settings) {
            if (settings.name == Navigator.defaultRouteName) {
68
              return CupertinoPageRoute<void>(
xster's avatar
xster committed
69 70 71
                settings: settings,
                builder: (BuildContext context) {
                  return const Text('generated home');
72
                },
73
              );
xster's avatar
xster committed
74
            }
75
            return null;
xster's avatar
xster committed
76 77
          },
        ),
78 79 80 81 82 83
      ),
    );

    expect(find.text('generated home'), findsOneWidget);
  });

84
  testWidgets('Use onUnknownRoute', (WidgetTester tester) async {
85
    late String unknownForRouteCalled;
86
    await tester.pumpWidget(
87 88
      CupertinoApp(
        home: CupertinoTabView(
xster's avatar
xster committed
89
          onUnknownRoute: (RouteSettings settings) {
90
            unknownForRouteCalled = settings.name!;
91
            return null;
xster's avatar
xster committed
92 93
          },
        ),
94 95 96 97 98
      ),
    );

    expect(tester.takeException(), isFlutterError);
    expect(unknownForRouteCalled, '/');
99 100 101 102

    // Work-around for https://github.com/flutter/flutter/issues/65655.
    await tester.pumpWidget(Container());
    expect(tester.takeException(), isAssertionError);
103
  });
104

105
  testWidgets('Can use navigatorKey to navigate', (WidgetTester tester) async {
106 107 108 109 110 111 112 113 114 115 116 117 118
    final GlobalKey<NavigatorState> key = GlobalKey();
    await tester.pumpWidget(
      CupertinoApp(
        home: CupertinoTabView(
          navigatorKey: key,
          builder: (BuildContext context) => const Text('first route'),
          routes: <String, WidgetBuilder>{
            '/2': (BuildContext context) => const Text('second route'),
          },
        ),
      ),
    );

119
    key.currentState!.pushNamed('/2');
120 121 122 123 124 125

    await tester.pump();
    await tester.pump(const Duration(milliseconds: 300));
    expect(find.text('second route'), findsOneWidget);
  });

126
  testWidgets('Changing the key resets the navigator', (WidgetTester tester) async {
127 128 129 130 131 132 133 134
    final GlobalKey<NavigatorState> key = GlobalKey();
    await tester.pumpWidget(
      CupertinoApp(
        home: CupertinoTabView(
          builder: (BuildContext context) {
            return CupertinoButton(
              child: const Text('go to second page'),
              onPressed: () {
135
                Navigator.of(context).pushNamed('/2');
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
              },
            );
          },
          routes: <String, WidgetBuilder>{
            '/2': (BuildContext context) => const Text('second route'),
          },
        ),
      ),
    );

    expect(find.text('go to second page'), findsOneWidget);
    await tester.tap(find.text('go to second page'));
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 300));
    expect(find.text('second route'), findsOneWidget);

    await tester.pumpWidget(
      CupertinoApp(
        home: CupertinoTabView(
          key: key,
          builder: (BuildContext context) {
            return CupertinoButton(
              child: const Text('go to second page'),
              onPressed: () {
160
                Navigator.of(context).pushNamed('/2');
161 162 163 164 165 166 167 168 169 170 171 172 173 174
              },
            );
          },
          routes: <String, WidgetBuilder>{
            '/2': (BuildContext context) => const Text('second route'),
          },
        ),
      ),
    );

    // The stack is gone and we're back to a re-built page 1.
    expect(find.text('go to second page'), findsOneWidget);
    expect(find.text('second route'), findsNothing);
  });
175

176
  testWidgets('Throws FlutterError when onUnknownRoute is null', (WidgetTester tester) async {
177 178 179 180 181 182 183 184 185
    final GlobalKey<NavigatorState> key = GlobalKey();
    await tester.pumpWidget(
      CupertinoApp(
        home: CupertinoTabView(
          navigatorKey: key,
          builder: (BuildContext context) => const Text('first route'),
        ),
      ),
    );
186
    late FlutterError error;
187
    try {
188
      key.currentState!.pushNamed('/2');
189 190 191 192
    } on FlutterError catch (e) {
      error = e;
    }
    expect(error, isNotNull);
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
    expect(
      error.toStringDeep(),
      equalsIgnoringHashCodes(
        'FlutterError\n'
        '   Could not find a generator for route RouteSettings("/2", null) in\n'
        '   the _CupertinoTabViewState.\n'
        '   Generators for routes are searched for in the following order:\n'
        '    1. For the "/" route, the "builder" property, if non-null, is\n'
        '   used.\n'
        '    2. Otherwise, the "routes" table is used, if it has an entry for\n'
        '   the route.\n'
        '    3. Otherwise, onGenerateRoute is called. It should return a\n'
        '   non-null value for any valid route not handled by "builder" and\n'
        '   "routes".\n'
        '    4. Finally if all else fails onUnknownRoute is called.\n'
        '   Unfortunately, onUnknownRoute was not set.\n',
      ),
    );
211 212
  });

213
  testWidgets('Throws FlutterError when onUnknownRoute returns null', (WidgetTester tester) async {
214 215 216 217 218 219 220 221 222 223
    final GlobalKey<NavigatorState> key = GlobalKey<NavigatorState>();
    await tester.pumpWidget(
      CupertinoApp(
        home: CupertinoTabView(
          navigatorKey: key,
          builder: (BuildContext context) => const Text('first route'),
          onUnknownRoute: (_) => null,
        ),
      ),
    );
224
    late FlutterError error;
225
    try {
226
      key.currentState!.pushNamed('/2');
227 228 229 230
    } on FlutterError catch (e) {
      error = e;
    }
    expect(error, isNotNull);
231 232 233 234 235 236 237 238 239 240
    expect(
      error.toStringDeep(),
      equalsIgnoringHashCodes(
        'FlutterError\n'
        '   The onUnknownRoute callback returned null.\n'
        '   When the _CupertinoTabViewState requested the route\n'
        '   RouteSettings("/2", null) from its onUnknownRoute callback, the\n'
        '   callback returned null. Such callbacks must never return null.\n',
      ),
    );
241
  });
242

243
  testWidgets('Navigator of CupertinoTabView restores state', (WidgetTester tester) async {
244 245 246 247 248 249 250 251
    await tester.pumpWidget(
      CupertinoApp(
        restorationScopeId: 'app',
        home: CupertinoTabView(
          restorationScopeId: 'tab',
          builder: (BuildContext context) => CupertinoButton(
            child: const Text('home'),
            onPressed: () {
252
              Navigator.of(context).restorablePushNamed('/2');
253 254 255 256
            },
          ),
          routes: <String, WidgetBuilder>{
            '/2' : (BuildContext context) => const Text('second route'),
257
          },
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
        ),
      ),
    );

    expect(find.text('home'), findsOneWidget);
    await tester.tap(find.text('home'));
    await tester.pumpAndSettle();

    expect(find.text('home'), findsNothing);
    expect(find.text('second route'), findsOneWidget);

    final TestRestorationData data = await tester.getRestorationData();

    await tester.restartAndRestore();

    expect(find.text('home'), findsNothing);
    expect(find.text('second route'), findsOneWidget);

276
    Navigator.of(tester.element(find.text('second route'))).pop();
277 278 279 280 281 282 283 284 285 286
    await tester.pumpAndSettle();

    expect(find.text('home'), findsOneWidget);
    expect(find.text('second route'), findsNothing);

    await tester.restoreFrom(data);

    expect(find.text('home'), findsNothing);
    expect(find.text('second route'), findsOneWidget);

287
    Navigator.of(tester.element(find.text('second route'))).pop();
288 289 290 291 292
    await tester.pumpAndSettle();

    expect(find.text('home'), findsOneWidget);
    expect(find.text('second route'), findsNothing);
  });
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325

  testWidgets('Handles Android back button', (WidgetTester tester) async {
    final GlobalKey<NavigatorState> key = GlobalKey<NavigatorState>();
    await tester.pumpWidget(
      CupertinoApp(
        home: CupertinoTabScaffold(
          tabBar: CupertinoTabBar(
            items: const <BottomNavigationBarItem>[
              BottomNavigationBarItem(label: '', icon: Text('1')),
              BottomNavigationBarItem(label: '', icon: Text('2'))
            ],
          ),
          tabBuilder: (_, int i) => PopScope(
            canPop: false,
            child: CupertinoTabView(
              navigatorKey: key,
              builder: (BuildContext context) => const Text('first route'),
            ),
          ),
        ),
      ),
    );

    expect(find.text('first route'), findsOneWidget);

    // Simulate android back button intent.
    final ByteData message = const JSONMethodCodec().encodeMethodCall(const MethodCall('popRoute'));
    await tester.binding.defaultBinaryMessenger.handlePlatformMessage('flutter/navigation', message, (_) {});
    await tester.pumpAndSettle();

    // Navigator didn't pop, so first route is still visible
    expect(find.text('first route'), findsOneWidget);
  });
326
}