Refactor the test framework (#3622)
* Refactor widget test framework
Instead of:
```dart
test("Card Collection smoke test", () {
testWidgets((WidgetTester tester) {
```
...you now say:
```dart
testWidgets("Card Collection smoke test", (WidgetTester tester) {
```
Instead of:
```dart
expect(tester, hasWidget(find.text('hello')));
```
...you now say:
```dart
expect(find.text('hello'), findsOneWidget);
```
Instead of the previous API (exists, widgets, widget, stateOf,
elementOf, etc), you now have the following comprehensive API. All these
are functions that take a Finder, except the all* properties.
* `any()` - true if anything matches, c.f. `Iterable.any`
* `allWidgets` - all the widgets in the tree
* `widget()` - the one and only widget that matches the finder
* `firstWidget()` - the first widget that matches the finder
* `allElements` - all the elements in the tree
* `element()` - the one and only element that matches the finder
* `firstElement()` - the first element that matches the finder
* `allStates` - all the `State`s in the tree
* `state()` - the one and only state that matches the finder
* `firstState()` - the first state that matches the finder
* `allRenderObjects` - all the render objects in the tree
* `renderObject()` - the one and only render object that matches the finder
* `firstRenderObject()` - the first render object that matches the finder
There's also `layers' which returns the list of current layers.
`tap`, `fling`, getCenter, getSize, etc, take Finders, like the APIs
above, and expect there to only be one matching widget.
The finders are:
* `find.text(String text)`
* `find.widgetWithText(Type widgetType, String text)`
* `find.byKey(Key key)`
* `find.byType(Type type)`
* `find.byElementType(Type type)`
* `find.byConfig(Widget config)`
* `find.byWidgetPredicate(WidgetPredicate predicate)`
* `find.byElementPredicate(ElementPredicate predicate)`
The matchers (for `expect`) are:
* `findsNothing`
* `findsWidgets`
* `findsOneWidget`
* `findsNWidgets(n)`
* `isOnStage`
* `isOffStage`
* `isInCard`
* `isNotInCard`
Benchmarks now use benchmarkWidgets instead of testWidgets.
Also, for those of you using mockers, `serviceMocker` now automatically
handles the binding initialization.
This patch also:
* changes how tests are run so that we can more easily swap the logic
out for a "real" mode instead of FakeAsync.
* introduces CachingIterable.
* changes how flutter_driver interacts with the widget tree to use the
aforementioned new API rather than ElementTreeTester, which is gone.
* removes ElementTreeTester.
* changes the semantics of a test for scrollables because we couldn't
convince ourselves that the old semantics made sense; it only worked
before because flushing the microtasks after every event was broken.
* fixes the flushing of microtasks after every event.
* Reindent the tests
* Fix review comments
This commit is contained in:
@@ -8,12 +8,10 @@ import 'package:test/test.dart';
|
||||
import '../lib/main.dart' as hello_world;
|
||||
|
||||
void main() {
|
||||
test("Hello world smoke test", () {
|
||||
testWidgets((WidgetTester tester) {
|
||||
hello_world.main(); // builds the app and schedules a frame but doesn't trigger one
|
||||
tester.pump(); // triggers a frame
|
||||
testWidgets("Hello world smoke test", (WidgetTester tester) {
|
||||
hello_world.main(); // builds the app and schedules a frame but doesn't trigger one
|
||||
tester.pump(); // triggers a frame
|
||||
|
||||
expect(tester, hasWidget(find.text('Hello, world!')));
|
||||
});
|
||||
expect(find.text('Hello, world!'), findsOneWidget);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ Finder findBackButton(WidgetTester tester) => byTooltip(tester, 'Back');
|
||||
void smokeDemo(WidgetTester tester, String routeName) {
|
||||
// Ensure that we're (likely to be) on the home page
|
||||
final Finder menuItem = findGalleryItemByRouteName(tester, routeName);
|
||||
expect(tester, hasWidget(menuItem));
|
||||
expect(menuItem, findsOneWidget);
|
||||
|
||||
tester.tap(menuItem);
|
||||
tester.pump(); // Launch the demo.
|
||||
@@ -43,58 +43,56 @@ void smokeDemo(WidgetTester tester, String routeName) {
|
||||
|
||||
// Go back
|
||||
Finder backButton = findBackButton(tester);
|
||||
expect(tester, hasWidget(backButton));
|
||||
expect(backButton, findsOneWidget);
|
||||
tester.tap(backButton);
|
||||
tester.pump(); // Start the navigator pop "back" operation.
|
||||
tester.pump(const Duration(seconds: 1)); // Wait until it has finished.
|
||||
}
|
||||
|
||||
void main() {
|
||||
test('Material Gallery app smoke test', () {
|
||||
testWidgets((WidgetTester tester) {
|
||||
material_gallery_main.main(); // builds the app and schedules a frame but doesn't trigger one
|
||||
tester.pump(); // see https://github.com/flutter/flutter/issues/1865
|
||||
tester.pump(); // triggers a frame
|
||||
testWidgets('Material Gallery app smoke test', (WidgetTester tester) {
|
||||
material_gallery_main.main(); // builds the app and schedules a frame but doesn't trigger one
|
||||
tester.pump(); // see https://github.com/flutter/flutter/issues/1865
|
||||
tester.pump(); // triggers a frame
|
||||
|
||||
// Expand the demo category submenus.
|
||||
for (String category in demoCategories.reversed) {
|
||||
tester.tap(find.text(category));
|
||||
tester.pump();
|
||||
tester.pump(const Duration(seconds: 1)); // Wait until the menu has expanded.
|
||||
}
|
||||
|
||||
final List<double> scrollDeltas = new List<double>();
|
||||
double previousY = tester.getTopRight(find.text(demoCategories[0])).y;
|
||||
final List<String> routeNames = material_gallery_app.kRoutes.keys.toList();
|
||||
for (String routeName in routeNames) {
|
||||
final double y = tester.getTopRight(findGalleryItemByRouteName(tester, routeName)).y;
|
||||
scrollDeltas.add(previousY - y);
|
||||
previousY = y;
|
||||
}
|
||||
|
||||
// Launch each demo and then scroll that item out of the way.
|
||||
for (int i = 0; i < routeNames.length; i += 1) {
|
||||
final String routeName = routeNames[i];
|
||||
smokeDemo(tester, routeName);
|
||||
tester.scroll(findGalleryItemByRouteName(tester, routeName), new Offset(0.0, scrollDeltas[i]));
|
||||
tester.pump();
|
||||
}
|
||||
|
||||
Finder navigationMenuButton = findNavigationMenuButton(tester);
|
||||
expect(tester, hasWidget(navigationMenuButton));
|
||||
tester.tap(navigationMenuButton);
|
||||
tester.pump(); // Start opening drawer.
|
||||
tester.pump(const Duration(seconds: 1)); // Wait until it's really opened.
|
||||
|
||||
// switch theme
|
||||
tester.tap(find.text('Dark'));
|
||||
// Expand the demo category submenus.
|
||||
for (String category in demoCategories.reversed) {
|
||||
tester.tap(find.text(category));
|
||||
tester.pump();
|
||||
tester.pump(const Duration(seconds: 1)); // Wait until it's changed.
|
||||
tester.pump(const Duration(seconds: 1)); // Wait until the menu has expanded.
|
||||
}
|
||||
|
||||
// switch theme
|
||||
tester.tap(find.text('Light'));
|
||||
final List<double> scrollDeltas = new List<double>();
|
||||
double previousY = tester.getTopRight(find.text(demoCategories[0])).y;
|
||||
final List<String> routeNames = material_gallery_app.kRoutes.keys.toList();
|
||||
for (String routeName in routeNames) {
|
||||
final double y = tester.getTopRight(findGalleryItemByRouteName(tester, routeName)).y;
|
||||
scrollDeltas.add(previousY - y);
|
||||
previousY = y;
|
||||
}
|
||||
|
||||
// Launch each demo and then scroll that item out of the way.
|
||||
for (int i = 0; i < routeNames.length; i += 1) {
|
||||
final String routeName = routeNames[i];
|
||||
smokeDemo(tester, routeName);
|
||||
tester.scroll(findGalleryItemByRouteName(tester, routeName), new Offset(0.0, scrollDeltas[i]));
|
||||
tester.pump();
|
||||
tester.pump(const Duration(seconds: 1)); // Wait until it's changed.
|
||||
});
|
||||
}
|
||||
|
||||
Finder navigationMenuButton = findNavigationMenuButton(tester);
|
||||
expect(navigationMenuButton, findsOneWidget);
|
||||
tester.tap(navigationMenuButton);
|
||||
tester.pump(); // Start opening drawer.
|
||||
tester.pump(const Duration(seconds: 1)); // Wait until it's really opened.
|
||||
|
||||
// switch theme
|
||||
tester.tap(find.text('Dark'));
|
||||
tester.pump();
|
||||
tester.pump(const Duration(seconds: 1)); // Wait until it's changed.
|
||||
|
||||
// switch theme
|
||||
tester.tap(find.text('Light'));
|
||||
tester.pump();
|
||||
tester.pump(const Duration(seconds: 1)); // Wait until it's changed.
|
||||
});
|
||||
}
|
||||
|
||||
@@ -38,11 +38,11 @@ Element findElementOfExactWidgetTypeGoingUp(Element node, Type targetType) {
|
||||
|
||||
final RegExp materialIconAssetNameColorExtractor = new RegExp(r'[^/]+/ic_.+_(white|black)_[0-9]+dp\.png');
|
||||
|
||||
void checkIconColor(ElementTreeTester tester, String label, Color color) {
|
||||
void checkIconColor(WidgetTester tester, String label, Color color) {
|
||||
// The icon is going to be in the same merged semantics box as the text
|
||||
// regardless of how the menu item is represented, so this is a good
|
||||
// way to find the menu item. I hope.
|
||||
Element semantics = findElementOfExactWidgetTypeGoingUp(tester.findText(label), MergeSemantics);
|
||||
Element semantics = findElementOfExactWidgetTypeGoingUp(tester.element(find.text(label)), MergeSemantics);
|
||||
expect(semantics, isNotNull);
|
||||
Element asset = findElementOfExactWidgetTypeGoingDown(semantics, Text);
|
||||
Text text = asset.widget;
|
||||
@@ -52,47 +52,44 @@ void checkIconColor(ElementTreeTester tester, String label, Color color) {
|
||||
void main() {
|
||||
stock_data.StockDataFetcher.actuallyFetchData = false;
|
||||
|
||||
test("Test icon colors", () {
|
||||
testWidgets((WidgetTester tester) {
|
||||
stocks.main(); // builds the app and schedules a frame but doesn't trigger one
|
||||
tester.pump(); // see https://github.com/flutter/flutter/issues/1865
|
||||
tester.pump(); // triggers a frame
|
||||
testWidgets("Test icon colors", (WidgetTester tester) {
|
||||
stocks.main(); // builds the app and schedules a frame but doesn't trigger one
|
||||
tester.pump(); // see https://github.com/flutter/flutter/issues/1865
|
||||
tester.pump(); // triggers a frame
|
||||
|
||||
// sanity check
|
||||
expect(tester, hasWidget(find.text('MARKET')));
|
||||
expect(tester, doesNotHaveWidget(find.text('Help & Feedback')));
|
||||
tester.pump(new Duration(seconds: 2));
|
||||
expect(tester, hasWidget(find.text('MARKET')));
|
||||
expect(tester, doesNotHaveWidget(find.text('Help & Feedback')));
|
||||
// sanity check
|
||||
expect(find.text('MARKET'), findsOneWidget);
|
||||
expect(find.text('Help & Feedback'), findsNothing);
|
||||
tester.pump(new Duration(seconds: 2));
|
||||
expect(find.text('MARKET'), findsOneWidget);
|
||||
expect(find.text('Help & Feedback'), findsNothing);
|
||||
|
||||
// drag the drawer out
|
||||
Point left = new Point(0.0, ui.window.size.height / 2.0);
|
||||
Point right = new Point(ui.window.size.width, left.y);
|
||||
TestGesture gesture = tester.startGesture(left);
|
||||
tester.pump();
|
||||
gesture.moveTo(right);
|
||||
tester.pump();
|
||||
gesture.up();
|
||||
tester.pump();
|
||||
expect(tester, hasWidget(find.text('MARKET')));
|
||||
expect(tester, hasWidget(find.text('Help & Feedback')));
|
||||
// drag the drawer out
|
||||
Point left = new Point(0.0, ui.window.size.height / 2.0);
|
||||
Point right = new Point(ui.window.size.width, left.y);
|
||||
TestGesture gesture = tester.startGesture(left);
|
||||
tester.pump();
|
||||
gesture.moveTo(right);
|
||||
tester.pump();
|
||||
gesture.up();
|
||||
tester.pump();
|
||||
expect(find.text('MARKET'), findsOneWidget);
|
||||
expect(find.text('Help & Feedback'), findsOneWidget);
|
||||
|
||||
// check the colour of the icon - light mode
|
||||
checkIconColor(tester.elementTreeTester, 'Stock List', Colors.purple[500]); // theme primary color
|
||||
checkIconColor(tester.elementTreeTester, 'Account Balance', Colors.black45); // enabled
|
||||
checkIconColor(tester.elementTreeTester, 'Help & Feedback', Colors.black26); // disabled
|
||||
// check the colour of the icon - light mode
|
||||
checkIconColor(tester, 'Stock List', Colors.purple[500]); // theme primary color
|
||||
checkIconColor(tester, 'Account Balance', Colors.black45); // enabled
|
||||
checkIconColor(tester, 'Help & Feedback', Colors.black26); // disabled
|
||||
|
||||
// switch to dark mode
|
||||
tester.tap(find.text('Pessimistic'));
|
||||
tester.pump(); // get the tap and send the notification that the theme has changed
|
||||
tester.pump(); // start the theme transition
|
||||
tester.pump(const Duration(seconds: 5)); // end the transition
|
||||
// switch to dark mode
|
||||
tester.tap(find.text('Pessimistic'));
|
||||
tester.pump(); // get the tap and send the notification that the theme has changed
|
||||
tester.pump(); // start the theme transition
|
||||
tester.pump(const Duration(seconds: 5)); // end the transition
|
||||
|
||||
// check the colour of the icon - dark mode
|
||||
checkIconColor(tester.elementTreeTester, 'Stock List', Colors.redAccent[200]); // theme accent color
|
||||
checkIconColor(tester.elementTreeTester, 'Account Balance', Colors.white); // enabled
|
||||
checkIconColor(tester.elementTreeTester, 'Help & Feedback', Colors.white30); // disabled
|
||||
|
||||
});
|
||||
// check the colour of the icon - dark mode
|
||||
checkIconColor(tester, 'Stock List', Colors.redAccent[200]); // theme accent color
|
||||
checkIconColor(tester, 'Account Balance', Colors.white); // enabled
|
||||
checkIconColor(tester, 'Help & Feedback', Colors.white30); // disabled
|
||||
});
|
||||
}
|
||||
|
||||
@@ -10,16 +10,13 @@ import 'package:test/test.dart';
|
||||
void main() {
|
||||
stock_data.StockDataFetcher.actuallyFetchData = false;
|
||||
|
||||
test("Test changing locale", () {
|
||||
testWidgets((WidgetTester tester) {
|
||||
stocks.main();
|
||||
tester.flushMicrotasks(); // see https://github.com/flutter/flutter/issues/1865
|
||||
tester.pump();
|
||||
|
||||
expect(tester, hasWidget(find.text('MARKET')));
|
||||
tester.setLocale("es", "US");
|
||||
tester.pump();
|
||||
expect(tester, hasWidget(find.text('MERCADO')));
|
||||
});
|
||||
testWidgets("Test changing locale", (WidgetTester tester) {
|
||||
stocks.main();
|
||||
tester.flushMicrotasks(); // see https://github.com/flutter/flutter/issues/1865
|
||||
tester.pump();
|
||||
expect(find.text('MARKET'), findsOneWidget);
|
||||
tester.binding.setLocale("es", "US");
|
||||
tester.pump();
|
||||
expect(find.text('MERCADO'), findsOneWidget);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user