Merge pull request #930 from abarth/scroll
Add scroll gestures and use them in Scrollable
This commit is contained in:
91
packages/unit/test/gestures/scroll_test.dart
Normal file
91
packages/unit/test/gestures/scroll_test.dart
Normal file
@@ -0,0 +1,91 @@
|
||||
import 'dart:sky' as sky;
|
||||
|
||||
import 'package:sky/base/pointer_router.dart';
|
||||
import 'package:sky/gestures/scroll.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
import '../engine/mock_events.dart';
|
||||
|
||||
TestPointerEvent down = new TestPointerEvent(
|
||||
pointer: 5,
|
||||
type: 'pointerdown',
|
||||
x: 10.0,
|
||||
y: 10.0
|
||||
);
|
||||
|
||||
TestPointerEvent move1 = new TestPointerEvent(
|
||||
pointer: 5,
|
||||
type: 'pointermove',
|
||||
x: 20.0,
|
||||
y: 20.0,
|
||||
dx: 10.0,
|
||||
dy: 10.0
|
||||
);
|
||||
|
||||
TestPointerEvent move2 = new TestPointerEvent(
|
||||
pointer: 5,
|
||||
type: 'pointermove',
|
||||
x: 20.0,
|
||||
y: 25.0,
|
||||
dx: 0.0,
|
||||
dy: 5.0
|
||||
);
|
||||
|
||||
TestPointerEvent up = new TestPointerEvent(
|
||||
pointer: 5,
|
||||
type: 'pointerup',
|
||||
x: 20.0,
|
||||
y: 25.0
|
||||
);
|
||||
|
||||
void main() {
|
||||
test('Should recognize scroll', () {
|
||||
PointerRouter router = new PointerRouter();
|
||||
ScrollGestureRecognizer scroll = new ScrollGestureRecognizer(router: router);
|
||||
|
||||
sky.Offset startOffset;
|
||||
scroll.onScrollStart = (sky.Offset offset) {
|
||||
startOffset = offset;
|
||||
};
|
||||
|
||||
sky.Offset updateOffset;
|
||||
scroll.onScrollUpdate = (sky.Offset offset) {
|
||||
updateOffset = offset;
|
||||
};
|
||||
|
||||
bool didEndScroll = false;
|
||||
scroll.onScrollEnd = () {
|
||||
didEndScroll = true;
|
||||
};
|
||||
|
||||
scroll.addPointer(down);
|
||||
expect(startOffset, isNull);
|
||||
expect(updateOffset, isNull);
|
||||
expect(didEndScroll, isFalse);
|
||||
|
||||
router.handleEvent(down, null);
|
||||
expect(startOffset, isNull);
|
||||
expect(updateOffset, isNull);
|
||||
expect(didEndScroll, isFalse);
|
||||
|
||||
router.handleEvent(move1, null);
|
||||
expect(startOffset, new sky.Offset(10.0, -10.0));
|
||||
startOffset = null;
|
||||
expect(updateOffset, isNull);
|
||||
expect(didEndScroll, isFalse);
|
||||
|
||||
router.handleEvent(move2, null);
|
||||
expect(startOffset, isNull);
|
||||
expect(updateOffset, new sky.Offset(0.0, -5.0));
|
||||
updateOffset = null;
|
||||
expect(didEndScroll, isFalse);
|
||||
|
||||
router.handleEvent(up, null);
|
||||
expect(startOffset, isNull);
|
||||
expect(updateOffset, isNull);
|
||||
expect(didEndScroll, isTrue);
|
||||
didEndScroll = false;
|
||||
|
||||
scroll.dispose();
|
||||
});
|
||||
}
|
||||
@@ -83,26 +83,39 @@ class WidgetTester {
|
||||
return box.localToGlobal(box.size.center(Point.origin));
|
||||
}
|
||||
|
||||
HitTestResult _hitTest(Point location) => SkyBinding.instance.hitTest(location);
|
||||
|
||||
EventDisposition _dispatchEvent(sky.Event event, HitTestResult result) {
|
||||
return SkyBinding.instance.dispatchEvent(event, result);
|
||||
}
|
||||
|
||||
void tap(Widget widget) {
|
||||
Point location = getCenter(widget);
|
||||
dispatchEvent(new TestPointerEvent(type: 'pointerdown', x: location.x, y: location.y), location);
|
||||
dispatchEvent(new TestPointerEvent(type: 'pointerup', x: location.x, y: location.y), location);
|
||||
HitTestResult result = _hitTest(location);
|
||||
_dispatchEvent(new TestPointerEvent(type: 'pointerdown', x: location.x, y: location.y), result);
|
||||
_dispatchEvent(new TestPointerEvent(type: 'pointerup', x: location.x, y: location.y), result);
|
||||
}
|
||||
|
||||
void scroll(Widget widget, Offset offset) {
|
||||
dispatchEvent(new TestGestureEvent(type: 'gesturescrollstart'), getCenter(widget));
|
||||
dispatchEvent(new TestGestureEvent(
|
||||
type: 'gesturescrollupdate',
|
||||
dx: offset.dx,
|
||||
dy: offset.dy), getCenter(widget));
|
||||
// pointerup to trigger scroll settling in Scrollable<T>
|
||||
dispatchEvent(new TestPointerEvent(
|
||||
type: 'pointerup', down: false, primary: true), getCenter(widget));
|
||||
Point startLocation = getCenter(widget);
|
||||
HitTestResult result = _hitTest(startLocation);
|
||||
_dispatchEvent(new TestPointerEvent(type: 'pointerdown', x: startLocation.x, y: startLocation.y), result);
|
||||
Point endLocation = startLocation + offset;
|
||||
_dispatchEvent(
|
||||
new TestPointerEvent(
|
||||
type: 'pointermove',
|
||||
x: endLocation.x,
|
||||
y: endLocation.y,
|
||||
dx: offset.dx,
|
||||
dy: offset.dy
|
||||
),
|
||||
result
|
||||
);
|
||||
_dispatchEvent(new TestPointerEvent(type: 'pointerup', x: endLocation.x, y: endLocation.y), result);
|
||||
}
|
||||
|
||||
void dispatchEvent(sky.Event event, Point position) {
|
||||
HitTestResult result = SkyBinding.instance.hitTest(position);
|
||||
SkyBinding.instance.dispatchEvent(event, result);
|
||||
void dispatchEvent(sky.Event event, Point location) {
|
||||
_dispatchEvent(event, _hitTest(location));
|
||||
}
|
||||
|
||||
void pumpFrame(WidgetBuilder builder, [double frameTimeMs = 0.0]) {
|
||||
|
||||
Reference in New Issue
Block a user