snap_scrolling_test.dart 2.63 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
  return new Container(
    width: itemExtent,
    height: itemExtent,
    child: new Text(item.toString())
  );
}

23
double snapOffsetCallback(double offset, Size size) {
24 25 26
  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
  return scrollableState.fling(velocity);
52 53 54
}

void main() {
55
  test('ScrollableList snap scrolling, fling(0.8)', () {
56 57 58 59 60 61 62 63
    testWidgets((WidgetTester tester) {
      tester.pumpWidget(buildFrame());

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

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

65
      fling(0.8);
66 67
      tester.pump(); // Start the scheduler at 0.0
      tester.pump(dt);
68 69
      expect(scrollOffset, closeTo(200.0, 1.0));

70 71 72
      scrollOffset = 0.0;
      tester.pump();
      expect(scrollOffset, 0.0);
73

74
      fling(2.0);
75 76
      tester.pump();
      tester.pump(dt);
77
      expect(scrollOffset, closeTo(400.0, 1.0));
78

79 80 81
      scrollOffset = 400.0;
      tester.pump();
      expect(scrollOffset, 400.0);
82

83
      fling(-0.8);
84 85
      tester.pump();
      tester.pump(dt);
86 87
      expect(scrollOffset, closeTo(0.0, 1.0));

88 89 90
      scrollOffset = 800.0;
      tester.pump();
      expect(scrollOffset, 800.0);
91

92
      fling(-2.0);
93 94
      tester.pump();
      tester.pump(dt);
95 96
      expect(scrollOffset, closeTo(200.0, 1.0));

97 98 99
      scrollOffset = 800.0;
      tester.pump();
      expect(scrollOffset, 800.0);
100

101
      bool completed = false;
102
      fling(-2.0).then((_) {
103 104 105
        completed = true;
        expect(scrollOffset, closeTo(200.0, 1.0));
      });
106 107
      tester.pump();
      tester.pump(dt);
108 109 110
      expect(completed, true);
    });
  });
111
}