diff --git a/test-runner/src/android/test/InstrumentationTestRunner.java b/test-runner/src/android/test/InstrumentationTestRunner.java index 4ae98e6010f35..e58601027d2d2 100644 --- a/test-runner/src/android/test/InstrumentationTestRunner.java +++ b/test-runner/src/android/test/InstrumentationTestRunner.java @@ -19,6 +19,7 @@ package android.test; import static android.test.suitebuilder.TestPredicates.REJECT_PERFORMANCE; import com.android.internal.util.Predicate; +import com.android.internal.util.Predicates; import android.app.Activity; import android.app.Instrumentation; @@ -31,11 +32,13 @@ import android.os.PerformanceCollector.PerformanceResultsWriter; import android.test.suitebuilder.TestMethod; import android.test.suitebuilder.TestPredicates; import android.test.suitebuilder.TestSuiteBuilder; +import android.test.suitebuilder.annotation.HasAnnotation; import android.util.Log; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.PrintStream; +import java.lang.annotation.Annotation; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; @@ -93,6 +96,18 @@ import junit.textui.ResultPrinter; * -e size large * com.android.foo/android.test.InstrumentationTestRunner *
+ * Filter test run to tests with given annotation: adb shell am instrument -w + * -e annotation com.android.foo.MyAnnotation + * com.android.foo/android.test.InstrumentationTestRunner + * + * If used with other options, the resulting test run will contain the union of the two options. + * e.g. "-e size large -e annotation com.android.foo.MyAnnotation" will run only tests with both + * the {@link LargeTest} and "com.android.foo.MyAnnotation" annotations. + * + * Filter test run to tests without given annotation: adb shell am instrument -w + * -e notAnnotation com.android.foo.MyAnnotation + * com.android.foo/android.test.InstrumentationTestRunner + * * Running a single testcase: adb shell am instrument -w * -e class com.android.foo.FooTest * com.android.foo/android.test.InstrumentationTestRunner @@ -161,6 +176,10 @@ public class InstrumentationTestRunner extends Instrumentation implements TestSu private static final String LARGE_SUITE = "large"; private static final String ARGUMENT_LOG_ONLY = "log"; + /** @hide */ + static final String ARGUMENT_ANNOTATION = "annotation"; + /** @hide */ + static final String ARGUMENT_NOT_ANNOTATION = "notAnnotation"; /** * This constant defines the maximum allowed runtime (in ms) for a test included in the "small" @@ -274,6 +293,8 @@ public class InstrumentationTestRunner extends Instrumentation implements TestSu ClassPathPackageInfoSource.setApkPaths(apkPaths); Predicatenull
+ */
+ private Predicatenull
+ */
+ private Predicatenull
+ */
+ private Class extends Annotation> getAnnotationClass(String annotationClassName) {
+ if (annotationClassName == null) {
+ return null;
+ }
+ try {
+ Class> annotationClass = Class.forName(annotationClassName);
+ if (annotationClass.isAnnotation()) {
+ return (Class extends Annotation>)annotationClass;
+ } else {
+ Log.e(LOG_TAG, String.format("Provided annotation value %s is not an Annotation",
+ annotationClassName));
+ }
+ } catch (ClassNotFoundException e) {
+ Log.e(LOG_TAG, String.format("Could not find class for specified annotation %s",
+ annotationClassName));
+ }
+ return null;
+ }
+
@Override
public void onStart() {
Looper.prepare();
@@ -471,7 +556,7 @@ public class InstrumentationTestRunner extends Instrumentation implements TestSu
String coverageFilePath = getCoverageFilePath();
java.io.File coverageFile = new java.io.File(coverageFilePath);
try {
- Class emmaRTClass = Class.forName("com.vladium.emma.rt.RT");
+ Class> emmaRTClass = Class.forName("com.vladium.emma.rt.RT");
Method dumpCoverageMethod = emmaRTClass.getMethod("dumpCoverageData",
coverageFile.getClass(), boolean.class, boolean.class);
diff --git a/test-runner/tests/src/android/test/InstrumentationTestRunnerTest.java b/test-runner/tests/src/android/test/InstrumentationTestRunnerTest.java
index d9afd545f822b..6db72ad5758a4 100644
--- a/test-runner/tests/src/android/test/InstrumentationTestRunnerTest.java
+++ b/test-runner/tests/src/android/test/InstrumentationTestRunnerTest.java
@@ -109,6 +109,33 @@ public class InstrumentationTestRunnerTest extends TestCase {
assertTrue(mStubAndroidTestRunner.isRun());
}
+ /**
+ * Test that the -e {@link InstrumentationTestRunner.ARGUMENT_ANNOTATION} parameter properly
+ * selects tests.
+ */
+ public void testAnnotationParameter() throws Exception {
+ String expectedTestClassName = AnnotationTest.class.getName();
+ Bundle args = new Bundle();
+ args.putString(InstrumentationTestRunner.ARGUMENT_TEST_CLASS, expectedTestClassName);
+ args.putString(InstrumentationTestRunner.ARGUMENT_ANNOTATION, FlakyTest.class.getName());
+ mInstrumentationTestRunner.onCreate(args);
+ assertTestRunnerCalledWithExpectedParameters(expectedTestClassName, "testAnnotated");
+ }
+
+ /**
+ * Test that the -e {@link InstrumentationTestRunner.ARGUMENT_NOT_ANNOTATION} parameter
+ * properly excludes tests.
+ */
+ public void testNotAnnotationParameter() throws Exception {
+ String expectedTestClassName = AnnotationTest.class.getName();
+ Bundle args = new Bundle();
+ args.putString(InstrumentationTestRunner.ARGUMENT_TEST_CLASS, expectedTestClassName);
+ args.putString(InstrumentationTestRunner.ARGUMENT_NOT_ANNOTATION,
+ FlakyTest.class.getName());
+ mInstrumentationTestRunner.onCreate(args);
+ assertTestRunnerCalledWithExpectedParameters(expectedTestClassName, "testNotAnnotated");
+ }
+
private void assertContentsInOrder(List