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.
206 lines
6.3 KiB
Dart
206 lines
6.3 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';
|
|
|
|
void main() {
|
|
testWidgets('AnimatedPositioned - basics', (WidgetTester tester) async {
|
|
GlobalKey key = new GlobalKey();
|
|
|
|
RenderBox box;
|
|
|
|
await tester.pumpWidget(
|
|
new Stack(
|
|
children: <Widget>[
|
|
new AnimatedPositioned(
|
|
child: new Container(key: key),
|
|
left: 50.0,
|
|
top: 30.0,
|
|
width: 70.0,
|
|
height: 110.0,
|
|
duration: const Duration(seconds: 2)
|
|
)
|
|
]
|
|
)
|
|
);
|
|
|
|
box = key.currentContext.findRenderObject();
|
|
expect(box.localToGlobal(box.size.center(Point.origin)), equals(const Point(50.0 + 70.0 / 2.0, 30.0 + 110.0 / 2.0)));
|
|
|
|
await tester.pump(const Duration(seconds: 1));
|
|
|
|
box = key.currentContext.findRenderObject();
|
|
expect(box.localToGlobal(box.size.center(Point.origin)), equals(const Point(50.0 + 70.0 / 2.0, 30.0 + 110.0 / 2.0)));
|
|
|
|
await tester.pumpWidget(
|
|
new Stack(
|
|
children: <Widget>[
|
|
new AnimatedPositioned(
|
|
child: new Container(key: key),
|
|
left: 37.0,
|
|
top: 31.0,
|
|
width: 59.0,
|
|
height: 71.0,
|
|
duration: const Duration(seconds: 2)
|
|
)
|
|
]
|
|
)
|
|
);
|
|
|
|
box = key.currentContext.findRenderObject();
|
|
expect(box.localToGlobal(box.size.center(Point.origin)), equals(const Point(50.0 + 70.0 / 2.0, 30.0 + 110.0 / 2.0)));
|
|
|
|
await tester.pump(const Duration(seconds: 1));
|
|
|
|
box = key.currentContext.findRenderObject();
|
|
expect(box.localToGlobal(box.size.center(Point.origin)), equals(const Point(50.0 - (50.0 - 37.0) / 2.0 + (70.0 - (70.0 - 59.0) / 2.0) / 2.0,
|
|
30.0 + (31.0 - 30.0) / 2.0 + (110.0 - (110.0 - 71.0) / 2.0) / 2.0)));
|
|
|
|
await tester.pump(const Duration(seconds: 1));
|
|
|
|
box = key.currentContext.findRenderObject();
|
|
expect(box.localToGlobal(box.size.center(Point.origin)), equals(const Point(37.0 + 59.0 / 2.0, 31.0 + 71.0 / 2.0)));
|
|
});
|
|
|
|
testWidgets('AnimatedPositioned - interrupted animation', (WidgetTester tester) async {
|
|
GlobalKey key = new GlobalKey();
|
|
|
|
RenderBox box;
|
|
|
|
await tester.pumpWidget(
|
|
new Stack(
|
|
children: <Widget>[
|
|
new AnimatedPositioned(
|
|
child: new Container(key: key),
|
|
left: 0.0,
|
|
top: 0.0,
|
|
width: 100.0,
|
|
height: 100.0,
|
|
duration: const Duration(seconds: 2)
|
|
)
|
|
]
|
|
)
|
|
);
|
|
|
|
box = key.currentContext.findRenderObject();
|
|
expect(box.localToGlobal(box.size.center(Point.origin)), equals(const Point(50.0, 50.0)));
|
|
|
|
await tester.pump(const Duration(seconds: 1));
|
|
|
|
box = key.currentContext.findRenderObject();
|
|
expect(box.localToGlobal(box.size.center(Point.origin)), equals(const Point(50.0, 50.0)));
|
|
|
|
await tester.pumpWidget(
|
|
new Stack(
|
|
children: <Widget>[
|
|
new AnimatedPositioned(
|
|
child: new Container(key: key),
|
|
left: 100.0,
|
|
top: 100.0,
|
|
width: 100.0,
|
|
height: 100.0,
|
|
duration: const Duration(seconds: 2)
|
|
)
|
|
]
|
|
)
|
|
);
|
|
|
|
box = key.currentContext.findRenderObject();
|
|
expect(box.localToGlobal(box.size.center(Point.origin)), equals(const Point(50.0, 50.0)));
|
|
|
|
await tester.pump(const Duration(seconds: 1));
|
|
|
|
box = key.currentContext.findRenderObject();
|
|
expect(box.localToGlobal(box.size.center(Point.origin)), equals(const Point(100.0, 100.0)));
|
|
|
|
await tester.pumpWidget(
|
|
new Stack(
|
|
children: <Widget>[
|
|
new AnimatedPositioned(
|
|
child: new Container(key: key),
|
|
left: 150.0,
|
|
top: 150.0,
|
|
width: 100.0,
|
|
height: 100.0,
|
|
duration: const Duration(seconds: 2)
|
|
)
|
|
]
|
|
)
|
|
);
|
|
|
|
box = key.currentContext.findRenderObject();
|
|
expect(box.localToGlobal(box.size.center(Point.origin)), equals(const Point(100.0, 100.0)));
|
|
|
|
await tester.pump(const Duration(seconds: 1));
|
|
|
|
box = key.currentContext.findRenderObject();
|
|
expect(box.localToGlobal(box.size.center(Point.origin)), equals(const Point(150.0, 150.0)));
|
|
|
|
await tester.pump(const Duration(seconds: 1));
|
|
|
|
box = key.currentContext.findRenderObject();
|
|
expect(box.localToGlobal(box.size.center(Point.origin)), equals(const Point(200.0, 200.0)));
|
|
});
|
|
|
|
testWidgets('AnimatedPositioned - switching variables', (WidgetTester tester) async {
|
|
GlobalKey key = new GlobalKey();
|
|
|
|
RenderBox box;
|
|
|
|
await tester.pumpWidget(
|
|
new Stack(
|
|
children: <Widget>[
|
|
new AnimatedPositioned(
|
|
child: new Container(key: key),
|
|
left: 0.0,
|
|
top: 0.0,
|
|
width: 100.0,
|
|
height: 100.0,
|
|
duration: const Duration(seconds: 2)
|
|
)
|
|
]
|
|
)
|
|
);
|
|
|
|
box = key.currentContext.findRenderObject();
|
|
expect(box.localToGlobal(box.size.center(Point.origin)), equals(const Point(50.0, 50.0)));
|
|
|
|
await tester.pump(const Duration(seconds: 1));
|
|
|
|
box = key.currentContext.findRenderObject();
|
|
expect(box.localToGlobal(box.size.center(Point.origin)), equals(const Point(50.0, 50.0)));
|
|
|
|
await tester.pumpWidget(
|
|
new Stack(
|
|
children: <Widget>[
|
|
new AnimatedPositioned(
|
|
child: new Container(key: key),
|
|
left: 0.0,
|
|
top: 100.0,
|
|
right: 100.0, // 700.0 from the left
|
|
height: 100.0,
|
|
duration: const Duration(seconds: 2)
|
|
)
|
|
]
|
|
)
|
|
);
|
|
|
|
box = key.currentContext.findRenderObject();
|
|
expect(box.localToGlobal(box.size.center(Point.origin)), equals(const Point(350.0, 50.0)));
|
|
|
|
await tester.pump(const Duration(seconds: 1));
|
|
|
|
box = key.currentContext.findRenderObject();
|
|
expect(box.localToGlobal(box.size.center(Point.origin)), equals(const Point(350.0, 100.0)));
|
|
|
|
await tester.pump(const Duration(seconds: 1));
|
|
|
|
box = key.currentContext.findRenderObject();
|
|
expect(box.localToGlobal(box.size.center(Point.origin)), equals(const Point(350.0, 150.0)));
|
|
});
|
|
|
|
}
|