scaffold_test.dart 39.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

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

12
void main() {
13
  testWidgets('Scaffold control test', (WidgetTester tester) async {
14
    final Key bodyKey = new UniqueKey();
15 16 17 18 19 20
    await tester.pumpWidget(new Directionality(
      textDirection: TextDirection.ltr,
      child: new Scaffold(
        appBar: new AppBar(title: const Text('Title')),
        body: new Container(key: bodyKey),
      ),
21
    ));
22
    expect(tester.takeException(), isFlutterError);
23

24 25 26 27 28 29
    await tester.pumpWidget(new MaterialApp(
      home: new Scaffold(
        appBar: new AppBar(title: const Text('Title')),
        body: new Container(key: bodyKey),
      ),
    ));
30
    RenderBox bodyBox = tester.renderObject(find.byKey(bodyKey));
31
    expect(bodyBox.size, equals(const Size(800.0, 544.0)));
32

33 34 35
    await tester.pumpWidget(new Directionality(
      textDirection: TextDirection.ltr,
      child: new MediaQuery(
36
        data: const MediaQueryData(viewInsets: EdgeInsets.only(bottom: 100.0)),
37 38 39 40 41
        child: new Scaffold(
          appBar: new AppBar(title: const Text('Title')),
          body: new Container(key: bodyKey),
        ),
      ),
42
    ));
43

44
    bodyBox = tester.renderObject(find.byKey(bodyKey));
45
    expect(bodyBox.size, equals(const Size(800.0, 444.0)));
46

47 48 49
    await tester.pumpWidget(new Directionality(
      textDirection: TextDirection.ltr,
      child: new MediaQuery(
50
        data: const MediaQueryData(viewInsets: EdgeInsets.only(bottom: 100.0)),
51 52 53 54 55 56
        child: new Scaffold(
          appBar: new AppBar(title: const Text('Title')),
          body: new Container(key: bodyKey),
          resizeToAvoidBottomPadding: false,
        ),
      ),
57
    ));
58

59
    bodyBox = tester.renderObject(find.byKey(bodyKey));
60
    expect(bodyBox.size, equals(const Size(800.0, 544.0)));
61
  });
62

63
  testWidgets('Scaffold large bottom padding test', (WidgetTester tester) async {
64
    final Key bodyKey = new UniqueKey();
65 66 67 68
    await tester.pumpWidget(new Directionality(
      textDirection: TextDirection.ltr,
      child: new MediaQuery(
        data: const MediaQueryData(
69
          viewInsets: EdgeInsets.only(bottom: 700.0),
70 71 72 73
        ),
        child: new Scaffold(
          body: new Container(key: bodyKey),
        ),
74 75 76
      ),
    ));

77
    final RenderBox bodyBox = tester.renderObject(find.byKey(bodyKey));
78 79
    expect(bodyBox.size, equals(const Size(800.0, 0.0)));

80 81 82 83
    await tester.pumpWidget(new Directionality(
      textDirection: TextDirection.ltr,
      child: new MediaQuery(
        data: const MediaQueryData(
84
          viewInsets: EdgeInsets.only(bottom: 500.0),
85 86 87 88
        ),
        child: new Scaffold(
          body: new Container(key: bodyKey),
        ),
89 90 91 92 93
      ),
    ));

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

94 95 96 97
    await tester.pumpWidget(new Directionality(
      textDirection: TextDirection.ltr,
      child: new MediaQuery(
        data: const MediaQueryData(
98
          viewInsets: EdgeInsets.only(bottom: 580.0),
99 100 101 102 103 104
        ),
        child: new Scaffold(
          appBar: new AppBar(
            title: const Text('Title'),
          ),
          body: new Container(key: bodyKey),
105 106 107 108 109 110 111
        ),
      ),
    ));

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

112
  testWidgets('Floating action entrance/exit animation', (WidgetTester tester) async {
113
    await tester.pumpWidget(new MaterialApp(home: const Scaffold(
114 115
      floatingActionButton: FloatingActionButton(
        key: Key('one'),
116
        onPressed: null,
117
        child: Text('1'),
118
      ),
119
    )));
120 121 122

    expect(tester.binding.transientCallbackCount, 0);

123
    await tester.pumpWidget(new MaterialApp(home: const Scaffold(
124 125
      floatingActionButton: FloatingActionButton(
        key: Key('two'),
126
        onPressed: null,
127
        child: Text('2'),
128
      ),
129
    )));
130 131 132 133

    expect(tester.binding.transientCallbackCount, greaterThan(0));
    await tester.pumpWidget(new Container());
    expect(tester.binding.transientCallbackCount, 0);
134

135
    await tester.pumpWidget(new MaterialApp(home: const Scaffold()));
136

137 138
    expect(tester.binding.transientCallbackCount, 0);

139
    await tester.pumpWidget(new MaterialApp(home: const Scaffold(
140 141
      floatingActionButton: FloatingActionButton(
        key: Key('one'),
142
        onPressed: null,
143
        child: Text('1'),
144
      ),
145
    )));
146 147 148

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

150
  testWidgets('Floating action button directionality', (WidgetTester tester) async {
151 152 153
    Widget build(TextDirection textDirection) {
      return new Directionality(
        textDirection: textDirection,
154
        child: const MediaQuery(
155 156
          data: MediaQueryData(
            viewInsets: EdgeInsets.only(bottom: 200.0),
157
          ),
158 159
          child: Scaffold(
            floatingActionButton: FloatingActionButton(
160
              onPressed: null,
161
              child: Text('1'),
162 163 164 165 166 167 168 169 170 171 172
            ),
          ),
        ),
      );
    }

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

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

    await tester.pumpWidget(build(TextDirection.rtl));
173
    expect(tester.binding.transientCallbackCount, 0);
174 175 176 177

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

178
  testWidgets('Drawer scrolling', (WidgetTester tester) async {
179
    final Key drawerKey = new UniqueKey();
180 181
    const double appBarHeight = 256.0;

182
    final ScrollController scrollOffset = new ScrollController();
183

184 185 186 187
    await tester.pumpWidget(
      new MaterialApp(
        home: new Scaffold(
          drawer: new Drawer(
188 189 190
            key: drawerKey,
            child: new ListView(
              controller: scrollOffset,
191 192 193 194 195
              children: new List<Widget>.generate(10,
                (int index) => new SizedBox(height: 100.0, child: new Text('D$index'))
              )
            )
          ),
196 197
          body: new CustomScrollView(
            slivers: <Widget>[
198
              const SliverAppBar(
199 200
                pinned: true,
                expandedHeight: appBarHeight,
201 202
                title: Text('Title'),
                flexibleSpace: FlexibleSpaceBar(title: Text('Title')),
203 204 205
              ),
              new SliverPadding(
                padding: const EdgeInsets.only(top: appBarHeight),
206
                sliver: new SliverList(
207 208 209 210 211 212
                  delegate: new SliverChildListDelegate(new List<Widget>.generate(
                    10, (int index) => new SizedBox(height: 100.0, child: new Text('B$index')),
                  )),
                ),
              ),
            ],
213 214 215 216 217
          ),
        )
      )
    );

218
    final ScaffoldState state = tester.firstState(find.byType(Scaffold));
219 220 221 222 223
    state.openDrawer();

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

224
    expect(scrollOffset.offset, 0.0);
225 226

    const double scrollDelta = 80.0;
227
    await tester.drag(find.byKey(drawerKey), const Offset(0.0, -scrollDelta));
228 229
    await tester.pump();

230
    expect(scrollOffset.offset, scrollDelta);
231

232
    final RenderBox renderBox = tester.renderObject(find.byType(AppBar));
233 234
    expect(renderBox.size.height, equals(appBarHeight));
  });
235

236 237 238 239
  Widget _buildStatusBarTestApp(TargetPlatform platform) {
    return new MaterialApp(
      theme: new ThemeData(platform: platform),
      home: new MediaQuery(
240
        data: const MediaQueryData(padding: EdgeInsets.only(top: 25.0)), // status bar
241 242 243 244
        child: new Scaffold(
          body: new CustomScrollView(
            primary: true,
            slivers: <Widget>[
245
              const SliverAppBar(
246
                title: Text('Title')
247 248 249 250 251 252 253 254 255 256
              ),
              new SliverList(
                delegate: new SliverChildListDelegate(new List<Widget>.generate(
                  20, (int index) => new SizedBox(height: 100.0, child: new Text('$index')),
                )),
              ),
            ],
          ),
        ),
      ),
257
    );
258
  }
259

260 261
  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
262
    final ScrollableState scrollable = tester.state(find.byType(Scrollable));
263 264
    scrollable.position.jumpTo(500.0);
    expect(scrollable.position.pixels, equals(500.0));
265
    await tester.tapAt(const Offset(100.0, 10.0));
266
    await tester.pumpAndSettle();
267
    expect(scrollable.position.pixels, equals(0.0));
268 269 270
  });

  testWidgets('Tapping the status bar does not scroll to top on Android', (WidgetTester tester) async {
271
    await tester.pumpWidget(_buildStatusBarTestApp(TargetPlatform.android));
Adam Barth's avatar
Adam Barth committed
272
    final ScrollableState scrollable = tester.state(find.byType(Scrollable));
273 274
    scrollable.position.jumpTo(500.0);
    expect(scrollable.position.pixels, equals(500.0));
275
    await tester.tapAt(const Offset(100.0, 10.0));
276
    await tester.pump();
277
    await tester.pump(const Duration(seconds: 1));
278
    expect(scrollable.position.pixels, equals(500.0));
279
  });
280 281

  testWidgets('Bottom sheet cannot overlap app bar', (WidgetTester tester) async {
282
    final Key sheetKey = new UniqueKey();
283 284 285 286 287 288

    await tester.pumpWidget(
      new MaterialApp(
        theme: new ThemeData(platform: TargetPlatform.android),
        home: new Scaffold(
          appBar: new AppBar(
289
            title: const Text('Title'),
290 291 292 293 294
          ),
          body: new Builder(
            builder: (BuildContext context) {
              return new GestureDetector(
                onTap: () {
295
                  Scaffold.of(context).showBottomSheet<Null>((BuildContext context) {
296 297
                    return new Container(
                      key: sheetKey,
298
                      color: Colors.blue[500],
299 300 301
                    );
                  });
                },
302
                child: const Text('X'),
303
              );
304 305 306 307
            },
          ),
        ),
      ),
308 309 310 311 312 313
    );

    await tester.tap(find.text('X'));
    await tester.pump(); // start animation
    await tester.pump(const Duration(seconds: 1));

314 315
    final RenderBox appBarBox = tester.renderObject(find.byType(AppBar));
    final RenderBox sheetBox = tester.renderObject(find.byKey(sheetKey));
316

317 318
    final Offset appBarBottomRight = appBarBox.localToGlobal(appBarBox.size.bottomRight(Offset.zero));
    final Offset sheetTopRight = sheetBox.localToGlobal(sheetBox.size.topRight(Offset.zero));
319 320 321

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

323 324 325 326 327
  testWidgets('Persistent bottom buttons are persistent', (WidgetTester tester) async {
    bool didPressButton = false;
    await tester.pumpWidget(
      new MaterialApp(
        home: new Scaffold(
328
          body: new SingleChildScrollView(
329
            child: new Container(
330
              color: Colors.amber[500],
331
              height: 5000.0,
332
              child: const Text('body'),
333 334 335 336 337 338 339
            ),
          ),
          persistentFooterButtons: <Widget>[
            new FlatButton(
              onPressed: () {
                didPressButton = true;
              },
340
              child: const Text('X'),
341 342 343 344 345 346
            )
          ],
        ),
      ),
    );

347
    await tester.drag(find.text('body'), const Offset(0.0, -1000.0));
348 349 350 351 352
    expect(didPressButton, isFalse);
    await tester.tap(find.text('X'));
    expect(didPressButton, isTrue);
  });

353 354 355 356 357 358
  testWidgets('Persistent bottom buttons apply media padding', (WidgetTester tester) async {
    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new MediaQuery(
          data: const MediaQueryData(
359
            padding: EdgeInsets.fromLTRB(10.0, 20.0, 30.0, 40.0),
360 361 362 363 364 365 366 367 368
          ),
          child: new Scaffold(
            body: new SingleChildScrollView(
              child: new Container(
                color: Colors.amber[500],
                height: 5000.0,
                child: const Text('body'),
              ),
            ),
369
            persistentFooterButtons: const <Widget>[Placeholder()],
370 371 372 373 374 375 376 377
          ),
        ),
      ),
    );
    expect(tester.getBottomLeft(find.byType(ButtonBar)), const Offset(10.0, 560.0));
    expect(tester.getBottomRight(find.byType(ButtonBar)), const Offset(770.0, 560.0));
  });

378 379
  group('back arrow', () {
    Future<Null> expectBackIcon(WidgetTester tester, TargetPlatform platform, IconData expectedIcon) async {
380
      final GlobalKey rootKey = new GlobalKey();
381
      final Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{
382
        '/': (_) => new Container(key: rootKey, child: const Text('Home')),
383 384
        '/scaffold': (_) => new Scaffold(
            appBar: new AppBar(),
385
            body: const Text('Scaffold'),
386 387 388 389 390 391 392 393 394 395
        )
      };
      await tester.pumpWidget(
        new MaterialApp(theme: new ThemeData(platform: platform), routes: routes)
      );

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

396
      final Icon icon = tester.widget(find.byType(Icon));
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
      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);
    });
  });
412

413
  group('close button', () {
414
    Future<Null> expectCloseIcon(WidgetTester tester, TargetPlatform platform, IconData expectedIcon, PageRoute<void> routeBuilder()) async {
415 416 417
      await tester.pumpWidget(
        new MaterialApp(
          theme: new ThemeData(platform: platform),
xster's avatar
xster committed
418
          home: new Scaffold(appBar: new AppBar(), body: const Text('Page 1')),
419 420 421
        )
      );

422
      tester.state<NavigatorState>(find.byType(Navigator)).push(routeBuilder());
423 424 425 426 427 428

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

      final Icon icon = tester.widget(find.byType(Icon));
      expect(icon.icon, expectedIcon);
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
      expect(find.byType(CloseButton), findsOneWidget);
    }

    PageRoute<void> materialRouteBuilder() {
      return new MaterialPageRoute<void>(
        builder: (BuildContext context) {
          return new Scaffold(appBar: new AppBar(), body: const Text('Page 2'));
        },
        fullscreenDialog: true,
      );
    }

    PageRoute<void> customPageRouteBuilder() {
      return new _CustomPageRoute<void>(
        builder: (BuildContext context) {
          return new Scaffold(appBar: new AppBar(), body: const Text('Page 2'));
        },
        fullscreenDialog: true,
      );
448 449 450
    }

    testWidgets('Close button shows correctly on Android', (WidgetTester tester) async {
451
      await expectCloseIcon(tester, TargetPlatform.android, Icons.close, materialRouteBuilder);
452 453 454
    });

    testWidgets('Close button shows correctly on Fuchsia', (WidgetTester tester) async {
455
      await expectCloseIcon(tester, TargetPlatform.fuchsia, Icons.close, materialRouteBuilder);
456 457 458
    });

    testWidgets('Close button shows correctly on iOS', (WidgetTester tester) async {
459 460 461 462 463 464 465 466 467 468 469 470 471
      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);
472 473 474
    });
  });

475 476
  group('body size', () {
    testWidgets('body size with container', (WidgetTester tester) async {
477
      final Key testKey = new UniqueKey();
478 479 480
      await tester.pumpWidget(new Directionality(
        textDirection: TextDirection.ltr,
        child: new MediaQuery(
481 482 483 484 485 486 487
          data: const MediaQueryData(),
          child: new Scaffold(
            body: new Container(
              key: testKey,
            ),
          ),
        ),
488
      ));
489
      expect(tester.element(find.byKey(testKey)).size, const Size(800.0, 600.0));
490
      expect(tester.renderObject<RenderBox>(find.byKey(testKey)).localToGlobal(Offset.zero), const Offset(0.0, 0.0));
491 492 493
    });

    testWidgets('body size with sized container', (WidgetTester tester) async {
494
      final Key testKey = new UniqueKey();
495 496 497
      await tester.pumpWidget(new Directionality(
        textDirection: TextDirection.ltr,
        child: new MediaQuery(
498 499 500 501 502 503 504 505
          data: const MediaQueryData(),
          child: new Scaffold(
            body: new Container(
              key: testKey,
              height: 100.0,
            ),
          ),
        ),
506
      ));
507
      expect(tester.element(find.byKey(testKey)).size, const Size(800.0, 100.0));
508
      expect(tester.renderObject<RenderBox>(find.byKey(testKey)).localToGlobal(Offset.zero), const Offset(0.0, 0.0));
509 510 511
    });

    testWidgets('body size with centered container', (WidgetTester tester) async {
512
      final Key testKey = new UniqueKey();
513 514 515
      await tester.pumpWidget(new Directionality(
        textDirection: TextDirection.ltr,
        child: new MediaQuery(
516 517 518 519 520 521 522 523 524
          data: const MediaQueryData(),
          child: new Scaffold(
            body: new Center(
              child: new Container(
                key: testKey,
              ),
            ),
          ),
        ),
525
      ));
526
      expect(tester.element(find.byKey(testKey)).size, const Size(800.0, 600.0));
527
      expect(tester.renderObject<RenderBox>(find.byKey(testKey)).localToGlobal(Offset.zero), const Offset(0.0, 0.0));
528 529 530
    });

    testWidgets('body size with button', (WidgetTester tester) async {
531
      final Key testKey = new UniqueKey();
532 533 534
      await tester.pumpWidget(new Directionality(
        textDirection: TextDirection.ltr,
        child: new MediaQuery(
535 536 537 538 539 540 541 542 543
          data: const MediaQueryData(),
          child: new Scaffold(
            body: new FlatButton(
              key: testKey,
              onPressed: () { },
              child: const Text(''),
            ),
          ),
        ),
544
      ));
545
      expect(tester.element(find.byKey(testKey)).size, const Size(88.0, 48.0));
546
      expect(tester.renderObject<RenderBox>(find.byKey(testKey)).localToGlobal(Offset.zero), const Offset(0.0, 0.0));
547 548
    });
  });
549 550 551 552 553 554 555 556 557

  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';

    final SemanticsTester semantics = new SemanticsTester(tester);
558
    await tester.pumpWidget(new MaterialApp(home: const Scaffold(
559 560 561 562 563
      body: Text(bodyLabel),
      persistentFooterButtons: <Widget>[Text(persistentFooterButtonLabel)],
      bottomNavigationBar: Text(bottomNavigationBarLabel),
      floatingActionButton: Text(floatingActionButtonLabel),
      drawer: Drawer(child: Text(drawerLabel)),
564 565
    )));

566 567 568 569 570
    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)));
