scaffold_test.dart 61.4 KB
Newer Older
1 2 3 4
// Copyright 2015 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 'package:flutter/foundation.dart';
6 7
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
8
import 'package:flutter_test/flutter_test.dart';
9
import 'package:flutter/gestures.dart' show DragStartBehavior;
10

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

13
void main() {
14
  testWidgets('Scaffold control test', (WidgetTester tester) async {
15
    final Key bodyKey = UniqueKey();
16 17 18 19 20 21 22 23 24 25 26 27 28 29
    Widget boilerplate(Widget child) {
      return Localizations(
        locale: const Locale('en', 'us'),
        delegates: const <LocalizationsDelegate<dynamic>>[
          DefaultWidgetsLocalizations.delegate,
          DefaultMaterialLocalizations.delegate,
        ],
        child: Directionality(
          textDirection: TextDirection.ltr,
          child: child,
        ),
      );
    }
    await tester.pumpWidget(boilerplate(Scaffold(
30 31
        appBar: AppBar(title: const Text('Title')),
        body: Container(key: bodyKey),
32
      ),
33
    ));
34
    expect(tester.takeException(), isFlutterError);
35

36 37 38 39
    await tester.pumpWidget(MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('Title')),
        body: Container(key: bodyKey),
40 41
      ),
    ));
42
    RenderBox bodyBox = tester.renderObject(find.byKey(bodyKey));
43
    expect(bodyBox.size, equals(const Size(800.0, 544.0)));
44

45
    await tester.pumpWidget(boilerplate(MediaQuery(
46
        data: const MediaQueryData(viewInsets: EdgeInsets.only(bottom: 100.0)),
47 48 49
        child: Scaffold(
          appBar: AppBar(title: const Text('Title')),
          body: Container(key: bodyKey),
50 51
        ),
      ),
52
    ));
53

54
    bodyBox = tester.renderObject(find.byKey(bodyKey));
55
    expect(bodyBox.size, equals(const Size(800.0, 444.0)));
56

57 58 59 60 61
    await tester.pumpWidget(boilerplate(MediaQuery(
      data: const MediaQueryData(viewInsets: EdgeInsets.only(bottom: 100.0)),
      child: Scaffold(
        appBar: AppBar(title: const Text('Title')),
        body: Container(key: bodyKey),
62 63 64 65 66 67 68
        resizeToAvoidBottomInset: false,
      ),
    )));

    bodyBox = tester.renderObject(find.byKey(bodyKey));
    expect(bodyBox.size, equals(const Size(800.0, 544.0)));

Chris Bracken's avatar
Chris Bracken committed
69
    // Backwards compatibility: deprecated resizeToAvoidBottomPadding flag
70 71 72 73 74
    await tester.pumpWidget(boilerplate(MediaQuery(
      data: const MediaQueryData(viewInsets: EdgeInsets.only(bottom: 100.0)),
      child: Scaffold(
        appBar: AppBar(title: const Text('Title')),
        body: Container(key: bodyKey),
75
        resizeToAvoidBottomPadding: false,
76
      ),
77
    )));
78

79
    bodyBox = tester.renderObject(find.byKey(bodyKey));
80
    expect(bodyBox.size, equals(const Size(800.0, 544.0)));
81
  });
82

83
  testWidgets('Scaffold large bottom padding test', (WidgetTester tester) async {
84
    final Key bodyKey = UniqueKey();
85 86 87 88 89 90 91 92 93 94 95

    Widget boilerplate(Widget child) {
      return Localizations(
        locale: const Locale('en', 'us'),
        delegates: const <LocalizationsDelegate<dynamic>>[
          DefaultWidgetsLocalizations.delegate,
          DefaultMaterialLocalizations.delegate,
        ],
        child: Directionality(
          textDirection: TextDirection.ltr,
          child: child,
96
        ),
97 98 99 100 101 102
      );
    }

    await tester.pumpWidget(boilerplate(MediaQuery(
      data: const MediaQueryData(
        viewInsets: EdgeInsets.only(bottom: 700.0),
103
      ),
104 105 106
      child: Scaffold(
        body: Container(key: bodyKey),
      ))
107 108
    ));

109
    final RenderBox bodyBox = tester.renderObject(find.byKey(bodyKey));
110 111
    expect(bodyBox.size, equals(const Size(800.0, 0.0)));

112
    await tester.pumpWidget(boilerplate(MediaQuery(
113
        data: const MediaQueryData(
114
          viewInsets: EdgeInsets.only(bottom: 500.0),
115
        ),
116 117
        child: Scaffold(
          body: Container(key: bodyKey),
118
        ),
119 120 121 122 123
      ),
    ));

    expect(bodyBox.size, equals(const Size(800.0, 100.0)));

124
    await tester.pumpWidget(boilerplate(MediaQuery(
125
        data: const MediaQueryData(
126
          viewInsets: EdgeInsets.only(bottom: 580.0),
127
        ),
128 129
        child: Scaffold(
          appBar: AppBar(
130 131
            title: const Text('Title'),
          ),
132
          body: Container(key: bodyKey),
133 134 135 136 137 138 139
        ),
      ),
    ));

    expect(bodyBox.size, equals(const Size(800.0, 0.0)));
  });

140
  testWidgets('Floating action entrance/exit animation', (WidgetTester tester) async {
141
    await tester.pumpWidget(const MaterialApp(home: Scaffold(
142 143
      floatingActionButton: FloatingActionButton(
        key: Key('one'),
144
        onPressed: null,
145
        child: Text('1'),
146
      ),
147
    )));
148 149 150

    expect(tester.binding.transientCallbackCount, 0);

151
    await tester.pumpWidget(const MaterialApp(home: Scaffold(
152 153
      floatingActionButton: FloatingActionButton(
        key: Key('two'),
154
        onPressed: null,
155
        child: Text('2'),
156
      ),
157
    )));
158 159

    expect(tester.binding.transientCallbackCount, greaterThan(0));
160
    await tester.pumpWidget(Container());
161
    expect(tester.binding.transientCallbackCount, 0);
162

163
    await tester.pumpWidget(const MaterialApp(home: Scaffold()));
164

165 166
    expect(tester.binding.transientCallbackCount, 0);

167
    await tester.pumpWidget(const MaterialApp(home: Scaffold(
168 169
      floatingActionButton: FloatingActionButton(
        key: Key('one'),
170
        onPressed: null,
171
        child: Text('1'),
172
      ),
173
    )));
174 175 176

    expect(tester.binding.transientCallbackCount, greaterThan(0));
  });
177

178
  testWidgets('Floating action button directionality', (WidgetTester tester) async {
179
    Widget build(TextDirection textDirection) {
180
      return Directionality(
181
        textDirection: textDirection,
182
        child: const MediaQuery(
183 184
          data: MediaQueryData(
            viewInsets: EdgeInsets.only(bottom: 200.0),
185
          ),
186 187
          child: Scaffold(
            floatingActionButton: FloatingActionButton(
188
              onPressed: null,
189
              child: Text('1'),
190 191 192 193 194 195 196 197 198 199 200
            ),
          ),
        ),
      );
    }

    await tester.pumpWidget(build(TextDirection.ltr));

    expect(tester.getCenter(find.byType(FloatingActionButton)), const Offset(756.0, 356.0));

    await tester.pumpWidget(build(TextDirection.rtl));
201
    expect(tester.binding.transientCallbackCount, 0);
202 203 204 205

    expect(tester.getCenter(find.byType(FloatingActionButton)), const Offset(44.0, 356.0));
  });

206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
  testWidgets('Floating Action Button bottom padding not consumed by viewInsets', (WidgetTester tester) async {
    final Widget child = Directionality(
      textDirection: TextDirection.ltr,
      child: Scaffold(
        resizeToAvoidBottomInset: false,
        body: Container(),
        floatingActionButton: const Placeholder(),
      ),
    );

    await tester.pumpWidget(
      MediaQuery(
        data: const MediaQueryData(
          padding: EdgeInsets.only(bottom: 20.0),
        ),
        child: child,
      )
    );
    final Offset initialPoint = tester.getCenter(find.byType(Placeholder));
    // Consume bottom padding - as if by the keyboard opening
    await tester.pumpWidget(
      MediaQuery(
        data: const MediaQueryData(
          padding: EdgeInsets.zero,
          viewPadding: EdgeInsets.only(bottom: 20),
          viewInsets: EdgeInsets.only(bottom: 300),
        ),
        child: child,
      ),
    );
    final Offset finalPoint = tester.getCenter(find.byType(Placeholder));
    expect(initialPoint, finalPoint);
  });

