tabs_test.dart 50.5 KB
Newer Older
Hixie's avatar
Hixie committed
1 2 3 4
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
import 'dart:ui' show SemanticsFlags, SemanticsAction;

Adam Barth's avatar
Adam Barth committed
7
import 'package:flutter_test/flutter_test.dart';
8 9
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
10
import 'package:flutter/rendering.dart';
11
import 'package:flutter/physics.dart';
12

13
import '../rendering/mock_canvas.dart';
14
import '../rendering/recording_canvas.dart';
15
import '../widgets/semantics_tester.dart';
16

Ian Hickson's avatar
Ian Hickson committed
17
Widget boilerplate({ Widget child, TextDirection textDirection: TextDirection.ltr }) {
18 19 20 21 22 23 24 25 26 27 28
  return new Localizations(
    locale: const Locale('en', 'US'),
    delegates: <LocalizationsDelegate<dynamic>>[
      DefaultMaterialLocalizations.delegate,
      DefaultWidgetsLocalizations.delegate,
    ],
    child: new Directionality(
      textDirection: textDirection,
      child: new Material(
        child: child,
      ),
29 30 31 32
    ),
  );
}

Adam Barth's avatar
Adam Barth committed
33
class StateMarker extends StatefulWidget {
34
  const StateMarker({ Key key, this.child }) : super(key: key);
Adam Barth's avatar
Adam Barth committed
35 36 37 38 39 40 41 42 43 44 45 46

  final Widget child;

  @override
  StateMarkerState createState() => new StateMarkerState();
}

class StateMarkerState extends State<StateMarker> {
  String marker;

  @override
  Widget build(BuildContext context) {
47 48
    if (widget.child != null)
      return widget.child;
Adam Barth's avatar
Adam Barth committed
49 50 51 52
    return new Container();
  }
}

53 54 55 56 57 58 59
Widget buildFrame({
    Key tabBarKey,
    List<String> tabs,
    String value,
    bool isScrollable: false,
    Color indicatorColor,
  }) {
60
  return boilerplate(
Hans Muller's avatar
Hans Muller committed
61 62 63 64
    child: new DefaultTabController(
      initialIndex: tabs.indexOf(value),
      length: tabs.length,
      child: new TabBar(
Hans Muller's avatar
Hans Muller committed
65
        key: tabBarKey,
Hans Muller's avatar
Hans Muller committed
66 67
        tabs: tabs.map((String tab) => new Tab(text: tab)).toList(),
        isScrollable: isScrollable,
68
        indicatorColor: indicatorColor,
Hans Muller's avatar
Hans Muller committed
69 70
      ),
    ),
71 72 73
  );
}

Hans Muller's avatar
Hans Muller committed
74 75 76
typedef Widget TabControllerFrameBuilder(BuildContext context, TabController controller);

class TabControllerFrame extends StatefulWidget {
77
  const TabControllerFrame({ this.length, this.initialIndex: 0, this.builder });
Hans Muller's avatar
Hans Muller committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

  final int length;
  final int initialIndex;
  final TabControllerFrameBuilder builder;

  @override
  TabControllerFrameState createState() => new TabControllerFrameState();
}

class TabControllerFrameState extends State<TabControllerFrame> with SingleTickerProviderStateMixin {
  TabController _controller;

  @override
  void initState() {
    super.initState();
    _controller = new TabController(
      vsync: this,
95 96
      length: widget.length,
      initialIndex: widget.initialIndex,
Hans Muller's avatar
Hans Muller committed
97 98 99 100 101 102 103 104 105 106 107
    );
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
108
    return widget.builder(context, _controller);
Hans Muller's avatar
Hans Muller committed
109 110
  }
}
111 112 113 114

Widget buildLeftRightApp({ List<String> tabs, String value }) {
  return new MaterialApp(
    theme: new ThemeData(platform: TargetPlatform.android),
Hans Muller's avatar
Hans Muller committed
115 116 117
    home: new DefaultTabController(
      initialIndex: tabs.indexOf(value),
      length: tabs.length,
118 119
      child: new Scaffold(
        appBar: new AppBar(
120
          title: const Text('tabs'),
Hans Muller's avatar
Hans Muller committed
121 122 123
          bottom: new TabBar(
            tabs: tabs.map((String tab) => new Tab(text: tab)).toList(),
          ),
124
        ),
Hans Muller's avatar
Hans Muller committed
125
        body: new TabBarView(
126
          children: <Widget>[
127 128
            const Center(child: const Text('LEFT CHILD')),
            const Center(child: const Text('RIGHT CHILD'))
129 130 131 132 133 134 135
          ]
        )
      )
    )
  );
}

136 137 138 139 140 141 142 143 144 145 146 147 148
class TabIndicatorRecordingCanvas extends TestRecordingCanvas {
  TabIndicatorRecordingCanvas(this.indicatorColor);

  final Color indicatorColor;
  Rect indicatorRect;

  @override
  void drawRect(Rect rect, Paint paint) {
    if (paint.color == indicatorColor)
      indicatorRect = rect;
  }
}

149 150
class TestScrollPhysics extends ScrollPhysics {
  const TestScrollPhysics({ ScrollPhysics parent }) : super(parent: parent);
151

152 153 154 155
  @override
  TestScrollPhysics applyTo(ScrollPhysics ancestor) {
    return new TestScrollPhysics(parent: buildParent(ancestor));
  }
156

157 158
  static final SpringDescription _kDefaultSpring = new SpringDescription.withDampingRatio(
    mass: 0.5,
159
    stiffness: 500.0,
160 161
    ratio: 1.1,
  );
162

163 164 165 166
  @override
  SpringDescription get spring => _kDefaultSpring;
}

