Unverified Commit 036c102c authored by Shi-Hao Hong's avatar Shi-Hao Hong Committed by GitHub

Improve DropdownButton assert message (#43511)

- Adds an assert message checking for exactly one unique value for `DropdownItem`s
parent c08de65b
......@@ -719,7 +719,15 @@ class DropdownButton<T> extends StatefulWidget {
this.focusColor,
this.focusNode,
this.autofocus = false,
}) : assert(items == null || items.isEmpty || value == null || items.where((DropdownMenuItem<T> item) => item.value == value).length == 1),
}) : assert(items == null || items.isEmpty || value == null ||
items.where((DropdownMenuItem<T> item) {
return item.value == value;
}).length == 1,
'There should be exactly one item with [DropdownButton]\'s value: '
'$value. \n'
'Either zero or 2 or more [DropdownMenuItem]s were detected '
'with the same value',
),
assert(elevation != null),
assert(iconSize != null),
assert(isDense != null),
......@@ -1287,7 +1295,15 @@ class DropdownButtonFormField<T> extends FormField<T> {
bool isDense = false,
bool isExpanded = false,
double itemHeight,
}) : assert(items == null || items.isEmpty || value == null || items.where((DropdownMenuItem<T> item) => item.value == value).length == 1),
}) : assert(items == null || items.isEmpty || value == null ||
items.where((DropdownMenuItem<T> item) {
return item.value == value;
}).length == 1,
'There should be exactly one item with [DropdownButton]\'s value: '
'$value. \n'
'Either zero or 2 or more [DropdownMenuItem]s were detected '
'with the same value',
),
assert(decoration != null),
assert(elevation != null),
assert(iconSize != null),
......
......@@ -342,6 +342,68 @@ void main() {
expect(value, equals('two'));
});
testWidgets('DropdownButton does not allow duplicate item values', (WidgetTester tester) async {
final List<DropdownMenuItem<String>> itemsWithDuplicateValues = <String>['a', 'b', 'c', 'c']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList();
try {
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: DropdownButton<String>(
value: 'c',
onChanged: (String newValue) {},
items: itemsWithDuplicateValues,
),
),
),
);
fail('Should not be possible to have duplicate item value');
} on AssertionError catch (error) {
expect(
error.toString(),
contains('There should be exactly one item with [DropdownButton]\'s value'),
);
}
});
testWidgets('DropdownButton value should only appear in one menu item', (WidgetTester tester) async {
final List<DropdownMenuItem<String>> itemsWithDuplicateValues = <String>['a', 'b', 'c', 'd']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList();
try {
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: DropdownButton<String>(
value: 'e',
onChanged: (String newValue) {},
items: itemsWithDuplicateValues,
),
),
),
);
fail('Should not be possible to have no items with passed in value');
} on AssertionError catch (error) {
expect(
error.toString(),
contains('There should be exactly one item with [DropdownButton]\'s value'),
);
}
});
testWidgets('Dropdown in ListView', (WidgetTester tester) async {
// Regression test for https://github.com/flutter/flutter/issues/12053
// Positions a DropdownButton at the left and right edges of the screen,
......@@ -2219,6 +2281,68 @@ void main() {
expect(tester.getCenter(item40.first).dy, tester.getCenter(item40.last).dy);
});
testWidgets('DropdownButtonFormField does not allow duplicate item values', (WidgetTester tester) async {
final List<DropdownMenuItem<String>> itemsWithDuplicateValues = <String>['a', 'b', 'c', 'c']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList();
try {
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: DropdownButtonFormField<String>(
value: 'c',
onChanged: (String newValue) {},
items: itemsWithDuplicateValues,
),
),
),
);
fail('Should not be possible to have duplicate item value');
} on AssertionError catch (error) {
expect(
error.toString(),
contains('There should be exactly one item with [DropdownButton]\'s value'),
);
}
});
testWidgets('DropdownButtonFormField value should only appear in one menu item', (WidgetTester tester) async {
final List<DropdownMenuItem<String>> itemsWithDuplicateValues = <String>['a', 'b', 'c', 'd']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList();
try {
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: DropdownButton<String>(
value: 'e',
onChanged: (String newValue) {},
items: itemsWithDuplicateValues,
),
),
),
);
fail('Should not be possible to have no items with passed in value');
} on AssertionError catch (error) {
expect(
error.toString(),
contains('There should be exactly one item with [DropdownButton]\'s value'),
);
}
});
testWidgets('DropdownButton hint is selected item', (WidgetTester tester) async {
const double hintPaddingOffset = 8;
const List<String> itemValues = <String>['item0', 'item1', 'item2', 'item3'];
......@@ -2286,6 +2410,7 @@ void main() {
expect(tester.getTopLeft(find.text('-item0-')).dx, 8);
});
testWidgets('DropdownButton can be focused, and has focusColor', (WidgetTester tester) async {
final UniqueKey buttonKey = UniqueKey();
final FocusNode focusNode = FocusNode(debugLabel: 'DropdownButton');
......@@ -2298,6 +2423,7 @@ void main() {
await tester.pumpWidget(buildFrame(buttonKey: buttonKey, onChanged: onChanged, focusNode: focusNode, focusColor: const Color(0xff00ff00)));
expect(buttonFinder, paints ..rrect(rrect: const RRect.fromLTRBXY(0.0, 0.0, 104.0, 48.0, 4.0, 4.0), color: const Color(0xff00ff00)));
});
testWidgets("DropdownButton won't be focused if not enabled", (WidgetTester tester) async {
final UniqueKey buttonKey = UniqueKey();
final FocusNode focusNode = FocusNode(debugLabel: 'DropdownButton');
......@@ -2306,6 +2432,7 @@ void main() {
expect(focusNode.hasPrimaryFocus, isFalse);
expect(find.byKey(buttonKey), isNot(paints ..rrect(rrect: const RRect.fromLTRBXY(0.0, 0.0, 104.0, 48.0, 4.0, 4.0), color: const Color(0xff00ff00))));
});
testWidgets('DropdownButton is activated with the enter key', (WidgetTester tester) async {
final FocusNode focusNode = FocusNode(debugLabel: 'DropdownButton');
String value = 'one';
......
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