nav_bar_transition_test.dart 40.7 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14
// 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';
import 'package:flutter_test/flutter_test.dart';

Future<void> startTransitionBetween(
  WidgetTester tester, {
  Widget from,
  Widget to,
  String fromTitle,
  String toTitle,
15
  TextDirection textDirection = TextDirection.ltr,
xster's avatar
xster committed
16
  CupertinoThemeData theme,
17 18
}) async {
  await tester.pumpWidget(
19
    CupertinoApp(
xster's avatar
xster committed
20
      theme: theme,
21 22 23 24 25 26 27
      builder: (BuildContext context, Widget navigator) {
        return Directionality(
          textDirection: textDirection,
          child: navigator,
        );
      },
      home: const Placeholder(),
28 29 30 31 32
    ),
  );

  tester
      .state<NavigatorState>(find.byType(Navigator))
33
      .push(CupertinoPageRoute<void>(
34 35 36 37 38 39 40 41 42
        title: fromTitle,
        builder: (BuildContext context) => scaffoldForNavBar(from),
      ));

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

  tester
      .state<NavigatorState>(find.byType(Navigator))
43
      .push(CupertinoPageRoute<void>(
44 45 46 47 48 49 50 51 52
        title: toTitle,
        builder: (BuildContext context) => scaffoldForNavBar(to),
      ));

  await tester.pump();
}

CupertinoPageScaffold scaffoldForNavBar(Widget navBar) {
  if (navBar is CupertinoNavigationBar || navBar == null) {
53
    return CupertinoPageScaffold(
54
      navigationBar: navBar as CupertinoNavigationBar ?? const CupertinoNavigationBar(),
55 56 57
      child: const Placeholder(),
    );
  } else if (navBar is CupertinoSliverNavigationBar) {
58 59
    return CupertinoPageScaffold(
      child: CustomScrollView(
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
        slivers: <Widget>[
          navBar,
          // Add filler so it's scrollable.
          const SliverToBoxAdapter(
            child: Placeholder(fallbackHeight: 1000.0),
          ),
        ],
      ),
    );
  }
  assert(false, 'Unexpected nav bar type ${navBar.runtimeType}');
  return null;
}

Finder flying(WidgetTester tester, Finder finder) {
75
  final ContainerRenderObjectMixin<RenderBox, StackParentData> theater = tester.renderObject(find.byType(Overlay));
76
  final Finder lastOverlayFinder = find.byElementPredicate((Element element) {
77
    return element is RenderObjectElement && element.renderObject == theater.lastChild;
78 79 80
  });

  assert(
xster's avatar
xster committed
81 82 83 84 85 86 87 88 89
    find.descendant(
      of: lastOverlayFinder,
      matching: find.byWidgetPredicate(
        (Widget widget) =>
            widget.runtimeType.toString() == '_NavigationBarTransition',
      ),
    ).evaluate().length == 1,
    'The last overlay in the navigator was not a flying hero',
  );
90 91 92 93 94 95 96 97 98 99 100

  return find.descendant(
    of: lastOverlayFinder,
    matching: finder,
  );
}

void checkBackgroundBoxHeight(WidgetTester tester, double height) {
  final Widget transitionBackgroundBox =
      tester.widget<Stack>(flying(tester, find.byType(Stack))).children[0];
  expect(
xster's avatar
xster committed
101 102 103 104 105 106
    tester.widget<SizedBox>(
      find.descendant(
        of: find.byWidget(transitionBackgroundBox),
        matching: find.byType(SizedBox),
      ),
    ).height,
107 108 109 110 111 112
    height,
  );
}

void checkOpacity(WidgetTester tester, Finder finder, double opacity) {
  expect(
xster's avatar
xster committed
113 114 115 116 117 118
    tester.renderObject<RenderAnimatedOpacity>(
      find.ancestor(
        of: finder,
        matching: find.byType(FadeTransition),
      ),
    ).opacity.value,
119
    moreOrLessEquals(opacity),
120 121 122 123
  );
}

void main() {
124
  testWidgets('Bottom middle moves between middle and back label', (WidgetTester tester) async {
125 126 127 128 129 130 131 132 133 134 135 136 137
    await startTransitionBetween(tester, fromTitle: 'Page 1');

    // Be mid-transition.
    await tester.pump(const Duration(milliseconds: 50));

    // There's 2 of them. One from the top route's back label and one from the
    // bottom route's middle widget.
    expect(flying(tester, find.text('Page 1')), findsNWidgets(2));

    // Since they have the same text, they should be more or less at the same
    // place.
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 1')).first),
138
      const Offset(337.0234375, 13.5),
139 140 141
    );
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 1')).last),
142
      const Offset(337.0234375, 13.5),
143 144 145
    );
  });

146
  testWidgets('Bottom middle moves between middle and back label RTL', (WidgetTester tester) async {
147 148 149 150 151 152 153 154 155 156 157 158
    await startTransitionBetween(
      tester,
      fromTitle: 'Page 1',
      textDirection: TextDirection.rtl,
    );

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

    expect(flying(tester, find.text('Page 1')), findsNWidgets(2));
    // Same as LTR but more to the right now.
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 1')).first),
159
      const Offset(362.9765625, 13.5),
160 161 162
    );
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 1')).last),
163
      const Offset(362.9765625, 13.5),
164 165 166
    );
  });

