dev
dart 260 lines 7.66 KB
Raw
1 import 'package:flutter/material.dart';
2 import 'package:flutter_test/flutter_test.dart';
3
4 class CommonTestCases {
5 WidgetTester tester;
6 CommonTestCases(this.tester);
7
8 Future<void> isSpecificPage<T>() async {
9 await tester.pumpAndSettle();
10 hasType<T>();
11 }
12
13 Future<void> tapItemByKey(
14 String key, {
15 bool shouldPumpAndSettle = true,
16 int pumpDuration = 100,
17 }) async {
18 await tester.pump(Duration(milliseconds: 500));
19
20 final widgetFinder = find.byKey(ValueKey(key));
21
22 expect(tester.any(widgetFinder), true, reason: 'Widget with key "$key" should be visible');
23
24 final widget = widgetFinder.first;
25 await tester.tap(widget, warnIfMissed: false);
26 shouldPumpAndSettle
27 ? await tester.pumpAndSettle(Duration(milliseconds: pumpDuration))
28 : await tester.pump();
29 }
30
31 Future<void> tapItemByFinder(Finder finder, {bool shouldPumpAndSettle = true}) async {
32 await tester.tap(finder);
33 shouldPumpAndSettle ? await tester.pumpAndSettle() : await tester.pump();
34 }
35
36 void hasText(String text, {bool hasWidget = true}) {
37 final textWidget = find.text(text);
38 expect(textWidget, hasWidget ? findsOneWidget : findsNothing);
39 }
40
41 void hasTextAtLestOnce(String text, {bool hasWidget = true}) {
42 final textWidget = find.text(text);
43 expect(textWidget, hasWidget ? findsAny : findsNothing);
44 }
45
46 void hasType<T>() {
47 final typeWidget = find.byType(T);
48 expect(typeWidget, findsOneWidget);
49 }
50
51 bool isKeyPresent(String key) {
52 final typeWidget = find.byKey(ValueKey(key));
53 return typeWidget.tryEvaluate();
54 }
55
56 void hasValueKey(String key) {
57 final typeWidget = find.byKey(ValueKey(key));
58 expect(typeWidget, findsOneWidget);
59 }
60
61 Future<void> swipePage({bool swipeRight = true}) async {
62 await tester.drag(find.byType(PageView), Offset(swipeRight ? -300 : 300, 0));
63 await tester.pumpAndSettle();
64 }
65
66 Future<void> swipeByPageKey({required String key, bool swipeRight = true}) async {
67 await tester.drag(find.byKey(ValueKey(key)), Offset(swipeRight ? -300 : 300, 0));
68 await tester.pumpAndSettle();
69 }
70
71 Future<void> goBack() async {
72 tester.printToConsole('Routing back to previous screen');
73 final NavigatorState navigator = tester.state(find.byType(Navigator));
74 navigator.pop();
75 await tester.pumpAndSettle();
76 }
77
78 Future<void> startGesture(String key, Offset gestureOffset) async {
79 await tester.pumpAndSettle();
80
81 final hasKey = isKeyPresent(key);
82
83 tester.printToConsole("Has gestureKey: $hasKey");
84
85 if (!hasKey) return;
86
87 final gesture = await tester.startGesture(tester.getCenter(find.byKey(ValueKey(key))));
88
89 // Drag to the left
90 await gesture.moveBy(gestureOffset);
91
92 // End the gesture
93 await gesture.up();
94
95 await tester.pump();
96 }
97
98 Future<void> dragUntilVisible(
99 String childKey,
100 String parentKey, {
101 int maxScrolls = 100,
102 double scrollStep = 50.0,
103 int maxReverseScrolls = 50,
104 }) async {
105 await tester.pumpAndSettle();
106
107 final itemFinder = find.byKey(ValueKey(childKey));
108 final listFinder = find.byKey(ValueKey(parentKey));
109
110 // Check if the widget is already in the widget tree
111 if (tester.any(itemFinder)) {
112 // Widget is already built and in the tree
113 tester.printToConsole('Child is already present');
114 return;
115 }
116
117 // Find the Scrollable associated with the Parent Ad
118 final scrollableFinder = find.descendant(
119 of: listFinder,
120 matching: find.byType(Scrollable),
121 );
122
123 // Ensure that the Scrollable is found
124 expect(
125 scrollableFinder,
126 findsOneWidget,
127 reason: 'Scrollable descendant of the Parent Widget not found.',
128 );
129
130 // Get the initial scroll position
131 final scrollableState = tester.state<ScrollableState>(scrollableFinder);
132 double previousScrollPosition = scrollableState.position.pixels;
133 bool scrollDown = true;
134 bool reversedDirection = false;
135 bool found = false;
136
137 int reverseScrollCount = 0;
138
139 for (int scrolls = 0; scrolls < maxScrolls; scrolls++) {
140 await tester.pumpAndSettle();
141
142 // Check if the widget is visible
143 if (tester.any(itemFinder)) {
144 found = true;
145 break;
146 }
147
148 // Log current state for debugging
149 tester.printToConsole('Scrolling ${scrollDown ? 'down' : 'up'}, attempt $scrolls');
150
151 // Stop if reverse scroll limit is exceeded
152 if (!scrollDown && reverseScrollCount >= maxReverseScrolls) {
153 tester.printToConsole('Maximum reverse scrolls reached. Widget not found.');
154 break;
155 }
156
157 // Perform scrolling in the current direction
158 await tester.drag(
159 scrollableFinder,
160 scrollDown ? Offset(0, -scrollStep) : Offset(0, scrollStep),
161 );
162 await tester.pumpAndSettle();
163
164 // Update the scroll position after the drag
165 final currentScrollPosition = scrollableState.position.pixels;
166
167 if (currentScrollPosition == previousScrollPosition) {
168 // Cannot scroll further in the current direction
169 if (reversedDirection) {
170 // We've already tried both directions
171 tester.printToConsole('Reached the scroll limit in both directions. Widget not found.');
172 break;
173 } else {
174 // Reverse the scroll direction and reset reverse scroll count
175 scrollDown = !scrollDown;
176 reversedDirection = true;
177 reverseScrollCount = 0;
178 tester.printToConsole('Reached the end, reversing direction');
179 }
180 } else {
181 // Update scroll position and reverse scroll count, incrementing only for reverse scrolling
182 previousScrollPosition = currentScrollPosition;
183 if (!scrollDown) reverseScrollCount++;
184 }
185 }
186
187 if (!found) {
188 tester.printToConsole('Widget not found after $maxScrolls scrolls.');
189 }
190 }
191
192 Future<void> scrollItemIntoView(
193 String itemKeyId,
194 double scrollPixels,
195 String scrollableFinderKey,
196 ) async {
197 final Finder itemFinder = find.byKey(ValueKey(itemKeyId));
198
199 final scrollableFinder = find.descendant(
200 of: find.byKey(ValueKey(scrollableFinderKey)),
201 matching: find.byType(Scrollable),
202 );
203
204 // Check if the item is already visible
205 if (tester.any(itemFinder)) {
206 tester.printToConsole('Item $itemKeyId is already visible');
207 return;
208 }
209
210 // Check if scrollable exists
211 if (!tester.any(scrollableFinder)) {
212 tester.printToConsole('Scrollable not found for $itemKeyId');
213 return;
214 }
215
216 try {
217 await tester.scrollUntilVisible(
218 itemFinder,
219 scrollPixels,
220 scrollable: scrollableFinder,
221 maxScrolls: 10,
222 );
223
224 // Wait for the scroll to complete
225 await tester.pumpAndSettle(Duration(milliseconds: 500));
226
227 // Verify the item is now visible
228 if (!tester.any(itemFinder)) {
229 tester.printToConsole('Item $itemKeyId not found after scrolling');
230 }
231 } catch (e) {
232 tester.printToConsole('Could not scroll to $itemKeyId: $e');
233 }
234 }
235
236 Future<void> enterText(String text, String editableTextKey) async {
237 final editableTextWidget = find.byKey(ValueKey((editableTextKey)));
238
239 await tester.enterText(editableTextWidget, text);
240
241 await tester.pumpAndSettle();
242 }
243
244 void findWidgetViaDescendant({
245 required FinderBase<Element> of,
246 required FinderBase<Element> matching,
247 }) {
248 final textWidget = find.descendant(of: of, matching: matching);
249
250 expect(textWidget, findsOneWidget);
251 }
252
253 Future<void> defaultSleepTime({int seconds = 2}) async =>
254 await Future.delayed(Duration(seconds: seconds));
255
256 Future<void> takeScreenshots(String screenshotName) async {
257 // Pausing this for now
258 // await (tester.binding as IntegrationTestWidgetsFlutterBinding).takeScreenshot(screenshotName);
259 }
260 }