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

5
import 'package:flutter/foundation.dart';
6
import 'package:flutter/material.dart';
7
import 'package:flutter/rendering.dart';
8 9
import 'package:flutter_test/flutter_test.dart';

10
Widget buildSliverAppBarApp({ bool floating, bool pinned, double expandedHeight, bool snap: false }) {
11 12
  return new Localizations(
    locale: const Locale('en', 'US'),
13
    delegates: const <LocalizationsDelegate<dynamic>>[
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
      DefaultMaterialLocalizations.delegate,
      DefaultWidgetsLocalizations.delegate,
    ],
    child: new Directionality(
      textDirection: TextDirection.ltr,
      child: new MediaQuery(
        data: const MediaQueryData(),
        child: new Scaffold(
          body: new DefaultTabController(
            length: 3,
            child: new CustomScrollView(
              primary: true,
              slivers: <Widget>[
                new SliverAppBar(
                  title: const Text('AppBar Title'),
                  floating: floating,
                  pinned: pinned,
                  expandedHeight: expandedHeight,
                  snap: snap,
                  bottom: new TabBar(
                    tabs: <String>['A','B','C'].map((String t) => new Tab(text: 'TAB $t')).toList(),
                  ),
36
                ),
37 38 39 40 41
                new SliverToBoxAdapter(
                  child: new Container(
                    height: 1200.0,
                    color: Colors.orange[400],
                  ),
42
                ),
43 44
              ],
            ),
45
          ),
46
        ),
47
      ),
48 49 50 51 52 53 54 55 56 57 58 59 60
    ),
  );
}

ScrollController primaryScrollController(WidgetTester tester) {
  return PrimaryScrollController.of(tester.element(find.byType(CustomScrollView)));
}

bool appBarIsVisible(WidgetTester tester) {
  final RenderSliver sliver = tester.element(find.byType(SliverAppBar)).findRenderObject();
  return sliver.geometry.visible;
}

61
double appBarHeight(WidgetTester tester) => tester.getSize(find.byType(AppBar)).height;
Hans Muller's avatar
Hans Muller committed
62 63
double appBarTop(WidgetTester tester) => tester.getTopLeft(find.byType(AppBar)).dy;
double appBarBottom(WidgetTester tester) => tester.getBottomLeft(find.byType(AppBar)).dy;
64

65
double tabBarHeight(WidgetTester tester) => tester.getSize(find.byType(TabBar)).height;
66

