nav_bar_transition_test.dart 47.6 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
@TestOn('!chrome')
library;
7

8 9
import 'dart:ui';

10 11
import 'package:flutter/cupertino.dart';
import 'package:flutter/rendering.dart';
12
import 'package:flutter/scheduler.dart';
13
import 'package:flutter_test/flutter_test.dart';
14
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
15 16 17

Future<void> startTransitionBetween(
  WidgetTester tester, {
18 19 20 21
  Widget? from,
  Widget? to,
  String? fromTitle,
  String? toTitle,
22
  TextDirection textDirection = TextDirection.ltr,
23
  CupertinoThemeData? theme,
24
  double textScale = 1.0,
25 26
}) async {
  await tester.pumpWidget(
27
    CupertinoApp(
xster's avatar
xster committed
28
      theme: theme,
29
      builder: (BuildContext context, Widget? navigator) {
30 31 32 33 34 35
        return MediaQuery(
          data: MediaQuery.of(context).copyWith(textScaleFactor: textScale),
          child: Directionality(
            textDirection: textDirection,
            child: navigator!,
          )
36 37 38
        );
      },
      home: const Placeholder(),
39 40 41 42 43
    ),
  );

  tester
      .state<NavigatorState>(find.byType(Navigator))
44
      .push(CupertinoPageRoute<void>(
45
        title: fromTitle,
46
        builder: (BuildContext context) => scaffoldForNavBar(from)!,
47 48 49
      ));

  await tester.pump();
50
  await tester.pump(const Duration(milliseconds: 600));
51 52 53

  tester
      .state<NavigatorState>(find.byType(Navigator))
54
      .push(CupertinoPageRoute<void>(
55
        title: toTitle,
56
        builder: (BuildContext context) => scaffoldForNavBar(to)!,
57 58 59 60 61
      ));

  await tester.pump();
}

62
CupertinoPageScaffold? scaffoldForNavBar(Widget? navBar) {
63
  if (navBar is CupertinoNavigationBar || navBar == null) {
64
    return CupertinoPageScaffold(
65
      navigationBar: navBar as CupertinoNavigationBar? ?? const CupertinoNavigationBar(),
66 67 68
      child: const Placeholder(),
    );
  } else if (navBar is CupertinoSliverNavigationBar) {
69 70
    return CupertinoPageScaffold(
      child: CustomScrollView(
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
        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) {
86
  final ContainerRenderObjectMixin<RenderBox, StackParentData> theater = tester.renderObject(find.byType(Overlay));
87
  final Finder lastOverlayFinder = find.byElementPredicate((Element element) {
88
    return element is RenderObjectElement && element.renderObject == theater.lastChild;
89 90 91
  });

  assert(
xster's avatar
xster committed
92 93 94 95 96 97 98 99 100
    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',
  );
101 102 103 104 105 106 107 108 109 110 111

  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
112 113 114 115 116 117
    tester.widget<SizedBox>(
      find.descendant(
        of: find.byWidget(transitionBackgroundBox),
        matching: find.byType(SizedBox),
      ),
    ).height,
118 119 120 121 122 123
    height,
  );
}

void checkOpacity(WidgetTester tester, Finder finder, double opacity) {
  expect(
124
    tester.firstRenderObject<RenderAnimatedOpacity>(
xster's avatar
xster committed
125 126 127 128 129
      find.ancestor(
        of: finder,
        matching: find.byType(FadeTransition),
      ),
    ).opacity.value,
130
    moreOrLessEquals(opacity),
131 132 133 134
  );
}

void main() {
135
  testWidgetsWithLeakTracking('Bottom middle moves between middle and back label', (WidgetTester tester) async {
136 137 138 139 140 141 142 143 144 145 146 147 148
    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),
149 150
      const Offset(
        342.547737105096302912,
151 152
        13.5,
      ),
153 154 155
    );
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 1')).last),
156 157
      const Offset(
        342.547737105096302912,
158 159
        13.5,
      ),
160 161 162
    );
  });

163
  testWidgetsWithLeakTracking('Bottom middle moves between middle and back label RTL', (WidgetTester tester) async {
164 165 166 167 168 169 170 171 172 173 174 175
    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),
176 177
      const Offset(
        357.912261979376353338,
178 179
        13.5,
      ),
180 181 182
    );
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 1')).last),
183 184
      const Offset(
        357.912261979376353338,
185 186
        13.5,
      ),
187 188 189
    );
  });

