1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.app.stubs;
18 
19 import android.app.Activity;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.test.AndroidTestCase;
23 import android.test.PerformanceTestCase;
24 
25 public class ActivityTestsBase extends AndroidTestCase implements PerformanceTestCase,
26         LaunchpadActivity.CallingTest {
27     public static final String PERMISSION_GRANTED = "android.app.cts.permission.TEST_GRANTED";
28     public static final String PERMISSION_DENIED = "android.app.cts.permission.TEST_DENIED";
29 
30     private static final int TIMEOUT_MS = 60 * 1000;
31 
32     protected Intent mIntent;
33 
34     private PerformanceTestCase.Intermediates mIntermediates;
35     private String mExpecting;
36 
37     // Synchronization of activity result.
38     private boolean mFinished;
39     private int mResultCode = 0;
40     private Intent mData;
41     private Activity mActivity;
42     private RuntimeException mResultStack = null;
43 
44     @Override
setUp()45     protected void setUp() throws Exception {
46         super.setUp();
47         mIntent = new Intent(mContext, LaunchpadActivity.class);
48         mIntermediates = null;
49     }
50 
51     @Override
tearDown()52     protected void tearDown() throws Exception {
53         mIntermediates = null;
54         super.tearDown();
55     }
56 
isPerformanceOnly()57     public boolean isPerformanceOnly() {
58         return false;
59     }
60 
setInternalIterations(int count)61     public void setInternalIterations(int count) {
62     }
63 
startTiming(boolean realTime)64     public void startTiming(boolean realTime) {
65         if (mIntermediates != null) {
66             mIntermediates.startTiming(realTime);
67         }
68     }
69 
addIntermediate(String name)70     public void addIntermediate(String name) {
71         if (mIntermediates != null) {
72             mIntermediates.addIntermediate(name);
73         }
74     }
75 
addIntermediate(String name, long timeInNS)76     public void addIntermediate(String name, long timeInNS) {
77         if (mIntermediates != null) {
78             mIntermediates.addIntermediate(name, timeInNS);
79         }
80     }
81 
finishTiming(boolean realTime)82     public void finishTiming(boolean realTime) {
83         if (mIntermediates != null) {
84             mIntermediates.finishTiming(realTime);
85         }
86     }
87 
activityRunning(Activity activity)88     public void activityRunning(Activity activity) {
89         finishWithActivity(activity);
90     }
91 
activityFinished(int resultCode, Intent data, RuntimeException where)92     public void activityFinished(int resultCode, Intent data, RuntimeException where) {
93         finishWithResult(resultCode, data, null, where);
94     }
95 
editIntent()96     public Intent editIntent() {
97         return mIntent;
98     }
99 
100     @Override
getContext()101     public Context getContext() {
102         return mContext;
103     }
104 
startPerformance(Intermediates intermediates)105     public int startPerformance(Intermediates intermediates) {
106         mIntermediates = intermediates;
107         return 1;
108     }
109 
finishGood()110     public void finishGood() {
111         finishWithResult(Activity.RESULT_OK, null);
112     }
113 
finishBad(String error)114     public void finishBad(String error) {
115         finishWithResult(Activity.RESULT_CANCELED, new Intent().setAction(error));
116     }
117 
finishWithActivity(Activity activity)118     public void finishWithActivity(Activity activity) {
119         final RuntimeException where = new RuntimeException("Original error was here");
120         where.fillInStackTrace();
121         finishWithResult(Activity.RESULT_OK, null, activity, where);
122 
123     }
124 
finishWithResult(int resultCode, Intent data)125     public void finishWithResult(int resultCode, Intent data) {
126         final RuntimeException where = new RuntimeException("Original error was here");
127         where.fillInStackTrace();
128         finishWithResult(resultCode, data, null, where);
129     }
130 
finishWithResult(int resultCode, Intent data, Activity activity, RuntimeException where)131     public void finishWithResult(int resultCode, Intent data, Activity activity,
132             RuntimeException where) {
133         synchronized (this) {
134             mResultCode = resultCode;
135             mData = data;
136             mActivity = activity;
137             mResultStack = where;
138             mFinished = true;
139             notifyAll();
140         }
141     }
142 
runLaunchpad(String action)143     public int runLaunchpad(String action) {
144         startLaunchpadActivity(action);
145         return waitForResultOrThrow(TIMEOUT_MS);
146     }
147 
startLaunchpadActivity(String action)148     private void startLaunchpadActivity(String action) {
149         LaunchpadActivity.setCallingTest(this);
150 
151         synchronized (this) {
152             mIntent.setAction(action);
153             mFinished = false;
154             mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
155             mContext.startActivity(mIntent);
156         }
157     }
158 
waitForResultOrThrow(int timeoutMs)159     public int waitForResultOrThrow(int timeoutMs) {
160         return waitForResultOrThrow(timeoutMs, null);
161     }
162 
waitForResultOrThrow(int timeoutMs, String expected)163     public int waitForResultOrThrow(int timeoutMs, String expected) {
164         final int res = waitForResult(timeoutMs, expected);
165 
166         if (res == Activity.RESULT_CANCELED) {
167             if (mResultStack != null) {
168                 throw new RuntimeException(mData != null ? mData.toString() : "Unable to launch",
169                         mResultStack);
170             } else {
171                 throw new RuntimeException(mData != null ? mData.toString() : "Unable to launch");
172             }
173         }
174         return res;
175     }
176 
waitForResult(int timeoutMs, String expected)177     public int waitForResult(int timeoutMs, String expected) {
178         mExpecting = expected;
179 
180         final long endTime = System.currentTimeMillis() + timeoutMs;
181 
182         boolean timeout = false;
183         synchronized (this) {
184             while (!mFinished) {
185                 final long delay = endTime - System.currentTimeMillis();
186                 if (delay < 0) {
187                     timeout = true;
188                     break;
189                 }
190 
191                 try {
192                     wait(delay);
193                 } catch (final java.lang.InterruptedException e) {
194                     // do nothing
195                 }
196             }
197         }
198 
199         mFinished = false;
200 
201         if (timeout) {
202             mResultCode = Activity.RESULT_CANCELED;
203             onTimeout();
204         }
205         return mResultCode;
206     }
207 
208 
getResultCode()209     public int getResultCode() {
210         return mResultCode;
211     }
212 
getResultData()213     public Intent getResultData() {
214         return mData;
215     }
216 
getResultStack()217     public RuntimeException getResultStack() {
218         return mResultStack;
219     }
220 
getRunningActivity()221     public Activity getRunningActivity() {
222         return mActivity;
223     }
224 
onTimeout()225     public void onTimeout() {
226         final String msg = mExpecting == null ? "Timeout" : "Timeout while expecting " + mExpecting;
227         finishWithResult(Activity.RESULT_CANCELED, new Intent().setAction(msg));
228     }
229 }
230