-
-
Notifications
You must be signed in to change notification settings - Fork 646
Expand file tree
/
Copy pathintro_screen_test.dart
More file actions
536 lines (495 loc) · 19 KB
/
intro_screen_test.dart
File metadata and controls
536 lines (495 loc) · 19 KB
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:getwidget/getwidget.dart';
class StateMarker extends StatefulWidget {
const StateMarker({Key? key, this.child}) : super(key: key);
final Widget? child;
@override
StateMarkerState createState() => StateMarkerState();
}
class StateMarkerState extends State<StateMarker> {
String? marker;
@override
Widget build(BuildContext context) {
if (widget.child != null) {
return widget.child!;
}
return Container();
}
}
void main() {
final Key bottomBarKey = UniqueKey();
final Key introScreenKey = UniqueKey();
final PageController _pageController = PageController(
initialPage: 0,
);
final int initialPage = _pageController.initialPage;
final List<Widget> slideList = [
Container(color: Colors.teal, child: const Text('Page 1')),
Container(color: Colors.amber, child: const Text('Page 2')),
Container(color: Colors.blueGrey, child: const Text('Page 3')),
Container(color: Colors.orange, child: const Text('Page 4')),
];
final textList = ['Page 1', 'Page 2', 'Page 3', 'Page 4'];
final List<Widget> slide = [
Container(color: Colors.teal, child: Text(textList[0])),
Container(color: Colors.amber, child: Text(textList[1])),
Container(color: Colors.blueGrey, child: Text(textList[2])),
Container(color: Colors.orange, child: Text(textList[3])),
];
String value = textList[0];
testWidgets('Basic GFIntroScreen can be constructed', (tester) async {
final GFIntroScreen introScreen = GFIntroScreen(
key: introScreenKey,
pageController: _pageController,
currentIndex: initialPage,
pageCount: slide.length,
slides: slide.map((data) => StateMarker(child: data)).toList());
StateMarkerState findStateMarkerState(String name) =>
tester.state(find.widgetWithText(StateMarker, name));
final TestApp app = TestApp(introScreen);
await tester.pumpWidget(app);
// find intro screen by key
expect(find.byKey(introScreenKey), findsOneWidget);
// find first screen text
expect(find.text('Page 1'), findsOneWidget);
TestGesture gesture =
await tester.startGesture(tester.getCenter(find.text('Page 1')));
await gesture.moveBy(const Offset(-600, 0));
await tester.pump();
expect(value, equals(textList[0]));
findStateMarkerState(textList[1]).marker = 'marked';
await gesture.up();
await tester.pump();
await tester.pump(const Duration(seconds: 1));
value = textList[_pageController.page!.toInt()];
expect(value, equals(textList[1]));
await tester.pumpWidget(app);
expect(findStateMarkerState(textList[1]).marker, equals('marked'));
// slide on to the third tab.
gesture =
await tester.startGesture(tester.getCenter(find.text(textList[1])));
await gesture.moveBy(const Offset(-600, 0));
await gesture.up();
await tester.pump();
expect(findStateMarkerState(textList[1]).marker, equals('marked'));
await tester.pump(const Duration(seconds: 1));
value = textList[_pageController.page!.toInt()];
expect(value, equals(textList[2]));
await tester.pumpWidget(app);
expect(find.text(textList[1]), findsNothing);
// slide back to the second tab.
gesture =
await tester.startGesture(tester.getCenter(find.text(textList[2])));
await gesture.moveBy(const Offset(600, 0));
await tester.pump();
final StateMarkerState markerState = findStateMarkerState(textList[1]);
expect(markerState.marker, isNull);
markerState.marker = 'marked';
await gesture.up();
await tester.pump();
await tester.pump(const Duration(seconds: 1));
value = textList[_pageController.page!.toInt()];
expect(value, equals(textList[1]));
await tester.pumpWidget(app);
expect(findStateMarkerState(textList[1]).marker, equals('marked'));
});
testWidgets(
'Basic GFIntroScreen can be constructed with GFIntroScreenBottomNavigationBar',
(tester) async {
final PageController _pageController = PageController(
initialPage: 0,
);
final int initialPage = _pageController.initialPage;
final GFIntroScreen introScreen = GFIntroScreen(
key: introScreenKey,
pageController: _pageController,
currentIndex: initialPage,
pageCount: slide.length,
slides: slide.map((data) => StateMarker(child: data)).toList(),
showIntroScreenBottomNavigationBar: true,
introScreenBottomNavigationBar: GFIntroScreenBottomNavigationBar(
key: bottomBarKey,
pageController: _pageController,
currentIndex: initialPage,
pageCount: 4,
),
);
final TestApp app = TestApp(introScreen);
await tester.pumpWidget(app);
// find intro screen by key
expect(find.byKey(bottomBarKey), findsOneWidget);
// find intro screen by key
expect(find.byKey(introScreenKey), findsOneWidget);
// find first screen text
expect(find.text('Page 1'), findsOneWidget);
expect(find.byKey(bottomBarKey), findsOneWidget);
expect(introScreen.showIntroScreenBottomNavigationBar, isTrue);
expect(_pageController, isNotNull);
expect(_pageController.initialPage, 0);
expect(_pageController.page!.toInt(), 0);
await tester.tap(find.text('NEXT'));
await tester.pump();
await tester.pump(const Duration(seconds: 1));
expect(_pageController.page!.toInt(), 1);
expect(find.text('Page 2'), findsOneWidget);
await tester.tap(find.text('NEXT'));
await tester.pump();
await tester.pump(const Duration(seconds: 1));
expect(_pageController.page!.toInt(), 2);
expect(find.text('Page 3'), findsOneWidget);
await tester.tap(find.text('BACK'));
await tester.pump();
await tester.pump(const Duration(seconds: 1));
expect(_pageController.page!.toInt(), 1);
expect(find.text('Page 2'), findsOneWidget);
});
testWidgets(
'Basic GFIntroScreenBottomNavigationBar can be constructed with no pagination and custom button text',
(tester) async {
final PageController _pageController = PageController(
initialPage: 0,
);
final int initialPage = _pageController.initialPage;
final GFIntroScreen introScreen = GFIntroScreen(
key: introScreenKey,
pageController: _pageController,
currentIndex: initialPage,
pageCount: slide.length,
slides: slide.map((data) => StateMarker(child: data)).toList(),
showIntroScreenBottomNavigationBar: true,
introScreenBottomNavigationBar: GFIntroScreenBottomNavigationBar(
key: bottomBarKey,
pageController: _pageController,
currentIndex: initialPage,
pageCount: 4,
showPagination: false,
forwardButtonText: 'go to next',
backButtonText: 'back to previous',
skipButtonText: 'skip to last',
doneButtonText: 'done, start from first',
onForwardButtonTap: () {
_pageController.nextPage(
duration: const Duration(milliseconds: 500),
curve: Curves.linear);
},
onBackButtonTap: () {
_pageController.previousPage(
duration: const Duration(milliseconds: 500),
curve: Curves.linear);
},
onSkipTap: () {
_pageController.jumpToPage(3);
},
onDoneTap: () {
_pageController.jumpToPage(0);
},
backButtonTextStyle: const TextStyle(fontSize: 12),
doneButtonTextStyle: const TextStyle(fontSize: 12),
forwardButtonTextStyle: const TextStyle(fontSize: 12),
skipButtonTextStyle: const TextStyle(fontSize: 12),
),
);
final TestApp app = TestApp(introScreen);
await tester.pumpWidget(app);
// find intro screen by key
expect(find.byKey(introScreenKey), findsOneWidget);
// find first screen text
expect(find.text('Page 1'), findsOneWidget);
expect(find.byKey(bottomBarKey), findsOneWidget);
expect(introScreen.showIntroScreenBottomNavigationBar, isTrue);
expect(_pageController, isNotNull);
// on first screen
expect(_pageController.initialPage, 0);
expect(_pageController.page!.toInt(), 0);
// tap go to next, to go to second screen
await tester.tap(find.text('go to next'));
await tester.pump();
await tester.pump(const Duration(seconds: 1));
expect(_pageController.page!.toInt(), 1);
// find second screen
expect(find.text('Page 2'), findsOneWidget);
// tap back to previous to go back to first screen
await tester.tap(find.text('back to previous'));
await tester.pump();
await tester.pump(const Duration(seconds: 1));
expect(_pageController.page!.toInt(), 0);
// find first screen
expect(find.text('Page 1'), findsOneWidget);
// tap skip to last, to jump to last screen
await tester.tap(find.text('skip to last'));
await tester.pump();
await tester.pump(const Duration(seconds: 1));
expect(_pageController.page!.toInt(), 3);
// find fourth screen
expect(find.text('Page 4'), findsOneWidget);
// tap done, start from first to jump to first screen
await tester.tap(find.text('done, start from first'));
await tester.pump();
await tester.pump(const Duration(seconds: 1));
expect(_pageController.page!.toInt(), 0);
// find first screen
expect(find.text('Page 1'), findsOneWidget);
});
testWidgets(
'Basic GFIntroScreenBottomNavigationBar can be constructed with no pagination and custom button',
(tester) async {
final PageController _pageController = PageController(
initialPage: 0,
);
final int initialPage = _pageController.initialPage;
final GFIntroScreen introScreen = GFIntroScreen(
key: introScreenKey,
pageController: _pageController,
currentIndex: initialPage,
pageCount: slide.length,
slides: slide.map((data) => StateMarker(child: data)).toList(),
showIntroScreenBottomNavigationBar: true,
introScreenBottomNavigationBar: GFIntroScreenBottomNavigationBar(
key: bottomBarKey,
pageController: _pageController,
currentIndex: initialPage,
pageCount: 4,
showPagination: false,
forwardButton: GFButton(
onPressed: () {
_pageController.nextPage(
duration: const Duration(milliseconds: 500),
curve: Curves.linear);
},
child: const Text('go to next'),
),
backButton: GFButton(
onPressed: () {
_pageController.previousPage(
duration: const Duration(milliseconds: 500),
curve: Curves.linear);
},
child: const Text('back to previous'),
),
skipButton: GFButton(
onPressed: () {
_pageController.jumpToPage(3);
},
child: const Text('skip to last'),
),
doneButton: GFButton(
onPressed: () {
_pageController.jumpToPage(0);
},
child: const Text('done, start from first'),
),
),
);
final TestApp app = TestApp(introScreen);
await tester.pumpWidget(app);
// find intro screen by key
expect(find.byKey(introScreenKey), findsOneWidget);
// find first screen text
expect(find.text('Page 1'), findsOneWidget);
expect(find.byKey(bottomBarKey), findsOneWidget);
expect(introScreen.showIntroScreenBottomNavigationBar, isTrue);
expect(_pageController, isNotNull);
// on first screen
expect(_pageController.initialPage, 0);
expect(_pageController.page!.toInt(), 0);
// tap go to next, to go to second screen
await tester.tap(find.text('go to next'));
await tester.pump();
await tester.pump(const Duration(seconds: 1));
expect(_pageController.page!.toInt(), 1);
// find second screen
expect(find.text('Page 2'), findsOneWidget);
// tap back to previous to go back to first screen
await tester.tap(find.text('back to previous'));
await tester.pump();
await tester.pump(const Duration(seconds: 1));
expect(_pageController.page!.toInt(), 0);
// find first screen
expect(find.text('Page 1'), findsOneWidget);
// tap skip to last, to jump to last screen
await tester.tap(find.text('skip to last'));
await tester.pump();
await tester.pump(const Duration(seconds: 1));
expect(_pageController.page!.toInt(), 3);
// find fourth screen
expect(find.text('Page 4'), findsOneWidget);
// tap done, start from first to jump to first screen
await tester.tap(find.text('done, start from first'));
await tester.pump();
await tester.pump(const Duration(seconds: 1));
expect(_pageController.page!.toInt(), 0);
// find first screen
expect(find.text('Page 1'), findsOneWidget);
});
testWidgets(
'Basic GFIntroScreenBottomNavigationBar can be constructed with custom pagination and no buttons',
(tester) async {
final PageController _pageController = PageController(
initialPage: 0,
);
final int initialPage = _pageController.initialPage;
final GFIntroScreen introScreen = GFIntroScreen(
key: introScreenKey,
pageController: _pageController,
currentIndex: initialPage,
pageCount: slide.length,
slides: slide.map((data) => StateMarker(child: data)).toList(),
showIntroScreenBottomNavigationBar: true,
introScreenBottomNavigationBar: GFIntroScreenBottomNavigationBar(
key: bottomBarKey,
pageController: _pageController,
currentIndex: initialPage,
pageCount: 4,
showButton: false,
dotHeight: 10,
dotWidth: 16,
dotShape: RoundedRectangleBorder(
side: const BorderSide(color: Colors.red, width: 2),
borderRadius: BorderRadius.circular(5)),
inactiveColor: Colors.black12,
activeColor: Colors.black87,
dotMargin: const EdgeInsets.symmetric(horizontal: 6),
),
);
final TestApp app = TestApp(introScreen);
await tester.pumpWidget(app);
// find intro screen by key
expect(find.byKey(introScreenKey), findsOneWidget);
// find first screen text
expect(find.text('Page 1'), findsOneWidget);
expect(find.byKey(bottomBarKey), findsOneWidget);
expect(introScreen.showIntroScreenBottomNavigationBar, isTrue);
expect(_pageController, isNotNull);
// on first screen
expect(_pageController.initialPage, 0);
expect(_pageController.page!.toInt(), 0);
expect(introScreen.currentIndex, _pageController.page!.toInt());
});
testWidgets(
'Basic GFIntroScreenBottomNavigationBar can be constructed with custom child',
(tester) async {
final PageController _pageController = PageController(
initialPage: 0,
);
final int initialPage = _pageController.initialPage;
final GFIntroScreen introScreen = GFIntroScreen(
key: introScreenKey,
pageController: _pageController,
currentIndex: initialPage,
pageCount: slide.length,
slides: slide.map((data) => StateMarker(child: data)).toList(),
showIntroScreenBottomNavigationBar: true,
introScreenBottomNavigationBar: GFIntroScreenBottomNavigationBar(
key: bottomBarKey,
pageController: _pageController,
currentIndex: initialPage,
pageCount: 4,
child: const Text('bottom bar'),
),
);
final TestApp app = TestApp(introScreen);
await tester.pumpWidget(app);
// find intro screen by key
expect(find.byKey(introScreenKey), findsOneWidget);
// find first screen text
expect(find.text('Page 1'), findsOneWidget);
expect(find.byKey(bottomBarKey), findsOneWidget);
// find first screen text
expect(find.text('bottom bar'), findsOneWidget);
});
testWidgets(
'Customized GFIntroScreenBottomNavigationBar can be constructed with properties',
(tester) async {
final PageController _pageController = PageController(
initialPage: 0,
);
final int initialPage = _pageController.initialPage;
final GFIntroScreen introScreen = GFIntroScreen(
key: introScreenKey,
pageController: _pageController,
currentIndex: initialPage,
pageCount: slide.length,
slides: slide.map((data) => StateMarker(child: data)).toList(),
showIntroScreenBottomNavigationBar: true,
introScreenBottomNavigationBar: GFIntroScreenBottomNavigationBar(
key: bottomBarKey,
pageController: _pageController,
currentIndex: initialPage,
pageCount: 4,
navigationBarHeight: 60,
navigationBarWidth: 400,
navigationBarMargin: const EdgeInsets.all(6),
navigationBarPadding: const EdgeInsets.all(5),
navigationBarShape: RoundedRectangleBorder(
side: const BorderSide(color: Colors.teal, width: 1),
borderRadius: BorderRadius.circular(5),
),
navigationBarColor: Colors.amber,
showDivider: true,
dividerHeight: 2,
dividerThickness: 4,
dividerColor: Colors.black,
dotHeight: 10,
dotWidth: 16,
dotShape: RoundedRectangleBorder(
side: const BorderSide(color: Colors.red, width: 2),
borderRadius: BorderRadius.circular(5)),
inactiveColor: Colors.black12,
activeColor: Colors.black87,
dotMargin: const EdgeInsets.symmetric(horizontal: 6),
),
);
final TestApp app = TestApp(introScreen);
await tester.pumpWidget(app);
// find intro screen by key
expect(find.byKey(bottomBarKey), findsOneWidget);
expect(app.introScreen.pageController, _pageController);
expect(app.introScreen.currentIndex, initialPage);
expect(app.introScreen.pageCount, 4);
});
testWidgets('Customized GFIntroScreen can be constructed with properties',
(tester) async {
final GFIntroScreen introScreen = GFIntroScreen(
key: introScreenKey,
pageController: _pageController,
currentIndex: initialPage,
pageCount: slideList.length,
slides: slideList,
height: 500,
width: 300,
color: Colors.blueGrey,
borderRadius: BorderRadius.circular(4),
border: Border.all(color: Colors.blueGrey.shade700, width: 3),
);
final TestApp app = TestApp(introScreen);
await tester.pumpWidget(app);
// find introScreen by key
expect(find.byKey(introScreenKey), findsOneWidget);
// find first screen text
expect(find.text('Page 1'), findsOneWidget);
expect(app.introScreen.pageCount, 4);
expect(app.introScreen.currentIndex, initialPage);
expect(app.introScreen.height, 500);
expect(app.introScreen.width, 300);
expect(app.introScreen.color, Colors.blueGrey);
expect(app.introScreen.borderRadius, BorderRadius.circular(4));
expect(app.introScreen.border,
Border.all(color: Colors.blueGrey.shade700, width: 3));
expect(app.introScreen.showIntroScreenBottomNavigationBar, isFalse);
});
}
class TestApp extends StatefulWidget {
const TestApp(this.introScreen);
final GFIntroScreen introScreen;
@override
_TestAppState createState() => _TestAppState();
}
class _TestAppState extends State<TestApp> {
@override
Widget build(BuildContext context) => MaterialApp(
home: Scaffold(
body: widget.introScreen,
),
);
}