-
Notifications
You must be signed in to change notification settings - Fork 787
/
ActivityUtils.java
executable file
·217 lines (187 loc) · 5.22 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
package com.jayway.android.robotium.solo;
import java.util.ArrayList;
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;
/**
* This class contains activity related methods. Examples are:
* getCurrentActivity(), getActivityList().
*
* @author Renas Reda, renas.reda@jayway.com
*
*/
class ActivityUtils {
private final Instrumentation inst;
private ActivityMonitor activityMonitor;
private Activity activity;
private final Sleeper sleeper;
private ArrayList<Activity> activityList = new ArrayList<Activity>();
/**
* Constructor that takes in the instrumentation and the start activity.
*
* @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;
setupActivityMonitor();
}
/**
* Returns a {@code List} of all the opened/active activities.
*
* @return a {@code List} of all the opened/active activities
*
*/
public ArrayList<Activity> getAllOpenedActivities()
{
return activityList;
}
/**
* 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 (Throwable e) {
}
}
/**
* Public method that 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}.
*
* @see android.app.Activity#setRequestedOrientation(int)
*
*/
public void setActivityOrientation(int orientation)
{
if(activity.equals(getCurrentActivity()))
sleeper.sleep();
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);
}
/**
* 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();
inst.waitForIdleSync();
}
Boolean found = false;
if (activityMonitor != null) {
if (activityMonitor.getLastActivity() != null)
activity = activityMonitor.getLastActivity();
}
for(Activity storedActivity : activityList){
if (storedActivity.getClass().getName().equals(
activity.getClass().getName()))
found = true;
}
if (found)
return activity;
else {
activityList.add(activity);
return activity;
}
}
/**
* 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(!getCurrentActivity().getClass().getSimpleName().equals(name) && now < endTime)
{
now = System.currentTimeMillis();
}
if(now < endTime)
return true;
else
return false;
}
/**
* 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)
{
boolean found = false;
for(Activity activity : activityList){
if(activity.getClass().getSimpleName().equals(name))
found = true;
}
if(found){
while(!getCurrentActivity().getClass().getSimpleName().equals(name))
{
try{
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
}catch(Throwable e){
Assert.assertTrue("Activity named " + name + " can not be returned to", false);}
}
}
else{
for (int i = 0; i < activityList.size(); i++)
Log.d("Robotium", "Activity priorly opened: "+ activityList.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)
{
return activity.getString(resId);
}
/**
*
* All activites that have been active are finished.
*
*/
public void finalize() throws Throwable {
try {
for (int i = 0; i < activityList.size(); i++) {
activityList.get(i).finish();
}
getCurrentActivity().finish();
activityList.clear();
} catch (Throwable e) {
}
super.finalize();
}
}