nav_bar_transition_test.dart 42.5 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
@TestOn('!chrome')
6 7 8 9 10 11
// TODO(gspencergoog): Remove this tag once this test's state leaks/test
// dependencies have been fixed.
// https://github.com/flutter/flutter/issues/85160
// Fails with "flutter test --test-randomize-ordering-seed=456"
@Tags(<String>['no-shuffle'])

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

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 25
}) async {
  await tester.pumpWidget(
26
    CupertinoApp(
xster's avatar
xster committed
27
      theme: theme,
28
      builder: (BuildContext context, Widget? navigator) {
29 30
        return Directionality(
          textDirection: textDirection,
31
          child: navigator!,
32 33 34
        );
      },
      home: const Placeholder(),
35 36 37 38 39
    ),
  );

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

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

  tester
      .state<NavigatorState>(find.byType(Navigator))
50
      .push(CupertinoPageRoute<void>(
51
        title: toTitle,
52
        builder: (BuildContext context) => scaffoldForNavBar(to)!,
53 54 55 56 57
      ));

  await tester.pump();
}

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

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

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

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

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

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

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
  testWidgets('Bottom middle never changes size during the animation', (WidgetTester tester) async {
    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);
    }
  });

195
  testWidgets('Bottom middle and top back label transitions their font', (WidgetTester tester) async {
196 197 198 199 200 201 202 203
    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);
204 205 206 207
    expect(bottomMiddle.text.style!.color, const Color(0xff00050a));
    expect(bottomMiddle.text.style!.fontWeight, FontWeight.w600);
    expect(bottomMiddle.text.style!.fontFamily, '.SF Pro Text');
    expect(bottomMiddle.text.style!.letterSpacing, -0.41);
208

209
    checkOpacity(tester, flying(tester, find.text('Page 1')).first, 0.9004602432250977);
210 211 212 213 214

    // 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);
215 216 217 218
    expect(topBackLabel.text.style!.color, const Color(0xff00050a));
    expect(topBackLabel.text.style!.fontWeight, FontWeight.w600);
    expect(topBackLabel.text.style!.fontFamily, '.SF Pro Text');
    expect(topBackLabel.text.style!.letterSpacing, -0.41);
219 220 221 222 223

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

    // Move animation further a bit.
    await tester.pump(const Duration(milliseconds: 200));
224 225 226 227
    expect(bottomMiddle.text.style!.color, const Color(0xff006de4));
    expect(bottomMiddle.text.style!.fontWeight, FontWeight.w400);
    expect(bottomMiddle.text.style!.fontFamily, '.SF Pro Text');
    expect(bottomMiddle.text.style!.letterSpacing, -0.41);
228 229 230

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

231 232 233 234
    expect(topBackLabel.text.style!.color, const Color(0xff006de4));
    expect(topBackLabel.text.style!.fontWeight, FontWeight.w400);
    expect(topBackLabel.text.style!.fontFamily, '.SF Pro Text');
    expect(topBackLabel.text.style!.letterSpacing, -0.41);
235

236
    checkOpacity(tester, flying(tester, find.text('Page 1')).last, 0.7630139589309692);
237 238
  });

239 240 241 242 243 244
  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
245

246 247
    // Be mid-transition.
    await tester.pump(const Duration(milliseconds: 50));
xster's avatar
xster committed
248

249 250 251
    // The transition's stack is ordered. The bottom middle is inserted first.
    final RenderParagraph bottomMiddle =
        tester.renderObject(flying(tester, find.text('Page 1')).first);
252 253 254 255
    expect(bottomMiddle.text.style!.color, const Color(0xFFF4F9FF));
    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
256

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

259 260 261 262
    // 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);
263 264 265 266
    expect(topBackLabel.text.style!.color, const Color(0xFFF4F9FF));
    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
267

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

270 271
    // Move animation further a bit.
    await tester.pump(const Duration(milliseconds: 200));
272 273 274 275
    expect(bottomMiddle.text.style!.color, const Color(0xFF2390FF));
    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
