Breaks the moveBy call from drag and dragFrom into two separate calls and changes the default behavior of DragStartBehavior to DragStartBehavior.start (#26438)

* Breaking change which sets the default DragStartBehavior to DragStartBehavior.start and changes WidgetTester.drag and WidgetTester.dragFrom by breaking the moveBy function call into multiple pieces.
This commit is contained in:
jslavitz
2019-02-25 14:15:31 -08:00
committed by GitHub
parent 62e55a45d4
commit 47724f97fa
31 changed files with 532 additions and 111 deletions

View File

@@ -13,6 +13,12 @@ import 'finders.dart';
import 'test_async_utils.dart';
import 'test_pointer.dart';
/// The default drag touch slop used to break up a large drag into multiple
/// smaller moves.
///
/// This value must be greater than [kTouchSlop].
const double kDragSlopDefault = 20.0;
/// Class that programmatically interacts with widgets.
///
/// For a variant of this class suited specifically for unit tests, see
@@ -410,8 +416,27 @@ abstract class WidgetController {
///
/// 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<void> drag(Finder finder, Offset offset, {int pointer}) {
return dragFrom(getCenter(finder), offset, pointer: pointer);
///
/// {@template flutter.flutter_test.drag}
/// By default, if the x or y component of offset is greater than [kTouchSlop], the
/// gesture is broken up into two separate moves calls. Changing 'touchSlopX' or
/// `touchSlopY` will change the minimum amount of movement in the respective axis
/// before the drag will be broken into multiple calls. To always send the
/// drag with just a single call to [TestGesture.moveBy], `touchSlopX` and `touchSlopY`
/// should be set to 0.
///
/// Breaking the drag into multiple moves is necessary for accurate execution
/// of drag update calls with a [DragStartBehavior] variable set to
/// [DragStartBehavior.start]. Without such a change, the dragUpdate callback
/// from a drag recognizer will never be invoked.
///
/// To force this function to a send a single move event, the 'touchSlopX' and
/// 'touchSlopY' variables should be set to 0. However, generally, these values
/// should be left to their default values.
/// {@end template}
Future<void> drag(Finder finder, Offset offset, { int pointer, double touchSlopX = kDragSlopDefault, double touchSlopY = kDragSlopDefault }) {
assert(kDragSlopDefault > kTouchSlop);
return dragFrom(getCenter(finder), offset, pointer: pointer, touchSlopX: touchSlopX, touchSlopY: touchSlopY);
}
/// Attempts a drag gesture consisting of a pointer down, a move by
@@ -420,11 +445,78 @@ abstract class WidgetController {
/// 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<void> dragFrom(Offset startLocation, Offset offset, {int pointer}) {
///
/// {@macro flutter.flutter_test.drag}
Future<void> dragFrom(Offset startLocation, Offset offset, { int pointer, double touchSlopX = kDragSlopDefault, double touchSlopY = kDragSlopDefault }) {
assert(kDragSlopDefault > kTouchSlop);
return TestAsyncUtils.guard<void>(() async {
final TestGesture gesture = await startGesture(startLocation, pointer: pointer);
assert(gesture != null);
await gesture.moveBy(offset);
final double xSign = offset.dx.sign;
final double ySign = offset.dy.sign;
final double offsetX = offset.dx;
final double offsetY = offset.dy;
final bool separateX = offset.dx.abs() > touchSlopX && touchSlopX > 0;
final bool separateY = offset.dy.abs() > touchSlopY && touchSlopY > 0;
if (separateY || separateX) {
final double offsetSlope = offsetY / offsetX;
final double inverseOffsetSlope = offsetX / offsetY;
final double slopSlope = touchSlopY / touchSlopX;
final double absoluteOffsetSlope = offsetSlope.abs();
final double signedSlopX = touchSlopX * xSign;
final double signedSlopY = touchSlopY * ySign;
if (absoluteOffsetSlope != slopSlope) {
// The drag goes through one or both of the extents of the edges of the box.
if (absoluteOffsetSlope < slopSlope) {
assert(offsetX.abs() > touchSlopX);
// The drag goes through the vertical edge of the box.
// It is guaranteed that the |offsetX| > touchSlopX.
final double diffY = offsetSlope.abs() * touchSlopX * ySign;
// The vector from the origin to the vertical edge.
await gesture.moveBy(Offset(signedSlopX, diffY));
if (offsetY.abs() <= touchSlopY) {
// The drag ends on or before getting to the horizontal extension of the horizontal edge.
await gesture.moveBy(Offset(offsetX - signedSlopX, offsetY - diffY));
} else {
final double diffY2 = signedSlopY - diffY;
final double diffX2 = inverseOffsetSlope * diffY2;
// The vector from the edge of the box to the horizontal extension of the horizontal edge.
await gesture.moveBy(Offset(diffX2, diffY2));
await gesture.moveBy(Offset(offsetX - diffX2 - signedSlopX, offsetY - signedSlopY));
}
} else {
assert(offsetY.abs() > touchSlopY);
// The drag goes through the horizontal edge of the box.
// It is guaranteed that the |offsetY| > touchSlopY.
final double diffX = inverseOffsetSlope.abs() * touchSlopY * xSign;
// The vector from the origin to the vertical edge.
await gesture.moveBy(Offset(diffX, signedSlopY));
if (offsetX.abs() <= touchSlopX) {
// The drag ends on or before getting to the vertical extension of the vertical edge.
await gesture.moveBy(Offset(offsetX - diffX, offsetY - signedSlopY));
} else {
final double diffX2 = signedSlopX - diffX;
final double diffY2 = offsetSlope * diffX2;
// The vector from the edge of the box to the vertical extension of the vertical edge.
await gesture.moveBy(Offset(diffX2, diffY2));
await gesture.moveBy(Offset(offsetX - signedSlopX, offsetY - diffY2 - signedSlopY));
}
}
} else { // The drag goes through the corner of the box.
await gesture.moveBy(Offset(signedSlopX, signedSlopY));
await gesture.moveBy(Offset(offsetX - signedSlopX, offsetY - signedSlopY));
}
} else { // The drag ends inside the box.
await gesture.moveBy(offset);
}
await gesture.up();
});
}

View File

@@ -0,0 +1,282 @@
// Copyright 2016 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 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/gestures.dart';
void main() {
testWidgets(
'WidgetTester.drag must break the offset into multiple parallel components if'
'the drag goes outside the touch slop values',
(WidgetTester tester) async {
// The first Offset in every sub array (ie. offsetResults[i][0]) is (touchSlopX, touchSlopY).
// The second Offset in every sub array (ie. offsetResults[i][1]) will be the total move offset.
// The remaining values in every sub array are the expected separated drag offsets.
// This test checks to make sure that the total drag will be correctly split into
// pieces such that the first (and potentially second) moveBy function call(s) in
// controller.drag() will never have a component greater than the touch
// slop in that component's respective axis.
final List<List<Offset>> offsetResults = <List<Offset>>[
<Offset>[
const Offset(10.0, 10.0),
const Offset(-150.0, 200.0),
const Offset(-7.5, 10.0),
const Offset(-2.5, 3.333333333333333),
const Offset(-140.0, 186.66666666666666),
],
<Offset>[
const Offset(10.0, 10.0),
const Offset(150, -200),
const Offset(7.5, -10),
const Offset(2.5, -3.333333333333333),
const Offset(140.0, -186.66666666666666),
],
<Offset>[
const Offset(10.0, 10.0),
const Offset(-200, 150),
const Offset(-10, 7.5),
const Offset(-3.333333333333333, 2.5),
const Offset(-186.66666666666666, 140.0),
],
<Offset>[
const Offset(10.0, 10.0),
const Offset(200.0, -150.0),
const Offset(10, -7.5),
const Offset(3.333333333333333, -2.5),
const Offset(186.66666666666666, -140.0),
],
<Offset>[
const Offset(10.0, 10.0),
const Offset(-150.0, -200.0),
const Offset(-7.5, -10.0),
const Offset(-2.5, -3.333333333333333),
const Offset(-140.0, -186.66666666666666),
],
<Offset>[
const Offset(10.0, 10.0),
const Offset(8.0, 3.0),
const Offset(8.0, 3.0),
],
<Offset>[
const Offset(10.0, 10.0),
const Offset(3.0, 8.0),
const Offset(3.0, 8.0),
],
<Offset>[
const Offset(10.0, 10.0),
const Offset(20.0, 5.0),
const Offset(10.0, 2.5),
const Offset(10.0, 2.5),
],
<Offset>[
const Offset(10.0, 10.0),
const Offset(5.0, 20.0),
const Offset(2.5, 10.0),
const Offset(2.5, 10.0),
],
<Offset>[
const Offset(10.0, 10.0),
const Offset(20.0, 15.0),
const Offset(10.0, 7.5),
const Offset(3.333333333333333, 2.5),
const Offset(6.666666666666668, 5.0),
],
<Offset>[
const Offset(10.0, 10.0),
const Offset(15.0, 20.0),
const Offset(7.5, 10.0),
const Offset(2.5, 3.333333333333333),
const Offset(5.0, 6.666666666666668),
],
<Offset>[
const Offset(10.0, 10.0),
const Offset(20.0, 20.0),
const Offset(10.0, 10.0),
const Offset(10.0, 10.0),
],
<Offset>[
const Offset(10.0, 10.0),
const Offset(0.0, 5.0),
const Offset(0.0, 5.0),
],
//// [VARYING TOUCH SLOP] ////
<Offset>[
const Offset(12.0, 5.0),
const Offset(0.0, 5.0),
const Offset(0.0, 5.0),
],
<Offset>[
const Offset(12.0, 5.0),
const Offset(20.0, 5.0),
const Offset(12.0, 3.0),
const Offset(8.0, 2.0),
],
<Offset>[
const Offset(12.0, 5.0),
const Offset(5.0, 20.0),
const Offset(1.25, 5.0),
const Offset(3.75, 15.0),
],
<Offset>[
const Offset(5.0, 12.0),
const Offset(5.0, 20.0),
const Offset(3.0, 12.0),
const Offset(2.0, 8.0),
],
<Offset>[
const Offset(5.0, 12.0),
const Offset(20.0, 5.0),
const Offset(5.0, 1.25),
const Offset(15.0, 3.75),
],
<Offset>[
const Offset(18.0, 18.0),
const Offset(0.0, 150.0),
const Offset(0.0, 18.0),
const Offset(0.0, 132.0),
],
<Offset>[
const Offset(18.0, 18.0),
const Offset(0.0, -150.0),
const Offset(0.0, -18.0),
const Offset(0.0, -132.0),
],
<Offset>[
const Offset(18.0, 18.0),
const Offset(-150.0, 0.0),
const Offset(-18.0, 0.0),
const Offset(-132.0, 0.0),
],
<Offset>[
const Offset(0.0, 0.0),
const Offset(-150.0, 0.0),
const Offset(-150.0, 0.0),
],
<Offset>[
const Offset(18.0, 18.0),
const Offset(-32.0, 0.0),
const Offset(-18.0, 0.0),
const Offset(-14.0, 0.0),
],
];
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: Text('test'),
),
);
final WidgetControllerSpy spyController = WidgetControllerSpy(tester.binding);
for (int resultIndex = 0; resultIndex < offsetResults.length; resultIndex += 1) {
final List<Offset> testResult = offsetResults[resultIndex];
await spyController.drag(
find.text('test'),
testResult[1],
touchSlopX: testResult[0].dx,
touchSlopY: testResult[0].dy,
);
final List<Offset> dragOffsets = spyController.testGestureSpy.offsets;
expect(
offsetResults[resultIndex].length - 2,
dragOffsets.length,
reason:
'There is a difference in the number of expected and actual split offsets for the drag with:\n'
'Touch Slop: ' + testResult[0].toString() + '\n'
'Delta: ' + testResult[1].toString() + '\n',
);
for (int valueIndex = 2; valueIndex < offsetResults[resultIndex].length; valueIndex += 1) {
expect(
offsetResults[resultIndex][valueIndex],
dragOffsets[valueIndex - 2],
reason:
'There is a difference in the expected and actual value of the ' +
(valueIndex == 2 ? 'first' : valueIndex == 3 ? 'second' : 'third') +
' split offset for the drag with:\n'
'Touch slop: ' + testResult[0].toString() + '\n'
'Delta: ' + testResult[1].toString() + '\n',
);
}
spyController.testGestureSpy.clearOffsets();
}
},
);
}
class WidgetControllerSpy extends WidgetController {
WidgetControllerSpy(
TestWidgetsFlutterBinding binding
) : super(binding) {
_binding = binding;
}
TestWidgetsFlutterBinding _binding;
@override
Future<void> pump(Duration duration) async {
if (duration != null)
await Future<void>.delayed(duration);
_binding.scheduleFrame();
await _binding.endOfFrame;
}
int _getNextPointer() {
final int result = nextPointer;
nextPointer += 1;
return result;
}
@override
Future<void> sendEventToBinding(PointerEvent event, HitTestResult result) {
return TestAsyncUtils.guard<void>(() async {
_binding.dispatchEvent(event, result, source: TestBindingEventSource.test);
});
}
TestGestureSpy testGestureSpy;
@override
Future<TestGesture> createGesture({int pointer, PointerDeviceKind kind = PointerDeviceKind.touch}) async {
return testGestureSpy = TestGestureSpy(
pointer: pointer ?? _getNextPointer(),
kind: kind,
dispatcher: sendEventToBinding,
hitTester: hitTestOnBinding
);
}
}
class TestGestureSpy extends TestGesture {
TestGestureSpy({
int pointer,
PointerDeviceKind kind,
EventDispatcher dispatcher,
HitTester hitTester
}) : super(
pointer: pointer,
kind: kind,
dispatcher: dispatcher,
hitTester: hitTester
);
List<Offset> offsets = <Offset>[];
void clearOffsets() {
offsets = <Offset>[];
}
@override
Future<void> moveBy(Offset offset, {Duration timeStamp = Duration.zero}) {
offsets.add(offset);
return super.moveBy(offset, timeStamp: timeStamp);
}
}