mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
* 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
212 lines
7.0 KiB
Dart
212 lines
7.0 KiB
Dart
// Copyright 2015 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 'package:flutter_test/flutter_test.dart';
|
|
import 'package:flutter/rendering.dart';
|
|
import 'package:flutter/widgets.dart';
|
|
import 'package:test/test.dart';
|
|
|
|
final BoxDecoration kBoxDecorationA = new BoxDecoration();
|
|
final BoxDecoration kBoxDecorationB = new BoxDecoration();
|
|
final BoxDecoration kBoxDecorationC = new BoxDecoration();
|
|
|
|
class TestWidget extends StatelessWidget {
|
|
const TestWidget({ this.child });
|
|
|
|
final Widget child;
|
|
|
|
@override
|
|
Widget build(BuildContext context) => child;
|
|
}
|
|
|
|
class TestOrientedBox extends SingleChildRenderObjectWidget {
|
|
TestOrientedBox({ Key key, Widget child }) : super(key: key, child: child);
|
|
|
|
Decoration _getDecoration(BuildContext context) {
|
|
switch (MediaQuery.of(context).orientation) {
|
|
case Orientation.landscape:
|
|
return new BoxDecoration(backgroundColor: const Color(0xFF00FF00));
|
|
case Orientation.portrait:
|
|
return new BoxDecoration(backgroundColor: const Color(0xFF0000FF));
|
|
}
|
|
}
|
|
|
|
@override
|
|
RenderDecoratedBox createRenderObject(BuildContext context) => new RenderDecoratedBox(decoration: _getDecoration(context));
|
|
|
|
@override
|
|
void updateRenderObject(BuildContext context, RenderDecoratedBox renderObject) {
|
|
renderObject.decoration = _getDecoration(context);
|
|
}
|
|
}
|
|
|
|
void main() {
|
|
testWidgets('RenderObjectWidget smoke test', (WidgetTester tester) {
|
|
tester.pumpWidget(new DecoratedBox(decoration: kBoxDecorationA));
|
|
SingleChildRenderObjectElement element =
|
|
tester.element(find.byElementType(SingleChildRenderObjectElement));
|
|
expect(element, isNotNull);
|
|
expect(element.renderObject is RenderDecoratedBox, isTrue);
|
|
RenderDecoratedBox renderObject = element.renderObject;
|
|
expect(renderObject.decoration, equals(kBoxDecorationA));
|
|
expect(renderObject.position, equals(DecorationPosition.background));
|
|
|
|
tester.pumpWidget(new DecoratedBox(decoration: kBoxDecorationB));
|
|
element = tester.element(find.byElementType(SingleChildRenderObjectElement));
|
|
expect(element, isNotNull);
|
|
expect(element.renderObject is RenderDecoratedBox, isTrue);
|
|
renderObject = element.renderObject;
|
|
expect(renderObject.decoration, equals(kBoxDecorationB));
|
|
expect(renderObject.position, equals(DecorationPosition.background));
|
|
});
|
|
|
|
testWidgets('RenderObjectWidget can add and remove children', (WidgetTester tester) {
|
|
|
|
void checkFullTree() {
|
|
SingleChildRenderObjectElement element =
|
|
tester.firstElement(find.byElementType(SingleChildRenderObjectElement));
|
|
expect(element, isNotNull);
|
|
expect(element.renderObject is RenderDecoratedBox, isTrue);
|
|
RenderDecoratedBox renderObject = element.renderObject;
|
|
expect(renderObject.decoration, equals(kBoxDecorationA));
|
|
expect(renderObject.position, equals(DecorationPosition.background));
|
|
expect(renderObject.child, isNotNull);
|
|
expect(renderObject.child is RenderDecoratedBox, isTrue);
|
|
RenderDecoratedBox child = renderObject.child;
|
|
expect(child.decoration, equals(kBoxDecorationB));
|
|
expect(child.position, equals(DecorationPosition.background));
|
|
expect(child.child, isNull);
|
|
}
|
|
|
|
void childBareTree() {
|
|
SingleChildRenderObjectElement element =
|
|
tester.element(find.byElementType(SingleChildRenderObjectElement));
|
|
expect(element, isNotNull);
|
|
expect(element.renderObject is RenderDecoratedBox, isTrue);
|
|
RenderDecoratedBox renderObject = element.renderObject;
|
|
expect(renderObject.decoration, equals(kBoxDecorationA));
|
|
expect(renderObject.position, equals(DecorationPosition.background));
|
|
expect(renderObject.child, isNull);
|
|
}
|
|
|
|
tester.pumpWidget(new DecoratedBox(
|
|
decoration: kBoxDecorationA,
|
|
child: new DecoratedBox(
|
|
decoration: kBoxDecorationB
|
|
)
|
|
));
|
|
|
|
checkFullTree();
|
|
|
|
tester.pumpWidget(new DecoratedBox(
|
|
decoration: kBoxDecorationA,
|
|
child: new TestWidget(
|
|
child: new DecoratedBox(
|
|
decoration: kBoxDecorationB
|
|
)
|
|
)
|
|
));
|
|
|
|
checkFullTree();
|
|
|
|
tester.pumpWidget(new DecoratedBox(
|
|
decoration: kBoxDecorationA,
|
|
child: new DecoratedBox(
|
|
decoration: kBoxDecorationB
|
|
)
|
|
));
|
|
|
|
checkFullTree();
|
|
|
|
tester.pumpWidget(new DecoratedBox(
|
|
decoration: kBoxDecorationA
|
|
));
|
|
|
|
childBareTree();
|
|
|
|
tester.pumpWidget(new DecoratedBox(
|
|
decoration: kBoxDecorationA,
|
|
child: new TestWidget(
|
|
child: new TestWidget(
|
|
child: new DecoratedBox(
|
|
decoration: kBoxDecorationB
|
|
)
|
|
)
|
|
)
|
|
));
|
|
|
|
checkFullTree();
|
|
|
|
tester.pumpWidget(new DecoratedBox(
|
|
decoration: kBoxDecorationA
|
|
));
|
|
|
|
childBareTree();
|
|
});
|
|
|
|
testWidgets('Detached render tree is intact', (WidgetTester tester) {
|
|
|
|
tester.pumpWidget(new DecoratedBox(
|
|
decoration: kBoxDecorationA,
|
|
child: new DecoratedBox(
|
|
decoration: kBoxDecorationB,
|
|
child: new DecoratedBox(
|
|
decoration: kBoxDecorationC
|
|
)
|
|
)
|
|
));
|
|
|
|
SingleChildRenderObjectElement element =
|
|
tester.firstElement(find.byElementType(SingleChildRenderObjectElement));
|
|
expect(element.renderObject is RenderDecoratedBox, isTrue);
|
|
RenderDecoratedBox parent = element.renderObject;
|
|
expect(parent.child is RenderDecoratedBox, isTrue);
|
|
RenderDecoratedBox child = parent.child;
|
|
expect(child.decoration, equals(kBoxDecorationB));
|
|
expect(child.child is RenderDecoratedBox, isTrue);
|
|
RenderDecoratedBox grandChild = child.child;
|
|
expect(grandChild.decoration, equals(kBoxDecorationC));
|
|
expect(grandChild.child, isNull);
|
|
|
|
tester.pumpWidget(new DecoratedBox(
|
|
decoration: kBoxDecorationA
|
|
));
|
|
|
|
element =
|
|
tester.element(find.byElementType(SingleChildRenderObjectElement));
|
|
expect(element.renderObject is RenderDecoratedBox, isTrue);
|
|
expect(element.renderObject, equals(parent));
|
|
expect(parent.child, isNull);
|
|
|
|
expect(child.parent, isNull);
|
|
expect(child.decoration, equals(kBoxDecorationB));
|
|
expect(child.child, equals(grandChild));
|
|
expect(grandChild.parent, equals(child));
|
|
expect(grandChild.decoration, equals(kBoxDecorationC));
|
|
expect(grandChild.child, isNull);
|
|
});
|
|
|
|
testWidgets('Can watch inherited widgets', (WidgetTester tester) {
|
|
Key boxKey = new UniqueKey();
|
|
TestOrientedBox box = new TestOrientedBox(key: boxKey);
|
|
|
|
tester.pumpWidget(new MediaQuery(
|
|
data: new MediaQueryData(size: const Size(400.0, 300.0)),
|
|
child: box
|
|
));
|
|
|
|
RenderDecoratedBox renderBox = tester.renderObject(find.byKey(boxKey));
|
|
BoxDecoration decoration = renderBox.decoration;
|
|
expect(decoration.backgroundColor, equals(new Color(0xFF00FF00)));
|
|
|
|
tester.pumpWidget(new MediaQuery(
|
|
data: new MediaQueryData(size: const Size(300.0, 400.0)),
|
|
child: box
|
|
));
|
|
|
|
decoration = renderBox.decoration;
|
|
expect(decoration.backgroundColor, equals(new Color(0xFF0000FF)));
|
|
});
|
|
}
|