popup_menu_test.dart 23.6 KB
Newer Older
1 2 3 4
// 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.

5
import 'dart:ui' show window, SemanticsFlag;
Ian Hickson's avatar
Ian Hickson committed
6

7
import 'package:flutter_test/flutter_test.dart';
8
import 'package:flutter/material.dart';
9

10 11
import '../widgets/semantics_tester.dart';

12
void main() {
13
  testWidgets('Navigator.push works within a PopupMenuButton', (WidgetTester tester) async {
14
    final Key targetKey = UniqueKey();
15
    await tester.pumpWidget(
16
      MaterialApp(
17
        routes: <String, WidgetBuilder>{
18
          '/next': (BuildContext context) {
19
            return const Text('Next');
20
          },
21
        },
22 23 24
        home: Material(
          child: Center(
            child: Builder(
25
              key: targetKey,
26
              builder: (BuildContext context) {
27
                return PopupMenuButton<int>(
28 29 30 31 32
                  onSelected: (int value) {
                    Navigator.pushNamed(context, '/next');
                  },
                  itemBuilder: (BuildContext context) {
                    return <PopupMenuItem<int>>[
33
                      const PopupMenuItem<int>(
34
                        value: 1,
35
                        child: Text('One'),
36
                      ),
37
                    ];
38
                  },
39
                );
40 41 42 43 44
              },
            ),
          ),
        ),
      ),
45 46
    );

47
    await tester.tap(find.byKey(targetKey));
48 49 50 51 52 53 54
    await tester.pump();
    await tester.pump(const Duration(seconds: 1)); // finish the menu animation

    expect(find.text('One'), findsOneWidget);
    expect(find.text('Next'), findsNothing);

    await tester.tap(find.text('One'));
55 56 57
    await tester.pump(); // return the future
    await tester.pump(); // start the navigation
    await tester.pump(const Duration(seconds: 1)); // end the navigation
58 59 60 61

    expect(find.text('One'), findsNothing);
    expect(find.text('Next'), findsOneWidget);
  });
62

63 64 65
  testWidgets('PopupMenuButton calls onCanceled callback when an item is not selected', (WidgetTester tester) async {
    int cancels = 0;
    BuildContext popupContext;
66 67
    final Key noCallbackKey = UniqueKey();
    final Key withCallbackKey = UniqueKey();
68 69

    await tester.pumpWidget(
70 71 72
      MaterialApp(
        home: Material(
          child: Column(
73
            children: <Widget>[
74
              PopupMenuButton<int>(
75 76 77 78 79
                key: noCallbackKey,
                itemBuilder: (BuildContext context) {
                  return <PopupMenuEntry<int>>[
                    const PopupMenuItem<int>(
                      value: 1,
80
                      child: Text('Tap me please!'),
81 82 83 84
                    ),
                  ];
                },
              ),
85
              PopupMenuButton<int>(
86 87 88 89 90 91 92
                key: withCallbackKey,
                onCanceled: () => cancels++,
                itemBuilder: (BuildContext context) {
                  popupContext = context;
                  return <PopupMenuEntry<int>>[
                    const PopupMenuItem<int>(
                      value: 1,
93
                      child: Text('Tap me, too!'),
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
                    ),
                  ];
                },
              ),
            ],
          ),
        ),
      ),
    );

    // Make sure everything works if no callback is provided
    await tester.tap(find.byKey(noCallbackKey));
    await tester.pump();
    await tester.pump(const Duration(seconds: 1));
    await tester.tapAt(const Offset(0.0, 0.0));
    await tester.pump();
    expect(cancels, equals(0));

    // Make sure callback is called when a non-selection tap occurs
    await tester.tap(find.byKey(withCallbackKey));
    await tester.pump();
    await tester.pump(const Duration(seconds: 1));
    await tester.tapAt(const Offset(0.0, 0.0));
    await tester.pump();
    expect(cancels, equals(1));

    // Make sure callback is called when back navigation occurs
    await tester.tap(find.byKey(withCallbackKey));
    await tester.pump();
    await tester.pump(const Duration(seconds: 1));
    Navigator.of(popupContext).pop();
    await tester.pump();
    expect(cancels, equals(2));
  });

129 130
  testWidgets('PopupMenuButton is horizontal on iOS', (WidgetTester tester) async {
    Widget build(TargetPlatform platform) {
131 132 133 134
      return MaterialApp(
        theme: ThemeData(platform: platform),
        home: Scaffold(
          appBar: AppBar(
135
            actions: <Widget>[
136
              PopupMenuButton<int>(
137 138 139 140
                itemBuilder: (BuildContext context) {
                  return <PopupMenuItem<int>>[
                    const PopupMenuItem<int>(
                      value: 1,
141
                      child: Text('One'),
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
                    ),
                  ];
                },
              ),
            ],
          ),
        ),
      );
    }

    await tester.pumpWidget(build(TargetPlatform.android));

    expect(find.byIcon(Icons.more_vert), findsOneWidget);
    expect(find.byIcon(Icons.more_horiz), findsNothing);

    await tester.pumpWidget(build(TargetPlatform.iOS));
    await tester.pumpAndSettle(); // Run theme change animation.

    expect(find.byIcon(Icons.more_vert), findsNothing);
    expect(find.byIcon(Icons.more_horiz), findsOneWidget);
  });
163 164 165 166 167 168 169

  group('PopupMenuButton with Icon', () {
    // Helper function to create simple and valid popup menus.
    List<PopupMenuItem<int>> simplePopupMenuItemBuilder(BuildContext context) {
      return <PopupMenuItem<int>>[
        const PopupMenuItem<int>(
            value: 1,
170
            child: Text('1'),
171 172 173 174 175 176
        ),
      ];
    }

    testWidgets('PopupMenuButton fails when given both child and icon', (WidgetTester tester) async {
      expect(() {
177
        PopupMenuButton<int>(
178 179 180 181
            child: const Text('heyo'),
            icon: const Icon(Icons.view_carousel),
            itemBuilder: simplePopupMenuItemBuilder,
        );
182
      }, throwsA(isInstanceOf<AssertionError>()));
183 184 185
    });

    testWidgets('PopupMenuButton creates IconButton when given an icon', (WidgetTester tester) async {
186
      final PopupMenuButton<int> button = PopupMenuButton<int>(
187 188 189 190
        icon: const Icon(Icons.view_carousel),
        itemBuilder: simplePopupMenuItemBuilder,
      );

191 192 193
      await tester.pumpWidget(MaterialApp(
          home: Scaffold(
            appBar: AppBar(
194 195 196 197 198 199 200 201 202 203
              actions: <Widget>[button],
            ),
          ),
        ),
      );

      expect(find.byType(IconButton), findsOneWidget);
      expect(find.byIcon(Icons.view_carousel), findsOneWidget);
    });
  });
Ian Hickson's avatar
Ian Hickson committed
204 205

  testWidgets('PopupMenu positioning', (WidgetTester tester) async {
206
    final Widget testButton = PopupMenuButton<int>(
Ian Hickson's avatar
Ian Hickson committed
207 208
      itemBuilder: (BuildContext context) {
        return <PopupMenuItem<int>>[
209 210 211
          const PopupMenuItem<int>(value: 1, child: Text('AAA')),
          const PopupMenuItem<int>(value: 2, child: Text('BBB')),
          const PopupMenuItem<int>(value: 3, child: Text('CCC')),
Ian Hickson's avatar
Ian Hickson committed
212 213 214 215 216
        ];
      },
      child: const SizedBox(
        height: 100.0,
        width: 100.0,
217
        child: Text('XXX'),
Ian Hickson's avatar
Ian Hickson committed
218 219
      ),
    );
220 221 222 223 224 225
    final WidgetPredicate popupMenu = (Widget widget) {
      final String widgetType = widget.runtimeType.toString();
      // TODO(mraleph): Remove the old case below.
      return widgetType == '_PopupMenu<int>' // normal case
          || widgetType == '_PopupMenu'; // for old versions of Dart that don't reify method type arguments
    };
Ian Hickson's avatar
Ian Hickson committed
226

227 228
    Future<void> openMenu(TextDirection textDirection, Alignment alignment) async {
      return TestAsyncUtils.guard<void>(() async {
229 230
        await tester.pumpWidget(Container()); // reset in case we had a menu up already
        await tester.pumpWidget(TestApp(
Ian Hickson's avatar
Ian Hickson committed
231
          textDirection: textDirection,
232
          child: Align(
Ian Hickson's avatar
Ian Hickson committed
233 234 235 236 237 238 239 240 241
            alignment: alignment,
            child: testButton,
          ),
        ));
        await tester.tap(find.text('XXX'));
        await tester.pump();
      });
    }

242
    Future<void> testPositioningDown(
Ian Hickson's avatar
Ian Hickson committed
243 244 245 246 247 248
      WidgetTester tester,
      TextDirection textDirection,
      Alignment alignment,
      TextDirection growthDirection,
      Rect startRect,
    ) {
249
      return TestAsyncUtils.guard<void>(() async {
Ian Hickson's avatar
Ian Hickson committed
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 288 289 290 291 292 293 294 295 296 297 298
        await openMenu(textDirection, alignment);
        Rect rect = tester.getRect(find.byWidgetPredicate(popupMenu));
        expect(rect, startRect);
        bool doneVertically = false;
        bool doneHorizontally = false;
        do {
          await tester.pump(const Duration(milliseconds: 20));
          final Rect newRect = tester.getRect(find.byWidgetPredicate(popupMenu));
          expect(newRect.top, rect.top);
          if (doneVertically) {
            expect(newRect.bottom, rect.bottom);
          } else {
            if (newRect.bottom == rect.bottom) {
              doneVertically = true;
            } else {
              expect(newRect.bottom, greaterThan(rect.bottom));
            }
          }
          switch (growthDirection) {
            case TextDirection.rtl:
              expect(newRect.right, rect.right);
              if (doneHorizontally) {
                expect(newRect.left, rect.left);
              } else {
                if (newRect.left == rect.left) {
                  doneHorizontally = true;
                } else {
                  expect(newRect.left, lessThan(rect.left));
                }
              }
              break;
            case TextDirection.ltr:
              expect(newRect.left, rect.left);
              if (doneHorizontally) {
                expect(newRect.right, rect.right);
              } else {
                if (newRect.right == rect.right) {
                  doneHorizontally = true;
                } else {
                  expect(newRect.right, greaterThan(rect.right));
                }
              }
              break;
          }
          rect = newRect;
        } while (tester.binding.hasScheduledFrame);
      });
    }

299
    Future<void> testPositioningDownThenUp(
Ian Hickson's avatar
Ian Hickson committed
300 301 302 303 304 305
      WidgetTester tester,
      TextDirection textDirection,
      Alignment alignment,
      TextDirection growthDirection,
      Rect startRect,
    ) {
306
      return TestAsyncUtils.guard<void>(() async {
Ian Hickson's avatar
Ian Hickson committed
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
        await openMenu(textDirection, alignment);
        Rect rect = tester.getRect(find.byWidgetPredicate(popupMenu));
        expect(rect, startRect);
        int verticalStage = 0; // 0=down, 1=up, 2=done
        bool doneHorizontally = false;
        do {
          await tester.pump(const Duration(milliseconds: 20));
          final Rect newRect = tester.getRect(find.byWidgetPredicate(popupMenu));
          switch (verticalStage) {
            case 0:
              if (newRect.top < rect.top) {
                verticalStage = 1;
                expect(newRect.bottom, greaterThanOrEqualTo(rect.bottom));
                break;
              }
              expect(newRect.top, rect.top);
              expect(newRect.bottom, greaterThan(rect.bottom));
              break;
            case 1:
              if (newRect.top == rect.top) {
                verticalStage = 2;
                expect(newRect.bottom, rect.bottom);
                break;
              }
              expect(newRect.top, lessThan(rect.top));
              expect(newRect.bottom, rect.bottom);
              break;
            case 2:
              expect(newRect.bottom, rect.bottom);
              expect(newRect.top, rect.top);
              break;
            default:
              assert(false);
          }
          switch (growthDirection) {
            case TextDirection.rtl:
              expect(newRect.right, rect.right);
              if (doneHorizontally) {
                expect(newRect.left, rect.left);
              } else {
                if (newRect.left == rect.left) {
                  doneHorizontally = true;
                } else {
                  expect(newRect.left, lessThan(rect.left));
                }
              }
              break;
            case TextDirection.ltr:
              expect(newRect.left, rect.left);
              if (doneHorizontally) {
                expect(newRect.right, rect.right);
              } else {
                if (newRect.right == rect.right) {
                  doneHorizontally = true;
                } else {
                  expect(newRect.right, greaterThan(rect.right));
                }
              }
              break;
          }
          rect = newRect;
        } while (tester.binding.hasScheduledFrame);
      });
    }

372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
    await testPositioningDown(tester, TextDirection.ltr, Alignment.topRight, TextDirection.rtl, Rect.fromLTWH(792.0, 8.0, 0.0, 0.0));
    await testPositioningDown(tester, TextDirection.rtl, Alignment.topRight, TextDirection.rtl, Rect.fromLTWH(792.0, 8.0, 0.0, 0.0));
    await testPositioningDown(tester, TextDirection.ltr, Alignment.topLeft, TextDirection.ltr, Rect.fromLTWH(8.0, 8.0, 0.0, 0.0));
    await testPositioningDown(tester, TextDirection.rtl, Alignment.topLeft, TextDirection.ltr, Rect.fromLTWH(8.0, 8.0, 0.0, 0.0));
    await testPositioningDown(tester, TextDirection.ltr, Alignment.topCenter, TextDirection.ltr, Rect.fromLTWH(350.0, 8.0, 0.0, 0.0));
    await testPositioningDown(tester, TextDirection.rtl, Alignment.topCenter, TextDirection.rtl, Rect.fromLTWH(450.0, 8.0, 0.0, 0.0));
    await testPositioningDown(tester, TextDirection.ltr, Alignment.centerRight, TextDirection.rtl, Rect.fromLTWH(792.0, 250.0, 0.0, 0.0));
    await testPositioningDown(tester, TextDirection.rtl, Alignment.centerRight, TextDirection.rtl, Rect.fromLTWH(792.0, 250.0, 0.0, 0.0));
    await testPositioningDown(tester, TextDirection.ltr, Alignment.centerLeft, TextDirection.ltr, Rect.fromLTWH(8.0, 250.0, 0.0, 0.0));
    await testPositioningDown(tester, TextDirection.rtl, Alignment.centerLeft, TextDirection.ltr, Rect.fromLTWH(8.0, 250.0, 0.0, 0.0));
    await testPositioningDown(tester, TextDirection.ltr, Alignment.center, TextDirection.ltr, Rect.fromLTWH(350.0, 250.0, 0.0, 0.0));
    await testPositioningDown(tester, TextDirection.rtl, Alignment.center, TextDirection.rtl, Rect.fromLTWH(450.0, 250.0, 0.0, 0.0));
    await testPositioningDownThenUp(tester, TextDirection.ltr, Alignment.bottomRight, TextDirection.rtl, Rect.fromLTWH(792.0, 500.0, 0.0, 0.0));
    await testPositioningDownThenUp(tester, TextDirection.rtl, Alignment.bottomRight, TextDirection.rtl, Rect.fromLTWH(792.0, 500.0, 0.0, 0.0));
    await testPositioningDownThenUp(tester, TextDirection.ltr, Alignment.bottomLeft, TextDirection.ltr, Rect.fromLTWH(8.0, 500.0, 0.0, 0.0));
    await testPositioningDownThenUp(tester, TextDirection.rtl, Alignment.bottomLeft, TextDirection.ltr, Rect.fromLTWH(8.0, 500.0, 0.0, 0.0));
    await testPositioningDownThenUp(tester, TextDirection.ltr, Alignment.bottomCenter, TextDirection.ltr, Rect.fromLTWH(350.0, 500.0, 0.0, 0.0));
    await testPositioningDownThenUp(tester, TextDirection.rtl, Alignment.bottomCenter, TextDirection.rtl, Rect.fromLTWH(450.0, 500.0, 0.0, 0.0));
Ian Hickson's avatar
Ian Hickson committed
390
  });
391 392 393 394

  testWidgets('PopupMenu removes MediaQuery padding', (WidgetTester tester) async {
    BuildContext popupContext;

395 396
    await tester.pumpWidget(MaterialApp(
      home: MediaQuery(
397
        data: const MediaQueryData(
398
          padding: EdgeInsets.all(50.0),
399
        ),
400 401
        child: Material(
          child: PopupMenuButton<int>(
402 403 404
            itemBuilder: (BuildContext context) {
              popupContext = context;
              return <PopupMenuItem<int>>[
405
                PopupMenuItem<int>(
406
                  value: 1,
407
                  child: Builder(
408 409 410 411 412 413 414 415 416 417 418
                    builder: (BuildContext context) {
                      popupContext = context;
                      return const Text('AAA');
                    },
                  ),
                ),
              ];
            },
            child: const SizedBox(
              height: 100.0,
              width: 100.0,
419
              child: Text('XXX'),
420 421 422
            ),
          ),
        ),
423
      ),
424 425 426 427 428 429 430 431
    ));

    await tester.tap(find.text('XXX'));

    await tester.pump();

    expect(MediaQuery.of(popupContext).padding, EdgeInsets.zero);
  });
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 459
  testWidgets('Popup Menu Offset Test', (WidgetTester tester) async {
    const Offset offset = Offset(100.0, 100.0);

    final PopupMenuButton<int> popupMenuButton =
      PopupMenuButton<int>(
        offset: offset,
        itemBuilder: (BuildContext context) {
          return <PopupMenuItem<int>>[
            PopupMenuItem<int>(
              value: 1,
              child: Builder(
                builder: (BuildContext context) {
                  return const Text('AAA');
                },
              ),
            ),
          ];
        },
      );

    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          body: Center(
            child: Material(
              child: popupMenuButton,
            ),
460
          ),
461 462 463 464 465 466 467 468 469 470 471
        ),
      ),
    );

    await tester.tap(find.byType(IconButton));
    await tester.pumpAndSettle();

    // The position is different than the offset because the default position isn't at the origin.
    expect(tester.getTopLeft(find.byWidgetPredicate((Widget w) => '${w.runtimeType}' == '_PopupMenu<int>')), const Offset(364.0, 324.0));
  });

