snap_scrolling_test.dart 2.75 KB
Newer Older
1 2 3 4
// Copyright 2015 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.

5 6
import 'dart:async';

Adam Barth's avatar
Adam Barth committed
7
import 'package:flutter_test/flutter_test.dart';
8
import 'package:flutter/widgets.dart';
9 10 11
import 'package:test/test.dart';

const double itemExtent = 200.0;
12
Axis scrollDirection = Axis.vertical;
13 14
GlobalKey scrollableListKey;

15
Widget buildItem(int item) {
16 17 18 19 20 21 22 23 24 25 26
  return new Container(
    width: itemExtent,
    height: itemExtent,
    child: new Text(item.toString())
  );
}

double snapOffsetCallback(double offset) {
  return (offset / itemExtent).floor() * itemExtent;
}

27
Widget buildFrame() {
28
  scrollableListKey = new GlobalKey();
29 30 31
  return new Center(
    child: new Container(
      height: itemExtent * 2.0,
32
      child: new ScrollableList(
33 34 35
        key: scrollableListKey,
        snapOffsetCallback: snapOffsetCallback,
        scrollDirection: scrollDirection,
36 37
        itemExtent: itemExtent,
        children: <int>[0, 1, 2, 3, 4, 5, 7, 8, 9].map(buildItem)
38
      )
39 40 41 42 43 44 45 46 47 48 49
    )
  );
}

ScrollableState get scrollableState => scrollableListKey.currentState;

double get scrollOffset =>  scrollableState.scrollOffset;
void set scrollOffset(double value) {
  scrollableState.scrollTo(value);
}

50
Future fling(double velocity) {
51
  Offset velocityOffset = scrollDirection == Axis.vertical
52 53
    ? new Offset(0.0, velocity)
    : new Offset(velocity, 0.0);
54
  return scrollableState.fling(velocityOffset);
55 56 57
}

void main() {
Hans Muller's avatar
Hans Muller committed
58
  test('ScrollableList snap scrolling, fling(-0.8)', () {
59 60 61 62 63 64 65 66
    testWidgets((WidgetTester tester) {
      tester.pumpWidget(buildFrame());

      scrollOffset = 0.0;
      tester.pump();
      expect(scrollOffset, 0.0);

      Duration dt = const Duration(seconds: 2);
67

Hans Muller's avatar
Hans Muller committed
68
      fling(-0.8);
69 70
      tester.pump(); // Start the scheduler at 0.0
      tester.pump(dt);
71 72
      expect(scrollOffset, closeTo(200.0, 1.0));

73 74 75
      scrollOffset = 0.0;
      tester.pump();
      expect(scrollOffset, 0.0);
76

Hans Muller's avatar
Hans Muller committed
77
      fling(-2.0);
78 79
      tester.pump();
      tester.pump(dt);
80
      expect(scrollOffset, closeTo(400.0, 1.0));
81

82 83 84
      scrollOffset = 400.0;
      tester.pump();
      expect(scrollOffset, 400.0);
85

Hans Muller's avatar
Hans Muller committed
86
      fling(0.8);
87 88
      tester.pump();
      tester.pump(dt);
89 90
      expect(scrollOffset, closeTo(0.0, 1.0));

91 92 93
      scrollOffset = 800.0;
      tester.pump();
      expect(scrollOffset, 800.0);
94

Hans Muller's avatar
Hans Muller committed
95
      fling(2.0);
96 97
      tester.pump();
      tester.pump(dt);
98 99
      expect(scrollOffset, closeTo(200.0, 1.0));

100 101 102
      scrollOffset = 800.0;
      tester.pump();
      expect(scrollOffset, 800.0);
103

104
      bool completed = false;
Hans Muller's avatar
Hans Muller committed
105
      fling(2.0).then((_) {
106 107 108
        completed = true;
        expect(scrollOffset, closeTo(200.0, 1.0));
      });
109 110
      tester.pump();
      tester.pump(dt);
111 112 113
      expect(completed, true);
    });
  });
114
}