2016-04-13 20:53:39 +00:00
|
|
|
// 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.
|
|
|
|
|
2016-06-13 23:50:12 +00:00
|
|
|
import 'dart:collection' show LinkedHashSet;
|
|
|
|
|
2016-04-13 20:53:39 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2016-11-10 20:55:56 +00:00
|
|
|
import 'package:flutter/rendering.dart';
|
2016-04-13 20:53:39 +00:00
|
|
|
import 'package:flutter_test/flutter_test.dart';
|
2016-06-13 23:50:12 +00:00
|
|
|
import 'package:flutter_gallery/gallery/item.dart' show GalleryItem, kAllGalleryItems;
|
2016-11-10 20:55:56 +00:00
|
|
|
import 'package:flutter_gallery/gallery/app.dart' show GalleryApp;
|
2016-04-13 20:53:39 +00:00
|
|
|
|
2016-08-11 20:40:26 +00:00
|
|
|
const String kCaption = 'Flutter Gallery';
|
|
|
|
|
2016-06-13 23:50:12 +00:00
|
|
|
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();
|
2016-04-21 17:24:22 +00:00
|
|
|
|
|
|
|
Finder findGalleryItemByRouteName(WidgetTester tester, String routeName) {
|
2016-04-21 23:35:46 +00:00
|
|
|
return find.byWidgetPredicate((Widget widget) {
|
2016-06-13 23:50:12 +00:00
|
|
|
return widget is GalleryItem && widget.routeName == routeName;
|
2016-04-21 17:24:22 +00:00
|
|
|
});
|
|
|
|
}
|
2016-04-21 15:54:43 +00:00
|
|
|
|
|
|
|
// Start a gallery demo and then go back. This function assumes that the
|
2016-06-13 23:50:12 +00:00
|
|
|
// 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.
|
2016-05-16 19:53:13 +00:00
|
|
|
Future<Null> smokeDemo(WidgetTester tester, String routeName) async {
|
2016-04-21 15:54:43 +00:00
|
|
|
// Ensure that we're (likely to be) on the home page
|
2016-04-21 17:24:22 +00:00
|
|
|
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);
|
2016-04-21 15:54:43 +00:00
|
|
|
|
2016-05-16 19:53:13 +00:00
|
|
|
await tester.tap(menuItem);
|
|
|
|
await tester.pump(); // Launch the demo.
|
2016-08-11 20:40:26 +00:00
|
|
|
await tester.pump(const Duration(seconds: 1)); // Wait until the demo has opened.
|
|
|
|
|
|
|
|
expect(find.text(kCaption), findsNothing);
|
2016-04-21 15:54:43 +00:00
|
|
|
|
|
|
|
// Go back
|
2016-10-03 19:52:06 +00:00
|
|
|
Finder backButton = find.byTooltip('Back');
|
2016-09-09 20:19:03 +00:00
|
|
|
expect(backButton, findsOneWidget);
|
|
|
|
await tester.tap(backButton);
|
|
|
|
await tester.pump(); // Start the pop "back" operation.
|
2016-05-16 19:53:13 +00:00
|
|
|
await tester.pump(const Duration(seconds: 1)); // Wait until it has finished.
|
|
|
|
return null;
|
2016-04-21 15:54:43 +00:00
|
|
|
}
|
|
|
|
|
2016-11-10 20:55:56 +00:00
|
|
|
Future<Null> runSmokeTest(WidgetTester tester) async {
|
2016-11-30 01:47:39 +00:00
|
|
|
bool hasFeedback = false;
|
|
|
|
void mockOnSendFeedback() {
|
|
|
|
hasFeedback = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
await tester.pumpWidget(new GalleryApp(onSendFeedback: mockOnSendFeedback));
|
2016-11-10 20:55:56 +00:00
|
|
|
await tester.pump(); // see https://github.com/flutter/flutter/issues/1865
|
|
|
|
await tester.pump(); // triggers a frame
|
|
|
|
|
|
|
|
expect(find.text(kCaption), findsOneWidget);
|
|
|
|
|
|
|
|
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;
|
|
|
|
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);
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
|
|
|
Finder navigationMenuButton = find.byTooltip('Open navigation menu');
|
|
|
|
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.
|
|
|
|
|
|
|
|
// switch theme
|
|
|
|
await tester.tap(find.text('Dark'));
|
|
|
|
await tester.pump();
|
|
|
|
await tester.pump(const Duration(seconds: 1)); // Wait until it's changed.
|
|
|
|
|
|
|
|
// switch theme
|
|
|
|
await tester.tap(find.text('Light'));
|
|
|
|
await tester.pump();
|
|
|
|
await tester.pump(const Duration(seconds: 1)); // Wait until it's changed.
|
2016-11-30 01:47:39 +00:00
|
|
|
|
|
|
|
// send feedback
|
|
|
|
expect(hasFeedback, false);
|
|
|
|
await tester.tap(find.text('Send feedback'));
|
|
|
|
await tester.pump();
|
|
|
|
expect(hasFeedback, true);
|
2016-11-10 20:55:56 +00:00
|
|
|
}
|
|
|
|
|
2016-04-13 20:53:39 +00:00
|
|
|
void main() {
|
2016-11-10 20:55:56 +00:00
|
|
|
testWidgets('Flutter Gallery app smoke test', runSmokeTest);
|
|
|
|
|
2016-05-16 19:53:13 +00:00
|
|
|
testWidgets('Flutter Gallery app smoke test', (WidgetTester tester) async {
|
2016-11-10 20:55:56 +00:00
|
|
|
RendererBinding.instance.setSemanticsEnabled(true);
|
|
|
|
await runSmokeTest(tester);
|
|
|
|
RendererBinding.instance.setSemanticsEnabled(false);
|
2016-06-01 22:34:48 +00:00
|
|
|
});
|
2016-04-13 20:53:39 +00:00
|
|
|
}
|