nav_bar_transition_test.dart 40.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright 2018 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';
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 55 56 57
      navigationBar: navBar ?? const CupertinoNavigationBar(),
      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 75 76 77 78 79 80 81 82 83
        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) {
  final RenderObjectWithChildMixin<RenderStack> theater =
      tester.renderObject(find.byType(Overlay));
  final RenderStack theaterStack = theater.child;
  final Finder lastOverlayFinder = find.byElementPredicate((Element element) {
    return element is RenderObjectElement &&
        element.renderObject == theaterStack.lastChild;
  });

  assert(
xster's avatar
xster committed
84 85 86 87 88 89 90 91 92
    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',
  );
93 94 95 96 97 98 99 100 101 102 103

  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
104 105 106 107 108 109
    tester.widget<SizedBox>(
      find.descendant(
        of: find.byWidget(transitionBackgroundBox),
        matching: find.byType(SizedBox),
      ),
    ).height,
110 111 112 113 114 115
    height,
  );
}

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

void main() {
127
  testWidgets('Bottom middle moves between middle and back label', (WidgetTester tester) async {
128 129 130 131 132 133 134 135 136 137 138 139 140
    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),
141
      const Offset(337.0234375, 13.5),
142 143 144
    );
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 1')).last),
145
      const Offset(337.0234375, 13.5),
146 147 148
    );
  });

149
  testWidgets('Bottom middle moves between middle and back label RTL', (WidgetTester tester) async {
150 151 152 153 154 155 156 157 158 159 160 161
    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),
162
      const Offset(362.9765625, 13.5),
163 164 165
    );
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 1')).last),
166
      const Offset(362.9765625, 13.5),
167 168 169
    );
  });

170
  testWidgets('Bottom middle and top back label transitions their font', (WidgetTester tester) async {
171 172 173 174 175 176 177 178
    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);
179
    expect(bottomMiddle.text.style.color, const Color(0xff00050a));
180
    expect(bottomMiddle.text.style.fontWeight, FontWeight.w600);
xster's avatar
xster committed
181 182
    expect(bottomMiddle.text.style.fontFamily, '.SF Pro Text');
    expect(bottomMiddle.text.style.letterSpacing, -0.41);
183 184

    checkOpacity(
185
        tester, flying(tester, find.text('Page 1')).first, 0.9004602432250977);
186 187 188 189 190

    // 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);
191
    expect(topBackLabel.text.style.color, const Color(0xff00050a));
192
    expect(topBackLabel.text.style.fontWeight, FontWeight.w600);
xster's avatar
xster committed
193 194
    expect(topBackLabel.text.style.fontFamily, '.SF Pro Text');
    expect(topBackLabel.text.style.letterSpacing, -0.41);
195 196 197 198 199

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

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

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

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

    checkOpacity(
213
        tester, flying(tester, find.text('Page 1')).last, 0.7630139589309692);
214 215
  });

216 217 218 219 220 221
  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
222

223 224
    // Be mid-transition.
    await tester.pump(const Duration(milliseconds: 50));
xster's avatar
xster committed
225

226 227 228 229 230 231 232
    // The transition's stack is ordered. The bottom middle is inserted first.
    final RenderParagraph bottomMiddle =
        tester.renderObject(flying(tester, find.text('Page 1')).first);
    expect(bottomMiddle.text.style.color, const Color(0xfffffaf4));
    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
233

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

236 237 238 239 240 241 242 243
    // 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);
    expect(topBackLabel.text.style.color, const Color(0xfffffaf4));
    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
244

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

247 248 249 250 251 252
    // Move animation further a bit.
    await tester.pump(const Duration(milliseconds: 200));
    expect(bottomMiddle.text.style.color, const Color(0xffffa01a));
    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
253

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

256 257 258 259
    expect(topBackLabel.text.style.color, const Color(0xffffa01a));
    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
260

261 262
    checkOpacity(tester, flying(tester, find.text('Page 1')).last, 0.7630139589309692);
  });