240
  testWidgets('Drawer scrolling', (WidgetTester tester) async {
241
    final Key drawerKey = UniqueKey();
242 243
    const double appBarHeight = 256.0;

244
    final ScrollController scrollOffset = ScrollController();
245

246
    await tester.pumpWidget(
247 248 249
      MaterialApp(
        home: Scaffold(
          drawer: Drawer(
250
            key: drawerKey,
251
            child: ListView(
252
              dragStartBehavior: DragStartBehavior.down,
253
              controller: scrollOffset,
254
              children: List<Widget>.generate(10,
255 256 257
                (int index) => SizedBox(height: 100.0, child: Text('D$index')),
              ),
            ),
258
          ),
259
          body: CustomScrollView(
260
            slivers: <Widget>[
261
              const SliverAppBar(
262 263
                pinned: true,
                expandedHeight: appBarHeight,
264 265
                title: Text('Title'),
                flexibleSpace: FlexibleSpaceBar(title: Text('Title')),
266
              ),
267
              SliverPadding(
268
                padding: const EdgeInsets.only(top: appBarHeight),
269 270 271
                sliver: SliverList(
                  delegate: SliverChildListDelegate(List<Widget>.generate(
                    10, (int index) => SizedBox(height: 100.0, child: Text('B$index')),
272 273 274 275
                  )),
                ),
              ),
            ],
276
          ),
277
        ),
278 279 280
      )
    );

281
    final ScaffoldState state = tester.firstState(find.byType(Scaffold));
282 283 284 285 286
    state.openDrawer();

    await tester.pump();
    await tester.pump(const Duration(seconds: 1));

287
    expect(scrollOffset.offset, 0.0);
288 289

    const double scrollDelta = 80.0;
290
    await tester.drag(find.byKey(drawerKey), const Offset(0.0, -scrollDelta));
291 292
    await tester.pump();

293
    expect(scrollOffset.offset, scrollDelta);
294

295
    final RenderBox renderBox = tester.renderObject(find.byType(AppBar));
296 297
    expect(renderBox.size.height, equals(appBarHeight));
  });
298

299
  Widget _buildStatusBarTestApp(TargetPlatform platform) {
300 301 302
    return MaterialApp(
      theme: ThemeData(platform: platform),
      home: MediaQuery(
303
        data: const MediaQueryData(padding: EdgeInsets.only(top: 25.0)), // status bar
304 305
        child: Scaffold(
          body: CustomScrollView(
306 307
            primary: true,
            slivers: <Widget>[
308
              const SliverAppBar(
309
                title: Text('Title'),
310
              ),
311 312 313
              SliverList(
                delegate: SliverChildListDelegate(List<Widget>.generate(
                  20, (int index) => SizedBox(height: 100.0, child: Text('$index')),
314 315 316 317 318 319
                )),
              ),
            ],
          ),
        ),
      ),
320
    );
321
  }
322

323 324
  testWidgets('Tapping the status bar scrolls to top on iOS', (WidgetTester tester) async {
    await tester.pumpWidget(_buildStatusBarTestApp(TargetPlatform.iOS));
Adam Barth's avatar
Adam Barth committed
325
    final ScrollableState scrollable = tester.state(find.byType(Scrollable));
326 327
    scrollable.position.jumpTo(500.0);
    expect(scrollable.position.pixels, equals(500.0));
328
    await tester.tapAt(const Offset(100.0, 10.0));
329
    await tester.pumpAndSettle();
330
    expect(scrollable.position.pixels, equals(0.0));
331 332 333
  });

  testWidgets('Tapping the status bar does not scroll to top on Android', (WidgetTester tester) async {
334
    await tester.pumpWidget(_buildStatusBarTestApp(TargetPlatform.android));
Adam Barth's avatar
Adam Barth committed
335
    final ScrollableState scrollable = tester.state(find.byType(Scrollable));
336 337
    scrollable.position.jumpTo(500.0);
    expect(scrollable.position.pixels, equals(500.0));
338
    await tester.tapAt(const Offset(100.0, 10.0));
339
    await tester.pump();
340
    await tester.pump(const Duration(seconds: 1));
341
    expect(scrollable.position.pixels, equals(500.0));
342
  });
343 344

  testWidgets('Bottom sheet cannot overlap app bar', (WidgetTester tester) async {
345
    final Key sheetKey = UniqueKey();
346 347

    await tester.pumpWidget(
348 349 350 351
      MaterialApp(
        theme: ThemeData(platform: TargetPlatform.android),
        home: Scaffold(
          appBar: AppBar(
352
            title: const Text('Title'),
353
          ),
354
          body: Builder(
355
            builder: (BuildContext context) {
356
              return GestureDetector(
357
                onTap: () {
358
                  Scaffold.of(context).showBottomSheet<void>((BuildContext context) {
359
                    return Container(
360
                      key: sheetKey,
361
                      color: Colors.blue[500],
362 363 364
                    );
                  });
                },
365
                child: const Text('X'),
366
              );
367 368 369 370
            },
          ),
        ),
      ),
371 372 373 374 375
    );
    await tester.tap(find.text('X'));
    await tester.pump(); // start animation
    await tester.pump(const Duration(seconds: 1));

376 377
    final RenderBox appBarBox = tester.renderObject(find.byType(AppBar));
    final RenderBox sheetBox = tester.renderObject(find.byKey(sheetKey));
378

379 380
    final Offset appBarBottomRight = appBarBox.localToGlobal(appBarBox.size.bottomRight(Offset.zero));
    final Offset sheetTopRight = sheetBox.localToGlobal(sheetBox.size.topRight(Offset.zero));
381 382 383

    expect(appBarBottomRight, equals(sheetTopRight));
  });
384

385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
  testWidgets('BottomSheet bottom padding is not consumed by viewInsets', (WidgetTester tester) async {
    final Widget child = Directionality(
      textDirection: TextDirection.ltr,
      child: Scaffold(
        resizeToAvoidBottomInset: false,
        body: Container(),
        bottomSheet: const Placeholder(),
      ),
    );

    await tester.pumpWidget(
      MediaQuery(
        data: const MediaQueryData(
          padding: EdgeInsets.only(bottom: 20.0),
        ),
        child: child,
      )
    );
    final Offset initialPoint = tester.getCenter(find.byType(Placeholder));
    // Consume bottom padding - as if by the keyboard opening
    await tester.pumpWidget(
      MediaQuery(
        data: const MediaQueryData(
          padding: EdgeInsets.zero,
          viewPadding: EdgeInsets.only(bottom: 20),
          viewInsets: EdgeInsets.only(bottom: 300),
        ),
        child: child,
      ),
    );
    final Offset finalPoint = tester.getCenter(find.byType(Placeholder));
    expect(initialPoint, finalPoint);
  });

419 420 421
  testWidgets('Persistent bottom buttons are persistent', (WidgetTester tester) async {
    bool didPressButton = false;
    await tester.pumpWidget(
422 423 424 425
      MaterialApp(
        home: Scaffold(
          body: SingleChildScrollView(
            child: Container(
426
              color: Colors.amber[500],
427
              height: 5000.0,
428
              child: const Text('body'),
429 430 431
            ),
          ),
          persistentFooterButtons: <Widget>[
432
            FlatButton(
433 434 435
              onPressed: () {
                didPressButton = true;
              },
436
              child: const Text('X'),
437
            ),
438 439 440 441 442
          ],
        ),
      ),
    );

443
    await tester.drag(find.text('body'), const Offset(0.0, -1000.0));
444 445 446 447 448
    expect(didPressButton, isFalse);
    await tester.tap(find.text('X'));
    expect(didPressButton, isTrue);
  });

449 450
  testWidgets('Persistent bottom buttons apply media padding', (WidgetTester tester) async {
    await tester.pumpWidget(
451
      Directionality(
452
        textDirection: TextDirection.ltr,
453
        child: MediaQuery(
454
          data: const MediaQueryData(
455
            padding: EdgeInsets.fromLTRB(10.0, 20.0, 30.0, 40.0),
456
          ),
457 458 459
          child: Scaffold(
            body: SingleChildScrollView(
              child: Container(
460 461 462 463 464
                color: Colors.amber[500],
                height: 5000.0,
                child: const Text('body'),
              ),
            ),
465
            persistentFooterButtons: const <Widget>[Placeholder()],
466 467 468 469 470 471 472 473
          ),
        ),
      ),
    );
    expect(tester.getBottomLeft(find.byType(ButtonBar)), const Offset(10.0, 560.0));
    expect(tester.getBottomRight(find.byType(ButtonBar)), const Offset(770.0, 560.0));
  });