190
  testWidgetsWithLeakTracking('Bottom middle never changes size during the animation', (WidgetTester tester) async {
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
    await tester.binding.setSurfaceSize(const Size(1080.0 / 2.75, 600));
    addTearDown(() async {
      await tester.binding.setSurfaceSize(const Size(800.0, 600.0));
    });

    await startTransitionBetween(
      tester,
      fromTitle: 'Page 1',
    );

    final Size size = tester.getSize(find.text('Page 1'));

    for (int i = 0; i < 150; i++) {
      await tester.pump(const Duration(milliseconds: 1));
      expect(flying(tester, find.text('Page 1')), findsNWidgets(2));
      expect(tester.getSize(flying(tester, find.text('Page 1')).first), size);
      expect(tester.getSize(flying(tester, find.text('Page 1')).last), size);
    }
  });

211
  testWidgetsWithLeakTracking('Bottom middle and top back label transitions their font', (WidgetTester tester) async {
212 213 214 215 216 217 218 219
    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);
220
    expect(bottomMiddle.text.style!.color, const Color(0xff000306));
221 222 223
    expect(bottomMiddle.text.style!.fontWeight, FontWeight.w600);
    expect(bottomMiddle.text.style!.fontFamily, '.SF Pro Text');
    expect(bottomMiddle.text.style!.letterSpacing, -0.41);
224

225
    checkOpacity(tester, flying(tester, find.text('Page 1')).first, 0.9404401779174805);
226 227 228 229 230

    // 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);
231
    expect(topBackLabel.text.style!.color, const Color(0xff000306));
232 233 234
    expect(topBackLabel.text.style!.fontWeight, FontWeight.w600);
    expect(topBackLabel.text.style!.fontFamily, '.SF Pro Text');
    expect(topBackLabel.text.style!.letterSpacing, -0.41);
235 236 237 238 239

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

    // Move animation further a bit.
    await tester.pump(const Duration(milliseconds: 200));
240
    expect(bottomMiddle.text.style!.color, const Color(0xff005ec5));
241 242 243
    expect(bottomMiddle.text.style!.fontWeight, FontWeight.w400);
    expect(bottomMiddle.text.style!.fontFamily, '.SF Pro Text');
    expect(bottomMiddle.text.style!.letterSpacing, -0.41);
244 245 246

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

247
    expect(topBackLabel.text.style!.color, const Color(0xff005ec5));
248 249 250
    expect(topBackLabel.text.style!.fontWeight, FontWeight.w400);
    expect(topBackLabel.text.style!.fontFamily, '.SF Pro Text');
    expect(topBackLabel.text.style!.letterSpacing, -0.41);
251

252
    checkOpacity(tester, flying(tester, find.text('Page 1')).last, 0.5292819738388062);
253 254
  });

255
  testWidgetsWithLeakTracking('Font transitions respect themes', (WidgetTester tester) async {
256 257 258 259 260
    await startTransitionBetween(
      tester,
      fromTitle: 'Page 1',
      theme: const CupertinoThemeData(brightness: Brightness.dark),
    );
xster's avatar
xster committed
261

262 263
    // Be mid-transition.
    await tester.pump(const Duration(milliseconds: 50));
xster's avatar
xster committed
264

265 266 267
    // The transition's stack is ordered. The bottom middle is inserted first.
    final RenderParagraph bottomMiddle =
        tester.renderObject(flying(tester, find.text('Page 1')).first);
268
    expect(bottomMiddle.text.style!.color, const Color(0xfff8fbff));
269 270 271
    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
272

273
    checkOpacity(tester, flying(tester, find.text('Page 1')).first, 0.9404401779174805);
xster's avatar
xster committed
274

275 276 277 278
    // 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);
279
    expect(topBackLabel.text.style!.color, const Color(0xfff8fbff));
280 281 282
    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
283

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

286 287
    // Move animation further a bit.
    await tester.pump(const Duration(milliseconds: 200));
288
    expect(bottomMiddle.text.style!.color, const Color(0xff409fff));
289 290 291
    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
292

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

295
    expect(topBackLabel.text.style!.color, const Color(0xff409fff));
296 297 298
    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
299

300
    checkOpacity(tester, flying(tester, find.text('Page 1')).last, 0.5292819738388062);
301
  });
xster's avatar
xster committed
302