571 572 573 574 575 576

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

577 578 579 580 581
    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));
582 583 584

    semantics.dispose();
  });
Ian Hickson's avatar
Ian Hickson committed
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603

  testWidgets('Scaffold and extreme window padding', (WidgetTester tester) async {
    final Key appBar = new UniqueKey();
    final Key body = new UniqueKey();
    final Key floatingActionButton = new UniqueKey();
    final Key persistentFooterButton = new UniqueKey();
    final Key drawer = new UniqueKey();
    final Key bottomNavigationBar = new UniqueKey();
    final Key insideAppBar = new UniqueKey();
    final Key insideBody = new UniqueKey();
    final Key insideFloatingActionButton = new UniqueKey();
    final Key insidePersistentFooterButton = new UniqueKey();
    final Key insideDrawer = new UniqueKey();
    final Key insideBottomNavigationBar = new UniqueKey();
    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.rtl,
        child: new MediaQuery(
          data: const MediaQueryData(
604
            padding: EdgeInsets.only(
Ian Hickson's avatar
Ian Hickson committed
605 606 607
              left: 20.0,
              top: 30.0,
              right: 50.0,
608
              bottom: 60.0,
Ian Hickson's avatar
Ian Hickson committed
609
            ),
610
            viewInsets: EdgeInsets.only(bottom: 200.0),
Ian Hickson's avatar
Ian Hickson committed
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 644 645 646 647 648 649 650 651 652 653 654
          ),
          child: new Scaffold(
            appBar: new PreferredSize(
              preferredSize: const Size(11.0, 13.0),
              child: new Container(
                key: appBar,
                child: new SafeArea(
                  child: new Placeholder(key: insideAppBar),
                ),
              ),
            ),
            body: new Container(
              key: body,
              child: new SafeArea(
                child: new Placeholder(key: insideBody),
              ),
            ),
            floatingActionButton: new SizedBox(
              key: floatingActionButton,
              width: 77.0,
              height: 77.0,
              child: new SafeArea(
                child: new Placeholder(key: insideFloatingActionButton),
              ),
            ),
            persistentFooterButtons: <Widget>[
              new SizedBox(
                key: persistentFooterButton,
                width: 100.0,
                height: 90.0,
                child: new SafeArea(
                  child: new Placeholder(key: insidePersistentFooterButton),
                ),
              ),
            ],
            drawer: new Container(
              key: drawer,
              width: 204.0,
              child: new SafeArea(
                child: new Placeholder(key: insideDrawer),
              ),
            ),
            bottomNavigationBar: new SizedBox(
              key: bottomNavigationBar,
655
              height: 85.0,
Ian Hickson's avatar
Ian Hickson committed
656 657 658 659 660 661 662 663 664 665 666 667 668 669
              child: new SafeArea(
                child: new Placeholder(key: insideBottomNavigationBar),
              ),
            ),
          ),
        ),
      ),
    );
    // 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));

    expect(tester.getRect(find.byKey(appBar)), new Rect.fromLTRB(0.0, 0.0, 800.0, 43.0));