167
void main() {
168
  testWidgets('TabBar tap selects tab', (WidgetTester tester) async {
169
    final List<String> tabs = <String>['A', 'B', 'C'];
170

171
    await tester.pumpWidget(buildFrame(tabs: tabs, value: 'C', isScrollable: false));
172 173 174
    expect(find.text('A'), findsOneWidget);
    expect(find.text('B'), findsOneWidget);
    expect(find.text('C'), findsOneWidget);
175
    final TabController controller = DefaultTabController.of(tester.element(find.text('A')));
Hans Muller's avatar
Hans Muller committed
176 177 178
    expect(controller, isNotNull);
    expect(controller.index, 2);
    expect(controller.previousIndex, 2);
179

Hans Muller's avatar
Hans Muller committed
180
    await tester.pumpWidget(buildFrame(tabs: tabs, value: 'C', isScrollable: false));
181 182
    await tester.tap(find.text('B'));
    await tester.pump();
Hans Muller's avatar
Hans Muller committed
183
    expect(controller.indexIsChanging, true);
184
    await tester.pump(const Duration(seconds: 1)); // finish the animation
Hans Muller's avatar
Hans Muller committed
185 186 187
    expect(controller.index, 1);
    expect(controller.previousIndex, 2);
    expect(controller.indexIsChanging, false);
188

189 190 191 192
    await tester.pumpWidget(buildFrame(tabs: tabs, value: 'C', isScrollable: false));
    await tester.tap(find.text('C'));
    await tester.pump();
    await tester.pump(const Duration(seconds: 1));
Hans Muller's avatar
Hans Muller committed
193 194
    expect(controller.index, 2);
    expect(controller.previousIndex, 1);
195

196 197 198 199
    await tester.pumpWidget(buildFrame(tabs: tabs, value: 'C', isScrollable: false));
    await tester.tap(find.text('A'));
    await tester.pump();
    await tester.pump(const Duration(seconds: 1));
Hans Muller's avatar
Hans Muller committed
200 201
    expect(controller.index, 0);
    expect(controller.previousIndex, 2);
202 203
  });

204
  testWidgets('Scrollable TabBar tap selects tab', (WidgetTester tester) async {
205
    final List<String> tabs = <String>['A', 'B', 'C'];
206

207
    await tester.pumpWidget(buildFrame(tabs: tabs, value: 'C', isScrollable: true));
208 209 210
    expect(find.text('A'), findsOneWidget);
    expect(find.text('B'), findsOneWidget);
    expect(find.text('C'), findsOneWidget);
211
    final TabController controller = DefaultTabController.of(tester.element(find.text('A')));
Hans Muller's avatar
Hans Muller committed
212 213
    expect(controller.index, 2);
    expect(controller.previousIndex, 2);
214

Hans Muller's avatar
Hans Muller committed
215
    await tester.tap(find.text('C'));
216
    await tester.pumpAndSettle();
Hans Muller's avatar
Hans Muller committed
217
    expect(controller.index, 2);
218

Hans Muller's avatar
Hans Muller committed
219
    await tester.tap(find.text('B'));
220
    await tester.pumpAndSettle();
Hans Muller's avatar
Hans Muller committed
221
    expect(controller.index, 1);
222

223
    await tester.tap(find.text('A'));
224
    await tester.pumpAndSettle();
Hans Muller's avatar
Hans Muller committed
225
    expect(controller.index, 0);
226
  });
Hans Muller's avatar
Hans Muller committed
227

228
  testWidgets('Scrollable TabBar tap centers selected tab', (WidgetTester tester) async {
229 230
    final List<String> tabs = <String>['AAAAAA', 'BBBBBB', 'CCCCCC', 'DDDDDD', 'EEEEEE', 'FFFFFF', 'GGGGGG', 'HHHHHH', 'IIIIII', 'JJJJJJ', 'KKKKKK', 'LLLLLL'];
    final Key tabBarKey = const Key('TabBar');
231
    await tester.pumpWidget(buildFrame(tabs: tabs, value: 'AAAAAA', isScrollable: true, tabBarKey: tabBarKey));
232
    final TabController controller = DefaultTabController.of(tester.element(find.text('AAAAAA')));
Hans Muller's avatar
Hans Muller committed
233 234
    expect(controller, isNotNull);
    expect(controller.index, 0);
235 236 237

    expect(tester.getSize(find.byKey(tabBarKey)).width, equals(800.0));
    // The center of the FFFFFF item is to the right of the TabBar's center
238
    expect(tester.getCenter(find.text('FFFFFF')).dx, greaterThan(401.0));
239

240
    await tester.tap(find.text('FFFFFF'));
241
    await tester.pumpAndSettle();
Hans Muller's avatar
Hans Muller committed
242
    expect(controller.index, 5);
243
    // The center of the FFFFFF item is now at the TabBar's center
244
    expect(tester.getCenter(find.text('FFFFFF')).dx, closeTo(400.0, 1.0));
Hans Muller's avatar
Hans Muller committed
245 246 247
  });


248
  testWidgets('TabBar can be scrolled independent of the selection', (WidgetTester tester) async {
249 250
    final List<String> tabs = <String>['AAAA', 'BBBB', 'CCCC', 'DDDD', 'EEEE', 'FFFF', 'GGGG', 'HHHH', 'IIII', 'JJJJ', 'KKKK', 'LLLL'];
    final Key tabBarKey = const Key('TabBar');
251
    await tester.pumpWidget(buildFrame(tabs: tabs, value: 'AAAA', isScrollable: true, tabBarKey: tabBarKey));
252
    final TabController controller = DefaultTabController.of(tester.element(find.text('AAAA')));
Hans Muller's avatar
Hans Muller committed
253 254
    expect(controller, isNotNull);
    expect(controller.index, 0);
255 256

    // Fling-scroll the TabBar to the left
257
    expect(tester.getCenter(find.text('HHHH')).dx, lessThan(700.0));
258
    await tester.fling(find.byKey(tabBarKey), const Offset(-200.0, 0.0), 10000.0);
259 260
    await tester.pump();
    await tester.pump(const Duration(seconds: 1)); // finish the scroll animation
261
    expect(tester.getCenter(find.text('HHHH')).dx, lessThan(500.0));
262 263

    // Scrolling the TabBar doesn't change the selection
Hans Muller's avatar
Hans Muller committed
264
    expect(controller.index, 0);
Hans Muller's avatar
Hans Muller committed
265
  });
Adam Barth's avatar
Adam Barth committed
266

Hans Muller's avatar
Hans Muller committed
267
  testWidgets('TabBarView maintains state', (WidgetTester tester) async {
268
    final List<String> tabs = <String>['AAAAAA', 'BBBBBB', 'CCCCCC', 'DDDDDD', 'EEEEEE'];
269 270 271
    String value = tabs[0];

    Widget builder() {
272
      return boilerplate(
Hans Muller's avatar
Hans Muller committed
273 274 275 276
        child: new DefaultTabController(
          initialIndex: tabs.indexOf(value),
          length: tabs.length,
          child: new TabBarView(
277 278 279 280 281
            children: tabs.map((String name) {
              return new StateMarker(
                child: new Text(name)
              );
            }).toList()
Hans Muller's avatar
Hans Muller committed
282 283
          ),
        ),
284 285 286 287 288 289 290
      );
    }

    StateMarkerState findStateMarkerState(String name) {
      return tester.state(find.widgetWithText(StateMarker, name));
    }

291
    await tester.pumpWidget(builder());
292
    final TabController controller = DefaultTabController.of(tester.element(find.text('AAAAAA')));
Hans Muller's avatar
Hans Muller committed
293

294
    TestGesture gesture = await tester.startGesture(tester.getCenter(find.text(tabs[0])));
295
    await gesture.moveBy(const Offset(-600.0, 0.0));
296
    await tester.pump();
297 298
    expect(value, equals(tabs[0]));
    findStateMarkerState(tabs[1]).marker = 'marked';
299 300 301
    await gesture.up();
    await tester.pump();
    await tester.pump(const Duration(seconds: 1));
Hans Muller's avatar
Hans Muller committed
302
    value = tabs[controller.index];
303
    expect(value, equals(tabs[1]));
304
    await tester.pumpWidget(builder());
305 306 307 308
    expect(findStateMarkerState(tabs[1]).marker, equals('marked'));

    // Move to the third tab.

309
    gesture = await tester.startGesture(tester.getCenter(find.text(tabs[1])));
310
    await gesture.moveBy(const Offset(-600.0, 0.0));
311 312
    await gesture.up();
    await tester.pump();
313
    expect(findStateMarkerState(tabs[1]).marker, equals('marked'));
314
    await tester.pump(const Duration(seconds: 1));
Hans Muller's avatar
Hans Muller committed
315
    value = tabs[controller.index];
316
    expect(value, equals(tabs[2]));
317
    await tester.pumpWidget(builder());
318 319 320 321 322 323 324

    // The state is now gone.

    expect(find.text(tabs[1]), findsNothing);

    // Move back to the second tab.

325
    gesture = await tester.startGesture(tester.getCenter(find.text(tabs[2])));
326
    await gesture.moveBy(const Offset(600.0, 0.0));
327
    await tester.pump();
328
    final StateMarkerState markerState = findStateMarkerState(tabs[1]);
329 330
    expect(markerState.marker, isNull);
    markerState.marker = 'marked';
331 332 333
    await gesture.up();
    await tester.pump();
    await tester.pump(const Duration(seconds: 1));
Hans Muller's avatar
Hans Muller committed
334
    value = tabs[controller.index];
335
    expect(value, equals(tabs[1]));
336
    await tester.pumpWidget(builder());
337
    expect(findStateMarkerState(tabs[1]).marker, equals('marked'));
Adam Barth's avatar
Adam Barth committed
338
  });
339 340

  testWidgets('TabBar left/right fling', (WidgetTester tester) async {
341
    final List<String> tabs = <String>['LEFT', 'RIGHT'];
342 343 344 345 346 347 348

    await tester.pumpWidget(buildLeftRightApp(tabs: tabs, value: 'LEFT'));
    expect(find.text('LEFT'), findsOneWidget);
    expect(find.text('RIGHT'), findsOneWidget);
    expect(find.text('LEFT CHILD'), findsOneWidget);
    expect(find.text('RIGHT CHILD'), findsNothing);

349
    final TabController controller = DefaultTabController.of(tester.element(find.text('LEFT')));
Hans Muller's avatar
Hans Muller committed
350
    expect(controller.index, 0);
351 352

    // Fling to the left, switch from the 'LEFT' tab to the 'RIGHT'
353
    Offset flingStart = tester.getCenter(find.text('LEFT CHILD'));
354
    await tester.flingFrom(flingStart, const Offset(-200.0, 0.0), 10000.0);
355
    await tester.pumpAndSettle();
Hans Muller's avatar
Hans Muller committed
356
    expect(controller.index, 1);
357 358 359 360 361
    expect(find.text('LEFT CHILD'), findsNothing);
    expect(find.text('RIGHT CHILD'), findsOneWidget);

    // Fling to the right, switch back to the 'LEFT' tab
    flingStart = tester.getCenter(find.text('RIGHT CHILD'));
362
    await tester.flingFrom(flingStart, const Offset(200.0, 0.0), 10000.0);
363
    await tester.pumpAndSettle();
Hans Muller's avatar
Hans Muller committed
364
    expect(controller.index, 0);
365 366 367 368
    expect(find.text('LEFT CHILD'), findsOneWidget);
    expect(find.text('RIGHT CHILD'), findsNothing);
  });

369
  testWidgets('TabBar left/right fling reverse (1)', (WidgetTester tester) async {
370
    final List<String> tabs = <String>['LEFT', 'RIGHT'];
371 372 373 374 375 376 377

    await tester.pumpWidget(buildLeftRightApp(tabs: tabs, value: 'LEFT'));
    expect(find.text('LEFT'), findsOneWidget);
    expect(find.text('RIGHT'), findsOneWidget);
    expect(find.text('LEFT CHILD'), findsOneWidget);
    expect(find.text('RIGHT CHILD'), findsNothing);

378
    final TabController controller = DefaultTabController.of(tester.element(find.text('LEFT')));
379 380
    expect(controller.index, 0);

381
    final Offset flingStart = tester.getCenter(find.text('LEFT CHILD'));
382 383 384 385 386 387 388 389 390
    await tester.flingFrom(flingStart, const Offset(200.0, 0.0), 10000.0);
    await tester.pump();
    await tester.pump(const Duration(seconds: 1)); // finish the scroll animation
    expect(controller.index, 0);
    expect(find.text('LEFT CHILD'), findsOneWidget);
    expect(find.text('RIGHT CHILD'), findsNothing);
  });

  testWidgets('TabBar left/right fling reverse (2)', (WidgetTester tester) async {
391
    final List<String> tabs = <String>['LEFT', 'RIGHT'];
392 393 394 395 396 397 398

    await tester.pumpWidget(buildLeftRightApp(tabs: tabs, value: 'LEFT'));
    expect(find.text('LEFT'), findsOneWidget);
    expect(find.text('RIGHT'), findsOneWidget);
    expect(find.text('LEFT CHILD'), findsOneWidget);
    expect(find.text('RIGHT CHILD'), findsNothing);

399
    final TabController controller = DefaultTabController.of(tester.element(find.text('LEFT')));
400 401
    expect(controller.index, 0);

402
    final Offset flingStart = tester.getCenter(find.text('LEFT CHILD'));
403 404 405 406 407 408 409 410 411
    await tester.flingFrom(flingStart, const Offset(-200.0, 0.0), 10000.0);
    await tester.pump();
    // this is similar to a test above, but that one does many more pumps
    await tester.pump(const Duration(seconds: 1)); // finish the scroll animation
    expect(controller.index, 1);
    expect(find.text('LEFT CHILD'), findsNothing);
    expect(find.text('RIGHT CHILD'), findsOneWidget);
  });

412
  // A regression test for https://github.com/flutter/flutter/issues/5095
413
  testWidgets('TabBar left/right fling reverse (2)', (WidgetTester tester) async {
414
    final List<String> tabs = <String>['LEFT', 'RIGHT'];
415 416 417 418 419 420 421

    await tester.pumpWidget(buildLeftRightApp(tabs: tabs, value: 'LEFT'));
    expect(find.text('LEFT'), findsOneWidget);
    expect(find.text('RIGHT'), findsOneWidget);
    expect(find.text('LEFT CHILD'), findsOneWidget);
    expect(find.text('RIGHT CHILD'), findsNothing);

422
    final TabController controller = DefaultTabController.of(tester.element(find.text('LEFT')));
Hans Muller's avatar
Hans Muller committed
423
    expect(controller.index, 0);
424

425
    final Offset flingStart = tester.getCenter(find.text('LEFT CHILD'));
426
    final TestGesture gesture = await tester.startGesture(flingStart);
427 428 429 430
    for (int index = 0; index > 50; index += 1) {
      await gesture.moveBy(const Offset(-10.0, 0.0));
      await tester.pump(const Duration(milliseconds: 1));
    }
431 432 433
    // End the fling by reversing direction. This should cause not cause
    // a change to the selected tab, everything should just settle back to
    // to where it started.
434 435 436 437 438
    for (int index = 0; index > 50; index += 1) {
      await gesture.moveBy(const Offset(10.0, 0.0));
      await tester.pump(const Duration(milliseconds: 1));
    }
    await gesture.up();
439 440
    await tester.pump();
    await tester.pump(const Duration(seconds: 1)); // finish the scroll animation
Hans Muller's avatar
Hans Muller committed
441
    expect(controller.index, 0);
442 443 444 445
    expect(find.text('LEFT CHILD'), findsOneWidget);
    expect(find.text('RIGHT CHILD'), findsNothing);
  });

446 447
  // A regression test for https://github.com/flutter/flutter/issues/7133
  testWidgets('TabBar fling velocity', (WidgetTester tester) async {
448
    final List<String> tabs = <String>['AAAAAA', 'BBBBBB', 'CCCCCC', 'DDDDDD', 'EEEEEE', 'FFFFFF', 'GGGGGG', 'HHHHHH', 'IIIIII', 'JJJJJJ', 'KKKKKK', 'LLLLLL'];
449 450 451 452 453
    int index = 0;

    await tester.pumpWidget(
      new MaterialApp(
        home: new Align(
454
          alignment: Alignment.topLeft,
455 456 457
          child: new SizedBox(
            width: 300.0,
            height: 200.0,
Hans Muller's avatar
Hans Muller committed
458 459
            child: new DefaultTabController(
              length: tabs.length,
460 461
              child: new Scaffold(
                appBar: new AppBar(
462
                  title: const Text('tabs'),
Hans Muller's avatar
Hans Muller committed
463
                  bottom: new TabBar(
464
                    isScrollable: true,
Hans Muller's avatar
Hans Muller committed
465
                    tabs: tabs.map((String tab) => new Tab(text: tab)).toList(),
466 467
                  ),
                ),
Hans Muller's avatar
Hans Muller committed
468
                body: new TabBarView(
469 470 471 472 473 474 475 476 477 478
                  children: tabs.map((String name) => new Text('${index++}')).toList(),
                ),
              ),
            ),
          ),
        ),
      ),
    );

    // After a small slow fling to the left, we expect the second item to still be visible.
479
    await tester.fling(find.text('AAAAAA'), const Offset(-25.0, 0.0), 100.0);
480 481 482
    await tester.pump();
    await tester.pump(const Duration(seconds: 1)); // finish the scroll animation
    final RenderBox box = tester.renderObject(find.text('BBBBBB'));
483
    expect(box.localToGlobal(Offset.zero).dx, greaterThan(0.0));
484
  });
Hans Muller's avatar
Hans Muller committed
485 486

  testWidgets('TabController change notification', (WidgetTester tester) async {
487
    final List<String> tabs = <String>['LEFT', 'RIGHT'];
Hans Muller's avatar
Hans Muller committed
488 489

    await tester.pumpWidget(buildLeftRightApp(tabs: tabs, value: 'LEFT'));
490
    final TabController controller = DefaultTabController.of(tester.element(find.text('LEFT')));
Hans Muller's avatar
Hans Muller committed
491 492 493 494 495 496 497 498 499 500

    expect(controller, isNotNull);
    expect(controller.index, 0);

    String value;
    controller.addListener(() {
      value = tabs[controller.index];
    });

    await tester.tap(find.text('RIGHT'));
501
    await tester.pumpAndSettle();
Hans Muller's avatar
Hans Muller committed
502 503 504
    expect(value, 'RIGHT');

    await tester.tap(find.text('LEFT'));
505
    await tester.pumpAndSettle();
Hans Muller's avatar
Hans Muller committed
506 507
    expect(value, 'LEFT');

508
    final Offset leftFlingStart = tester.getCenter(find.text('LEFT CHILD'));
Hans Muller's avatar
Hans Muller committed
509
    await tester.flingFrom(leftFlingStart, const Offset(-200.0, 0.0), 10000.0);
510
    await tester.pumpAndSettle();
Hans Muller's avatar
Hans Muller committed
511 512
    expect(value, 'RIGHT');

513
    final Offset rightFlingStart = tester.getCenter(find.text('RIGHT CHILD'));
Hans Muller's avatar
Hans Muller committed
514
    await tester.flingFrom(rightFlingStart, const Offset(200.0, 0.0), 10000.0);
515
    await tester.pumpAndSettle();
Hans Muller's avatar
Hans Muller committed
516 517 518 519
    expect(value, 'LEFT');
  });

  testWidgets('Explicit TabController', (WidgetTester tester) async {
520
    final List<String> tabs = <String>['LEFT', 'RIGHT'];
Hans Muller's avatar
Hans Muller committed
521 522 523 524 525 526 527 528
    TabController tabController;

    Widget buildTabControllerFrame(BuildContext context, TabController controller) {
      tabController = controller;
      return new MaterialApp(
        theme: new ThemeData(platform: TargetPlatform.android),
        home: new Scaffold(
          appBar: new AppBar(
529
            title: const Text('tabs'),
Hans Muller's avatar
Hans Muller committed
530 531 532 533 534 535 536 537
            bottom: new TabBar(
              controller: controller,
              tabs: tabs.map((String tab) => new Tab(text: tab)).toList(),
            ),
          ),
          body: new TabBarView(
            controller: controller,
            children: <Widget>[
538 539
              const Center(child: const Text('LEFT CHILD')),
              const Center(child: const Text('RIGHT CHILD'))
Hans Muller's avatar
Hans Muller committed
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
            ]
          ),
        ),
      );
    }

    await tester.pumpWidget(new TabControllerFrame(
      builder: buildTabControllerFrame,
      length: tabs.length,
      initialIndex: 1,
    ));

    expect(find.text('LEFT'), findsOneWidget);
    expect(find.text('RIGHT'), findsOneWidget);
    expect(find.text('LEFT CHILD'), findsNothing);
    expect(find.text('RIGHT CHILD'), findsOneWidget);
    expect(tabController.index, 1);
    expect(tabController.previousIndex, 1);
    expect(tabController.indexIsChanging, false);
    expect(tabController.animation.value, 1.0);
    expect(tabController.animation.status, AnimationStatus.completed);

    tabController.index = 0;
    await tester.pump(const Duration(milliseconds: 500));
    await tester.pump(const Duration(milliseconds: 500));
    expect(find.text('LEFT CHILD'), findsOneWidget);
    expect(find.text('RIGHT CHILD'), findsNothing);

    tabController.index = 1;
    await tester.pump(const Duration(milliseconds: 500));
    await tester.pump(const Duration(milliseconds: 500));
    expect(find.text('LEFT CHILD'), findsNothing);
    expect(find.text('RIGHT CHILD'), findsOneWidget);
  });

  testWidgets('TabController listener resets index', (WidgetTester tester) async {
    // This is a regression test for the scenario brought up here
    // https://github.com/flutter/flutter/pull/7387#pullrequestreview-15630946

579
    final List<String> tabs = <String>['A', 'B', 'C'];
Hans Muller's avatar
Hans Muller committed
580 581 582 583 584 585 586 587
    TabController tabController;

    Widget buildTabControllerFrame(BuildContext context, TabController controller) {
      tabController = controller;
      return new MaterialApp(
        theme: new ThemeData(platform: TargetPlatform.android),
        home: new Scaffold(
          appBar: new AppBar(
588
            title: const Text('tabs'),
Hans Muller's avatar
Hans Muller committed
589 590 591 592 593 594 595 596
            bottom: new TabBar(
              controller: controller,
              tabs: tabs.map((String tab) => new Tab(text: tab)).toList(),
            ),
          ),
          body: new TabBarView(
            controller: controller,
            children: <Widget>[
597 598 599
              const Center(child: const Text('CHILD A')),
              const Center(child: const Text('CHILD B')),
              const Center(child: const Text('CHILD C')),
Hans Muller's avatar
Hans Muller committed
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
            ]
          ),
        ),
      );
    }

    await tester.pumpWidget(new TabControllerFrame(
      builder: buildTabControllerFrame,
      length: tabs.length,
    ));

    tabController.animation.addListener(() {
      if (tabController.animation.status == AnimationStatus.forward)
        tabController.index = 2;
      expect(tabController.indexIsChanging, true);
    });

    expect(tabController.index, 0);
    expect(tabController.indexIsChanging, false);

    tabController.animateTo(1, duration: const Duration(milliseconds: 200), curve: Curves.linear);
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 300));

    expect(tabController.index, 2);
    expect(tabController.indexIsChanging, false);
  });

  testWidgets('TabBarView child disposed during animation', (WidgetTester tester) async {
    // This is a regression test for the scenario brought up here
    // https://github.com/flutter/flutter/pull/7387#discussion_r95089191x

632
    final List<String> tabs = <String>['LEFT', 'RIGHT'];
Hans Muller's avatar
Hans Muller committed
633 634 635
    await tester.pumpWidget(buildLeftRightApp(tabs: tabs, value: 'LEFT'));

    // Fling to the left, switch from the 'LEFT' tab to the 'RIGHT'
636
    final Offset flingStart = tester.getCenter(find.text('LEFT CHILD'));
Hans Muller's avatar
Hans Muller committed
637 638 639 640 641
    await tester.flingFrom(flingStart, const Offset(-200.0, 0.0), 10000.0);
    await tester.pump();
    await tester.pump(const Duration(seconds: 1)); // finish the scroll animation
  });