167
  testWidgets('Bottom middle and top back label transitions their font', (WidgetTester tester) async {
168 169 170 171 172 173 174 175
    await startTransitionBetween(tester, fromTitle: 'Page 1');

    // Be mid-transition.
    await tester.pump(const Duration(milliseconds: 50));

    // The transition's stack is ordered. The bottom middle is inserted first.
    final RenderParagraph bottomMiddle =
        tester.renderObject(flying(tester, find.text('Page 1')).first);
176
    expect(bottomMiddle.text.style.color, const Color(0xff00050a));
177
    expect(bottomMiddle.text.style.fontWeight, FontWeight.w600);
xster's avatar
xster committed
178 179
    expect(bottomMiddle.text.style.fontFamily, '.SF Pro Text');
    expect(bottomMiddle.text.style.letterSpacing, -0.41);
180 181

    checkOpacity(
182
        tester, flying(tester, find.text('Page 1')).first, 0.9004602432250977);
183 184 185 186 187

    // The top back label is styled exactly the same way. But the opacity tweens
    // are flipped.
    final RenderParagraph topBackLabel =
        tester.renderObject(flying(tester, find.text('Page 1')).last);
188
    expect(topBackLabel.text.style.color, const Color(0xff00050a));
189
    expect(topBackLabel.text.style.fontWeight, FontWeight.w600);
xster's avatar
xster committed
190 191
    expect(topBackLabel.text.style.fontFamily, '.SF Pro Text');
    expect(topBackLabel.text.style.letterSpacing, -0.41);
192 193 194 195 196

    checkOpacity(tester, flying(tester, find.text('Page 1')).last, 0.0);

    // Move animation further a bit.
    await tester.pump(const Duration(milliseconds: 200));
197
    expect(bottomMiddle.text.style.color, const Color(0xff006de4));
198
    expect(bottomMiddle.text.style.fontWeight, FontWeight.w400);
xster's avatar
xster committed
199 200
    expect(bottomMiddle.text.style.fontFamily, '.SF Pro Text');
    expect(bottomMiddle.text.style.letterSpacing, -0.41);
201 202 203

    checkOpacity(tester, flying(tester, find.text('Page 1')).first, 0.0);

204
    expect(topBackLabel.text.style.color, const Color(0xff006de4));
205
    expect(topBackLabel.text.style.fontWeight, FontWeight.w400);
xster's avatar
xster committed
206 207
    expect(topBackLabel.text.style.fontFamily, '.SF Pro Text');
    expect(topBackLabel.text.style.letterSpacing, -0.41);
208 209

    checkOpacity(
210
        tester, flying(tester, find.text('Page 1')).last, 0.7630139589309692);
211 212
  });

213 214 215 216 217 218
  testWidgets('Font transitions respect themes', (WidgetTester tester) async {
    await startTransitionBetween(
      tester,
      fromTitle: 'Page 1',
      theme: const CupertinoThemeData(brightness: Brightness.dark),
    );
xster's avatar
xster committed
219

220 221
    // Be mid-transition.
    await tester.pump(const Duration(milliseconds: 50));
xster's avatar
xster committed
222

223 224 225
    // The transition's stack is ordered. The bottom middle is inserted first.
    final RenderParagraph bottomMiddle =
        tester.renderObject(flying(tester, find.text('Page 1')).first);
226
    expect(bottomMiddle.text.style.color, const Color(0xFFF4F9FF));
227 228 229
    expect(bottomMiddle.text.style.fontWeight, FontWeight.w600);
    expect(bottomMiddle.text.style.fontFamily, '.SF Pro Text');
    expect(bottomMiddle.text.style.letterSpacing, -0.41);
xster's avatar
xster committed
230

231
    checkOpacity(tester, flying(tester, find.text('Page 1')).first, 0.9004602432250977);
xster's avatar
xster committed
232

233 234 235 236
    // The top back label is styled exactly the same way. But the opacity tweens
    // are flipped.
    final RenderParagraph topBackLabel =
        tester.renderObject(flying(tester, find.text('Page 1')).last);
237
    expect(topBackLabel.text.style.color, const Color(0xFFF4F9FF));
238 239 240
    expect(topBackLabel.text.style.fontWeight, FontWeight.w600);
    expect(topBackLabel.text.style.fontFamily, '.SF Pro Text');
    expect(topBackLabel.text.style.letterSpacing, -0.41);
xster's avatar
xster committed
241

242
    checkOpacity(tester, flying(tester, find.text('Page 1')).last, 0.0);
xster's avatar
xster committed
243

244 245
    // Move animation further a bit.
    await tester.pump(const Duration(milliseconds: 200));
246
    expect(bottomMiddle.text.style.color, const Color(0xFF2390FF));
247 248 249
    expect(bottomMiddle.text.style.fontWeight, FontWeight.w400);
    expect(bottomMiddle.text.style.fontFamily, '.SF Pro Text');
    expect(bottomMiddle.text.style.letterSpacing, -0.41);
xster's avatar
xster committed
250

251
    checkOpacity(tester, flying(tester, find.text('Page 1')).first, 0.0);
xster's avatar
xster committed
252

253
    expect(topBackLabel.text.style.color, const Color(0xFF2390FF));
254 255 256
    expect(topBackLabel.text.style.fontWeight, FontWeight.w400);
    expect(topBackLabel.text.style.fontFamily, '.SF Pro Text');
    expect(topBackLabel.text.style.letterSpacing, -0.41);
xster's avatar
xster committed
257

258 259
    checkOpacity(tester, flying(tester, find.text('Page 1')).last, 0.7630139589309692);
  });
xster's avatar
xster committed
260