472
  testWidgets('open PopupMenu has correct semantics', (WidgetTester tester) async {
473 474 475 476
    final SemanticsTester semantics = SemanticsTester(tester);
    await tester.pumpWidget(MaterialApp(
      home: Material(
        child: PopupMenuButton<int>(
477 478
          itemBuilder: (BuildContext context) {
            return <PopupMenuItem<int>>[
479 480 481 482 483
              const PopupMenuItem<int>(value: 1, child: Text('1')),
              const PopupMenuItem<int>(value: 2, child: Text('2')),
              const PopupMenuItem<int>(value: 3, child: Text('3')),
              const PopupMenuItem<int>(value: 4, child: Text('4')),
              const PopupMenuItem<int>(value: 5, child: Text('5')),
484 485 486 487 488
            ];
          },
          child: const SizedBox(
            height: 100.0,
            width: 100.0,
489
            child: Text('XXX'),
490 491 492 493 494 495 496
          ),
        ),
      ),
    ));
    await tester.tap(find.text('XXX'));
    await tester.pumpAndSettle();

497
    expect(semantics, hasSemantics(
498
      TestSemantics.root(
499
        children: <TestSemantics>[
500
          TestSemantics(
501 502
            textDirection: TextDirection.ltr,
            children: <TestSemantics>[
503
              TestSemantics(
504 505 506 507 508 509 510
                flags: <SemanticsFlag>[
                  SemanticsFlag.scopesRoute,
                  SemanticsFlag.namesRoute,
                ],
                label: 'Popup menu',
                textDirection: TextDirection.ltr,
                children: <TestSemantics>[
511
                  TestSemantics(
512 513 514
                    flags: <SemanticsFlag>[
                      SemanticsFlag.hasImplicitScrolling,
                    ],
515
                    children: <TestSemantics>[
516
                      TestSemantics(
517 518 519 520
                        actions: <SemanticsAction>[SemanticsAction.tap],
                        label: '1',
                        textDirection: TextDirection.ltr,
                      ),
521
                      TestSemantics(
522 523 524 525
                        actions: <SemanticsAction>[SemanticsAction.tap],
                        label: '2',
                        textDirection: TextDirection.ltr,
                      ),
526
                      TestSemantics(
527 528 529 530
                        actions: <SemanticsAction>[SemanticsAction.tap],
                        label: '3',
                        textDirection: TextDirection.ltr,
                      ),
531
                      TestSemantics(
532 533 534 535
                        actions: <SemanticsAction>[SemanticsAction.tap],
                        label: '4',
                        textDirection: TextDirection.ltr,
                      ),
536
                      TestSemantics(
537 538 539 540 541 542 543 544 545 546 547 548 549
                        actions: <SemanticsAction>[SemanticsAction.tap],
                        label: '5',
                        textDirection: TextDirection.ltr,
                      ),
                    ],
                  ),
                ],
              ),
            ],
          ),
        ],
      ),
      ignoreId: true, ignoreTransform: true, ignoreRect: true,
550 551 552 553
    ));

    semantics.dispose();
  });
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 607 608

  testWidgets('PopupMenuButton PopupMenuDivider', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/27072

    String selectedValue;
    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          body: Container(
            child: Center(
              child: PopupMenuButton<String>(
                onSelected: (String result) {
                  selectedValue = result;
                },
                child: const Text('Menu Button'),
                initialValue: '1',
                itemBuilder: (BuildContext context) => <PopupMenuEntry<String>>[
                  const PopupMenuItem<String>(
                    child: Text('1'),
                    value: '1',
                  ),
                  const PopupMenuDivider(),
                  const PopupMenuItem<String>(
                    child: Text('2'),
                    value: '2',
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );

    await tester.tap(find.text('Menu Button'));
    await tester.pumpAndSettle();
    expect(find.text('1'), findsOneWidget);
    expect(find.byType(PopupMenuDivider), findsOneWidget);
    expect(find.text('2'), findsOneWidget);

    await tester.tap(find.text('1'));
    await tester.pumpAndSettle();
    expect(selectedValue, '1');

    await tester.tap(find.text('Menu Button'));
    await tester.pumpAndSettle();
    expect(find.text('1'), findsOneWidget);
    expect(find.byType(PopupMenuDivider), findsOneWidget);
    expect(find.text('2'), findsOneWidget);

    await tester.tap(find.text('2'));
    await tester.pumpAndSettle();
    expect(selectedValue, '2');
  });

609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
  testWidgets('showMenu position required', (WidgetTester tester) async {
    // Test for https://github.com/flutter/flutter/issues/22256
    await tester.pumpWidget(
      MaterialApp(
        home: Material(
          child: Center(
            child: Builder(
              builder: (BuildContext context) {
                return RaisedButton(
                  onPressed: () {
                    // Ensure showMenu throws an assertion without a position
                    expect(() {
                      // ignore: missing_required_param
                      showMenu<int>(
                        context: context,
                        items: <PopupMenuItem<int>>[
                          const PopupMenuItem<int>(
                              value: 1, child: Text('1')
                          ),
                        ],
                      );
                    }, throwsAssertionError);
                  },
                  child: const Text('Menu Button'),
                );
              },
            ),
          ),
        ),
      )
    );

    await tester.tap(find.text('Menu Button'));
  });

Ian Hickson's avatar
Ian Hickson committed
644 645 646 647 648 649 650
}

class TestApp extends StatefulWidget {
  const TestApp({ this.textDirection, this.child });
  final TextDirection textDirection;
  final Widget child;
  @override
651
  _TestAppState createState() => _TestAppState();
Ian Hickson's avatar
Ian Hickson committed
652 653 654 655 656
}

class _TestAppState extends State<TestApp> {
  @override
  Widget build(BuildContext context) {
657
    return Localizations(
658
      locale: const Locale('en', 'US'),
659
      delegates: const <LocalizationsDelegate<dynamic>>[
660 661 662
        DefaultWidgetsLocalizations.delegate,
        DefaultMaterialLocalizations.delegate,
      ],
663 664 665
      child: MediaQuery(
        data: MediaQueryData.fromWindow(window),
        child: Directionality(
666
          textDirection: widget.textDirection,
667
          child: Navigator(
668 669
            onGenerateRoute: (RouteSettings settings) {
              assert(settings.name == '/');
670
              return MaterialPageRoute<void>(
671
                settings: settings,
672
                builder: (BuildContext context) => Material(
673 674 675 676 677
                  child: widget.child,
                ),
              );
            },
          ),
Ian Hickson's avatar
Ian Hickson committed
678 679 680 681
        ),
      ),
    );
  }
682
}