670 671 672
    expect(tester.getRect(find.byKey(body)), new Rect.fromLTRB(0.0, 43.0, 800.0, 348.0));
    expect(tester.getRect(find.byKey(floatingActionButton)), new Rect.fromLTRB(36.0, 255.0, 113.0, 332.0));
    expect(tester.getRect(find.byKey(persistentFooterButton)), new Rect.fromLTRB(28.0, 357.0, 128.0, 447.0)); // Note: has 8px each top/bottom padding.
673 674 675
    expect(tester.getRect(find.byKey(drawer)), new Rect.fromLTRB(596.0, 0.0, 800.0, 600.0));
    expect(tester.getRect(find.byKey(bottomNavigationBar)), new Rect.fromLTRB(0.0, 515.0, 800.0, 600.0));
    expect(tester.getRect(find.byKey(insideAppBar)), new Rect.fromLTRB(20.0, 30.0, 750.0, 43.0));
676 677 678
    expect(tester.getRect(find.byKey(insideBody)), new Rect.fromLTRB(20.0, 43.0, 750.0, 348.0));
    expect(tester.getRect(find.byKey(insideFloatingActionButton)), new Rect.fromLTRB(36.0, 255.0, 113.0, 332.0));
    expect(tester.getRect(find.byKey(insidePersistentFooterButton)), new Rect.fromLTRB(28.0, 357.0, 128.0, 447.0));
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
    expect(tester.getRect(find.byKey(insideDrawer)), new Rect.fromLTRB(596.0, 30.0, 750.0, 540.0));
    expect(tester.getRect(find.byKey(insideBottomNavigationBar)), new Rect.fromLTRB(20.0, 515.0, 750.0, 540.0));
  });

  testWidgets('Scaffold and extreme window padding - persistent footer buttons only', (WidgetTester tester) async {
    final Key appBar = new UniqueKey();
    final Key body = new UniqueKey();
    final Key floatingActionButton = new UniqueKey();
    final Key persistentFooterButton = new UniqueKey();
    final Key drawer = new UniqueKey();
    final Key insideAppBar = new UniqueKey();
    final Key insideBody = new UniqueKey();
    final Key insideFloatingActionButton = new UniqueKey();
    final Key insidePersistentFooterButton = new UniqueKey();
    final Key insideDrawer = new UniqueKey();
    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.rtl,
        child: new MediaQuery(
          data: const MediaQueryData(
699
            padding: EdgeInsets.only(
700 701 702 703 704
              left: 20.0,
              top: 30.0,
              right: 50.0,
              bottom: 60.0,
            ),
705
            viewInsets: EdgeInsets.only(bottom: 200.0),
706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
          ),
          child: new Scaffold(
            appBar: new PreferredSize(
              preferredSize: const Size(11.0, 13.0),
              child: new Container(
                key: appBar,
                child: new SafeArea(
                  child: new Placeholder(key: insideAppBar),
                ),
              ),
            ),
            body: new Container(
              key: body,
              child: new SafeArea(
                child: new Placeholder(key: insideBody),
              ),
            ),
            floatingActionButton: new SizedBox(
              key: floatingActionButton,
              width: 77.0,
              height: 77.0,
              child: new SafeArea(
                child: new Placeholder(key: insideFloatingActionButton),
              ),
            ),
            persistentFooterButtons: <Widget>[
              new SizedBox(
                key: persistentFooterButton,
                width: 100.0,
                height: 90.0,
                child: new SafeArea(
                  child: new Placeholder(key: insidePersistentFooterButton),
                ),
              ),
            ],
            drawer: new Container(
              key: drawer,
              width: 204.0,
              child: new SafeArea(
                child: new Placeholder(key: insideDrawer),
              ),
            ),
          ),
        ),
      ),
    );
    // 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));

    expect(tester.getRect(find.byKey(appBar)), new Rect.fromLTRB(0.0, 0.0, 800.0, 43.0));
    expect(tester.getRect(find.byKey(body)), new Rect.fromLTRB(0.0, 43.0, 800.0, 400.0));
    expect(tester.getRect(find.byKey(floatingActionButton)), new Rect.fromLTRB(36.0, 307.0, 113.0, 384.0));
