diff options
author | Brett Chabot <brettchabot@android.com> | 2010-02-19 09:57:11 -0800 |
---|---|---|
committer | Brett Chabot <brettchabot@android.com> | 2010-02-22 18:08:35 -0800 |
commit | 88e03a97366d08fc69d852cf2219f9d0b1916af4 (patch) | |
tree | 99e50be3bfd8a264b6d84133e4e05ea59e2c50ef /test-runner/src/android/test/InstrumentationTestRunner.java | |
parent | b09cc2cde25d02f668b8bc2115a9479425f48703 (diff) | |
download | frameworks_base-88e03a97366d08fc69d852cf2219f9d0b1916af4.zip frameworks_base-88e03a97366d08fc69d852cf2219f9d0b1916af4.tar.gz frameworks_base-88e03a97366d08fc69d852cf2219f9d0b1916af4.tar.bz2 |
Add ability to run tests restricted to given annotation.
And ability to exclude tests with given annotation.
Also fix class cast compile warning in emma output method.
Bug 2239240
Change-Id: I56273a51a8c58a690680bdb612615fab69e6e13f
Diffstat (limited to 'test-runner/src/android/test/InstrumentationTestRunner.java')
-rw-r--r-- | test-runner/src/android/test/InstrumentationTestRunner.java | 87 |
1 files changed, 86 insertions, 1 deletions
diff --git a/test-runner/src/android/test/InstrumentationTestRunner.java b/test-runner/src/android/test/InstrumentationTestRunner.java index 4ae98e6..e586010 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 * <p/> + * <b>Filter test run to tests with given annotation:</b> adb shell am instrument -w + * -e annotation com.android.foo.MyAnnotation + * com.android.foo/android.test.InstrumentationTestRunner + * <p/> + * 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. + * <p/> + * <b>Filter test run to tests <i>without</i> given annotation:</b> adb shell am instrument -w + * -e notAnnotation com.android.foo.MyAnnotation + * com.android.foo/android.test.InstrumentationTestRunner + * <p/> * <b>Running a single testcase:</b> 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); Predicate<TestMethod> testSizePredicate = null; + Predicate<TestMethod> testAnnotationPredicate = null; + Predicate<TestMethod> testNotAnnotationPredicate = null; boolean includePerformance = false; String testClassesArg = null; boolean logOnly = false; @@ -287,6 +308,11 @@ public class InstrumentationTestRunner extends Instrumentation implements TestSu mPackageOfTests = arguments.getString(ARGUMENT_TEST_PACKAGE); testSizePredicate = getSizePredicateFromArg( arguments.getString(ARGUMENT_TEST_SIZE_PREDICATE)); + testAnnotationPredicate = getAnnotationPredicate( + arguments.getString(ARGUMENT_ANNOTATION)); + testNotAnnotationPredicate = getNotAnnotationPredicate( + arguments.getString(ARGUMENT_NOT_ANNOTATION)); + includePerformance = getBooleanArgument(arguments, ARGUMENT_INCLUDE_PERF); logOnly = getBooleanArgument(arguments, ARGUMENT_LOG_ONLY); mCoverage = getBooleanArgument(arguments, "coverage"); @@ -306,6 +332,12 @@ public class InstrumentationTestRunner extends Instrumentation implements TestSu if (testSizePredicate != null) { testSuiteBuilder.addRequirements(testSizePredicate); } + if (testAnnotationPredicate != null) { + testSuiteBuilder.addRequirements(testAnnotationPredicate); + } + if (testNotAnnotationPredicate != null) { + testSuiteBuilder.addRequirements(testNotAnnotationPredicate); + } if (!includePerformance) { testSuiteBuilder.addRequirements(REJECT_PERFORMANCE); } @@ -406,6 +438,59 @@ public class InstrumentationTestRunner extends Instrumentation implements TestSu } } + /** + * Returns the test predicate object, corresponding to the annotation class value provided via + * the {@link ARGUMENT_ANNOTATION} argument. + * + * @return the predicate or <code>null</code> + */ + private Predicate<TestMethod> getAnnotationPredicate(String annotationClassName) { + Class<? extends Annotation> annotationClass = getAnnotationClass(annotationClassName); + if (annotationClass != null) { + return new HasAnnotation(annotationClass); + } + return null; + } + + /** + * Returns the negative test predicate object, corresponding to the annotation class value + * provided via the {@link ARGUMENT_NOT_ANNOTATION} argument. + * + * @return the predicate or <code>null</code> + */ + private Predicate<TestMethod> getNotAnnotationPredicate(String annotationClassName) { + Class<? extends Annotation> annotationClass = getAnnotationClass(annotationClassName); + if (annotationClass != null) { + return Predicates.not(new HasAnnotation(annotationClass)); + } + return null; + } + + /** + * Helper method to return the annotation class with specified name + * + * @param annotationClassName the fully qualified name of the class + * @return the annotation class or <code>null</code> + */ + 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); |