1 /*
2  * Copyright (C) 2007 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.test;
18 
19 import android.app.Activity;
20 import android.app.Instrumentation;
21 import android.content.Intent;
22 import android.os.Bundle;
23 import android.util.Log;
24 import android.view.KeyEvent;
25 
26 import java.lang.reflect.Field;
27 import java.lang.reflect.InvocationTargetException;
28 import java.lang.reflect.Method;
29 import java.lang.reflect.Modifier;
30 
31 import junit.framework.TestCase;
32 
33 /**
34  * A test case that has access to {@link Instrumentation}.
35  */
36 public class InstrumentationTestCase extends TestCase {
37 
38     private Instrumentation mInstrumentation;
39 
40     /**
41      * Injects instrumentation into this test case. This method is
42      * called by the test runner during test setup.
43      *
44      * @param instrumentation the instrumentation to use with this instance
45      */
injectInstrumentation(Instrumentation instrumentation)46     public void injectInstrumentation(Instrumentation instrumentation) {
47         mInstrumentation = instrumentation;
48     }
49 
50     /**
51      * Injects instrumentation into this test case. This method is
52      * called by the test runner during test setup.
53      *
54      * @param instrumentation the instrumentation to use with this instance
55      *
56      * @deprecated Incorrect spelling,
57      * use {@link #injectInstrumentation(android.app.Instrumentation)} instead.
58      */
59     @Deprecated
injectInsrumentation(Instrumentation instrumentation)60     public void injectInsrumentation(Instrumentation instrumentation) {
61         injectInstrumentation(instrumentation);
62     }
63 
64     /**
65      * Inheritors can access the instrumentation using this.
66      * @return instrumentation
67      */
getInstrumentation()68     public Instrumentation getInstrumentation() {
69         return mInstrumentation;
70     }
71 
72     /**
73      * Utility method for launching an activity.
74      *
75      * <p>The {@link Intent} used to launch the Activity is:
76      *  action = {@link Intent#ACTION_MAIN}
77      *  extras = null, unless a custom bundle is provided here
78      * All other fields are null or empty.
79      *
80      * <p><b>NOTE:</b> The parameter <i>pkg</i> must refer to the package identifier of the
81      * package hosting the activity to be launched, which is specified in the AndroidManifest.xml
82      * file.  This is not necessarily the same as the java package name.
83      *
84      * @param pkg The package hosting the activity to be launched.
85      * @param activityCls The activity class to launch.
86      * @param extras Optional extra stuff to pass to the activity.
87      * @return The activity, or null if non launched.
88      */
launchActivity( String pkg, Class<T> activityCls, Bundle extras)89     public final <T extends Activity> T launchActivity(
90             String pkg,
91             Class<T> activityCls,
92             Bundle extras) {
93         Intent intent = new Intent(Intent.ACTION_MAIN);
94         if (extras != null) {
95             intent.putExtras(extras);
96         }
97         return launchActivityWithIntent(pkg, activityCls, intent);
98     }
99 
100     /**
101      * Utility method for launching an activity with a specific Intent.
102      *
103      * <p><b>NOTE:</b> The parameter <i>pkg</i> must refer to the package identifier of the
104      * package hosting the activity to be launched, which is specified in the AndroidManifest.xml
105      * file.  This is not necessarily the same as the java package name.
106      *
107      * @param pkg The package hosting the activity to be launched.
108      * @param activityCls The activity class to launch.
109      * @param intent The intent to launch with
110      * @return The activity, or null if non launched.
111      */
112     @SuppressWarnings("unchecked")
launchActivityWithIntent( String pkg, Class<T> activityCls, Intent intent)113     public final <T extends Activity> T launchActivityWithIntent(
114             String pkg,
115             Class<T> activityCls,
116             Intent intent) {
117         intent.setClassName(pkg, activityCls.getName());
118         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
119         T activity = (T) getInstrumentation().startActivitySync(intent);
120         getInstrumentation().waitForIdleSync();
121         return activity;
122     }
123 
124     /**
125      * Helper for running portions of a test on the UI thread.
126      *
127      * Note, in most cases it is simpler to annotate the test method with
128      * {@link android.test.UiThreadTest}, which will run the entire test method on the UI thread.
129      * Use this method if you need to switch in and out of the UI thread to perform your test.
130      *
131      * @param r runnable containing test code in the {@link Runnable#run()} method
132      */
runTestOnUiThread(final Runnable r)133     public void runTestOnUiThread(final Runnable r) throws Throwable {
134         final Throwable[] exceptions = new Throwable[1];
135         getInstrumentation().runOnMainSync(new Runnable() {
136             public void run() {
137                 try {
138                     r.run();
139                 } catch (Throwable throwable) {
140                     exceptions[0] = throwable;
141                 }
142             }
143         });
144         if (exceptions[0] != null) {
145             throw exceptions[0];
146         }
147     }
148 
149     /**
150      * Runs the current unit test. If the unit test is annotated with
151      * {@link android.test.UiThreadTest}, the test is run on the UI thread.
152      */
153     @Override
runTest()154     protected void runTest() throws Throwable {
155         String fName = getName();
156         assertNotNull(fName);
157         Method method = null;
158         try {
159             // use getMethod to get all public inherited
160             // methods. getDeclaredMethods returns all
161             // methods of this class but excludes the
162             // inherited ones.
163             method = getClass().getMethod(fName, (Class[]) null);
164         } catch (NoSuchMethodException e) {
165             fail("Method \""+fName+"\" not found");
166         }
167 
168         if (!Modifier.isPublic(method.getModifiers())) {
169             fail("Method \""+fName+"\" should be public");
170         }
171 
172         int runCount = 1;
173         boolean isRepetitive = false;
174         if (method.isAnnotationPresent(FlakyTest.class)) {
175             runCount = method.getAnnotation(FlakyTest.class).tolerance();
176         } else if (method.isAnnotationPresent(RepetitiveTest.class)) {
177             runCount = method.getAnnotation(RepetitiveTest.class).numIterations();
178             isRepetitive = true;
179         }
180 
181         if (method.isAnnotationPresent(UiThreadTest.class)) {
182             final int tolerance = runCount;
183             final boolean repetitive = isRepetitive;
184             final Method testMethod = method;
185             final Throwable[] exceptions = new Throwable[1];
186             getInstrumentation().runOnMainSync(new Runnable() {
187                 public void run() {
188                     try {
189                         runMethod(testMethod, tolerance, repetitive);
190                     } catch (Throwable throwable) {
191                         exceptions[0] = throwable;
192                     }
193                 }
194             });
195             if (exceptions[0] != null) {
196                 throw exceptions[0];
197             }
198         } else {
199             runMethod(method, runCount, isRepetitive);
200         }
201     }
202 
203     // For backwards-compatibility after adding isRepetitive
runMethod(Method runMethod, int tolerance)204     private void runMethod(Method runMethod, int tolerance) throws Throwable {
205         runMethod(runMethod, tolerance, false);
206     }
207 
runMethod(Method runMethod, int tolerance, boolean isRepetitive)208     private void runMethod(Method runMethod, int tolerance, boolean isRepetitive) throws Throwable {
209         Throwable exception = null;
210 
211         int runCount = 0;
212         do {
213             try {
214                 runMethod.invoke(this, (Object[]) null);
215                 exception = null;
216             } catch (InvocationTargetException e) {
217                 e.fillInStackTrace();
218                 exception = e.getTargetException();
219             } catch (IllegalAccessException e) {
220                 e.fillInStackTrace();
221                 exception = e;
222             } finally {
223                 runCount++;
224                 // Report current iteration number, if test is repetitive
225                 if (isRepetitive) {
226                     Bundle iterations = new Bundle();
227                     iterations.putInt("currentiterations", runCount);
228                     getInstrumentation().sendStatus(2, iterations);
229                 }
230             }
231         } while ((runCount < tolerance) && (isRepetitive || exception != null));
232 
233         if (exception != null) {
234             throw exception;
235         }
236     }
237 
238     /**
239      * Sends a series of key events through instrumentation and waits for idle. The sequence
240      * of keys is a string containing the key names as specified in KeyEvent, without the
241      * KEYCODE_ prefix. For instance: sendKeys("DPAD_LEFT A B C DPAD_CENTER"). Each key can
242      * be repeated by using the N* prefix. For instance, to send two KEYCODE_DPAD_LEFT, use
243      * the following: sendKeys("2*DPAD_LEFT").
244      *
245      * @param keysSequence The sequence of keys.
246      */
sendKeys(String keysSequence)247     public void sendKeys(String keysSequence) {
248         final String[] keys = keysSequence.split(" ");
249         final int count = keys.length;
250 
251         final Instrumentation instrumentation = getInstrumentation();
252 
253         for (int i = 0; i < count; i++) {
254             String key = keys[i];
255             int repeater = key.indexOf('*');
256 
257             int keyCount;
258             try {
259                 keyCount = repeater == -1 ? 1 : Integer.parseInt(key.substring(0, repeater));
260             } catch (NumberFormatException e) {
261                 Log.w("ActivityTestCase", "Invalid repeat count: " + key);
262                 continue;
263             }
264 
265             if (repeater != -1) {
266                 key = key.substring(repeater + 1);
267             }
268 
269             for (int j = 0; j < keyCount; j++) {
270                 try {
271                     final Field keyCodeField = KeyEvent.class.getField("KEYCODE_" + key);
272                     final int keyCode = keyCodeField.getInt(null);
273                     try {
274                         instrumentation.sendKeyDownUpSync(keyCode);
275                     } catch (SecurityException e) {
276                         // Ignore security exceptions that are now thrown
277                         // when trying to send to another app, to retain
278                         // compatibility with existing tests.
279                     }
280                 } catch (NoSuchFieldException e) {
281                     Log.w("ActivityTestCase", "Unknown keycode: KEYCODE_" + key);
282                     break;
283                 } catch (IllegalAccessException e) {
284                     Log.w("ActivityTestCase", "Unknown keycode: KEYCODE_" + key);
285                     break;
286                 }
287             }
288         }
289 
290         instrumentation.waitForIdleSync();
291     }
292 
293     /**
294      * Sends a series of key events through instrumentation and waits for idle. For instance:
295      * sendKeys(KEYCODE_DPAD_LEFT, KEYCODE_DPAD_CENTER).
296      *
297      * @param keys The series of key codes to send through instrumentation.
298      */
sendKeys(int... keys)299     public void sendKeys(int... keys) {
300         final int count = keys.length;
301         final Instrumentation instrumentation = getInstrumentation();
302 
303         for (int i = 0; i < count; i++) {
304             try {
305                 instrumentation.sendKeyDownUpSync(keys[i]);
306             } catch (SecurityException e) {
307                 // Ignore security exceptions that are now thrown
308                 // when trying to send to another app, to retain
309                 // compatibility with existing tests.
310             }
311         }
312 
313         instrumentation.waitForIdleSync();
314     }
315 
316     /**
317      * Sends a series of key events through instrumentation and waits for idle. Each key code
318      * must be preceded by the number of times the key code must be sent. For instance:
319      * sendRepeatedKeys(1, KEYCODE_DPAD_CENTER, 2, KEYCODE_DPAD_LEFT).
320      *
321      * @param keys The series of key repeats and codes to send through instrumentation.
322      */
sendRepeatedKeys(int... keys)323     public void sendRepeatedKeys(int... keys) {
324         final int count = keys.length;
325         if ((count & 0x1) == 0x1) {
326             throw new IllegalArgumentException("The size of the keys array must "
327                     + "be a multiple of 2");
328         }
329 
330         final Instrumentation instrumentation = getInstrumentation();
331 
332         for (int i = 0; i < count; i += 2) {
333             final int keyCount = keys[i];
334             final int keyCode = keys[i + 1];
335             for (int j = 0; j < keyCount; j++) {
336                 try {
337                     instrumentation.sendKeyDownUpSync(keyCode);
338                 } catch (SecurityException e) {
339                     // Ignore security exceptions that are now thrown
340                     // when trying to send to another app, to retain
341                     // compatibility with existing tests.
342                 }
343             }
344         }
345 
346         instrumentation.waitForIdleSync();
347     }
348 
349     /**
350      * Make sure all resources are cleaned up and garbage collected before moving on to the next
351      * test. Subclasses that override this method should make sure they call super.tearDown()
352      * at the end of the overriding method.
353      *
354      * @throws Exception
355      */
356     @Override
tearDown()357     protected void tearDown() throws Exception {
358         Runtime.getRuntime().gc();
359         Runtime.getRuntime().runFinalization();
360         Runtime.getRuntime().gc();
361         super.tearDown();
362     }
363 }