Unverified Commit f4da0962 authored by Qun Cheng's avatar Qun Cheng Committed by GitHub

Update `DropdownMenu`, `SnackBarTheme` and `Stepper` tests for M2/M3 (#130464)

Updated unit tests for `DropdownMenu`, `SnackBarTheme` and `Stepper` to have M2 and M3 versions.

More info in #127064
parent 5a866a03
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
import 'dart:ui'; import 'dart:ui';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart'; import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
...@@ -109,11 +110,10 @@ void main() { ...@@ -109,11 +110,10 @@ void main() {
expect(updatedMenuMaterial, findsNothing); expect(updatedMenuMaterial, findsNothing);
}); });
testWidgets('The width of the text field should always be the same as the menu view', testWidgets('Material2 - The width of the text field should always be the same as the menu view',
(WidgetTester tester) async { (WidgetTester tester) async {
final ThemeData themeData = ThemeData(useMaterial3: false); final ThemeData themeData = ThemeData(useMaterial3: false);
final bool useMaterial3 = themeData.useMaterial3;
await tester.pumpWidget( await tester.pumpWidget(
MaterialApp( MaterialApp(
theme: themeData, theme: themeData,
...@@ -129,7 +129,7 @@ void main() { ...@@ -129,7 +129,7 @@ void main() {
final Finder textField = find.byType(TextField); final Finder textField = find.byType(TextField);
final Size anchorSize = tester.getSize(textField); final Size anchorSize = tester.getSize(textField);
expect(anchorSize, useMaterial3 ? const Size(195.0, 60.0) : const Size(180.0, 56.0)); expect(anchorSize, const Size(180.0, 56.0));
await tester.tap(find.byType(DropdownMenu<TestMenu>)); await tester.tap(find.byType(DropdownMenu<TestMenu>));
await tester.pumpAndSettle(); await tester.pumpAndSettle();
...@@ -139,7 +139,7 @@ void main() { ...@@ -139,7 +139,7 @@ void main() {
matching: find.byType(Material), matching: find.byType(Material),
); );
final Size menuSize = tester.getSize(menuMaterial); final Size menuSize = tester.getSize(menuMaterial);
expect(menuSize, useMaterial3 ? const Size(195.0, 304.0) : const Size(180.0, 304.0)); expect(menuSize, const Size(180.0, 304.0));
// The text field should have same width as the menu // The text field should have same width as the menu
// when the width property is not null. // when the width property is not null.
...@@ -147,7 +147,64 @@ void main() { ...@@ -147,7 +147,64 @@ void main() {
final Finder anchor = find.byType(TextField); final Finder anchor = find.byType(TextField);
final Size size = tester.getSize(anchor); final Size size = tester.getSize(anchor);
expect(size, useMaterial3 ? const Size(200.0, 60.0) : const Size(200.0, 56.0)); expect(size, const Size(200.0, 56.0));
await tester.tap(anchor);
await tester.pumpAndSettle();
final Finder updatedMenu = find.ancestor(
of: find.byType(SingleChildScrollView),
matching: find.byType(Material),
);
final Size updatedMenuSize = tester.getSize(updatedMenu);
expect(updatedMenuSize, const Size(200.0, 304.0));
});
testWidgets('Material3 - The width of the text field should always be the same as the menu view',
(WidgetTester tester) async {
final ThemeData themeData = ThemeData(useMaterial3: true);
await tester.pumpWidget(
MaterialApp(
theme: themeData,
home: Scaffold(
body: SafeArea(
child: DropdownMenu<TestMenu>(
dropdownMenuEntries: menuChildren,
),
),
),
)
);
final Finder textField = find.byType(TextField);
final Size anchorSize = tester.getSize(textField);
if (kIsWeb && !isCanvasKit) {
expect(anchorSize, const Size(195.0, 61.0));
} else {
expect(anchorSize, const Size(195.0, 60.0));
}
await tester.tap(find.byType(DropdownMenu<TestMenu>));
await tester.pumpAndSettle();
final Finder menuMaterial = find.ancestor(
of: find.byType(SingleChildScrollView),
matching: find.byType(Material),
);
final Size menuSize = tester.getSize(menuMaterial);
expect(menuSize, const Size(195.0, 304.0));
// The text field should have same width as the menu
// when the width property is not null.
await tester.pumpWidget(buildTest(themeData, menuChildren, width: 200.0));
final Finder anchor = find.byType(TextField);
final Size size = tester.getSize(anchor);
if (kIsWeb && !isCanvasKit) {
expect(size, const Size(200.0, 61.0));
} else {
expect(size, const Size(200.0, 60.0));
}
await tester.tap(anchor); await tester.tap(anchor);
await tester.pumpAndSettle(); await tester.pumpAndSettle();
...@@ -280,10 +337,9 @@ void main() { ...@@ -280,10 +337,9 @@ void main() {
expect(buttonSize.width, parentWidth - 35.0 - 20.0); expect(buttonSize.width, parentWidth - 35.0 - 20.0);
}); });
testWidgets('The menuHeight property can be used to show a shorter scrollable menu list instead of the complete list', testWidgets('Material2 - The menuHeight property can be used to show a shorter scrollable menu list instead of the complete list',
(WidgetTester tester) async { (WidgetTester tester) async {
final ThemeData themeData = ThemeData(); final ThemeData themeData = ThemeData(useMaterial3: false);
final bool material3 = themeData.useMaterial3;
await tester.pumpWidget(buildTest(themeData, menuChildren)); await tester.pumpWidget(buildTest(themeData, menuChildren));
await tester.tap(find.byType(DropdownMenu<TestMenu>)); await tester.tap(find.byType(DropdownMenu<TestMenu>));
...@@ -303,7 +359,7 @@ void main() { ...@@ -303,7 +359,7 @@ void main() {
matching: find.byType(Padding), matching: find.byType(Padding),
).first; ).first;
final Size menuViewSize = tester.getSize(menuView); final Size menuViewSize = tester.getSize(menuView);
expect(menuViewSize, material3 ? const Size(195.0, 304.0) : const Size(180.0, 304.0)); // 304 = 288 + vertical padding(2 * 8) expect(menuViewSize, const Size(180.0, 304.0)); // 304 = 288 + vertical padding(2 * 8)
// Constrains the menu height. // Constrains the menu height.
await tester.pumpWidget(Container()); await tester.pumpWidget(Container());
...@@ -319,9 +375,50 @@ void main() { ...@@ -319,9 +375,50 @@ void main() {
).first; ).first;
final Size updatedMenuSize = tester.getSize(updatedMenu); final Size updatedMenuSize = tester.getSize(updatedMenu);
expect(updatedMenuSize, material3 ? const Size(195.0, 100.0) : const Size(180.0, 100.0)); expect(updatedMenuSize, const Size(180.0, 100.0));
}); });
testWidgets('Material3 - The menuHeight property can be used to show a shorter scrollable menu list instead of the complete list',
(WidgetTester tester) async {
final ThemeData themeData = ThemeData(useMaterial3: true);
await tester.pumpWidget(buildTest(themeData, menuChildren));
await tester.tap(find.byType(DropdownMenu<TestMenu>));
await tester.pumpAndSettle();
final Element firstItem = tester.element(find.widgetWithText(MenuItemButton, 'Item 0').last);
final RenderBox firstBox = firstItem.renderObject! as RenderBox;
final Offset topLeft = firstBox.localToGlobal(firstBox.size.topLeft(Offset.zero));
final Element lastItem = tester.element(find.widgetWithText(MenuItemButton, 'Item 5').last);
final RenderBox lastBox = lastItem.renderObject! as RenderBox;
final Offset bottomRight = lastBox.localToGlobal(lastBox.size.bottomRight(Offset.zero));
// height = height of MenuItemButton * 6 = 48 * 6
expect(bottomRight.dy - topLeft.dy, 288.0);
final Finder menuView = find.ancestor(
of: find.byType(SingleChildScrollView),
matching: find.byType(Padding),
).first;
final Size menuViewSize = tester.getSize(menuView);
expect(menuViewSize, const Size(195.0, 304.0)); // 304 = 288 + vertical padding(2 * 8)
// Constrains the menu height.
await tester.pumpWidget(Container());
await tester.pumpWidget(buildTest(themeData, menuChildren, menuHeight: 100));
await tester.pumpAndSettle();
await tester.tap(find.byType(DropdownMenu<TestMenu>));
await tester.pumpAndSettle();
final Finder updatedMenu = find.ancestor(
of: find.byType(SingleChildScrollView),
matching: find.byType(Padding),
).first;
final Size updatedMenuSize = tester.getSize(updatedMenu);
expect(updatedMenuSize, const Size(195.0, 100.0));
});
testWidgets('The text in the menu button should be aligned with the text of ' testWidgets('The text in the menu button should be aligned with the text of '
'the text field - LTR', (WidgetTester tester) async { 'the text field - LTR', (WidgetTester tester) async {
final ThemeData themeData = ThemeData(); final ThemeData themeData = ThemeData();
......
...@@ -96,10 +96,43 @@ void main() { ...@@ -96,10 +96,43 @@ void main() {
]); ]);
}); });
testWidgets('Passing no SnackBarThemeData returns defaults', (WidgetTester tester) async { testWidgets('Material2 - Passing no SnackBarThemeData returns defaults', (WidgetTester tester) async {
const String text = 'I am a snack bar.'; const String text = 'I am a snack bar.';
final ThemeData theme = ThemeData(); await tester.pumpWidget(MaterialApp(
final bool material3 = theme.useMaterial3; theme: ThemeData(useMaterial3: false),
home: Scaffold(
body: Builder(
builder: (BuildContext context) {
return GestureDetector(
onTap: () {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: const Text(text),
duration: const Duration(seconds: 2),
action: SnackBarAction(label: 'ACTION', onPressed: () {}),
));
},
child: const Text('X'),
);
},
),
),
));
await tester.tap(find.text('X'));
await tester.pumpAndSettle();
final Material material = _getSnackBarMaterial(tester);
final RenderParagraph content = _getSnackBarTextRenderObject(tester, text);
expect(content.text.style, Typography.material2018().white.titleMedium);
expect(material.color, const Color(0xFF333333));
expect(material.elevation, 6.0);
expect(material.shape, null);
});
testWidgets('Material3 - Passing no SnackBarThemeData returns defaults', (WidgetTester tester) async {
const String text = 'I am a snack bar.';
final ThemeData theme = ThemeData(useMaterial3: true);
await tester.pumpWidget(MaterialApp( await tester.pumpWidget(MaterialApp(
theme: theme, theme: theme,
home: Scaffold( home: Scaffold(
...@@ -126,10 +159,8 @@ void main() { ...@@ -126,10 +159,8 @@ void main() {
final Material material = _getSnackBarMaterial(tester); final Material material = _getSnackBarMaterial(tester);
final RenderParagraph content = _getSnackBarTextRenderObject(tester, text); final RenderParagraph content = _getSnackBarTextRenderObject(tester, text);
expect(content.text.style, material3 expect(content.text.style, Typography.material2021().englishLike.bodyMedium?.merge(Typography.material2021().black.bodyMedium).copyWith(color: theme.colorScheme.onInverseSurface, decorationColor: theme.colorScheme.onSurface));
? Typography.material2021().englishLike.bodyMedium?.merge(Typography.material2021().black.bodyMedium).copyWith(color: theme.colorScheme.onInverseSurface, decorationColor: theme.colorScheme.onSurface) expect(material.color, theme.colorScheme.inverseSurface);
: Typography.material2018().white.titleMedium);
expect(material.color, material3 ? theme.colorScheme.inverseSurface : const Color(0xFF333333));
expect(material.elevation, 6.0); expect(material.elevation, 6.0);
expect(material.shape, null); expect(material.shape, null);
}); });
......
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