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

5 6
// @dart = 2.8

7
import 'package:flutter/semantics.dart';
8
import 'package:flutter_test/flutter_test.dart';
9
import 'package:flutter/cupertino.dart';
10
import 'package:flutter/material.dart';
11

12
class StateMarker extends StatefulWidget {
13
  const StateMarker({ Key key, this.child }) : super(key: key);
14 15 16 17

  final Widget child;

  @override
18
  StateMarkerState createState() => StateMarkerState();
19 20 21 22 23 24 25
}

class StateMarkerState extends State<StateMarker> {
  String marker;

  @override
  Widget build(BuildContext context) {
26 27
    if (widget.child != null)
      return widget.child;
28
    return Container();
29 30 31
  }
}

32 33 34
void main() {
  testWidgets('Can nest apps', (WidgetTester tester) async {
    await tester.pumpWidget(
35
      const MaterialApp(
36
        home: MaterialApp(
37
          home: Text('Home sweet home'),
38 39
        ),
      ),
40 41 42 43 44 45
    );

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

  testWidgets('Focus handling', (WidgetTester tester) async {
46 47 48 49 50
    final FocusNode focusNode = FocusNode();
    await tester.pumpWidget(MaterialApp(
      home: Material(
        child: Center(
          child: TextField(focusNode: focusNode, autofocus: true),
51 52
        ),
      ),
53 54
    ));

55
    expect(focusNode.hasFocus, isTrue);
56
  });
57

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

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

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

72 73
  testWidgets('Can show grid without losing sync', (WidgetTester tester) async {
    await tester.pumpWidget(
74 75
      const MaterialApp(
        home: StateMarker(),
76
      ),
77 78
    );

79
    final StateMarkerState state1 = tester.state(find.byType(StateMarker));
80 81 82
    state1.marker = 'original';

    await tester.pumpWidget(
83
      const MaterialApp(
84
        debugShowMaterialGrid: true,
85
        home: StateMarker(),
86
      ),
87 88
    );

89
    final StateMarkerState state2 = tester.state(find.byType(StateMarker));
90 91 92
    expect(state1, equals(state2));
    expect(state2.marker, equals('original'));
  });
93

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

    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));
135
    expect(buildCounter, 1);
136
    expect(find.text('Y'), findsOneWidget);
137 138
  });

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

211
  testWidgets('Cannot pop the initial route', (WidgetTester tester) async {
212
    await tester.pumpWidget(const MaterialApp(home: Text('Home')));
213 214 215

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

216 217
    final NavigatorState navigator = tester.state(find.byType(Navigator));
    final bool result = await navigator.maybePop();
218 219 220 221 222

    expect(result, isFalse);

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

  testWidgets('Default initialRoute', (WidgetTester tester) async {
225
    await tester.pumpWidget(MaterialApp(routes: <String, WidgetBuilder>{
226 227 228 229 230 231
      '/': (BuildContext context) => const Text('route "/"'),
    }));

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

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

245
    expect(find.text('route "/"', skipOffstage: false), findsOneWidget);
246
    expect(find.text('route "/a"'), findsOneWidget);
247 248
    expect(find.text('route "/a/b"', skipOffstage: false), findsNothing);
    expect(find.text('route "/b"', skipOffstage: false), findsNothing);
249 250
  });

251
  testWidgets('Return value from pop is correct', (WidgetTester tester) async {
252
    Future<Object> result;
253
    await tester.pumpWidget(
254 255
        MaterialApp(
          home: Builder(
256
              builder: (BuildContext context) {
257 258
                return Material(
                  child: RaisedButton(
259 260 261
                      child: const Text('X'),
                      onPressed: () async {
                        result = Navigator.of(context).pushNamed('/a');
262
                      },
263 264 265 266 267 268
                  ),
                );
              }
          ),
          routes: <String, WidgetBuilder>{
            '/a': (BuildContext context) {
269 270
              return Material(
                child: RaisedButton(
271 272 273 274 275 276
                  child: const Text('Y'),
                  onPressed: () {
                    Navigator.of(context).pop('all done');
                  },
                ),
              );
277
            },
278
          },
279
        ),
280 281 282 283 284 285 286 287 288 289 290
    );
    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'));
  });

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

    await tester.pumpWidget(
300
      MaterialApp(
301
        initialRoute: '/a/b',
302
        routes: routes,
303
      ),
304
    );
305 306
    expect(find.text('route "/"', skipOffstage: false), findsOneWidget);
    expect(find.text('route "/a"', skipOffstage: false), findsOneWidget);
307
    expect(find.text('route "/a/b"'), findsOneWidget);
308
    expect(find.text('route "/b"', skipOffstage: false), findsNothing);
309 310 311 312 313 314 315 316 317 318 319
  });

  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(
320
      MaterialApp(
321 322
        initialRoute: '/a/b/c',
        routes: routes,
323
      ),
324 325
    );
    final dynamic exception = tester.takeException();