261
  testWidgets('Fullscreen dialogs do not create heroes', (WidgetTester tester) async {
262
    await tester.pumpWidget(
263 264
      const CupertinoApp(
        home: Placeholder(),
265 266 267 268 269
      ),
    );

    tester
        .state<NavigatorState>(find.byType(Navigator))
270
        .push(CupertinoPageRoute<void>(
271 272 273 274 275 276 277 278 279
          title: 'Page 1',
          builder: (BuildContext context) => scaffoldForNavBar(null),
        ));

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

    tester
        .state<NavigatorState>(find.byType(Navigator))
280
        .push(CupertinoPageRoute<void>(
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
          title: 'Page 2',
          fullscreenDialog: true,
          builder: (BuildContext context) => scaffoldForNavBar(null),
        ));

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

    // Only the first (non-fullscreen-dialog) page has a Hero.
    expect(find.byType(Hero), findsOneWidget);
    // No Hero transition happened.
    expect(() => flying(tester, find.text('Page 2')), throwsAssertionError);
  });

  testWidgets('Turning off transition works', (WidgetTester tester) async {
    await startTransitionBetween(
      tester,
      from: const CupertinoNavigationBar(
        transitionBetweenRoutes: false,
        middle: Text('Page 1'),
      ),
      toTitle: 'Page 2',
    );

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

    // Only the second page that doesn't have the transitionBetweenRoutes
    // override off has a Hero.
    expect(find.byType(Hero), findsOneWidget);
    expect(
      find.descendant(of: find.byType(Hero), matching: find.text('Page 2')),
      findsOneWidget,
    );

    // No Hero transition happened.
    expect(() => flying(tester, find.text('Page 2')), throwsAssertionError);
  });

319
  testWidgets('Popping mid-transition is symmetrical', (WidgetTester tester) async {
320 321 322 323 324 325 326 327 328
    await startTransitionBetween(tester, fromTitle: 'Page 1');

    // Be mid-transition.
    await tester.pump(const Duration(milliseconds: 50));

    void checkColorAndPositionAt50ms() {
      // The transition's stack is ordered. The bottom middle is inserted first.
      final RenderParagraph bottomMiddle =
          tester.renderObject(flying(tester, find.text('Page 1')).first);
329
      expect(bottomMiddle.text.style.color, const Color(0xff00050a));
330 331
      expect(
        tester.getTopLeft(flying(tester, find.text('Page 1')).first),
332
        const Offset(337.0234375, 13.5),
333 334 335 336 337 338
      );

      // The top back label is styled exactly the same way. But the opacity tweens
      // are flipped.
      final RenderParagraph topBackLabel =
          tester.renderObject(flying(tester, find.text('Page 1')).last);
339
      expect(topBackLabel.text.style.color, const Color(0xff00050a));
340 341
      expect(
        tester.getTopLeft(flying(tester, find.text('Page 1')).last),
342
        const Offset(337.0234375, 13.5),
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
      );
    }

    checkColorAndPositionAt50ms();

    // Advance more.
    await tester.pump(const Duration(milliseconds: 100));

    // Pop and reverse the same amount of time.
    tester.state<NavigatorState>(find.byType(Navigator)).pop();
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 100));

    // Check that everything's the same as on the way in.
    checkColorAndPositionAt50ms();
  });

360
  testWidgets('Popping mid-transition is symmetrical RTL', (WidgetTester tester) async {
361 362 363 364 365 366 367 368 369 370 371 372 373
    await startTransitionBetween(
      tester,
      fromTitle: 'Page 1',
      textDirection: TextDirection.rtl,
    );

    // Be mid-transition.
    await tester.pump(const Duration(milliseconds: 50));

    void checkColorAndPositionAt50ms() {
      // The transition's stack is ordered. The bottom middle is inserted first.
      final RenderParagraph bottomMiddle =
          tester.renderObject(flying(tester, find.text('Page 1')).first);
374
      expect(bottomMiddle.text.style.color, const Color(0xff00050a));
375 376
      expect(
        tester.getTopLeft(flying(tester, find.text('Page 1')).first),
377
        const Offset(362.9765625, 13.5),
378 379 380 381 382 383
      );

      // The top back label is styled exactly the same way. But the opacity tweens
      // are flipped.
      final RenderParagraph topBackLabel =
          tester.renderObject(flying(tester, find.text('Page 1')).last);
384
      expect(topBackLabel.text.style.color, const Color(0xff00050a));
385 386
      expect(
        tester.getTopLeft(flying(tester, find.text('Page 1')).last),
387
        const Offset(362.9765625, 13.5),
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
      );
    }

    checkColorAndPositionAt50ms();

    // Advance more.
    await tester.pump(const Duration(milliseconds: 100));

    // Pop and reverse the same amount of time.
    tester.state<NavigatorState>(find.byType(Navigator)).pop();
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 100));

    // Check that everything's the same as on the way in.
    checkColorAndPositionAt50ms();
  });

405
  testWidgets('There should be no global keys in the hero flight', (WidgetTester tester) async {
406 407 408 409 410 411 412 413 414 415 416 417 418 419
    await startTransitionBetween(tester, fromTitle: 'Page 1');

    // Be mid-transition.
    await tester.pump(const Duration(milliseconds: 50));

    expect(
      flying(
        tester,
        find.byWidgetPredicate((Widget widget) => widget.key != null),
      ),
      findsNothing,
    );
  });

