• 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.graphics.drawable.cts;
18 
19 import com.android.cts.stub.R;
20 
21 import dalvik.annotation.TestLevel;
22 import dalvik.annotation.TestTargetClass;
23 import dalvik.annotation.TestTargetNew;
24 import dalvik.annotation.TestTargets;
25 import dalvik.annotation.ToBeFixed;
26 
27 import org.xmlpull.v1.XmlPullParserException;
28 
29 import android.content.res.Resources;
30 import android.content.res.XmlResourceParser;
31 import android.graphics.Canvas;
32 import android.graphics.ColorFilter;
33 import android.graphics.Rect;
34 import android.graphics.drawable.BitmapDrawable;
35 import android.graphics.drawable.Drawable;
36 import android.graphics.drawable.ScaleDrawable;
37 import android.graphics.drawable.Drawable.Callback;
38 import android.graphics.drawable.Drawable.ConstantState;
39 import android.test.AndroidTestCase;
40 import android.util.AttributeSet;
41 import android.util.DisplayMetrics;
42 import android.util.StateSet;
43 import android.view.Gravity;
44 
45 import java.io.IOException;
46 
47 @TestTargetClass(android.graphics.drawable.ScaleDrawable.class)
48 public class ScaleDrawableTest extends AndroidTestCase {
49 
50     @TestTargets({
51         @TestTargetNew(
52             level = TestLevel.COMPLETE,
53             method = "ScaleDrawable",
54             args = {android.graphics.drawable.Drawable.class, int.class, float.class, float.class}
55         ),
56         @TestTargetNew(
57             level = TestLevel.COMPLETE,
58             method = "getDrawable",
59             args = {}
60         )
61     })
62     @SuppressWarnings("deprecation")
testConstructor()63     public void testConstructor() {
64         Drawable d = new BitmapDrawable();
65         ScaleDrawable scaleDrawable = new ScaleDrawable(d, Gravity.CENTER, 100, 200);
66         assertSame(d, scaleDrawable.getDrawable());
67 
68         new ScaleDrawable(null, -1, Float.MAX_VALUE, Float.MIN_VALUE);
69     }
70 
71     @TestTargetNew(
72         level = TestLevel.COMPLETE,
73         method = "invalidateDrawable",
74         args = {android.graphics.drawable.Drawable.class}
75     )
76     @SuppressWarnings("deprecation")
testInvalidateDrawable()77     public void testInvalidateDrawable() {
78         ScaleDrawable scaleDrawable = new ScaleDrawable(new BitmapDrawable(),
79                 Gravity.CENTER, 100, 200);
80 
81         MockCallback cb = new MockCallback();
82         scaleDrawable.setCallback(cb);
83         scaleDrawable.invalidateDrawable(null);
84         assertTrue(cb.hasCalledInvalidate());
85 
86         cb.reset();
87         scaleDrawable.invalidateDrawable(new BitmapDrawable());
88         assertTrue(cb.hasCalledInvalidate());
89 
90         cb.reset();
91         scaleDrawable.setCallback(null);
92         scaleDrawable.invalidateDrawable(null);
93         assertFalse(cb.hasCalledInvalidate());
94     }
95 
96     @TestTargetNew(
97         level = TestLevel.COMPLETE,
98         method = "scheduleDrawable",
99         args = {android.graphics.drawable.Drawable.class, java.lang.Runnable.class, long.class}
100     )
101     @SuppressWarnings("deprecation")
testScheduleDrawable()102     public void testScheduleDrawable() {
103         ScaleDrawable scaleDrawable = new ScaleDrawable(new BitmapDrawable(),
104                 Gravity.CENTER, 100, 200);
105 
106         MockCallback cb = new MockCallback();
107         scaleDrawable.setCallback(cb);
108         scaleDrawable.scheduleDrawable(null, null, 0);
109         assertTrue(cb.hasCalledSchedule());
110 
111         cb.reset();
112         scaleDrawable.scheduleDrawable(new BitmapDrawable(), new Runnable() {
113             public void run() {
114             }
115         }, 1000L);
116         assertTrue(cb.hasCalledSchedule());
117 
118         cb.reset();
119         scaleDrawable.setCallback(null);
120         scaleDrawable.scheduleDrawable(null, null, 0);
121         assertFalse(cb.hasCalledSchedule());
122     }
123 
124     @TestTargetNew(
125         level = TestLevel.COMPLETE,
126         method = "unscheduleDrawable",
127         args = {android.graphics.drawable.Drawable.class, java.lang.Runnable.class}
128     )
129     @SuppressWarnings("deprecation")
testUnscheduleDrawable()130     public void testUnscheduleDrawable() {
131         ScaleDrawable scaleDrawable = new ScaleDrawable(new BitmapDrawable(),
132                 Gravity.CENTER, 100, 200);
133 
134         MockCallback cb = new MockCallback();
135         scaleDrawable.setCallback(cb);
136         scaleDrawable.unscheduleDrawable(null, null);
137         assertTrue(cb.hasCalledUnschedule());
138 
139         cb.reset();
140         scaleDrawable.unscheduleDrawable(new BitmapDrawable(), new Runnable() {
141             public void run() {
142             }
143         });
144         assertTrue(cb.hasCalledUnschedule());
145 
146         cb.reset();
147         scaleDrawable.setCallback(null);
148         scaleDrawable.unscheduleDrawable(null, null);
149         assertFalse(cb.hasCalledUnschedule());
150     }
151 
152     private static class MockCallback implements Callback {
153         private boolean mCalledInvalidate;
154         private boolean mCalledSchedule;
155         private boolean mCalledUnschedule;
156 
invalidateDrawable(Drawable who)157         public void invalidateDrawable(Drawable who) {
158             mCalledInvalidate = true;
159         }
160 
scheduleDrawable(Drawable who, Runnable what, long when)161         public void scheduleDrawable(Drawable who, Runnable what, long when) {
162             mCalledSchedule = true;
163         }
164 
unscheduleDrawable(Drawable who, Runnable what)165         public void unscheduleDrawable(Drawable who, Runnable what) {
166             mCalledUnschedule = true;
167         }
168 
hasCalledInvalidate()169         public boolean hasCalledInvalidate() {
170             return mCalledInvalidate;
171         }
172 
hasCalledSchedule()173         public boolean hasCalledSchedule() {
174             return mCalledSchedule;
175         }
176 
hasCalledUnschedule()177         public boolean hasCalledUnschedule() {
178             return mCalledUnschedule;
179         }
180 
reset()181         public void reset() {
182             mCalledInvalidate = false;
183             mCalledSchedule = false;
184             mCalledUnschedule = false;
185         }
186     }
187 
188     @TestTargetNew(
189         level = TestLevel.COMPLETE,
190         method = "draw",
191         args = {android.graphics.Canvas.class}
192     )
testDraw()193     public void testDraw() {
194         MockDrawable mockDrawable = new MockDrawable();
195         ScaleDrawable scaleDrawable = new ScaleDrawable(mockDrawable, Gravity.CENTER, 100, 200);
196 
197         scaleDrawable.draw(new Canvas());
198         assertFalse(mockDrawable.hasCalledDraw());
199 
200         // this method will call the contained drawable's draw method
201         // if the contained drawable's level doesn't equal 0.
202         mockDrawable.setLevel(1);
203         scaleDrawable.draw(new Canvas());
204         assertTrue(mockDrawable.hasCalledDraw());
205 
206         mockDrawable.reset();
207         scaleDrawable.draw(null);
208         assertTrue(mockDrawable.hasCalledDraw());
209     }
210 
211     @TestTargetNew(
212         level = TestLevel.COMPLETE,
213         method = "getChangingConfigurations",
214         args = {}
215     )
testGetChangingConfigurations()216     public void testGetChangingConfigurations() {
217         final int SUPER_CONFIG = 1;
218         final int CONTAINED_DRAWABLE_CONFIG = 2;
219 
220         MockDrawable mockDrawable = new MockDrawable();
221         ScaleDrawable scaleDrawable = new ScaleDrawable(mockDrawable, Gravity.CENTER, 100, 200);
222 
223         assertEquals(0, scaleDrawable.getChangingConfigurations());
224 
225         mockDrawable.setChangingConfigurations(CONTAINED_DRAWABLE_CONFIG);
226         assertEquals(CONTAINED_DRAWABLE_CONFIG, scaleDrawable.getChangingConfigurations());
227 
228         scaleDrawable.setChangingConfigurations(SUPER_CONFIG);
229         assertEquals(SUPER_CONFIG | CONTAINED_DRAWABLE_CONFIG,
230                 scaleDrawable.getChangingConfigurations());
231     }
232 
233     @TestTargetNew(
234         level = TestLevel.COMPLETE,
235         method = "getPadding",
236         args = {android.graphics.Rect.class}
237     )
238     @ToBeFixed(bug = "1695243", explanation = "the javadoc for getPadding is incomplete." +
239             "1. not clear what is supposed to happen if padding is null.")
testGetPadding()240     public void testGetPadding() {
241         MockDrawable mockDrawable = new MockDrawable();
242         ScaleDrawable scaleDrawable = new ScaleDrawable(mockDrawable, Gravity.CENTER, 100, 200);
243 
244         // this method will call contained drawable's getPadding method.
245         scaleDrawable.getPadding(new Rect());
246         assertTrue(mockDrawable.hasCalledGetPadding());
247 
248         // input null as param
249         try {
250             scaleDrawable.getPadding(null);
251             fail("Should throw NullPointerException");
252         } catch (NullPointerException e) {
253         }
254     }
255 
256     @TestTargetNew(
257         level = TestLevel.COMPLETE,
258         method = "setVisible",
259         args = {boolean.class, boolean.class}
260     )
testSetVisible()261     public void testSetVisible() {
262         MockDrawable mockDrawable = new MockDrawable();
263         ScaleDrawable scaleDrawable = new ScaleDrawable(mockDrawable, Gravity.CENTER, 100, 200);
264         assertTrue(scaleDrawable.isVisible());
265 
266         assertTrue(scaleDrawable.setVisible(false, false));
267         assertFalse(scaleDrawable.isVisible());
268         assertTrue(mockDrawable.hasCalledSetVisible());
269 
270         mockDrawable.reset();
271         assertFalse(scaleDrawable.setVisible(false, false));
272         assertFalse(scaleDrawable.isVisible());
273         assertTrue(mockDrawable.hasCalledSetVisible());
274 
275         mockDrawable.reset();
276         assertTrue(scaleDrawable.setVisible(true, false));
277         assertTrue(scaleDrawable.isVisible());
278         assertTrue(mockDrawable.hasCalledSetVisible());
279     }
280 
281     @TestTargetNew(
282         level = TestLevel.COMPLETE,
283         method = "setAlpha",
284         args = {int.class}
285     )
testSetAlpha()286     public void testSetAlpha() {
287         MockDrawable mockDrawable = new MockDrawable();
288         ScaleDrawable scaleDrawable = new ScaleDrawable(mockDrawable, Gravity.CENTER, 100, 200);
289 
290         // this method will call contained drawable's setAlpha method.
291         scaleDrawable.setAlpha(100);
292         assertTrue(mockDrawable.hasCalledSetAlpha());
293 
294         mockDrawable.reset();
295         scaleDrawable.setAlpha(Integer.MAX_VALUE);
296         assertTrue(mockDrawable.hasCalledSetAlpha());
297 
298         mockDrawable.reset();
299         scaleDrawable.setAlpha(-1);
300         assertTrue(mockDrawable.hasCalledSetAlpha());
301     }
302 
303     @TestTargetNew(
304         level = TestLevel.COMPLETE,
305         method = "setColorFilter",
306         args = {android.graphics.ColorFilter.class}
307     )
testSetColorFilter()308     public void testSetColorFilter() {
309         MockDrawable mockDrawable = new MockDrawable();
310         ScaleDrawable scaleDrawable = new ScaleDrawable(mockDrawable, Gravity.CENTER, 100, 200);
311 
312         // this method will call contained drawable's setColorFilter method.
313         scaleDrawable.setColorFilter(new ColorFilter());
314         assertTrue(mockDrawable.hasCalledSetColorFilter());
315 
316         mockDrawable.reset();
317         scaleDrawable.setColorFilter(null);
318         assertTrue(mockDrawable.hasCalledSetColorFilter());
319     }
320 
321     @TestTargetNew(
322         level = TestLevel.COMPLETE,
323         method = "getOpacity",
324         args = {}
325     )
testGetOpacity()326     public void testGetOpacity() {
327         MockDrawable mockDrawable = new MockDrawable();
328         ScaleDrawable scaleDrawable = new ScaleDrawable(mockDrawable, Gravity.CENTER, 100, 200);
329 
330         // this method will call contained drawable's getOpacity method.
331         scaleDrawable.getOpacity();
332         assertTrue(mockDrawable.hasCalledGetOpacity());
333     }
334 
335     @TestTargetNew(
336         level = TestLevel.COMPLETE,
337         method = "isStateful",
338         args = {}
339     )
testIsStateful()340     public void testIsStateful() {
341         MockDrawable mockDrawable = new MockDrawable();
342         ScaleDrawable scaleDrawable = new ScaleDrawable(mockDrawable, Gravity.CENTER, 100, 200);
343 
344         // this method will call contained drawable's isStateful method.
345         scaleDrawable.isStateful();
346         assertTrue(mockDrawable.hasCalledIsStateful());
347     }
348 
349     @TestTargetNew(
350         level = TestLevel.COMPLETE,
351         method = "onStateChange",
352         args = {int[].class}
353     )
testOnStateChange()354     public void testOnStateChange() {
355         MockDrawable mockDrawable = new MockDrawable();
356         MockScaleDrawable mockScaleDrawable = new MockScaleDrawable(
357                 mockDrawable, Gravity.CENTER, 100, 200);
358 
359         assertFalse(mockScaleDrawable.onStateChange(StateSet.WILD_CARD));
360         assertTrue(mockDrawable.hasCalledSetState());
361         assertTrue(mockScaleDrawable.hasCalledOnBoundsChange());
362 
363         mockDrawable.reset();
364         mockScaleDrawable.reset();
365         assertFalse(mockScaleDrawable.onStateChange(null));
366         assertTrue(mockDrawable.hasCalledSetState());
367         assertTrue(mockScaleDrawable.hasCalledOnBoundsChange());
368     }
369 
370     @TestTargetNew(
371         level = TestLevel.COMPLETE,
372         method = "onLevelChange",
373         args = {int.class}
374     )
testOnLevelChange()375     public void testOnLevelChange() {
376         MockDrawable mockDrawable = new MockDrawable();
377         MockScaleDrawable mockScaleDrawable = new MockScaleDrawable(
378                 mockDrawable, Gravity.CENTER, 100, 200);
379 
380         assertTrue(mockScaleDrawable.onLevelChange(0));
381         assertFalse(mockDrawable.hasCalledOnLevelChange());
382         assertTrue(mockScaleDrawable.hasCalledOnBoundsChange());
383 
384         mockDrawable.reset();
385         mockScaleDrawable.reset();
386         assertTrue(mockScaleDrawable.onLevelChange(Integer.MIN_VALUE));
387         assertTrue(mockDrawable.hasCalledOnLevelChange());
388         assertTrue(mockScaleDrawable.hasCalledOnBoundsChange());
389     }
390 
391     @TestTargetNew(
392         level = TestLevel.COMPLETE,
393         method = "onBoundsChange",
394         args = {android.graphics.Rect.class}
395     )
testOnBoundsChange()396     public void testOnBoundsChange() {
397         MockDrawable mockDrawable = new MockDrawable();
398         float scaleWidth = 0.3f;
399         float scaleHeight = 0.3f;
400         MockScaleDrawable mockScaleDrawable = new MockScaleDrawable(
401                 mockDrawable, Gravity.LEFT, scaleWidth, scaleHeight);
402         Rect bounds = new Rect(2, 2, 26, 32);
403         mockDrawable.setBounds(bounds);
404         mockScaleDrawable.onBoundsChange(bounds);
405         Rect expected = new Rect();
406         Gravity.apply(Gravity.LEFT, bounds.width() - (int) (bounds.width() * scaleWidth),
407                 bounds.height() - (int) (bounds.height() * scaleHeight), bounds, expected);
408         assertEquals(expected.left, mockDrawable.getBounds().left);
409         assertEquals(expected.top, mockDrawable.getBounds().top);
410         assertEquals(expected.right, mockDrawable.getBounds().right);
411         assertEquals(expected.bottom, mockDrawable.getBounds().bottom);
412 
413         scaleWidth = 0.6f;
414         scaleHeight = 0.7f;
415         int level = 4000;
416         mockScaleDrawable = new MockScaleDrawable(
417                 mockDrawable, Gravity.BOTTOM | Gravity.RIGHT, scaleWidth, scaleHeight);
418         mockDrawable.setBounds(bounds);
419         mockScaleDrawable.setLevel(level);
420         mockScaleDrawable.onBoundsChange(bounds);
421         Gravity.apply(Gravity.BOTTOM | Gravity.RIGHT,
422                 bounds.width() - (int) (bounds.width() * scaleWidth * (10000 - level) / 10000),
423                 bounds.height() - (int) (bounds.height() * scaleHeight * (10000 - level) / 10000),
424                 bounds, expected);
425         assertEquals(expected.left, mockDrawable.getBounds().left);
426         assertEquals(expected.top, mockDrawable.getBounds().top);
427         assertEquals(expected.right, mockDrawable.getBounds().right);
428         assertEquals(expected.bottom, mockDrawable.getBounds().bottom);
429 
430         scaleWidth = 0f;
431         scaleHeight = -0.3f;
432         mockScaleDrawable = new MockScaleDrawable(
433                 mockDrawable, Gravity.BOTTOM | Gravity.RIGHT, scaleWidth, scaleHeight);
434         mockDrawable.setBounds(bounds);
435         mockScaleDrawable.onBoundsChange(bounds);
436         assertEquals(bounds.left, mockDrawable.getBounds().left);
437         assertEquals(bounds.top, mockDrawable.getBounds().top);
438         assertEquals(bounds.right, mockDrawable.getBounds().right);
439         assertEquals(bounds.bottom, mockDrawable.getBounds().bottom);
440 
441         scaleWidth = 1f;
442         scaleHeight = 1.7f;
443         mockScaleDrawable = new MockScaleDrawable(
444                 mockDrawable, Gravity.BOTTOM | Gravity.RIGHT, scaleWidth, scaleHeight);
445         mockDrawable.setBounds(bounds);
446         mockScaleDrawable.onBoundsChange(bounds);
447         assertEquals(bounds.left, mockDrawable.getBounds().left);
448         assertEquals(bounds.top, mockDrawable.getBounds().top);
449         assertEquals(bounds.right, mockDrawable.getBounds().right);
450         assertEquals(bounds.bottom, mockDrawable.getBounds().bottom);
451     }
452 
453     @TestTargetNew(
454         level = TestLevel.COMPLETE,
455         method = "getIntrinsicWidth",
456         args = {}
457     )
testGetIntrinsicWidth()458     public void testGetIntrinsicWidth() {
459         MockDrawable mockDrawable = new MockDrawable();
460         ScaleDrawable scaleDrawable = new ScaleDrawable(mockDrawable, Gravity.CENTER, 100, 200);
461 
462         // this method will call contained drawable's getIntrinsicWidth method.
463         scaleDrawable.getIntrinsicWidth();
464         assertTrue(mockDrawable.hasCalledGetIntrinsicWidth());
465     }
466 
467     @TestTargetNew(
468         level = TestLevel.COMPLETE,
469         method = "getIntrinsicHeight",
470         args = {}
471     )
testGetIntrinsicHeight()472     public void testGetIntrinsicHeight() {
473         MockDrawable mockDrawable = new MockDrawable();
474         ScaleDrawable scaleDrawable = new ScaleDrawable(mockDrawable, Gravity.CENTER, 100, 200);
475 
476         // this method will call contained drawable's getIntrinsicHeight method.
477         scaleDrawable.getIntrinsicHeight();
478         assertTrue(mockDrawable.hasCalledGetIntrinsicHeight());
479     }
480 
481     @TestTargetNew(
482         level = TestLevel.COMPLETE,
483         method = "getConstantState",
484         args = {}
485     )
486     @SuppressWarnings("deprecation")
testGetConstantState()487     public void testGetConstantState() {
488         ScaleDrawable scaleDrawable = new ScaleDrawable(new BitmapDrawable(),
489                 Gravity.CENTER, 100, 200);
490 
491         ConstantState constantState = scaleDrawable.getConstantState();
492         assertNotNull(constantState);
493         assertEquals(0, constantState.getChangingConfigurations());
494 
495         scaleDrawable.setChangingConfigurations(1);
496         constantState = scaleDrawable.getConstantState();
497         assertNotNull(constantState);
498         assertEquals(1, constantState.getChangingConfigurations());
499     }
500 
501     @TestTargetNew(
502         level = TestLevel.COMPLETE,
503         method = "inflate",
504         args = {android.content.res.Resources.class, org.xmlpull.v1.XmlPullParser.class,
505                 android.util.AttributeSet.class}
506     )
507     @ToBeFixed(bug = "1695243", explanation = "the javadoc for inflate is incomplete." +
508             "1. not clear what is supposed to happen if any parameter is null.")
509     @SuppressWarnings("deprecation")
testInflate()510     public void testInflate() throws XmlPullParserException, IOException {
511         ScaleDrawable scaleDrawable = new ScaleDrawable(new BitmapDrawable(),
512                 Gravity.RIGHT, 100, 200);
513 
514         Resources res = mContext.getResources();
515         XmlResourceParser parser = res.getXml(R.xml.scaledrawable);
516         AttributeSet attrs = DrawableTestUtils.getAttributeSet(parser, "scale_allattrs");
517         scaleDrawable.inflate(res, parser, attrs);
518         int bitmapSize = 48 * res.getDisplayMetrics().densityDpi / DisplayMetrics.DENSITY_DEFAULT;
519         assertEquals(bitmapSize, scaleDrawable.getIntrinsicWidth());
520         assertEquals(bitmapSize, scaleDrawable.getIntrinsicHeight());
521 
522         parser = res.getXml(R.xml.scaledrawable);
523         attrs = DrawableTestUtils.getAttributeSet(parser, "scale_nodrawable");
524         try {
525             scaleDrawable.inflate(res, parser, attrs);
526             fail("Should throw IllegalArgumentException");
527         } catch (IllegalArgumentException e) {
528         }
529 
530         try {
531             scaleDrawable.inflate(null, parser, attrs);
532             fail("Should throw NullPointerException if resource is null");
533         } catch (NullPointerException e) {
534         }
535 
536         try {
537             scaleDrawable.inflate(res, null, attrs);
538             fail("Should throw NullPointerException if parser is null");
539         } catch (NullPointerException e) {
540         }
541 
542         try {
543             scaleDrawable.inflate(res, parser, null);
544             fail("Should throw NullPointerException if attribute set is null");
545         } catch (NullPointerException e) {
546         }
547     }
548 
549     @TestTargetNew(
550         level = TestLevel.COMPLETE,
551         method = "mutate",
552         args = {}
553     )
testMutate()554     public void testMutate() {
555         Resources resources = mContext.getResources();
556         ScaleDrawable d1 = (ScaleDrawable) resources.getDrawable(R.drawable.scaledrawable);
557         ScaleDrawable d2 = (ScaleDrawable) resources.getDrawable(R.drawable.scaledrawable);
558         ScaleDrawable d3 = (ScaleDrawable) resources.getDrawable(R.drawable.scaledrawable);
559 
560         d1.setAlpha(100);
561         assertEquals(100, ((BitmapDrawable) d1.getDrawable()).getPaint().getAlpha());
562         assertEquals(100, ((BitmapDrawable) d2.getDrawable()).getPaint().getAlpha());
563         assertEquals(100, ((BitmapDrawable) d3.getDrawable()).getPaint().getAlpha());
564 
565         d1.mutate();
566         d1.setAlpha(200);
567         assertEquals(200, ((BitmapDrawable) d1.getDrawable()).getPaint().getAlpha());
568         assertEquals(100, ((BitmapDrawable) d2.getDrawable()).getPaint().getAlpha());
569         assertEquals(100, ((BitmapDrawable) d3.getDrawable()).getPaint().getAlpha());
570 
571         d2.setAlpha(50);
572         assertEquals(200, ((BitmapDrawable) d1.getDrawable()).getPaint().getAlpha());
573         assertEquals(50, ((BitmapDrawable) d2.getDrawable()).getPaint().getAlpha());
574         assertEquals(50, ((BitmapDrawable) d3.getDrawable()).getPaint().getAlpha());
575     }
576 
577     private static class MockDrawable extends Drawable {
578         private boolean mCalledDraw = false;
579         private boolean mCalledGetPadding = false;
580         private boolean mCalledSetVisible = false;
581         private boolean mCalledSetAlpha = false;
582         private boolean mCalledGetOpacity = false;
583         private boolean mCalledSetColorFilter = false;
584         private boolean mCalledIsStateful = false;
585         private boolean mCalledGetIntrinsicWidth = false;
586         private boolean mCalledGetIntrinsicHeight = false;
587         private boolean mCalledSetState = false;
588         private boolean mCalledOnLevelChange = false;
589 
590         @Override
draw(Canvas canvas)591         public void draw(Canvas canvas) {
592             mCalledDraw = true;
593         }
594 
595         @Override
getOpacity()596         public int getOpacity() {
597             mCalledGetOpacity = true;
598             return 0;
599         }
600 
601         @Override
setAlpha(int alpha)602         public void setAlpha(int alpha) {
603             mCalledSetAlpha = true;
604         }
605 
606         @Override
setColorFilter(ColorFilter cf)607         public void setColorFilter(ColorFilter cf) {
608             mCalledSetColorFilter = true;
609         }
610 
611         @Override
getPadding(Rect padding)612         public boolean getPadding(Rect padding) {
613             mCalledGetPadding = true;
614             return super.getPadding(padding);
615         }
616 
617         @Override
setVisible(boolean visible, boolean restart)618         public boolean setVisible(boolean visible, boolean restart) {
619             mCalledSetVisible = true;
620             return super.setVisible(visible, restart);
621         }
622 
623         @Override
isStateful()624         public boolean isStateful() {
625             mCalledIsStateful = true;
626             return super.isStateful();
627         }
628 
629         @Override
getIntrinsicWidth()630         public int getIntrinsicWidth() {
631             mCalledGetIntrinsicWidth = true;
632             return super.getIntrinsicWidth();
633         }
634 
635         @Override
getIntrinsicHeight()636         public int getIntrinsicHeight() {
637             mCalledGetIntrinsicHeight = true;
638             return super.getIntrinsicHeight();
639 
640         }
641 
642         @Override
setState(final int[] stateSet)643         public boolean setState(final int[] stateSet) {
644             mCalledSetState = true;
645             return super.setState(stateSet);
646         }
647 
648         @Override
onLevelChange(int level)649         protected boolean onLevelChange(int level) {
650             mCalledOnLevelChange = true;
651             return super.onLevelChange(level);
652         }
653 
hasCalledDraw()654         public boolean hasCalledDraw() {
655             return mCalledDraw;
656         }
657 
hasCalledGetPadding()658         public boolean hasCalledGetPadding() {
659             return mCalledGetPadding;
660         }
661 
hasCalledSetVisible()662         public boolean hasCalledSetVisible() {
663             return mCalledSetVisible;
664         }
665 
hasCalledSetAlpha()666         public boolean hasCalledSetAlpha() {
667             return mCalledSetAlpha;
668         }
669 
hasCalledGetOpacity()670         public boolean hasCalledGetOpacity() {
671             return mCalledGetOpacity;
672         }
673 
hasCalledSetColorFilter()674         public boolean hasCalledSetColorFilter() {
675             return mCalledSetColorFilter;
676         }
677 
hasCalledIsStateful()678         public boolean hasCalledIsStateful() {
679             return mCalledIsStateful;
680         }
681 
hasCalledGetIntrinsicWidth()682         public boolean hasCalledGetIntrinsicWidth() {
683             return mCalledGetIntrinsicWidth;
684         }
685 
hasCalledGetIntrinsicHeight()686         public boolean hasCalledGetIntrinsicHeight() {
687             return mCalledGetIntrinsicHeight;
688         }
689 
hasCalledSetState()690         public boolean hasCalledSetState() {
691             return mCalledSetState;
692         }
693 
hasCalledOnLevelChange()694         public boolean hasCalledOnLevelChange() {
695             return mCalledOnLevelChange;
696         }
697 
reset()698         public void reset() {
699             mCalledDraw = false;
700             mCalledGetPadding = false;
701             mCalledSetVisible = false;
702             mCalledSetAlpha = false;
703             mCalledGetOpacity = false;
704             mCalledSetColorFilter = false;
705             mCalledIsStateful = false;
706             mCalledGetIntrinsicWidth = false;
707             mCalledGetIntrinsicHeight = false;
708             mCalledSetState = false;
709             mCalledOnLevelChange = false;
710         }
711     }
712 
713     private static class MockScaleDrawable extends ScaleDrawable {
714         private boolean mCalledOnBoundsChange = false;
715 
MockScaleDrawable()716         MockScaleDrawable() {
717             super(null, Gravity.CENTER, 100, 200);
718         }
719 
MockScaleDrawable(Drawable drawable, int gravity, float scaleWidth, float scaleHeight)720         public MockScaleDrawable(Drawable drawable, int gravity,
721                 float scaleWidth, float scaleHeight) {
722             super(drawable, gravity, scaleWidth, scaleHeight);
723         }
724 
725         @Override
onStateChange(int[] state)726         protected boolean onStateChange(int[] state) {
727             return super.onStateChange(state);
728         }
729 
730         @Override
onLevelChange(int level)731         protected boolean onLevelChange(int level) {
732             return super.onLevelChange(level);
733         }
734 
735         @Override
onBoundsChange(Rect bounds)736         protected void onBoundsChange(Rect bounds) {
737             mCalledOnBoundsChange = true;
738             super.onBoundsChange(bounds);
739         }
740 
hasCalledOnBoundsChange()741         public boolean hasCalledOnBoundsChange() {
742             return mCalledOnBoundsChange;
743         }
744 
reset()745         public void reset() {
746             mCalledOnBoundsChange = false;
747         }
748     }
749 }
750