bottom_tab_bar_test.dart 5.63 KB
Newer Older
xster's avatar
xster committed
1 2 3 4 5 6 7
// 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';
import 'package:flutter_test/flutter_test.dart';

8
import '../painting/mocks_for_image_cache.dart';
xster's avatar
xster committed
9

10 11 12 13 14 15 16 17 18
Future<Null> pumpWidgetWithBoilerplate(WidgetTester tester, Widget widget) async {
  await tester.pumpWidget(
    new Directionality(
      textDirection: TextDirection.ltr,
      child: widget,
    ),
  );
}

xster's avatar
xster committed
19 20 21
void main() {
  testWidgets('Need at least 2 tabs', (WidgetTester tester) async {
    try {
22
      await pumpWidgetWithBoilerplate(tester, new CupertinoTabBar(
23
        items: const <BottomNavigationBarItem>[
xster's avatar
xster committed
24 25 26 27 28 29 30
          const BottomNavigationBarItem(
            icon: const ImageIcon(const TestImageProvider(24, 24)),
            title: const Text('Tab 1'),
          ),
        ],
      ));
      fail('Should not be possible to create a tab bar with just one item');
31 32
    } on AssertionError catch (e) {
      expect(e.toString(), contains('items.length'));
xster's avatar
xster committed
33 34 35 36 37
      // Exception expected.
    }
  });

  testWidgets('Active and inactive colors', (WidgetTester tester) async {
38 39 40
    await pumpWidgetWithBoilerplate(tester, new MediaQuery(
      data: const MediaQueryData(),
      child: new CupertinoTabBar(
41
        items: const <BottomNavigationBarItem>[
42 43 44 45 46 47 48 49 50 51 52 53 54
          const BottomNavigationBarItem(
            icon: const ImageIcon(const TestImageProvider(24, 24)),
            title: const Text('Tab 1'),
          ),
          const BottomNavigationBarItem(
            icon: const ImageIcon(const TestImageProvider(24, 24)),
            title: const Text('Tab 2'),
          ),
        ],
        currentIndex: 1,
        activeColor: const Color(0xFF123456),
        inactiveColor: const Color(0xFF654321),
      ),
xster's avatar
xster committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    ));

    final RichText actualInactive = tester.widget(find.descendant(
      of: find.text('Tab 1'),
      matching: find.byType(RichText),
    ));
    expect(actualInactive.text.style.color, const Color(0xFF654321));

    final RichText actualActive = tester.widget(find.descendant(
      of: find.text('Tab 2'),
      matching: find.byType(RichText),
    ));
    expect(actualActive.text.style.color, const Color(0xFF123456));
  });

70 71
  testWidgets('Adjusts height to account for bottom padding', (WidgetTester tester) async {
    final CupertinoTabBar tabBar = new CupertinoTabBar(
72
      items: const <BottomNavigationBarItem>[
xster's avatar
xster committed
73 74
        const BottomNavigationBarItem(
          icon: const ImageIcon(const TestImageProvider(24, 24)),
75
          title: const Text('Aka'),
xster's avatar
xster committed
76 77 78
        ),
        const BottomNavigationBarItem(
          icon: const ImageIcon(const TestImageProvider(24, 24)),
79
          title: const Text('Shiro'),
xster's avatar
xster committed
80 81
        ),
      ],
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    );

    // Verify height with no bottom padding.
    await pumpWidgetWithBoilerplate(tester, new MediaQuery(
      data: const MediaQueryData(),
      child: new CupertinoTabScaffold(
        tabBar: tabBar,
        tabBuilder: (BuildContext context, int index) {
          return const Placeholder();
        },
      ),
    ));
    expect(tester.getSize(find.byType(CupertinoTabBar)).height, 50.0);

    // Verify height with bottom padding.
    await pumpWidgetWithBoilerplate(tester, new MediaQuery(
      data: const MediaQueryData(padding: const EdgeInsets.only(bottom: 40.0)),
      child: new CupertinoTabScaffold(
        tabBar: tabBar,
        tabBuilder: (BuildContext context, int index) {
          return const Placeholder();
        },
      ),
    ));
    expect(tester.getSize(find.byType(CupertinoTabBar)).height, 90.0);
  });

  testWidgets('Opaque background does not add blur effects', (WidgetTester tester) async {
    await pumpWidgetWithBoilerplate(tester, new MediaQuery(
      data: const MediaQueryData(),
      child: new CupertinoTabBar(
113
        items: const <BottomNavigationBarItem>[
114 115 116 117 118 119 120 121 122 123
          const BottomNavigationBarItem(
            icon: const ImageIcon(const TestImageProvider(24, 24)),
            title: const Text('Tab 1'),
          ),
          const BottomNavigationBarItem(
            icon: const ImageIcon(const TestImageProvider(24, 24)),
            title: const Text('Tab 2'),
          ),
        ],
      ),
xster's avatar
xster committed
124 125 126 127
    ));

    expect(find.byType(BackdropFilter), findsOneWidget);

128 129 130
    await pumpWidgetWithBoilerplate(tester, new MediaQuery(
      data: const MediaQueryData(),
      child: new CupertinoTabBar(
131
        items: const <BottomNavigationBarItem>[
132 133 134 135 136 137 138 139 140 141 142
          const BottomNavigationBarItem(
            icon: const ImageIcon(const TestImageProvider(24, 24)),
            title: const Text('Tab 1'),
          ),
          const BottomNavigationBarItem(
            icon: const ImageIcon(const TestImageProvider(24, 24)),
            title: const Text('Tab 2'),
          ),
        ],
        backgroundColor: const Color(0xFFFFFFFF), // Opaque white.
      ),
xster's avatar
xster committed
143 144 145 146 147 148 149 150
    ));

    expect(find.byType(BackdropFilter), findsNothing);
  });

  testWidgets('Tap callback', (WidgetTester tester) async {
    int callbackTab;

151 152 153
      await pumpWidgetWithBoilerplate(tester, new MediaQuery(
        data: const MediaQueryData(),
        child: new CupertinoTabBar(
154
          items: const <BottomNavigationBarItem>[
155 156 157 158 159 160 161 162 163 164 165
            const BottomNavigationBarItem(
              icon: const ImageIcon(const TestImageProvider(24, 24)),
              title: const Text('Tab 1'),
            ),
            const BottomNavigationBarItem(
              icon: const ImageIcon(const TestImageProvider(24, 24)),
              title: const Text('Tab 2'),
            ),
          ],
          currentIndex: 1,
          onTap: (int tab) { callbackTab = tab; },
xster's avatar
xster committed
166 167 168 169 170 171
        ),
    ));

    await tester.tap(find.text('Tab 1'));
    expect(callbackTab, 0);
  });
172
}