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
diff --git a/test-runner/tests/src/android/test/InstrumentationTestRunnerTest.java b/test-runner/tests/src/android/test/InstrumentationTestRunnerTest.java
index d9afd54..6db72ad 100644
--- a/test-runner/tests/src/android/test/InstrumentationTestRunnerTest.java
+++ b/test-runner/tests/src/android/test/InstrumentationTestRunnerTest.java
@@ -109,6 +109,33 @@
         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<TestDescriptor> actual, TestDescriptor... source) {
         TestDescriptor[] clonedSource = source.clone();
         assertEquals("Unexpected number of items.", clonedSource.length, actual.size());
@@ -269,4 +296,17 @@
 
         }
     }
+
+    /**
+     * Annotated test used for validation.
+     */
+    public static class AnnotationTest extends TestCase {
+
+        public void testNotAnnotated() throws Exception {
+        }
+
+        @FlakyTest
+        public void testAnnotated() throws Exception {
+        }
+    }
 }