474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
  testWidgets('Persistent bottom buttons bottom padding is not consumed by viewInsets', (WidgetTester tester) async {
    final Widget child = Directionality(
      textDirection: TextDirection.ltr,
      child: Scaffold(
        resizeToAvoidBottomInset: false,
        body: Container(),
        persistentFooterButtons: const <Widget>[Placeholder()],
      ),
    );

    await tester.pumpWidget(
      MediaQuery(
        data: const MediaQueryData(
          padding: EdgeInsets.only(bottom: 20.0),
        ),
        child: child,
      )
    );
    final Offset initialPoint = tester.getCenter(find.byType(Placeholder));
    // Consume bottom padding - as if by the keyboard opening
    await tester.pumpWidget(
      MediaQuery(
        data: const MediaQueryData(
          padding: EdgeInsets.zero,
          viewPadding: EdgeInsets.only(bottom: 20),
          viewInsets: EdgeInsets.only(bottom: 300),
        ),
        child: child,
      ),
    );
    final Offset finalPoint = tester.getCenter(find.byType(Placeholder));
    expect(initialPoint, finalPoint);
  });

508
  group('back arrow', () {
509
    Future<void> expectBackIcon(WidgetTester tester, TargetPlatform platform, IconData expectedIcon) async {
510
      final GlobalKey rootKey = GlobalKey();
511
      final Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{
512 513 514
        '/': (_) => Container(key: rootKey, child: const Text('Home')),
        '/scaffold': (_) => Scaffold(
            appBar: AppBar(),
515
            body: const Text('Scaffold'),
516
        ),
517 518
      };
      await tester.pumpWidget(
519
        MaterialApp(theme: ThemeData(platform: platform), routes: routes)
520 521 522 523 524 525
      );

      Navigator.pushNamed(rootKey.currentContext, '/scaffold');
      await tester.pump();
      await tester.pump(const Duration(seconds: 1));

526
      final Icon icon = tester.widget(find.byType(Icon));
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
      expect(icon.icon, expectedIcon);
    }

    testWidgets('Back arrow uses correct default on Android', (WidgetTester tester) async {
      await expectBackIcon(tester, TargetPlatform.android, Icons.arrow_back);
    });

    testWidgets('Back arrow uses correct default on Fuchsia', (WidgetTester tester) async {
      await expectBackIcon(tester, TargetPlatform.fuchsia, Icons.arrow_back);
    });

    testWidgets('Back arrow uses correct default on iOS', (WidgetTester tester) async {
      await expectBackIcon(tester, TargetPlatform.iOS, Icons.arrow_back_ios);
    });
  });
542

543
  group('close button', () {
544
    Future<void> expectCloseIcon(WidgetTester tester, TargetPlatform platform, IconData expectedIcon, PageRoute<void> routeBuilder()) async {
545
      await tester.pumpWidget(
546 547 548
        MaterialApp(
          theme: ThemeData(platform: platform),
          home: Scaffold(appBar: AppBar(), body: const Text('Page 1')),
549 550 551
        )
      );

552
      tester.state<NavigatorState>(find.byType(Navigator)).push(routeBuilder());
553 554 555 556 557 558

      await tester.pump();
      await tester.pump(const Duration(seconds: 1));

      final Icon icon = tester.widget(find.byType(Icon));
      expect(icon.icon, expectedIcon);
559 560 561 562
      expect(find.byType(CloseButton), findsOneWidget);
    }

    PageRoute<void> materialRouteBuilder() {
563
      return MaterialPageRoute<void>(
564
        builder: (BuildContext context) {
565
          return Scaffold(appBar: AppBar(), body: const Text('Page 2'));
566 567 568 569 570 571
        },
        fullscreenDialog: true,
      );
    }

    PageRoute<void> customPageRouteBuilder() {
572
      return _CustomPageRoute<void>(
573
        builder: (BuildContext context) {
574
          return Scaffold(appBar: AppBar(), body: const Text('Page 2'));
575 576 577
        },
        fullscreenDialog: true,
      );
578 579 580
    }

    testWidgets('Close button shows correctly on Android', (WidgetTester tester) async {
581
      await expectCloseIcon(tester, TargetPlatform.android, Icons.close, materialRouteBuilder);
582 583 584
    });

    testWidgets('Close button shows correctly on Fuchsia', (WidgetTester tester) async {
585
      await expectCloseIcon(tester, TargetPlatform.fuchsia, Icons.close, materialRouteBuilder);
586 587 588
    });

    testWidgets('Close button shows correctly on iOS', (WidgetTester tester) async {
589 590 591 592 593 594 595 596 597 598 599 600 601
      await expectCloseIcon(tester, TargetPlatform.iOS, Icons.close, materialRouteBuilder);
    });

    testWidgets('Close button shows correctly with custom page route on Android', (WidgetTester tester) async {
      await expectCloseIcon(tester, TargetPlatform.android, Icons.close, customPageRouteBuilder);
    });

    testWidgets('Close button shows correctly with custom page route on Fuchsia', (WidgetTester tester) async {
      await expectCloseIcon(tester, TargetPlatform.fuchsia, Icons.close, customPageRouteBuilder);
    });

    testWidgets('Close button shows correctly with custom page route on iOS', (WidgetTester tester) async {
      await expectCloseIcon(tester, TargetPlatform.iOS, Icons.close, customPageRouteBuilder);
602 603 604
    });
  });

