diff --git a/examples/flutter_gallery/lib/demo/material/expansion_panels_demo.dart b/examples/flutter_gallery/lib/demo/material/expansion_panels_demo.dart index c3f2b86d07..1eb7b620e3 100644 --- a/examples/flutter_gallery/lib/demo/material/expansion_panels_demo.dart +++ b/examples/flutter_gallery/lib/demo/material/expansion_panels_demo.dart @@ -6,7 +6,8 @@ import 'package:flutter/material.dart'; import '../../gallery/demo.dart'; -enum _Location { +@visibleForTesting +enum Location { Barbados, Bahamas, Bermuda @@ -225,12 +226,12 @@ class _ExpansionPanelsDemoState extends State { ); }, ), - DemoItem<_Location>( + DemoItem( name: 'Location', - value: _Location.Bahamas, + value: Location.Bahamas, hint: 'Select location', - valueToString: (_Location location) => location.toString().split('.')[1], - builder: (DemoItem<_Location> item) { + valueToString: (Location location) => location.toString().split('.')[1], + builder: (DemoItem item) { void close() { setState(() { item.isExpanded = false; @@ -242,47 +243,32 @@ class _ExpansionPanelsDemoState extends State { return CollapsibleBody( onSave: () { Form.of(context).save(); close(); }, onCancel: () { Form.of(context).reset(); close(); }, - child: FormField<_Location>( + child: FormField( initialValue: item.value, - onSaved: (_Location result) { item.value = result; }, - builder: (FormFieldState<_Location> field) { + onSaved: (Location result) { item.value = result; }, + builder: (FormFieldState field) { return Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, children: [ - Row( - mainAxisSize: MainAxisSize.min, - children: [ - Radio<_Location>( - value: _Location.Bahamas, - groupValue: field.value, - onChanged: field.didChange, - ), - const Text('Bahamas') - ] + RadioListTile( + value: Location.Bahamas, + title: const Text('Bahamas'), + groupValue: field.value, + onChanged: field.didChange, ), - Row( - mainAxisSize: MainAxisSize.min, - children: [ - Radio<_Location>( - value: _Location.Barbados, - groupValue: field.value, - onChanged: field.didChange, - ), - const Text('Barbados') - ] + RadioListTile( + value: Location.Barbados, + title: const Text('Barbados'), + groupValue: field.value, + onChanged: field.didChange, + ), + RadioListTile( + value: Location.Bermuda, + title: const Text('Bermuda'), + groupValue: field.value, + onChanged: field.didChange, ), - Row( - mainAxisSize: MainAxisSize.min, - children: [ - Radio<_Location>( - value: _Location.Bermuda, - groupValue: field.value, - onChanged: field.didChange, - ), - const Text('Bermuda') - ] - ) ] ); } diff --git a/examples/flutter_gallery/test/demo/material/expansion_panels_demo_test.dart b/examples/flutter_gallery/test/demo/material/expansion_panels_demo_test.dart new file mode 100644 index 0000000000..29c940bdd3 --- /dev/null +++ b/examples/flutter_gallery/test/demo/material/expansion_panels_demo_test.dart @@ -0,0 +1,54 @@ +// Copyright 2018 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:flutter/material.dart'; +import 'package:flutter_gallery/demo/material/expansion_panels_demo.dart'; +import 'package:flutter_test/flutter_test.dart'; + +Future main() async { + testWidgets('Expansion panel demo: radio tile selection changes on tap', + (WidgetTester tester) async { + await tester.pumpWidget(MaterialApp(home: ExpansionPanelsDemo())); + + expect(_expandIcons, findsNWidgets(3)); + + await tester.tap(find.byWidget(_radioPanelExpandIcon)); + await tester.pumpAndSettle(); + + expect(_radioFinder, findsNWidgets(3)); + + const int i = 1; + + expect(_isRadioSelected(0), isTrue); + expect(_isRadioSelected(i), isFalse); + + await tester.tap(find.byWidget(_radioListTiles[i])); + await tester.pumpAndSettle(); + + expect(_isRadioSelected(0), isFalse); + expect(_isRadioSelected(i), isTrue); + }); +} + +Finder get _expandIcons => find.byType(ExpandIcon); + +Widget get _radioPanelExpandIcon => _expandIcons.evaluate().toList()[1].widget; + +bool _isRadioSelected(int index) => + _radios[index].value == _radios[index].groupValue; + +List> get _radios => List>.from( + _radioFinder.evaluate().map((Element e) => e.widget)); + +// [find.byType] and [find.widgetWithText] do not match subclasses; `Radio` is not sufficient to find a `Radio<_Location>`. +// Another approach is to grab the `runtimeType` of a dummy instance; see packages/flutter/test/material/control_list_tile_test.dart. +Finder get _radioFinder => + find.byWidgetPredicate((Widget w) => w is Radio); + +List> get _radioListTiles => + List>.from( + _radioListTilesFinder.evaluate().map((Element e) => e.widget)); + +Finder get _radioListTilesFinder => + find.byWidgetPredicate((Widget w) => w is RadioListTile);