-
-
Notifications
You must be signed in to change notification settings - Fork 550
/
flutter_slidable_test.dart
475 lines (427 loc) · 14 KB
/
flutter_slidable_test.dart
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
const double itemExtent = 100.0;
const double actionExtentRatio = 0.2;
const int a0 = 0;
const int a1 = 1;
const int s0 = 10;
const int s1 = 11;
const List<int> allActions = const <int>[a0, a1, s0, s1];
const List<AxisDirection> axisDirections = const <AxisDirection>[
AxisDirection.right,
AxisDirection.left,
AxisDirection.down,
AxisDirection.up
];
const Size screenSize = const Size(800.0, 600.0);
SlideActionDelegate _buildActionDelegate(int index) {
return SlideActionListDelegate(
actions: <Widget>[
SlideAction(
key: new ValueKey(getSlideActionBaseKey(index) + a0),
child: const Text('a0'),
),
SlideAction(
key: new ValueKey(getSlideActionBaseKey(index) + a1),
child: const Text('a1'),
),
],
);
}
SlideActionDelegate _buildSecondaryActionDelegate(int index) {
return SlideActionListDelegate(
actions: <Widget>[
SlideAction(
key: new ValueKey(getSlideActionBaseKey(index) + s0),
child: const Text('s0'),
),
SlideAction(
key: new ValueKey(getSlideActionBaseKey(index) + s1),
child: const Text('s1'),
),
],
);
}
Widget buildTest(
SlidableDelegate delegate, {
TextDirection textDirection = TextDirection.ltr,
Axis scrollDirection = Axis.vertical,
}) {
return new Directionality(
textDirection: textDirection,
child: new StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
Widget buildSlidableWidget(int item) {
return new Slidable.builder(
key: new ValueKey(item),
delegate: delegate,
enabled: item != 3,
direction: flipAxis(scrollDirection),
actionExtentRatio: actionExtentRatio,
actionDelegate: _buildActionDelegate(item),
secondaryActionDelegate: _buildSecondaryActionDelegate(item),
child: new Container(
width: scrollDirection == Axis.horizontal
? itemExtent
: screenSize.width,
height: scrollDirection == Axis.horizontal
? screenSize.height
: itemExtent,
child: new Text('item $item'),
),
);
}
return new ListView(
scrollDirection: scrollDirection,
itemExtent: itemExtent,
children: List.generate(5, (int index) => buildSlidableWidget(index))
.toList(),
);
},
),
);
}
Offset getOffset(AxisDirection gestureDirection, double value) {
switch (gestureDirection) {
case AxisDirection.left:
return new Offset(-value, 0.0);
case AxisDirection.right:
return new Offset(value, 0.0);
case AxisDirection.up:
return new Offset(0.0, -value);
case AxisDirection.down:
return new Offset(0.0, value);
default:
fail('unsupported gestureDirection');
}
}
Future<Null> flingElement(
WidgetTester tester,
Finder finder, {
@required AxisDirection gestureDirection,
double initialOffsetFactor = 0.0,
}) async {
final double itemExtent =
axisDirectionToAxis(gestureDirection) == Axis.horizontal
? screenSize.width
: screenSize.height;
final Offset delta =
getOffset(gestureDirection, initialOffsetFactor * itemExtent);
await tester.fling(finder, delta, 1000.0);
}
Future<Null> dragElement(
WidgetTester tester,
Finder finder, {
@required AxisDirection gestureDirection,
double endOffsetFactor,
}) async {
final double itemExtent =
axisDirectionToAxis(gestureDirection) == Axis.horizontal
? screenSize.width
: screenSize.height;
final Offset delta =
getOffset(gestureDirection, endOffsetFactor * itemExtent);
await tester.drag(finder, delta);
}
Future<Null> dragItem(
WidgetTester tester,
int item, {
@required AxisDirection gestureDirection,
double endOffsetFactor,
}) async {
await dragElement(
tester,
find.text('item $item'),
gestureDirection: gestureDirection,
endOffsetFactor: endOffsetFactor,
);
await tester.pump(); // start the slide.
}
Future<Null> flingItem(
WidgetTester tester,
int item, {
@required AxisDirection gestureDirection,
double initialOffsetFactor = 0.0,
}) async {
await flingElement(tester, find.text('item $item'),
gestureDirection: gestureDirection,
initialOffsetFactor: initialOffsetFactor);
await tester.pump(); // start the slide.
await tester.pump(
const Duration(seconds: 1)); // finish the slide and start shrinking...
await tester.pump();
}
int getSlideActionBaseKey(int index) {
return (index + 1) * 100;
}
void checkActions(int index,
{List<int> visible = const <int>[], List<int> hidden = const <int>[]}) {
for (int key in visible) {
expect(find.byKey(new ValueKey(getSlideActionBaseKey(index) + key)),
findsOneWidget);
}
for (int key in hidden) {
expect(find.byKey(new ValueKey(getSlideActionBaseKey(index) + key)),
findsNothing);
}
}
void checkAction(
{@required int index,
@required int key,
@required WidgetTester tester,
@required AxisDirection gestureDirection,
@required double edgeRatio,
@required double extentRatio}) {
Finder finder = find.byKey(new ValueKey(getSlideActionBaseKey(index) + key));
double actualEdge;
double actualExtent;
final double fullExtent =
axisDirectionToAxis(gestureDirection) == Axis.horizontal
? screenSize.width
: screenSize.height;
double expectedEdge = fullExtent * edgeRatio;
double expectedExtent = fullExtent * extentRatio;
switch (gestureDirection) {
case AxisDirection.left:
actualEdge = screenSize.width - tester.getTopLeft(finder).dx;
actualExtent = tester.getSize(finder).width;
break;
case AxisDirection.right:
actualEdge = tester.getTopRight(finder).dx;
actualExtent = tester.getSize(finder).width;
break;
case AxisDirection.up:
actualEdge = screenSize.height - tester.getTopRight(finder).dy;
actualExtent = tester.getSize(finder).height;
break;
case AxisDirection.down:
actualEdge = tester.getBottomRight(finder).dy;
actualExtent = tester.getSize(finder).height;
break;
default:
fail('unsupported gestureDirection');
}
expect(actualEdge.roundToDouble(), expectedEdge.roundToDouble(),
reason: 'edges are not'
' equal');
expect(actualExtent.roundToDouble(), expectedExtent.roundToDouble(),
reason: 'exten'
'ts are not equal');
}
typedef List<_CheckActionValues> SlidableDelegateTestMethod(
AxisDirection direction);
void testSlidableDelegate(
SlidableDelegate delegate,
SlidableDelegateTestMethod slidableDelegateTestMethod,
double endOffsetFactor) {
final int index = 0;
axisDirections.forEach((direction) {
testSlidableDelegateScenario(
delegate,
index,
endOffsetFactor,
slidableDelegateTestMethod,
direction,
);
});
}
List<_CheckActionValues> getSlidableStrechDelegateHalfValues(
AxisDirection direction) {
final double extentRatio = actionExtentRatio / 2;
switch (direction) {
case AxisDirection.right:
return <_CheckActionValues>[
new _CheckActionValues(a0, .1, extentRatio),
new _CheckActionValues(a1, .2, extentRatio),
];
case AxisDirection.left:
return <_CheckActionValues>[
new _CheckActionValues(s0, .2, extentRatio),
new _CheckActionValues(s1, .1, extentRatio),
];
case AxisDirection.down:
return <_CheckActionValues>[
new _CheckActionValues(a0, .1, extentRatio),
new _CheckActionValues(a1, .2, extentRatio),
];
case AxisDirection.up:
return <_CheckActionValues>[
new _CheckActionValues(s0, .2, extentRatio),
new _CheckActionValues(s1, .1, extentRatio),
];
default:
return null;
}
}
List<_CheckActionValues> getSlidableBehindDelegateHalfValues(
AxisDirection direction) {
// All the actions are entirely built.
final double extentRatio = actionExtentRatio;
switch (direction) {
case AxisDirection.right:
return <_CheckActionValues>[
new _CheckActionValues(a0, actionExtentRatio, extentRatio),
new _CheckActionValues(a1, actionExtentRatio * 2, extentRatio),
];
case AxisDirection.left:
return <_CheckActionValues>[
new _CheckActionValues(s0, actionExtentRatio * 2, extentRatio),
new _CheckActionValues(s1, actionExtentRatio, extentRatio),
];
case AxisDirection.down:
return <_CheckActionValues>[
new _CheckActionValues(a0, actionExtentRatio, extentRatio),
new _CheckActionValues(a1, actionExtentRatio * 2, extentRatio),
];
case AxisDirection.up:
return <_CheckActionValues>[
new _CheckActionValues(s0, actionExtentRatio * 2, extentRatio),
new _CheckActionValues(s1, actionExtentRatio, extentRatio),
];
default:
return null;
}
}
List<_CheckActionValues> getSlidableScrollDelegateHalfValues(
AxisDirection direction) {
final double extentRatio = actionExtentRatio;
switch (direction) {
case AxisDirection.right:
return <_CheckActionValues>[
new _CheckActionValues(a0, .0, extentRatio),
new _CheckActionValues(a1, actionExtentRatio, extentRatio),
];
case AxisDirection.left:
return <_CheckActionValues>[
new _CheckActionValues(s0, actionExtentRatio, extentRatio),
new _CheckActionValues(s1, .0, extentRatio),
];
case AxisDirection.down:
return <_CheckActionValues>[
new _CheckActionValues(a0, .0, extentRatio),
new _CheckActionValues(a1, actionExtentRatio, extentRatio),
];
case AxisDirection.up:
return <_CheckActionValues>[
new _CheckActionValues(s0, actionExtentRatio, extentRatio),
new _CheckActionValues(s1, .0, extentRatio),
];
default:
return null;
}
}
List<_CheckActionValues> getSlidableDrawerDelegateHalfValues(
AxisDirection direction) {
final double extentRatio = actionExtentRatio;
switch (direction) {
case AxisDirection.right:
return <_CheckActionValues>[
new _CheckActionValues(a0, actionExtentRatio / 2, extentRatio),
new _CheckActionValues(a1, actionExtentRatio, extentRatio),
];
case AxisDirection.left:
return <_CheckActionValues>[
new _CheckActionValues(s0, actionExtentRatio, extentRatio),
new _CheckActionValues(s1, actionExtentRatio / 2, extentRatio),
];
case AxisDirection.down:
return <_CheckActionValues>[
new _CheckActionValues(a0, actionExtentRatio / 2, extentRatio),
new _CheckActionValues(a1, actionExtentRatio, extentRatio),
];
case AxisDirection.up:
return <_CheckActionValues>[
new _CheckActionValues(s0, actionExtentRatio, extentRatio),
new _CheckActionValues(s1, actionExtentRatio / 2, extentRatio),
];
default:
return null;
}
}
void testSlidableDelegateScenario(
SlidableDelegate delegate,
int index,
double endOffsetFactor,
SlidableDelegateTestMethod slidableDelegateTestMethod,
AxisDirection direction) {
final List<_CheckActionValues> values = slidableDelegateTestMethod(direction);
Axis scrollDirection = flipAxis(axisDirectionToAxis(direction));
testWidgets(
'Drag shows half of ${delegate.runtimeType}, scrollDirection=$scrollDirection, '
'gestureDirection=$direction', (WidgetTester tester) async {
await tester
.pumpWidget(buildTest(delegate, scrollDirection: scrollDirection));
checkActions(index, hidden: allActions);
await dragItem(
tester,
index,
gestureDirection: direction,
endOffsetFactor: endOffsetFactor,
);
checkActions(
index,
visible: values.map((v) => v.key).toList(),
hidden: allActions
.where((i) => !values.map((v) => v.key).contains(i))
.toList(),
);
values.forEach((v) {
checkAction(
index: index,
key: v.key,
tester: tester,
gestureDirection: direction,
edgeRatio: v.edgeRatio,
extentRatio: v.extentRatio,
);
});
await flingItem(
tester,
index,
gestureDirection: flipAxisDirection(direction),
initialOffsetFactor: endOffsetFactor,
);
checkActions(index, hidden: allActions);
});
}
class _CheckActionValues {
const _CheckActionValues(this.key, this.edgeRatio, this.extentRatio);
final int key;
final double extentRatio;
final double edgeRatio;
}
void main() {
setUp(() {});
// Tests all delegates dragging half of total action extents.
testSlidableDelegate(const SlidableStrechDelegate(),
getSlidableStrechDelegateHalfValues, actionExtentRatio);
testSlidableDelegate(const SlidableBehindDelegate(),
getSlidableBehindDelegateHalfValues, actionExtentRatio);
testSlidableDelegate(const SlidableScrollDelegate(),
getSlidableScrollDelegateHalfValues, actionExtentRatio);
testSlidableDelegate(const SlidableDrawerDelegate(),
getSlidableDrawerDelegateHalfValues, actionExtentRatio);
testWidgets('Cannot slide if slidable disabled', (WidgetTester tester) async {
await tester.pumpWidget(buildTest(const SlidableBehindDelegate()));
checkActions(3, hidden: allActions);
await dragItem(tester, 3,
gestureDirection: AxisDirection.left, endOffsetFactor: 0.2);
checkActions(3, hidden: allActions);
});
testWidgets('Close slidables when scroll', (WidgetTester tester) async {
await tester.pumpWidget(buildTest(const SlidableBehindDelegate()));
final int index = 1;
checkActions(index, hidden: allActions);
await dragItem(tester, index,
gestureDirection: AxisDirection.right, endOffsetFactor: 0.2);
checkActions(index, visible: <int>[a0, a1]);
await flingItem(
tester,
index,
gestureDirection: AxisDirection.up,
initialOffsetFactor: 0.2,
);
checkActions(index, hidden: allActions);
});
}