276

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

279 280 281 282
    expect(topBackLabel.text.style!.color, const Color(0xFF2390FF));
    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
283

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

287
  testWidgets('Fullscreen dialogs do not create heroes', (WidgetTester tester) async {
288
    await tester.pumpWidget(
289 290
      const CupertinoApp(
        home: Placeholder(),
291 292 293 294 295
      ),
    );

    tester
        .state<NavigatorState>(find.byType(Navigator))
296
        .push(CupertinoPageRoute<void>(
297
          title: 'Page 1',
298
          builder: (BuildContext context) => scaffoldForNavBar(null)!,
299 300 301 302 303 304 305
        ));

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

    tester
        .state<NavigatorState>(find.byType(Navigator))
306
        .push(CupertinoPageRoute<void>(
307 308
          title: 'Page 2',
          fullscreenDialog: true,
309
          builder: (BuildContext context) => scaffoldForNavBar(null)!,
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
        ));

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

345
  testWidgets('Popping mid-transition is symmetrical', (WidgetTester tester) async {
346 347 348 349 350 351 352 353 354
    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);
355
      expect(bottomMiddle.text.style!.color, const Color(0xff00050a));
356 357
      expect(
        tester.getTopLeft(flying(tester, find.text('Page 1')).first),
358
        const Offset(337.1953125, 13.5),
359 360 361 362 363 364
      );

      // 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);
365
      expect(topBackLabel.text.style!.color, const Color(0xff00050a));
366 367
      expect(
        tester.getTopLeft(flying(tester, find.text('Page 1')).last),
368
        const Offset(337.1953125, 13.5),
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
      );
    }

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

386
  testWidgets('Popping mid-transition is symmetrical RTL', (WidgetTester tester) async {
387 388 389 390 391 392 393 394 395 396 397 398 399
    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);
400
      expect(bottomMiddle.text.style!.color, const Color(0xff00050a));
401 402
      expect(
        tester.getTopLeft(flying(tester, find.text('Page 1')).first),
403
        const Offset(362.8046875, 13.5),
404 405 406 407 408 409
      );

      // 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);
410
      expect(topBackLabel.text.style!.color, const Color(0xff00050a));
411 412
      expect(
        tester.getTopLeft(flying(tester, find.text('Page 1')).last),
413
        const Offset(362.8046875, 13.5),
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
      );
    }

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

431
  testWidgets('There should be no global keys in the hero flight', (WidgetTester tester) async {
432 433 434 435 436 437 438 439 440 441 442 443 444 445
    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,
    );
  });

446
  testWidgets('Multiple nav bars tags do not conflict if in different navigators', (WidgetTester tester) async {
447 448 449 450 451 452 453
    await tester.pumpWidget(
      CupertinoApp(
        home: CupertinoTabScaffold(
          tabBar: CupertinoTabBar(
            items: const <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                icon: Icon(CupertinoIcons.search),
454
                label: 'Tab 1',
455 456 457
              ),
              BottomNavigationBarItem(
                icon: Icon(CupertinoIcons.settings),
458
                label: 'Tab 2',
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
              ),
            ],
          ),
          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(),
                            );
480
                          },
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
                        ));
                      },
                    ),
                  ),
                );
              },
            );
          },
        ),
      ),
    );

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

