Make it possible to run tests live on a device (#3936)

This makes it possible to substitute 'flutter run' for 'flutter test'
and actually watch a test run on a device.

For any test that depends on flutter_test:

1. Remove any import of 'package:test/test.dart'.

2. Replace `testWidgets('...', (WidgetTester tester) {`
      with `testWidgets('...', (WidgetTester tester) async {`

3. Add an "await" in front of calls to any of the following:
    * tap()
    * tapAt()
    * fling()
    * flingFrom()
    * scroll()
    * scrollAt()
    * pump()
    * pumpWidget()

4. Replace any calls to `tester.flushMicrotasks()` with calls to
   `await tester.idle()`.

There's a guarding API that you can use, if you have particularly
complicated tests, to get better error messages. Search for
TestAsyncUtils.
This commit is contained in:
Ian Hickson
2016-05-16 12:53:13 -07:00
parent 5fa6c0e9b0
commit 3252701753
134 changed files with 3254 additions and 1987 deletions

View File

@@ -41,7 +41,7 @@ class _WeatherDemoState extends State<WeatherDemo> {
'packages/flutter_gallery_assets/icon-snow.png'
]);
String json = await DefaultAssetBundle.of(context).loadString('packages/flutter_gallery_assets/weathersprites.json');
String json = await bundle.loadString('packages/flutter_gallery_assets/weathersprites.json');
_sprites = new SpriteSheet(_images['packages/flutter_gallery_assets/weathersprites.png'], json);
}

View File

@@ -7,7 +7,6 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_gallery/gallery/app.dart' as flutter_gallery_app;
import 'package:flutter_gallery/gallery/item.dart' as flutter_gallery_item;
import 'package:flutter_gallery/main.dart' as flutter_gallery_main;
import 'package:test/test.dart';
// Warning: the following strings must be kept in sync with GalleryHome.
const List<String> demoCategories = const <String>['Demos', 'Components', 'Style'];
@@ -32,34 +31,39 @@ Finder findBackButton(WidgetTester tester) => byTooltip(tester, 'Back');
// Start a gallery demo and then go back. This function assumes that the
// we're starting on home route and that the submenu that contains the demo
// called 'name' is already open.
void smokeDemo(WidgetTester tester, String routeName) {
Future<Null> smokeDemo(WidgetTester tester, String routeName) async {
// Ensure that we're (likely to be) on the home page
final Finder menuItem = findGalleryItemByRouteName(tester, routeName);
expect(menuItem, findsOneWidget);
tester.tap(menuItem);
tester.pump(); // Launch the demo.
tester.pump(const Duration(seconds: 1)); // Wait until the demo has opened.
await tester.tap(menuItem);
await tester.pump(); // Launch the demo.
await tester.pump(const Duration(seconds: 1)); // Wait until the demo has opened.
// Go back
Finder backButton = findBackButton(tester);
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.
await tester.tap(backButton);
await tester.pump(); // Start the navigator pop "back" operation.
await tester.pump(const Duration(seconds: 1)); // Wait until it has finished.
return null;
}
void main() {
testWidgets('Flutter gallery app smoke test', (WidgetTester tester) {
TestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.ensureInitialized();
if (binding is LiveTestWidgetsFlutterBinding)
binding.allowAllFrames = true;
testWidgets('Flutter Gallery app smoke test', (WidgetTester tester) async {
flutter_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
await tester.pump(); // see https://github.com/flutter/flutter/issues/1865
await 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.
await tester.tap(find.text(category));
await tester.pump();
await tester.pump(const Duration(seconds: 1)); // Wait until the menu has expanded.
}
final List<double> scrollDeltas = new List<double>();
@@ -74,25 +78,25 @@ void main() {
// 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();
await smokeDemo(tester, routeName);
await tester.scroll(findGalleryItemByRouteName(tester, routeName), new Offset(0.0, scrollDeltas[i]));
await tester.pump();
}
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.
await tester.tap(navigationMenuButton);
await tester.pump(); // Start opening drawer.
await 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.
await tester.tap(find.text('Dark'));
await tester.pump();
await 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.
await tester.tap(find.text('Light'));
await tester.pump();
await tester.pump(const Duration(seconds: 1)); // Wait until it's changed.
}, skip: true);
}

View File

@@ -3,14 +3,13 @@
// found in the LICENSE file.
import 'package:flutter_test/flutter_test.dart';
import 'package:test/test.dart';
import '../lib/main.dart' as hello_world;
void main() {
testWidgets("Hello world smoke test", (WidgetTester tester) {
testWidgets("Hello world smoke test", (WidgetTester tester) async {
hello_world.main(); // builds the app and schedules a frame but doesn't trigger one
tester.pump(); // triggers a frame
await tester.pump(); // triggers a frame
expect(find.text('Hello, world!'), findsOneWidget);
});

View File

@@ -9,7 +9,6 @@ import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:stocks/main.dart' as stocks;
import 'package:stocks/stock_data.dart' as stock_data;
import 'package:test/test.dart';
Element findElementOfExactWidgetTypeGoingDown(Element node, Type targetType) {
void walker(Element child) {
@@ -52,27 +51,27 @@ void checkIconColor(WidgetTester tester, String label, Color color) {
void main() {
stock_data.StockDataFetcher.actuallyFetchData = false;
testWidgets("Test icon colors", (WidgetTester tester) {
testWidgets("Test icon colors", (WidgetTester tester) async {
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
await tester.pump(); // see https://github.com/flutter/flutter/issues/1865
await tester.pump(); // triggers a frame
// sanity check
expect(find.text('MARKET'), findsOneWidget);
expect(find.text('Help & Feedback'), findsNothing);
tester.pump(new Duration(seconds: 2));
await 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();
TestGesture gesture = await tester.startGesture(left);
await tester.pump();
await gesture.moveTo(right);
await tester.pump();
await gesture.up();
await tester.pump();
expect(find.text('MARKET'), findsOneWidget);
expect(find.text('Help & Feedback'), findsOneWidget);
@@ -82,10 +81,10 @@ void main() {
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
await tester.tap(find.text('Pessimistic'));
await tester.pump(); // get the tap and send the notification that the theme has changed
await tester.pump(); // start the theme transition
await tester.pump(const Duration(seconds: 5)); // end the transition
// check the colour of the icon - dark mode
checkIconColor(tester, 'Stock List', Colors.redAccent[200]); // theme accent color

View File

@@ -5,18 +5,18 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:stocks/main.dart' as stocks;
import 'package:stocks/stock_data.dart' as stock_data;
import 'package:test/test.dart';
void main() {
stock_data.StockDataFetcher.actuallyFetchData = false;
testWidgets("Test changing locale", (WidgetTester tester) {
testWidgets("Test changing locale", (WidgetTester tester) async {
stocks.main();
tester.flushMicrotasks(); // see https://github.com/flutter/flutter/issues/1865
tester.pump();
await tester.idle(); // see https://github.com/flutter/flutter/issues/1865
await tester.pump();
expect(find.text('MARKET'), findsOneWidget);
tester.binding.setLocale("es", "US");
tester.pump();
await tester.binding.setLocale("es", "US");
await tester.idle();
await tester.pump();
expect(find.text('MERCADO'), findsOneWidget);
});
}