scroll_events_test.dart 6.35 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
// 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';
import 'package:test/test.dart';

Widget _buildScroller({Key key, List<String> log}) {
  return new ScrollableViewport(
    key: key,
    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() {
  test('Scroll event drag', () {
    testWidgets((WidgetTester tester) {
      List<String> log = <String>[];
      tester.pumpWidget(_buildScroller(log: log));

      expect(log, equals([]));
      TestGesture gesture = tester.startGesture(new Point(100.0, 100.0));
      expect(log, equals(['scrollstart']));
      tester.pump(const Duration(seconds: 1));
      expect(log, equals(['scrollstart']));
      gesture.moveBy(new Offset(-10.0, -10.0));
      expect(log, equals(['scrollstart', 'scroll']));
      tester.pump(const Duration(seconds: 1));
      expect(log, equals(['scrollstart', 'scroll']));
      gesture.up();
      expect(log, equals(['scrollstart', 'scroll']));
      tester.pump(const Duration(seconds: 1));
      expect(log, equals(['scrollstart', 'scroll', 'scrollend']));
    });
  });

  test('Scroll scrollTo animation', () {
    testWidgets((WidgetTester tester) {
Hixie's avatar
Hixie committed
49
      GlobalKey<ScrollableState<Scrollable>> scrollKey = new GlobalKey<ScrollableState<Scrollable>>();
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
      List<String> log = <String>[];
      tester.pumpWidget(_buildScroller(key: scrollKey, log: log));

      expect(log, equals([]));
      scrollKey.currentState.scrollTo(100.0, duration: const Duration(seconds: 1));
      expect(log, equals(['scrollstart']));
      tester.pump(const Duration(milliseconds: 100));
      expect(log, equals(['scrollstart']));
      tester.pump(const Duration(milliseconds: 100));
      expect(log, equals(['scrollstart', 'scroll']));
      tester.pump(const Duration(milliseconds: 1500));
      expect(log, equals(['scrollstart', 'scroll', 'scroll', 'scrollend']));
    });
  });

  test('Scroll scrollTo no animation', () {
    testWidgets((WidgetTester tester) {
Hixie's avatar
Hixie committed
67
      GlobalKey<ScrollableState<Scrollable>> scrollKey = new GlobalKey<ScrollableState<Scrollable>>();
68 69 70 71 72 73 74 75 76 77 78
      List<String> log = <String>[];
      tester.pumpWidget(_buildScroller(key: scrollKey, log: log));

      expect(log, equals([]));
      scrollKey.currentState.scrollTo(100.0);
      expect(log, equals(['scrollstart', 'scroll', 'scrollend']));
    });
  });

  test('Scroll during animation', () {
    testWidgets((WidgetTester tester) {
Hixie's avatar
Hixie committed
79
      GlobalKey<ScrollableState<Scrollable>> scrollKey = new GlobalKey<ScrollableState<Scrollable>>();
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
      List<String> log = <String>[];
      tester.pumpWidget(_buildScroller(key: scrollKey, log: log));

      expect(log, equals([]));
      scrollKey.currentState.scrollTo(100.0, duration: const Duration(seconds: 1));
      expect(log, equals(['scrollstart']));
      tester.pump(const Duration(milliseconds: 100));
      expect(log, equals(['scrollstart']));
      tester.pump(const Duration(milliseconds: 100));
      expect(log, equals(['scrollstart', 'scroll']));
      scrollKey.currentState.scrollTo(100.0);
      expect(log, equals(['scrollstart', 'scroll', 'scroll']));
      tester.pump(const Duration(milliseconds: 100));
      expect(log, equals(['scrollstart', 'scroll', 'scroll', 'scrollend']));
      tester.pump(const Duration(milliseconds: 1500));
      expect(log, equals(['scrollstart', 'scroll', 'scroll', 'scrollend']));
    });
  });
98 99 100

  test('Scroll during animation', () {
    testWidgets((WidgetTester tester) {
Hixie's avatar
Hixie committed
101
      GlobalKey<ScrollableState<Scrollable>> scrollKey = new GlobalKey<ScrollableState<Scrollable>>();
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
      List<String> log = <String>[];
      tester.pumpWidget(_buildScroller(key: scrollKey, log: log));

      expect(log, equals([]));
      scrollKey.currentState.scrollTo(100.0, duration: const Duration(seconds: 1));
      expect(log, equals(['scrollstart']));
      tester.pump(const Duration(milliseconds: 100));
      expect(log, equals(['scrollstart']));
      tester.pump(const Duration(milliseconds: 100));
      expect(log, equals(['scrollstart', 'scroll']));
      scrollKey.currentState.scrollTo(100.0, duration: const Duration(seconds: 1));
      expect(log, equals(['scrollstart', 'scroll']));
      tester.pump(const Duration(milliseconds: 100));
      expect(log, equals(['scrollstart', 'scroll']));
      tester.pump(const Duration(milliseconds: 1500));
      expect(log, equals(['scrollstart', 'scroll', 'scroll', 'scrollend']));
    });
  });

  test('fling, fling generates one start/end pair', () {
    testWidgets((WidgetTester tester) {
Hixie's avatar
Hixie committed
123
      GlobalKey<ScrollableState<Scrollable>> scrollKey = new GlobalKey<ScrollableState<Scrollable>>();
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
      List<String> log = <String>[];
      tester.pumpWidget(_buildScroller(key: scrollKey, log: log));

      expect(log, equals([]));
      tester.flingFrom(new Point(100.0, 100.0), new Offset(-50.0, -50.0), 500.0);
      tester.pump(new Duration(seconds: 1));
      log.removeWhere((String value) => value == 'scroll');
      expect(log, equals(['scrollstart']));
      tester.flingFrom(new Point(100.0, 100.0), new Offset(-50.0, -50.0), 500.0);
      tester.pump(new Duration(seconds: 1));
      tester.pump(new Duration(seconds: 1));
      log.removeWhere((String value) => value == 'scroll');
      expect(log, equals(['scrollstart', 'scrollend']));
    });
  });
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157

  test('fling up ends', () {
    testWidgets((WidgetTester tester) {
      GlobalKey<ScrollableState<Scrollable>> scrollKey = new GlobalKey<ScrollableState<Scrollable>>();
      List<String> log = <String>[];
      tester.pumpWidget(_buildScroller(key: scrollKey, log: log));

      expect(log, equals([]));
      tester.flingFrom(new Point(100.0, 100.0), new Offset(50.0, 50.0), 500.0);
      tester.pump(new Duration(seconds: 1));
      tester.pump(new Duration(seconds: 1));
      tester.pump(new Duration(seconds: 1));
      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));
    });
  });
158
}