521
  testWidgets('Transition box grows to large title size', (WidgetTester tester) async {
522 523 524 525 526 527 528 529
    await startTransitionBetween(
      tester,
      fromTitle: 'Page 1',
      to: const CupertinoSliverNavigationBar(),
      toTitle: 'Page 2',
    );

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

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

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

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

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

545
  testWidgets('Large transition box shrinks to standard nav bar size', (WidgetTester tester) async {
546 547 548 549 550 551 552 553
    await startTransitionBetween(
      tester,
      from: const CupertinoSliverNavigationBar(),
      fromTitle: 'Page 1',
      toTitle: 'Page 2',
    );

    await tester.pump(const Duration(milliseconds: 50));
554
    checkBackgroundBoxHeight(tester, 93.765625);
555 556

    await tester.pump(const Duration(milliseconds: 50));
557
    checkBackgroundBoxHeight(tester, 83.6767258644104);
558 559

    await tester.pump(const Duration(milliseconds: 50));
560
    checkBackgroundBoxHeight(tester, 66.95932388305664);
561 562

    await tester.pump(const Duration(milliseconds: 50));
563
    checkBackgroundBoxHeight(tester, 55.66981500387192);
564 565

    await tester.pump(const Duration(milliseconds: 50));
566
    checkBackgroundBoxHeight(tester, 49.46662837266922);
567 568
  });

569
  testWidgets('Hero flight removed at the end of page transition', (WidgetTester tester) async {
570 571 572 573 574 575 576 577 578 579 580 581 582 583
    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);
  });

584
  testWidgets('Exact widget is reused to build inside the transition', (WidgetTester tester) async {
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
    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);
  });

600
  testWidgets('First appearance of back chevron fades in from the right', (WidgetTester tester) async {
601
    await tester.pumpWidget(
602
      CupertinoApp(
603 604 605 606 607 608
        home: scaffoldForNavBar(null),
      ),
    );

    tester
        .state<NavigatorState>(find.byType(Navigator))
609
        .push(CupertinoPageRoute<void>(
610
          title: 'Page 1',
611
          builder: (BuildContext context) => scaffoldForNavBar(null)!,
612 613 614 615 616
        ));

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

617
    final Finder backChevron = flying(tester, find.text(String.fromCharCode(CupertinoIcons.back.codePoint)));
618 619 620 621 622 623 624 625

    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);
626
    expect(tester.getTopLeft(backChevron), const Offset(86.734375, 7.0));
627 628

    await tester.pump(const Duration(milliseconds: 150));
629
    checkOpacity(tester, backChevron, 0.09497911669313908);
630
    expect(tester.getTopLeft(backChevron), const Offset(31.055883467197418, 7.0));
631 632
  });

633
  testWidgets('First appearance of back chevron fades in from the left in RTL', (WidgetTester tester) async {
634 635
    await tester.pumpWidget(
      CupertinoApp(
636
        builder: (BuildContext context, Widget? navigator) {
637 638
          return Directionality(
            textDirection: TextDirection.rtl,
639
            child: navigator!,
640 641 642 643 644 645 646 647 648 649
          );
        },
        home: scaffoldForNavBar(null),
      ),
    );

    tester
        .state<NavigatorState>(find.byType(Navigator))
        .push(CupertinoPageRoute<void>(
          title: 'Page 1',
650
          builder: (BuildContext context) => scaffoldForNavBar(null)!,
651 652 653 654 655
        ));

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

656
    final Finder backChevron = flying(tester, find.text(String.fromCharCode(CupertinoIcons.back.codePoint)));
657 658 659 660 661 662 663 664 665 666 667

    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),
668
      const Offset(687.265625, 7.0),
669 670 671
    );

    await tester.pump(const Duration(milliseconds: 150));
672
    checkOpacity(tester, backChevron, 0.09497911669313908);
673 674
    expect(
      tester.getTopRight(backChevron),
675
      const Offset(742.9441165328026, 7.0),
676 677 678
    );
  });

679
  testWidgets('Back chevron fades out and in when both pages have it', (WidgetTester tester) async {
680 681 682 683
    await startTransitionBetween(tester, fromTitle: 'Page 1');

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

684
    final Finder backChevrons = flying(tester, find.text(String.fromCharCode(CupertinoIcons.back.codePoint)));
685 686 687 688 689 690

    expect(
      backChevrons,
      findsNWidgets(2),
    );

691
    checkOpacity(tester, backChevrons.first, 0.8833301812410355);
692 693
    checkOpacity(tester, backChevrons.last, 0.0);
    // Both overlap at the same place.
694 695
    expect(tester.getTopLeft(backChevrons.first), const Offset(14.0, 7.0));
    expect(tester.getTopLeft(backChevrons.last), const Offset(14.0, 7.0));
696 697 698

    await tester.pump(const Duration(milliseconds: 150));
    checkOpacity(tester, backChevrons.first, 0.0);
699
    checkOpacity(tester, backChevrons.last, 0.4604858811944723);
700
    // Still in the same place.
701 702
    expect(tester.getTopLeft(backChevrons.first), const Offset(14.0, 7.0));
    expect(tester.getTopLeft(backChevrons.last), const Offset(14.0, 7.0));
703 704
  });

