app_test.dart 12.5 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/material.dart';

8
class StateMarker extends StatefulWidget {
9
  const StateMarker({ Key key, this.child }) : super(key: key);
10 11 12 13 14 15 16 17 18 19 20 21

  final Widget child;

  @override
  StateMarkerState createState() => new StateMarkerState();
}

class StateMarkerState extends State<StateMarker> {
  String marker;

  @override
  Widget build(BuildContext context) {
22 23
    if (widget.child != null)
      return widget.child;
24 25 26 27
    return new Container();
  }
}

28 29 30 31 32
void main() {
  testWidgets('Can nest apps', (WidgetTester tester) async {
    await tester.pumpWidget(
      new MaterialApp(
        home: new MaterialApp(
33
          home: const Text('Home sweet home'),
34 35
        ),
      ),
36 37 38 39 40 41
    );

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

  testWidgets('Focus handling', (WidgetTester tester) async {
42
    final FocusNode focusNode = new FocusNode();
43 44 45
    await tester.pumpWidget(new MaterialApp(
      home: new Material(
        child: new Center(
46 47 48
          child: new TextField(focusNode: focusNode, autofocus: true),
        ),
      ),
49 50
    ));

51
    expect(focusNode.hasFocus, isTrue);
52
  });
53

54 55 56 57 58 59 60
  testWidgets('Can place app inside FocusScope', (WidgetTester tester) async {
    final FocusScopeNode focusScopeNode = new FocusScopeNode();

    await tester.pumpWidget(new FocusScope(
      autofocus: true,
      node: focusScopeNode,
      child: new MaterialApp(
61
        home: const Text('Home'),
62 63 64 65 66 67
      ),
    ));

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

68 69 70
  testWidgets('Can show grid without losing sync', (WidgetTester tester) async {
    await tester.pumpWidget(
      new MaterialApp(
71
        home: const StateMarker(),
72
      ),
73 74
    );

75
    final StateMarkerState state1 = tester.state(find.byType(StateMarker));
76 77 78 79 80
    state1.marker = 'original';

    await tester.pumpWidget(
      new MaterialApp(
        debugShowMaterialGrid: true,
81
        home: const StateMarker(),
82
      ),
83 84
    );

85
    final StateMarkerState state2 = tester.state(find.byType(StateMarker));
86 87 88
    expect(state1, equals(state2));
    expect(state2.marker, equals('original'));
  });
89

90
  testWidgets('Do not rebuild page during a route transition', (WidgetTester tester) async {
91 92 93 94 95 96 97
    int buildCounter = 0;
    await tester.pumpWidget(
      new MaterialApp(
        home: new Builder(
          builder: (BuildContext context) {
            return new Material(
              child: new RaisedButton(
98
                child: const Text('X'),
99 100
                onPressed: () { Navigator.of(context).pushNamed('/next'); },
              ),
101 102 103 104 105 106 107 108
            );
          }
        ),
        routes: <String, WidgetBuilder>{
          '/next': (BuildContext context) {
            return new Builder(
              builder: (BuildContext context) {
                ++buildCounter;
109
                return const Text('Y');
110
              },
111
            );
112 113 114
          },
        },
      ),
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    );

    expect(buildCounter, 0);
    await tester.tap(find.text('X'));
    expect(buildCounter, 0);
    await tester.pump();
    expect(buildCounter, 1);
    await tester.pump(const Duration(milliseconds: 10));
    expect(buildCounter, 1);
    await tester.pump(const Duration(milliseconds: 10));
    expect(buildCounter, 1);
    await tester.pump(const Duration(milliseconds: 10));
    expect(buildCounter, 1);
    await tester.pump(const Duration(milliseconds: 10));
    expect(buildCounter, 1);
    await tester.pump(const Duration(seconds: 1));
131
    expect(buildCounter, 1);
132
    expect(find.text('Y'), findsOneWidget);
133 134
  });

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
  testWidgets('Do rebuild the home page if it changes', (WidgetTester tester) async {
    int buildCounter = 0;
    await tester.pumpWidget(
      new MaterialApp(
        home: new Builder(
          builder: (BuildContext context) {
            ++buildCounter;
            return const Text('A');
          }
        ),
      ),
    );
    expect(buildCounter, 1);
    expect(find.text('A'), findsOneWidget);
    await tester.pumpWidget(
      new MaterialApp(
        home: new Builder(
          builder: (BuildContext context) {
            ++buildCounter;
            return const Text('B');
          }
        ),
      ),
    );
    expect(buildCounter, 2);
    expect(find.text('B'), findsOneWidget);
  });

  testWidgets('Do not rebuild the home page if it does not actually change', (WidgetTester tester) async {
    int buildCounter = 0;
    final Widget home = new Builder(
      builder: (BuildContext context) {
        ++buildCounter;
        return const Placeholder();
      }
    );
    await tester.pumpWidget(
      new MaterialApp(
        home: home,
      ),
    );
    expect(buildCounter, 1);
    await tester.pumpWidget(
      new MaterialApp(
        home: home,
      ),
    );
    expect(buildCounter, 1);
  });

  testWidgets('Do rebuild pages that come from the routes table if the MaterialApp changes', (WidgetTester tester) async {
    int buildCounter = 0;
    final Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{
      '/': (BuildContext context) {
        ++buildCounter;
        return const Placeholder();
      },
    };
    await tester.pumpWidget(
      new MaterialApp(
        routes: routes,
      ),
    );
    expect(buildCounter, 1);
    await tester.pumpWidget(
      new MaterialApp(
        routes: routes,
      ),
    );
    expect(buildCounter, 2);
  });

207
  testWidgets('Cannot pop the initial route', (WidgetTester tester) async {
208
    await tester.pumpWidget(new MaterialApp(home: const Text('Home')));
209 210 211

    expect(find.text('Home'), findsOneWidget);

212 213
    final NavigatorState navigator = tester.state(find.byType(Navigator));
    final bool result = await navigator.maybePop();
214 215 216 217 218

    expect(result, isFalse);

    expect(find.text('Home'), findsOneWidget);
  });
219 220 221 222 223 224 225 226 227

  testWidgets('Default initialRoute', (WidgetTester tester) async {
    await tester.pumpWidget(new MaterialApp(routes: <String, WidgetBuilder>{
      '/': (BuildContext context) => const Text('route "/"'),
    }));

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

228
  testWidgets('One-step initial route', (WidgetTester tester) async {
229 230 231 232
    await tester.pumpWidget(
      new MaterialApp(
        initialRoute: '/a',
        routes: <String, WidgetBuilder>{
233
          '/': (BuildContext context) => const Text('route "/"'),
234
          '/a': (BuildContext context) => const Text('route "/a"'),
235 236
          '/a/b': (BuildContext context) => const Text('route "/a/b"'),
          '/b': (BuildContext context) => const Text('route "/b"'),
237 238 239 240
        },
      )
    );

241
    expect(find.text('route "/"'), findsOneWidget);
242
    expect(find.text('route "/a"'), findsOneWidget);
243 244
    expect(find.text('route "/a/b"'), findsNothing);
    expect(find.text('route "/b"'), findsNothing);
245 246
  });

247
  testWidgets('Return value from pop is correct', (WidgetTester tester) async {
248
    Future<Object> result;
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
    await tester.pumpWidget(
        new MaterialApp(
          home: new Builder(
              builder: (BuildContext context) {
                return new Material(
                  child: new RaisedButton(
                      child: const Text('X'),
                      onPressed: () async {
                        result = Navigator.of(context).pushNamed('/a');
                      }
                  ),
                );
              }
          ),
          routes: <String, WidgetBuilder>{
            '/a': (BuildContext context) {
              return new Material(
                child: new RaisedButton(
                  child: const Text('Y'),
                  onPressed: () {
                    Navigator.of(context).pop('all done');
                  },
                ),
              );
            }
          },
        )
    );
    await tester.tap(find.text('X'));
    await tester.pump();
    await tester.pump(const Duration(seconds: 1));
    expect(find.text('Y'), findsOneWidget);
    await tester.tap(find.text('Y'));
    await tester.pump();

    expect(await result, equals('all done'));
  });

    testWidgets('Two-step initial route', (WidgetTester tester) async {
288 289 290
    final Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{
      '/': (BuildContext context) => const Text('route "/"'),
      '/a': (BuildContext context) => const Text('route "/a"'),
291
      '/a/b': (BuildContext context) => const Text('route "/a/b"'),
292 293 294 295 296
      '/b': (BuildContext context) => const Text('route "/b"'),
    };

    await tester.pumpWidget(
      new MaterialApp(
297
        initialRoute: '/a/b',
298 299 300
        routes: routes,
      )
    );
301
    expect(find.text('route "/"'), findsOneWidget);
302
    expect(find.text('route "/a"'), findsOneWidget);
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
    expect(find.text('route "/a/b"'), findsOneWidget);
    expect(find.text('route "/b"'), findsNothing);
  });

  testWidgets('Initial route with missing step', (WidgetTester tester) async {
    final Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{
      '/': (BuildContext context) => const Text('route "/"'),
      '/a': (BuildContext context) => const Text('route "/a"'),
      '/a/b': (BuildContext context) => const Text('route "/a/b"'),
      '/b': (BuildContext context) => const Text('route "/b"'),
    };

    await tester.pumpWidget(
      new MaterialApp(
        initialRoute: '/a/b/c',
        routes: routes,
      )
    );
    final dynamic exception = tester.takeException();
    expect(exception is String, isTrue);
    expect(exception.startsWith('Could not navigate to initial route.'), isTrue);
    expect(find.text('route "/"'), findsOneWidget);
    expect(find.text('route "/a"'), findsNothing);
    expect(find.text('route "/a/b"'), findsNothing);
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
    expect(find.text('route "/b"'), findsNothing);
  });

  testWidgets('Make sure initialRoute is only used the first time', (WidgetTester tester) async {
    final Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{
      '/': (BuildContext context) => const Text('route "/"'),
      '/a': (BuildContext context) => const Text('route "/a"'),
      '/b': (BuildContext context) => const Text('route "/b"'),
    };

    await tester.pumpWidget(
      new MaterialApp(
        initialRoute: '/a',
        routes: routes,
      )
    );
343
    expect(find.text('route "/"'), findsOneWidget);
344 345 346
    expect(find.text('route "/a"'), findsOneWidget);
    expect(find.text('route "/b"'), findsNothing);

347
    // changing initialRoute has no effect
348 349 350 351 352 353
    await tester.pumpWidget(
      new MaterialApp(
        initialRoute: '/b',
        routes: routes,
      )
    );
354
    expect(find.text('route "/"'), findsOneWidget);
355 356 357
    expect(find.text('route "/a"'), findsOneWidget);
    expect(find.text('route "/b"'), findsNothing);

358
    // removing it has no effect
359
    await tester.pumpWidget(new MaterialApp(routes: routes));
360
    expect(find.text('route "/"'), findsOneWidget);
361 362 363
    expect(find.text('route "/a"'), findsOneWidget);
    expect(find.text('route "/b"'), findsNothing);
  });
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379

  testWidgets('onGenerateRoute / onUnknownRoute', (WidgetTester tester) async {
    final List<String> log = <String>[];
    await tester.pumpWidget(
      new MaterialApp(
        onGenerateRoute: (RouteSettings settings) {
          log.add('onGenerateRoute ${settings.name}');
        },
        onUnknownRoute: (RouteSettings settings) {
          log.add('onUnknownRoute ${settings.name}');
        },
      )
    );
    expect(tester.takeException(), isFlutterError);
    expect(log, <String>['onGenerateRoute /', 'onUnknownRoute /']);
  });
380 381 382 383 384 385 386 387 388 389 390 391

  testWidgets('Can get text scale from media query', (WidgetTester tester) async {
    double textScaleFactor;
    await tester.pumpWidget(new MaterialApp(
      home: new Builder(builder:(BuildContext context) {
        textScaleFactor = MediaQuery.of(context).textScaleFactor;
        return new Container();
      }),
    ));
    expect(textScaleFactor, isNotNull);
    expect(textScaleFactor, equals(1.0));
  });
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412

  testWidgets('MaterialApp.navigatorKey', (WidgetTester tester) async {
    final GlobalKey<NavigatorState> key = new GlobalKey<NavigatorState>();
    await tester.pumpWidget(new MaterialApp(
      navigatorKey: key,
      color: const Color(0xFF112233),
      home: const Placeholder(),
    ));
    expect(key.currentState, const isInstanceOf<NavigatorState>());
    await tester.pumpWidget(new MaterialApp(
      color: const Color(0xFF112233),
      home: const Placeholder(),
    ));
    expect(key.currentState, isNull);
    await tester.pumpWidget(new MaterialApp(
      navigatorKey: key,
      color: const Color(0xFF112233),
      home: const Placeholder(),
    ));
    expect(key.currentState, const isInstanceOf<NavigatorState>());
  });
413
}