scrollable_fling_test.dart 6.18 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6
// 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/material.dart';
7
import 'package:flutter/gestures.dart' show DragStartBehavior;
8

9 10
const TextStyle testFont = TextStyle(
  color: Color(0xFF00FF00),
11 12 13
  fontFamily: 'Ahem',
);

14
Future<void> pumpTest(WidgetTester tester, TargetPlatform platform) async {
15 16 17
  await tester.pumpWidget(Container());
  await tester.pumpWidget(MaterialApp(
    theme: ThemeData(
18
      platform: platform,
19
    ),
20
    home: Container(
21
      color: const Color(0xFF111111),
22
      child: ListView.builder(
23
        dragStartBehavior: DragStartBehavior.down,
24
        itemBuilder: (BuildContext context, int index) {
25
          return Text('$index', style: testFont);
26 27
        },
      ),
28 29 30 31 32 33 34 35
    ),
  ));
}

const double dragOffset = 213.82;

void main() {
  testWidgets('Flings on different platforms', (WidgetTester tester) async {
36
    double getCurrentOffset() {
Adam Barth's avatar
Adam Barth committed
37
      return tester.state<ScrollableState>(find.byType(Scrollable)).position.pixels;
38 39
    }

40
    await pumpTest(tester, TargetPlatform.android);
41 42
    await tester.fling(find.byType(ListView), const Offset(0.0, -dragOffset), 1000.0);
    expect(getCurrentOffset(), dragOffset);
43
    await tester.pump(); // trigger fling
44
    expect(getCurrentOffset(), dragOffset);
45
    await tester.pump(const Duration(seconds: 5));
Dan Field's avatar
Dan Field committed
46
    final double androidResult = getCurrentOffset();
47

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
    await pumpTest(tester, TargetPlatform.linux);
    await tester.fling(find.byType(ListView), const Offset(0.0, -dragOffset), 1000.0);
    expect(getCurrentOffset(), dragOffset);
    await tester.pump(); // trigger fling
    expect(getCurrentOffset(), dragOffset);
    await tester.pump(const Duration(seconds: 5));
    final double linuxResult = getCurrentOffset();

    await pumpTest(tester, TargetPlatform.windows);
    await tester.fling(find.byType(ListView), const Offset(0.0, -dragOffset), 1000.0);
    expect(getCurrentOffset(), dragOffset);
    await tester.pump(); // trigger fling
    expect(getCurrentOffset(), dragOffset);
    await tester.pump(const Duration(seconds: 5));
    final double windowsResult = getCurrentOffset();

64
    await pumpTest(tester, TargetPlatform.iOS);
65
    await tester.fling(find.byType(ListView), const Offset(0.0, -dragOffset), 1000.0);
66 67
    // Scroll starts ease into the scroll on iOS.
    expect(getCurrentOffset(), moreOrLessEquals(210.71026666666666));
68
    await tester.pump(); // trigger fling
69
    expect(getCurrentOffset(), moreOrLessEquals(210.71026666666666));
70
    await tester.pump(const Duration(seconds: 5));
Dan Field's avatar
Dan Field committed
71
    final double iOSResult = getCurrentOffset();
72

Dan Field's avatar
Dan Field committed
73 74 75 76 77 78 79 80 81 82 83
    await pumpTest(tester, TargetPlatform.macOS);
    await tester.fling(find.byType(ListView), const Offset(0.0, -dragOffset), 1000.0);
    // Scroll starts ease into the scroll on iOS.
    expect(getCurrentOffset(), moreOrLessEquals(210.71026666666666));
    await tester.pump(); // trigger fling
    expect(getCurrentOffset(), moreOrLessEquals(210.71026666666666));
    await tester.pump(const Duration(seconds: 5));
    final double macOSResult = getCurrentOffset();

    expect(androidResult, lessThan(iOSResult)); // iOS is slipperier than Android
    expect(androidResult, lessThan(macOSResult)); // macOS is slipperier than Android
84 85 86 87 88 89 90 91
    expect(linuxResult, lessThan(iOSResult)); // iOS is slipperier than Linux
    expect(linuxResult, lessThan(macOSResult)); // macOS is slipperier than Linux
    expect(windowsResult, lessThan(iOSResult)); // iOS is slipperier than Windows
    expect(windowsResult, lessThan(macOSResult)); // macOS is slipperier than Windows
    expect(windowsResult, equals(androidResult));
    expect(windowsResult, equals(androidResult));
    expect(linuxResult, equals(androidResult));
    expect(linuxResult, equals(androidResult));
92
  });
93 94

  testWidgets('fling and tap to stop', (WidgetTester tester) async {
95
    final List<String> log = <String>[];
96
    await tester.pumpWidget(
97
      Directionality(
98
        textDirection: TextDirection.ltr,
99 100 101 102 103 104 105
        child: ListView(
          dragStartBehavior: DragStartBehavior.down,
          children: List<Widget>.generate(250, (int i) => GestureDetector(
            onTap: () { log.add('tap $i'); },
            child: Text('$i', style: testFont),
          )),
        ),
106 107
      ),
    );
108 109

    expect(log, equals(<String>[]));
Adam Barth's avatar
Adam Barth committed
110
    await tester.tap(find.byType(Scrollable));
111
    await tester.pump(const Duration(milliseconds: 50));
112
    expect(log, equals(<String>['tap 21']));
Adam Barth's avatar
Adam Barth committed
113
    await tester.fling(find.byType(Scrollable), const Offset(0.0, -200.0), 1000.0);
114
    await tester.pump(const Duration(milliseconds: 50));
115 116
    expect(log, equals(<String>['tap 21']));
    await tester.tap(find.byType(Scrollable)); // should stop the fling but not tap anything
117
    await tester.pump(const Duration(milliseconds: 50));
118
    expect(log, equals(<String>['tap 21']));
Adam Barth's avatar
Adam Barth committed
119
    await tester.tap(find.byType(Scrollable));
120
    await tester.pump(const Duration(milliseconds: 50));
121
    expect(log, equals(<String>['tap 21', 'tap 35']));
122
  });
123 124

  testWidgets('fling and wait and tap', (WidgetTester tester) async {
125
    final List<String> log = <String>[];
126
    await tester.pumpWidget(
127
      Directionality(
128
        textDirection: TextDirection.ltr,
129 130 131 132 133 134 135
        child: ListView(
          dragStartBehavior: DragStartBehavior.down,
          children: List<Widget>.generate(250, (int i) => GestureDetector(
            onTap: () { log.add('tap $i'); },
            child: Text('$i', style: testFont),
          )),
        ),
136 137
      ),
    );
138 139

    expect(log, equals(<String>[]));
Adam Barth's avatar
Adam Barth committed
140
    await tester.tap(find.byType(Scrollable));
141
    await tester.pump(const Duration(milliseconds: 50));
142
    expect(log, equals(<String>['tap 21']));
Adam Barth's avatar
Adam Barth committed
143
    await tester.fling(find.byType(Scrollable), const Offset(0.0, -200.0), 1000.0);
144
    await tester.pump(const Duration(milliseconds: 50));
145 146 147
    expect(log, equals(<String>['tap 21']));
    await tester.pump(const Duration(seconds: 50)); // long wait, so the fling will have ended at the end of it
    expect(log, equals(<String>['tap 21']));
Adam Barth's avatar
Adam Barth committed
148
    await tester.tap(find.byType(Scrollable));
149
    await tester.pump(const Duration(milliseconds: 50));
150
    expect(log, equals(<String>['tap 21', 'tap 48']));
151
  });
152
}