scroll_events_test.dart 6.64 KB
Newer Older
1 2 3 4 5 6 7 8 9
// 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.

import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/widgets.dart';

Widget _buildScroller({Key key, List<String> log}) {
  return new ScrollableViewport(
10
    scrollableKey: key,
11 12 13 14 15 16 17 18 19 20 21 22 23 24
    onScrollStart: (double scrollOffset) {
      log.add('scrollstart');
    },
    onScroll: (double scrollOffset) {
      log.add('scroll');
    },
    onScrollEnd: (double scrollOffset) {
      log.add('scrollend');
    },
    child: new Container(width: 1000.0, height: 1000.0)
  );
}

void main() {
25
  testWidgets('Scroll event drag', (WidgetTester tester) async {
26
    List<String> log = <String>[];
27
    await tester.pumpWidget(_buildScroller(log: log));
28

29
    expect(log, equals(<String>[]));
30
    TestGesture gesture = await tester.startGesture(new Point(100.0, 100.0));
31
    expect(log, equals(<String>['scrollstart']));
32
    await tester.pump(const Duration(seconds: 1));
33
    expect(log, equals(<String>['scrollstart']));
34
    await gesture.moveBy(new Offset(-10.0, -10.0));
35
    expect(log, equals(<String>['scrollstart', 'scroll']));
36
    await tester.pump(const Duration(seconds: 1));
37
    expect(log, equals(<String>['scrollstart', 'scroll']));
38
    await gesture.up();
39
    expect(log, equals(<String>['scrollstart', 'scroll']));
40
    await tester.pump(const Duration(seconds: 1));
41
    expect(log, equals(<String>['scrollstart', 'scroll', 'scrollend']));
42 43
  });

44
  testWidgets('Scroll scrollTo animation', (WidgetTester tester) async {
45 46
    GlobalKey<ScrollableState<Scrollable>> scrollKey = new GlobalKey<ScrollableState<Scrollable>>();
    List<String> log = <String>[];
47
    await tester.pumpWidget(_buildScroller(key: scrollKey, log: log));
48

49
    expect(log, equals(<String>[]));
50
    scrollKey.currentState.scrollTo(100.0, duration: const Duration(seconds: 1));
51
    expect(log, equals(<String>['scrollstart']));
52
    await tester.pump(const Duration(milliseconds: 100));
53
    expect(log, equals(<String>['scrollstart']));
54
    await tester.pump(const Duration(milliseconds: 100));
55
    expect(log, equals(<String>['scrollstart', 'scroll']));
56
    await tester.pump(const Duration(milliseconds: 1500));
57
    expect(log, equals(<String>['scrollstart', 'scroll', 'scroll', 'scrollend']));
58 59
  });

60
  testWidgets('Scroll scrollTo no animation', (WidgetTester tester) async {
61 62
    GlobalKey<ScrollableState<Scrollable>> scrollKey = new GlobalKey<ScrollableState<Scrollable>>();
    List<String> log = <String>[];
63
    await tester.pumpWidget(_buildScroller(key: scrollKey, log: log));
64

65
    expect(log, equals(<String>[]));
66
    scrollKey.currentState.scrollTo(100.0);
67
    expect(log, equals(<String>['scrollstart', 'scroll', 'scrollend']));
68 69
  });

70
  testWidgets('Scroll during animation', (WidgetTester tester) async {
71 72
    GlobalKey<ScrollableState<Scrollable>> scrollKey = new GlobalKey<ScrollableState<Scrollable>>();
    List<String> log = <String>[];
73
    await tester.pumpWidget(_buildScroller(key: scrollKey, log: log));
74

75
    expect(log, equals(<String>[]));
76
    scrollKey.currentState.scrollTo(100.0, duration: const Duration(seconds: 1));
77
    expect(log, equals(<String>['scrollstart']));
78
    await tester.pump(const Duration(milliseconds: 100));
79
    expect(log, equals(<String>['scrollstart']));
80
    await tester.pump(const Duration(milliseconds: 100));
81
    expect(log, equals(<String>['scrollstart', 'scroll']));
82
    scrollKey.currentState.scrollTo(100.0);
83
    expect(log, equals(<String>['scrollstart', 'scroll', 'scroll']));
84
    await tester.pump(const Duration(milliseconds: 100));
85
    expect(log, equals(<String>['scrollstart', 'scroll', 'scroll', 'scrollend']));
86
    await tester.pump(const Duration(milliseconds: 1500));
87
    expect(log, equals(<String>['scrollstart', 'scroll', 'scroll', 'scrollend']));
88
  });
89

90
  testWidgets('Scroll during animation', (WidgetTester tester) async {
91 92
    GlobalKey<ScrollableState<Scrollable>> scrollKey = new GlobalKey<ScrollableState<Scrollable>>();
    List<String> log = <String>[];
93
    await tester.pumpWidget(_buildScroller(key: scrollKey, log: log));
94

95
    expect(log, equals(<String>[]));
96
    scrollKey.currentState.scrollTo(100.0, duration: const Duration(seconds: 1));
97
    expect(log, equals(<String>['scrollstart']));
98
    await tester.pump(const Duration(milliseconds: 100));
99
    expect(log, equals(<String>['scrollstart']));
100
    await tester.pump(const Duration(milliseconds: 100));
101
    expect(log, equals(<String>['scrollstart', 'scroll']));
102
    scrollKey.currentState.scrollTo(100.0, duration: const Duration(seconds: 1));
103
    expect(log, equals(<String>['scrollstart', 'scroll']));
104
    await tester.pump(const Duration(milliseconds: 100));
105
    expect(log, equals(<String>['scrollstart', 'scroll']));
106
    await tester.pump(const Duration(milliseconds: 1500));
107
    expect(log, equals(<String>['scrollstart', 'scroll', 'scroll', 'scrollend']));
108 109
  });

110
  testWidgets('fling, fling generates two start/end pairs', (WidgetTester tester) async {
111 112
    GlobalKey<ScrollableState<Scrollable>> scrollKey = new GlobalKey<ScrollableState<Scrollable>>();
    List<String> log = <String>[];
113
    await tester.pumpWidget(_buildScroller(key: scrollKey, log: log));
114

115
    expect(log, equals(<String>[]));
116 117
    await tester.flingFrom(new Point(100.0, 100.0), new Offset(-50.0, -50.0), 500.0);
    await tester.pump(new Duration(seconds: 1));
118
    log.removeWhere((String value) => value == 'scroll');
119
    expect(log, equals(<String>['scrollstart']));
120
    await tester.flingFrom(new Point(100.0, 100.0), new Offset(-50.0, -50.0), 500.0);
121
    log.removeWhere((String value) => value == 'scroll');
122
    expect(log, equals(<String>['scrollstart', 'scrollend', 'scrollstart']));
123 124
    await tester.pump(new Duration(seconds: 1));
    await tester.pump(new Duration(seconds: 1));
125
    log.removeWhere((String value) => value == 'scroll');
126
    expect(log, equals(<String>['scrollstart', 'scrollend', 'scrollstart', 'scrollend']));
127
  });
128

129
  testWidgets('fling up ends', (WidgetTester tester) async {
130 131
    GlobalKey<ScrollableState<Scrollable>> scrollKey = new GlobalKey<ScrollableState<Scrollable>>();
    List<String> log = <String>[];
132
    await tester.pumpWidget(_buildScroller(key: scrollKey, log: log));
133

134
    expect(log, equals(<String>[]));
135 136 137 138
    await tester.flingFrom(new Point(100.0, 100.0), new Offset(50.0, 50.0), 500.0);
    await tester.pump(new Duration(seconds: 1));
    await tester.pump(new Duration(seconds: 1));
    await tester.pump(new Duration(seconds: 1));
139 140 141 142 143
    expect(log.first, equals('scrollstart'));
    expect(log.last, equals('scrollend'));
    log.removeWhere((String value) => value == 'scroll');
    expect(log.length, equals(2));
    expect(scrollKey.currentState.scrollOffset, equals(0.0));
144
  });
145
}