OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / frameworks / base / test-runner / tests / src / android / test / TestBrowserActivityTest.java
1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package android.test;
18
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;
27
28 import com.google.android.collect.Lists;
29
30 import junit.framework.Test;
31 import junit.framework.TestCase;
32 import junit.framework.TestSuite;
33
34 import java.util.List;
35
36 public class TestBrowserActivityTest extends InstrumentationTestCase {
37
38     private TestBrowserActivity mTestBrowserActivity;
39     private StubTestBrowserController mTestBrowserController;
40
41     @Override
42     protected void setUp() throws Exception {
43         super.setUp();
44         StubTestBrowserActivity.setTopTestSuite(null);
45         mTestBrowserController = new StubTestBrowserController();
46         ServiceLocator.setTestBrowserController(mTestBrowserController);
47     }
48
49     @Override
50     protected void tearDown() throws Exception {
51         if (mTestBrowserActivity != null) {
52             mTestBrowserActivity.finish();
53         }
54         mTestBrowserActivity = null;
55         super.tearDown();
56     }
57
58     public void testEmptyListContent() throws Exception {
59         StubTestBrowserActivity.setTopTestSuite(new TestSuite());
60
61         mTestBrowserActivity = createActivity();
62
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());
66
67         assertEquals("Stubbed Test Browser", mTestBrowserActivity.getTitle().toString());
68     }
69
70     public void testOneListContent() throws Exception {
71         List<String> testCaseNames = Lists.newArrayList("AllTests");
72         StubTestBrowserActivity.setTopTestSuite(createTestSuite(testCaseNames));
73
74         mTestBrowserActivity = createActivity();
75
76         ListView listView = getListView();
77         assertListViewContents(testCaseNames, listView);
78     }
79
80     public void testListWithTestCases() throws Exception {
81         List<String> testCaseNames = Lists.newArrayList("AllTests", "Apples", "Bananas", "Oranges");
82         StubTestBrowserActivity.setTopTestSuite(createTestSuite(testCaseNames));
83
84         mTestBrowserActivity = createActivity();
85
86         ListView listView = getListView();
87         assertListViewContents(testCaseNames, listView);
88     }
89
90     public void testListWithTestSuite() throws Exception {
91         List<String> testCaseNames = Lists.newArrayList(OneTestTestCase.class.getSimpleName());
92         StubTestBrowserActivity.setTopTestSuite(new OneTestInTestSuite());
93
94         mTestBrowserActivity = createActivity();
95
96         ListView listView = getListView();
97         assertListViewContents(testCaseNames, listView);
98     }
99
100     public void testSelectATestCase() throws Exception {
101         List<String> testCaseNames = Lists.newArrayList("AllTests");
102         TestSuite testSuite = createTestSuite(testCaseNames);
103         StubTestBrowserActivity.setTopTestSuite(testSuite);
104
105         mTestBrowserController.setTestCase(OneTestTestCase.class);
106         mTestBrowserActivity = createActivity();
107
108         Instrumentation.ActivityMonitor activityMonitor = getInstrumentation().addMonitor(
109                 TestBrowserControllerImpl.TEST_RUNNER_ACTIVITY_CLASS_NAME, null, false);
110         try {
111             assertEquals(0, activityMonitor.getHits());
112
113             ListView listView = getListView();
114             int invokedTestCaseIndex = 0;
115             listView.performItemClick(listView, invokedTestCaseIndex, 0);
116
117             Activity activity = activityMonitor.waitForActivityWithTimeout(2000);
118             assertNotNull(activity);
119             try {
120                 assertEquals(1, activityMonitor.getHits());
121                 assertEquals(invokedTestCaseIndex, mTestBrowserController.getLastPosition());
122             } finally {
123                 activity.finish();
124             }
125         } finally {
126             getInstrumentation().removeMonitor(activityMonitor);
127         }
128     }
129
130     public void testCreateFromIntentWithOneTest() throws Exception {
131         List<String> testCaseNames = Lists.newArrayList("testOne");
132
133         mTestBrowserActivity = launchTestBrowserActivity(new TestSuite(OneTestTestCase.class));
134
135         ListView listView = getListView();
136         assertListViewContents(testCaseNames, listView);
137     }
138
139     public void testUpdateListOnStart() throws Exception {
140         StubTestBrowserActivity.setTopTestSuite(new TestSuite());
141
142         mTestBrowserActivity = createActivity();
143
144         ListView listView = getListView();
145         assertEquals("Unexpected number of items on list view.", 1, listView.getCount());
146
147         List<String> testCaseNames = Lists.newArrayList("AllTests");
148         StubTestBrowserActivity.setTopTestSuite(createTestSuite(testCaseNames));
149
150         getInstrumentation().runOnMainSync(new Runnable() {
151             public void run() {
152                 ((StubTestBrowserActivity) mTestBrowserActivity).onStart();
153             }
154         });
155
156         listView = getListView();
157         assertListViewContents(testCaseNames, listView);
158     }
159
160     public void testTitleHasTestSuiteName() throws Exception {
161         final String testSuiteName = "com.android.TestSuite";
162         StubTestBrowserActivity.setTopTestSuite(new TestSuite(testSuiteName));
163
164         mTestBrowserActivity = createActivity();
165
166         assertEquals("TestSuite", mTestBrowserActivity.getTitle().toString());
167     }
168     
169     private TestSuite createTestSuite(List<String> testCaseNames) {
170         return createTestSuite(testCaseNames.toArray(new String[testCaseNames.size()]));
171     }
172
173     private TestSuite createTestSuite(String... testCaseNames) {
174         TestSuite testSuite = new TestSuite();
175         for (String testCaseName : testCaseNames) {
176             testSuite.addTest(new FakeTestCase(testCaseName));
177         }
178
179         return testSuite;
180     }
181
182     public static class FakeTestCase extends TestCase {
183         public FakeTestCase(String name) {
184             super(name);
185         }
186     }
187
188     public static class OneTestTestCase extends TestCase {
189         public void testOne() throws Exception {
190         }
191     }
192
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);
197             return suite;
198         }
199     }
200
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);
210         }
211     }
212
213     private ListView getListView() {
214         return mTestBrowserActivity.getListView();
215     }
216
217     private TestBrowserActivity createActivity() throws RemoteException {
218         return launchActivity(getAndroidPackageName(), StubTestBrowserActivity.class, null);
219     }
220
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()));
228         return intent;
229     }
230
231     private String getAndroidPackageName() {
232         String packageName = getInstrumentation().getTargetContext().getPackageName();
233         return packageName;
234     }
235
236     private TestBrowserActivity launchTestBrowserActivity(TestSuite testSuite)
237             throws RemoteException {
238         getInstrumentation().setInTouchMode(false);
239
240         TestBrowserActivity activity =
241                 (TestBrowserActivity) getInstrumentation().startActivitySync(
242                         createIntent(testSuite));
243         getInstrumentation().waitForIdleSync();
244         return activity;
245     }
246
247     private static class StubTestBrowserController extends TestBrowserControllerImpl {
248         private int mPosition;
249         private Class<? extends TestCase> mTestCaseClass;
250
251         public Intent getIntentForTestAt(int position) {
252             mPosition = position;
253
254             Intent intent = new Intent();
255             intent.setAction(Intent.ACTION_RUN);
256
257             String className = TestBrowserControllerImpl.TEST_RUNNER_ACTIVITY_CLASS_NAME;
258             String testName = mTestCaseClass.getClass().getName();
259
260             String packageName = className.substring(0, className.lastIndexOf("."));
261             intent.setClassName(packageName, className);
262             intent.setData(Uri.parse(testName));
263
264             return intent;
265         }
266
267         public void setTestCase(Class<? extends TestCase> testCaseClass) {
268             mTestCaseClass = testCaseClass;
269         }
270
271         public int getLastPosition() {
272             return mPosition;
273         }
274     }
275 }