snap_scrolling_test.dart 3.12 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

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

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

22
double snapOffsetCallback(double offset, Size size) {
23 24 25
  return (offset / itemExtent).floor() * itemExtent;
}

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

ScrollableState get scrollableState => scrollableListKey.currentState;

double get scrollOffset =>  scrollableState.scrollOffset;
45
set scrollOffset(double value) {
46 47 48
  scrollableState.scrollTo(value);
}

49 50 51 52
Completer<Null> fling(double velocity) {
  Completer<Null> completer = new Completer<Null>();
  scrollableState.fling(velocity).whenComplete(completer.complete);
  return completer;
53 54 55
}

void main() {
56
  testWidgets('ScrollableList snap scrolling', (WidgetTester tester) async {
57
    await tester.pumpWidget(buildFrame());
58 59

    scrollOffset = 0.0;
60
    await tester.pump();
61 62 63 64
    expect(scrollOffset, 0.0);

    Duration dt = const Duration(seconds: 2);

65 66
    Completer<Null> completer = fling(1000.0);
    expect(completer.isCompleted, isFalse);
67 68
    await tester.pump(); // Start the scheduler at 0.0
    await tester.pump(dt);
69
    expect(scrollOffset, closeTo(200.0, 1.0));
70
    expect(completer.isCompleted, isTrue);
71 72

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

76 77
    completer = fling(2000.0);
    expect(completer.isCompleted, isFalse);
78 79
    await tester.pump();
    await tester.pump(dt);
80
    expect(scrollOffset, closeTo(400.0, 1.0));
81
    expect(completer.isCompleted, isTrue);
82 83

    scrollOffset = 400.0;
84
    await tester.pump();
85 86
    expect(scrollOffset, 400.0);

87 88
    completer = fling(-800.0);
    expect(completer.isCompleted, isFalse);
89 90
    await tester.pump();
    await tester.pump(dt);
91
    expect(scrollOffset, closeTo(0.0, 1.0));
92
    expect(completer.isCompleted, isTrue);
93 94

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

98 99
    completer = fling(-2000.0);
    expect(completer.isCompleted, isFalse);
100 101
    await tester.pump();
    await tester.pump(dt);
102
    expect(scrollOffset, closeTo(200.0, 1.0));
103
    expect(completer.isCompleted, isTrue);
104 105

    scrollOffset = 800.0;
106
    await tester.pump();
107 108
    expect(scrollOffset, 800.0);

109 110
    completer = fling(-2000.0);
    expect(completer.isCompleted, isFalse);
111 112
    await tester.pump();
    await tester.pump(dt);
113 114
    expect(completer.isCompleted, isTrue);
    expectSync(scrollOffset, closeTo(200.0, 1.0));
115
  });
116
}