1 /* 2 * Copyright (C) 2009 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.view.cts; 18 19 import android.view.cts.R; 20 21 22 import android.app.Activity; 23 import android.app.Instrumentation; 24 import android.cts.util.PollingCheck; 25 import android.graphics.Bitmap; 26 import android.graphics.Color; 27 import android.test.ActivityInstrumentationTestCase2; 28 import android.test.TouchUtils; 29 import android.test.UiThreadTest; 30 import android.view.View; 31 import android.view.View.OnClickListener; 32 import android.view.View.OnFocusChangeListener; 33 import android.view.View.OnLongClickListener; 34 import android.widget.Button; 35 import android.widget.EditText; 36 import android.widget.RelativeLayout; 37 import android.widget.TextView; 38 39 40 public class View_UsingViewsTest extends ActivityInstrumentationTestCase2<UsingViewsCtsActivity> { 41 /** 42 * country of Argentina 43 */ 44 private static final String ARGENTINA = "Argentina"; 45 46 /** 47 * country of America 48 */ 49 private static final String AMERICA = "America"; 50 51 /** 52 * country of China 53 */ 54 private static final String CHINA = "China"; 55 56 /** 57 * the symbol of Argentina is football 58 */ 59 private static final String ARGENTINA_SYMBOL = "football"; 60 61 /** 62 * the symbol of America is basketball 63 */ 64 private static final String AMERICA_SYMBOL = "basketball"; 65 66 /** 67 * the symbol of China is table tennis 68 */ 69 private static final String CHINA_SYMBOL = "table tennis"; 70 71 private Activity mActivity; 72 private Instrumentation mInstrumentation; 73 74 private EditText mEditText; 75 private Button mButtonOk; 76 private Button mButtonCancel; 77 private TextView mSymbolTextView; 78 private TextView mWarningTextView; 79 View_UsingViewsTest()80 public View_UsingViewsTest() { 81 super("android.view.cts", UsingViewsCtsActivity.class); 82 } 83 84 @Override setUp()85 protected void setUp() throws Exception { 86 super.setUp(); 87 88 mActivity = getActivity(); 89 mInstrumentation = getInstrumentation(); 90 91 mEditText = (EditText) mActivity.findViewById(R.id.entry); 92 mButtonOk = (Button) mActivity.findViewById(R.id.ok); 93 mButtonCancel = (Button) mActivity.findViewById(R.id.cancel); 94 mSymbolTextView = (TextView) mActivity.findViewById(R.id.symbolball); 95 mWarningTextView = (TextView) mActivity.findViewById(R.id.warning); 96 } 97 98 @UiThreadTest testSetProperties()99 public void testSetProperties() { 100 /** 101 * setClickable, setOnClickListener 102 */ 103 mButtonOk.setClickable(true); 104 assertTrue(mButtonOk.isClickable()); 105 106 MockOnClickOkListener okButtonListener = new MockOnClickOkListener(); 107 mButtonOk.setOnClickListener(okButtonListener); 108 assertFalse(okButtonListener.hasOnClickCalled()); 109 110 mButtonOk.performClick(); 111 assertTrue(okButtonListener.hasOnClickCalled()); 112 113 mButtonCancel.setClickable(false); 114 assertFalse(mButtonCancel.isClickable()); 115 116 MockOnClickCancelListener cancelButtonListener = new MockOnClickCancelListener(); 117 mButtonCancel.setOnClickListener(cancelButtonListener); 118 assertFalse(cancelButtonListener.hasOnClickCalled()); 119 assertTrue(mButtonCancel.isClickable()); 120 121 mButtonCancel.performClick(); 122 assertTrue(cancelButtonListener.hasOnClickCalled()); 123 124 /** 125 * setDrawingCacheEnabled, setDrawingCacheQuality, setDrawingCacheBackgroundColor, 126 */ 127 mEditText.setDrawingCacheEnabled(true); 128 assertTrue(mEditText.isDrawingCacheEnabled()); 129 130 // the default quality is auto 131 assertEquals(View.DRAWING_CACHE_QUALITY_AUTO, mEditText.getDrawingCacheQuality()); 132 mEditText.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_LOW); 133 assertEquals(View.DRAWING_CACHE_QUALITY_LOW, mEditText.getDrawingCacheQuality()); 134 mEditText.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH); 135 assertEquals(View.DRAWING_CACHE_QUALITY_HIGH, mEditText.getDrawingCacheQuality()); 136 137 mEditText.setDrawingCacheBackgroundColor(Color.GREEN); 138 assertEquals(Color.GREEN, mEditText.getDrawingCacheBackgroundColor()); 139 140 // create the cache 141 Bitmap b = mEditText.getDrawingCache(); 142 assertNotNull(b); 143 assertEquals(mEditText.getHeight(), b.getHeight()); 144 assertEquals(mEditText.getWidth(), b.getWidth()); 145 assertEquals(Color.GREEN, b.getPixel(0, 0)); 146 147 // setDrawingCacheEnabled to false 148 mEditText.setDrawingCacheEnabled(false); 149 assertFalse(mEditText.isDrawingCacheEnabled()); 150 151 mEditText.setDrawingCacheBackgroundColor(Color.YELLOW); 152 assertEquals(Color.YELLOW, mEditText.getDrawingCacheBackgroundColor()); 153 154 // build drawable cache 155 mEditText.buildDrawingCache(); 156 b = mEditText.getDrawingCache(); 157 assertNotNull(b); 158 assertEquals(mEditText.getHeight(), b.getHeight()); 159 assertEquals(mEditText.getWidth(), b.getWidth()); 160 assertEquals(Color.YELLOW, b.getPixel(0, 0)); 161 mEditText.destroyDrawingCache(); 162 163 /** 164 * setDuplicateParentStateEnabled 165 */ 166 TextView v = new TextView(mActivity); 167 v.setSingleLine(); // otherwise the multiline state interferes with theses tests 168 v.setEnabled(false); 169 v.setText("Test setDuplicateParentStateEnabled"); 170 171 v.setDuplicateParentStateEnabled(false); 172 assertFalse(v.isDuplicateParentStateEnabled()); 173 174 RelativeLayout parent = (RelativeLayout) mEditText.getParent(); 175 parent.addView(v); 176 177 assertFalse(parent.getDrawableState().length == v.getDrawableState().length); 178 parent.removeView(v); 179 180 v.setDuplicateParentStateEnabled(true); 181 assertTrue(v.isDuplicateParentStateEnabled()); 182 183 parent.addView(v); 184 v.refreshDrawableState(); 185 186 assertEquals(parent.getDrawableState().length, v.getDrawableState().length); 187 assertEquals(parent.getDrawableState().toString(), v.getDrawableState().toString()); 188 parent.removeView(v); 189 190 /** 191 * setEnabled 192 */ 193 mWarningTextView.setEnabled(false); 194 assertFalse(mWarningTextView.isEnabled()); 195 196 mWarningTextView.setEnabled(true); 197 assertTrue(mWarningTextView.isEnabled()); 198 199 /** 200 * setFadingEdgeLength, setVerticalFadingEdgeEnabled and 201 * setHorizontalFadingEdgeEnabled(boolean) 202 */ 203 mWarningTextView.setVerticalFadingEdgeEnabled(true); 204 assertTrue(mWarningTextView.isVerticalFadingEdgeEnabled()); 205 mWarningTextView.setFadingEdgeLength(10); 206 207 mSymbolTextView.setHorizontalFadingEdgeEnabled(true); 208 assertTrue(mSymbolTextView.isHorizontalFadingEdgeEnabled()); 209 mSymbolTextView.setFadingEdgeLength(100); 210 211 /** 212 * setFocusable and setFocusableInTouchMode 213 */ 214 mButtonCancel.setFocusable(false); 215 assertFalse(mButtonCancel.isFocusable()); 216 assertFalse(mButtonCancel.isFocusableInTouchMode()); 217 218 mButtonCancel.setFocusable(true); 219 assertTrue(mButtonCancel.isFocusable()); 220 assertFalse(mButtonCancel.isFocusableInTouchMode()); 221 222 mButtonCancel.setFocusableInTouchMode(true); 223 assertTrue(mButtonCancel.isFocusable()); 224 assertTrue(mButtonCancel.isFocusableInTouchMode()); 225 226 mButtonOk.setFocusable(false); 227 assertFalse(mButtonOk.isFocusable()); 228 assertFalse(mButtonOk.isFocusableInTouchMode()); 229 230 mButtonOk.setFocusableInTouchMode(true); 231 assertTrue(mButtonOk.isFocusable()); 232 assertTrue(mButtonOk.isFocusableInTouchMode()); 233 234 /** 235 * setHorizontalScrollBarEnabled and setVerticalScrollBarEnabled 236 */ 237 // both two bar is not drawn by default 238 assertFalse(parent.isHorizontalScrollBarEnabled()); 239 assertFalse(parent.isVerticalScrollBarEnabled()); 240 241 parent.setHorizontalScrollBarEnabled(true); 242 assertTrue(parent.isHorizontalScrollBarEnabled()); 243 244 parent.setVerticalScrollBarEnabled(true); 245 assertTrue(parent.isVerticalScrollBarEnabled()); 246 247 /** 248 * setId 249 */ 250 assertEquals(View.NO_ID, parent.getId()); 251 assertEquals(R.id.entry, mEditText.getId()); 252 assertEquals(R.id.symbolball, mSymbolTextView.getId()); 253 254 mSymbolTextView.setId(0x5555); 255 assertEquals(0x5555, mSymbolTextView.getId()); 256 TextView t = (TextView) parent.findViewById(0x5555); 257 assertSame(mSymbolTextView, t); 258 259 mSymbolTextView.setId(R.id.symbolball); 260 assertEquals(R.id.symbolball, mSymbolTextView.getId()); 261 } 262 263 @UiThreadTest testSetFocus()264 public void testSetFocus() throws Throwable { 265 boolean focusWasOnEditText = mEditText.hasFocus(); 266 267 MockOnFocusChangeListener editListener = new MockOnFocusChangeListener(); 268 MockOnFocusChangeListener okListener = new MockOnFocusChangeListener(); 269 MockOnFocusChangeListener cancelListener = new MockOnFocusChangeListener(); 270 MockOnFocusChangeListener symbolListener = new MockOnFocusChangeListener(); 271 MockOnFocusChangeListener warningListener = new MockOnFocusChangeListener(); 272 273 mEditText.setOnFocusChangeListener(editListener); 274 mButtonOk.setOnFocusChangeListener(okListener); 275 mButtonCancel.setOnFocusChangeListener(cancelListener); 276 mSymbolTextView.setOnFocusChangeListener(symbolListener); 277 mWarningTextView.setOnFocusChangeListener(warningListener); 278 279 mSymbolTextView.setText(ARGENTINA_SYMBOL); 280 mWarningTextView.setVisibility(View.VISIBLE); 281 282 assertTrue(mEditText.requestFocus()); 283 assertTrue(mEditText.hasFocus()); 284 assertFalse(mButtonOk.hasFocus()); 285 assertFalse(mButtonCancel.hasFocus()); 286 assertFalse(mSymbolTextView.hasFocus()); 287 assertFalse(mWarningTextView.hasFocus()); 288 289 assertTrue(editListener.hasFocus() || focusWasOnEditText); 290 assertFalse(okListener.hasFocus()); 291 assertFalse(cancelListener.hasFocus()); 292 assertFalse(symbolListener.hasFocus()); 293 assertFalse(warningListener.hasFocus()); 294 295 // set ok button to focus 296 assertTrue(mButtonOk.requestFocus()); 297 assertTrue(mButtonOk.hasFocus()); 298 assertTrue(okListener.hasFocus()); 299 assertFalse(mEditText.hasFocus()); 300 assertFalse(editListener.hasFocus()); 301 302 // set cancel button to focus 303 assertTrue(mButtonCancel.requestFocus()); 304 assertTrue(mButtonCancel.hasFocus()); 305 assertTrue(cancelListener.hasFocus()); 306 assertFalse(mButtonOk.hasFocus()); 307 assertFalse(okListener.hasFocus()); 308 309 // set symbol text to focus 310 mSymbolTextView.setFocusable(true); 311 assertTrue(mSymbolTextView.requestFocus()); 312 assertTrue(mSymbolTextView.hasFocus()); 313 assertTrue(symbolListener.hasFocus()); 314 assertFalse(mButtonCancel.hasFocus()); 315 assertFalse(cancelListener.hasFocus()); 316 317 // set warning text to focus 318 mWarningTextView.setFocusable(true); 319 assertTrue(mWarningTextView.requestFocus()); 320 assertTrue(mWarningTextView.hasFocus()); 321 assertTrue(warningListener.hasFocus()); 322 assertFalse(mSymbolTextView.hasFocus()); 323 assertFalse(symbolListener.hasFocus()); 324 325 // set edit text to focus 326 assertTrue(mEditText.requestFocus()); 327 assertTrue(mEditText.hasFocus()); 328 assertTrue(editListener.hasFocus()); 329 assertFalse(mWarningTextView.hasFocus()); 330 assertFalse(warningListener.hasFocus()); 331 } 332 testSetupListeners()333 public void testSetupListeners() throws Throwable { 334 // set ok button OnClick listener 335 mButtonOk.setClickable(true); 336 assertTrue(mButtonOk.isClickable()); 337 338 MockOnClickOkListener okButtonListener = new MockOnClickOkListener(); 339 mButtonOk.setOnClickListener(okButtonListener); 340 341 // set cancel button OnClick listener 342 mButtonCancel.setClickable(true); 343 assertTrue(mButtonCancel.isClickable()); 344 345 MockOnClickCancelListener cancelButtonListener = new MockOnClickCancelListener(); 346 mButtonCancel.setOnClickListener(cancelButtonListener); 347 348 // set edit text OnLongClick listener 349 mEditText.setLongClickable(true); 350 assertTrue(mEditText.isLongClickable()); 351 352 final MockOnLongClickListener onLongClickListener = new MockOnLongClickListener(); 353 mEditText.setOnLongClickListener(onLongClickListener); 354 355 // long click the edit text 356 assertFalse(onLongClickListener.isOnLongClickCalled()); 357 assertNull(onLongClickListener.getView()); 358 359 mInstrumentation.waitForIdleSync(); 360 TouchUtils.longClickView(this, mEditText); 361 new PollingCheck() { 362 @Override 363 protected boolean check() { 364 return onLongClickListener.isOnLongClickCalled(); 365 } 366 }.run(); 367 assertSame(mEditText, onLongClickListener.getView()); 368 369 // click the Cancel button 370 runTestOnUiThread(new Runnable() { 371 public void run() { 372 mEditText.setText("Germany"); 373 } 374 }); 375 mInstrumentation.waitForIdleSync(); 376 377 TouchUtils.clickView(this, mButtonCancel); 378 assertEquals("", mEditText.getText().toString()); 379 380 // click the OK button 381 runTestOnUiThread(new Runnable() { 382 public void run() { 383 mEditText.setText(ARGENTINA); 384 } 385 }); 386 mInstrumentation.waitForIdleSync(); 387 388 TouchUtils.clickView(this, mButtonOk); 389 assertEquals(ARGENTINA_SYMBOL, mSymbolTextView.getText().toString()); 390 391 runTestOnUiThread(new Runnable() { 392 public void run() { 393 mEditText.setText(AMERICA); 394 } 395 }); 396 mInstrumentation.waitForIdleSync(); 397 398 TouchUtils.clickView(this, mButtonOk); 399 assertEquals(AMERICA_SYMBOL, mSymbolTextView.getText().toString()); 400 401 runTestOnUiThread(new Runnable() { 402 public void run() { 403 mEditText.setText(CHINA); 404 } 405 }); 406 mInstrumentation.waitForIdleSync(); 407 408 TouchUtils.clickView(this, mButtonOk); 409 assertEquals(CHINA_SYMBOL, mSymbolTextView.getText().toString()); 410 411 runTestOnUiThread(new Runnable() { 412 public void run() { 413 mEditText.setText("Unknown"); 414 } 415 }); 416 mInstrumentation.waitForIdleSync(); 417 418 TouchUtils.clickView(this, mButtonOk); 419 assertEquals(View.VISIBLE, mWarningTextView.getVisibility()); 420 } 421 422 @UiThreadTest testSetVisibility()423 public void testSetVisibility() throws Throwable { 424 mActivity.setContentView(R.layout.view_visibility_layout); 425 426 View v1 = mActivity.findViewById(R.id.textview1); 427 View v2 = mActivity.findViewById(R.id.textview2); 428 View v3 = mActivity.findViewById(R.id.textview3); 429 430 assertNotNull(v1); 431 assertNotNull(v2); 432 assertNotNull(v3); 433 434 assertEquals(View.VISIBLE, v1.getVisibility()); 435 assertEquals(View.INVISIBLE, v2.getVisibility()); 436 assertEquals(View.GONE, v3.getVisibility()); 437 438 v1.setVisibility(View.GONE); 439 assertEquals(View.GONE, v1.getVisibility()); 440 441 v2.setVisibility(View.VISIBLE); 442 assertEquals(View.VISIBLE, v2.getVisibility()); 443 444 v3.setVisibility(View.INVISIBLE); 445 assertEquals(View.INVISIBLE, v3.getVisibility()); 446 } 447 448 private static class MockOnFocusChangeListener implements OnFocusChangeListener { 449 private boolean mHasFocus; 450 onFocusChange(View v, boolean hasFocus)451 public void onFocusChange(View v, boolean hasFocus) { 452 mHasFocus = hasFocus; 453 } 454 hasFocus()455 public boolean hasFocus() { 456 return mHasFocus; 457 } 458 } 459 460 private class MockOnClickOkListener implements OnClickListener { 461 private boolean mHasOnClickCalled = false; 462 showPicture(String country)463 private boolean showPicture(String country) { 464 if (ARGENTINA.equals(country)) { 465 mSymbolTextView.setText(ARGENTINA_SYMBOL); 466 return true; 467 } else if (AMERICA.equals(country)) { 468 mSymbolTextView.setText(AMERICA_SYMBOL); 469 return true; 470 } else if (CHINA.equals(country)) { 471 mSymbolTextView.setText(CHINA_SYMBOL); 472 return true; 473 } 474 475 return false; 476 } 477 onClick(View v)478 public void onClick(View v) { 479 mHasOnClickCalled = true; 480 481 String country = mEditText.getText().toString(); 482 if (!showPicture(country)) { 483 mWarningTextView.setVisibility(View.VISIBLE); 484 } else if (View.VISIBLE == mWarningTextView.getVisibility()) { 485 mWarningTextView.setVisibility(View.INVISIBLE); 486 } 487 } 488 hasOnClickCalled()489 public boolean hasOnClickCalled() { 490 return mHasOnClickCalled; 491 } 492 reset()493 public void reset() { 494 mHasOnClickCalled = false; 495 } 496 } 497 498 private class MockOnClickCancelListener implements OnClickListener { 499 private boolean mHasOnClickCalled = false; 500 onClick(View v)501 public void onClick(View v) { 502 mHasOnClickCalled = true; 503 504 mEditText.setText(null); 505 } 506 hasOnClickCalled()507 public boolean hasOnClickCalled() { 508 return mHasOnClickCalled; 509 } 510 reset()511 public void reset() { 512 mHasOnClickCalled = false; 513 } 514 } 515 516 private static class MockOnLongClickListener implements OnLongClickListener { 517 private boolean mIsOnLongClickCalled; 518 private View mView; 519 onLongClick(View v)520 public boolean onLongClick(View v) { 521 mIsOnLongClickCalled = true; 522 mView = v; 523 return true; 524 } 525 isOnLongClickCalled()526 public boolean isOnLongClickCalled() { 527 return mIsOnLongClickCalled; 528 } 529 getView()530 public View getView() { 531 return mView; 532 } 533 } 534 } 535