From 036c102c345fe5fe3723f30f24681b2caa3dad18 Mon Sep 17 00:00:00 2001 From: Shi-Hao Hong Date: Mon, 28 Oct 2019 13:44:21 -0700 Subject: [PATCH] Improve DropdownButton assert message (#43511) - Adds an assert message checking for exactly one unique value for `DropdownItem`s --- .../flutter/lib/src/material/dropdown.dart | 20 ++- .../flutter/test/material/dropdown_test.dart | 127 ++++++++++++++++++ 2 files changed, 145 insertions(+), 2 deletions(-) diff --git a/packages/flutter/lib/src/material/dropdown.dart b/packages/flutter/lib/src/material/dropdown.dart index 68eb8f944d..fbe7260e1b 100644 --- a/packages/flutter/lib/src/material/dropdown.dart +++ b/packages/flutter/lib/src/material/dropdown.dart @@ -719,7 +719,15 @@ class DropdownButton extends StatefulWidget { this.focusColor, this.focusNode, this.autofocus = false, - }) : assert(items == null || items.isEmpty || value == null || items.where((DropdownMenuItem item) => item.value == value).length == 1), + }) : assert(items == null || items.isEmpty || value == null || + items.where((DropdownMenuItem 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 extends FormField { bool isDense = false, bool isExpanded = false, double itemHeight, - }) : assert(items == null || items.isEmpty || value == null || items.where((DropdownMenuItem item) => item.value == value).length == 1), + }) : assert(items == null || items.isEmpty || value == null || + items.where((DropdownMenuItem 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), diff --git a/packages/flutter/test/material/dropdown_test.dart b/packages/flutter/test/material/dropdown_test.dart index cc61f2015e..c7f088d870 100644 --- a/packages/flutter/test/material/dropdown_test.dart +++ b/packages/flutter/test/material/dropdown_test.dart @@ -342,6 +342,68 @@ void main() { expect(value, equals('two')); }); + testWidgets('DropdownButton does not allow duplicate item values', (WidgetTester tester) async { + final List> itemsWithDuplicateValues = ['a', 'b', 'c', 'c'] + .map>((String value) { + return DropdownMenuItem( + value: value, + child: Text(value), + ); + }).toList(); + + try { + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: DropdownButton( + 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> itemsWithDuplicateValues = ['a', 'b', 'c', 'd'] + .map>((String value) { + return DropdownMenuItem( + value: value, + child: Text(value), + ); + }).toList(); + + try { + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: DropdownButton( + 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> itemsWithDuplicateValues = ['a', 'b', 'c', 'c'] + .map>((String value) { + return DropdownMenuItem( + value: value, + child: Text(value), + ); + }).toList(); + + try { + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: DropdownButtonFormField( + 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> itemsWithDuplicateValues = ['a', 'b', 'c', 'd'] + .map>((String value) { + return DropdownMenuItem( + value: value, + child: Text(value), + ); + }).toList(); + + try { + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: DropdownButton( + 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 itemValues = ['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';