xster's avatar
xster committed
263

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

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

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

    tester
        .state<NavigatorState>(find.byType(Navigator))
283
        .push(CupertinoPageRoute<void>(
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 319 320 321
          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);
  });

322
  testWidgets('Popping mid-transition is symmetrical', (WidgetTester tester) async {
323 324 325 326 327 328 329 330 331
    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);
332
      expect(bottomMiddle.text.style.color, const Color(0xff00050a));
333 334
      expect(
        tester.getTopLeft(flying(tester, find.text('Page 1')).first),
335
        const Offset(337.0234375, 13.5),
336 337 338 339 340 341
      );

      // 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);
342
      expect(topBackLabel.text.style.color, const Color(0xff00050a));
343 344
      expect(
        tester.getTopLeft(flying(tester, find.text('Page 1')).last),
345
        const Offset(337.0234375, 13.5),
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
      );
    }

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

363
  testWidgets('Popping mid-transition is symmetrical RTL', (WidgetTester tester) async {
364 365 366 367 368 369 370 371 372 373 374 375 376
    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);
377
      expect(bottomMiddle.text.style.color, const Color(0xff00050a));
378 379
      expect(
        tester.getTopLeft(flying(tester, find.text('Page 1')).first),
380
        const Offset(362.9765625, 13.5),
381 382 383 384 385 386
      );

      // 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);
387
      expect(topBackLabel.text.style.color, const Color(0xff00050a));
388 389
      expect(
        tester.getTopLeft(flying(tester, find.text('Page 1')).last),
390
        const Offset(362.9765625, 13.5),
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
      );
    }

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

408
  testWidgets('There should be no global keys in the hero flight', (WidgetTester tester) async {
409 410 411 412 413 414 415 416 417 418 419 420 421 422
    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,
    );
  });

423
  testWidgets('Multiple nav bars tags do not conflict if in different navigators', (WidgetTester tester) async {
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 454 455 456
    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(),
                            );
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 495 496 497
                        ));
                      },
                    ),
                  ),
                );
              },
            );
          },
        ),
      ),
    );

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

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

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

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

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

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

    await tester.pump(const Duration(milliseconds: 50));
519
    checkBackgroundBoxHeight(tester, 90.53337162733078);
520 521
  });

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

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

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

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

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

    await tester.pump(const Duration(milliseconds: 50));
543
    checkBackgroundBoxHeight(tester, 49.46662837266922);
544 545
  });

546
  testWidgets('Hero flight removed at the end of page transition', (WidgetTester tester) async {
547 548 549 550 551 552 553 554 555 556 557 558 559 560
    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);
  });

561
  testWidgets('Exact widget is reused to build inside the transition', (WidgetTester tester) async {
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
    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);
  });

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

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

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

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

    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(
605
        tester.getTopLeft(backChevron), const Offset(73.078125, 5.0));
606 607

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

613
  testWidgets('First appearance of back chevron fades in from the left in RTL', (WidgetTester tester) async {
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
    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),
649
      const Offset(692.921875, 5.0),
650 651 652
    );

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

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

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

    final Finder backChevrons = flying(tester,
666
        find.text(String.fromCharCode(CupertinoIcons.back.codePoint)));
667 668 669 670 671 672

    expect(
      backChevrons,
      findsNWidgets(2),
    );

673
    checkOpacity(tester, backChevrons.first, 0.8833301812410355);
674 675 676 677 678 679 680
    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);
681
    checkOpacity(tester, backChevrons.last, 0.4604858811944723);
682 683 684 685 686
    // 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));
  });

687
  testWidgets('Bottom middle just fades if top page has a custom leading', (WidgetTester tester) async {
688 689 690 691 692 693 694 695 696 697 698 699 700 701
    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);

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

    // 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);

730
    checkOpacity(tester, flying(tester, find.text('custom')), 0.828093871474266);
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
    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);

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

    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
766
      const Offset(684.0, 13.5),
767 768 769
    );
  });

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

    await tester.pump(const Duration(milliseconds: 500));
    tester
        .state<NavigatorState>(find.byType(Navigator))