420
  testWidgets('Multiple nav bars tags do not conflict if in different navigators', (WidgetTester tester) async {
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 453
    await tester.pumpWidget(
      CupertinoApp(
        home: CupertinoTabScaffold(
          tabBar: CupertinoTabBar(
            items: const <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                icon: Icon(CupertinoIcons.search),
                title: Text('Tab 1'),
              ),
              BottomNavigationBarItem(
                icon: Icon(CupertinoIcons.settings),
                title: Text('Tab 2'),
              ),
            ],
          ),
          tabBuilder: (BuildContext context, int tab) {
            return CupertinoTabView(
              builder: (BuildContext context) {
                return CupertinoPageScaffold(
                  navigationBar: CupertinoNavigationBar(
                    middle: Text('Tab ${tab + 1} Page 1'),
                  ),
                  child: Center(
                    child: CupertinoButton(
                      child: const Text('Next'),
                      onPressed: () {
                        Navigator.push<void>(context, CupertinoPageRoute<void>(
                          title: 'Tab ${tab + 1} Page 2',
                          builder: (BuildContext context) {
                            return const CupertinoPageScaffold(
                              navigationBar: CupertinoNavigationBar(),
                              child: Placeholder(),
                            );
454
                          },
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
                        ));
                      },
                    ),
                  ),
                );
              },
            );
          },
        ),
      ),
    );

    await tester.tap(find.text('Tab 2'));
    await tester.pump();

    expect(find.text('Tab 1 Page 1', skipOffstage: false), findsOneWidget);
    expect(find.text('Tab 2 Page 1'), findsOneWidget);

    // At this point, there are 2 nav bars seeded with the same _defaultHeroTag.
    // But they're inside different navigators.

    await tester.tap(find.text('Next'));
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 200));

    // One is inside the flight shuttle and another is invisible in the
    // incoming route in case a new flight needs to be created midflight.
    expect(find.text('Tab 2 Page 2'), findsNWidgets(2));

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

    expect(find.text('Tab 2 Page 2'), findsOneWidget);
    // Offstaged by tab 2's navigator.
    expect(find.text('Tab 2 Page 1', skipOffstage: false), findsOneWidget);
    // Offstaged by the CupertinoTabScaffold.
    expect(find.text('Tab 1 Page 1', skipOffstage: false), findsOneWidget);
    // Never navigated to tab 1 page 2.
    expect(find.text('Tab 1 Page 2', skipOffstage: false), findsNothing);
  });

495
  testWidgets('Transition box grows to large title size', (WidgetTester tester) async {
496 497 498 499 500 501 502 503
    await startTransitionBetween(
      tester,
      fromTitle: 'Page 1',
      to: const CupertinoSliverNavigationBar(),
      toTitle: 'Page 2',
    );

    await tester.pump(const Duration(milliseconds: 50));
504
    checkBackgroundBoxHeight(tester, 46.234375);
505 506

    await tester.pump(const Duration(milliseconds: 50));
507
    checkBackgroundBoxHeight(tester, 56.3232741355896);
508 509

    await tester.pump(const Duration(milliseconds: 50));
510
    checkBackgroundBoxHeight(tester, 73.04067611694336);
511 512

    await tester.pump(const Duration(milliseconds: 50));
513
    checkBackgroundBoxHeight(tester, 84.33018499612808);
514 515

    await tester.pump(const Duration(milliseconds: 50));
516
    checkBackgroundBoxHeight(tester, 90.53337162733078);
517 518
  });

519
  testWidgets('Large transition box shrinks to standard nav bar size', (WidgetTester tester) async {
520 521 522 523 524 525 526 527
    await startTransitionBetween(
      tester,
      from: const CupertinoSliverNavigationBar(),
      fromTitle: 'Page 1',
      toTitle: 'Page 2',
    );

    await tester.pump(const Duration(milliseconds: 50));
528
    checkBackgroundBoxHeight(tester, 93.765625);
529 530

    await tester.pump(const Duration(milliseconds: 50));
531
    checkBackgroundBoxHeight(tester, 83.6767258644104);
532 533

    await tester.pump(const Duration(milliseconds: 50));
534
    checkBackgroundBoxHeight(tester, 66.95932388305664);
535 536

    await tester.pump(const Duration(milliseconds: 50));
537
    checkBackgroundBoxHeight(tester, 55.66981500387192);
538 539

    await tester.pump(const Duration(milliseconds: 50));
540
    checkBackgroundBoxHeight(tester, 49.46662837266922);
541 542
  });

543
  testWidgets('Hero flight removed at the end of page transition', (WidgetTester tester) async {
544 545 546 547 548 549 550 551 552 553 554 555 556 557
    await startTransitionBetween(tester, fromTitle: 'Page 1');

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

    // There's 2 of them. One from the top route's back label and one from the
    // bottom route's middle widget.
    expect(flying(tester, find.text('Page 1')), findsNWidgets(2));

    // End the transition.
    await tester.pump(const Duration(milliseconds: 500));

    expect(() => flying(tester, find.text('Page 1')), throwsAssertionError);
  });

558
  testWidgets('Exact widget is reused to build inside the transition', (WidgetTester tester) async {
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
    const Widget userMiddle = Placeholder();
    await startTransitionBetween(
      tester,
      from: const CupertinoSliverNavigationBar(
        middle: userMiddle,
      ),
      fromTitle: 'Page 1',
      toTitle: 'Page 2',
    );

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

    expect(flying(tester, find.byWidget(userMiddle)), findsOneWidget);
  });

