alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Android example source code file (TestBrowserControllerImplTest.java)

This example Android source code file (TestBrowserControllerImplTest.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Android by Example" TM.

Java - Android tags/keywords

all, android, class, content, dummytestcase, dummytestsuite, exception, intent, list, override, run, string, testbrowsercontrollerimpl, testbrowsercontrollerimpltest, testbrowserviewstub, testsuite, util

The TestBrowserControllerImplTest.java Android example source code

/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.test;

import android.content.Intent;

import junit.framework.TestCase;
import junit.framework.TestSuite;

import java.util.Arrays;
import java.util.List;

public class TestBrowserControllerImplTest extends TestCase {
    private TestBrowserControllerImpl mTestBrowserController;
    private TestBrowserViewStub mTestBrowserView;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        mTestBrowserController = new TestBrowserControllerImpl();
        mTestBrowserView = new TestBrowserViewStub();
        mTestBrowserController.registerView(mTestBrowserView);
    }

    public void testSetTestSuite() throws Exception {
        TestSuite testSuite = new TestSuite();
        testSuite.addTestSuite(DummyTestCase.class);

        mTestBrowserController.setTestSuite(testSuite);

        verifyTestNames(Arrays.asList("Run All", DummyTestCase.class.getSimpleName()),
                mTestBrowserView.getTestNames());
    }

    private static void verifyTestNames(List<String> expectedTestNames,
            List<String> actualTestNames) {
        assertEquals(expectedTestNames.size(), actualTestNames.size());

        // We use endsWith instead of equals because the return value of
        // class.getSimpleName(), when called on an inner class, varies
        // from one vm to another.
        // This allows the test to pass in multiple environments.
        for (int i = 0; i < expectedTestNames.size(); i++) {
            assertTrue(actualTestNames.get(i).endsWith(expectedTestNames.get(i)));
        }
    }

    public void testGetIntentForTestSuite() throws Exception {
        TestSuite testSuite = new TestSuite();
        testSuite.addTestSuite(DummyTestCase.class);

        String targetBrowserActvityClassName = "com.android.bogus.DummyActivity";
        String expectedTargetPackageName = "com.android.bogus";
        mTestBrowserController.setTargetBrowserActivityClassName(targetBrowserActvityClassName);
        mTestBrowserController.setTestSuite(testSuite);
        mTestBrowserController.setTargetPackageName(expectedTargetPackageName);
        Intent intent = mTestBrowserController.getIntentForTestAt(1);
        verifyIntent(intent, DummyTestCase.class, expectedTargetPackageName);
        assertEquals(targetBrowserActvityClassName, intent.getComponent().getClassName());
    }

    public void testGetIntentForTestCase() throws Exception {
        TestSuite testSuite = new TestSuite();
        testSuite.addTest(new DummyTestCase());

        mTestBrowserController.setTestSuite(testSuite);
        Intent intent = mTestBrowserController.getIntentForTestAt(1);
        verifyIntent(intent, DummyTestCase.class, "com.android.testharness");
        assertEquals(TestBrowserControllerImpl.TEST_RUNNER_ACTIVITY_CLASS_NAME,
                intent.getComponent().getClassName());
        assertEquals("testDummyTest",
                intent.getStringExtra(TestBrowserController.BUNDLE_EXTRA_TEST_METHOD_NAME));
    }

    public void testGetIntentForRunAll() throws Exception {
        TestSuite testSuite = new DummyTestSuite();
        testSuite.addTestSuite(DummyTestCase.class);

        mTestBrowserController.setTestSuite(testSuite);
        Intent intent = mTestBrowserController.getIntentForTestAt(0);
        verifyIntent(intent, DummyTestSuite.class, "com.android.testharness");
    }

    private static void verifyIntent(Intent intent, Class testClass, String expectedPackageName) {
        assertEquals(Intent.ACTION_RUN, intent.getAction());
        assertEquals(Intent.FLAG_ACTIVITY_NEW_TASK,
                intent.getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK);
        assertEquals(Intent.FLAG_ACTIVITY_MULTIPLE_TASK,
                intent.getFlags() & Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
        assertEquals(testClass.getName(), intent.getData().toString());
        assertEquals(expectedPackageName, intent.getComponent().getPackageName());
    }

    private static class DummyTestSuite extends TestSuite {
        private DummyTestSuite() {
            super(DummyTestSuite.class.getName());
        }
    }

    private static class DummyTestCase extends TestCase {
        private DummyTestCase() {
            super("testDummyTest");
        }

        public void testDummyTest() throws Exception {
        }
    }

    private class TestBrowserViewStub implements TestBrowserView {
        private List<String> mTestNames;

        public void setTestNames(List<String> testNames) {
            mTestNames = testNames;
        }

        public List<String> getTestNames() {
            return mTestNames;
        }
    }
}

Other Android examples (source code examples)

Here is a short list of links related to this Android TestBrowserControllerImplTest.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.