nav_bar_test.dart 36.3 KB
Newer Older
1 2 3 4 5 6
// Copyright 2017 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.

import 'package:flutter/cupertino.dart';
import 'package:flutter/rendering.dart';
7
import 'package:flutter/services.dart';
8
import 'package:flutter_test/flutter_test.dart';
9

10
import '../rendering/mock_canvas.dart';
11 12
import '../widgets/semantics_tester.dart';

13 14
int count = 0;

15 16 17
void main() {
  testWidgets('Middle still in center with asymmetrical actions', (WidgetTester tester) async {
    await tester.pumpWidget(
18 19
      const CupertinoApp(
        home: CupertinoNavigationBar(
20 21
          leading: CupertinoButton(child: Text('Something'), onPressed: null,),
          middle: Text('Title'),
xster's avatar
xster committed
22
        ),
23 24 25 26 27 28 29
      ),
    );

    // Expect the middle of the title to be exactly in the middle of the screen.
    expect(tester.getCenter(find.text('Title')).dx, 400.0);
  });

30 31
  testWidgets('Middle still in center with back button', (WidgetTester tester) async {
    await tester.pumpWidget(
32 33
      const CupertinoApp(
        home: CupertinoNavigationBar(
34
          middle: Text('Title'),
35 36 37 38
        ),
      ),
    );

39
    tester.state<NavigatorState>(find.byType(Navigator)).push(CupertinoPageRoute<void>(
40 41
      builder: (BuildContext context) {
        return const CupertinoNavigationBar(
42
          middle: Text('Page 2'),
43 44 45 46 47 48 49 50 51 52 53
        );
      },
    ));

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

    // Expect the middle of the title to be exactly in the middle of the screen.
    expect(tester.getCenter(find.text('Page 2')).dx, 400.0);
  });

54 55 56 57 58 59
  testWidgets('Opaque background does not add blur effects, non-opaque background adds blur effects', (WidgetTester tester) async {
    const CupertinoDynamicColor background = CupertinoDynamicColor.withBrightness(
      color: Color(0xFFE5E5E5),
      darkColor: Color(0xF3E5E5E5),
    );

60
    await tester.pumpWidget(
61
      const CupertinoApp(
62
        theme: CupertinoThemeData(brightness: Brightness.light),
63
        home: CupertinoNavigationBar(
64
          middle: Text('Title'),
65
          backgroundColor: background,
xster's avatar
xster committed
66
        ),
67 68 69
      ),
    );
    expect(find.byType(BackdropFilter), findsNothing);
70 71 72 73 74 75 76 77 78 79 80 81 82
    expect(find.byType(CupertinoNavigationBar), paints..rect(color: background.color));

    await tester.pumpWidget(
      const CupertinoApp(
        theme: CupertinoThemeData(brightness: Brightness.dark),
        home: CupertinoNavigationBar(
          middle: Text('Title'),
          backgroundColor: background,
        ),
      ),
    );
    expect(find.byType(BackdropFilter), findsOneWidget);
    expect(find.byType(CupertinoNavigationBar), paints..rect(color: background.darkColor));
83 84 85 86
  });

  testWidgets('Non-opaque background adds blur effects', (WidgetTester tester) async {
    await tester.pumpWidget(
87 88
      const CupertinoApp(
        home: CupertinoNavigationBar(
89
          middle: Text('Title'),
xster's avatar
xster committed
90
        ),
91 92 93 94
      ),
    );
    expect(find.byType(BackdropFilter), findsOneWidget);
  });
95

96
  testWidgets('Can specify custom padding', (WidgetTester tester) async {
97
    final Key middleBox = GlobalKey();
98
    await tester.pumpWidget(
99 100
      CupertinoApp(
        home: Align(
101
          alignment: Alignment.topCenter,
102
          child: CupertinoNavigationBar(
103
            leading: const CupertinoButton(child: Text('Cheetah'), onPressed: null),
104 105
            // Let the box take all the vertical space to test vertical padding but let
            // the nav bar position it horizontally.
106
            middle: Align(
107 108 109
              key: middleBox,
              alignment: Alignment.center,
              widthFactor: 1.0,
110
              child: const Text('Title'),
111
            ),
112
            trailing: const CupertinoButton(child: Text('Puma'), onPressed: null),
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
            padding: const EdgeInsetsDirectional.only(
              start: 10.0,
              end: 20.0,
              top: 3.0,
              bottom: 4.0,
            ),
          ),
        ),
      ),
    );

    expect(tester.getRect(find.byKey(middleBox)).top, 3.0);
    // 44 is the standard height of the nav bar.
    expect(
      tester.getRect(find.byKey(middleBox)).bottom,
      // 44 is the standard height of the nav bar.
      44.0 - 4.0,
    );

    expect(tester.getTopLeft(find.widgetWithText(CupertinoButton, 'Cheetah')).dx, 10.0);
    expect(tester.getTopRight(find.widgetWithText(CupertinoButton, 'Puma')).dx, 800.0 - 20.0);

    // Title is still exactly centered.
    expect(tester.getCenter(find.text('Title')).dx, 400.0);
  });

  testWidgets('Padding works in RTL', (WidgetTester tester) async {
    await tester.pumpWidget(
141 142
      const CupertinoApp(
        home: Directionality(
143
          textDirection: TextDirection.rtl,
144
          child: Align(
145
            alignment: Alignment.topCenter,
146 147
            child: CupertinoNavigationBar(
              leading: CupertinoButton(child: Text('Cheetah'), onPressed: null),
148 149
              // Let the box take all the vertical space to test vertical padding but let
              // the nav bar position it horizontally.
150 151 152
              middle: Text('Title'),
              trailing: CupertinoButton(child: Text('Puma'), onPressed: null),
              padding: EdgeInsetsDirectional.only(
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
                start: 10.0,
                end: 20.0,
              ),
            ),
          ),
        ),
      ),
    );

    expect(tester.getTopRight(find.widgetWithText(CupertinoButton, 'Cheetah')).dx, 800.0 - 10.0);
    expect(tester.getTopLeft(find.widgetWithText(CupertinoButton, 'Puma')).dx, 20.0);

    // Title is still exactly centered.
    expect(tester.getCenter(find.text('Title')).dx, 400.0);
  });

xster's avatar
xster committed
169
  testWidgets('Nav bar uses theme defaults', (WidgetTester tester) async {
170 171
    count = 0x000000;
    await tester.pumpWidget(
xster's avatar
xster committed
172 173 174
      CupertinoApp(
        home: CupertinoNavigationBar(
          leading: CupertinoButton(
175
            onPressed: () { },
xster's avatar
xster committed
176 177 178 179
            child: const _ExpectStyles(color: CupertinoColors.activeBlue, index: 0x000001),
          ),
          middle: const _ExpectStyles(color: CupertinoColors.black, index: 0x000100),
          trailing: CupertinoButton(
180
            onPressed: () { },
xster's avatar
xster committed
181 182 183 184 185 186 187 188 189 190
            child: const _ExpectStyles(color: CupertinoColors.activeBlue, index: 0x010000),
          ),
        ),
      ),
    );
    expect(count, 0x010101);
  });

  testWidgets('Nav bar respects themes', (WidgetTester tester) async {
    count = 0x000000;
191
    const CupertinoDynamicColor orange = CupertinoColors.activeOrange;
xster's avatar
xster committed
192 193 194
    await tester.pumpWidget(
      CupertinoApp(
        theme: const CupertinoThemeData(brightness: Brightness.dark),
195
        home: CupertinoNavigationBar(
xster's avatar
xster committed
196
          leading: CupertinoButton(
197
            onPressed: () { },
198
            child: _ExpectStyles(color: orange.darkColor, index: 0x000001),
xster's avatar
xster committed
199 200 201
          ),
          middle: const _ExpectStyles(color: CupertinoColors.white, index: 0x000100),
          trailing: CupertinoButton(
202
            onPressed: () { },
203
            child: _ExpectStyles(color: orange.darkColor, index: 0x010000),
xster's avatar
xster committed
204 205 206 207 208 209 210 211 212 213 214 215 216
          ),
        ),
      ),
    );
    expect(count, 0x010101);
  });

  testWidgets('Theme active color can be overriden', (WidgetTester tester) async {
    count = 0x000000;
    await tester.pumpWidget(
      CupertinoApp(
        home: CupertinoNavigationBar(
          leading: CupertinoButton(
217
            onPressed: () { },
xster's avatar
xster committed
218 219 220 221
            child: const _ExpectStyles(color: Color(0xFF001122), index: 0x000001),
          ),
          middle: const _ExpectStyles(color: Color(0xFF000000), index: 0x000100),
          trailing: CupertinoButton(
222
            onPressed: () { },
xster's avatar
xster committed
223 224 225
            child: const _ExpectStyles(color: Color(0xFF001122), index: 0x010000),
          ),
          actionsForegroundColor: const Color(0xFF001122),
xster's avatar
xster committed
226
        ),
227 228 229 230
      ),
    );
    expect(count, 0x010101);
  });
231 232 233

  testWidgets('No slivers with no large titles', (WidgetTester tester) async {
    await tester.pumpWidget(
234 235
      const CupertinoApp(
        home: CupertinoPageScaffold(
236 237
          navigationBar: CupertinoNavigationBar(
            middle: Text('Title'),
xster's avatar
xster committed
238
          ),
239
          child: Center(),
xster's avatar
xster committed
240
        ),
241 242 243 244 245 246
      ),
    );

    expect(find.byType(SliverPersistentHeader), findsNothing);
  });

247
  testWidgets('Media padding is applied to CupertinoSliverNavigationBar', (WidgetTester tester) async {
248 249 250 251 252
    final ScrollController scrollController = ScrollController();
    final Key leadingKey = GlobalKey();
    final Key middleKey = GlobalKey();
    final Key trailingKey = GlobalKey();
    final Key titleKey = GlobalKey();
253
    await tester.pumpWidget(
254 255
      CupertinoApp(
        home: MediaQuery(
xster's avatar
xster committed
256
          data: const MediaQueryData(
257
            padding: EdgeInsets.only(
xster's avatar
xster committed
258 259 260 261 262 263
              top: 10.0,
              left: 20.0,
              bottom: 30.0,
              right: 40.0,
            ),
          ),
264 265
          child: CupertinoPageScaffold(
            child: CustomScrollView(
xster's avatar
xster committed
266 267
              controller: scrollController,
              slivers: <Widget>[
268 269 270 271 272
                CupertinoSliverNavigationBar(
                  leading: Placeholder(key: leadingKey),
                  middle: Placeholder(key: middleKey),
                  largeTitle: Text('Large Title', key: titleKey),
                  trailing: Placeholder(key: trailingKey),
273
                ),
274 275
                SliverToBoxAdapter(
                  child: Container(
xster's avatar
xster committed
276
                    height: 1200.0,
277 278
                  ),
                ),
xster's avatar
xster committed
279 280 281 282
              ],
            ),
          ),
        ),
283 284 285
      ),
    );

286 287 288 289
    // Media padding applied to leading (T,L), middle (T), trailing (T, R).
    expect(tester.getTopLeft(find.byKey(leadingKey)), const Offset(16.0 + 20.0, 10.0));
    expect(tester.getRect(find.byKey(middleKey)).top, 10.0);
    expect(tester.getTopRight(find.byKey(trailingKey)), const Offset(800.0 - 16.0 - 40.0, 10.0));
290 291

    // Top and left padding is applied to large title.
292
    expect(tester.getTopLeft(find.byKey(titleKey)), const Offset(16.0 + 20.0, 54.0 + 10.0));
293 294
  });

295
  testWidgets('Large title nav bar scrolls', (WidgetTester tester) async {
296
    final ScrollController scrollController = ScrollController();
297
    await tester.pumpWidget(
298 299 300
      CupertinoApp(
        home: CupertinoPageScaffold(
          child: CustomScrollView(
xster's avatar
xster committed
301 302 303
            controller: scrollController,
            slivers: <Widget>[
              const CupertinoSliverNavigationBar(
304
                largeTitle: Text('Title'),
xster's avatar
xster committed
305
              ),
306 307
              SliverToBoxAdapter(
                child: Container(
xster's avatar
xster committed
308
                  height: 1200.0,
309
                ),
xster's avatar
xster committed
310 311 312 313
              ),
            ],
          ),
        ),
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
      ),
    );

    expect(scrollController.offset, 0.0);
    expect(tester.getTopLeft(find.byType(NavigationToolbar)).dy, 0.0);
    expect(tester.getSize(find.byType(NavigationToolbar)).height, 44.0);

    expect(find.text('Title'), findsNWidgets(2)); // Though only one is visible.

    List<Element> titles = tester.elementList(find.text('Title'))
        .toList()
        ..sort((Element a, Element b) {
          final RenderParagraph aParagraph = a.renderObject;
          final RenderParagraph bParagraph = b.renderObject;
          return aParagraph.text.style.fontSize.compareTo(bParagraph.text.style.fontSize);
        });

331
    Iterable<double> opacities = titles.map<double>((Element element) {
332 333
      final RenderAnimatedOpacity renderOpacity = element.ancestorRenderObjectOfType(const TypeMatcher<RenderAnimatedOpacity>());
      return renderOpacity.opacity.value;
334 335 336
    });

    expect(opacities, <double> [
337 338
      0.0, // Initially the smaller font title is invisible.
      1.0, // The larger font title is visible.
339 340 341
    ]);

    expect(tester.getTopLeft(find.widgetWithText(OverflowBox, 'Title')).dy, 44.0);
342
    expect(tester.getSize(find.widgetWithText(OverflowBox, 'Title')).height, 52.0);
343 344 345 346 347 348 349 350 351 352 353 354 355

    scrollController.jumpTo(600.0);
    await tester.pump(); // Once to trigger the opacity animation.
    await tester.pump(const Duration(milliseconds: 300));

    titles = tester.elementList(find.text('Title'))
        .toList()
        ..sort((Element a, Element b) {
          final RenderParagraph aParagraph = a.renderObject;
          final RenderParagraph bParagraph = b.renderObject;
          return aParagraph.text.style.fontSize.compareTo(bParagraph.text.style.fontSize);
        });

356
    opacities = titles.map<double>((Element element) {
357 358
      final RenderAnimatedOpacity renderOpacity = element.ancestorRenderObjectOfType(const TypeMatcher<RenderAnimatedOpacity>());
      return renderOpacity.opacity.value;
359 360 361
    });

    expect(opacities, <double> [
362 363
      1.0, // Smaller font title now visible
      0.0, // Larger font title invisible.
364 365 366 367 368 369 370 371 372 373
    ]);

    // The persistent toolbar doesn't move or change size.
    expect(tester.getTopLeft(find.byType(NavigationToolbar)).dy, 0.0);
    expect(tester.getSize(find.byType(NavigationToolbar)).height, 44.0);

    expect(tester.getTopLeft(find.widgetWithText(OverflowBox, 'Title')).dy, 44.0);
    // The OverflowBox is squished with the text in it.
    expect(tester.getSize(find.widgetWithText(OverflowBox, 'Title')).height, 0.0);
  });
374

375
  testWidgets('User specified middle is always visible in sliver', (WidgetTester tester) async {
376 377
    final ScrollController scrollController = ScrollController();
    final Key segmentedControlsKey = UniqueKey();
378
    await tester.pumpWidget(
379 380 381
      CupertinoApp(
        home: CupertinoPageScaffold(
          child: CustomScrollView(
382 383
            controller: scrollController,
            slivers: <Widget>[
384 385
              CupertinoSliverNavigationBar(
                middle: ConstrainedBox(
386
                  constraints: const BoxConstraints(maxWidth: 200.0),
387
                  child: CupertinoSegmentedControl<int>(
388 389 390 391 392 393 394 395 396 397 398
                    key: segmentedControlsKey,
                    children: const <int, Widget>{
                      0: Text('Option A'),
                      1: Text('Option B'),
                    },
                    onValueChanged: (int selected) { },
                    groupValue: 0,
                  ),
                ),
                largeTitle: const Text('Title'),
              ),
399 400
              SliverToBoxAdapter(
                child: Container(
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
                  height: 1200.0,
                ),
              ),
            ],
          ),
        ),
      ),
    );

    expect(scrollController.offset, 0.0);
    expect(tester.getTopLeft(find.byType(NavigationToolbar)).dy, 0.0);
    expect(tester.getSize(find.byType(NavigationToolbar)).height, 44.0);

    expect(find.text('Title'), findsOneWidget);
    expect(tester.getCenter(find.byKey(segmentedControlsKey)).dx, 400.0);

    expect(tester.getTopLeft(find.widgetWithText(OverflowBox, 'Title')).dy, 44.0);
    expect(tester.getSize(find.widgetWithText(OverflowBox, 'Title')).height, 52.0);

    scrollController.jumpTo(600.0);
    await tester.pump(); // Once to trigger the opacity animation.
    await tester.pump(const Duration(milliseconds: 300));

    expect(tester.getCenter(find.byKey(segmentedControlsKey)).dx, 400.0);
    // The large title is invisible now.
    expect(
      tester.renderObject<RenderAnimatedOpacity>(
        find.widgetWithText(AnimatedOpacity, 'Title')
      ).opacity.value,
      0.0,
    );
  });

Josh Soref's avatar
Josh Soref committed
434
  testWidgets('Small title can be overridden', (WidgetTester tester) async {
435
    final ScrollController scrollController = ScrollController();
436
    await tester.pumpWidget(
437 438 439
      CupertinoApp(
        home: CupertinoPageScaffold(
          child: CustomScrollView(
xster's avatar
xster committed
440 441 442
            controller: scrollController,
            slivers: <Widget>[
              const CupertinoSliverNavigationBar(
443 444
                middle: Text('Different title'),
                largeTitle: Text('Title'),
xster's avatar
xster committed
445
              ),
446 447
              SliverToBoxAdapter(
                child: Container(
xster's avatar
xster committed
448
                  height: 1200.0,
449
                ),
xster's avatar
xster committed
450 451 452 453
              ),
            ],
          ),
        ),
454 455 456 457 458 459 460 461 462 463
      ),
    );

    expect(scrollController.offset, 0.0);
    expect(tester.getTopLeft(find.byType(NavigationToolbar)).dy, 0.0);
    expect(tester.getSize(find.byType(NavigationToolbar)).height, 44.0);

    expect(find.text('Title'), findsOneWidget);
    expect(find.text('Different title'), findsOneWidget);

464 465
    RenderAnimatedOpacity largeTitleOpacity =
        tester.element(find.text('Title')).ancestorRenderObjectOfType(const TypeMatcher<RenderAnimatedOpacity>());
466 467
    // Large title initially visible.
    expect(
468
      largeTitleOpacity.opacity.value,
469
      1.0,
470 471 472 473 474 475 476
    );
    // Middle widget not even wrapped with RenderOpacity, i.e. is always visible.
    expect(
      tester.element(find.text('Different title')).ancestorRenderObjectOfType(const TypeMatcher<RenderOpacity>()),
      isNull,
    );

477
    expect(tester.getBottomLeft(find.text('Title')).dy, 44.0 + 52.0 - 8.0); // Static part + extension - padding.
478 479 480 481 482 483

    scrollController.jumpTo(600.0);
    await tester.pump(); // Once to trigger the opacity animation.
    await tester.pump(const Duration(milliseconds: 300));

    largeTitleOpacity =
484
        tester.element(find.text('Title')).ancestorRenderObjectOfType(const TypeMatcher<RenderAnimatedOpacity>());
485 486
    // Large title no longer visible.
    expect(
487
      largeTitleOpacity.opacity.value,
488
      0.0,
489 490 491 492 493 494 495 496
    );

    // The persistent toolbar doesn't move or change size.
    expect(tester.getTopLeft(find.byType(NavigationToolbar)).dy, 0.0);
    expect(tester.getSize(find.byType(NavigationToolbar)).height, 44.0);

    expect(tester.getBottomLeft(find.text('Title')).dy, 44.0 - 8.0); // Extension gone, (static part - padding) left.
  });
497 498 499

  testWidgets('Auto back/close button', (WidgetTester tester) async {
    await tester.pumpWidget(
500 501
      const CupertinoApp(
        home: CupertinoNavigationBar(
502
          middle: Text('Home page'),
xster's avatar
xster committed
503
        ),
504 505 506 507 508
      ),
    );

    expect(find.byType(CupertinoButton), findsNothing);

509
    tester.state<NavigatorState>(find.byType(Navigator)).push(CupertinoPageRoute<void>(
510 511
      builder: (BuildContext context) {
        return const CupertinoNavigationBar(
512
          middle: Text('Page 2'),
513 514 515 516 517
        );
      },
    ));

    await tester.pump();
518
    await tester.pump(const Duration(milliseconds: 500));
519 520

    expect(find.byType(CupertinoButton), findsOneWidget);
521
    expect(find.text(String.fromCharCode(CupertinoIcons.back.codePoint)), findsOneWidget);
522

523
    tester.state<NavigatorState>(find.byType(Navigator)).push(CupertinoPageRoute<void>(
524 525 526
      fullscreenDialog: true,
      builder: (BuildContext context) {
        return const CupertinoNavigationBar(
527
          middle: Text('Dialog page'),
528 529 530 531 532
        );
      },
    ));

    await tester.pump();
533
    await tester.pump(const Duration(milliseconds: 500));
534

535
    expect(find.widgetWithText(CupertinoButton, 'Close'), findsOneWidget);
536 537 538 539 540

    // Test popping goes back correctly.
    await tester.tap(find.text('Close'));

    await tester.pump();
541
    await tester.pump(const Duration(milliseconds: 500));
542 543 544

    expect(find.text('Page 2'), findsOneWidget);

545
    await tester.tap(find.text(String.fromCharCode(CupertinoIcons.back.codePoint)));
546 547

    await tester.pump();
548
    await tester.pump(const Duration(milliseconds: 500));
549 550 551

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

553 554
  testWidgets('Long back label turns into "back"', (WidgetTester tester) async {
    await tester.pumpWidget(
555 556
      const CupertinoApp(
        home: Placeholder(),
557 558 559 560
      ),
    );

    tester.state<NavigatorState>(find.byType(Navigator)).push(
561
      CupertinoPageRoute<void>(
562 563 564
        builder: (BuildContext context) {
          return const CupertinoPageScaffold(
            navigationBar: CupertinoNavigationBar(
565
              previousPageTitle: '012345678901',
566 567 568 569 570 571 572 573 574 575
            ),
            child: Placeholder(),
          );
        }
      )
    );

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

576
    expect(find.widgetWithText(CupertinoButton, '012345678901'), findsOneWidget);
577 578

    tester.state<NavigatorState>(find.byType(Navigator)).push(
579
      CupertinoPageRoute<void>(
580 581 582
        builder: (BuildContext context) {
          return const CupertinoPageScaffold(
            navigationBar: CupertinoNavigationBar(
583
              previousPageTitle: '0123456789012',
584 585 586 587 588 589 590 591 592 593 594 595
            ),
            child: Placeholder(),
          );
        }
      )
    );

    await tester.pump();
    await tester.pump(const Duration(milliseconds: 500));
    expect(find.widgetWithText(CupertinoButton, 'Back'), findsOneWidget);
  });

596 597
  testWidgets('Border should be displayed by default', (WidgetTester tester) async {
    await tester.pumpWidget(
598 599
      const CupertinoApp(
        home: CupertinoNavigationBar(
600
          middle: Text('Title'),
xster's avatar
xster committed
601
        ),
602 603 604
      ),
    );

605 606 607 608
    final DecoratedBox decoratedBox = tester.widgetList(find.descendant(
      of: find.byType(CupertinoNavigationBar),
      matching: find.byType(DecoratedBox),
    )).first;
609 610 611 612 613 614 615 616 617 618 619
    expect(decoratedBox.decoration.runtimeType, BoxDecoration);

    final BoxDecoration decoration = decoratedBox.decoration;
    expect(decoration.border, isNotNull);

    final BorderSide side = decoration.border.bottom;
    expect(side, isNotNull);
  });

  testWidgets('Overrides border color', (WidgetTester tester) async {
    await tester.pumpWidget(
620 621
      const CupertinoApp(
        home: CupertinoNavigationBar(
622 623 624 625
          middle: Text('Title'),
          border: Border(
            bottom: BorderSide(
              color: Color(0xFFAABBCC),
xster's avatar
xster committed
626 627 628 629
              width: 0.0,
            ),
          ),
        ),
630 631 632
      ),
    );

633 634 635 636
    final DecoratedBox decoratedBox = tester.widgetList(find.descendant(
      of: find.byType(CupertinoNavigationBar),
      matching: find.byType(DecoratedBox),
    )).first;
637 638 639 640 641 642 643 644 645 646 647 648
    expect(decoratedBox.decoration.runtimeType, BoxDecoration);

    final BoxDecoration decoration = decoratedBox.decoration;
    expect(decoration.border, isNotNull);

    final BorderSide side = decoration.border.bottom;
    expect(side, isNotNull);
    expect(side.color, const Color(0xFFAABBCC));
  });

  testWidgets('Border should not be displayed when null', (WidgetTester tester) async {
    await tester.pumpWidget(
649 650
      const CupertinoApp(
        home: CupertinoNavigationBar(
651
          middle: Text('Title'),
xster's avatar
xster committed
652 653
          border: null,
        ),
654 655 656
      ),
    );

657 658 659 660
    final DecoratedBox decoratedBox = tester.widgetList(find.descendant(
      of: find.byType(CupertinoNavigationBar),
      matching: find.byType(DecoratedBox),
    )).first;
661 662 663 664 665
    expect(decoratedBox.decoration.runtimeType, BoxDecoration);

    final BoxDecoration decoration = decoratedBox.decoration;
    expect(decoration.border, isNull);
  });
666

667
  testWidgets('Border is displayed by default in sliver nav bar', (WidgetTester tester) async {
668
    await tester.pumpWidget(
669
      const CupertinoApp(
670 671
        home: CupertinoPageScaffold(
          child: CustomScrollView(
672
            slivers: <Widget>[
673 674
              CupertinoSliverNavigationBar(
                largeTitle: Text('Large Title'),
xster's avatar
xster committed
675 676 677 678
              ),
            ],
          ),
        ),
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
      ),
    );

    final DecoratedBox decoratedBox = tester.widgetList(find.descendant(
      of: find.byType(CupertinoSliverNavigationBar),
      matching: find.byType(DecoratedBox),
    )).first;
    expect(decoratedBox.decoration.runtimeType, BoxDecoration);

    final BoxDecoration decoration = decoratedBox.decoration;
    expect(decoration.border, isNotNull);

    final BorderSide bottom = decoration.border.bottom;
    expect(bottom, isNotNull);
  });

695
  testWidgets('Border is not displayed when null in sliver nav bar', (WidgetTester tester) async {
696
    await tester.pumpWidget(
697
      const CupertinoApp(
698 699
        home: CupertinoPageScaffold(
          child: CustomScrollView(
700
            slivers: <Widget>[
701 702
              CupertinoSliverNavigationBar(
                largeTitle: Text('Large Title'),
xster's avatar
xster committed
703 704 705 706 707
                border: null,
              ),
            ],
          ),
        ),
708 709 710 711 712 713 714 715 716 717 718 719 720
      ),
    );

    final DecoratedBox decoratedBox = tester.widgetList(find.descendant(
      of: find.byType(CupertinoSliverNavigationBar),
      matching: find.byType(DecoratedBox),
    )).first;
    expect(decoratedBox.decoration.runtimeType, BoxDecoration);

    final BoxDecoration decoration = decoratedBox.decoration;
    expect(decoration.border, isNull);
  });

721
  testWidgets('CupertinoSliverNavigationBar has semantics', (WidgetTester tester) async {
722
    final SemanticsTester semantics = SemanticsTester(tester);
723

724
    await tester.pumpWidget(const CupertinoApp(
725 726
      home: CupertinoPageScaffold(
        child: CustomScrollView(
727
          slivers: <Widget>[
728 729
            CupertinoSliverNavigationBar(
              largeTitle: Text('Large Title'),
xster's avatar
xster committed
730 731 732 733 734
              border: null,
            ),
          ],
        ),
      ),
735 736 737 738 739 740 741 742 743 744 745 746
    ));

    expect(semantics.nodesWith(
      label: 'Large Title',
      flags: <SemanticsFlag>[SemanticsFlag.isHeader],
      textDirection: TextDirection.ltr,
    ), hasLength(1));

    semantics.dispose();
  });

  testWidgets('CupertinoNavigationBar has semantics', (WidgetTester tester) async {
747
    final SemanticsTester semantics = SemanticsTester(tester);
748

749 750
    await tester.pumpWidget(CupertinoApp(
      home: CupertinoPageScaffold(
xster's avatar
xster committed
751
        navigationBar: const CupertinoNavigationBar(
752
          middle: Text('Fixed Title'),
xster's avatar
xster committed
753
        ),
754
        child: Container(),
xster's avatar
xster committed
755
      ),
756 757 758 759 760 761 762 763 764 765 766
    ));

    expect(semantics.nodesWith(
      label: 'Fixed Title',
      flags: <SemanticsFlag>[SemanticsFlag.isHeader],
      textDirection: TextDirection.ltr,
    ), hasLength(1));

    semantics.dispose();
  });

767
  testWidgets('Border can be overridden in sliver nav bar', (WidgetTester tester) async {
768
    await tester.pumpWidget(
769
      const CupertinoApp(
770 771
        home: CupertinoPageScaffold(
          child: CustomScrollView(
772
            slivers: <Widget>[
773 774 775 776 777
              CupertinoSliverNavigationBar(
                largeTitle: Text('Large Title'),
                border: Border(
                  bottom: BorderSide(
                    color: Color(0xFFAABBCC),
xster's avatar
xster committed
778 779
                    width: 0.0,
                  ),
780
                ),
xster's avatar
xster committed
781 782 783 784
              ),
            ],
          ),
        ),
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
      ),
    );

    final DecoratedBox decoratedBox = tester.widgetList(find.descendant(
      of: find.byType(CupertinoSliverNavigationBar),
      matching: find.byType(DecoratedBox),
    )).first;
    expect(decoratedBox.decoration.runtimeType, BoxDecoration);

    final BoxDecoration decoration = decoratedBox.decoration;
    expect(decoration.border, isNotNull);

    final BorderSide top = decoration.border.top;
    expect(top, isNotNull);
    expect(top, BorderSide.none);
    final BorderSide bottom = decoration.border.bottom;
    expect(bottom, isNotNull);
    expect(bottom.color, const Color(0xFFAABBCC));
  });

805 806 807 808
  testWidgets(
    'Standard title golden',
    (WidgetTester tester) async {
      await tester.pumpWidget(
809 810
        const CupertinoApp(
          home: RepaintBoundary(
811 812 813
            child: CupertinoPageScaffold(
              navigationBar: CupertinoNavigationBar(
                middle: Text('Bling bling'),
xster's avatar
xster committed
814
              ),
815
              child: Center(),
xster's avatar
xster committed
816 817
            ),
          ),
818 819 820 821 822
        ),
      );

      await expectLater(
        find.byType(RepaintBoundary).last,
823 824
        matchesGoldenFile(
          'nav_bar_test.standard_title.png',
825
          version: 2,
826
        ),
827 828 829 830 831 832 833 834
      );
    },
  );

  testWidgets(
    'Large title golden',
    (WidgetTester tester) async {
      await tester.pumpWidget(
835 836 837 838
        CupertinoApp(
          home: RepaintBoundary(
            child: CupertinoPageScaffold(
              child: CustomScrollView(
xster's avatar
xster committed
839 840
                slivers: <Widget>[
                  const CupertinoSliverNavigationBar(
841
                    largeTitle: Text('Bling bling'),
xster's avatar
xster committed
842
                  ),
843 844
                  SliverToBoxAdapter(
                    child: Container(
xster's avatar
xster committed
845 846
                      height: 1200.0,
                    ),
847
                  ),
xster's avatar
xster committed
848 849 850 851
                ],
              ),
            ),
          ),
852 853 854 855 856
        ),
      );

      await expectLater(
        find.byType(RepaintBoundary).last,
857 858
        matchesGoldenFile(
          'nav_bar_test.large_title.png',
859
          version: 2,
860
        ),
861 862
      );
    },
863
  );
864 865 866 867


  testWidgets('NavBar draws a light system bar for a dark background', (WidgetTester tester) async {
    await tester.pumpWidget(
868
      WidgetsApp(
869 870
        color: const Color(0xFFFFFFFF),
        onGenerateRoute: (RouteSettings settings) {
871
          return CupertinoPageRoute<void>(
872 873 874
            settings: settings,
            builder: (BuildContext context) {
              return const CupertinoNavigationBar(
875 876
                middle: Text('Test'),
                backgroundColor: Color(0xFF000000),
877 878 879 880 881 882 883 884 885 886 887
              );
            },
          );
        },
      ),
    );
    expect(SystemChrome.latestStyle, SystemUiOverlayStyle.light);
  });

  testWidgets('NavBar draws a dark system bar for a light background', (WidgetTester tester) async {
    await tester.pumpWidget(
888
      WidgetsApp(
889 890
        color: const Color(0xFFFFFFFF),
        onGenerateRoute: (RouteSettings settings) {
891
          return CupertinoPageRoute<void>(
892 893 894
            settings: settings,
            builder: (BuildContext context) {
              return const CupertinoNavigationBar(
895 896
                middle: Text('Test'),
                backgroundColor: Color(0xFFFFFFFF),
897 898 899 900 901 902 903 904
              );
            },
          );
        },
      ),
    );
    expect(SystemChrome.latestStyle, SystemUiOverlayStyle.dark);
  });
905 906 907 908 909 910 911 912 913 914 915 916

  testWidgets('CupertinoNavigationBarBackButton shows an error when manually added outside a route', (WidgetTester tester) async {
    await tester.pumpWidget(
      const CupertinoNavigationBarBackButton()
    );

    final dynamic exception = tester.takeException();
    expect(exception, isAssertionError);
    expect(exception.toString(), contains('CupertinoNavigationBarBackButton should only be used in routes that can be popped'));
  });

  testWidgets('CupertinoNavigationBarBackButton shows an error when placed in a route that cannot be popped', (WidgetTester tester) async {
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950
    await tester.pumpWidget(
      const CupertinoApp(
        home: CupertinoNavigationBarBackButton(),
      ),
    );

    final dynamic exception = tester.takeException();
    expect(exception, isAssertionError);
    expect(exception.toString(), contains('CupertinoNavigationBarBackButton should only be used in routes that can be popped'));
  });

  testWidgets('CupertinoNavigationBarBackButton with a custom onPressed callback can be placed anywhere', (WidgetTester tester) async {
    bool backPressed = false;

    await tester.pumpWidget(
      CupertinoApp(
        home: CupertinoNavigationBarBackButton(
          onPressed: () => backPressed = true,
        ),
      ),
    );

    expect(tester.takeException(), isNull);
    expect(find.text(String.fromCharCode(CupertinoIcons.back.codePoint)), findsOneWidget);

    await tester.tap(find.byType(CupertinoNavigationBarBackButton));

    expect(backPressed, true);
  });

  testWidgets(
    'Manually inserted CupertinoNavigationBarBackButton still automatically '
        'show previous page title when possible',
    (WidgetTester tester) async {
951 952
      await tester.pumpWidget(
        const CupertinoApp(
953
          home: Placeholder(),
954 955 956
        ),
      );

957 958 959 960 961 962 963 964 965
      tester.state<NavigatorState>(find.byType(Navigator)).push(
        CupertinoPageRoute<void>(
          title: 'An iPod',
          builder: (BuildContext context) {
            return const CupertinoPageScaffold(
              navigationBar: CupertinoNavigationBar(),
              child: Placeholder(),
            );
          },
966
        ),
967 968 969 970 971 972 973 974 975 976 977
      );

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

      tester.state<NavigatorState>(find.byType(Navigator)).push(
        CupertinoPageRoute<void>(
          title: 'A Phone',
          builder: (BuildContext context) {
            return const CupertinoNavigationBarBackButton();
          },
978
        ),
979 980 981 982 983 984
      );

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

      expect(find.widgetWithText(CupertinoButton, 'An iPod'), findsOneWidget);
985
    },
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
  );

  testWidgets(
    'CupertinoNavigationBarBackButton onPressed overrides default pop behavior',
    (WidgetTester tester) async {
      bool backPressed = false;
      await tester.pumpWidget(
        const CupertinoApp(
          home: Placeholder(),
        ),
      );

      tester.state<NavigatorState>(find.byType(Navigator)).push(
        CupertinoPageRoute<void>(
          title: 'An iPod',
          builder: (BuildContext context) {
            return const CupertinoPageScaffold(
              navigationBar: CupertinoNavigationBar(),
              child: Placeholder(),
            );
          },
1007
        ),
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
      );

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

      tester.state<NavigatorState>(find.byType(Navigator)).push(
        CupertinoPageRoute<void>(
          title: 'A Phone',
          builder: (BuildContext context) {
            return CupertinoPageScaffold(
              navigationBar: CupertinoNavigationBar(
                leading: CupertinoNavigationBarBackButton(
                  onPressed: () => backPressed = true,
                ),
              ),
              child: const Placeholder(),
            );
          },
1026
        ),
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
      );

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

      await tester.tap(find.byType(CupertinoNavigationBarBackButton));
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 500));

      // The second page is still on top and didn't pop.
      expect(find.text('A Phone'), findsOneWidget);
      // Custom onPressed called.
      expect(backPressed, true);
1040
    },
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 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 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 1119 1120 1121 1122 1123 1124 1125 1126

  testWidgets('textScaleFactor is set to 1.0', (WidgetTester tester) async {
    await tester.pumpWidget(
      CupertinoApp(
        home: Builder(builder: (BuildContext context) {
          return MediaQuery(
            data: MediaQuery.of(context).copyWith(textScaleFactor: 99),
            child: CupertinoPageScaffold(
              child: CustomScrollView(
                slivers: <Widget>[
                  const CupertinoSliverNavigationBar(
                    leading: Text('leading'),
                    middle: Text('middle'),
                    largeTitle: Text('Large Title'),
                    trailing: Text('trailing'),
                  ),
                  SliverToBoxAdapter(
                    child: Container(
                      child: const Text('content'),
                    ),
                  ),
                ],
              ),
            ),
          );
        }),
      ),
    );

    final Iterable<RichText> barItems = tester.widgetList<RichText>(
      find.descendant(
        of: find.byType(CupertinoSliverNavigationBar),
        matching: find.byType(RichText),
      ),
    );

    final Iterable<RichText> contents = tester.widgetList<RichText>(
      find.descendant(
        of: find.text('content'),
        matching: find.byType(RichText),
      ),
    );

    expect(barItems.length, greaterThan(0));
    expect(barItems.any((RichText t) => t.textScaleFactor != 1), isFalse);

    expect(contents.length, greaterThan(0));
    expect(contents.any((RichText t) => t.textScaleFactor != 99), isFalse);

    // Also works with implicitly added widgets.
    tester.state<NavigatorState>(find.byType(Navigator)).push(CupertinoPageRoute<void>(
      title: 'title',
      builder: (BuildContext context) {
        return MediaQuery(
          data: MediaQuery.of(context).copyWith(textScaleFactor: 99),
          child: Container(
            child: const CupertinoPageScaffold(
              child: CustomScrollView(
                slivers: <Widget>[
                  CupertinoSliverNavigationBar(
                    automaticallyImplyLeading: true,
                    automaticallyImplyTitle: true,
                    previousPageTitle: 'previous title',
                  ),
                ],
              ),
            ),
          ),
        );
      },
    ));

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

    final Iterable<RichText> barItems2 = tester.widgetList<RichText>(
      find.descendant(
        of: find.byType(CupertinoSliverNavigationBar),
        matching: find.byType(RichText),
      ),
    );

    expect(barItems2.length, greaterThan(0));
    expect(barItems2.any((RichText t) => t.textScaleFactor != 1), isFalse);
  });
1127 1128 1129
}

class _ExpectStyles extends StatelessWidget {
xster's avatar
xster committed
1130
  const _ExpectStyles({ this.color, this.index });
1131 1132 1133 1134 1135 1136 1137

  final Color color;
  final int index;

  @override
  Widget build(BuildContext context) {
    final TextStyle style = DefaultTextStyle.of(context).style;
1138
    expect(style.color.value, color.value);
xster's avatar
xster committed
1139
    expect(style.fontFamily, '.SF Pro Text');
1140
    expect(style.fontSize, 17.0);
xster's avatar
xster committed
1141
    expect(style.letterSpacing, -0.41);
1142
    count += index;
1143
    return Container();
1144
  }
1145
}