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 setUp()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 tearDown()50 protected void tearDown() throws Exception { 51 if (mTestBrowserActivity != null) { 52 mTestBrowserActivity.finish(); 53 } 54 mTestBrowserActivity = null; 55 super.tearDown(); 56 } 57 testEmptyListContent()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 testOneListContent()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 testListWithTestCases()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 testListWithTestSuite()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 testSelectATestCase()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 testCreateFromIntentWithOneTest()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 testUpdateListOnStart()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 testTitleHasTestSuiteName()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 createTestSuite(List<String> testCaseNames)169 private TestSuite createTestSuite(List<String> testCaseNames) { 170 return createTestSuite(testCaseNames.toArray(new String[testCaseNames.size()])); 171 } 172 createTestSuite(String... testCaseNames)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 { FakeTestCase(String name)183 public FakeTestCase(String name) { 184 super(name); 185 } 186 } 187 188 public static class OneTestTestCase extends TestCase { testOne()189 public void testOne() throws Exception { 190 } 191 } 192 193 public static class OneTestInTestSuite extends TestSuite { suite()194 public static Test suite() { 195 TestSuite suite = new TestSuite(OneTestInTestSuite.class.getName()); 196 suite.addTestSuite(OneTestTestCase.class); 197 return suite; 198 } 199 } 200 assertListViewContents(List<String> expectedTestCaseNames, ListView listView)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 getListView()213 private ListView getListView() { 214 return mTestBrowserActivity.getListView(); 215 } 216 createActivity()217 private TestBrowserActivity createActivity() throws RemoteException { 218 return launchActivity("android.test", StubTestBrowserActivity.class, null); 219 } 220 createIntent(TestSuite testSuite)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 = className.substring(0, className.lastIndexOf(".")); 226 intent.setClassName(packageName, className); 227 intent.setData(Uri.parse(testSuite.getName())); 228 return intent; 229 } 230 launchTestBrowserActivity(TestSuite testSuite)231 private TestBrowserActivity launchTestBrowserActivity(TestSuite testSuite) 232 throws RemoteException { 233 getInstrumentation().setInTouchMode(false); 234 235 TestBrowserActivity activity = 236 (TestBrowserActivity) getInstrumentation().startActivitySync( 237 createIntent(testSuite)); 238 getInstrumentation().waitForIdleSync(); 239 return activity; 240 } 241 242 private static class StubTestBrowserController extends TestBrowserControllerImpl { 243 private int mPosition; 244 private Class<? extends TestCase> mTestCaseClass; 245 getIntentForTestAt(int position)246 public Intent getIntentForTestAt(int position) { 247 mPosition = position; 248 249 Intent intent = new Intent(); 250 intent.setAction(Intent.ACTION_RUN); 251 252 String className = TestBrowserControllerImpl.TEST_RUNNER_ACTIVITY_CLASS_NAME; 253 String testName = mTestCaseClass.getClass().getName(); 254 255 String packageName = className.substring(0, className.lastIndexOf(".")); 256 intent.setClassName(packageName, className); 257 intent.setData(Uri.parse(testName)); 258 259 return intent; 260 } 261 setTestCase(Class<? extends TestCase> testCaseClass)262 public void setTestCase(Class<? extends TestCase> testCaseClass) { 263 mTestCaseClass = testCaseClass; 264 } 265 getLastPosition()266 public int getLastPosition() { 267 return mPosition; 268 } 269 } 270 } 271