705
  testWidgets('Bottom middle just fades if top page has a custom leading', (WidgetTester tester) async {
706 707 708 709 710 711 712 713 714 715 716 717 718 719
    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);

720
    checkOpacity(tester, flying(tester, find.text('Page 1')), 0.9004602432250977);
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747

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

748
    checkOpacity(tester, flying(tester, find.text('custom')), 0.828093871474266);
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
    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);

774
    checkOpacity(tester, flying(tester, find.text('custom')), 0.8833301812410355);
775 776
    expect(
      tester.getTopLeft(flying(tester, find.text('custom'))),
xster's avatar
xster committed
777
      const Offset(684.0, 13.5),
778 779 780 781 782 783
    );

    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
784
      const Offset(684.0, 13.5),
785 786 787
    );
  });

788
  testWidgets('Bottom back label fades and slides to the left', (WidgetTester tester) async {
789 790 791 792 793 794 795 796 797
    await startTransitionBetween(
      tester,
      fromTitle: 'Page 1',
      toTitle: 'Page 2',
    );

    await tester.pump(const Duration(milliseconds: 500));
    tester
        .state<NavigatorState>(find.byType(Navigator))
798
        .push(CupertinoPageRoute<void>(
799
          title: 'Page 3',
800
          builder: (BuildContext context) => scaffoldForNavBar(null)!,
801 802 803 804 805 806 807 808 809
        ));

    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.
810
    checkOpacity(tester, flying(tester, find.text('Page 1')), 0.6697911769151688);
811 812
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 1'))),
813
      const Offset(34.8125, 13.5),
814 815 816 817 818 819
    );

    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'))),
820
      const Offset(-258.2321922779083, 13.5),
821 822 823
    );
  });

824
  testWidgets('Bottom back label fades and slides to the right in RTL', (WidgetTester tester) async {
825 826 827 828 829 830 831 832 833 834 835 836
    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',
837
          builder: (BuildContext context) => scaffoldForNavBar(null)!,
838 839 840 841 842 843 844 845 846
        ));

    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.
847
    checkOpacity(tester, flying(tester, find.text('Page 1')), 0.6697911769151688);
848 849
    expect(
      tester.getTopRight(flying(tester, find.text('Page 1'))),
850
      const Offset(765.1875, 13.5),
851 852 853 854 855 856 857
    );

    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.
858
      const Offset(1058.2321922779083, 13.5),
859 860 861
    );
  });

862
  testWidgets('Bottom large title moves to top back label', (WidgetTester tester) async {
863 864 865 866 867 868 869 870 871 872 873 874 875
    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));

876
    checkOpacity(tester, flying(tester, find.text('Page 1')).first, 0.8833301812410355);
877 878 879
    checkOpacity(tester, flying(tester, find.text('Page 1')).last, 0.0);
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 1')).first),
880
      const Offset(17.546875, 52.39453125),
881 882 883
    );
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 1')).last),
884
      const Offset(17.546875, 52.39453125),
885 886 887 888
    );

    await tester.pump(const Duration(milliseconds: 150));
    checkOpacity(tester, flying(tester, find.text('Page 1')).first, 0.0);
889
    checkOpacity(tester, flying(tester, find.text('Page 1')).last, 0.4604858811944723);
890 891
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 1')).first),
892
      const Offset(43.92089730501175, 22.49655644595623),
893 894 895
    );
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 1')).last),
896
      const Offset(43.92089730501175, 22.49655644595623),