574
  testWidgets('First appearance of back chevron fades in from the right', (WidgetTester tester) async {
575
    await tester.pumpWidget(
576
      CupertinoApp(
577 578 579 580 581 582
        home: scaffoldForNavBar(null),
      ),
    );

    tester
        .state<NavigatorState>(find.byType(Navigator))
583
        .push(CupertinoPageRoute<void>(
584 585 586 587 588 589 590 591
          title: 'Page 1',
          builder: (BuildContext context) => scaffoldForNavBar(null),
        ));

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

    final Finder backChevron = flying(tester,
592
        find.text(String.fromCharCode(CupertinoIcons.back.codePoint)));
593 594 595 596 597 598 599 600 601

    expect(
      backChevron,
      // Only one exists from the top page. The bottom page has no back chevron.
      findsOneWidget,
    );
    // Come in from the right and fade in.
    checkOpacity(tester, backChevron, 0.0);
    expect(
602
        tester.getTopLeft(backChevron), const Offset(73.078125, 5.0));
603 604

    await tester.pump(const Duration(milliseconds: 150));
605
    checkOpacity(tester, backChevron, 0.09497911669313908);
606
    expect(
607
        tester.getTopLeft(backChevron), const Offset(23.260527312755585, 5.0));
608 609
  });

610
  testWidgets('First appearance of back chevron fades in from the left in RTL', (WidgetTester tester) async {
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
    await tester.pumpWidget(
      CupertinoApp(
        builder: (BuildContext context, Widget navigator) {
          return Directionality(
            textDirection: TextDirection.rtl,
            child: navigator,
          );
        },
        home: scaffoldForNavBar(null),
      ),
    );

    tester
        .state<NavigatorState>(find.byType(Navigator))
        .push(CupertinoPageRoute<void>(
          title: 'Page 1',
          builder: (BuildContext context) => scaffoldForNavBar(null),
        ));

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

    final Finder backChevron = flying(tester,
        find.text(String.fromCharCode(CupertinoIcons.back.codePoint)));

    expect(
      backChevron,
      // Only one exists from the top page. The bottom page has no back chevron.
      findsOneWidget,
    );

    // Come in from the right and fade in.
    checkOpacity(tester, backChevron, 0.0);
    expect(
      tester.getTopRight(backChevron),
646
      const Offset(692.921875, 5.0),
647 648 649
    );

    await tester.pump(const Duration(milliseconds: 150));
650
    checkOpacity(tester, backChevron, 0.09497911669313908);
651 652
    expect(
      tester.getTopRight(backChevron),
653
      const Offset(742.7394726872444, 5.0),
654 655 656
    );
  });

657
  testWidgets('Back chevron fades out and in when both pages have it', (WidgetTester tester) async {
658 659 660 661 662
    await startTransitionBetween(tester, fromTitle: 'Page 1');

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

    final Finder backChevrons = flying(tester,
663
        find.text(String.fromCharCode(CupertinoIcons.back.codePoint)));
664 665 666 667 668 669

    expect(
      backChevrons,
      findsNWidgets(2),
    );

670
    checkOpacity(tester, backChevrons.first, 0.8833301812410355);
671 672 673 674 675 676 677
    checkOpacity(tester, backChevrons.last, 0.0);
    // Both overlap at the same place.
    expect(tester.getTopLeft(backChevrons.first), const Offset(8.0, 5.0));
    expect(tester.getTopLeft(backChevrons.last), const Offset(8.0, 5.0));

    await tester.pump(const Duration(milliseconds: 150));
    checkOpacity(tester, backChevrons.first, 0.0);
678
    checkOpacity(tester, backChevrons.last, 0.4604858811944723);
679 680 681 682 683
    // Still in the same place.
    expect(tester.getTopLeft(backChevrons.first), const Offset(8.0, 5.0));
    expect(tester.getTopLeft(backChevrons.last), const Offset(8.0, 5.0));
  });

684
  testWidgets('Bottom middle just fades if top page has a custom leading', (WidgetTester tester) async {
685 686 687 688 689 690 691 692 693 694 695 696 697 698
    await startTransitionBetween(
      tester,
      fromTitle: 'Page 1',
      to: const CupertinoSliverNavigationBar(
        leading: Text('custom'),
      ),
      toTitle: 'Page 2',
    );

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

    // There's just 1 in flight because there's no back label on the top page.
    expect(flying(tester, find.text('Page 1')), findsOneWidget);

699
    checkOpacity(tester, flying(tester, find.text('Page 1')), 0.9004602432250977);
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726

    // The middle widget doesn't move.
    expect(
      tester.getCenter(flying(tester, find.text('Page 1'))),
      const Offset(400.0, 22.0),
    );

    await tester.pump(const Duration(milliseconds: 150));
    checkOpacity(tester, flying(tester, find.text('Page 1')), 0.0);
    expect(
      tester.getCenter(flying(tester, find.text('Page 1'))),
      const Offset(400.0, 22.0),
    );
  });

  testWidgets('Bottom leading fades in place', (WidgetTester tester) async {
    await startTransitionBetween(
      tester,
      from: const CupertinoSliverNavigationBar(leading: Text('custom')),
      fromTitle: 'Page 1',
      toTitle: 'Page 2',
    );

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

    expect(flying(tester, find.text('custom')), findsOneWidget);

727
    checkOpacity(tester, flying(tester, find.text('custom')), 0.828093871474266);
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
    expect(
      tester.getTopLeft(flying(tester, find.text('custom'))),
      const Offset(16.0, 0.0),
    );

    await tester.pump(const Duration(milliseconds: 150));
    checkOpacity(tester, flying(tester, find.text('custom')), 0.0);
    expect(
      tester.getTopLeft(flying(tester, find.text('custom'))),
      const Offset(16.0, 0.0),
    );
  });

  testWidgets('Bottom trailing fades in place', (WidgetTester tester) async {
    await startTransitionBetween(
      tester,
      from: const CupertinoSliverNavigationBar(trailing: Text('custom')),
      fromTitle: 'Page 1',
      toTitle: 'Page 2',
    );

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

    expect(flying(tester, find.text('custom')), findsOneWidget);

753
    checkOpacity(tester, flying(tester, find.text('custom')), 0.8833301812410355);
754 755
    expect(
      tester.getTopLeft(flying(tester, find.text('custom'))),
xster's avatar
xster committed
756
      const Offset(684.0, 13.5),
757 758 759 760 761 762
    );

    await tester.pump(const Duration(milliseconds: 150));
    checkOpacity(tester, flying(tester, find.text('custom')), 0.0);
    expect(
      tester.getTopLeft(flying(tester, find.text('custom'))),
xster's avatar
xster committed
763
      const Offset(684.0, 13.5),
764 765 766
    );
  });

