tab_scaffold_test.dart 7.92 KB
Newer Older
1 2 3 4 5
// Copyright 2017 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.

import 'package:flutter/cupertino.dart';
6
import 'package:flutter/material.dart';
7 8
import 'package:flutter_test/flutter_test.dart';

9
import '../painting/mocks_for_image_cache.dart';
10 11 12 13 14 15 16 17 18 19 20 21 22
import '../rendering/rendering_tester.dart';

List<int> selectedTabs;

void main() {
  setUp(() {
    selectedTabs = <int>[];
  });

  testWidgets('Tab switching', (WidgetTester tester) async {
    final List<int> tabsPainted = <int>[];

    await tester.pumpWidget(
xster's avatar
xster committed
23 24 25 26 27 28 29 30 31 32 33 34
      new CupertinoApp(
        home: new CupertinoTabScaffold(
          tabBar: _buildTabBar(),
          tabBuilder: (BuildContext context, int index) {
            return new CustomPaint(
              child: new Text('Page ${index + 1}'),
              painter: new TestCallbackPainter(
                onPaint: () { tabsPainted.add(index); }
              )
            );
          },
        ),
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
      ),
    );

    expect(tabsPainted, <int>[0]);
    RichText tab1 = tester.widget(find.descendant(
      of: find.text('Tab 1'),
      matching: find.byType(RichText),
    ));
    expect(tab1.text.style.color, CupertinoColors.activeBlue);
    RichText tab2 = tester.widget(find.descendant(
      of: find.text('Tab 2'),
      matching: find.byType(RichText),
    ));
    expect(tab2.text.style.color, CupertinoColors.inactiveGray);

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

    expect(tabsPainted, <int>[0, 1]);
    tab1 = tester.widget(find.descendant(
      of: find.text('Tab 1'),
      matching: find.byType(RichText),
    ));
    expect(tab1.text.style.color, CupertinoColors.inactiveGray);
    tab2 = tester.widget(find.descendant(
      of: find.text('Tab 2'),
      matching: find.byType(RichText),
    ));
    expect(tab2.text.style.color, CupertinoColors.activeBlue);

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

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

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

    await tester.pumpWidget(
xster's avatar
xster committed
77 78 79 80 81 82 83 84
      new CupertinoApp(
        home: new CupertinoTabScaffold(
          tabBar: _buildTabBar(),
          tabBuilder: (BuildContext context, int index) {
            tabsBuilt.add(index);
            return new Text('Page ${index + 1}');
          },
        ),
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
      ),
    );

    expect(tabsBuilt, <int>[0]);
    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.
    expect(tabsBuilt, <int>[0, 0, 1]);
    expect(find.text('Page 1', skipOffstage: false), isOffstage);
    expect(find.text('Page 2'), findsOneWidget);

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

    expect(tabsBuilt, <int>[0, 0, 1, 0, 1]);
    expect(find.text('Page 1'), findsOneWidget);
    expect(find.text('Page 2', skipOffstage: false), isOffstage);
  });
107 108 109 110 111 112

  testWidgets('Last tab gets focus', (WidgetTester tester) async {
    // 2 nodes for 2 tabs
    final List<FocusNode> focusNodes = <FocusNode>[new FocusNode(), new FocusNode()];

    await tester.pumpWidget(
xster's avatar
xster committed
113 114 115 116 117 118 119 120
      new CupertinoApp(
        home: new Material(
          child: new CupertinoTabScaffold(
            tabBar: _buildTabBar(),
            tabBuilder: (BuildContext context, int index) {
              return new TextField(
                focusNode: focusNodes[index],
                autofocus: true,
121 122
              );
            },
xster's avatar
xster committed
123 124
          ),
        ),
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
      ),
    );

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

  testWidgets('Do not affect focus order in the route', (WidgetTester tester) async {
    final List<FocusNode> focusNodes = <FocusNode>[
      new FocusNode(), new FocusNode(), new FocusNode(), new FocusNode(),
    ];

    await tester.pumpWidget(
xster's avatar
xster committed
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
      new CupertinoApp(
        home: new Material(
          child: new CupertinoTabScaffold(
            tabBar: _buildTabBar(),
            tabBuilder: (BuildContext context, int index) {
              return new Column(
                children: <Widget>[
                  new TextField(
                    focusNode: focusNodes[index * 2],
                    decoration: const InputDecoration(
                      hintText: 'TextField 1',
                    ),
                  ),
                  new TextField(
                    focusNode: focusNodes[index * 2 + 1],
                    decoration: const InputDecoration(
                      hintText: 'TextField 2',
                    ),
                  ),
                ],
169 170
              );
            },
xster's avatar
xster committed
171 172
          ),
        ),
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
      ),
    );

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

    await tester.tap(find.widgetWithText(TextField, 'TextField 2'));

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

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

    await tester.tap(find.widgetWithText(TextField, 'TextField 1'));

    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,
    );
  });
208 209 210 211 212

  testWidgets('Programmatic tab switching', (WidgetTester tester) async {
    final List<int> tabsPainted = <int>[];

    await tester.pumpWidget(
xster's avatar
xster committed
213 214 215 216 217 218 219 220 221 222 223 224
      new CupertinoApp(
        home: new CupertinoTabScaffold(
          tabBar: _buildTabBar(),
          tabBuilder: (BuildContext context, int index) {
            return new CustomPaint(
              child: new Text('Page ${index + 1}'),
              painter: new TestCallbackPainter(
                onPaint: () { tabsPainted.add(index); }
              )
            );
          },
        ),
225 226 227 228 229 230
      ),
    );

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

    await tester.pumpWidget(
xster's avatar
xster committed
231 232 233 234 235 236 237 238 239 240 241 242
      new CupertinoApp(
        home: new CupertinoTabScaffold(
          tabBar: _buildTabBar(selectedTab: 1), // Programmatically change the tab now.
          tabBuilder: (BuildContext context, int index) {
            return new CustomPaint(
              child: new Text('Page ${index + 1}'),
              painter: new TestCallbackPainter(
                onPaint: () { tabsPainted.add(index); }
              )
            );
          },
        ),
243 244 245 246 247 248 249 250 251 252 253 254 255 256
      ),
    );

    expect(tabsPainted, <int>[0, 1]);
    // 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();

    expect(tabsPainted, <int>[0, 1, 0]);
    expect(selectedTabs, <int>[0]);
  });
257 258
}

259
CupertinoTabBar _buildTabBar({ int selectedTab = 0 }) {
260
  return new CupertinoTabBar(
261
    items: const <BottomNavigationBarItem>[
262 263 264
      const BottomNavigationBarItem(
        icon: const ImageIcon(const TestImageProvider(24, 24)),
        title: const Text('Tab 1'),
265
      ),
266 267 268
      const BottomNavigationBarItem(
        icon: const ImageIcon(const TestImageProvider(24, 24)),
        title: const Text('Tab 2'),
269 270 271
      ),
    ],
    backgroundColor: CupertinoColors.white,
272
    currentIndex: selectedTab,
273 274
    onTap: (int newTab) => selectedTabs.add(newTab),
  );
275
}