Skip to content

Commit cea728a

Browse files
authored
Update tests in material library for Material 3 by default (#128300)
1 parent ce248e8 commit cea728a

19 files changed

+952
-513
lines changed

packages/flutter/test/material/card_theme_test.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ void main() {
119119

120120
testWidgets('ThemeData properties are used when no CardTheme is set', (WidgetTester tester) async {
121121
final ThemeData themeData = _themeData();
122+
final bool material3 = themeData.useMaterial3;
122123

123124
await tester.pumpWidget(MaterialApp(
124125
theme: themeData,
@@ -128,7 +129,7 @@ void main() {
128129
));
129130

130131
final Material material = _getCardMaterial(tester);
131-
expect(material.color, themeData.cardColor);
132+
expect(material.color, material3 ? themeData.colorScheme.surface: themeData.cardColor);
132133
});
133134

134135
testWidgets('CardTheme customizes shape', (WidgetTester tester) async {

packages/flutter/test/material/checkbox_test.dart

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -474,14 +474,16 @@ void main() {
474474
});
475475

476476
testWidgets('Checkbox color rendering', (WidgetTester tester) async {
477+
final ThemeData theme = ThemeData();
477478
const Color borderColor = Color(0xff2196f3);
479+
const Color m3BorderColor = Color(0xFF6750A4);
478480
Color checkColor = const Color(0xffFFFFFF);
479481
Color activeColor;
480482

481483
Widget buildFrame({Color? activeColor, Color? checkColor, ThemeData? themeData}) {
482484
return Material(
483485
child: Theme(
484-
data: themeData ?? ThemeData(),
486+
data: themeData ?? theme,
485487
child: StatefulBuilder(
486488
builder: (BuildContext context, StateSetter setState) {
487489
return Checkbox(
@@ -502,13 +504,13 @@ void main() {
502504

503505
await tester.pumpWidget(buildFrame(checkColor: checkColor));
504506
await tester.pumpAndSettle();
505-
expect(getCheckboxRenderer(), paints..path(color: borderColor)..path(color: checkColor)); // paints's color is 0xFFFFFFFF (default color)
507+
expect(getCheckboxRenderer(), paints..path(color: theme.useMaterial3 ? m3BorderColor : borderColor)..path(color: checkColor)); // paints's color is 0xFFFFFFFF (default color)
506508

507509
checkColor = const Color(0xFF000000);
508510

509511
await tester.pumpWidget(buildFrame(checkColor: checkColor));
510512
await tester.pumpAndSettle();
511-
expect(getCheckboxRenderer(), paints..path(color: borderColor)..path(color: checkColor)); // paints's color is 0xFF000000 (params)
513+
expect(getCheckboxRenderer(), paints..path(color: theme.useMaterial3 ? m3BorderColor : borderColor)..path(color: checkColor)); // paints's color is 0xFF000000 (params)
512514

513515
activeColor = const Color(0xFF00FF00);
514516

@@ -520,7 +522,7 @@ void main() {
520522
themeData = themeData.copyWith(colorScheme: colorScheme);
521523
await tester.pumpWidget(buildFrame(
522524
themeData: themeData),
523-
);
525+
);
524526
await tester.pumpAndSettle();
525527
expect(getCheckboxRenderer(), paints..path(color: activeColor)); // paints's color is 0xFF00FF00 (theme)
526528

@@ -567,7 +569,7 @@ void main() {
567569
material3
568570
? (paints
569571
..circle(color: Colors.orange[500])
570-
..path(color: const Color(0xff2196f3))
572+
..path(color: theme.colorScheme.primary)
571573
..path(color: theme.colorScheme.onPrimary))
572574
: (paints
573575
..circle(color: Colors.orange[500])
@@ -586,7 +588,7 @@ void main() {
586588
..circle(color: Colors.orange[500])
587589
..drrect(
588590
color: material3 ? theme.colorScheme.onSurface : const Color(0x8a000000),
589-
outer: RRect.fromLTRBR(15.0, 15.0, 33.0, 33.0, const Radius.circular(1.0)),
591+
outer: RRect.fromLTRBR(15.0, 15.0, 33.0, 33.0, material3 ? const Radius.circular(2.0) : const Radius.circular(1.0)),
590592
inner: RRect.fromLTRBR(17.0, 17.0, 31.0, 31.0, Radius.zero),
591593
),
592594
);
@@ -601,7 +603,7 @@ void main() {
601603
paints
602604
..drrect(
603605
color: material3 ? theme.colorScheme.onSurface.withOpacity(0.38) : const Color(0x61000000),
604-
outer: RRect.fromLTRBR(15.0, 15.0, 33.0, 33.0, const Radius.circular(1.0)),
606+
outer: RRect.fromLTRBR(15.0, 15.0, 33.0, 33.0, material3 ? const Radius.circular(2.0) : const Radius.circular(1.0)),
605607
inner: RRect.fromLTRBR(17.0, 17.0, 31.0, 31.0, Radius.zero),
606608
),
607609
);
@@ -697,7 +699,7 @@ void main() {
697699
expect(
698700
Material.of(tester.element(find.byType(Checkbox))),
699701
paints
700-
..path(color: const Color(0xff2196f3))
702+
..path(color: material3 ? const Color(0xff6750a4) : const Color(0xff2196f3))
701703
..path(color: material3 ? theme.colorScheme.onPrimary : const Color(0xffffffff), style: PaintingStyle.stroke, strokeWidth: 2.0),
702704
);
703705

@@ -710,7 +712,7 @@ void main() {
710712
expect(
711713
Material.of(tester.element(find.byType(Checkbox))),
712714
paints
713-
..path(color: const Color(0xff2196f3))
715+
..path(color: material3 ? const Color(0xff6750a4) : const Color(0xff2196f3))
714716
..path(color: material3 ? theme.colorScheme.onPrimary : const Color(0xffffffff), style: PaintingStyle.stroke, strokeWidth: 2.0),
715717
);
716718

@@ -1450,7 +1452,7 @@ void main() {
14501452
await gesture.up();
14511453
});
14521454

1453-
testWidgets('Checkbox BorderSide side only applies when unselected', (WidgetTester tester) async {
1455+
testWidgets('Checkbox BorderSide side only applies when unselected in M2', (WidgetTester tester) async {
14541456
const Color borderColor = Color(0xfff44336);
14551457
const Color activeColor = Color(0xff123456);
14561458
const BorderSide side = BorderSide(
@@ -1460,7 +1462,7 @@ void main() {
14601462

14611463
Widget buildApp({ bool? value, bool enabled = true }) {
14621464
return MaterialApp(
1463-
theme: theme,
1465+
theme: ThemeData(useMaterial3: false),
14641466
home: Material(
14651467
child: Center(
14661468
child: Checkbox(
@@ -1520,6 +1522,7 @@ void main() {
15201522
width: 4,
15211523
color: borderColor,
15221524
);
1525+
final bool material3 = theme.useMaterial3;
15231526

15241527
Widget buildApp({ bool? value, bool enabled = true }) {
15251528
return MaterialApp(
@@ -1543,7 +1546,7 @@ void main() {
15431546
paints
15441547
..drrect(
15451548
color: borderColor,
1546-
outer: RRect.fromLTRBR(15, 15, 33, 33, const Radius.circular(1)),
1549+
outer: material3 ? RRect.fromLTRBR(15, 15, 33, 33, const Radius.circular(2)) : RRect.fromLTRBR(15, 15, 33, 33, const Radius.circular(1)),
15471550
inner: RRect.fromLTRBR(19, 19, 29, 29, Radius.zero),
15481551
),
15491552
);

packages/flutter/test/material/drawer_theme_test.dart

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,11 @@ void main() {
6060

6161
testWidgets('Default values are used when no Drawer or DrawerThemeData properties are specified', (WidgetTester tester) async {
6262
final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
63-
final bool useMaterial3 = ThemeData().useMaterial3;
63+
final ThemeData theme = ThemeData();
64+
final bool useMaterial3 = theme.useMaterial3;
6465
await tester.pumpWidget(
6566
MaterialApp(
67+
theme: theme,
6668
home: Scaffold(
6769
key: scaffoldKey,
6870
drawer: const Drawer(),
@@ -72,10 +74,10 @@ void main() {
7274
scaffoldKey.currentState!.openDrawer();
7375
await tester.pumpAndSettle();
7476

75-
expect(_drawerMaterial(tester).color, null);
76-
expect(_drawerMaterial(tester).elevation, 16.0);
77+
expect(_drawerMaterial(tester).color, useMaterial3 ? theme.colorScheme.surface : null);
78+
expect(_drawerMaterial(tester).elevation, useMaterial3 ? 1.0 : 16.0);
7779
expect(_drawerMaterial(tester).shadowColor, useMaterial3 ? Colors.transparent : ThemeData().shadowColor);
78-
expect(_drawerMaterial(tester).surfaceTintColor, useMaterial3 ? ThemeData().colorScheme.surfaceTint : null);
80+
expect(_drawerMaterial(tester).surfaceTintColor, useMaterial3 ? theme.colorScheme.surfaceTint : null);
7981
expect(
8082
_drawerMaterial(tester).shape,
8183
useMaterial3
@@ -88,9 +90,11 @@ void main() {
8890

8991
testWidgets('Default values are used when no Drawer or DrawerThemeData properties are specified in end drawer', (WidgetTester tester) async {
9092
final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
91-
final bool useMaterial3 = ThemeData().useMaterial3;
93+
final ThemeData theme = ThemeData();
94+
final bool useMaterial3 = theme.useMaterial3;
9295
await tester.pumpWidget(
9396
MaterialApp(
97+
theme: theme,
9498
home: Scaffold(
9599
key: scaffoldKey,
96100
endDrawer: const Drawer(),
@@ -100,8 +104,8 @@ void main() {
100104
scaffoldKey.currentState!.openEndDrawer();
101105
await tester.pumpAndSettle();
102106

103-
expect(_drawerMaterial(tester).color, null);
104-
expect(_drawerMaterial(tester).elevation, 16.0);
107+
expect(_drawerMaterial(tester).color, useMaterial3 ? theme.colorScheme.surface : null);
108+
expect(_drawerMaterial(tester).elevation, useMaterial3 ? 1.0 : 16.0);
105109
expect(_drawerMaterial(tester).shadowColor, useMaterial3 ? Colors.transparent : ThemeData().shadowColor);
106110
expect(_drawerMaterial(tester).surfaceTintColor, useMaterial3 ? ThemeData().colorScheme.surfaceTint : null);
107111
expect(

packages/flutter/test/material/dropdown_menu_test.dart

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ void main() {
113113
(WidgetTester tester) async {
114114

115115
final ThemeData themeData = ThemeData();
116+
final bool useMaterial3 = themeData.useMaterial3;
116117
await tester.pumpWidget(
117118
MaterialApp(
118119
theme: themeData,
@@ -128,7 +129,7 @@ void main() {
128129

129130
final Finder textField = find.byType(TextField);
130131
final Size anchorSize = tester.getSize(textField);
131-
expect(anchorSize, const Size(180.0, 56.0));
132+
expect(anchorSize, useMaterial3 ? const Size(195.0, 60.0) : const Size(180.0, 56.0));
132133

133134
await tester.tap(find.byType(DropdownMenu<TestMenu>));
134135
await tester.pumpAndSettle();
@@ -138,15 +139,15 @@ void main() {
138139
matching: find.byType(Material),
139140
);
140141
final Size menuSize = tester.getSize(menuMaterial);
141-
expect(menuSize, const Size(180.0, 304.0));
142+
expect(menuSize, useMaterial3 ? const Size(195.0, 304.0) : const Size(180.0, 304.0));
142143

143144
// The text field should have same width as the menu
144145
// when the width property is not null.
145146
await tester.pumpWidget(buildTest(themeData, menuChildren, width: 200.0));
146147

147148
final Finder anchor = find.byType(TextField);
148149
final Size size = tester.getSize(anchor);
149-
expect(size, const Size(200.0, 56.0));
150+
expect(size, useMaterial3 ? const Size(200.0, 60.0) : const Size(200.0, 56.0));
150151

151152
await tester.tap(anchor);
152153
await tester.pumpAndSettle();
@@ -218,6 +219,7 @@ void main() {
218219
testWidgets('The menuHeight property can be used to show a shorter scrollable menu list instead of the complete list',
219220
(WidgetTester tester) async {
220221
final ThemeData themeData = ThemeData();
222+
final bool material3 = themeData.useMaterial3;
221223
await tester.pumpWidget(buildTest(themeData, menuChildren));
222224

223225
await tester.tap(find.byType(DropdownMenu<TestMenu>));
@@ -237,7 +239,7 @@ void main() {
237239
matching: find.byType(Padding),
238240
).first;
239241
final Size menuViewSize = tester.getSize(menuView);
240-
expect(menuViewSize, const Size(180.0, 304.0)); // 304 = 288 + vertical padding(2 * 8)
242+
expect(menuViewSize, material3 ? const Size(195.0, 304.0) : const Size(180.0, 304.0)); // 304 = 288 + vertical padding(2 * 8)
241243

242244
// Constrains the menu height.
243245
await tester.pumpWidget(Container());
@@ -253,7 +255,7 @@ void main() {
253255
).first;
254256

255257
final Size updatedMenuSize = tester.getSize(updatedMenu);
256-
expect(updatedMenuSize, const Size(180.0, 100.0));
258+
expect(updatedMenuSize, material3 ? const Size(195.0, 100.0) : const Size(180.0, 100.0));
257259
});
258260

259261
testWidgets('The text in the menu button should be aligned with the text of '

packages/flutter/test/material/navigation_bar_test.dart

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,8 @@ void main() {
246246
);
247247
});
248248

249-
testWidgets('NavigationBar uses proper defaults when no parameters are given', (WidgetTester tester) async {
250-
// Pre-M3 settings that were hand coded.
249+
testWidgets('NavigationBar uses proper defaults when no parameters are given - M2', (WidgetTester tester) async {
250+
// M2 settings that were hand coded.
251251
await tester.pumpWidget(
252252
_buildWidget(
253253
NavigationBar(
@@ -263,6 +263,7 @@ void main() {
263263
],
264264
onDestinationSelected: (int i) {},
265265
),
266+
useMaterial3: false,
266267
),
267268
);
268269

@@ -272,13 +273,14 @@ void main() {
272273
expect(tester.getSize(find.byType(NavigationBar)).height, 80);
273274
expect(_getIndicatorDecoration(tester)?.color, const Color(0x3d2196f3));
274275
expect(_getIndicatorDecoration(tester)?.shape, RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)));
276+
});
275277

278+
testWidgets('NavigationBar uses proper defaults when no parameters are given - M3', (WidgetTester tester) async {
276279
// M3 settings from the token database.
280+
final ThemeData theme = ThemeData(useMaterial3: true);
277281
await tester.pumpWidget(
278282
_buildWidget(
279-
Theme(
280-
data: ThemeData.light().copyWith(useMaterial3: true),
281-
child: NavigationBar(
283+
NavigationBar(
282284
destinations: const <Widget>[
283285
NavigationDestination(
284286
icon: Icon(Icons.ac_unit),
@@ -291,15 +293,15 @@ void main() {
291293
],
292294
onDestinationSelected: (int i) {},
293295
),
294-
),
296+
useMaterial3: theme.useMaterial3
295297
),
296298
);
297299

298-
expect(_getMaterial(tester).color, ThemeData().colorScheme.surface);
299-
expect(_getMaterial(tester).surfaceTintColor, ThemeData().colorScheme.surfaceTint);
300+
expect(_getMaterial(tester).color, theme.colorScheme.surface);
301+
expect(_getMaterial(tester).surfaceTintColor, theme.colorScheme.surfaceTint);
300302
expect(_getMaterial(tester).elevation, 3);
301303
expect(tester.getSize(find.byType(NavigationBar)).height, 80);
302-
expect(_getIndicatorDecoration(tester)?.color, const Color(0xff2196f3));
304+
expect(_getIndicatorDecoration(tester)?.color, theme.colorScheme.secondaryContainer);
303305
expect(_getIndicatorDecoration(tester)?.shape, const StadiumBorder());
304306
});
305307

@@ -315,9 +317,9 @@ void main() {
315317
DefaultMaterialLocalizations.delegate,
316318
DefaultWidgetsLocalizations.delegate,
317319
],
318-
child: Directionality(
319-
textDirection: TextDirection.ltr,
320-
child: Navigator(
320+
child: MaterialApp(
321+
theme: ThemeData(useMaterial3: false),
322+
home: Navigator(
321323
onGenerateRoute: (RouteSettings settings) {
322324
return MaterialPageRoute<void>(
323325
builder: (BuildContext context) {
@@ -1269,9 +1271,9 @@ void main() {
12691271
});
12701272
}
12711273

1272-
Widget _buildWidget(Widget child) {
1274+
Widget _buildWidget(Widget child, { bool? useMaterial3 }) {
12731275
return MaterialApp(
1274-
theme: ThemeData.light(),
1276+
theme: ThemeData(useMaterial3: useMaterial3),
12751277
home: Scaffold(
12761278
bottomNavigationBar: Center(
12771279
child: child,

0 commit comments

Comments
 (0)