767
  testWidgets('Bottom back label fades and slides to the left', (WidgetTester tester) async {
768 769 770 771 772 773 774 775 776
    await startTransitionBetween(
      tester,
      fromTitle: 'Page 1',
      toTitle: 'Page 2',
    );

    await tester.pump(const Duration(milliseconds: 500));
    tester
        .state<NavigatorState>(find.byType(Navigator))
777
        .push(CupertinoPageRoute<void>(
778 779 780 781 782 783 784 785 786 787 788
          title: 'Page 3',
          builder: (BuildContext context) => scaffoldForNavBar(null),
        ));

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

    // 'Page 1' appears once on Page 2 as the back label.
    expect(flying(tester, find.text('Page 1')), findsOneWidget);

    // Back label fades out faster.
789
    checkOpacity(tester, flying(tester, find.text('Page 1')), 0.6697911769151688);
790 791
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 1'))),
792
      const Offset(30.8125, 13.5),
793 794 795 796 797 798
    );

    await tester.pump(const Duration(milliseconds: 150));
    checkOpacity(tester, flying(tester, find.text('Page 1')), 0.0);
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 1'))),
799
      const Offset(-262.2321922779083, 13.5),
800 801 802
    );
  });

803
  testWidgets('Bottom back label fades and slides to the right in RTL', (WidgetTester tester) async {
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
    await startTransitionBetween(
      tester,
      fromTitle: 'Page 1',
      toTitle: 'Page 2',
      textDirection: TextDirection.rtl,
    );

    await tester.pump(const Duration(milliseconds: 500));
    tester
        .state<NavigatorState>(find.byType(Navigator))
        .push(CupertinoPageRoute<void>(
          title: 'Page 3',
          builder: (BuildContext context) => scaffoldForNavBar(null),
        ));

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

    // 'Page 1' appears once on Page 2 as the back label.
    expect(flying(tester, find.text('Page 1')), findsOneWidget);

    // Back label fades out faster.
826
    checkOpacity(tester, flying(tester, find.text('Page 1')), 0.6697911769151688);
827 828
    expect(
      tester.getTopRight(flying(tester, find.text('Page 1'))),
829
      const Offset(769.1875, 13.5),
830 831 832 833 834 835 836
    );

    await tester.pump(const Duration(milliseconds: 150));
    checkOpacity(tester, flying(tester, find.text('Page 1')), 0.0);
    expect(
      tester.getTopRight(flying(tester, find.text('Page 1'))),
      // >1000. It's now off the screen.
837
      const Offset(1062.2321922779083, 13.5),
838 839 840
    );
  });

841
  testWidgets('Bottom large title moves to top back label', (WidgetTester tester) async {
842 843 844 845 846 847 848 849 850 851 852 853 854
    await startTransitionBetween(
      tester,
      from: const CupertinoSliverNavigationBar(),
      fromTitle: 'Page 1',
      toTitle: 'Page 2',
    );

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

    // There's 2, one from the bottom large title fading out and one from the
    // bottom back label fading in.
    expect(flying(tester, find.text('Page 1')), findsNWidgets(2));

855
    checkOpacity(tester, flying(tester, find.text('Page 1')).first, 0.8833301812410355);
856 857 858
    checkOpacity(tester, flying(tester, find.text('Page 1')).last, 0.0);
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 1')).first),
859
      const Offset(17.375, 52.39453125),
860 861 862
    );
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 1')).last),
863
      const Offset(17.375, 52.39453125),
864 865 866 867
    );

    await tester.pump(const Duration(milliseconds: 150));
    checkOpacity(tester, flying(tester, find.text('Page 1')).first, 0.0);
868
    checkOpacity(tester, flying(tester, find.text('Page 1')).last, 0.4604858811944723);
869 870
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 1')).first),
871
      const Offset(40.818575382232666, 22.49655644595623),
872 873 874
    );
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 1')).last),
875
      const Offset(40.818575382232666, 22.49655644595623),
876 877 878
    );
  });

879
  testWidgets('Long title turns into the word back mid transition', (WidgetTester tester) async {
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
    await startTransitionBetween(
      tester,
      from: const CupertinoSliverNavigationBar(),
      fromTitle: 'A title too long to fit',
      toTitle: 'Page 2',
    );

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

    expect(
        flying(tester, find.text('A title too long to fit')), findsOneWidget);
    // Automatically changed to the word 'Back' in the back label.
    expect(flying(tester, find.text('Back')), findsOneWidget);

    checkOpacity(tester, flying(tester, find.text('A title too long to fit')),
895
        0.8833301812410355);
896 897 898
    checkOpacity(tester, flying(tester, find.text('Back')), 0.0);
    expect(
      tester.getTopLeft(flying(tester, find.text('A title too long to fit'))),
899
      const Offset(17.375, 52.39453125),
900 901 902
    );
    expect(
      tester.getTopLeft(flying(tester, find.text('Back'))),
903
      const Offset(17.375, 52.39453125),
904 905 906
    );

    await tester.pump(const Duration(milliseconds: 150));
907
    checkOpacity(tester, flying(tester, find.text('A title too long to fit')), 0.0);
908
    checkOpacity(tester, flying(tester, find.text('Back')), 0.4604858811944723);
909 910
    expect(
      tester.getTopLeft(flying(tester, find.text('A title too long to fit'))),
911
      const Offset(40.818575382232666, 22.49655644595623),
912 913 914
    );
    expect(
      tester.getTopLeft(flying(tester, find.text('Back'))),
915
      const Offset(40.818575382232666, 22.49655644595623),
916 917 918
    );
  });