642
  testWidgets('TabBar unselectedLabelColor control test', (WidgetTester tester) async {
643
    final TabController controller = new TabController(
644 645 646 647 648 649 650 651
      vsync: const TestVSync(),
      length: 2,
    );

    Color firstColor;
    Color secondColor;

    await tester.pumpWidget(
652
      boilerplate(
653 654 655 656 657 658 659 660
        child: new TabBar(
          controller: controller,
          labelColor: Colors.green[500],
          unselectedLabelColor: Colors.blue[500],
          tabs: <Widget>[
            new Builder(
              builder: (BuildContext context) {
                firstColor = IconTheme.of(context).color;
661
                return const Text('First');
662 663 664 665 666
              }
            ),
            new Builder(
              builder: (BuildContext context) {
                secondColor = IconTheme.of(context).color;
667
                return const Text('Second');
668 669 670 671 672 673 674 675 676 677 678
              }
            ),
          ],
        ),
      ),
    );

    expect(firstColor, equals(Colors.green[500]));
    expect(secondColor, equals(Colors.blue[500]));
  });

679
  testWidgets('TabBarView page left and right test', (WidgetTester tester) async {
680
    final TabController controller = new TabController(
681 682 683 684 685
      vsync: const TestVSync(),
      length: 2,
    );

    await tester.pumpWidget(
686
      boilerplate(
687 688
        child: new TabBarView(
          controller: controller,
689
          children: <Widget>[ const Text('First'), const Text('Second') ],
690 691 692 693 694 695
        ),
      ),
    );

    expect(controller.index, equals(0));

696
    TestGesture gesture = await tester.startGesture(const Offset(100.0, 100.0));
697 698
    expect(controller.index, equals(0));

699 700 701 702
    // Drag to the left and right, by less than the TabBarView's width.
    // The selected index (controller.index) should not change.
    await gesture.moveBy(const Offset(-100.0, 0.0));
    await gesture.moveBy(const Offset(100.0, 0.0));
703
    expect(controller.index, equals(0));
704 705
    expect(find.text('First'), findsOneWidget);
    expect(find.text('Second'), findsNothing);
706

707 708 709 710 711 712
    // Drag more than the TabBarView's width to the right. This forces
    // the selected index to change to 1.
    await gesture.moveBy(const Offset(-500.0, 0.0));
    await gesture.up();
    await tester.pump(); // start the scroll animation
    await tester.pump(const Duration(seconds: 1)); // finish the scroll animation
713
    expect(controller.index, equals(1));
714 715
    expect(find.text('First'), findsNothing);
    expect(find.text('Second'), findsOneWidget);
716

717
    gesture = await tester.startGesture(const Offset(100.0, 100.0));
718 719
    expect(controller.index, equals(1));

720 721 722 723
    // Drag to the left and right, by less than the TabBarView's width.
    // The selected index (controller.index) should not change.
    await gesture.moveBy(const Offset(-100.0, 0.0));
    await gesture.moveBy(const Offset(100.0, 0.0));
724 725 726 727
    expect(controller.index, equals(1));
    expect(find.text('First'), findsNothing);
    expect(find.text('Second'), findsOneWidget);

728 729 730 731 732 733 734 735 736 737
    // Drag more than the TabBarView's width to the left. This forces
    // the selected index to change back to 0.
    await gesture.moveBy(const Offset(500.0, 0.0));
    await gesture.up();
    await tester.pump(); // start the scroll animation
    await tester.pump(const Duration(seconds: 1)); // finish the scroll animation
    expect(controller.index, equals(0));
    expect(find.text('First'), findsOneWidget);
    expect(find.text('Second'), findsNothing);
  });
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772

  testWidgets('TabBar tap animates the selection indicator', (WidgetTester tester) async {
    // This is a regression test for https://github.com/flutter/flutter/issues/7479

    final List<String> tabs = <String>['A', 'B'];

    const Color indicatorColor = const Color(0xFFFF0000);
    await tester.pumpWidget(buildFrame(tabs: tabs, value: 'A', indicatorColor: indicatorColor));

    final RenderBox box = tester.renderObject(find.byType(TabBar));
    final TabIndicatorRecordingCanvas canvas = new TabIndicatorRecordingCanvas(indicatorColor);
    final TestRecordingPaintingContext context = new TestRecordingPaintingContext(canvas);

    box.paint(context, Offset.zero);
    final Rect indicatorRect0 = canvas.indicatorRect;
    expect(indicatorRect0.left, 0.0);
    expect(indicatorRect0.width, 400.0);
    expect(indicatorRect0.height, 2.0);

    await tester.tap(find.text('B'));
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 50));
    box.paint(context, Offset.zero);
    final Rect indicatorRect1 = canvas.indicatorRect;
    expect(indicatorRect1.left, greaterThan(indicatorRect0.left));
    expect(indicatorRect1.right, lessThan(800.0));
    expect(indicatorRect1.height, 2.0);

    await tester.pump(const Duration(milliseconds: 300));
    box.paint(context, Offset.zero);
    final Rect indicatorRect2 = canvas.indicatorRect;
    expect(indicatorRect2.left, 400.0);
    expect(indicatorRect2.width, 400.0);
    expect(indicatorRect2.height, 2.0);
  });
