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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright 2014 The Flutter 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/material.dart';
import 'package:flutter/gestures.dart' show DragStartBehavior;
const TextStyle testFont = TextStyle(
color: Color(0xFF00FF00),
fontFamily: 'Ahem',
);
Future<void> pumpTest(WidgetTester tester, TargetPlatform platform) async {
await tester.pumpWidget(Container());
await tester.pumpWidget(MaterialApp(
theme: ThemeData(
platform: platform,
),
home: Container(
color: const Color(0xFF111111),
child: ListView.builder(
dragStartBehavior: DragStartBehavior.down,
itemBuilder: (BuildContext context, int index) {
return Text('$index', style: testFont);
},
),
),
));
}
const double dragOffset = 213.82;
void main() {
testWidgets('Flings on different platforms', (WidgetTester tester) async {
double getCurrentOffset() {
return tester.state<ScrollableState>(find.byType(Scrollable)).position.pixels;
}
await pumpTest(tester, TargetPlatform.android);
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 androidResult = getCurrentOffset();
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();
await pumpTest(tester, TargetPlatform.iOS);
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 iOSResult = getCurrentOffset();
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
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));
});
testWidgets('fling and tap to stop', (WidgetTester tester) async {
final List<String> log = <String>[];
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: ListView(
dragStartBehavior: DragStartBehavior.down,
children: List<Widget>.generate(250, (int i) => GestureDetector(
onTap: () { log.add('tap $i'); },
child: Text('$i', style: testFont),
)),
),
),
);
expect(log, equals(<String>[]));
await tester.tap(find.byType(Scrollable));
await tester.pump(const Duration(milliseconds: 50));
expect(log, equals(<String>['tap 21']));
await tester.fling(find.byType(Scrollable), const Offset(0.0, -200.0), 1000.0);
await tester.pump(const Duration(milliseconds: 50));
expect(log, equals(<String>['tap 21']));
await tester.tap(find.byType(Scrollable)); // should stop the fling but not tap anything
await tester.pump(const Duration(milliseconds: 50));
expect(log, equals(<String>['tap 21']));
await tester.tap(find.byType(Scrollable));
await tester.pump(const Duration(milliseconds: 50));
expect(log, equals(<String>['tap 21', 'tap 35']));
});
testWidgets('fling and wait and tap', (WidgetTester tester) async {
final List<String> log = <String>[];
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: ListView(
dragStartBehavior: DragStartBehavior.down,
children: List<Widget>.generate(250, (int i) => GestureDetector(
onTap: () { log.add('tap $i'); },
child: Text('$i', style: testFont),
)),
),
),
);
expect(log, equals(<String>[]));
await tester.tap(find.byType(Scrollable));
await tester.pump(const Duration(milliseconds: 50));
expect(log, equals(<String>['tap 21']));
await tester.fling(find.byType(Scrollable), const Offset(0.0, -200.0), 1000.0);
await tester.pump(const Duration(milliseconds: 50));
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']));
await tester.tap(find.byType(Scrollable));
await tester.pump(const Duration(milliseconds: 50));
expect(log, equals(<String>['tap 21', 'tap 48']));
});
}