• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.test;
18 
19 import java.io.File;
20 import java.lang.reflect.Field;
21 import java.lang.reflect.Modifier;
22 import java.util.List;
23 
24 import junit.framework.AssertionFailedError;
25 import junit.framework.Test;
26 import junit.framework.TestCase;
27 import junit.framework.TestListener;
28 import android.os.Bundle;
29 import android.test.suitebuilder.TestMethod;
30 import android.test.suitebuilder.annotation.HasAnnotation;
31 import android.util.Log;
32 
33 /**
34  * This test runner extends the default InstrumentationTestRunner. It overrides
35  * the {@code onCreate(Bundle)} method and sets the system properties necessary
36  * for many core tests to run. This is needed because there are some core tests
37  * that need writing access to the file system. We also need to set the harness
38  * Thread's context ClassLoader. Otherwise some classes and resources will not
39  * be found. Finally, we add a means to free memory allocated by a TestCase
40  * after its execution.
41  *
42  * @hide
43  */
44 @Deprecated
45 public class InstrumentationCoreTestRunner extends InstrumentationTestRunner {
46 
47     /**
48      * Convenience definition of our log tag.
49      */
50     private static final String TAG = "InstrumentationCoreTestRunner";
51 
52     /**
53      * True if (and only if) we are running in single-test mode (as opposed to
54      * batch mode).
55      */
56     private boolean singleTest = false;
57 
58     @Override
onCreate(Bundle arguments)59     public void onCreate(Bundle arguments) {
60         // We might want to move this to /sdcard, if is is mounted/writable.
61         File cacheDir = getTargetContext().getCacheDir();
62 
63         // Set some properties that the core tests absolutely need.
64         System.setProperty("user.language", "en");
65         System.setProperty("user.region", "US");
66 
67         System.setProperty("java.home", cacheDir.getAbsolutePath());
68         System.setProperty("user.home", cacheDir.getAbsolutePath());
69         System.setProperty("java.io.tmpdir", cacheDir.getAbsolutePath());
70 
71         if (arguments != null) {
72             String classArg = arguments.getString(ARGUMENT_TEST_CLASS);
73             singleTest = classArg != null && classArg.contains("#");
74         }
75 
76         super.onCreate(arguments);
77     }
78 
79     @Override
getAndroidTestRunner()80     protected AndroidTestRunner getAndroidTestRunner() {
81         AndroidTestRunner runner = super.getAndroidTestRunner();
82 
83         runner.addTestListener(new TestListener() {
84             /**
85              * The last test class we executed code from.
86              */
87             private Class<?> lastClass;
88 
89             /**
90              * The minimum time we expect a test to take.
91              */
92             private static final int MINIMUM_TIME = 100;
93 
94             /**
95              * The start time of our current test in System.currentTimeMillis().
96              */
97             private long startTime;
98 
99             public void startTest(Test test) {
100                 if (test.getClass() != lastClass) {
101                     lastClass = test.getClass();
102                     printMemory(test.getClass());
103                 }
104 
105                 Thread.currentThread().setContextClassLoader(
106                         test.getClass().getClassLoader());
107 
108                 startTime = System.currentTimeMillis();
109             }
110 
111             public void endTest(Test test) {
112                 if (test instanceof TestCase) {
113                     cleanup((TestCase)test);
114 
115                     /*
116                      * Make sure all tests take at least MINIMUM_TIME to
117                      * complete. If they don't, we wait a bit. The Cupcake
118                      * Binder can't handle too many operations in a very
119                      * short time, which causes headache for the CTS.
120                      */
121                     long timeTaken = System.currentTimeMillis() - startTime;
122 
123                     if (timeTaken < MINIMUM_TIME) {
124                         try {
125                             Thread.sleep(MINIMUM_TIME - timeTaken);
126                         } catch (InterruptedException ignored) {
127                             // We don't care.
128                         }
129                     }
130                 }
131             }
132 
133             public void addError(Test test, Throwable t) {
134                 // This space intentionally left blank.
135             }
136 
137             public void addFailure(Test test, AssertionFailedError t) {
138                 // This space intentionally left blank.
139             }
140 
141             /**
142              * Dumps some memory info.
143              */
144             private void printMemory(Class<? extends Test> testClass) {
145                 Runtime runtime = Runtime.getRuntime();
146 
147                 long total = runtime.totalMemory();
148                 long free = runtime.freeMemory();
149                 long used = total - free;
150 
151                 Log.d(TAG, "Total memory  : " + total);
152                 Log.d(TAG, "Used memory   : " + used);
153                 Log.d(TAG, "Free memory   : " + free);
154                 Log.d(TAG, "Now executing : " + testClass.getName());
155             }
156 
157             /**
158              * Nulls all non-static reference fields in the given test class.
159              * This method helps us with those test classes that don't have an
160              * explicit tearDown() method. Normally the garbage collector should
161              * take care of everything, but since JUnit keeps references to all
162              * test cases, a little help might be a good idea.
163              */
164             private void cleanup(TestCase test) {
165                 Class<?> clazz = test.getClass();
166 
167                 while (clazz != TestCase.class) {
168                     Field[] fields = clazz.getDeclaredFields();
169                     for (int i = 0; i < fields.length; i++) {
170                         Field f = fields[i];
171                         if (!f.getType().isPrimitive() &&
172                                 !Modifier.isStatic(f.getModifiers())) {
173                             try {
174                                 f.setAccessible(true);
175                                 f.set(test, null);
176                             } catch (Exception ignored) {
177                                 // Nothing we can do about it.
178                             }
179                         }
180                     }
181 
182                     clazz = clazz.getSuperclass();
183                 }
184             }
185 
186         });
187 
188         return runner;
189     }
190 }
191