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

import 'package:flutter/cupertino.dart';
6 7
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
8
import 'package:flutter_test/flutter_test.dart';
9
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
10

11
import '../image_data.dart';
12
import '../rendering/rendering_tester.dart' show TestCallbackPainter;
13
import '../widgets/navigator_utils.dart';
14

15
late List<int> selectedTabs;
16

17
class MockCupertinoTabController extends CupertinoTabController {
18
  MockCupertinoTabController({ required super.initialIndex });
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

  bool isDisposed = false;
  int numOfListeners = 0;

  @override
  void addListener(VoidCallback listener) {
    numOfListeners++;
    super.addListener(listener);
  }

  @override
  void removeListener(VoidCallback listener) {
    numOfListeners--;
    super.removeListener(listener);
  }

  @override
  void dispose() {
    isDisposed = true;
    super.dispose();
  }
}

42 43 44 45 46
void main() {
  setUp(() {
    selectedTabs = <int>[];
  });

47 48
  BottomNavigationBarItem tabGenerator(int index) {
    return BottomNavigationBarItem(
49
      icon: ImageIcon(MemoryImage(Uint8List.fromList(kTransparentImage))),
50
      label: 'Tab ${index + 1}',
51 52 53
    );
  }

54
  testWidgetsWithLeakTracking('Tab switching', (WidgetTester tester) async {
55 56 57
    final List<int> tabsPainted = <int>[];

    await tester.pumpWidget(
58 59
      CupertinoApp(
        home: CupertinoTabScaffold(
xster's avatar
xster committed
60 61
          tabBar: _buildTabBar(),
          tabBuilder: (BuildContext context, int index) {
62 63
            return CustomPaint(
              painter: TestCallbackPainter(
64
                onPaint: () { tabsPainted.add(index); },
65
              ),
66
              child: Text('Page ${index + 1}'),
xster's avatar
xster committed
67 68 69
            );
          },
        ),
70 71 72
      ),
    );

73
    expect(tabsPainted, const <int>[0]);
74 75 76 77
    RichText tab1 = tester.widget(find.descendant(
      of: find.text('Tab 1'),
      matching: find.byType(RichText),
    ));
78
    expect(tab1.text.style!.color, CupertinoColors.activeBlue);
79 80 81 82
    RichText tab2 = tester.widget(find.descendant(
      of: find.text('Tab 2'),
      matching: find.byType(RichText),
    ));
83
    expect(tab2.text.style!.color!.value, 0xFF999999);
84 85 86 87

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

88
    expect(tabsPainted, const <int>[0, 1]);
89 90 91 92
    tab1 = tester.widget(find.descendant(
      of: find.text('Tab 1'),
      matching: find.byType(RichText),
    ));
93
    expect(tab1.text.style!.color!.value, 0xFF999999);
94 95 96 97
    tab2 = tester.widget(find.descendant(
      of: find.text('Tab 2'),
      matching: find.byType(RichText),
    ));
98
    expect(tab2.text.style!.color, CupertinoColors.activeBlue);
99 100 101 102

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

103
    expect(tabsPainted, const <int>[0, 1, 0]);
104
    // CupertinoTabBar's onTap callbacks are passed on.
105
    expect(selectedTabs, const <int>[1, 0]);
106 107
  });

108
  testWidgetsWithLeakTracking('Tabs are lazy built and moved offstage when inactive', (WidgetTester tester) async {
109 110 111
    final List<int> tabsBuilt = <int>[];

    await tester.pumpWidget(
112 113
      CupertinoApp(
        home: CupertinoTabScaffold(
xster's avatar
xster committed
114 115 116
          tabBar: _buildTabBar(),
          tabBuilder: (BuildContext context, int index) {
            tabsBuilt.add(index);
117
            return Text('Page ${index + 1}');
xster's avatar
xster committed
118 119
          },
        ),
120 121 122
      ),
    );

123
    expect(tabsBuilt, const <int>[0]);
124 125 126 127 128 129 130
    expect(find.text('Page 1'), findsOneWidget);
    expect(find.text('Page 2'), findsNothing);

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

    // Both tabs are built but only one is onstage.
131
    expect(tabsBuilt, const <int>[0, 0, 1]);
132 133 134 135 136 137
    expect(find.text('Page 1', skipOffstage: false), isOffstage);
    expect(find.text('Page 2'), findsOneWidget);

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

138
    expect(tabsBuilt, const <int>[0, 0, 1, 0, 1]);
139 140 141
    expect(find.text('Page 1'), findsOneWidget);
    expect(find.text('Page 2', skipOffstage: false), isOffstage);
  });
142

143
  testWidgetsWithLeakTracking('Last tab gets focus', (WidgetTester tester) async {
144
    // 2 nodes for 2 tabs
145 146 147 148
    final List<FocusNode> focusNodes = <FocusNode>[
      FocusNode(debugLabel: 'Node 1'),
      FocusNode(debugLabel: 'Node 2'),
    ];
149 150 151
    for (final FocusNode focusNode in focusNodes) {
      addTearDown(focusNode.dispose);
    }
152 153

    await tester.pumpWidget(
154
      CupertinoApp(
155 156 157 158 159 160 161 162
        home: CupertinoTabScaffold(
          tabBar: _buildTabBar(),
          tabBuilder: (BuildContext context, int index) {
            return CupertinoTextField(
              focusNode: focusNodes[index],
              autofocus: true,
            );
          },
xster's avatar
xster committed
163
        ),
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
      ),
    );

    expect(focusNodes[0].hasFocus, isTrue);

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

    expect(focusNodes[0].hasFocus, isFalse);
    expect(focusNodes[1].hasFocus, isTrue);

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

    expect(focusNodes[0].hasFocus, isTrue);
    expect(focusNodes[1].hasFocus, isFalse);
  });

182
  testWidgetsWithLeakTracking('Do not affect focus order in the route', (WidgetTester tester) async {
183
    final List<FocusNode> focusNodes = <FocusNode>[
184 185 186 187
      FocusNode(debugLabel: 'Node 1'),
      FocusNode(debugLabel: 'Node 2'),
      FocusNode(debugLabel: 'Node 3'),
      FocusNode(debugLabel: 'Node 4'),
188
    ];
189 190 191
    for (final FocusNode focusNode in focusNodes) {
      addTearDown(focusNode.dispose);
    }
192 193

    await tester.pumpWidget(
194
      CupertinoApp(
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
        home: CupertinoTabScaffold(
          tabBar: _buildTabBar(),
          tabBuilder: (BuildContext context, int index) {
            return Column(
              children: <Widget>[
                CupertinoTextField(
                  focusNode: focusNodes[index * 2],
                  placeholder: 'TextField 1',
                ),
                CupertinoTextField(
                  focusNode: focusNodes[index * 2 + 1],
                  placeholder: 'TextField 2',
                ),
              ],
            );
          },
xster's avatar
xster committed
211
        ),
212 213 214 215 216 217 218 219
      ),
    );

    expect(
      focusNodes.any((FocusNode node) => node.hasFocus),
      isFalse,
    );

220
    await tester.tap(find.widgetWithText(CupertinoTextField, 'TextField 2'));
221 222 223 224 225 226 227 228 229

    expect(
      focusNodes.indexOf(focusNodes.singleWhere((FocusNode node) => node.hasFocus)),
      1,
    );

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

230
    await tester.tap(find.widgetWithText(CupertinoTextField, 'TextField 1'));
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246

    expect(
      focusNodes.indexOf(focusNodes.singleWhere((FocusNode node) => node.hasFocus)),
      2,
    );

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

    // Upon going back to tab 1, the item it tab 1 that previously had the focus
    // (TextField 2) gets it back.
    expect(
      focusNodes.indexOf(focusNodes.singleWhere((FocusNode node) => node.hasFocus)),
      1,
    );
  });
247

248
  testWidgetsWithLeakTracking('Programmatic tab switching by changing the index of an existing controller', (WidgetTester tester) async {
249
    final CupertinoTabController controller = CupertinoTabController(initialIndex: 1);
250
    addTearDown(controller.dispose);
251 252 253 254 255 256 257 258 259 260
    final List<int> tabsPainted = <int>[];

    await tester.pumpWidget(
      CupertinoApp(
        home: CupertinoTabScaffold(
          tabBar: _buildTabBar(),
          controller: controller,
          tabBuilder: (BuildContext context, int index) {
            return CustomPaint(
              painter: TestCallbackPainter(
261
                onPaint: () { tabsPainted.add(index); },
262
              ),
263
              child: Text('Page ${index + 1}'),
264 265 266 267 268 269
            );
          },
        ),
      ),
    );

270
    expect(tabsPainted, const <int>[1]);
271 272 273 274

    controller.index = 0;
    await tester.pump();

275
    expect(tabsPainted, const <int>[1, 0]);
276 277 278 279 280 281 282
    // onTap is not called when changing tabs programmatically.
    expect(selectedTabs, isEmpty);

    // Can still tap out of the programmatically selected tab.
    await tester.tap(find.text('Tab 2'));
    await tester.pump();

283 284
    expect(tabsPainted, const <int>[1, 0, 1]);
    expect(selectedTabs, const <int>[1]);
285 286
  });

287
  testWidgetsWithLeakTracking('Programmatic tab switching by passing in a new controller', (WidgetTester tester) async {
288 289 290
    final List<int> tabsPainted = <int>[];

    await tester.pumpWidget(
291 292
      CupertinoApp(
        home: CupertinoTabScaffold(
xster's avatar
xster committed
293 294
          tabBar: _buildTabBar(),
          tabBuilder: (BuildContext context, int index) {
295 296
            return CustomPaint(
              painter: TestCallbackPainter(
297
                onPaint: () { tabsPainted.add(index); },
298
              ),
299
              child: Text('Page ${index + 1}'),
xster's avatar
xster committed
300 301 302
            );
          },
        ),
303 304 305
      ),
    );

306
    expect(tabsPainted, const <int>[0]);
307

308 309
    final CupertinoTabController controller = CupertinoTabController(initialIndex: 1);
    addTearDown(controller.dispose);
310
    await tester.pumpWidget(
311 312
      CupertinoApp(
        home: CupertinoTabScaffold(
313
          tabBar: _buildTabBar(),
314
          controller: controller, // Programmatically change the tab now.
xster's avatar
xster committed
315
          tabBuilder: (BuildContext context, int index) {
316 317
            return CustomPaint(
              painter: TestCallbackPainter(
318
                onPaint: () { tabsPainted.add(index); },
319
              ),
320
              child: Text('Page ${index + 1}'),
xster's avatar
xster committed
321 322 323
            );
          },
        ),
324 325 326
      ),
    );

327
    expect(tabsPainted, const <int>[0, 1]);
328 329 330 331 332 333 334
    // onTap is not called when changing tabs programmatically.
    expect(selectedTabs, isEmpty);

    // Can still tap out of the programmatically selected tab.
    await tester.tap(find.text('Tab 1'));
    await tester.pump();

335 336
    expect(tabsPainted, const <int>[0, 1, 0]);
    expect(selectedTabs, const <int>[0]);
337
  });
xster's avatar
xster committed
338

339
  testWidgetsWithLeakTracking('Tab bar respects themes', (WidgetTester tester) async {
xster's avatar
xster committed
340 341 342 343 344 345 346 347 348 349 350 351 352 353
    await tester.pumpWidget(
      CupertinoApp(
        home: CupertinoTabScaffold(
          tabBar: _buildTabBar(),
          tabBuilder: (BuildContext context, int index) {
            return const Placeholder();
          },
        ),
      ),
    );

    BoxDecoration tabDecoration = tester.widget<DecoratedBox>(find.descendant(
      of: find.byType(CupertinoTabBar),
      matching: find.byType(DecoratedBox),
354
    )).decoration as BoxDecoration;
xster's avatar
xster committed
355

356
    expect(tabDecoration.color, isSameColorAs(const Color(0xF0F9F9F9))); // Inherited from theme.
xster's avatar
xster committed
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379

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

    // Pump again but with dark theme.
    await tester.pumpWidget(
      CupertinoApp(
        theme: const CupertinoThemeData(
          brightness: Brightness.dark,
          primaryColor: CupertinoColors.destructiveRed,
        ),
        home: CupertinoTabScaffold(
          tabBar: _buildTabBar(),
          tabBuilder: (BuildContext context, int index) {
            return const Placeholder();
          },
        ),
      ),
    );

    tabDecoration = tester.widget<DecoratedBox>(find.descendant(
      of: find.byType(CupertinoTabBar),
      matching: find.byType(DecoratedBox),
380
    )).decoration as BoxDecoration;
xster's avatar
xster committed
381

382
    expect(tabDecoration.color, isSameColorAs(const Color(0xF01D1D1D)));
xster's avatar
xster committed
383 384 385 386 387 388

    final RichText tab1 = tester.widget(find.descendant(
      of: find.text('Tab 1'),
      matching: find.byType(RichText),
    ));
    // Tab 2 should still be selected after changing theme.
389
    expect(tab1.text.style!.color!.value, 0xFF757575);
xster's avatar
xster committed
390 391 392 393
    final RichText tab2 = tester.widget(find.descendant(
      of: find.text('Tab 2'),
      matching: find.byType(RichText),
    ));
394
    expect(tab2.text.style!.color, isSameColorAs(CupertinoColors.systemRed.darkColor));
xster's avatar
xster committed
395
  });
396

397
  testWidgetsWithLeakTracking('Tab contents are padded when there are view insets', (WidgetTester tester) async {
398
    late BuildContext innerContext;
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416

    await tester.pumpWidget(
      CupertinoApp(
        home: MediaQuery(
          data: const MediaQueryData(
            viewInsets: EdgeInsets.only(bottom: 200),
          ),
          child: CupertinoTabScaffold(
            tabBar: _buildTabBar(),
            tabBuilder: (BuildContext context, int index) {
              innerContext = context;
              return const Placeholder();
            },
          ),
        ),
      ),
    );

Dan Field's avatar
Dan Field committed
417
    expect(tester.getRect(find.byType(Placeholder)), const Rect.fromLTWH(0, 0, 800, 400));
418 419
    // Don't generate more media query padding from the translucent bottom
    // tab since the tab is behind the keyboard now.
420
    expect(MediaQuery.of(innerContext).padding.bottom, 0);
421 422
  });

423
  testWidgetsWithLeakTracking('Tab contents are not inset when resizeToAvoidBottomInset overridden', (WidgetTester tester) async {
424
    late BuildContext innerContext;
425 426 427 428 429 430 431 432 433 434 435 436 437

    await tester.pumpWidget(
      CupertinoApp(
        home: MediaQuery(
          data: const MediaQueryData(
            viewInsets: EdgeInsets.only(bottom: 200),
          ),
          child: CupertinoTabScaffold(
            resizeToAvoidBottomInset: false,
            tabBar: _buildTabBar(),
            tabBuilder: (BuildContext context, int index) {
              innerContext = context;
              return const Placeholder();
438
            },
439 440 441 442 443
          ),
        ),
      ),
    );

Dan Field's avatar
Dan Field committed
444
    expect(tester.getRect(find.byType(Placeholder)), const Rect.fromLTWH(0, 0, 800, 600));
445 446
    // Media query padding shows up in the inner content because it wasn't masked
    // by the view inset.
447
    expect(MediaQuery.of(innerContext).padding.bottom, 50);
448 449
  });

450
  testWidgetsWithLeakTracking('Tab contents bottom padding are not consumed by viewInsets when resizeToAvoidBottomInset overridden', (WidgetTester tester) async {
451 452
    final Widget child = Localizations(
      locale: const Locale('en', 'US'),
453
      delegates: const <LocalizationsDelegate<dynamic>>[
454 455 456 457 458 459 460 461 462 463 464 465
        DefaultWidgetsLocalizations.delegate,
        DefaultCupertinoLocalizations.delegate,
      ],
      child: Directionality(
        textDirection: TextDirection.ltr,
        child: CupertinoTabScaffold(
          resizeToAvoidBottomInset: false,
          tabBar: _buildTabBar(),
          tabBuilder: (BuildContext context, int index) {
            return const Placeholder();
          },
        ),
466
      ),
467 468 469 470 471 472 473 474
    );

    await tester.pumpWidget(
      CupertinoApp(
        home: MediaQuery(
          data: const MediaQueryData(
            viewInsets: EdgeInsets.only(bottom: 20.0),
          ),
475
          child: child,
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
        ),
      ),
    );

    final Offset initialPoint = tester.getCenter(find.byType(Placeholder));

    // Consume bottom padding - as if by the keyboard opening
    await tester.pumpWidget(
      MediaQuery(
        data: const MediaQueryData(
          viewPadding: EdgeInsets.only(bottom: 20),
          viewInsets: EdgeInsets.only(bottom: 300),
        ),
        child: child,
      ),
    );

    final Offset finalPoint = tester.getCenter(find.byType(Placeholder));

    expect(initialPoint, finalPoint);
  });

498
  testWidgetsWithLeakTracking(
499 500 501 502
    'Opaque tab bar consumes bottom padding while non opaque tab bar does not',
    (WidgetTester tester) async {
      // Regression test for https://github.com/flutter/flutter/issues/43581.
      Future<EdgeInsets> getContentPaddingWithTabBarColor(Color color) async {
503
        late EdgeInsets contentPadding;
504 505 506 507 508 509 510 511 512 513 514

        await tester.pumpWidget(
          CupertinoApp(
            home: MediaQuery(
              data: const MediaQueryData(padding: EdgeInsets.only(bottom: 50)),
              child: CupertinoTabScaffold(
                tabBar: CupertinoTabBar(
                  backgroundColor: color,
                  items: List<BottomNavigationBarItem>.generate(2, tabGenerator),
                ),
                tabBuilder: (BuildContext context, int index) {
515
                  contentPadding = MediaQuery.paddingOf(context);
516
                  return const Placeholder();
517
                },
518 519 520 521 522 523 524 525 526
              ),
            ),
          ),
        );
        return contentPadding;
      }

      expect(await getContentPaddingWithTabBarColor(const Color(0xAAFFFFFF)), isNot(EdgeInsets.zero));
      expect(await getContentPaddingWithTabBarColor(const Color(0xFFFFFFFF)), EdgeInsets.zero);
527 528
    },
  );
529

530
  testWidgetsWithLeakTracking('Tab and page scaffolds do not double stack view insets', (WidgetTester tester) async {
531
    late BuildContext innerContext;
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555

    await tester.pumpWidget(
      CupertinoApp(
        home: MediaQuery(
          data: const MediaQueryData(
            viewInsets: EdgeInsets.only(bottom: 200),
          ),
          child: CupertinoTabScaffold(
            tabBar: _buildTabBar(),
            tabBuilder: (BuildContext context, int index) {
              return CupertinoPageScaffold(
                child: Builder(
                  builder: (BuildContext context) {
                    innerContext = context;
                    return const Placeholder();
                  },
                ),
              );
            },
          ),
        ),
      ),
    );

Dan Field's avatar
Dan Field committed
556
    expect(tester.getRect(find.byType(Placeholder)), const Rect.fromLTWH(0, 0, 800, 400));
557
    expect(MediaQuery.of(innerContext).padding.bottom, 0);
558
  });
559

560
  testWidgetsWithLeakTracking('Deleting tabs after selecting them should switch to the last available tab', (WidgetTester tester) async {
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
    final List<int> tabsBuilt = <int>[];

    await tester.pumpWidget(
      CupertinoApp(
        home: CupertinoTabScaffold(
          tabBar: CupertinoTabBar(
            items: List<BottomNavigationBarItem>.generate(4, tabGenerator),
            onTap: (int newTab) => selectedTabs.add(newTab),
          ),
          tabBuilder: (BuildContext context, int index) {
            tabsBuilt.add(index);
            return Text('Page ${index + 1}');
          },
        ),
      ),
    );

578
    expect(tabsBuilt, const <int>[0]);
579 580
    // selectedTabs list is appended to on onTap callbacks. We didn't tap
    // any tabs yet.
581
    expect(selectedTabs, const <int>[]);
582 583 584 585 586 587
    tabsBuilt.clear();

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

    // Tabs 1 and 4 are built but only one is onstage.
588 589
    expect(tabsBuilt, const <int>[0, 3]);
    expect(selectedTabs, const <int>[3]);
590 591 592 593
    expect(find.text('Page 1', skipOffstage: false), isOffstage);
    expect(find.text('Page 4'), findsOneWidget);
    tabsBuilt.clear();

594
    // Delete 2 tabs while Page 4 is still selected.
595 596 597 598 599 600 601 602 603 604 605 606 607
    await tester.pumpWidget(
      CupertinoApp(
        home: CupertinoTabScaffold(
          tabBar: CupertinoTabBar(
            items: List<BottomNavigationBarItem>.generate(2, tabGenerator),
            onTap: (int newTab) => selectedTabs.add(newTab),
          ),
          tabBuilder: (BuildContext context, int index) {
            tabsBuilt.add(index);
            // Change the builder too.
            return Text('Different page ${index + 1}');
          },
        ),
608
      ),
609 610
    );

611
    expect(tabsBuilt, const <int>[0, 1]);
612 613
    // We didn't tap on any additional tabs to invoke the onTap callback. We
    // just deleted a tab.
614
    expect(selectedTabs, const <int>[3]);
615 616 617 618 619 620 621 622 623 624 625 626 627
    // Tab 1 was previously built so it's rebuilt again, albeit offstage.
    expect(find.text('Different page 1', skipOffstage: false), isOffstage);
    // Since all the tabs after tab 2 are deleted, tab 2 is now the last tab and
    // the actively shown tab.
    expect(find.text('Different page 2'), findsOneWidget);
    // No more tab 4 since it's deleted.
    expect(find.text('Different page 4', skipOffstage: false), findsNothing);
    // We also changed the builder so no tabs should be built with the old
    // builder.
    expect(find.text('Page 1', skipOffstage: false), findsNothing);
    expect(find.text('Page 2', skipOffstage: false), findsNothing);
    expect(find.text('Page 4', skipOffstage: false), findsNothing);
  });
628

629
  // Regression test for https://github.com/flutter/flutter/issues/33455
630
  testWidgetsWithLeakTracking('Adding new tabs does not crash the app', (WidgetTester tester) async {
631
    final List<int> tabsPainted = <int>[];
632
    final CupertinoTabController controller = CupertinoTabController();
633
    addTearDown(controller.dispose);
634 635 636 637 638 639 640 641 642 643 644

    await tester.pumpWidget(
      CupertinoApp(
        home: CupertinoTabScaffold(
          tabBar: CupertinoTabBar(
            items: List<BottomNavigationBarItem>.generate(10, tabGenerator),
          ),
          controller: controller,
          tabBuilder: (BuildContext context, int index) {
            return CustomPaint(
              painter: TestCallbackPainter(
645
                onPaint: () { tabsPainted.add(index); },
646
              ),
647
              child: Text('Page ${index + 1}'),
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
            );
          },
        ),
      ),
    );

    expect(tabsPainted, const <int> [0]);

    // Increase the num of tabs to 20.
    await tester.pumpWidget(
      CupertinoApp(
        home: CupertinoTabScaffold(
          tabBar: CupertinoTabBar(
            items: List<BottomNavigationBarItem>.generate(20, tabGenerator),
          ),
          controller: controller,
          tabBuilder: (BuildContext context, int index) {
            return CustomPaint(
              painter: TestCallbackPainter(
667
                onPaint: () { tabsPainted.add(index); },
668
              ),
669
              child: Text('Page ${index + 1}'),
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
            );
          },
        ),
      ),
    );

    expect(tabsPainted, const <int> [0, 0]);

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

    // Tapping the tabs should still work.
    expect(tabsPainted, const <int>[0, 0, 18]);
  });

685
  testWidgetsWithLeakTracking(
686 687 688
    'If a controller is initially provided then the parent stops doing so for rebuilds, '
    'a new instance of CupertinoTabController should be created and used by the widget, '
    "while preserving the previous controller's tab index",
689 690
    (WidgetTester tester) async {
      final List<int> tabsPainted = <int>[];
691
      final CupertinoTabController oldController = CupertinoTabController();
692
      addTearDown(oldController.dispose);
693 694 695 696 697 698 699 700 701 702 703

      await tester.pumpWidget(
        CupertinoApp(
          home: CupertinoTabScaffold(
            tabBar: CupertinoTabBar(
              items: List<BottomNavigationBarItem>.generate(10, tabGenerator),
            ),
            controller: oldController,
            tabBuilder: (BuildContext context, int index) {
              return CustomPaint(
                painter: TestCallbackPainter(
704
                  onPaint: () { tabsPainted.add(index); },
705
                ),
706
                child: Text('Page ${index + 1}'),
707
              );
708
            },
709
          ),
710
        ),
711 712
      );

713
      expect(tabsPainted, const <int> [0]);
714 715 716 717 718 719 720 721 722 723 724

      await tester.pumpWidget(
        CupertinoApp(
          home: CupertinoTabScaffold(
            tabBar: CupertinoTabBar(
              items: List<BottomNavigationBarItem>.generate(10, tabGenerator),
            ),
            tabBuilder:
            (BuildContext context, int index) {
              return CustomPaint(
                painter: TestCallbackPainter(
725
                  onPaint: () { tabsPainted.add(index); },
726
                ),
727
                child: Text('Page ${index + 1}'),
728
              );
729
            },
730
          ),
731
        ),
732 733
      );

734
      expect(tabsPainted, const <int> [0, 0]);
735 736 737 738 739

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

      // Tapping the tabs should still work.
740
      expect(tabsPainted, const <int>[0, 0, 1]);
741 742 743 744 745

      oldController.index = 10;
      await tester.pump();

      // Changing [index] of the oldController should not work.
746
      expect(tabsPainted, const <int> [0, 0, 1]);
747 748
    },
  );
749

750
  testWidgetsWithLeakTracking(
751 752
    'Do not call dispose on a controller that we do not own '
    'but do remove from its listeners when done listening to it',
753 754
    (WidgetTester tester) async {
      final MockCupertinoTabController mockController = MockCupertinoTabController(initialIndex: 0);
755
      addTearDown(mockController.dispose);
756 757 758 759 760 761 762 763 764 765

      await tester.pumpWidget(
        CupertinoApp(
          home: CupertinoTabScaffold(
            tabBar: CupertinoTabBar(
              items: List<BottomNavigationBarItem>.generate(2, tabGenerator),
            ),
            controller: mockController,
            tabBuilder: (BuildContext context, int index) => const Placeholder(),
          ),
766
        ),
767 768 769 770 771 772 773 774 775 776 777 778 779
      );

      expect(mockController.numOfListeners, 1);
      expect(mockController.isDisposed, isFalse);

      await tester.pumpWidget(
        CupertinoApp(
          home: CupertinoTabScaffold(
            tabBar: CupertinoTabBar(
              items: List<BottomNavigationBarItem>.generate(2, tabGenerator),
            ),
            tabBuilder: (BuildContext context, int index) => const Placeholder(),
          ),
780
        ),
781 782 783 784
      );

      expect(mockController.numOfListeners, 0);
      expect(mockController.isDisposed, isFalse);
785 786
    },
  );
787

788
  testWidgetsWithLeakTracking('The owner can dispose the old controller', (WidgetTester tester) async {
789 790 791 792 793 794 795 796 797
    CupertinoTabController controller = CupertinoTabController(initialIndex: 2);

    await tester.pumpWidget(
      CupertinoApp(
        home: CupertinoTabScaffold(
          tabBar: CupertinoTabBar(
            items: List<BottomNavigationBarItem>.generate(3, tabGenerator),
          ),
          controller: controller,
798
          tabBuilder: (BuildContext context, int index) => const Placeholder(),
799
        ),
800
      ),
801 802 803 804 805 806
    );
    expect(find.text('Tab 1'), findsOneWidget);
    expect(find.text('Tab 2'), findsOneWidget);
    expect(find.text('Tab 3'), findsOneWidget);

    controller.dispose();
807
    controller = CupertinoTabController();
808
    addTearDown(controller.dispose);
809 810 811 812 813 814 815
    await tester.pumpWidget(
      CupertinoApp(
        home: CupertinoTabScaffold(
          tabBar: CupertinoTabBar(
            items: List<BottomNavigationBarItem>.generate(2, tabGenerator),
          ),
          controller: controller,
816
          tabBuilder: (BuildContext context, int index) => const Placeholder(),
817
        ),
818
      ),
819 820 821 822 823 824 825 826
    );

    // Should not crash here.
    expect(find.text('Tab 1'), findsOneWidget);
    expect(find.text('Tab 2'), findsOneWidget);
    expect(find.text('Tab 3'), findsNothing);
  });

827
  testWidgetsWithLeakTracking('A controller can control more than one CupertinoTabScaffold, '
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
    'removal of listeners does not break the controller',
    (WidgetTester tester) async {
      final List<int> tabsPainted0 = <int>[];
      final List<int> tabsPainted1 = <int>[];
      MockCupertinoTabController controller = MockCupertinoTabController(initialIndex: 2);

      await tester.pumpWidget(
        CupertinoApp(
          home: CupertinoPageScaffold(
            child: Stack(
              children: <Widget>[
                CupertinoTabScaffold(
                  tabBar: CupertinoTabBar(
                    items: List<BottomNavigationBarItem>.generate(3, tabGenerator),
                  ),
                  controller: controller,
                  tabBuilder: (BuildContext context, int index) {
                    return CustomPaint(
                      painter: TestCallbackPainter(
847
                        onPaint: () => tabsPainted0.add(index),
848
                      ),
849
                    );
850
                  },
851 852 853 854 855 856 857 858 859
                ),
                CupertinoTabScaffold(
                  tabBar: CupertinoTabBar(
                    items: List<BottomNavigationBarItem>.generate(3, tabGenerator),
                  ),
                  controller: controller,
                  tabBuilder: (BuildContext context, int index) {
                    return CustomPaint(
                      painter: TestCallbackPainter(
860
                        onPaint: () => tabsPainted1.add(index),
861
                      ),
862
                    );
863
                  },
864
                ),
865 866 867 868
              ],
            ),
          ),
        ),
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893
      );
      expect(tabsPainted0, const <int>[2]);
      expect(tabsPainted1, const <int>[2]);
      expect(controller.numOfListeners, 2);

      controller.index = 0;
      await tester.pump();
      expect(tabsPainted0, const <int>[2, 0]);
      expect(tabsPainted1, const <int>[2, 0]);

      controller.index = 1;
      // Removing one of the tabs works.
      await tester.pumpWidget(
        CupertinoApp(
          home: CupertinoPageScaffold(
            child: Stack(
              children: <Widget>[
                CupertinoTabScaffold(
                  tabBar: CupertinoTabBar(
                    items: List<BottomNavigationBarItem>.generate(3, tabGenerator),
                  ),
                  controller: controller,
                  tabBuilder: (BuildContext context, int index) {
                    return CustomPaint(
                      painter: TestCallbackPainter(
894
                        onPaint: () => tabsPainted0.add(index),
895
                      ),
896
                    );
897
                  },
898
                ),
899 900 901 902
              ],
            ),
          ),
        ),
903 904 905 906 907 908 909
      );

      expect(tabsPainted0, const <int>[2, 0, 1]);
      expect(tabsPainted1, const <int>[2, 0]);
      expect(controller.numOfListeners, 1);

      // Replacing controller works.
910
      controller.dispose();
911
      controller = MockCupertinoTabController(initialIndex: 2);
912
      addTearDown(controller.dispose);
913 914 915 916 917 918 919 920 921 922 923 924 925
      await tester.pumpWidget(
        CupertinoApp(
          home: CupertinoPageScaffold(
            child: Stack(
              children: <Widget>[
                CupertinoTabScaffold(
                  tabBar: CupertinoTabBar(
                    items: List<BottomNavigationBarItem>.generate(3, tabGenerator),
                  ),
                  controller: controller,
                  tabBuilder: (BuildContext context, int index) {
                    return CustomPaint(
                      painter: TestCallbackPainter(
926 927
                        onPaint: () => tabsPainted0.add(index),
                      ),
928
                    );
929
                  },
930
                ),
931 932 933 934
              ],
            ),
          ),
        ),
935 936 937 938
      );
      expect(tabsPainted0, const <int>[2, 0, 1, 2]);
      expect(tabsPainted1, const <int>[2, 0]);
      expect(controller.numOfListeners, 1);
939 940
    },
  );
941

942
  testWidgetsWithLeakTracking('Assert when current tab index >= number of tabs', (WidgetTester tester) async {
943
    final CupertinoTabController controller = CupertinoTabController(initialIndex: 2);
944
    addTearDown(controller.dispose);
945 946 947 948 949 950 951 952 953 954 955

    try {
      await tester.pumpWidget(
        CupertinoApp(
          home: CupertinoTabScaffold(
            tabBar: CupertinoTabBar(
              items: List<BottomNavigationBarItem>.generate(2, tabGenerator),
            ),
            controller: controller,
            tabBuilder: (BuildContext context, int index) => Text('Different page ${index + 1}'),
          ),
956
        ),
957 958 959 960 961 962 963 964 965 966 967 968 969 970
      );
    } on AssertionError catch (e) {
      expect(e.toString(), contains('controller.index < tabBar.items.length'));
    }

    await tester.pumpWidget(
      CupertinoApp(
        home: CupertinoTabScaffold(
          tabBar: CupertinoTabBar(
            items: List<BottomNavigationBarItem>.generate(3, tabGenerator),
          ),
          controller: controller,
          tabBuilder: (BuildContext context, int index) => Text('Different page ${index + 1}'),
        ),
971
      ),
972 973 974 975 976 977 978 979 980 981 982 983
    );

    expect(tester.takeException(), null);

    controller.index = 10;
    await tester.pump();

    final String message = tester.takeException().toString();
    expect(message, contains('current index ${controller.index}'));
    expect(message, contains('with 3 tabs'));
  });

984
  testWidgetsWithLeakTracking("Don't replace focus nodes for existing tabs when changing tab count", (WidgetTester tester) async {
985
    final CupertinoTabController controller = CupertinoTabController(initialIndex: 2);
986
    addTearDown(controller.dispose);
987

988 989 990 991 992 993
    final List<FocusScopeNode> scopes = <FocusScopeNode>[];
    for (int i = 0; i < 5; i++) {
      final FocusScopeNode scope = FocusScopeNode();
      addTearDown(scope.dispose);
      scopes.add(scope);
    }
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
    await tester.pumpWidget(
        CupertinoApp(
          home: CupertinoTabScaffold(
            tabBar: CupertinoTabBar(
              items: List<BottomNavigationBarItem>.generate(3, tabGenerator),
            ),
            controller: controller,
            tabBuilder: (BuildContext context, int index) {
              scopes[index] = FocusScope.of(context);
              return Container();
            },
          ),
1006
        ),
1007 1008 1009 1010 1011 1012 1013 1014
    );

    for (int i = 0; i < 3; i++) {
      controller.index = i;
      await tester.pump();
    }
    await tester.pump();

1015
    final List<FocusScopeNode> newScopes = <FocusScopeNode>[];
1016 1017 1018 1019 1020 1021 1022 1023
    await tester.pumpWidget(
        CupertinoApp(
          home: CupertinoTabScaffold(
            tabBar: CupertinoTabBar(
              items: List<BottomNavigationBarItem>.generate(5, tabGenerator),
            ),
            controller: controller,
            tabBuilder: (BuildContext context, int index) {
1024
              newScopes.add(FocusScope.of(context));
1025 1026 1027
              return Container();
            },
          ),
1028
        ),
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
    );
    for (int i = 0; i < 5; i++) {
      controller.index = i;
      await tester.pump();
    }
    await tester.pump();

    expect(scopes.sublist(0, 3), equals(newScopes.sublist(0, 3)));
  });

1039
  testWidgetsWithLeakTracking('Current tab index cannot go below zero or be null', (WidgetTester tester) async {
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
    void expectAssertionError(VoidCallback callback, String errorMessage) {
      try {
        callback();
      } on AssertionError catch (e) {
        expect(e.toString(), contains(errorMessage));
      }
    }

    expectAssertionError(() => CupertinoTabController(initialIndex: -1), '>= 0');

    final CupertinoTabController controller = CupertinoTabController();
1051
    addTearDown(controller.dispose);
1052 1053 1054 1055

    expectAssertionError(() => controller.index = -1, '>= 0');
  });

1056
  testWidgetsWithLeakTracking('Does not lose state when focusing on text input', (WidgetTester tester) async {
1057 1058 1059 1060
    // Regression testing for https://github.com/flutter/flutter/issues/28457.

    await tester.pumpWidget(
      MediaQuery(
1061
        data: const MediaQueryData(),
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
        child: CupertinoApp(
          home: CupertinoTabScaffold(
            tabBar: _buildTabBar(),
            tabBuilder: (BuildContext context, int index) {
              return const CupertinoTextField();
            },
          ),
        ),
      ),
    );

    final EditableTextState editableState = tester.state<EditableTextState>(find.byType(EditableText));

    await tester.enterText(find.byType(CupertinoTextField), "don't lose me");

    await tester.pumpWidget(
      MediaQuery(
        data: const MediaQueryData(
          viewInsets:  EdgeInsets.only(bottom: 100),
        ),
        child: CupertinoApp(
          home: CupertinoTabScaffold(
            tabBar: _buildTabBar(),
            tabBuilder: (BuildContext context, int index) {
              return const CupertinoTextField();
            },
          ),
        ),
      ),
    );

    // The exact same state instance is still there.
    expect(tester.state<EditableTextState>(find.byType(EditableText)), editableState);
    expect(find.text("don't lose me"), findsOneWidget);
  });
1097

1098
  testWidgetsWithLeakTracking('textScaleFactor is set to 1.0', (WidgetTester tester) async {
1099 1100 1101 1102
    await tester.pumpWidget(
      CupertinoApp(
        home: Builder(builder: (BuildContext context) {
          return MediaQuery(
1103
            data: MediaQuery.of(context).copyWith(textScaleFactor: 99),
1104 1105 1106 1107
            child: CupertinoTabScaffold(
              tabBar: CupertinoTabBar(
                items: List<BottomNavigationBarItem>.generate(
                  10,
1108
                  (int i) => BottomNavigationBarItem(icon: ImageIcon(MemoryImage(Uint8List.fromList(kTransparentImage))), label: '$i'),
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
                ),
              ),
              tabBuilder: (BuildContext context, int index) => const Text('content'),
            ),
          );
        }),
      ),
    );

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

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

    expect(barItems.length, greaterThan(0));
1134
    expect(barItems, isNot(contains(predicate((RichText t) => t.textScaler != TextScaler.noScaling))));
1135 1136

    expect(contents.length, greaterThan(0));
1137
    expect(contents, isNot(contains(predicate((RichText t) => t.textScaler != const TextScaler.linear(99.0)))));
1138
  });
1139

1140
  testWidgetsWithLeakTracking('state restoration', (WidgetTester tester) async {
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
    await tester.pumpWidget(
      CupertinoApp(
        restorationScopeId: 'app',
        home: CupertinoTabScaffold(
          restorationId: 'scaffold',
          tabBar: CupertinoTabBar(
            items: List<BottomNavigationBarItem>.generate(
              4,
              (int i) => BottomNavigationBarItem(icon: const Icon(CupertinoIcons.map), label: 'Tab $i'),
            ),
          ),
          tabBuilder: (BuildContext context, int i) => Text('Content $i'),
        ),
      ),
    );

    expect(find.text('Content 0'), findsOneWidget);
    expect(find.text('Content 1'), findsNothing);
    expect(find.text('Content 2'), findsNothing);
    expect(find.text('Content 3'), findsNothing);

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

    expect(find.text('Content 0'), findsNothing);
    expect(find.text('Content 1'), findsNothing);
    expect(find.text('Content 2'), findsOneWidget);
    expect(find.text('Content 3'), findsNothing);

    await tester.restartAndRestore();

    expect(find.text('Content 0'), findsNothing);
    expect(find.text('Content 1'), findsNothing);
    expect(find.text('Content 2'), findsOneWidget);
    expect(find.text('Content 3'), findsNothing);

    final TestRestorationData data = await tester.getRestorationData();

    await tester.tap(find.text('Tab 1'));
    await tester.pumpAndSettle();

    expect(find.text('Content 0'), findsNothing);
    expect(find.text('Content 1'), findsOneWidget);
    expect(find.text('Content 2'), findsNothing);
    expect(find.text('Content 3'), findsNothing);

    await tester.restoreFrom(data);

    expect(find.text('Content 0'), findsNothing);
    expect(find.text('Content 1'), findsNothing);
    expect(find.text('Content 2'), findsOneWidget);
    expect(find.text('Content 3'), findsNothing);
  });

1195
  testWidgetsWithLeakTracking('switch from internal to external controller with state restoration', (WidgetTester tester) async {
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
    Widget buildWidget({CupertinoTabController? controller}) {
      return CupertinoApp(
        restorationScopeId: 'app',
        home: CupertinoTabScaffold(
          controller: controller,
          restorationId: 'scaffold',
          tabBar: CupertinoTabBar(
            items: List<BottomNavigationBarItem>.generate(
              4,
              (int i) => BottomNavigationBarItem(icon: const Icon(CupertinoIcons.map), label: 'Tab $i'),
            ),
          ),
          tabBuilder: (BuildContext context, int i) => Text('Content $i'),
        ),
      );
    }

    await tester.pumpWidget(buildWidget());

    expect(find.text('Content 0'), findsOneWidget);
    expect(find.text('Content 1'), findsNothing);
    expect(find.text('Content 2'), findsNothing);
    expect(find.text('Content 3'), findsNothing);

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

    expect(find.text('Content 0'), findsNothing);
    expect(find.text('Content 1'), findsNothing);
    expect(find.text('Content 2'), findsOneWidget);
    expect(find.text('Content 3'), findsNothing);

    final CupertinoTabController controller = CupertinoTabController(initialIndex: 3);
1229
    addTearDown(controller.dispose);
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
    await tester.pumpWidget(buildWidget(controller: controller));

    expect(find.text('Content 0'), findsNothing);
    expect(find.text('Content 1'), findsNothing);
    expect(find.text('Content 2'), findsNothing);
    expect(find.text('Content 3'), findsOneWidget);

    await tester.pumpWidget(buildWidget());

    expect(find.text('Content 0'), findsOneWidget);
    expect(find.text('Content 1'), findsNothing);
    expect(find.text('Content 2'), findsNothing);
    expect(find.text('Content 3'), findsNothing);
  });
1244 1245 1246

  group('Android Predictive Back', () {
    bool? lastFrameworkHandlesBack;
1247
    setUp(() async {
1248 1249 1250 1251 1252 1253 1254 1255 1256
      lastFrameworkHandlesBack = null;
      TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
        .setMockMethodCallHandler(SystemChannels.platform, (MethodCall methodCall) async {
          if (methodCall.method == 'SystemNavigator.setFrameworkHandlesBack') {
            expect(methodCall.arguments, isA<bool>());
            lastFrameworkHandlesBack = methodCall.arguments as bool;
          }
          return;
        });
1257 1258 1259 1260 1261 1262
      await TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
          .handlePlatformMessage(
            'flutter/lifecycle',
            const StringCodec().encodeMessage(AppLifecycleState.resumed.toString()),
            (ByteData? data) {},
          );
1263 1264 1265 1266 1267 1268 1269
    });

    tearDown(() {
      TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
          .setMockMethodCallHandler(SystemChannels.platform, null);
    });

1270
    testWidgetsWithLeakTracking('System back navigation inside of tabs', (WidgetTester tester) async {
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 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 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
      await tester.pumpWidget(
        CupertinoApp(
          home: MediaQuery(
            data: const MediaQueryData(
              viewInsets: EdgeInsets.only(bottom: 200),
            ),
            child: CupertinoTabScaffold(
              tabBar: _buildTabBar(),
              tabBuilder: (BuildContext context, int index) {
                return CupertinoTabView(
                  builder: (BuildContext context) {
                    return CupertinoPageScaffold(
                      navigationBar: CupertinoNavigationBar(
                        middle: Text('Page 1 of tab ${index + 1}'),
                      ),
                      child: Center(
                        child: CupertinoButton(
                          child: const Text('Next page'),
                          onPressed: () {
                            Navigator.of(context).push(
                              CupertinoPageRoute<void>(
                                builder: (BuildContext context) {
                                  return CupertinoPageScaffold(
                                    navigationBar: CupertinoNavigationBar(
                                      middle: Text('Page 2 of tab ${index + 1}'),
                                    ),
                                    child: Center(
                                      child: CupertinoButton(
                                        child: const Text('Back'),
                                        onPressed: () {
                                          Navigator.of(context).pop();
                                        },
                                      ),
                                    ),
                                  );
                                },
                              ),
                            );
                          },
                        ),
                      ),
                    );
                  },
                );
              },
            ),
          ),
        ),
      );

      expect(find.text('Page 1 of tab 1'), findsOneWidget);
      expect(find.text('Page 2 of tab 1'), findsNothing);
      expect(lastFrameworkHandlesBack, isFalse);

      await tester.tap(find.text('Next page'));
      await tester.pumpAndSettle();
      expect(find.text('Page 1 of tab 1'), findsNothing);
      expect(find.text('Page 2 of tab 1'), findsOneWidget);
      expect(lastFrameworkHandlesBack, isTrue);

      await simulateSystemBack();
      await tester.pumpAndSettle();
      expect(find.text('Page 1 of tab 1'), findsOneWidget);
      expect(find.text('Page 2 of tab 1'), findsNothing);
      expect(lastFrameworkHandlesBack, isFalse);

      await tester.tap(find.text('Next page'));
      await tester.pumpAndSettle();
      expect(find.text('Page 1 of tab 1'), findsNothing);
      expect(find.text('Page 2 of tab 1'), findsOneWidget);
      expect(lastFrameworkHandlesBack, isTrue);

      await tester.tap(find.text('Tab 2'));
      await tester.pumpAndSettle();
      expect(find.text('Page 1 of tab 2'), findsOneWidget);
      expect(find.text('Page 2 of tab 2'), findsNothing);
      expect(lastFrameworkHandlesBack, isFalse);

      await tester.tap(find.text('Tab 1'));
      await tester.pumpAndSettle();
      expect(find.text('Page 1 of tab 1'), findsNothing);
      expect(find.text('Page 2 of tab 1'), findsOneWidget);
      expect(lastFrameworkHandlesBack, isTrue);

      await simulateSystemBack();
      await tester.pumpAndSettle();
      expect(find.text('Page 1 of tab 1'), findsOneWidget);
      expect(find.text('Page 2 of tab 1'), findsNothing);
      expect(lastFrameworkHandlesBack, isFalse);

      await tester.tap(find.text('Tab 2'));
      await tester.pumpAndSettle();
      expect(find.text('Page 1 of tab 2'), findsOneWidget);
      expect(find.text('Page 2 of tab 2'), findsNothing);
      expect(lastFrameworkHandlesBack, isFalse);
    },
      variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.android }),
      skip: kIsWeb, // [intended] frameworkHandlesBack not used on web.
    );
  });
1371 1372
}

1373
CupertinoTabBar _buildTabBar({ int selectedTab = 0 }) {
1374
  return CupertinoTabBar(
1375
    items: <BottomNavigationBarItem>[
1376
      BottomNavigationBarItem(
1377
        icon: ImageIcon(MemoryImage(Uint8List.fromList(kTransparentImage))),
1378
        label: 'Tab 1',
1379
      ),
1380
      BottomNavigationBarItem(
1381
        icon: ImageIcon(MemoryImage(Uint8List.fromList(kTransparentImage))),
1382
        label: 'Tab 2',
1383 1384
      ),
    ],
1385
    currentIndex: selectedTab,
1386 1387
    onTap: (int newTab) => selectedTabs.add(newTab),
  );
1388
}