605 606
  group('body size', () {
    testWidgets('body size with container', (WidgetTester tester) async {
607 608
      final Key testKey = UniqueKey();
      await tester.pumpWidget(Directionality(
609
        textDirection: TextDirection.ltr,
610
        child: MediaQuery(
611
          data: const MediaQueryData(),
612 613
          child: Scaffold(
            body: Container(
614 615 616 617
              key: testKey,
            ),
          ),
        ),
618
      ));
619
      expect(tester.element(find.byKey(testKey)).size, const Size(800.0, 600.0));
620
      expect(tester.renderObject<RenderBox>(find.byKey(testKey)).localToGlobal(Offset.zero), const Offset(0.0, 0.0));
621 622 623
    });

    testWidgets('body size with sized container', (WidgetTester tester) async {
624 625
      final Key testKey = UniqueKey();
      await tester.pumpWidget(Directionality(
626
        textDirection: TextDirection.ltr,
627
        child: MediaQuery(
628
          data: const MediaQueryData(),
629 630
          child: Scaffold(
            body: Container(
631 632 633 634 635
              key: testKey,
              height: 100.0,
            ),
          ),
        ),
636
      ));
637
      expect(tester.element(find.byKey(testKey)).size, const Size(800.0, 100.0));
638
      expect(tester.renderObject<RenderBox>(find.byKey(testKey)).localToGlobal(Offset.zero), const Offset(0.0, 0.0));
639 640 641
    });

    testWidgets('body size with centered container', (WidgetTester tester) async {
642 643
      final Key testKey = UniqueKey();
      await tester.pumpWidget(Directionality(
644
        textDirection: TextDirection.ltr,
645
        child: MediaQuery(
646
          data: const MediaQueryData(),
647 648 649
          child: Scaffold(
            body: Center(
              child: Container(
650 651 652 653 654
                key: testKey,
              ),
            ),
          ),
        ),
655
      ));
656
      expect(tester.element(find.byKey(testKey)).size, const Size(800.0, 600.0));
657
      expect(tester.renderObject<RenderBox>(find.byKey(testKey)).localToGlobal(Offset.zero), const Offset(0.0, 0.0));
658 659 660
    });

    testWidgets('body size with button', (WidgetTester tester) async {
661 662
      final Key testKey = UniqueKey();
      await tester.pumpWidget(Directionality(
663
        textDirection: TextDirection.ltr,
664
        child: MediaQuery(
665
          data: const MediaQueryData(),
666 667
          child: Scaffold(
            body: FlatButton(
668 669 670 671 672 673
              key: testKey,
              onPressed: () { },
              child: const Text(''),
            ),
          ),
        ),
674
      ));
675
      expect(tester.element(find.byKey(testKey)).size, const Size(88.0, 48.0));
676
      expect(tester.renderObject<RenderBox>(find.byKey(testKey)).localToGlobal(Offset.zero), const Offset(0.0, 0.0));
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

    testWidgets('body size with extendBody', (WidgetTester tester) async {
      final Key bodyKey = UniqueKey();
      double mediaQueryBottom;

      Widget buildFrame({ bool extendBody, bool resizeToAvoidBottomInset, double viewInsetBottom = 0.0 }) {
        return Directionality(
          textDirection: TextDirection.ltr,
          child: MediaQuery(
            data: MediaQueryData(
              viewInsets: EdgeInsets.only(bottom: viewInsetBottom),
            ),
            child: Scaffold(
              resizeToAvoidBottomInset: resizeToAvoidBottomInset,
              extendBody: extendBody,
              body: Builder(
                builder: (BuildContext context) {
                  mediaQueryBottom = MediaQuery.of(context).padding.bottom;
                  return Container(key: bodyKey);
                },
              ),
              bottomNavigationBar: const BottomAppBar(
                child: SizedBox(height: 48.0,),
              ),
            ),
          ),
        );
      }

      await tester.pumpWidget(buildFrame(extendBody: true));
      expect(tester.getSize(find.byKey(bodyKey)), const Size(800.0, 600.0));
      expect(mediaQueryBottom, 48.0);

      await tester.pumpWidget(buildFrame(extendBody: false));
      expect(tester.getSize(find.byKey(bodyKey)), const Size(800.0, 552.0)); // 552 = 600 - 48 (BAB height)
      expect(mediaQueryBottom, 0.0);

      // If resizeToAvoidBottomInsets is false, same results as if it was unspecified (null).
      await tester.pumpWidget(buildFrame(extendBody: true, resizeToAvoidBottomInset: false, viewInsetBottom: 100.0));
      expect(tester.getSize(find.byKey(bodyKey)), const Size(800.0, 600.0));
      expect(mediaQueryBottom, 48.0);

      await tester.pumpWidget(buildFrame(extendBody: false, resizeToAvoidBottomInset: false, viewInsetBottom: 100.0));
      expect(tester.getSize(find.byKey(bodyKey)), const Size(800.0, 552.0));
      expect(mediaQueryBottom, 0.0);

      // If resizeToAvoidBottomInsets is true and viewInsets.bottom is > the bottom
      // navigation bar's height then the body always resizes and the MediaQuery
      // isn't adjusted. This case corresponds to the keyboard appearing.
      await tester.pumpWidget(buildFrame(extendBody: true, resizeToAvoidBottomInset: true, viewInsetBottom: 100.0));
      expect(tester.getSize(find.byKey(bodyKey)), const Size(800.0, 500.0));
      expect(mediaQueryBottom, 0.0);

      await tester.pumpWidget(buildFrame(extendBody: false, resizeToAvoidBottomInset: true, viewInsetBottom: 100.0));
      expect(tester.getSize(find.byKey(bodyKey)), const Size(800.0, 500.0));
      expect(mediaQueryBottom, 0.0);
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 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

    testWidgets('body size with extendBodyBehindAppBar', (WidgetTester tester) async {
      final Key appBarKey = UniqueKey();
      final Key bodyKey = UniqueKey();

      const double appBarHeight = 100;
      const double windowPaddingTop = 24;
      bool fixedHeightAppBar;
      double mediaQueryTop;

      Widget buildFrame({ bool extendBodyBehindAppBar, bool hasAppBar }) {
        return Directionality(
          textDirection: TextDirection.ltr,
          child: MediaQuery(
            data: const MediaQueryData(
              padding: EdgeInsets.only(top: windowPaddingTop),
            ),
            child: Builder(
              builder: (BuildContext context) {
                return Scaffold(
                  extendBodyBehindAppBar: extendBodyBehindAppBar,
                  appBar: !hasAppBar ? null : PreferredSize(
                    key: appBarKey,
                    preferredSize: const Size.fromHeight(appBarHeight),
                    child: Container(
                      constraints: BoxConstraints(
                        minHeight: appBarHeight,
                        maxHeight: fixedHeightAppBar ? appBarHeight : double.infinity,
                      ),
                    ),
                  ),
                  body: Builder(
                    builder: (BuildContext context) {
                      mediaQueryTop = MediaQuery.of(context).padding.top;
                      return Container(key: bodyKey);
                    }
                  ),
                );
              },
            ),
          ),
        );
      }

      fixedHeightAppBar = false;

      // When an appbar is provided, the Scaffold's body is built within a
      // MediaQuery with padding.top = 0, and the appBar's maxHeight is
      // constrained to its preferredSize.height + the original MediaQuery
      // padding.top. When extendBodyBehindAppBar is true, an additional
      // inner MediaQuery is added around the Scaffold's body with padding.top
      // equal to the overall height of the appBar. See _BodyBuilder in
      // material/scaffold.dart.

      await tester.pumpWidget(buildFrame(extendBodyBehindAppBar: true, hasAppBar: true));
      expect(tester.getSize(find.byKey(bodyKey)), const Size(800.0, 600.0));
      expect(tester.getSize(find.byKey(appBarKey)), const Size(800.0, appBarHeight + windowPaddingTop));
      expect(mediaQueryTop, appBarHeight + windowPaddingTop);

      await tester.pumpWidget(buildFrame(extendBodyBehindAppBar: true, hasAppBar: false));
      expect(tester.getSize(find.byKey(bodyKey)), const Size(800.0, 600.0));
      expect(find.byKey(appBarKey), findsNothing);
      expect(mediaQueryTop, windowPaddingTop);

      await tester.pumpWidget(buildFrame(extendBodyBehindAppBar: false, hasAppBar: true));
      expect(tester.getSize(find.byKey(bodyKey)), const Size(800.0, 600.0 - appBarHeight - windowPaddingTop));
      expect(tester.getSize(find.byKey(appBarKey)), const Size(800.0, appBarHeight + windowPaddingTop));
      expect(mediaQueryTop, 0.0);

      await tester.pumpWidget(buildFrame(extendBodyBehindAppBar: false, hasAppBar: false));
      expect(tester.getSize(find.byKey(bodyKey)), const Size(800.0, 600.0));
      expect(find.byKey(appBarKey), findsNothing);
      expect(mediaQueryTop, windowPaddingTop);

      fixedHeightAppBar = true;

      await tester.pumpWidget(buildFrame(extendBodyBehindAppBar: true, hasAppBar: true));
      expect(tester.getSize(find.byKey(bodyKey)), const Size(800.0, 600.0));
      expect(tester.getSize(find.byKey(appBarKey)), const Size(800.0, appBarHeight));
      expect(mediaQueryTop, appBarHeight);

      await tester.pumpWidget(buildFrame(extendBodyBehindAppBar: true, hasAppBar: false));
      expect(tester.getSize(find.byKey(bodyKey)), const Size(800.0, 600.0));
      expect(find.byKey(appBarKey), findsNothing);
      expect(mediaQueryTop, windowPaddingTop);

      await tester.pumpWidget(buildFrame(extendBodyBehindAppBar: false, hasAppBar: true));
      expect(tester.getSize(find.byKey(bodyKey)), const Size(800.0, 600.0 - appBarHeight));
      expect(tester.getSize(find.byKey(appBarKey)), const Size(800.0, appBarHeight));
      expect(mediaQueryTop, 0.0);

      await tester.pumpWidget(buildFrame(extendBodyBehindAppBar: false, hasAppBar: false));
      expect(tester.getSize(find.byKey(bodyKey)), const Size(800.0, 600.0));
      expect(find.byKey(appBarKey), findsNothing);
      expect(mediaQueryTop, windowPaddingTop);
    });
831
  });
832 833 834 835 836 837 838 839

  testWidgets('Open drawer hides underlying semantics tree', (WidgetTester tester) async {
    const String bodyLabel = 'I am the body';
    const String persistentFooterButtonLabel = 'a button on the bottom';
    const String bottomNavigationBarLabel = 'a bar in an app';
    const String floatingActionButtonLabel = 'I float in space';
    const String drawerLabel = 'I am the reason for this test';

840
    final SemanticsTester semantics = SemanticsTester(tester);
841
    await tester.pumpWidget(const MaterialApp(home: Scaffold(
842 843 844 845 846
      body: Text(bodyLabel),
      persistentFooterButtons: <Widget>[Text(persistentFooterButtonLabel)],
      bottomNavigationBar: Text(bottomNavigationBarLabel),
      floatingActionButton: Text(floatingActionButtonLabel),
      drawer: Drawer(child: Text(drawerLabel)),
847 848
    )));

849 850 851 852 853
    expect(semantics, includesNodeWith(label: bodyLabel));
    expect(semantics, includesNodeWith(label: persistentFooterButtonLabel));
    expect(semantics, includesNodeWith(label: bottomNavigationBarLabel));
    expect(semantics, includesNodeWith(label: floatingActionButtonLabel));
    expect(semantics, isNot(includesNodeWith(label: drawerLabel)));
854 855 856 857 858 859

    final ScaffoldState state = tester.firstState(find.byType(Scaffold));
    state.openDrawer();
    await tester.pump();
    await tester.pump(const Duration(seconds: 1));

860 861 862 863 864
    expect(semantics, isNot(includesNodeWith(label: bodyLabel)));
    expect(semantics, isNot(includesNodeWith(label: persistentFooterButtonLabel)));
    expect(semantics, isNot(includesNodeWith(label: bottomNavigationBarLabel)));
    expect(semantics, isNot(includesNodeWith(label: floatingActionButtonLabel)));
    expect(semantics, includesNodeWith(label: drawerLabel));
865 866 867

    semantics.dispose();
  });
Ian Hickson's avatar
Ian Hickson committed
868 869

  testWidgets('Scaffold and extreme window padding', (WidgetTester tester) async {
870 871 872 873 874 875 876 877 878 879 880 881
    final Key appBar = UniqueKey();
    final Key body = UniqueKey();
    final Key floatingActionButton = UniqueKey();
    final Key persistentFooterButton = UniqueKey();
    final Key drawer = UniqueKey();
    final Key bottomNavigationBar = UniqueKey();
    final Key insideAppBar = UniqueKey();
    final Key insideBody = UniqueKey();
    final Key insideFloatingActionButton = UniqueKey();
    final Key insidePersistentFooterButton = UniqueKey();
    final Key insideDrawer = UniqueKey();
    final Key insideBottomNavigationBar = UniqueKey();
Ian Hickson's avatar
Ian Hickson committed
882
    await tester.pumpWidget(
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
      Localizations(
        locale: const Locale('en', 'us'),
        delegates: const <LocalizationsDelegate<dynamic>>[
          DefaultWidgetsLocalizations.delegate,
          DefaultMaterialLocalizations.delegate,
        ],
        child: Directionality(
          textDirection: TextDirection.rtl,
          child: MediaQuery(
            data: const MediaQueryData(
              padding: EdgeInsets.only(
                left: 20.0,
                top: 30.0,
                right: 50.0,
                bottom: 60.0,
Ian Hickson's avatar
Ian Hickson committed
898
              ),
899
              viewInsets: EdgeInsets.only(bottom: 200.0),
Ian Hickson's avatar
Ian Hickson committed
900
            ),
901
            child: Scaffold(
902
              drawerDragStartBehavior: DragStartBehavior.down,
903 904 905 906 907 908 909 910
              appBar: PreferredSize(
                preferredSize: const Size(11.0, 13.0),
                child: Container(
                  key: appBar,
                  child: SafeArea(
                    child: Placeholder(key: insideAppBar),
                  ),
                ),
Ian Hickson's avatar
Ian Hickson committed
911
              ),
912 913 914 915 916
              body: Container(
                key: body,
                child: SafeArea(
                  child: Placeholder(key: insideBody),
                ),
Ian Hickson's avatar
Ian Hickson committed
917
              ),
918 919 920 921
              floatingActionButton: SizedBox(
                key: floatingActionButton,
                width: 77.0,
                height: 77.0,
922
                child: SafeArea(
923
                  child: Placeholder(key: insideFloatingActionButton),
Ian Hickson's avatar
Ian Hickson committed
924 925
                ),
              ),
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
              persistentFooterButtons: <Widget>[
                SizedBox(
                  key: persistentFooterButton,
                  width: 100.0,
                  height: 90.0,
                  child: SafeArea(
                    child: Placeholder(key: insidePersistentFooterButton),
                  ),
                ),
              ],
              drawer: Container(
                key: drawer,
                width: 204.0,
                child: SafeArea(
                  child: Placeholder(key: insideDrawer),
                ),
Ian Hickson's avatar
Ian Hickson committed
942
              ),
943 944 945 946 947 948
              bottomNavigationBar: SizedBox(
                key: bottomNavigationBar,
                height: 85.0,
                child: SafeArea(
                  child: Placeholder(key: insideBottomNavigationBar),
                ),
Ian Hickson's avatar
Ian Hickson committed
949 950 951 952 953 954 955 956 957 958 959
              ),
            ),
          ),
        ),
      ),
    );
    // open drawer
    await tester.flingFrom(const Offset(795.0, 5.0), const Offset(-200.0, 0.0), 10.0);
    await tester.pump();
    await tester.pump(const Duration(seconds: 1));

Dan Field's avatar
Dan Field committed
960 961 962 963 964 965 966 967 968 969 970 971
    expect(tester.getRect(find.byKey(appBar)), const Rect.fromLTRB(0.0, 0.0, 800.0, 43.0));
    expect(tester.getRect(find.byKey(body)), const Rect.fromLTRB(0.0, 43.0, 800.0, 348.0));
    expect(tester.getRect(find.byKey(floatingActionButton)), rectMoreOrLessEquals(const Rect.fromLTRB(36.0, 255.0, 113.0, 332.0)));
    expect(tester.getRect(find.byKey(persistentFooterButton)),const  Rect.fromLTRB(28.0, 357.0, 128.0, 447.0)); // Note: has 8px each top/bottom padding.
    expect(tester.getRect(find.byKey(drawer)), const Rect.fromLTRB(596.0, 0.0, 800.0, 600.0));
    expect(tester.getRect(find.byKey(bottomNavigationBar)), const Rect.fromLTRB(0.0, 515.0, 800.0, 600.0));
    expect(tester.getRect(find.byKey(insideAppBar)), const Rect.fromLTRB(20.0, 30.0, 750.0, 43.0));
    expect(tester.getRect(find.byKey(insideBody)), const Rect.fromLTRB(20.0, 43.0, 750.0, 348.0));
    expect(tester.getRect(find.byKey(insideFloatingActionButton)), rectMoreOrLessEquals(const Rect.fromLTRB(36.0, 255.0, 113.0, 332.0)));
    expect(tester.getRect(find.byKey(insidePersistentFooterButton)), const Rect.fromLTRB(28.0, 357.0, 128.0, 447.0));
    expect(tester.getRect(find.byKey(insideDrawer)), const Rect.fromLTRB(596.0, 30.0, 750.0, 540.0));
    expect(tester.getRect(find.byKey(insideBottomNavigationBar)), const Rect.fromLTRB(20.0, 515.0, 750.0, 540.0));
972 973 974
  });

  testWidgets('Scaffold and extreme window padding - persistent footer buttons only', (WidgetTester tester) async {
975 976 977 978 979 980 981 982 983 984
    final Key appBar = UniqueKey();
    final Key body = UniqueKey();
    final Key floatingActionButton = UniqueKey();
    final Key persistentFooterButton = UniqueKey();
    final Key drawer = UniqueKey();
    final Key insideAppBar = UniqueKey();
    final Key insideBody = UniqueKey();
    final Key insideFloatingActionButton = UniqueKey();
    final Key insidePersistentFooterButton = UniqueKey();
    final Key insideDrawer = UniqueKey();
985
    await tester.pumpWidget(
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
      Localizations(
        locale: const Locale('en', 'us'),
        delegates: const <LocalizationsDelegate<dynamic>>[
          DefaultWidgetsLocalizations.delegate,
          DefaultMaterialLocalizations.delegate,
        ],
        child: Directionality(
          textDirection: TextDirection.rtl,
          child: MediaQuery(
            data: const MediaQueryData(
              padding: EdgeInsets.only(
                left: 20.0,
                top: 30.0,
                right: 50.0,
                bottom: 60.0,
1001
              ),
1002
              viewInsets: EdgeInsets.only(bottom: 200.0),
1003
            ),
1004 1005 1006 1007 1008 1009 1010 1011 1012
            child: Scaffold(
              appBar: PreferredSize(
                preferredSize: const Size(11.0, 13.0),
                child: Container(
                  key: appBar,
                  child: SafeArea(
                    child: Placeholder(key: insideAppBar),
                  ),
                ),
1013
              ),
1014 1015 1016 1017 1018
              body: Container(
                key: body,
                child: SafeArea(
                  child: Placeholder(key: insideBody),
                ),
1019
              ),
1020 1021 1022 1023
              floatingActionButton: SizedBox(
                key: floatingActionButton,
                width: 77.0,
                height: 77.0,
1024
                child: SafeArea(
1025
                  child: Placeholder(key: insideFloatingActionButton),
1026 1027
                ),
              ),
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
              persistentFooterButtons: <Widget>[
                SizedBox(
                  key: persistentFooterButton,
                  width: 100.0,
                  height: 90.0,
                  child: SafeArea(
                    child: Placeholder(key: insidePersistentFooterButton),
                  ),
                ),
              ],
              drawer: Container(
                key: drawer,
                width: 204.0,
                child: SafeArea(
                  child: Placeholder(key: insideDrawer),
                ),
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
              ),
            ),
          ),
        ),
      ),
    );
    // open drawer
    await tester.flingFrom(const Offset(795.0, 5.0), const Offset(-200.0, 0.0), 10.0);
    await tester.pump();
    await tester.pump(const Duration(seconds: 1));

Dan Field's avatar
Dan Field committed
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
    expect(tester.getRect(find.byKey(appBar)), const Rect.fromLTRB(0.0, 0.0, 800.0, 43.0));
    expect(tester.getRect(find.byKey(body)), const Rect.fromLTRB(0.0, 43.0, 800.0, 400.0));
    expect(tester.getRect(find.byKey(floatingActionButton)), rectMoreOrLessEquals(const Rect.fromLTRB(36.0, 307.0, 113.0, 384.0)));
    expect(tester.getRect(find.byKey(persistentFooterButton)), const Rect.fromLTRB(28.0, 442.0, 128.0, 532.0)); // Note: has 8px each top/bottom padding.
    expect(tester.getRect(find.byKey(drawer)), const Rect.fromLTRB(596.0, 0.0, 800.0, 600.0));
    expect(tester.getRect(find.byKey(insideAppBar)), const Rect.fromLTRB(20.0, 30.0, 750.0, 43.0));
    expect(tester.getRect(find.byKey(insideBody)), const Rect.fromLTRB(20.0, 43.0, 750.0, 400.0));
    expect(tester.getRect(find.byKey(insideFloatingActionButton)), rectMoreOrLessEquals(const Rect.fromLTRB(36.0, 307.0, 113.0, 384.0)));
    expect(tester.getRect(find.byKey(insidePersistentFooterButton)), const Rect.fromLTRB(28.0, 442.0, 128.0, 532.0));
    expect(tester.getRect(find.byKey(insideDrawer)), const Rect.fromLTRB(596.0, 30.0, 750.0, 540.0));
Ian Hickson's avatar
Ian Hickson committed
1065
  });
1066 1067


1068 1069
  group('ScaffoldGeometry', () {
    testWidgets('bottomNavigationBar', (WidgetTester tester) async {
1070 1071 1072 1073
      final GlobalKey key = GlobalKey();
      await tester.pumpWidget(MaterialApp(home: Scaffold(
            body: Container(),
            bottomNavigationBar: ConstrainedBox(
1074 1075
              key: key,
              constraints: const BoxConstraints.expand(height: 80.0),
1076
              child: _GeometryListener(),
1077 1078 1079 1080 1081
            ),
      )));

      final RenderBox navigationBox = tester.renderObject(find.byKey(key));
      final RenderBox appBox = tester.renderObject(find.byType(MaterialApp));
1082
      final _GeometryListenerState listenerState = tester.state(find.byType(_GeometryListener));
1083 1084 1085 1086
      final ScaffoldGeometry geometry = listenerState.cache.value;

      expect(
        geometry.bottomNavigationBarTop,
1087
        appBox.size.height - navigationBox.size.height,
1088 1089 1090 1091
      );
    });

    testWidgets('no bottomNavigationBar', (WidgetTester tester) async {
1092 1093
      await tester.pumpWidget(MaterialApp(home: Scaffold(
            body: ConstrainedBox(
1094
              constraints: const BoxConstraints.expand(height: 80.0),
1095
              child: _GeometryListener(),
1096 1097 1098
            ),
      )));

1099
      final _GeometryListenerState listenerState = tester.state(find.byType(_GeometryListener));
1100 1101 1102 1103
      final ScaffoldGeometry geometry = listenerState.cache.value;

      expect(
        geometry.bottomNavigationBarTop,
1104
        null,
1105 1106 1107
      );
    });

1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
    testWidgets('Scaffold BottomNavigationBar bottom padding is not consumed by viewInsets.', (WidgetTester tester) async {
      Widget boilerplate(Widget child) {
        return Localizations(
          locale: const Locale('en', 'us'),
          delegates: const <LocalizationsDelegate<dynamic>>[
            DefaultWidgetsLocalizations.delegate,
            DefaultMaterialLocalizations.delegate,
          ],
          child: Directionality(
            textDirection: TextDirection.ltr,
            child: child,
          ),
        );
      }

      final Widget child = boilerplate(
        Scaffold(
          resizeToAvoidBottomInset: false,
          body: const Placeholder(),
          bottomNavigationBar: BottomNavigationBar(
            items: const <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                icon: Icon(Icons.add),
                title: Text('test'),
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.add),
                title: Text('test'),
              )
            ]
          ),
        ),
      );

      await tester.pumpWidget(
        MediaQuery(
          data: const MediaQueryData(padding: EdgeInsets.only(bottom: 20.0)),
          child: child,
        ),
      );
      final Offset initialPoint = tester.getCenter(find.byType(Placeholder));
      // Consume bottom padding - as if by the keyboard opening
      await tester.pumpWidget(
        MediaQuery(
          data: const MediaQueryData(
            padding: EdgeInsets.zero,
            viewPadding: EdgeInsets.only(bottom: 20),
            viewInsets: EdgeInsets.only(bottom: 300),
          ),
          child: child,
        ),
      );
      final Offset finalPoint = tester.getCenter(find.byType(Placeholder));
      expect(initialPoint, finalPoint);
    });

1164
    testWidgets('floatingActionButton', (WidgetTester tester) async {
1165 1166 1167 1168
      final GlobalKey key = GlobalKey();
      await tester.pumpWidget(MaterialApp(home: Scaffold(
            body: Container(),
            floatingActionButton: FloatingActionButton(
1169
              key: key,
1170
              child: _GeometryListener(),
1171
              onPressed: () { },
1172 1173 1174 1175
            ),
      )));

      final RenderBox floatingActionButtonBox = tester.renderObject(find.byKey(key));
1176
      final _GeometryListenerState listenerState = tester.state(find.byType(_GeometryListener));
1177 1178 1179 1180 1181 1182
      final ScaffoldGeometry geometry = listenerState.cache.value;

      final Rect fabRect = floatingActionButtonBox.localToGlobal(Offset.zero) & floatingActionButtonBox.size;

      expect(
        geometry.floatingActionButtonArea,
1183
        fabRect,
1184 1185 1186 1187
      );
    });

    testWidgets('no floatingActionButton', (WidgetTester tester) async {
1188 1189
      await tester.pumpWidget(MaterialApp(home: Scaffold(
            body: ConstrainedBox(
1190
              constraints: const BoxConstraints.expand(height: 80.0),
1191
              child: _GeometryListener(),
1192 1193 1194
            ),
      )));

1195
      final _GeometryListenerState listenerState = tester.state(find.byType(_GeometryListener));
1196 1197 1198 1199
      final ScaffoldGeometry geometry = listenerState.cache.value;

      expect(
          geometry.floatingActionButtonArea,
1200
          null,
1201 1202 1203
      );
    });

1204
    testWidgets('floatingActionButton entrance/exit animation', (WidgetTester tester) async {
1205 1206 1207
      final GlobalKey key = GlobalKey();
      await tester.pumpWidget(MaterialApp(home: Scaffold(
            body: ConstrainedBox(
1208
              constraints: const BoxConstraints.expand(height: 80.0),
1209
              child: _GeometryListener(),
1210 1211 1212
            ),
      )));

1213 1214 1215
      await tester.pumpWidget(MaterialApp(home: Scaffold(
            body: Container(),
            floatingActionButton: FloatingActionButton(
1216
              key: key,
1217
              child: _GeometryListener(),
1218
              onPressed: () { },
1219 1220 1221
            ),
      )));

1222
      final _GeometryListenerState listenerState = tester.state(find.byType(_GeometryListener));
1223 1224
      await tester.pump(const Duration(milliseconds: 50));

1225 1226 1227
      ScaffoldGeometry geometry = listenerState.cache.value;
      final Rect transitioningFabRect = geometry.floatingActionButtonArea;

1228 1229 1230 1231
      final double transitioningRotation = tester.widget<RotationTransition>(
        find.byType(RotationTransition),
      ).turns.value;

1232 1233 1234 1235 1236
      await tester.pump(const Duration(seconds: 3));
      geometry = listenerState.cache.value;
      final RenderBox floatingActionButtonBox = tester.renderObject(find.byKey(key));
      final Rect fabRect = floatingActionButtonBox.localToGlobal(Offset.zero) & floatingActionButtonBox.size;

1237 1238 1239 1240 1241 1242 1243 1244
      final double completedRotation = tester.widget<RotationTransition>(
        find.byType(RotationTransition),
      ).turns.value;

      expect(transitioningRotation, lessThan(1.0));

      expect(completedRotation, equals(1.0));

1245 1246
      expect(
        geometry.floatingActionButtonArea,
1247
        fabRect,
1248 1249 1250 1251
      );

      expect(
        geometry.floatingActionButtonArea.center,
1252
        transitioningFabRect.center,
1253
      );
1254 1255

      expect(
1256
        geometry.floatingActionButtonArea.width,
1257
        greaterThan(transitioningFabRect.width),
1258 1259 1260 1261
      );

      expect(
        geometry.floatingActionButtonArea.height,
1262
        greaterThan(transitioningFabRect.height),
1263 1264 1265
      );
    });

1266
    testWidgets('change notifications', (WidgetTester tester) async {
1267
      final GlobalKey key = GlobalKey();
1268
      int numNotificationsAtLastFrame = 0;
1269 1270
      await tester.pumpWidget(MaterialApp(home: Scaffold(
            body: ConstrainedBox(
1271
              constraints: const BoxConstraints.expand(height: 80.0),
1272
              child: _GeometryListener(),
1273 1274 1275
            ),
      )));

1276
      final _GeometryListenerState listenerState = tester.state(find.byType(_GeometryListener));
1277 1278 1279 1280

      expect(listenerState.numNotifications, greaterThan(numNotificationsAtLastFrame));
      numNotificationsAtLastFrame = listenerState.numNotifications;

1281 1282 1283
      await tester.pumpWidget(MaterialApp(home: Scaffold(
            body: Container(),
            floatingActionButton: FloatingActionButton(
1284
              key: key,
1285
              child: _GeometryListener(),
1286
              onPressed: () { },
1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
            ),
      )));

      expect(listenerState.numNotifications, greaterThan(numNotificationsAtLastFrame));
      numNotificationsAtLastFrame = listenerState.numNotifications;

      await tester.pump(const Duration(milliseconds: 50));

      expect(listenerState.numNotifications, greaterThan(numNotificationsAtLastFrame));
      numNotificationsAtLastFrame = listenerState.numNotifications;

      await tester.pump(const Duration(seconds: 3));

      expect(listenerState.numNotifications, greaterThan(numNotificationsAtLastFrame));
      numNotificationsAtLastFrame = listenerState.numNotifications;
    });
1303

jslavitz's avatar
jslavitz committed
1304 1305 1306 1307 1308
    testWidgets('Simultaneous drawers on either side', (WidgetTester tester) async {
      const String bodyLabel = 'I am the body';
      const String drawerLabel = 'I am the label on start side';
      const String endDrawerLabel = 'I am the label on end side';

1309
      final SemanticsTester semantics = SemanticsTester(tester);
1310
      await tester.pumpWidget(const MaterialApp(home: Scaffold(
1311 1312 1313
        body: Text(bodyLabel),
        drawer: Drawer(child: Text(drawerLabel)),
        endDrawer: Drawer(child: Text(endDrawerLabel)),
jslavitz's avatar
jslavitz committed
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
      )));

      expect(semantics, includesNodeWith(label: bodyLabel));
      expect(semantics, isNot(includesNodeWith(label: drawerLabel)));
      expect(semantics, isNot(includesNodeWith(label: endDrawerLabel)));

      final ScaffoldState state = tester.firstState(find.byType(Scaffold));
      state.openDrawer();
      await tester.pump();
      await tester.pump(const Duration(seconds: 1));

      expect(semantics, isNot(includesNodeWith(label: bodyLabel)));
      expect(semantics, includesNodeWith(label: drawerLabel));

      state.openEndDrawer();
      await tester.pump();
      await tester.pump(const Duration(seconds: 1));

      expect(semantics, isNot(includesNodeWith(label: bodyLabel)));
      expect(semantics, includesNodeWith(label: endDrawerLabel));

      semantics.dispose();
    });

1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355
    testWidgets('Drawer state query correctly', (WidgetTester tester) async {
      await tester.pumpWidget(
        MaterialApp(
          home: SafeArea(
            left: false,
            top: true,
            right: false,
            bottom: false,
            child: Scaffold(
              endDrawer: const Drawer(
                child: Text('endDrawer'),
              ),
              drawer: const Drawer(
                child: Text('drawer'),
              ),
              body: const Text('scaffold body'),
              appBar: AppBar(
                centerTitle: true,
1356
                title: const Text('Title'),
1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380
              ),
            ),
          ),
        ),
      );

      final ScaffoldState scaffoldState = tester.state(find.byType(Scaffold));

      final Finder drawerOpenButton = find.byType(IconButton).first;
      final Finder endDrawerOpenButton = find.byType(IconButton).last;

      await tester.tap(drawerOpenButton);
      await tester.pumpAndSettle();
      expect(true, scaffoldState.isDrawerOpen);
      await tester.tap(endDrawerOpenButton);
      await tester.pumpAndSettle();
      expect(false, scaffoldState.isDrawerOpen);

      await tester.tap(endDrawerOpenButton);
      await tester.pumpAndSettle();
      expect(true, scaffoldState.isEndDrawerOpen);
      await tester.tap(drawerOpenButton);
      await tester.pumpAndSettle();
      expect(false, scaffoldState.isEndDrawerOpen);
jslavitz's avatar
jslavitz committed
1381

1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
      scaffoldState.openDrawer();
      expect(true, scaffoldState.isDrawerOpen);
      await tester.tap(drawerOpenButton);
      await tester.pumpAndSettle();

      scaffoldState.openEndDrawer();
      expect(true, scaffoldState.isEndDrawerOpen);
    });

    testWidgets('Dual Drawer Opening', (WidgetTester tester) async {
jslavitz's avatar
jslavitz committed
1392
      await tester.pumpWidget(
1393 1394
        MaterialApp(
          home: SafeArea(
jslavitz's avatar
jslavitz committed
1395 1396 1397 1398
            left: false,
            top: true,
            right: false,
            bottom: false,
1399
            child: Scaffold(
jslavitz's avatar
jslavitz committed
1400
              endDrawer: const Drawer(
1401
                child: Text('endDrawer'),
jslavitz's avatar
jslavitz committed
1402 1403
              ),
              drawer: const Drawer(
1404
                child: Text('drawer'),
jslavitz's avatar
jslavitz committed
1405 1406
              ),
              body: const Text('scaffold body'),
1407
              appBar: AppBar(
jslavitz's avatar
jslavitz committed
1408
                centerTitle: true,
1409 1410
                title: const Text('Title'),
              ),
jslavitz's avatar
jslavitz committed
1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442
            ),
          ),
        ),
      );

      // Open Drawer, tap on end drawer, which closes the drawer, but does
      // not open the drawer.
      await tester.tap(find.byType(IconButton).first);
      await tester.pumpAndSettle();
      await tester.tap(find.byType(IconButton).last);
      await tester.pumpAndSettle();

      expect(find.text('endDrawer'), findsNothing);
      expect(find.text('drawer'), findsNothing);

      // Tapping the first opens the first drawer
      await tester.tap(find.byType(IconButton).first);
      await tester.pumpAndSettle();

      expect(find.text('endDrawer'), findsNothing);
      expect(find.text('drawer'), findsOneWidget);

      // Tapping on the end drawer and then on the drawer should close the
      // drawer and then reopen it.
      await tester.tap(find.byType(IconButton).last);
      await tester.pumpAndSettle();
      await tester.tap(find.byType(IconButton).first);
      await tester.pumpAndSettle();

      expect(find.text('endDrawer'), findsNothing);
      expect(find.text('drawer'), findsOneWidget);
    });
1443

1444 1445
    testWidgets('Drawer opens correctly with padding from MediaQuery (LTR)', (WidgetTester tester) async {
      const double simulatedNotchSize = 40.0;
1446 1447 1448 1449
      await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            drawer: const Drawer(
1450
              child: Text('Drawer'),
1451
            ),
1452
            body: const Text('Scaffold Body'),
1453 1454
            appBar: AppBar(
              centerTitle: true,
1455
              title: const Text('Title'),
1456 1457 1458 1459 1460 1461 1462 1463
            ),
          ),
        ),
      );

      ScaffoldState scaffoldState = tester.state(find.byType(Scaffold));
      expect(scaffoldState.isDrawerOpen, false);

1464
      await tester.dragFrom(const Offset(simulatedNotchSize + 15.0, 100), const Offset(300, 0));
1465 1466 1467 1468 1469 1470 1471
      await tester.pumpAndSettle();
      expect(scaffoldState.isDrawerOpen, false);

      await tester.pumpWidget(
        MaterialApp(
          home: MediaQuery(
            data: const MediaQueryData(
1472
              padding: EdgeInsets.fromLTRB(simulatedNotchSize, 0, 0, 0),
1473 1474 1475
            ),
            child: Scaffold(
              drawer: const Drawer(
1476
                child: Text('Drawer'),
1477
              ),
1478
              body: const Text('Scaffold Body'),
1479 1480
              appBar: AppBar(
                centerTitle: true,
1481
                title: const Text('Title'),
1482 1483 1484 1485 1486 1487 1488 1489
              ),
            ),
          ),
        ),
      );
      scaffoldState = tester.state(find.byType(Scaffold));
      expect(scaffoldState.isDrawerOpen, false);

1490 1491 1492 1493
      await tester.dragFrom(
        const Offset(simulatedNotchSize + 15.0, 100),
        const Offset(300, 0),
      );
1494 1495 1496 1497
      await tester.pumpAndSettle();
      expect(scaffoldState.isDrawerOpen, true);
    });

