Skip to content

Commit

Permalink
Update DropdownMenu, SnackBarTheme and Stepper tests for M2/M3 (#…
Browse files Browse the repository at this point in the history
…130464)

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

More info in #127064
  • Loading branch information
QuncCccccc committed Jul 17, 2023
1 parent 5a866a0 commit f4da096
Show file tree
Hide file tree
Showing 3 changed files with 331 additions and 45 deletions.
117 changes: 107 additions & 10 deletions packages/flutter/test/material/dropdown_menu_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import 'dart:ui';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
Expand Down Expand Up @@ -109,11 +110,10 @@ void main() {
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 {

final ThemeData themeData = ThemeData(useMaterial3: false);
final bool useMaterial3 = themeData.useMaterial3;
await tester.pumpWidget(
MaterialApp(
theme: themeData,
Expand All @@ -129,7 +129,7 @@ void main() {

final Finder textField = find.byType(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.pumpAndSettle();
Expand All @@ -139,15 +139,72 @@ void main() {
matching: find.byType(Material),
);
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
// 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);
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.pumpAndSettle();
Expand Down Expand Up @@ -280,10 +337,9 @@ void main() {
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 {
final ThemeData themeData = ThemeData();
final bool material3 = themeData.useMaterial3;
final ThemeData themeData = ThemeData(useMaterial3: false);
await tester.pumpWidget(buildTest(themeData, menuChildren));

await tester.tap(find.byType(DropdownMenu<TestMenu>));
Expand All @@ -303,7 +359,7 @@ void main() {
matching: find.byType(Padding),
).first;
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.
await tester.pumpWidget(Container());
Expand All @@ -319,9 +375,50 @@ void main() {
).first;

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 '
'the text field - LTR', (WidgetTester tester) async {
final ThemeData themeData = ThemeData();
Expand Down
45 changes: 38 additions & 7 deletions packages/flutter/test/material/snack_bar_theme_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.';
final ThemeData theme = ThemeData();
final bool material3 = theme.useMaterial3;
await tester.pumpWidget(MaterialApp(
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(
theme: theme,
home: Scaffold(
Expand All @@ -126,10 +159,8 @@ void main() {
final Material material = _getSnackBarMaterial(tester);
final RenderParagraph content = _getSnackBarTextRenderObject(tester, text);

expect(content.text.style, material3
? Typography.material2021().englishLike.bodyMedium?.merge(Typography.material2021().black.bodyMedium).copyWith(color: theme.colorScheme.onInverseSurface, decorationColor: theme.colorScheme.onSurface)
: Typography.material2018().white.titleMedium);
expect(material.color, material3 ? theme.colorScheme.inverseSurface : const Color(0xFF333333));
expect(content.text.style, Typography.material2021().englishLike.bodyMedium?.merge(Typography.material2021().black.bodyMedium).copyWith(color: theme.colorScheme.onInverseSurface, decorationColor: theme.colorScheme.onSurface));
expect(material.color, theme.colorScheme.inverseSurface);
expect(material.elevation, 6.0);
expect(material.shape, null);
});
Expand Down
Loading

0 comments on commit f4da096

Please sign in to comment.