773 774 775 776 777 778 779 780 781 782 783

  testWidgets('TabBarView child disposed during animation', (WidgetTester tester) async {
    // This is a regression test for this patch:
    // https://github.com/flutter/flutter/pull/9015

    final TabController controller = new TabController(
      vsync: const TestVSync(),
      length: 2,
    );

    Widget buildFrame() {
784
      return boilerplate(
785 786 787
        child: new TabBar(
          key: new UniqueKey(),
          controller: controller,
788
          tabs: <Widget>[ const Text('A'), const Text('B') ],
789 790 791 792 793 794 795 796 797 798 799 800 801
        ),
      );
    }

    await tester.pumpWidget(buildFrame());

    // The original TabBar will be disposed. The controller should no
    // longer have any listeners from the original TabBar.
    await tester.pumpWidget(buildFrame());

    controller.index = 1;
    await tester.pump(const Duration(milliseconds: 300));
  });
802 803 804 805 806 807 808 809 810 811

  testWidgets('TabBarView scrolls end very VERY close to a new page', (WidgetTester tester) async {
    // This is a regression test for https://github.com/flutter/flutter/issues/9375

    final TabController tabController = new TabController(
      vsync: const TestVSync(),
      initialIndex: 1,
      length: 3,
    );

812 813 814
    await tester.pumpWidget(new Directionality(
      textDirection: TextDirection.ltr,
      child: new SizedBox.expand(
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
        child: new Center(
          child: new SizedBox(
            width: 400.0,
            height: 400.0,
            child: new TabBarView(
              controller: tabController,
              children: <Widget>[
                const Center(child: const Text('0')),
                const Center(child: const Text('1')),
                const Center(child: const Text('2')),
              ],
            ),
          ),
        ),
      ),
830
    ));
831 832 833 834 835 836

    expect(tabController.index, 1);

    final PageView pageView = tester.widget(find.byType(PageView));
    final PageController pageController = pageView.controller;
    final ScrollPosition position = pageController.position;
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858

    // The TabBarView's page width is 400, so page 0 is at scroll offset 0.0,
    // page 1 is at 400.0, page 2 is at 800.0.

    expect(position.pixels, 400.0);

    // Not close enough to switch to page 2
    pageController.jumpTo(800.0 - 1.25 * position.physics.tolerance.distance);
    expect(tabController.index, 1);

    // Close enough to switch to page 2
    pageController.jumpTo(800.0 - 0.75 * position.physics.tolerance.distance);
    expect(tabController.index, 2);
  });

  testWidgets('TabBarView scrolls end very close to a new page with custom physics', (WidgetTester tester) async {
    final TabController tabController = new TabController(
      vsync: const TestVSync(),
      initialIndex: 1,
      length: 3,
    );

859 860 861
    await tester.pumpWidget(new Directionality(
      textDirection: TextDirection.ltr,
      child: new SizedBox.expand(
862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
        child: new Center(
          child: new SizedBox(
            width: 400.0,
            height: 400.0,
            child: new TabBarView(
              controller: tabController,
              physics: const TestScrollPhysics(),
              children: <Widget>[
                const Center(child: const Text('0')),
                const Center(child: const Text('1')),
                const Center(child: const Text('2')),
              ],
            ),
          ),
        ),
      ),
878
    ));
879 880 881 882 883 884

    expect(tabController.index, 1);

    final PageView pageView = tester.widget(find.byType(PageView));
    final PageController pageController = pageView.controller;
    final ScrollPosition position = pageController.position;
885 886 887 888 889 890 891 892 893 894 895 896 897

    // The TabBarView's page width is 400, so page 0 is at scroll offset 0.0,
    // page 1 is at 400.0, page 2 is at 800.0.

    expect(position.pixels, 400.0);

    // Not close enough to switch to page 2
    pageController.jumpTo(800.0 - 1.25 * position.physics.tolerance.distance);
    expect(tabController.index, 1);

    // Close enough to switch to page 2
    pageController.jumpTo(800.0 - 0.75 * position.physics.tolerance.distance);
    expect(tabController.index, 2);
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
  });

  testWidgets('Scrollable TabBar with a non-zero TabController initialIndex', (WidgetTester tester) async {
    // This is a regression test for https://github.com/flutter/flutter/issues/9374

    final List<Tab> tabs = new List<Tab>.generate(20, (int index) {
      return new Tab(text: 'TAB #$index');
    });

    final TabController controller = new TabController(
      vsync: const TestVSync(),
      length: tabs.length,
      initialIndex: tabs.length - 1,
    );

    await tester.pumpWidget(
914
      boilerplate(
915 916 917 918 919 920 921
        child: new TabBar(
          isScrollable: true,
          controller: controller,
          tabs: tabs,
        ),
      ),
    );
922

923 924 925
    // The initialIndex tab should be visible and right justified
    expect(find.text('TAB #19'), findsOneWidget);
    expect(tester.getTopRight(find.widgetWithText(Tab, 'TAB #19')).dx, 800.0);
926
  });
927

Ian Hickson's avatar
Ian Hickson committed
928
  testWidgets('TabBar with indicatorWeight, indicatorPadding (LTR)', (WidgetTester tester) async {
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947
    const Color color = const Color(0xFF00FF00);
    const double height = 100.0;
    const double weight = 8.0;
    const double padLeft = 8.0;
    const double padRight = 4.0;

    final List<Widget> tabs = new List<Widget>.generate(4, (int index) {
      return new Container(
        key: new ValueKey<int>(index),
        height: height,
      );
    });

    final TabController controller = new TabController(
      vsync: const TestVSync(),
      length: tabs.length,
    );

    await tester.pumpWidget(
948
      boilerplate(
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
        child: new Column(
          children: <Widget>[
            new TabBar(
              indicatorWeight: 8.0,
              indicatorColor: color,
              indicatorPadding: const EdgeInsets.only(left: padLeft, right: padRight),
              controller: controller,
              tabs: tabs,
            ),
            new Flexible(child: new Container()),
          ],
        ),
      ),
    );

    final RenderBox tabBarBox = tester.firstRenderObject<RenderBox>(find.byType(TabBar));

    // Selected tab dimensions
    double tabWidth = tester.getSize(find.byKey(const ValueKey<int>(0))).width;
    double tabLeft = tester.getTopLeft(find.byKey(const ValueKey<int>(0))).dx;
    double tabRight = tabLeft + tabWidth;

    expect(tabBarBox, paints..rect(
      style: PaintingStyle.fill,
      color: color,
      rect: new Rect.fromLTRB(tabLeft + padLeft, height, tabRight - padRight, height + weight)
    ));

    // Select tab 3
    controller.index = 3;
    await tester.pumpAndSettle();

    tabWidth = tester.getSize(find.byKey(const ValueKey<int>(3))).width;
    tabLeft = tester.getTopLeft(find.byKey(const ValueKey<int>(3))).dx;
    tabRight = tabLeft + tabWidth;

    expect(tabBarBox, paints..rect(
      style: PaintingStyle.fill,
      color: color,
      rect: new Rect.fromLTRB(tabLeft + padLeft, height, tabRight - padRight, height + weight)
    ));
  });