1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527
    testWidgets('Drawer opens correctly with padding from MediaQuery (RTL)', (WidgetTester tester) async {
      const double simulatedNotchSize = 40.0;
      await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            drawer: const Drawer(
              child: Text('Drawer'),
            ),
            body: const Text('Scaffold Body'),
            appBar: AppBar(
              centerTitle: true,
              title: const Text('Title'),
            ),
          ),
        ),
      );

      final double scaffoldWidth = tester.renderObject<RenderBox>(
        find.byType(Scaffold),
      ).size.width;
      ScaffoldState scaffoldState = tester.state(find.byType(Scaffold));
      expect(scaffoldState.isDrawerOpen, false);

      await tester.dragFrom(
        Offset(scaffoldWidth - simulatedNotchSize - 15.0, 100),
        const Offset(-300, 0),
      );
      await tester.pumpAndSettle();
      expect(scaffoldState.isDrawerOpen, false);

1528 1529 1530 1531
      await tester.pumpWidget(
        MaterialApp(
          home: MediaQuery(
            data: const MediaQueryData(
1532
              padding: EdgeInsets.fromLTRB(0, 0, simulatedNotchSize, 0),
1533 1534 1535 1536 1537
            ),
            child: Directionality(
              textDirection: TextDirection.rtl,
              child: Scaffold(
                drawer: const Drawer(
1538
                  child: Text('Drawer'),
1539
                ),
1540
                body: const Text('Scaffold body'),
1541 1542
                appBar: AppBar(
                  centerTitle: true,
1543
                  title: const Text('Title'),
1544 1545 1546 1547 1548 1549
                ),
              ),
            ),
          ),
        ),
      );
