snap_scrolling_test.dart 3.7 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 27 28 29 30 31 32 33 34 35 36 37 38 39 40
class TestScrollConfigurationDelegate extends ScrollConfigurationDelegate {
  const TestScrollConfigurationDelegate();

  // Not testing platform-specific fling scrolling, use the default fling
  // decleration simulation.
  @override
  TargetPlatform get platform => null;

  @override
  ExtentScrollBehavior createScrollBehavior() => new OverscrollWhenScrollableBehavior(platform: platform);

  @override
  bool updateShouldNotify(ScrollConfigurationDelegate old) => false;
}

41
Widget buildFrame() {
42
  scrollableListKey = new GlobalKey();
43 44 45 46 47 48 49 50 51 52 53 54
  return new ScrollConfiguration(
    delegate: const TestScrollConfigurationDelegate(),
    child: new Center(
      child: new Container(
        height: itemExtent * 2.0,
        child: new ScrollableList(
          scrollableKey: scrollableListKey,
          snapOffsetCallback: snapOffsetCallback,
          scrollDirection: scrollDirection,
          itemExtent: itemExtent,
          children: <int>[0, 1, 2, 3, 4, 5, 7, 8, 9].map(buildItem)
        )
55
      )
56 57 58 59 60 61 62
    )
  );
}

ScrollableState get scrollableState => scrollableListKey.currentState;

double get scrollOffset =>  scrollableState.scrollOffset;
63
set scrollOffset(double value) {
64 65 66
  scrollableState.scrollTo(value);
}

67 68 69 70
Completer<Null> fling(double velocity) {
  Completer<Null> completer = new Completer<Null>();
  scrollableState.fling(velocity).whenComplete(completer.complete);
  return completer;
71 72 73
}

void main() {
74
  testWidgets('ScrollableList snap scrolling', (WidgetTester tester) async {
75
    await tester.pumpWidget(buildFrame());
76 77

    scrollOffset = 0.0;
78
    await tester.pump();
79 80 81 82
    expect(scrollOffset, 0.0);

    Duration dt = const Duration(seconds: 2);

83 84
    Completer<Null> completer = fling(1000.0);
    expect(completer.isCompleted, isFalse);
85 86
    await tester.pump(); // Start the scheduler at 0.0
    await tester.pump(dt);
87
    expect(scrollOffset, closeTo(200.0, 1.0));
88
    expect(completer.isCompleted, isTrue);
89 90

    scrollOffset = 0.0;
91
    await tester.pump();
92 93
    expect(scrollOffset, 0.0);

94 95
    completer = fling(2000.0);
    expect(completer.isCompleted, isFalse);
96 97
    await tester.pump();
    await tester.pump(dt);
98
    expect(scrollOffset, closeTo(400.0, 1.0));
99
    expect(completer.isCompleted, isTrue);
100 101

    scrollOffset = 400.0;
102
    await tester.pump();
103 104
    expect(scrollOffset, 400.0);

105 106
    completer = fling(-800.0);
    expect(completer.isCompleted, isFalse);
107 108
    await tester.pump();
    await tester.pump(dt);
109
    expect(scrollOffset, closeTo(0.0, 1.0));
110
    expect(completer.isCompleted, isTrue);
111 112

    scrollOffset = 800.0;
113
    await tester.pump();
114 115
    expect(scrollOffset, 800.0);

116 117
    completer = fling(-2000.0);
    expect(completer.isCompleted, isFalse);
118 119
    await tester.pump();
    await tester.pump(dt);
120
    expect(scrollOffset, closeTo(200.0, 1.0));
121
    expect(completer.isCompleted, isTrue);
122 123

    scrollOffset = 800.0;
124
    await tester.pump();
125 126
    expect(scrollOffset, 800.0);

127 128
    completer = fling(-2000.0);
    expect(completer.isCompleted, isFalse);
129 130
    await tester.pump();
    await tester.pump(dt);
131 132
    expect(completer.isCompleted, isTrue);
    expectSync(scrollOffset, closeTo(200.0, 1.0));
133
  });
134
}