760
    expect(tester.getRect(find.byKey(persistentFooterButton)), new Rect.fromLTRB(28.0, 442.0, 128.0, 532.0)); // Note: has 8px each top/bottom padding.
Ian Hickson's avatar
Ian Hickson committed
761 762
    expect(tester.getRect(find.byKey(drawer)), new Rect.fromLTRB(596.0, 0.0, 800.0, 600.0));
    expect(tester.getRect(find.byKey(insideAppBar)), new Rect.fromLTRB(20.0, 30.0, 750.0, 43.0));
763 764
    expect(tester.getRect(find.byKey(insideBody)), new Rect.fromLTRB(20.0, 43.0, 750.0, 400.0));
    expect(tester.getRect(find.byKey(insideFloatingActionButton)), new Rect.fromLTRB(36.0, 307.0, 113.0, 384.0));
765
    expect(tester.getRect(find.byKey(insidePersistentFooterButton)), new Rect.fromLTRB(28.0, 442.0, 128.0, 532.0));
766
    expect(tester.getRect(find.byKey(insideDrawer)), new Rect.fromLTRB(596.0, 30.0, 750.0, 540.0));
Ian Hickson's avatar
Ian Hickson committed
767
  });
768 769


770 771 772 773 774 775 776 777
  group('ScaffoldGeometry', () {
    testWidgets('bottomNavigationBar', (WidgetTester tester) async {
      final GlobalKey key = new GlobalKey();
      await tester.pumpWidget(new MaterialApp(home: new Scaffold(
            body: new Container(),
            bottomNavigationBar: new ConstrainedBox(
              key: key,
              constraints: const BoxConstraints.expand(height: 80.0),
778
              child: new _GeometryListener(),
779 780 781 782 783
            ),
      )));

      final RenderBox navigationBox = tester.renderObject(find.byKey(key));
      final RenderBox appBox = tester.renderObject(find.byType(MaterialApp));
784
      final _GeometryListenerState listenerState = tester.state(find.byType(_GeometryListener));
785 786 787 788 789 790 791 792 793 794 795 796
      final ScaffoldGeometry geometry = listenerState.cache.value;

      expect(
        geometry.bottomNavigationBarTop,
        appBox.size.height - navigationBox.size.height
      );
    });

    testWidgets('no bottomNavigationBar', (WidgetTester tester) async {
      await tester.pumpWidget(new MaterialApp(home: new Scaffold(
            body: new ConstrainedBox(
              constraints: const BoxConstraints.expand(height: 80.0),
797
              child: new _GeometryListener(),
798 799 800
            ),
      )));

801
      final _GeometryListenerState listenerState = tester.state(find.byType(_GeometryListener));
802 803 804 805 806 807 808 809 810 811 812 813 814 815
      final ScaffoldGeometry geometry = listenerState.cache.value;

      expect(
        geometry.bottomNavigationBarTop,
        null
      );
    });

    testWidgets('floatingActionButton', (WidgetTester tester) async {
      final GlobalKey key = new GlobalKey();
      await tester.pumpWidget(new MaterialApp(home: new Scaffold(
            body: new Container(),
            floatingActionButton: new FloatingActionButton(
              key: key,
816
              child: new _GeometryListener(),
817 818 819 820 821
              onPressed: () {},
            ),
      )));

      final RenderBox floatingActionButtonBox = tester.renderObject(find.byKey(key));
822
      final _GeometryListenerState listenerState = tester.state(find.byType(_GeometryListener));
823 824 825 826 827 828 829 830 831 832 833 834 835 836
      final ScaffoldGeometry geometry = listenerState.cache.value;

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

      expect(
        geometry.floatingActionButtonArea,
        fabRect
      );
    });

    testWidgets('no floatingActionButton', (WidgetTester tester) async {
      await tester.pumpWidget(new MaterialApp(home: new Scaffold(
            body: new ConstrainedBox(
              constraints: const BoxConstraints.expand(height: 80.0),
837
              child: new _GeometryListener(),
838 839 840
            ),
      )));

841
      final _GeometryListenerState listenerState = tester.state(find.byType(_GeometryListener));
842 843 844 845 846 847 848 849
      final ScaffoldGeometry geometry = listenerState.cache.value;

      expect(
          geometry.floatingActionButtonArea,
          null
      );
    });

850
    testWidgets('floatingActionButton entrance/exit animation', (WidgetTester tester) async {
851 852 853 854
      final GlobalKey key = new GlobalKey();
      await tester.pumpWidget(new MaterialApp(home: new Scaffold(
            body: new ConstrainedBox(
              constraints: const BoxConstraints.expand(height: 80.0),
855
              child: new _GeometryListener(),
856 857 858 859 860 861 862
            ),
      )));

      await tester.pumpWidget(new MaterialApp(home: new Scaffold(
            body: new Container(),
            floatingActionButton: new FloatingActionButton(
              key: key,
863
              child: new _GeometryListener(),
864 865 866 867
              onPressed: () {},
            ),
      )));

868
      final _GeometryListenerState listenerState = tester.state(find.byType(_GeometryListener));
869 870
      await tester.pump(const Duration(milliseconds: 50));

871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
      ScaffoldGeometry geometry = listenerState.cache.value;

      final Rect transitioningFabRect = geometry.floatingActionButtonArea;

      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;

      expect(
        geometry.floatingActionButtonArea,
        fabRect
      );

      expect(
        geometry.floatingActionButtonArea.center,
        transitioningFabRect.center
      );
889 890

      expect(
891 892 893 894 895 896 897
        geometry.floatingActionButtonArea.width,
        greaterThan(transitioningFabRect.width)
      );

      expect(
        geometry.floatingActionButtonArea.height,
        greaterThan(transitioningFabRect.height)
898 899 900
      );
    });

901 902 903 904 905 906
    testWidgets('change notifications', (WidgetTester tester) async {
      final GlobalKey key = new GlobalKey();
      int numNotificationsAtLastFrame = 0;
      await tester.pumpWidget(new MaterialApp(home: new Scaffold(
            body: new ConstrainedBox(
              constraints: const BoxConstraints.expand(height: 80.0),
907
              child: new _GeometryListener(),
908 909 910
            ),
      )));

911
      final _GeometryListenerState listenerState = tester.state(find.byType(_GeometryListener));
912 913 914 915 916 917 918 919

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

      await tester.pumpWidget(new MaterialApp(home: new Scaffold(
            body: new Container(),
            floatingActionButton: new FloatingActionButton(
              key: key,
920
              child: new _GeometryListener(),
921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
              onPressed: () {},
            ),
      )));

      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;
    });
938

jslavitz's avatar
jslavitz committed
939 940 941 942 943 944 945
    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';

      final SemanticsTester semantics = new SemanticsTester(tester);
      await tester.pumpWidget(new MaterialApp(home: const Scaffold(
946 947 948
        body: Text(bodyLabel),
        drawer: Drawer(child: Text(drawerLabel)),
        endDrawer: Drawer(child: Text(endDrawerLabel)),
jslavitz's avatar
jslavitz committed
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
      )));

      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();
    });

    testWidgets('Dual Drawer Opening', (WidgetTester tester) async {

      await tester.pumpWidget(
        new MaterialApp(
          home: new SafeArea(
            left: false,
            top: true,
            right: false,
            bottom: false,
            child: new Scaffold(
              endDrawer: const Drawer(
984
                child: Text('endDrawer'),
jslavitz's avatar
jslavitz committed
985 986
              ),
              drawer: const Drawer(
987
                child: Text('drawer'),
jslavitz's avatar
jslavitz committed
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
              ),
              body: const Text('scaffold body'),
              appBar: new AppBar(
                centerTitle: true,
                title: const Text('Title')
              )
            ),
          ),
        ),
      );

      // 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);
    });
