Skip to content

Commit 468eb03

Browse files
justinmcvictorsanni
authored andcommitted
[material_ui] Migrate off of flutter_test's find.byTooltip and on to the local findByTooltip (flutter#12492)
The [find.byTooltip](https://github.com/flutter/flutter/blob/9a353b5ad5e00932819a1ff959f0dc1a65af8f73/packages/flutter_test/lib/src/finders.dart#L396-L415) finder in flutter/flutter's flutter_test package is unreliable for material_ui due to using flutter/flutter's Tooltip class. This PR migrates all usages to the existing [findByTooltip](https://github.com/flutter/packages/blob/a3e763e60321146e9f79571ca917c824b25e62ce/packages/material_ui/test/finders.dart#L24-L43) in material_ui, which does not have this problem. See flutter/flutter#191063 (comment) Fixes flutter/flutter#191063
1 parent b61c1e3 commit 468eb03

14 files changed

Lines changed: 176 additions & 89 deletions

‎packages/material_ui/example/test/bottom_app_bar/bottom_app_bar.2_test.dart‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,49 @@ void main() {
7070
await tester.pumpWidget(const example.BottomAppBarDemo());
7171

7272
// Trigger the SnackBar.
73-
await tester.tap(find.byTooltip('Open popup menu'));
73+
await tester.tap(findByTooltip('Open popup menu'));
7474
await tester.pump();
7575

7676
expect(find.text('Yay! A SnackBar!'), findsOneWidget);
7777

7878
expect(find.text('Undo'), findsOneWidget);
7979
});
8080
}
81+
82+
/// Finds [RawTooltip] or [Tooltip] widgets with the given `message`.
83+
///
84+
/// ## Sample code
85+
///
86+
/// ```dart
87+
/// expect(findByTooltip('Back'), findsOneWidget);
88+
/// expect(findByTooltip(RegExp('Back.*')), findsNWidgets(2));
89+
/// ```
90+
///
91+
/// If the `skipOffstage` argument is true (the default), then this skips
92+
/// nodes that are [Offstage] or that are from inactive [Route]s.
93+
///
94+
/// This was copied from flutter_test, which uses flutter/material.dart.
95+
///
96+
Finder findByTooltip(Pattern message, {bool skipOffstage = true}) {
97+
return find.byWidgetPredicate((Widget widget) {
98+
// Compare RawTooltip's semantics tooltip with the given message.
99+
// However, Tooltip's message needs to be checked directly if:
100+
// 1. Tooltip.excludeFromSemantics is true, since in this case Tooltip
101+
// provides no semantics tooltip to the underlying RawTooltip.
102+
// 2. Tooltip.message and Tooltip.richMessage are empty, since in this
103+
// case no RawTooltip is created.
104+
if (widget is Tooltip) {
105+
final String tooltipMessage =
106+
widget.message ?? widget.richMessage!.toPlainText();
107+
if ((widget.excludeFromSemantics ?? false) || tooltipMessage.isEmpty) {
108+
return message is RegExp
109+
? message.hasMatch(tooltipMessage)
110+
: tooltipMessage == message;
111+
}
112+
}
113+
return widget is RawTooltip &&
114+
(message is RegExp
115+
? message.hasMatch(widget.semanticsTooltip ?? '')
116+
: widget.semanticsTooltip == message);
117+
}, skipOffstage: skipOffstage);
118+
}

