1 /* 2 * Copyright (C) 2008 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.widget.cts; 18 19 import android.app.Activity; 20 import android.app.ActivityGroup; 21 import android.content.Intent; 22 import android.cts.util.WidgetTestUtils; 23 import android.test.ActivityInstrumentationTestCase2; 24 import android.test.UiThreadTest; 25 import android.test.suitebuilder.annotation.SmallTest; 26 import android.view.View; 27 import android.widget.ListView; 28 import android.widget.TabHost; 29 import android.widget.TabHost.OnTabChangeListener; 30 import android.widget.TabHost.TabSpec; 31 import android.widget.TextView; 32 33 import static org.mockito.Mockito.*; 34 35 /** 36 * Test {@link TabHost}. 37 */ 38 @SmallTest 39 public class TabHostTest extends ActivityInstrumentationTestCase2<TabHostCtsActivity> { 40 private static final String TAG_TAB1 = "tab 1"; 41 private static final String TAG_TAB2 = "tab 2"; 42 private static final int TAB_HOST_ID = android.R.id.tabhost; 43 44 private TabHostCtsActivity mActivity; 45 46 public TabHostTest() { 47 super("android.widget.cts", TabHostCtsActivity.class); 48 } 49 50 @Override 51 protected void setUp() throws Exception { 52 super.setUp(); 53 mActivity = getActivity(); 54 } 55 56 public void testConstructor() { 57 new TabHost(mActivity); 58 59 new TabHost(mActivity, null); 60 } 61 62 public void testNewTabSpec() { 63 TabHost tabHost = new TabHost(mActivity); 64 65 assertNotNull(tabHost.newTabSpec(TAG_TAB2)); 66 67 assertNotNull(tabHost.newTabSpec(null)); 68 } 69 70 /* 71 * Check points: 72 * 1. the tabWidget view and tabContent view associated with tabHost are created. 73 * 2. no exception occurs when doing normal operation after setup(). 74 */ 75 public void testSetup1() throws Throwable { 76 final Activity activity = launchActivity("android.widget.cts", CtsActivity.class, null); 77 78 runTestOnUiThread(new Runnable() { 79 public void run() { 80 activity.setContentView(R.layout.tabhost_layout); 81 82 TabHost tabHost = (TabHost) activity.findViewById(TAB_HOST_ID); 83 assertNull(tabHost.getTabWidget()); 84 assertNull(tabHost.getTabContentView()); 85 tabHost.setup(); 86 assertNotNull(tabHost.getTabWidget()); 87 assertNotNull(tabHost.getTabContentView()); 88 89 TabSpec tabSpec = tabHost.newTabSpec(TAG_TAB1); 90 tabSpec.setIndicator(TAG_TAB1); 91 tabSpec.setContent(new MyTabContentFactoryList()); 92 tabHost.addTab(tabSpec); 93 tabHost.setCurrentTab(0); 94 } 95 }); 96 getInstrumentation().waitForIdleSync(); 97 98 activity.finish(); 99 } 100 101 /* 102 * Check points: 103 * 1. the tabWidget view and tabContent view associated with tabHost are created. 104 * 2. no exception occurs when uses TabSpec.setContent(android.content.Intent) after setup(). 105 */ 106 public void testSetup2() throws Throwable { 107 final ActivityGroup activity = launchActivity("android.widget.cts", 108 ActivityGroup.class, null); 109 110 111 runTestOnUiThread(new Runnable() { 112 public void run() { 113 activity.setContentView(R.layout.tabhost_layout); 114 115 TabHost tabHost = (TabHost) activity.findViewById(TAB_HOST_ID); 116 assertNull(tabHost.getTabWidget()); 117 assertNull(tabHost.getTabContentView()); 118 tabHost.setup(activity.getLocalActivityManager()); 119 assertNotNull(tabHost.getTabWidget()); 120 assertNotNull(tabHost.getTabContentView()); 121 122 TabSpec tabSpec = tabHost.newTabSpec(TAG_TAB1); 123 tabSpec.setIndicator(TAG_TAB1); 124 Intent intent = new Intent(Intent.ACTION_VIEW, null, 125 mActivity, CtsActivity.class); 126 tabSpec.setContent(intent); 127 tabHost.addTab(tabSpec); 128 tabHost.setCurrentTab(0); 129 } 130 }); 131 getInstrumentation().waitForIdleSync(); 132 133 activity.finish(); 134 } 135 136 public void testOnTouchModeChanged() { 137 // implementation details 138 } 139 140 @UiThreadTest 141 public void testAddTab() { 142 TabHost tabHost = mActivity.getTabHost(); 143 // there is a initial tab 144 assertEquals(1, tabHost.getTabWidget().getChildCount()); 145 146 TabSpec tabSpec = tabHost.newTabSpec(TAG_TAB2); 147 tabSpec.setIndicator(TAG_TAB2); 148 tabSpec.setContent(new MyTabContentFactoryList()); 149 tabHost.addTab(tabSpec); 150 assertEquals(2, tabHost.getTabWidget().getChildCount()); 151 tabHost.setCurrentTab(1); 152 assertTrue(tabHost.getCurrentView() instanceof ListView); 153 assertEquals(TAG_TAB2, tabHost.getCurrentTabTag()); 154 155 try { 156 tabHost.addTab(tabHost.newTabSpec("tab 3")); 157 fail("Should throw IllegalArgumentException"); 158 } catch (IllegalArgumentException e) { 159 } 160 161 try { 162 tabHost.addTab(tabHost.newTabSpec("tab 3").setIndicator("tab 3")); 163 fail("Should throw IllegalArgumentException"); 164 } catch (IllegalArgumentException e) { 165 } 166 167 try { 168 tabHost.addTab(null); 169 fail("Should throw NullPointerException"); 170 } catch (NullPointerException e) { 171 } 172 } 173 174 @UiThreadTest 175 public void testClearAllTabs() { 176 TabHost tabHost = mActivity.getTabHost(); 177 MyTabContentFactoryText tcf = new MyTabContentFactoryText(); 178 // add two additional tabs 179 tabHost.addTab(tabHost.newTabSpec(TAG_TAB1).setIndicator(TAG_TAB1).setContent(tcf)); 180 tabHost.addTab(tabHost.newTabSpec(TAG_TAB2).setIndicator(TAG_TAB2).setContent(tcf)); 181 assertEquals(3, tabHost.getTabWidget().getChildCount()); 182 assertEquals(3, tabHost.getTabContentView().getChildCount()); 183 assertEquals(0, tabHost.getCurrentTab()); 184 assertNotNull(tabHost.getCurrentView()); 185 186 tabHost.clearAllTabs(); 187 188 assertEquals(0, tabHost.getTabWidget().getChildCount()); 189 assertEquals(0, tabHost.getTabContentView().getChildCount()); 190 assertEquals(-1, tabHost.getCurrentTab()); 191 assertNull(tabHost.getCurrentView()); 192 } 193 194 public void testGetTabWidget() { 195 TabHost tabHost = mActivity.getTabHost(); 196 197 // The attributes defined in tabhost_layout.xml 198 assertEquals(android.R.id.tabs, tabHost.getTabWidget().getId()); 199 WidgetTestUtils.assertScaledPixels(1, tabHost.getTabWidget().getPaddingLeft(), 200 getActivity()); 201 WidgetTestUtils.assertScaledPixels(1, tabHost.getTabWidget().getPaddingRight(), 202 getActivity()); 203 WidgetTestUtils.assertScaledPixels(4, tabHost.getTabWidget().getPaddingTop(), 204 getActivity()); 205 } 206 207 @UiThreadTest 208 public void testAccessCurrentTab() { 209 TabHost tabHost = mActivity.getTabHost(); 210 assertEquals(0, tabHost.getCurrentTab()); 211 212 // normal value 213 TabSpec tabSpec = tabHost.newTabSpec(TAG_TAB2); 214 tabSpec.setIndicator(TAG_TAB2); 215 tabSpec.setContent(new MyTabContentFactoryText()); 216 tabHost.addTab(tabSpec); 217 tabHost.setCurrentTab(1); 218 assertEquals(1, tabHost.getCurrentTab()); 219 tabHost.setCurrentTab(0); 220 assertEquals(0, tabHost.getCurrentTab()); 221 222 // exceptional value 223 tabHost.setCurrentTab(tabHost.getTabWidget().getChildCount() + 1); 224 assertEquals(0, tabHost.getCurrentTab()); 225 tabHost.setCurrentTab(-1); 226 assertEquals(0, tabHost.getCurrentTab()); 227 } 228 229 @UiThreadTest 230 public void testGetCurrentTabView() { 231 TabHost tabHost = mActivity.getTabHost(); 232 // current tab view is the first child of tabWidget. 233 assertSame(tabHost.getTabWidget().getChildAt(0), tabHost.getCurrentTabView()); 234 235 TabSpec tabSpec = tabHost.newTabSpec(TAG_TAB2); 236 tabSpec.setIndicator(TAG_TAB2); 237 tabSpec.setContent(new MyTabContentFactoryText()); 238 tabHost.addTab(tabSpec); 239 tabHost.setCurrentTab(1); 240 // current tab view is the second child of tabWidget. 241 assertSame(tabHost.getTabWidget().getChildAt(1), tabHost.getCurrentTabView()); 242 } 243 244 @UiThreadTest 245 public void testGetCurrentView() { 246 TabHost tabHost = mActivity.getTabHost(); 247 TextView textView = (TextView) tabHost.getCurrentView(); 248 assertEquals(TabHostCtsActivity.INITIAL_VIEW_TEXT, textView.getText().toString()); 249 250 TabSpec tabSpec = tabHost.newTabSpec(TAG_TAB2); 251 tabSpec.setIndicator(TAG_TAB2); 252 tabSpec.setContent(new MyTabContentFactoryList()); 253 tabHost.addTab(tabSpec); 254 tabHost.setCurrentTab(1); 255 assertTrue(tabHost.getCurrentView() instanceof ListView); 256 } 257 258 @UiThreadTest 259 public void testSetCurrentTabByTag() { 260 TabHost tabHost = mActivity.getTabHost(); 261 262 // set CurrentTab 263 TabSpec tabSpec = tabHost.newTabSpec(TAG_TAB2); 264 tabSpec.setIndicator(TAG_TAB2); 265 tabSpec.setContent(new MyTabContentFactoryText()); 266 tabHost.addTab(tabSpec); 267 268 tabHost.setCurrentTabByTag(TAG_TAB2); 269 assertEquals(1, tabHost.getCurrentTab()); 270 271 tabHost.setCurrentTabByTag(TabHostCtsActivity.INITIAL_TAB_TAG); 272 assertEquals(0, tabHost.getCurrentTab()); 273 274 // exceptional value 275 tabHost.setCurrentTabByTag(null); 276 assertEquals(0, tabHost.getCurrentTab()); 277 278 tabHost.setCurrentTabByTag("unknown tag"); 279 assertEquals(0, tabHost.getCurrentTab()); 280 } 281 282 @UiThreadTest 283 public void testGetTabContentView() { 284 TabHost tabHost = mActivity.getTabHost(); 285 assertEquals(3, tabHost.getTabContentView().getChildCount()); 286 287 TextView child0 = (TextView) tabHost.getTabContentView().getChildAt(0); 288 assertEquals(mActivity.getResources().getString(R.string.hello_world), 289 child0.getText().toString()); 290 assertTrue(tabHost.getTabContentView().getChildAt(1) instanceof ListView); 291 TextView child2 = (TextView) tabHost.getTabContentView().getChildAt(2); 292 tabHost.setCurrentTab(0); 293 assertEquals(TabHostCtsActivity.INITIAL_VIEW_TEXT, child2.getText().toString()); 294 295 TabSpec tabSpec = tabHost.newTabSpec(TAG_TAB2); 296 tabSpec.setIndicator(TAG_TAB2); 297 tabSpec.setContent(new MyTabContentFactoryList()); 298 tabHost.addTab(tabSpec); 299 assertEquals(3, tabHost.getTabContentView().getChildCount()); 300 tabHost.setCurrentTab(1); 301 assertEquals(4, tabHost.getTabContentView().getChildCount()); 302 303 child0 = (TextView) tabHost.getTabContentView().getChildAt(0); 304 assertEquals(mActivity.getResources().getString(R.string.hello_world), 305 child0.getText().toString()); 306 assertTrue(tabHost.getTabContentView().getChildAt(1) instanceof ListView); 307 child2 = (TextView) tabHost.getTabContentView().getChildAt(2); 308 tabHost.setCurrentTab(0); 309 assertEquals(TabHostCtsActivity.INITIAL_VIEW_TEXT, child2.getText().toString()); 310 } 311 312 @UiThreadTest 313 public void testDispatchKeyEvent() { 314 // Implementation details. 315 } 316 317 @UiThreadTest 318 public void testDispatchWindowFocusChanged() { 319 // Implementation details 320 } 321 322 /** 323 * Check points: 324 * 1. the specified callback should be invoked when the selected state of any of the items 325 * in this list changes 326 */ 327 @UiThreadTest 328 public void testSetOnTabChangedListener() { 329 TabHost tabHost = mActivity.getTabHost(); 330 331 // add a tab, and change current tab to the new tab 332 OnTabChangeListener mockTabChangeListener = mock(OnTabChangeListener.class); 333 tabHost.setOnTabChangedListener(mockTabChangeListener); 334 335 TabSpec tabSpec = tabHost.newTabSpec(TAG_TAB2); 336 tabSpec.setIndicator(TAG_TAB2); 337 tabSpec.setContent(new MyTabContentFactoryList()); 338 tabHost.addTab(tabSpec); 339 tabHost.setCurrentTab(1); 340 verify(mockTabChangeListener, times(1)).onTabChanged(TAG_TAB2); 341 342 // change current tab to the first one 343 tabHost.setCurrentTab(0); 344 verify(mockTabChangeListener, times(1)).onTabChanged(TabHostCtsActivity.INITIAL_TAB_TAG); 345 346 // set the same tab 347 tabHost.setCurrentTab(0); 348 verifyNoMoreInteractions(mockTabChangeListener); 349 } 350 351 @UiThreadTest 352 public void testGetCurrentTabTag() { 353 TabHost tabHost = mActivity.getTabHost(); 354 assertEquals(TabHostCtsActivity.INITIAL_TAB_TAG, tabHost.getCurrentTabTag()); 355 356 TabSpec tabSpec = tabHost.newTabSpec(TAG_TAB2); 357 tabSpec.setIndicator(TAG_TAB2); 358 tabSpec.setContent(new MyTabContentFactoryList()); 359 tabHost.addTab(tabSpec); 360 tabHost.setCurrentTab(1); 361 assertEquals(TAG_TAB2, tabHost.getCurrentTabTag()); 362 } 363 364 @UiThreadTest 365 public void testOnAttachedToAndDetachedFromWindow() { 366 // implementation details 367 } 368 369 private class MyTabContentFactoryText implements TabHost.TabContentFactory { 370 public View createTabContent(String tag) { 371 final TextView tv = new TextView(mActivity); 372 tv.setText(tag); 373 return tv; 374 } 375 } 376 377 private class MyTabContentFactoryList implements TabHost.TabContentFactory { 378 public View createTabContent(String tag) { 379 final ListView lv = new ListView(mActivity); 380 return lv; 381 } 382 } 383 } 384