1026
  });
1027 1028
}

1029
class _GeometryListener extends StatefulWidget {
1030
  @override
1031
  _GeometryListenerState createState() => new _GeometryListenerState();
1032 1033
}

1034
class _GeometryListenerState extends State<_GeometryListener> {
1035 1036 1037 1038 1039 1040 1041 1042 1043
  @override
  Widget build(BuildContext context) {
    return new CustomPaint(
      painter: cache
    );
  }

  int numNotifications = 0;
  ValueListenable<ScaffoldGeometry> geometryListenable;
1044
  _GeometryCachePainter cache;
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054

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

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

1056 1057
    geometryListenable = newListenable;
    geometryListenable.addListener(onGeometryChanged);
1058
    cache = new _GeometryCachePainter(geometryListenable);
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
  }

  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.
1069 1070
class _GeometryCachePainter extends CustomPainter {
  _GeometryCachePainter(this.geometryListenable) : super(repaint: geometryListenable);
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080

  final ValueListenable<ScaffoldGeometry> geometryListenable;

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

  @override
1081
  bool shouldRepaint(_GeometryCachePainter oldDelegate) {
1082 1083
    return true;
  }
1084
}
1085

1086 1087 1088
class _CustomPageRoute<T> extends PageRoute<T> {
  _CustomPageRoute({
    @required this.builder,
1089 1090 1091
    RouteSettings settings = const RouteSettings(),
    this.maintainState = true,
    bool fullscreenDialog = false,
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
  }) : 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;
  }
}