303
  testWidgetsWithLeakTracking('Fullscreen dialogs do not create heroes', (WidgetTester tester) async {
304
    await tester.pumpWidget(
305 306
      const CupertinoApp(
        home: Placeholder(),
307 308 309 310 311
      ),
    );

    tester
        .state<NavigatorState>(find.byType(Navigator))
312
        .push(CupertinoPageRoute<void>(
313
          title: 'Page 1',
314
          builder: (BuildContext context) => scaffoldForNavBar(null)!,
315 316 317 318 319 320 321
        ));

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

    tester
        .state<NavigatorState>(find.byType(Navigator))
322
        .push(CupertinoPageRoute<void>(
323 324
          title: 'Page 2',
          fullscreenDialog: true,
325
          builder: (BuildContext context) => scaffoldForNavBar(null)!,
326 327 328 329 330 331 332 333 334 335 336
        ));

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

337
  testWidgetsWithLeakTracking('Turning off transition works', (WidgetTester tester) async {
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
    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);
  });

361
  testWidgetsWithLeakTracking('Popping mid-transition is symmetrical', (WidgetTester tester) async {
362 363 364 365 366 367 368 369 370
    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);
371
      expect(bottomMiddle.text.style!.color, const Color(0xff000306));
372

373 374
      expect(
        tester.getTopLeft(flying(tester, find.text('Page 1')).first),
375 376
        const Offset(
          342.547737105096302912,
377 378
          13.5,
        ),
379 380 381 382 383 384
      );

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

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

409
  testWidgetsWithLeakTracking('Popping mid-transition is symmetrical RTL', (WidgetTester tester) async {
410 411 412 413 414 415 416 417 418 419 420 421 422
    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);
423
      expect(bottomMiddle.text.style!.color, const Color(0xff000306));
424 425
      expect(
        tester.getTopLeft(flying(tester, find.text('Page 1')).first),
426 427
        const Offset(
          357.912261979376353338,
428 429
          13.5,
        ),
430 431 432 433 434 435
      );

      // 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);
436
      expect(topBackLabel.text.style!.color, const Color(0xff000306));
437 438
      expect(
        tester.getTopLeft(flying(tester, find.text('Page 1')).last),
439 440
        const Offset(
          357.912261979376353338,
441 442
          13.5,
        ),
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
      );
    }

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

460
  testWidgetsWithLeakTracking('There should be no global keys in the hero flight', (WidgetTester tester) async {
461 462 463 464 465 466 467 468 469 470 471 472 473 474
    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,
    );
  });

475
  testWidgetsWithLeakTracking('DartPerformanceMode is latency mid-animation', (WidgetTester tester) async {
476 477 478 479 480 481 482 483 484 485 486 487 488
    DartPerformanceMode? mode;

    // before the animation starts, no requests are active.
    mode = SchedulerBinding.instance.debugGetRequestedPerformanceMode();
    expect(mode, isNull);

    await startTransitionBetween(tester, fromTitle: 'Page 1');

    // mid-transition, latency mode is expected.
    await tester.pump(const Duration(milliseconds: 50));
    mode = SchedulerBinding.instance.debugGetRequestedPerformanceMode();
    expect(mode, equals(DartPerformanceMode.latency));

Lioness100's avatar
Lioness100 committed
489
    // end of transition, go back to no requests active.
490 491 492 493 494
    await tester.pump(const Duration(milliseconds: 500));
    mode = SchedulerBinding.instance.debugGetRequestedPerformanceMode();
    expect(mode, isNull);
  });

495
  testWidgetsWithLeakTracking('Multiple nav bars tags do not conflict if in different navigators', (WidgetTester tester) async {
496 497 498 499 500 501 502
    await tester.pumpWidget(
      CupertinoApp(
        home: CupertinoTabScaffold(
          tabBar: CupertinoTabBar(
            items: const <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                icon: Icon(CupertinoIcons.search),
503
                label: 'Tab 1',
504 505 506
              ),
              BottomNavigationBarItem(
                icon: Icon(CupertinoIcons.settings),
507
                label: 'Tab 2',
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
              ),
            ],
          ),
          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(),
                            );
529
                          },
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
                        ));
                      },
                    ),
                  ),
                );
              },
            );
          },
        ),
      ),
    );

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

570
  testWidgetsWithLeakTracking('Transition box grows to large title size', (WidgetTester tester) async {
571 572 573 574 575 576 577 578
    await startTransitionBetween(
      tester,
      fromTitle: 'Page 1',
      to: const CupertinoSliverNavigationBar(),
      toTitle: 'Page 2',
    );

    await tester.pump(const Duration(milliseconds: 50));
579
    checkBackgroundBoxHeight(tester, 45.3376561999321);
580 581

    await tester.pump(const Duration(milliseconds: 50));
582
    checkBackgroundBoxHeight(tester, 51.012951374053955);
583 584

    await tester.pump(const Duration(milliseconds: 50));
585
    checkBackgroundBoxHeight(tester, 63.06760931015015);
586 587

    await tester.pump(const Duration(milliseconds: 50));
588
    checkBackgroundBoxHeight(tester, 75.89544230699539);
589 590

    await tester.pump(const Duration(milliseconds: 50));
591
    checkBackgroundBoxHeight(tester, 84.33018499612808);
592 593
  });

