• 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 com.android.internal.util.XmlUtils;
20 
21 
22 import android.content.Context;
23 import android.content.Intent;
24 import android.cts.util.CTSResult;
25 import android.content.res.XmlResourceParser;
26 import android.graphics.Bitmap;
27 import android.graphics.Canvas;
28 import android.graphics.Point;
29 import android.graphics.Rect;
30 import android.graphics.Region;
31 import android.graphics.Bitmap.Config;
32 import android.graphics.drawable.BitmapDrawable;
33 import android.os.Parcelable;
34 import android.os.SystemClock;
35 import android.test.InstrumentationTestCase;
36 import android.util.AttributeSet;
37 import android.util.DisplayMetrics;
38 import android.util.SparseArray;
39 import android.view.Display;
40 import android.view.KeyEvent;
41 import android.view.MotionEvent;
42 import android.view.View;
43 import android.view.ViewGroup;
44 import android.view.WindowManager;
45 import android.view.View.BaseSavedState;
46 import android.view.View.MeasureSpec;
47 import android.view.View.OnTouchListener;
48 import android.view.ViewGroup.LayoutParams;
49 import android.view.ViewGroup.OnHierarchyChangeListener;
50 import android.view.animation.AlphaAnimation;
51 import android.view.animation.Animation;
52 import android.view.animation.LayoutAnimationController;
53 import android.view.animation.RotateAnimation;
54 import android.view.animation.Transformation;
55 import android.view.animation.Animation.AnimationListener;
56 import android.widget.TextView;
57 
58 import java.util.ArrayList;
59 
60 public class ViewGroupTest extends InstrumentationTestCase implements CTSResult{
61 
62     private Context mContext;
63     private MotionEvent mMotionEvent;
64     private int mResultCode;
65 
66     private Sync mSync = new Sync();
67     private static class Sync {
68         boolean mHasNotify;
69     }
70 
71     @Override
setUp()72     protected void setUp() throws Exception {
73         super.setUp();
74         mContext = getInstrumentation().getTargetContext();
75     }
76 
testConstructor()77     public void testConstructor() {
78         new MockViewGroup(mContext);
79         new MockViewGroup(mContext, null);
80         new MockViewGroup(mContext, null, 0);
81     }
82 
testAddFocusables()83     public void testAddFocusables() {
84         MockViewGroup vg = new MockViewGroup(mContext);
85         vg.setFocusable(true);
86 
87         ArrayList<View> list = new ArrayList<View>();
88         TextView textView = new TextView(mContext);
89         list.add(textView);
90         vg.addView(textView);
91         vg.addFocusables(list, 0);
92 
93         assertEquals(2, list.size());
94 
95         list = new ArrayList<View>();
96         list.add(textView);
97         vg.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
98         vg.setFocusable(false);
99         vg.addFocusables(list, 0);
100         assertEquals(1, list.size());
101     }
102 
testAddStatesFromChildren()103     public void testAddStatesFromChildren() {
104         MockViewGroup vg = new MockViewGroup(mContext);
105         TextView textView = new TextView(mContext);
106         vg.addView(textView);
107         assertFalse(vg.addStatesFromChildren());
108 
109         vg.setAddStatesFromChildren(true);
110         textView.performClick();
111         assertTrue(vg.addStatesFromChildren());
112         assertTrue(vg.isDrawableStateChangedCalled);
113     }
114 
testAddTouchables()115     public void testAddTouchables() {
116         MockViewGroup vg = new MockViewGroup(mContext);
117         vg.setFocusable(true);
118 
119         ArrayList<View> list = new ArrayList<View>();
120         TextView textView = new TextView(mContext);
121         textView.setVisibility(View.VISIBLE);
122         textView.setClickable(true);
123         textView.setEnabled(true);
124 
125         list.add(textView);
126         vg.addView(textView);
127         vg.addTouchables(list);
128 
129         assertEquals(2, list.size());
130 
131         View v = vg.getChildAt(0);
132         assertSame(textView, v);
133 
134         v = vg.getChildAt(-1);
135         assertNull(v);
136 
137         v = vg.getChildAt(1);
138         assertNull(v);
139 
140         v = vg.getChildAt(100);
141         assertNull(v);
142 
143         v = vg.getChildAt(-100);
144         assertNull(v);
145     }
146 
testAddView()147     public void testAddView() {
148         MockViewGroup vg = new MockViewGroup(mContext);
149         TextView textView = new TextView(mContext);
150 
151         assertEquals(0, vg.getChildCount());
152 
153         vg.addView(textView);
154         assertEquals(1, vg.getChildCount());
155     }
156 
testAddViewWithParaViewInt()157     public void testAddViewWithParaViewInt() {
158         MockViewGroup vg = new MockViewGroup(mContext);
159         TextView textView = new TextView(mContext);
160 
161         assertEquals(0, vg.getChildCount());
162 
163         vg.addView(textView, -1);
164         assertEquals(1, vg.getChildCount());
165     }
166 
testAddViewWithParaViewLayoutPara()167     public void testAddViewWithParaViewLayoutPara() {
168         MockViewGroup vg = new MockViewGroup(mContext);
169         TextView textView = new TextView(mContext);
170 
171         assertEquals(0, vg.getChildCount());
172 
173         vg.addView(textView, new ViewGroup.LayoutParams(100, 200));
174 
175         assertEquals(1, vg.getChildCount());
176     }
177 
testAddViewWithParaViewIntInt()178     public void testAddViewWithParaViewIntInt() {
179         final int width = 100;
180         final int height = 200;
181         MockViewGroup vg = new MockViewGroup(mContext);
182         TextView textView = new TextView(mContext);
183 
184         assertEquals(0, vg.getChildCount());
185 
186         vg.addView(textView, width, height);
187         assertEquals(width, textView.getLayoutParams().width);
188         assertEquals(height, textView.getLayoutParams().height);
189 
190         assertEquals(1, vg.getChildCount());
191     }
192 
testAddViewWidthParaViewIntLayoutParam()193     public void testAddViewWidthParaViewIntLayoutParam() {
194         MockViewGroup vg = new MockViewGroup(mContext);
195         TextView textView = new TextView(mContext);
196 
197         assertEquals(0, vg.getChildCount());
198 
199         vg.addView(textView, -1, new ViewGroup.LayoutParams(100, 200));
200 
201         assertEquals(1, vg.getChildCount());
202     }
203 
testAddViewInLayout()204     public void testAddViewInLayout() {
205         MockViewGroup vg = new MockViewGroup(mContext);
206         TextView textView = new TextView(mContext);
207 
208         assertEquals(0, vg.getChildCount());
209 
210         assertTrue(vg.isRequestLayoutCalled);
211         vg.isRequestLayoutCalled = false;
212         assertTrue(vg.addViewInLayout(textView, -1, new ViewGroup.LayoutParams(100, 200)));
213         assertEquals(1, vg.getChildCount());
214         // check that calling addViewInLayout() does not trigger a
215         // requestLayout() on this ViewGroup
216         assertFalse(vg.isRequestLayoutCalled);
217     }
218 
testAttachLayoutAnimationParameters()219     public void testAttachLayoutAnimationParameters() {
220         MockViewGroup vg = new MockViewGroup(mContext);
221         ViewGroup.LayoutParams param = new ViewGroup.LayoutParams(10, 10);
222 
223         vg.attachLayoutAnimationParameters(null, param, 1, 2);
224         assertEquals(2, param.layoutAnimationParameters.count);
225         assertEquals(1, param.layoutAnimationParameters.index);
226     }
227 
testAttachViewToParent()228     public void testAttachViewToParent() {
229         MockViewGroup vg = new MockViewGroup(mContext);
230         vg.setFocusable(true);
231         assertEquals(0, vg.getChildCount());
232 
233         ViewGroup.LayoutParams param = new ViewGroup.LayoutParams(10, 10);
234 
235         TextView child = new TextView(mContext);
236         child.setFocusable(true);
237         vg.attachViewToParent(child, -1, param);
238         assertSame(vg, child.getParent());
239         assertEquals(1, vg.getChildCount());
240         assertSame(child, vg.getChildAt(0));
241     }
242 
testAddViewInLayoutWithParamViewIntLayB()243     public void testAddViewInLayoutWithParamViewIntLayB() {
244         MockViewGroup vg = new MockViewGroup(mContext);
245         TextView textView = new TextView(mContext);
246 
247         assertEquals(0, vg.getChildCount());
248 
249         assertTrue(vg.isRequestLayoutCalled);
250         vg.isRequestLayoutCalled = false;
251         assertTrue(vg.addViewInLayout(textView, -1, new ViewGroup.LayoutParams(100, 200), true));
252 
253         assertEquals(1, vg.getChildCount());
254         // check that calling addViewInLayout() does not trigger a
255         // requestLayout() on this ViewGroup
256         assertFalse(vg.isRequestLayoutCalled);
257     }
258 
testBringChildToFront()259     public void testBringChildToFront() {
260         MockViewGroup vg = new MockViewGroup(mContext);
261         TextView textView1 = new TextView(mContext);
262         TextView textView2 = new TextView(mContext);
263 
264         assertEquals(0, vg.getChildCount());
265 
266         vg.addView(textView1);
267         vg.addView(textView2);
268         assertEquals(2, vg.getChildCount());
269 
270         vg.bringChildToFront(textView1);
271         assertEquals(vg, textView1.getParent());
272         assertEquals(2, vg.getChildCount());
273         assertNotNull(vg.getChildAt(0));
274         assertSame(textView2, vg.getChildAt(0));
275 
276         vg.bringChildToFront(textView2);
277         assertEquals(vg, textView2.getParent());
278         assertEquals(2, vg.getChildCount());
279         assertNotNull(vg.getChildAt(0));
280         assertSame(textView1, vg.getChildAt(0));
281     }
282 
testCanAnimate()283     public void testCanAnimate() {
284         MockViewGroup vg = new MockViewGroup(mContext);
285 
286         assertFalse(vg.canAnimate());
287 
288         RotateAnimation animation = new RotateAnimation(0.1f, 0.1f);
289         LayoutAnimationController la = new LayoutAnimationController(animation);
290         vg.setLayoutAnimation(la);
291         assertTrue(vg.canAnimate());
292     }
293 
testCheckLayoutParams()294     public void testCheckLayoutParams() {
295         MockViewGroup view = new MockViewGroup(mContext);
296         assertFalse(view.checkLayoutParams(null));
297 
298         assertTrue(view.checkLayoutParams(new ViewGroup.LayoutParams(100, 200)));
299     }
300 
testChildDrawableStateChanged()301     public void testChildDrawableStateChanged() {
302         MockViewGroup vg = new MockViewGroup(mContext);
303         vg.setAddStatesFromChildren(true);
304 
305         vg.childDrawableStateChanged(null);
306         assertTrue(vg.isRefreshDrawableStateCalled);
307     }
308 
testCleanupLayoutState()309     public void testCleanupLayoutState() {
310         MockViewGroup vg = new MockViewGroup(mContext);
311         TextView textView = new TextView(mContext);
312 
313         assertTrue(textView.isLayoutRequested());
314 
315         vg.cleanupLayoutState(textView);
316         assertFalse(textView.isLayoutRequested());
317     }
318 
testClearChildFocus()319     public void testClearChildFocus() {
320         MockViewGroup vg = new MockViewGroup(mContext);
321         TextView textView = new TextView(mContext);
322 
323         vg.addView(textView);
324         vg.requestChildFocus(textView, null);
325 
326         View focusedView = vg.getFocusedChild();
327         assertSame(textView, focusedView);
328 
329         vg.clearChildFocus(textView);
330         assertNull(vg.getFocusedChild());
331     }
332 
testClearDisappearingChildren()333     public void testClearDisappearingChildren() {
334 
335         Canvas canvas = new Canvas();
336         MockViewGroup vg = new MockViewGroup(mContext);
337         MockViewGroup son = new MockViewGroup(mContext);
338         son.setAnimation(new MockAnimation());
339         vg.addView(son);
340         assertEquals(1, vg.getChildCount());
341 
342         assertNotNull(son.getAnimation());
343         vg.dispatchDraw(canvas);
344         assertEquals(1, vg.drawChildCalledTime);
345 
346         son.setAnimation(new MockAnimation());
347         vg.removeAllViewsInLayout();
348 
349         vg.drawChildCalledTime = 0;
350         vg.dispatchDraw(canvas);
351         assertEquals(1, vg.drawChildCalledTime);
352 
353         son.setAnimation(new MockAnimation());
354         vg.clearDisappearingChildren();
355 
356         vg.drawChildCalledTime = 0;
357         vg.dispatchDraw(canvas);
358         assertEquals(0, vg.drawChildCalledTime);
359     }
360 
testClearFocus()361     public void testClearFocus() {
362         MockViewGroup vg = new MockViewGroup(mContext);
363         MockTextView textView = new MockTextView(mContext);
364 
365         vg.addView(textView);
366         vg.requestChildFocus(textView, null);
367         vg.clearFocus();
368         assertTrue(textView.isClearFocusCalled);
369     }
370 
testDetachAllViewsFromParent()371     public void testDetachAllViewsFromParent() {
372         MockViewGroup vg = new MockViewGroup(mContext);
373         TextView textView = new TextView(mContext);
374 
375         vg.addView(textView);
376         assertEquals(1, vg.getChildCount());
377         assertSame(vg, textView.getParent());
378         vg.detachAllViewsFromParent();
379         assertEquals(0, vg.getChildCount());
380         assertNull(textView.getParent());
381     }
382 
testDetachViewFromParent()383     public void testDetachViewFromParent() {
384         MockViewGroup vg = new MockViewGroup(mContext);
385         TextView textView = new TextView(mContext);
386 
387         vg.addView(textView);
388         assertEquals(1, vg.getChildCount());
389 
390         vg.detachViewFromParent(0);
391 
392         assertEquals(0, vg.getChildCount());
393         assertNull(textView.getParent());
394     }
395 
testDetachViewFromParentWithParamView()396     public void testDetachViewFromParentWithParamView() {
397         MockViewGroup vg = new MockViewGroup(mContext);
398         TextView textView = new TextView(mContext);
399 
400         vg.addView(textView);
401         assertEquals(1, vg.getChildCount());
402         assertSame(vg, textView.getParent());
403 
404         vg.detachViewFromParent(textView);
405 
406         assertEquals(0, vg.getChildCount());
407         assertNull(vg.getParent());
408     }
409 
testDetachViewsFromParent()410     public void testDetachViewsFromParent() {
411         MockViewGroup vg = new MockViewGroup(mContext);
412         TextView textView1 = new TextView(mContext);
413         TextView textView2 = new TextView(mContext);
414         TextView textView3 = new TextView(mContext);
415 
416         vg.addView(textView1);
417         vg.addView(textView2);
418         vg.addView(textView3);
419         assertEquals(3, vg.getChildCount());
420 
421         vg.detachViewsFromParent(0, 2);
422 
423         assertEquals(1, vg.getChildCount());
424         assertNull(textView1.getParent());
425         assertNull(textView2.getParent());
426     }
427 
testDispatchDraw()428     public void testDispatchDraw() {
429         MockViewGroup vg = new MockViewGroup(mContext);
430         Canvas canvas = new Canvas();
431 
432         vg.draw(canvas);
433         assertTrue(vg.isDispatchDrawCalled);
434         assertSame(canvas, vg.canvas);
435     }
436 
437     @SuppressWarnings("unchecked")
testDispatchFreezeSelfOnly()438     public void testDispatchFreezeSelfOnly() {
439         MockViewGroup vg = new MockViewGroup(mContext);
440         vg.setId(1);
441         vg.setSaveEnabled(true);
442 
443         SparseArray container = new SparseArray();
444         assertEquals(0, container.size());
445         vg.dispatchFreezeSelfOnly(container);
446         assertEquals(1, container.size());
447     }
448 
testDispatchKeyEvent()449     public void testDispatchKeyEvent() {
450         MockViewGroup vg = new MockViewGroup(mContext);
451         KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER);
452         assertFalse(vg.dispatchKeyEvent(event));
453 
454         MockTextView textView = new MockTextView(mContext);
455         vg.addView(textView);
456         vg.requestChildFocus(textView, null);
457         textView.setFrame(1, 1, 100, 100);
458 
459         assertTrue(vg.dispatchKeyEvent(event));
460     }
461 
462     @SuppressWarnings("unchecked")
testDispatchSaveInstanceState()463     public void testDispatchSaveInstanceState() {
464         MockViewGroup vg = new MockViewGroup(mContext);
465         vg.setId(2);
466         vg.setSaveEnabled(true);
467         MockTextView textView = new MockTextView(mContext);
468         textView.setSaveEnabled(true);
469         textView.setId(1);
470         vg.addView(textView);
471 
472         SparseArray array = new SparseArray();
473         vg.dispatchSaveInstanceState(array);
474 
475         assertTrue(array.size() > 0);
476         assertNotNull(array.get(2));
477 
478         array = new SparseArray();
479         vg.dispatchRestoreInstanceState(array);
480         assertTrue(textView.isDispatchRestoreInstanceStateCalled);
481     }
482 
testDispatchSetPressed()483     public void testDispatchSetPressed() {
484         MockViewGroup vg = new MockViewGroup(mContext);
485         MockTextView textView = new MockTextView(mContext);
486         vg.addView(textView);
487 
488         vg.dispatchSetPressed(true);
489         assertTrue(textView.isPressed());
490 
491         vg.dispatchSetPressed(false);
492         assertFalse(textView.isPressed());
493     }
494 
testDispatchSetSelected()495     public void testDispatchSetSelected() {
496         MockViewGroup vg = new MockViewGroup(mContext);
497         MockTextView textView = new MockTextView(mContext);
498         vg.addView(textView);
499 
500         vg.dispatchSetSelected(true);
501         assertTrue(textView.isSelected());
502 
503         vg.dispatchSetSelected(false);
504         assertFalse(textView.isSelected());
505     }
506 
507     @SuppressWarnings("unchecked")
testDispatchThawSelfOnly()508     public void testDispatchThawSelfOnly() {
509         MockViewGroup vg = new MockViewGroup(mContext);
510         vg.setId(1);
511         SparseArray array = new SparseArray();
512         array.put(1, BaseSavedState.EMPTY_STATE);
513 
514         vg.dispatchThawSelfOnly(array);
515         assertTrue(vg.isOnRestoreInstanceStateCalled);
516 
517     }
518 
testDispatchTouchEvent()519     public void testDispatchTouchEvent() {
520         MockViewGroup vg = new MockViewGroup(mContext);
521 
522         DisplayMetrics metrics = new DisplayMetrics();
523         WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
524         Display d = wm.getDefaultDisplay();
525         d.getMetrics(metrics);
526         int screenWidth = metrics.widthPixels;
527         int screenHeight = metrics.heightPixels;
528         vg.setFrame(0, 0, screenWidth, screenHeight);
529         vg.setLayoutParams(new ViewGroup.LayoutParams(screenWidth, screenHeight));
530 
531         MockTextView textView = new MockTextView(mContext);
532         mMotionEvent = null;
533         textView.setOnTouchListener(new OnTouchListener() {
534             public boolean onTouch(View v, MotionEvent event) {
535                 mMotionEvent = event;
536                 return true;
537             }
538         });
539 
540         textView.setVisibility(View.VISIBLE);
541         textView.setEnabled(true);
542 
543         vg.addView(textView, new LayoutParams(screenWidth, screenHeight));
544 
545         vg.requestDisallowInterceptTouchEvent(true);
546         MotionEvent me = MotionEvent.obtain(SystemClock.uptimeMillis(),
547                 SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN,
548                 screenWidth / 2, screenHeight / 2, 0);
549 
550         assertFalse(vg.dispatchTouchEvent(me));
551         assertNull(mMotionEvent);
552 
553         textView.setFrame(0, 0, screenWidth, screenHeight);
554         assertTrue(vg.dispatchTouchEvent(me));
555         assertSame(me, mMotionEvent);
556     }
557 
testDispatchTrackballEvent()558     public void testDispatchTrackballEvent() {
559         MockViewGroup vg = new MockViewGroup(mContext);
560         MotionEvent me = MotionEvent.obtain(SystemClock.uptimeMillis(),
561                 SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 100, 100,
562                 0);
563         assertFalse(vg.dispatchTrackballEvent(me));
564 
565         MockTextView textView = new MockTextView(mContext);
566         vg.addView(textView);
567         textView.setFrame(1, 1, 100, 100);
568         vg.requestChildFocus(textView, null);
569         assertTrue(vg.dispatchTrackballEvent(me));
570     }
571 
testDispatchUnhandledMove()572     public void testDispatchUnhandledMove() {
573         MockViewGroup vg = new MockViewGroup(mContext);
574         MockTextView textView = new MockTextView(mContext);
575         assertFalse(vg.dispatchUnhandledMove(textView, View.FOCUS_DOWN));
576 
577         vg.addView(textView);
578         textView.setFrame(1, 1, 100, 100);
579         vg.requestChildFocus(textView, null);
580         assertTrue(vg.dispatchUnhandledMove(textView, View.FOCUS_DOWN));
581     }
582 
testDispatchWindowFocusChanged()583     public void testDispatchWindowFocusChanged() {
584         MockViewGroup vg = new MockViewGroup(mContext);
585         MockTextView textView = new MockTextView(mContext);
586 
587         vg.addView(textView);
588         textView.setPressed(true);
589         assertTrue(textView.isPressed());
590 
591         vg.dispatchWindowFocusChanged(false);
592         assertFalse(textView.isPressed());
593     }
594 
testDispatchWindowVisibilityChanged()595     public void testDispatchWindowVisibilityChanged() {
596         int expected = 10;
597         MockViewGroup vg = new MockViewGroup(mContext);
598         MockTextView textView = new MockTextView(mContext);
599 
600         vg.addView(textView);
601         vg.dispatchWindowVisibilityChanged(expected);
602         assertEquals(expected, textView.visibility);
603     }
604 
testDrawableStateChanged()605     public void testDrawableStateChanged() {
606         MockViewGroup vg = new MockViewGroup(mContext);
607         MockTextView textView = new MockTextView(mContext);
608         textView.setDuplicateParentStateEnabled(true);
609 
610         vg.addView(textView);
611         vg.setAddStatesFromChildren(false);
612         vg.drawableStateChanged();
613         assertTrue(textView.mIsRefreshDrawableStateCalled);
614     }
615 
testDrawChild()616     public void testDrawChild() {
617         MockViewGroup vg = new MockViewGroup(mContext);
618         MockTextView textView = new MockTextView(mContext);
619         vg.addView(textView);
620 
621         MockCanvas canvas = new MockCanvas();
622         textView.setBackgroundDrawable(new BitmapDrawable(Bitmap.createBitmap(100, 100,
623                 Config.ALPHA_8)));
624         assertFalse(vg.drawChild(canvas, textView, 100));
625         // test whether child's draw method is called.
626         assertTrue(textView.isDrawCalled);
627     }
628 
testFindFocus()629     public void testFindFocus() {
630         MockViewGroup vg = new MockViewGroup(mContext);
631 
632         assertNull(vg.findFocus());
633         vg.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
634         vg.setFocusable(true);
635         vg.setVisibility(View.VISIBLE);
636         vg.setFocusableInTouchMode(true);
637         assertTrue(vg.requestFocus(1, new Rect()));
638 
639         assertSame(vg, vg.findFocus());
640     }
641 
testFitSystemWindows()642     public void testFitSystemWindows() {
643         Rect rect = new Rect(1, 1, 100, 100);
644         MockViewGroup vg = new MockViewGroup(mContext);
645         assertFalse(vg.fitSystemWindows(rect));
646 
647         vg = new MockViewGroup(mContext, null, 0);
648         MockView mv = new MockView(mContext);
649         vg.addView(mv);
650         assertTrue(vg.fitSystemWindows(rect));
651     }
652 
653     static class MockView extends ViewGroup {
654 
655         public int mWidthMeasureSpec;
656         public int mHeightMeasureSpec;
657 
MockView(Context context)658         public MockView(Context context) {
659             super(context);
660         }
661 
662         @Override
onLayout(boolean changed, int l, int t, int r, int b)663         public void onLayout(boolean changed, int l, int t, int r, int b) {
664         }
665 
666         @Override
fitSystemWindows(Rect insets)667         public boolean fitSystemWindows(Rect insets) {
668             return true;
669         }
670 
671         @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)672         public void onMeasure(int widthMeasureSpec,
673                 int heightMeasureSpec) {
674             mWidthMeasureSpec = widthMeasureSpec;
675             mHeightMeasureSpec = heightMeasureSpec;
676             super.onMeasure(widthMeasureSpec, heightMeasureSpec);
677         }
678     }
679 
testFocusableViewAvailable()680     public void testFocusableViewAvailable() {
681         MockViewGroup vg = new MockViewGroup(mContext);
682         MockView son = new MockView(mContext);
683         vg.addView(son);
684 
685         son.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
686         son.focusableViewAvailable(vg);
687 
688         assertTrue(vg.isFocusableViewAvailable);
689     }
690 
testFocusSearch()691     public void testFocusSearch() {
692         MockViewGroup vg = new MockViewGroup(mContext);
693         MockTextView textView = new MockTextView(mContext);
694         MockView son = new MockView(mContext);
695         vg.addView(son);
696         son.addView(textView);
697         assertNotNull(son.focusSearch(textView, 1));
698         assertSame(textView, son.focusSearch(textView, 1));
699     }
700 
testGatherTransparentRegion()701     public void testGatherTransparentRegion() {
702         Region region = new Region();
703         MockViewGroup vg = new MockViewGroup(mContext);
704         MockTextView textView = new MockTextView(mContext);
705         textView.setAnimation(new AlphaAnimation(mContext, null));
706         textView.setVisibility(100);
707         vg.addView(textView);
708         assertEquals(1, vg.getChildCount());
709 
710         assertTrue(vg.gatherTransparentRegion(region));
711         assertTrue(vg.gatherTransparentRegion(null));
712     }
713 
testGenerateDefaultLayoutParams()714     public void testGenerateDefaultLayoutParams(){
715         MockViewGroup vg = new MockViewGroup(mContext);
716         LayoutParams lp = vg.generateDefaultLayoutParams();
717 
718         assertEquals(LayoutParams.WRAP_CONTENT, lp.width);
719         assertEquals(LayoutParams.WRAP_CONTENT, lp.height);
720     }
721 
testGenerateLayoutParamsWithParaAttributeSet()722     public void testGenerateLayoutParamsWithParaAttributeSet() throws Exception{
723         MockViewGroup vg = new MockViewGroup(mContext);
724         XmlResourceParser set = mContext.getResources().getLayout(
725                 com.android.cts.view.R.layout.abslistview_layout);
726         XmlUtils.beginDocument(set, "ViewGroup_Layout");
727         LayoutParams lp = vg.generateLayoutParams(set);
728         assertNotNull(lp);
729         assertEquals(25, lp.height);
730         assertEquals(25, lp.width);
731     }
732 
testGenerateLayoutParams()733     public void testGenerateLayoutParams() {
734         MockViewGroup vg = new MockViewGroup(mContext);
735         LayoutParams p = new LayoutParams(LayoutParams.WRAP_CONTENT,
736                 LayoutParams.MATCH_PARENT);
737         assertSame(p, vg.generateLayoutParams(p));
738     }
739 
testGetChildDrawingOrder()740     public void testGetChildDrawingOrder() {
741         MockViewGroup vg = new MockViewGroup(mContext);
742         assertEquals(1, vg.getChildDrawingOrder(0, 1));
743         assertEquals(2, vg.getChildDrawingOrder(0, 2));
744     }
745 
testGetChildMeasureSpec()746     public void testGetChildMeasureSpec() {
747         int spec = 1;
748         int padding = 1;
749         int childDimension = 1;
750         assertEquals(MeasureSpec.makeMeasureSpec(childDimension, MeasureSpec.EXACTLY),
751                 ViewGroup.getChildMeasureSpec(spec, padding, childDimension));
752         spec = 4;
753         padding = 6;
754         childDimension = 9;
755         assertEquals(MeasureSpec.makeMeasureSpec(childDimension, MeasureSpec.EXACTLY),
756                 ViewGroup.getChildMeasureSpec(spec, padding, childDimension));
757     }
758 
testGetChildStaticTransformation()759     public void testGetChildStaticTransformation() {
760         MockViewGroup vg = new MockViewGroup(mContext);
761         assertFalse(vg.getChildStaticTransformation(null, null));
762     }
763 
testGetChildVisibleRect()764     public void testGetChildVisibleRect() {
765         MockViewGroup vg = new MockViewGroup(mContext);
766         MockTextView textView = new MockTextView(mContext);
767 
768         textView.setFrame(1, 1, 100, 100);
769         Rect rect = new Rect(1, 1, 50, 50);
770         Point p = new Point();
771         assertFalse(vg.getChildVisibleRect(textView, rect, p));
772 
773         textView.setFrame(0, 0, 0, 0);
774         vg.setFrame(20, 20, 60, 60);
775         rect = new Rect(10, 10, 40, 40);
776         p = new Point();
777         assertTrue(vg.getChildVisibleRect(textView, rect, p));
778     }
779 
testGetDescendantFocusability()780     public void testGetDescendantFocusability() {
781         MockViewGroup vg = new MockViewGroup(mContext);
782         final int FLAG_MASK_FOCUSABILITY = 0x60000;
783         assertFalse((vg.getDescendantFocusability() & FLAG_MASK_FOCUSABILITY) == 0);
784 
785         vg.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
786         assertFalse((vg.getDescendantFocusability() & FLAG_MASK_FOCUSABILITY) == 0);
787     }
788 
testGetLayoutAnimation()789     public void testGetLayoutAnimation() {
790         MockViewGroup vg = new MockViewGroup(mContext);
791 
792         assertNull(vg.getLayoutAnimation());
793         RotateAnimation animation = new RotateAnimation(0.1f, 0.1f);
794         LayoutAnimationController la = new LayoutAnimationController(animation);
795         vg.setLayoutAnimation(la);
796         assertTrue(vg.canAnimate());
797         assertSame(la, vg.getLayoutAnimation());
798     }
799 
testGetLayoutAnimationListener()800     public void testGetLayoutAnimationListener() {
801         MockViewGroup vg = new MockViewGroup(mContext);
802 
803         assertNull(vg.getLayoutAnimationListener());
804 
805         AnimationListener al = new AnimationListener() {
806 
807             public void onAnimationEnd(Animation animation) {
808             }
809 
810             public void onAnimationRepeat(Animation animation) {
811             }
812 
813             public void onAnimationStart(Animation animation) {
814             }
815         };
816         vg.setLayoutAnimationListener(al);
817         assertSame(al, vg.getLayoutAnimationListener());
818     }
819 
testGetPersistentDrawingCache()820     public void testGetPersistentDrawingCache() {
821         MockViewGroup vg = new MockViewGroup(mContext);
822         final int mPersistentDrawingCache1 = 2;
823         final int mPersistentDrawingCache2 = 3;
824         assertEquals(mPersistentDrawingCache1, vg.getPersistentDrawingCache());
825 
826         vg.setPersistentDrawingCache(mPersistentDrawingCache2);
827         assertEquals(mPersistentDrawingCache2, vg.getPersistentDrawingCache());
828     }
829 
testHasFocus()830     public void testHasFocus() {
831         MockViewGroup vg = new MockViewGroup(mContext);
832         assertFalse(vg.hasFocus());
833 
834         TextView textView = new TextView(mContext);
835 
836         vg.addView(textView);
837         vg.requestChildFocus(textView, null);
838 
839         assertTrue(vg.hasFocus());
840     }
841 
testHasFocusable()842     public void testHasFocusable() {
843         MockViewGroup vg = new MockViewGroup(mContext);
844         assertFalse(vg.hasFocusable());
845 
846         vg.setVisibility(View.VISIBLE);
847         vg.setFocusable(true);
848         assertTrue(vg.hasFocusable());
849     }
850 
testIndexOfChild()851     public void testIndexOfChild() {
852         MockViewGroup vg = new MockViewGroup(mContext);
853         TextView textView = new TextView(mContext);
854 
855         assertEquals(-1, vg.indexOfChild(textView));
856 
857         vg.addView(textView);
858         assertEquals(0, vg.indexOfChild(textView));
859     }
860 
setupActivity(String action)861     private void setupActivity(String action) {
862 
863         Intent intent = new Intent(getInstrumentation().getTargetContext(),
864                 ViewGroupCtsActivity.class);
865         intent.setAction(action);
866         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
867         getInstrumentation().getTargetContext().startActivity(intent);
868     }
869 
testInvalidateChild()870     public void testInvalidateChild() {
871         ViewGroupCtsActivity.setResult(this);
872         setupActivity(ViewGroupCtsActivity.ACTION_INVALIDATE_CHILD);
873         waitForResult();
874         assertEquals(CTSResult.RESULT_OK, mResultCode);
875     }
876 
waitForResult()877     private void waitForResult() {
878         synchronized (mSync) {
879             while(!mSync.mHasNotify) {
880                 try {
881                     mSync.wait();
882                 } catch (InterruptedException e) {
883                 }
884             }
885         }
886     }
887 
testIsAlwaysDrawnWithCacheEnabled()888     public void testIsAlwaysDrawnWithCacheEnabled() {
889         MockViewGroup vg = new MockViewGroup(mContext);
890 
891         assertTrue(vg.isAlwaysDrawnWithCacheEnabled());
892 
893         vg.setAlwaysDrawnWithCacheEnabled(false);
894         assertFalse(vg.isAlwaysDrawnWithCacheEnabled());
895         vg.setAlwaysDrawnWithCacheEnabled(true);
896         assertTrue(vg.isAlwaysDrawnWithCacheEnabled());
897     }
898 
testIsAnimationCacheEnabled()899     public void testIsAnimationCacheEnabled() {
900         MockViewGroup vg = new MockViewGroup(mContext);
901 
902         assertTrue(vg.isAnimationCacheEnabled());
903 
904         vg.setAnimationCacheEnabled(false);
905         assertFalse(vg.isAnimationCacheEnabled());
906         vg.setAnimationCacheEnabled(true);
907         assertTrue(vg.isAnimationCacheEnabled());
908     }
909 
testIsChildrenDrawnWithCacheEnabled()910     public void testIsChildrenDrawnWithCacheEnabled() {
911         MockViewGroup vg = new MockViewGroup(mContext);
912 
913         assertFalse(vg.isChildrenDrawnWithCacheEnabled());
914 
915         vg.setChildrenDrawnWithCacheEnabled(true);
916         assertTrue(vg.isChildrenDrawnWithCacheEnabled());
917     }
918 
testMeasureChild()919     public void testMeasureChild() {
920         final int width = 100;
921         final int height = 200;
922         MockViewGroup vg = new MockViewGroup(mContext);
923         MockView son = new MockView(mContext);
924         son.setLayoutParams(new LayoutParams(width, height));
925         son.forceLayout();
926         vg.addView(son);
927 
928         final int parentWidthMeasureSpec = 1;
929         final int parentHeightMeasureSpec = 2;
930         vg.measureChild(son, parentWidthMeasureSpec, parentHeightMeasureSpec);
931         assertEquals(ViewGroup.getChildMeasureSpec(parentWidthMeasureSpec, 0, width),
932                 son.mWidthMeasureSpec);
933         assertEquals(ViewGroup.getChildMeasureSpec(parentHeightMeasureSpec, 0, height),
934                 son.mHeightMeasureSpec);
935     }
936 
testMeasureChildren()937     public void testMeasureChildren() {
938         final int widthMeasureSpec = 100;
939         final int heightMeasureSpec = 200;
940         MockViewGroup vg = new MockViewGroup(mContext);
941         MockTextView textView1 = new MockTextView(mContext);
942 
943         vg.addView(textView1);
944         vg.measureChildCalledTime = 0;
945         vg.measureChildren(widthMeasureSpec, heightMeasureSpec);
946         assertEquals(1, vg.measureChildCalledTime);
947 
948         MockTextView textView2 = new MockTextView(mContext);
949         textView2.setVisibility(View.GONE);
950         vg.addView(textView2);
951 
952         vg.measureChildCalledTime = 0;
953         vg.measureChildren(widthMeasureSpec, heightMeasureSpec);
954         assertEquals(1, vg.measureChildCalledTime);
955     }
956 
testMeasureChildWithMargins()957     public void testMeasureChildWithMargins() {
958         final int width = 10;
959         final int height = 20;
960         final int parentWidthMeasureSpec = 1;
961         final int widthUsed = 2;
962         final int parentHeightMeasureSpec = 3;
963         final int heightUsed = 4;
964         MockViewGroup vg = new MockViewGroup(mContext);
965         MockView son = new MockView(mContext);
966 
967         vg.addView(son);
968         son.setLayoutParams(new ViewGroup.LayoutParams(width, height));
969         try {
970             vg.measureChildWithMargins(son, parentWidthMeasureSpec, widthUsed,
971                     parentHeightMeasureSpec, heightUsed);
972             fail("measureChildWithMargins should throw out class cast exception");
973         } catch (RuntimeException e) {
974         }
975         son.setLayoutParams(new ViewGroup.MarginLayoutParams(width, height));
976 
977         vg.measureChildWithMargins(son, parentWidthMeasureSpec, widthUsed, parentHeightMeasureSpec,
978                 heightUsed);
979         assertEquals(ViewGroup.getChildMeasureSpec(parentWidthMeasureSpec, parentHeightMeasureSpec,
980                 width), son.mWidthMeasureSpec);
981         assertEquals(ViewGroup.getChildMeasureSpec(widthUsed, heightUsed, height),
982                 son.mHeightMeasureSpec);
983     }
984 
testOffsetDescendantRectToMyCoords()985     public void testOffsetDescendantRectToMyCoords() {
986         MockViewGroup vg = new MockViewGroup(mContext);
987         MockTextView textView = new MockTextView(mContext);
988 
989         try {
990             vg.offsetDescendantRectToMyCoords(textView, new Rect());
991             fail("offsetDescendantRectToMyCoords should throw out "
992                     + "IllegalArgumentException");
993         } catch (RuntimeException e) {
994             // expected
995         }
996         vg.addView(textView);
997         textView.setFrame(1, 2, 3, 4);
998         Rect rect = new Rect();
999         vg.offsetDescendantRectToMyCoords(textView, rect);
1000         assertEquals(2, rect.bottom);
1001         assertEquals(2, rect.top);
1002         assertEquals(1, rect.left);
1003         assertEquals(1, rect.right);
1004     }
1005 
testOffsetRectIntoDescendantCoords()1006     public void testOffsetRectIntoDescendantCoords() {
1007         MockViewGroup vg = new MockViewGroup(mContext);
1008         vg.setFrame(10, 20, 30, 40);
1009         MockTextView textView = new MockTextView(mContext);
1010 
1011         try {
1012             vg.offsetRectIntoDescendantCoords(textView, new Rect());
1013             fail("offsetRectIntoDescendantCoords should throw out "
1014                     + "IllegalArgumentException");
1015         } catch (RuntimeException e) {
1016             // expected
1017         }
1018         textView.setFrame(1, 2, 3, 4);
1019         vg.addView(textView);
1020 
1021         Rect rect = new Rect(5, 6, 7, 8);
1022         vg.offsetRectIntoDescendantCoords(textView, rect);
1023         assertEquals(6, rect.bottom);
1024         assertEquals(4, rect.top);
1025         assertEquals(4, rect.left);
1026         assertEquals(6, rect.right);
1027     }
1028 
testOnAnimationEnd()1029     public void testOnAnimationEnd() {
1030         // this function is a call back function it should be tested in ViewGroup#drawChild.
1031         MockViewGroup father = new MockViewGroup(mContext);
1032         MockViewGroup son = new MockViewGroup(mContext);
1033         son.setAnimation(new MockAnimation());
1034         // this call will make mPrivateFlags |= ANIMATION_STARTED;
1035         son.onAnimationStart();
1036         father.addView(son);
1037 
1038         MockCanvas canvas = new MockCanvas();
1039         assertFalse(father.drawChild(canvas, son, 100));
1040         assertTrue(son.isOnAnimationEndCalled);
1041     }
1042 
1043     private class MockAnimation extends Animation {
MockAnimation()1044         public MockAnimation() {
1045             super();
1046         }
1047 
MockAnimation(Context context, AttributeSet attrs)1048         public MockAnimation(Context context, AttributeSet attrs) {
1049             super(context, attrs);
1050         }
1051 
1052         @Override
getTransformation(long currentTime, Transformation outTransformation)1053         public boolean getTransformation(long currentTime, Transformation outTransformation) {
1054            super.getTransformation(currentTime, outTransformation);
1055            return false;
1056         }
1057     }
1058 
testOnAnimationStart()1059     public void testOnAnimationStart() {
1060         // This is a call back method. It should be tested in ViewGroup#drawChild.
1061         MockViewGroup father = new MockViewGroup(mContext);
1062         MockViewGroup son = new MockViewGroup(mContext);
1063 
1064         father.addView(son);
1065 
1066         MockCanvas canvas = new MockCanvas();
1067         try {
1068             assertFalse(father.drawChild(canvas, son, 100));
1069             assertFalse(son.isOnAnimationStartCalled);
1070         } catch (Exception e) {
1071             // expected
1072         }
1073 
1074         son.setAnimation(new MockAnimation());
1075         assertFalse(father.drawChild(canvas, son, 100));
1076         assertTrue(son.isOnAnimationStartCalled);
1077     }
1078 
testOnCreateDrawableState()1079     public void testOnCreateDrawableState() {
1080         MockViewGroup vg = new MockViewGroup(mContext);
1081         // Call back function. Called in View#getDrawableState()
1082         int[] data = vg.getDrawableState();
1083         assertTrue(vg.isOnCreateDrawableStateCalled);
1084         assertEquals(1, data.length);
1085     }
1086 
testOnInterceptTouchEvent()1087     public void testOnInterceptTouchEvent() {
1088         MockViewGroup vg = new MockViewGroup(mContext);
1089         MotionEvent me = MotionEvent.obtain(SystemClock.uptimeMillis(),
1090                 SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 100, 100,
1091                 0);
1092 
1093         assertFalse(vg.dispatchTouchEvent(me));
1094         assertTrue(vg.isOnInterceptTouchEventCalled);
1095     }
1096 
testOnLayout()1097     public void testOnLayout() {
1098         final int left = 1;
1099         final int top = 2;
1100         final int right = 100;
1101         final int bottom = 200;
1102         MockViewGroup mv = new MockViewGroup(mContext);
1103         mv.layout(left, top, right, bottom);
1104         assertEquals(left, mv.left);
1105         assertEquals(top, mv.top);
1106         assertEquals(right, mv.right);
1107         assertEquals(bottom, mv.bottom);
1108     }
1109 
testOnRequestFocusInDescendants()1110     public void testOnRequestFocusInDescendants() {
1111         MockViewGroup vg = new MockViewGroup(mContext);
1112 
1113         vg.requestFocus(View.FOCUS_DOWN, new Rect());
1114         assertTrue(vg.isOnRequestFocusInDescendantsCalled);
1115     }
1116 
testRemoveAllViews()1117     public void testRemoveAllViews() {
1118         MockViewGroup vg = new MockViewGroup(mContext);
1119         MockTextView textView = new MockTextView(mContext);
1120         assertEquals(0, vg.getChildCount());
1121 
1122         vg.addView(textView);
1123         assertEquals(1, vg.getChildCount());
1124 
1125         vg.removeAllViews();
1126         assertEquals(0, vg.getChildCount());
1127         assertNull(textView.getParent());
1128     }
1129 
testRemoveAllViewsInLayout()1130     public void testRemoveAllViewsInLayout() {
1131         MockViewGroup father = new MockViewGroup(mContext);
1132         MockViewGroup son = new MockViewGroup(mContext);
1133         MockTextView textView = new MockTextView(mContext);
1134 
1135         assertEquals(0, father.getChildCount());
1136 
1137         son.addView(textView);
1138         father.addView(son);
1139         assertEquals(1, father.getChildCount());
1140 
1141         father.removeAllViewsInLayout();
1142         assertEquals(0, father.getChildCount());
1143         assertEquals(1, son.getChildCount());
1144         assertNull(son.getParent());
1145         assertSame(son, textView.getParent());
1146     }
1147 
testRemoveDetachedView()1148     public void testRemoveDetachedView() {
1149         MockViewGroup father = new MockViewGroup(mContext);
1150         MockViewGroup son1 = new MockViewGroup(mContext);
1151         MockViewGroup son2 = new MockViewGroup(mContext);
1152         MockOnHierarchyChangeListener listener = new MockOnHierarchyChangeListener();
1153         father.setOnHierarchyChangeListener(listener);
1154         father.addView(son1);
1155         father.addView(son2);
1156 
1157         father.removeDetachedView(son1, false);
1158         assertSame(father, listener.sParent);
1159         assertSame(son1, listener.sChild);
1160     }
1161 
1162     static class MockOnHierarchyChangeListener implements OnHierarchyChangeListener {
1163 
1164         public View sParent;
1165         public View sChild;
1166 
onChildViewAdded(View parent, View child)1167         public void onChildViewAdded(View parent, View child) {
1168         }
1169 
onChildViewRemoved(View parent, View child)1170         public void onChildViewRemoved(View parent, View child) {
1171             sParent = parent;
1172             sChild = child;
1173         }
1174     }
1175 
testRemoveView()1176     public void testRemoveView() {
1177         MockViewGroup father = new MockViewGroup(mContext);
1178         MockViewGroup son = new MockViewGroup(mContext);
1179 
1180         assertEquals(0, father.getChildCount());
1181 
1182         father.addView(son);
1183         assertEquals(1, father.getChildCount());
1184 
1185         father.removeView(son);
1186         assertEquals(0, father.getChildCount());
1187         assertNull(son.getParent());
1188     }
1189 
testRemoveViewAt()1190     public void testRemoveViewAt() {
1191         MockViewGroup father = new MockViewGroup(mContext);
1192         MockViewGroup son = new MockViewGroup(mContext);
1193 
1194         assertEquals(0, father.getChildCount());
1195 
1196         father.addView(son);
1197         assertEquals(1, father.getChildCount());
1198 
1199         try {
1200             father.removeViewAt(2);
1201             fail("should throw out null pointer exception");
1202         } catch (RuntimeException e) {
1203             // expected
1204         }
1205         assertEquals(1, father.getChildCount());
1206 
1207         father.removeViewAt(0);
1208         assertEquals(0, father.getChildCount());
1209         assertNull(son.getParent());
1210     }
1211 
testRemoveViewInLayout()1212     public void testRemoveViewInLayout() {
1213         MockViewGroup father = new MockViewGroup(mContext);
1214         MockViewGroup son = new MockViewGroup(mContext);
1215 
1216         assertEquals(0, father.getChildCount());
1217 
1218         father.addView(son);
1219         assertEquals(1, father.getChildCount());
1220 
1221         father.removeViewInLayout(son);
1222         assertEquals(0, father.getChildCount());
1223         assertNull(son.getParent());
1224     }
1225 
testRemoveViews()1226     public void testRemoveViews() {
1227         MockViewGroup father = new MockViewGroup(mContext);
1228         MockViewGroup son1 = new MockViewGroup(mContext);
1229         MockViewGroup son2 = new MockViewGroup(mContext);
1230 
1231         assertEquals(0, father.getChildCount());
1232 
1233         father.addView(son1);
1234         father.addView(son2);
1235         assertEquals(2, father.getChildCount());
1236 
1237         father.removeViews(0, 1);
1238         assertEquals(1, father.getChildCount());
1239         assertNull(son1.getParent());
1240 
1241         father.removeViews(0, 1);
1242         assertEquals(0, father.getChildCount());
1243         assertNull(son2.getParent());
1244     }
1245 
testRemoveViewsInLayout()1246     public void testRemoveViewsInLayout() {
1247         MockViewGroup father = new MockViewGroup(mContext);
1248         MockViewGroup son1 = new MockViewGroup(mContext);
1249         MockViewGroup son2 = new MockViewGroup(mContext);
1250 
1251         assertEquals(0, father.getChildCount());
1252 
1253         father.addView(son1);
1254         father.addView(son2);
1255         assertEquals(2, father.getChildCount());
1256 
1257         father.removeViewsInLayout(0, 1);
1258         assertEquals(1, father.getChildCount());
1259         assertNull(son1.getParent());
1260 
1261         father.removeViewsInLayout(0, 1);
1262         assertEquals(0, father.getChildCount());
1263         assertNull(son2.getParent());
1264     }
1265 
testRequestChildFocus()1266     public void testRequestChildFocus() {
1267         MockViewGroup vg = new MockViewGroup(mContext);
1268         TextView textView = new TextView(mContext);
1269 
1270         vg.addView(textView);
1271         vg.requestChildFocus(textView, null);
1272 
1273         assertNotNull(vg.getFocusedChild());
1274 
1275         vg.clearChildFocus(textView);
1276         assertNull(vg.getFocusedChild());
1277     }
1278 
testRequestChildRectangleOnScreen()1279     public void testRequestChildRectangleOnScreen() {
1280         MockViewGroup vg = new MockViewGroup(mContext);
1281         assertFalse(vg.requestChildRectangleOnScreen(null, null, false));
1282     }
1283 
testRequestDisallowInterceptTouchEvent()1284     public void testRequestDisallowInterceptTouchEvent() {
1285         MockViewGroup father = new MockViewGroup(mContext);
1286         MockView son = new MockView(mContext);
1287 
1288         father.addView(son);
1289         son.requestDisallowInterceptTouchEvent(true);
1290         son.requestDisallowInterceptTouchEvent(false);
1291         assertTrue(father.isRequestDisallowInterceptTouchEventCalled);
1292     }
1293 
testRequestFocus()1294     public void testRequestFocus() {
1295         MockViewGroup vg = new MockViewGroup(mContext);
1296 
1297         vg.requestFocus(View.FOCUS_DOWN, new Rect());
1298         assertTrue(vg.isOnRequestFocusInDescendantsCalled);
1299     }
1300 
testRequestTransparentRegion()1301     public void testRequestTransparentRegion() {
1302         MockViewGroup father = new MockViewGroup(mContext);
1303         MockView son1 = new MockView(mContext);
1304         MockView son2 = new MockView(mContext);
1305         son1.addView(son2);
1306         father.addView(son1);
1307         son1.requestTransparentRegion(son2);
1308         assertTrue(father.isRequestTransparentRegionCalled);
1309     }
1310 
testScheduleLayoutAnimation()1311     public void testScheduleLayoutAnimation() {
1312         MockViewGroup vg = new MockViewGroup(mContext);
1313         Animation animation = new AlphaAnimation(mContext, null);
1314 
1315         MockLayoutAnimationController al = new MockLayoutAnimationController(animation);
1316         vg.setLayoutAnimation(al);
1317         vg.scheduleLayoutAnimation();
1318         vg.dispatchDraw(new Canvas());
1319         assertTrue(al.mIsStartCalled);
1320     }
1321 
1322     static class MockLayoutAnimationController extends LayoutAnimationController {
1323 
1324         public boolean mIsStartCalled;
1325 
MockLayoutAnimationController(Animation animation)1326         public MockLayoutAnimationController(Animation animation) {
1327             super(animation);
1328         }
1329 
1330         @Override
start()1331         public void start() {
1332             mIsStartCalled = true;
1333             super.start();
1334         }
1335     }
1336 
testSetAddStatesFromChildren()1337     public void testSetAddStatesFromChildren() {
1338         MockViewGroup vg = new MockViewGroup(mContext);
1339         vg.setAddStatesFromChildren(true);
1340         assertTrue(vg.addStatesFromChildren());
1341 
1342         vg.setAddStatesFromChildren(false);
1343         assertFalse(vg.addStatesFromChildren());
1344     }
1345 
testSetChildrenDrawingCacheEnabled()1346     public void testSetChildrenDrawingCacheEnabled() {
1347         MockViewGroup vg = new MockViewGroup(mContext);
1348 
1349         assertTrue(vg.isAnimationCacheEnabled());
1350 
1351         vg.setAnimationCacheEnabled(false);
1352         assertFalse(vg.isAnimationCacheEnabled());
1353 
1354         vg.setAnimationCacheEnabled(true);
1355         assertTrue(vg.isAnimationCacheEnabled());
1356     }
1357 
testSetChildrenDrawnWithCacheEnabled()1358     public void testSetChildrenDrawnWithCacheEnabled() {
1359         MockViewGroup vg = new MockViewGroup(mContext);
1360 
1361         assertFalse(vg.isChildrenDrawnWithCacheEnabled());
1362 
1363         vg.setChildrenDrawnWithCacheEnabled(true);
1364         assertTrue(vg.isChildrenDrawnWithCacheEnabled());
1365 
1366         vg.setChildrenDrawnWithCacheEnabled(false);
1367         assertFalse(vg.isChildrenDrawnWithCacheEnabled());
1368     }
1369 
testSetClipChildren()1370     public void testSetClipChildren() {
1371         Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
1372 
1373         MockViewGroup vg = new MockViewGroup(mContext);
1374         MockTextView textView = new MockTextView(mContext);
1375         textView.setFrame(1, 2, 30, 40);
1376         vg.setFrame(1, 1, 100, 200);
1377         vg.setClipChildren(true);
1378 
1379         MockCanvas canvas = new MockCanvas(bitmap);
1380         vg.drawChild(canvas, textView, 100);
1381         Rect rect = canvas.getClipBounds();
1382         assertEquals(0, rect.top);
1383         assertEquals(100, rect.bottom);
1384         assertEquals(0, rect.left);
1385         assertEquals(100, rect.right);
1386     }
1387 
1388     class MockCanvas extends Canvas {
1389 
1390         public boolean mIsSaveCalled;
1391         public int mLeft;
1392         public int mTop;
1393         public int mRight;
1394         public int mBottom;
1395 
MockCanvas()1396         public MockCanvas() {
1397             super(Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888));
1398         }
1399 
MockCanvas(Bitmap bitmap)1400         public MockCanvas(Bitmap bitmap) {
1401             super(bitmap);
1402         }
1403 
1404         @Override
quickReject(float left, float top, float right, float bottom, EdgeType type)1405         public boolean quickReject(float left, float top, float right,
1406                 float bottom, EdgeType type) {
1407             super.quickReject(left, top, right, bottom, type);
1408             return false;
1409         }
1410 
1411         @Override
save()1412         public int save() {
1413             mIsSaveCalled = true;
1414             return super.save();
1415         }
1416 
1417         @Override
clipRect(int left, int top, int right, int bottom)1418         public boolean clipRect(int left, int top, int right, int bottom) {
1419             mLeft = left;
1420             mTop = top;
1421             mRight = right;
1422             mBottom = bottom;
1423             return super.clipRect(left, top, right, bottom);
1424         }
1425     }
1426 
testSetClipToPadding()1427     public void testSetClipToPadding() {
1428         final int frameLeft = 1;
1429         final int frameTop = 2;
1430         final int frameRight = 100;
1431         final int frameBottom = 200;
1432         MockViewGroup vg = new MockViewGroup(mContext);
1433         vg.setFrame(frameLeft, frameTop, frameRight, frameBottom);
1434 
1435         vg.setClipToPadding(true);
1436         MockCanvas canvas = new MockCanvas();
1437         final int paddingLeft = 10;
1438         final int paddingTop = 20;
1439         final int paddingRight = 100;
1440         final int paddingBottom = 200;
1441         vg.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
1442         vg.dispatchDraw(canvas);
1443         //check that the clip region does not contain the padding area
1444         assertTrue(canvas.mIsSaveCalled);
1445         assertEquals(10, canvas.mLeft);
1446         assertEquals(20, canvas.mTop);
1447         assertEquals(-frameLeft, canvas.mRight);
1448         assertEquals(-frameTop, canvas.mBottom);
1449 
1450         vg.setClipToPadding(false);
1451         canvas = new MockCanvas();
1452         vg.dispatchDraw(canvas);
1453         assertFalse(canvas.mIsSaveCalled);
1454         assertEquals(0, canvas.mLeft);
1455         assertEquals(0, canvas.mTop);
1456         assertEquals(0, canvas.mRight);
1457         assertEquals(0, canvas.mBottom);
1458     }
1459 
testSetDescendantFocusability()1460     public void testSetDescendantFocusability() {
1461         MockViewGroup vg = new MockViewGroup(mContext);
1462         final int FLAG_MASK_FOCUSABILITY = 0x60000;
1463         assertFalse((vg.getDescendantFocusability() & FLAG_MASK_FOCUSABILITY) == 0);
1464 
1465         vg.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
1466         assertFalse((vg.getDescendantFocusability() & FLAG_MASK_FOCUSABILITY) == 0);
1467 
1468         vg.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
1469         assertFalse((vg.getDescendantFocusability() & FLAG_MASK_FOCUSABILITY) == 0);
1470         assertFalse((vg.getDescendantFocusability() &
1471                 ViewGroup.FOCUS_BEFORE_DESCENDANTS) == 0);
1472     }
1473 
testSetOnHierarchyChangeListener()1474     public void testSetOnHierarchyChangeListener() {
1475         MockViewGroup father = new MockViewGroup(mContext);
1476         MockViewGroup son = new MockViewGroup(mContext);
1477         MockOnHierarchyChangeListener listener = new MockOnHierarchyChangeListener();
1478         father.setOnHierarchyChangeListener(listener);
1479         father.addView(son);
1480 
1481         father.removeDetachedView(son, false);
1482         assertSame(father, listener.sParent);
1483         assertSame(son, listener.sChild);
1484     }
1485 
testSetPadding()1486     public void testSetPadding() {
1487         final int left = 1;
1488         final int top = 2;
1489         final int right = 3;
1490         final int bottom = 4;
1491 
1492         MockViewGroup vg = new MockViewGroup(mContext);
1493 
1494         assertEquals(0, vg.getPaddingBottom());
1495         assertEquals(0, vg.getPaddingTop());
1496         assertEquals(0, vg.getPaddingLeft());
1497         assertEquals(0, vg.getPaddingRight());
1498         assertEquals(0, vg.getPaddingStart());
1499         assertEquals(0, vg.getPaddingEnd());
1500 
1501         vg.setPadding(left, top, right, bottom);
1502 
1503         assertEquals(bottom, vg.getPaddingBottom());
1504         assertEquals(top, vg.getPaddingTop());
1505         assertEquals(left, vg.getPaddingLeft());
1506         assertEquals(right, vg.getPaddingRight());
1507 
1508         assertEquals(left, vg.getPaddingStart());
1509         assertEquals(right, vg.getPaddingEnd());
1510         assertEquals(false, vg.isPaddingRelative());
1511 
1512         // force RTL direction
1513         vg.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
1514 
1515         assertEquals(bottom, vg.getPaddingBottom());
1516         assertEquals(top, vg.getPaddingTop());
1517         assertEquals(left, vg.getPaddingLeft());
1518         assertEquals(right, vg.getPaddingRight());
1519 
1520         assertEquals(right, vg.getPaddingStart());
1521         assertEquals(left, vg.getPaddingEnd());
1522         assertEquals(false, vg.isPaddingRelative());
1523     }
1524 
testSetPaddingRelative()1525     public void testSetPaddingRelative() {
1526         final int start = 1;
1527         final int top = 2;
1528         final int end = 3;
1529         final int bottom = 4;
1530 
1531         MockViewGroup vg = new MockViewGroup(mContext);
1532 
1533         assertEquals(0, vg.getPaddingBottom());
1534         assertEquals(0, vg.getPaddingTop());
1535         assertEquals(0, vg.getPaddingLeft());
1536         assertEquals(0, vg.getPaddingRight());
1537         assertEquals(0, vg.getPaddingStart());
1538         assertEquals(0, vg.getPaddingEnd());
1539 
1540         vg.setPaddingRelative(start, top, end, bottom);
1541 
1542         assertEquals(bottom, vg.getPaddingBottom());
1543         assertEquals(top, vg.getPaddingTop());
1544         assertEquals(start, vg.getPaddingLeft());
1545         assertEquals(end, vg.getPaddingRight());
1546 
1547         assertEquals(start, vg.getPaddingStart());
1548         assertEquals(end, vg.getPaddingEnd());
1549         assertEquals(true, vg.isPaddingRelative());
1550 
1551         // force RTL direction after setting relative padding
1552         vg.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
1553 
1554         assertEquals(bottom, vg.getPaddingBottom());
1555         assertEquals(top, vg.getPaddingTop());
1556         assertEquals(end, vg.getPaddingLeft());
1557         assertEquals(start, vg.getPaddingRight());
1558 
1559         assertEquals(start, vg.getPaddingStart());
1560         assertEquals(end, vg.getPaddingEnd());
1561         assertEquals(true, vg.isPaddingRelative());
1562 
1563         // force RTL direction before setting relative padding
1564         vg = new MockViewGroup(mContext);
1565         vg.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
1566 
1567         assertEquals(0, vg.getPaddingBottom());
1568         assertEquals(0, vg.getPaddingTop());
1569         assertEquals(0, vg.getPaddingLeft());
1570         assertEquals(0, vg.getPaddingRight());
1571         assertEquals(0, vg.getPaddingStart());
1572         assertEquals(0, vg.getPaddingEnd());
1573 
1574         vg.setPaddingRelative(start, top, end, bottom);
1575 
1576         assertEquals(bottom, vg.getPaddingBottom());
1577         assertEquals(top, vg.getPaddingTop());
1578         assertEquals(end, vg.getPaddingLeft());
1579         assertEquals(start, vg.getPaddingRight());
1580 
1581         assertEquals(start, vg.getPaddingStart());
1582         assertEquals(end, vg.getPaddingEnd());
1583         assertEquals(true, vg.isPaddingRelative());
1584     }
1585 
testSetPersistentDrawingCache()1586     public void testSetPersistentDrawingCache() {
1587         MockViewGroup vg = new MockViewGroup(mContext);
1588         vg.setPersistentDrawingCache(1);
1589         assertEquals(1 & ViewGroup.PERSISTENT_ALL_CACHES, vg
1590                 .getPersistentDrawingCache());
1591     }
1592 
testShowContextMenuForChild()1593     public void testShowContextMenuForChild() {
1594         MockViewGroup father = new MockViewGroup(mContext);
1595         MockViewGroup son = new MockViewGroup(mContext);
1596         father.addView(son);
1597 
1598         son.showContextMenuForChild(null);
1599         assertTrue(father.isShowContextMenuForChildCalled);
1600     }
1601 
testStartLayoutAnimation()1602     public void testStartLayoutAnimation() {
1603         MockViewGroup vg = new MockViewGroup(mContext);
1604         RotateAnimation animation = new RotateAnimation(0.1f, 0.1f);
1605         LayoutAnimationController la = new LayoutAnimationController(animation);
1606         vg.setLayoutAnimation(la);
1607 
1608         vg.layout(1, 1, 100, 100);
1609         assertFalse(vg.isLayoutRequested());
1610         vg.startLayoutAnimation();
1611         assertTrue(vg.isLayoutRequested());
1612     }
1613 
testUpdateViewLayout()1614     public void testUpdateViewLayout() {
1615         MockViewGroup father = new MockViewGroup(mContext);
1616         MockViewGroup son = new MockViewGroup(mContext);
1617 
1618         father.addView(son);
1619         LayoutParams param = new LayoutParams(100, 200);
1620         father.updateViewLayout(son, param);
1621         assertEquals(param.width, son.getLayoutParams().width);
1622         assertEquals(param.height, son.getLayoutParams().height);
1623     }
1624 
testDebug()1625     public void testDebug() {
1626         final int EXPECTED = 100;
1627         MockViewGroup father = new MockViewGroup(mContext);
1628         MockViewGroup son = new MockViewGroup(mContext);
1629         father.addView(son);
1630 
1631         father.debug(EXPECTED);
1632         assertEquals(EXPECTED + 1, son.debugDepth);
1633     }
1634 
testDispatchKeyEventPreIme()1635     public void testDispatchKeyEventPreIme() {
1636         MockViewGroup vg = new MockViewGroup(mContext);
1637         KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER);
1638         assertFalse(vg.dispatchKeyEventPreIme(event));
1639         assertFalse(vg.dispatchKeyShortcutEvent(event));
1640         MockTextView textView = new MockTextView(mContext);
1641 
1642         vg.addView(textView);
1643         vg.requestChildFocus(textView, null);
1644         vg.layout(0, 0, 100, 200);
1645         assertFalse(vg.dispatchKeyEventPreIme(event));
1646         assertFalse(vg.dispatchKeyShortcutEvent(event));
1647 
1648         vg.requestChildFocus(textView, null);
1649         textView.layout(0, 0, 50, 50);
1650         assertTrue(vg.dispatchKeyEventPreIme(event));
1651         assertTrue(vg.dispatchKeyShortcutEvent(event));
1652 
1653         vg.setStaticTransformationsEnabled(true);
1654         Canvas canvas = new Canvas();
1655         vg.drawChild(canvas, textView, 100);
1656         assertTrue(vg.isGetChildStaticTransformationCalled);
1657         vg.isGetChildStaticTransformationCalled = false;
1658         vg.setStaticTransformationsEnabled(false);
1659         vg.drawChild(canvas, textView, 100);
1660         assertFalse(vg.isGetChildStaticTransformationCalled);
1661     }
1662 
1663     static public int resetRtlPropertiesCount;
1664     static public int resetResolvedLayoutDirectionCount;
1665     static public int resetResolvedTextDirectionCount;
1666     static public int resetResolvedTextAlignmentCount;
1667     static public int resetResolvedPaddingCount;
1668     static public int resetResolvedDrawablesCount;
1669 
1670 
clearRtlCounters()1671     private static void clearRtlCounters() {
1672         resetRtlPropertiesCount = 0;
1673         resetResolvedLayoutDirectionCount = 0;
1674         resetResolvedTextDirectionCount = 0;
1675         resetResolvedTextAlignmentCount = 0;
1676         resetResolvedPaddingCount = 0;
1677         resetResolvedDrawablesCount = 0;
1678     }
1679 
testResetRtlProperties()1680     public void testResetRtlProperties() {
1681         clearRtlCounters();
1682 
1683         MockViewGroup vg = new MockViewGroup(mContext);
1684         MockView2 v1 = new MockView2(mContext);
1685         MockView2 v2 = new MockView2(mContext);
1686 
1687         MockViewGroup v3 = new MockViewGroup(mContext);
1688         MockView2 v4 = new MockView2(mContext);
1689 
1690         v3.addView(v4);
1691         assertEquals(1, resetRtlPropertiesCount);
1692         assertEquals(1, resetResolvedLayoutDirectionCount);
1693         assertEquals(1, resetResolvedTextDirectionCount);
1694         assertEquals(1, resetResolvedTextAlignmentCount);
1695         assertEquals(1, resetResolvedPaddingCount);
1696         assertEquals(1, resetResolvedDrawablesCount);
1697 
1698         clearRtlCounters();
1699         vg.addView(v1);
1700         vg.addView(v2);
1701         vg.addView(v3);
1702 
1703         assertEquals(3, resetRtlPropertiesCount); // for v1 / v2 / v3 only
1704         assertEquals(4, resetResolvedLayoutDirectionCount); // for v1 / v2 / v3 / v4
1705         assertEquals(4, resetResolvedTextDirectionCount);
1706         assertEquals(3, resetResolvedTextAlignmentCount); // for v1 / v2 / v3 only
1707         assertEquals(4, resetResolvedPaddingCount);
1708         assertEquals(4, resetResolvedDrawablesCount);
1709 
1710         clearRtlCounters();
1711         vg.resetRtlProperties();
1712         assertEquals(1, resetRtlPropertiesCount); // for vg only
1713         assertEquals(5, resetResolvedLayoutDirectionCount); // for all
1714         assertEquals(5, resetResolvedTextDirectionCount);
1715         assertEquals(1, resetResolvedTextAlignmentCount); // for vg only as TextAlignment is not inherited (default is Gravity)
1716         assertEquals(5, resetResolvedPaddingCount);
1717         assertEquals(5, resetResolvedDrawablesCount);
1718     }
1719 
1720     static class MockTextView extends TextView {
1721 
1722         public boolean isClearFocusCalled;
1723         public boolean isDispatchRestoreInstanceStateCalled;
1724         public int visibility;
1725         public boolean mIsRefreshDrawableStateCalled;
1726         public boolean isDrawCalled;
1727 
MockTextView(Context context)1728         public MockTextView(Context context) {
1729             super(context);
1730         }
1731 
1732         @Override
draw(Canvas canvas)1733         public void draw(Canvas canvas) {
1734             super.draw(canvas);
1735             isDrawCalled = true;
1736         }
1737 
1738         @Override
clearFocus()1739         public void clearFocus() {
1740             isClearFocusCalled = true;
1741             super.clearFocus();
1742         }
1743 
1744         @Override
dispatchKeyEvent(KeyEvent event)1745         public boolean dispatchKeyEvent(KeyEvent event) {
1746             return true;
1747         }
1748 
1749         @Override
setFrame(int l, int t, int r, int b)1750         public boolean setFrame(int l, int t, int r, int b) {
1751             return super.setFrame(l, t, r, b);
1752         }
1753 
1754         @Override
dispatchRestoreInstanceState( SparseArray<Parcelable> container)1755         public void dispatchRestoreInstanceState(
1756                 SparseArray<Parcelable> container) {
1757             isDispatchRestoreInstanceStateCalled = true;
1758             super.dispatchRestoreInstanceState(container);
1759         }
1760 
1761         @Override
onTrackballEvent(MotionEvent event)1762         public boolean onTrackballEvent(MotionEvent event) {
1763             return true;
1764         }
1765 
1766         @Override
dispatchUnhandledMove(View focused, int direction)1767         public boolean dispatchUnhandledMove(View focused, int direction) {
1768             return true;
1769         }
1770 
1771         @Override
onWindowVisibilityChanged(int visibility)1772         public void onWindowVisibilityChanged(int visibility) {
1773             this.visibility = visibility;
1774             super.onWindowVisibilityChanged(visibility);
1775         }
1776 
1777         @Override
refreshDrawableState()1778         public void refreshDrawableState() {
1779             mIsRefreshDrawableStateCalled = true;
1780             super.refreshDrawableState();
1781         }
1782 
1783         @Override
gatherTransparentRegion(Region region)1784         public boolean gatherTransparentRegion(Region region) {
1785             return false;
1786         }
1787 
1788         @Override
dispatchTouchEvent(MotionEvent event)1789         public boolean dispatchTouchEvent(MotionEvent event) {
1790             super.dispatchTouchEvent(event);
1791             return true;
1792         }
1793 
1794         @Override
dispatchKeyEventPreIme(KeyEvent event)1795         public boolean dispatchKeyEventPreIme(KeyEvent event) {
1796             return true;
1797         }
1798 
1799         @Override
dispatchKeyShortcutEvent(KeyEvent event)1800         public boolean dispatchKeyShortcutEvent(KeyEvent event) {
1801             return true;
1802         }
1803     }
1804 
1805     static class MockViewGroup extends ViewGroup {
1806 
1807         public boolean isRecomputeViewAttributesCalled;
1808         public boolean isShowContextMenuForChildCalled;
1809         public boolean isRefreshDrawableStateCalled;
1810         public boolean isOnRestoreInstanceStateCalled;
1811         public boolean isOnCreateDrawableStateCalled;
1812         public boolean isOnInterceptTouchEventCalled;
1813         public boolean isOnRequestFocusInDescendantsCalled;
1814         public boolean isFocusableViewAvailable;
1815         public boolean isDispatchDrawCalled;
1816         public boolean isRequestDisallowInterceptTouchEventCalled;
1817         public boolean isRequestTransparentRegionCalled;
1818         public boolean isGetChildStaticTransformationCalled;
1819         public int[] location;
1820         public int measureChildCalledTime;
1821         public boolean isOnAnimationEndCalled;
1822         public boolean isOnAnimationStartCalled;
1823         public int debugDepth;
1824         public int drawChildCalledTime;
1825         public Canvas canvas;
1826         public boolean isInvalidateChildInParentCalled;
1827         public boolean isDrawableStateChangedCalled;
1828         public boolean isRequestLayoutCalled;
1829         public boolean isOnLayoutCalled;
1830         public int left;
1831         public int top;
1832         public int right;
1833         public int bottom;
1834 
MockViewGroup(Context context, AttributeSet attrs, int defStyle)1835         public MockViewGroup(Context context, AttributeSet attrs, int defStyle) {
1836             super(context, attrs, defStyle);
1837         }
1838 
MockViewGroup(Context context, AttributeSet attrs)1839         public MockViewGroup(Context context, AttributeSet attrs) {
1840             super(context, attrs);
1841         }
1842 
MockViewGroup(Context context)1843         public MockViewGroup(Context context) {
1844             super(context);
1845         }
1846 
1847         @Override
onLayout(boolean changed, int l, int t, int r, int b)1848         public void onLayout(boolean changed, int l, int t, int r, int b) {
1849             isOnLayoutCalled = true;
1850             left = l;
1851             top = t;
1852             right = r;
1853             bottom = b;
1854         }
1855 
1856         @Override
addViewInLayout(View child, int index, ViewGroup.LayoutParams params)1857         public boolean addViewInLayout(View child, int index,
1858                 ViewGroup.LayoutParams params) {
1859             return super.addViewInLayout(child, index, params);
1860         }
1861 
1862         @Override
addViewInLayout(View child, int index, ViewGroup.LayoutParams params, boolean preventRequestLayout)1863         public boolean addViewInLayout(View child, int index,
1864                 ViewGroup.LayoutParams params, boolean preventRequestLayout) {
1865             return super.addViewInLayout(child, index, params, preventRequestLayout);
1866         }
1867 
1868         @Override
attachLayoutAnimationParameters(View child, ViewGroup.LayoutParams params, int index, int count)1869         public void attachLayoutAnimationParameters(View child,
1870                 ViewGroup.LayoutParams params, int index, int count) {
1871             super.attachLayoutAnimationParameters(child, params, index, count);
1872         }
1873 
1874         @Override
attachViewToParent(View child, int index, LayoutParams params)1875         public void attachViewToParent(View child, int index,
1876                 LayoutParams params) {
1877             super.attachViewToParent(child, index, params);
1878         }
1879 
1880         @Override
canAnimate()1881         public boolean canAnimate() {
1882             return super.canAnimate();
1883         }
1884 
1885         @Override
checkLayoutParams(LayoutParams p)1886         public boolean checkLayoutParams(LayoutParams p) {
1887             return super.checkLayoutParams(p);
1888         }
1889 
1890         @Override
refreshDrawableState()1891         public void refreshDrawableState() {
1892             isRefreshDrawableStateCalled = true;
1893             super.refreshDrawableState();
1894         }
1895 
1896         @Override
cleanupLayoutState(View child)1897         public void cleanupLayoutState(View child) {
1898             super.cleanupLayoutState(child);
1899         }
1900 
1901         @Override
detachAllViewsFromParent()1902         public void detachAllViewsFromParent() {
1903             super.detachAllViewsFromParent();
1904         }
1905 
1906         @Override
detachViewFromParent(int index)1907         public void detachViewFromParent(int index) {
1908             super.detachViewFromParent(index);
1909         }
1910 
1911         @Override
detachViewFromParent(View child)1912         public void detachViewFromParent(View child) {
1913             super.detachViewFromParent(child);
1914         }
1915         @Override
1916 
detachViewsFromParent(int start, int count)1917         public void detachViewsFromParent(int start, int count) {
1918             super.detachViewsFromParent(start, count);
1919         }
1920 
1921         @Override
dispatchDraw(Canvas canvas)1922         public void dispatchDraw(Canvas canvas) {
1923             isDispatchDrawCalled = true;
1924             super.dispatchDraw(canvas);
1925             this.canvas = canvas;
1926         }
1927 
1928         @Override
dispatchFreezeSelfOnly(SparseArray<Parcelable> container)1929         public void dispatchFreezeSelfOnly(SparseArray<Parcelable> container) {
1930             super.dispatchFreezeSelfOnly(container);
1931         }
1932 
1933         @Override
dispatchRestoreInstanceState( SparseArray<Parcelable> container)1934         public void dispatchRestoreInstanceState(
1935                 SparseArray<Parcelable> container) {
1936             super.dispatchRestoreInstanceState(container);
1937         }
1938 
1939         @Override
dispatchSaveInstanceState( SparseArray<Parcelable> container)1940         public void dispatchSaveInstanceState(
1941                 SparseArray<Parcelable> container) {
1942             super.dispatchSaveInstanceState(container);
1943         }
1944 
1945         @Override
dispatchSetPressed(boolean pressed)1946         public void dispatchSetPressed(boolean pressed) {
1947             super.dispatchSetPressed(pressed);
1948         }
1949 
1950         @Override
dispatchThawSelfOnly(SparseArray<Parcelable> container)1951         public void dispatchThawSelfOnly(SparseArray<Parcelable> container) {
1952             super.dispatchThawSelfOnly(container);
1953         }
1954 
1955         @Override
onRestoreInstanceState(Parcelable state)1956         public void onRestoreInstanceState(Parcelable state) {
1957             isOnRestoreInstanceStateCalled = true;
1958             super.onRestoreInstanceState(state);
1959         }
1960 
1961         @Override
drawableStateChanged()1962         public void drawableStateChanged() {
1963             isDrawableStateChangedCalled = true;
1964             super.drawableStateChanged();
1965         }
1966 
1967         @Override
drawChild(Canvas canvas, View child, long drawingTime)1968         public boolean drawChild(Canvas canvas, View child, long drawingTime) {
1969             drawChildCalledTime++;
1970             return super.drawChild(canvas, child, drawingTime);
1971         }
1972 
1973         @Override
fitSystemWindows(Rect insets)1974         public boolean fitSystemWindows(Rect insets) {
1975             return super.fitSystemWindows(insets);
1976         }
1977 
1978         @Override
generateDefaultLayoutParams()1979         public LayoutParams generateDefaultLayoutParams() {
1980             return super.generateDefaultLayoutParams();
1981         }
1982 
1983         @Override
generateLayoutParams(LayoutParams p)1984         public LayoutParams generateLayoutParams(LayoutParams p) {
1985             return super.generateLayoutParams(p);
1986         }
1987 
1988         @Override
getChildDrawingOrder(int childCount, int i)1989         public int getChildDrawingOrder(int childCount, int i) {
1990             return super.getChildDrawingOrder(childCount, i);
1991         }
1992 
1993         @Override
getChildStaticTransformation(View child, Transformation t)1994         public boolean getChildStaticTransformation(View child,
1995                 Transformation t) {
1996             isGetChildStaticTransformationCalled = true;
1997             return super.getChildStaticTransformation(child, t);
1998         }
1999 
2000         @Override
setFrame(int left, int top, int right, int bottom)2001         public boolean setFrame(int left, int top, int right, int bottom) {
2002             return super.setFrame(left, top, right, bottom);
2003         }
2004 
2005         @Override
isChildrenDrawnWithCacheEnabled()2006         public boolean isChildrenDrawnWithCacheEnabled() {
2007             return super.isChildrenDrawnWithCacheEnabled();
2008         }
2009 
2010         @Override
setChildrenDrawnWithCacheEnabled(boolean enabled)2011         public void setChildrenDrawnWithCacheEnabled(boolean enabled) {
2012             super.setChildrenDrawnWithCacheEnabled(enabled);
2013         }
2014 
2015         @Override
measureChild(View child, int parentWidthMeasureSpec, int parentHeightMeasureSpec)2016         public void measureChild(View child, int parentWidthMeasureSpec,
2017                 int parentHeightMeasureSpec) {
2018             measureChildCalledTime++;
2019             super.measureChild(child, parentWidthMeasureSpec, parentHeightMeasureSpec);
2020         }
2021 
2022         @Override
measureChildren(int widthMeasureSpec, int heightMeasureSpec)2023         public void measureChildren(int widthMeasureSpec,
2024                 int heightMeasureSpec) {
2025             super.measureChildren(widthMeasureSpec, heightMeasureSpec);
2026         }
2027 
2028         @Override
measureChildWithMargins(View child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed)2029         public void measureChildWithMargins(View child,
2030                 int parentWidthMeasureSpec, int widthUsed,
2031                 int parentHeightMeasureSpec, int heightUsed) {
2032             super.measureChildWithMargins(child, parentWidthMeasureSpec, widthUsed,
2033                     parentHeightMeasureSpec, heightUsed);
2034         }
2035 
2036         @Override
onAnimationEnd()2037         public void onAnimationEnd() {
2038             isOnAnimationEndCalled = true;
2039             super.onAnimationEnd();
2040         }
2041 
2042         @Override
onAnimationStart()2043         public void onAnimationStart() {
2044             super.onAnimationStart();
2045             isOnAnimationStartCalled = true;
2046         }
2047 
2048         @Override
onCreateDrawableState(int extraSpace)2049         public int[] onCreateDrawableState(int extraSpace) {
2050             isOnCreateDrawableStateCalled = true;
2051             return super.onCreateDrawableState(extraSpace);
2052         }
2053 
2054         @Override
onInterceptTouchEvent(MotionEvent ev)2055         public boolean onInterceptTouchEvent(MotionEvent ev) {
2056             isOnInterceptTouchEventCalled = true;
2057             return super.onInterceptTouchEvent(ev);
2058         }
2059 
2060         @Override
onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect)2061         public boolean onRequestFocusInDescendants(int direction,
2062                 Rect previouslyFocusedRect) {
2063             isOnRequestFocusInDescendantsCalled = true;
2064             return super.onRequestFocusInDescendants(direction, previouslyFocusedRect);
2065         }
2066 
2067         @Override
recomputeViewAttributes(View child)2068         public void recomputeViewAttributes(View child) {
2069             isRecomputeViewAttributesCalled = true;
2070             super.recomputeViewAttributes(child);
2071         }
2072 
2073         @Override
removeDetachedView(View child, boolean animate)2074         public void removeDetachedView(View child, boolean animate) {
2075             super.removeDetachedView(child, animate);
2076         }
2077 
2078         @Override
showContextMenuForChild(View originalView)2079         public boolean showContextMenuForChild(View originalView) {
2080             isShowContextMenuForChildCalled = true;
2081             return super.showContextMenuForChild(originalView);
2082         }
2083 
2084         @Override
isInTouchMode()2085         public boolean isInTouchMode() {
2086             super.isInTouchMode();
2087             return false;
2088         }
2089 
2090         @Override
focusableViewAvailable(View v)2091         public void focusableViewAvailable(View v) {
2092             isFocusableViewAvailable = true;
2093             super.focusableViewAvailable(v);
2094         }
2095 
2096         @Override
focusSearch(View focused, int direction)2097         public View focusSearch(View focused, int direction) {
2098             super.focusSearch(focused, direction);
2099             return focused;
2100         }
2101 
2102         @Override
requestDisallowInterceptTouchEvent(boolean disallowIntercept)2103         public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
2104             isRequestDisallowInterceptTouchEventCalled = true;
2105             super.requestDisallowInterceptTouchEvent(disallowIntercept);
2106         }
2107 
2108         @Override
requestTransparentRegion(View child)2109         public void requestTransparentRegion(View child) {
2110             isRequestTransparentRegionCalled = true;
2111             super.requestTransparentRegion(child);
2112         }
2113 
2114         @Override
debug(int depth)2115         public void debug(int depth) {
2116             debugDepth = depth;
2117             super.debug(depth);
2118         }
2119 
2120         @Override
requestLayout()2121         public void requestLayout() {
2122             isRequestLayoutCalled = true;
2123             super.requestLayout();
2124         }
2125 
2126         @Override
setStaticTransformationsEnabled(boolean enabled)2127         public void setStaticTransformationsEnabled(boolean enabled) {
2128             super.setStaticTransformationsEnabled(enabled);
2129         }
2130 
2131         @Override
resetRtlProperties()2132         public void resetRtlProperties() {
2133             super.resetRtlProperties();
2134             resetRtlPropertiesCount++;
2135         }
2136 
2137         @Override
resetResolvedLayoutDirection()2138         public void resetResolvedLayoutDirection() {
2139             super.resetResolvedLayoutDirection();
2140             resetResolvedLayoutDirectionCount++;
2141         }
2142 
2143         @Override
resetResolvedTextDirection()2144         public void resetResolvedTextDirection() {
2145             super.resetResolvedTextDirection();
2146             resetResolvedTextDirectionCount++;
2147         }
2148 
2149         @Override
resetResolvedTextAlignment()2150         public void resetResolvedTextAlignment() {
2151             super.resetResolvedTextAlignment();
2152             resetResolvedTextAlignmentCount++;
2153         }
2154 
2155         @Override
resetResolvedPadding()2156         public void resetResolvedPadding() {
2157             super.resetResolvedPadding();
2158             resetResolvedPaddingCount++;
2159         }
2160 
2161         @Override
resetResolvedDrawables()2162         protected void resetResolvedDrawables() {
2163             super.resetResolvedDrawables();
2164             resetResolvedDrawablesCount++;
2165         }
2166     }
2167 
2168     static class MockView2 extends View {
2169 
MockView2(Context context)2170         public MockView2(Context context) {
2171             super(context);
2172         }
2173 
MockView2(Context context, AttributeSet attrs)2174         public MockView2(Context context, AttributeSet attrs) {
2175             super(context, attrs);
2176         }
2177 
MockView2(Context context, AttributeSet attrs, int defStyle)2178         public MockView2(Context context, AttributeSet attrs, int defStyle) {
2179             super(context, attrs, defStyle);
2180         }
2181 
2182         @Override
resetRtlProperties()2183         public void resetRtlProperties() {
2184             super.resetRtlProperties();
2185             resetRtlPropertiesCount++;
2186         }
2187 
2188         @Override
resetResolvedLayoutDirection()2189         public void resetResolvedLayoutDirection() {
2190             super.resetResolvedLayoutDirection();
2191             resetResolvedLayoutDirectionCount++;
2192         }
2193 
2194         @Override
resetResolvedTextDirection()2195         public void resetResolvedTextDirection() {
2196             super.resetResolvedTextDirection();
2197             resetResolvedTextDirectionCount++;
2198         }
2199 
2200         @Override
resetResolvedTextAlignment()2201         public void resetResolvedTextAlignment() {
2202             super.resetResolvedTextAlignment();
2203             resetResolvedTextAlignmentCount++;
2204         }
2205 
2206         @Override
resetResolvedPadding()2207         public void resetResolvedPadding() {
2208             super.resetResolvedPadding();
2209             resetResolvedPaddingCount++;
2210         }
2211 
2212         @Override
resetResolvedDrawables()2213         protected void resetResolvedDrawables() {
2214             super.resetResolvedDrawables();
2215             resetResolvedDrawablesCount++;
2216         }
2217     }
2218 
setResult(int resultCode)2219     public void setResult(int resultCode) {
2220         synchronized (mSync) {
2221             mSync.mHasNotify = true;
2222             mSync.notify();
2223             mResultCode = resultCode;
2224         }
2225     }
2226 }
2227