Unverified Commit 7fc49906 authored by Qun Cheng's avatar Qun Cheng Committed by GitHub

Update `CardTheme`, `DrawerTheme`, `NavigationBar`, and `NavigationRailTheme`...

Update `CardTheme`, `DrawerTheme`, `NavigationBar`, and `NavigationRailTheme` tests for M2/M3  (#130047)

Updated unit tests for `CardTheme`, `DrawerTheme`, `NavigationBar`, and `NavigationRailTheme` to have M2 and M3 versions.

More info in https://github.com/flutter/flutter/issues/127064
parent e1d025ae
......@@ -22,7 +22,7 @@ void main() {
expect(identical(CardTheme.lerp(theme, theme, 0.5), theme), true);
});
testWidgets('Passing no CardTheme returns defaults', (WidgetTester tester) async {
testWidgets('Material3 - Passing no CardTheme returns defaults', (WidgetTester tester) async {
final ThemeData theme = ThemeData(useMaterial3: true);
await tester.pumpWidget(MaterialApp(
theme: theme,
......@@ -117,9 +117,8 @@ void main() {
expect(material.color, cardTheme.color);
});
testWidgets('ThemeData properties are used when no CardTheme is set', (WidgetTester tester) async {
final ThemeData themeData = _themeData();
final bool material3 = themeData.useMaterial3;
testWidgets('Material3 - ThemeData properties are used when no CardTheme is set', (WidgetTester tester) async {
final ThemeData themeData = ThemeData(useMaterial3: true);
await tester.pumpWidget(MaterialApp(
theme: themeData,
......@@ -129,10 +128,10 @@ void main() {
));
final Material material = _getCardMaterial(tester);
expect(material.color, material3 ? themeData.colorScheme.surface: themeData.cardColor);
expect(material.color, themeData.colorScheme.surface);
});
testWidgets('CardTheme customizes shape', (WidgetTester tester) async {
testWidgets('Material3 - CardTheme customizes shape', (WidgetTester tester) async {
const CardTheme cardTheme = CardTheme(
color: Colors.white,
shape: BeveledRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(7))),
......@@ -166,7 +165,21 @@ void main() {
// support is deprecated and the APIs are removed, these tests
// can be deleted.
testWidgets('Passing no CardTheme returns defaults - M2', (WidgetTester tester) async {
testWidgets('Material2 - ThemeData properties are used when no CardTheme is set', (WidgetTester tester) async {
final ThemeData themeData = ThemeData(useMaterial3: false);
await tester.pumpWidget(MaterialApp(
theme: themeData,
home: const Scaffold(
body: Card(),
),
));
final Material material = _getCardMaterial(tester);
expect(material.color, themeData.cardColor);
});
testWidgets('Material2 - Passing no CardTheme returns defaults', (WidgetTester tester) async {
await tester.pumpWidget(MaterialApp(
theme: ThemeData(useMaterial3: false),
home: const Scaffold(
......@@ -188,7 +201,7 @@ void main() {
));
});
testWidgets('CardTheme customizes shape - M2', (WidgetTester tester) async {
testWidgets('Material2 - CardTheme customizes shape', (WidgetTester tester) async {
const CardTheme cardTheme = CardTheme(
color: Colors.white,
shape: BeveledRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(7))),
......
......@@ -58,10 +58,9 @@ void main() {
]);
});
testWidgets('Default values are used when no Drawer or DrawerThemeData properties are specified', (WidgetTester tester) async {
testWidgets('Material2 - Default values are used when no Drawer or DrawerThemeData properties are specified', (WidgetTester tester) async {
final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
final ThemeData theme = ThemeData();
final bool useMaterial3 = theme.useMaterial3;
final ThemeData theme = ThemeData(useMaterial3: false);
await tester.pumpWidget(
MaterialApp(
theme: theme,
......@@ -74,24 +73,69 @@ void main() {
scaffoldKey.currentState!.openDrawer();
await tester.pumpAndSettle();
expect(_drawerMaterial(tester).color, useMaterial3 ? theme.colorScheme.surface : null);
expect(_drawerMaterial(tester).elevation, useMaterial3 ? 1.0 : 16.0);
expect(_drawerMaterial(tester).shadowColor, useMaterial3 ? Colors.transparent : ThemeData().shadowColor);
expect(_drawerMaterial(tester).surfaceTintColor, useMaterial3 ? theme.colorScheme.surfaceTint : null);
expect(_drawerMaterial(tester).color, null);
expect(_drawerMaterial(tester).elevation, 16.0);
expect(_drawerMaterial(tester).shadowColor, theme.shadowColor);
expect(_drawerMaterial(tester).surfaceTintColor, null);
expect(_drawerMaterial(tester).shape, null);
expect(_scrim(tester).color, Colors.black54);
expect(_drawerRenderBox(tester).size.width, 304.0);
});
testWidgets('Material3 - Default values are used when no Drawer or DrawerThemeData properties are specified', (WidgetTester tester) async {
final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
final ThemeData theme = ThemeData(useMaterial3: true);
await tester.pumpWidget(
MaterialApp(
theme: theme,
home: Scaffold(
key: scaffoldKey,
drawer: const Drawer(),
),
),
);
scaffoldKey.currentState!.openDrawer();
await tester.pumpAndSettle();
expect(_drawerMaterial(tester).color, theme.colorScheme.surface);
expect(_drawerMaterial(tester).elevation, 1.0);
expect(_drawerMaterial(tester).shadowColor, Colors.transparent);
expect(_drawerMaterial(tester).surfaceTintColor, theme.colorScheme.surfaceTint);
expect(
_drawerMaterial(tester).shape,
useMaterial3
? const RoundedRectangleBorder(borderRadius: BorderRadius.horizontal(right: Radius.circular(16.0)))
: null,
const RoundedRectangleBorder(borderRadius: BorderRadius.horizontal(right: Radius.circular(16.0)))
);
expect(_scrim(tester).color, Colors.black54);
expect(_drawerRenderBox(tester).size.width, 304.0);
});
testWidgets('Default values are used when no Drawer or DrawerThemeData properties are specified in end drawer', (WidgetTester tester) async {
testWidgets('Material2 - Default values are used when no Drawer or DrawerThemeData properties are specified in end drawer', (WidgetTester tester) async {
final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
final ThemeData theme = ThemeData(useMaterial3: false);
await tester.pumpWidget(
MaterialApp(
theme: theme,
home: Scaffold(
key: scaffoldKey,
endDrawer: const Drawer(),
),
),
);
scaffoldKey.currentState!.openEndDrawer();
await tester.pumpAndSettle();
expect(_drawerMaterial(tester).color, null);
expect(_drawerMaterial(tester).elevation, 16.0);
expect(_drawerMaterial(tester).shadowColor, theme.shadowColor);
expect(_drawerMaterial(tester).surfaceTintColor, null);
expect(_drawerMaterial(tester).shape, null);
expect(_scrim(tester).color, Colors.black54);
expect(_drawerRenderBox(tester).size.width, 304.0);
});
testWidgets('Material3 - Default values are used when no Drawer or DrawerThemeData properties are specified in end drawer', (WidgetTester tester) async {
final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
final ThemeData theme = ThemeData();
final bool useMaterial3 = theme.useMaterial3;
final ThemeData theme = ThemeData(useMaterial3: true);
await tester.pumpWidget(
MaterialApp(
theme: theme,
......@@ -104,15 +148,13 @@ void main() {
scaffoldKey.currentState!.openEndDrawer();
await tester.pumpAndSettle();
expect(_drawerMaterial(tester).color, useMaterial3 ? theme.colorScheme.surface : null);
expect(_drawerMaterial(tester).elevation, useMaterial3 ? 1.0 : 16.0);
expect(_drawerMaterial(tester).shadowColor, useMaterial3 ? Colors.transparent : ThemeData().shadowColor);
expect(_drawerMaterial(tester).surfaceTintColor, useMaterial3 ? ThemeData().colorScheme.surfaceTint : null);
expect(_drawerMaterial(tester).color, theme.colorScheme.surface);
expect(_drawerMaterial(tester).elevation, 1.0);
expect(_drawerMaterial(tester).shadowColor, Colors.transparent);
expect(_drawerMaterial(tester).surfaceTintColor, theme.colorScheme.surfaceTint);
expect(
_drawerMaterial(tester).shape,
useMaterial3
? const RoundedRectangleBorder(borderRadius: BorderRadius.horizontal(left: Radius.circular(16.0)))
: null,
const RoundedRectangleBorder(borderRadius: BorderRadius.horizontal(left: Radius.circular(16.0)))
);
expect(_scrim(tester).color, Colors.black54);
expect(_drawerRenderBox(tester).size.width, 304.0);
......
......@@ -246,7 +246,7 @@ void main() {
);
});
testWidgets('NavigationBar uses proper defaults when no parameters are given - M2', (WidgetTester tester) async {
testWidgets('Material2 - NavigationBar uses proper defaults when no parameters are given', (WidgetTester tester) async {
// M2 settings that were hand coded.
await tester.pumpWidget(
_buildWidget(
......@@ -275,7 +275,7 @@ void main() {
expect(_getIndicatorDecoration(tester)?.shape, RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)));
});
testWidgets('NavigationBar uses proper defaults when no parameters are given - M3', (WidgetTester tester) async {
testWidgets('Material3 - NavigationBar uses proper defaults when no parameters are given', (WidgetTester tester) async {
// M3 settings from the token database.
final ThemeData theme = ThemeData(useMaterial3: true);
await tester.pumpWidget(
......@@ -305,7 +305,7 @@ void main() {
expect(_getIndicatorDecoration(tester)?.shape, const StadiumBorder());
});
testWidgets('NavigationBar shows tooltips with text scaling ', (WidgetTester tester) async {
testWidgets('Material2 - NavigationBar shows tooltips with text scaling', (WidgetTester tester) async {
const String label = 'A';
Widget buildApp({ required double textScaleFactor }) {
......@@ -364,6 +364,72 @@ void main() {
expect(tester.getSize(find.text(label).last), Size(defaultTooltipSize.width * 4, defaultTooltipSize.height * 4));
});
testWidgets('Material3 - NavigationBar shows tooltips with text scaling', (WidgetTester tester) async {
const String label = 'A';
Widget buildApp({ required double textScaleFactor }) {
return MediaQuery(
data: MediaQueryData(textScaleFactor: textScaleFactor),
child: Localizations(
locale: const Locale('en', 'US'),
delegates: const <LocalizationsDelegate<dynamic>>[
DefaultMaterialLocalizations.delegate,
DefaultWidgetsLocalizations.delegate,
],
child: MaterialApp(
theme: ThemeData(useMaterial3: true),
home: Navigator(
onGenerateRoute: (RouteSettings settings) {
return MaterialPageRoute<void>(
builder: (BuildContext context) {
return Scaffold(
bottomNavigationBar: NavigationBar(
destinations: const <NavigationDestination>[
NavigationDestination(
label: label,
icon: Icon(Icons.ac_unit),
tooltip: label,
),
NavigationDestination(
label: 'B',
icon: Icon(Icons.battery_alert),
),
],
),
);
},
);
},
),
),
),
);
}
await tester.pumpWidget(buildApp(textScaleFactor: 1.0));
expect(find.text(label), findsOneWidget);
await tester.longPress(find.text(label));
expect(find.text(label), findsNWidgets(2));
if (kIsWeb && !isCanvasKit) {
expect(tester.getSize(find.text(label).last), const Size(15.0, 21.0));
} else {
expect(tester.getSize(find.text(label).last), const Size(15.0, 20.0));
}
// The duration is needed to ensure the tooltip disappears.
await tester.pumpAndSettle(const Duration(seconds: 2));
await tester.pumpWidget(buildApp(textScaleFactor: 4.0));
expect(find.text(label), findsOneWidget);
await tester.longPress(find.text(label));
if (kIsWeb && !isCanvasKit) {
expect(tester.getSize(find.text(label).last), const Size(57.0, 81.0));
} else {
expect(tester.getSize(find.text(label).last), const Size(57.0, 80.0));
}
});
testWidgets('Custom tooltips in NavigationBarDestination', (WidgetTester tester) async {
await tester.pumpWidget(
MaterialApp(
......@@ -566,7 +632,7 @@ void main() {
expect(newHeight, equals(initialHeight));
});
testWidgets('Navigation indicator renders ripple', (WidgetTester tester) async {
testWidgets('Material3 - Navigation indicator renders ripple', (WidgetTester tester) async {
// This is a regression test for https://github.com/flutter/flutter/issues/116751.
int selectedIndex = 0;
......@@ -767,7 +833,7 @@ void main() {
);
});
testWidgets('Navigation indicator ripple golden test', (WidgetTester tester) async {
testWidgets('Material3 - Navigation indicator ripple golden test', (WidgetTester tester) async {
// This is a regression test for https://github.com/flutter/flutter/issues/117420.
Widget buildWidget({ NavigationDestinationLabelBehavior? labelBehavior }) {
......@@ -875,7 +941,7 @@ void main() {
expect(transform.getColumn(0)[0], 1.0);
});
testWidgets('Navigation destination updates indicator color and shape', (WidgetTester tester) async {
testWidgets('Material3 - Navigation destination updates indicator color and shape', (WidgetTester tester) async {
final ThemeData theme = ThemeData(useMaterial3: true);
const Color color = Color(0xff0000ff);
const ShapeBorder shape = RoundedRectangleBorder();
......@@ -921,7 +987,7 @@ void main() {
// support is deprecated and the APIs are removed, these tests
// can be deleted.
testWidgets('Navigation destination updates indicator color and shape', (WidgetTester tester) async {
testWidgets('Material2 - Navigation destination updates indicator color and shape', (WidgetTester tester) async {
final ThemeData theme = ThemeData(useMaterial3: false);
const Color color = Color(0xff0000ff);
const ShapeBorder shape = RoundedRectangleBorder();
......@@ -965,7 +1031,7 @@ void main() {
expect(_getIndicatorDecoration(tester)?.shape, shape);
});
testWidgets('Navigation indicator renders ripple', (WidgetTester tester) async {
testWidgets('Material2 - Navigation indicator renders ripple', (WidgetTester tester) async {
// This is a regression test for https://github.com/flutter/flutter/issues/116751.
int selectedIndex = 0;
......@@ -1166,7 +1232,7 @@ void main() {
);
});
testWidgets('Navigation indicator ripple golden test', (WidgetTester tester) async {
testWidgets('Material2 - Navigation indicator ripple golden test', (WidgetTester tester) async {
// This is a regression test for https://github.com/flutter/flutter/issues/117420.
Widget buildWidget({ NavigationDestinationLabelBehavior? labelBehavior }) {
......
......@@ -12,7 +12,7 @@ void main() {
expect(const NavigationRailThemeData().hashCode, const NavigationRailThemeData().copyWith().hashCode);
});
testWidgets('Default values are used when no NavigationRail or NavigationRailThemeData properties are specified', (WidgetTester tester) async {
testWidgets('Material3 - Default values are used when no NavigationRail or NavigationRailThemeData properties are specified', (WidgetTester tester) async {
final ThemeData theme = ThemeData.light(useMaterial3: true);
// Material 3 defaults
await tester.pumpWidget(
......@@ -47,7 +47,7 @@ void main() {
expect(inkResponse.customBorder, const StadiumBorder());
});
testWidgets('Default values are used when no NavigationRail or NavigationRailThemeData properties are specified (Material 2)', (WidgetTester tester) async {
testWidgets('Material2 - Default values are used when no NavigationRail or NavigationRailThemeData properties are specified', (WidgetTester tester) async {
// This test can be removed when `useMaterial3` is deprecated.
await tester.pumpWidget(
MaterialApp(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment