Commit 1484add1 authored by Adam Barth's avatar Adam Barth

Add TestGesture

This helper makes it easier to write correct tests that involve
gestures.

Fixes #1855
parent aeb94191
......@@ -76,14 +76,13 @@ void main() {
expect(tester.findText('Help & Feedback'), isNull);
// drag the drawer out
TestPointer pointer = new TestPointer(1);
Point left = new Point(0.0, ui.window.size.height / 2.0);
Point right = new Point(ui.window.size.width, left.y);
tester.dispatchEvent(pointer.down(left), left);
TestGesture gesture = tester.startGesture(left);
tester.pump();
tester.dispatchEvent(pointer.move(right), left);
gesture.moveTo(right);
tester.pump();
tester.dispatchEvent(pointer.up(), left);
gesture.up();
tester.pump();
expect(tester.findText('MARKET'), isNotNull);
expect(tester.findText('Help & Feedback'), isNotNull);
......
......@@ -26,15 +26,14 @@ void main() {
Point middleOfContainer = tester.getCenter(tester.findText('Hello'));
Point target = tester.getCenter(tester.findElementByKey(blockKey));
TestPointer pointer = new TestPointer();
tester.dispatchEvent(pointer.down(target), target);
tester.dispatchEvent(pointer.move(target + const Offset(0.0, -10.0)), target);
TestGesture gesture = tester.startGesture(target);
gesture.moveBy(const Offset(0.0, -10.0));
tester.pump(const Duration(milliseconds: 1));
expect(tester.getCenter(tester.findText('Hello')) == middleOfContainer, isTrue);
tester.dispatchEvent(pointer.up(), target);
gesture.up();
});
});
......@@ -55,15 +54,14 @@ void main() {
Point middleOfContainer = tester.getCenter(tester.findText('Hello'));
Point target = tester.getCenter(tester.findElementByKey(blockKey));
TestPointer pointer = new TestPointer();
tester.dispatchEvent(pointer.down(target), target);
tester.dispatchEvent(pointer.move(target + const Offset(0.0, -10.0)), target);
TestGesture gesture = tester.startGesture(target);
gesture.moveBy(const Offset(0.0, -10.0));
tester.pump(const Duration(milliseconds: 1));
expect(tester.getCenter(tester.findText('Hello')) == middleOfContainer, isFalse);
tester.dispatchEvent(pointer.up(), target);
gesture.up();
});
});
......
......@@ -82,10 +82,9 @@ void dismissElement(WidgetTester tester, Element itemElement, { DismissDirection
fail("unsupported gestureDirection");
}
TestPointer pointer = new TestPointer(5);
tester.dispatchEvent(pointer.down(downLocation), downLocation);
tester.dispatchEvent(pointer.move(upLocation), downLocation);
tester.dispatchEvent(pointer.up(), downLocation);
TestGesture gesture = tester.startGesture(downLocation, pointer: 5);
gesture.moveTo(upLocation);
gesture.up();
}
void dismissItem(WidgetTester tester, int item, { DismissDirection gestureDirection }) {
......@@ -247,18 +246,18 @@ void main() {
tester.pumpWidget(widgetBuilder());
Element itemElement = tester.findText('0');
TestPointer pointer = new TestPointer(5);
Point location = tester.getTopLeft(itemElement);
Offset offset = new Offset(0.0, 5.0);
tester.dispatchEvent(pointer.down(location), location);
tester.dispatchEvent(pointer.move(location + offset), location);
TestGesture gesture = tester.startGesture(location, pointer: 5);
gesture.moveBy(offset);
tester.pumpWidget(widgetBuilder());
tester.dispatchEvent(pointer.move(location + (offset * 2.0)), location);
gesture.moveBy(offset);
tester.pumpWidget(widgetBuilder());
tester.dispatchEvent(pointer.move(location + (offset * 3.0)), location);
gesture.moveBy(offset);
tester.pumpWidget(widgetBuilder());
tester.dispatchEvent(pointer.move(location + (offset * 4.0)), location);
gesture.moveBy(offset);
tester.pumpWidget(widgetBuilder());
gesture.up();
});
});
......
......@@ -9,8 +9,6 @@ import 'package:test/test.dart';
void main() {
test('Uncontested scrolls start immediately', () {
testWidgets((WidgetTester tester) {
TestPointer pointer = new TestPointer(7);
bool didStartDrag = false;
double updatedDragDelta;
bool didEndDrag = false;
......@@ -38,20 +36,20 @@ void main() {
expect(didEndDrag, isFalse);
Point firstLocation = new Point(10.0, 10.0);
tester.dispatchEvent(pointer.down(firstLocation), firstLocation);
TestGesture gesture = tester.startGesture(firstLocation, pointer: 7);
expect(didStartDrag, isTrue);
didStartDrag = false;
expect(updatedDragDelta, isNull);
expect(didEndDrag, isFalse);
Point secondLocation = new Point(10.0, 9.0);
tester.dispatchEvent(pointer.move(secondLocation), firstLocation);
gesture.moveTo(secondLocation);
expect(didStartDrag, isFalse);
expect(updatedDragDelta, -1.0);
updatedDragDelta = null;
expect(didEndDrag, isFalse);
tester.dispatchEvent(pointer.up(), firstLocation);
gesture.up();
expect(didStartDrag, isFalse);
expect(updatedDragDelta, isNull);
expect(didEndDrag, isTrue);
......@@ -63,8 +61,6 @@ void main() {
test('Match two scroll gestures in succession', () {
testWidgets((WidgetTester tester) {
TestPointer pointer = new TestPointer(7);
int gestureCount = 0;
double dragDistance = 0.0;
......@@ -84,13 +80,13 @@ void main() {
);
tester.pumpWidget(widget);
tester.dispatchEvent(pointer.down(downLocation), downLocation);
tester.dispatchEvent(pointer.move(upLocation), downLocation);
tester.dispatchEvent(pointer.up(), downLocation);
TestGesture gesture = tester.startGesture(downLocation, pointer: 7);
gesture.moveTo(upLocation);
gesture.up();
tester.dispatchEvent(pointer.down(downLocation), downLocation);
tester.dispatchEvent(pointer.move(upLocation), downLocation);
tester.dispatchEvent(pointer.up(), downLocation);
gesture = tester.startGesture(downLocation, pointer: 7);
gesture.moveTo(upLocation);
gesture.up();
expect(gestureCount, 2);
expect(dragDistance, 20.0);
......
......@@ -58,11 +58,10 @@ void main() {
test('setState() smoke test', () {
testWidgets((WidgetTester tester) {
tester.pumpWidget(new Outside());
TestPointer pointer = new TestPointer(1);
Point location = tester.getCenter(tester.findText('INSIDE'));
tester.dispatchEvent(pointer.down(location), location);
TestGesture gesture = tester.startGesture(location);
tester.pump();
tester.dispatchEvent(pointer.up(), location);
gesture.up();
tester.pump();
});
});
......
......@@ -5,6 +5,7 @@
/// Testing library for flutter, built on top of package:test.
library flutter_test;
export 'src/instrumentation.dart';
export 'src/service_mocker.dart';
export 'src/test_pointer.dart';
export 'src/widget_tester.dart';
......@@ -161,6 +161,14 @@ class Instrumentation {
_dispatchEvent(p.up(), result);
}
TestGesture startGesture(Point downLocation, { int pointer: 1 }) {
TestPointer p = new TestPointer(pointer);
HitTestResult result = _hitTest(downLocation);
_dispatchEvent(p.down(downLocation), result);
return new TestGesture._(this, result, p);
}
@Deprecated('soon. Use startGesture instead.')
void dispatchEvent(PointerEvent event, Point location) {
_dispatchEvent(event, _hitTest(location));
}
......@@ -175,3 +183,34 @@ class Instrumentation {
binding.dispatchEvent(event, result);
}
}
class TestGesture {
TestGesture._(this._target, this._result, this.pointer);
final Instrumentation _target;
final HitTestResult _result;
final TestPointer pointer;
bool _isDown = true;
void moveTo(Point location) {
assert(_isDown);
_target._dispatchEvent(pointer.move(location), _result);
}
void moveBy(Offset offset) {
assert(_isDown);
moveTo(pointer.location + offset);
}
void up() {
assert(_isDown);
_isDown = false;
_target._dispatchEvent(pointer.up(), _result);
}
void cancel() {
assert(_isDown);
_isDown = false;
_target._dispatchEvent(pointer.cancel(), _result);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment