| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
| Fangqiu Su | 4c35c95 | 2023-12-19 18:14:19 +0000 | [diff] [blame] | 19 | import static android.test.suitebuilder.TestPredicates.hasAnnotation; |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 20 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import android.app.Activity; |
| 22 | import android.app.Instrumentation; |
| 23 | import android.os.Bundle; |
| 24 | import android.os.Debug; |
| 25 | import android.os.Looper; |
| 26 | import android.test.suitebuilder.TestMethod; |
| 27 | import android.test.suitebuilder.TestPredicates; |
| 28 | import android.test.suitebuilder.TestSuiteBuilder; |
| Doug Zongker | 0375fa1 | 2010-02-23 12:36:06 -0800 | [diff] [blame] | 29 | import android.test.suitebuilder.annotation.LargeTest; |
| Fangqiu Su | 4c35c95 | 2023-12-19 18:14:19 +0000 | [diff] [blame] | 30 | import android.test.suitebuilder.annotation.MediumTest; |
| 31 | import android.test.suitebuilder.annotation.SmallTest; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | import android.util.Log; |
| 33 | |
| Fangqiu Su | 4c35c95 | 2023-12-19 18:14:19 +0000 | [diff] [blame] | 34 | import com.android.internal.util.Predicate; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | |
| 36 | import junit.framework.AssertionFailedError; |
| 37 | import junit.framework.Test; |
| 38 | import junit.framework.TestCase; |
| 39 | import junit.framework.TestListener; |
| 40 | import junit.framework.TestResult; |
| 41 | import junit.framework.TestSuite; |
| 42 | import junit.runner.BaseTestRunner; |
| 43 | import junit.textui.ResultPrinter; |
| 44 | |
| Fangqiu Su | 4c35c95 | 2023-12-19 18:14:19 +0000 | [diff] [blame] | 45 | import java.io.ByteArrayOutputStream; |
| 46 | import java.io.File; |
| 47 | import java.io.PrintStream; |
| 48 | import java.lang.annotation.Annotation; |
| 49 | import java.lang.reflect.InvocationTargetException; |
| 50 | import java.lang.reflect.Method; |
| 51 | import java.util.ArrayList; |
| 52 | import java.util.List; |
| Paul Duffin | fedb4b7 | 2017-06-22 10:47:25 +0100 | [diff] [blame] | 53 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | /** |
| 55 | * An {@link Instrumentation} that runs various types of {@link junit.framework.TestCase}s against |
| Joe Fernandez | 3aef8e1d | 2011-12-20 10:38:34 -0800 | [diff] [blame] | 56 | * an Android package (application). |
| 57 | * |
| 58 | * <div class="special reference"> |
| 59 | * <h3>Developer Guides</h3> |
| 60 | * <p>For more information about application testing, read the |
| 61 | * <a href="{@docRoot}guide/topics/testing/index.html">Testing</a> developer guide.</p> |
| 62 | * </div> |
| 63 | * |
| 64 | * <h3>Typical Usage</h3> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | * <ol> |
| 66 | * <li>Write {@link junit.framework.TestCase}s that perform unit, functional, or performance tests |
| 67 | * against the classes in your package. Typically these are subclassed from: |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 68 | * <ul><li>{@link android.test.ActivityInstrumentationTestCase2}</li> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | * <li>{@link android.test.ActivityUnitTestCase}</li> |
| 70 | * <li>{@link android.test.AndroidTestCase}</li> |
| 71 | * <li>{@link android.test.ApplicationTestCase}</li> |
| 72 | * <li>{@link android.test.InstrumentationTestCase}</li> |
| 73 | * <li>{@link android.test.ProviderTestCase}</li> |
| 74 | * <li>{@link android.test.ServiceTestCase}</li> |
| 75 | * <li>{@link android.test.SingleLaunchActivityTestCase}</li></ul> |
| quddusc | e8df6f4 | 2014-01-06 16:28:03 -0800 | [diff] [blame] | 76 | * <li>Set the <code>android:targetPackage</code> attribute of the <code><instrumentation></code> |
| 77 | * element in the test package's manifest. You should set the attribute value |
| 78 | * to the package name of the target application under test. |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | * <li>Run the instrumentation using "adb shell am instrument -w", |
| 80 | * with no optional arguments, to run all tests (except performance tests). |
| 81 | * <li>Run the instrumentation using "adb shell am instrument -w", |
| 82 | * with the argument '-e func true' to run all functional tests. These are tests that derive from |
| 83 | * {@link android.test.InstrumentationTestCase}. |
| 84 | * <li>Run the instrumentation using "adb shell am instrument -w", |
| 85 | * with the argument '-e unit true' to run all unit tests. These are tests that <i>do not</i>derive |
| 86 | * from {@link android.test.InstrumentationTestCase} (and are not performance tests). |
| 87 | * <li>Run the instrumentation using "adb shell am instrument -w", |
| 88 | * with the argument '-e class' set to run an individual {@link junit.framework.TestCase}. |
| 89 | * </ol> |
| 90 | * <p/> |
| 91 | * <b>Running all tests:</b> adb shell am instrument -w |
| 92 | * com.android.foo/android.test.InstrumentationTestRunner |
| 93 | * <p/> |
| 94 | * <b>Running all small tests:</b> adb shell am instrument -w |
| 95 | * -e size small |
| 96 | * com.android.foo/android.test.InstrumentationTestRunner |
| 97 | * <p/> |
| 98 | * <b>Running all medium tests:</b> adb shell am instrument -w |
| 99 | * -e size medium |
| 100 | * com.android.foo/android.test.InstrumentationTestRunner |
| 101 | * <p/> |
| 102 | * <b>Running all large tests:</b> adb shell am instrument -w |
| 103 | * -e size large |
| 104 | * com.android.foo/android.test.InstrumentationTestRunner |
| 105 | * <p/> |
| Brett Chabot | 88e03a9 | 2010-02-19 09:57:11 -0800 | [diff] [blame] | 106 | * <b>Filter test run to tests with given annotation:</b> adb shell am instrument -w |
| 107 | * -e annotation com.android.foo.MyAnnotation |
| 108 | * com.android.foo/android.test.InstrumentationTestRunner |
| 109 | * <p/> |
| 110 | * If used with other options, the resulting test run will contain the union of the two options. |
| 111 | * e.g. "-e size large -e annotation com.android.foo.MyAnnotation" will run only tests with both |
| 112 | * the {@link LargeTest} and "com.android.foo.MyAnnotation" annotations. |
| 113 | * <p/> |
| 114 | * <b>Filter test run to tests <i>without</i> given annotation:</b> adb shell am instrument -w |
| 115 | * -e notAnnotation com.android.foo.MyAnnotation |
| 116 | * com.android.foo/android.test.InstrumentationTestRunner |
| 117 | * <p/> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | * <b>Running a single testcase:</b> adb shell am instrument -w |
| 119 | * -e class com.android.foo.FooTest |
| 120 | * com.android.foo/android.test.InstrumentationTestRunner |
| 121 | * <p/> |
| 122 | * <b>Running a single test:</b> adb shell am instrument -w |
| 123 | * -e class com.android.foo.FooTest#testFoo |
| 124 | * com.android.foo/android.test.InstrumentationTestRunner |
| 125 | * <p/> |
| 126 | * <b>Running multiple tests:</b> adb shell am instrument -w |
| 127 | * -e class com.android.foo.FooTest,com.android.foo.TooTest |
| 128 | * com.android.foo/android.test.InstrumentationTestRunner |
| 129 | * <p/> |
| Brett Chabot | 89c0ef4 | 2010-03-18 20:03:31 -0700 | [diff] [blame] | 130 | * <b>Running all tests in a java package:</b> adb shell am instrument -w |
| 131 | * -e package com.android.foo.subpkg |
| 132 | * com.android.foo/android.test.InstrumentationTestRunner |
| 133 | * <p/> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 134 | * <b>Including performance tests:</b> adb shell am instrument -w |
| 135 | * -e perf true |
| 136 | * com.android.foo/android.test.InstrumentationTestRunner |
| 137 | * <p/> |
| 138 | * <b>To debug your tests, set a break point in your code and pass:</b> |
| 139 | * -e debug true |
| 140 | * <p/> |
| 141 | * <b>To run in 'log only' mode</b> |
| 142 | * -e log true |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 143 | * This option will load and iterate through all test classes and methods, but will bypass actual |
| 144 | * test execution. Useful for quickly obtaining info on the tests to be executed by an |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | * instrumentation command. |
| 146 | * <p/> |
| 147 | * <b>To generate EMMA code coverage:</b> |
| 148 | * -e coverage true |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 149 | * Note: this requires an emma instrumented build. By default, the code coverage results file |
| Brett Chabot | 51e0364 | 2009-05-28 18:18:15 -0700 | [diff] [blame] | 150 | * will be saved in a /data/<app>/coverage.ec file, unless overridden by coverageFile flag (see |
| 151 | * below) |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | * <p/> |
| 153 | * <b> To specify EMMA code coverage results file path:</b> |
| 154 | * -e coverageFile /sdcard/myFile.ec |
| 155 | * <br/> |
| 156 | * in addition to the other arguments. |
| Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 157 | * @deprecated Use |
| 158 | * <a href="{@docRoot}reference/android/support/test/runner/AndroidJUnitRunner.html"> |
| 159 | * AndroidJUnitRunner</a> instead. New tests should be written using the |
| 160 | * <a href="{@docRoot}tools/testing-support-library/index.html">Android Testing Support Library</a>. |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 161 | */ |
| 162 | |
| 163 | /* (not JavaDoc) |
| 164 | * Although not necessary in most case, another way to use this class is to extend it and have the |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 165 | * derived class return the desired test suite from the {@link #getTestSuite()} method. The test |
| 166 | * suite returned from this method will be used if no target class is defined in the meta-data or |
| 167 | * command line argument parameters. If a derived class is used it needs to be added as an |
| 168 | * instrumentation to the AndroidManifest.xml and the command to run it would look like: |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | * <p/> |
| 170 | * adb shell am instrument -w com.android.foo/<i>com.android.FooInstrumentationTestRunner</i> |
| 171 | * <p/> |
| 172 | * Where <i>com.android.FooInstrumentationTestRunner</i> is the derived class. |
| 173 | * |
| 174 | * This model is used by many existing app tests, but can probably be deprecated. |
| 175 | */ |
| Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 176 | @Deprecated |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | public class InstrumentationTestRunner extends Instrumentation implements TestSuiteProvider { |
| 178 | |
| 179 | /** @hide */ |
| Paul Duffin | 575f9da | 2017-06-20 14:41:20 +0100 | [diff] [blame] | 180 | static final String ARGUMENT_TEST_CLASS = "class"; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | /** @hide */ |
| Paul Duffin | 575f9da | 2017-06-20 14:41:20 +0100 | [diff] [blame] | 182 | private static final String ARGUMENT_TEST_PACKAGE = "package"; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | /** @hide */ |
| Paul Duffin | 575f9da | 2017-06-20 14:41:20 +0100 | [diff] [blame] | 184 | private static final String ARGUMENT_TEST_SIZE_PREDICATE = "size"; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | /** @hide */ |
| Paul Duffin | 575f9da | 2017-06-20 14:41:20 +0100 | [diff] [blame] | 186 | static final String ARGUMENT_DELAY_MSEC = "delay_msec"; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | |
| 188 | private static final String SMALL_SUITE = "small"; |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 189 | private static final String MEDIUM_SUITE = "medium"; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | private static final String LARGE_SUITE = "large"; |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 191 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | private static final String ARGUMENT_LOG_ONLY = "log"; |
| Brett Chabot | 88e03a9 | 2010-02-19 09:57:11 -0800 | [diff] [blame] | 193 | /** @hide */ |
| 194 | static final String ARGUMENT_ANNOTATION = "annotation"; |
| 195 | /** @hide */ |
| 196 | static final String ARGUMENT_NOT_ANNOTATION = "notAnnotation"; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 197 | |
| Paul Duffin | fedb4b7 | 2017-06-22 10:47:25 +0100 | [diff] [blame] | 198 | private static final Predicate<TestMethod> SELECT_SMALL = hasAnnotation(SmallTest.class); |
| 199 | |
| 200 | private static final Predicate<TestMethod> SELECT_MEDIUM = hasAnnotation(MediumTest.class); |
| 201 | |
| 202 | private static final Predicate<TestMethod> SELECT_LARGE = hasAnnotation(LargeTest.class); |
| 203 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | /** |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 205 | * This constant defines the maximum allowed runtime (in ms) for a test included in the "small" |
| 206 | * suite. It is used to make an educated guess at what suite an unlabeled test belongs. |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 207 | */ |
| 208 | private static final float SMALL_SUITE_MAX_RUNTIME = 100; |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 209 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 | /** |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 211 | * This constant defines the maximum allowed runtime (in ms) for a test included in the |
| 212 | * "medium" suite. It is used to make an educated guess at what suite an unlabeled test belongs. |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 213 | */ |
| 214 | private static final float MEDIUM_SUITE_MAX_RUNTIME = 1000; |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 215 | |
| Paul Duffin | 575f9da | 2017-06-20 14:41:20 +0100 | [diff] [blame] | 216 | /* |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 217 | * The following keys are used in the status bundle to provide structured reports to |
| 218 | * an IInstrumentationWatcher. |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | */ |
| 220 | |
| 221 | /** |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 222 | * This value, if stored with key {@link android.app.Instrumentation#REPORT_KEY_IDENTIFIER}, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | * identifies InstrumentationTestRunner as the source of the report. This is sent with all |
| 224 | * status messages. |
| 225 | */ |
| 226 | public static final String REPORT_VALUE_ID = "InstrumentationTestRunner"; |
| 227 | /** |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 228 | * If included in the status or final bundle sent to an IInstrumentationWatcher, this key |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | * identifies the total number of tests that are being run. This is sent with all status |
| 230 | * messages. |
| 231 | */ |
| 232 | public static final String REPORT_KEY_NUM_TOTAL = "numtests"; |
| 233 | /** |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 234 | * If included in the status or final bundle sent to an IInstrumentationWatcher, this key |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 235 | * identifies the sequence number of the current test. This is sent with any status message |
| 236 | * describing a specific test being started or completed. |
| 237 | */ |
| 238 | public static final String REPORT_KEY_NUM_CURRENT = "current"; |
| 239 | /** |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 240 | * If included in the status or final bundle sent to an IInstrumentationWatcher, this key |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | * identifies the name of the current test class. This is sent with any status message |
| 242 | * describing a specific test being started or completed. |
| 243 | */ |
| 244 | public static final String REPORT_KEY_NAME_CLASS = "class"; |
| 245 | /** |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 246 | * If included in the status or final bundle sent to an IInstrumentationWatcher, this key |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | * identifies the name of the current test. This is sent with any status message |
| 248 | * describing a specific test being started or completed. |
| 249 | */ |
| 250 | public static final String REPORT_KEY_NAME_TEST = "test"; |
| 251 | /** |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 252 | * If included in the status or final bundle sent to an IInstrumentationWatcher, this key |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 253 | * reports the run time in seconds of the current test. |
| 254 | */ |
| 255 | private static final String REPORT_KEY_RUN_TIME = "runtime"; |
| 256 | /** |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 257 | * If included in the status or final bundle sent to an IInstrumentationWatcher, this key |
| Jack Wang | 3fc03e6 | 2010-10-19 15:13:07 -0700 | [diff] [blame] | 258 | * reports the number of total iterations of the current test. |
| 259 | */ |
| 260 | private static final String REPORT_KEY_NUM_ITERATIONS = "numiterations"; |
| 261 | /** |
| 262 | * If included in the status or final bundle sent to an IInstrumentationWatcher, this key |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 263 | * reports the guessed suite assignment for the current test. |
| 264 | */ |
| 265 | private static final String REPORT_KEY_SUITE_ASSIGNMENT = "suiteassignment"; |
| 266 | /** |
| Brett Chabot | 51e0364 | 2009-05-28 18:18:15 -0700 | [diff] [blame] | 267 | * If included in the status or final bundle sent to an IInstrumentationWatcher, this key |
| 268 | * identifies the path to the generated code coverage file. |
| 269 | */ |
| 270 | private static final String REPORT_KEY_COVERAGE_PATH = "coverageFilePath"; |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 271 | |
| 272 | /** |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 273 | * The test is starting. |
| 274 | */ |
| 275 | public static final int REPORT_VALUE_RESULT_START = 1; |
| 276 | /** |
| 277 | * The test completed successfully. |
| 278 | */ |
| 279 | public static final int REPORT_VALUE_RESULT_OK = 0; |
| 280 | /** |
| 281 | * The test completed with an error. |
| 282 | */ |
| 283 | public static final int REPORT_VALUE_RESULT_ERROR = -1; |
| 284 | /** |
| 285 | * The test completed with a failure. |
| 286 | */ |
| 287 | public static final int REPORT_VALUE_RESULT_FAILURE = -2; |
| 288 | /** |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 289 | * If included in the status bundle sent to an IInstrumentationWatcher, this key |
| 290 | * identifies a stack trace describing an error or failure. This is sent with any status |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 291 | * message describing a specific test being completed. |
| 292 | */ |
| 293 | public static final String REPORT_KEY_STACK = "stack"; |
| 294 | |
| Brett Chabot | 51e0364 | 2009-05-28 18:18:15 -0700 | [diff] [blame] | 295 | // Default file name for code coverage |
| 296 | private static final String DEFAULT_COVERAGE_FILE_NAME = "coverage.ec"; |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 297 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 298 | private static final String LOG_TAG = "InstrumentationTestRunner"; |
| 299 | |
| 300 | private final Bundle mResults = new Bundle(); |
| David Hu | cbc584d | 2011-09-16 18:08:35 -0700 | [diff] [blame] | 301 | private Bundle mArguments; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 302 | private AndroidTestRunner mTestRunner; |
| 303 | private boolean mDebug; |
| 304 | private boolean mJustCount; |
| 305 | private boolean mSuiteAssignmentMode; |
| 306 | private int mTestCount; |
| 307 | private String mPackageOfTests; |
| 308 | private boolean mCoverage; |
| 309 | private String mCoverageFilePath; |
| 310 | private int mDelayMsec; |
| 311 | |
| 312 | @Override |
| 313 | public void onCreate(Bundle arguments) { |
| 314 | super.onCreate(arguments); |
| David Hu | cbc584d | 2011-09-16 18:08:35 -0700 | [diff] [blame] | 315 | mArguments = arguments; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 316 | |
| 317 | // Apk paths used to search for test classes when using TestSuiteBuilders. |
| 318 | String[] apkPaths = |
| 319 | {getTargetContext().getPackageCodePath(), getContext().getPackageCodePath()}; |
| 320 | ClassPathPackageInfoSource.setApkPaths(apkPaths); |
| 321 | |
| 322 | Predicate<TestMethod> testSizePredicate = null; |
| Brett Chabot | 88e03a9 | 2010-02-19 09:57:11 -0800 | [diff] [blame] | 323 | Predicate<TestMethod> testAnnotationPredicate = null; |
| 324 | Predicate<TestMethod> testNotAnnotationPredicate = null; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 325 | String testClassesArg = null; |
| 326 | boolean logOnly = false; |
| 327 | |
| 328 | if (arguments != null) { |
| 329 | // Test class name passed as an argument should override any meta-data declaration. |
| 330 | testClassesArg = arguments.getString(ARGUMENT_TEST_CLASS); |
| 331 | mDebug = getBooleanArgument(arguments, "debug"); |
| 332 | mJustCount = getBooleanArgument(arguments, "count"); |
| 333 | mSuiteAssignmentMode = getBooleanArgument(arguments, "suiteAssignment"); |
| 334 | mPackageOfTests = arguments.getString(ARGUMENT_TEST_PACKAGE); |
| 335 | testSizePredicate = getSizePredicateFromArg( |
| 336 | arguments.getString(ARGUMENT_TEST_SIZE_PREDICATE)); |
| Brett Chabot | 88e03a9 | 2010-02-19 09:57:11 -0800 | [diff] [blame] | 337 | testAnnotationPredicate = getAnnotationPredicate( |
| 338 | arguments.getString(ARGUMENT_ANNOTATION)); |
| 339 | testNotAnnotationPredicate = getNotAnnotationPredicate( |
| 340 | arguments.getString(ARGUMENT_NOT_ANNOTATION)); |
| 341 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 342 | logOnly = getBooleanArgument(arguments, ARGUMENT_LOG_ONLY); |
| 343 | mCoverage = getBooleanArgument(arguments, "coverage"); |
| 344 | mCoverageFilePath = arguments.getString("coverageFile"); |
| 345 | |
| 346 | try { |
| 347 | Object delay = arguments.get(ARGUMENT_DELAY_MSEC); // Accept either string or int |
| 348 | if (delay != null) mDelayMsec = Integer.parseInt(delay.toString()); |
| 349 | } catch (NumberFormatException e) { |
| 350 | Log.e(LOG_TAG, "Invalid delay_msec parameter", e); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | TestSuiteBuilder testSuiteBuilder = new TestSuiteBuilder(getClass().getName(), |
| 355 | getTargetContext().getClassLoader()); |
| 356 | |
| 357 | if (testSizePredicate != null) { |
| 358 | testSuiteBuilder.addRequirements(testSizePredicate); |
| 359 | } |
| Brett Chabot | 88e03a9 | 2010-02-19 09:57:11 -0800 | [diff] [blame] | 360 | if (testAnnotationPredicate != null) { |
| 361 | testSuiteBuilder.addRequirements(testAnnotationPredicate); |
| 362 | } |
| 363 | if (testNotAnnotationPredicate != null) { |
| 364 | testSuiteBuilder.addRequirements(testNotAnnotationPredicate); |
| 365 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 366 | |
| 367 | if (testClassesArg == null) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 368 | if (mPackageOfTests != null) { |
| 369 | testSuiteBuilder.includePackages(mPackageOfTests); |
| 370 | } else { |
| Brett Chabot | 61b10ac | 2009-03-31 17:04:34 -0700 | [diff] [blame] | 371 | TestSuite testSuite = getTestSuite(); |
| 372 | if (testSuite != null) { |
| 373 | testSuiteBuilder.addTestSuite(testSuite); |
| 374 | } else { |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 375 | // no package or class bundle arguments were supplied, and no test suite |
| Brett Chabot | 61b10ac | 2009-03-31 17:04:34 -0700 | [diff] [blame] | 376 | // provided so add all tests in application |
| 377 | testSuiteBuilder.includePackages(""); |
| 378 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 379 | } |
| 380 | } else { |
| 381 | parseTestClasses(testClassesArg, testSuiteBuilder); |
| 382 | } |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 383 | |
| Urs Grob | da13ef5 | 2009-04-17 11:30:14 -0700 | [diff] [blame] | 384 | testSuiteBuilder.addRequirements(getBuilderRequirements()); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 385 | |
| 386 | mTestRunner = getAndroidTestRunner(); |
| 387 | mTestRunner.setContext(getTargetContext()); |
| Jack Wang | 7aba54b | 2009-08-20 19:20:54 -0700 | [diff] [blame] | 388 | mTestRunner.setInstrumentation(this); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 389 | mTestRunner.setSkipExecution(logOnly); |
| 390 | mTestRunner.setTest(testSuiteBuilder.build()); |
| 391 | mTestCount = mTestRunner.getTestCases().size(); |
| 392 | if (mSuiteAssignmentMode) { |
| 393 | mTestRunner.addTestListener(new SuiteAssignmentPrinter()); |
| 394 | } else { |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 395 | WatcherResultPrinter resultPrinter = new WatcherResultPrinter(mTestCount); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 396 | mTestRunner.addTestListener(new TestPrinter("TestRunner", false)); |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 397 | mTestRunner.addTestListener(resultPrinter); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 398 | } |
| 399 | start(); |
| 400 | } |
| 401 | |
| David Hu | cbc584d | 2011-09-16 18:08:35 -0700 | [diff] [blame] | 402 | /** |
| Svetoslav Ganov | 80943d8 | 2013-01-02 10:25:37 -0800 | [diff] [blame] | 403 | * Get the arguments passed to this instrumentation. |
| David Hu | cbc584d | 2011-09-16 18:08:35 -0700 | [diff] [blame] | 404 | * |
| 405 | * @return the Bundle object |
| David Hu | cbc584d | 2011-09-16 18:08:35 -0700 | [diff] [blame] | 406 | */ |
| Svetoslav | 3a34d17 | 2013-01-28 19:55:35 -0800 | [diff] [blame] | 407 | public Bundle getArguments() { |
| David Hu | cbc584d | 2011-09-16 18:08:35 -0700 | [diff] [blame] | 408 | return mArguments; |
| 409 | } |
| 410 | |
| David Hu | 8cc9a8e | 2011-12-13 15:57:42 -0800 | [diff] [blame] | 411 | /** |
| 412 | * Add a {@link TestListener} |
| David Hu | 8cc9a8e | 2011-12-13 15:57:42 -0800 | [diff] [blame] | 413 | */ |
| 414 | protected void addTestListener(TestListener listener){ |
| 415 | if(mTestRunner!=null && listener!=null){ |
| 416 | mTestRunner.addTestListener(listener); |
| 417 | } |
| 418 | } |
| 419 | |
| Urs Grob | da13ef5 | 2009-04-17 11:30:14 -0700 | [diff] [blame] | 420 | List<Predicate<TestMethod>> getBuilderRequirements() { |
| 421 | return new ArrayList<Predicate<TestMethod>>(); |
| 422 | } |
| 423 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 424 | /** |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 425 | * Parses and loads the specified set of test classes |
| 426 | * |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 427 | * @param testClassArg - comma-separated list of test classes and methods |
| 428 | * @param testSuiteBuilder - builder to add tests to |
| 429 | */ |
| 430 | private void parseTestClasses(String testClassArg, TestSuiteBuilder testSuiteBuilder) { |
| 431 | String[] testClasses = testClassArg.split(","); |
| 432 | for (String testClass : testClasses) { |
| 433 | parseTestClass(testClass, testSuiteBuilder); |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * Parse and load the given test class and, optionally, method |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 439 | * |
| 440 | * @param testClassName - full package name of test class and optionally method to add. |
| 441 | * Expected format: com.android.TestClass#testMethod |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 442 | * @param testSuiteBuilder - builder to add tests to |
| 443 | */ |
| 444 | private void parseTestClass(String testClassName, TestSuiteBuilder testSuiteBuilder) { |
| 445 | int methodSeparatorIndex = testClassName.indexOf('#'); |
| 446 | String testMethodName = null; |
| 447 | |
| 448 | if (methodSeparatorIndex > 0) { |
| 449 | testMethodName = testClassName.substring(methodSeparatorIndex + 1); |
| 450 | testClassName = testClassName.substring(0, methodSeparatorIndex); |
| 451 | } |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 452 | testSuiteBuilder.addTestClassByName(testClassName, testMethodName, getTargetContext()); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | protected AndroidTestRunner getAndroidTestRunner() { |
| 456 | return new AndroidTestRunner(); |
| 457 | } |
| 458 | |
| 459 | private boolean getBooleanArgument(Bundle arguments, String tag) { |
| 460 | String tagString = arguments.getString(tag); |
| 461 | return tagString != null && Boolean.parseBoolean(tagString); |
| 462 | } |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 463 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 464 | /* |
| 465 | * Returns the size predicate object, corresponding to the "size" argument value. |
| 466 | */ |
| 467 | private Predicate<TestMethod> getSizePredicateFromArg(String sizeArg) { |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 468 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 469 | if (SMALL_SUITE.equals(sizeArg)) { |
| Paul Duffin | fedb4b7 | 2017-06-22 10:47:25 +0100 | [diff] [blame] | 470 | return SELECT_SMALL; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 471 | } else if (MEDIUM_SUITE.equals(sizeArg)) { |
| Paul Duffin | fedb4b7 | 2017-06-22 10:47:25 +0100 | [diff] [blame] | 472 | return SELECT_MEDIUM; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 473 | } else if (LARGE_SUITE.equals(sizeArg)) { |
| Paul Duffin | fedb4b7 | 2017-06-22 10:47:25 +0100 | [diff] [blame] | 474 | return SELECT_LARGE; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 475 | } else { |
| 476 | return null; |
| 477 | } |
| 478 | } |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 479 | |
| Brett Chabot | 88e03a9 | 2010-02-19 09:57:11 -0800 | [diff] [blame] | 480 | /** |
| 481 | * Returns the test predicate object, corresponding to the annotation class value provided via |
| Paul Duffin | 575f9da | 2017-06-20 14:41:20 +0100 | [diff] [blame] | 482 | * the {@link #ARGUMENT_ANNOTATION} argument. |
| Brett Chabot | 88e03a9 | 2010-02-19 09:57:11 -0800 | [diff] [blame] | 483 | * |
| 484 | * @return the predicate or <code>null</code> |
| 485 | */ |
| 486 | private Predicate<TestMethod> getAnnotationPredicate(String annotationClassName) { |
| 487 | Class<? extends Annotation> annotationClass = getAnnotationClass(annotationClassName); |
| 488 | if (annotationClass != null) { |
| Paul Duffin | fedb4b7 | 2017-06-22 10:47:25 +0100 | [diff] [blame] | 489 | return hasAnnotation(annotationClass); |
| Brett Chabot | 88e03a9 | 2010-02-19 09:57:11 -0800 | [diff] [blame] | 490 | } |
| 491 | return null; |
| 492 | } |
| 493 | |
| 494 | /** |
| 495 | * Returns the negative test predicate object, corresponding to the annotation class value |
| Paul Duffin | 575f9da | 2017-06-20 14:41:20 +0100 | [diff] [blame] | 496 | * provided via the {@link #ARGUMENT_NOT_ANNOTATION} argument. |
| Brett Chabot | 88e03a9 | 2010-02-19 09:57:11 -0800 | [diff] [blame] | 497 | * |
| 498 | * @return the predicate or <code>null</code> |
| 499 | */ |
| 500 | private Predicate<TestMethod> getNotAnnotationPredicate(String annotationClassName) { |
| 501 | Class<? extends Annotation> annotationClass = getAnnotationClass(annotationClassName); |
| 502 | if (annotationClass != null) { |
| Paul Duffin | fedb4b7 | 2017-06-22 10:47:25 +0100 | [diff] [blame] | 503 | return TestPredicates.not(hasAnnotation(annotationClass)); |
| Brett Chabot | 88e03a9 | 2010-02-19 09:57:11 -0800 | [diff] [blame] | 504 | } |
| 505 | return null; |
| 506 | } |
| 507 | |
| 508 | /** |
| 509 | * Helper method to return the annotation class with specified name |
| 510 | * |
| 511 | * @param annotationClassName the fully qualified name of the class |
| 512 | * @return the annotation class or <code>null</code> |
| 513 | */ |
| 514 | private Class<? extends Annotation> getAnnotationClass(String annotationClassName) { |
| 515 | if (annotationClassName == null) { |
| 516 | return null; |
| 517 | } |
| 518 | try { |
| 519 | Class<?> annotationClass = Class.forName(annotationClassName); |
| 520 | if (annotationClass.isAnnotation()) { |
| 521 | return (Class<? extends Annotation>)annotationClass; |
| 522 | } else { |
| 523 | Log.e(LOG_TAG, String.format("Provided annotation value %s is not an Annotation", |
| 524 | annotationClassName)); |
| 525 | } |
| 526 | } catch (ClassNotFoundException e) { |
| 527 | Log.e(LOG_TAG, String.format("Could not find class for specified annotation %s", |
| 528 | annotationClassName)); |
| 529 | } |
| 530 | return null; |
| 531 | } |
| 532 | |
| Brett Chabot | 31e7ce7 | 2010-07-07 17:19:08 -0700 | [diff] [blame] | 533 | /** |
| 534 | * Initialize the current thread as a looper. |
| 535 | * <p/> |
| 536 | * Exposed for unit testing. |
| 537 | */ |
| 538 | void prepareLooper() { |
| 539 | Looper.prepare(); |
| 540 | } |
| 541 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 542 | @Override |
| 543 | public void onStart() { |
| Brett Chabot | 31e7ce7 | 2010-07-07 17:19:08 -0700 | [diff] [blame] | 544 | prepareLooper(); |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 545 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 546 | if (mJustCount) { |
| 547 | mResults.putString(Instrumentation.REPORT_KEY_IDENTIFIER, REPORT_VALUE_ID); |
| 548 | mResults.putInt(REPORT_KEY_NUM_TOTAL, mTestCount); |
| 549 | finish(Activity.RESULT_OK, mResults); |
| 550 | } else { |
| 551 | if (mDebug) { |
| 552 | Debug.waitForDebugger(); |
| 553 | } |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 554 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 555 | ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); |
| 556 | PrintStream writer = new PrintStream(byteArrayOutputStream); |
| 557 | try { |
| 558 | StringResultPrinter resultPrinter = new StringResultPrinter(writer); |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 559 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 560 | mTestRunner.addTestListener(resultPrinter); |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 561 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 562 | long startTime = System.currentTimeMillis(); |
| 563 | mTestRunner.runTest(); |
| 564 | long runTime = System.currentTimeMillis() - startTime; |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 565 | |
| Brett Chabot | 41e173d | 2012-10-24 14:34:07 -0700 | [diff] [blame] | 566 | resultPrinter.printResult(mTestRunner.getTestResult(), runTime); |
| Brett Chabot | 31e7ce7 | 2010-07-07 17:19:08 -0700 | [diff] [blame] | 567 | } catch (Throwable t) { |
| 568 | // catch all exceptions so a more verbose error message can be outputted |
| 569 | writer.println(String.format("Test run aborted due to unexpected exception: %s", |
| 570 | t.getMessage())); |
| 571 | t.printStackTrace(writer); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 572 | } finally { |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 573 | mResults.putString(Instrumentation.REPORT_KEY_STREAMRESULT, |
| 574 | String.format("\nTest results for %s=%s", |
| 575 | mTestRunner.getTestClassName(), |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 576 | byteArrayOutputStream.toString())); |
| 577 | |
| 578 | if (mCoverage) { |
| 579 | generateCoverageReport(); |
| 580 | } |
| 581 | writer.close(); |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 582 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 583 | finish(Activity.RESULT_OK, mResults); |
| 584 | } |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | public TestSuite getTestSuite() { |
| 589 | return getAllTests(); |
| 590 | } |
| 591 | |
| 592 | /** |
| 593 | * Override this to define all of the tests to run in your package. |
| 594 | */ |
| 595 | public TestSuite getAllTests() { |
| 596 | return null; |
| 597 | } |
| 598 | |
| 599 | /** |
| 600 | * Override this to provide access to the class loader of your package. |
| 601 | */ |
| 602 | public ClassLoader getLoader() { |
| 603 | return null; |
| 604 | } |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 605 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 606 | private void generateCoverageReport() { |
| 607 | // use reflection to call emma dump coverage method, to avoid |
| 608 | // always statically compiling against emma jar |
| Brett Chabot | 51e0364 | 2009-05-28 18:18:15 -0700 | [diff] [blame] | 609 | String coverageFilePath = getCoverageFilePath(); |
| 610 | java.io.File coverageFile = new java.io.File(coverageFilePath); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 611 | try { |
| Brett Chabot | 88e03a9 | 2010-02-19 09:57:11 -0800 | [diff] [blame] | 612 | Class<?> emmaRTClass = Class.forName("com.vladium.emma.rt.RT"); |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 613 | Method dumpCoverageMethod = emmaRTClass.getMethod("dumpCoverageData", |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 614 | coverageFile.getClass(), boolean.class, boolean.class); |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 615 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 616 | dumpCoverageMethod.invoke(null, coverageFile, false, false); |
| Brett Chabot | 51e0364 | 2009-05-28 18:18:15 -0700 | [diff] [blame] | 617 | // output path to generated coverage file so it can be parsed by a test harness if |
| 618 | // needed |
| 619 | mResults.putString(REPORT_KEY_COVERAGE_PATH, coverageFilePath); |
| 620 | // also output a more user friendly msg |
| Brett Chabot | 08d13c3 | 2010-02-18 15:42:13 -0800 | [diff] [blame] | 621 | final String currentStream = mResults.getString( |
| 622 | Instrumentation.REPORT_KEY_STREAMRESULT); |
| Brett Chabot | 51e0364 | 2009-05-28 18:18:15 -0700 | [diff] [blame] | 623 | mResults.putString(Instrumentation.REPORT_KEY_STREAMRESULT, |
| Brett Chabot | 08d13c3 | 2010-02-18 15:42:13 -0800 | [diff] [blame] | 624 | String.format("%s\nGenerated code coverage data to %s", currentStream, |
| 625 | coverageFilePath)); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 626 | } catch (ClassNotFoundException e) { |
| 627 | reportEmmaError("Is emma jar on classpath?", e); |
| 628 | } catch (SecurityException e) { |
| 629 | reportEmmaError(e); |
| 630 | } catch (NoSuchMethodException e) { |
| 631 | reportEmmaError(e); |
| 632 | } catch (IllegalArgumentException e) { |
| 633 | reportEmmaError(e); |
| 634 | } catch (IllegalAccessException e) { |
| 635 | reportEmmaError(e); |
| 636 | } catch (InvocationTargetException e) { |
| 637 | reportEmmaError(e); |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | private String getCoverageFilePath() { |
| 642 | if (mCoverageFilePath == null) { |
| Brett Chabot | 51e0364 | 2009-05-28 18:18:15 -0700 | [diff] [blame] | 643 | return getTargetContext().getFilesDir().getAbsolutePath() + File.separator + |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 644 | DEFAULT_COVERAGE_FILE_NAME; |
| 645 | } else { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 646 | return mCoverageFilePath; |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | private void reportEmmaError(Exception e) { |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 651 | reportEmmaError("", e); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | private void reportEmmaError(String hint, Exception e) { |
| 655 | String msg = "Failed to generate emma coverage. " + hint; |
| 656 | Log.e(LOG_TAG, msg, e); |
| 657 | mResults.putString(Instrumentation.REPORT_KEY_STREAMRESULT, "\nError: " + msg); |
| 658 | } |
| 659 | |
| 660 | // TODO kill this, use status() and prettyprint model for better output |
| 661 | private class StringResultPrinter extends ResultPrinter { |
| 662 | |
| 663 | public StringResultPrinter(PrintStream writer) { |
| 664 | super(writer); |
| 665 | } |
| 666 | |
| Brett Chabot | 41e173d | 2012-10-24 14:34:07 -0700 | [diff] [blame] | 667 | public synchronized void printResult(TestResult result, long runTime) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 668 | printHeader(runTime); |
| 669 | printFooter(result); |
| 670 | } |
| 671 | } |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 672 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 673 | /** |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 674 | * This class sends status reports back to the IInstrumentationWatcher about |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 675 | * which suite each test belongs. |
| 676 | */ |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 677 | private class SuiteAssignmentPrinter implements TestListener { |
| 678 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 679 | private Bundle mTestResult; |
| 680 | private long mStartTime; |
| 681 | private long mEndTime; |
| 682 | private boolean mTimingValid; |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 683 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 684 | public SuiteAssignmentPrinter() { |
| 685 | } |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 686 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 687 | /** |
| 688 | * send a status for the start of a each test, so long tests can be seen as "running" |
| 689 | */ |
| 690 | public void startTest(Test test) { |
| 691 | mTimingValid = true; |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 692 | mStartTime = System.currentTimeMillis(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 693 | } |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 694 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 695 | /** |
| 696 | * @see junit.framework.TestListener#addError(Test, Throwable) |
| 697 | */ |
| 698 | public void addError(Test test, Throwable t) { |
| 699 | mTimingValid = false; |
| 700 | } |
| 701 | |
| 702 | /** |
| 703 | * @see junit.framework.TestListener#addFailure(Test, AssertionFailedError) |
| 704 | */ |
| 705 | public void addFailure(Test test, AssertionFailedError t) { |
| 706 | mTimingValid = false; |
| 707 | } |
| 708 | |
| 709 | /** |
| 710 | * @see junit.framework.TestListener#endTest(Test) |
| 711 | */ |
| 712 | public void endTest(Test test) { |
| 713 | float runTime; |
| 714 | String assignmentSuite; |
| 715 | mEndTime = System.currentTimeMillis(); |
| 716 | mTestResult = new Bundle(); |
| 717 | |
| 718 | if (!mTimingValid || mStartTime < 0) { |
| 719 | assignmentSuite = "NA"; |
| 720 | runTime = -1; |
| 721 | } else { |
| 722 | runTime = mEndTime - mStartTime; |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 723 | if (runTime < SMALL_SUITE_MAX_RUNTIME |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 724 | && !InstrumentationTestCase.class.isAssignableFrom(test.getClass())) { |
| 725 | assignmentSuite = SMALL_SUITE; |
| 726 | } else if (runTime < MEDIUM_SUITE_MAX_RUNTIME) { |
| 727 | assignmentSuite = MEDIUM_SUITE; |
| 728 | } else { |
| 729 | assignmentSuite = LARGE_SUITE; |
| 730 | } |
| 731 | } |
| 732 | // Clear mStartTime so that we can verify that it gets set next time. |
| 733 | mStartTime = -1; |
| 734 | |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 735 | mTestResult.putString(Instrumentation.REPORT_KEY_STREAMRESULT, |
| 736 | test.getClass().getName() + "#" + ((TestCase) test).getName() |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 737 | + "\nin " + assignmentSuite + " suite\nrunTime: " |
| 738 | + String.valueOf(runTime) + "\n"); |
| 739 | mTestResult.putFloat(REPORT_KEY_RUN_TIME, runTime); |
| 740 | mTestResult.putString(REPORT_KEY_SUITE_ASSIGNMENT, assignmentSuite); |
| 741 | |
| 742 | sendStatus(0, mTestResult); |
| 743 | } |
| 744 | } |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 745 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 746 | /** |
| 747 | * This class sends status reports back to the IInstrumentationWatcher |
| 748 | */ |
| Paul Duffin | 5361c48 | 2017-06-15 14:50:08 +0100 | [diff] [blame] | 749 | private class WatcherResultPrinter implements TestListener { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 750 | private final Bundle mResultTemplate; |
| 751 | Bundle mTestResult; |
| 752 | int mTestNum = 0; |
| 753 | int mTestResultCode = 0; |
| 754 | String mTestClass = null; |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 755 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 756 | public WatcherResultPrinter(int numTests) { |
| 757 | mResultTemplate = new Bundle(); |
| 758 | mResultTemplate.putString(Instrumentation.REPORT_KEY_IDENTIFIER, REPORT_VALUE_ID); |
| 759 | mResultTemplate.putInt(REPORT_KEY_NUM_TOTAL, numTests); |
| 760 | } |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 761 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 762 | /** |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 763 | * send a status for the start of a each test, so long tests can be seen |
| 764 | * as "running" |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 765 | */ |
| 766 | public void startTest(Test test) { |
| 767 | String testClass = test.getClass().getName(); |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 768 | String testName = ((TestCase)test).getName(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 769 | mTestResult = new Bundle(mResultTemplate); |
| 770 | mTestResult.putString(REPORT_KEY_NAME_CLASS, testClass); |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 771 | mTestResult.putString(REPORT_KEY_NAME_TEST, testName); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 772 | mTestResult.putInt(REPORT_KEY_NUM_CURRENT, ++mTestNum); |
| 773 | // pretty printing |
| 774 | if (testClass != null && !testClass.equals(mTestClass)) { |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 775 | mTestResult.putString(Instrumentation.REPORT_KEY_STREAMRESULT, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 776 | String.format("\n%s:", testClass)); |
| 777 | mTestClass = testClass; |
| 778 | } else { |
| 779 | mTestResult.putString(Instrumentation.REPORT_KEY_STREAMRESULT, ""); |
| 780 | } |
| 781 | |
| Jack Wang | 3fc03e6 | 2010-10-19 15:13:07 -0700 | [diff] [blame] | 782 | Method testMethod = null; |
| 783 | try { |
| 784 | testMethod = test.getClass().getMethod(testName); |
| 785 | // Report total number of iterations, if test is repetitive |
| 786 | if (testMethod.isAnnotationPresent(RepetitiveTest.class)) { |
| 787 | int numIterations = testMethod.getAnnotation( |
| 788 | RepetitiveTest.class).numIterations(); |
| 789 | mTestResult.putInt(REPORT_KEY_NUM_ITERATIONS, numIterations); |
| 790 | } |
| 791 | } catch (NoSuchMethodException e) { |
| 792 | // ignore- the test with given name does not exist. Will be handled during test |
| 793 | // execution |
| 794 | } |
| 795 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 796 | // The delay_msec parameter is normally used to provide buffers of idle time |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 797 | // for power measurement purposes. To make sure there is a delay before and after |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 798 | // every test in a suite, we delay *after* every test (see endTest below) and also |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 799 | // delay *before* the first test. So, delay test1 delay test2 delay. |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 800 | |
| 801 | try { |
| 802 | if (mTestNum == 1) Thread.sleep(mDelayMsec); |
| 803 | } catch (InterruptedException e) { |
| 804 | throw new IllegalStateException(e); |
| 805 | } |
| 806 | |
| 807 | sendStatus(REPORT_VALUE_RESULT_START, mTestResult); |
| 808 | mTestResultCode = 0; |
| 809 | } |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 810 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 811 | /** |
| 812 | * @see junit.framework.TestListener#addError(Test, Throwable) |
| 813 | */ |
| 814 | public void addError(Test test, Throwable t) { |
| 815 | mTestResult.putString(REPORT_KEY_STACK, BaseTestRunner.getFilteredTrace(t)); |
| 816 | mTestResultCode = REPORT_VALUE_RESULT_ERROR; |
| 817 | // pretty printing |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 818 | mTestResult.putString(Instrumentation.REPORT_KEY_STREAMRESULT, |
| 819 | String.format("\nError in %s:\n%s", |
| 820 | ((TestCase)test).getName(), BaseTestRunner.getFilteredTrace(t))); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | /** |
| 824 | * @see junit.framework.TestListener#addFailure(Test, AssertionFailedError) |
| 825 | */ |
| 826 | public void addFailure(Test test, AssertionFailedError t) { |
| 827 | mTestResult.putString(REPORT_KEY_STACK, BaseTestRunner.getFilteredTrace(t)); |
| 828 | mTestResultCode = REPORT_VALUE_RESULT_FAILURE; |
| 829 | // pretty printing |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 830 | mTestResult.putString(Instrumentation.REPORT_KEY_STREAMRESULT, |
| 831 | String.format("\nFailure in %s:\n%s", |
| 832 | ((TestCase)test).getName(), BaseTestRunner.getFilteredTrace(t))); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | /** |
| 836 | * @see junit.framework.TestListener#endTest(Test) |
| 837 | */ |
| 838 | public void endTest(Test test) { |
| 839 | if (mTestResultCode == 0) { |
| 840 | mTestResult.putString(Instrumentation.REPORT_KEY_STREAMRESULT, "."); |
| 841 | } |
| 842 | sendStatus(mTestResultCode, mTestResult); |
| 843 | |
| Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 844 | try { // Sleep after every test, if specified |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 845 | Thread.sleep(mDelayMsec); |
| 846 | } catch (InterruptedException e) { |
| 847 | throw new IllegalStateException(e); |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | // TODO report the end of the cycle |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 852 | } |
| 853 | } |