991

Ian Hickson's avatar
Ian Hickson committed
992 993 994 995 996 997 998 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 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 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 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 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 1134 1135 1136 1137 1138 1139 1140 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
  testWidgets('TabBar with indicatorWeight, indicatorPadding (RTL)', (WidgetTester tester) async {
    const Color color = const Color(0xFF00FF00);
    const double height = 100.0;
    const double weight = 8.0;
    const double padLeft = 8.0;
    const double padRight = 4.0;

    final List<Widget> tabs = new List<Widget>.generate(4, (int index) {
      return new Container(
        key: new ValueKey<int>(index),
        height: height,
      );
    });

    final TabController controller = new TabController(
      vsync: const TestVSync(),
      length: tabs.length,
    );

    await tester.pumpWidget(
      boilerplate(
        textDirection: TextDirection.rtl,
        child: new Column(
          children: <Widget>[
            new TabBar(
              indicatorWeight: 8.0,
              indicatorColor: color,
              indicatorPadding: const EdgeInsets.only(left: padLeft, right: padRight),
              controller: controller,
              tabs: tabs,
            ),
            new Flexible(child: new Container()),
          ],
        ),
      ),
    );

    final RenderBox tabBarBox = tester.firstRenderObject<RenderBox>(find.byType(TabBar));

    // Selected tab dimensions
    double tabWidth = tester.getSize(find.byKey(const ValueKey<int>(0))).width;
    double tabLeft = tester.getTopLeft(find.byKey(const ValueKey<int>(0))).dx;
    double tabRight = tabLeft + tabWidth;

    expect(tabBarBox, paints..rect(
      style: PaintingStyle.fill,
      color: color,
      rect: new Rect.fromLTRB(tabLeft + padLeft, height, tabRight - padRight, height + weight)
    ));

    // Select tab 3
    controller.index = 3;
    await tester.pumpAndSettle();

    tabWidth = tester.getSize(find.byKey(const ValueKey<int>(3))).width;
    tabLeft = tester.getTopLeft(find.byKey(const ValueKey<int>(3))).dx;
    tabRight = tabLeft + tabWidth;

    expect(tabBarBox, paints..rect(
      style: PaintingStyle.fill,
      color: color,
      rect: new Rect.fromLTRB(tabLeft + padLeft, height, tabRight - padRight, height + weight)
    ));
  });

  testWidgets('TabBar with directional indicatorPadding (LTR)', (WidgetTester tester) async {
    final List<Widget> tabs = <Widget>[
      new SizedBox(key: new UniqueKey(), width: 130.0, height: 30.0),
      new SizedBox(key: new UniqueKey(), width: 140.0, height: 40.0),
      new SizedBox(key: new UniqueKey(), width: 150.0, height: 50.0),
    ];

    final TabController controller = new TabController(
      vsync: const TestVSync(),
      length: tabs.length,
    );

    await tester.pumpWidget(
      boilerplate(
        child: new Center(
          child: new SizedBox(
            width: 800.0,
            child: new TabBar(
              indicatorPadding: const EdgeInsetsDirectional.only(start: 100.0),
              isScrollable: true,
              controller: controller,
              tabs: tabs,
            ),
          ),
        ),
      ),
    );

    expect(tester.getRect(find.byKey(tabs[0].key)), new Rect.fromLTRB(0.0, 284.0, 130.0, 314.0));
    expect(tester.getRect(find.byKey(tabs[1].key)), new Rect.fromLTRB(130.0, 279.0, 270.0, 319.0));
    expect(tester.getRect(find.byKey(tabs[2].key)), new Rect.fromLTRB(270.0, 274.0, 420.0, 324.0));

    expect(tester.firstRenderObject<RenderBox>(find.byType(TabBar)), paints..rect(
      style: PaintingStyle.fill,
      rect: new Rect.fromLTRB(100.0, 50.0, 130.0, 52.0),
    ));
  });

  testWidgets('TabBar with directional indicatorPadding (RTL)', (WidgetTester tester) async {
    final List<Widget> tabs = <Widget>[
      new SizedBox(key: new UniqueKey(), width: 130.0, height: 30.0),
      new SizedBox(key: new UniqueKey(), width: 140.0, height: 40.0),
      new SizedBox(key: new UniqueKey(), width: 150.0, height: 50.0),
    ];

    final TabController controller = new TabController(
      vsync: const TestVSync(),
      length: tabs.length,
    );

    await tester.pumpWidget(
      boilerplate(
        textDirection: TextDirection.rtl,
        child: new Center(
          child: new SizedBox(
            width: 800.0,
            child: new TabBar(
              indicatorPadding: const EdgeInsetsDirectional.only(start: 100.0),
              isScrollable: true,
              controller: controller,
              tabs: tabs,
            ),
          ),
        ),
      ),
    );

    expect(tester.getRect(find.byKey(tabs[0].key)), new Rect.fromLTRB(670.0, 284.0, 800.0, 314.0));
    expect(tester.getRect(find.byKey(tabs[1].key)), new Rect.fromLTRB(530.0, 279.0, 670.0, 319.0));
    expect(tester.getRect(find.byKey(tabs[2].key)), new Rect.fromLTRB(380.0, 274.0, 530.0, 324.0));

    final RenderBox tabBar = tester.renderObject<RenderBox>(find.byType(CustomPaint).at(1));

    expect(tabBar.size, const Size(420.0, 52.0));
    expect(tabBar, paints..rect(
      style: PaintingStyle.fill,
      rect: new Rect.fromLTRB(tabBar.size.width - 130.0, 50.0, tabBar.size.width - 100.0, 52.0),
    ));
  });

  testWidgets('Overflowing RTL tab bar', (WidgetTester tester) async {
    final List<Widget> tabs = new List<Widget>.filled(100,
      new SizedBox(key: new UniqueKey(), width: 30.0, height: 20.0),
    );

    final TabController controller = new TabController(
      vsync: const TestVSync(),
      length: tabs.length,
    );

    await tester.pumpWidget(
      boilerplate(
        textDirection: TextDirection.rtl,
        child: new Center(
          child: new TabBar(
            isScrollable: true,
            controller: controller,
            tabs: tabs,
          ),
        ),
      ),
    );

    expect(tester.firstRenderObject<RenderBox>(find.byType(TabBar)), paints..rect(
      style: PaintingStyle.fill,
      rect: new Rect.fromLTRB(2970.0, 20.0, 3000.0, 22.0),
    ));

    controller.animateTo(tabs.length - 1, duration: const Duration(seconds: 1), curve: Curves.linear);
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 500));

    expect(tester.firstRenderObject<RenderBox>(find.byType(TabBar)), paints..rect(
      style: PaintingStyle.fill,
      rect: new Rect.fromLTRB(742.5, 20.0, 772.5, 22.0), // (these values were derived empirically, not analytically)
    ));

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

    expect(tester.firstRenderObject<RenderBox>(find.byType(TabBar)), paints..rect(
      style: PaintingStyle.fill,
      rect: new Rect.fromLTRB(0.0, 20.0, 30.0, 22.0),
    ));
  });