897 898 899
    );
  });

900
  testWidgets('Long title turns into the word back mid transition', (WidgetTester tester) async {
901 902 903 904 905 906 907 908 909
    await startTransitionBetween(
      tester,
      from: const CupertinoSliverNavigationBar(),
      fromTitle: 'A title too long to fit',
      toTitle: 'Page 2',
    );

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

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

914
    checkOpacity(tester, flying(tester, find.text('A title too long to fit')), 0.8833301812410355);
915 916 917
    checkOpacity(tester, flying(tester, find.text('Back')), 0.0);
    expect(
      tester.getTopLeft(flying(tester, find.text('A title too long to fit'))),
918
      const Offset(17.546875, 52.39453125),
919 920 921
    );
    expect(
      tester.getTopLeft(flying(tester, find.text('Back'))),
922
      const Offset(17.546875, 52.39453125),
923 924 925
    );

    await tester.pump(const Duration(milliseconds: 150));
926
    checkOpacity(tester, flying(tester, find.text('A title too long to fit')), 0.0);
927
    checkOpacity(tester, flying(tester, find.text('Back')), 0.4604858811944723);
928 929
    expect(
      tester.getTopLeft(flying(tester, find.text('A title too long to fit'))),
930
      const Offset(43.92089730501175, 22.49655644595623),
931 932 933
    );
    expect(
      tester.getTopLeft(flying(tester, find.text('Back'))),
934
      const Offset(43.92089730501175, 22.49655644595623),
935 936 937
    );
  });

938
  testWidgets('Bottom large title and top back label transitions their font', (WidgetTester tester) async {
939 940 941 942 943 944 945 946 947 948 949 950
    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);
951 952 953 954
    expect(bottomLargeTitle.text.style!.color, const Color(0xff00050a));
    expect(bottomLargeTitle.text.style!.fontWeight, FontWeight.w700);
    expect(bottomLargeTitle.text.style!.fontFamily, '.SF Pro Display');
    expect(bottomLargeTitle.text.style!.letterSpacing, moreOrLessEquals(0.374765625));
955 956 957 958

    // The top back label is styled exactly the same way.
    final RenderParagraph topBackLabel =
        tester.renderObject(flying(tester, find.text('Page 1')).last);
959 960 961 962
    expect(topBackLabel.text.style!.color, const Color(0xff00050a));
    expect(topBackLabel.text.style!.fontWeight, FontWeight.w700);
    expect(topBackLabel.text.style!.fontFamily, '.SF Pro Display');
    expect(topBackLabel.text.style!.letterSpacing, moreOrLessEquals(0.374765625));
963 964 965

    // Move animation further a bit.
    await tester.pump(const Duration(milliseconds: 200));
966 967 968 969 970 971 972 973 974
    expect(bottomLargeTitle.text.style!.color, const Color(0xff006de4));
    expect(bottomLargeTitle.text.style!.fontWeight, FontWeight.w400);
    expect(bottomLargeTitle.text.style!.fontFamily, '.SF Pro Text');
    expect(bottomLargeTitle.text.style!.letterSpacing, moreOrLessEquals(-0.32379547566175454));

    expect(topBackLabel.text.style!.color, const Color(0xff006de4));
    expect(topBackLabel.text.style!.fontWeight, FontWeight.w400);
    expect(topBackLabel.text.style!.fontFamily, '.SF Pro Text');
    expect(topBackLabel.text.style!.letterSpacing, moreOrLessEquals(-0.32379547566175454));
975 976
  });

977
  testWidgets('Top middle fades in and slides in from the right', (WidgetTester tester) async {
978 979 980 981 982 983 984 985 986
    await startTransitionBetween(
      tester,
      toTitle: 'Page 2',
    );

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

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

987
    checkOpacity(tester, flying(tester, find.text('Page 2')), 0.0);
988 989
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 2'))),
990
      const Offset(732.8125, 13.5),
991 992 993 994
    );

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

995
    checkOpacity(tester, flying(tester, find.text('Page 2')), 0.5555618554353714);
996 997
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 2'))),
998
      const Offset(439.7678077220917, 13.5),
999 1000 1001
    );
  });

