forked from RobotiumTech/robotium
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Waiter.java
301 lines (248 loc) · 9.36 KB
/
Waiter.java
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
package com.jayway.android.robotium.solo;
import java.util.ArrayList;
import android.view.View;
import android.widget.TextView;
/**
* This class contains various wait methods. Examples are: waitForText(),
* waitForIdle().
*
* @author Renas Reda, renas.reda@jayway.com
*
*/
class Waiter {
private final ActivityUtils activityUtils;
private final ViewFetcher viewFetcher;
private final int TIMEOUT = 20000;
private final int SMALLTIMEOUT = 10000;
private final int MINITIMEOUT = 300;
private final Searcher searcher;
private final Scroller scroller;
private final Sleeper sleeper;
private final MatchCounter matchCounter;
/**
* Constructs this object.
*
* @param activityUtils the {@code ActivityUtils} instance
* @param viewFetcher the {@code ViewFetcher} instance
* @param searcher the {@code Searcher} instance
* @param scroller the {@code Scroller} instance
* @param sleeper the {@code Sleeper} instance
*/
public Waiter(ActivityUtils activityUtils, ViewFetcher viewFetcher, Searcher searcher, Scroller scroller, Sleeper sleeper){
this.activityUtils = activityUtils;
this.viewFetcher = viewFetcher;
this.searcher = searcher;
this.scroller = scroller;
this.sleeper = sleeper;
matchCounter = new MatchCounter();
}
/**
* Waits for the given {@link Activity}.
*
* @param name the name of the {@code Activity} to wait for e.g. {@code "MyActivity"}
* @return {@code true} if {@code Activity} appears before the timeout and {@code false} if it does not
*
*/
public boolean waitForActivity(String name){
return waitForActivity(name, SMALLTIMEOUT);
}
/**
* Waits for the given {@link Activity}.
*
* @param name the name of the {@code Activity} to wait for e.g. {@code "MyActivity"}
* @param timeout the amount of time in milliseconds to wait
* @return {@code true} if {@code Activity} appears before the timeout and {@code false} if it does not
*
*/
public boolean waitForActivity(String name, int timeout)
{
long now = System.currentTimeMillis();
final long endTime = now + timeout;
while(!activityUtils.getCurrentActivity().getClass().getSimpleName().equals(name) && now < endTime)
{
now = System.currentTimeMillis();
}
if(now < endTime)
return true;
else
return false;
}
/**
* Waits for a view to be shown.
*
* @param viewClass the {@code View} class to wait for
* @param minimumNumberOfMatches the minimum number of matches that are expected to be shown. {@code 0} means any number of matches
* @param timeout the amount of time in milliseconds to wait
* @return {@code true} if view is shown and {@code false} if it is not shown before the timeout
*/
public <T extends View> boolean waitForView(final Class<T> viewClass, final int index){
return waitForView(viewClass, index, SMALLTIMEOUT, true);
}
/**
* Waits for a view to be shown.
*
* @param viewClass the {@code View} class to wait for
* @param minimumNumberOfMatches the minimum number of matches that are expected to be shown. {@code 0} means any number of matches
* @param timeout the amount of time in milliseconds to wait
* @param scroll true if scrolling should be performed
* @return {@code true} if view is shown and {@code false} if it is not shown before the timeout
*/
public <T extends View> boolean waitForView(final Class<T> viewClass, final int index, final boolean scroll){
return waitForView(viewClass, index, SMALLTIMEOUT, scroll);
}
/**
* Waits for a view to be shown.
*
* @param viewClass the {@code View} class to wait for
* @param index the index of the view that is expected to be shown.
* @param timeout the amount of time in milliseconds to wait
* @param scroll {@code true} if scrolling should be performed
* @return {@code true} if view is shown and {@code false} if it is not shown before the timeout
*/
public <T extends View> boolean waitForView(final Class<T> viewClass, final int index, final int timeout, final boolean scroll){
ArrayList<T> typeList = new ArrayList<T>();
final long endTime = System.currentTimeMillis() + timeout;
while (System.currentTimeMillis() < endTime) {
sleeper.sleepMini();
typeList = viewFetcher.getCurrentViews(viewClass);
typeList = RobotiumUtils.removeInvisibleViews(typeList);
matchCounter.addMatchesToCount(typeList.size());
typeList=null;
if(matchCounter.getTotalCount() > 0 && index < matchCounter.getTotalCount()){
matchCounter.resetCount();
return true;
}
if(index == 0 && matchCounter.getTotalCount() > 0){
matchCounter.resetCount();
return true;
}
if(scroll && !scroller.scroll(Scroller.DOWN))
matchCounter.resetCount();
if(!scroll)
matchCounter.resetCount();
}
matchCounter.resetCount();
return false;
}
/**
* Waits for two views to be shown
*
* @param viewClass the first {@code View} class to wait for
* @param viewClass2 the second {@code View} class to wait for
* @return {@code true} if any of the views are shown and {@code false} if none of the views are shown before the timeout
*/
public <T extends View> boolean waitForViews(final Class<T> viewClass, final Class<? extends View> viewClass2){
final long endTime = System.currentTimeMillis() + SMALLTIMEOUT;
while (System.currentTimeMillis() < endTime) {
if(waitForView(viewClass, 0, MINITIMEOUT, false)){
return true;
}
if(waitForView(viewClass2, 0, MINITIMEOUT, false)){
return true;
}
scroller.scroll(Scroller.DOWN);
}
return false;
}
/**
* Used instead of instrumentation.waitForIdleSync().
*
*/
public void waitForClickableItems() {
sleeper.sleep();
long startTime = System.currentTimeMillis();
long timeout = 10000;
long endTime = startTime + timeout;
while (System.currentTimeMillis() <= endTime) {
if (clickableItemsExist())
break;
sleeper.sleep();
}
}
/**
* Checks if any of the views currently shown are clickable
*
* @return true if clickable views exist
*/
private boolean clickableItemsExist(){
ArrayList<View> clickableItems = new ArrayList<View>();
clickableItems.addAll(viewFetcher.getAllViews(true));
for(View view : clickableItems){
if(view.getTouchables().size() > 0)
return true;
}
return false;
}
/**
* Waits for a text to be shown. Default timeout is 20 seconds.
*
* @param text the text that needs to be shown
* @return {@code true} if text is found and {@code false} if it is not found before the timeout
*
*/
public boolean waitForText(String text) {
return waitForText(text, 0, TIMEOUT, true);
}
/**
* Waits for a text to be shown. Default timeout is 20 seconds.
*
* @param text the text that needs to be shown
* @param expectedMinimumNumberOfMatches the minimum number of matches of text that must be shown. {@code 0} means any number of matches
* @return {@code true} if text is found and {@code false} if it is not found before the timeout
*
*/
public boolean waitForText(String text, int expectedMinimumNumberOfMatches) {
return waitForText(text, expectedMinimumNumberOfMatches, TIMEOUT, true);
}
/**
* Waits for a text to be shown.
*
* @param text the text that needs to be shown
* @param expectedMinimumNumberOfMatches the minimum number of matches of text that must be shown. {@code 0} means any number of matches
* @param timeout the the amount of time in milliseconds to wait
* @return {@code true} if text is found and {@code false} if it is not found before the timeout
*
*/
public boolean waitForText(String text, int expectedMinimumNumberOfMatches, long timeout)
{
return waitForText(text, expectedMinimumNumberOfMatches, timeout, true);
}
/**
* Waits for a text to be shown.
*
* @param text the text that needs to be shown
* @param expectedMinimumNumberOfMatches the minimum number of matches of text that must be shown. {@code 0} means any number of matches
* @param timeout the the amount of time in milliseconds to wait
* @param scroll {@code true} if scrolling should be performed
* @return {@code true} if text is found and {@code false} if it is not found before the timeout
*
*/
public boolean waitForText(String text, int expectedMinimumNumberOfMatches, long timeout, boolean scroll) {
return waitForText(text, expectedMinimumNumberOfMatches, timeout, scroll, false);
}
/**
* Waits for a text to be shown.
*
* @param text the text that needs to be shown
* @param expectedMinimumNumberOfMatches the minimum number of matches of text that must be shown. {@code 0} means any number of matches
* @param timeout the the amount of time in milliseconds to wait
* @param scroll {@code true} if scrolling should be performed
* @param onlyVisible {@code true} if only visible text views should be waited for
* @return {@code true} if text is found and {@code false} if it is not found before the timeout
*
*/
public boolean waitForText(String text, int expectedMinimumNumberOfMatches, long timeout, boolean scroll, boolean onlyVisible) {
final long endTime = System.currentTimeMillis() + timeout;
while (true) {
final boolean timedOut = System.currentTimeMillis() > endTime;
if (timedOut){
return false;
}
sleeper.sleep();
final boolean foundAnyTextView = searcher.searchFor(TextView.class, text, expectedMinimumNumberOfMatches, scroll, onlyVisible);
if (foundAnyTextView){
return true;
}
}
}
}