mirror of
https://github.com/flutter/flutter
synced 2024-11-05 18:37:51 +00:00
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
This commit is contained in:
parent
a415c76b5b
commit
abfc560b9d
2 changed files with 124 additions and 169 deletions
|
@ -19,6 +19,12 @@ Widget boilerplate({Widget child}) {
|
|||
|
||||
void main() {
|
||||
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();
|
||||
await tester.pumpWidget(
|
||||
Material(
|
||||
|
@ -35,22 +41,26 @@ void main() {
|
|||
),
|
||||
);
|
||||
|
||||
final DefaultTextStyle textStyleOne = tester.firstWidget(
|
||||
find.widgetWithText(DefaultTextStyle, 'First child'),
|
||||
);
|
||||
expect(
|
||||
textStyleOne.style.color,
|
||||
buttonTextStyle('First child').color,
|
||||
theme.colorScheme.onSurface.withOpacity(0.87),
|
||||
);
|
||||
final DefaultTextStyle textStyleTwo = tester.firstWidget(
|
||||
find.widgetWithText(DefaultTextStyle, 'Second child'),
|
||||
expect(
|
||||
buttonTextStyle('Second child').color,
|
||||
theme.colorScheme.primary,
|
||||
);
|
||||
expect(textStyleTwo.style.color, theme.colorScheme.primary);
|
||||
});
|
||||
|
||||
testWidgets(
|
||||
'onPressed is triggered on button tap',
|
||||
(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 ThemeData theme = ThemeData();
|
||||
await tester.pumpWidget(
|
||||
|
@ -76,40 +86,28 @@ void main() {
|
|||
),
|
||||
);
|
||||
|
||||
DefaultTextStyle textStyleOne;
|
||||
DefaultTextStyle textStyleTwo;
|
||||
|
||||
expect(_isSelected[0], isFalse);
|
||||
expect(_isSelected[1], isTrue);
|
||||
textStyleOne = tester.firstWidget(
|
||||
find.widgetWithText(DefaultTextStyle, 'First child'),
|
||||
);
|
||||
expect(
|
||||
textStyleOne.style.color,
|
||||
buttonTextStyle('First child').color,
|
||||
theme.colorScheme.onSurface.withOpacity(0.87),
|
||||
);
|
||||
textStyleTwo = tester.firstWidget(
|
||||
find.widgetWithText(DefaultTextStyle, 'Second child'),
|
||||
expect(
|
||||
buttonTextStyle('Second child').color,
|
||||
theme.colorScheme.primary,
|
||||
);
|
||||
expect(textStyleTwo.style.color, theme.colorScheme.primary);
|
||||
|
||||
await tester.tap(find.text('Second child'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(_isSelected[0], isFalse);
|
||||
expect(_isSelected[1], isFalse);
|
||||
textStyleOne = tester.firstWidget(
|
||||
find.widgetWithText(DefaultTextStyle, 'First child'),
|
||||
);
|
||||
expect(
|
||||
textStyleOne.style.color,
|
||||
buttonTextStyle('First child').color,
|
||||
theme.colorScheme.onSurface.withOpacity(0.87),
|
||||
);
|
||||
textStyleTwo = tester.firstWidget(
|
||||
find.widgetWithText(DefaultTextStyle, 'Second child'),
|
||||
);
|
||||
expect(
|
||||
textStyleTwo.style.color,
|
||||
buttonTextStyle('Second child').color,
|
||||
theme.colorScheme.onSurface.withOpacity(0.87),
|
||||
);
|
||||
},
|
||||
|
@ -118,6 +116,12 @@ void main() {
|
|||
testWidgets(
|
||||
'onPressed that is null disables buttons',
|
||||
(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 ThemeData theme = ThemeData();
|
||||
|
||||
|
@ -135,44 +139,29 @@ void main() {
|
|||
),
|
||||
);
|
||||
|
||||
DefaultTextStyle textStyleOne;
|
||||
DefaultTextStyle textStyleTwo;
|
||||
|
||||
expect(_isSelected[0], isFalse);
|
||||
expect(_isSelected[1], isTrue);
|
||||
textStyleOne = tester.firstWidget(
|
||||
find.widgetWithText(DefaultTextStyle, 'First child'),
|
||||
);
|
||||
expect(
|
||||
textStyleOne.style.color,
|
||||
buttonTextStyle('First child').color,
|
||||
theme.colorScheme.onSurface.withOpacity(0.38),
|
||||
);
|
||||
textStyleTwo = tester.firstWidget(
|
||||
find.widgetWithText(DefaultTextStyle, 'Second child'),
|
||||
);
|
||||
expect(
|
||||
textStyleTwo.style.color,
|
||||
buttonTextStyle('Second child').color,
|
||||
theme.colorScheme.onSurface.withOpacity(0.38),
|
||||
);
|
||||
|
||||
await tester.tap(find.text('Second child'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// nothing should change
|
||||
// Nothing should change
|
||||
expect(_isSelected[0], isFalse);
|
||||
expect(_isSelected[1], isTrue);
|
||||
textStyleOne = tester.firstWidget(
|
||||
find.widgetWithText(DefaultTextStyle, 'First child'),
|
||||
);
|
||||
expect(
|
||||
textStyleOne.style.color,
|
||||
buttonTextStyle('First child').color,
|
||||
theme.colorScheme.onSurface.withOpacity(0.38),
|
||||
);
|
||||
textStyleTwo = tester.firstWidget(
|
||||
find.widgetWithText(DefaultTextStyle, 'Second child'),
|
||||
);
|
||||
expect(
|
||||
textStyleTwo.style.color,
|
||||
buttonTextStyle('Second child').color,
|
||||
theme.colorScheme.onSurface.withOpacity(0.38),
|
||||
);
|
||||
},
|
||||
|
@ -191,8 +180,7 @@ void main() {
|
|||
),
|
||||
),
|
||||
);
|
||||
fail(
|
||||
'Should not be possible to create a toggle button with no children.');
|
||||
fail('Should not be possible to create a toggle button with no children.');
|
||||
} on AssertionError catch (e) {
|
||||
expect(e.toString(), contains('children != null'));
|
||||
}
|
||||
|
@ -216,8 +204,7 @@ void main() {
|
|||
),
|
||||
),
|
||||
);
|
||||
fail(
|
||||
'Should not be possible to create a toggle button with no isSelected.');
|
||||
fail('Should not be possible to create a toggle button with no isSelected.');
|
||||
} on AssertionError catch (e) {
|
||||
expect(e.toString(), contains('isSelected != null'));
|
||||
}
|
||||
|
@ -325,6 +312,18 @@ void main() {
|
|||
testWidgets(
|
||||
'Default text/icon colors for enabled, selected and disabled states',
|
||||
(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();
|
||||
await tester.pumpWidget(
|
||||
Material(
|
||||
|
@ -343,22 +342,13 @@ void main() {
|
|||
),
|
||||
);
|
||||
|
||||
DefaultTextStyle textStyle;
|
||||
IconTheme iconTheme;
|
||||
|
||||
// default enabled color
|
||||
textStyle = tester.firstWidget(
|
||||
find.widgetWithText(DefaultTextStyle, 'First child'),
|
||||
);
|
||||
// Default enabled color
|
||||
expect(
|
||||
textStyle.style.color,
|
||||
buttonTextStyle('First child').color,
|
||||
theme.colorScheme.onSurface.withOpacity(0.87),
|
||||
);
|
||||
iconTheme = tester.firstWidget(
|
||||
find.widgetWithIcon(IconTheme, Icons.check),
|
||||
);
|
||||
expect(
|
||||
iconTheme.data.color,
|
||||
iconTheme(Icons.check).data.color,
|
||||
theme.colorScheme.onSurface.withOpacity(0.87),
|
||||
);
|
||||
|
||||
|
@ -379,15 +369,15 @@ void main() {
|
|||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
// default selected color
|
||||
textStyle = tester.firstWidget(
|
||||
find.widgetWithText(DefaultTextStyle, 'First child'),
|
||||
// Default selected color
|
||||
expect(
|
||||
buttonTextStyle('First child').color,
|
||||
theme.colorScheme.primary,
|
||||
);
|
||||
expect(textStyle.style.color, theme.colorScheme.primary);
|
||||
iconTheme = tester.firstWidget(
|
||||
find.widgetWithIcon(IconTheme, Icons.check),
|
||||
expect(
|
||||
iconTheme(Icons.check).data.color,
|
||||
theme.colorScheme.primary,
|
||||
);
|
||||
expect(iconTheme.data.color, theme.colorScheme.primary);
|
||||
|
||||
await tester.pumpWidget(
|
||||
Material(
|
||||
|
@ -405,19 +395,13 @@ void main() {
|
|||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
// default disabled color
|
||||
textStyle = tester.firstWidget(
|
||||
find.widgetWithText(DefaultTextStyle, 'First child'),
|
||||
);
|
||||
// Default disabled color
|
||||
expect(
|
||||
textStyle.style.color,
|
||||
buttonTextStyle('First child').color,
|
||||
theme.colorScheme.onSurface.withOpacity(0.38),
|
||||
);
|
||||
iconTheme = tester.firstWidget(
|
||||
find.widgetWithIcon(IconTheme, Icons.check),
|
||||
);
|
||||
expect(
|
||||
iconTheme.data.color,
|
||||
iconTheme(Icons.check).data.color,
|
||||
theme.colorScheme.onSurface.withOpacity(0.38),
|
||||
);
|
||||
},
|
||||
|
@ -426,12 +410,24 @@ void main() {
|
|||
testWidgets(
|
||||
'Custom text/icon colors for enabled, selected and disabled states',
|
||||
(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();
|
||||
const Color enabledColor = Colors.lime;
|
||||
const Color selectedColor = Colors.green;
|
||||
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.primary, isNot(selectedColor));
|
||||
expect(theme.colorScheme.onSurface.withOpacity(0.38), isNot(disabledColor));
|
||||
|
@ -454,18 +450,9 @@ void main() {
|
|||
),
|
||||
);
|
||||
|
||||
DefaultTextStyle textStyle;
|
||||
IconTheme iconTheme;
|
||||
|
||||
// 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);
|
||||
// Custom enabled color
|
||||
expect(buttonTextStyle('First child').color, enabledColor);
|
||||
expect(iconTheme(Icons.check).data.color, enabledColor);
|
||||
|
||||
await tester.pumpWidget(
|
||||
Material(
|
||||
|
@ -485,15 +472,9 @@ void main() {
|
|||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
// custom selected color
|
||||
textStyle = tester.firstWidget(
|
||||
find.widgetWithText(DefaultTextStyle, 'First child'),
|
||||
);
|
||||
expect(textStyle.style.color, selectedColor);
|
||||
iconTheme = tester.firstWidget(
|
||||
find.widgetWithIcon(IconTheme, Icons.check),
|
||||
);
|
||||
expect(iconTheme.data.color, selectedColor);
|
||||
// Custom selected color
|
||||
expect(buttonTextStyle('First child').color, selectedColor);
|
||||
expect(iconTheme(Icons.check).data.color, selectedColor);
|
||||
|
||||
await tester.pumpWidget(
|
||||
Material(
|
||||
|
@ -512,15 +493,9 @@ void main() {
|
|||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
// custom disabled color
|
||||
textStyle = tester.firstWidget(
|
||||
find.widgetWithText(DefaultTextStyle, 'First child'),
|
||||
);
|
||||
expect(textStyle.style.color, disabledColor);
|
||||
iconTheme = tester.firstWidget(
|
||||
find.widgetWithIcon(IconTheme, Icons.check),
|
||||
);
|
||||
expect(iconTheme.data.color, disabledColor);
|
||||
// Custom disabled color
|
||||
expect(buttonTextStyle('First child').color, disabledColor);
|
||||
expect(iconTheme(Icons.check).data.color, disabledColor);
|
||||
},
|
||||
);
|
||||
|
||||
|
@ -542,12 +517,10 @@ void main() {
|
|||
),
|
||||
);
|
||||
|
||||
final Material material = tester.firstWidget<Material>(
|
||||
find.descendant(
|
||||
of: find.byType(RawMaterialButton),
|
||||
matching: find.byType(Material),
|
||||
),
|
||||
);
|
||||
final Material material = tester.widget<Material>(find.descendant(
|
||||
of: find.byType(RawMaterialButton),
|
||||
matching: find.byType(Material),
|
||||
));
|
||||
expect(
|
||||
material.color,
|
||||
theme.colorScheme.surface.withOpacity(0.0),
|
||||
|
@ -573,12 +546,10 @@ void main() {
|
|||
),
|
||||
);
|
||||
|
||||
final Material material = tester.firstWidget<Material>(
|
||||
find.descendant(
|
||||
of: find.byType(RawMaterialButton),
|
||||
matching: find.byType(Material),
|
||||
),
|
||||
);
|
||||
final Material material = tester.widget<Material>(find.descendant(
|
||||
of: find.byType(RawMaterialButton),
|
||||
matching: find.byType(Material),
|
||||
));
|
||||
expect(
|
||||
material.color,
|
||||
theme.colorScheme.primary.withOpacity(0.12),
|
||||
|
@ -604,12 +575,10 @@ void main() {
|
|||
),
|
||||
);
|
||||
|
||||
final Material material = tester.firstWidget<Material>(
|
||||
find.descendant(
|
||||
of: find.byType(RawMaterialButton),
|
||||
matching: find.byType(Material),
|
||||
),
|
||||
);
|
||||
final Material material = tester.widget<Material>(find.descendant(
|
||||
of: find.byType(RawMaterialButton),
|
||||
matching: find.byType(Material),
|
||||
));
|
||||
expect(
|
||||
material.color,
|
||||
theme.colorScheme.surface.withOpacity(0.0),
|
||||
|
@ -636,12 +605,10 @@ void main() {
|
|||
),
|
||||
);
|
||||
|
||||
final Material material = tester.firstWidget<Material>(
|
||||
find.descendant(
|
||||
of: find.byType(RawMaterialButton),
|
||||
matching: find.byType(Material),
|
||||
),
|
||||
);
|
||||
final Material material = tester.widget<Material>(find.descendant(
|
||||
of: find.byType(RawMaterialButton),
|
||||
matching: find.byType(Material),
|
||||
));
|
||||
expect(material.color, customFillColor);
|
||||
expect(material.type, MaterialType.button);
|
||||
});
|
||||
|
|
|
@ -157,6 +157,18 @@ void main() {
|
|||
testWidgets(
|
||||
'Theme text/icon colors for enabled, selected and disabled states',
|
||||
(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();
|
||||
const Color enabledColor = Colors.lime;
|
||||
const Color selectedColor = Colors.green;
|
||||
|
@ -184,20 +196,10 @@ void main() {
|
|||
),
|
||||
),
|
||||
);
|
||||
|
||||
DefaultTextStyle textStyle;
|
||||
IconTheme iconTheme;
|
||||
|
||||
// custom theme enabled color
|
||||
// Custom theme enabled color
|
||||
expect(theme.colorScheme.onSurface, isNot(enabledColor));
|
||||
textStyle = tester.firstWidget<DefaultTextStyle>(
|
||||
find.widgetWithText(DefaultTextStyle, 'First child'),
|
||||
);
|
||||
expect(textStyle.style.color, enabledColor);
|
||||
iconTheme = tester.firstWidget<IconTheme>(
|
||||
find.widgetWithIcon(IconTheme, Icons.check),
|
||||
);
|
||||
expect(iconTheme.data.color, enabledColor);
|
||||
expect(buttonTextStyle('First child').color, enabledColor);
|
||||
expect(iconTheme(Icons.check).data.color, enabledColor);
|
||||
|
||||
await tester.pumpWidget(
|
||||
Material(
|
||||
|
@ -222,16 +224,10 @@ void main() {
|
|||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
// custom theme selected color
|
||||
// Custom theme selected color
|
||||
expect(theme.colorScheme.primary, isNot(selectedColor));
|
||||
textStyle = tester.firstWidget<DefaultTextStyle>(
|
||||
find.widgetWithText(DefaultTextStyle, 'First child'),
|
||||
);
|
||||
expect(textStyle.style.color, selectedColor);
|
||||
iconTheme = tester.firstWidget<IconTheme>(
|
||||
find.widgetWithIcon(IconTheme, Icons.check),
|
||||
);
|
||||
expect(iconTheme.data.color, selectedColor);
|
||||
expect(buttonTextStyle('First child').color, selectedColor);
|
||||
expect(iconTheme(Icons.check).data.color, selectedColor);
|
||||
|
||||
await tester.pumpWidget(
|
||||
Material(
|
||||
|
@ -255,16 +251,10 @@ void main() {
|
|||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
// custom theme disabled color
|
||||
// Custom theme disabled color
|
||||
expect(theme.disabledColor, isNot(disabledColor));
|
||||
textStyle = tester.firstWidget<DefaultTextStyle>(
|
||||
find.widgetWithText(DefaultTextStyle, 'First child'),
|
||||
);
|
||||
expect(textStyle.style.color, disabledColor);
|
||||
iconTheme = tester.firstWidget<IconTheme>(
|
||||
find.widgetWithIcon(IconTheme, Icons.check),
|
||||
);
|
||||
expect(iconTheme.data.color, disabledColor);
|
||||
expect(buttonTextStyle('First child').color, disabledColor);
|
||||
expect(iconTheme(Icons.check).data.color, disabledColor);
|
||||
},
|
||||
);
|
||||
|
||||
|
@ -289,12 +279,10 @@ void main() {
|
|||
),
|
||||
);
|
||||
|
||||
final Material material = tester.firstWidget<Material>(
|
||||
find.descendant(
|
||||
of: find.byType(RawMaterialButton),
|
||||
matching: find.byType(Material),
|
||||
),
|
||||
);
|
||||
final Material material = tester.widget<Material>(find.descendant(
|
||||
of: find.byType(RawMaterialButton),
|
||||
matching: find.byType(Material),
|
||||
));
|
||||
expect(material.color, customFillColor);
|
||||
expect(material.type, MaterialType.button);
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue