diff --git a/examples/stocks/test/icon_color_test.dart b/examples/stocks/test/icon_color_test.dart index 9afd3b1590..b3aed5cf4c 100644 --- a/examples/stocks/test/icon_color_test.dart +++ b/examples/stocks/test/icon_color_test.dart @@ -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); diff --git a/packages/flutter/test/widget/block_test.dart b/packages/flutter/test/widget/block_test.dart index ac2de5b236..57b61c659b 100644 --- a/packages/flutter/test/widget/block_test.dart +++ b/packages/flutter/test/widget/block_test.dart @@ -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(); }); }); diff --git a/packages/flutter/test/widget/dismissable_test.dart b/packages/flutter/test/widget/dismissable_test.dart index d56a89e361..5d43ad8f54 100644 --- a/packages/flutter/test/widget/dismissable_test.dart +++ b/packages/flutter/test/widget/dismissable_test.dart @@ -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(); }); }); diff --git a/packages/flutter/test/widget/draggable_test.dart b/packages/flutter/test/widget/draggable_test.dart index 9bd3c31406..603b326712 100644 --- a/packages/flutter/test/widget/draggable_test.dart +++ b/packages/flutter/test/widget/draggable_test.dart @@ -9,8 +9,6 @@ import 'package:test/test.dart'; void main() { test('Drag and drop - control test', () { testWidgets((WidgetTester tester) { - TestPointer pointer = new TestPointer(7); - List accepted = []; tester.pumpWidget(new MaterialApp( @@ -44,7 +42,7 @@ void main() { expect(tester.findText('Target'), isNotNull); Point firstLocation = tester.getCenter(tester.findText('Source')); - tester.dispatchEvent(pointer.down(firstLocation), firstLocation); + TestGesture gesture = tester.startGesture(firstLocation, pointer: 7); tester.pump(); expect(accepted, isEmpty); @@ -53,7 +51,7 @@ void main() { expect(tester.findText('Target'), isNotNull); Point secondLocation = tester.getCenter(tester.findText('Target')); - tester.dispatchEvent(pointer.move(secondLocation), firstLocation); + gesture.moveTo(secondLocation); tester.pump(); expect(accepted, isEmpty); @@ -61,7 +59,7 @@ void main() { expect(tester.findText('Dragging'), isNotNull); expect(tester.findText('Target'), isNotNull); - tester.dispatchEvent(pointer.up(), firstLocation); + gesture.up(); tester.pump(); expect(accepted, equals([1])); @@ -73,8 +71,6 @@ void main() { test('Drag and drop - dragging over button', () { testWidgets((WidgetTester tester) { - TestPointer pointer = new TestPointer(7); - List events = []; Point firstLocation, secondLocation; @@ -138,15 +134,15 @@ void main() { // drag and drop firstLocation = tester.getCenter(tester.findText('Source')); - tester.dispatchEvent(pointer.down(firstLocation), firstLocation); + TestGesture gesture = tester.startGesture(firstLocation, pointer: 7); tester.pump(); secondLocation = tester.getCenter(tester.findText('Target')); - tester.dispatchEvent(pointer.move(secondLocation), firstLocation); + gesture.moveTo(secondLocation); tester.pump(); expect(events, isEmpty); - tester.dispatchEvent(pointer.up(), firstLocation); + gesture.up(); tester.pump(); expect(events, equals(['drop'])); events.clear(); @@ -154,17 +150,17 @@ void main() { // drag and tap and drop firstLocation = tester.getCenter(tester.findText('Source')); - tester.dispatchEvent(pointer.down(firstLocation), firstLocation); + gesture = tester.startGesture(firstLocation, pointer: 7); tester.pump(); secondLocation = tester.getCenter(tester.findText('Target')); - tester.dispatchEvent(pointer.move(secondLocation), firstLocation); + gesture.moveTo(secondLocation); tester.pump(); expect(events, isEmpty); tester.tap(tester.findText('Button')); tester.tap(tester.findText('Target')); - tester.dispatchEvent(pointer.up(), firstLocation); + gesture.up(); tester.pump(); expect(events, equals(['tap', 'tap', 'drop'])); events.clear(); @@ -173,8 +169,6 @@ void main() { test('Drag and drop - tapping button', () { testWidgets((WidgetTester tester) { - TestPointer pointer = new TestPointer(7); - List events = []; Point firstLocation, secondLocation; @@ -218,15 +212,15 @@ void main() { events.clear(); firstLocation = tester.getCenter(tester.findText('Button')); - tester.dispatchEvent(pointer.down(firstLocation), firstLocation); + TestGesture gesture = tester.startGesture(firstLocation, pointer: 7); tester.pump(); secondLocation = tester.getCenter(tester.findText('Target')); - tester.dispatchEvent(pointer.move(secondLocation), firstLocation); + gesture.moveTo(secondLocation); tester.pump(); expect(events, isEmpty); - tester.dispatchEvent(pointer.up(), firstLocation); + gesture.up(); tester.pump(); expect(events, equals(['drop'])); events.clear(); @@ -236,8 +230,6 @@ void main() { test('Drag and drop - long press draggable, short press', () { testWidgets((WidgetTester tester) { - TestPointer pointer = new TestPointer(7); - List events = []; Point firstLocation, secondLocation; @@ -272,15 +264,15 @@ void main() { expect(events, isEmpty); firstLocation = tester.getCenter(tester.findText('Source')); - tester.dispatchEvent(pointer.down(firstLocation), firstLocation); + TestGesture gesture = tester.startGesture(firstLocation, pointer: 7); tester.pump(); secondLocation = tester.getCenter(tester.findText('Target')); - tester.dispatchEvent(pointer.move(secondLocation), firstLocation); + gesture.moveTo(secondLocation); tester.pump(); expect(events, isEmpty); - tester.dispatchEvent(pointer.up(), firstLocation); + gesture.up(); tester.pump(); expect(events, isEmpty); @@ -289,8 +281,6 @@ void main() { test('Drag and drop - long press draggable, long press', () { testWidgets((WidgetTester tester) { - TestPointer pointer = new TestPointer(7); - List events = []; Point firstLocation, secondLocation; @@ -325,17 +315,17 @@ void main() { expect(events, isEmpty); firstLocation = tester.getCenter(tester.findText('Source')); - tester.dispatchEvent(pointer.down(firstLocation), firstLocation); + TestGesture gesture = tester.startGesture(firstLocation, pointer: 7); tester.pump(); tester.pump(const Duration(seconds: 20)); secondLocation = tester.getCenter(tester.findText('Target')); - tester.dispatchEvent(pointer.move(secondLocation), firstLocation); + gesture.moveTo(secondLocation); tester.pump(); expect(events, isEmpty); - tester.dispatchEvent(pointer.up(), firstLocation); + gesture.up(); tester.pump(); expect(events, equals(['drop'])); }); @@ -343,8 +333,6 @@ void main() { test('Drag and drop - horizontal and vertical draggables in vertical block', () { testWidgets((WidgetTester tester) { - TestPointer pointer = new TestPointer(7); - List events = []; Point firstLocation, secondLocation, thirdLocation; @@ -391,11 +379,11 @@ void main() { expect(events, isEmpty); firstLocation = tester.getCenter(tester.findText('V')); secondLocation = tester.getCenter(tester.findText('Target')); - tester.dispatchEvent(pointer.down(firstLocation), firstLocation); + TestGesture gesture = tester.startGesture(firstLocation, pointer: 7); tester.pump(); - tester.dispatchEvent(pointer.move(secondLocation), firstLocation); + gesture.moveTo(secondLocation); tester.pump(); - tester.dispatchEvent(pointer.up(), firstLocation); + gesture.up(); tester.pump(); expect(events, equals(['drop 2'])); expect(tester.getCenter(tester.findText('Target')).y, greaterThan(0.0)); @@ -406,13 +394,13 @@ void main() { firstLocation = tester.getTopLeft(tester.findText('H')); secondLocation = tester.getTopRight(tester.findText('H')); thirdLocation = tester.getCenter(tester.findText('Target')); - tester.dispatchEvent(pointer.down(firstLocation), firstLocation); + gesture = tester.startGesture(firstLocation, pointer: 7); tester.pump(); - tester.dispatchEvent(pointer.move(secondLocation), firstLocation); + gesture.moveTo(secondLocation); tester.pump(); - tester.dispatchEvent(pointer.move(thirdLocation), firstLocation); + gesture.moveTo(thirdLocation); tester.pump(); - tester.dispatchEvent(pointer.up(), firstLocation); + gesture.up(); tester.pump(); expect(events, equals(['drop 1'])); expect(tester.getCenter(tester.findText('Target')).y, greaterThan(0.0)); @@ -424,13 +412,13 @@ void main() { firstLocation = tester.getTopLeft(tester.findText('V')); secondLocation = tester.getTopRight(tester.findText('V')); thirdLocation = tester.getCenter(tester.findText('Target')); - tester.dispatchEvent(pointer.down(firstLocation), firstLocation); + gesture = tester.startGesture(firstLocation, pointer: 7); tester.pump(); - tester.dispatchEvent(pointer.move(secondLocation), firstLocation); + gesture.moveTo(secondLocation); tester.pump(); - tester.dispatchEvent(pointer.move(thirdLocation), firstLocation); + gesture.moveTo(thirdLocation); tester.pump(); - tester.dispatchEvent(pointer.up(), firstLocation); + gesture.up(); tester.pump(); expect(events, equals(['drop 2'])); expect(tester.getCenter(tester.findText('Target')).y, greaterThan(0.0)); @@ -441,11 +429,11 @@ void main() { expect(events, isEmpty); firstLocation = tester.getCenter(tester.findText('H')); secondLocation = tester.getCenter(tester.findText('Target')); - tester.dispatchEvent(pointer.down(firstLocation), firstLocation); + gesture = tester.startGesture(firstLocation, pointer: 7); tester.pump(); - tester.dispatchEvent(pointer.move(secondLocation), firstLocation); + gesture.moveTo(secondLocation); tester.pump(); // scrolls off screen! - tester.dispatchEvent(pointer.up(), firstLocation); + gesture.up(); tester.pump(); expect(events, equals([])); expect(tester.getCenter(tester.findText('Target')).y, lessThan(0.0)); @@ -456,8 +444,6 @@ void main() { test('Drag and drop - horizontal and vertical draggables in horizontal block', () { testWidgets((WidgetTester tester) { - TestPointer pointer = new TestPointer(7); - List events = []; Point firstLocation, secondLocation, thirdLocation; @@ -505,11 +491,11 @@ void main() { expect(events, isEmpty); firstLocation = tester.getCenter(tester.findText('H')); secondLocation = tester.getCenter(tester.findText('Target')); - tester.dispatchEvent(pointer.down(firstLocation), firstLocation); + TestGesture gesture = tester.startGesture(firstLocation, pointer: 7); tester.pump(); - tester.dispatchEvent(pointer.move(secondLocation), firstLocation); + gesture.moveTo(secondLocation); tester.pump(); - tester.dispatchEvent(pointer.up(), firstLocation); + gesture.up(); tester.pump(); expect(events, equals(['drop 1'])); expect(tester.getCenter(tester.findText('Target')).x, greaterThan(0.0)); @@ -520,13 +506,13 @@ void main() { firstLocation = tester.getTopLeft(tester.findText('V')); secondLocation = tester.getBottomLeft(tester.findText('V')); thirdLocation = tester.getCenter(tester.findText('Target')); - tester.dispatchEvent(pointer.down(firstLocation), firstLocation); + gesture = tester.startGesture(firstLocation, pointer: 7); tester.pump(); - tester.dispatchEvent(pointer.move(secondLocation), firstLocation); + gesture.moveTo(secondLocation); tester.pump(); - tester.dispatchEvent(pointer.move(thirdLocation), firstLocation); + gesture.moveTo(thirdLocation); tester.pump(); - tester.dispatchEvent(pointer.up(), firstLocation); + gesture.up(); tester.pump(); expect(events, equals(['drop 2'])); expect(tester.getCenter(tester.findText('Target')).x, greaterThan(0.0)); @@ -538,13 +524,13 @@ void main() { firstLocation = tester.getTopLeft(tester.findText('H')); secondLocation = tester.getBottomLeft(tester.findText('H')); thirdLocation = tester.getCenter(tester.findText('Target')); - tester.dispatchEvent(pointer.down(firstLocation), firstLocation); + gesture = tester.startGesture(firstLocation, pointer: 7); tester.pump(); - tester.dispatchEvent(pointer.move(secondLocation), firstLocation); + gesture.moveTo(secondLocation); tester.pump(); - tester.dispatchEvent(pointer.move(thirdLocation), firstLocation); + gesture.moveTo(thirdLocation); tester.pump(); - tester.dispatchEvent(pointer.up(), firstLocation); + gesture.up(); tester.pump(); expect(events, equals(['drop 1'])); expect(tester.getCenter(tester.findText('Target')).x, greaterThan(0.0)); @@ -555,11 +541,11 @@ void main() { expect(events, isEmpty); firstLocation = tester.getCenter(tester.findText('V')); secondLocation = tester.getCenter(tester.findText('Target')); - tester.dispatchEvent(pointer.down(firstLocation), firstLocation); + gesture = tester.startGesture(firstLocation, pointer: 7); tester.pump(); - tester.dispatchEvent(pointer.move(secondLocation), firstLocation); + gesture.moveTo(secondLocation); tester.pump(); // scrolls off screen! - tester.dispatchEvent(pointer.up(), firstLocation); + gesture.up(); tester.pump(); expect(events, equals([])); expect(tester.getCenter(tester.findText('Target')).x, lessThan(0.0)); diff --git a/packages/flutter/test/widget/gesture_detector_test.dart b/packages/flutter/test/widget/gesture_detector_test.dart index 47fe00d3cb..f7502c4f35 100644 --- a/packages/flutter/test/widget/gesture_detector_test.dart +++ b/packages/flutter/test/widget/gesture_detector_test.dart @@ -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); diff --git a/packages/flutter/test/widget/set_state_test.dart b/packages/flutter/test/widget/set_state_test.dart index 2c78e7e92e..16f45a057f 100644 --- a/packages/flutter/test/widget/set_state_test.dart +++ b/packages/flutter/test/widget/set_state_test.dart @@ -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(); }); }); diff --git a/packages/flutter_test/lib/flutter_test.dart b/packages/flutter_test/lib/flutter_test.dart index 83326f9d6b..27db964486 100644 --- a/packages/flutter_test/lib/flutter_test.dart +++ b/packages/flutter_test/lib/flutter_test.dart @@ -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'; diff --git a/packages/flutter_test/lib/src/instrumentation.dart b/packages/flutter_test/lib/src/instrumentation.dart index 1c00ba00ad..f31716fa72 100644 --- a/packages/flutter_test/lib/src/instrumentation.dart +++ b/packages/flutter_test/lib/src/instrumentation.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); + } +}