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

5 6 7
// This file is run as part of a reduced test set in CI on Mac and Windows
// machines.
@Tags(<String>['reduced-test-set'])
8
library;
9

10 11
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
12

13
void main() {
14 15 16 17 18 19
  test('BottomAppBarTheme lerp special cases', () {
    expect(BottomAppBarTheme.lerp(null, null, 0), const BottomAppBarTheme());
    const BottomAppBarTheme data = BottomAppBarTheme();
    expect(identical(BottomAppBarTheme.lerp(data, data, 0.5), data), true);
  });

20
  group('Material 2 tests', () {
21
    testWidgets('Material2 - BAB theme overrides color', (WidgetTester tester) async {
22 23
      const Color themedColor = Colors.black87;
      const BottomAppBarTheme theme = BottomAppBarTheme(color: themedColor);
24

25
      await tester.pumpWidget(_withTheme(theme, useMaterial3: false));
26

27 28 29
      final PhysicalShape widget = _getBabRenderObject(tester);
      expect(widget.color, themedColor);
    });
30

31
    testWidgets('Material2 - BAB color - Widget', (WidgetTester tester) async {
32 33 34
      const Color babThemeColor = Colors.black87;
      const Color babColor = Colors.pink;
      const BottomAppBarTheme theme = BottomAppBarTheme(color: babThemeColor);
35

36 37
      await tester.pumpWidget(MaterialApp(
        theme: ThemeData(
38
          useMaterial3: false,
39 40 41 42
          bottomAppBarTheme: theme,
        ),
        home: const Scaffold(body: BottomAppBar(color: babColor)),
      ));
43

44 45 46
      final PhysicalShape widget = _getBabRenderObject(tester);
      expect(widget.color, babColor);
    });
47

48
    testWidgets('Material2 - BAB color - BabTheme', (WidgetTester tester) async {
49 50
      const Color babThemeColor = Colors.black87;
      const BottomAppBarTheme theme = BottomAppBarTheme(color: babThemeColor);
51

52 53
      await tester.pumpWidget(MaterialApp(
        theme: ThemeData(
54
          useMaterial3: false,
55 56 57 58
          bottomAppBarTheme: theme,
        ),
        home: const Scaffold(body: BottomAppBar()),
      ));
59

60 61 62
      final PhysicalShape widget = _getBabRenderObject(tester);
      expect(widget.color, babThemeColor);
    });
63

64
    testWidgets('Material2 - BAB color - Theme', (WidgetTester tester) async {
65
      const Color themeColor = Colors.white10;
66

67
      await tester.pumpWidget(MaterialApp(
68
        theme: ThemeData(useMaterial3: false, bottomAppBarTheme: const BottomAppBarTheme(color: themeColor)),
69 70
        home: const Scaffold(body: BottomAppBar()),
      ));
71

72 73 74
      final PhysicalShape widget = _getBabRenderObject(tester);
      expect(widget.color, themeColor);
    });
75

76
    testWidgets('Material2 - BAB color - Default', (WidgetTester tester) async {
77
      await tester.pumpWidget(MaterialApp(
78
        theme: ThemeData(useMaterial3: false),
79 80
        home: const Scaffold(body: BottomAppBar()),
      ));
81

82
      final PhysicalShape widget = _getBabRenderObject(tester);
83

84 85
      expect(widget.color, Colors.white);
    });
86

87
    testWidgets('Material2 - BAB theme customizes shape', (WidgetTester tester) async {
88
      const BottomAppBarTheme theme = BottomAppBarTheme(
89 90 91
        color: Colors.white30,
        shape: CircularNotchedRectangle(),
        elevation: 1.0,
92 93
      );

94
      await tester.pumpWidget(_withTheme(theme, useMaterial3: false));
95 96 97 98 99 100

      await expectLater(
        find.byKey(_painterKey),
        matchesGoldenFile('bottom_app_bar_theme.custom_shape.png'),
      );
    });
101

102
    testWidgets('Material2 - BAB theme does not affect defaults', (WidgetTester tester) async {
103 104 105
      await tester.pumpWidget(MaterialApp(
        theme: ThemeData(useMaterial3: false),
        home: const Scaffold(body: BottomAppBar()),
106
      ));
107

108 109 110 111 112
      final PhysicalShape widget = _getBabRenderObject(tester);

      expect(widget.color, Colors.white);
      expect(widget.elevation, equals(8.0));
    });
113
  });
114

115
  group('Material 3 tests', () {
116
    testWidgets('Material3 - BAB theme overrides color', (WidgetTester tester) async {
117 118 119 120 121
      const Color themedColor = Colors.black87;
      const BottomAppBarTheme theme = BottomAppBarTheme(
        color: themedColor,
        elevation: 0
      );
122
      await tester.pumpWidget(_withTheme(theme, useMaterial3: true));
123 124 125 126 127

      final PhysicalShape widget = _getBabRenderObject(tester);
      expect(widget.color, themedColor);
    });

128
    testWidgets('Material3 - BAB color - Widget', (WidgetTester tester) async {
129 130 131 132 133 134 135 136 137
      const Color babThemeColor = Colors.black87;
      const Color babColor = Colors.pink;
      const BottomAppBarTheme theme = BottomAppBarTheme(color: babThemeColor);

      await tester.pumpWidget(MaterialApp(
        theme: ThemeData(
          useMaterial3: true,
          bottomAppBarTheme: theme,
        ),
138
        home: const Scaffold(body: BottomAppBar(color: babColor, surfaceTintColor: Colors.transparent)),
139 140 141 142 143
      ));

      final PhysicalShape widget = _getBabRenderObject(tester);
      expect(widget.color, babColor);
    });
144

145
    testWidgets('Material3 - BAB color - BabTheme', (WidgetTester tester) async {
146 147
      const Color babThemeColor = Colors.black87;
      const BottomAppBarTheme theme = BottomAppBarTheme(color: babThemeColor);
148

149 150 151 152 153
      await tester.pumpWidget(MaterialApp(
        theme: ThemeData(
          useMaterial3: true,
          bottomAppBarTheme: theme,
        ),
154
        home: const Scaffold(body: BottomAppBar(surfaceTintColor: Colors.transparent)),
155 156 157 158 159 160
      ));

      final PhysicalShape widget = _getBabRenderObject(tester);
      expect(widget.color, babThemeColor);
    });

161
    testWidgets('Material3 - BAB theme does not affect defaults', (WidgetTester tester) async {
162 163 164
      final ThemeData theme = ThemeData(useMaterial3: true);
      await tester.pumpWidget(MaterialApp(
        theme: theme,
165
        home: const Scaffold(body: BottomAppBar(surfaceTintColor: Colors.transparent)),
166 167
      ));

168
      final PhysicalShape widget = _getBabRenderObject(tester);
169

170
      expect(widget.color, theme.colorScheme.surfaceContainer);
171
      expect(widget.elevation, equals(3.0));
172 173
    });

174
    testWidgets('Material3 - BAB theme overrides surfaceTintColor', (WidgetTester tester) async {
175
      const Color color = Colors.blue; // base color that the surface tint will be applied to
176 177
      const Color babThemeSurfaceTintColor = Colors.black87;
      const BottomAppBarTheme theme = BottomAppBarTheme(
178
          color: color, surfaceTintColor: babThemeSurfaceTintColor, elevation: 0,
179
      );
180
      await tester.pumpWidget(_withTheme(theme, useMaterial3: true));
181

182 183
      final PhysicalShape widget = _getBabRenderObject(tester);
      expect(widget.color, ElevationOverlay.applySurfaceTint(color, babThemeSurfaceTintColor, 0));
184 185
    });

186
    testWidgets('Material3 - BAB theme overrides shadowColor', (WidgetTester tester) async {
187 188 189 190
      const Color babThemeShadowColor = Colors.yellow;
      const BottomAppBarTheme theme = BottomAppBarTheme(
        shadowColor: babThemeShadowColor, elevation: 0
      );
191
      await tester.pumpWidget(_withTheme(theme, useMaterial3: true));
192

193
      final PhysicalShape widget = _getBabRenderObject(tester);
194 195 196
      expect(widget.shadowColor, babThemeShadowColor);
    });

197
    testWidgets('Material3 - BAB surfaceTintColor - Widget', (WidgetTester tester) async {
198
      const Color color = Colors.white10; // base color that the surface tint will be applied to
199 200 201 202 203 204 205 206 207 208 209
      const Color babThemeSurfaceTintColor = Colors.black87;
      const Color babSurfaceTintColor = Colors.pink;
      const BottomAppBarTheme theme = BottomAppBarTheme(
        surfaceTintColor: babThemeSurfaceTintColor
      );
      await tester.pumpWidget(MaterialApp(
        theme: ThemeData(
          useMaterial3: true,
          bottomAppBarTheme: theme,
        ),
        home: const Scaffold(
210
          body: BottomAppBar(color: color, surfaceTintColor: babSurfaceTintColor)
211 212 213
        ),
      ));

214 215
      final PhysicalShape widget = _getBabRenderObject(tester);
      expect(widget.color, ElevationOverlay.applySurfaceTint(color, babSurfaceTintColor, 3.0));
216 217
    });

218
    testWidgets('Material3 - BAB surfaceTintColor - BabTheme', (WidgetTester tester) async {
219
      const Color color = Colors.blue; // base color that the surface tint will be applied to
220 221 222 223 224 225 226 227 228 229
      const Color babThemeColor = Colors.black87;
      const BottomAppBarTheme theme = BottomAppBarTheme(
        surfaceTintColor: babThemeColor
      );

      await tester.pumpWidget(MaterialApp(
        theme: ThemeData(
          useMaterial3: true,
          bottomAppBarTheme: theme,
        ),
230
        home: const Scaffold(body: BottomAppBar(color: color)),
231 232
      ));

233 234
      final PhysicalShape widget = _getBabRenderObject(tester);
      expect(widget.color, ElevationOverlay.applySurfaceTint(color, babThemeColor, 3.0));
235
    });
236 237 238 239 240 241
  });
}

PhysicalShape _getBabRenderObject(WidgetTester tester) {
  return tester.widget<PhysicalShape>(
      find.descendant(
242 243
        of: find.byType(BottomAppBar),
        matching: find.byType(PhysicalShape),
244 245 246 247 248 249
      ),
  );
}

final Key _painterKey = UniqueKey();

250
Widget _withTheme(BottomAppBarTheme theme, {required bool useMaterial3}) {
251
  return MaterialApp(
252
    theme: ThemeData(useMaterial3: useMaterial3, bottomAppBarTheme: theme),
253
    home: Scaffold(
254 255 256 257
      floatingActionButton: const FloatingActionButton(onPressed: null),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
      bottomNavigationBar: RepaintBoundary(
        key: _painterKey,
258
        child: const BottomAppBar(
259
          child: Row(
260
            children: <Widget>[
261 262 263 264
              Icon(Icons.add),
              Expanded(child: SizedBox()),
              Icon(Icons.add),
            ],
265
          ),
266
        ),
267
      ),
268 269 270
    ),
  );
}