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.Context;
20 import android.content.res.TypedArray;
21 import android.graphics.Canvas;
22 import android.graphics.Rect;
23 import android.graphics.drawable.Drawable;
24 import android.os.Handler;
25 import android.os.Parcelable;
26 import android.util.AttributeSet;
27 import android.util.SparseArray;
28 import android.view.ContextMenu;
29 import android.view.KeyEvent;
30 import android.view.MotionEvent;
31 import android.view.View;
32 import android.view.ViewGroup;
33 import android.view.ViewParent;
34 import android.view.ContextMenu.ContextMenuInfo;
35 
36 public class MockView extends View {
37     private boolean mCalledOnCreateContextMenu = false;
38     private boolean mCalledOnAnimationStart = false;
39     private boolean mCalledOnAnimationEnd = false;
40     private boolean mCalledOnAttachedToWindow = false;
41     private boolean mCalledOnDetachedFromWindow = false;
42     private boolean mCalledOnCreateDrawableState = false;
43     private boolean mCalledDrawableStateChanged = false;
44     private boolean mCalledOnDraw = false;
45     private boolean mCalledDispatchDraw = false;
46     private boolean mCalledOnFinishInflate = false;
47     private boolean mCalledOnFocusChanged = false;
48     private boolean mCalledOnKeyDown = false;
49     private boolean mCalledOnKeyUp = false;
50     private boolean mCalledOnKeyMultiple = false;
51     private boolean mCalledOnKeyShortcut = false;
52     private boolean mCalledOnLayout = false;
53     private boolean mCalledOnMeasure = false;
54     private boolean mCalledOnSizeChanged = false;
55     private boolean mCalledOnSetAlpha = false;
56     private boolean mCalledOnTouchEvent = false;
57     private boolean mCalledOnTrackballEvent = false;
58     private boolean mCalledOnWindowFocusChanged = false;
59     private boolean mCalledDispatchRestoreInstanceState = false;
60     private boolean mCalledDispatchSaveInstanceState = false;
61     private boolean mCalledOnRestoreInstanceState = false;
62     private boolean mCalledOnSaveInstanceState = false;
63     private boolean mCalledOnWindowVisibilityChanged = false;
64     private boolean mCalledDispatchUnhandledMove = false;
65     private boolean mCalledDispatchWindowFocusChanged = false;
66     private boolean mCalledDispatchWindowVisibilityChanged =false;
67     private boolean mCalledOnScrollChanged = false;
68     private boolean mCalledInvalidate = false;
69     private boolean mCalledComputeScroll = false;
70     private boolean mCalledDispatchKeyEventPreIme = false;
71     private boolean mCalledOnKeyPreIme = false;
72 
73     private int mOldWidth = -1;
74     private int mOldHeight = -1;
75 
MockView(Context context)76     public MockView(Context context) {
77         super(context);
78     }
79 
MockView(Context context, AttributeSet attrs)80     public MockView(Context context, AttributeSet attrs) {
81         super(context, attrs, 0);
82     }
83 
MockView(Context context, AttributeSet attrs, int defStyle)84     public MockView(Context context, AttributeSet attrs, int defStyle) {
85         super(context, attrs, defStyle);
86     }
87 
88     @Override
onKeyShortcut(int keyCode, KeyEvent event)89     public boolean onKeyShortcut(int keyCode, KeyEvent event) {
90         mCalledOnKeyShortcut = true;
91         return super.onKeyShortcut(keyCode, event);
92     }
93 
hasCalledOnKeyShortcut()94     public boolean hasCalledOnKeyShortcut() {
95         return mCalledOnKeyShortcut;
96     }
97 
98     @Override
invalidate()99     public void invalidate() {
100         super.invalidate();
101         mCalledInvalidate = true;
102     }
103 
hasCalledInvalidate()104     public boolean hasCalledInvalidate() {
105         return mCalledInvalidate;
106     }
107 
setParent(ViewGroup parent)108     public void setParent(ViewGroup parent) {
109         parent.addView(this);
110     }
111 
getEnabledStateSet()112     public static int[] getEnabledStateSet() {
113         return ENABLED_STATE_SET;
114     }
115 
getPressedEnabledStateSet()116     public static int[] getPressedEnabledStateSet() {
117         return PRESSED_ENABLED_STATE_SET;
118     }
119 
120     @Override
isPaddingOffsetRequired()121     protected boolean isPaddingOffsetRequired() {
122         return super.isPaddingOffsetRequired();
123     }
124 
125     @Override
getBottomPaddingOffset()126     protected int getBottomPaddingOffset() {
127         return super.getBottomPaddingOffset();
128     }
129 
130     @Override
getLeftPaddingOffset()131     protected int getLeftPaddingOffset() {
132         return super.getLeftPaddingOffset();
133     }
134 
135     @Override
getRightPaddingOffset()136     protected int getRightPaddingOffset() {
137         return super.getRightPaddingOffset();
138     }
139 
140     @Override
getTopPaddingOffset()141     protected int getTopPaddingOffset() {
142         return super.getTopPaddingOffset();
143     }
144 
145     @Override
onAnimationEnd()146     protected void onAnimationEnd() {
147         super.onAnimationEnd();
148         mCalledOnAnimationEnd = true;
149     }
150 
151     @Override
onAnimationStart()152     protected void onAnimationStart() {
153         super.onAnimationStart();
154         mCalledOnAnimationStart = true;
155     }
156 
hasCalledOnAnimationStart()157     public boolean hasCalledOnAnimationStart() {
158         return mCalledOnAnimationStart;
159     }
160 
hasCalledOnAnimationEnd()161     public boolean hasCalledOnAnimationEnd() {
162         return mCalledOnAnimationEnd;
163     }
164 
165     @Override
getHorizontalScrollbarHeight()166     protected int getHorizontalScrollbarHeight() {
167         return super.getHorizontalScrollbarHeight();
168     }
169 
170     @Override
onKeyDown(int keyCode, KeyEvent event)171     public boolean onKeyDown(int keyCode, KeyEvent event) {
172         mCalledOnKeyDown = true;
173         return super.onKeyDown(keyCode, event);
174     }
175 
hasCalledOnKeyDown()176     public boolean hasCalledOnKeyDown() {
177         return mCalledOnKeyDown;
178     }
179 
180     @Override
onKeyMultiple(int keyCode, int repeatCount, KeyEvent event)181     public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
182         mCalledOnKeyMultiple = true;
183         return super.onKeyMultiple(keyCode, repeatCount, event);
184     }
185 
hasCalledOnKeyMultiple()186     public boolean hasCalledOnKeyMultiple() {
187         return mCalledOnKeyMultiple;
188     }
189 
190     @Override
onKeyUp(int keyCode, KeyEvent event)191     public boolean onKeyUp(int keyCode, KeyEvent event) {
192         mCalledOnKeyUp = true;
193         return super.onKeyUp(keyCode, event);
194     }
195 
hasCalledOnKeyUp()196     public boolean hasCalledOnKeyUp() {
197         return mCalledOnKeyUp;
198     }
199 
200     @Override
onTouchEvent(MotionEvent event)201     public boolean onTouchEvent(MotionEvent event) {
202         mCalledOnTouchEvent = true;
203         return super.onTouchEvent(event);
204     }
205 
206     @Override
onTrackballEvent(MotionEvent event)207     public boolean onTrackballEvent(MotionEvent event) {
208         mCalledOnTrackballEvent = true;
209         return super.onTrackballEvent(event);
210     }
211 
hasCalledOnTouchEvent()212     public boolean hasCalledOnTouchEvent() {
213         return mCalledOnTouchEvent;
214     }
215 
hasCalledOnTrackballEvent()216     public boolean hasCalledOnTrackballEvent() {
217         return mCalledOnTrackballEvent;
218     }
219 
220     @Override
getSuggestedMinimumHeight()221     protected int getSuggestedMinimumHeight() {
222         return super.getSuggestedMinimumHeight();
223     }
224 
225     @Override
getSuggestedMinimumWidth()226     protected int getSuggestedMinimumWidth() {
227         return super.getSuggestedMinimumWidth();
228     }
229 
230     @Override
verifyDrawable(Drawable who)231     protected boolean verifyDrawable(Drawable who) {
232         return super.verifyDrawable(who);
233     }
234 
235     @Override
computeHorizontalScrollExtent()236     protected int computeHorizontalScrollExtent() {
237         return super.computeHorizontalScrollExtent();
238     }
239 
240     @Override
computeHorizontalScrollOffset()241     protected int computeHorizontalScrollOffset() {
242         return super.computeHorizontalScrollOffset();
243     }
244 
245     @Override
computeHorizontalScrollRange()246     protected int computeHorizontalScrollRange() {
247         return super.computeHorizontalScrollRange();
248     }
249 
250     @Override
computeVerticalScrollExtent()251     protected int computeVerticalScrollExtent() {
252         return super.computeVerticalScrollExtent();
253     }
254 
255     @Override
computeVerticalScrollOffset()256     protected int computeVerticalScrollOffset() {
257         return super.computeVerticalScrollOffset();
258     }
259 
260     @Override
computeVerticalScrollRange()261     protected int computeVerticalScrollRange() {
262         return super.computeVerticalScrollRange();
263     }
264 
265     @Override
getLeftFadingEdgeStrength()266     protected float getLeftFadingEdgeStrength() {
267         return super.getLeftFadingEdgeStrength();
268     }
269 
270     @Override
getRightFadingEdgeStrength()271     protected float getRightFadingEdgeStrength() {
272         return super.getRightFadingEdgeStrength();
273     }
274 
275     @Override
getBottomFadingEdgeStrength()276     protected float getBottomFadingEdgeStrength() {
277         return super.getBottomFadingEdgeStrength();
278     }
279 
280     @Override
getTopFadingEdgeStrength()281     protected float getTopFadingEdgeStrength() {
282         return super.getTopFadingEdgeStrength();
283     }
284 
285     @Override
getContextMenuInfo()286     protected ContextMenuInfo getContextMenuInfo() {
287         return super.getContextMenuInfo();
288     }
289 
290     @Override
onCreateContextMenu(ContextMenu menu)291     protected void onCreateContextMenu(ContextMenu menu) {
292         super.onCreateContextMenu(menu);
293         mCalledOnCreateContextMenu = true;
294     }
295 
hasCalledOnCreateContextMenu()296     public boolean hasCalledOnCreateContextMenu() {
297         return mCalledOnCreateContextMenu;
298     }
299 
300     @Override
onScrollChanged(int l, int t, int oldl, int oldt)301     protected void onScrollChanged(int l, int t, int oldl, int oldt) {
302         super.onScrollChanged(l, t, oldl, oldt);
303         mCalledOnScrollChanged = true;
304     }
305 
hasCalledOnScrollChanged()306     public boolean hasCalledOnScrollChanged() {
307         return mCalledOnScrollChanged;
308     }
309 
310     @Override
onSizeChanged(int w, int h, int oldw, int oldh)311     protected void onSizeChanged(int w, int h, int oldw, int oldh) {
312         super.onSizeChanged(w, h, oldw, oldh);
313         mCalledOnSizeChanged = true;
314         mOldWidth = oldw;
315         mOldHeight = oldh;
316     }
317 
getOldWOnSizeChanged()318     public int getOldWOnSizeChanged() {
319         return mOldWidth;
320     }
321 
getOldHOnSizeChanged()322     public int getOldHOnSizeChanged() {
323         return mOldHeight;
324     }
325 
hasCalledOnSizeChanged()326     public boolean hasCalledOnSizeChanged() {
327         return mCalledOnSizeChanged;
328     }
329 
330     @Override
onLayout(boolean changed, int left, int top, int right, int bottom)331     protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
332         super.onLayout(changed, left, top, right, bottom);
333         mCalledOnLayout = true;
334     }
335 
hasCalledOnLayout()336     public boolean hasCalledOnLayout() {
337         return mCalledOnLayout;
338     }
339 
340     @Override
computeScroll()341     public void computeScroll() {
342         super.computeScroll();
343         mCalledComputeScroll = true;
344     }
345 
hasCalledComputeScroll()346     public boolean hasCalledComputeScroll() {
347         return mCalledComputeScroll;
348     }
349 
350     @Override
dispatchSetSelected(boolean selected)351     protected void dispatchSetSelected(boolean selected) {
352         super.dispatchSetSelected(selected);
353     }
354 
355     @Override
dispatchSetPressed(boolean pressed)356     protected void dispatchSetPressed(boolean pressed) {
357         super.dispatchSetPressed(pressed);
358     }
359 
360     @Override
dispatchWindowFocusChanged(boolean hasFocus)361     public void dispatchWindowFocusChanged(boolean hasFocus) {
362         super.dispatchWindowFocusChanged(hasFocus);
363         mCalledDispatchWindowFocusChanged = true;
364     }
365 
hasCalledDispatchWindowFocusChanged()366     public boolean hasCalledDispatchWindowFocusChanged() {
367         return mCalledDispatchWindowFocusChanged;
368     }
369 
370     @Override
fitSystemWindows(Rect insets)371     protected boolean fitSystemWindows(Rect insets) {
372         return super.fitSystemWindows(insets);
373     }
374 
setMeasuredDimensionWrapper(int measuredWidth, int measuredHeight)375     public void setMeasuredDimensionWrapper(int measuredWidth, int measuredHeight) {
376         super.setMeasuredDimension(measuredWidth, measuredHeight);
377     }
378 
379     @Override
getHandler()380     public Handler getHandler() {
381         return super.getHandler();
382     }
383 
384     @Override
getWindowAttachCount()385     protected int getWindowAttachCount() {
386         return super.getWindowAttachCount();
387     }
388 
389     @Override
dispatchDraw(Canvas canvas)390     protected void dispatchDraw(Canvas canvas) {
391         super.dispatchDraw(canvas);
392         mCalledDispatchDraw = true;
393     }
394 
hasCalledDispatchDraw()395     public boolean hasCalledDispatchDraw() {
396         return mCalledDispatchDraw;
397     }
398 
399     @Override
dispatchUnhandledMove(View focused, int direction)400     public boolean dispatchUnhandledMove(View focused, int direction) {
401         mCalledDispatchUnhandledMove = true;
402         return super.dispatchUnhandledMove(focused, direction);
403     }
404 
hasCalledDispatchUnhandledMove()405     public boolean hasCalledDispatchUnhandledMove() {
406         return mCalledDispatchUnhandledMove;
407     }
408 
409     @Override
onWindowVisibilityChanged(int visibility)410     protected void onWindowVisibilityChanged(int visibility) {
411         super.onWindowVisibilityChanged(visibility);
412         mCalledOnWindowVisibilityChanged = true;
413     }
414 
hasCalledOnWindowVisibilityChanged()415     public boolean hasCalledOnWindowVisibilityChanged() {
416         return mCalledOnWindowVisibilityChanged;
417     }
418 
419     @Override
dispatchWindowVisibilityChanged(int visibility)420     public void dispatchWindowVisibilityChanged(int visibility) {
421         super.dispatchWindowVisibilityChanged(visibility);
422         mCalledDispatchWindowVisibilityChanged = true;
423     }
424 
hasCalledDispatchWindowVisibilityChanged()425     public boolean hasCalledDispatchWindowVisibilityChanged() {
426         return mCalledDispatchWindowVisibilityChanged;
427     }
428 
429     @Override
onWindowFocusChanged(boolean hasWindowFocus)430     public void onWindowFocusChanged(boolean hasWindowFocus) {
431         mCalledOnWindowFocusChanged = true;
432         super.onWindowFocusChanged(hasWindowFocus);
433     }
434 
hasCalledOnWindowFocusChanged()435     public boolean hasCalledOnWindowFocusChanged() {
436         return mCalledOnWindowFocusChanged;
437     }
438 
mergeDrawableStatesWrapper(int[] baseState, int[] additionalState)439     protected int[] mergeDrawableStatesWrapper(int[] baseState, int[] additionalState) {
440         return super.mergeDrawableStates(baseState, additionalState);
441     }
442 
443     @Override
onAttachedToWindow()444     protected void onAttachedToWindow() {
445         super.onAttachedToWindow();
446         mCalledOnAttachedToWindow = true;
447     }
448 
hasCalledOnAttachedToWindow()449     public boolean hasCalledOnAttachedToWindow() {
450         return mCalledOnAttachedToWindow;
451     }
452 
453     @Override
onDetachedFromWindow()454     protected void onDetachedFromWindow() {
455         super.onDetachedFromWindow();
456         mCalledOnDetachedFromWindow = true;
457     }
458 
hasCalledOnDetachedFromWindow()459     public boolean hasCalledOnDetachedFromWindow() {
460         return mCalledOnDetachedFromWindow;
461     }
462 
463     @Override
onDraw(Canvas canvas)464     protected void onDraw(Canvas canvas) {
465         super.onDraw(canvas);
466         mCalledOnDraw = true;
467     }
468 
hasCalledOnDraw()469     public boolean hasCalledOnDraw() {
470         return mCalledOnDraw;
471     }
472 
473     @Override
onFinishInflate()474     protected void onFinishInflate() {
475         super.onFinishInflate();
476         mCalledOnFinishInflate = true;
477     }
478 
hasCalledOnFinishInflate()479     public boolean hasCalledOnFinishInflate() {
480         return mCalledOnFinishInflate;
481     }
482 
483     @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)484     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
485         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
486         mCalledOnMeasure = true;
487     }
488 
hasCalledOnMeasure()489     public boolean hasCalledOnMeasure() {
490         return mCalledOnMeasure;
491     }
492 
493     @Override
onSaveInstanceState()494     protected Parcelable onSaveInstanceState() {
495         mCalledOnSaveInstanceState = true;
496         return super.onSaveInstanceState();
497     }
498 
hasCalledOnSaveInstanceState()499     public boolean hasCalledOnSaveInstanceState() {
500         return mCalledOnSaveInstanceState;
501     }
502 
503     @Override
onRestoreInstanceState(Parcelable state)504     protected void onRestoreInstanceState(Parcelable state) {
505         super.onRestoreInstanceState(state);
506         mCalledOnRestoreInstanceState = true;
507     }
508 
hasCalledOnRestoreInstanceState()509     public boolean hasCalledOnRestoreInstanceState() {
510         return mCalledOnRestoreInstanceState;
511     }
512 
513     @Override
onSetAlpha(int alpha)514     protected boolean onSetAlpha(int alpha) {
515         mCalledOnSetAlpha = true;
516         return super.onSetAlpha(alpha);
517     }
518 
hasCalledOnSetAlpha()519     public boolean hasCalledOnSetAlpha() {
520         return mCalledOnSetAlpha;
521     }
522 
523     @Override
onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect)524     protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
525         super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
526         mCalledOnFocusChanged = true;
527     }
528 
hasCalledOnFocusChanged()529     public boolean hasCalledOnFocusChanged() {
530         return mCalledOnFocusChanged;
531     }
532 
533     @Override
onCreateDrawableState(int extraSpace)534     protected int[] onCreateDrawableState(int extraSpace) {
535         mCalledOnCreateDrawableState = true;
536         return super.onCreateDrawableState(extraSpace);
537     }
538 
hasCalledOnCreateDrawableState()539     public boolean hasCalledOnCreateDrawableState() {
540         return mCalledOnCreateDrawableState;
541     }
542 
543     @Override
playSoundEffect(int soundConstant)544     public void playSoundEffect(int soundConstant) {
545         super.playSoundEffect(soundConstant);
546     }
547 
548     @Override
dispatchRestoreInstanceState(SparseArray<Parcelable> container)549     protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
550         super.dispatchRestoreInstanceState(container);
551         mCalledDispatchRestoreInstanceState = true;
552     }
553 
hasCalledDispatchRestoreInstanceState()554     public boolean hasCalledDispatchRestoreInstanceState() {
555         return mCalledDispatchRestoreInstanceState;
556     }
557 
558     @Override
dispatchSaveInstanceState(SparseArray<Parcelable> container)559     protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
560         super.dispatchSaveInstanceState(container);
561         mCalledDispatchSaveInstanceState = true;
562     }
563 
hasCalledDispatchSaveInstanceState()564     public boolean hasCalledDispatchSaveInstanceState() {
565         return mCalledDispatchSaveInstanceState;
566     }
567 
568     @Override
drawableStateChanged()569     protected void drawableStateChanged() {
570         super.drawableStateChanged();
571         mCalledDrawableStateChanged = true;
572     }
573 
hasCalledDrawableStateChanged()574     public boolean hasCalledDrawableStateChanged() {
575         return mCalledDrawableStateChanged;
576     }
577 
578     @Override
dispatchKeyEventPreIme(KeyEvent event)579     public boolean dispatchKeyEventPreIme(KeyEvent event) {
580         mCalledDispatchKeyEventPreIme = true;
581         return super.dispatchKeyEventPreIme(event);
582     }
583 
hasCalledDispatchKeyEventPreIme()584     public boolean hasCalledDispatchKeyEventPreIme() {
585         return mCalledDispatchKeyEventPreIme;
586     }
587 
588     @Override
onKeyPreIme(int keyCode, KeyEvent event)589     public boolean onKeyPreIme(int keyCode, KeyEvent event) {
590         mCalledOnKeyPreIme = true;
591         return super.onKeyPreIme(keyCode, event);
592     }
593 
hasCalledOnKeyPreIme()594     public boolean hasCalledOnKeyPreIme() {
595         return mCalledOnKeyPreIme;
596     }
597 
reset()598     public void reset() {
599         mCalledOnCreateContextMenu = false;
600 
601         mCalledOnAnimationStart = false;
602         mCalledOnAnimationEnd = false;
603         mCalledOnAttachedToWindow = false;
604         mCalledOnDetachedFromWindow = false;
605         mCalledOnCreateDrawableState = false;
606         mCalledDrawableStateChanged = false;
607         mCalledOnDraw = false;
608         mCalledDispatchDraw = false;
609         mCalledOnFinishInflate = false;
610         mCalledOnFocusChanged = false;
611         mCalledOnKeyDown = false;
612         mCalledOnKeyUp = false;
613         mCalledOnKeyMultiple = false;
614         mCalledOnKeyShortcut = false;
615         mCalledOnLayout = false;
616         mCalledOnMeasure = false;
617         mCalledOnSizeChanged = false;
618         mCalledOnSetAlpha = false;
619         mCalledOnTouchEvent = false;
620         mCalledOnTrackballEvent = false;
621         mCalledOnWindowFocusChanged = false;
622         mCalledDispatchRestoreInstanceState = false;
623         mCalledDispatchSaveInstanceState = false;
624         mCalledOnRestoreInstanceState = false;
625         mCalledOnSaveInstanceState = false;
626         mCalledDispatchUnhandledMove = false;
627         mCalledDispatchWindowFocusChanged = false;
628         mCalledDispatchWindowVisibilityChanged = false;
629         mCalledOnWindowVisibilityChanged = false;
630         mCalledOnScrollChanged = false;
631         mCalledInvalidate = false;
632         mCalledComputeScroll = false;
633         mCalledDispatchKeyEventPreIme = false;
634         mCalledOnKeyPreIme = false;
635 
636         mOldWidth = -1;
637         mOldHeight = -1;
638     }
639 }
640