1550 1551
      scaffoldState = tester.state(find.byType(Scaffold));
      expect(scaffoldState.isDrawerOpen, false);
1552

1553 1554 1555 1556 1557 1558 1559 1560
      await tester.dragFrom(
        Offset(scaffoldWidth - simulatedNotchSize - 15.0, 100),
        const Offset(-300, 0),
      );
      await tester.pumpAndSettle();
      expect(scaffoldState.isDrawerOpen, true);
    });
  });
1561

1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578
    testWidgets('Drawer opens correctly with custom edgeDragWidth', (WidgetTester tester) async {
      // The default edge drag width is 20.0.
      await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            drawer: const Drawer(
              child: Text('Drawer'),
            ),
            body: const Text('Scaffold body'),
            appBar: AppBar(
              centerTitle: true,
              title: const Text('Title'),
            ),
          ),
        ),
      );
      ScaffoldState scaffoldState = tester.state(find.byType(Scaffold));
1579 1580
      expect(scaffoldState.isDrawerOpen, false);

1581
      await tester.dragFrom(const Offset(35, 100), const Offset(300, 0));
1582
      await tester.pumpAndSettle();
1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601
      expect(scaffoldState.isDrawerOpen, false);

      await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            drawer: const Drawer(
              child: Text('Drawer'),
            ),
            drawerEdgeDragWidth: 40.0,
            body: const Text('Scaffold Body'),
            appBar: AppBar(
              centerTitle: true,
              title: const Text('Title'),
            ),
          ),
        ),
      );
      scaffoldState = tester.state(find.byType(Scaffold));
      expect(scaffoldState.isDrawerOpen, false);