Dan Field's avatar
Dan Field committed
326
    expect(exception, isA<String>());
327 328 329 330
    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);
331 332 333 334 335 336 337 338 339 340 341
    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(
342
      MaterialApp(
343 344
        initialRoute: '/a',
        routes: routes,
345
      ),
346
    );
347
    expect(find.text('route "/"', skipOffstage: false), findsOneWidget);
348
    expect(find.text('route "/a"'), findsOneWidget);
349
    expect(find.text('route "/b"', skipOffstage: false), findsNothing);
350

351
    // changing initialRoute has no effect
352
    await tester.pumpWidget(
353
      MaterialApp(
354 355
        initialRoute: '/b',
        routes: routes,
356
      ),
357
    );
358
    expect(find.text('route "/"', skipOffstage: false), findsOneWidget);
359
    expect(find.text('route "/a"'), findsOneWidget);
360
    expect(find.text('route "/b"', skipOffstage: false), findsNothing);
361

362
    // removing it has no effect
363
    await tester.pumpWidget(MaterialApp(routes: routes));
364
    expect(find.text('route "/"', skipOffstage: false), findsOneWidget);
365
    expect(find.text('route "/a"'), findsOneWidget);
366
    expect(find.text('route "/b"', skipOffstage: false), findsNothing);
367
  });
368 369 370 371

  testWidgets('onGenerateRoute / onUnknownRoute', (WidgetTester tester) async {
    final List<String> log = <String>[];
    await tester.pumpWidget(
372
      MaterialApp(
373 374
        onGenerateRoute: (RouteSettings settings) {
          log.add('onGenerateRoute ${settings.name}');
375
          return null;
376 377 378
        },
        onUnknownRoute: (RouteSettings settings) {
          log.add('onUnknownRoute ${settings.name}');
379
          return null;
380
        },
381
      ),
382 383 384 385
    );
    expect(tester.takeException(), isFlutterError);
    expect(log, <String>['onGenerateRoute /', 'onUnknownRoute /']);
  });
386

387 388 389 390 391 392 393 394 395 396 397
  testWidgets('MaterialApp with builder and no route information works.', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/18904
    await tester.pumpWidget(
      MaterialApp(
        builder: (BuildContext context, Widget child) {
          return const SizedBox();
        },
      ),
    );
  });

398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
  testWidgets("WidgetsApp don't rebuild routes when MediaQuery updates", (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/37878
    int routeBuildCount = 0;
    int dependentBuildCount = 0;

    await tester.pumpWidget(WidgetsApp(
      color: const Color.fromARGB(255, 255, 255, 255),
      onGenerateRoute: (_) {
        return PageRouteBuilder<void>(pageBuilder: (_, __, ___) {
          routeBuildCount++;
          return Builder(
            builder: (BuildContext context) {
              dependentBuildCount++;
              MediaQuery.of(context);
              return Container();
            },
          );
        });
      },
    ));

    expect(routeBuildCount, equals(1));
    expect(dependentBuildCount, equals(1));

    // didChangeMetrics
    tester.binding.window.physicalSizeTestValue = const Size(42, 42);
    addTearDown(tester.binding.window.clearPhysicalSizeTestValue);

    await tester.pump();

    expect(routeBuildCount, equals(1));
    expect(dependentBuildCount, equals(2));

    // didChangeTextScaleFactor
    tester.binding.window.textScaleFactorTestValue = 42;
    addTearDown(tester.binding.window.clearTextScaleFactorTestValue);

    await tester.pump();

    expect(routeBuildCount, equals(1));
    expect(dependentBuildCount, equals(3));

    // didChangePlatformBrightness
    tester.binding.window.platformBrightnessTestValue = Brightness.dark;
    addTearDown(tester.binding.window.clearPlatformBrightnessTestValue);

    await tester.pump();

    expect(routeBuildCount, equals(1));
    expect(dependentBuildCount, equals(4));

    // didChangeAccessibilityFeatures
    tester.binding.window.accessibilityFeaturesTestValue = MockAccessibilityFeature();
    addTearDown(tester.binding.window.clearAccessibilityFeaturesTestValue);

    await tester.pump();

    expect(routeBuildCount, equals(1));
    expect(dependentBuildCount, equals(5));
  });