594
  testWidgetsWithLeakTracking('Large transition box shrinks to standard nav bar size', (WidgetTester tester) async {
595 596 597 598 599 600 601 602
    await startTransitionBetween(
      tester,
      from: const CupertinoSliverNavigationBar(),
      fromTitle: 'Page 1',
      toTitle: 'Page 2',
    );

    await tester.pump(const Duration(milliseconds: 50));
603
    checkBackgroundBoxHeight(tester, 94.6623438000679);
604 605

    await tester.pump(const Duration(milliseconds: 50));
606
    checkBackgroundBoxHeight(tester, 88.98704862594604);
607 608

    await tester.pump(const Duration(milliseconds: 50));
609
    checkBackgroundBoxHeight(tester, 76.93239068984985);
610 611

    await tester.pump(const Duration(milliseconds: 50));
612
    checkBackgroundBoxHeight(tester, 64.10455769300461);
613 614

    await tester.pump(const Duration(milliseconds: 50));
615
    checkBackgroundBoxHeight(tester, 55.66981500387192);
616 617
  });

618
  testWidgetsWithLeakTracking('Hero flight removed at the end of page transition', (WidgetTester tester) async {
619 620 621 622 623 624 625 626 627 628 629 630 631 632
    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);
  });

633
  testWidgetsWithLeakTracking('Exact widget is reused to build inside the transition', (WidgetTester tester) async {
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
    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);
  });

649
  testWidgetsWithLeakTracking('Middle is not shown if alwaysShowMiddle is false and the nav bar is expanded', (WidgetTester tester) async {
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
    const Widget userMiddle = Placeholder();
    await startTransitionBetween(
      tester,
      from: const CupertinoSliverNavigationBar(
        middle: userMiddle,
        alwaysShowMiddle: false,
      ),
      fromTitle: 'Page 1',
      toTitle: 'Page 2',
    );

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

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

666
  testWidgetsWithLeakTracking('Middle is shown if alwaysShowMiddle is false but the nav bar is collapsed', (WidgetTester tester) async {
667 668
    const Widget userMiddle = Placeholder();
    final ScrollController scrollController = ScrollController();
669
    addTearDown(scrollController.dispose);
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714

    await tester.pumpWidget(
      CupertinoApp(
        home: CupertinoPageScaffold(
          child: CustomScrollView(
            controller: scrollController,
            slivers: const <Widget>[
              CupertinoSliverNavigationBar(
                largeTitle: Text('Page 1'),
                middle: userMiddle,
                alwaysShowMiddle: false,
              ),
              SliverToBoxAdapter(
                child: SizedBox(
                  height: 1200.0,
                ),
              ),
            ],
          ),
        ),
      ),
    );

    scrollController.jumpTo(600.0);
    await tester.pumpAndSettle();

    // Middle widget is visible when nav bar is collapsed.
    final RenderAnimatedOpacity userMiddleOpacity = tester
        .element(find.byWidget(userMiddle))
        .findAncestorRenderObjectOfType<RenderAnimatedOpacity>()!;
    expect(userMiddleOpacity.opacity.value, 1.0);

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

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

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

715
  testWidgetsWithLeakTracking('First appearance of back chevron fades in from the right', (WidgetTester tester) async {
716
    await tester.pumpWidget(
717
      CupertinoApp(
718 719 720 721 722 723
        home: scaffoldForNavBar(null),
      ),
    );

    tester
        .state<NavigatorState>(find.byType(Navigator))
724
        .push(CupertinoPageRoute<void>(
725
          title: 'Page 1',
726
          builder: (BuildContext context) => scaffoldForNavBar(null)!,
727 728 729 730 731
        ));

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

732
    final Finder backChevron = flying(tester, find.text(String.fromCharCode(CupertinoIcons.back.codePoint)));
733 734 735 736 737 738 739 740

    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);
741 742
    expect(tester.getTopLeft(backChevron), const Offset(
      87.2460581221158690823,
743 744
      7.0,
    ));
745

746
    await tester.pump(const Duration(milliseconds: 200));
747
    checkOpacity(tester, backChevron, 0.09497911669313908);
748 749
    expect(tester.getTopLeft(backChevron), const Offset(
      30.8718595298545324113,
750 751
      7.0,
    ));
752 753
  });

754
  testWidgetsWithLeakTracking('First appearance of back chevron fades in from the left in RTL', (WidgetTester tester) async {
755 756
    await tester.pumpWidget(
      CupertinoApp(
757
        builder: (BuildContext context, Widget? navigator) {
758 759
          return Directionality(
            textDirection: TextDirection.rtl,
760
            child: navigator!,
761 762 763 764 765 766 767 768 769 770
          );
        },
        home: scaffoldForNavBar(null),
      ),
    );

    tester
        .state<NavigatorState>(find.byType(Navigator))
        .push(CupertinoPageRoute<void>(
          title: 'Page 1',
771
          builder: (BuildContext context) => scaffoldForNavBar(null)!,
772 773 774 775 776
        ));

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

777
    final Finder backChevron = flying(tester, find.text(String.fromCharCode(CupertinoIcons.back.codePoint)));
778 779 780 781 782 783 784 785 786 787 788

    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),
789 790
      const Offset(
        687.163941725296126606,
791 792
        7.0,
      ),
793 794
    );

795
    await tester.pump(const Duration(milliseconds: 200));
796
    checkOpacity(tester, backChevron, 0.09497911669313908);
797 798
    expect(
      tester.getTopRight(backChevron),
799 800
      const Offset(
        743.538140317557690651,
801 802
        7.0,
      ),
803 804 805
    );
  });

806
  testWidgetsWithLeakTracking('Back chevron fades out and in when both pages have it', (WidgetTester tester) async {
807 808 809 810
    await startTransitionBetween(tester, fromTitle: 'Page 1');

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

811
    final Finder backChevrons = flying(tester, find.text(String.fromCharCode(CupertinoIcons.back.codePoint)));
812 813 814 815 816 817

    expect(
      backChevrons,
      findsNWidgets(2),
    );

818
    checkOpacity(tester, backChevrons.first, 0.9280824661254883);
819 820
    checkOpacity(tester, backChevrons.last, 0.0);
    // Both overlap at the same place.
821 822
    expect(tester.getTopLeft(backChevrons.first), const Offset(14.0, 7.0));
    expect(tester.getTopLeft(backChevrons.last), const Offset(14.0, 7.0));
823

824
    await tester.pump(const Duration(milliseconds: 200));
825
    checkOpacity(tester, backChevrons.first, 0.0);
826
    checkOpacity(tester, backChevrons.last, 0.4604858811944723);
827
    // Still in the same place.
828 829
    expect(tester.getTopLeft(backChevrons.first), const Offset(14.0, 7.0));
    expect(tester.getTopLeft(backChevrons.last), const Offset(14.0, 7.0));
830 831
  });

832
  testWidgetsWithLeakTracking('Bottom middle just fades if top page has a custom leading', (WidgetTester tester) async {
833 834 835 836 837 838 839 840 841 842 843 844 845 846
    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);

847
    checkOpacity(tester, flying(tester, find.text('Page 1')), 0.9404401779174805);
848 849 850 851 852 853 854

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

855
    await tester.pump(const Duration(milliseconds: 200));
856 857 858 859 860 861 862
    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),
    );
  });

