-
Notifications
You must be signed in to change notification settings - Fork 45
/
main_test.dart
233 lines (182 loc) · 7.61 KB
/
main_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
import 'package:convenient_test_dev/convenient_test_dev.dart';
import 'package:convenient_test_example/home_page.dart';
import 'package:convenient_test_example/main.dart' as app;
import 'package:convenient_test_example/main.dart';
import 'package:convenient_test_example/random_page.dart';
import 'package:convenient_test_example/zoom_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
// Avoid forever animation (blinking), which makes pumping never ends
EditableText.debugDeterministicCursor = true;
convenientTestMain(MyConvenientTestSlot(), () {
group('simple test group', () {
tTestWidgets('choose some fruits', (t) async {
await t.get(HomePageMark.fetchFruits).tap();
await find.text('HomePage').should(findsOneWidget);
await find.text('You chose nothing').should(findsOneWidget);
await find.text('Cherry').tap();
await find.text('You chose: Cherry').should(findsOneWidget);
await t.tester.scrollUntilVisible(find.text('Orange'), 100);
await find.text('Orange').tap();
await find.text('You chose: Cherry, Orange').should(findsOneWidget);
await t.get(HomePageMark.fab).tap();
await find.text('HomePage').should(findsNothing);
await find.text('SecondPage').should(findsOneWidget);
await find.text('See fruits: Cherry, Orange').should(findsOneWidget);
await t.pageBack();
await find.text('HomePage').should(findsOneWidget);
});
tTestWidgets('deliberately failing test', (t) async {
expect(1, 0, reason: 'this expect should deliberately fail');
});
tTestWidgets('deliberately flaky test', (t) async {
final shouldFailThisTime = !_deliberatelyFlakyTestHasRun;
_deliberatelyFlakyTestHasRun = true;
await t.get(HomePageMark.fetchFruits).tap();
if (shouldFailThisTime) {
await find.text('NotExistString').should(findsOneWidget);
} else {
await find.text('Apple').should(findsOneWidget);
}
});
tTestWidgets('navigation', (t) async {
await t.visit('/second');
await find.text('HomePage').should(findsNothing);
await find.text('SecondPage').should(findsOneWidget);
// you can also assert route names
await t.routeName().shouldEquals('/second');
await t.pageBack();
await find.text('HomePage').should(findsOneWidget);
await find.text('SecondPage').should(findsNothing);
});
tTestWidgets('find by icon', (t) async {
await find.byIcon(Icons.done).should(findsOneWidget);
});
tTestWidgets('golden test', (t) async {
await find.text('HomePage').should(findsOneWidget);
await find
.byType(MaterialApp)
.should(matchesGoldenFile('goldens/sample_golden.png'));
});
tTestWidgets('deliberately failed golden test', (t) async {
await t.visit('/random');
await find.text('RandomPage').should(findsOneWidget);
await find.get(RandomPageMark.randomText).should(
matchesGoldenFile('goldens/deliberately_failed_golden.png'));
// let's assert something else
await find.textContaining('Random Height').should(findsOneWidget);
await find.textContaining('Height').should(findsOneWidget);
});
tTestWidgets('custom logging and snapshotting', (t) async {
// suppose you do something normal...
await find.text('HomePage').should(findsOneWidget);
// then you want to log and snapshot
final log = t.log('HELLO', 'Just a demonstration of custom logging');
await log.snapshot();
});
tTestWidgets('custom commands', (t) async {
await t.myCustomCommand();
});
tTestWidgets('sections', (t) async {
t.section('sample section one');
// do something
await t.get(HomePageMark.fetchFruits).tap();
await find.text('Apple').tap();
await find.text('Banana').tap();
await find.text('Cherry').tap();
t.section('sample section two');
// do something
await find.text('Apple').tap();
await find.text('Banana').tap();
await find.text('Cherry').tap();
await find.text('HomePage').should(findsOneWidget);
});
tTestWidgets('timer page', (t) async {
await t.visit('/timer');
for (var iter = 0; iter < 5; ++iter) {
final log = t.log('HELLO', 'Wait a second to have a look (#$iter)');
await log.snapshot(name: 'before');
final stopwatch = Stopwatch()..start();
while (stopwatch.elapsed < const Duration(seconds: 1)) {
await t.tester.pump();
}
await log.snapshot(name: 'after');
}
await t.pageBack();
});
tTestWidgets('enter and append text', (t) async {
await t.visit('/text_field');
await find.byType(TextField).enterTextWithoutReplace('first');
await find.text('first').should(findsOneWidget);
await find.byType(TextField).enterTextWithoutReplace(' second');
await find.text('first second').should(findsOneWidget);
});
group('zoom page', () {
tTestWidgets('single finger drag', (t) async {
await t.visit('/zoom');
await t.pumpAndSettle();
await find
.get(ZoomPageMark.palette)
.should(matchesGoldenFile('goldens/zoom_page_drag_before.png'));
await find.get(ZoomPageMark.palette).drag(const Offset(0, -50));
await find
.get(ZoomPageMark.palette)
.should(matchesGoldenFile('goldens/zoom_page_drag_after.png'));
// alternative approach
// await t.tester.drag(find.get(ZoomPageMark.palette), const Offset(0, -50));
// sample logging
// await t.tester.pumpAndSettle();
// await t.log('HELLO', 'look at it').snapshot();
});
tTestWidgets('double finger zooming', (t) async {
await t.visit('/zoom');
await find
.get(ZoomPageMark.palette)
.should(matchesGoldenFile('goldens/zoom_page_zoom_before.png'));
await find.get(ZoomPageMark.palette).multiDrag(
firstDownOffset: const Offset(0, -30),
secondDownOffset: const Offset(0, 30),
firstFingerOffsets: const [
Offset(0, -20),
Offset(0, -20),
Offset(0, -10)
],
secondFingerOffsets: const [
Offset(0, 20),
Offset(0, 20),
Offset(0, 10)
],
logMove: true,
);
await find
.get(ZoomPageMark.palette)
.should(matchesGoldenFile('goldens/zoom_page_zoom_after.png'));
});
});
});
group('some other test group', () {
tTestWidgets('empty test', (t) async {});
group('sample sub-group', () {
tTestWidgets('another empty test', (t) async {});
group('sample sub-sub-group', () {
tTestWidgets('yet another empty test', (t) async {});
});
});
});
});
}
var _deliberatelyFlakyTestHasRun = false;
class MyConvenientTestSlot extends ConvenientTestSlot {
@override
Future<void> appMain(AppMainExecuteMode mode) async => app.main();
@override
BuildContext? getNavContext(ConvenientTest t) =>
MyApp.navigatorKey.currentContext;
}
extension on ConvenientTest {
Future<void> myCustomCommand() async {
// Do anything you like... This is just a normal function
debugPrint('Hello, world!');
}
}