forked from firka/flutter
Test API improvements: pumpAndSettle, autogenerate pointer IDs (#8751)
This commit is contained in:
@@ -248,16 +248,16 @@ class WidgetController {
|
||||
// INTERACTION
|
||||
|
||||
/// Dispatch a pointer down / pointer up sequence at the center of
|
||||
/// the given widget, assuming it is exposed. If the center of the
|
||||
/// widget is not exposed, this might send events to another
|
||||
/// object.
|
||||
Future<Null> tap(Finder finder, { int pointer: 1 }) {
|
||||
/// the given widget, assuming it is exposed.
|
||||
///
|
||||
/// If the center of the widget is not exposed, this might send events to
|
||||
/// another object.
|
||||
Future<Null> tap(Finder finder, { int pointer }) {
|
||||
return tapAt(getCenter(finder), pointer: pointer);
|
||||
}
|
||||
|
||||
/// Dispatch a pointer down / pointer up sequence at the given
|
||||
/// location.
|
||||
Future<Null> tapAt(Point location, { int pointer: 1 }) {
|
||||
/// Dispatch a pointer down / pointer up sequence at the given location.
|
||||
Future<Null> tapAt(Point location, { int pointer }) {
|
||||
return TestAsyncUtils.guard(() async {
|
||||
final TestGesture gesture = await startGesture(location, pointer: pointer);
|
||||
await gesture.up();
|
||||
@@ -267,16 +267,17 @@ class WidgetController {
|
||||
|
||||
/// Dispatch a pointer down / pointer up sequence (with a delay of
|
||||
/// [kLongPressTimeout] + [kPressTimeout] between the two events) at the
|
||||
/// center of the given widget, assuming it is exposed. If the center of the
|
||||
/// widget is not exposed, this might send events to another
|
||||
/// object.
|
||||
Future<Null> longPress(Finder finder, { int pointer: 1 }) {
|
||||
/// center of the given widget, assuming it is exposed.
|
||||
///
|
||||
/// If the center of the widget is not exposed, this might send events to
|
||||
/// another object.
|
||||
Future<Null> longPress(Finder finder, { int pointer }) {
|
||||
return longPressAt(getCenter(finder), pointer: pointer);
|
||||
}
|
||||
|
||||
/// Dispatch a pointer down / pointer up sequence at the given location with
|
||||
/// a delay of [kLongPressTimeout] + [kPressTimeout] between the two events.
|
||||
Future<Null> longPressAt(Point location, { int pointer: 1 }) {
|
||||
Future<Null> longPressAt(Point location, { int pointer }) {
|
||||
return TestAsyncUtils.guard(() async {
|
||||
final TestGesture gesture = await startGesture(location, pointer: pointer);
|
||||
await pump(kLongPressTimeout + kPressTimeout);
|
||||
@@ -286,25 +287,33 @@ class WidgetController {
|
||||
}
|
||||
|
||||
/// Attempts a fling gesture starting from the center of the given
|
||||
/// widget, moving the given distance, reaching the given velocity.
|
||||
/// widget, moving the given distance, reaching the given speed.
|
||||
///
|
||||
/// If the middle of the widget is not exposed, this might send
|
||||
/// events to another object.
|
||||
///
|
||||
/// This can pump frames. See [flingFrom] for a discussion of how the
|
||||
/// `offset`, `velocity` and `frameInterval` arguments affect this.
|
||||
Future<Null> fling(Finder finder, Offset offset, double velocity, { int pointer: 1, Duration frameInterval: const Duration(milliseconds: 16) }) {
|
||||
return flingFrom(getCenter(finder), offset, velocity, pointer: pointer, frameInterval: frameInterval);
|
||||
///
|
||||
/// The `speed` is in pixels per second in the direction given by `offset`.
|
||||
///
|
||||
/// A fling is essentially a drag that ends at a particular speed. If you
|
||||
/// just want to drag and end without a fling, use [drag].
|
||||
Future<Null> fling(Finder finder, Offset offset, double speed, {
|
||||
int pointer,
|
||||
Duration frameInterval: const Duration(milliseconds: 16),
|
||||
}) {
|
||||
return flingFrom(getCenter(finder), offset, speed, pointer: pointer, frameInterval: frameInterval);
|
||||
}
|
||||
|
||||
/// Attempts a fling gesture starting from the given location,
|
||||
/// moving the given distance, reaching the given velocity.
|
||||
/// Attempts a fling gesture starting from the given location, moving the
|
||||
/// given distance, reaching the given speed.
|
||||
///
|
||||
/// Exactly 50 pointer events are synthesized.
|
||||
///
|
||||
/// The offset and velocity control the interval between each pointer event.
|
||||
/// For example, if the offset is 200 pixels, and the velocity is 800 pixels
|
||||
/// per second, the pointer events will be sent for each increment of 4 pixels
|
||||
/// The offset and speed control the interval between each pointer event. For
|
||||
/// example, if the offset is 200 pixels down, and the speed is 800 pixels per
|
||||
/// second, the pointer events will be sent for each increment of 4 pixels
|
||||
/// (200/50), over 250ms (200/800), meaning events will be sent every 1.25ms
|
||||
/// (250/200).
|
||||
///
|
||||
@@ -312,27 +321,30 @@ class WidgetController {
|
||||
/// calls to [pump]). If the total duration is longer than `frameInterval`,
|
||||
/// then one frame is pumped each time that amount of time elapses while
|
||||
/// sending events, or each time an event is synthesised, whichever is rarer.
|
||||
Future<Null> flingFrom(Point startLocation, Offset offset, double velocity, { int pointer: 1, Duration frameInterval: const Duration(milliseconds: 16) }) {
|
||||
///
|
||||
/// A fling is essentially a drag that ends at a particular speed. If you
|
||||
/// just want to drag and end without a fling, use [dragFrom].
|
||||
Future<Null> flingFrom(Point startLocation, Offset offset, double speed, { int pointer, Duration frameInterval: const Duration(milliseconds: 16) }) {
|
||||
assert(offset.distance > 0.0);
|
||||
assert(velocity > 0.0); // velocity is pixels/second
|
||||
assert(speed > 0.0); // speed is pixels/second
|
||||
return TestAsyncUtils.guard(() async {
|
||||
final TestPointer p = new TestPointer(pointer);
|
||||
final TestPointer testPointer = new TestPointer(pointer ?? _getNextPointer());
|
||||
final HitTestResult result = hitTestOnBinding(startLocation);
|
||||
const int kMoveCount = 50; // Needs to be >= kHistorySize, see _LeastSquaresVelocityTrackerStrategy
|
||||
final double timeStampDelta = 1000.0 * offset.distance / (kMoveCount * velocity);
|
||||
final double timeStampDelta = 1000.0 * offset.distance / (kMoveCount * speed);
|
||||
double timeStamp = 0.0;
|
||||
double lastTimeStamp = timeStamp;
|
||||
await sendEventToBinding(p.down(startLocation, timeStamp: new Duration(milliseconds: timeStamp.round())), result);
|
||||
await sendEventToBinding(testPointer.down(startLocation, timeStamp: new Duration(milliseconds: timeStamp.round())), result);
|
||||
for (int i = 0; i <= kMoveCount; i += 1) {
|
||||
final Point location = startLocation + Offset.lerp(Offset.zero, offset, i / kMoveCount);
|
||||
await sendEventToBinding(p.move(location, timeStamp: new Duration(milliseconds: timeStamp.round())), result);
|
||||
await sendEventToBinding(testPointer.move(location, timeStamp: new Duration(milliseconds: timeStamp.round())), result);
|
||||
timeStamp += timeStampDelta;
|
||||
if (timeStamp - lastTimeStamp > frameInterval.inMilliseconds) {
|
||||
await pump(new Duration(milliseconds: (timeStamp - lastTimeStamp).truncate()));
|
||||
lastTimeStamp = timeStamp;
|
||||
}
|
||||
}
|
||||
await sendEventToBinding(p.up(timeStamp: new Duration(milliseconds: timeStamp.round())), result);
|
||||
await sendEventToBinding(testPointer.up(timeStamp: new Duration(milliseconds: timeStamp.round())), result);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
@@ -352,13 +364,20 @@ class WidgetController {
|
||||
///
|
||||
/// If the middle of the widget is not exposed, this might send
|
||||
/// events to another object.
|
||||
Future<Null> scroll(Finder finder, Offset offset, { int pointer: 1 }) {
|
||||
return scrollAt(getCenter(finder), offset, pointer: pointer);
|
||||
///
|
||||
/// If you want the drag to end with a speed so that the gesture recognition
|
||||
/// system identifies the gesture as a fling, consider using [fling] instead.
|
||||
Future<Null> drag(Finder finder, Offset offset, { int pointer }) {
|
||||
return dragFrom(getCenter(finder), offset, pointer: pointer);
|
||||
}
|
||||
|
||||
/// Attempts a drag gesture consisting of a pointer down, a move by
|
||||
/// the given offset, and a pointer up.
|
||||
Future<Null> scrollAt(Point startLocation, Offset offset, { int pointer: 1 }) {
|
||||
///
|
||||
/// If you want the drag to end with a speed so that the gesture recognition
|
||||
/// system identifies the gesture as a fling, consider using [flingFrom]
|
||||
/// instead.
|
||||
Future<Null> dragFrom(Point startLocation, Offset offset, { int pointer }) {
|
||||
return TestAsyncUtils.guard(() async {
|
||||
final TestGesture gesture = await startGesture(startLocation, pointer: pointer);
|
||||
await gesture.moveBy(offset);
|
||||
@@ -367,10 +386,27 @@ class WidgetController {
|
||||
});
|
||||
}
|
||||
|
||||
/// The next available pointer identifier.
|
||||
///
|
||||
/// This is the default pointer identifier that will be used the next time the
|
||||
/// [startGesture] method is called without an explicit pointer identifier.
|
||||
int nextPointer = 1;
|
||||
|
||||
int _getNextPointer() {
|
||||
final int result = nextPointer;
|
||||
nextPointer += 1;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Begins a gesture at a particular point, and returns the
|
||||
/// [TestGesture] object which you can use to continue the gesture.
|
||||
Future<TestGesture> startGesture(Point downLocation, { int pointer: 1 }) {
|
||||
return TestGesture.down(downLocation, pointer: pointer, hitTester: hitTestOnBinding, dispatcher: sendEventToBinding);
|
||||
Future<TestGesture> startGesture(Point downLocation, { int pointer }) {
|
||||
return TestGesture.down(
|
||||
downLocation,
|
||||
pointer: pointer ?? _getNextPointer(),
|
||||
hitTester: hitTestOnBinding,
|
||||
dispatcher: sendEventToBinding,
|
||||
);
|
||||
}
|
||||
|
||||
/// Forwards the given location to the binding's hitTest logic.
|
||||
|
||||
@@ -15,8 +15,12 @@ export 'dart:ui' show Point;
|
||||
/// You can use this to manually simulate individual events, but the
|
||||
/// simplest way to generate coherent gestures is to use [TestGesture].
|
||||
class TestPointer {
|
||||
/// Creates a [TestPointer]. By default, the pointer identifier used is 1, however
|
||||
/// this can be overridden by providing an argument to the constructor.
|
||||
/// Creates a [TestPointer]. By default, the pointer identifier used is 1,
|
||||
/// however this can be overridden by providing an argument to the
|
||||
/// constructor.
|
||||
///
|
||||
/// Multiple [TestPointer]s created with the same pointer identifier will
|
||||
/// interfere with each other if they are used in parallel.
|
||||
TestPointer([ this.pointer = 1 ]);
|
||||
|
||||
/// The pointer identifier used for events generated by this object.
|
||||
@@ -123,7 +127,7 @@ class TestGesture {
|
||||
/// Create a [TestGesture] by starting with a pointerDown at the
|
||||
/// given point.
|
||||
///
|
||||
/// By default, the pointer ID used is 1. This can be overridden by
|
||||
/// By default, the pointer identifier used is 1. This can be overridden by
|
||||
/// providing the `pointer` argument.
|
||||
///
|
||||
/// A function to use for hit testing should be provided via the `hitTester`
|
||||
|
||||
@@ -191,8 +191,10 @@ class WidgetTester extends WidgetController implements HitTestDispatcher, Ticker
|
||||
}
|
||||
|
||||
/// Repeatedly calls [pump] with the given `duration` until there are no
|
||||
/// longer any transient callbacks scheduled. If no transient callbacks are
|
||||
/// scheduled when the function is called, it returns without calling [pump].
|
||||
/// longer any transient callbacks scheduled. This will call [pump] at least
|
||||
/// once, even if no transient callbacks are scheduled when the function is
|
||||
/// called, in case there are dirty widgets to rebuild which will themselves
|
||||
/// register new transient callbacks.
|
||||
///
|
||||
/// This essentially waits for all animations to have completed.
|
||||
///
|
||||
@@ -209,7 +211,7 @@ class WidgetTester extends WidgetController implements HitTestDispatcher, Ticker
|
||||
///
|
||||
/// Alternatively, one can check that the return value from this function
|
||||
/// matches the expected number of pumps.
|
||||
Future<int> pumpUntilNoTransientCallbacks([
|
||||
Future<int> pumpAndSettle([
|
||||
Duration duration = const Duration(milliseconds: 100),
|
||||
EnginePhase phase = EnginePhase.sendSemanticsTree
|
||||
]) {
|
||||
@@ -217,10 +219,10 @@ class WidgetTester extends WidgetController implements HitTestDispatcher, Ticker
|
||||
assert(duration > Duration.ZERO);
|
||||
int count = 0;
|
||||
return TestAsyncUtils.guard(() async {
|
||||
while (binding.transientCallbackCount > 0) {
|
||||
do {
|
||||
await binding.pump(duration, phase);
|
||||
count += 1;
|
||||
}
|
||||
} while (binding.transientCallbackCount > 0);
|
||||
}).then<int>((Null _) => count);
|
||||
}
|
||||
|
||||
|
||||
@@ -77,11 +77,11 @@ void main() {
|
||||
duration: const Duration(milliseconds: 5100),
|
||||
vsync: tester,
|
||||
);
|
||||
count = await tester.pumpUntilNoTransientCallbacks(const Duration(seconds: 1));
|
||||
expect(count, 0);
|
||||
count = await tester.pumpAndSettle(const Duration(seconds: 1));
|
||||
expect(count, 1); // it always pumps at least one frame
|
||||
|
||||
test.forward(from: 0.0);
|
||||
count = await tester.pumpUntilNoTransientCallbacks(const Duration(seconds: 1));
|
||||
count = await tester.pumpAndSettle(const Duration(seconds: 1));
|
||||
// 1 frame at t=0, starting the animation
|
||||
// 1 frame at t=1
|
||||
// 1 frame at t=2
|
||||
@@ -93,13 +93,13 @@ void main() {
|
||||
|
||||
test.forward(from: 0.0);
|
||||
await tester.pump(); // starts the animation
|
||||
count = await tester.pumpUntilNoTransientCallbacks(const Duration(seconds: 1));
|
||||
count = await tester.pumpAndSettle(const Duration(seconds: 1));
|
||||
expect(count, 6);
|
||||
|
||||
test.forward(from: 0.0);
|
||||
await tester.pump(); // starts the animation
|
||||
await tester.pump(); // has no effect
|
||||
count = await tester.pumpUntilNoTransientCallbacks(const Duration(seconds: 1));
|
||||
count = await tester.pumpAndSettle(const Duration(seconds: 1));
|
||||
expect(count, 6);
|
||||
});
|
||||
});
|
||||
@@ -180,9 +180,9 @@ void main() {
|
||||
|
||||
expect(failure, isNotNull);
|
||||
expect(
|
||||
failure.message,
|
||||
contains('Actual: ?:<zero widgets with text "bar" that has ancestor(s) with type Column with text "foo"')
|
||||
);
|
||||
failure.message,
|
||||
contains('Actual: ?:<zero widgets with text "bar" that has ancestor(s) with type Column with text "foo"')
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user