863
  testWidgetsWithLeakTracking('Bottom leading fades in place', (WidgetTester tester) async {
864 865 866 867 868 869 870 871 872 873 874
    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);

875
    checkOpacity(tester, flying(tester, find.text('custom')), 0.8948725312948227);
876 877 878 879 880 881 882 883 884 885 886 887 888
    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),
    );
  });

889
  testWidgetsWithLeakTracking('Bottom trailing fades in place', (WidgetTester tester) async {
890 891 892 893 894 895 896 897 898 899 900
    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);

901
    checkOpacity(tester, flying(tester, find.text('custom')), 0.9280824661254883);
902 903
    expect(
      tester.getTopLeft(flying(tester, find.text('custom'))),
904 905
      const Offset(
        684.459999084472656250,
906 907
        13.5,
      ),
908 909 910 911 912 913
    );

    await tester.pump(const Duration(milliseconds: 150));
    checkOpacity(tester, flying(tester, find.text('custom')), 0.0);
    expect(
      tester.getTopLeft(flying(tester, find.text('custom'))),
914 915
      const Offset(
        684.459999084472656250,
916 917
        13.5,
      ),
918 919 920
    );
  });

921
  testWidgetsWithLeakTracking('Bottom back label fades and slides to the left', (WidgetTester tester) async {
922 923 924 925 926 927 928 929 930
    await startTransitionBetween(
      tester,
      fromTitle: 'Page 1',
      toTitle: 'Page 2',
    );

    await tester.pump(const Duration(milliseconds: 500));
    tester
        .state<NavigatorState>(find.byType(Navigator))
931
        .push(CupertinoPageRoute<void>(
932
          title: 'Page 3',
933
          builder: (BuildContext context) => scaffoldForNavBar(null)!,
934 935 936 937 938 939 940 941 942
        ));

    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.
943
    checkOpacity(tester, flying(tester, find.text('Page 1')), 0.7952219992876053);
944 945
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 1'))),
946 947
      const Offset(
        41.3003370761871337891,
948 949
        13.5,
      ),
950 951
    );

952
    await tester.pump(const Duration(milliseconds: 200));
953 954 955
    checkOpacity(tester, flying(tester, find.text('Page 1')), 0.0);
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 1'))),
956 957
      const Offset(
        -258.642192125320434570,
958 959
        13.5,
      ),
960 961 962
    );
  });

