tabs_test.dart 6.11 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.

Adam Barth's avatar
Adam Barth committed
5
import 'package:flutter_test/flutter_test.dart';
6 7 8 9
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:test/test.dart';

Hans Muller's avatar
Hans Muller committed
10
Widget buildFrame({ List<String> tabs, String value, bool isScrollable: false, Key tabBarKey }) {
11
  return new Material(
12 13 14 15
    child: new TabBarSelection<String>(
      value: value,
      values: tabs,
      child: new TabBar<String>(
Hans Muller's avatar
Hans Muller committed
16
        key: tabBarKey,
17
        labels: new Map<String, TabLabel>.fromIterable(tabs, value: (String tab) => new TabLabel(text: tab)),
18 19
        isScrollable: isScrollable
      )
20
    )
21 22 23 24 25 26 27 28
  );
}

void main() {
  test('TabBar tap selects tab', () {
    testWidgets((WidgetTester tester) {
      List<String> tabs = <String>['A', 'B', 'C'];

29 30
      tester.pumpWidget(buildFrame(tabs: tabs, value: 'C', isScrollable: false));
      TabBarSelectionState<String> selection = TabBarSelection.of(tester.findText('A'));
31
      expect(selection, isNotNull);
32 33 34
      expect(selection.indexOf('A'), equals(0));
      expect(selection.indexOf('B'), equals(1));
      expect(selection.indexOf('C'), equals(2));
35 36 37
      expect(tester.findText('A'), isNotNull);
      expect(tester.findText('B'), isNotNull);
      expect(tester.findText('C'), isNotNull);
38
      expect(selection.index, equals(2));
39 40 41
      expect(selection.previousIndex, equals(2));
      expect(selection.value, equals('C'));
      expect(selection.previousValue, equals('C'));
42

43
      tester.pumpWidget(buildFrame(tabs: tabs, value: 'C' ,isScrollable: false));
44 45
      tester.tap(tester.findText('B'));
      tester.pump();
46 47 48 49 50
      expect(selection.valueIsChanging, true);
      tester.pump(const Duration(seconds: 1)); // finish the animation
      expect(selection.valueIsChanging, false);
      expect(selection.value, equals('B'));
      expect(selection.previousValue, equals('C'));
51
      expect(selection.index, equals(1));
52
      expect(selection.previousIndex, equals(2));
53

54
      tester.pumpWidget(buildFrame(tabs: tabs, value: 'C', isScrollable: false));
55 56
      tester.tap(tester.findText('C'));
      tester.pump();
57 58 59
      tester.pump(const Duration(seconds: 1));
      expect(selection.value, equals('C'));
      expect(selection.previousValue, equals('B'));
60
      expect(selection.index, equals(2));
61
      expect(selection.previousIndex, equals(1));
62

63
      tester.pumpWidget(buildFrame(tabs: tabs, value: 'C', isScrollable: false));
64 65
      tester.tap(tester.findText('A'));
      tester.pump();
66 67 68
      tester.pump(const Duration(seconds: 1));
      expect(selection.value, equals('A'));
      expect(selection.previousValue, equals('C'));
69
      expect(selection.index, equals(0));
70
      expect(selection.previousIndex, equals(2));
71 72 73 74 75 76 77
    });
  });

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

78 79
      tester.pumpWidget(buildFrame(tabs: tabs, value: 'C', isScrollable: true));
      TabBarSelectionState<String> selection = TabBarSelection.of(tester.findText('A'));
80
      expect(selection, isNotNull);
81 82 83
      expect(tester.findText('A'), isNotNull);
      expect(tester.findText('B'), isNotNull);
      expect(tester.findText('C'), isNotNull);
84
      expect(selection.value, equals('C'));
85

86
      tester.pumpWidget(buildFrame(tabs: tabs, value: 'C', isScrollable: true));
87 88
      tester.tap(tester.findText('B'));
      tester.pump();
89
      expect(selection.value, equals('B'));
90

91
      tester.pumpWidget(buildFrame(tabs: tabs, value: 'C', isScrollable: true));
92 93
      tester.tap(tester.findText('C'));
      tester.pump();
94
      expect(selection.value, equals('C'));
95

96
      tester.pumpWidget(buildFrame(tabs: tabs, value: 'C', isScrollable: true));
97 98
      tester.tap(tester.findText('A'));
      tester.pump();
99
      expect(selection.value, equals('A'));
100 101
    });
  });
Hans Muller's avatar
Hans Muller committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145

  test('Scrollable TabBar tap centers selected tab', () {
    testWidgets((WidgetTester tester) {
      List<String> tabs = <String>['AAAAAA', 'BBBBBB', 'CCCCCC', 'DDDDDD', 'EEEEEE', 'FFFFFF', 'GGGGGG', 'HHHHHH', 'IIIIII', 'JJJJJJ', 'KKKKKK', 'LLLLLL'];
      Key tabBarKey = new Key('TabBar');
      tester.pumpWidget(buildFrame(tabs: tabs, value: 'AAAAAA', isScrollable: true, tabBarKey: tabBarKey));
      TabBarSelectionState<String> selection = TabBarSelection.of(tester.findText('AAAAAA'));
      expect(selection, isNotNull);
      expect(selection.value, equals('AAAAAA'));

      expect(tester.getSize(tester.findElementByKey(tabBarKey)).width, equals(800.0));
      // The center of the FFFFFF item is to the right of the TabBar's center
      expect(tester.getCenter(tester.findText('FFFFFF')).x, greaterThan(401.0));

      tester.tap(tester.findText('FFFFFF'));
      tester.pump();
      tester.pump(const Duration(seconds: 1)); // finish the scroll animation
      expect(selection.value, equals('FFFFFF'));
      // The center of the FFFFFF item is now at the TabBar's center
      expect(tester.getCenter(tester.findText('FFFFFF')).x, closeTo(400.0, 1.0));
    });
  });


  test('TabBar can be scrolled independent of the selection', () {
    testWidgets((WidgetTester tester) {
      List<String> tabs = <String>['AAAAAA', 'BBBBBB', 'CCCCCC', 'DDDDDD', 'EEEEEE', 'FFFFFF', 'GGGGGG', 'HHHHHH', 'IIIIII', 'JJJJJJ', 'KKKKKK', 'LLLLLL'];
      Key tabBarKey = new Key('TabBar');
      tester.pumpWidget(buildFrame(tabs: tabs, value: 'AAAAAA', isScrollable: true, tabBarKey: tabBarKey));
      TabBarSelectionState<String> selection = TabBarSelection.of(tester.findText('AAAAAA'));
      expect(selection, isNotNull);
      expect(selection.value, equals('AAAAAA'));

      // Fling-scroll the TabBar to the left
      expect(tester.getCenter(tester.findText('HHHHHH')).x, lessThan(700.0));
      tester.fling(tester.findElementByKey(tabBarKey), const Offset(-20.0, 0.0), 1000.0);
      tester.pump();
      tester.pump(const Duration(seconds: 1)); // finish the scroll animation
      expect(tester.getCenter(tester.findText('HHHHHH')).x, lessThan(500.0));

      // Scrolling the TabBar doesn't change the selection
      expect(selection.value, equals('AAAAAA'));
    });
  });
146
}