1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
  testWidgets('Top middle never changes size during the animation', (WidgetTester tester) async {
    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')));
      if (previousSize != null)
        expect(size, previousSize);
      previousSize = size;
    }
  });

1025
  testWidgets('Top middle fades in and slides in from the left in RTL', (WidgetTester tester) async {
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
    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'))),
1039
      const Offset(67.1875, 13.5),
1040 1041 1042 1043
    );

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

1044
    checkOpacity(tester, flying(tester, find.text('Page 2')), 0.5555618554353714);
1045 1046
    expect(
      tester.getTopRight(flying(tester, find.text('Page 2'))),
1047
      const Offset(360.2321922779083, 13.5),
1048 1049 1050
    );
  });

1051
  testWidgets('Top large title fades in and slides in from the right', (WidgetTester tester) async {
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
    await startTransitionBetween(
      tester,
      to: const CupertinoSliverNavigationBar(),
      toTitle: 'Page 2',
    );

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

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

1062
    checkOpacity(tester, flying(tester, find.text('Page 2')), 0.0);
1063 1064
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 2'))),
1065
      const Offset(781.625, 54.0),
1066 1067 1068 1069
    );

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

1070
    checkOpacity(tester, flying(tester, find.text('Page 2')), 0.5292819738388062);
1071 1072
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 2'))),
1073
      const Offset(195.53561544418335, 54.0),
1074 1075 1076
    );
  });

1077
  testWidgets('Top large title fades in and slides in from the left in RTL', (WidgetTester tester) async {
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
    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'))),
1092
      const Offset(18.375, 54.0),
1093 1094 1095 1096
    );

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

1097
    checkOpacity(tester, flying(tester, find.text('Page 2')), 0.5292819738388062);
1098 1099
    expect(
      tester.getTopRight(flying(tester, find.text('Page 2'))),
1100
      const Offset(604.4643845558167, 54.0),
1101 1102 1103
    );
  });

1104
  testWidgets('Components are not unnecessarily rebuilt during transitions', (WidgetTester tester) async {
1105 1106 1107 1108
    int bottomBuildTimes = 0;
    int topBuildTimes = 0;
    await startTransitionBetween(
      tester,
1109 1110
      from: CupertinoNavigationBar(
        middle: Builder(builder: (BuildContext context) {
1111 1112 1113 1114
          bottomBuildTimes++;
          return const Text('Page 1');
        }),
      ),
1115 1116
      to: CupertinoSliverNavigationBar(
        largeTitle: Builder(builder: (BuildContext context) {
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
          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
1148

1149
  testWidgets('Back swipe gesture transitions', (WidgetTester tester) async {
xster's avatar
xster committed
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
    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
1171
      const Offset(353.5802058875561, 13.5),
xster's avatar
xster committed
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
    );

    // 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
1186
      const Offset(655.2055835723877, 13.5),
xster's avatar
xster committed
1187 1188 1189 1190
    );
    await tester.pump(const Duration(milliseconds: 50));
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 2'))),
1191
      const Offset(749.6335566043854, 13.5),
xster's avatar
xster committed
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
    );

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

1203
  testWidgets('Back swipe gesture cancels properly with transition', (WidgetTester tester) async {
xster's avatar
xster committed
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
    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
1225
      const Offset(353.5802058875561, 13.5),
xster's avatar
xster committed
1226 1227 1228 1229 1230 1231 1232 1233
    );

    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
1234
      const Offset(353.5802058875561, 13.5),
xster's avatar
xster committed
1235 1236 1237 1238
    );
    await tester.pump(const Duration(milliseconds: 50));
    expect(
      tester.getTopLeft(flying(tester, find.text('Page 2'))),
1239
      const Offset(350.0011436641216, 13.5),
xster's avatar
xster committed
1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
    );

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