963
  testWidgetsWithLeakTracking('Bottom back label fades and slides to the right in RTL', (WidgetTester tester) async {
964 965 966 967 968 969 970 971 972 973 974 975
    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',
976
          builder: (BuildContext context) => scaffoldForNavBar(null)!,
977 978 979 980 981 982 983 984 985
        ));

    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.
986
    checkOpacity(tester, flying(tester, find.text('Page 1')), 0.7952219992876053);
987 988
    expect(
      tester.getTopRight(flying(tester, find.text('Page 1'))),
989 990
      const Offset(
        758.699662923812866211,
991 992
        13.5,
      ),
993 994
    );

995
    await tester.pump(const Duration(milliseconds: 200));
996 997 998 999
    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.
1000 1001
      const Offset(
        1058.64219212532043457,
1002 1003
        13.5,
      ),
1004 1005 1006
    );
  });

1007
  testWidgetsWithLeakTracking('Bottom large title moves to top back label', (WidgetTester tester) async {
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
    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));

1021
    checkOpacity(tester, flying(tester, find.text('Page 1')).first, 0.9280824661254883);
1022
    checkOpacity(tester, flying(tester, find.text('Page 1')).last, 0.0);
1023

1024 1025
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 1')).first),
1026 1027
      const Offset(
        16.9155227761479522997,
1028 1029
        52.73951627314091,
      ),
1030 1031 1032
    );
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 1')).last),
1033 1034
      const Offset(
        16.9155227761479522997,
1035 1036
        52.73951627314091,
      ),
1037 1038
    );

1039
    await tester.pump(const Duration(milliseconds: 200));
1040
    checkOpacity(tester, flying(tester, find.text('Page 1')).first, 0.0);
1041
    checkOpacity(tester, flying(tester, find.text('Page 1')).last, 0.4604858811944723);
1042

1043 1044
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 1')).first),
1045 1046
      const Offset(
        43.6029094262710827934,
1047 1048
        22.49655644595623,
      ),
1049 1050 1051
    );
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 1')).last),
1052 1053
      const Offset(
        43.6029094262710827934,
1054 1055
        22.49655644595623,
      ),
1056 1057 1058
    );
  });

1059
  testWidgetsWithLeakTracking('Long title turns into the word back mid transition', (WidgetTester tester) async {
1060 1061 1062 1063 1064 1065 1066 1067 1068
    await startTransitionBetween(
      tester,
      from: const CupertinoSliverNavigationBar(),
      fromTitle: 'A title too long to fit',
      toTitle: 'Page 2',
    );

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

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

1073
    checkOpacity(tester, flying(tester, find.text('A title too long to fit')), 0.9280824661254883);
1074 1075 1076
    checkOpacity(tester, flying(tester, find.text('Back')), 0.0);
    expect(
      tester.getTopLeft(flying(tester, find.text('A title too long to fit'))),
1077 1078
      const Offset(
        16.9155227761479522997,
1079 1080
        52.73951627314091,
      ),
1081 1082 1083
    );
    expect(
      tester.getTopLeft(flying(tester, find.text('Back'))),
1084 1085
      const Offset(
        16.9155227761479522997,
1086 1087
        52.73951627314091,
      ),
1088 1089
    );

1090
    await tester.pump(const Duration(milliseconds: 200));
1091
    checkOpacity(tester, flying(tester, find.text('A title too long to fit')), 0.0);
1092
    checkOpacity(tester, flying(tester, find.text('Back')), 0.4604858811944723);
1093 1094
    expect(
      tester.getTopLeft(flying(tester, find.text('A title too long to fit'))),
1095 1096
      const Offset(
        43.6029094262710827934,
1097 1098
        22.49655644595623,
      ),
1099 1100 1101
    );
    expect(
      tester.getTopLeft(flying(tester, find.text('Back'))),
1102 1103
      const Offset(
        43.6029094262710827934,
1104 1105
        22.49655644595623,
      ),
1106 1107 1108
    );
  });

1109
  testWidgetsWithLeakTracking('Bottom large title and top back label transitions their font', (WidgetTester tester) async {
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
    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);
1122
    expect(bottomLargeTitle.text.style!.color, const Color(0xff000306));
1123 1124
    expect(bottomLargeTitle.text.style!.fontWeight, FontWeight.w700);
    expect(bottomLargeTitle.text.style!.fontFamily, '.SF Pro Display');
1125
    expect(bottomLargeTitle.text.style!.letterSpacing, moreOrLessEquals(0.38890619069337845));
1126 1127 1128 1129

    // The top back label is styled exactly the same way.
    final RenderParagraph topBackLabel =
        tester.renderObject(flying(tester, find.text('Page 1')).last);
1130
    expect(topBackLabel.text.style!.color, const Color(0xff000306));
1131 1132
    expect(topBackLabel.text.style!.fontWeight, FontWeight.w700);
    expect(topBackLabel.text.style!.fontFamily, '.SF Pro Display');
1133
    expect(topBackLabel.text.style!.letterSpacing, moreOrLessEquals(0.38890619069337845));
1134 1135 1136

    // Move animation further a bit.
    await tester.pump(const Duration(milliseconds: 200));
1137 1138
    expect(bottomLargeTitle.text.style!.color, const Color(0xff005ec5));
    expect(bottomLargeTitle.text.style!.fontWeight, FontWeight.w500);
1139
    expect(bottomLargeTitle.text.style!.fontFamily, '.SF Pro Text');
1140
    expect(bottomLargeTitle.text.style!.letterSpacing, moreOrLessEquals(-0.2259759941697121));
1141

1142 1143
    expect(topBackLabel.text.style!.color, const Color(0xff005ec5));
    expect(topBackLabel.text.style!.fontWeight, FontWeight.w500);
1144
    expect(topBackLabel.text.style!.fontFamily, '.SF Pro Text');
1145
    expect(topBackLabel.text.style!.letterSpacing, moreOrLessEquals(-0.2259759941697121));
1146 1147
  });

1148
  testWidgetsWithLeakTracking('Top middle fades in and slides in from the right', (WidgetTester tester) async {
1149 1150 1151 1152 1153 1154 1155 1156 1157
    await startTransitionBetween(
      tester,
      toTitle: 'Page 2',
    );

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

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

1158
    checkOpacity(tester, flying(tester, find.text('Page 2')), 0.0);
1159 1160
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 2'))),
1161 1162
      const Offset(
        739.940336465835571289,
1163 1164
        13.5,
      ),
1165 1166 1167 1168
    );

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

1169
    checkOpacity(tester, flying(tester, find.text('Page 2')), 0.29867843724787235);
1170 1171
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 2'))),
1172 1173
      const Offset(
        504.880443334579467773,
1174 1175
        13.5,
      ),
1176 1177 1178
    );
  });

1179
  testWidgetsWithLeakTracking('Top middle never changes size during the animation', (WidgetTester tester) async {
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
    await tester.binding.setSurfaceSize(const Size(1080.0 / 2.75, 600));
    addTearDown(() async {
      await tester.binding.setSurfaceSize(const Size(800.0, 600.0));
    });

    await startTransitionBetween(
      tester,
      toTitle: 'Page 2',
    );

    Size? previousSize;

    for (int i = 0; i < 150; i++) {
      await tester.pump(const Duration(milliseconds: 1));
      expect(flying(tester, find.text('Page 2')), findsOneWidget);
      final Size size = tester.getSize(flying(tester, find.text('Page 2')));
1196
      if (previousSize != null) {
1197
        expect(size, previousSize);
1198
      }
1199 1200 1201 1202
      previousSize = size;
    }
  });

1203
  testWidgetsWithLeakTracking('Top middle fades in and slides in from the left in RTL', (WidgetTester tester) async {
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
    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'))),
1217 1218
      const Offset(
        60.0596635341644287109,
1219 1220
        13.5,
      ),
1221 1222 1223 1224
    );

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

1225
    checkOpacity(tester, flying(tester, find.text('Page 2')), 0.29867843724787235);
1226 1227
    expect(
      tester.getTopRight(flying(tester, find.text('Page 2'))),
1228 1229
      const Offset(
        295.119556665420532227,
1230 1231
        13.5,
      ),
1232 1233 1234
    );
  });