459 460
  testWidgets('Can get text scale from media query', (WidgetTester tester) async {
    double textScaleFactor;
461 462
    await tester.pumpWidget(MaterialApp(
      home: Builder(builder:(BuildContext context) {
463
        textScaleFactor = MediaQuery.of(context).textScaleFactor;
464
        return Container();
465 466 467 468 469
      }),
    ));
    expect(textScaleFactor, isNotNull);
    expect(textScaleFactor, equals(1.0));
  });
470 471

  testWidgets('MaterialApp.navigatorKey', (WidgetTester tester) async {
472 473
    final GlobalKey<NavigatorState> key = GlobalKey<NavigatorState>();
    await tester.pumpWidget(MaterialApp(
474 475 476 477
      navigatorKey: key,
      color: const Color(0xFF112233),
      home: const Placeholder(),
    ));
Dan Field's avatar
Dan Field committed
478
    expect(key.currentState, isA<NavigatorState>());
479 480 481
    await tester.pumpWidget(const MaterialApp(
      color: Color(0xFF112233),
      home: Placeholder(),
482 483
    ));
    expect(key.currentState, isNull);
484
    await tester.pumpWidget(MaterialApp(
485 486 487 488
      navigatorKey: key,
      color: const Color(0xFF112233),
      home: const Placeholder(),
    ));
Dan Field's avatar
Dan Field committed
489
    expect(key.currentState, isA<NavigatorState>());
490
  });
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508

  testWidgets('Has default material and cupertino localizations', (WidgetTester tester) async {
    await tester.pumpWidget(
      MaterialApp(
        home: Builder(
          builder: (BuildContext context) {
            return Column(
              children: <Widget>[
                Text(MaterialLocalizations.of(context).selectAllButtonLabel),
                Text(CupertinoLocalizations.of(context).selectAllButtonLabel),
              ],
            );
          },
        ),
      ),
    );

    // Default US "select all" text.
509
    expect(find.text('Select all'), findsOneWidget);
510 511 512
    // Default Cupertino US "select all" text.
    expect(find.text('Select All'), findsOneWidget);
  });
513

