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