‎packages/material_ui/example/test/tooltip/tooltip.3_test.dart‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,46 @@ void main() {
2121
await tester.pump(const Duration(milliseconds: 10));
2222
expect(find.text(tooltipText), findsOneWidget);
2323
// Tap on the tooltip and wait for the tooltip to disappear.
24-
await tester.tap(find.byTooltip(tooltipText));
24+
await tester.tap(findByTooltip(tooltipText));
2525
await tester.pump(const Duration(seconds: 1));
2626
expect(find.text(tooltipText), findsNothing);
2727
});
2828
}
29+
30+
/// Finds [RawTooltip] or [Tooltip] widgets with the given `message`.
31+
///
32+
/// ## Sample code
33+
///
34+
/// ```dart
35+
/// expect(findByTooltip('Back'), findsOneWidget);
36+
/// expect(findByTooltip(RegExp('Back.*')), findsNWidgets(2));
37+
/// ```
38+
///
39+
/// If the `skipOffstage` argument is true (the default), then this skips
40+
/// nodes that are [Offstage] or that are from inactive [Route]s.
41+
///
42+
/// This was copied from flutter_test, which uses flutter/material.dart.
43+
///
44+
Finder findByTooltip(Pattern message, {bool skipOffstage = true}) {
45+
return find.byWidgetPredicate((Widget widget) {
46+
// Compare RawTooltip's semantics tooltip with the given message.
47+
// However, Tooltip's message needs to be checked directly if:
48+
// 1. Tooltip.excludeFromSemantics is true, since in this case Tooltip
49+
// provides no semantics tooltip to the underlying RawTooltip.
50+
// 2. Tooltip.message and Tooltip.richMessage are empty, since in this
51+
// case no RawTooltip is created.
52+
if (widget is Tooltip) {
53+
final String tooltipMessage =
54+
widget.message ?? widget.richMessage!.toPlainText();
55+
if ((widget.excludeFromSemantics ?? false) || tooltipMessage.isEmpty) {
56+
return message is RegExp
57+
? message.hasMatch(tooltipMessage)
58+
: tooltipMessage == message;
59+
}
60+
}
61+
return widget is RawTooltip &&
62+
(message is RegExp
63+
? message.hasMatch(widget.semanticsTooltip ?? '')
64+
: widget.semanticsTooltip == message);
65+
}, skipOffstage: skipOffstage);
66+
}

‎packages/material_ui/test/app_bar_test.dart‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:flutter_test/flutter_test.dart';
99
import 'package:material_ui/material_ui.dart';
1010

1111
import 'app_bar_utils.dart';
12+
import 'finders.dart';
1213
import 'semantics_tester.dart';
1314

1415
TextStyle? _iconStyle(WidgetTester tester, IconData icon) {
@@ -1956,7 +1957,7 @@ void main() {
19561957
),
19571958
);
19581959

1959-
final Finder endDrawerFinder = find.byTooltip('Open navigation menu');
1960+
final Finder endDrawerFinder = findByTooltip('Open navigation menu');
19601961
await tester.tap(endDrawerFinder);
19611962
await tester.pump();
19621963

‎packages/material_ui/test/chip_test.dart‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import 'package:flutter_test/flutter_test.dart';
1616
import 'package:material_ui/material_ui.dart';
1717

1818
import 'feedback_tester.dart';
19+
import 'finders.dart';
1920
import 'semantics_tester.dart';
2021

2122
Finder findRenderChipElement() {
@@ -327,19 +328,19 @@ void main() {
327328
),
328329
);
329330

330-
expect(tester.widget(find.byTooltip('Delete chip A')), isNotNull);
331-
expect(tester.widget(find.byTooltip('Delete chip B')), isNotNull);
331+
expect(tester.widget(findByTooltip('Delete chip A')), isNotNull);
332+
expect(tester.widget(findByTooltip('Delete chip B')), isNotNull);
332333

333334
expect(feedback.clickSoundCount, 0);
334335

335336
expect(deletedChipLabels, isEmpty);
336-
await tester.tap(find.byTooltip('Delete chip A'));
337+
await tester.tap(findByTooltip('Delete chip A'));
337338
expect(deletedChipLabels, equals(<String>['A']));
338339

339340
await tester.pumpAndSettle(const Duration(seconds: 1));
340341
expect(feedback.clickSoundCount, 1);
341342

342-
await tester.tap(find.byTooltip('Delete chip B'));
343+
await tester.tap(findByTooltip('Delete chip B'));
343344
expect(deletedChipLabels, equals(<String>['A', 'B']));
344345

345346
await tester.pumpAndSettle(const Duration(seconds: 1));

