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