67 68 69 70 71 72 73
void main() {
  testWidgets('AppBar centers title on iOS', (WidgetTester tester) async {
    await tester.pumpWidget(
      new MaterialApp(
        theme: new ThemeData(platform: TargetPlatform.android),
        home: new Scaffold(
          appBar: new AppBar(
74
            title: const Text('X'),
75 76 77
          ),
        ),
      ),
78 79
    );

80
    final Finder title = find.text('X');
81
    Offset center = tester.getCenter(title);
82
    Size size = tester.getSize(title);
83
    expect(center.dx, lessThan(400 - size.width / 2.0));
84 85 86 87 88 89 90 91 92

    // Clear the widget tree to avoid animating between Android and iOS.
    await tester.pumpWidget(new Container(key: new UniqueKey()));

    await tester.pumpWidget(
      new MaterialApp(
        theme: new ThemeData(platform: TargetPlatform.iOS),
        home: new Scaffold(
          appBar: new AppBar(
93
            title: const Text('X'),
94 95 96
          ),
        ),
      ),
97 98 99 100
    );

    center = tester.getCenter(title);
    size = tester.getSize(title);
101 102
    expect(center.dx, greaterThan(400 - size.width / 2.0));
    expect(center.dx, lessThan(400 + size.width / 2.0));
103 104 105 106 107 108 109 110 111

    // One action is still centered.

    await tester.pumpWidget(
      new MaterialApp(
        theme: new ThemeData(platform: TargetPlatform.iOS),
        home: new Scaffold(
          appBar: new AppBar(
            title: const Text('X'),
112
            actions: const <Widget>[
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
              const Icon(Icons.thumb_up),
            ],
          ),
        ),
      ),
    );

    center = tester.getCenter(title);
    size = tester.getSize(title);
    expect(center.dx, greaterThan(400 - size.width / 2.0));
    expect(center.dx, lessThan(400 + size.width / 2.0));

    // Two actions is left aligned again.

    await tester.pumpWidget(
      new MaterialApp(
        theme: new ThemeData(platform: TargetPlatform.iOS),
        home: new Scaffold(
          appBar: new AppBar(
            title: const Text('X'),
133
            actions: const <Widget>[
134 135 136 137 138 139 140 141 142 143 144
              const Icon(Icons.thumb_up),
              const Icon(Icons.thumb_up),
            ],
          ),
        ),
      ),
    );

    center = tester.getCenter(title);
    size = tester.getSize(title);
    expect(center.dx, lessThan(400 - size.width / 2.0));
145
  });
146 147 148 149 150 151 152 153

  testWidgets('AppBar centerTitle:true centers on Android', (WidgetTester tester) async {
    await tester.pumpWidget(
      new MaterialApp(
        theme: new ThemeData(platform: TargetPlatform.android),
        home: new Scaffold(
          appBar: new AppBar(
            centerTitle: true,
154
            title: const Text('X'),
155 156 157 158 159
          )
        )
      )
    );

160
    final Finder title = find.text('X');
161
    final Offset center = tester.getCenter(title);
162
    final Size size = tester.getSize(title);
163 164
    expect(center.dx, greaterThan(400 - size.width / 2.0));
    expect(center.dx, lessThan(400 + size.width / 2.0));
165 166
  });

167
  testWidgets('AppBar centerTitle:false title start edge is 16.0 (LTR)', (WidgetTester tester) async {
168 169 170 171 172
    await tester.pumpWidget(
      new MaterialApp(
        home: new Scaffold(
          appBar: new AppBar(
            centerTitle: false,
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
            title: const Placeholder(key: const Key('X')),
          ),
        ),
      ),
    );

    final Finder titleWidget = find.byKey(const Key('X'));
    expect(tester.getTopLeft(titleWidget).dx, 16.0);
    // 4.0 is due to AppBar right padding.
    expect(tester.getTopRight(titleWidget).dx, 800 - 16.0 - 4.0);
  });

  testWidgets('AppBar centerTitle:false title start edge is 16.0 (RTL)', (WidgetTester tester) async {
    await tester.pumpWidget(
      new MaterialApp(
        home: new Directionality(
          textDirection: TextDirection.rtl,
          child: new Scaffold(
            appBar: new AppBar(
              centerTitle: false,
              title: const Placeholder(key: const Key('X')),
            ),
          ),
        ),
      ),
    );

    final Finder titleWidget = find.byKey(const Key('X'));
    expect(tester.getTopRight(titleWidget).dx, 800.0 - 16.0);
    // 4.0 is due to AppBar right padding.
    expect(tester.getTopLeft(titleWidget).dx, 16.0 + 4.0);
  });

  testWidgets('AppBar titleSpacing:32 title start edge is 32.0 (LTR)', (WidgetTester tester) async {
    await tester.pumpWidget(
      new MaterialApp(
        home: new Scaffold(
          appBar: new AppBar(
            centerTitle: false,
            titleSpacing: 32.0,
            title: const Placeholder(key: const Key('X')),
214 215 216 217 218
          ),
        ),
      ),
    );

219 220 221 222
    final Finder titleWidget = find.byKey(const Key('X'));
    expect(tester.getTopLeft(titleWidget).dx, 32.0);
    // 4.0 is due to AppBar right padding.
    expect(tester.getTopRight(titleWidget).dx, 800 - 32.0 - 4.0);
223 224
  });

225
  testWidgets('AppBar titleSpacing:32 title start edge is 32.0 (RTL)', (WidgetTester tester) async {
226 227 228 229 230 231 232
    await tester.pumpWidget(
      new MaterialApp(
        home: new Directionality(
          textDirection: TextDirection.rtl,
          child: new Scaffold(
            appBar: new AppBar(
              centerTitle: false,
233 234
              titleSpacing: 32.0,
              title: const Placeholder(key: const Key('X')),
235 236 237 238 239 240
            ),
          ),
        ),
      ),
    );

241 242 243 244
    final Finder titleWidget = find.byKey(const Key('X'));
    expect(tester.getTopRight(titleWidget).dx, 800.0 - 32.0);
    // 4.0 is due to AppBar right padding.
    expect(tester.getTopLeft(titleWidget).dx, 32.0 + 4.0);
245 246
  });

247
  testWidgets(
248
    'AppBar centerTitle:false leading button title left edge is 72.0 (LTR)',
249 250 251 252 253 254
    (WidgetTester tester) async {
    await tester.pumpWidget(
      new MaterialApp(
        home: new Scaffold(
          appBar: new AppBar(
            centerTitle: false,
255
            title: const Text('X'),
256 257
          ),
          // A drawer causes a leading hamburger.
258
          drawer: const Drawer(),
259 260
        ),
      ),
261 262
    );

263
    expect(tester.getTopLeft(find.text('X')).dx, 72.0);
264 265
  });

266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
  testWidgets(
    'AppBar centerTitle:false leading button title left edge is 72.0 (RTL)',
    (WidgetTester tester) async {
    await tester.pumpWidget(
      new MaterialApp(
        home: new Directionality(
          textDirection: TextDirection.rtl,
          child: new Scaffold(
            appBar: new AppBar(
              centerTitle: false,
              title: const Text('X'),
            ),
            // A drawer causes a leading hamburger.
            drawer: const Drawer(),
          ),
        ),
      ),
    );

    expect(tester.getTopRight(find.text('X')).dx, 800.0 - 72.0);
  });

Ian Hickson's avatar
Ian Hickson committed
288
  testWidgets('AppBar centerTitle:false title overflow OK', (WidgetTester tester) async {
289 290 291
    // The app bar's title should be constrained to fit within the available space
    // between the leading and actions widgets.

292
    final Key titleKey = new UniqueKey();
293
    Widget leading = new Container();
294 295 296 297 298 299 300 301 302 303
    List<Widget> actions;

    Widget buildApp() {
      return new MaterialApp(
        home: new Scaffold(
          appBar: new AppBar(
            leading: leading,
            centerTitle: false,
            title: new Container(
              key: titleKey,
304
              constraints: new BoxConstraints.loose(const Size(1000.0, 1000.0)),
305
            ),
306 307 308
            actions: actions,
          ),
        ),
309 310 311 312 313
      );
    }

    await tester.pumpWidget(buildApp());

314
    final Finder title = find.byKey(titleKey);
315
    expect(tester.getTopLeft(title).dx, 72.0);
316 317 318 319 320 321
    expect(tester.getSize(title).width, equals(
        800.0 // Screen width.
        - 4.0 // Left margin before the leading button.
        - 56.0 // Leading button width.
        - 16.0 // Leading button to title padding.
        - 16.0)); // Title right side padding.
322 323

    actions = <Widget>[
324 325
      const SizedBox(width: 100.0),
      const SizedBox(width: 100.0)
326 327 328
    ];
    await tester.pumpWidget(buildApp());

329
    expect(tester.getTopLeft(title).dx, 72.0);
330
    // The title shrinks by 200.0 to allow for the actions widgets.
331 332 333 334 335 336 337
    expect(tester.getSize(title).width, equals(
        800.0 // Screen width.
        - 4.0 // Left margin before the leading button.
        - 56.0 // Leading button width.
        - 16.0 // Leading button to title padding.
        - 16.0 // Title to actions padding
        - 200.0)); // Actions' width.
338 339 340

    leading = new Container(); // AppBar will constrain the width to 24.0
    await tester.pumpWidget(buildApp());
341
    expect(tester.getTopLeft(title).dx, 72.0);
342
    // Adding a leading widget shouldn't effect the title's size
343
    expect(tester.getSize(title).width, equals(800.0 - 4.0 - 56.0 - 16.0 - 16.0 - 200.0));
344 345
  });

346
  testWidgets('AppBar centerTitle:true title overflow OK (LTR)', (WidgetTester tester) async {
347 348
    // The app bar's title should be constrained to fit within the available space
    // between the leading and actions widgets. When it's also centered it may
349
    // also be start or end justified if it doesn't fit in the overall center.
350

351
    final Key titleKey = new UniqueKey();
352 353 354 355 356 357 358 359 360 361 362 363
    double titleWidth = 700.0;
    Widget leading = new Container();
    List<Widget> actions;

    Widget buildApp() {
      return new MaterialApp(
        home: new Scaffold(
          appBar: new AppBar(
            leading: leading,
            centerTitle: true,
            title: new Container(
              key: titleKey,
364
              constraints: new BoxConstraints.loose(new Size(titleWidth, 1000.0)),
365
            ),
366 367 368
            actions: actions,
          ),
        ),
369 370 371 372
      );
    }

    // Centering a title with width 700 within the 800 pixel wide test widget
373 374
    // would mean that its start edge would have to be 50. The material spec says
    // that the start edge of the title must be atleast 72.
375 376
    await tester.pumpWidget(buildApp());

377
    final Finder title = find.byKey(titleKey);
378
    expect(tester.getTopLeft(title).dx, 72.0);
379 380 381
    expect(tester.getSize(title).width, equals(700.0));

    // Centering a title with width 620 within the 800 pixel wide test widget
382 383 384
    // would mean that its start edge would have to be 90. We reserve 72
    // on the start and the padded actions occupy 96 + 4 on the end. That
    // leaves 628, so the title is end justified but its width isn't changed.
385 386 387 388 389

    await tester.pumpWidget(buildApp());
    leading = null;
    titleWidth = 620.0;
    actions = <Widget>[
390 391
      const SizedBox(width: 48.0),
      const SizedBox(width: 48.0)
392 393
    ];
    await tester.pumpWidget(buildApp());
394
    expect(tester.getTopLeft(title).dx, 800 - 620 - 48 - 48 - 4);
395 396 397
    expect(tester.getSize(title).width, equals(620.0));
  });

398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
  testWidgets('AppBar centerTitle:true title overflow OK (RTL)', (WidgetTester tester) async {
    // The app bar's title should be constrained to fit within the available space
    // between the leading and actions widgets. When it's also centered it may
    // also be start or end justified if it doesn't fit in the overall center.

    final Key titleKey = new UniqueKey();
    double titleWidth = 700.0;
    Widget leading = new Container();
    List<Widget> actions;

    Widget buildApp() {
      return new MaterialApp(
        home: new Directionality(
          textDirection: TextDirection.rtl,
          child: new Scaffold(
            appBar: new AppBar(
              leading: leading,
              centerTitle: true,
              title: new Container(
                key: titleKey,
                constraints: new BoxConstraints.loose(new Size(titleWidth, 1000.0)),
              ),
              actions: actions,
            ),
          ),
        ),
      );
    }

    // Centering a title with width 700 within the 800 pixel wide test widget
    // would mean that its start edge would have to be 50. The material spec says
    // that the start edge of the title must be atleast 72.
    await tester.pumpWidget(buildApp());

    final Finder title = find.byKey(titleKey);
    expect(tester.getTopRight(title).dx, 800.0 - 72.0);
    expect(tester.getSize(title).width, equals(700.0));

    // Centering a title with width 620 within the 800 pixel wide test widget
    // would mean that its start edge would have to be 90. We reserve 72
    // on the start and the padded actions occupy 96 + 4 on the end. That
    // leaves 628, so the title is end justified but its width isn't changed.

    await tester.pumpWidget(buildApp());
    leading = null;
    titleWidth = 620.0;
    actions = <Widget>[
      const SizedBox(width: 48.0),
      const SizedBox(width: 48.0)
    ];
    await tester.pumpWidget(buildApp());
    expect(tester.getTopRight(title).dx, 620 + 48 + 48 + 4);
    expect(tester.getSize(title).width, equals(620.0));
  });

453 454
  testWidgets('AppBar with no Scaffold', (WidgetTester tester) async {
    await tester.pumpWidget(
455 456 457 458 459 460
      new MaterialApp(
        home: new SizedBox(
          height: kToolbarHeight,
          child: new AppBar(
            leading: const Text('L'),
            title: const Text('No Scaffold'),
461
            actions: const <Widget>[const Text('A1'), const Text('A2')],
462
          ),
463 464 465 466 467 468 469 470 471 472
        ),
      ),
    );

    expect(find.text('L'), findsOneWidget);
    expect(find.text('No Scaffold'), findsOneWidget);
    expect(find.text('A1'), findsOneWidget);
    expect(find.text('A2'), findsOneWidget);
  });

473 474
  testWidgets('AppBar render at zero size', (WidgetTester tester) async {
    await tester.pumpWidget(
475 476 477 478 479 480 481 482 483
      new MaterialApp(
        home: new Center(
          child: new Container(
            height: 0.0,
            width: 0.0,
            child: new Scaffold(
              appBar: new AppBar(
                title: const Text('X'),
              ),
484 485 486 487
            ),
          ),
        ),
      ),
488 489
    );

490
    final Finder title = find.text('X');
491 492
    expect(tester.getSize(title).isEmpty, isTrue);
  });
493 494

  testWidgets('AppBar actions are vertically centered', (WidgetTester tester) async {
495 496 497 498 499
    final UniqueKey appBarKey = new UniqueKey();
    final UniqueKey leadingKey = new UniqueKey();
    final UniqueKey titleKey = new UniqueKey();
    final UniqueKey action0Key = new UniqueKey();
    final UniqueKey action1Key = new UniqueKey();
500 501 502 503 504 505 506 507 508 509 510 511 512 513

    await tester.pumpWidget(
      new MaterialApp(
        home: new Scaffold(
          appBar: new AppBar(
            key: appBarKey,
            leading: new SizedBox(key: leadingKey, height: 50.0),
            title: new SizedBox(key: titleKey, height: 40.0),
            actions: <Widget>[
              new SizedBox(key: action0Key, height: 20.0),
              new SizedBox(key: action1Key, height: 30.0),
            ],
          ),
        ),
514
      ),
515 516 517
    );

    // The vertical center of the widget with key, in global coordinates.
518
    double yCenter(Key key) => tester.getCenter(find.byKey(key)).dy;
519 520 521 522 523 524 525

    expect(yCenter(appBarKey), equals(yCenter(leadingKey)));
    expect(yCenter(appBarKey), equals(yCenter(titleKey)));
    expect(yCenter(appBarKey), equals(yCenter(action0Key)));
    expect(yCenter(appBarKey), equals(yCenter(action1Key)));
  });

526 527 528 529 530 531
  testWidgets('leading button extends to edge and is square', (WidgetTester tester) async {
    await tester.pumpWidget(
      new MaterialApp(
        theme: new ThemeData(platform: TargetPlatform.android),
        home: new Scaffold(
          appBar: new AppBar(
532
            title: const Text('X'),
533 534
          ),
          drawer: new Column(), // Doesn't really matter. Triggers a hamburger regardless.
535 536
        ),
      ),
537 538
    );

539
    final Finder hamburger = find.byTooltip('Open navigation menu');
540
    expect(tester.getTopLeft(hamburger), const Offset(0.0, 0.0));
541
    expect(tester.getSize(hamburger), const Size(56.0, 56.0));
542 543 544 545 546 547 548 549
  });

  testWidgets('test action is 4dp from edge and 48dp min', (WidgetTester tester) async {
    await tester.pumpWidget(
      new MaterialApp(
        theme: new ThemeData(platform: TargetPlatform.android),
        home: new Scaffold(
          appBar: new AppBar(
550
            title: const Text('X'),
551
            actions: const <Widget> [
552 553
              const IconButton(
                icon: const Icon(Icons.share),
554 555 556 557
                onPressed: null,
                tooltip: 'Share',
                iconSize: 20.0,
              ),
558 559
              const IconButton(
                icon: const Icon(Icons.add),
560 561 562 563 564 565
                onPressed: null,
                tooltip: 'Add',
                iconSize: 60.0,
              ),
            ],
          ),
566 567
        ),
      ),
568 569
    );

570
    final Finder addButton = find.byTooltip('Add');
571
    // Right padding is 4dp.
572
    expect(tester.getTopRight(addButton), const Offset(800.0 - 4.0, 0.0));
573
    // It's still the size it was plus the 2 * 8dp padding from IconButton.
574
    expect(tester.getSize(addButton), const Size(60.0 + 2 * 8.0, 56.0));
575

576
    final Finder shareButton = find.byTooltip('Share');
577
    // The 20dp icon is expanded to fill the IconButton's touch target to 48dp.
578
    expect(tester.getSize(shareButton), const Size(48.0, 56.0));
579 580
  });

581 582 583 584 585 586 587
  testWidgets('SliverAppBar default configuration', (WidgetTester tester) async {
    await tester.pumpWidget(buildSliverAppBarApp(
      floating: false,
      pinned: false,
      expandedHeight: null,
    ));

588
    final ScrollController controller = primaryScrollController(tester);
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
    expect(controller.offset, 0.0);
    expect(appBarIsVisible(tester), true);

    final double initialAppBarHeight = appBarHeight(tester);
    final double initialTabBarHeight = tabBarHeight(tester);

    // Scroll the not-pinned appbar partially out of view
    controller.jumpTo(50.0);
    await tester.pump();
    expect(appBarIsVisible(tester), true);
    expect(appBarHeight(tester), initialAppBarHeight);
    expect(tabBarHeight(tester), initialTabBarHeight);

    // Scroll the not-pinned appbar out of view
    controller.jumpTo(600.0);
    await tester.pump();
    expect(appBarIsVisible(tester), false);
    expect(appBarHeight(tester), initialAppBarHeight);
    expect(tabBarHeight(tester), initialTabBarHeight);

    // Scroll the not-pinned appbar back into view
    controller.jumpTo(0.0);
    await tester.pump();
    expect(appBarIsVisible(tester), true);
    expect(appBarHeight(tester), initialAppBarHeight);
    expect(tabBarHeight(tester), initialTabBarHeight);
  });

  testWidgets('SliverAppBar expandedHeight, pinned', (WidgetTester tester) async {

    await tester.pumpWidget(buildSliverAppBarApp(
      floating: false,
      pinned: true,
      expandedHeight: 128.0,
    ));

625
    final ScrollController controller = primaryScrollController(tester);
626 627 628 629
    expect(controller.offset, 0.0);
    expect(appBarIsVisible(tester), true);
    expect(appBarHeight(tester), 128.0);

630
    const double initialAppBarHeight = 128.0;
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
    final double initialTabBarHeight = tabBarHeight(tester);

    // Scroll the not-pinned appbar, collapsing the expanded height. At this
    // point both the toolbar and the tabbar are visible.
    controller.jumpTo(600.0);
    await tester.pump();
    expect(appBarIsVisible(tester), true);
    expect(tabBarHeight(tester), initialTabBarHeight);
    expect(appBarHeight(tester), lessThan(initialAppBarHeight));
    expect(appBarHeight(tester), greaterThan(initialTabBarHeight));

    // Scroll the not-pinned appbar back into view
    controller.jumpTo(0.0);
    await tester.pump();
    expect(appBarIsVisible(tester), true);
    expect(appBarHeight(tester), initialAppBarHeight);
    expect(tabBarHeight(tester), initialTabBarHeight);
  });

  testWidgets('SliverAppBar expandedHeight, pinned and floating', (WidgetTester tester) async {

    await tester.pumpWidget(buildSliverAppBarApp(
      floating: true,
      pinned: true,
      expandedHeight: 128.0,
    ));

658
    final ScrollController controller = primaryScrollController(tester);
659 660 661 662
    expect(controller.offset, 0.0);
    expect(appBarIsVisible(tester), true);
    expect(appBarHeight(tester), 128.0);

663
    const double initialAppBarHeight = 128.0;
664 665
    final double initialTabBarHeight = tabBarHeight(tester);

666
    // Scroll the floating-pinned appbar, collapsing the expanded height. At this
667 668 669 670 671 672 673 674
    // point only the tabBar is visible.
    controller.jumpTo(600.0);
    await tester.pump();
    expect(appBarIsVisible(tester), true);
    expect(tabBarHeight(tester), initialTabBarHeight);
    expect(appBarHeight(tester), lessThan(initialAppBarHeight));
    expect(appBarHeight(tester), initialTabBarHeight);

675
    // Scroll the floating-pinned appbar back into view
676 677 678 679 680 681
    controller.jumpTo(0.0);
    await tester.pump();
    expect(appBarIsVisible(tester), true);
    expect(appBarHeight(tester), initialAppBarHeight);
    expect(tabBarHeight(tester), initialTabBarHeight);
  });
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702

  testWidgets('SliverAppBar expandedHeight, floating with snap:true', (WidgetTester tester) async {
    await tester.pumpWidget(buildSliverAppBarApp(
      floating: true,
      pinned: false,
      snap: true,
      expandedHeight: 128.0,
    ));
    expect(appBarIsVisible(tester), true);
    expect(appBarTop(tester), 0.0);
    expect(appBarHeight(tester), 128.0);
    expect(appBarBottom(tester), 128.0);

    // Scroll to the middle of the list. The (floating) appbar is no longer visible.
    final ScrollPosition position = tester.state<ScrollableState>(find.byType(Scrollable)).position;
    position.jumpTo(256.00);
    await tester.pumpAndSettle();
    expect(appBarIsVisible(tester), false);
    expect(appBarTop(tester), lessThanOrEqualTo(-128.0));

    // Drag the scrollable up and down. The app bar should not snap open, its
703
    // height should just track the drag offset.
Hans Muller's avatar
Hans Muller committed
704
    TestGesture gesture = await tester.startGesture(const Offset(50.0, 256.0));
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
    await gesture.moveBy(const Offset(0.0, 128.0)); // drag the appbar all the way open
    await tester.pump();
    expect(appBarTop(tester), 0.0);
    expect(appBarHeight(tester), 128.0);

    await gesture.moveBy(const Offset(0.0, -50.0));
    await tester.pump();
    expect(appBarBottom(tester), 78.0); // 78 == 128 - 50

    // Trigger the snap open animation: drag down and release
    await gesture.moveBy(const Offset(0.0, 10.0));
    await gesture.up();

    // Now verify that the appbar is animating open
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 50));
    double bottom = appBarBottom(tester);
    expect(bottom, greaterThan(88.0)); // 88 = 78 + 10

    await tester.pump();
    await tester.pump(const Duration(milliseconds: 50));
    expect(appBarBottom(tester), greaterThan(bottom));

    // The animation finishes when the appbar is full height.
    await tester.pumpAndSettle();
    expect(appBarHeight(tester), 128.0);

    // Now that the app bar is open, perform the same drag scenario
    // in reverse: drag the appbar up and down and then trigger the
    // snap closed animation.
Hans Muller's avatar
Hans Muller committed
735
    gesture = await tester.startGesture(const Offset(50.0, 256.0));
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
    await gesture.moveBy(const Offset(0.0, -128.0)); // drag the appbar closed
    await tester.pump();
    expect(appBarBottom(tester), 0.0);

    await gesture.moveBy(const Offset(0.0, 100.0));
    await tester.pump();
    expect(appBarBottom(tester), 100.0);

    // Trigger the snap close animation: drag upwards and release
    await gesture.moveBy(const Offset(0.0, -10.0));
    await gesture.up();

    // Now verify that the appbar is animating closed
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 50));
    bottom = appBarBottom(tester);
    expect(bottom, lessThan(90.0));

    await tester.pump();
    await tester.pump(const Duration(milliseconds: 50));
    expect(appBarBottom(tester), lessThan(bottom));

    // The animation finishes when the appbar is off screen.
    await tester.pumpAndSettle();
    expect(appBarTop(tester), lessThanOrEqualTo(0.0));
    expect(appBarBottom(tester), lessThanOrEqualTo(0.0));
  });

  testWidgets('SliverAppBar expandedHeight, floating and pinned with snap:true', (WidgetTester tester) async {
    await tester.pumpWidget(buildSliverAppBarApp(
      floating: true,
      pinned: true,
      snap: true,
      expandedHeight: 128.0,
    ));
    expect(appBarIsVisible(tester), true);
    expect(appBarTop(tester), 0.0);
    expect(appBarHeight(tester), 128.0);
    expect(appBarBottom(tester), 128.0);

    // Scroll to the middle of the list. The only the tab bar is visible
    // because this is a pinned appbar.
    final ScrollPosition position = tester.state<ScrollableState>(find.byType(Scrollable)).position;
    position.jumpTo(256.0);
    await tester.pumpAndSettle();
    expect(appBarIsVisible(tester), true);
    expect(appBarTop(tester), 0.0);
    expect(appBarHeight(tester), kTextTabBarHeight);

    // Drag the scrollable up and down. The app bar should not snap open, the
    // bottof of the appbar should just track the drag offset.
Hans Muller's avatar
Hans Muller committed
787
    TestGesture gesture = await tester.startGesture(const Offset(50.0, 200.0));
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
    await gesture.moveBy(const Offset(0.0, 100.0));
    await tester.pump();
    expect(appBarHeight(tester), 100.0);

    await gesture.moveBy(const Offset(0.0, -25.0));
    await tester.pump();
    expect(appBarHeight(tester), 75.0);

    // Trigger the snap animation: drag down and release
    await gesture.moveBy(const Offset(0.0, 10.0));
    await gesture.up();

    // Now verify that the appbar is animating open
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 50));
    final double height = appBarHeight(tester);
    expect(height, greaterThan(85.0));
    expect(height, lessThan(128.0));

    await tester.pump();
    await tester.pump(const Duration(milliseconds: 50));
    expect(appBarHeight(tester), greaterThan(height));
    expect(appBarHeight(tester), lessThan(128.0));

    // The animation finishes when the appbar is fully expanded
    await tester.pumpAndSettle();
    expect(appBarTop(tester), 0.0);
    expect(appBarHeight(tester), 128.0);
    expect(appBarBottom(tester), 128.0);

    // Now that the appbar is fully expanded, Perform the same drag
    // scenario in reverse: drag the appbar up and down and then trigger
    // the snap closed animation.
