Skip to content

Commit

Permalink
Update ToggleButtons, ExpansionPanel, and ExpandIcon tests for …
Browse files Browse the repository at this point in the history
…Material 3 (#141868)

Updated unit tests for `ToggleButtons`, `ExpansionPanel`, and `ExpandIcon` to have M2 and M3 versions.

More info in #139076
  • Loading branch information
TahaTesser authored Jan 22, 2024
1 parent 5caa8b8 commit 0ef4638
Show file tree
Hide file tree
Showing 3 changed files with 419 additions and 22 deletions.
86 changes: 82 additions & 4 deletions packages/flutter/test/material/expand_icon_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ void main() {
expect(iconTheme.data.color, equals(Colors.white60));
});

testWidgets('ExpandIcon disabled', (WidgetTester tester) async {
testWidgets('Material2 - ExpandIcon disabled', (WidgetTester tester) async {
IconTheme iconTheme;
// Light mode test
// Test light mode.
await tester.pumpWidget(wrap(
theme: ThemeData(useMaterial3: false),
child: const ExpandIcon(onPressed: null),
Expand All @@ -85,7 +85,7 @@ void main() {
iconTheme = tester.firstWidget(find.byType(IconTheme).last);
expect(iconTheme.data.color, equals(Colors.black38));

// Dark mode test
// Test dark mode.
await tester.pumpWidget(wrap(
child: const ExpandIcon(onPressed: null),
theme: ThemeData(useMaterial3: false, brightness: Brightness.dark),
Expand All @@ -96,6 +96,37 @@ void main() {
expect(iconTheme.data.color, equals(Colors.white38));
});

testWidgets('Material3 - ExpandIcon disabled', (WidgetTester tester) async {
ThemeData theme = ThemeData();
IconTheme iconTheme;
// Test light mode.
await tester.pumpWidget(wrap(
theme: theme,
child: const ExpandIcon(onPressed: null),
));
await tester.pumpAndSettle();

iconTheme = tester.firstWidget(find.byType(IconTheme).last);
expect(
iconTheme.data.color,
equals(theme.colorScheme.onSurface.withOpacity(0.38)),
);

theme = ThemeData(brightness: Brightness.dark);
// Test dark mode.
await tester.pumpWidget(wrap(
theme: theme,
child: const ExpandIcon(onPressed: null),
));
await tester.pumpAndSettle();

iconTheme = tester.firstWidget(find.byType(IconTheme).last);
expect(
iconTheme.data.color,
equals(theme.colorScheme.onSurface.withOpacity(0.38)),
);
});

testWidgets('ExpandIcon test isExpanded does not trigger callback', (WidgetTester tester) async {
bool expanded = false;

Expand Down Expand Up @@ -173,7 +204,7 @@ void main() {
expect(icon.size, 48);
});

testWidgets('ExpandIcon has correct semantic hints', (WidgetTester tester) async {
testWidgets('Material2 - ExpandIcon has correct semantic hints', (WidgetTester tester) async {
final SemanticsHandle handle = tester.ensureSemantics();
const DefaultMaterialLocalizations localizations = DefaultMaterialLocalizations();
await tester.pumpWidget(wrap(
Expand All @@ -194,6 +225,7 @@ void main() {
));

await tester.pumpWidget(wrap(
theme: ThemeData(useMaterial3: false),
child: ExpandIcon(
onPressed: (bool _) { },
),
Expand All @@ -210,6 +242,52 @@ void main() {
handle.dispose();
});

testWidgets('Material3 - ExpandIcon has correct semantic hints', (WidgetTester tester) async {
final SemanticsHandle handle = tester.ensureSemantics();
const DefaultMaterialLocalizations localizations = DefaultMaterialLocalizations();

await tester.pumpWidget(wrap(
child: ExpandIcon(
isExpanded: true,
onPressed: (bool _) { },
),
));

expect(tester.getSemantics(find.byType(ExpandIcon)), matchesSemantics(
onTapHint: localizations.expandedIconTapHint,
children: <Matcher>[
matchesSemantics(
hasTapAction: true,
hasEnabledState: true,
isEnabled: true,
isFocusable: true,
isButton: true,
),
],
));

await tester.pumpWidget(wrap(
child: ExpandIcon(
onPressed: (bool _) { },
),
));

expect(tester.getSemantics(find.byType(ExpandIcon)), matchesSemantics(
onTapHint: localizations.collapsedIconTapHint,
children: <Matcher>[
matchesSemantics(
hasTapAction: true,
hasEnabledState: true,
isEnabled: true,
isFocusable: true,
isButton: true,
),
],
));

handle.dispose();
});

testWidgets('ExpandIcon uses custom icon color and expanded icon color', (WidgetTester tester) async {
bool expanded = false;
IconTheme iconTheme;
Expand Down
149 changes: 147 additions & 2 deletions packages/flutter/test/material/expansion_panel_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ void main() {
expect(box.size.height - oldHeight, greaterThanOrEqualTo(100.0)); // 100 + some margin
});

testWidgets('ExpansionPanelList does not merge header when canTapOnHeader is false', (WidgetTester tester) async {
testWidgets('Material2 - ExpansionPanelList does not merge header when canTapOnHeader is false', (WidgetTester tester) async {
final SemanticsHandle handle = tester.ensureSemantics();
final Key headerKey = UniqueKey();
await tester.pumpWidget(
Expand Down Expand Up @@ -226,6 +226,58 @@ void main() {
handle.dispose();
});

testWidgets('Material3 - ExpansionPanelList does not merge header when canTapOnHeader is false', (WidgetTester tester) async {
final SemanticsHandle handle = tester.ensureSemantics();
final Key headerKey = UniqueKey();
await tester.pumpWidget(
MaterialApp(
home: ExpansionPanelListSemanticsTest(headerKey: headerKey),
),
);

// Make sure custom gesture detector widget is clickable.
await tester.tap(find.text('head1'));
await tester.pump();

final ExpansionPanelListSemanticsTestState state =
tester.state(find.byType(ExpansionPanelListSemanticsTest));
expect(state.headerTapped, true);

// Check the expansion icon semantics does not merged with header widget.
final Finder expansionIcon = find.descendant(
of: find.ancestor(
of: find.byKey(headerKey),
matching: find.byType(Row),
),
matching: find.byType(ExpandIcon),
);

expect(tester.getSemantics(expansionIcon), matchesSemantics(
label: 'Expand',
children: <Matcher>[
matchesSemantics(
isButton: true,
hasEnabledState: true,
isEnabled: true,
isFocusable: true,
hasTapAction: true,
),
],
));

// Check custom header widget semantics is preserved.
final Finder headerWidget = find.descendant(
of: find.byKey(headerKey),
matching: find.byType(RichText),
);
expect(tester.getSemantics(headerWidget), matchesSemantics(
label: 'head1',
hasTapAction: true,
));

handle.dispose();
});

testWidgets('Multiple Panel List test', (WidgetTester tester) async {
await tester.pumpWidget(
MaterialApp(
Expand Down Expand Up @@ -998,7 +1050,7 @@ void main() {
await tester.pumpAndSettle();
});

testWidgets('Panel header has semantics, canTapOnHeader = false ', (WidgetTester tester) async {
testWidgets('Material2 - Panel header has semantics, canTapOnHeader = false', (WidgetTester tester) async {
const Key expandedKey = Key('expanded');
const Key collapsedKey = Key('collapsed');
const DefaultMaterialLocalizations localizations = DefaultMaterialLocalizations();
Expand Down Expand Up @@ -1083,6 +1135,99 @@ void main() {
handle.dispose();
});

testWidgets('Material3 - Panel header has semantics, canTapOnHeader = false', (WidgetTester tester) async {
const Key expandedKey = Key('expanded');
const Key collapsedKey = Key('collapsed');
const DefaultMaterialLocalizations localizations = DefaultMaterialLocalizations();
final SemanticsHandle handle = tester.ensureSemantics();
final List<ExpansionPanel> demoItems = <ExpansionPanel>[
ExpansionPanel(
headerBuilder: (BuildContext context, bool isExpanded) {
return const Text('Expanded', key: expandedKey);
},
body: const SizedBox(height: 100.0),
isExpanded: true,
),
ExpansionPanel(
headerBuilder: (BuildContext context, bool isExpanded) {
return const Text('Collapsed', key: collapsedKey);
},
body: const SizedBox(height: 100.0),
),
];

final ExpansionPanelList expansionList = ExpansionPanelList(
children: demoItems,
);

await tester.pumpWidget(
MaterialApp(
home: SingleChildScrollView(
child: expansionList,
),
),
);

// Check the semantics of [ExpandIcon] for expanded panel.
final Finder expandedIcon = find.descendant(
of: find.ancestor(
of: find.byKey(expandedKey),
matching: find.byType(Row),
),
matching: find.byType(ExpandIcon),
);

expect(tester.getSemantics(expandedIcon), matchesSemantics(
label: 'Collapse',
onTapHint: localizations.expandedIconTapHint,
children: <Matcher>[
matchesSemantics(
isButton: true,
hasEnabledState: true,
isEnabled: true,
isFocusable: true,
hasTapAction: true,
),
],
));

// Check the semantics of the header widget for expanded panel.
final Finder expandedHeader = find.byKey(expandedKey);
expect(tester.getSemantics(expandedHeader), matchesSemantics(
label: 'Expanded',
));

// Check the semantics of [ExpandIcon] for collapsed panel.
final Finder collapsedIcon = find.descendant(
of: find.ancestor(
of: find.byKey(collapsedKey),
matching: find.byType(Row),
),
matching: find.byType(ExpandIcon),
);
expect(tester.getSemantics(collapsedIcon), matchesSemantics(
label: 'Expand',
onTapHint: localizations.collapsedIconTapHint,
children: <Matcher>[
matchesSemantics(
isButton: true,
hasEnabledState: true,
isEnabled: true,
isFocusable: true,
hasTapAction: true,
),
],
));

// Check the semantics of the header widget for expanded panel.
final Finder collapsedHeader = find.byKey(collapsedKey);
expect(tester.getSemantics(collapsedHeader), matchesSemantics(
label: 'Collapsed',
));

handle.dispose();
});

testWidgets('Panel header has semantics, canTapOnHeader = true', (WidgetTester tester) async {
const Key expandedKey = Key('expanded');
const Key collapsedKey = Key('collapsed');
Expand Down
Loading

0 comments on commit 0ef4638

Please sign in to comment.