mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
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.
76 lines
2.0 KiB
Dart
76 lines
2.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 'test_widgets.dart';
|
|
|
|
void main() {
|
|
testWidgets('Stateful widget smoke test', (WidgetTester tester) async {
|
|
|
|
void checkTree(BoxDecoration expectedDecoration) {
|
|
SingleChildRenderObjectElement element = tester.element(
|
|
find.byElementPredicate((Element element) => element is SingleChildRenderObjectElement)
|
|
);
|
|
expect(element, isNotNull);
|
|
expect(element.renderObject is RenderDecoratedBox, isTrue);
|
|
RenderDecoratedBox renderObject = element.renderObject;
|
|
expect(renderObject.decoration, equals(expectedDecoration));
|
|
}
|
|
|
|
await tester.pumpWidget(
|
|
new FlipWidget(
|
|
left: new DecoratedBox(decoration: kBoxDecorationA),
|
|
right: new DecoratedBox(decoration: kBoxDecorationB)
|
|
)
|
|
);
|
|
|
|
checkTree(kBoxDecorationA);
|
|
|
|
await tester.pumpWidget(
|
|
new FlipWidget(
|
|
left: new DecoratedBox(decoration: kBoxDecorationB),
|
|
right: new DecoratedBox(decoration: kBoxDecorationA)
|
|
)
|
|
);
|
|
|
|
checkTree(kBoxDecorationB);
|
|
|
|
flipStatefulWidget(tester);
|
|
|
|
await tester.pump();
|
|
|
|
checkTree(kBoxDecorationA);
|
|
|
|
await tester.pumpWidget(
|
|
new FlipWidget(
|
|
left: new DecoratedBox(decoration: kBoxDecorationA),
|
|
right: new DecoratedBox(decoration: kBoxDecorationB)
|
|
)
|
|
);
|
|
|
|
checkTree(kBoxDecorationB);
|
|
});
|
|
|
|
testWidgets('Don\'t rebuild subwidgets', (WidgetTester tester) async {
|
|
await tester.pumpWidget(
|
|
new FlipWidget(
|
|
key: new Key('rebuild test'),
|
|
left: new TestBuildCounter(),
|
|
right: new DecoratedBox(decoration: kBoxDecorationB)
|
|
)
|
|
);
|
|
|
|
expect(TestBuildCounter.buildCount, equals(1));
|
|
|
|
flipStatefulWidget(tester);
|
|
|
|
await tester.pump();
|
|
|
|
expect(TestBuildCounter.buildCount, equals(1));
|
|
});
|
|
}
|