Merge pull request #1415 from HansMuller/snap_scrolling_tests

Snap scrolling: additional tests, cleanup
This commit is contained in:
Hans Muller
2015-09-30 15:06:45 -07:00
3 changed files with 95 additions and 51 deletions

View File

@@ -147,8 +147,9 @@ abstract class ScrollableState<T extends Scrollable> extends State<T> {
if (!endScrollOffset.isNaN) {
double alignedScrollOffset = _alignedScrollSnapOffset(endScrollOffset);
if (_scrollOffsetIsInBounds(alignedScrollOffset)) {
double snapVelocity = velocity.abs() * (alignedScrollOffset - scrollOffset).sign;
Simulation toSnapSimulation = scrollBehavior.createSnapScrollSimulation(
scrollOffset, alignedScrollOffset, velocity);
scrollOffset, alignedScrollOffset, snapVelocity);
_toEndAnimation.start(toSnapSimulation);
return;
}

View File

@@ -25,17 +25,19 @@ double snapOffsetCallback(double offset) {
return (offset / itemExtent).floor() * itemExtent;
}
Widget buildScrollableList() {
Widget buildFrame() {
scrollableListKey = new GlobalKey();
return new Container(
height: itemExtent * 2.0,
child: new ScrollableList<int>(
key: scrollableListKey,
snapOffsetCallback: snapOffsetCallback,
scrollDirection: scrollDirection,
items: [0, 1, 2, 3, 4, 5, 7, 8, 9],
itemBuilder: buildItem,
itemExtent: itemExtent
return new Center(
child: new Container(
height: itemExtent * 2.0,
child: new ScrollableList<int>(
key: scrollableListKey,
snapOffsetCallback: snapOffsetCallback,
scrollDirection: scrollDirection,
items: [0, 1, 2, 3, 4, 5, 7, 8, 9],
itemBuilder: buildItem,
itemExtent: itemExtent
)
)
);
}
@@ -55,33 +57,71 @@ void fling(double velocity) {
}
void main() {
test('ScrollableList snap scrolling, fling(-800)', () {
WidgetTester tester = new WidgetTester();
WidgetTester tester = new WidgetTester();
tester.pumpFrame(buildFrame());
tester.pumpFrame(new Center(child: buildScrollableList()));
test('ScrollableList snap scrolling, fling(-800)', () {
scrollOffset = 0.0;
tester.pumpFrameWithoutChange();
expect(scrollOffset, 0.0);
double t0 = 0.0;
int dt = 1000;
new FakeAsync().run((async) {
fling(-800.0);
tester.pumpFrameWithoutChange(); // Start the scheduler at 0.0
tester.pumpFrameWithoutChange(1000.0);
async.elapse(new Duration(seconds: 1));
tester.pumpFrameWithoutChange(t0); // Start the scheduler at 0.0
tester.pumpFrameWithoutChange(t0 + dt);
async.elapse(new Duration(milliseconds: dt));
expect(scrollOffset, closeTo(200.0, 1.0));
});
});
test('ScrollableList snap scrolling, fling(-2000)', () {
WidgetTester tester = new WidgetTester();
tester.pumpFrame(new Center(child: buildScrollableList()));
scrollOffset = 0.0;
tester.pumpFrameWithoutChange();
expect(scrollOffset, 0.0);
double t0 = 0.0;
int dt = 1000;
new FakeAsync().run((async) {
fling(-2000.0);
tester.pumpFrameWithoutChange(); // Start the scheduler at 0.0
tester.pumpFrameWithoutChange(1000.0);
async.elapse(new Duration(seconds: 1));
tester.pumpFrameWithoutChange(t0); // Start the scheduler at 0.0
tester.pumpFrameWithoutChange(t0 + dt);
async.elapse(new Duration(milliseconds: dt));
expect(scrollOffset, closeTo(400.0, 1.0));
});
});
test('ScrollableList snap scrolling, fling(800)', () {
scrollOffset = 400.0;
tester.pumpFrameWithoutChange(1000.0);
expect(scrollOffset, 400.0);
double t0 = 0.0;
int dt = 2000;
new FakeAsync().run((async) {
fling(800.0);
tester.pumpFrameWithoutChange(t0); // Start the scheduler at 0.0
tester.pumpFrameWithoutChange(t0 + dt);
async.elapse(new Duration(milliseconds: dt));
expect(scrollOffset, closeTo(0.0, 1.0));
});
});
test('ScrollableList snap scrolling, fling(2000)', () {
scrollOffset = 800.0;
tester.pumpFrameWithoutChange(1000.0);
expect(scrollOffset, 800.0);
double t0 = 0.0;
int dt = 1500;
new FakeAsync().run((async) {
fling(2000.0);
tester.pumpFrameWithoutChange(t0); // Start the scheduler at 0.0
tester.pumpFrameWithoutChange(t0 + dt);
async.elapse(new Duration(milliseconds: dt));
expect(scrollOffset, closeTo(200.0, 1.0));
});
});
}