780
        .push(CupertinoPageRoute<void>(
781 782 783 784 785 786 787 788 789 790 791
          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.
792
    checkOpacity(tester, flying(tester, find.text('Page 1')), 0.6697911769151688);
793 794
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 1'))),
795
      const Offset(30.8125, 13.5),
796 797 798 799 800 801
    );

    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'))),
802
      const Offset(-262.2321922779083, 13.5),
803 804 805
    );
  });

806
  testWidgets('Bottom back label fades and slides to the right in RTL', (WidgetTester tester) async {
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
    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.
829
    checkOpacity(tester, flying(tester, find.text('Page 1')), 0.6697911769151688);
830 831
    expect(
      tester.getTopRight(flying(tester, find.text('Page 1'))),
832
      const Offset(769.1875, 13.5),
833 834 835 836 837 838 839
    );

    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.
840
      const Offset(1062.2321922779083, 13.5),
841 842 843
    );
  });

844
  testWidgets('Bottom large title moves to top back label', (WidgetTester tester) async {
845 846 847 848 849 850 851 852 853 854 855 856 857
    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));

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

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

882
  testWidgets('Long title turns into the word back mid transition', (WidgetTester tester) async {
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
    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')),
898
        0.8833301812410355);
899 900 901
    checkOpacity(tester, flying(tester, find.text('Back')), 0.0);
    expect(
      tester.getTopLeft(flying(tester, find.text('A title too long to fit'))),
902
      const Offset(17.375, 52.39453125),
903 904 905
    );
    expect(
      tester.getTopLeft(flying(tester, find.text('Back'))),
906
      const Offset(17.375, 52.39453125),
907 908 909
    );

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

922
  testWidgets('Bottom large title and top back label transitions their font', (WidgetTester tester) async {
923 924 925 926 927 928 929 930 931 932 933 934
    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);
935
    expect(bottomLargeTitle.text.style.color, const Color(0xff00050a));
936 937
    expect(bottomLargeTitle.text.style.fontWeight, FontWeight.w700);
    expect(bottomLargeTitle.text.style.fontFamily, '.SF Pro Display');
938
    expect(bottomLargeTitle.text.style.letterSpacing, 0.374765625);
939 940 941 942

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

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

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

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

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

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

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

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

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

986
  testWidgets('Top middle fades in and slides in from the left in RTL', (WidgetTester tester) async {
987 988 989 990 991 992 993 994 995 996 997 998 999
    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'))),
1000
      const Offset(67.1875, 13.5),
1001 1002 1003 1004
    );

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

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

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

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

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

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

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

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

1038
  testWidgets('Top large title fades in and slides in from the left in RTL', (WidgetTester tester) async {
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
    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'))),
1053
      const Offset(18.375, 54.0),
1054 1055 1056 1057
    );

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

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

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

1110
  testWidgets('Back swipe gesture transitions', (WidgetTester tester) async {
xster's avatar
xster committed
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
    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
1132
      const Offset(353.5802058875561, 13.5),
xster's avatar
xster committed
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
    );

    // 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
1147
      const Offset(655.2055835723877, 13.5),
xster's avatar
xster committed
1148 1149 1150 1151
    );
    await tester.pump(const Duration(milliseconds: 50));
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 2'))),
1152
      const Offset(749.6335566043854, 13.5),
xster's avatar
xster committed
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
    );

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

1164
  testWidgets('Back swipe gesture cancels properly with transition', (WidgetTester tester) async {
xster's avatar
xster committed
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
    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
1186
      const Offset(353.5802058875561, 13.5),
xster's avatar
xster committed
1187 1188 1189 1190 1191 1192 1193 1194
    );

    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
1195
      const Offset(353.5802058875561, 13.5),
xster's avatar
xster committed
1196 1197 1198 1199
    );
    await tester.pump(const Duration(milliseconds: 50));
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 2'))),
1200
      const Offset(350.0011436641216, 13.5),
xster's avatar
xster committed
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
    );

    // 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);
  });
1212
}