Update DropdownMenu tests to remove some dependence on platforms (#156131)

Keyboard navigation in  `DropdownMenu` depends on `focus` instead of platforms.
Updating tests to remove `variant: TargetPlatformVariant.desktop()`  from keyboard navigation tests.
This commit is contained in:
PurplePolyhedron 2024-10-08 23:55:18 +08:00 committed by GitHub
parent 49ccfdb7d8
commit 138144bb2f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 25 deletions

View File

@ -114,9 +114,9 @@ class DropdownMenuEntry<T> {
/// will be updated based on the selection from the menu entries. The text field
/// will stay empty if the selected entry is disabled.
///
/// The dropdown menu can be traversed by pressing the up or down key. During the
/// process, the corresponding item will be highlighted and the text field will be updated.
/// Disabled items will be skipped during traversal.
/// When the dropdown menu has focus, it can be traversed by pressing the up or down key.
/// During the process, the corresponding item will be highlighted and
/// the text field will be updated. Disabled items will be skipped during traversal.
///
/// The menu can be scrollable if not all items in the list are displayed at once.
///

View File

@ -1137,12 +1137,13 @@ void main() {
expect(menuMaterial, findsOneWidget);
});
testWidgets('Down key can highlight the menu item on desktop platforms', (WidgetTester tester) async {
testWidgets('Down key can highlight the menu item while focused', (WidgetTester tester) async {
final ThemeData themeData = ThemeData();
await tester.pumpWidget(MaterialApp(
theme: themeData,
home: Scaffold(
body: DropdownMenu<TestMenu>(
requestFocusOnTap: true,
trailingIcon: const Icon(Icons.ac_unit),
dropdownMenuEntries: menuChildren,
),
@ -1177,14 +1178,15 @@ void main() {
);
item0material = tester.widget<Material>(button0Material);
expect(item0material.color, Colors.transparent); // the previous item should not be highlighted.
}, variant: TargetPlatformVariant.desktop());
});
testWidgets('Up key can highlight the menu item on desktop platforms', (WidgetTester tester) async {
testWidgets('Up key can highlight the menu item while focused', (WidgetTester tester) async {
final ThemeData themeData = ThemeData();
await tester.pumpWidget(MaterialApp(
theme: themeData,
home: Scaffold(
body: DropdownMenu<TestMenu>(
requestFocusOnTap: true,
dropdownMenuEntries: menuChildren,
),
),
@ -1219,7 +1221,7 @@ void main() {
item5material = tester.widget<Material>(button5Material);
expect(item5material.color, Colors.transparent); // the previous item should not be highlighted.
}, variant: TargetPlatformVariant.desktop());
});
testWidgets('Left and right keys can move text field selection', (WidgetTester tester) async {
final TextEditingController controller = TextEditingController();
@ -1262,13 +1264,14 @@ void main() {
}, variant: TargetPlatformVariant.desktop());
// Regression test for https://github.com/flutter/flutter/issues/147253.
testWidgets('Down key and up key can navigate on desktop platforms '
'when a label text contains another label text', (WidgetTester tester) async {
testWidgets('Down key and up key can navigate while focused when a label text '
'contains another label text', (WidgetTester tester) async {
final ThemeData themeData = ThemeData();
await tester.pumpWidget(MaterialApp(
theme: themeData,
home: const Scaffold(
body: DropdownMenu<int>(
requestFocusOnTap: true,
dropdownMenuEntries: <DropdownMenuEntry<int>>[
DropdownMenuEntry<int>(
value: 0,
@ -1330,7 +1333,7 @@ void main() {
item0Material = tester.widget<Material>(button0Material);
expect(item0Material.color, themeData.colorScheme.onSurface.withOpacity(0.12));
}, variant: TargetPlatformVariant.desktop());
});
// Regression test for https://github.com/flutter/flutter/issues/151878.
testWidgets('Searching for non matching item does not crash',
@ -1360,6 +1363,7 @@ void main() {
await tester.pumpWidget(MaterialApp(
home: Scaffold(
body: DropdownMenu<TestMenu>(
requestFocusOnTap: true,
enableFilter: true,
dropdownMenuEntries: menuChildren,
),
@ -1374,16 +1378,17 @@ void main() {
await tester.enterText(find.byType(TextField).first, 'No match 2');
await tester.pump();
expect(tester.takeException(), isNull);
}, variant: TargetPlatformVariant.desktop());
});
// Regression test for https://github.com/flutter/flutter/issues/147253.
testWidgets('Default search prioritises the current highlight on desktop platforms',
testWidgets('Default search prioritises the current highlight',
(WidgetTester tester) async {
final ThemeData themeData = ThemeData();
await tester.pumpWidget(MaterialApp(
theme: themeData,
home: Scaffold(
body: DropdownMenu<TestMenu>(
requestFocusOnTap: true,
dropdownMenuEntries: menuChildren,
),
),
@ -1413,16 +1418,17 @@ void main() {
);
item2material = tester.widget<Material>(button2Material);
expect(item2material.color, themeData.colorScheme.onSurface.withOpacity(0.12));
}, variant: TargetPlatformVariant.desktop());
});
// Regression test for https://github.com/flutter/flutter/issues/152375.
testWidgets('Down key and up key can navigate on desktop platforms when a label text contains '
testWidgets('Down key and up key can navigate while focused when a label text contains '
'another label text using customized search algorithm', (WidgetTester tester) async {
final ThemeData themeData = ThemeData();
await tester.pumpWidget(MaterialApp(
theme: themeData,
home: Scaffold(
body: DropdownMenu<int>(
requestFocusOnTap: true,
searchCallback: (List<DropdownMenuEntry<int>> entries, String query) {
if (query.isEmpty) {
return null;
@ -1493,15 +1499,16 @@ void main() {
item0Material = tester.widget<Material>(button0Material);
expect(item0Material.color, themeData.colorScheme.onSurface.withOpacity(0.12));
}, variant: TargetPlatformVariant.desktop());
});
// Regression test for https://github.com/flutter/flutter/issues/152375.
testWidgets('Searching can hightlight entry after keyboard navigation', (WidgetTester tester) async {
testWidgets('Searching can hightlight entry after keyboard navigation while focused', (WidgetTester tester) async {
final ThemeData themeData = ThemeData();
await tester.pumpWidget(MaterialApp(
theme: themeData,
home: Scaffold(
body: DropdownMenu<TestMenu>(
requestFocusOnTap: true,
dropdownMenuEntries: menuChildren,
),
),
@ -1521,15 +1528,16 @@ void main() {
);
final Material itemMaterial = tester.widget<Material>(buttonMaterial);
expect(itemMaterial.color, themeData.colorScheme.onSurface.withOpacity(0.12)); // Menu 1 button is highlighted.
}, variant: TargetPlatformVariant.desktop());
});
testWidgets('The text input should match the label of the menu item '
'while pressing down key on desktop platforms', (WidgetTester tester) async {
'when pressing down key while focused', (WidgetTester tester) async {
final ThemeData themeData = ThemeData();
await tester.pumpWidget(MaterialApp(
theme: themeData,
home: Scaffold(
body: DropdownMenu<TestMenu>(
requestFocusOnTap: true,
dropdownMenuEntries: menuChildren,
),
),
@ -1552,15 +1560,16 @@ void main() {
await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown);
await tester.pump();
expect(find.widgetWithText(TextField, 'Item 2'), findsOneWidget);
}, variant: TargetPlatformVariant.desktop());
});
testWidgets('The text input should match the label of the menu item '
'while pressing up key on desktop platforms', (WidgetTester tester) async {
'when pressing up key while focused', (WidgetTester tester) async {
final ThemeData themeData = ThemeData();
await tester.pumpWidget(MaterialApp(
theme: themeData,
home: Scaffold(
body: DropdownMenu<TestMenu>(
requestFocusOnTap: true,
dropdownMenuEntries: menuChildren,
),
),
@ -1583,9 +1592,9 @@ void main() {
await tester.sendKeyEvent(LogicalKeyboardKey.arrowUp);
await tester.pump();
expect(find.widgetWithText(TextField, 'Item 3'), findsOneWidget);
}, variant: TargetPlatformVariant.desktop());
});
testWidgets('Disabled button will be skipped while pressing up/down key on desktop platforms', (WidgetTester tester) async {
testWidgets('Disabled button will be skipped while pressing up/down key while focused', (WidgetTester tester) async {
final ThemeData themeData = ThemeData();
final List<DropdownMenuEntry<TestMenu>> menuWithDisabledItems = <DropdownMenuEntry<TestMenu>>[
const DropdownMenuEntry<TestMenu>(value: TestMenu.mainMenu0, label: 'Item 0'),
@ -1599,6 +1608,7 @@ void main() {
theme: themeData,
home: Scaffold(
body: DropdownMenu<TestMenu>(
requestFocusOnTap: true,
dropdownMenuEntries: menuWithDisabledItems,
),
),
@ -1627,7 +1637,7 @@ void main() {
);
final Material item3Material = tester.widget<Material>(button3Material);
expect(item3Material.color, themeData.colorScheme.onSurface.withOpacity(0.12));
}, variant: TargetPlatformVariant.desktop());
});
testWidgets('Searching is enabled by default on mobile platforms if initialSelection is non null', (WidgetTester tester) async {
final ThemeData themeData = ThemeData();
@ -1676,12 +1686,13 @@ void main() {
expect(itemMaterial.color, themeData.colorScheme.onSurface.withOpacity(0.12)); // Menu 1 button is highlighted.
}, variant: TargetPlatformVariant.desktop());
testWidgets('Highlight can move up/down starting from the searching result on desktop platforms', (WidgetTester tester) async {
testWidgets('Highlight can move up/down starting from the searching result while focused', (WidgetTester tester) async {
final ThemeData themeData = ThemeData();
await tester.pumpWidget(MaterialApp(
theme: themeData,
home: Scaffold(
body: DropdownMenu<TestMenu>(
requestFocusOnTap: true,
dropdownMenuEntries: menuChildren,
),
),
@ -1722,7 +1733,7 @@ void main() {
);
final Material item5Material = tester.widget<Material>(button5Material);
expect(item5Material.color, themeData.colorScheme.onSurface.withOpacity(0.12));
}, variant: TargetPlatformVariant.desktop());
});
testWidgets('Filtering is disabled by default', (WidgetTester tester) async {
final ThemeData themeData = ThemeData();