Home | History | Annotate | Download | only in cts
      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.view.cts;
     18 
     19 import com.android.cts.stub.R;
     20 import com.android.internal.view.menu.ContextMenuBuilder;
     21 import com.google.android.collect.Lists;
     22 
     23 import android.app.Activity;
     24 import android.content.Context;
     25 import android.content.res.Resources;
     26 import android.content.res.XmlResourceParser;
     27 import android.cts.util.PollingCheck;
     28 import android.graphics.Bitmap;
     29 import android.graphics.Point;
     30 import android.graphics.Rect;
     31 import android.graphics.drawable.BitmapDrawable;
     32 import android.graphics.drawable.ColorDrawable;
     33 import android.graphics.drawable.Drawable;
     34 import android.graphics.drawable.StateListDrawable;
     35 import android.os.Parcelable;
     36 import android.os.SystemClock;
     37 import android.os.Vibrator;
     38 import android.test.ActivityInstrumentationTestCase2;
     39 import android.test.TouchUtils;
     40 import android.test.UiThreadTest;
     41 import android.util.AttributeSet;
     42 import android.util.Log;
     43 import android.util.SparseArray;
     44 import android.util.Xml;
     45 import android.view.ActionMode;
     46 import android.view.ContextMenu;
     47 import android.view.ContextMenu.ContextMenuInfo;
     48 import android.view.Display;
     49 import android.view.HapticFeedbackConstants;
     50 import android.view.KeyEvent;
     51 import android.view.MotionEvent;
     52 import android.view.SoundEffectConstants;
     53 import android.view.TouchDelegate;
     54 import android.view.View;
     55 import android.view.View.BaseSavedState;
     56 import android.view.View.OnClickListener;
     57 import android.view.View.OnCreateContextMenuListener;
     58 import android.view.View.OnFocusChangeListener;
     59 import android.view.View.OnKeyListener;
     60 import android.view.View.OnLongClickListener;
     61 import android.view.View.OnTouchListener;
     62 import android.view.ViewConfiguration;
     63 import android.view.ViewGroup;
     64 import android.view.ViewParent;
     65 import android.view.WindowManager;
     66 import android.view.accessibility.AccessibilityEvent;
     67 import android.view.animation.AlphaAnimation;
     68 import android.view.animation.Animation;
     69 import android.view.inputmethod.EditorInfo;
     70 import android.view.inputmethod.InputConnection;
     71 import android.view.inputmethod.InputMethodManager;
     72 import android.widget.ArrayAdapter;
     73 import android.widget.Button;
     74 import android.widget.EditText;
     75 import android.widget.LinearLayout;
     76 import android.widget.ListView;
     77 import android.widget.cts.StubActivity;
     78 
     79 import java.util.ArrayList;
     80 import java.util.Arrays;
     81 import java.util.List;
     82 
     83 /**
     84  * Test {@link View}.
     85  */
     86 public class ViewTest extends ActivityInstrumentationTestCase2<ViewTestStubActivity> {
     87     public ViewTest() {
     88         super(ViewTestStubActivity.class);
     89     }
     90 
     91     private Resources mResources;
     92     private MockViewParent mMockParent;
     93     private Activity mActivity;
     94 
     95     /** timeout delta when wait in case the system is sluggish */
     96     private static final long TIMEOUT_DELTA = 10000;
     97 
     98     private static final String LOG_TAG = "ViewTest";
     99 
    100     @Override
    101     protected void setUp() throws Exception {
    102         super.setUp();
    103         mActivity = getActivity();
    104         mResources = mActivity.getResources();
    105         mMockParent = new MockViewParent(mActivity);
    106     }
    107 
    108     public void testConstructor() {
    109         new View(mActivity);
    110 
    111         final XmlResourceParser parser = mResources.getLayout(R.layout.view_layout);
    112         final AttributeSet attrs = Xml.asAttributeSet(parser);
    113         new View(mActivity, attrs);
    114 
    115         new View(mActivity, null);
    116 
    117         try {
    118             new View(null, attrs);
    119             fail("should throw NullPointerException");
    120         } catch (NullPointerException e) {
    121         }
    122 
    123         new View(mActivity, attrs, 0);
    124 
    125         new View(mActivity, null, 1);
    126 
    127         try {
    128             new View(null, null, 1);
    129             fail("should throw NullPointerException");
    130         } catch (NullPointerException e) {
    131         }
    132     }
    133 
    134     public void testGetContext() {
    135         View view = new View(mActivity);
    136         assertSame(mActivity, view.getContext());
    137     }
    138 
    139     public void testGetResources() {
    140         View view = new View(mActivity);
    141         assertSame(mResources, view.getResources());
    142     }
    143 
    144     public void testGetAnimation() {
    145         Animation animation = new AlphaAnimation(0.0f, 1.0f);
    146         View view = new View(mActivity);
    147         assertNull(view.getAnimation());
    148 
    149         view.setAnimation(animation);
    150         assertSame(animation, view.getAnimation());
    151 
    152         view.clearAnimation();
    153         assertNull(view.getAnimation());
    154     }
    155 
    156     public void testSetAnimation() {
    157         Animation animation = new AlphaAnimation(0.0f, 1.0f);
    158         View view = new View(mActivity);
    159         assertNull(view.getAnimation());
    160 
    161         animation.initialize(100, 100, 100, 100);
    162         assertTrue(animation.isInitialized());
    163         view.setAnimation(animation);
    164         assertSame(animation, view.getAnimation());
    165         assertFalse(animation.isInitialized());
    166 
    167         view.setAnimation(null);
    168         assertNull(view.getAnimation());
    169     }
    170 
    171     public void testClearAnimation() {
    172         Animation animation = new AlphaAnimation(0.0f, 1.0f);
    173         View view = new View(mActivity);
    174 
    175         assertNull(view.getAnimation());
    176         view.clearAnimation();
    177         assertNull(view.getAnimation());
    178 
    179         view.setAnimation(animation);
    180         assertNotNull(view.getAnimation());
    181         view.clearAnimation();
    182         assertNull(view.getAnimation());
    183     }
    184 
    185     public void testStartAnimation() {
    186         Animation animation = new AlphaAnimation(0.0f, 1.0f);
    187         View view = new View(mActivity);
    188 
    189         try {
    190             view.startAnimation(null);
    191             fail("should throw NullPointerException");
    192         } catch (NullPointerException e) {
    193         }
    194 
    195         animation.setStartTime(1L);
    196         assertEquals(1L, animation.getStartTime());
    197         view.startAnimation(animation);
    198         assertEquals(Animation.START_ON_FIRST_FRAME, animation.getStartTime());
    199     }
    200 
    201     public void testOnAnimation() throws Throwable {
    202         final Animation animation = new AlphaAnimation(0.0f, 1.0f);
    203         long duration = 2000L;
    204         animation.setDuration(duration);
    205         final MockView view = (MockView) mActivity.findViewById(R.id.mock_view);
    206 
    207         // check whether it has started
    208         runTestOnUiThread(new Runnable() {
    209             public void run() {
    210                 view.startAnimation(animation);
    211             }
    212         });
    213         getInstrumentation().waitForIdleSync();
    214 
    215         new PollingCheck() {
    216             @Override
    217             protected boolean check() {
    218                 return view.hasCalledOnAnimationStart();
    219             }
    220         }.run();
    221 
    222         // check whether it has ended after duration, and alpha changed during this time.
    223         new PollingCheck(duration + TIMEOUT_DELTA) {
    224             @Override
    225             protected boolean check() {
    226                 return view.hasCalledOnSetAlpha() && view.hasCalledOnAnimationEnd();
    227             }
    228         }.run();
    229     }
    230 
    231     public void testGetParent() {
    232         MockView view = (MockView) mActivity.findViewById(R.id.mock_view);
    233         ViewGroup parent = (ViewGroup) mActivity.findViewById(R.id.viewlayout_root);
    234         assertSame(parent, view.getParent());
    235     }
    236 
    237     public void testFindViewById() {
    238         View parent = mActivity.findViewById(R.id.viewlayout_root);
    239         assertSame(parent, parent.findViewById(R.id.viewlayout_root));
    240 
    241         View view = parent.findViewById(R.id.mock_view);
    242         assertTrue(view instanceof MockView);
    243     }
    244 
    245     public void testAccessTouchDelegate() throws Throwable {
    246         final MockView view = (MockView) mActivity.findViewById(R.id.mock_view);
    247         Rect rect = new Rect();
    248         final Button button = new Button(mActivity);
    249         final int WRAP_CONTENT = ViewGroup.LayoutParams.WRAP_CONTENT;
    250         runTestOnUiThread(new Runnable() {
    251             public void run() {
    252                 mActivity.addContentView(button,
    253                         new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
    254             }
    255         });
    256         getInstrumentation().waitForIdleSync();
    257         button.getHitRect(rect);
    258         MockTouchDelegate delegate = new MockTouchDelegate(rect, button);
    259 
    260         assertNull(view.getTouchDelegate());
    261 
    262         view.setTouchDelegate(delegate);
    263         assertSame(delegate, view.getTouchDelegate());
    264         assertFalse(delegate.hasCalledOnTouchEvent());
    265         TouchUtils.clickView(this, view);
    266         assertTrue(view.hasCalledOnTouchEvent());
    267         assertTrue(delegate.hasCalledOnTouchEvent());
    268 
    269         view.setTouchDelegate(null);
    270         assertNull(view.getTouchDelegate());
    271     }
    272 
    273     @UiThreadTest
    274     public void testAccessTag() {
    275         ViewGroup viewGroup = (ViewGroup) mActivity.findViewById(R.id.viewlayout_root);
    276         MockView mockView = (MockView) mActivity.findViewById(R.id.mock_view);
    277         MockView scrollView = (MockView) mActivity.findViewById(R.id.scroll_view);
    278 
    279         ViewData viewData = new ViewData();
    280         viewData.childCount = 3;
    281         viewData.tag = "linearLayout";
    282         viewData.firstChild = mockView;
    283         viewGroup.setTag(viewData);
    284         viewGroup.setFocusable(true);
    285         assertSame(viewData, viewGroup.getTag());
    286 
    287         final String tag = "mock";
    288         assertNull(mockView.getTag());
    289         mockView.setTag(tag);
    290         assertEquals(tag, mockView.getTag());
    291 
    292         scrollView.setTag(viewGroup);
    293         assertSame(viewGroup, scrollView.getTag());
    294 
    295         assertSame(viewGroup, viewGroup.findViewWithTag(viewData));
    296         assertSame(mockView, viewGroup.findViewWithTag(tag));
    297         assertSame(scrollView, viewGroup.findViewWithTag(viewGroup));
    298 
    299         mockView.setTag(null);
    300         assertNull(mockView.getTag());
    301     }
    302 
    303     public void testOnSizeChanged() throws Throwable {
    304         final ViewGroup viewGroup = (ViewGroup) mActivity.findViewById(R.id.viewlayout_root);
    305         final MockView mockView = new MockView(mActivity);
    306         assertEquals(-1, mockView.getOldWOnSizeChanged());
    307         assertEquals(-1, mockView.getOldHOnSizeChanged());
    308         runTestOnUiThread(new Runnable() {
    309             public void run() {
    310                 viewGroup.addView(mockView);
    311             }
    312         });
    313         getInstrumentation().waitForIdleSync();
    314         assertTrue(mockView.hasCalledOnSizeChanged());
    315         assertEquals(0, mockView.getOldWOnSizeChanged());
    316         assertEquals(0, mockView.getOldHOnSizeChanged());
    317 
    318         final MockView view = (MockView) mActivity.findViewById(R.id.mock_view);
    319         assertTrue(view.hasCalledOnSizeChanged());
    320         view.reset();
    321         assertEquals(-1, view.getOldWOnSizeChanged());
    322         assertEquals(-1, view.getOldHOnSizeChanged());
    323         int oldw = view.getWidth();
    324         int oldh = view.getHeight();
    325         final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(200, 100);
    326         runTestOnUiThread(new Runnable() {
    327             public void run() {
    328                 view.setLayoutParams(layoutParams);
    329             }
    330         });
    331         getInstrumentation().waitForIdleSync();
    332         assertTrue(view.hasCalledOnSizeChanged());
    333         assertEquals(oldw, view.getOldWOnSizeChanged());
    334         assertEquals(oldh, view.getOldHOnSizeChanged());
    335     }
    336 
    337     public void testGetHitRect() {
    338         MockView view = new MockView(mActivity);
    339         Rect outRect = new Rect();
    340 
    341         try {
    342             view.getHitRect(null);
    343             fail("should throw NullPointerException");
    344         } catch (NullPointerException e) {
    345         }
    346 
    347         View mockView = mActivity.findViewById(R.id.mock_view);
    348         mockView.getHitRect(outRect);
    349         assertEquals(0, outRect.left);
    350         assertEquals(0, outRect.top);
    351         assertEquals(mockView.getWidth(), outRect.right);
    352         assertEquals(mockView.getHeight(), outRect.bottom);
    353     }
    354 
    355     public void testForceLayout() {
    356         View view = new View(mActivity);
    357 
    358         assertFalse(view.isLayoutRequested());
    359         view.forceLayout();
    360         assertTrue(view.isLayoutRequested());
    361 
    362         view.forceLayout();
    363         assertTrue(view.isLayoutRequested());
    364     }
    365 
    366     public void testIsLayoutRequested() {
    367         View view = new View(mActivity);
    368 
    369         assertFalse(view.isLayoutRequested());
    370         view.forceLayout();
    371         assertTrue(view.isLayoutRequested());
    372 
    373         view.layout(0, 0, 0, 0);
    374         assertFalse(view.isLayoutRequested());
    375     }
    376 
    377     public void testRequestLayout() {
    378         MockView view = new MockView(mActivity);
    379         assertFalse(view.isLayoutRequested());
    380         assertNull(view.getParent());
    381 
    382         view.requestLayout();
    383         assertTrue(view.isLayoutRequested());
    384 
    385         view.setParent(mMockParent);
    386         assertFalse(mMockParent.hasRequestLayout());
    387         view.requestLayout();
    388         assertTrue(view.isLayoutRequested());
    389         assertTrue(mMockParent.hasRequestLayout());
    390     }
    391 
    392     public void testLayout() throws Throwable {
    393         final MockView view = (MockView) mActivity.findViewById(R.id.mock_view);
    394         assertTrue(view.hasCalledOnLayout());
    395 
    396         view.reset();
    397         assertFalse(view.hasCalledOnLayout());
    398         runTestOnUiThread(new Runnable() {
    399             public void run() {
    400                 view.requestLayout();
    401             }
    402         });
    403         getInstrumentation().waitForIdleSync();
    404         assertTrue(view.hasCalledOnLayout());
    405     }
    406 
    407     public void testGetBaseline() {
    408         View view = new View(mActivity);
    409 
    410         assertEquals(-1, view.getBaseline());
    411     }
    412 
    413     public void testAccessBackground() {
    414         View view = new View(mActivity);
    415         Drawable d1 = mResources.getDrawable(R.drawable.scenery);
    416         Drawable d2 = mResources.getDrawable(R.drawable.pass);
    417 
    418         assertNull(view.getBackground());
    419 
    420         view.setBackgroundDrawable(d1);
    421         assertEquals(d1, view.getBackground());
    422 
    423         view.setBackgroundDrawable(d2);
    424         assertEquals(d2, view.getBackground());
    425 
    426         view.setBackgroundDrawable(null);
    427         assertNull(view.getBackground());
    428     }
    429 
    430     public void testSetBackgroundResource() {
    431         View view = new View(mActivity);
    432 
    433         assertNull(view.getBackground());
    434 
    435         view.setBackgroundResource(R.drawable.pass);
    436         assertNotNull(view.getBackground());
    437 
    438         view.setBackgroundResource(0);
    439         assertNull(view.getBackground());
    440     }
    441 
    442     public void testAccessDrawingCacheBackgroundColor() {
    443         View view = new View(mActivity);
    444 
    445         assertEquals(0, view.getDrawingCacheBackgroundColor());
    446 
    447         view.setDrawingCacheBackgroundColor(0xFF00FF00);
    448         assertEquals(0xFF00FF00, view.getDrawingCacheBackgroundColor());
    449 
    450         view.setDrawingCacheBackgroundColor(-1);
    451         assertEquals(-1, view.getDrawingCacheBackgroundColor());
    452     }
    453 
    454     public void testSetBackgroundColor() {
    455         View view = new View(mActivity);
    456         ColorDrawable colorDrawable;
    457         assertNull(view.getBackground());
    458 
    459         view.setBackgroundColor(0xFFFF0000);
    460         colorDrawable = (ColorDrawable) view.getBackground();
    461         assertNotNull(colorDrawable);
    462         assertEquals(0xFF, colorDrawable.getAlpha());
    463 
    464         view.setBackgroundColor(0);
    465         colorDrawable = (ColorDrawable) view.getBackground();
    466         assertNotNull(colorDrawable);
    467         assertEquals(0, colorDrawable.getAlpha());
    468     }
    469 
    470     public void testVerifyDrawable() {
    471         MockView view = new MockView(mActivity);
    472         Drawable d1 = mResources.getDrawable(R.drawable.scenery);
    473         Drawable d2 = mResources.getDrawable(R.drawable.pass);
    474 
    475         assertNull(view.getBackground());
    476         assertTrue(view.verifyDrawable(null));
    477         assertFalse(view.verifyDrawable(d1));
    478 
    479         view.setBackgroundDrawable(d1);
    480         assertTrue(view.verifyDrawable(d1));
    481         assertFalse(view.verifyDrawable(d2));
    482     }
    483 
    484     public void testGetDrawingRect() {
    485         MockView view = new MockView(mActivity);
    486         Rect outRect = new Rect();
    487 
    488         view.getDrawingRect(outRect);
    489         assertEquals(0, outRect.left);
    490         assertEquals(0, outRect.top);
    491         assertEquals(0, outRect.right);
    492         assertEquals(0, outRect.bottom);
    493 
    494         view.scrollTo(10, 100);
    495         view.getDrawingRect(outRect);
    496         assertEquals(10, outRect.left);
    497         assertEquals(100, outRect.top);
    498         assertEquals(10, outRect.right);
    499         assertEquals(100, outRect.bottom);
    500 
    501         View mockView = mActivity.findViewById(R.id.mock_view);
    502         mockView.getDrawingRect(outRect);
    503         assertEquals(0, outRect.left);
    504         assertEquals(0, outRect.top);
    505         assertEquals(mockView.getWidth(), outRect.right);
    506         assertEquals(mockView.getHeight(), outRect.bottom);
    507     }
    508 
    509     public void testGetFocusedRect() {
    510         MockView view = new MockView(mActivity);
    511         Rect outRect = new Rect();
    512 
    513         view.getFocusedRect(outRect);
    514         assertEquals(0, outRect.left);
    515         assertEquals(0, outRect.top);
    516         assertEquals(0, outRect.right);
    517         assertEquals(0, outRect.bottom);
    518 
    519         view.scrollTo(10, 100);
    520         view.getFocusedRect(outRect);
    521         assertEquals(10, outRect.left);
    522         assertEquals(100, outRect.top);
    523         assertEquals(10, outRect.right);
    524         assertEquals(100, outRect.bottom);
    525     }
    526 
    527     public void testGetGlobalVisibleRectPoint() throws Throwable {
    528         final View view = mActivity.findViewById(R.id.mock_view);
    529         final ViewGroup viewGroup = (ViewGroup) mActivity.findViewById(R.id.viewlayout_root);
    530         Rect rect = new Rect();
    531         Point point = new Point();
    532 
    533         assertTrue(view.getGlobalVisibleRect(rect, point));
    534         Rect rcParent = new Rect();
    535         Point ptParent = new Point();
    536         viewGroup.getGlobalVisibleRect(rcParent, ptParent);
    537         assertEquals(rcParent.left, rect.left);
    538         assertEquals(rcParent.top, rect.top);
    539         assertEquals(rect.left + view.getWidth(), rect.right);
    540         assertEquals(rect.top + view.getHeight(), rect.bottom);
    541         assertEquals(ptParent.x, point.x);
    542         assertEquals(ptParent.y, point.y);
    543 
    544         // width is 0
    545         final LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(0, 300);
    546         runTestOnUiThread(new Runnable() {
    547             public void run() {
    548                 view.setLayoutParams(layoutParams1);
    549             }
    550         });
    551         getInstrumentation().waitForIdleSync();
    552         assertFalse(view.getGlobalVisibleRect(rect, point));
    553 
    554         // height is -10
    555         final LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(200, -10);
    556         runTestOnUiThread(new Runnable() {
    557             public void run() {
    558                 view.setLayoutParams(layoutParams2);
    559             }
    560         });
    561         getInstrumentation().waitForIdleSync();
    562         assertFalse(view.getGlobalVisibleRect(rect, point));
    563 
    564         Display display = getActivity().getWindowManager().getDefaultDisplay();
    565         int halfWidth = display.getWidth() / 2;
    566         int halfHeight = display.getHeight() /2;
    567 
    568         final LinearLayout.LayoutParams layoutParams3 =
    569                 new LinearLayout.LayoutParams(halfWidth, halfHeight);
    570         runTestOnUiThread(new Runnable() {
    571             public void run() {
    572                 view.setLayoutParams(layoutParams3);
    573             }
    574         });
    575         getInstrumentation().waitForIdleSync();
    576         assertTrue(view.getGlobalVisibleRect(rect, point));
    577         assertEquals(rcParent.left, rect.left);
    578         assertEquals(rcParent.top, rect.top);
    579         assertEquals(rect.left + halfWidth, rect.right);
    580         assertEquals(rect.top + halfHeight, rect.bottom);
    581         assertEquals(ptParent.x, point.x);
    582         assertEquals(ptParent.y, point.y);
    583     }
    584 
    585     public void testGetGlobalVisibleRect() throws Throwable {
    586         final View view = mActivity.findViewById(R.id.mock_view);
    587         final ViewGroup viewGroup = (ViewGroup) mActivity.findViewById(R.id.viewlayout_root);
    588         Rect rect = new Rect();
    589 
    590         assertTrue(view.getGlobalVisibleRect(rect));
    591         Rect rcParent = new Rect();
    592         viewGroup.getGlobalVisibleRect(rcParent);
    593         assertEquals(rcParent.left, rect.left);
    594         assertEquals(rcParent.top, rect.top);
    595         assertEquals(rect.left + view.getWidth(), rect.right);
    596         assertEquals(rect.top + view.getHeight(), rect.bottom);
    597 
    598         // width is 0
    599         final LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(0, 300);
    600         runTestOnUiThread(new Runnable() {
    601             public void run() {
    602                 view.setLayoutParams(layoutParams1);
    603             }
    604         });
    605         getInstrumentation().waitForIdleSync();
    606         assertFalse(view.getGlobalVisibleRect(rect));
    607 
    608         // height is -10
    609         final LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(200, -10);
    610         runTestOnUiThread(new Runnable() {
    611             public void run() {
    612                 view.setLayoutParams(layoutParams2);
    613             }
    614         });
    615         getInstrumentation().waitForIdleSync();
    616         assertFalse(view.getGlobalVisibleRect(rect));
    617 
    618         Display display = getActivity().getWindowManager().getDefaultDisplay();
    619         int halfWidth = display.getWidth() / 2;
    620         int halfHeight = display.getHeight() /2;
    621 
    622         final LinearLayout.LayoutParams layoutParams3 =
    623                 new LinearLayout.LayoutParams(halfWidth, halfHeight);
    624         runTestOnUiThread(new Runnable() {
    625             public void run() {
    626                 view.setLayoutParams(layoutParams3);
    627             }
    628         });
    629         getInstrumentation().waitForIdleSync();
    630         assertTrue(view.getGlobalVisibleRect(rect));
    631         assertEquals(rcParent.left, rect.left);
    632         assertEquals(rcParent.top, rect.top);
    633         assertEquals(rect.left + halfWidth, rect.right);
    634         assertEquals(rect.top + halfHeight, rect.bottom);
    635     }
    636 
    637     public void testComputeHorizontalScroll() throws Throwable {
    638         final MockView view = (MockView) mActivity.findViewById(R.id.mock_view);
    639 
    640         assertEquals(0, view.computeHorizontalScrollOffset());
    641         assertEquals(view.getWidth(), view.computeHorizontalScrollRange());
    642         assertEquals(view.getWidth(), view.computeHorizontalScrollExtent());
    643 
    644         runTestOnUiThread(new Runnable() {
    645             public void run() {
    646                 view.scrollTo(12, 0);
    647             }
    648         });
    649         getInstrumentation().waitForIdleSync();
    650         assertEquals(12, view.computeHorizontalScrollOffset());
    651         assertEquals(view.getWidth(), view.computeHorizontalScrollRange());
    652         assertEquals(view.getWidth(), view.computeHorizontalScrollExtent());
    653 
    654         runTestOnUiThread(new Runnable() {
    655             public void run() {
    656                 view.scrollBy(12, 0);
    657             }
    658         });
    659         getInstrumentation().waitForIdleSync();
    660         assertEquals(24, view.computeHorizontalScrollOffset());
    661         assertEquals(view.getWidth(), view.computeHorizontalScrollRange());
    662         assertEquals(view.getWidth(), view.computeHorizontalScrollExtent());
    663 
    664         int newWidth = 200;
    665         final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(newWidth, 100);
    666         runTestOnUiThread(new Runnable() {
    667             public void run() {
    668                 view.setLayoutParams(layoutParams);
    669             }
    670         });
    671         getInstrumentation().waitForIdleSync();
    672         assertEquals(24, view.computeHorizontalScrollOffset());
    673         assertEquals(newWidth, view.getWidth());
    674         assertEquals(view.getWidth(), view.computeHorizontalScrollRange());
    675         assertEquals(view.getWidth(), view.computeHorizontalScrollExtent());
    676     }
    677 
    678     public void testComputeVerticalScroll() throws Throwable {
    679         final MockView view = (MockView) mActivity.findViewById(R.id.mock_view);
    680 
    681         assertEquals(0, view.computeVerticalScrollOffset());
    682         assertEquals(view.getHeight(), view.computeVerticalScrollRange());
    683         assertEquals(view.getHeight(), view.computeVerticalScrollExtent());
    684 
    685         final int scrollToY = 34;
    686         runTestOnUiThread(new Runnable() {
    687             public void run() {
    688                 view.scrollTo(0, scrollToY);
    689             }
    690         });
    691         getInstrumentation().waitForIdleSync();
    692         assertEquals(scrollToY, view.computeVerticalScrollOffset());
    693         assertEquals(view.getHeight(), view.computeVerticalScrollRange());
    694         assertEquals(view.getHeight(), view.computeVerticalScrollExtent());
    695 
    696         final int scrollByY = 200;
    697         runTestOnUiThread(new Runnable() {
    698             public void run() {
    699                 view.scrollBy(0, scrollByY);
    700             }
    701         });
    702         getInstrumentation().waitForIdleSync();
    703         assertEquals(scrollToY + scrollByY, view.computeVerticalScrollOffset());
    704         assertEquals(view.getHeight(), view.computeVerticalScrollRange());
    705         assertEquals(view.getHeight(), view.computeVerticalScrollExtent());
    706 
    707         int newHeight = 333;
    708         final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(200, newHeight);
    709         runTestOnUiThread(new Runnable() {
    710             public void run() {
    711                 view.setLayoutParams(layoutParams);
    712             }
    713         });
    714         getInstrumentation().waitForIdleSync();
    715         assertEquals(scrollToY + scrollByY, view.computeVerticalScrollOffset());
    716         assertEquals(newHeight, view.getHeight());
    717         assertEquals(view.getHeight(), view.computeVerticalScrollRange());
    718         assertEquals(view.getHeight(), view.computeVerticalScrollExtent());
    719     }
    720 
    721     public void testGetFadingEdgeStrength() throws Throwable {
    722         final MockView view = (MockView) mActivity.findViewById(R.id.mock_view);
    723 
    724         assertEquals(0f, view.getLeftFadingEdgeStrength());
    725         assertEquals(0f, view.getRightFadingEdgeStrength());
    726         assertEquals(0f, view.getTopFadingEdgeStrength());
    727         assertEquals(0f, view.getBottomFadingEdgeStrength());
    728 
    729         runTestOnUiThread(new Runnable() {
    730             public void run() {
    731                 view.scrollTo(10, 10);
    732             }
    733         });
    734         getInstrumentation().waitForIdleSync();
    735         assertEquals(1f, view.getLeftFadingEdgeStrength());
    736         assertEquals(0f, view.getRightFadingEdgeStrength());
    737         assertEquals(1f, view.getTopFadingEdgeStrength());
    738         assertEquals(0f, view.getBottomFadingEdgeStrength());
    739 
    740         runTestOnUiThread(new Runnable() {
    741             public void run() {
    742                 view.scrollTo(-10, -10);
    743             }
    744         });
    745         getInstrumentation().waitForIdleSync();
    746         assertEquals(0f, view.getLeftFadingEdgeStrength());
    747         assertEquals(1f, view.getRightFadingEdgeStrength());
    748         assertEquals(0f, view.getTopFadingEdgeStrength());
    749         assertEquals(1f, view.getBottomFadingEdgeStrength());
    750     }
    751 
    752     public void testGetLeftFadingEdgeStrength() {
    753         MockView view = new MockView(mActivity);
    754 
    755         assertEquals(0.0f, view.getLeftFadingEdgeStrength());
    756 
    757         view.scrollTo(1, 0);
    758         assertEquals(1.0f, view.getLeftFadingEdgeStrength());
    759     }
    760 
    761     public void testGetRightFadingEdgeStrength() {
    762         MockView view = new MockView(mActivity);
    763 
    764         assertEquals(0.0f, view.getRightFadingEdgeStrength());
    765 
    766         view.scrollTo(-1, 0);
    767         assertEquals(1.0f, view.getRightFadingEdgeStrength());
    768     }
    769 
    770     public void testGetBottomFadingEdgeStrength() {
    771         MockView view = new MockView(mActivity);
    772 
    773         assertEquals(0.0f, view.getBottomFadingEdgeStrength());
    774 
    775         view.scrollTo(0, -2);
    776         assertEquals(1.0f, view.getBottomFadingEdgeStrength());
    777     }
    778 
    779     public void testGetTopFadingEdgeStrength() {
    780         MockView view = new MockView(mActivity);
    781 
    782         assertEquals(0.0f, view.getTopFadingEdgeStrength());
    783 
    784         view.scrollTo(0, 2);
    785         assertEquals(1.0f, view.getTopFadingEdgeStrength());
    786     }
    787 
    788     public void testResolveSize() {
    789         assertEquals(50, View.resolveSize(50, View.MeasureSpec.UNSPECIFIED));
    790 
    791         assertEquals(40, View.resolveSize(50, 40 | View.MeasureSpec.EXACTLY));
    792 
    793         assertEquals(30, View.resolveSize(50, 30 | View.MeasureSpec.AT_MOST));
    794 
    795         assertEquals(20, View.resolveSize(20, 30 | View.MeasureSpec.AT_MOST));
    796     }
    797 
    798     public void testGetDefaultSize() {
    799         assertEquals(50, View.getDefaultSize(50, View.MeasureSpec.UNSPECIFIED));
    800 
    801         assertEquals(40, View.getDefaultSize(50, 40 | View.MeasureSpec.EXACTLY));
    802 
    803         assertEquals(30, View.getDefaultSize(50, 30 | View.MeasureSpec.AT_MOST));
    804 
    805         assertEquals(30, View.getDefaultSize(20, 30 | View.MeasureSpec.AT_MOST));
    806     }
    807 
    808     public void testAccessId() {
    809         View view = new View(mActivity);
    810 
    811         assertEquals(View.NO_ID, view.getId());
    812 
    813         view.setId(10);
    814         assertEquals(10, view.getId());
    815 
    816         view.setId(0xFFFFFFFF);
    817         assertEquals(0xFFFFFFFF, view.getId());
    818     }
    819 
    820     public void testAccessLongClickable() {
    821         View view = new View(mActivity);
    822 
    823         assertFalse(view.isLongClickable());
    824 
    825         view.setLongClickable(true);
    826         assertTrue(view.isLongClickable());
    827 
    828         view.setLongClickable(false);
    829         assertFalse(view.isLongClickable());
    830     }
    831 
    832     public void testAccessClickable() {
    833         View view = new View(mActivity);
    834 
    835         assertFalse(view.isClickable());
    836 
    837         view.setClickable(true);
    838         assertTrue(view.isClickable());
    839 
    840         view.setClickable(false);
    841         assertFalse(view.isClickable());
    842     }
    843 
    844     public void testGetContextMenuInfo() {
    845         MockView view = new MockView(mActivity);
    846 
    847         assertNull(view.getContextMenuInfo());
    848     }
    849 
    850     public void testSetOnCreateContextMenuListener() {
    851         View view = new View(mActivity);
    852         assertFalse(view.isLongClickable());
    853 
    854         view.setOnCreateContextMenuListener(null);
    855         assertTrue(view.isLongClickable());
    856 
    857         view.setOnCreateContextMenuListener(new OnCreateContextMenuListenerImpl());
    858         assertTrue(view.isLongClickable());
    859     }
    860 
    861     public void testCreateContextMenu() {
    862         OnCreateContextMenuListenerImpl listener = new OnCreateContextMenuListenerImpl();
    863         MockView view = new MockView(mActivity);
    864         ContextMenu contextMenu = new ContextMenuBuilder(mActivity);
    865         view.setParent(mMockParent);
    866         view.setOnCreateContextMenuListener(listener);
    867         assertFalse(view.hasCalledOnCreateContextMenu());
    868         assertFalse(mMockParent.hasCreateContextMenu());
    869         assertFalse(listener.hasOnCreateContextMenu());
    870 
    871         view.createContextMenu(contextMenu);
    872         assertTrue(view.hasCalledOnCreateContextMenu());
    873         assertTrue(mMockParent.hasCreateContextMenu());
    874         assertTrue(listener.hasOnCreateContextMenu());
    875 
    876         try {
    877             view.createContextMenu(null);
    878             fail("should throw NullPointerException");
    879         } catch (NullPointerException e) {
    880         }
    881     }
    882 
    883     public void testAddFocusables() {
    884         View view = new View(mActivity);
    885         ArrayList<View> viewList = new ArrayList<View>();
    886 
    887         // view is not focusable
    888         assertFalse(view.isFocusable());
    889         assertEquals(0, viewList.size());
    890         view.addFocusables(viewList, 0);
    891         assertEquals(0, viewList.size());
    892 
    893         // view is focusable
    894         view.setFocusable(true);
    895         view.addFocusables(viewList, 0);
    896         assertEquals(1, viewList.size());
    897         assertEquals(view, viewList.get(0));
    898 
    899         // null array should be ignored
    900         view.addFocusables(null, 0);
    901     }
    902 
    903     public void testGetFocusables() {
    904         View view = new View(mActivity);
    905         ArrayList<View> viewList;
    906 
    907         // view is not focusable
    908         assertFalse(view.isFocusable());
    909         viewList = view.getFocusables(0);
    910         assertEquals(0, viewList.size());
    911 
    912         // view is focusable
    913         view.setFocusable(true);
    914         viewList = view.getFocusables(0);
    915         assertEquals(1, viewList.size());
    916         assertEquals(view, viewList.get(0));
    917 
    918         viewList = view.getFocusables(-1);
    919         assertEquals(1, viewList.size());
    920         assertEquals(view, viewList.get(0));
    921     }
    922 
    923     public void testGetRootView() {
    924         MockView view = new MockView(mActivity);
    925 
    926         assertNull(view.getParent());
    927         assertEquals(view, view.getRootView());
    928 
    929         view.setParent(mMockParent);
    930         assertEquals(mMockParent, view.getRootView());
    931     }
    932 
    933     public void testGetSolidColor() {
    934         View view = new View(mActivity);
    935 
    936         assertEquals(0, view.getSolidColor());
    937     }
    938 
    939     public void testSetMinimumWidth() {
    940         MockView view = new MockView(mActivity);
    941         assertEquals(0, view.getSuggestedMinimumWidth());
    942 
    943         view.setMinimumWidth(100);
    944         assertEquals(100, view.getSuggestedMinimumWidth());
    945 
    946         view.setMinimumWidth(-100);
    947         assertEquals(-100, view.getSuggestedMinimumWidth());
    948     }
    949 
    950     public void testGetSuggestedMinimumWidth() {
    951         MockView view = new MockView(mActivity);
    952         Drawable d = mResources.getDrawable(R.drawable.scenery);
    953         int drawableMinimumWidth = d.getMinimumWidth();
    954 
    955         // drawable is null
    956         view.setMinimumWidth(100);
    957         assertNull(view.getBackground());
    958         assertEquals(100, view.getSuggestedMinimumWidth());
    959 
    960         // drawable minimum width is larger than mMinWidth
    961         view.setBackgroundDrawable(d);
    962         view.setMinimumWidth(drawableMinimumWidth - 10);
    963         assertEquals(drawableMinimumWidth, view.getSuggestedMinimumWidth());
    964 
    965         // drawable minimum width is smaller than mMinWidth
    966         view.setMinimumWidth(drawableMinimumWidth + 10);
    967         assertEquals(drawableMinimumWidth + 10, view.getSuggestedMinimumWidth());
    968     }
    969 
    970     public void testSetMinimumHeight() {
    971         MockView view = new MockView(mActivity);
    972         assertEquals(0, view.getSuggestedMinimumHeight());
    973 
    974         view.setMinimumHeight(100);
    975         assertEquals(100, view.getSuggestedMinimumHeight());
    976 
    977         view.setMinimumHeight(-100);
    978         assertEquals(-100, view.getSuggestedMinimumHeight());
    979     }
    980 
    981     public void testGetSuggestedMinimumHeight() {
    982         MockView view = new MockView(mActivity);
    983         Drawable d = mResources.getDrawable(R.drawable.scenery);
    984         int drawableMinimumHeight = d.getMinimumHeight();
    985 
    986         // drawable is null
    987         view.setMinimumHeight(100);
    988         assertNull(view.getBackground());
    989         assertEquals(100, view.getSuggestedMinimumHeight());
    990 
    991         // drawable minimum height is larger than mMinHeight
    992         view.setBackgroundDrawable(d);
    993         view.setMinimumHeight(drawableMinimumHeight - 10);
    994         assertEquals(drawableMinimumHeight, view.getSuggestedMinimumHeight());
    995 
    996         // drawable minimum height is smaller than mMinHeight
    997         view.setMinimumHeight(drawableMinimumHeight + 10);
    998         assertEquals(drawableMinimumHeight + 10, view.getSuggestedMinimumHeight());
    999     }
   1000 
   1001     public void testAccessWillNotCacheDrawing() {
   1002         View view = new View(mActivity);
   1003 
   1004         assertFalse(view.willNotCacheDrawing());
   1005 
   1006         view.setWillNotCacheDrawing(true);
   1007         assertTrue(view.willNotCacheDrawing());
   1008     }
   1009 
   1010     public void testAccessDrawingCacheEnabled() {
   1011         View view = new View(mActivity);
   1012 
   1013         assertFalse(view.isDrawingCacheEnabled());
   1014 
   1015         view.setDrawingCacheEnabled(true);
   1016         assertTrue(view.isDrawingCacheEnabled());
   1017     }
   1018 
   1019     public void testGetDrawingCache() {
   1020         MockView view = new MockView(mActivity);
   1021 
   1022         // should not call buildDrawingCache when getDrawingCache
   1023         assertNull(view.getDrawingCache());
   1024 
   1025         // should call buildDrawingCache when getDrawingCache
   1026         view = (MockView) mActivity.findViewById(R.id.mock_view);
   1027         view.setDrawingCacheEnabled(true);
   1028         Bitmap bitmap1 = view.getDrawingCache();
   1029         assertNotNull(bitmap1);
   1030         assertEquals(view.getWidth(), bitmap1.getWidth());
   1031         assertEquals(view.getHeight(), bitmap1.getHeight());
   1032 
   1033         view.setWillNotCacheDrawing(true);
   1034         assertNull(view.getDrawingCache());
   1035 
   1036         view.setWillNotCacheDrawing(false);
   1037         // build a new drawingcache
   1038         Bitmap bitmap2 = view.getDrawingCache();
   1039         assertNotSame(bitmap1, bitmap2);
   1040     }
   1041 
   1042     public void testBuildAndDestroyDrawingCache() {
   1043         MockView view = (MockView) mActivity.findViewById(R.id.mock_view);
   1044 
   1045         assertNull(view.getDrawingCache());
   1046 
   1047         view.buildDrawingCache();
   1048         Bitmap bitmap = view.getDrawingCache();
   1049         assertNotNull(bitmap);
   1050         assertEquals(view.getWidth(), bitmap.getWidth());
   1051         assertEquals(view.getHeight(), bitmap.getHeight());
   1052 
   1053         view.destroyDrawingCache();
   1054         assertNull(view.getDrawingCache());
   1055     }
   1056 
   1057     public void testAccessWillNotDraw() {
   1058         View view = new View(mActivity);
   1059 
   1060         assertFalse(view.willNotDraw());
   1061 
   1062         view.setWillNotDraw(true);
   1063         assertTrue(view.willNotDraw());
   1064     }
   1065 
   1066     public void testAccessDrawingCacheQuality() {
   1067         View view = new View(mActivity);
   1068 
   1069         assertEquals(0, view.getDrawingCacheQuality());
   1070 
   1071         view.setDrawingCacheQuality(1);
   1072         assertEquals(0, view.getDrawingCacheQuality());
   1073 
   1074         view.setDrawingCacheQuality(0x00100000);
   1075         assertEquals(0x00100000, view.getDrawingCacheQuality());
   1076 
   1077         view.setDrawingCacheQuality(0x00080000);
   1078         assertEquals(0x00080000, view.getDrawingCacheQuality());
   1079 
   1080         view.setDrawingCacheQuality(0xffffffff);
   1081         // 0x00180000 is View.DRAWING_CACHE_QUALITY_MASK
   1082         assertEquals(0x00180000, view.getDrawingCacheQuality());
   1083     }
   1084 
   1085     public void testDispatchSetSelected() {
   1086         MockView mockView1 = new MockView(mActivity);
   1087         MockView mockView2 = new MockView(mActivity);
   1088         mockView1.setParent(mMockParent);
   1089         mockView2.setParent(mMockParent);
   1090 
   1091         mMockParent.dispatchSetSelected(true);
   1092         assertFalse(mockView1.isSelected());
   1093         assertFalse(mockView2.isSelected());
   1094 
   1095         mMockParent.dispatchSetSelected(false);
   1096         assertFalse(mockView1.isSelected());
   1097         assertFalse(mockView2.isSelected());
   1098     }
   1099 
   1100     public void testAccessSelected() {
   1101         View view = new View(mActivity);
   1102 
   1103         assertFalse(view.isSelected());
   1104 
   1105         view.setSelected(true);
   1106         assertTrue(view.isSelected());
   1107     }
   1108 
   1109     public void testDispatchSetPressed() {
   1110         MockView mockView1 = new MockView(mActivity);
   1111         MockView mockView2 = new MockView(mActivity);
   1112         mockView1.setParent(mMockParent);
   1113         mockView2.setParent(mMockParent);
   1114 
   1115         mMockParent.dispatchSetPressed(true);
   1116         assertFalse(mockView1.isPressed());
   1117         assertFalse(mockView2.isPressed());
   1118 
   1119         mMockParent.dispatchSetPressed(false);
   1120         assertFalse(mockView1.isPressed());
   1121         assertFalse(mockView2.isPressed());
   1122     }
   1123 
   1124     public void testAccessPressed() {
   1125         View view = new View(mActivity);
   1126 
   1127         assertFalse(view.isPressed());
   1128 
   1129         view.setPressed(true);
   1130         assertTrue(view.isPressed());
   1131     }
   1132 
   1133     public void testAccessSoundEffectsEnabled() {
   1134         View view = new View(mActivity);
   1135 
   1136         assertTrue(view.isSoundEffectsEnabled());
   1137 
   1138         view.setSoundEffectsEnabled(false);
   1139         assertFalse(view.isSoundEffectsEnabled());
   1140     }
   1141 
   1142     public void testAccessKeepScreenOn() {
   1143         View view = new View(mActivity);
   1144 
   1145         assertFalse(view.getKeepScreenOn());
   1146 
   1147         view.setKeepScreenOn(true);
   1148         assertTrue(view.getKeepScreenOn());
   1149     }
   1150 
   1151     public void testAccessDuplicateParentStateEnabled() {
   1152         View view = new View(mActivity);
   1153 
   1154         assertFalse(view.isDuplicateParentStateEnabled());
   1155 
   1156         view.setDuplicateParentStateEnabled(true);
   1157         assertTrue(view.isDuplicateParentStateEnabled());
   1158     }
   1159 
   1160     public void testAccessEnabled() {
   1161         View view = new View(mActivity);
   1162 
   1163         assertTrue(view.isEnabled());
   1164 
   1165         view.setEnabled(false);
   1166         assertFalse(view.isEnabled());
   1167     }
   1168 
   1169     public void testAccessSaveEnabled() {
   1170         View view = new View(mActivity);
   1171 
   1172         assertTrue(view.isSaveEnabled());
   1173 
   1174         view.setSaveEnabled(false);
   1175         assertFalse(view.isSaveEnabled());
   1176     }
   1177 
   1178     public void testShowContextMenu() {
   1179         MockView view = new MockView(mActivity);
   1180 
   1181         assertNull(view.getParent());
   1182         try {
   1183             view.showContextMenu();
   1184             fail("should throw NullPointerException");
   1185         } catch (NullPointerException e) {
   1186         }
   1187 
   1188         view.setParent(mMockParent);
   1189         assertFalse(mMockParent.hasShowContextMenuForChild());
   1190 
   1191         assertFalse(view.showContextMenu());
   1192         assertTrue(mMockParent.hasShowContextMenuForChild());
   1193     }
   1194 
   1195     public void testFitSystemWindows() {
   1196         final XmlResourceParser parser = mResources.getLayout(R.layout.view_layout);
   1197         final AttributeSet attrs = Xml.asAttributeSet(parser);
   1198         Rect insets = new Rect(10, 20, 30, 50);
   1199 
   1200         MockView view = new MockView(mActivity);
   1201         assertFalse(view.fitSystemWindows(insets));
   1202         assertFalse(view.fitSystemWindows(null));
   1203 
   1204         view = new MockView(mActivity, attrs, com.android.internal.R.attr.fitsSystemWindows);
   1205         assertFalse(view.fitSystemWindows(insets));
   1206         assertFalse(view.fitSystemWindows(null));
   1207     }
   1208 
   1209     public void testPerformClick() {
   1210         View view = new View(mActivity);
   1211         OnClickListenerImpl listener = new OnClickListenerImpl();
   1212 
   1213         assertFalse(view.performClick());
   1214 
   1215         assertFalse(listener.hasOnClick());
   1216         view.setOnClickListener(listener);
   1217 
   1218         assertTrue(view.performClick());
   1219         assertTrue(listener.hasOnClick());
   1220 
   1221         view.setOnClickListener(null);
   1222         assertFalse(view.performClick());
   1223     }
   1224 
   1225     public void testSetOnClickListener() {
   1226         View view = new View(mActivity);
   1227         assertFalse(view.performClick());
   1228         assertFalse(view.isClickable());
   1229 
   1230         view.setOnClickListener(null);
   1231         assertFalse(view.performClick());
   1232         assertTrue(view.isClickable());
   1233 
   1234         view.setOnClickListener(new OnClickListenerImpl());
   1235         assertTrue(view.performClick());
   1236         assertTrue(view.isClickable());
   1237     }
   1238 
   1239     public void testPerformLongClick() {
   1240         MockView view = new MockView(mActivity);
   1241         OnLongClickListenerImpl listener = new OnLongClickListenerImpl();
   1242 
   1243         try {
   1244             view.performLongClick();
   1245             fail("should throw NullPointerException");
   1246         } catch (NullPointerException e) {
   1247         }
   1248 
   1249         view.setParent(mMockParent);
   1250         assertFalse(mMockParent.hasShowContextMenuForChild());
   1251         assertFalse(view.performLongClick());
   1252         assertTrue(mMockParent.hasShowContextMenuForChild());
   1253 
   1254         view.setOnLongClickListener(listener);
   1255         mMockParent.reset();
   1256         assertFalse(mMockParent.hasShowContextMenuForChild());
   1257         assertFalse(listener.hasOnLongClick());
   1258         assertTrue(view.performLongClick());
   1259         assertFalse(mMockParent.hasShowContextMenuForChild());
   1260         assertTrue(listener.hasOnLongClick());
   1261     }
   1262 
   1263     public void testSetOnLongClickListener() {
   1264         MockView view = new MockView(mActivity);
   1265         view.setParent(mMockParent);
   1266         assertFalse(view.performLongClick());
   1267         assertFalse(view.isLongClickable());
   1268 
   1269         view.setOnLongClickListener(null);
   1270         assertFalse(view.performLongClick());
   1271         assertTrue(view.isLongClickable());
   1272 
   1273         view.setOnLongClickListener(new OnLongClickListenerImpl());
   1274         assertTrue(view.performLongClick());
   1275         assertTrue(view.isLongClickable());
   1276     }
   1277 
   1278     public void testAccessOnFocusChangeListener() {
   1279         View view = new View(mActivity);
   1280         OnFocusChangeListener listener = new OnFocusChangeListenerImpl();
   1281 
   1282         assertNull(view.getOnFocusChangeListener());
   1283 
   1284         view.setOnFocusChangeListener(listener);
   1285         assertSame(listener, view.getOnFocusChangeListener());
   1286     }
   1287 
   1288     public void testAccessNextFocusUpId() {
   1289         View view = new View(mActivity);
   1290 
   1291         assertEquals(View.NO_ID, view.getNextFocusUpId());
   1292 
   1293         view.setNextFocusUpId(1);
   1294         assertEquals(1, view.getNextFocusUpId());
   1295 
   1296         view.setNextFocusUpId(Integer.MAX_VALUE);
   1297         assertEquals(Integer.MAX_VALUE, view.getNextFocusUpId());
   1298 
   1299         view.setNextFocusUpId(Integer.MIN_VALUE);
   1300         assertEquals(Integer.MIN_VALUE, view.getNextFocusUpId());
   1301     }
   1302 
   1303     public void testAccessNextFocusDownId() {
   1304         View view = new View(mActivity);
   1305 
   1306         assertEquals(View.NO_ID, view.getNextFocusDownId());
   1307 
   1308         view.setNextFocusDownId(1);
   1309         assertEquals(1, view.getNextFocusDownId());
   1310 
   1311         view.setNextFocusDownId(Integer.MAX_VALUE);
   1312         assertEquals(Integer.MAX_VALUE, view.getNextFocusDownId());
   1313 
   1314         view.setNextFocusDownId(Integer.MIN_VALUE);
   1315         assertEquals(Integer.MIN_VALUE, view.getNextFocusDownId());
   1316     }
   1317 
   1318     public void testAccessNextFocusLeftId() {
   1319         View view = new View(mActivity);
   1320 
   1321         assertEquals(View.NO_ID, view.getNextFocusLeftId());
   1322 
   1323         view.setNextFocusLeftId(1);
   1324         assertEquals(1, view.getNextFocusLeftId());
   1325 
   1326         view.setNextFocusLeftId(Integer.MAX_VALUE);
   1327         assertEquals(Integer.MAX_VALUE, view.getNextFocusLeftId());
   1328 
   1329         view.setNextFocusLeftId(Integer.MIN_VALUE);
   1330         assertEquals(Integer.MIN_VALUE, view.getNextFocusLeftId());
   1331     }
   1332 
   1333     public void testAccessNextFocusRightId() {
   1334         View view = new View(mActivity);
   1335 
   1336         assertEquals(View.NO_ID, view.getNextFocusRightId());
   1337 
   1338         view.setNextFocusRightId(1);
   1339         assertEquals(1, view.getNextFocusRightId());
   1340 
   1341         view.setNextFocusRightId(Integer.MAX_VALUE);
   1342         assertEquals(Integer.MAX_VALUE, view.getNextFocusRightId());
   1343 
   1344         view.setNextFocusRightId(Integer.MIN_VALUE);
   1345         assertEquals(Integer.MIN_VALUE, view.getNextFocusRightId());
   1346     }
   1347 
   1348     public void testAccessMeasuredDimension() {
   1349         MockView view = new MockView(mActivity);
   1350         assertEquals(0, view.getMeasuredWidth());
   1351         assertEquals(0, view.getMeasuredHeight());
   1352 
   1353         view.setMeasuredDimensionWrapper(20, 30);
   1354         assertEquals(20, view.getMeasuredWidth());
   1355         assertEquals(30, view.getMeasuredHeight());
   1356     }
   1357 
   1358     public void testMeasure() throws Throwable {
   1359         final MockView view = (MockView) mActivity.findViewById(R.id.mock_view);
   1360         assertTrue(view.hasCalledOnMeasure());
   1361         assertEquals(100, view.getMeasuredWidth());
   1362         assertEquals(200, view.getMeasuredHeight());
   1363 
   1364         view.reset();
   1365         runTestOnUiThread(new Runnable() {
   1366             public void run() {
   1367                 view.requestLayout();
   1368             }
   1369         });
   1370         getInstrumentation().waitForIdleSync();
   1371         assertTrue(view.hasCalledOnMeasure());
   1372         assertEquals(100, view.getMeasuredWidth());
   1373         assertEquals(200, view.getMeasuredHeight());
   1374 
   1375         view.reset();
   1376         final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(200, 100);
   1377         runTestOnUiThread(new Runnable() {
   1378             public void run() {
   1379                 view.setLayoutParams(layoutParams);
   1380             }
   1381         });
   1382         getInstrumentation().waitForIdleSync();
   1383         assertTrue(view.hasCalledOnMeasure());
   1384         assertEquals(200, view.getMeasuredWidth());
   1385         assertEquals(100, view.getMeasuredHeight());
   1386     }
   1387 
   1388     public void testAccessLayoutParams() {
   1389         View view = new View(mActivity);
   1390         ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(10, 20);
   1391 
   1392         assertNull(view.getLayoutParams());
   1393 
   1394         try {
   1395             view.setLayoutParams(null);
   1396             fail("should throw NullPointerException");
   1397         } catch (NullPointerException e) {
   1398         }
   1399 
   1400         assertFalse(view.isLayoutRequested());
   1401         view.setLayoutParams(params);
   1402         assertSame(params, view.getLayoutParams());
   1403         assertTrue(view.isLayoutRequested());
   1404     }
   1405 
   1406     public void testIsShown() {
   1407         MockView view = new MockView(mActivity);
   1408 
   1409         view.setVisibility(View.INVISIBLE);
   1410         assertFalse(view.isShown());
   1411 
   1412         view.setVisibility(View.VISIBLE);
   1413         assertNull(view.getParent());
   1414         assertFalse(view.isShown());
   1415 
   1416         view.setParent(mMockParent);
   1417         // mMockParent is not a instance of ViewRoot
   1418         assertFalse(view.isShown());
   1419     }
   1420 
   1421     public void testGetDrawingTime() {
   1422         View view = new View(mActivity);
   1423         // mAttachInfo is null
   1424         assertEquals(0, view.getDrawingTime());
   1425 
   1426         // mAttachInfo is not null
   1427         view = mActivity.findViewById(R.id.fit_windows);
   1428         assertEquals(SystemClock.uptimeMillis(), view.getDrawingTime(), 1000);
   1429     }
   1430 
   1431     public void testScheduleDrawable() {
   1432         View view = new View(mActivity);
   1433         Drawable drawable = new StateListDrawable();
   1434         Runnable what = new Runnable() {
   1435             public void run() {
   1436                 // do nothing
   1437             }
   1438         };
   1439 
   1440         // mAttachInfo is null
   1441         view.scheduleDrawable(drawable, what, 1000);
   1442 
   1443         view.setBackgroundDrawable(drawable);
   1444         view.scheduleDrawable(drawable, what, 1000);
   1445 
   1446         view.scheduleDrawable(null, null, -1000);
   1447 
   1448         // mAttachInfo is not null
   1449         view = mActivity.findViewById(R.id.fit_windows);
   1450         view.scheduleDrawable(drawable, what, 1000);
   1451 
   1452         view.scheduleDrawable(view.getBackground(), what, 1000);
   1453         view.unscheduleDrawable(view.getBackground(), what);
   1454 
   1455         view.scheduleDrawable(null, null, -1000);
   1456     }
   1457 
   1458     public void testUnscheduleDrawable() {
   1459         View view = new View(mActivity);
   1460         Drawable drawable = new StateListDrawable();
   1461         Runnable what = new Runnable() {
   1462             public void run() {
   1463                 // do nothing
   1464             }
   1465         };
   1466 
   1467         // mAttachInfo is null
   1468         view.unscheduleDrawable(drawable, what);
   1469 
   1470         view.setBackgroundDrawable(drawable);
   1471         view.unscheduleDrawable(drawable);
   1472 
   1473         view.unscheduleDrawable(null, null);
   1474         view.unscheduleDrawable(null);
   1475 
   1476         // mAttachInfo is not null
   1477         view = mActivity.findViewById(R.id.fit_windows);
   1478         view.unscheduleDrawable(drawable);
   1479 
   1480         view.scheduleDrawable(view.getBackground(), what, 1000);
   1481         view.unscheduleDrawable(view.getBackground(), what);
   1482 
   1483         view.unscheduleDrawable(null);
   1484         view.unscheduleDrawable(null, null);
   1485     }
   1486 
   1487     public void testGetWindowVisibility() {
   1488         View view = new View(mActivity);
   1489         // mAttachInfo is null
   1490         assertEquals(View.GONE, view.getWindowVisibility());
   1491 
   1492         // mAttachInfo is not null
   1493         view = mActivity.findViewById(R.id.fit_windows);
   1494         assertEquals(View.VISIBLE, view.getWindowVisibility());
   1495     }
   1496 
   1497     public void testGetWindowToken() {
   1498         View view = new View(mActivity);
   1499         // mAttachInfo is null
   1500         assertNull(view.getWindowToken());
   1501 
   1502         // mAttachInfo is not null
   1503         view = mActivity.findViewById(R.id.fit_windows);
   1504         assertNotNull(view.getWindowToken());
   1505     }
   1506 
   1507     public void testHasWindowFocus() {
   1508         View view = new View(mActivity);
   1509         // mAttachInfo is null
   1510         assertFalse(view.hasWindowFocus());
   1511 
   1512         // mAttachInfo is not null
   1513         final View view2 = mActivity.findViewById(R.id.fit_windows);
   1514         // Wait until the window has been focused.
   1515         new PollingCheck(TIMEOUT_DELTA) {
   1516             @Override
   1517             protected boolean check() {
   1518                 return view2.hasWindowFocus();
   1519             }
   1520         }.run();
   1521     }
   1522 
   1523     public void testGetHandler() {
   1524         MockView view = new MockView(mActivity);
   1525         // mAttachInfo is null
   1526         assertNull(view.getHandler());
   1527     }
   1528 
   1529     public void testRemoveCallbacks() throws InterruptedException {
   1530         final long delay = 500L;
   1531         View view = mActivity.findViewById(R.id.mock_view);
   1532         MockRunnable runner = new MockRunnable();
   1533         assertTrue(view.postDelayed(runner, delay));
   1534         assertTrue(view.removeCallbacks(runner));
   1535         assertTrue(view.removeCallbacks(null));
   1536         assertTrue(view.removeCallbacks(new MockRunnable()));
   1537         Thread.sleep(delay * 2);
   1538         assertFalse(runner.hasRun);
   1539         // check that the runner actually works
   1540         runner = new MockRunnable();
   1541         assertTrue(view.postDelayed(runner, delay));
   1542         Thread.sleep(delay * 2);
   1543         assertTrue(runner.hasRun);
   1544     }
   1545 
   1546     public void testCancelLongPress() {
   1547         View view = new View(mActivity);
   1548         // mAttachInfo is null
   1549         view.cancelLongPress();
   1550 
   1551         // mAttachInfo is not null
   1552         view = mActivity.findViewById(R.id.fit_windows);
   1553         view.cancelLongPress();
   1554     }
   1555 
   1556     public void testGetViewTreeObserver() {
   1557         View view = new View(mActivity);
   1558         // mAttachInfo is null
   1559         assertNotNull(view.getViewTreeObserver());
   1560 
   1561         // mAttachInfo is not null
   1562         view = mActivity.findViewById(R.id.fit_windows);
   1563         assertNotNull(view.getViewTreeObserver());
   1564     }
   1565 
   1566     public void testGetWindowAttachCount() {
   1567         MockView view = new MockView(mActivity);
   1568         // mAttachInfo is null
   1569         assertEquals(0, view.getWindowAttachCount());
   1570     }
   1571 
   1572     @UiThreadTest
   1573     public void testOnAttachedToAndDetachedFromWindow() {
   1574         MockView mockView = new MockView(mActivity);
   1575         ViewGroup viewGroup = (ViewGroup) mActivity.findViewById(R.id.viewlayout_root);
   1576 
   1577         viewGroup.addView(mockView);
   1578         assertTrue(mockView.hasCalledOnAttachedToWindow());
   1579 
   1580         viewGroup.removeView(mockView);
   1581         assertTrue(mockView.hasCalledOnDetachedFromWindow());
   1582 
   1583         mockView.reset();
   1584         mActivity.setContentView(mockView);
   1585         assertTrue(mockView.hasCalledOnAttachedToWindow());
   1586 
   1587         mActivity.setContentView(R.layout.view_layout);
   1588         assertTrue(mockView.hasCalledOnDetachedFromWindow());
   1589     }
   1590 
   1591     public void testGetLocationInWindow() {
   1592         int[] location = new int[] { -1, -1 };
   1593 
   1594         View layout = mActivity.findViewById(R.id.viewlayout_root);
   1595         int[] layoutLocation = new int[] { -1, -1 };
   1596         layout.getLocationInWindow(layoutLocation);
   1597 
   1598         final View mockView = mActivity.findViewById(R.id.mock_view);
   1599         mockView.getLocationInWindow(location);
   1600         assertEquals(layoutLocation[0], location[0]);
   1601         assertEquals(layoutLocation[1], location[1]);
   1602 
   1603         View scrollView = mActivity.findViewById(R.id.scroll_view);
   1604         scrollView.getLocationInWindow(location);
   1605         assertEquals(layoutLocation[0], location[0]);
   1606         assertEquals(layoutLocation[1] + mockView.getHeight(), location[1]);
   1607 
   1608         try {
   1609             mockView.getLocationInWindow(null);
   1610             fail("should throw IllegalArgumentException");
   1611         } catch (IllegalArgumentException e) {
   1612         }
   1613 
   1614         try {
   1615             mockView.getLocationInWindow(new int[] { 0 });
   1616             fail("should throw IllegalArgumentException");
   1617         } catch (IllegalArgumentException e) {
   1618         }
   1619     }
   1620 
   1621     public void testGetLocationOnScreen() {
   1622         View view = new View(mActivity);
   1623         int[] location = new int[] { -1, -1 };
   1624 
   1625         // mAttachInfo is not null
   1626         View layout = mActivity.findViewById(R.id.viewlayout_root);
   1627         int[] layoutLocation = new int[] { -1, -1 };
   1628         layout.getLocationOnScreen(layoutLocation);
   1629 
   1630         View mockView = mActivity.findViewById(R.id.mock_view);
   1631         mockView.getLocationOnScreen(location);
   1632         assertEquals(layoutLocation[0], location[0]);
   1633         assertEquals(layoutLocation[1], location[1]);
   1634 
   1635         View scrollView = mActivity.findViewById(R.id.scroll_view);
   1636         scrollView.getLocationOnScreen(location);
   1637         assertEquals(layoutLocation[0], location[0]);
   1638         assertEquals(layoutLocation[1] + mockView.getHeight(), location[1]);
   1639 
   1640         try {
   1641             scrollView.getLocationOnScreen(null);
   1642             fail("should throw IllegalArgumentException");
   1643         } catch (IllegalArgumentException e) {
   1644         }
   1645 
   1646         try {
   1647             scrollView.getLocationOnScreen(new int[] { 0 });
   1648             fail("should throw IllegalArgumentException");
   1649         } catch (IllegalArgumentException e) {
   1650         }
   1651     }
   1652 
   1653     public void testAddTouchables() {
   1654         View view = new View(mActivity);
   1655         ArrayList<View> result = new ArrayList<View>();
   1656         assertEquals(0, result.size());
   1657 
   1658         view.addTouchables(result);
   1659         assertEquals(0, result.size());
   1660 
   1661         view.setClickable(true);
   1662         view.addTouchables(result);
   1663         assertEquals(1, result.size());
   1664         assertSame(view, result.get(0));
   1665 
   1666         try {
   1667             view.addTouchables(null);
   1668             fail("should throw NullPointerException");
   1669         } catch (NullPointerException e) {
   1670         }
   1671 
   1672         result.clear();
   1673         view.setEnabled(false);
   1674         assertTrue(view.isClickable());
   1675         view.addTouchables(result);
   1676         assertEquals(0, result.size());
   1677     }
   1678 
   1679     public void testGetTouchables() {
   1680         View view = new View(mActivity);
   1681         ArrayList<View> result;
   1682 
   1683         result = view.getTouchables();
   1684         assertEquals(0, result.size());
   1685 
   1686         view.setClickable(true);
   1687         result = view.getTouchables();
   1688         assertEquals(1, result.size());
   1689         assertSame(view, result.get(0));
   1690 
   1691         result.clear();
   1692         view.setEnabled(false);
   1693         assertTrue(view.isClickable());
   1694         result = view.getTouchables();
   1695         assertEquals(0, result.size());
   1696     }
   1697 
   1698     public void testInflate() {
   1699         View view = View.inflate(mActivity, R.layout.view_layout, null);
   1700         assertNotNull(view);
   1701         assertTrue(view instanceof LinearLayout);
   1702 
   1703         MockView mockView = (MockView) view.findViewById(R.id.mock_view);
   1704         assertNotNull(mockView);
   1705         assertTrue(mockView.hasCalledOnFinishInflate());
   1706     }
   1707 
   1708     public void testIsInTouchMode() {
   1709         View view = new View(mActivity);
   1710         // mAttachInfo is null
   1711         assertFalse(view.isInTouchMode());
   1712 
   1713         // mAttachInfo is not null
   1714         view = mActivity.findViewById(R.id.fit_windows);
   1715         assertFalse(view.isInTouchMode());
   1716     }
   1717 
   1718     public void testIsInEditMode() {
   1719         View view = new View(mActivity);
   1720         assertFalse(view.isInEditMode());
   1721     }
   1722 
   1723     public void testPostInvalidate1() {
   1724         View view = new View(mActivity);
   1725         // mAttachInfo is null
   1726         view.postInvalidate();
   1727 
   1728         // mAttachInfo is not null
   1729         view = mActivity.findViewById(R.id.fit_windows);
   1730         view.postInvalidate();
   1731     }
   1732 
   1733     public void testPostInvalidate2() {
   1734         View view = new View(mActivity);
   1735         // mAttachInfo is null
   1736         view.postInvalidate(0, 1, 2, 3);
   1737 
   1738         // mAttachInfo is not null
   1739         view = mActivity.findViewById(R.id.fit_windows);
   1740         view.postInvalidate(10, 20, 30, 40);
   1741         view.postInvalidate(0, -20, -30, -40);
   1742     }
   1743 
   1744     public void testPostInvalidateDelayed() {
   1745         View view = new View(mActivity);
   1746         // mAttachInfo is null
   1747         view.postInvalidateDelayed(1000);
   1748         view.postInvalidateDelayed(500, 0, 0, 100, 200);
   1749 
   1750         // mAttachInfo is not null
   1751         view = mActivity.findViewById(R.id.fit_windows);
   1752         view.postInvalidateDelayed(1000);
   1753         view.postInvalidateDelayed(500, 0, 0, 100, 200);
   1754         view.postInvalidateDelayed(-1);
   1755     }
   1756 
   1757     public void testPost() {
   1758         View view = new View(mActivity);
   1759         MockRunnable action = new MockRunnable();
   1760 
   1761         // mAttachInfo is null
   1762         assertTrue(view.post(action));
   1763         assertTrue(view.post(null));
   1764 
   1765         // mAttachInfo is not null
   1766         view = mActivity.findViewById(R.id.fit_windows);
   1767         assertTrue(view.post(action));
   1768         assertTrue(view.post(null));
   1769     }
   1770 
   1771     public void testPostDelayed() {
   1772         View view = new View(mActivity);
   1773         MockRunnable action = new MockRunnable();
   1774 
   1775         // mAttachInfo is null
   1776         assertTrue(view.postDelayed(action, 1000));
   1777         assertTrue(view.postDelayed(null, -1));
   1778 
   1779         // mAttachInfo is not null
   1780         view = mActivity.findViewById(R.id.fit_windows);
   1781         assertTrue(view.postDelayed(action, 1000));
   1782         assertTrue(view.postDelayed(null, 0));
   1783     }
   1784 
   1785     @UiThreadTest
   1786     public void testPlaySoundEffect() {
   1787         MockView view = (MockView) mActivity.findViewById(R.id.mock_view);
   1788         // sound effect enabled
   1789         view.playSoundEffect(SoundEffectConstants.CLICK);
   1790 
   1791         // sound effect disabled
   1792         view.setSoundEffectsEnabled(false);
   1793         view.playSoundEffect(SoundEffectConstants.NAVIGATION_DOWN);
   1794 
   1795         // no way to assert the soundConstant be really played.
   1796     }
   1797 
   1798     public void testOnKeyShortcut() throws Throwable {
   1799         final MockView view = (MockView) mActivity.findViewById(R.id.mock_view);
   1800         runTestOnUiThread(new Runnable() {
   1801             public void run() {
   1802                 view.setFocusable(true);
   1803                 view.requestFocus();
   1804             }
   1805         });
   1806         getInstrumentation().waitForIdleSync();
   1807         assertTrue(view.isFocused());
   1808 
   1809         KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MENU);
   1810         getInstrumentation().sendKeySync(event);
   1811         event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_0);
   1812         getInstrumentation().sendKeySync(event);
   1813         assertTrue(view.hasCalledOnKeyShortcut());
   1814     }
   1815 
   1816     public void testOnKeyMultiple() throws Throwable {
   1817         final MockView view = (MockView) mActivity.findViewById(R.id.mock_view);
   1818         runTestOnUiThread(new Runnable() {
   1819             public void run() {
   1820                 view.setFocusable(true);
   1821             }
   1822         });
   1823 
   1824         assertFalse(view.hasCalledOnKeyMultiple());
   1825         view.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_MULTIPLE, KeyEvent.KEYCODE_ENTER));
   1826         assertTrue(view.hasCalledOnKeyMultiple());
   1827     }
   1828 
   1829     @UiThreadTest
   1830     public void testDispatchKeyShortcutEvent() {
   1831         MockView view = (MockView) mActivity.findViewById(R.id.mock_view);
   1832         view.setFocusable(true);
   1833 
   1834         KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_0);
   1835         view.dispatchKeyShortcutEvent(event);
   1836         assertTrue(view.hasCalledOnKeyShortcut());
   1837 
   1838         try {
   1839             view.dispatchKeyShortcutEvent(null);
   1840             fail("should throw NullPointerException");
   1841         } catch (NullPointerException e) {
   1842         }
   1843     }
   1844 
   1845     public void testOnTrackballEvent() throws Throwable {
   1846         final MockView view = (MockView) mActivity.findViewById(R.id.mock_view);
   1847         runTestOnUiThread(new Runnable() {
   1848             public void run() {
   1849                 view.setEnabled(true);
   1850                 view.setFocusable(true);
   1851                 view.requestFocus();
   1852             }
   1853         });
   1854         getInstrumentation().waitForIdleSync();
   1855 
   1856         int[] xy = new int[2];
   1857         view.getLocationOnScreen(xy);
   1858 
   1859         final int viewWidth = view.getWidth();
   1860         final int viewHeight = view.getHeight();
   1861         final float x = xy[0] + viewWidth / 2.0f;
   1862         final float y = xy[1] + viewHeight / 2.0f;
   1863 
   1864         long downTime = SystemClock.uptimeMillis();
   1865         long eventTime = SystemClock.uptimeMillis();
   1866         MotionEvent event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN,
   1867                 x, y, 0);
   1868         getInstrumentation().sendTrackballEventSync(event);
   1869         getInstrumentation().waitForIdleSync();
   1870         assertTrue(view.hasCalledOnTrackballEvent());
   1871     }
   1872 
   1873     @UiThreadTest
   1874     public void testDispatchTrackballEvent() {
   1875         ViewGroup viewGroup = (ViewGroup) mActivity.findViewById(R.id.viewlayout_root);
   1876         MockView mockView1 = new MockView(mActivity);
   1877         MockView mockView2 = new MockView(mActivity);
   1878         viewGroup.addView(mockView1);
   1879         viewGroup.addView(mockView2);
   1880         mockView1.setFocusable(true);
   1881         mockView2.setFocusable(true);
   1882         mockView2.requestFocus();
   1883 
   1884         long downTime = SystemClock.uptimeMillis();
   1885         long eventTime = SystemClock.uptimeMillis();
   1886         MotionEvent event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN,
   1887                 1, 2, 0);
   1888         mockView1.dispatchTrackballEvent(event);
   1889         // issue 1695243
   1890         // It passes a trackball motion event down to itself even if it is not the focused view.
   1891         assertTrue(mockView1.hasCalledOnTrackballEvent());
   1892         assertFalse(mockView2.hasCalledOnTrackballEvent());
   1893 
   1894         mockView1.reset();
   1895         mockView2.reset();
   1896         downTime = SystemClock.uptimeMillis();
   1897         eventTime = SystemClock.uptimeMillis();
   1898         event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, 1, 2, 0);
   1899         mockView2.dispatchTrackballEvent(event);
   1900         assertFalse(mockView1.hasCalledOnTrackballEvent());
   1901         assertTrue(mockView2.hasCalledOnTrackballEvent());
   1902     }
   1903 
   1904     public void testDispatchUnhandledMove() throws Throwable {
   1905         final MockView view = (MockView) mActivity.findViewById(R.id.mock_view);
   1906         runTestOnUiThread(new Runnable() {
   1907             public void run() {
   1908                 view.setFocusable(true);
   1909                 view.requestFocus();
   1910             }
   1911         });
   1912         getInstrumentation().waitForIdleSync();
   1913 
   1914         KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_RIGHT);
   1915         getInstrumentation().sendKeySync(event);
   1916 
   1917         assertTrue(view.hasCalledDispatchUnhandledMove());
   1918     }
   1919 
   1920     public void testWindowVisibilityChanged() throws Throwable {
   1921         final MockView mockView = new MockView(mActivity);
   1922         final ViewGroup viewGroup = (ViewGroup) mActivity.findViewById(R.id.viewlayout_root);
   1923 
   1924         runTestOnUiThread(new Runnable() {
   1925             public void run() {
   1926                 viewGroup.addView(mockView);
   1927             }
   1928         });
   1929         getInstrumentation().waitForIdleSync();
   1930         assertTrue(mockView.hasCalledOnWindowVisibilityChanged());
   1931 
   1932         mockView.reset();
   1933         runTestOnUiThread(new Runnable() {
   1934             public void run() {
   1935                 getActivity().setVisible(false);
   1936             }
   1937         });
   1938         getInstrumentation().waitForIdleSync();
   1939         assertTrue(mockView.hasCalledDispatchWindowVisibilityChanged());
   1940         assertTrue(mockView.hasCalledOnWindowVisibilityChanged());
   1941 
   1942         mockView.reset();
   1943         runTestOnUiThread(new Runnable() {
   1944             public void run() {
   1945                 getActivity().setVisible(true);
   1946             }
   1947         });
   1948         getInstrumentation().waitForIdleSync();
   1949         assertTrue(mockView.hasCalledDispatchWindowVisibilityChanged());
   1950         assertTrue(mockView.hasCalledOnWindowVisibilityChanged());
   1951 
   1952         mockView.reset();
   1953         runTestOnUiThread(new Runnable() {
   1954             public void run() {
   1955                 viewGroup.removeView(mockView);
   1956             }
   1957         });
   1958         getInstrumentation().waitForIdleSync();
   1959         assertTrue(mockView.hasCalledOnWindowVisibilityChanged());
   1960     }
   1961 
   1962     public void testGetLocalVisibleRect() throws Throwable {
   1963         final View view = mActivity.findViewById(R.id.mock_view);
   1964         Rect rect = new Rect();
   1965 
   1966         assertTrue(view.getLocalVisibleRect(rect));
   1967         assertEquals(0, rect.left);
   1968         assertEquals(0, rect.top);
   1969         assertEquals(100, rect.right);
   1970         assertEquals(200, rect.bottom);
   1971 
   1972         final LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(0, 300);
   1973         runTestOnUiThread(new Runnable() {
   1974             public void run() {
   1975                 view.setLayoutParams(layoutParams1);
   1976             }
   1977         });
   1978         getInstrumentation().waitForIdleSync();
   1979         assertFalse(view.getLocalVisibleRect(rect));
   1980 
   1981         final LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(200, -10);
   1982         runTestOnUiThread(new Runnable() {
   1983             public void run() {
   1984                 view.setLayoutParams(layoutParams2);
   1985             }
   1986         });
   1987         getInstrumentation().waitForIdleSync();
   1988         assertFalse(view.getLocalVisibleRect(rect));
   1989 
   1990         Display display = getActivity().getWindowManager().getDefaultDisplay();
   1991         int halfWidth = display.getWidth() / 2;
   1992         int halfHeight = display.getHeight() /2;
   1993 
   1994         final LinearLayout.LayoutParams layoutParams3 =
   1995                 new LinearLayout.LayoutParams(halfWidth, halfHeight);
   1996         runTestOnUiThread(new Runnable() {
   1997             public void run() {
   1998                 view.setLayoutParams(layoutParams3);
   1999                 view.scrollTo(20, -30);
   2000             }
   2001         });
   2002         getInstrumentation().waitForIdleSync();
   2003         assertTrue(view.getLocalVisibleRect(rect));
   2004         assertEquals(20, rect.left);
   2005         assertEquals(-30, rect.top);
   2006         assertEquals(halfWidth + 20, rect.right);
   2007         assertEquals(halfHeight - 30, rect.bottom);
   2008 
   2009         try {
   2010             view.getLocalVisibleRect(null);
   2011             fail("should throw NullPointerException");
   2012         } catch (NullPointerException e) {
   2013         }
   2014     }
   2015 
   2016     public void testMergeDrawableStates() {
   2017         MockView view = new MockView(mActivity);
   2018 
   2019         int[] states = view.mergeDrawableStatesWrapper(new int[] { 0, 1, 2, 0, 0 },
   2020                 new int[] { 3 });
   2021         assertNotNull(states);
   2022         assertEquals(5, states.length);
   2023         assertEquals(0, states[0]);
   2024         assertEquals(1, states[1]);
   2025         assertEquals(2, states[2]);
   2026         assertEquals(3, states[3]);
   2027         assertEquals(0, states[4]);
   2028 
   2029         try {
   2030             view.mergeDrawableStatesWrapper(new int[] { 1, 2 }, new int[] { 3 });
   2031             fail("should throw IndexOutOfBoundsException");
   2032         } catch (IndexOutOfBoundsException e) {
   2033         }
   2034 
   2035         try {
   2036             view.mergeDrawableStatesWrapper(null, new int[] { 0 });
   2037             fail("should throw NullPointerException");
   2038         } catch (NullPointerException e) {
   2039         }
   2040 
   2041         try {
   2042             view.mergeDrawableStatesWrapper(new int [] { 0 }, null);
   2043             fail("should throw NullPointerException");
   2044         } catch (NullPointerException e) {
   2045         }
   2046     }
   2047 
   2048     public void testOnSaveAndRestoreInstanceState() {
   2049         // it is hard to simulate operation to make callback be called.
   2050     }
   2051 
   2052     public void testSaveAndRestoreHierarchyState() {
   2053         int viewId = R.id.mock_view;
   2054         MockView view = (MockView) mActivity.findViewById(viewId);
   2055         SparseArray<Parcelable> container = new SparseArray<Parcelable>();
   2056         view.saveHierarchyState(container);
   2057         assertTrue(view.hasCalledDispatchSaveInstanceState());
   2058         assertTrue(view.hasCalledOnSaveInstanceState());
   2059         assertEquals(viewId, container.keyAt(0));
   2060 
   2061         view.reset();
   2062         container.put(R.id.mock_view, BaseSavedState.EMPTY_STATE);
   2063         view.restoreHierarchyState(container);
   2064         assertTrue(view.hasCalledDispatchRestoreInstanceState());
   2065         assertTrue(view.hasCalledOnRestoreInstanceState());
   2066         container.clear();
   2067         view.saveHierarchyState(container);
   2068         assertTrue(view.hasCalledDispatchSaveInstanceState());
   2069         assertTrue(view.hasCalledOnSaveInstanceState());
   2070         assertEquals(viewId, container.keyAt(0));
   2071 
   2072         container.clear();
   2073         container.put(viewId, new BaseSavedState(BaseSavedState.EMPTY_STATE));
   2074         try {
   2075             view.restoreHierarchyState(container);
   2076             fail("should throw IllegalArgumentException");
   2077         } catch (IllegalArgumentException e) {
   2078             // expected
   2079         }
   2080 
   2081         try {
   2082             view.restoreHierarchyState(null);
   2083             fail("should throw NullPointerException");
   2084         } catch (NullPointerException e) {
   2085             // expected
   2086         }
   2087 
   2088         try {
   2089             view.saveHierarchyState(null);
   2090             fail("should throw NullPointerException");
   2091         } catch (NullPointerException e) {
   2092             // expected
   2093         }
   2094     }
   2095 
   2096     public void testOnKeyDownOrUp() throws Throwable {
   2097         final MockView view = (MockView) mActivity.findViewById(R.id.mock_view);
   2098         runTestOnUiThread(new Runnable() {
   2099             public void run() {
   2100                 view.setFocusable(true);
   2101                 view.requestFocus();
   2102             }
   2103         });
   2104         getInstrumentation().waitForIdleSync();
   2105         assertTrue(view.isFocused());
   2106 
   2107         KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_0);
   2108         getInstrumentation().sendKeySync(event);
   2109         assertTrue(view.hasCalledOnKeyDown());
   2110 
   2111         event = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_0);
   2112         getInstrumentation().sendKeySync(event);
   2113         assertTrue(view.hasCalledOnKeyUp());
   2114 
   2115         view.reset();
   2116         assertTrue(view.isEnabled());
   2117         assertFalse(view.isClickable());
   2118         assertFalse(view.isPressed());
   2119         event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER);
   2120         getInstrumentation().sendKeySync(event);
   2121         assertFalse(view.isPressed());
   2122         assertTrue(view.hasCalledOnKeyDown());
   2123 
   2124         runTestOnUiThread(new Runnable() {
   2125             public void run() {
   2126                 view.setEnabled(true);
   2127                 view.setClickable(true);
   2128             }
   2129         });
   2130         view.reset();
   2131         OnClickListenerImpl listener = new OnClickListenerImpl();
   2132         view.setOnClickListener(listener);
   2133 
   2134         assertFalse(view.isPressed());
   2135         event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER);
   2136         getInstrumentation().sendKeySync(event);
   2137         assertTrue(view.isPressed());
   2138         assertTrue(view.hasCalledOnKeyDown());
   2139         event = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_ENTER);
   2140         getInstrumentation().sendKeySync(event);
   2141         assertFalse(view.isPressed());
   2142         assertTrue(view.hasCalledOnKeyUp());
   2143         assertTrue(listener.hasOnClick());
   2144 
   2145         view.setPressed(false);
   2146         listener.reset();
   2147         view.reset();
   2148 
   2149         assertFalse(view.isPressed());
   2150         event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_CENTER);
   2151         getInstrumentation().sendKeySync(event);
   2152         assertTrue(view.isPressed());
   2153         assertTrue(view.hasCalledOnKeyDown());
   2154         event = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_CENTER);
   2155         getInstrumentation().sendKeySync(event);
   2156         assertFalse(view.isPressed());
   2157         assertTrue(view.hasCalledOnKeyUp());
   2158         assertTrue(listener.hasOnClick());
   2159     }
   2160 
   2161     @UiThreadTest
   2162     public void testDispatchKeyEvent() {
   2163         MockView view = (MockView) mActivity.findViewById(R.id.mock_view);
   2164         MockView mockView1 = new MockView(mActivity);
   2165         MockView mockView2 = new MockView(mActivity);
   2166         ViewGroup viewGroup = (ViewGroup) mActivity.findViewById(R.id.viewlayout_root);
   2167         viewGroup.addView(mockView1);
   2168         viewGroup.addView(mockView2);
   2169         view.setFocusable(true);
   2170         mockView1.setFocusable(true);
   2171         mockView2.setFocusable(true);
   2172 
   2173         assertFalse(view.hasCalledOnKeyDown());
   2174         assertFalse(mockView1.hasCalledOnKeyDown());
   2175         assertFalse(mockView2.hasCalledOnKeyDown());
   2176         KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_0);
   2177         assertFalse(view.dispatchKeyEvent(event));
   2178         assertTrue(view.hasCalledOnKeyDown());
   2179         assertFalse(mockView1.hasCalledOnKeyDown());
   2180         assertFalse(mockView2.hasCalledOnKeyDown());
   2181 
   2182         view.reset();
   2183         mockView1.reset();
   2184         mockView2.reset();
   2185         assertFalse(view.hasCalledOnKeyDown());
   2186         assertFalse(mockView1.hasCalledOnKeyDown());
   2187         assertFalse(mockView2.hasCalledOnKeyDown());
   2188         event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_0);
   2189         assertFalse(mockView1.dispatchKeyEvent(event));
   2190         assertFalse(view.hasCalledOnKeyDown());
   2191         // issue 1695243
   2192         // When the view has NOT focus, it dispatches to itself, which disobey the javadoc.
   2193         assertTrue(mockView1.hasCalledOnKeyDown());
   2194         assertFalse(mockView2.hasCalledOnKeyDown());
   2195 
   2196         assertFalse(view.hasCalledOnKeyUp());
   2197         event = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_0);
   2198         assertFalse(view.dispatchKeyEvent(event));
   2199         assertTrue(view.hasCalledOnKeyUp());
   2200 
   2201         assertFalse(view.hasCalledOnKeyMultiple());
   2202         event = new KeyEvent(1, 2, KeyEvent.ACTION_MULTIPLE, KeyEvent.KEYCODE_0, 2);
   2203         assertFalse(view.dispatchKeyEvent(event));
   2204         assertTrue(view.hasCalledOnKeyMultiple());
   2205 
   2206         try {
   2207             view.dispatchKeyEvent(null);
   2208             fail("should throw NullPointerException");
   2209         } catch (NullPointerException e) {
   2210             // expected
   2211         }
   2212 
   2213         view.reset();
   2214         event = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_0);
   2215         OnKeyListenerImpl listener = new OnKeyListenerImpl();
   2216         view.setOnKeyListener(listener);
   2217         assertFalse(listener.hasOnKey());
   2218         assertTrue(view.dispatchKeyEvent(event));
   2219         assertTrue(listener.hasOnKey());
   2220         assertFalse(view.hasCalledOnKeyUp());
   2221     }
   2222 
   2223     @UiThreadTest
   2224     public void testDispatchTouchEvent() {
   2225         ViewGroup viewGroup = (ViewGroup) mActivity.findViewById(R.id.viewlayout_root);
   2226         MockView mockView1 = new MockView(mActivity);
   2227         MockView mockView2 = new MockView(mActivity);
   2228         viewGroup.addView(mockView1);
   2229         viewGroup.addView(mockView2);
   2230 
   2231         int[] xy = new int[2];
   2232         mockView1.getLocationOnScreen(xy);
   2233 
   2234         final int viewWidth = mockView1.getWidth();
   2235         final int viewHeight = mockView1.getHeight();
   2236         final float x = xy[0] + viewWidth / 2.0f;
   2237         final float y = xy[1] + viewHeight / 2.0f;
   2238 
   2239         long downTime = SystemClock.uptimeMillis();
   2240         long eventTime = SystemClock.uptimeMillis();
   2241         MotionEvent event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE,
   2242                 x, y, 0);
   2243 
   2244         assertFalse(mockView1.hasCalledOnTouchEvent());
   2245         assertFalse(mockView1.dispatchTouchEvent(event));
   2246         assertTrue(mockView1.hasCalledOnTouchEvent());
   2247 
   2248         assertFalse(mockView2.hasCalledOnTouchEvent());
   2249         assertFalse(mockView2.dispatchTouchEvent(event));
   2250         // issue 1695243
   2251         // it passes the touch screen motion event down to itself even if it is not the target view.
   2252         assertTrue(mockView2.hasCalledOnTouchEvent());
   2253 
   2254         mockView1.reset();
   2255         OnTouchListenerImpl listener = new OnTouchListenerImpl();
   2256         mockView1.setOnTouchListener(listener);
   2257         assertFalse(listener.hasOnTouch());
   2258         assertTrue(mockView1.dispatchTouchEvent(event));
   2259         assertTrue(listener.hasOnTouch());
   2260         assertFalse(mockView1.hasCalledOnTouchEvent());
   2261     }
   2262 
   2263     public void testInvalidate1() throws Throwable {
   2264         final MockView view = (MockView) mActivity.findViewById(R.id.mock_view);
   2265         assertTrue(view.hasCalledOnDraw());
   2266 
   2267         view.reset();
   2268         runTestOnUiThread(new Runnable() {
   2269             public void run() {
   2270                 view.invalidate();
   2271             }
   2272         });
   2273         getInstrumentation().waitForIdleSync();
   2274         new PollingCheck() {
   2275             @Override
   2276             protected boolean check() {
   2277                 return view.hasCalledOnDraw();
   2278             }
   2279         }.run();
   2280 
   2281         view.reset();
   2282         runTestOnUiThread(new Runnable() {
   2283             public void run() {
   2284                 view.setVisibility(View.INVISIBLE);
   2285                 view.invalidate();
   2286             }
   2287         });
   2288         getInstrumentation().waitForIdleSync();
   2289         assertFalse(view.hasCalledOnDraw());
   2290     }
   2291 
   2292     public void testInvalidate2() throws Throwable {
   2293         final MockView view = (MockView) mActivity.findViewById(R.id.mock_view);
   2294         assertTrue(view.hasCalledOnDraw());
   2295 
   2296         try {
   2297             view.invalidate(null);
   2298             fail("should throw NullPointerException");
   2299         } catch (NullPointerException e) {
   2300         }
   2301 
   2302         view.reset();
   2303         final Rect dirty = new Rect(view.getLeft() + 1, view.getTop() + 1,
   2304                 view.getLeft() + view.getWidth() / 2, view.getTop() + view.getHeight() / 2);
   2305         runTestOnUiThread(new Runnable() {
   2306             public void run() {
   2307                 view.invalidate(dirty);
   2308             }
   2309         });
   2310         getInstrumentation().waitForIdleSync();
   2311         new PollingCheck() {
   2312             @Override
   2313             protected boolean check() {
   2314                 return view.hasCalledOnDraw();
   2315             }
   2316         }.run();
   2317 
   2318         view.reset();
   2319         runTestOnUiThread(new Runnable() {
   2320             public void run() {
   2321                 view.setVisibility(View.INVISIBLE);
   2322                 view.invalidate(dirty);
   2323             }
   2324         });
   2325         getInstrumentation().waitForIdleSync();
   2326         assertFalse(view.hasCalledOnDraw());
   2327     }
   2328 
   2329     public void testInvalidate3() throws Throwable {
   2330         final MockView view = (MockView) mActivity.findViewById(R.id.mock_view);
   2331         assertTrue(view.hasCalledOnDraw());
   2332 
   2333         view.reset();
   2334         final Rect dirty = new Rect(view.getLeft() + 1, view.getTop() + 1,
   2335                 view.getLeft() + view.getWidth() / 2, view.getTop() + view.getHeight() / 2);
   2336         runTestOnUiThread(new Runnable() {
   2337             public void run() {
   2338                 view.invalidate(dirty.left, dirty.top, dirty.right, dirty.bottom);
   2339             }
   2340         });
   2341         getInstrumentation().waitForIdleSync();
   2342         new PollingCheck() {
   2343             @Override
   2344             protected boolean check() {
   2345                 return view.hasCalledOnDraw();
   2346             }
   2347         }.run();
   2348 
   2349         view.reset();
   2350         runTestOnUiThread(new Runnable() {
   2351             public void run() {
   2352                 view.setVisibility(View.INVISIBLE);
   2353                 view.invalidate(dirty.left, dirty.top, dirty.right, dirty.bottom);
   2354             }
   2355         });
   2356         getInstrumentation().waitForIdleSync();
   2357         assertFalse(view.hasCalledOnDraw());
   2358     }
   2359 
   2360     public void testInvalidateDrawable() throws Throwable {
   2361         final MockView view = (MockView) mActivity.findViewById(R.id.mock_view);
   2362         final Drawable d1 = mResources.getDrawable(R.drawable.scenery);
   2363         final Drawable d2 = mResources.getDrawable(R.drawable.pass);
   2364 
   2365         view.reset();
   2366         runTestOnUiThread(new Runnable() {
   2367             public void run() {
   2368                 view.setBackgroundDrawable(d1);
   2369                 view.invalidateDrawable(d1);
   2370             }
   2371         });
   2372         getInstrumentation().waitForIdleSync();
   2373         new PollingCheck() {
   2374             @Override
   2375             protected boolean check() {
   2376                 return view.hasCalledOnDraw();
   2377             }
   2378         }.run();
   2379 
   2380         view.reset();
   2381         runTestOnUiThread(new Runnable() {
   2382             public void run() {
   2383                 view.invalidateDrawable(d2);
   2384             }
   2385         });
   2386         getInstrumentation().waitForIdleSync();
   2387         assertFalse(view.hasCalledOnDraw());
   2388 
   2389         MockView viewTestNull = new MockView(mActivity);
   2390         try {
   2391             viewTestNull.invalidateDrawable(null);
   2392             fail("should throw NullPointerException");
   2393         } catch (NullPointerException e) {
   2394         }
   2395     }
   2396 
   2397     @UiThreadTest
   2398     public void testOnFocusChanged() {
   2399         MockView view = (MockView) mActivity.findViewById(R.id.mock_view);
   2400 
   2401         mActivity.findViewById(R.id.fit_windows).setFocusable(true);
   2402         view.setFocusable(true);
   2403         assertFalse(view.hasCalledOnFocusChanged());
   2404 
   2405         view.requestFocus();
   2406         assertTrue(view.hasCalledOnFocusChanged());
   2407 
   2408         view.reset();
   2409         view.clearFocus();
   2410         assertTrue(view.hasCalledOnFocusChanged());
   2411     }
   2412 
   2413     public void testDrawableState() {
   2414         MockView view = new MockView(mActivity);
   2415         view.setParent(mMockParent);
   2416 
   2417         assertFalse(view.hasCalledOnCreateDrawableState());
   2418         assertTrue(Arrays.equals(MockView.getEnabledStateSet(), view.getDrawableState()));
   2419         assertTrue(view.hasCalledOnCreateDrawableState());
   2420 
   2421         view.reset();
   2422         assertFalse(view.hasCalledOnCreateDrawableState());
   2423         assertTrue(Arrays.equals(MockView.getEnabledStateSet(), view.getDrawableState()));
   2424         assertFalse(view.hasCalledOnCreateDrawableState());
   2425 
   2426         view.reset();
   2427         assertFalse(view.hasCalledDrawableStateChanged());
   2428         view.setPressed(true);
   2429         assertTrue(view.hasCalledDrawableStateChanged());
   2430         assertFalse(view.hasCalledOnCreateDrawableState());
   2431         assertTrue(Arrays.equals(MockView.getPressedEnabledStateSet(), view.getDrawableState()));
   2432         assertTrue(view.hasCalledOnCreateDrawableState());
   2433 
   2434         view.reset();
   2435         mMockParent.reset();
   2436         assertFalse(view.hasCalledDrawableStateChanged());
   2437         assertFalse(mMockParent.hasChildDrawableStateChanged());
   2438         view.refreshDrawableState();
   2439         assertTrue(view.hasCalledDrawableStateChanged());
   2440         assertTrue(mMockParent.hasChildDrawableStateChanged());
   2441         assertFalse(view.hasCalledOnCreateDrawableState());
   2442         assertTrue(Arrays.equals(MockView.getPressedEnabledStateSet(), view.getDrawableState()));
   2443         assertTrue(view.hasCalledOnCreateDrawableState());
   2444     }
   2445 
   2446     public void testWindowFocusChanged() {
   2447         final MockView view = (MockView) mActivity.findViewById(R.id.mock_view);
   2448 
   2449         // Wait until the window has been focused.
   2450         new PollingCheck(TIMEOUT_DELTA) {
   2451             @Override
   2452             protected boolean check() {
   2453                 return view.hasWindowFocus();
   2454             }
   2455         }.run();
   2456 
   2457         new PollingCheck() {
   2458             protected boolean check() {
   2459                 return view.hasCalledOnWindowFocusChanged();
   2460             }
   2461         }.run();
   2462 
   2463         assertTrue(view.hasCalledOnWindowFocusChanged());
   2464         assertTrue(view.hasCalledDispatchWindowFocusChanged());
   2465 
   2466         view.reset();
   2467         assertFalse(view.hasCalledOnWindowFocusChanged());
   2468         assertFalse(view.hasCalledDispatchWindowFocusChanged());
   2469 
   2470         StubActivity activity = launchActivity("com.android.cts.stub", StubActivity.class, null);
   2471 
   2472         // Wait until the window lost focus.
   2473         new PollingCheck(TIMEOUT_DELTA) {
   2474             @Override
   2475             protected boolean check() {
   2476                 return !view.hasWindowFocus();
   2477             }
   2478         }.run();
   2479 
   2480         assertTrue(view.hasCalledOnWindowFocusChanged());
   2481         assertTrue(view.hasCalledDispatchWindowFocusChanged());
   2482 
   2483         activity.finish();
   2484     }
   2485 
   2486     public void testDraw() throws Throwable {
   2487         final MockView view = (MockView) mActivity.findViewById(R.id.mock_view);
   2488         runTestOnUiThread(new Runnable() {
   2489             public void run() {
   2490                 view.requestLayout();
   2491             }
   2492         });
   2493         getInstrumentation().waitForIdleSync();
   2494 
   2495         assertTrue(view.hasCalledOnDraw());
   2496         assertTrue(view.hasCalledDispatchDraw());
   2497     }
   2498 
   2499     public void testRequestFocusFromTouch() {
   2500         View view = new View(mActivity);
   2501         view.setFocusable(true);
   2502         assertFalse(view.isFocused());
   2503 
   2504         view.requestFocusFromTouch();
   2505         assertTrue(view.isFocused());
   2506 
   2507         view.requestFocusFromTouch();
   2508         assertTrue(view.isFocused());
   2509     }
   2510 
   2511     public void testRequestRectangleOnScreen1() {
   2512         MockView view = new MockView(mActivity);
   2513         Rect rectangle = new Rect(10, 10, 20, 30);
   2514         MockViewGroupParent parent = new MockViewGroupParent(mActivity);
   2515 
   2516         // parent is null
   2517         assertFalse(view.requestRectangleOnScreen(rectangle, true));
   2518         assertFalse(view.requestRectangleOnScreen(rectangle, false));
   2519         assertFalse(view.requestRectangleOnScreen(null, true));
   2520 
   2521         view.setParent(parent);
   2522         view.scrollTo(1, 2);
   2523         assertFalse(parent.hasRequestChildRectangleOnScreen());
   2524 
   2525         assertFalse(view.requestRectangleOnScreen(rectangle, true));
   2526         assertTrue(parent.hasRequestChildRectangleOnScreen());
   2527 
   2528         parent.reset();
   2529         view.scrollTo(11, 22);
   2530         assertFalse(parent.hasRequestChildRectangleOnScreen());
   2531 
   2532         assertFalse(view.requestRectangleOnScreen(rectangle, true));
   2533         assertTrue(parent.hasRequestChildRectangleOnScreen());
   2534 
   2535         try {
   2536             view.requestRectangleOnScreen(null, true);
   2537             fail("should throw NullPointerException");
   2538         } catch (NullPointerException e) {
   2539         }
   2540     }
   2541 
   2542     public void testRequestRectangleOnScreen2() {
   2543         MockView view = new MockView(mActivity);
   2544         Rect rectangle = new Rect();
   2545         MockViewGroupParent parent = new MockViewGroupParent(mActivity);
   2546 
   2547         final Rect requestedRect = new Rect();
   2548         MockViewGroupParent grandparent = new MockViewGroupParent(mActivity) {
   2549             @Override
   2550             public boolean requestChildRectangleOnScreen(View child, Rect rectangle,
   2551                     boolean immediate) {
   2552                 requestedRect.set(rectangle);
   2553                 return super.requestChildRectangleOnScreen(child, rectangle, immediate);
   2554             }
   2555         };
   2556 
   2557         // parent is null
   2558         assertFalse(view.requestRectangleOnScreen(rectangle));
   2559         assertFalse(view.requestRectangleOnScreen(null));
   2560         assertEquals(0, rectangle.left);
   2561         assertEquals(0, rectangle.top);
   2562         assertEquals(0, rectangle.right);
   2563         assertEquals(0, rectangle.bottom);
   2564 
   2565         parent.addView(view);
   2566         parent.scrollTo(1, 2);
   2567         grandparent.addView(parent);
   2568 
   2569         assertFalse(parent.hasRequestChildRectangleOnScreen());
   2570         assertFalse(grandparent.hasRequestChildRectangleOnScreen());
   2571 
   2572         assertFalse(view.requestRectangleOnScreen(rectangle));
   2573 
   2574         assertTrue(parent.hasRequestChildRectangleOnScreen());
   2575         assertTrue(grandparent.hasRequestChildRectangleOnScreen());
   2576 
   2577         assertEquals(-1, requestedRect.left);
   2578         assertEquals(-2, requestedRect.top);
   2579         assertEquals(-1, requestedRect.right);
   2580         assertEquals(-2, requestedRect.bottom);
   2581 
   2582         try {
   2583             view.requestRectangleOnScreen(null);
   2584             fail("should throw NullPointerException");
   2585         } catch (NullPointerException e) {
   2586         }
   2587     }
   2588 
   2589     /**
   2590      * For the duration of the tap timeout we are in a 'prepressed' state
   2591      * to differentiate between taps and touch scrolls.
   2592      * Wait at least this long before testing if the view is pressed
   2593      * by calling this function.
   2594      */
   2595     private void waitPrepressedTimeout() {
   2596         try {
   2597             Thread.sleep(ViewConfiguration.getTapTimeout() + 10);
   2598         } catch (InterruptedException e) {
   2599             Log.e(LOG_TAG, "waitPrepressedTimeout() interrupted! Test may fail!", e);
   2600         }
   2601         getInstrumentation().waitForIdleSync();
   2602     }
   2603 
   2604     public void testOnTouchEvent() throws Throwable {
   2605         final MockView view = (MockView) mActivity.findViewById(R.id.mock_view);
   2606 
   2607         assertTrue(view.isEnabled());
   2608         assertFalse(view.isClickable());
   2609         assertFalse(view.isLongClickable());
   2610 
   2611         TouchUtils.clickView(this, view);
   2612         assertTrue(view.hasCalledOnTouchEvent());
   2613 
   2614         runTestOnUiThread(new Runnable() {
   2615             public void run() {
   2616                 view.setEnabled(true);
   2617                 view.setClickable(true);
   2618                 view.setLongClickable(true);
   2619             }
   2620         });
   2621         getInstrumentation().waitForIdleSync();
   2622         assertTrue(view.isEnabled());
   2623         assertTrue(view.isClickable());
   2624         assertTrue(view.isLongClickable());
   2625 
   2626         // MotionEvent.ACTION_DOWN
   2627         int[] xy = new int[2];
   2628         view.getLocationOnScreen(xy);
   2629 
   2630         final int viewWidth = view.getWidth();
   2631         final int viewHeight = view.getHeight();
   2632         float x = xy[0] + viewWidth / 2.0f;
   2633         float y = xy[1] + viewHeight / 2.0f;
   2634 
   2635         long downTime = SystemClock.uptimeMillis();
   2636         long eventTime = SystemClock.uptimeMillis();
   2637         MotionEvent event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN,
   2638                 x, y, 0);
   2639         assertFalse(view.isPressed());
   2640         getInstrumentation().sendPointerSync(event);
   2641         waitPrepressedTimeout();
   2642         assertTrue(view.hasCalledOnTouchEvent());
   2643         assertTrue(view.isPressed());
   2644 
   2645         // MotionEvent.ACTION_MOVE
   2646         // move out of the bound.
   2647         view.reset();
   2648         downTime = SystemClock.uptimeMillis();
   2649         eventTime = SystemClock.uptimeMillis();
   2650         int slop = ViewConfiguration.get(mActivity).getScaledTouchSlop();
   2651         x = xy[0] + viewWidth + slop;
   2652         y = xy[1] + viewHeight + slop;
   2653         event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE, x, y, 0);
   2654         getInstrumentation().sendPointerSync(event);
   2655         assertTrue(view.hasCalledOnTouchEvent());
   2656         assertFalse(view.isPressed());
   2657 
   2658         // move into view
   2659         view.reset();
   2660         downTime = SystemClock.uptimeMillis();
   2661         eventTime = SystemClock.uptimeMillis();
   2662         x = xy[0] + viewWidth - 1;
   2663         y = xy[1] + viewHeight - 1;
   2664         event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE, x, y, 0);
   2665         getInstrumentation().sendPointerSync(event);
   2666         waitPrepressedTimeout();
   2667         assertTrue(view.hasCalledOnTouchEvent());
   2668         assertFalse(view.isPressed());
   2669 
   2670         // MotionEvent.ACTION_UP
   2671         OnClickListenerImpl listener = new OnClickListenerImpl();
   2672         view.setOnClickListener(listener);
   2673         view.reset();
   2674         downTime = SystemClock.uptimeMillis();
   2675         eventTime = SystemClock.uptimeMillis();
   2676         event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, x, y, 0);
   2677         getInstrumentation().sendPointerSync(event);
   2678         assertTrue(view.hasCalledOnTouchEvent());
   2679         assertFalse(listener.hasOnClick());
   2680 
   2681         view.reset();
   2682         x = xy[0] + viewWidth / 2.0f;
   2683         y = xy[1] + viewHeight / 2.0f;
   2684         downTime = SystemClock.uptimeMillis();
   2685         eventTime = SystemClock.uptimeMillis();
   2686         event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, x, y, 0);
   2687         getInstrumentation().sendPointerSync(event);
   2688         assertTrue(view.hasCalledOnTouchEvent());
   2689 
   2690         // MotionEvent.ACTION_CANCEL
   2691         view.reset();
   2692         listener.reset();
   2693         downTime = SystemClock.uptimeMillis();
   2694         eventTime = SystemClock.uptimeMillis();
   2695         event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_CANCEL, x, y, 0);
   2696         getInstrumentation().sendPointerSync(event);
   2697         assertTrue(view.hasCalledOnTouchEvent());
   2698         assertFalse(view.isPressed());
   2699         assertFalse(listener.hasOnClick());
   2700     }
   2701 
   2702     public void testBringToFront() {
   2703         MockView view = new MockView(mActivity);
   2704         view.setParent(mMockParent);
   2705 
   2706         assertFalse(mMockParent.hasBroughtChildToFront());
   2707         view.bringToFront();
   2708         assertTrue(mMockParent.hasBroughtChildToFront());
   2709     }
   2710 
   2711     public void testGetApplicationWindowToken() {
   2712         View view = new View(mActivity);
   2713         // mAttachInfo is null
   2714         assertNull(view.getApplicationWindowToken());
   2715 
   2716         // mAttachInfo is not null
   2717         view = mActivity.findViewById(R.id.fit_windows);
   2718         assertNotNull(view.getApplicationWindowToken());
   2719     }
   2720 
   2721     public void testGetBottomPaddingOffset() {
   2722         MockView view = new MockView(mActivity);
   2723         assertEquals(0, view.getBottomPaddingOffset());
   2724     }
   2725 
   2726     public void testGetLeftPaddingOffset() {
   2727         MockView view = new MockView(mActivity);
   2728         assertEquals(0, view.getLeftPaddingOffset());
   2729     }
   2730 
   2731     public void testGetRightPaddingOffset() {
   2732         MockView view = new MockView(mActivity);
   2733         assertEquals(0, view.getRightPaddingOffset());
   2734     }
   2735 
   2736     public void testGetTopPaddingOffset() {
   2737         MockView view = new MockView(mActivity);
   2738         assertEquals(0, view.getTopPaddingOffset());
   2739     }
   2740 
   2741     public void testIsPaddingOffsetRequired() {
   2742         MockView view = new MockView(mActivity);
   2743         assertFalse(view.isPaddingOffsetRequired());
   2744     }
   2745 
   2746     public void testGetWindowVisibleDisplayFrame() {
   2747         Rect outRect = new Rect();
   2748         View view = new View(mActivity);
   2749         // mAttachInfo is null
   2750         WindowManager wm = (WindowManager)mActivity.getSystemService(Context.WINDOW_SERVICE);
   2751         Display d = wm.getDefaultDisplay();
   2752         view.getWindowVisibleDisplayFrame(outRect);
   2753         assertEquals(0, outRect.left);
   2754         assertEquals(0, outRect.top);
   2755         assertEquals(d.getWidth(), outRect.right);
   2756         assertEquals(d.getHeight(), outRect.bottom);
   2757 
   2758         // mAttachInfo is not null
   2759         outRect = new Rect();
   2760         view = mActivity.findViewById(R.id.fit_windows);
   2761         // it's implementation detail
   2762         view.getWindowVisibleDisplayFrame(outRect);
   2763     }
   2764 
   2765     public void testSetScrollContainer() throws Throwable {
   2766         final MockView mockView = (MockView) mActivity.findViewById(R.id.mock_view);
   2767         final MockView scrollView = (MockView) mActivity.findViewById(R.id.scroll_view);
   2768         Bitmap bitmap = Bitmap.createBitmap(200, 300, Bitmap.Config.RGB_565);
   2769         final BitmapDrawable d = new BitmapDrawable(bitmap);
   2770         final InputMethodManager imm = InputMethodManager.getInstance(getActivity());
   2771         final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(300, 500);
   2772         runTestOnUiThread(new Runnable() {
   2773             public void run() {
   2774                 mockView.setBackgroundDrawable(d);
   2775                 mockView.setHorizontalFadingEdgeEnabled(true);
   2776                 mockView.setVerticalFadingEdgeEnabled(true);
   2777                 mockView.setLayoutParams(layoutParams);
   2778                 scrollView.setLayoutParams(layoutParams);
   2779 
   2780                 mockView.setFocusable(true);
   2781                 mockView.requestFocus();
   2782                 mockView.setScrollContainer(true);
   2783                 scrollView.setScrollContainer(false);
   2784                 imm.showSoftInput(mockView, 0);
   2785             }
   2786         });
   2787         getInstrumentation().waitForIdleSync();
   2788 
   2789         // FIXME: why the size of view doesn't change?
   2790 
   2791         runTestOnUiThread(new Runnable() {
   2792             public void run() {
   2793                 imm.hideSoftInputFromInputMethod(mockView.getWindowToken(), 0);
   2794             }
   2795         });
   2796         getInstrumentation().waitForIdleSync();
   2797     }
   2798 
   2799     public void testTouchMode() throws Throwable {
   2800         final MockView mockView = (MockView) mActivity.findViewById(R.id.mock_view);
   2801         final View fitWindowsView = mActivity.findViewById(R.id.fit_windows);
   2802         runTestOnUiThread(new Runnable() {
   2803             public void run() {
   2804                 mockView.setFocusableInTouchMode(true);
   2805                 fitWindowsView.setFocusable(true);
   2806                 fitWindowsView.requestFocus();
   2807             }
   2808         });
   2809         getInstrumentation().waitForIdleSync();
   2810         assertTrue(mockView.isFocusableInTouchMode());
   2811         assertFalse(fitWindowsView.isFocusableInTouchMode());
   2812         assertTrue(mockView.isFocusable());
   2813         assertTrue(fitWindowsView.isFocusable());
   2814         assertFalse(mockView.isFocused());
   2815         assertTrue(fitWindowsView.isFocused());
   2816         assertFalse(mockView.isInTouchMode());
   2817         assertFalse(fitWindowsView.isInTouchMode());
   2818 
   2819         TouchUtils.tapView(this, mockView);
   2820         assertFalse(fitWindowsView.isFocused());
   2821         assertFalse(mockView.isFocused());
   2822         runTestOnUiThread(new Runnable() {
   2823             public void run() {
   2824                 mockView.requestFocus();
   2825             }
   2826         });
   2827         getInstrumentation().waitForIdleSync();
   2828         assertTrue(mockView.isFocused());
   2829         runTestOnUiThread(new Runnable() {
   2830             public void run() {
   2831                 fitWindowsView.requestFocus();
   2832             }
   2833         });
   2834         getInstrumentation().waitForIdleSync();
   2835         assertFalse(fitWindowsView.isFocused());
   2836         assertTrue(mockView.isInTouchMode());
   2837         assertTrue(fitWindowsView.isInTouchMode());
   2838 
   2839         KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_0);
   2840         getInstrumentation().sendKeySync(keyEvent);
   2841         assertTrue(mockView.isFocused());
   2842         assertFalse(fitWindowsView.isFocused());
   2843         runTestOnUiThread(new Runnable() {
   2844             public void run() {
   2845                 fitWindowsView.requestFocus();
   2846             }
   2847         });
   2848         getInstrumentation().waitForIdleSync();
   2849         assertFalse(mockView.isFocused());
   2850         assertTrue(fitWindowsView.isFocused());
   2851         assertFalse(mockView.isInTouchMode());
   2852         assertFalse(fitWindowsView.isInTouchMode());
   2853     }
   2854 
   2855     @UiThreadTest
   2856     public void testScrollbarStyle() {
   2857         MockView view = (MockView) mActivity.findViewById(R.id.mock_view);
   2858         Bitmap bitmap = Bitmap.createBitmap(200, 300, Bitmap.Config.RGB_565);
   2859         BitmapDrawable d = new BitmapDrawable(bitmap);
   2860         view.setBackgroundDrawable(d);
   2861         view.setHorizontalFadingEdgeEnabled(true);
   2862         view.setVerticalFadingEdgeEnabled(true);
   2863 
   2864         view.setHorizontalScrollBarEnabled(true);
   2865         view.setVerticalScrollBarEnabled(true);
   2866         view.initializeScrollbars(mActivity.obtainStyledAttributes(android.R.styleable.View));
   2867         assertTrue(view.isHorizontalScrollBarEnabled());
   2868         assertTrue(view.isVerticalScrollBarEnabled());
   2869         int verticalScrollBarWidth = view.getVerticalScrollbarWidth();
   2870         int horizontalScrollBarHeight = view.getHorizontalScrollbarHeight();
   2871         assertTrue(verticalScrollBarWidth > 0);
   2872         assertTrue(horizontalScrollBarHeight > 0);
   2873         assertEquals(0, view.getPaddingRight());
   2874         assertEquals(0, view.getPaddingBottom());
   2875 
   2876         view.setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET);
   2877         assertEquals(View.SCROLLBARS_INSIDE_INSET, view.getScrollBarStyle());
   2878         assertEquals(verticalScrollBarWidth, view.getPaddingRight());
   2879         assertEquals(horizontalScrollBarHeight, view.getPaddingBottom());
   2880 
   2881         view.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
   2882         assertEquals(View.SCROLLBARS_OUTSIDE_OVERLAY, view.getScrollBarStyle());
   2883         assertEquals(0, view.getPaddingRight());
   2884         assertEquals(0, view.getPaddingBottom());
   2885 
   2886         view.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_INSET);
   2887         assertEquals(View.SCROLLBARS_OUTSIDE_INSET, view.getScrollBarStyle());
   2888         assertEquals(verticalScrollBarWidth, view.getPaddingRight());
   2889         assertEquals(horizontalScrollBarHeight, view.getPaddingBottom());
   2890 
   2891         // TODO: how to get the position of the Scrollbar to assert it is inside or outside.
   2892     }
   2893 
   2894     @UiThreadTest
   2895     public void testScrollFading() {
   2896         MockView view = (MockView) mActivity.findViewById(R.id.mock_view);
   2897         Bitmap bitmap = Bitmap.createBitmap(200, 300, Bitmap.Config.RGB_565);
   2898         BitmapDrawable d = new BitmapDrawable(bitmap);
   2899         view.setBackgroundDrawable(d);
   2900 
   2901         assertFalse(view.isHorizontalFadingEdgeEnabled());
   2902         assertFalse(view.isVerticalFadingEdgeEnabled());
   2903         assertEquals(0, view.getHorizontalFadingEdgeLength());
   2904         assertEquals(0, view.getVerticalFadingEdgeLength());
   2905 
   2906         view.setHorizontalFadingEdgeEnabled(true);
   2907         view.setVerticalFadingEdgeEnabled(true);
   2908         assertTrue(view.isHorizontalFadingEdgeEnabled());
   2909         assertTrue(view.isVerticalFadingEdgeEnabled());
   2910         assertTrue(view.getHorizontalFadingEdgeLength() > 0);
   2911         assertTrue(view.getVerticalFadingEdgeLength() > 0);
   2912 
   2913         final int fadingLength = 20;
   2914         view.setFadingEdgeLength(fadingLength);
   2915         assertEquals(fadingLength, view.getHorizontalFadingEdgeLength());
   2916         assertEquals(fadingLength, view.getVerticalFadingEdgeLength());
   2917     }
   2918 
   2919     @UiThreadTest
   2920     public void testScrolling() {
   2921         MockView view = (MockView) mActivity.findViewById(R.id.mock_view);
   2922         view.reset();
   2923         assertEquals(0, view.getScrollX());
   2924         assertEquals(0, view.getScrollY());
   2925         assertFalse(view.hasCalledOnScrollChanged());
   2926 
   2927         view.scrollTo(0, 0);
   2928         assertEquals(0, view.getScrollX());
   2929         assertEquals(0, view.getScrollY());
   2930         assertFalse(view.hasCalledOnScrollChanged());
   2931 
   2932         view.scrollBy(0, 0);
   2933         assertEquals(0, view.getScrollX());
   2934         assertEquals(0, view.getScrollY());
   2935         assertFalse(view.hasCalledOnScrollChanged());
   2936 
   2937         view.scrollTo(10, 100);
   2938         assertEquals(10, view.getScrollX());
   2939         assertEquals(100, view.getScrollY());
   2940         assertTrue(view.hasCalledOnScrollChanged());
   2941 
   2942         view.reset();
   2943         assertFalse(view.hasCalledOnScrollChanged());
   2944         view.scrollBy(-10, -100);
   2945         assertEquals(0, view.getScrollX());
   2946         assertEquals(0, view.getScrollY());
   2947         assertTrue(view.hasCalledOnScrollChanged());
   2948 
   2949         view.reset();
   2950         assertFalse(view.hasCalledOnScrollChanged());
   2951         view.scrollTo(-1, -2);
   2952         assertEquals(-1, view.getScrollX());
   2953         assertEquals(-2, view.getScrollY());
   2954         assertTrue(view.hasCalledOnScrollChanged());
   2955     }
   2956 
   2957     public void testInitializeScrollbarsAndFadingEdge() {
   2958         MockView view = (MockView) mActivity.findViewById(R.id.scroll_view);
   2959 
   2960         assertTrue(view.isHorizontalScrollBarEnabled());
   2961         assertTrue(view.isVerticalScrollBarEnabled());
   2962         assertFalse(view.isHorizontalFadingEdgeEnabled());
   2963         assertFalse(view.isVerticalFadingEdgeEnabled());
   2964 
   2965         view = (MockView) mActivity.findViewById(R.id.scroll_view_2);
   2966         final int fadingEdgeLength = 20;
   2967 
   2968         assertTrue(view.isHorizontalScrollBarEnabled());
   2969         assertTrue(view.isVerticalScrollBarEnabled());
   2970         assertTrue(view.isHorizontalFadingEdgeEnabled());
   2971         assertTrue(view.isVerticalFadingEdgeEnabled());
   2972         assertEquals(fadingEdgeLength, view.getHorizontalFadingEdgeLength());
   2973         assertEquals(fadingEdgeLength, view.getVerticalFadingEdgeLength());
   2974     }
   2975 
   2976     public void testOnStartAndFinishTemporaryDetach() throws Throwable {
   2977         final MockListView listView = new MockListView(mActivity);
   2978         List<String> items = Lists.newArrayList("1", "2", "3");
   2979         final Adapter<String> adapter = new Adapter<String>(mActivity, 0, items);
   2980 
   2981         runTestOnUiThread(new Runnable() {
   2982             public void run() {
   2983                 mActivity.setContentView(listView);
   2984                 listView.setAdapter(adapter);
   2985             }
   2986         });
   2987         getInstrumentation().waitForIdleSync();
   2988         final MockView focusChild = (MockView) listView.getChildAt(0);
   2989 
   2990         runTestOnUiThread(new Runnable() {
   2991             public void run() {
   2992                 focusChild.requestFocus();
   2993             }
   2994         });
   2995         getInstrumentation().waitForIdleSync();
   2996         assertTrue(listView.getChildAt(0).isFocused());
   2997 
   2998         runTestOnUiThread(new Runnable() {
   2999             public void run() {
   3000                 listView.detachViewFromParent(focusChild);
   3001             }
   3002         });
   3003         getInstrumentation().waitForIdleSync();
   3004         assertFalse(listView.hasCalledOnStartTemporaryDetach());
   3005         assertFalse(listView.hasCalledOnFinishTemporaryDetach());
   3006     }
   3007 
   3008     private static class MockListView extends ListView {
   3009         private boolean mCalledOnStartTemporaryDetach = false;
   3010         private boolean mCalledOnFinishTemporaryDetach = false;
   3011 
   3012         public MockListView(Context context) {
   3013             super(context);
   3014         }
   3015 
   3016         @Override
   3017         protected void detachViewFromParent(View child) {
   3018             super.detachViewFromParent(child);
   3019         }
   3020 
   3021         @Override
   3022         public void onFinishTemporaryDetach() {
   3023             super.onFinishTemporaryDetach();
   3024             mCalledOnFinishTemporaryDetach = true;
   3025         }
   3026 
   3027         public boolean hasCalledOnFinishTemporaryDetach() {
   3028             return mCalledOnFinishTemporaryDetach;
   3029         }
   3030 
   3031         @Override
   3032         public void onStartTemporaryDetach() {
   3033             super.onStartTemporaryDetach();
   3034             mCalledOnStartTemporaryDetach = true;
   3035         }
   3036 
   3037         public boolean hasCalledOnStartTemporaryDetach() {
   3038             return mCalledOnStartTemporaryDetach;
   3039         }
   3040 
   3041         public void reset() {
   3042             mCalledOnStartTemporaryDetach = false;
   3043             mCalledOnFinishTemporaryDetach = false;
   3044         }
   3045     }
   3046 
   3047     private static class Adapter<T> extends ArrayAdapter<T> {
   3048         ArrayList<MockView> views = new ArrayList<MockView>();
   3049 
   3050         public Adapter(Context context, int textViewResourceId, List<T> objects) {
   3051             super(context, textViewResourceId, objects);
   3052             for (int i = 0; i < objects.size(); i++) {
   3053                 views.add(new MockView(context));
   3054                 views.get(i).setFocusable(true);
   3055             }
   3056         }
   3057 
   3058         @Override
   3059         public int getCount() {
   3060             return views.size();
   3061         }
   3062 
   3063         @Override
   3064         public long getItemId(int position) {
   3065             return position;
   3066         }
   3067 
   3068         @Override
   3069         public View getView(int position, View convertView, ViewGroup parent) {
   3070             return views.get(position);
   3071         }
   3072     }
   3073 
   3074     public void testKeyPreIme() throws Throwable {
   3075         final MockView view = (MockView) mActivity.findViewById(R.id.mock_view);
   3076 
   3077         runTestOnUiThread(new Runnable() {
   3078             public void run() {
   3079                 view.setFocusable(true);
   3080                 view.requestFocus();
   3081             }
   3082         });
   3083         getInstrumentation().waitForIdleSync();
   3084 
   3085         getInstrumentation().sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
   3086         assertTrue(view.hasCalledDispatchKeyEventPreIme());
   3087         assertTrue(view.hasCalledOnKeyPreIme());
   3088     }
   3089 
   3090     public void testHapticFeedback() {
   3091         Vibrator vib = (Vibrator) mActivity.getSystemService(Context.VIBRATOR_SERVICE);
   3092         boolean hasVibrator = vib.hasVibrator();
   3093 
   3094         final MockView view = (MockView) mActivity.findViewById(R.id.mock_view);
   3095         final int LONG_PRESS = HapticFeedbackConstants.LONG_PRESS;
   3096         final int FLAG_IGNORE_VIEW_SETTING = HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING;
   3097         final int FLAG_IGNORE_GLOBAL_SETTING = HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING;
   3098         final int ALWAYS = FLAG_IGNORE_VIEW_SETTING | FLAG_IGNORE_GLOBAL_SETTING;
   3099 
   3100         view.setHapticFeedbackEnabled(false);
   3101         assertFalse(view.isHapticFeedbackEnabled());
   3102         assertFalse(view.performHapticFeedback(LONG_PRESS));
   3103         assertFalse(view.performHapticFeedback(LONG_PRESS, FLAG_IGNORE_GLOBAL_SETTING));
   3104         assertEquals(hasVibrator, view.performHapticFeedback(LONG_PRESS, ALWAYS));
   3105 
   3106         view.setHapticFeedbackEnabled(true);
   3107         assertTrue(view.isHapticFeedbackEnabled());
   3108         assertEquals(hasVibrator, view.performHapticFeedback(LONG_PRESS, FLAG_IGNORE_GLOBAL_SETTING));
   3109     }
   3110 
   3111     public void testInputConnection() throws Throwable {
   3112         final InputMethodManager imm = InputMethodManager.getInstance(getActivity());
   3113         final MockView view = (MockView) mActivity.findViewById(R.id.mock_view);
   3114         final ViewGroup viewGroup = (ViewGroup) mActivity.findViewById(R.id.viewlayout_root);
   3115         final MockEditText editText = new MockEditText(mActivity);
   3116 
   3117         runTestOnUiThread(new Runnable() {
   3118             @Override
   3119             public void run() {
   3120                 viewGroup.addView(editText);
   3121                 editText.requestFocus();
   3122             }
   3123         });
   3124         getInstrumentation().waitForIdleSync();
   3125         assertTrue(editText.isFocused());
   3126 
   3127         runTestOnUiThread(new Runnable() {
   3128             @Override
   3129             public void run() {
   3130                 imm.showSoftInput(editText, 0);
   3131             }
   3132         });
   3133         getInstrumentation().waitForIdleSync();
   3134 
   3135         new PollingCheck(TIMEOUT_DELTA) {
   3136             @Override
   3137             protected boolean check() {
   3138                 return editText.hasCalledOnCreateInputConnection();
   3139             }
   3140         }.run();
   3141 
   3142         assertTrue(editText.hasCalledOnCheckIsTextEditor());
   3143 
   3144         runTestOnUiThread(new Runnable() {
   3145             @Override
   3146             public void run() {
   3147                 assertTrue(imm.isActive(editText));
   3148                 assertFalse(editText.hasCalledCheckInputConnectionProxy());
   3149                 imm.isActive(view);
   3150                 assertTrue(editText.hasCalledCheckInputConnectionProxy());
   3151             }
   3152         });
   3153     }
   3154 
   3155     private static class MockEditText extends EditText {
   3156         private boolean mCalledCheckInputConnectionProxy = false;
   3157         private boolean mCalledOnCreateInputConnection = false;
   3158         private boolean mCalledOnCheckIsTextEditor = false;
   3159 
   3160         public MockEditText(Context context) {
   3161             super(context);
   3162         }
   3163 
   3164         @Override
   3165         public boolean checkInputConnectionProxy(View view) {
   3166             mCalledCheckInputConnectionProxy = true;
   3167             return super.checkInputConnectionProxy(view);
   3168         }
   3169 
   3170         public boolean hasCalledCheckInputConnectionProxy() {
   3171             return mCalledCheckInputConnectionProxy;
   3172         }
   3173 
   3174         @Override
   3175         public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
   3176             mCalledOnCreateInputConnection = true;
   3177             return super.onCreateInputConnection(outAttrs);
   3178         }
   3179 
   3180         public boolean hasCalledOnCreateInputConnection() {
   3181             return mCalledOnCreateInputConnection;
   3182         }
   3183 
   3184         @Override
   3185         public boolean onCheckIsTextEditor() {
   3186             mCalledOnCheckIsTextEditor = true;
   3187             return super.onCheckIsTextEditor();
   3188         }
   3189 
   3190         public boolean hasCalledOnCheckIsTextEditor() {
   3191             return mCalledOnCheckIsTextEditor;
   3192         }
   3193 
   3194         public void reset() {
   3195             mCalledCheckInputConnectionProxy = false;
   3196             mCalledOnCreateInputConnection = false;
   3197             mCalledOnCheckIsTextEditor = false;
   3198         }
   3199     }
   3200 
   3201     private final static class MockViewParent extends View implements ViewParent {
   3202         private boolean mHasClearChildFocus = false;
   3203         private boolean mHasRequestLayout = false;
   3204         private boolean mHasCreateContextMenu = false;
   3205         private boolean mHasShowContextMenuForChild = false;
   3206         private boolean mHasGetChildVisibleRect = false;
   3207         private boolean mHasInvalidateChild = false;
   3208         private boolean mHasOnCreateDrawableState = false;
   3209         private boolean mHasChildDrawableStateChanged = false;
   3210         private boolean mHasBroughtChildToFront = false;
   3211         private Rect mTempRect;
   3212 
   3213         private final static int[] DEFAULT_PARENT_STATE_SET = new int[] { 789 };
   3214 
   3215         public boolean requestChildRectangleOnScreen(View child, Rect rectangle,
   3216                 boolean immediate) {
   3217             return false;
   3218         }
   3219 
   3220         public MockViewParent(Context context) {
   3221             super(context);
   3222         }
   3223 
   3224         public void bringChildToFront(View child) {
   3225             mHasBroughtChildToFront = true;
   3226         }
   3227 
   3228         public boolean hasBroughtChildToFront() {
   3229             return mHasBroughtChildToFront;
   3230         }
   3231 
   3232         public void childDrawableStateChanged(View child) {
   3233             mHasChildDrawableStateChanged = true;
   3234         }
   3235 
   3236         public boolean hasChildDrawableStateChanged() {
   3237             return mHasChildDrawableStateChanged;
   3238         }
   3239 
   3240         @Override
   3241         protected void dispatchSetPressed(boolean pressed) {
   3242             super.dispatchSetPressed(pressed);
   3243         }
   3244 
   3245         @Override
   3246         protected void dispatchSetSelected(boolean selected) {
   3247             super.dispatchSetSelected(selected);
   3248         }
   3249 
   3250         public void clearChildFocus(View child) {
   3251             mHasClearChildFocus = true;
   3252         }
   3253 
   3254         public boolean hasClearChildFocus() {
   3255             return mHasClearChildFocus;
   3256         }
   3257 
   3258         public void createContextMenu(ContextMenu menu) {
   3259             mHasCreateContextMenu = true;
   3260         }
   3261 
   3262         public boolean hasCreateContextMenu() {
   3263             return mHasCreateContextMenu;
   3264         }
   3265 
   3266         public View focusSearch(View v, int direction) {
   3267             return v;
   3268         }
   3269 
   3270         public void focusableViewAvailable(View v) {
   3271 
   3272         }
   3273 
   3274         public boolean getChildVisibleRect(View child, Rect r, Point offset) {
   3275             mHasGetChildVisibleRect = true;
   3276             return false;
   3277         }
   3278 
   3279         public boolean hasGetChildVisibleRect() {
   3280             return mHasGetChildVisibleRect;
   3281         }
   3282 
   3283         public void invalidateChild(View child, Rect r) {
   3284             mTempRect = new Rect(r);
   3285             mHasInvalidateChild = true;
   3286         }
   3287 
   3288         public Rect getTempRect() {
   3289             return mTempRect;
   3290         }
   3291 
   3292         public boolean hasInvalidateChild() {
   3293             return mHasInvalidateChild;
   3294         }
   3295 
   3296         public ViewParent invalidateChildInParent(int[] location, Rect r) {
   3297             return null;
   3298         }
   3299 
   3300         public boolean isLayoutRequested() {
   3301             return false;
   3302         }
   3303 
   3304         public void recomputeViewAttributes(View child) {
   3305 
   3306         }
   3307 
   3308         public void requestChildFocus(View child, View focused) {
   3309 
   3310         }
   3311 
   3312         public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
   3313 
   3314         }
   3315 
   3316         public void requestLayout() {
   3317             mHasRequestLayout = true;
   3318         }
   3319 
   3320         public boolean hasRequestLayout() {
   3321             return mHasRequestLayout;
   3322         }
   3323 
   3324         public void requestTransparentRegion(View child) {
   3325 
   3326         }
   3327 
   3328         public boolean showContextMenuForChild(View originalView) {
   3329             mHasShowContextMenuForChild = true;
   3330             return false;
   3331         }
   3332 
   3333         public ActionMode startActionModeForChild(View originalView,
   3334                 ActionMode.Callback callback) {
   3335             return null;
   3336         }
   3337 
   3338         public boolean hasShowContextMenuForChild() {
   3339             return mHasShowContextMenuForChild;
   3340         }
   3341 
   3342         @Override
   3343         protected int[] onCreateDrawableState(int extraSpace) {
   3344             mHasOnCreateDrawableState = true;
   3345             return DEFAULT_PARENT_STATE_SET;
   3346         }
   3347 
   3348         public boolean requestSendAccessibilityEvent(View child, AccessibilityEvent event) {
   3349             return false;
   3350         }
   3351 
   3352         public static int[] getDefaultParentStateSet() {
   3353             return DEFAULT_PARENT_STATE_SET;
   3354         }
   3355 
   3356         public boolean hasOnCreateDrawableState() {
   3357             return mHasOnCreateDrawableState;
   3358         }
   3359 
   3360         public void reset() {
   3361             mHasClearChildFocus = false;
   3362             mHasRequestLayout = false;
   3363             mHasCreateContextMenu = false;
   3364             mHasShowContextMenuForChild = false;
   3365             mHasGetChildVisibleRect = false;
   3366             mHasInvalidateChild = false;
   3367             mHasOnCreateDrawableState = false;
   3368             mHasChildDrawableStateChanged = false;
   3369             mHasBroughtChildToFront = false;
   3370         }
   3371 
   3372         public void childOverlayStateChanged(View child) {
   3373 
   3374         }
   3375 
   3376         @Override
   3377         public void childHasTransientStateChanged(View child, boolean hasTransientState) {
   3378 
   3379         }
   3380 
   3381         @Override
   3382         public ViewParent getParentForAccessibility() {
   3383             return null;
   3384         }
   3385 
   3386         @Override
   3387         public void childAccessibilityStateChanged(View child) {
   3388 
   3389         }
   3390     }
   3391 
   3392     private final class OnCreateContextMenuListenerImpl implements OnCreateContextMenuListener {
   3393         private boolean mHasOnCreateContextMenu = false;
   3394 
   3395         public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
   3396             mHasOnCreateContextMenu = true;
   3397         }
   3398 
   3399         public boolean hasOnCreateContextMenu() {
   3400             return mHasOnCreateContextMenu;
   3401         }
   3402 
   3403         public void reset() {
   3404             mHasOnCreateContextMenu = false;
   3405         }
   3406     }
   3407 
   3408     private static class MockViewGroupParent extends ViewGroup implements ViewParent {
   3409         private boolean mHasRequestChildRectangleOnScreen = false;
   3410 
   3411         public MockViewGroupParent(Context context) {
   3412             super(context);
   3413         }
   3414 
   3415         @Override
   3416         protected void onLayout(boolean changed, int l, int t, int r, int b) {
   3417 
   3418         }
   3419 
   3420         @Override
   3421         public boolean requestChildRectangleOnScreen(View child,
   3422                 Rect rectangle, boolean immediate) {
   3423             mHasRequestChildRectangleOnScreen = true;
   3424             return super.requestChildRectangleOnScreen(child, rectangle, immediate);
   3425         }
   3426 
   3427         public boolean hasRequestChildRectangleOnScreen() {
   3428             return mHasRequestChildRectangleOnScreen;
   3429         }
   3430 
   3431         @Override
   3432         protected void detachViewFromParent(View child) {
   3433             super.detachViewFromParent(child);
   3434         }
   3435 
   3436         public void reset() {
   3437             mHasRequestChildRectangleOnScreen = false;
   3438         }
   3439     }
   3440 
   3441     private static final class OnClickListenerImpl implements OnClickListener {
   3442         private boolean mHasOnClick = false;
   3443 
   3444         public void onClick(View v) {
   3445             mHasOnClick = true;
   3446         }
   3447 
   3448         public boolean hasOnClick() {
   3449             return mHasOnClick;
   3450         }
   3451 
   3452         public void reset() {
   3453             mHasOnClick = false;
   3454         }
   3455     }
   3456 
   3457     private static final class OnLongClickListenerImpl implements OnLongClickListener {
   3458         private boolean mHasOnLongClick = false;
   3459 
   3460         public boolean hasOnLongClick() {
   3461             return mHasOnLongClick;
   3462         }
   3463 
   3464         public void reset() {
   3465             mHasOnLongClick = false;
   3466         }
   3467 
   3468         public boolean onLongClick(View v) {
   3469             mHasOnLongClick = true;
   3470             return true;
   3471         }
   3472     }
   3473 
   3474     private static final class OnFocusChangeListenerImpl implements OnFocusChangeListener {
   3475         private boolean mHasOnFocusChange = false;
   3476 
   3477         public void onFocusChange(View v, boolean hasFocus) {
   3478             mHasOnFocusChange = true;
   3479         }
   3480 
   3481         public boolean hasOnFocusChange() {
   3482             return mHasOnFocusChange;
   3483         }
   3484 
   3485         public void reset() {
   3486             mHasOnFocusChange = false;
   3487         }
   3488     }
   3489 
   3490     private static final class OnKeyListenerImpl implements OnKeyListener {
   3491         private boolean mHasOnKey = false;
   3492 
   3493         public boolean onKey(View v, int keyCode, KeyEvent event) {
   3494             mHasOnKey = true;
   3495             return true;
   3496         }
   3497 
   3498         public void reset() {
   3499             mHasOnKey = false;
   3500         }
   3501 
   3502         public boolean hasOnKey() {
   3503             return mHasOnKey;
   3504         }
   3505     }
   3506 
   3507     private static final class OnTouchListenerImpl implements OnTouchListener {
   3508         private boolean mHasOnTouch = false;
   3509 
   3510         public boolean onTouch(View v, MotionEvent event) {
   3511             mHasOnTouch = true;
   3512             return true;
   3513         }
   3514 
   3515         public void reset() {
   3516             mHasOnTouch = false;
   3517         }
   3518 
   3519         public boolean hasOnTouch() {
   3520             return mHasOnTouch;
   3521         }
   3522     }
   3523 
   3524     private static final class MockTouchDelegate extends TouchDelegate {
   3525         public MockTouchDelegate(Rect bounds, View delegateView) {
   3526             super(bounds, delegateView);
   3527         }
   3528 
   3529         private boolean mCalledOnTouchEvent = false;
   3530 
   3531         @Override
   3532         public boolean onTouchEvent(MotionEvent event) {
   3533             mCalledOnTouchEvent = true;
   3534             return super.onTouchEvent(event);
   3535         }
   3536 
   3537         public boolean hasCalledOnTouchEvent() {
   3538             return mCalledOnTouchEvent;
   3539         }
   3540 
   3541         public void reset() {
   3542             mCalledOnTouchEvent = false;
   3543         }
   3544     };
   3545 
   3546     private static final class ViewData {
   3547         public int childCount;
   3548         public String tag;
   3549         public View firstChild;
   3550     }
   3551 
   3552     private static final class MockRunnable implements Runnable {
   3553         public boolean hasRun = false;
   3554 
   3555         public void run() {
   3556             hasRun = true;
   3557         }
   3558     }
   3559 }
   3560