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