2 * Copyright (C) 2007 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 import android.app.Activity;
20 import android.app.Instrumentation;
21 import android.content.Intent;
22 import android.net.Uri;
23 import android.os.RemoteException;
24 import android.os.ServiceManager;
25 import android.view.IWindowManager;
26 import android.widget.ListView;
28 import com.google.android.collect.Lists;
30 import junit.framework.Test;
31 import junit.framework.TestCase;
32 import junit.framework.TestSuite;
34 import java.util.List;
36 public class TestBrowserActivityTest extends InstrumentationTestCase {
38 private TestBrowserActivity mTestBrowserActivity;
39 private StubTestBrowserController mTestBrowserController;
42 protected void setUp() throws Exception {
44 StubTestBrowserActivity.setTopTestSuite(null);
45 mTestBrowserController = new StubTestBrowserController();
46 ServiceLocator.setTestBrowserController(mTestBrowserController);
50 protected void tearDown() throws Exception {
51 if (mTestBrowserActivity != null) {
52 mTestBrowserActivity.finish();
54 mTestBrowserActivity = null;
58 public void testEmptyListContent() throws Exception {
59 StubTestBrowserActivity.setTopTestSuite(new TestSuite());
61 mTestBrowserActivity = createActivity();
63 ListView listView = getListView();
64 // There is always an item on the list for running all tests.
65 assertEquals("Unexpected number of items on list view.", 1, listView.getCount());
67 assertEquals("Stubbed Test Browser", mTestBrowserActivity.getTitle().toString());
70 public void testOneListContent() throws Exception {
71 List<String> testCaseNames = Lists.newArrayList("AllTests");
72 StubTestBrowserActivity.setTopTestSuite(createTestSuite(testCaseNames));
74 mTestBrowserActivity = createActivity();
76 ListView listView = getListView();
77 assertListViewContents(testCaseNames, listView);
80 public void testListWithTestCases() throws Exception {
81 List<String> testCaseNames = Lists.newArrayList("AllTests", "Apples", "Bananas", "Oranges");
82 StubTestBrowserActivity.setTopTestSuite(createTestSuite(testCaseNames));
84 mTestBrowserActivity = createActivity();
86 ListView listView = getListView();
87 assertListViewContents(testCaseNames, listView);
90 public void testListWithTestSuite() throws Exception {
91 List<String> testCaseNames = Lists.newArrayList(OneTestTestCase.class.getSimpleName());
92 StubTestBrowserActivity.setTopTestSuite(new OneTestInTestSuite());
94 mTestBrowserActivity = createActivity();
96 ListView listView = getListView();
97 assertListViewContents(testCaseNames, listView);
100 public void testSelectATestCase() throws Exception {
101 List<String> testCaseNames = Lists.newArrayList("AllTests");
102 TestSuite testSuite = createTestSuite(testCaseNames);
103 StubTestBrowserActivity.setTopTestSuite(testSuite);
105 mTestBrowserController.setTestCase(OneTestTestCase.class);
106 mTestBrowserActivity = createActivity();
108 Instrumentation.ActivityMonitor activityMonitor = getInstrumentation().addMonitor(
109 TestBrowserControllerImpl.TEST_RUNNER_ACTIVITY_CLASS_NAME, null, false);
111 assertEquals(0, activityMonitor.getHits());
113 ListView listView = getListView();
114 int invokedTestCaseIndex = 0;
115 listView.performItemClick(listView, invokedTestCaseIndex, 0);
117 Activity activity = activityMonitor.waitForActivityWithTimeout(2000);
118 assertNotNull(activity);
120 assertEquals(1, activityMonitor.getHits());
121 assertEquals(invokedTestCaseIndex, mTestBrowserController.getLastPosition());
126 getInstrumentation().removeMonitor(activityMonitor);
130 public void testCreateFromIntentWithOneTest() throws Exception {
131 List<String> testCaseNames = Lists.newArrayList("testOne");
133 mTestBrowserActivity = launchTestBrowserActivity(new TestSuite(OneTestTestCase.class));
135 ListView listView = getListView();
136 assertListViewContents(testCaseNames, listView);
139 public void testUpdateListOnStart() throws Exception {
140 StubTestBrowserActivity.setTopTestSuite(new TestSuite());
142 mTestBrowserActivity = createActivity();
144 ListView listView = getListView();
145 assertEquals("Unexpected number of items on list view.", 1, listView.getCount());
147 List<String> testCaseNames = Lists.newArrayList("AllTests");
148 StubTestBrowserActivity.setTopTestSuite(createTestSuite(testCaseNames));
150 getInstrumentation().runOnMainSync(new Runnable() {
152 ((StubTestBrowserActivity) mTestBrowserActivity).onStart();
156 listView = getListView();
157 assertListViewContents(testCaseNames, listView);
160 public void testTitleHasTestSuiteName() throws Exception {
161 final String testSuiteName = "com.android.TestSuite";
162 StubTestBrowserActivity.setTopTestSuite(new TestSuite(testSuiteName));
164 mTestBrowserActivity = createActivity();
166 assertEquals("TestSuite", mTestBrowserActivity.getTitle().toString());
169 private TestSuite createTestSuite(List<String> testCaseNames) {
170 return createTestSuite(testCaseNames.toArray(new String[testCaseNames.size()]));
173 private TestSuite createTestSuite(String... testCaseNames) {
174 TestSuite testSuite = new TestSuite();
175 for (String testCaseName : testCaseNames) {
176 testSuite.addTest(new FakeTestCase(testCaseName));
182 public static class FakeTestCase extends TestCase {
183 public FakeTestCase(String name) {
188 public static class OneTestTestCase extends TestCase {
189 public void testOne() throws Exception {
193 public static class OneTestInTestSuite extends TestSuite {
194 public static Test suite() {
195 TestSuite suite = new TestSuite(OneTestInTestSuite.class.getName());
196 suite.addTestSuite(OneTestTestCase.class);
201 private void assertListViewContents(List<String> expectedTestCaseNames, ListView listView) {
202 assertEquals("Run All", listView.getItemAtPosition(0).toString());
203 assertEquals("Unexpected number of items on list view.",
204 expectedTestCaseNames.size() + 1, listView.getCount());
205 for (int i = 0; i < expectedTestCaseNames.size(); i++) {
206 String expectedTestCaseName = expectedTestCaseNames.get(i);
207 String actualTestCaseName = listView.getItemAtPosition(i + 1).toString();
208 assertEquals("Unexpected test case name. Index: " + i,
209 expectedTestCaseName, actualTestCaseName);
213 private ListView getListView() {
214 return mTestBrowserActivity.getListView();
217 private TestBrowserActivity createActivity() throws RemoteException {
218 return launchActivity(getAndroidPackageName(), StubTestBrowserActivity.class, null);
221 private Intent createIntent(TestSuite testSuite) {
222 Intent intent = new Intent(Intent.ACTION_RUN);
223 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
224 String className = StubTestBrowserActivity.class.getName();
225 String packageName = getAndroidPackageName();
226 intent.setClassName(packageName, className);
227 intent.setData(Uri.parse(testSuite.getName()));
231 private String getAndroidPackageName() {
232 String packageName = getInstrumentation().getTargetContext().getPackageName();
236 private TestBrowserActivity launchTestBrowserActivity(TestSuite testSuite)
237 throws RemoteException {
238 getInstrumentation().setInTouchMode(false);
240 TestBrowserActivity activity =
241 (TestBrowserActivity) getInstrumentation().startActivitySync(
242 createIntent(testSuite));
243 getInstrumentation().waitForIdleSync();
247 private static class StubTestBrowserController extends TestBrowserControllerImpl {
248 private int mPosition;
249 private Class<? extends TestCase> mTestCaseClass;
251 public Intent getIntentForTestAt(int position) {
252 mPosition = position;
254 Intent intent = new Intent();
255 intent.setAction(Intent.ACTION_RUN);
257 String className = TestBrowserControllerImpl.TEST_RUNNER_ACTIVITY_CLASS_NAME;
258 String testName = mTestCaseClass.getClass().getName();
260 String packageName = className.substring(0, className.lastIndexOf("."));
261 intent.setClassName(packageName, className);
262 intent.setData(Uri.parse(testName));
267 public void setTestCase(Class<? extends TestCase> testCaseClass) {
268 mTestCaseClass = testCaseClass;
271 public int getLastPosition() {