919
  testWidgets('Bottom large title and top back label transitions their font', (WidgetTester tester) async {
920 921 922 923 924 925 926 927 928 929 930 931
    await startTransitionBetween(
      tester,
      from: const CupertinoSliverNavigationBar(),
      fromTitle: 'Page 1',
    );

    // Be mid-transition.
    await tester.pump(const Duration(milliseconds: 50));

    // The transition's stack is ordered. The bottom large title is inserted first.
    final RenderParagraph bottomLargeTitle =
        tester.renderObject(flying(tester, find.text('Page 1')).first);
932
    expect(bottomLargeTitle.text.style.color, const Color(0xff00050a));
933 934
    expect(bottomLargeTitle.text.style.fontWeight, FontWeight.w700);
    expect(bottomLargeTitle.text.style.fontFamily, '.SF Pro Display');
935
    expect(bottomLargeTitle.text.style.letterSpacing, 0.374765625);
936 937 938 939

    // The top back label is styled exactly the same way.
    final RenderParagraph topBackLabel =
        tester.renderObject(flying(tester, find.text('Page 1')).last);
940
    expect(topBackLabel.text.style.color, const Color(0xff00050a));
941 942
    expect(topBackLabel.text.style.fontWeight, FontWeight.w700);
    expect(topBackLabel.text.style.fontFamily, '.SF Pro Display');
943
    expect(topBackLabel.text.style.letterSpacing, 0.374765625);
944 945 946

    // Move animation further a bit.
    await tester.pump(const Duration(milliseconds: 200));
947
    expect(bottomLargeTitle.text.style.color, const Color(0xff006de4));
948
    expect(bottomLargeTitle.text.style.fontWeight, FontWeight.w400);
xster's avatar
xster committed
949
    expect(bottomLargeTitle.text.style.fontFamily, '.SF Pro Text');
950
    expect(bottomLargeTitle.text.style.letterSpacing, -0.32379547566175454);
951

952
    expect(topBackLabel.text.style.color, const Color(0xff006de4));
953
    expect(topBackLabel.text.style.fontWeight, FontWeight.w400);
xster's avatar
xster committed
954
    expect(topBackLabel.text.style.fontFamily, '.SF Pro Text');
955
    expect(topBackLabel.text.style.letterSpacing, -0.32379547566175454);
956 957
  });

958
  testWidgets('Top middle fades in and slides in from the right', (WidgetTester tester) async {
959 960 961 962 963 964 965 966 967
    await startTransitionBetween(
      tester,
      toTitle: 'Page 2',
    );

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

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

968
    checkOpacity(tester, flying(tester, find.text('Page 2')), 0.0);
969 970
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 2'))),
971
      const Offset(732.8125, 13.5),
972 973 974 975
    );

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

976
    checkOpacity(tester, flying(tester, find.text('Page 2')), 0.5555618554353714);
977 978
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 2'))),
979
      const Offset(439.7678077220917, 13.5),
980 981 982
    );
  });

983
  testWidgets('Top middle fades in and slides in from the left in RTL', (WidgetTester tester) async {
984 985 986 987 988 989 990 991 992 993 994 995 996
    await startTransitionBetween(
      tester,
      toTitle: 'Page 2',
      textDirection: TextDirection.rtl,
    );

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

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

    checkOpacity(tester, flying(tester, find.text('Page 2')), 0.0);
    expect(
      tester.getTopRight(flying(tester, find.text('Page 2'))),
997
      const Offset(67.1875, 13.5),
998 999 1000 1001
    );

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

1002
    checkOpacity(tester, flying(tester, find.text('Page 2')), 0.5555618554353714);
1003 1004
    expect(
      tester.getTopRight(flying(tester, find.text('Page 2'))),
1005
      const Offset(360.2321922779083, 13.5),
1006 1007 1008
    );
  });

1009
  testWidgets('Top large title fades in and slides in from the right', (WidgetTester tester) async {
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
    await startTransitionBetween(
      tester,
      to: const CupertinoSliverNavigationBar(),
      toTitle: 'Page 2',
    );

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

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

1020
    checkOpacity(tester, flying(tester, find.text('Page 2')), 0.0);
1021 1022
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 2'))),
1023
      const Offset(781.625, 54.0),
1024 1025 1026 1027
    );

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

1028
    checkOpacity(tester, flying(tester, find.text('Page 2')), 0.5292819738388062);
1029 1030
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 2'))),
1031
      const Offset(195.53561544418335, 54.0),
1032 1033 1034
    );
  });

1035
  testWidgets('Top large title fades in and slides in from the left in RTL', (WidgetTester tester) async {
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
    await startTransitionBetween(
      tester,
      to: const CupertinoSliverNavigationBar(),
      toTitle: 'Page 2',
      textDirection: TextDirection.rtl,
    );

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

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

    checkOpacity(tester, flying(tester, find.text('Page 2')), 0.0);
    expect(
      tester.getTopRight(flying(tester, find.text('Page 2'))),
1050
      const Offset(18.375, 54.0),
1051 1052 1053 1054
    );

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

1055
    checkOpacity(tester, flying(tester, find.text('Page 2')), 0.5292819738388062);
1056 1057
    expect(
      tester.getTopRight(flying(tester, find.text('Page 2'))),
1058
      const Offset(604.4643845558167, 54.0),
1059 1060 1061
    );
  });

