/
ActivityUtils.java
359 lines (308 loc) · 9.01 KB
/
ActivityUtils.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
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
package com.BrowserTest;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Stack;
import java.util.Timer;
import java.util.TimerTask;
import junit.framework.Assert;
import android.app.Activity;
import android.app.Instrumentation;
import android.app.Instrumentation.ActivityMonitor;
import android.content.IntentFilter;
import android.util.Log;
import android.view.KeyEvent;
/**
* Contains activity related methods. Examples are:
* getCurrentActivity(), getActivityList(), getAllOpenedActivities().
*
* @author Renas Reda, renasreda@gmail.com
*
*/
class ActivityUtils {
private final Instrumentation inst;
private ActivityMonitor activityMonitor;
private Activity activity;
private final Sleeper sleeper;
private final String LOG_TAG = "Robotium";
private final int MINISLEEP = 100;
private static final int ACTIVITYSYNCTIME = 50;
private Stack<WeakReference<Activity>> activityStack;
private WeakReference<Activity> weakActivityReference;
private Stack<String> activitiesStoredInActivityStack;
private Timer activitySyncTimer;
/**
* Constructs this object.
*
* @param inst the {@code Instrumentation} instance.
* @param activity the start {@code Activity}
* @param sleeper the {@code Sleeper} instance
*/
public ActivityUtils(Instrumentation inst, Activity activity, Sleeper sleeper) {
this.inst = inst;
this.activity = activity;
this.sleeper = sleeper;
createStackAndPushStartActivity();
activitySyncTimer = new Timer();
activitiesStoredInActivityStack = new Stack<String>();
setupActivityMonitor();
setupActivityStackListener();
}
/**
* Creates a new activity stack and pushes the start activity
*/
private void createStackAndPushStartActivity(){
activityStack = new Stack<WeakReference<Activity>>();
if (activity != null){
WeakReference<Activity> weakReference = new WeakReference<Activity>(activity);
activity = null;
activityStack.push(weakReference);
}
}
/**
* Returns a {@code List} of all the opened/active activities.
*
* @return a {@code List} of all the opened/active activities
*/
public ArrayList<Activity> getAllOpenedActivities()
{
ArrayList<Activity> activities = new ArrayList<Activity>();
Iterator<WeakReference<Activity>> activityStackIterator = activityStack.iterator();
while(activityStackIterator.hasNext()){
Activity activity = activityStackIterator.next().get();
if(activity!=null)
activities.add(activity);
}
return activities;
}
/**
* This is were the activityMonitor is set up. The monitor will keep check
* for the currently active activity.
*/
private void setupActivityMonitor() {
try {
IntentFilter filter = null;
activityMonitor = inst.addMonitor(filter, null, false);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* This is were the activityStack listener is set up. The listener will keep track of the
* opened activities and their positions.
*/
private void setupActivityStackListener() {
TimerTask activitySyncTimerTask = new TimerTask() {
@Override
public void run() {
if (activityMonitor != null){
Activity activity = activityMonitor.getLastActivity();
if (activity != null){
if(!activitiesStoredInActivityStack.isEmpty() && activitiesStoredInActivityStack.peek().equals(activity.toString())){
return;
}
if (activitiesStoredInActivityStack.remove(activity.toString())){
removeActivityFromStack(activity);
}
if (!activity.isFinishing()){
addActivityToStack(activity);
}
}
}
}
};
activitySyncTimer.schedule(activitySyncTimerTask, 0, ACTIVITYSYNCTIME);
}
/**
* Removes a given activity from the activity stack
*
* @param activity the activity to remove
*/
private void removeActivityFromStack(Activity activity){
Iterator<WeakReference<Activity>> activityStackIterator = activityStack.iterator();
while(activityStackIterator.hasNext()){
Activity activityFromWeakReference = activityStackIterator.next().get();
if(activityFromWeakReference == null){
activityStackIterator.remove();
}
if(activity!=null && activityFromWeakReference!=null && activityFromWeakReference.equals(activity)){
activityStackIterator.remove();
}
}
}
/**
* Returns the ActivityMonitor used by Robotium.
*
* @return the ActivityMonitor used by Robotium
*/
public ActivityMonitor getActivityMonitor(){
return activityMonitor;
}
/**
* Sets the Orientation (Landscape/Portrait) for the current activity.
*
* @param orientation An orientation constant such as {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_LANDSCAPE} or {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_PORTRAIT}
*/
public void setActivityOrientation(int orientation)
{
Activity activity = getCurrentActivity();
activity.setRequestedOrientation(orientation);
}
/**
* Returns the current {@code Activity}, after sleeping a default pause length.
*
* @return the current {@code Activity}
*/
public Activity getCurrentActivity() {
return getCurrentActivity(true);
}
/**
* Adds an activity to the stack
*
* @param activity the activity to add
*/
private void addActivityToStack(Activity activity){
activitiesStoredInActivityStack.add(activity.toString());
weakActivityReference = new WeakReference<Activity>(activity);
activity = null;
activityStack.push(weakActivityReference);
}
/**
* Waits for an activity to be started if one is not provided
* by the constructor.
*/
private final void waitForActivityIfNotAvailable(){
if(activityStack.isEmpty() || activityStack.peek().get() == null){
if (activityMonitor != null) {
Activity activity = activityMonitor.getLastActivity();
while (activity == null){
sleeper.sleepMini();
activity = activityMonitor.getLastActivity();
}
addActivityToStack(activity);
}
else{
sleeper.sleepMini();
setupActivityMonitor();
waitForActivityIfNotAvailable();
}
}
}
/**
* Returns the current {@code Activity}.
*
* @param shouldSleepFirst whether to sleep a default pause first
* @return the current {@code Activity}
*/
public Activity getCurrentActivity(boolean shouldSleepFirst) {
if(shouldSleepFirst){
sleeper.sleep();
}
waitForActivityIfNotAvailable();
if(!activityStack.isEmpty()){
activity=activityStack.peek().get();
}
return activity;
}
/**
* Returns to the given {@link Activity}.
*
* @param name the name of the {@code Activity} to return to, e.g. {@code "MyActivity"}
*/
public void goBackToActivity(String name)
{
ArrayList<Activity> activitiesOpened = getAllOpenedActivities();
boolean found = false;
for(int i = 0; i < activitiesOpened.size(); i++){
if(activitiesOpened.get(i).getClass().getSimpleName().equals(name)){
found = true;
break;
}
}
if(found){
while(!getCurrentActivity().getClass().getSimpleName().equals(name))
{
try{
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
}catch(SecurityException ignored){}
}
}
else{
for (int i = 0; i < activitiesOpened.size(); i++){
Log.d(LOG_TAG, "Activity priorly opened: "+ activitiesOpened.get(i).getClass().getSimpleName());
}
Assert.assertTrue("No Activity named: '" + name + "' has been priorly opened", false);
}
}
/**
* Returns a localized string
*
* @param resId the resource ID for the string
* @return the localized string
*/
public String getString(int resId)
{
Activity activity = getCurrentActivity(false);
return activity.getString(resId);
}
/**
* Finalizes the solo object.
*/
@Override
public void finalize() throws Throwable {
activitySyncTimer.cancel();
try {
// Remove the monitor added during startup
if (activityMonitor != null) {
inst.removeMonitor(activityMonitor);
activityMonitor = null;
}
} catch (Exception ignored) {}
super.finalize();
}
/**
* All activites that have been opened are finished.
*/
public void finishOpenedActivities(){
// Stops the activityStack listener
activitySyncTimer.cancel();
ArrayList<Activity> activitiesOpened = getAllOpenedActivities();
// Finish all opened activities
for (int i = activitiesOpened.size()-1; i >= 0; i--) {
sleeper.sleep(MINISLEEP);
finishActivity(activitiesOpened.get(i));
}
activitiesOpened = null;
// Finish the initial activity, pressing Back for good measure
finishActivity(getCurrentActivity());
this.activity = null;
sleeper.sleepMini();
try {
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
sleeper.sleep(MINISLEEP);
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
} catch (Throwable ignored) {
// Guard against lack of INJECT_EVENT permission
}
clearActivityStack();
}
/**
* Clears the activity stack
*/
private void clearActivityStack(){
activityStack.clear();
activitiesStoredInActivityStack.clear();
}
/**
* Finishes an activity
*
* @param activity the activity to finish
*/
private void finishActivity(Activity activity){
try{
activity.finish();
}catch(Throwable e){
e.printStackTrace();
}
}
}