1235
  testWidgetsWithLeakTracking('Top large title fades in and slides in from the right', (WidgetTester tester) async {
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
    await startTransitionBetween(
      tester,
      to: const CupertinoSliverNavigationBar(),
      toTitle: 'Page 2',
    );

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

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

1246
    checkOpacity(tester, flying(tester, find.text('Page 2')), 0.0);
1247 1248
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 2'))),
1249
      const Offset(795.4206738471985, 54.0),
1250 1251 1252 1253
    );

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

1254
    checkOpacity(tester, flying(tester, find.text('Page 2')), 0.2601277381181717);
1255 1256
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 2'))),
1257
      const Offset(325.3008875846863, 54.0),
1258 1259 1260
    );
  });

1261
  testWidgetsWithLeakTracking('Top large title fades in and slides in from the left in RTL', (WidgetTester tester) async {
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
    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'))),
1276
      const Offset(4.579326152801514, 54.0),
1277 1278 1279 1280
    );

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

1281
    checkOpacity(tester, flying(tester, find.text('Page 2')), 0.2601277381181717);
1282 1283
    expect(
      tester.getTopRight(flying(tester, find.text('Page 2'))),
1284
      const Offset(474.6991124153137, 54.0),
1285 1286 1287
    );
  });

1288
  testWidgetsWithLeakTracking('Components are not unnecessarily rebuilt during transitions', (WidgetTester tester) async {
1289 1290 1291 1292
    int bottomBuildTimes = 0;
    int topBuildTimes = 0;
    await startTransitionBetween(
      tester,
1293 1294
      from: CupertinoNavigationBar(
        middle: Builder(builder: (BuildContext context) {
1295 1296 1297 1298
          bottomBuildTimes++;
          return const Text('Page 1');
        }),
      ),
1299 1300
      to: CupertinoSliverNavigationBar(
        largeTitle: Builder(builder: (BuildContext context) {
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
          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
1332

1333
  testWidgetsWithLeakTracking('Back swipe gesture transitions', (WidgetTester tester) async {
xster's avatar
xster committed
1334 1335 1336 1337 1338 1339 1340
    await startTransitionBetween(
      tester,
      fromTitle: 'Page 1',
      toTitle: 'Page 2',
    );

    // Go to the next page.
1341
    await tester.pump(const Duration(milliseconds: 600));
xster's avatar
xster committed
1342 1343

    // Start the gesture at the edge of the screen.
1344
    final TestGesture gesture = await tester.startGesture(const Offset(5.0, 200.0));
xster's avatar
xster committed
1345 1346 1347 1348 1349 1350 1351 1352 1353 1354
    // 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'))),
1355 1356
      const Offset(
        353.810205429792404175,
1357 1358
        13.5,
      ),
xster's avatar
xster committed
1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372
    );

    // 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'))),
1373 1374
      const Offset(
        655.435583114624023438,
1375 1376
        13.5,
      ),
xster's avatar
xster committed
1377 1378 1379 1380
    );
    await tester.pump(const Duration(milliseconds: 50));
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 2'))),
1381 1382
      const Offset(
        749.863556146621704102,
1383 1384
        13.5,
      ),
xster's avatar
xster committed
1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395
    );

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

1396
  testWidgetsWithLeakTracking('textScaleFactor is set to 1.0 on transition', (WidgetTester tester) async {
1397 1398 1399 1400 1401 1402 1403
    await startTransitionBetween(tester, fromTitle: 'Page 1', textScale: 99);

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

    expect(tester.firstWidget<RichText>(flying(tester, find.byType(RichText))).textScaleFactor, 1);
  });

1404
  testWidgetsWithLeakTracking('Back swipe gesture cancels properly with transition', (WidgetTester tester) async {
xster's avatar
xster committed
1405 1406 1407 1408 1409 1410 1411
    await startTransitionBetween(
      tester,
      fromTitle: 'Page 1',
      toTitle: 'Page 2',
    );

    // Go to the next page.
1412
    await tester.pump(const Duration(milliseconds: 600));
xster's avatar
xster committed
1413 1414

    // Start the gesture at the edge of the screen.
1415
    final TestGesture gesture = await tester.startGesture(const Offset(5.0, 200.0));
xster's avatar
xster committed
1416 1417 1418 1419 1420 1421 1422 1423 1424 1425
    // 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'))),
1426 1427
      const Offset(
        353.810205429792404175,
1428 1429
        13.5,
      ),
xster's avatar
xster committed
1430 1431 1432 1433 1434 1435 1436 1437
    );

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

    // Transition continues from the point we let off.
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 2'))),
1438 1439
      const Offset(
        353.810205429792404175,
1440 1441
        13.5,
      ),
xster's avatar
xster committed
1442 1443 1444 1445
    );
    await tester.pump(const Duration(milliseconds: 50));
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 2'))),
1446 1447
      const Offset(
        350.231143206357955933,
1448 1449
        13.5,
      ),
xster's avatar
xster committed
1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460
    );

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