flutter/examples/flutter_gallery/test/smoke_test.dart

95 lines
3.9 KiB
Dart
Raw Normal View History

// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:collection' show LinkedHashSet;
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_gallery/gallery/item.dart' show GalleryItem, kAllGalleryItems;
2016-05-09 18:00:54 +00:00
import 'package:flutter_gallery/main.dart' as flutter_gallery_main;
const String kCaption = 'Flutter Gallery';
final List<String> demoCategories = new LinkedHashSet<String>.from(
kAllGalleryItems.map((GalleryItem item) => item.category)
).toList();
final List<String> routeNames =
kAllGalleryItems.map((GalleryItem item) => item.routeName).toList();
Finder findGalleryItemByRouteName(WidgetTester tester, String routeName) {
return find.byWidgetPredicate((Widget widget) {
return widget is GalleryItem && widget.routeName == routeName;
});
}
// Start a gallery demo and then go back. This function assumes that the
// we're starting on the home route and that the submenu that contains
// the item for a demo that pushes route 'routeName' is already open.
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);
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
2016-04-29 20:23:27 +00:00
expect(menuItem, findsOneWidget);
await tester.tap(menuItem);
await tester.pump(); // Launch the demo.
await tester.pump(const Duration(seconds: 1)); // Wait until the demo has opened.
expect(find.text(kCaption), findsNothing);
// Go back
Finder backButton = find.byTooltip('Back');
expect(backButton, findsOneWidget);
await tester.tap(backButton);
await tester.pump(); // Start the 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) async {
flutter_gallery_main
.main(); // builds the app and schedules a frame but doesn't trigger one
await tester.pump(); // see https://github.com/flutter/flutter/issues/1865
await tester.pump(); // triggers a frame
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
2016-04-29 20:23:27 +00:00
expect(find.text(kCaption), findsOneWidget);
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
2016-04-29 20:23:27 +00:00
final List<double> scrollDeltas = new List<double>();
double previousY = tester.getTopRight(find.text(demoCategories[0])).y;
for (String routeName in routeNames) {
final double y = tester.getTopRight(findGalleryItemByRouteName(tester, routeName)).y;
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
2016-04-29 20:23:27 +00:00
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];
await smokeDemo(tester, routeName);
2016-07-11 14:26:36 +00:00
await tester.scroll(findGalleryItemByRouteName(tester, routeName), new Offset(0.0, scrollDeltas[i]));
await tester.pump(); // start the scroll
await tester.pump(const Duration(milliseconds: 500)); // wait for overscroll to timeout, if necessary
await tester.pump(const Duration(seconds: 3)); // wait for overscroll to fade away, if necessary
tester.binding.debugAssertNoTransientCallbacks('A transient callback was still active after leaving route $routeName');
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
2016-04-29 20:23:27 +00:00
}
Finder navigationMenuButton = find.byTooltip('Open navigation menu');
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
2016-04-29 20:23:27 +00:00
expect(navigationMenuButton, findsOneWidget);
await tester.tap(navigationMenuButton);
await tester.pump(); // Start opening drawer.
await tester.pump(const Duration(seconds: 1)); // Wait until it's really opened.
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
2016-04-29 20:23:27 +00:00
// switch theme
await tester.tap(find.text('Dark'));
await tester.pump();
await tester.pump(const Duration(seconds: 1)); // Wait until it's changed.
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
2016-04-29 20:23:27 +00:00
// switch theme
await tester.tap(find.text('Light'));
await tester.pump();
await tester.pump(const Duration(seconds: 1)); // Wait until it's changed.
});
}