514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
  testWidgets('MaterialApp uses regular theme when themeMode is light', (WidgetTester tester) async {
    // Mock the Window to explicitly report a light platformBrightness.
    tester.binding.window.platformBrightnessTestValue = Brightness.light;

    ThemeData appliedTheme;
    await tester.pumpWidget(
      MaterialApp(
        theme: ThemeData(
            brightness: Brightness.light
        ),
        darkTheme: ThemeData(
          brightness: Brightness.dark,
        ),
        themeMode: ThemeMode.light,
        home: Builder(
          builder: (BuildContext context) {
            appliedTheme = Theme.of(context);
            return const SizedBox();
          },
        ),
      ),
    );
    expect(appliedTheme.brightness, Brightness.light);

    // Mock the Window to explicitly report a dark platformBrightness.
    tester.binding.window.platformBrightnessTestValue = Brightness.dark;
    await tester.pumpWidget(
      MaterialApp(
        theme: ThemeData(
            brightness: Brightness.light
        ),
        darkTheme: ThemeData(
          brightness: Brightness.dark,
        ),
        themeMode: ThemeMode.light,
        home: Builder(
          builder: (BuildContext context) {
            appliedTheme = Theme.of(context);
            return const SizedBox();
          },
        ),
      ),
    );
    expect(appliedTheme.brightness, Brightness.light);
  });

  testWidgets('MaterialApp uses darkTheme when themeMode is dark', (WidgetTester tester) async {
    // Mock the Window to explicitly report a light platformBrightness.
    tester.binding.window.platformBrightnessTestValue = Brightness.light;

    ThemeData appliedTheme;
    await tester.pumpWidget(
      MaterialApp(
        theme: ThemeData(
            brightness: Brightness.light
        ),
        darkTheme: ThemeData(
          brightness: Brightness.dark,
        ),
        themeMode: ThemeMode.dark,
        home: Builder(
          builder: (BuildContext context) {
            appliedTheme = Theme.of(context);
            return const SizedBox();
          },
        ),
      ),
    );
    expect(appliedTheme.brightness, Brightness.dark);

    // Mock the Window to explicitly report a dark platformBrightness.
    tester.binding.window.platformBrightnessTestValue = Brightness.dark;
    await tester.pumpWidget(
      MaterialApp(
        theme: ThemeData(
            brightness: Brightness.light
        ),
        darkTheme: ThemeData(
          brightness: Brightness.dark,
        ),
        themeMode: ThemeMode.dark,
        home: Builder(
          builder: (BuildContext context) {
            appliedTheme = Theme.of(context);
            return const SizedBox();
          },
        ),
      ),
    );
    expect(appliedTheme.brightness, Brightness.dark);
  });

  testWidgets('MaterialApp uses regular theme when themeMode is system and platformBrightness is light', (WidgetTester tester) async {
607 608 609 610 611 612 613 614 615 616 617 618 619 620
    // Mock the Window to explicitly report a light platformBrightness.
    final TestWidgetsFlutterBinding binding = tester.binding;
    binding.window.platformBrightnessTestValue = Brightness.light;

    ThemeData appliedTheme;

    await tester.pumpWidget(
      MaterialApp(
        theme: ThemeData(
          brightness: Brightness.light
        ),
        darkTheme: ThemeData(
          brightness: Brightness.dark,
        ),
621
        themeMode: ThemeMode.system,
622 623 624 625 626 627 628 629 630 631 632 633
        home: Builder(
          builder: (BuildContext context) {
            appliedTheme = Theme.of(context);
            return const SizedBox();
          },
        ),
      ),
    );

    expect(appliedTheme.brightness, Brightness.light);
  });

634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
  testWidgets('MaterialApp uses darkTheme when themeMode is system and platformBrightness is dark', (WidgetTester tester) async {
    // Mock the Window to explicitly report a dark platformBrightness.
    tester.binding.window.platformBrightnessTestValue = Brightness.dark;

    ThemeData appliedTheme;
    await tester.pumpWidget(
      MaterialApp(
        theme: ThemeData(
            brightness: Brightness.light
        ),
        darkTheme: ThemeData(
          brightness: Brightness.dark,
        ),
        themeMode: ThemeMode.system,
        home: Builder(
          builder: (BuildContext context) {
            appliedTheme = Theme.of(context);
            return const SizedBox();
          },
        ),
      ),
    );
    expect(appliedTheme.brightness, Brightness.dark);
  });

