FileDocCategorySizeDatePackage
TestBrowserActivityTest.javaAPI DocAndroid 1.5 API9838Wed May 06 22:42:02 BST 2009android.test

TestBrowserActivityTest

public class TestBrowserActivityTest extends InstrumentationTestCase

Fields Summary
private TestBrowserActivity
mTestBrowserActivity
private StubTestBrowserController
mTestBrowserController
Constructors Summary
Methods Summary
private voidassertListViewContents(java.util.List expectedTestCaseNames, android.widget.ListView listView)

        assertEquals("Run All", listView.getItemAtPosition(0).toString());
        assertEquals("Unexpected number of items on list view.",
                expectedTestCaseNames.size() + 1, listView.getCount());
        for (int i = 0; i < expectedTestCaseNames.size(); i++) {
            String expectedTestCaseName = expectedTestCaseNames.get(i);
            String actualTestCaseName = listView.getItemAtPosition(i + 1).toString();
            assertEquals("Unexpected test case name. Index: " + i,
                    expectedTestCaseName, actualTestCaseName);
        }
    
private TestBrowserActivitycreateActivity()

        return launchActivity("android.test", StubTestBrowserActivity.class, null);
    
private android.content.IntentcreateIntent(junit.framework.TestSuite testSuite)

        Intent intent = new Intent(Intent.ACTION_RUN);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        String className = StubTestBrowserActivity.class.getName();
        String packageName = className.substring(0, className.lastIndexOf("."));
        intent.setClassName(packageName, className);
        intent.setData(Uri.parse(testSuite.getName()));
        return intent;
    
private junit.framework.TestSuitecreateTestSuite(java.util.List testCaseNames)

        return createTestSuite(testCaseNames.toArray(new String[testCaseNames.size()]));
    
private junit.framework.TestSuitecreateTestSuite(java.lang.String testCaseNames)

        TestSuite testSuite = new TestSuite();
        for (String testCaseName : testCaseNames) {
            testSuite.addTest(new FakeTestCase(testCaseName));
        }

        return testSuite;
    
private android.widget.ListViewgetListView()

        return mTestBrowserActivity.getListView();
    
private TestBrowserActivitylaunchTestBrowserActivity(junit.framework.TestSuite testSuite)

        getInstrumentation().setInTouchMode(false);

        TestBrowserActivity activity =
                (TestBrowserActivity) getInstrumentation().startActivitySync(
                        createIntent(testSuite));
        getInstrumentation().waitForIdleSync();
        return activity;
    
protected voidsetUp()

        super.setUp();
        StubTestBrowserActivity.setTopTestSuite(null);
        mTestBrowserController = new StubTestBrowserController();
        ServiceLocator.setTestBrowserController(mTestBrowserController);
    
protected voidtearDown()

        if (mTestBrowserActivity != null) {
            mTestBrowserActivity.finish();
        }
        mTestBrowserActivity = null;
        super.tearDown();
    
public voidtestCreateFromIntentWithOneTest()

        List<String> testCaseNames = Lists.newArrayList("testOne");

        mTestBrowserActivity = launchTestBrowserActivity(new TestSuite(OneTestTestCase.class));

        ListView listView = getListView();
        assertListViewContents(testCaseNames, listView);
    
public voidtestEmptyListContent()

        StubTestBrowserActivity.setTopTestSuite(new TestSuite());

        mTestBrowserActivity = createActivity();

        ListView listView = getListView();
        // There is always an item on the list for running all tests.
        assertEquals("Unexpected number of items on list view.", 1, listView.getCount());

        assertEquals("Stubbed Test Browser", mTestBrowserActivity.getTitle().toString());
    
public voidtestListWithTestCases()

        List<String> testCaseNames = Lists.newArrayList("AllTests", "Apples", "Bananas", "Oranges");
        StubTestBrowserActivity.setTopTestSuite(createTestSuite(testCaseNames));

        mTestBrowserActivity = createActivity();

        ListView listView = getListView();
        assertListViewContents(testCaseNames, listView);
    
public voidtestListWithTestSuite()

        List<String> testCaseNames = Lists.newArrayList(OneTestTestCase.class.getSimpleName());
        StubTestBrowserActivity.setTopTestSuite(new OneTestInTestSuite());

        mTestBrowserActivity = createActivity();

        ListView listView = getListView();
        assertListViewContents(testCaseNames, listView);
    
public voidtestOneListContent()

        List<String> testCaseNames = Lists.newArrayList("AllTests");
        StubTestBrowserActivity.setTopTestSuite(createTestSuite(testCaseNames));

        mTestBrowserActivity = createActivity();

        ListView listView = getListView();
        assertListViewContents(testCaseNames, listView);
    
public voidtestSelectATestCase()

        List<String> testCaseNames = Lists.newArrayList("AllTests");
        TestSuite testSuite = createTestSuite(testCaseNames);
        StubTestBrowserActivity.setTopTestSuite(testSuite);

        mTestBrowserController.setTestCase(OneTestTestCase.class);
        mTestBrowserActivity = createActivity();

        Instrumentation.ActivityMonitor activityMonitor = getInstrumentation().addMonitor(
                TestBrowserControllerImpl.TEST_RUNNER_ACTIVITY_CLASS_NAME, null, false);
        try {
            assertEquals(0, activityMonitor.getHits());

            ListView listView = getListView();
            int invokedTestCaseIndex = 0;
            listView.performItemClick(listView, invokedTestCaseIndex, 0);

            Activity activity = activityMonitor.waitForActivityWithTimeout(2000);
            assertNotNull(activity);
            try {
                assertEquals(1, activityMonitor.getHits());
                assertEquals(invokedTestCaseIndex, mTestBrowserController.getLastPosition());
            } finally {
                activity.finish();
            }
        } finally {
            getInstrumentation().removeMonitor(activityMonitor);
        }
    
public voidtestTitleHasTestSuiteName()

        final String testSuiteName = "com.android.TestSuite";
        StubTestBrowserActivity.setTopTestSuite(new TestSuite(testSuiteName));

        mTestBrowserActivity = createActivity();

        assertEquals("TestSuite", mTestBrowserActivity.getTitle().toString());
    
public voidtestUpdateListOnStart()

        StubTestBrowserActivity.setTopTestSuite(new TestSuite());

        mTestBrowserActivity = createActivity();

        ListView listView = getListView();
        assertEquals("Unexpected number of items on list view.", 1, listView.getCount());

        List<String> testCaseNames = Lists.newArrayList("AllTests");
        StubTestBrowserActivity.setTopTestSuite(createTestSuite(testCaseNames));

        getInstrumentation().runOnMainSync(new Runnable() {
            public void run() {
                ((StubTestBrowserActivity) mTestBrowserActivity).onStart();
            }
        });

        listView = getListView();
        assertListViewContents(testCaseNames, listView);