Hans Muller's avatar
Hans Muller committed
821
    gesture = await tester.startGesture(const Offset(50.0, 256.0));
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
    await gesture.moveBy(const Offset(0.0, -128.0));
    await tester.pump();
    expect(appBarBottom(tester), kTextTabBarHeight);

    await gesture.moveBy(const Offset(0.0, 100.0));
    await tester.pump();
    expect(appBarBottom(tester), 100.0);

    // Trigger the snap close animation: drag upwards and release
    await gesture.moveBy(const Offset(0.0, -10.0));
    await gesture.up();

    // Now verify that the appbar is animating closed
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 50));
    final double bottom = appBarBottom(tester);
    expect(bottom, lessThan(90.0));

    await tester.pump();
    await tester.pump(const Duration(milliseconds: 50));
    expect(appBarBottom(tester), lessThan(bottom));

    // The animation finishes when the appbar shrinks back to its pinned height
    await tester.pumpAndSettle();
    expect(appBarTop(tester), lessThanOrEqualTo(0.0));
    expect(appBarBottom(tester), kTextTabBarHeight);
  });
849 850 851 852 853

  testWidgets('AppBar dimensions, with and without bottom, primary', (WidgetTester tester) async {
    const MediaQueryData topPadding100 = const MediaQueryData(padding: const EdgeInsets.only(top: 100.0));

    await tester.pumpWidget(
854 855 856 857 858 859 860 861
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new MediaQuery(
          data: topPadding100,
          child: new Scaffold(
            primary: false,
            appBar: new AppBar(),
          ),
862 863 864 865 866 867 868
        ),
      ),
    );
    expect(appBarTop(tester), 0.0);
    expect(appBarHeight(tester), kToolbarHeight);

    await tester.pumpWidget(
869 870 871 872 873 874 875 876
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new MediaQuery(
          data: topPadding100,
          child: new Scaffold(
            primary: true,
            appBar: new AppBar(title: const Text('title'))
          ),
877 878 879 880 881 882 883 884
        ),
      ),
    );
    expect(appBarTop(tester), 0.0);
    expect(tester.getTopLeft(find.text('title')).dy, greaterThan(100.0));
    expect(appBarHeight(tester), kToolbarHeight + 100.0);

    await tester.pumpWidget(
885 886 887 888 889 890 891 892 893 894 895
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new MediaQuery(
          data: topPadding100,
          child: new Scaffold(
            primary: false,
            appBar: new AppBar(
              bottom: new PreferredSize(
                preferredSize: const Size.fromHeight(200.0),
                child: new Container(),
              ),
896 897 898 899 900 901 902 903 904
            ),
          ),
        ),
      ),
    );
    expect(appBarTop(tester), 0.0);
    expect(appBarHeight(tester), kToolbarHeight + 200.0);

    await tester.pumpWidget(
905 906 907 908 909 910 911 912 913 914 915
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new MediaQuery(
          data: topPadding100,
          child: new Scaffold(
            primary: true,
            appBar: new AppBar(
              bottom: new PreferredSize(
                preferredSize: const Size.fromHeight(200.0),
                child: new Container(),
              ),
916 917 918 919 920 921 922 923 924
            ),
          ),
        ),
      ),
    );
    expect(appBarTop(tester), 0.0);
    expect(appBarHeight(tester), kToolbarHeight + 100.0 + 200.0);

    await tester.pumpWidget(
925 926 927 928 929 930 931 932
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new MediaQuery(
          data: topPadding100,
          child: new AppBar(
            primary: false,
            title: const Text('title'),
          ),
933 934 935 936 937 938
        ),
      ),
    );
    expect(appBarTop(tester), 0.0);
    expect(tester.getTopLeft(find.text('title')).dy, lessThan(100.0));
  });
