snap_scrolling_test.dart 2.86 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';

7
import 'package:flutter/widgets.dart';
8 9
import 'package:test/test.dart';

Adam Barth's avatar
Adam Barth committed
10
import 'widget_tester.dart';
11 12 13 14 15

const double itemExtent = 200.0;
ScrollDirection scrollDirection = ScrollDirection.vertical;
GlobalKey scrollableListKey;

16
Widget buildItem(BuildContext context, int item, int index) {
17 18 19 20 21 22 23 24 25 26 27 28
  return new Container(
    key: new ValueKey<int>(item),
    width: itemExtent,
    height: itemExtent,
    child: new Text(item.toString())
  );
}

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

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

ScrollableState get scrollableState => scrollableListKey.currentState;

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

53
Future fling(double velocity) {
54 55 56
  Offset velocityOffset = scrollDirection == ScrollDirection.vertical
    ? new Offset(0.0, velocity)
    : new Offset(velocity, 0.0);
57
  return scrollableState.fling(velocityOffset);
58 59 60
}

void main() {
61
  test('ScrollableList snap scrolling, fling(-800)', () {
62 63 64 65 66 67 68 69
    testWidgets((WidgetTester tester) {
      tester.pumpWidget(buildFrame());

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

      Duration dt = const Duration(seconds: 2);
70 71

      fling(-800.0);
72 73
      tester.pump(); // Start the scheduler at 0.0
      tester.pump(dt);
74 75
      expect(scrollOffset, closeTo(200.0, 1.0));

76 77 78
      scrollOffset = 0.0;
      tester.pump();
      expect(scrollOffset, 0.0);
79 80

      fling(-2000.0);
81 82
      tester.pump();
      tester.pump(dt);
83
      expect(scrollOffset, closeTo(400.0, 1.0));
84

85 86 87
      scrollOffset = 400.0;
      tester.pump();
      expect(scrollOffset, 400.0);
88 89

      fling(800.0);
90 91
      tester.pump();
      tester.pump(dt);
92 93
      expect(scrollOffset, closeTo(0.0, 1.0));

94 95 96
      scrollOffset = 800.0;
      tester.pump();
      expect(scrollOffset, 800.0);
97 98

      fling(2000.0);
99 100
      tester.pump();
      tester.pump(dt);
101 102
      expect(scrollOffset, closeTo(200.0, 1.0));

103 104 105
      scrollOffset = 800.0;
      tester.pump();
      expect(scrollOffset, 800.0);
106

107
      bool completed = false;
108 109 110 111
      fling(2000.0).then((_) {
        completed = true;
        expect(scrollOffset, closeTo(200.0, 1.0));
      });
112 113
      tester.pump();
      tester.pump(dt);
114 115 116
      expect(completed, true);
    });
  });
117
}