Commit a499dc1d authored by Adam Barth's avatar Adam Barth

Merge pull request #2065 from abarth/test_gesture

Add TestGesture
parents aeb94191 1484add1
...@@ -76,14 +76,13 @@ void main() { ...@@ -76,14 +76,13 @@ void main() {
expect(tester.findText('Help & Feedback'), isNull); expect(tester.findText('Help & Feedback'), isNull);
// drag the drawer out // drag the drawer out
TestPointer pointer = new TestPointer(1);
Point left = new Point(0.0, ui.window.size.height / 2.0); Point left = new Point(0.0, ui.window.size.height / 2.0);
Point right = new Point(ui.window.size.width, left.y); Point right = new Point(ui.window.size.width, left.y);
tester.dispatchEvent(pointer.down(left), left); TestGesture gesture = tester.startGesture(left);
tester.pump(); tester.pump();
tester.dispatchEvent(pointer.move(right), left); gesture.moveTo(right);
tester.pump(); tester.pump();
tester.dispatchEvent(pointer.up(), left); gesture.up();
tester.pump(); tester.pump();
expect(tester.findText('MARKET'), isNotNull); expect(tester.findText('MARKET'), isNotNull);
expect(tester.findText('Help & Feedback'), isNotNull); expect(tester.findText('Help & Feedback'), isNotNull);
......
...@@ -26,15 +26,14 @@ void main() { ...@@ -26,15 +26,14 @@ void main() {
Point middleOfContainer = tester.getCenter(tester.findText('Hello')); Point middleOfContainer = tester.getCenter(tester.findText('Hello'));
Point target = tester.getCenter(tester.findElementByKey(blockKey)); Point target = tester.getCenter(tester.findElementByKey(blockKey));
TestPointer pointer = new TestPointer(); TestGesture gesture = tester.startGesture(target);
tester.dispatchEvent(pointer.down(target), target); gesture.moveBy(const Offset(0.0, -10.0));
tester.dispatchEvent(pointer.move(target + const Offset(0.0, -10.0)), target);
tester.pump(const Duration(milliseconds: 1)); tester.pump(const Duration(milliseconds: 1));
expect(tester.getCenter(tester.findText('Hello')) == middleOfContainer, isTrue); expect(tester.getCenter(tester.findText('Hello')) == middleOfContainer, isTrue);
tester.dispatchEvent(pointer.up(), target); gesture.up();
}); });
}); });
...@@ -55,15 +54,14 @@ void main() { ...@@ -55,15 +54,14 @@ void main() {
Point middleOfContainer = tester.getCenter(tester.findText('Hello')); Point middleOfContainer = tester.getCenter(tester.findText('Hello'));
Point target = tester.getCenter(tester.findElementByKey(blockKey)); Point target = tester.getCenter(tester.findElementByKey(blockKey));
TestPointer pointer = new TestPointer(); TestGesture gesture = tester.startGesture(target);
tester.dispatchEvent(pointer.down(target), target); gesture.moveBy(const Offset(0.0, -10.0));
tester.dispatchEvent(pointer.move(target + const Offset(0.0, -10.0)), target);
tester.pump(const Duration(milliseconds: 1)); tester.pump(const Duration(milliseconds: 1));
expect(tester.getCenter(tester.findText('Hello')) == middleOfContainer, isFalse); 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 ...@@ -82,10 +82,9 @@ void dismissElement(WidgetTester tester, Element itemElement, { DismissDirection
fail("unsupported gestureDirection"); fail("unsupported gestureDirection");
} }
TestPointer pointer = new TestPointer(5); TestGesture gesture = tester.startGesture(downLocation, pointer: 5);
tester.dispatchEvent(pointer.down(downLocation), downLocation); gesture.moveTo(upLocation);
tester.dispatchEvent(pointer.move(upLocation), downLocation); gesture.up();
tester.dispatchEvent(pointer.up(), downLocation);
} }
void dismissItem(WidgetTester tester, int item, { DismissDirection gestureDirection }) { void dismissItem(WidgetTester tester, int item, { DismissDirection gestureDirection }) {
...@@ -247,18 +246,18 @@ void main() { ...@@ -247,18 +246,18 @@ void main() {
tester.pumpWidget(widgetBuilder()); tester.pumpWidget(widgetBuilder());
Element itemElement = tester.findText('0'); Element itemElement = tester.findText('0');
TestPointer pointer = new TestPointer(5);
Point location = tester.getTopLeft(itemElement); Point location = tester.getTopLeft(itemElement);
Offset offset = new Offset(0.0, 5.0); Offset offset = new Offset(0.0, 5.0);
tester.dispatchEvent(pointer.down(location), location); TestGesture gesture = tester.startGesture(location, pointer: 5);
tester.dispatchEvent(pointer.move(location + offset), location); gesture.moveBy(offset);
tester.pumpWidget(widgetBuilder()); tester.pumpWidget(widgetBuilder());
tester.dispatchEvent(pointer.move(location + (offset * 2.0)), location); gesture.moveBy(offset);
tester.pumpWidget(widgetBuilder()); tester.pumpWidget(widgetBuilder());
tester.dispatchEvent(pointer.move(location + (offset * 3.0)), location); gesture.moveBy(offset);
tester.pumpWidget(widgetBuilder()); tester.pumpWidget(widgetBuilder());
tester.dispatchEvent(pointer.move(location + (offset * 4.0)), location); gesture.moveBy(offset);
tester.pumpWidget(widgetBuilder()); tester.pumpWidget(widgetBuilder());
gesture.up();
}); });
}); });
......
...@@ -9,8 +9,6 @@ import 'package:test/test.dart'; ...@@ -9,8 +9,6 @@ import 'package:test/test.dart';
void main() { void main() {
test('Uncontested scrolls start immediately', () { test('Uncontested scrolls start immediately', () {
testWidgets((WidgetTester tester) { testWidgets((WidgetTester tester) {
TestPointer pointer = new TestPointer(7);
bool didStartDrag = false; bool didStartDrag = false;
double updatedDragDelta; double updatedDragDelta;
bool didEndDrag = false; bool didEndDrag = false;
...@@ -38,20 +36,20 @@ void main() { ...@@ -38,20 +36,20 @@ void main() {
expect(didEndDrag, isFalse); expect(didEndDrag, isFalse);
Point firstLocation = new Point(10.0, 10.0); Point firstLocation = new Point(10.0, 10.0);
tester.dispatchEvent(pointer.down(firstLocation), firstLocation); TestGesture gesture = tester.startGesture(firstLocation, pointer: 7);
expect(didStartDrag, isTrue); expect(didStartDrag, isTrue);
didStartDrag = false; didStartDrag = false;
expect(updatedDragDelta, isNull); expect(updatedDragDelta, isNull);
expect(didEndDrag, isFalse); expect(didEndDrag, isFalse);
Point secondLocation = new Point(10.0, 9.0); Point secondLocation = new Point(10.0, 9.0);
tester.dispatchEvent(pointer.move(secondLocation), firstLocation); gesture.moveTo(secondLocation);
expect(didStartDrag, isFalse); expect(didStartDrag, isFalse);
expect(updatedDragDelta, -1.0); expect(updatedDragDelta, -1.0);
updatedDragDelta = null; updatedDragDelta = null;
expect(didEndDrag, isFalse); expect(didEndDrag, isFalse);
tester.dispatchEvent(pointer.up(), firstLocation); gesture.up();
expect(didStartDrag, isFalse); expect(didStartDrag, isFalse);
expect(updatedDragDelta, isNull); expect(updatedDragDelta, isNull);
expect(didEndDrag, isTrue); expect(didEndDrag, isTrue);
...@@ -63,8 +61,6 @@ void main() { ...@@ -63,8 +61,6 @@ void main() {
test('Match two scroll gestures in succession', () { test('Match two scroll gestures in succession', () {
testWidgets((WidgetTester tester) { testWidgets((WidgetTester tester) {
TestPointer pointer = new TestPointer(7);
int gestureCount = 0; int gestureCount = 0;
double dragDistance = 0.0; double dragDistance = 0.0;
...@@ -84,13 +80,13 @@ void main() { ...@@ -84,13 +80,13 @@ void main() {
); );
tester.pumpWidget(widget); tester.pumpWidget(widget);
tester.dispatchEvent(pointer.down(downLocation), downLocation); TestGesture gesture = tester.startGesture(downLocation, pointer: 7);
tester.dispatchEvent(pointer.move(upLocation), downLocation); gesture.moveTo(upLocation);
tester.dispatchEvent(pointer.up(), downLocation); gesture.up();
tester.dispatchEvent(pointer.down(downLocation), downLocation); gesture = tester.startGesture(downLocation, pointer: 7);
tester.dispatchEvent(pointer.move(upLocation), downLocation); gesture.moveTo(upLocation);
tester.dispatchEvent(pointer.up(), downLocation); gesture.up();
expect(gestureCount, 2); expect(gestureCount, 2);
expect(dragDistance, 20.0); expect(dragDistance, 20.0);
......
...@@ -58,11 +58,10 @@ void main() { ...@@ -58,11 +58,10 @@ void main() {
test('setState() smoke test', () { test('setState() smoke test', () {
testWidgets((WidgetTester tester) { testWidgets((WidgetTester tester) {
tester.pumpWidget(new Outside()); tester.pumpWidget(new Outside());
TestPointer pointer = new TestPointer(1);
Point location = tester.getCenter(tester.findText('INSIDE')); Point location = tester.getCenter(tester.findText('INSIDE'));
tester.dispatchEvent(pointer.down(location), location); TestGesture gesture = tester.startGesture(location);
tester.pump(); tester.pump();
tester.dispatchEvent(pointer.up(), location); gesture.up();
tester.pump(); tester.pump();
}); });
}); });
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
/// Testing library for flutter, built on top of package:test. /// Testing library for flutter, built on top of package:test.
library flutter_test; library flutter_test;
export 'src/instrumentation.dart';
export 'src/service_mocker.dart'; export 'src/service_mocker.dart';
export 'src/test_pointer.dart'; export 'src/test_pointer.dart';
export 'src/widget_tester.dart'; export 'src/widget_tester.dart';
...@@ -161,6 +161,14 @@ class Instrumentation { ...@@ -161,6 +161,14 @@ class Instrumentation {
_dispatchEvent(p.up(), result); _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) { void dispatchEvent(PointerEvent event, Point location) {
_dispatchEvent(event, _hitTest(location)); _dispatchEvent(event, _hitTest(location));
} }
...@@ -175,3 +183,34 @@ class Instrumentation { ...@@ -175,3 +183,34 @@ class Instrumentation {
binding.dispatchEvent(event, result); 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