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

ToggleButtons test improvement (#38898)

* Improve finders throughout toggle_buttons_test.dart

* Improve Material finders

* Update toggle_buttons_theme_test.dart finders to be more precise

* Refactor out textStyle and iconTheme finders

* Refactor iconTheme and buttonTextTheme logic
parent a415c76b
...@@ -19,6 +19,12 @@ Widget boilerplate({Widget child}) { ...@@ -19,6 +19,12 @@ Widget boilerplate({Widget child}) {
void main() { void main() {
testWidgets('Initial toggle state is reflected', (WidgetTester tester) async { testWidgets('Initial toggle state is reflected', (WidgetTester tester) async {
TextStyle buttonTextStyle(String text) {
return tester.widget<DefaultTextStyle>(find.descendant(
of: find.widgetWithText(RawMaterialButton, text),
matching: find.byType(DefaultTextStyle),
)).style;
}
final ThemeData theme = ThemeData(); final ThemeData theme = ThemeData();
await tester.pumpWidget( await tester.pumpWidget(
Material( Material(
...@@ -35,22 +41,26 @@ void main() { ...@@ -35,22 +41,26 @@ void main() {
), ),
); );
final DefaultTextStyle textStyleOne = tester.firstWidget(
find.widgetWithText(DefaultTextStyle, 'First child'),
);
expect( expect(
textStyleOne.style.color, buttonTextStyle('First child').color,
theme.colorScheme.onSurface.withOpacity(0.87), theme.colorScheme.onSurface.withOpacity(0.87),
); );
final DefaultTextStyle textStyleTwo = tester.firstWidget( expect(
find.widgetWithText(DefaultTextStyle, 'Second child'), buttonTextStyle('Second child').color,
theme.colorScheme.primary,
); );
expect(textStyleTwo.style.color, theme.colorScheme.primary);
}); });
testWidgets( testWidgets(
'onPressed is triggered on button tap', 'onPressed is triggered on button tap',
(WidgetTester tester) async { (WidgetTester tester) async {
TextStyle buttonTextStyle(String text) {
return tester.widget<DefaultTextStyle>(find.descendant(
of: find.widgetWithText(RawMaterialButton, text),
matching: find.byType(DefaultTextStyle),
)).style;
}
final List<bool> _isSelected = <bool>[false, true]; final List<bool> _isSelected = <bool>[false, true];
final ThemeData theme = ThemeData(); final ThemeData theme = ThemeData();
await tester.pumpWidget( await tester.pumpWidget(
...@@ -76,40 +86,28 @@ void main() { ...@@ -76,40 +86,28 @@ void main() {
), ),
); );
DefaultTextStyle textStyleOne;
DefaultTextStyle textStyleTwo;
expect(_isSelected[0], isFalse); expect(_isSelected[0], isFalse);
expect(_isSelected[1], isTrue); expect(_isSelected[1], isTrue);
textStyleOne = tester.firstWidget(
find.widgetWithText(DefaultTextStyle, 'First child'),
);
expect( expect(
textStyleOne.style.color, buttonTextStyle('First child').color,
theme.colorScheme.onSurface.withOpacity(0.87), theme.colorScheme.onSurface.withOpacity(0.87),
); );
textStyleTwo = tester.firstWidget( expect(
find.widgetWithText(DefaultTextStyle, 'Second child'), buttonTextStyle('Second child').color,
theme.colorScheme.primary,
); );
expect(textStyleTwo.style.color, theme.colorScheme.primary);
await tester.tap(find.text('Second child')); await tester.tap(find.text('Second child'));
await tester.pumpAndSettle(); await tester.pumpAndSettle();
expect(_isSelected[0], isFalse); expect(_isSelected[0], isFalse);
expect(_isSelected[1], isFalse); expect(_isSelected[1], isFalse);
textStyleOne = tester.firstWidget(
find.widgetWithText(DefaultTextStyle, 'First child'),
);
expect( expect(
textStyleOne.style.color, buttonTextStyle('First child').color,
theme.colorScheme.onSurface.withOpacity(0.87), theme.colorScheme.onSurface.withOpacity(0.87),
); );
textStyleTwo = tester.firstWidget(
find.widgetWithText(DefaultTextStyle, 'Second child'),
);
expect( expect(
textStyleTwo.style.color, buttonTextStyle('Second child').color,
theme.colorScheme.onSurface.withOpacity(0.87), theme.colorScheme.onSurface.withOpacity(0.87),
); );
}, },
...@@ -118,6 +116,12 @@ void main() { ...@@ -118,6 +116,12 @@ void main() {
testWidgets( testWidgets(
'onPressed that is null disables buttons', 'onPressed that is null disables buttons',
(WidgetTester tester) async { (WidgetTester tester) async {
TextStyle buttonTextStyle(String text) {
return tester.widget<DefaultTextStyle>(find.descendant(
of: find.widgetWithText(RawMaterialButton, text),
matching: find.byType(DefaultTextStyle),
)).style;
}
final List<bool> _isSelected = <bool>[false, true]; final List<bool> _isSelected = <bool>[false, true];
final ThemeData theme = ThemeData(); final ThemeData theme = ThemeData();
...@@ -135,44 +139,29 @@ void main() { ...@@ -135,44 +139,29 @@ void main() {
), ),
); );
DefaultTextStyle textStyleOne;
DefaultTextStyle textStyleTwo;
expect(_isSelected[0], isFalse); expect(_isSelected[0], isFalse);
expect(_isSelected[1], isTrue); expect(_isSelected[1], isTrue);
textStyleOne = tester.firstWidget(
find.widgetWithText(DefaultTextStyle, 'First child'),
);
expect( expect(
textStyleOne.style.color, buttonTextStyle('First child').color,
theme.colorScheme.onSurface.withOpacity(0.38), theme.colorScheme.onSurface.withOpacity(0.38),
); );
textStyleTwo = tester.firstWidget(
find.widgetWithText(DefaultTextStyle, 'Second child'),
);
expect( expect(
textStyleTwo.style.color, buttonTextStyle('Second child').color,
theme.colorScheme.onSurface.withOpacity(0.38), theme.colorScheme.onSurface.withOpacity(0.38),
); );
await tester.tap(find.text('Second child')); await tester.tap(find.text('Second child'));
await tester.pumpAndSettle(); await tester.pumpAndSettle();
// nothing should change // Nothing should change
expect(_isSelected[0], isFalse); expect(_isSelected[0], isFalse);
expect(_isSelected[1], isTrue); expect(_isSelected[1], isTrue);
textStyleOne = tester.firstWidget(
find.widgetWithText(DefaultTextStyle, 'First child'),
);
expect( expect(
textStyleOne.style.color, buttonTextStyle('First child').color,
theme.colorScheme.onSurface.withOpacity(0.38), theme.colorScheme.onSurface.withOpacity(0.38),
); );
textStyleTwo = tester.firstWidget(
find.widgetWithText(DefaultTextStyle, 'Second child'),
);
expect( expect(
textStyleTwo.style.color, buttonTextStyle('Second child').color,
theme.colorScheme.onSurface.withOpacity(0.38), theme.colorScheme.onSurface.withOpacity(0.38),
); );
}, },
...@@ -191,8 +180,7 @@ void main() { ...@@ -191,8 +180,7 @@ void main() {
), ),
), ),
); );
fail( fail('Should not be possible to create a toggle button with no children.');
'Should not be possible to create a toggle button with no children.');
} on AssertionError catch (e) { } on AssertionError catch (e) {
expect(e.toString(), contains('children != null')); expect(e.toString(), contains('children != null'));
} }
...@@ -216,8 +204,7 @@ void main() { ...@@ -216,8 +204,7 @@ void main() {
), ),
), ),
); );
fail( fail('Should not be possible to create a toggle button with no isSelected.');
'Should not be possible to create a toggle button with no isSelected.');
} on AssertionError catch (e) { } on AssertionError catch (e) {
expect(e.toString(), contains('isSelected != null')); expect(e.toString(), contains('isSelected != null'));
} }
...@@ -325,6 +312,18 @@ void main() { ...@@ -325,6 +312,18 @@ void main() {
testWidgets( testWidgets(
'Default text/icon colors for enabled, selected and disabled states', 'Default text/icon colors for enabled, selected and disabled states',
(WidgetTester tester) async { (WidgetTester tester) async {
TextStyle buttonTextStyle(String text) {
return tester.widget<DefaultTextStyle>(find.descendant(
of: find.widgetWithText(RawMaterialButton, text),
matching: find.byType(DefaultTextStyle),
)).style;
}
IconTheme iconTheme(IconData icon) {
return tester.widget(find.descendant(
of: find.widgetWithIcon(RawMaterialButton, icon),
matching: find.byType(IconTheme),
));
}
final ThemeData theme = ThemeData(); final ThemeData theme = ThemeData();
await tester.pumpWidget( await tester.pumpWidget(
Material( Material(
...@@ -343,22 +342,13 @@ void main() { ...@@ -343,22 +342,13 @@ void main() {
), ),
); );
DefaultTextStyle textStyle; // Default enabled color
IconTheme iconTheme;
// default enabled color
textStyle = tester.firstWidget(
find.widgetWithText(DefaultTextStyle, 'First child'),
);
expect( expect(
textStyle.style.color, buttonTextStyle('First child').color,
theme.colorScheme.onSurface.withOpacity(0.87), theme.colorScheme.onSurface.withOpacity(0.87),
); );
iconTheme = tester.firstWidget(
find.widgetWithIcon(IconTheme, Icons.check),
);
expect( expect(
iconTheme.data.color, iconTheme(Icons.check).data.color,
theme.colorScheme.onSurface.withOpacity(0.87), theme.colorScheme.onSurface.withOpacity(0.87),
); );
...@@ -379,15 +369,15 @@ void main() { ...@@ -379,15 +369,15 @@ void main() {
), ),
); );
await tester.pumpAndSettle(); await tester.pumpAndSettle();
// default selected color // Default selected color
textStyle = tester.firstWidget( expect(
find.widgetWithText(DefaultTextStyle, 'First child'), buttonTextStyle('First child').color,
theme.colorScheme.primary,
); );
expect(textStyle.style.color, theme.colorScheme.primary); expect(
iconTheme = tester.firstWidget( iconTheme(Icons.check).data.color,
find.widgetWithIcon(IconTheme, Icons.check), theme.colorScheme.primary,
); );
expect(iconTheme.data.color, theme.colorScheme.primary);
await tester.pumpWidget( await tester.pumpWidget(
Material( Material(
...@@ -405,19 +395,13 @@ void main() { ...@@ -405,19 +395,13 @@ void main() {
), ),
); );
await tester.pumpAndSettle(); await tester.pumpAndSettle();
// default disabled color // Default disabled color
textStyle = tester.firstWidget(
find.widgetWithText(DefaultTextStyle, 'First child'),
);
expect( expect(
textStyle.style.color, buttonTextStyle('First child').color,
theme.colorScheme.onSurface.withOpacity(0.38), theme.colorScheme.onSurface.withOpacity(0.38),
); );
iconTheme = tester.firstWidget(
find.widgetWithIcon(IconTheme, Icons.check),
);
expect( expect(
iconTheme.data.color, iconTheme(Icons.check).data.color,
theme.colorScheme.onSurface.withOpacity(0.38), theme.colorScheme.onSurface.withOpacity(0.38),
); );
}, },
...@@ -426,12 +410,24 @@ void main() { ...@@ -426,12 +410,24 @@ void main() {
testWidgets( testWidgets(
'Custom text/icon colors for enabled, selected and disabled states', 'Custom text/icon colors for enabled, selected and disabled states',
(WidgetTester tester) async { (WidgetTester tester) async {
TextStyle buttonTextStyle(String text) {
return tester.widget<DefaultTextStyle>(find.descendant(
of: find.widgetWithText(RawMaterialButton, text),
matching: find.byType(DefaultTextStyle),
)).style;
}
IconTheme iconTheme(IconData icon) {
return tester.widget(find.descendant(
of: find.widgetWithIcon(RawMaterialButton, icon),
matching: find.byType(IconTheme),
));
}
final ThemeData theme = ThemeData(); final ThemeData theme = ThemeData();
const Color enabledColor = Colors.lime; const Color enabledColor = Colors.lime;
const Color selectedColor = Colors.green; const Color selectedColor = Colors.green;
const Color disabledColor = Colors.yellow; const Color disabledColor = Colors.yellow;
// tests are ineffective if the custom colors are the same as the theme's // Tests are ineffective if the custom colors are the same as the theme's
expect(theme.colorScheme.onSurface, isNot(enabledColor)); expect(theme.colorScheme.onSurface, isNot(enabledColor));
expect(theme.colorScheme.primary, isNot(selectedColor)); expect(theme.colorScheme.primary, isNot(selectedColor));
expect(theme.colorScheme.onSurface.withOpacity(0.38), isNot(disabledColor)); expect(theme.colorScheme.onSurface.withOpacity(0.38), isNot(disabledColor));
...@@ -454,18 +450,9 @@ void main() { ...@@ -454,18 +450,9 @@ void main() {
), ),
); );
DefaultTextStyle textStyle; // Custom enabled color
IconTheme iconTheme; expect(buttonTextStyle('First child').color, enabledColor);
expect(iconTheme(Icons.check).data.color, enabledColor);
// custom enabled color
textStyle = tester.firstWidget(
find.widgetWithText(DefaultTextStyle, 'First child'),
);
expect(textStyle.style.color, enabledColor);
iconTheme = tester.firstWidget(
find.widgetWithIcon(IconTheme, Icons.check),
);
expect(iconTheme.data.color, enabledColor);
await tester.pumpWidget( await tester.pumpWidget(
Material( Material(
...@@ -485,15 +472,9 @@ void main() { ...@@ -485,15 +472,9 @@ void main() {
), ),
); );
await tester.pumpAndSettle(); await tester.pumpAndSettle();
// custom selected color // Custom selected color
textStyle = tester.firstWidget( expect(buttonTextStyle('First child').color, selectedColor);
find.widgetWithText(DefaultTextStyle, 'First child'), expect(iconTheme(Icons.check).data.color, selectedColor);
);
expect(textStyle.style.color, selectedColor);
iconTheme = tester.firstWidget(
find.widgetWithIcon(IconTheme, Icons.check),
);
expect(iconTheme.data.color, selectedColor);
await tester.pumpWidget( await tester.pumpWidget(
Material( Material(
...@@ -512,15 +493,9 @@ void main() { ...@@ -512,15 +493,9 @@ void main() {
), ),
); );
await tester.pumpAndSettle(); await tester.pumpAndSettle();
// custom disabled color // Custom disabled color
textStyle = tester.firstWidget( expect(buttonTextStyle('First child').color, disabledColor);
find.widgetWithText(DefaultTextStyle, 'First child'), expect(iconTheme(Icons.check).data.color, disabledColor);
);
expect(textStyle.style.color, disabledColor);
iconTheme = tester.firstWidget(
find.widgetWithIcon(IconTheme, Icons.check),
);
expect(iconTheme.data.color, disabledColor);
}, },
); );
...@@ -542,12 +517,10 @@ void main() { ...@@ -542,12 +517,10 @@ void main() {
), ),
); );
final Material material = tester.firstWidget<Material>( final Material material = tester.widget<Material>(find.descendant(
find.descendant(
of: find.byType(RawMaterialButton), of: find.byType(RawMaterialButton),
matching: find.byType(Material), matching: find.byType(Material),
), ));
);
expect( expect(
material.color, material.color,
theme.colorScheme.surface.withOpacity(0.0), theme.colorScheme.surface.withOpacity(0.0),
...@@ -573,12 +546,10 @@ void main() { ...@@ -573,12 +546,10 @@ void main() {
), ),
); );
final Material material = tester.firstWidget<Material>( final Material material = tester.widget<Material>(find.descendant(
find.descendant(
of: find.byType(RawMaterialButton), of: find.byType(RawMaterialButton),
matching: find.byType(Material), matching: find.byType(Material),
), ));
);
expect( expect(
material.color, material.color,
theme.colorScheme.primary.withOpacity(0.12), theme.colorScheme.primary.withOpacity(0.12),
...@@ -604,12 +575,10 @@ void main() { ...@@ -604,12 +575,10 @@ void main() {
), ),
); );
final Material material = tester.firstWidget<Material>( final Material material = tester.widget<Material>(find.descendant(
find.descendant(
of: find.byType(RawMaterialButton), of: find.byType(RawMaterialButton),
matching: find.byType(Material), matching: find.byType(Material),
), ));
);
expect( expect(
material.color, material.color,
theme.colorScheme.surface.withOpacity(0.0), theme.colorScheme.surface.withOpacity(0.0),
...@@ -636,12 +605,10 @@ void main() { ...@@ -636,12 +605,10 @@ void main() {
), ),
); );
final Material material = tester.firstWidget<Material>( final Material material = tester.widget<Material>(find.descendant(
find.descendant(
of: find.byType(RawMaterialButton), of: find.byType(RawMaterialButton),
matching: find.byType(Material), matching: find.byType(Material),
), ));
);
expect(material.color, customFillColor); expect(material.color, customFillColor);
expect(material.type, MaterialType.button); expect(material.type, MaterialType.button);
}); });
......
...@@ -157,6 +157,18 @@ void main() { ...@@ -157,6 +157,18 @@ void main() {
testWidgets( testWidgets(
'Theme text/icon colors for enabled, selected and disabled states', 'Theme text/icon colors for enabled, selected and disabled states',
(WidgetTester tester) async { (WidgetTester tester) async {
TextStyle buttonTextStyle(String text) {
return tester.widget<DefaultTextStyle>(find.descendant(
of: find.widgetWithText(RawMaterialButton, text),
matching: find.byType(DefaultTextStyle),
)).style;
}
IconTheme iconTheme(IconData icon) {
return tester.widget(find.descendant(
of: find.widgetWithIcon(RawMaterialButton, icon),
matching: find.byType(IconTheme),
));
}
final ThemeData theme = ThemeData(); final ThemeData theme = ThemeData();
const Color enabledColor = Colors.lime; const Color enabledColor = Colors.lime;
const Color selectedColor = Colors.green; const Color selectedColor = Colors.green;
...@@ -184,20 +196,10 @@ void main() { ...@@ -184,20 +196,10 @@ void main() {
), ),
), ),
); );
// Custom theme enabled color
DefaultTextStyle textStyle;
IconTheme iconTheme;
// custom theme enabled color
expect(theme.colorScheme.onSurface, isNot(enabledColor)); expect(theme.colorScheme.onSurface, isNot(enabledColor));
textStyle = tester.firstWidget<DefaultTextStyle>( expect(buttonTextStyle('First child').color, enabledColor);
find.widgetWithText(DefaultTextStyle, 'First child'), expect(iconTheme(Icons.check).data.color, enabledColor);
);
expect(textStyle.style.color, enabledColor);
iconTheme = tester.firstWidget<IconTheme>(
find.widgetWithIcon(IconTheme, Icons.check),
);
expect(iconTheme.data.color, enabledColor);
await tester.pumpWidget( await tester.pumpWidget(
Material( Material(
...@@ -222,16 +224,10 @@ void main() { ...@@ -222,16 +224,10 @@ void main() {
), ),
); );
await tester.pumpAndSettle(); await tester.pumpAndSettle();
// custom theme selected color // Custom theme selected color
expect(theme.colorScheme.primary, isNot(selectedColor)); expect(theme.colorScheme.primary, isNot(selectedColor));
textStyle = tester.firstWidget<DefaultTextStyle>( expect(buttonTextStyle('First child').color, selectedColor);
find.widgetWithText(DefaultTextStyle, 'First child'), expect(iconTheme(Icons.check).data.color, selectedColor);
);
expect(textStyle.style.color, selectedColor);
iconTheme = tester.firstWidget<IconTheme>(
find.widgetWithIcon(IconTheme, Icons.check),
);
expect(iconTheme.data.color, selectedColor);
await tester.pumpWidget( await tester.pumpWidget(
Material( Material(
...@@ -255,16 +251,10 @@ void main() { ...@@ -255,16 +251,10 @@ void main() {
), ),
); );
await tester.pumpAndSettle(); await tester.pumpAndSettle();
// custom theme disabled color // Custom theme disabled color
expect(theme.disabledColor, isNot(disabledColor)); expect(theme.disabledColor, isNot(disabledColor));
textStyle = tester.firstWidget<DefaultTextStyle>( expect(buttonTextStyle('First child').color, disabledColor);
find.widgetWithText(DefaultTextStyle, 'First child'), expect(iconTheme(Icons.check).data.color, disabledColor);
);
expect(textStyle.style.color, disabledColor);
iconTheme = tester.firstWidget<IconTheme>(
find.widgetWithIcon(IconTheme, Icons.check),
);
expect(iconTheme.data.color, disabledColor);
}, },
); );
...@@ -289,12 +279,10 @@ void main() { ...@@ -289,12 +279,10 @@ void main() {
), ),
); );
final Material material = tester.firstWidget<Material>( final Material material = tester.widget<Material>(find.descendant(
find.descendant(
of: find.byType(RawMaterialButton), of: find.byType(RawMaterialButton),
matching: find.byType(Material), matching: find.byType(Material),
), ));
);
expect(material.color, customFillColor); expect(material.color, customFillColor);
expect(material.type, MaterialType.button); expect(material.type, MaterialType.button);
}); });
......
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