Skip to content

Commit f3a4f72

Browse files
committed
[flutter_test] new WidgetTester API based on finder objects (#3288)
1 parent a5a34d9 commit f3a4f72

File tree

67 files changed

+1670
-1121
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+1670
-1121
lines changed

dev/manual_tests/test/card_collection_test.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,27 @@ void main() {
1515
tester.pump(); // see https://github.com/flutter/flutter/issues/1865
1616
tester.pump(); // triggers a frame
1717

18-
Element navigationMenu = tester.findElement((Element element) {
18+
Finder navigationMenu = find.byElement((Element element) {
1919
Widget widget = element.widget;
2020
if (widget is Tooltip)
2121
return widget.message == 'Open navigation menu';
2222
return false;
2323
});
2424

25-
expect(navigationMenu, isNotNull);
25+
expect(tester, hasWidget(navigationMenu));
2626

2727
tester.tap(navigationMenu);
2828
tester.pump(); // start opening menu
2929
tester.pump(const Duration(seconds: 1)); // wait til it's really opened
3030

3131
// smoke test for various checkboxes
32-
tester.tap(tester.findText('Make card labels editable'));
32+
tester.tap(find.text('Make card labels editable'));
3333
tester.pump();
34-
tester.tap(tester.findText('Let the sun shine'));
34+
tester.tap(find.text('Let the sun shine'));
3535
tester.pump();
36-
tester.tap(tester.findText('Make card labels editable'));
36+
tester.tap(find.text('Make card labels editable'));
3737
tester.pump();
38-
tester.tap(tester.findText('Vary font sizes'));
38+
tester.tap(find.text('Vary font sizes'));
3939
tester.pump();
4040
});
4141
});

examples/hello_world/test/hello_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ void main() {
1313
hello_world.main(); // builds the app and schedules a frame but doesn't trigger one
1414
tester.pump(); // triggers a frame
1515

16-
expect(tester.findText('Hello, world!'), isNotNull);
16+
expect(tester, hasWidget(find.text('Hello, world!')));
1717
});
1818
});
1919
}