‎packages/material_ui/test/finders.dart‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import 'package:material_ui/material_ui.dart';
1010
/// ## Sample code
1111
///
1212
/// ```dart
13-
/// expect(find.byTooltip('Back'), findsOneWidget);
14-
/// expect(find.byTooltip(RegExp('Back.*')), findsNWidgets(2));
13+
/// expect(findByTooltip('Back'), findsOneWidget);
14+
/// expect(findByTooltip(RegExp('Back.*')), findsNWidgets(2));
1515
/// ```
1616
///
1717
/// If the `skipOffstage` argument is true (the default), then this skips

‎packages/material_ui/test/floating_action_button_test.dart‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import 'package:flutter_test/flutter_test.dart';
1717
import 'package:material_ui/material_ui.dart';
1818

1919
import 'feedback_tester.dart';
20+
import 'finders.dart';
2021
import 'semantics_tester.dart';
2122

2223
void main() {
@@ -58,7 +59,7 @@ void main() {
5859
);
5960

6061
await tester.tap(find.byType(Icon));
61-
expect(find.byTooltip('Add'), findsOneWidget);
62+
expect(findByTooltip('Add'), findsOneWidget);
6263
});
6364

6465
// Regression test for: https://github.com/flutter/flutter/pull/21084

‎packages/material_ui/test/icon_button_test.dart‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import 'package:flutter_test/flutter_test.dart';
1616
import 'package:material_ui/material_ui.dart';
1717

1818
import 'feedback_tester.dart';
19+
import 'finders.dart';
1920
import 'semantics_tester.dart';
2021