1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
  testWidgets('correct semantics', (WidgetTester tester) async {
    final SemanticsTester semantics = new SemanticsTester(tester);

    final List<Tab> tabs = new List<Tab>.generate(2, (int index) {
      return new Tab(text: 'TAB #$index');
    });

    final TabController controller = new TabController(
      vsync: const TestVSync(),
      length: tabs.length,
      initialIndex: 0,
    );

    await tester.pumpWidget(
1196
      boilerplate(
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
        child: new Semantics(
          container: true,
          child: new TabBar(
            isScrollable: true,
            controller: controller,
            tabs: tabs,
          ),
        ),
      ),
    );

    final TestSemantics expectedSemantics = new TestSemantics.root(
      children: <TestSemantics>[
        new TestSemantics.rootChild(
1211
          id: 1,
1212 1213 1214
          rect: TestSemantics.fullScreen,
          children: <TestSemantics>[
            new TestSemantics(
1215
              id: 2,
1216 1217
              actions: SemanticsAction.tap.index,
              flags: SemanticsFlags.isSelected.index,
1218
              label: 'TAB #0\nTab 1 of 2',
1219
              rect: new Rect.fromLTRB(0.0, 0.0, 108.0, kTextTabBarHeight),
1220 1221 1222
              transform: new Matrix4.translationValues(0.0, 276.0, 0.0),
            ),
            new TestSemantics(
1223
              id: 3,
1224
              actions: SemanticsAction.tap.index,
1225
              label: 'TAB #1\nTab 2 of 2',
1226
              rect: new Rect.fromLTRB(0.0, 0.0, 108.0, kTextTabBarHeight),
1227 1228
              transform: new Matrix4.translationValues(108.0, 276.0, 0.0),
            ),
1229 1230
          ],
        ),
1231 1232 1233 1234 1235 1236 1237
      ],
    );

    expect(semantics, hasSemantics(expectedSemantics));

    semantics.dispose();
  });
1238

1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
  testWidgets('correct scrolling semantics', (WidgetTester tester) async {
    final SemanticsTester semantics = new SemanticsTester(tester);

    final List<Tab> tabs = new List<Tab>.generate(20, (int index) {
      return new Tab(text: 'This is a very wide tab #$index');
    });

    final TabController controller = new TabController(
      vsync: const TestVSync(),
      length: tabs.length,
      initialIndex: 0,
    );

    await tester.pumpWidget(
      boilerplate(
        child: new Semantics(
          container: true,
          child: new TabBar(
            isScrollable: true,
            controller: controller,
            tabs: tabs,
          ),
        ),
      ),
    );

1265 1266 1267
    const String tab0title = 'This is a very wide tab #0\nTab 1 of 20';
    const String tab10title = 'This is a very wide tab #10\nTab 11 of 20';

1268
    expect(semantics, includesNodeWith(actions: <SemanticsAction>[SemanticsAction.scrollLeft]));
1269 1270
    expect(semantics, includesNodeWith(label: tab0title));
    expect(semantics, isNot(includesNodeWith(label: tab10title)));
1271 1272 1273 1274

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

1275
    expect(semantics, isNot(includesNodeWith(label: tab0title)));
1276
    expect(semantics, includesNodeWith(actions: <SemanticsAction>[SemanticsAction.scrollLeft, SemanticsAction.scrollRight]));
1277
    expect(semantics, includesNodeWith(label: tab10title));
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287

    controller.index = 19;
    await tester.pumpAndSettle();

    expect(semantics, includesNodeWith(actions: <SemanticsAction>[SemanticsAction.scrollRight]));

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

    expect(semantics, includesNodeWith(actions: <SemanticsAction>[SemanticsAction.scrollLeft]));
1288 1289
    expect(semantics, includesNodeWith(label: tab0title));
    expect(semantics, isNot(includesNodeWith(label: tab10title)));
1290 1291 1292 1293

    semantics.dispose();
  });