939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958

  testWidgets('AppBar updates when you add a drawer', (WidgetTester tester) async {
    await tester.pumpWidget(
      new MaterialApp(
        home: new Scaffold(
          appBar: new AppBar(),
        ),
      ),
    );
    expect(find.byIcon(Icons.menu), findsNothing);
    await tester.pumpWidget(
      new MaterialApp(
        home: new Scaffold(
          drawer: const Drawer(),
          appBar: new AppBar(),
        ),
      ),
    );
    expect(find.byIcon(Icons.menu), findsOneWidget);
  });
959

960 961 962 963 964 965 966 967 968 969 970 971
  testWidgets('AppBar does not draw menu for drawer if automaticallyImplyLeading is false', (WidgetTester tester) async {
    await tester.pumpWidget(
      new MaterialApp(
        home: new Scaffold(
          drawer: const Drawer(),
          appBar: new AppBar(automaticallyImplyLeading: false),
        ),
      ),
    );
    expect(find.byIcon(Icons.menu), findsNothing);
  });

972 973 974 975 976 977 978 979
  testWidgets('AppBar handles loose children 0', (WidgetTester tester) async {
    final GlobalKey key = new GlobalKey();
    await tester.pumpWidget(
      new MaterialApp(
        home: new Center(
          child: new AppBar(
            leading: new Placeholder(key: key),
            title: const Text('Abc'),
980
            actions: const <Widget>[
981 982 983
              const Placeholder(fallbackWidth: 10.0),
              const Placeholder(fallbackWidth: 10.0),
              const Placeholder(fallbackWidth: 10.0),
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
            ],
          ),
        ),
      ),
    );
    expect(tester.renderObject<RenderBox>(find.byKey(key)).localToGlobal(Offset.zero), const Offset(0.0, 0.0));
    expect(tester.renderObject<RenderBox>(find.byKey(key)).size, const Size(56.0, 56.0));
  });

  testWidgets('AppBar handles loose children 1', (WidgetTester tester) async {
    final GlobalKey key = new GlobalKey();
    await tester.pumpWidget(
      new MaterialApp(
        home: new Center(
          child: new AppBar(
            leading: new Placeholder(key: key),
            title: const Text('Abc'),
1001
            actions: const <Widget>[
1002 1003 1004
              const Placeholder(fallbackWidth: 10.0),
              const Placeholder(fallbackWidth: 10.0),
              const Placeholder(fallbackWidth: 10.0),
1005 1006 1007 1008
            ],
            flexibleSpace: new DecoratedBox(
              decoration: new BoxDecoration(
                gradient: new LinearGradient(
1009 1010
                  begin: const Alignment(0.0, -1.0),
                  end: const Alignment(-0.04, 1.0),
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
                  colors: <Color>[Colors.blue.shade500, Colors.blue.shade800],
                ),
              ),
            ),
          ),
        ),
      ),
    );
    expect(tester.renderObject<RenderBox>(find.byKey(key)).localToGlobal(Offset.zero), const Offset(0.0, 0.0));
    expect(tester.renderObject<RenderBox>(find.byKey(key)).size, const Size(56.0, 56.0));
  });

  testWidgets('AppBar handles loose children 2', (WidgetTester tester) async {
    final GlobalKey key = new GlobalKey();
    await tester.pumpWidget(
      new MaterialApp(
        home: new Center(
          child: new AppBar(
            leading: new Placeholder(key: key),
            title: const Text('Abc'),
1031
            actions: const <Widget>[
1032 1033 1034
              const Placeholder(fallbackWidth: 10.0),
              const Placeholder(fallbackWidth: 10.0),
              const Placeholder(fallbackWidth: 10.0),
1035 1036 1037 1038
            ],
            flexibleSpace: new DecoratedBox(
              decoration: new BoxDecoration(
                gradient: new LinearGradient(
1039 1040
                  begin: const Alignment(0.0, -1.0),
                  end: const Alignment(-0.04, 1.0),
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
                  colors: <Color>[Colors.blue.shade500, Colors.blue.shade800],
                ),
              ),
            ),
            bottom: new PreferredSize(
              preferredSize: const Size(0.0, kToolbarHeight),
              child: new Container(
                height: 50.0,
                padding: const EdgeInsets.all(4.0),
                child: const Placeholder(
                  strokeWidth: 2.0,
                  color: const Color(0xFFFFFFFF),
                ),
              ),
            ),
          ),
        ),
      ),
    );
    expect(tester.renderObject<RenderBox>(find.byKey(key)).localToGlobal(Offset.zero), const Offset(0.0, 0.0));
    expect(tester.renderObject<RenderBox>(find.byKey(key)).size, const Size(56.0, 56.0));
  });

  testWidgets('AppBar handles loose children 3', (WidgetTester tester) async {
    final GlobalKey key = new GlobalKey();
    await tester.pumpWidget(
      new MaterialApp(
        home: new Center(
          child: new AppBar(
            leading: new Placeholder(key: key),
            title: const Text('Abc'),
1072
            actions: const <Widget>[
1073 1074 1075
              const Placeholder(fallbackWidth: 10.0),
              const Placeholder(fallbackWidth: 10.0),
              const Placeholder(fallbackWidth: 10.0),
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
            ],
            bottom: new PreferredSize(
              preferredSize: const Size(0.0, kToolbarHeight),
              child: new Container(
                height: 50.0,
                padding: const EdgeInsets.all(4.0),
                child: const Placeholder(
                  strokeWidth: 2.0,
                  color: const Color(0xFFFFFFFF),
                ),
              ),
            ),
          ),
        ),
      ),
    );
    expect(tester.renderObject<RenderBox>(find.byKey(key)).localToGlobal(Offset.zero), const Offset(0.0, 0.0));
    expect(tester.renderObject<RenderBox>(find.byKey(key)).size, const Size(56.0, 56.0));
  });
