FileDocCategorySizeDatePackage
InstrumentationTestRunnerTest.javaAPI DocAndroid 5.1 API12787Thu Mar 12 22:22:42 GMT 2015android.test

InstrumentationTestRunnerTest

public class InstrumentationTestRunnerTest extends TestCase
Tests for {@link InstrumentationTestRunner}

Fields Summary
private StubInstrumentationTestRunner
mInstrumentationTestRunner
private StubAndroidTestRunner
mStubAndroidTestRunner
private String
mTargetContextPackageName
Constructors Summary
Methods Summary
private voidassertContentsInOrder(java.util.List actual, android.test.suitebuilder.ListTestCaseNames.TestDescriptor source)

        TestDescriptor[] clonedSource = source.clone();
        assertEquals("Unexpected number of items.", clonedSource.length, actual.size());
        for (int i = 0; i < actual.size(); i++) {
            TestDescriptor actualItem = actual.get(i);
            TestDescriptor sourceItem = clonedSource[i];
            assertEquals("Unexpected item. Index: " + i, sourceItem, actualItem);
        }
    
private voidassertTestRunnerCalledWithExpectedParameters(java.lang.String expectedTestClassName, java.lang.String expectedTestMethodName)

        Test test = mStubAndroidTestRunner.getTest();
        assertContentsInOrder(ListTestCaseNames.getTestNames((TestSuite) test),
                new TestDescriptor(expectedTestClassName, expectedTestMethodName));  
        assertTrue(mInstrumentationTestRunner.isStarted());
        assertFalse(mInstrumentationTestRunner.isFinished());
    
private android.os.BundlecreateBundle(java.lang.String key, java.lang.String value)

        Bundle bundle = new Bundle();
        bundle.putString(key, value);
        return bundle;
    
protected voidsetUp()

        super.setUp();
        mStubAndroidTestRunner = new StubAndroidTestRunner();
        mTargetContextPackageName = "android.test.suitebuilder.examples";
        mInstrumentationTestRunner = new StubInstrumentationTestRunner(
                new StubContext("com.google.foo.tests"),
                new StubContext(mTargetContextPackageName), mStubAndroidTestRunner);
    
public voidtestAnnotationParameter()
Test that the -e {@link InstrumentationTestRunner.ARGUMENT_ANNOTATION} parameter properly selects tests.

        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");
    
public voidtestBadMethodArgument()
Test that specifying a method which does not exist is handled gracefully

        String testClassName = PlaceHolderTest.class.getName();
        String invalidMethodName = "testNoExist";
        String classAndMethod = testClassName + "#" + invalidMethodName;
        mInstrumentationTestRunner.onCreate(createBundle(
                InstrumentationTestRunner.ARGUMENT_TEST_CLASS, classAndMethod));
        assertTestRunnerCalledWithExpectedParameters(testClassName,
                invalidMethodName);
    
public voidtestDelayParameter()

        int delayMsec = 1000;
        Bundle args = new Bundle();
        args.putInt(InstrumentationTestRunner.ARGUMENT_DELAY_MSEC, delayMsec);
        args.putString(InstrumentationTestRunner.ARGUMENT_TEST_CLASS,
                PlaceHolderTest.class.getName() + "," +
                PlaceHolderTest2.class.getName());
        mInstrumentationTestRunner.onCreate(args);
        Thread t = new Thread() { public void run() { mInstrumentationTestRunner.onStart(); } };

        // Should delay three times: before, between, and after the two tests.
        long beforeTest = System.currentTimeMillis();
        t.start();
        t.join();
        assertTrue(System.currentTimeMillis() > beforeTest + delayMsec * 3);
        assertTrue(mInstrumentationTestRunner.isStarted());
        assertTrue(mInstrumentationTestRunner.isFinished());
        assertTrue(mStubAndroidTestRunner.isRun());
    
public voidtestMultipleTestClass()

        String classArg = PlaceHolderTest.class.getName() + "," + 
            PlaceHolderTest2.class.getName();
        mInstrumentationTestRunner.onCreate(createBundle(
                InstrumentationTestRunner.ARGUMENT_TEST_CLASS, classArg));
        
        Test test = mStubAndroidTestRunner.getTest();

        assertContentsInOrder(ListTestCaseNames.getTestNames((TestSuite) test),
            new TestDescriptor(PlaceHolderTest.class.getName(), "testPlaceHolder"), 
            new TestDescriptor(PlaceHolderTest2.class.getName(), "testPlaceHolder2"));
        
    
public voidtestNotAnnotationParameter()
Test that the -e {@link InstrumentationTestRunner.ARGUMENT_NOT_ANNOTATION} parameter properly excludes tests.

        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");
    
public voidtestOverrideTestToRunWithClassAndMethodArgument()

        String expectedTestClassName = PlaceHolderTest.class.getName();
        String expectedTestMethodName = "testPlaceHolder";
        String classAndMethod = expectedTestClassName + "#" + expectedTestMethodName;
        mInstrumentationTestRunner.onCreate(createBundle(
                InstrumentationTestRunner.ARGUMENT_TEST_CLASS, classAndMethod));

        assertTestRunnerCalledWithExpectedParameters(expectedTestClassName,
                expectedTestMethodName);
    
public voidtestOverrideTestToRunWithClassArgument()

        String expectedTestClassName = PlaceHolderTest.class.getName();
        mInstrumentationTestRunner.onCreate(createBundle(
                InstrumentationTestRunner.ARGUMENT_TEST_CLASS, expectedTestClassName));

        assertTestRunnerCalledWithExpectedParameters(expectedTestClassName, "testPlaceHolder");
    
public voidtestUnhandledException()
Test that runtime exceptions during runTest are handled gracefully

        StubAndroidTestRunner stubAndroidTestRunner = new StubAndroidTestRunner() {
            @Override
            public void runTest() {
                throw new RuntimeException();
            }
        };
        StubInstrumentationTestRunner instrumentationTestRunner = new StubInstrumentationTestRunner(
                new StubContext("com.google.foo.tests"),
                new StubContext(mTargetContextPackageName), stubAndroidTestRunner);
        instrumentationTestRunner.onCreate(new Bundle());
        instrumentationTestRunner.onStart();
        assertTrue("Instrumentation did not finish", instrumentationTestRunner.isFinished());
        // ensure a meaningful error message placed in results
        String resultsData = instrumentationTestRunner.mResults.getString(
                Instrumentation.REPORT_KEY_STREAMRESULT);
        assertTrue("Instrumentation results is missing RuntimeException",
                resultsData.contains("RuntimeException"));
    
public voidtestUseSelfAsTestSuiteProviderWhenNoMetaDataOrClassArgument()

        TestSuite testSuite = new TestSuite();
        testSuite.addTestSuite(PlaceHolderTest.class);
        mInstrumentationTestRunner.setAllTestsSuite(testSuite);
        mInstrumentationTestRunner.onCreate(null);
        assertTestRunnerCalledWithExpectedParameters(
                PlaceHolderTest.class.getName(), "testPlaceHolder");