Home | History | Annotate | Download | only in cts
      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 com.android.cts.stub.R;
     20 
     21 import dalvik.annotation.TestLevel;
     22 import dalvik.annotation.TestTargetClass;
     23 import dalvik.annotation.TestTargetNew;
     24 import dalvik.annotation.TestTargets;
     25 
     26 import android.app.Activity;
     27 import android.app.Instrumentation;
     28 import android.graphics.Bitmap;
     29 import android.graphics.Color;
     30 import android.test.ActivityInstrumentationTestCase2;
     31 import android.test.TouchUtils;
     32 import android.test.UiThreadTest;
     33 import android.view.View;
     34 import android.view.View.OnClickListener;
     35 import android.view.View.OnFocusChangeListener;
     36 import android.view.View.OnLongClickListener;
     37 import android.widget.Button;
     38 import android.widget.EditText;
     39 import android.widget.RelativeLayout;
     40 import android.widget.TextView;
     41 
     42 @TestTargetClass(View.class)
     43 public class View_UsingViewsTest extends ActivityInstrumentationTestCase2<UsingViewsStubActivity> {
     44     /**
     45      * country of Argentina
     46      */
     47     private static final String ARGENTINA = "Argentina";
     48 
     49     /**
     50      * country of America
     51      */
     52     private static final String AMERICA = "America";
     53 
     54     /**
     55      * country of China
     56      */
     57     private static final String CHINA = "China";
     58 
     59     /**
     60      * the symbol of Argentina is football
     61      */
     62     private static final String ARGENTINA_SYMBOL = "football";
     63 
     64     /**
     65      * the symbol of America is basketball
     66      */
     67     private static final String AMERICA_SYMBOL = "basketball";
     68 
     69     /**
     70      * the symbol of China is table tennis
     71      */
     72     private static final String CHINA_SYMBOL = "table tennis";
     73 
     74     private Activity mActivity;
     75     private Instrumentation mInstrumentation;
     76 
     77     private EditText mEditText;
     78     private Button mButtonOk;
     79     private Button mButtonCancel;
     80     private TextView mSymbolTextView;
     81     private TextView mWarningTextView;
     82 
     83     public View_UsingViewsTest() {
     84         super("com.android.cts.stub", UsingViewsStubActivity.class);
     85     }
     86 
     87     @Override
     88     protected void setUp() throws Exception {
     89         super.setUp();
     90 
     91         mActivity = getActivity();
     92         mInstrumentation = getInstrumentation();
     93 
     94         mEditText = (EditText) mActivity.findViewById(R.id.entry);
     95         mButtonOk = (Button) mActivity.findViewById(R.id.ok);
     96         mButtonCancel = (Button) mActivity.findViewById(R.id.cancel);
     97         mSymbolTextView = (TextView) mActivity.findViewById(R.id.symbolball);
     98         mWarningTextView = (TextView) mActivity.findViewById(R.id.warning);
     99     }
    100 
    101     @TestTargets({
    102         @TestTargetNew(
    103             level = TestLevel.COMPLETE,
    104             method = "setClickable",
    105             args = {boolean.class}
    106         ),
    107         @TestTargetNew(
    108             level = TestLevel.COMPLETE,
    109             method = "isClickable",
    110             args = {}
    111         ),
    112         @TestTargetNew(
    113             level = TestLevel.COMPLETE,
    114             method = "setOnClickListener",
    115             args = {OnClickListener.class}
    116         ),
    117         @TestTargetNew(
    118             level = TestLevel.COMPLETE,
    119             method = "performClick",
    120             args = {}
    121         ),
    122         @TestTargetNew(
    123             level = TestLevel.COMPLETE,
    124             method = "setDrawingCacheEnabled",
    125             args = {boolean.class}
    126         ),
    127         @TestTargetNew(
    128             level = TestLevel.COMPLETE,
    129             method = "isDrawingCacheEnabled",
    130             args = {}
    131         ),
    132         @TestTargetNew(
    133             level = TestLevel.COMPLETE,
    134             method = "setDrawingCacheQuality",
    135             args = {int.class}
    136         ),
    137         @TestTargetNew(
    138             level = TestLevel.COMPLETE,
    139             method = "getDrawingCacheQuality",
    140             args = {}
    141         ),
    142         @TestTargetNew(
    143             level = TestLevel.COMPLETE,
    144             method = "setDrawingCacheBackgroundColor",
    145             args = {int.class}
    146         ),
    147         @TestTargetNew(
    148             level = TestLevel.COMPLETE,
    149             method = "getDrawingCacheBackgroundColor",
    150             args = {}
    151         ),
    152         @TestTargetNew(
    153             level = TestLevel.COMPLETE,
    154             method = "getDrawingCache",
    155             args = {}
    156         ),
    157         @TestTargetNew(
    158             level = TestLevel.COMPLETE,
    159             method = "buildDrawingCache",
    160             args = {}
    161         ),
    162         @TestTargetNew(
    163             level = TestLevel.COMPLETE,
    164             method = "destroyDrawingCache",
    165             args = {}
    166         ),
    167         @TestTargetNew(
    168             level = TestLevel.COMPLETE,
    169             method = "setDuplicateParentStateEnabled",
    170             args = {boolean.class}
    171         ),
    172         @TestTargetNew(
    173             level = TestLevel.COMPLETE,
    174             method = "isDuplicateParentStateEnabled",
    175             args = {}
    176         ),
    177         @TestTargetNew(
    178             level = TestLevel.COMPLETE,
    179             method = "refreshDrawableState",
    180             args = {}
    181         ),
    182         @TestTargetNew(
    183             level = TestLevel.COMPLETE,
    184             method = "getDrawableState",
    185             args = {}
    186         ),
    187         @TestTargetNew(
    188             level = TestLevel.COMPLETE,
    189             method = "setEnabled",
    190             args = {boolean.class}
    191         ),
    192         @TestTargetNew(
    193             level = TestLevel.COMPLETE,
    194             method = "isEnabled",
    195             args = {}
    196         ),
    197         @TestTargetNew(
    198             level = TestLevel.COMPLETE,
    199             method = "setVerticalFadingEdgeEnabled",
    200             args = {boolean.class}
    201         ),
    202         @TestTargetNew(
    203             level = TestLevel.COMPLETE,
    204             method = "isVerticalFadingEdgeEnabled",
    205             args = {}
    206         ),
    207         @TestTargetNew(
    208             level = TestLevel.COMPLETE,
    209             method = "setFadingEdgeLength",
    210             args = {int.class}
    211         ),
    212         @TestTargetNew(
    213             level = TestLevel.COMPLETE,
    214             method = "setHorizontalFadingEdgeEnabled",
    215             args = {boolean.class}
    216         ),
    217         @TestTargetNew(
    218             level = TestLevel.COMPLETE,
    219             method = "isHorizontalFadingEdgeEnabled",
    220             args = {}
    221         ),
    222         @TestTargetNew(
    223             level = TestLevel.COMPLETE,
    224             method = "setFocusable",
    225             args = {boolean.class}
    226         ),
    227         @TestTargetNew(
    228             level = TestLevel.COMPLETE,
    229             method = "isFocusable",
    230             args = {}
    231         ),
    232         @TestTargetNew(
    233             level = TestLevel.COMPLETE,
    234             method = "setFocusableInTouchMode",
    235             args = {boolean.class}
    236         ),
    237         @TestTargetNew(
    238             level = TestLevel.COMPLETE,
    239             method = "isFocusableInTouchMode",
    240             args = {}
    241         ),
    242         @TestTargetNew(
    243             level = TestLevel.COMPLETE,
    244             method = "isHorizontalScrollBarEnabled",
    245             args = {}
    246         ),
    247         @TestTargetNew(
    248             level = TestLevel.COMPLETE,
    249             method = "isVerticalScrollBarEnabled",
    250             args = {}
    251         ),
    252         @TestTargetNew(
    253             level = TestLevel.COMPLETE,
    254             method = "setHorizontalScrollBarEnabled",
    255             args = {boolean.class}
    256         ),
    257         @TestTargetNew(
    258             level = TestLevel.COMPLETE,
    259             method = "setVerticalScrollBarEnabled",
    260             args = {boolean.class}
    261         ),
    262         @TestTargetNew(
    263             level = TestLevel.COMPLETE,
    264             method = "setId",
    265             args = {int.class}
    266         ),
    267         @TestTargetNew(
    268             level = TestLevel.COMPLETE,
    269             method = "getId",
    270             args = {}
    271         ),
    272         @TestTargetNew(
    273             level = TestLevel.COMPLETE,
    274             method = "findViewById",
    275             args = {int.class}
    276         )
    277     })
    278     @UiThreadTest
    279     public void testSetProperties() {
    280         /**
    281          * setClickable, setOnClickListener
    282          */
    283         mButtonOk.setClickable(true);
    284         assertTrue(mButtonOk.isClickable());
    285 
    286         MockOnClickOkListener okButtonListener = new MockOnClickOkListener();
    287         mButtonOk.setOnClickListener(okButtonListener);
    288         assertFalse(okButtonListener.hasOnClickCalled());
    289 
    290         mButtonOk.performClick();
    291         assertTrue(okButtonListener.hasOnClickCalled());
    292 
    293         mButtonCancel.setClickable(false);
    294         assertFalse(mButtonCancel.isClickable());
    295 
    296         MockOnClickCancelListener cancelButtonListener = new MockOnClickCancelListener();
    297         mButtonCancel.setOnClickListener(cancelButtonListener);
    298         assertFalse(cancelButtonListener.hasOnClickCalled());
    299         assertTrue(mButtonCancel.isClickable());
    300 
    301         mButtonCancel.performClick();
    302         assertTrue(cancelButtonListener.hasOnClickCalled());
    303 
    304         /**
    305          * setDrawingCacheEnabled, setDrawingCacheQuality, setDrawingCacheBackgroundColor,
    306          */
    307         mEditText.setDrawingCacheEnabled(true);
    308         assertTrue(mEditText.isDrawingCacheEnabled());
    309 
    310         // the default quality is auto
    311         assertEquals(View.DRAWING_CACHE_QUALITY_AUTO, mEditText.getDrawingCacheQuality());
    312         mEditText.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_LOW);
    313         assertEquals(View.DRAWING_CACHE_QUALITY_LOW, mEditText.getDrawingCacheQuality());
    314         mEditText.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
    315         assertEquals(View.DRAWING_CACHE_QUALITY_HIGH, mEditText.getDrawingCacheQuality());
    316 
    317         mEditText.setDrawingCacheBackgroundColor(Color.GREEN);
    318         assertEquals(Color.GREEN, mEditText.getDrawingCacheBackgroundColor());
    319 
    320         // create the cache
    321         Bitmap b = mEditText.getDrawingCache();
    322         assertNotNull(b);
    323         assertEquals(mEditText.getHeight(), b.getHeight());
    324         assertEquals(mEditText.getWidth(), b.getWidth());
    325         assertEquals(Color.GREEN, b.getPixel(0, 0));
    326 
    327         // setDrawingCacheEnabled to false
    328         mEditText.setDrawingCacheEnabled(false);
    329         assertFalse(mEditText.isDrawingCacheEnabled());
    330 
    331         mEditText.setDrawingCacheBackgroundColor(Color.YELLOW);
    332         assertEquals(Color.YELLOW, mEditText.getDrawingCacheBackgroundColor());
    333 
    334         // build drawable cache
    335         mEditText.buildDrawingCache();
    336         b = mEditText.getDrawingCache();
    337         assertNotNull(b);
    338         assertEquals(mEditText.getHeight(), b.getHeight());
    339         assertEquals(mEditText.getWidth(), b.getWidth());
    340         assertEquals(Color.YELLOW, b.getPixel(0, 0));
    341         mEditText.destroyDrawingCache();
    342 
    343         /**
    344          * setDuplicateParentStateEnabled
    345          */
    346         TextView v = new TextView(mActivity);
    347         v.setText("Test setDuplicateParentStateEnabled");
    348 
    349         v.setDuplicateParentStateEnabled(false);
    350         assertFalse(v.isDuplicateParentStateEnabled());
    351 
    352         RelativeLayout parent = (RelativeLayout) mEditText.getParent();
    353         parent.addView(v);
    354 
    355         assertFalse(parent.getDrawableState().length == v.getDrawableState().length);
    356         parent.removeView(v);
    357 
    358         v.setDuplicateParentStateEnabled(true);
    359         assertTrue(v.isDuplicateParentStateEnabled());
    360 
    361         parent.addView(v);
    362         v.refreshDrawableState();
    363 
    364         assertEquals(parent.getDrawableState().length, v.getDrawableState().length);
    365         assertEquals(parent.getDrawableState().toString(), v.getDrawableState().toString());
    366         parent.removeView(v);
    367 
    368         /**
    369          * setEnabled
    370          */
    371         mWarningTextView.setEnabled(false);
    372         assertFalse(mWarningTextView.isEnabled());
    373 
    374         mWarningTextView.setEnabled(true);
    375         assertTrue(mWarningTextView.isEnabled());
    376 
    377         /**
    378          * setFadingEdgeLength, setVerticalFadingEdgeEnabled and
    379          * setHorizontalFadingEdgeEnabled(boolean)
    380          */
    381         mWarningTextView.setVerticalFadingEdgeEnabled(true);
    382         assertTrue(mWarningTextView.isVerticalFadingEdgeEnabled());
    383         mWarningTextView.setFadingEdgeLength(10);
    384 
    385         mSymbolTextView.setHorizontalFadingEdgeEnabled(true);
    386         assertTrue(mSymbolTextView.isHorizontalFadingEdgeEnabled());
    387         mSymbolTextView.setFadingEdgeLength(100);
    388 
    389         /**
    390          * setFocusable and setFocusableInTouchMode
    391          */
    392         mButtonCancel.setFocusable(false);
    393         assertFalse(mButtonCancel.isFocusable());
    394         assertFalse(mButtonCancel.isFocusableInTouchMode());
    395 
    396         mButtonCancel.setFocusable(true);
    397         assertTrue(mButtonCancel.isFocusable());
    398         assertFalse(mButtonCancel.isFocusableInTouchMode());
    399 
    400         mButtonCancel.setFocusableInTouchMode(true);
    401         assertTrue(mButtonCancel.isFocusable());
    402         assertTrue(mButtonCancel.isFocusableInTouchMode());
    403 
    404         mButtonOk.setFocusable(false);
    405         assertFalse(mButtonOk.isFocusable());
    406         assertFalse(mButtonOk.isFocusableInTouchMode());
    407 
    408         mButtonOk.setFocusableInTouchMode(true);
    409         assertTrue(mButtonOk.isFocusable());
    410         assertTrue(mButtonOk.isFocusableInTouchMode());
    411 
    412         /**
    413          * setHorizontalScrollBarEnabled and setVerticalScrollBarEnabled
    414          */
    415         // both two bar is not drawn by default
    416         assertFalse(parent.isHorizontalScrollBarEnabled());
    417         assertFalse(parent.isVerticalScrollBarEnabled());
    418 
    419         parent.setHorizontalScrollBarEnabled(true);
    420         assertTrue(parent.isHorizontalScrollBarEnabled());
    421 
    422         parent.setVerticalScrollBarEnabled(true);
    423         assertTrue(parent.isVerticalScrollBarEnabled());
    424 
    425         /**
    426          * setId
    427          */
    428         assertEquals(View.NO_ID, parent.getId());
    429         assertEquals(R.id.entry, mEditText.getId());
    430         assertEquals(R.id.symbolball, mSymbolTextView.getId());
    431 
    432         mSymbolTextView.setId(0x5555);
    433         assertEquals(0x5555, mSymbolTextView.getId());
    434         TextView t = (TextView) parent.findViewById(0x5555);
    435         assertSame(mSymbolTextView, t);
    436 
    437         mSymbolTextView.setId(R.id.symbolball);
    438         assertEquals(R.id.symbolball, mSymbolTextView.getId());
    439     }
    440 
    441     @TestTargets({
    442         @TestTargetNew(
    443             level = TestLevel.COMPLETE,
    444             method = "setOnFocusChangeListener",
    445             args = {OnFocusChangeListener.class}
    446         ),
    447         @TestTargetNew(
    448             level = TestLevel.COMPLETE,
    449             method = "setVisibility",
    450             args = {int.class}
    451         ),
    452         @TestTargetNew(
    453             level = TestLevel.COMPLETE,
    454             method = "hasFocus",
    455             args = {}
    456         ),
    457         @TestTargetNew(
    458             level = TestLevel.COMPLETE,
    459             method = "requestFocus",
    460             args = {}
    461         ),
    462         @TestTargetNew(
    463             level = TestLevel.COMPLETE,
    464             method = "setFocusable",
    465             args = {boolean.class}
    466         )
    467     })
    468     @UiThreadTest
    469     public void testSetFocus() throws Throwable {
    470         MockOnFocusChangeListener editListener = new MockOnFocusChangeListener();
    471         MockOnFocusChangeListener okListener = new MockOnFocusChangeListener();
    472         MockOnFocusChangeListener cancelListener = new MockOnFocusChangeListener();
    473         MockOnFocusChangeListener symbolListener = new MockOnFocusChangeListener();
    474         MockOnFocusChangeListener warningListener = new MockOnFocusChangeListener();
    475 
    476         mEditText.setOnFocusChangeListener(editListener);
    477         mButtonOk.setOnFocusChangeListener(okListener);
    478         mButtonCancel.setOnFocusChangeListener(cancelListener);
    479         mSymbolTextView.setOnFocusChangeListener(symbolListener);
    480         mWarningTextView.setOnFocusChangeListener(warningListener);
    481 
    482         mSymbolTextView.setText(ARGENTINA_SYMBOL);
    483         mWarningTextView.setVisibility(View.VISIBLE);
    484 
    485         assertTrue(mEditText.hasFocus());
    486         assertFalse(mButtonOk.hasFocus());
    487         assertFalse(mButtonCancel.hasFocus());
    488         assertFalse(mSymbolTextView.hasFocus());
    489         assertFalse(mWarningTextView.hasFocus());
    490 
    491         assertFalse(editListener.hasFocus());
    492         assertFalse(okListener.hasFocus());
    493         assertFalse(cancelListener.hasFocus());
    494         assertFalse(symbolListener.hasFocus());
    495         assertFalse(warningListener.hasFocus());
    496 
    497         // set ok button to focus
    498         assertTrue(mButtonOk.requestFocus());
    499         assertTrue(mButtonOk.hasFocus());
    500         assertTrue(okListener.hasFocus());
    501         assertFalse(mEditText.hasFocus());
    502         assertFalse(editListener.hasFocus());
    503 
    504         // set cancel button to focus
    505         assertTrue(mButtonCancel.requestFocus());
    506         assertTrue(mButtonCancel.hasFocus());
    507         assertTrue(cancelListener.hasFocus());
    508         assertFalse(mButtonOk.hasFocus());
    509         assertFalse(okListener.hasFocus());
    510 
    511         // set symbol text to focus
    512         mSymbolTextView.setFocusable(true);
    513         assertTrue(mSymbolTextView.requestFocus());
    514         assertTrue(mSymbolTextView.hasFocus());
    515         assertTrue(symbolListener.hasFocus());
    516         assertFalse(mButtonCancel.hasFocus());
    517         assertFalse(cancelListener.hasFocus());
    518 
    519         // set warning text to focus
    520         mWarningTextView.setFocusable(true);
    521         assertTrue(mWarningTextView.requestFocus());
    522         assertTrue(mWarningTextView.hasFocus());
    523         assertTrue(warningListener.hasFocus());
    524         assertFalse(mSymbolTextView.hasFocus());
    525         assertFalse(symbolListener.hasFocus());
    526 
    527         // set edit text to focus
    528         assertTrue(mEditText.requestFocus());
    529         assertTrue(mEditText.hasFocus());
    530         assertTrue(editListener.hasFocus());
    531         assertFalse(mWarningTextView.hasFocus());
    532         assertFalse(warningListener.hasFocus());
    533     }
    534 
    535     @TestTargets({
    536         @TestTargetNew(
    537             level = TestLevel.COMPLETE,
    538             method = "setClickable",
    539             args = {boolean.class}
    540         ),
    541         @TestTargetNew(
    542             level = TestLevel.COMPLETE,
    543             method = "isClickable",
    544             args = {}
    545         ),
    546         @TestTargetNew(
    547             level = TestLevel.COMPLETE,
    548             method = "setLongClickable",
    549             args = {boolean.class}
    550         ),
    551         @TestTargetNew(
    552             level = TestLevel.COMPLETE,
    553             method = "isLongClickable",
    554             args = {}
    555         ),
    556         @TestTargetNew(
    557             level = TestLevel.COMPLETE,
    558             method = "getVisibility",
    559             args = {}
    560         ),
    561         @TestTargetNew(
    562             level = TestLevel.COMPLETE,
    563             method = "setOnClickListener",
    564             args = {OnClickListener.class}
    565         ),
    566         @TestTargetNew(
    567             level = TestLevel.COMPLETE,
    568             method = "setOnLongClickListener",
    569             args = {OnLongClickListener.class}
    570         )
    571     })
    572     public void testSetupListeners() throws Throwable {
    573         // set ok button OnClick listener
    574         mButtonOk.setClickable(true);
    575         assertTrue(mButtonOk.isClickable());
    576 
    577         MockOnClickOkListener okButtonListener = new MockOnClickOkListener();
    578         mButtonOk.setOnClickListener(okButtonListener);
    579 
    580         // set cancel button OnClick listener
    581         mButtonCancel.setClickable(true);
    582         assertTrue(mButtonCancel.isClickable());
    583 
    584         MockOnClickCancelListener cancelButtonListener = new MockOnClickCancelListener();
    585         mButtonCancel.setOnClickListener(cancelButtonListener);
    586 
    587         // set edit text OnLongClick listener
    588         mEditText.setLongClickable(true);
    589         assertTrue(mEditText.isLongClickable());
    590 
    591         MockOnLongClickListener onLongClickListener = new MockOnLongClickListener();
    592         mEditText.setOnLongClickListener(onLongClickListener);
    593 
    594         // long click the edit text
    595         assertFalse(onLongClickListener.isOnLongClickCalled());
    596         assertNull(onLongClickListener.getView());
    597 
    598         TouchUtils.longClickView(this, mEditText);
    599         assertTrue(onLongClickListener.isOnLongClickCalled());
    600         assertSame(mEditText, onLongClickListener.getView());
    601 
    602         // click the Cancel button
    603         runTestOnUiThread(new Runnable() {
    604             public void run() {
    605                 mEditText.setText("Germany");
    606             }
    607         });
    608         mInstrumentation.waitForIdleSync();
    609 
    610         TouchUtils.clickView(this, mButtonCancel);
    611         assertEquals("", mEditText.getText().toString());
    612 
    613         // click the OK button
    614         runTestOnUiThread(new Runnable() {
    615             public void run() {
    616                 mEditText.setText(ARGENTINA);
    617             }
    618         });
    619         mInstrumentation.waitForIdleSync();
    620 
    621         TouchUtils.clickView(this, mButtonOk);
    622         assertEquals(ARGENTINA_SYMBOL, mSymbolTextView.getText().toString());
    623 
    624         runTestOnUiThread(new Runnable() {
    625             public void run() {
    626                 mEditText.setText(AMERICA);
    627             }
    628         });
    629         mInstrumentation.waitForIdleSync();
    630 
    631         TouchUtils.clickView(this, mButtonOk);
    632         assertEquals(AMERICA_SYMBOL, mSymbolTextView.getText().toString());
    633 
    634         runTestOnUiThread(new Runnable() {
    635             public void run() {
    636                 mEditText.setText(CHINA);
    637             }
    638         });
    639         mInstrumentation.waitForIdleSync();
    640 
    641         TouchUtils.clickView(this, mButtonOk);
    642         assertEquals(CHINA_SYMBOL, mSymbolTextView.getText().toString());
    643 
    644         runTestOnUiThread(new Runnable() {
    645             public void run() {
    646                 mEditText.setText("Unknown");
    647             }
    648         });
    649         mInstrumentation.waitForIdleSync();
    650 
    651         TouchUtils.clickView(this, mButtonOk);
    652         assertEquals(View.VISIBLE, mWarningTextView.getVisibility());
    653     }
    654 
    655     @TestTargets({
    656         @TestTargetNew(
    657             level = TestLevel.COMPLETE,
    658             method = "setVisibility",
    659             args = {int.class}
    660         ),
    661         @TestTargetNew(
    662             level = TestLevel.COMPLETE,
    663             method = "getVisibility",
    664             args = {}
    665         )
    666     })
    667     @UiThreadTest
    668     public void testSetVisibility() throws Throwable {
    669         mActivity.setContentView(R.layout.view_visibility_layout);
    670 
    671         View v1 = mActivity.findViewById(R.id.textview1);
    672         View v2 = mActivity.findViewById(R.id.textview2);
    673         View v3 = mActivity.findViewById(R.id.textview3);
    674 
    675         assertNotNull(v1);
    676         assertNotNull(v2);
    677         assertNotNull(v3);
    678 
    679         assertEquals(View.VISIBLE, v1.getVisibility());
    680         assertEquals(View.INVISIBLE, v2.getVisibility());
    681         assertEquals(View.GONE, v3.getVisibility());
    682 
    683         v1.setVisibility(View.GONE);
    684         assertEquals(View.GONE, v1.getVisibility());
    685 
    686         v2.setVisibility(View.VISIBLE);
    687         assertEquals(View.VISIBLE, v2.getVisibility());
    688 
    689         v3.setVisibility(View.INVISIBLE);
    690         assertEquals(View.INVISIBLE, v3.getVisibility());
    691     }
    692 
    693     private static class MockOnFocusChangeListener implements OnFocusChangeListener {
    694         private boolean mHasFocus;
    695 
    696         public void onFocusChange(View v, boolean hasFocus) {
    697             mHasFocus = hasFocus;
    698         }
    699 
    700         public boolean hasFocus() {
    701             return mHasFocus;
    702         }
    703     }
    704 
    705     private class MockOnClickOkListener implements OnClickListener {
    706         private boolean mHasOnClickCalled = false;
    707 
    708         private boolean showPicture(String country) {
    709             if (ARGENTINA.equals(country)) {
    710                 mSymbolTextView.setText(ARGENTINA_SYMBOL);
    711                 return true;
    712             } else if (AMERICA.equals(country)) {
    713                 mSymbolTextView.setText(AMERICA_SYMBOL);
    714                 return true;
    715             } else if (CHINA.equals(country)) {
    716                 mSymbolTextView.setText(CHINA_SYMBOL);
    717                 return true;
    718             }
    719 
    720             return false;
    721         }
    722 
    723         public void onClick(View v) {
    724             mHasOnClickCalled = true;
    725 
    726             String country = mEditText.getText().toString();
    727             if (!showPicture(country)) {
    728                 mWarningTextView.setVisibility(View.VISIBLE);
    729             } else if (View.VISIBLE == mWarningTextView.getVisibility()) {
    730                 mWarningTextView.setVisibility(View.INVISIBLE);
    731             }
    732         }
    733 
    734         public boolean hasOnClickCalled() {
    735             return mHasOnClickCalled;
    736         }
    737 
    738         public void reset() {
    739             mHasOnClickCalled = false;
    740         }
    741     }
    742 
    743     private class MockOnClickCancelListener implements OnClickListener {
    744         private boolean mHasOnClickCalled = false;
    745 
    746         public void onClick(View v) {
    747             mHasOnClickCalled = true;
    748 
    749             mEditText.setText(null);
    750         }
    751 
    752         public boolean hasOnClickCalled() {
    753             return mHasOnClickCalled;
    754         }
    755 
    756         public void reset() {
    757             mHasOnClickCalled = false;
    758         }
    759     }
    760 
    761     private static class MockOnLongClickListener implements OnLongClickListener {
    762         private boolean mIsOnLongClickCalled;
    763         private View mView;
    764 
    765         public boolean onLongClick(View v) {
    766             mIsOnLongClickCalled = true;
    767             mView = v;
    768             return true;
    769         }
    770 
    771         public boolean isOnLongClickCalled() {
    772             return mIsOnLongClickCalled;
    773         }
    774 
    775         public View getView() {
    776             return mView;
    777         }
    778     }
    779 }
    780