Ian Hickson's avatar
Ian Hickson committed
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 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

  testWidgets('AppBar positioning of leading and trailing widgets with top padding', (WidgetTester tester) async {
    const MediaQueryData topPadding100 = const MediaQueryData(padding: const EdgeInsets.only(top: 100.0));

    final Key leadingKey = new UniqueKey();
    final Key titleKey = new UniqueKey();
    final Key trailingKey = new UniqueKey();

    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.rtl,
        child: new MediaQuery(
          data: topPadding100,
          child: new Scaffold(
            primary: false,
            appBar: new AppBar(
              leading: new Placeholder(key: leadingKey),
              title: new Placeholder(key: titleKey),
              actions: <Widget>[ new Placeholder(key: trailingKey) ],
            ),
          ),
        ),
      ),
    );
    expect(tester.getTopLeft(find.byType(AppBar)), const Offset(0.0, 0.0));
    expect(tester.getTopLeft(find.byKey(leadingKey)), const Offset(800.0 - 56.0, 100.0));
    expect(tester.getTopLeft(find.byKey(titleKey)), const Offset(420.0, 100.0));
    expect(tester.getTopLeft(find.byKey(trailingKey)), const Offset(4.0, 100.0));
  });

  testWidgets('SliverAppBar positioning of leading and trailing widgets with top padding', (WidgetTester tester) async {
    const MediaQueryData topPadding100 = const MediaQueryData(padding: const EdgeInsets.only(top: 100.0));

    final Key leadingKey = new UniqueKey();
    final Key titleKey = new UniqueKey();
    final Key trailingKey = new UniqueKey();

    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.rtl,
        child: new MediaQuery(
          data: topPadding100,
          child: new CustomScrollView(
            primary: true,
            slivers: <Widget>[
              new SliverAppBar(
                leading: new Placeholder(key: leadingKey),
                title: new Placeholder(key: titleKey),
                actions: <Widget>[ new Placeholder(key: trailingKey) ],
              ),
            ],
          ),
        ),
      ),
    );
    expect(tester.getTopLeft(find.byType(AppBar)), const Offset(0.0, 0.0));
    expect(tester.getTopLeft(find.byKey(leadingKey)), const Offset(800.0 - 56.0, 100.0));
    expect(tester.getTopLeft(find.byKey(titleKey)), const Offset(420.0, 100.0));
    expect(tester.getTopLeft(find.byKey(trailingKey)), const Offset(4.0, 100.0));
  });
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184

  testWidgets('SliverAppBar positioning of leading and trailing widgets with bottom padding', (WidgetTester tester) async {
    const MediaQueryData topPadding100 = const MediaQueryData(padding: const EdgeInsets.only(top: 100.0, bottom: 50.0));

    final Key leadingKey = new UniqueKey();
    final Key titleKey = new UniqueKey();
    final Key trailingKey = new UniqueKey();

    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.rtl,
        child: new MediaQuery(
          data: topPadding100,
          child: new CustomScrollView(
            primary: true,
            slivers: <Widget>[
              new SliverAppBar(
                leading: new Placeholder(key: leadingKey),
                title: new Placeholder(key: titleKey),
                actions: <Widget>[ new Placeholder(key: trailingKey) ],
              ),
            ],
          ),
        ),
      ),
    );
    expect(tester.getRect(find.byType(AppBar)), new Rect.fromLTRB(0.0, 0.0, 800.00, 100.0 + 56.0));
    expect(tester.getRect(find.byKey(leadingKey)), new Rect.fromLTRB(800.0 - 56.0, 100.0, 800.0, 100.0 + 56.0));
    expect(tester.getRect(find.byKey(trailingKey)), new Rect.fromLTRB(4.0, 100.0, 400.0 + 4.0, 100.0 + 56.0));
  });
1185
}