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