1062
  testWidgets('Components are not unnecessarily rebuilt during transitions', (WidgetTester tester) async {
1063 1064 1065 1066
    int bottomBuildTimes = 0;
    int topBuildTimes = 0;
    await startTransitionBetween(
      tester,
1067 1068
      from: CupertinoNavigationBar(
        middle: Builder(builder: (BuildContext context) {
1069 1070 1071 1072
          bottomBuildTimes++;
          return const Text('Page 1');
        }),
      ),
1073 1074
      to: CupertinoSliverNavigationBar(
        largeTitle: Builder(builder: (BuildContext context) {
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
          topBuildTimes++;
          return const Text('Page 2');
        }),
      ),
    );

    expect(bottomBuildTimes, 1);
    // RenderSliverPersistentHeader.layoutChild causes 2 builds.
    expect(topBuildTimes, 2);

    await tester.pump();

    // The shuttle builder builds the component widgets one more time.
    expect(bottomBuildTimes, 2);
    expect(topBuildTimes, 3);

    // Subsequent animation needs to use reprojection of children.
    await tester.pump();
    expect(bottomBuildTimes, 2);
    expect(topBuildTimes, 3);

    await tester.pump(const Duration(milliseconds: 100));
    expect(bottomBuildTimes, 2);
    expect(topBuildTimes, 3);

    // Finish animations.
    await tester.pump(const Duration(milliseconds: 400));

    expect(bottomBuildTimes, 2);
    expect(topBuildTimes, 3);
  });
xster's avatar
xster committed
1106

1107
  testWidgets('Back swipe gesture transitions', (WidgetTester tester) async {
xster's avatar
xster committed
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
    await startTransitionBetween(
      tester,
      fromTitle: 'Page 1',
      toTitle: 'Page 2',
    );

    // Go to the next page.
    await tester.pump(const Duration(milliseconds: 500));

    // Start the gesture at the edge of the screen.
    final TestGesture gesture =  await tester.startGesture(const Offset(5.0, 200.0));
    // Trigger the swipe.
    await gesture.moveBy(const Offset(100.0, 0.0));

    // Back gestures should trigger and draw the hero transition in the very same
    // frame (since the "from" route has already moved to reveal the "to" route).
    await tester.pump();

    // Page 2, which is the middle of the top route, start to fly back to the right.
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 2'))),
xster's avatar
xster committed
1129
      const Offset(353.5802058875561, 13.5),
xster's avatar
xster committed
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
    );

    // Page 1 is in transition in 2 places. Once as the top back label and once
    // as the bottom middle.
    expect(flying(tester, find.text('Page 1')), findsNWidgets(2));

    // Past the halfway point now.
    await gesture.moveBy(const Offset(500.0, 0.0));
    await gesture.up();

    await tester.pump();
    // Transition continues.
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 2'))),
xster's avatar
xster committed
1144
      const Offset(655.2055835723877, 13.5),
xster's avatar
xster committed
1145 1146 1147 1148
    );
    await tester.pump(const Duration(milliseconds: 50));
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 2'))),
1149
      const Offset(749.6335566043854, 13.5),
xster's avatar
xster committed
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
    );

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

    // Cleans up properly
    expect(() => flying(tester, find.text('Page 1')), throwsAssertionError);
    expect(() => flying(tester, find.text('Page 2')), throwsAssertionError);
    // Just the bottom route's middle now.
    expect(find.text('Page 1'), findsOneWidget);
  });

1161
  testWidgets('Back swipe gesture cancels properly with transition', (WidgetTester tester) async {
xster's avatar
xster committed
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
    await startTransitionBetween(
      tester,
      fromTitle: 'Page 1',
      toTitle: 'Page 2',
    );

    // Go to the next page.
    await tester.pump(const Duration(milliseconds: 500));

    // Start the gesture at the edge of the screen.
    final TestGesture gesture =  await tester.startGesture(const Offset(5.0, 200.0));
    // Trigger the swipe.
    await gesture.moveBy(const Offset(100.0, 0.0));

    // Back gestures should trigger and draw the hero transition in the very same
    // frame (since the "from" route has already moved to reveal the "to" route).
    await tester.pump();

    // Page 2, which is the middle of the top route, start to fly back to the right.
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 2'))),
xster's avatar
xster committed
1183
      const Offset(353.5802058875561, 13.5),
xster's avatar
xster committed
1184 1185 1186 1187 1188 1189 1190 1191
    );

    await gesture.up();
    await tester.pump();

    // Transition continues from the point we let off.
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 2'))),
xster's avatar
xster committed
1192
      const Offset(353.5802058875561, 13.5),
xster's avatar
xster committed
1193 1194 1195 1196
    );
    await tester.pump(const Duration(milliseconds: 50));
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 2'))),
1197
      const Offset(350.0011436641216, 13.5),
xster's avatar
xster committed
1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
    );

    // Finish the snap back animation.
    await tester.pump(const Duration(milliseconds: 500));

    // Cleans up properly
    expect(() => flying(tester, find.text('Page 1')), throwsAssertionError);
    expect(() => flying(tester, find.text('Page 2')), throwsAssertionError);
    // Back to page 2.
    expect(find.text('Page 2'), findsOneWidget);
  });
1209
}