summaryrefslogtreecommitdiffstats
path: root/test-runner/tests/src/android/test/TestBrowserControllerImplTest.java
blob: 1315606896030f55b19d1eb874c9674eaac4bfe3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
 * 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;
        }
    }
}