examples/material_gallery/test/smoke_test.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,45 +16,45 @@ void main() {
1616
tester.pump(); // triggers a frame
1717

1818
// Try loading Weather demo
19-
tester.tap(tester.findText('Demos'));
19+
tester.tap(find.text('Demos'));
2020
tester.pump();
2121
tester.pump(const Duration(seconds: 1)); // wait til it's really opened
2222

23-
tester.tap(tester.findText('Weather'));
23+
tester.tap(find.text('Weather'));
2424
tester.pump();
2525
tester.pump(const Duration(seconds: 1)); // wait til it's really opened
2626

2727
// Go back
28-
Element backButton = tester.findElement((Element element) {
28+
Finder backButton = find.byElement((Element element) {
2929
Widget widget = element.widget;
3030
if (widget is Tooltip)
3131
return widget.message == 'Back';
3232
return false;
3333
});
34-
expect(backButton, isNotNull);
34+
expect(tester, hasWidget(backButton));
3535
tester.tap(backButton);
3636
tester.pump(); // start going back
3737
tester.pump(const Duration(seconds: 1)); // wait til it's finished
3838

3939
// Open menu
40-
Element navigationMenu = tester.findElement((Element element) {
40+
Finder navigationMenu = find.byElement((Element element) {
4141
Widget widget = element.widget;
4242
if (widget is Tooltip)
4343
return widget.message == 'Open navigation menu';
4444
return false;
4545
});
46-
expect(navigationMenu, isNotNull);
46+
expect(tester, hasWidget(navigationMenu));
4747
tester.tap(navigationMenu);
4848
tester.pump(); // start opening menu
4949
tester.pump(const Duration(seconds: 1)); // wait til it's really opened
5050

5151
// switch theme
52-
tester.tap(tester.findText('Dark'));
52+
tester.tap(find.text('Dark'));
5353
tester.pump();
5454
tester.pump(const Duration(seconds: 1)); // wait til it's changed
5555

5656
// switch theme
57-
tester.tap(tester.findText('Light'));
57+
tester.tap(find.text('Light'));
5858
tester.pump();
5959
tester.pump(const Duration(seconds: 1)); // wait til it's changed
6060
});

examples/stocks/test/icon_color_test.dart

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Element findElementOfExactWidgetTypeGoingUp(Element node, Type targetType) {
3838

3939
final RegExp materialIconAssetNameColorExtractor = new RegExp(r'[^/]+/ic_.+_(white|black)_[0-9]+dp\.png');
4040

41-
void checkIconColor(WidgetTester tester, String label, Color color) {
41+
void checkIconColor(ElementTreeTester tester, String label, Color color) {
4242
// The icon is going to be in the same merged semantics box as the text
4343
// regardless of how the menu item is represented, so this is a good
4444
// way to find the menu item. I hope.
@@ -59,11 +59,11 @@ void main() {
5959
tester.pump(); // triggers a frame
6060

6161
// sanity check
62-
expect(tester.findText('MARKET'), isNotNull);
63-
expect(tester.findText('Help & Feedback'), isNull);
62+
expect(tester, hasWidget(find.text('MARKET')));
63+
expect(tester, doesNotHaveWidget(find.text('Help & Feedback')));
6464
tester.pump(new Duration(seconds: 2));
65-
expect(tester.findText('MARKET'), isNotNull);
66-
expect(tester.findText('Help & Feedback'), isNull);
65+
expect(tester, hasWidget(find.text('MARKET')));
66+
expect(tester, doesNotHaveWidget(find.text('Help & Feedback')));
6767

6868
// drag the drawer out
6969
Point left = new Point(0.0, ui.window.size.height / 2.0);
@@ -74,24 +74,24 @@ void main() {
7474
tester.pump();
7575
gesture.up();
7676
tester.pump();
77-
expect(tester.findText('MARKET'), isNotNull);
78-
expect(tester.findText('Help & Feedback'), isNotNull);
77+
expect(tester, hasWidget(find.text('MARKET')));
78+
expect(tester, hasWidget(find.text('Help & Feedback')));
7979

8080
// check the colour of the icon - light mode
81-
checkIconColor(tester, 'Stock List', Colors.purple[500]); // theme primary color
82-
checkIconColor(tester, 'Account Balance', Colors.black45); // enabled
83-
checkIconColor(tester, 'Help & Feedback', Colors.black26); // disabled
81+
checkIconColor(tester.elementTreeTester, 'Stock List', Colors.purple[500]); // theme primary color
82+
checkIconColor(tester.elementTreeTester, 'Account Balance', Colors.black45); // enabled
83+
checkIconColor(tester.elementTreeTester, 'Help & Feedback', Colors.black26); // disabled
8484

8585
// switch to dark mode
86-
tester.tap(tester.findText('Pessimistic'));
86+
tester.tap(find.text('Pessimistic'));
8787
tester.pump(); // get the tap and send the notification that the theme has changed
8888
tester.pump(); // start the theme transition
8989
tester.pump(const Duration(seconds: 5)); // end the transition
9090

9191
// check the colour of the icon - dark mode
92-
checkIconColor(tester, 'Stock List', Colors.redAccent[200]); // theme accent color
93-
checkIconColor(tester, 'Account Balance', Colors.white); // enabled
94-
checkIconColor(tester, 'Help & Feedback', Colors.white30); // disabled
92+
checkIconColor(tester.elementTreeTester, 'Stock List', Colors.redAccent[200]); // theme accent color
93+
checkIconColor(tester.elementTreeTester, 'Account Balance', Colors.white); // enabled
94+
checkIconColor(tester.elementTreeTester, 'Help & Feedback', Colors.white30); // disabled
9595

9696
});
9797
});

examples/stocks/test/locale_test.dart

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
import 'package:flutter/material.dart';
65
import 'package:flutter_test/flutter_test.dart';
76
import 'package:stocks/main.dart' as stocks;
87
import 'package:stocks/stock_data.dart' as stock_data;
@@ -14,15 +13,13 @@ void main() {
1413
test("Test changing locale", () {
1514
testWidgets((WidgetTester tester) {
1615
stocks.main();
17-
tester.async.flushMicrotasks(); // see https://github.com/flutter/flutter/issues/1865
16+
tester.flushMicrotasks(); // see https://github.com/flutter/flutter/issues/1865
1817
tester.pump();
1918

20-
Element tab = tester.findText('MARKET');
21-
expect(tab, isNotNull);
19+
expect(tester, hasWidget(find.text('MARKET')));
2220
tester.setLocale("es", "US");
2321
tester.pump();
24-
Text text = tab.widget;
25-
expect(text.data, equals("MERCADO"));
22+
expect(tester, hasWidget(find.text('MERCADO')));
2623

2724
// TODO(abarth): We're leaking an animation. We should track down the leak
2825
// and plug it rather than waiting for the animation to end here.

packages/flutter/benchmark/stocks/build_bench.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ void main() {
2424
tester.tapAt(new Point(20.0, 20.0)); // Open drawer
2525
tester.pump(); // Start drawer animation
2626
tester.pump(const Duration(seconds: 1)); // Complete drawer animation
27-
appState = tester.findStateOfType(stocks.StocksAppState);
27+
appState = tester.stateOf(find.byType(stocks.StocksApp));
2828
});
2929

3030
WidgetFlutterBinding binding = WidgetFlutterBinding.instance;

packages/flutter/test/material/slider_test.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ void main() {
3434
);
3535

3636
expect(value, equals(0.0));
37-
tester.tap(tester.findElementByKey(sliderKey));
37+
tester.tap(find.byKey(sliderKey));
3838
expect(value, equals(0.5));
3939
tester.pump(); // No animation should start.
4040
expect(Scheduler.instance.transientCallbackCount, equals(0));
@@ -70,11 +70,11 @@ void main() {
7070
);
7171

7272
expect(value, equals(0.0));
73-
tester.tap(tester.findElementByKey(sliderKey));
73+
tester.tap(find.byKey(sliderKey));
7474
expect(value, equals(50.0));
75-
tester.scroll(tester.findElementByKey(sliderKey), const Offset(5.0, 0.0));
75+
tester.scroll(find.byKey(sliderKey), const Offset(5.0, 0.0));
7676
expect(value, equals(50.0));
77-
tester.scroll(tester.findElementByKey(sliderKey), const Offset(40.0, 0.0));
77+
tester.scroll(find.byKey(sliderKey), const Offset(40.0, 0.0));
7878
expect(value, equals(80.0));
7979

8080
tester.pump(); // Starts animation.

0 commit comments

Comments
 (0)