• 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.graphics.R;
20 
21 import org.xmlpull.v1.XmlPullParser;
22 import org.xmlpull.v1.XmlPullParserException;
23 
24 import java.io.IOException;
25 
26 import android.graphics.Bitmap;
27 import android.graphics.Bitmap.Config;
28 import android.graphics.Canvas;
29 import android.graphics.ColorFilter;
30 import android.graphics.PixelFormat;
31 import android.graphics.Rect;
32 import android.graphics.drawable.BitmapDrawable;
33 import android.graphics.drawable.ClipDrawable;
34 import android.graphics.drawable.Drawable;
35 import android.graphics.drawable.Drawable.ConstantState;
36 import android.test.AndroidTestCase;
37 import android.util.AttributeSet;
38 import android.util.StateSet;
39 import android.util.Xml;
40 import android.view.Gravity;
41 
42 public class ClipDrawableTest extends AndroidTestCase {
43     @SuppressWarnings("deprecation")
testClipDrawable()44     public void testClipDrawable() {
45         new ClipDrawable((Drawable) null, Gravity.BOTTOM, ClipDrawable.HORIZONTAL);
46 
47         BitmapDrawable bmpDrawable = new BitmapDrawable();
48         new ClipDrawable(bmpDrawable, Gravity.BOTTOM, ClipDrawable.HORIZONTAL);
49     }
50 
testDraw()51     public void testDraw() {
52         MockDrawable mockDrawable = new MockDrawable();
53         mockDrawable.setLevel(5000);
54         ClipDrawable clipDrawable = new ClipDrawable(mockDrawable,
55                 Gravity.BOTTOM, ClipDrawable.HORIZONTAL);
56         clipDrawable.setBounds(new Rect(0, 0, 100, 100));
57         clipDrawable.setLevel(5000);
58         assertFalse(mockDrawable.getCalledDraw());
59         clipDrawable.draw(new Canvas());
60         assertTrue(mockDrawable.getCalledDraw());
61 
62         try {
63             clipDrawable.draw(null);
64             fail("should throw NullPointerException.");
65         } catch (NullPointerException e) {
66         }
67     }
68 
testGetChangingConfigurations()69     public void testGetChangingConfigurations() {
70         MockDrawable mockDrawable = new MockDrawable();
71         ClipDrawable clipDrawable = new ClipDrawable(mockDrawable,
72                 Gravity.BOTTOM, ClipDrawable.HORIZONTAL);
73         assertEquals(0, clipDrawable.getChangingConfigurations());
74 
75         clipDrawable.setChangingConfigurations(1);
76         assertEquals(1, clipDrawable.getChangingConfigurations());
77 
78         mockDrawable.setChangingConfigurations(2);
79         clipDrawable = new ClipDrawable(mockDrawable,
80                 Gravity.BOTTOM, ClipDrawable.HORIZONTAL);
81         clipDrawable.setChangingConfigurations(1);
82         assertEquals(3, clipDrawable.getChangingConfigurations());
83     }
84 
testGetConstantState()85     public void testGetConstantState() {
86         MockDrawable mockDrawable = new MockDrawable();
87         ClipDrawable clipDrawable = new ClipDrawable(mockDrawable,
88                 Gravity.BOTTOM, ClipDrawable.HORIZONTAL);
89         assertNull(clipDrawable.getConstantState());
90 
91         mockDrawable.setConstantState(new MockConstantState());
92         clipDrawable = new ClipDrawable(mockDrawable, Gravity.BOTTOM, ClipDrawable.HORIZONTAL);
93         clipDrawable.setChangingConfigurations(1);
94         assertNotNull(clipDrawable.getConstantState());
95         assertEquals(1, clipDrawable.getConstantState().getChangingConfigurations());
96     }
97 
98     @SuppressWarnings("deprecation")
testGetIntrinsicHeight()99     public void testGetIntrinsicHeight() {
100         MockDrawable mockDrawable = new MockDrawable();
101         ClipDrawable clipDrawable = new ClipDrawable(mockDrawable,
102                 Gravity.BOTTOM, ClipDrawable.HORIZONTAL);
103         assertEquals(-1, clipDrawable.getIntrinsicHeight());
104 
105         Bitmap bitmap = Bitmap.createBitmap(100, 50, Config.RGB_565);
106         BitmapDrawable bmpDrawable = new BitmapDrawable(bitmap);
107         bmpDrawable.setTargetDensity(bitmap.getDensity()); // avoid scaling
108         clipDrawable = new ClipDrawable(bmpDrawable, Gravity.BOTTOM, ClipDrawable.HORIZONTAL);
109         assertEquals(50, clipDrawable.getIntrinsicHeight());
110     }
111 
112     @SuppressWarnings("deprecation")
testGetIntrinsicWidth()113     public void testGetIntrinsicWidth() {
114         MockDrawable mockDrawable = new MockDrawable();
115         ClipDrawable clipDrawable = new ClipDrawable(mockDrawable,
116                 Gravity.BOTTOM, ClipDrawable.HORIZONTAL);
117         assertEquals(-1, clipDrawable.getIntrinsicWidth());
118 
119         Bitmap bitmap = Bitmap.createBitmap(100, 50, Config.RGB_565);
120         BitmapDrawable bmpDrawable = new BitmapDrawable(bitmap);
121         bmpDrawable.setTargetDensity(bitmap.getDensity()); // avoid scaling
122         clipDrawable = new ClipDrawable(bmpDrawable, Gravity.BOTTOM, ClipDrawable.HORIZONTAL);
123         assertEquals(100, clipDrawable.getIntrinsicWidth());
124     }
125 
126     @SuppressWarnings("deprecation")
testGetOpacity()127     public void testGetOpacity() {
128         BitmapDrawable bmpDrawable =
129             new BitmapDrawable(Bitmap.createBitmap(100, 50, Config.RGB_565));
130         ClipDrawable clipDrawable = new ClipDrawable(bmpDrawable,
131                 Gravity.BOTTOM, ClipDrawable.HORIZONTAL);
132         assertEquals(PixelFormat.OPAQUE, clipDrawable.getOpacity());
133 
134         bmpDrawable = new BitmapDrawable(Bitmap.createBitmap(100, 50, Config.RGB_565));
135         bmpDrawable.setGravity(Gravity.CENTER);
136         clipDrawable = new ClipDrawable(bmpDrawable, Gravity.BOTTOM, ClipDrawable.HORIZONTAL);
137         assertEquals(PixelFormat.TRANSLUCENT, clipDrawable.getOpacity());
138     }
139 
testGetPadding()140     public void testGetPadding() {
141         MockDrawable mockDrawable = new MockDrawable();
142         ClipDrawable clipDrawable = new ClipDrawable(mockDrawable,
143                 Gravity.BOTTOM, ClipDrawable.HORIZONTAL);
144         Rect padding = new Rect(10, 10, 100, 100);
145         assertFalse(clipDrawable.getPadding(padding));
146         assertEquals(0, padding.left);
147         assertEquals(0, padding.top);
148         assertEquals(0, padding.bottom);
149         assertEquals(0, padding.right);
150 
151         try {
152             clipDrawable.getPadding(null);
153             fail("should throw NullPointerException.");
154         } catch (NullPointerException e) {
155         }
156     }
157 
158     @SuppressWarnings("deprecation")
testInflate()159     public void testInflate() throws XmlPullParserException, IOException {
160         BitmapDrawable bmpDrawable = new BitmapDrawable();
161         ClipDrawable clipDrawable = new ClipDrawable(bmpDrawable,
162                 Gravity.BOTTOM, ClipDrawable.HORIZONTAL);
163 
164         XmlPullParser parser = mContext.getResources().getXml(R.drawable.gradientdrawable);
165         AttributeSet attrs = Xml.asAttributeSet(parser);
166         clipDrawable.inflate(mContext.getResources(), parser, attrs);
167     }
168 
testInvalidateDrawable()169     public void testInvalidateDrawable() {
170         MockDrawable mockDrawable = new MockDrawable();
171         ClipDrawable clipDrawable = new ClipDrawable(mockDrawable,
172                 Gravity.BOTTOM, ClipDrawable.HORIZONTAL);
173         MockCallback callback = new MockCallback();
174         clipDrawable.setCallback(callback);
175         clipDrawable.invalidateDrawable(mockDrawable);
176         assertSame(clipDrawable, callback.getInvalidateDrawable());
177 
178         clipDrawable.invalidateDrawable(null);
179     }
180 
181     @SuppressWarnings("deprecation")
testIsStateful()182     public void testIsStateful() {
183         MockDrawable mockDrawable = new MockDrawable();
184         ClipDrawable clipDrawable = new ClipDrawable(mockDrawable,
185                 Gravity.BOTTOM, ClipDrawable.HORIZONTAL);
186         assertFalse(clipDrawable.isStateful());
187 
188         BitmapDrawable bmpDrawable =
189                 new BitmapDrawable(Bitmap.createBitmap(100, 50, Config.RGB_565));
190         clipDrawable = new ClipDrawable(bmpDrawable, Gravity.BOTTOM, ClipDrawable.HORIZONTAL);
191         assertFalse(clipDrawable.isStateful());
192     }
193 
testOnBoundsChange()194     public void testOnBoundsChange() {
195         MockDrawable mockDrawable = new MockDrawable();
196         MockClipDrawable mockClipDrawable = new MockClipDrawable(mockDrawable,
197                 Gravity.BOTTOM, ClipDrawable.HORIZONTAL);
198         assertEquals(0, mockDrawable.getBounds().left);
199         assertEquals(0, mockDrawable.getBounds().top);
200         assertEquals(0, mockDrawable.getBounds().bottom);
201         assertEquals(0, mockDrawable.getBounds().right);
202         mockClipDrawable.onBoundsChange(new Rect(10, 10, 100, 100));
203         assertEquals(10, mockDrawable.getBounds().left);
204         assertEquals(10, mockDrawable.getBounds().top);
205         assertEquals(100, mockDrawable.getBounds().bottom);
206         assertEquals(100, mockDrawable.getBounds().right);
207 
208         try {
209             mockClipDrawable.onBoundsChange(null);
210             fail("should throw NullPointerException.");
211         } catch (NullPointerException e) {
212         }
213     }
214 
testOnLevelChange()215     public void testOnLevelChange() {
216         MockDrawable mockDrawable = new MockDrawable();
217         MockClipDrawable mockClipDrawable = new MockClipDrawable(mockDrawable,
218                 Gravity.BOTTOM, ClipDrawable.HORIZONTAL);
219         MockCallback callback = new MockCallback();
220         mockClipDrawable.setCallback(callback);
221 
222         assertEquals(0, mockDrawable.getLevel());
223         mockClipDrawable.onLevelChange(1000);
224         assertEquals(1000, mockDrawable.getLevel());
225         assertSame(mockClipDrawable, callback.getInvalidateDrawable());
226 
227         mockClipDrawable.onLevelChange(0);
228         assertEquals(0, mockDrawable.getLevel());
229 
230         mockClipDrawable.onLevelChange(10000);
231         assertEquals(10000, mockDrawable.getLevel());
232     }
233 
testOnStateChange()234     public void testOnStateChange() {
235         MockDrawable mockDrawable = new MockDrawable();
236         MockClipDrawable mockClipDrawable = new MockClipDrawable(mockDrawable,
237                 Gravity.BOTTOM, ClipDrawable.HORIZONTAL);
238         assertEquals(StateSet.WILD_CARD, mockDrawable.getState());
239 
240         int[] states = new int[] {1, 2, 3};
241         assertFalse(mockClipDrawable.onStateChange(states));
242         assertEquals(states, mockDrawable.getState());
243 
244         mockClipDrawable.onStateChange(null);
245     }
246 
testScheduleDrawable()247     public void testScheduleDrawable() {
248         MockDrawable mockDrawable = new MockDrawable();
249         ClipDrawable clipDrawable = new ClipDrawable(mockDrawable,
250                 Gravity.BOTTOM, ClipDrawable.HORIZONTAL);
251         MockCallback callback = new MockCallback();
252         clipDrawable.setCallback(callback);
253         clipDrawable.scheduleDrawable(mockDrawable, null, 1000L);
254         assertEquals(clipDrawable, callback.getScheduleDrawable());
255         assertNull(callback.getRunnable());
256         assertEquals(1000L, callback.getWhen());
257     }
258 
testSetAlpha()259     public void testSetAlpha() {
260         MockDrawable mockDrawable = new MockDrawable();
261         ClipDrawable clipDrawable = new ClipDrawable(mockDrawable,
262                 Gravity.BOTTOM, ClipDrawable.HORIZONTAL);
263 
264         clipDrawable.setAlpha(0);
265         assertEquals(0, mockDrawable.getAlpha());
266 
267         clipDrawable.setAlpha(128);
268         assertEquals(128, mockDrawable.getAlpha());
269 
270         clipDrawable.setAlpha(255);
271         assertEquals(255, mockDrawable.getAlpha());
272     }
273 
testSetColorFilter()274     public void testSetColorFilter() {
275         MockDrawable mockDrawable = new MockDrawable();
276         ClipDrawable clipDrawable = new ClipDrawable(mockDrawable,
277                 Gravity.BOTTOM, ClipDrawable.HORIZONTAL);
278 
279         ColorFilter cf = new ColorFilter();
280         clipDrawable.setColorFilter(cf);
281         assertSame(cf, mockDrawable.getColorFilter());
282 
283         clipDrawable.setColorFilter(null);
284         assertNull(mockDrawable.getColorFilter());
285     }
286 
testSetVisible()287     public void testSetVisible() {
288         MockDrawable mockDrawable = new MockDrawable();
289         ClipDrawable clipDrawable = new ClipDrawable(mockDrawable,
290                 Gravity.BOTTOM, ClipDrawable.HORIZONTAL);
291         assertTrue(clipDrawable.isVisible());
292 
293         assertTrue(clipDrawable.setVisible(false, false));
294         assertFalse(clipDrawable.isVisible());
295 
296         assertFalse(clipDrawable.setVisible(false, false));
297         assertFalse(clipDrawable.isVisible());
298 
299         assertTrue(clipDrawable.setVisible(true, false));
300         assertTrue(clipDrawable.isVisible());
301     }
302 
testUnscheduleDrawable()303     public void testUnscheduleDrawable() {
304         MockDrawable mockDrawable = new MockDrawable();
305         ClipDrawable clipDrawable = new ClipDrawable(mockDrawable,
306                 Gravity.BOTTOM, ClipDrawable.HORIZONTAL);
307         MockCallback callback = new MockCallback();
308         clipDrawable.setCallback(callback);
309         clipDrawable.unscheduleDrawable(mockDrawable, null);
310         assertEquals(clipDrawable, callback.getScheduleDrawable());
311         assertNull(callback.getRunnable());
312     }
313 
314     private class MockClipDrawable extends ClipDrawable {
MockClipDrawable(Drawable drawable, int gravity, int orientation)315         public MockClipDrawable(Drawable drawable, int gravity, int orientation) {
316             super(drawable, gravity, orientation);
317         }
318 
319         @Override
onStateChange(int[] state)320         protected boolean onStateChange(int[] state) {
321             return super.onStateChange(state);
322         }
323 
324         @Override
onLevelChange(int level)325         protected boolean onLevelChange(int level) {
326             return super.onLevelChange(level);
327         }
328 
329         @Override
onBoundsChange(Rect bounds)330         protected void onBoundsChange(Rect bounds) {
331             super.onBoundsChange(bounds);
332         }
333     }
334 
335     private class MockDrawable extends Drawable {
336         private ColorFilter mColorFilter;
337         private ConstantState mConstantState;
338         private boolean mCalledDraw = false;
339         private int mAlpha;
340 
getCalledDraw()341         public boolean getCalledDraw() {
342             return mCalledDraw;
343         }
344 
draw(Canvas canvas)345         public void draw(Canvas canvas) {
346             mCalledDraw = true;
347         }
348 
setAlpha(int alpha)349         public void setAlpha(int alpha) {
350             mAlpha = alpha;
351         }
352 
getAlpha()353         public int getAlpha() {
354             return mAlpha;
355         }
356 
setColorFilter(ColorFilter cf)357         public void setColorFilter(ColorFilter cf) {
358             mColorFilter = cf;
359         }
360 
getColorFilter()361         public ColorFilter getColorFilter() {
362             return mColorFilter;
363         }
364 
getOpacity()365         public int getOpacity() {
366             return 0;
367         }
368 
onBoundsChange(Rect bounds)369         protected void onBoundsChange(Rect bounds) {
370             super.onBoundsChange(bounds);
371         }
372 
onLevelChange(int level)373         protected boolean onLevelChange(int level) {
374             return super.onLevelChange(level);
375         }
376 
onStateChange(int[] state)377         protected boolean onStateChange(int[] state) {
378             return super.onStateChange(state);
379         }
380 
getConstantState()381         public ConstantState getConstantState() {
382             return mConstantState;
383         }
384 
setConstantState(ConstantState cs)385         public void setConstantState(ConstantState cs) {
386             mConstantState = cs;
387         }
388     }
389 
390     private class MockConstantState extends ConstantState {
newDrawable()391         public Drawable newDrawable() {
392             return null;
393         }
394 
getChangingConfigurations()395         public int getChangingConfigurations() {
396             return 0;
397         }
398     }
399 
400     private class MockCallback implements Drawable.Callback {
401         private Drawable mInvalidateDrawable;
402         private Drawable mScheduleDrawable;
403         private Runnable mRunnable;
404         private long mWhen;
405 
getInvalidateDrawable()406         public Drawable getInvalidateDrawable() {
407             return mInvalidateDrawable;
408         }
409 
getScheduleDrawable()410         public Drawable getScheduleDrawable() {
411             return mScheduleDrawable;
412         }
413 
getRunnable()414         public Runnable getRunnable() {
415             return mRunnable;
416         }
417 
getWhen()418         public long getWhen() {
419             return mWhen;
420         }
421 
invalidateDrawable(Drawable who)422         public void invalidateDrawable(Drawable who) {
423             mInvalidateDrawable = who;
424         }
425 
scheduleDrawable(Drawable who, Runnable what, long when)426         public void scheduleDrawable(Drawable who, Runnable what, long when) {
427             mScheduleDrawable = who;
428             mRunnable = what;
429             mWhen = when;
430         }
431 
unscheduleDrawable(Drawable who, Runnable what)432         public void unscheduleDrawable(Drawable who, Runnable what) {
433             mScheduleDrawable = who;
434             mRunnable = what;
435         }
436 
getResolvedLayoutDirection(Drawable who)437         public int getResolvedLayoutDirection(Drawable who) {
438             return 0;
439         }
440     }
441 }
442