1294 1295 1296 1297 1298 1299 1300
  testWidgets('TabBar etc with zero tabs', (WidgetTester tester) async {
    final TabController controller = new TabController(
      vsync: const TestVSync(),
      length: 0,
    );

    await tester.pumpWidget(
1301
      boilerplate(
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
        child: new Column(
          children: <Widget>[
            new TabBar(
              controller: controller,
              tabs: const <Widget>[],
            ),
            new Flexible(
              child: new TabBarView(
                controller: controller,
                children: const <Widget>[],
              ),
            ),
          ],
        ),
      ),
    );

    expect(controller.index, 0);
    expect(tester.getSize(find.byType(TabBar)), const Size(800.0, 48.0));
    expect(tester.getSize(find.byType(TabBarView)), const Size(800.0, 600.0 - 48.0));

    // A fling in the TabBar or TabBarView, shouldn't do anything.

    await(tester.fling(find.byType(TabBar), const Offset(-100.0, 0.0), 5000.0));
    await(tester.pumpAndSettle());

    await(tester.fling(find.byType(TabBarView), const Offset(100.0, 0.0), 5000.0));
    await(tester.pumpAndSettle());

    expect(controller.index, 0);
  });

  testWidgets('TabBar etc with one tab', (WidgetTester tester) async {
    final TabController controller = new TabController(
      vsync: const TestVSync(),
      length: 1,
    );

    await tester.pumpWidget(
1341
      boilerplate(
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 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
        child: new Column(
          children: <Widget>[
            new TabBar(
              controller: controller,
              tabs: const <Widget>[const Tab(text: 'TAB')],
            ),
            new Flexible(
              child: new TabBarView(
                controller: controller,
                children: const <Widget>[const Text('PAGE')],
              ),
            ),
          ],
        ),
      ),
    );

    expect(controller.index, 0);
    expect(find.text('TAB'), findsOneWidget);
    expect(find.text('PAGE'), findsOneWidget);
    expect(tester.getSize(find.byType(TabBar)), const Size(800.0, 48.0));
    expect(tester.getSize(find.byType(TabBarView)), const Size(800.0, 600.0 - 48.0));

    // The one tab spans the app's width
    expect(tester.getTopLeft(find.widgetWithText(Tab, 'TAB')).dx, 0);
    expect(tester.getTopRight(find.widgetWithText(Tab, 'TAB')).dx, 800);

    // A fling in the TabBar or TabBarView, shouldn't move the tab.

    await(tester.fling(find.byType(TabBar), const Offset(-100.0, 0.0), 5000.0));
    await(tester.pump(const Duration(milliseconds: 50)));
    expect(tester.getTopLeft(find.widgetWithText(Tab, 'TAB')).dx, 0);
    expect(tester.getTopRight(find.widgetWithText(Tab, 'TAB')).dx, 800);
    await(tester.pumpAndSettle());

    await(tester.fling(find.byType(TabBarView), const Offset(100.0, 0.0), 5000.0));
    await(tester.pump(const Duration(milliseconds: 50)));
    expect(tester.getTopLeft(find.widgetWithText(Tab, 'TAB')).dx, 0);
    expect(tester.getTopRight(find.widgetWithText(Tab, 'TAB')).dx, 800);
    await(tester.pumpAndSettle());

    expect(controller.index, 0);
    expect(find.text('TAB'), findsOneWidget);
    expect(find.text('PAGE'), findsOneWidget);
  });

1388 1389 1390 1391 1392 1393 1394 1395
  testWidgets('can tap on indicator at very bottom of TabBar to switch tabs', (WidgetTester tester) async {
    final TabController controller = new TabController(
      vsync: const TestVSync(),
      length: 2,
      initialIndex: 0,
    );

    await tester.pumpWidget(
1396
      boilerplate(
1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423
        child: new Column(
          children: <Widget>[
            new TabBar(
              controller: controller,
              indicatorWeight: 30.0,
              tabs: const <Widget>[const Tab(text: 'TAB1'), const Tab(text: 'TAB2')],
            ),
            new Flexible(
              child: new TabBarView(
                controller: controller,
                children: const <Widget>[const Text('PAGE1'), const Text('PAGE2')],
              ),
            ),
          ],
        ),
      ),
    );

    expect(controller.index, 0);

    final Offset bottomRight = tester.getBottomRight(find.byType(TabBar)) - const Offset(1.0, 1.0);
    final TestGesture gesture = await tester.startGesture(bottomRight);
    await gesture.up();
    await tester.pumpAndSettle();

    expect(controller.index, 1);
  });
1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460

  testWidgets('can override semantics of tabs', (WidgetTester tester) async {
    final SemanticsTester semantics = new SemanticsTester(tester);

    final List<Tab> tabs = new List<Tab>.generate(2, (int index) {
      return new Tab(
        child: new Semantics(
          label: 'Semantics override $index',
          child: new ExcludeSemantics(
            child: new Text('TAB #$index'),
          ),
        ),
      );
    });

    final TabController controller = new TabController(
      vsync: const TestVSync(),
      length: tabs.length,
      initialIndex: 0,
    );

    await tester.pumpWidget(
      boilerplate(
        child: new Semantics(
          container: true,
          child: new TabBar(
            isScrollable: true,
            controller: controller,
            tabs: tabs,
          ),
        ),
      ),
    );

    final TestSemantics expectedSemantics = new TestSemantics.root(
      children: <TestSemantics>[
        new TestSemantics.rootChild(
1461
          id: 23,
1462 1463 1464
          rect: TestSemantics.fullScreen,
          children: <TestSemantics>[
            new TestSemantics(
1465
              id: 24,
1466 1467 1468 1469 1470 1471 1472
              actions: SemanticsAction.tap.index,
              flags: SemanticsFlags.isSelected.index,
              label: 'Semantics override 0\nTab 1 of 2',
              rect: new Rect.fromLTRB(0.0, 0.0, 108.0, kTextTabBarHeight),
              transform: new Matrix4.translationValues(0.0, 276.0, 0.0),
            ),
            new TestSemantics(
1473
              id: 25,
1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494
              actions: SemanticsAction.tap.index,
              label: 'Semantics override 1\nTab 2 of 2',
              rect: new Rect.fromLTRB(0.0, 0.0, 108.0, kTextTabBarHeight),
              transform: new Matrix4.translationValues(108.0, 276.0, 0.0),
            ),
          ],
        ),
      ],
    );

    expect(semantics, hasSemantics(expectedSemantics));

    semantics.dispose();
  });

  test('illegal constructor combinations', () {
    final Widget $null = null;
    expect(() => new Tab(icon: $null), throwsAssertionError);
    expect(() => new Tab(icon: new Container(), text: 'foo', child: new Container()), throwsAssertionError);
    expect(() => new Tab(text: 'foo', child: new Container()), throwsAssertionError);
  });
1495
}