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;
|
2017-02-17 20:56:25 +00:00
|
|
|
import 'dart:math' as math;
|
2016-06-13 23:50:12 +00:00
|
|
|
|
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(
|
2017-03-28 21:53:03 +00:00
|
|
|
kAllGalleryItems.map<String>((GalleryItem item) => item.category)
|
2016-06-13 23:50:12 +00:00
|
|
|
).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
|
|
|
|
2017-02-17 20:56:25 +00:00
|
|
|
int errors = 0;
|
|
|
|
|
|
|
|
void reportToStringError(String name, String route, int lineNumber, List<String> lines, String message) {
|
|
|
|
// If you're on line 12, then it has index 11.
|
|
|
|
// If you want 1 line before and 1 line after, then you want lines with index 10, 11, and 12.
|
|
|
|
// That's (lineNumber-1)-margin .. (lineNumber-1)+margin, or lineNumber-(margin+1) .. lineNumber+(margin-1)
|
2017-03-04 01:51:21 +00:00
|
|
|
final int margin = 5;
|
|
|
|
final int firstLine = math.max(0, lineNumber - margin);
|
|
|
|
final int lastLine = math.min(lines.length, lineNumber + margin);
|
2017-02-17 20:56:25 +00:00
|
|
|
print('$name : $route : line $lineNumber of ${lines.length} : $message; nearby lines were:\n ${lines.sublist(firstLine, lastLine).join("\n ")}');
|
|
|
|
errors += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void verifyToStringOutput(String name, String route, String testString) {
|
|
|
|
int lineNumber = 0;
|
2017-03-04 01:51:21 +00:00
|
|
|
final List<String> lines = testString.split('\n');
|
2017-02-17 20:56:25 +00:00
|
|
|
if (!testString.endsWith('\n'))
|
|
|
|
reportToStringError(name, route, lines.length, lines, 'does not end with a line feed');
|
|
|
|
for (String line in lines) {
|
|
|
|
lineNumber += 1;
|
|
|
|
if (line == '' && lineNumber != lines.length) {
|
|
|
|
reportToStringError(name, route, lineNumber, lines, 'found empty line');
|
|
|
|
} else if (line.contains('Instance of ')) {
|
|
|
|
reportToStringError(name, route, lineNumber, lines, 'found a class that does not have its own toString');
|
|
|
|
} else if (line.endsWith(' ')) {
|
|
|
|
reportToStringError(name, route, lineNumber, lines, 'found a line with trailing whitespace');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2017-02-17 20:56:25 +00:00
|
|
|
// Don't use pumpUntilNoTransientCallbacks in this function, because some of
|
|
|
|
// the smoketests have infinitely-running animations (e.g. the progress
|
|
|
|
// indicators demo).
|
|
|
|
|
2016-05-16 19:53:13 +00:00
|
|
|
await tester.tap(menuItem);
|
|
|
|
await tester.pump(); // Launch the demo.
|
2017-02-17 20:56:25 +00:00
|
|
|
await tester.pump(const Duration(milliseconds: 400)); // Wait until the demo has opened.
|
2016-08-11 20:40:26 +00:00
|
|
|
expect(find.text(kCaption), findsNothing);
|
2017-02-17 20:56:25 +00:00
|
|
|
|
|
|
|
// Leave the demo on the screen briefly for manual testing.
|
|
|
|
await tester.pump(const Duration(milliseconds: 400));
|
|
|
|
|
|
|
|
// Scroll the demo around a bit.
|
2017-04-12 22:06:12 +00:00
|
|
|
await tester.flingFrom(const Offset(400.0, 300.0), const Offset(-100.0, 0.0), 500.0);
|
|
|
|
await tester.flingFrom(const Offset(400.0, 300.0), const Offset(0.0, -100.0), 500.0);
|
2017-02-17 20:56:25 +00:00
|
|
|
await tester.pump();
|
|
|
|
await tester.pump(const Duration(milliseconds: 50));
|
|
|
|
await tester.pump(const Duration(milliseconds: 200));
|
|
|
|
await tester.pump(const Duration(milliseconds: 400));
|
|
|
|
|
|
|
|
// Verify that the dumps are pretty.
|
|
|
|
verifyToStringOutput('debugDumpApp', routeName, WidgetsBinding.instance.renderViewElement.toStringDeep());
|
|
|
|
verifyToStringOutput('debugDumpRenderTree', routeName, RendererBinding.instance?.renderView?.toStringDeep());
|
|
|
|
verifyToStringOutput('debugDumpLayerTree', routeName, RendererBinding.instance?.renderView?.debugLayer?.toStringDeep());
|
|
|
|
|
|
|
|
// Scroll the demo around a bit more.
|
2017-04-12 22:06:12 +00:00
|
|
|
await tester.flingFrom(const Offset(400.0, 300.0), const Offset(-200.0, 0.0), 500.0);
|
2017-02-17 20:56:25 +00:00
|
|
|
await tester.pump();
|
|
|
|
await tester.pump(const Duration(milliseconds: 50));
|
|
|
|
await tester.pump(const Duration(milliseconds: 200));
|
|
|
|
await tester.pump(const Duration(milliseconds: 400));
|
2017-04-12 22:06:12 +00:00
|
|
|
await tester.flingFrom(const Offset(400.0, 300.0), const Offset(100.0, 0.0), 500.0);
|
2017-02-17 20:56:25 +00:00
|
|
|
await tester.pump();
|
|
|
|
await tester.pump(const Duration(milliseconds: 400));
|
2017-04-12 22:06:12 +00:00
|
|
|
await tester.flingFrom(const Offset(400.0, 300.0), const Offset(0.0, 400.0), 1000.0);
|
2017-02-17 20:56:25 +00:00
|
|
|
await tester.pump();
|
|
|
|
await tester.pump(const Duration(milliseconds: 400));
|
2016-04-21 15:54:43 +00:00
|
|
|
|
|
|
|
// Go back
|
2017-03-04 01:51:21 +00:00
|
|
|
final 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.
|
2017-01-18 19:04:18 +00:00
|
|
|
await tester.pump(); // Complete the willPop() Future.
|
2017-02-17 20:56:25 +00:00
|
|
|
await tester.pump(const Duration(milliseconds: 400)); // Wait until it has finished.
|
2016-05-16 19:53:13 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
for (String routeName in routeNames) {
|
2017-03-04 01:51:21 +00:00
|
|
|
final Finder finder = findGalleryItemByRouteName(tester, routeName);
|
2017-02-17 22:06:15 +00:00
|
|
|
Scrollable.ensureVisible(tester.element(finder), alignment: 0.5);
|
2017-03-13 23:27:15 +00:00
|
|
|
await tester.pumpAndSettle();
|
2016-11-10 20:55:56 +00:00
|
|
|
await smokeDemo(tester, routeName);
|
|
|
|
tester.binding.debugAssertNoTransientCallbacks('A transient callback was still active after leaving route $routeName');
|
|
|
|
}
|
2017-02-17 20:56:25 +00:00
|
|
|
expect(errors, 0);
|
|
|
|
|
2017-03-04 01:51:21 +00:00
|
|
|
final Finder navigationMenuButton = find.byTooltip('Open navigation menu');
|
2016-11-10 20:55:56 +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.
|
|
|
|
|
2017-10-14 04:44:24 +00:00
|
|
|
// Switch theme.
|
2016-11-10 20:55:56 +00:00
|
|
|
await tester.tap(find.text('Dark'));
|
|
|
|
await tester.pump();
|
|
|
|
await tester.pump(const Duration(seconds: 1)); // Wait until it's changed.
|
|
|
|
|
2017-10-14 04:44:24 +00:00
|
|
|
// Switch theme.
|
2016-11-10 20:55:56 +00:00
|
|
|
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
|
|
|
|
2017-10-14 04:44:24 +00:00
|
|
|
// Switch font scale.
|
|
|
|
await tester.tap(find.text('Small'));
|
|
|
|
await tester.pump();
|
|
|
|
await tester.pump(const Duration(seconds: 1)); // Wait until it's changed.
|
|
|
|
// Switch font scale back to default.
|
|
|
|
await tester.tap(find.text('System Default'));
|
|
|
|
await tester.pump();
|
|
|
|
await tester.pump(const Duration(seconds: 1)); // Wait until it's changed.
|
|
|
|
|
|
|
|
// Scroll the 'Send feedback' item into view.
|
|
|
|
await tester.drag(find.text('Small'), const Offset(0.0, -450.0));
|
2017-03-24 22:34:43 +00:00
|
|
|
await tester.pump();
|
|
|
|
await tester.pump(const Duration(seconds: 1)); // Wait until it's changed.
|
|
|
|
|
2017-10-14 04:44:24 +00:00
|
|
|
// Send feedback.
|
2016-11-30 01:47:39 +00:00
|
|
|
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);
|
|
|
|
|
Don't trigger an assert when markNeedsSemanticsUpdate is called multiple times in edge cases (#11544)
* Don't trigger assert if a render object ceases to be a semantic boundary
This bug was exposed by https://github.com/flutter/flutter/pull/11309, which caused the following assertion to trigger when scrolling in the Animation demo:
```
The following assertion was thrown during _updateSemantics():
'package:flutter/src/rendering/object.dart': Failed assertion: line 2626 pos 16: 'fragment is
_InterestingSemanticsFragment': is not true.
```
A minimal reproduction of the bug can be found in `semantics_10_test.dart`, which has been added as a regression test for the bug by this PR.
Looking at that test, here is a description of the faulty behaviour:
1. During the second `pumpWidget` call `RenderExcludeSemantics` marks itself as needing a semantics update (due to excluding going from `false` -> `true`).
2. This causes the nearest ancestor with semantics information (here: `RenderSemanticsAnnotations` representing the "container" Semantics widget) to be added to the `_nodesNeedingSemantics` list.
3. `RenderSliverList` (implementation behind ListView) marks itself as needing a semantics update (due to its changing children).
4. This causes the `RenderSemanticsGestureHandler` to be added to the `_nodesNeedingSemantics` list.
5. Next, canDrag is updated from `true` -> `false`. This means, `RenderSemanticsGestureHandler` is no longer a semantics boundary, it marks itself as needing a semantics update.
6. The nearest ancestor with semantics (`RenderSemanticsAnnotations`, the "container") is added to the `_nodesNeedingSemantics` list (this is a no-op because it is already in the list).
7. During `flushSemantics`, the `_nodesNeedingSemantics` list is walked. The first entry (`RenderSemanticsAnnotations`) updates the semantics tree to only contain the container widget and drop everything else (= no children of the ExcludeSemantics widget are walked).
8. The second entry (`RenderSemanticsGestureHandler`) is updated. It does not add any semantics of its own and is no longer a semantics boundary. Therefore, it wants to merge its descendent semantics into its parents. Here is where the assert throws because the algorithm assumes that every entry in the `_nodesNeedingSemantics` list will produce and own an `_InterestingSemanticsFragment` (passing your semantics on to your parents is not interesting).
The problem here seems to be step 4 in combination with step 5. In step 4 we rely on the fact that `RenderSemanticsGestureHandler` is an (explicit or implicit) semantics boundary and that it will be able to absorb the semantics change of `RenderSliverList`. This is true
at this time. However, in step 4 `RenderSemanticsGestureHandler` decides to no longer be an (explicit or implicit) semantics boundary and our assumption from step 5 becomes incorrect. We did nothing to correct this assumption.
This PR removes a node, that could potentially cease to be a (explicit or implicit) semantics boundary from the `_nodesNeedingSemantics` list to fix that problem. Please node that this does not mean that the node's semantics will not be updated: The node's closest ances
tor with semantics is added to that list during the `markNeedsSemanticsUpdate` call. During `flushSemantics` we will walk from this node to update the semantics of it's children (if changed), which will include the node in question.
* tiny fix
* simplify test
* analyzer fixes
* review comments
2017-08-08 21:17:20 +00:00
|
|
|
testWidgets('Flutter Gallery app smoke test with semantics', (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
|
|
|
}
|