659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
  testWidgets('MaterialApp uses light theme when platformBrightness is dark but no dark theme is provided', (WidgetTester tester) async {
    // Mock the Window to explicitly report a dark platformBrightness.
    final TestWidgetsFlutterBinding binding = tester.binding;
    binding.window.platformBrightnessTestValue = Brightness.dark;

    ThemeData appliedTheme;

    await tester.pumpWidget(
      MaterialApp(
        theme: ThemeData(
          brightness: Brightness.light
        ),
        home: Builder(
          builder: (BuildContext context) {
            appliedTheme = Theme.of(context);
            return const SizedBox();
          },
        ),
      ),
    );

    expect(appliedTheme.brightness, Brightness.light);
  });

  testWidgets('MaterialApp uses fallback light theme when platformBrightness is dark but no theme is provided at all', (WidgetTester tester) async {
    // Mock the Window to explicitly report a dark platformBrightness.
    final TestWidgetsFlutterBinding binding = tester.binding;
    binding.window.platformBrightnessTestValue = Brightness.dark;

    ThemeData appliedTheme;

    await tester.pumpWidget(
      MaterialApp(
        home: Builder(
          builder: (BuildContext context) {
            appliedTheme = Theme.of(context);
            return const SizedBox();
          },
        ),
      ),
    );

    expect(appliedTheme.brightness, Brightness.light);
  });

  testWidgets('MaterialApp uses fallback light theme when platformBrightness is light and a dark theme is provided', (WidgetTester tester) async {
    // Mock the Window to explicitly report a dark platformBrightness.
    final TestWidgetsFlutterBinding binding = tester.binding;
    binding.window.platformBrightnessTestValue = Brightness.light;

    ThemeData appliedTheme;

    await tester.pumpWidget(
      MaterialApp(
        darkTheme: ThemeData(
          brightness: Brightness.dark,
        ),
        home: Builder(
          builder: (BuildContext context) {
            appliedTheme = Theme.of(context);
            return const SizedBox();
          },
        ),
      ),
    );

    expect(appliedTheme.brightness, Brightness.light);
  });

  testWidgets('MaterialApp uses dark theme when platformBrightness is dark', (WidgetTester tester) async {
    // Mock the Window to explicitly report a dark platformBrightness.
    final TestWidgetsFlutterBinding binding = tester.binding;
    binding.window.platformBrightnessTestValue = Brightness.dark;

    ThemeData appliedTheme;

    await tester.pumpWidget(
      MaterialApp(
        theme: ThemeData(
          brightness: Brightness.light
        ),
        darkTheme: ThemeData(
          brightness: Brightness.dark,
        ),
        home: Builder(
          builder: (BuildContext context) {
            appliedTheme = Theme.of(context);
            return const SizedBox();
          },
        ),
      ),
    );

    expect(appliedTheme.brightness, Brightness.dark);
  });

  testWidgets('MaterialApp switches themes when the Window platformBrightness changes.', (WidgetTester tester) async {
    // Mock the Window to explicitly report a light platformBrightness.
    final TestWidgetsFlutterBinding binding = tester.binding;
    binding.window.platformBrightnessTestValue = Brightness.light;

    ThemeData themeBeforeBrightnessChange;
    ThemeData themeAfterBrightnessChange;

    await tester.pumpWidget(
      MaterialApp(
        theme: ThemeData(
          brightness: Brightness.light
        ),
        darkTheme: ThemeData(
          brightness: Brightness.dark,
        ),
        home: Builder(
          builder: (BuildContext context) {
            if (themeBeforeBrightnessChange == null) {
              themeBeforeBrightnessChange = Theme.of(context);
            } else {
              themeAfterBrightnessChange = Theme.of(context);
            }
            return const SizedBox();
          },
        ),
      ),
    );

    // Switch the platformBrightness from light to dark and pump the widget tree
    // to process changes.
    binding.window.platformBrightnessTestValue = Brightness.dark;
    await tester.pumpAndSettle();

    expect(themeBeforeBrightnessChange.brightness, Brightness.light);
    expect(themeAfterBrightnessChange.brightness, Brightness.dark);
  });
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836

  testWidgets('MaterialApp can customize initial routes', (WidgetTester tester) async {
    final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
    await tester.pumpWidget(
      MaterialApp(
        navigatorKey: navigatorKey,
        onGenerateInitialRoutes: (String initialRoute) {
          expect(initialRoute, '/abc');
          return <Route<void>>[
            PageRouteBuilder<void>(
              pageBuilder: (
                BuildContext context,
                Animation<double> animation,
                Animation<double> secondaryAnimation) {
                return const Text('non-regular page one');
              }
            ),
            PageRouteBuilder<void>(
              pageBuilder: (
                BuildContext context,
                Animation<double> animation,
                Animation<double> secondaryAnimation) {
                return const Text('non-regular page two');
              }
            ),
          ];
        },
        initialRoute: '/abc',
        routes: <String, WidgetBuilder>{
          '/': (BuildContext context) => const Text('regular page one'),
          '/abc': (BuildContext context) => const Text('regular page two'),
        },
      )
    );
    expect(find.text('non-regular page two'), findsOneWidget);
    expect(find.text('non-regular page one'), findsNothing);
    expect(find.text('regular page one'), findsNothing);
    expect(find.text('regular page two'), findsNothing);
    navigatorKey.currentState.pop();
    await tester.pumpAndSettle();
    expect(find.text('non-regular page two'), findsNothing);
    expect(find.text('non-regular page one'), findsOneWidget);
    expect(find.text('regular page one'), findsNothing);
    expect(find.text('regular page two'), findsNothing);
  });
837
}
838

839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
class MockAccessibilityFeature implements AccessibilityFeatures {
  @override
  bool get accessibleNavigation => true;

  @override
  bool get boldText => true;

  @override
  bool get disableAnimations => true;

  @override
  bool get highContrast => true;

  @override
  bool get invertColors => true;

  @override
  bool get reduceMotion => true;
}