2122
class MockOnPressedFunction {
@@ -437,9 +438,9 @@ void main() {
437438
await tester.pumpWidget(buildIconButton(tooltip: tooltipText));
438439

439440
expect(find.byType(Tooltip), findsOneWidget);
440-
expect(find.byTooltip(tooltipText), findsOneWidget);
441+
expect(findByTooltip(tooltipText), findsOneWidget);
441442

442-
await tester.tap(find.byTooltip(tooltipText));
443+
await tester.tap(findByTooltip(tooltipText));
443444
expect(mockOnPressedFunction.called, 1);
444445

445446
// Hovering over the button should show the tooltip.

‎packages/material_ui/test/page_test.dart‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import 'package:flutter/rendering.dart';
1414
import 'package:flutter/services.dart';
1515
import 'package:flutter_test/flutter_test.dart';
1616
import 'package:material_ui/material_ui.dart';
17+
import 'finders.dart';
1718

1819
void main() {
1920
testWidgets(
@@ -1063,7 +1064,7 @@ void main() {
10631064
expect(pageTapCount, 1);
10641065

10651066
// Tapping the "page" route's back button doesn't do anything either.
1066-
await tester.tap(find.byTooltip('Back'), warnIfMissed: false);
1067+
await tester.tap(findByTooltip('Back'), warnIfMissed: false);
10671068
await tester.pumpAndSettle();
10681069
expect(tester.getTopLeft(find.byKey(pageScaffoldKey)), const Offset(400, 0));
10691070
expect(tester.getTopLeft(find.byKey(homeScaffoldKey)).dx, lessThan(0));

‎packages/material_ui/test/paginated_data_table_test.dart‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'package:flutter_test/flutter_test.dart';
88
import 'package:material_ui/material_ui.dart';
99

1010
import 'data_table_test_utils.dart';
11+
import 'finders.dart';
1112

1213
class TestDataSource extends DataTableSource {
1314
TestDataSource({this.allowSelection = false});
@@ -94,7 +95,7 @@ void main() {
9495
),
9596
);
9697

97-
await tester.tap(find.byTooltip('Next page'));
98+
await tester.tap(findByTooltip('Next page'));
9899

99100
expect(log, <String>['page-changed: 2']);
100101
log.clear();
@@ -117,7 +118,7 @@ void main() {
117118
expect(find.text('Gingerbread (0)'), findsNothing);
118119

119120
final Finder lastPageButton = find.ancestor(
120-
of: find.byTooltip('Last page'),
121+
of: findByTooltip('Last page'),
121122
matching: find.byWidgetPredicate((Widget widget) => widget is IconButton),
122123
);
123124

@@ -137,7 +138,7 @@ void main() {
137138
expect(find.text('KitKat (49)'), findsOneWidget);
138139

139140
final Finder firstPageButton = find.ancestor(
140-
of: find.byTooltip('First page'),
141+
of: findByTooltip('First page'),
141142
matching: find.byWidgetPredicate((Widget widget) => widget is IconButton),
142143
);
143144

@@ -194,13 +195,13 @@ void main() {
194195

195196
expect(find.text('1–2 of 500'), findsOneWidget);
196197

197-
await tester.tap(find.byTooltip('Next page'));
198+
await tester.tap(findByTooltip('Next page'));
198199
await tester.pump();
199200

200201
expect(find.text('3–4 of 500'), findsOneWidget);
201202

202203
final Finder lastPageButton = find.ancestor(
203-
of: find.byTooltip('Last page'),
204+
of: findByTooltip('Last page'),
204205
matching: find.byWidgetPredicate((Widget widget) => widget is IconButton),
205206
);
206207

@@ -220,7 +221,7 @@ void main() {
220221

221222
expect(find.textContaining('1–3 of 500'), findsOneWidget);
222223

223-
await tester.tap(find.byTooltip('Next page'));
224+
await tester.tap(findByTooltip('Next page'));
224225
await tester.pump();
225226

226227
expect(find.text('4–6 of 500'), findsOneWidget);
@@ -238,7 +239,7 @@ void main() {
238239

239240
expect(find.textContaining('1–4 of 500'), findsOneWidget);
240241

241-
await tester.tap(find.byTooltip('Next page'));
242+
await tester.tap(findByTooltip('Next page'));
242243
await tester.pump();
243244

244245
expect(find.text('5–8 of 500'), findsOneWidget);
@@ -256,7 +257,7 @@ void main() {
256257

257258
expect(find.textContaining('1–5 of 500'), findsOneWidget);
258259

259-
await tester.tap(find.byTooltip('Next page'));
260+
await tester.tap(findByTooltip('Next page'));
260261
await tester.pump();
261262

262263
expect(find.text('6–10 of 500'), findsOneWidget);
@@ -274,7 +275,7 @@ void main() {
274275

275276
expect(find.textContaining('1–8 of 500'), findsOneWidget);
276277

277-
await tester.tap(find.byTooltip('Next page'));
278+
await tester.tap(findByTooltip('Next page'));
278279
await tester.pump();
279280

280281
expect(find.text('9–16 of 500'), findsOneWidget);

‎packages/material_ui/test/popup_menu_test.dart‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import 'package:flutter_test/flutter_test.dart';
1111
import 'package:material_ui/material_ui.dart';
1212

1313
import 'feedback_tester.dart';
14+
import 'finders.dart';
1415
import 'semantics_tester.dart';
1516

1617
void main() {
@@ -2356,7 +2357,7 @@ void main() {
23562357
// The default tooltip is defined as [MaterialLocalizations.showMenuTooltip]
23572358
// and it is used when no tooltip is provided.
23582359
expect(find.byType(Tooltip), findsNWidgets(3));
2359-
expect(find.byTooltip(const DefaultMaterialLocalizations().showMenuTooltip), findsNWidgets(3));
2360+
expect(findByTooltip(const DefaultMaterialLocalizations().showMenuTooltip), findsNWidgets(3));
23602361
});
23612362

23622363
testWidgets('PopupMenuButton custom tooltip', (WidgetTester tester) async {
@@ -2404,7 +2405,7 @@ void main() {
24042405
);
24052406

24062407
expect(find.byType(Tooltip), findsNWidgets(3));
2407-
expect(find.byTooltip('Test tooltip'), findsNWidgets(3));
2408+
expect(findByTooltip('Test tooltip'), findsNWidgets(3));
24082409
});
24092410

24102411
testWidgets('Allow Widget for PopupMenuButton.icon', (WidgetTester tester) async {

0 commit comments

Comments
 (0)