1602

1603 1604
      await tester.dragFrom(const Offset(35, 100), const Offset(300, 0));
      await tester.pumpAndSettle();
1605 1606
      expect(scaffoldState.isDrawerOpen, true);
    });
1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657

  testWidgets('Nested scaffold body insets', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/20295
    final Key bodyKey = UniqueKey();

    Widget buildFrame(bool innerResizeToAvoidBottomInset, bool outerResizeToAvoidBottomInset) {
      return Directionality(
        textDirection: TextDirection.ltr,
        child: MediaQuery(
          data: const MediaQueryData(viewInsets: EdgeInsets.only(bottom: 100.0)),
          child: Builder(
            builder: (BuildContext context) {
              return Scaffold(
                resizeToAvoidBottomInset: outerResizeToAvoidBottomInset,
                body: Builder(
                  builder: (BuildContext context) {
                    return Scaffold(
                      resizeToAvoidBottomInset: innerResizeToAvoidBottomInset,
                      body: Container(key: bodyKey),
                    );
                  },
                ),
              );
            },
          ),
        ),
      );
    }

    await tester.pumpWidget(buildFrame(true, true));
    expect(tester.getSize(find.byKey(bodyKey)), const Size(800.0, 500.0));

    await tester.pumpWidget(buildFrame(false, true));
    expect(tester.getSize(find.byKey(bodyKey)), const Size(800.0, 500.0));

    await tester.pumpWidget(buildFrame(true, false));
    expect(tester.getSize(find.byKey(bodyKey)), const Size(800.0, 500.0));

    // This is the only case where the body is not bottom inset.
    await tester.pumpWidget(buildFrame(false, false));
    expect(tester.getSize(find.byKey(bodyKey)), const Size(800.0, 600.0));

    await tester.pumpWidget(buildFrame(null, null));  // resizeToAvoidBottomInset default  is true
    expect(tester.getSize(find.byKey(bodyKey)), const Size(800.0, 500.0));

    await tester.pumpWidget(buildFrame(null, false));
    expect(tester.getSize(find.byKey(bodyKey)), const Size(800.0, 500.0));

    await tester.pumpWidget(buildFrame(false, null));
    expect(tester.getSize(find.byKey(bodyKey)), const Size(800.0, 500.0));
  });
1658 1659
}

1660
class _GeometryListener extends StatefulWidget {
1661
  @override
1662
  _GeometryListenerState createState() => _GeometryListenerState();
1663 1664
}

1665
class _GeometryListenerState extends State<_GeometryListener> {
1666 1667
  @override
  Widget build(BuildContext context) {
1668
    return CustomPaint(
1669 1670 1671 1672 1673 1674
      painter: cache
    );
  }

  int numNotifications = 0;
  ValueListenable<ScaffoldGeometry> geometryListenable;
1675
  _GeometryCachePainter cache;
1676 1677 1678 1679 1680 1681 1682 1683 1684 1685

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    final ValueListenable<ScaffoldGeometry> newListenable = Scaffold.geometryOf(context);
    if (geometryListenable == newListenable)
      return;

    if (geometryListenable != null)
      geometryListenable.removeListener(onGeometryChanged);
1686

1687 1688
    geometryListenable = newListenable;
    geometryListenable.addListener(onGeometryChanged);
1689
    cache = _GeometryCachePainter(geometryListenable);
1690 1691 1692 1693 1694 1695 1696 1697 1698 1699
  }

  void onGeometryChanged() {
    numNotifications += 1;
  }
}

// The Scaffold.geometryOf() value is only available at paint time.
// To fetch it for the tests we implement this CustomPainter that just
// caches the ScaffoldGeometry value in its paint method.
1700 1701
class _GeometryCachePainter extends CustomPainter {
  _GeometryCachePainter(this.geometryListenable) : super(repaint: geometryListenable);
1702 1703 1704 1705 1706 1707 1708 1709 1710 1711

  final ValueListenable<ScaffoldGeometry> geometryListenable;

  ScaffoldGeometry value;
  @override
  void paint(Canvas canvas, Size size) {
    value = geometryListenable.value;
  }

  @override
1712
  bool shouldRepaint(_GeometryCachePainter oldDelegate) {
1713 1714
    return true;
  }
1715
}
1716

1717 1718 1719
class _CustomPageRoute<T> extends PageRoute<T> {
  _CustomPageRoute({
    @required this.builder,
1720 1721 1722
    RouteSettings settings = const RouteSettings(),
    this.maintainState = true,
    bool fullscreenDialog = false,
1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749
  }) : assert(builder != null),
       super(settings: settings, fullscreenDialog: fullscreenDialog);

  final WidgetBuilder builder;

  @override
  Duration get transitionDuration => const Duration(milliseconds: 300);

  @override
  Color get barrierColor => null;

  @override
  String get barrierLabel => null;

  @override
  final bool maintainState;

  @override
  Widget buildPage(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) {
    return builder(context);
  }

  @override
  Widget buildTransitions(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
    return child;
  }
}