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.ToBeFixed; 25 26 import org.xmlpull.v1.XmlPullParser; 27 import org.xmlpull.v1.XmlPullParserException; 28 29 import android.graphics.Bitmap; 30 import android.graphics.Canvas; 31 import android.graphics.ColorFilter; 32 import android.graphics.PixelFormat; 33 import android.graphics.Rect; 34 import android.graphics.Bitmap.Config; 35 import android.graphics.drawable.BitmapDrawable; 36 import android.graphics.drawable.ClipDrawable; 37 import android.graphics.drawable.Drawable; 38 import android.graphics.drawable.Drawable.Callback; 39 import android.graphics.drawable.Drawable.ConstantState; 40 import android.test.AndroidTestCase; 41 import android.util.AttributeSet; 42 import android.util.StateSet; 43 import android.util.Xml; 44 import android.view.Gravity; 45 46 import java.io.IOException; 47 48 @TestTargetClass(android.graphics.drawable.ClipDrawable.class) 49 public class ClipDrawableTest extends AndroidTestCase { 50 @TestTargetNew( 51 level = TestLevel.COMPLETE, 52 notes = "Test Constructor", 53 method = "ClipDrawable", 54 args = {android.graphics.drawable.Drawable.class, int.class, int.class} 55 ) 56 @SuppressWarnings("deprecation") testClipDrawable()57 public void testClipDrawable() { 58 new ClipDrawable((Drawable) null, Gravity.BOTTOM, ClipDrawable.HORIZONTAL); 59 60 BitmapDrawable bmpDrawable = new BitmapDrawable(); 61 new ClipDrawable(bmpDrawable, Gravity.BOTTOM, ClipDrawable.HORIZONTAL); 62 } 63 64 @TestTargetNew( 65 level = TestLevel.COMPLETE, 66 notes = "Test draw(Canvas)", 67 method = "draw", 68 args = {android.graphics.Canvas.class} 69 ) 70 @ToBeFixed(bug = "1400249", explanation = "It will be tested by functional test, " + 71 "and NPE is not expected.") testDraw()72 public void testDraw() { 73 MockDrawable mockDrawable = new MockDrawable(); 74 mockDrawable.setLevel(5000); 75 ClipDrawable clipDrawable = new ClipDrawable(mockDrawable, 76 Gravity.BOTTOM, ClipDrawable.HORIZONTAL); 77 clipDrawable.setBounds(new Rect(0, 0, 100, 100)); 78 clipDrawable.setLevel(5000); 79 assertFalse(mockDrawable.getCalledDraw()); 80 clipDrawable.draw(new Canvas()); 81 assertTrue(mockDrawable.getCalledDraw()); 82 83 try { 84 clipDrawable.draw(null); 85 fail("should throw NullPointerException."); 86 } catch (NullPointerException e) { 87 } 88 } 89 90 @TestTargetNew( 91 level = TestLevel.COMPLETE, 92 notes = "Test getChangingConfigurations()", 93 method = "getChangingConfigurations", 94 args = {} 95 ) testGetChangingConfigurations()96 public void testGetChangingConfigurations() { 97 MockDrawable mockDrawable = new MockDrawable(); 98 ClipDrawable clipDrawable = new ClipDrawable(mockDrawable, 99 Gravity.BOTTOM, ClipDrawable.HORIZONTAL); 100 assertEquals(0, clipDrawable.getChangingConfigurations()); 101 102 clipDrawable.setChangingConfigurations(1); 103 assertEquals(1, clipDrawable.getChangingConfigurations()); 104 105 mockDrawable.setChangingConfigurations(2); 106 clipDrawable = new ClipDrawable(mockDrawable, 107 Gravity.BOTTOM, ClipDrawable.HORIZONTAL); 108 clipDrawable.setChangingConfigurations(1); 109 assertEquals(3, clipDrawable.getChangingConfigurations()); 110 } 111 112 @TestTargetNew( 113 level = TestLevel.COMPLETE, 114 notes = "Test getConstantState()", 115 method = "getConstantState", 116 args = {} 117 ) testGetConstantState()118 public void testGetConstantState() { 119 MockDrawable mockDrawable = new MockDrawable(); 120 ClipDrawable clipDrawable = new ClipDrawable(mockDrawable, 121 Gravity.BOTTOM, ClipDrawable.HORIZONTAL); 122 assertNull(clipDrawable.getConstantState()); 123 124 mockDrawable.setConstantState(new MockConstantState()); 125 clipDrawable = new ClipDrawable(mockDrawable, Gravity.BOTTOM, ClipDrawable.HORIZONTAL); 126 clipDrawable.setChangingConfigurations(1); 127 assertNotNull(clipDrawable.getConstantState()); 128 assertEquals(1, clipDrawable.getConstantState().getChangingConfigurations()); 129 } 130 131 @TestTargetNew( 132 level = TestLevel.COMPLETE, 133 notes = "Test getIntrinsicHeight()", 134 method = "getIntrinsicHeight", 135 args = {} 136 ) 137 @SuppressWarnings("deprecation") testGetIntrinsicHeight()138 public void testGetIntrinsicHeight() { 139 MockDrawable mockDrawable = new MockDrawable(); 140 ClipDrawable clipDrawable = new ClipDrawable(mockDrawable, 141 Gravity.BOTTOM, ClipDrawable.HORIZONTAL); 142 assertEquals(-1, clipDrawable.getIntrinsicHeight()); 143 144 Bitmap bitmap = Bitmap.createBitmap(100, 50, Config.RGB_565); 145 BitmapDrawable bmpDrawable = new BitmapDrawable(bitmap); 146 bmpDrawable.setTargetDensity(bitmap.getDensity()); // avoid scaling 147 clipDrawable = new ClipDrawable(bmpDrawable, Gravity.BOTTOM, ClipDrawable.HORIZONTAL); 148 assertEquals(50, clipDrawable.getIntrinsicHeight()); 149 } 150 151 @TestTargetNew( 152 level = TestLevel.COMPLETE, 153 notes = "Test getIntrinsicWidth()", 154 method = "getIntrinsicWidth", 155 args = {} 156 ) 157 @SuppressWarnings("deprecation") testGetIntrinsicWidth()158 public void testGetIntrinsicWidth() { 159 MockDrawable mockDrawable = new MockDrawable(); 160 ClipDrawable clipDrawable = new ClipDrawable(mockDrawable, 161 Gravity.BOTTOM, ClipDrawable.HORIZONTAL); 162 assertEquals(-1, clipDrawable.getIntrinsicWidth()); 163 164 Bitmap bitmap = Bitmap.createBitmap(100, 50, Config.RGB_565); 165 BitmapDrawable bmpDrawable = new BitmapDrawable(bitmap); 166 bmpDrawable.setTargetDensity(bitmap.getDensity()); // avoid scaling 167 clipDrawable = new ClipDrawable(bmpDrawable, Gravity.BOTTOM, ClipDrawable.HORIZONTAL); 168 assertEquals(100, clipDrawable.getIntrinsicWidth()); 169 } 170 171 @TestTargetNew( 172 level = TestLevel.COMPLETE, 173 notes = "Test getOpacity()", 174 method = "getOpacity", 175 args = {} 176 ) 177 @SuppressWarnings("deprecation") testGetOpacity()178 public void testGetOpacity() { 179 BitmapDrawable bmpDrawable = 180 new BitmapDrawable(Bitmap.createBitmap(100, 50, Config.RGB_565)); 181 ClipDrawable clipDrawable = new ClipDrawable(bmpDrawable, 182 Gravity.BOTTOM, ClipDrawable.HORIZONTAL); 183 assertEquals(PixelFormat.OPAQUE, clipDrawable.getOpacity()); 184 185 bmpDrawable = new BitmapDrawable(Bitmap.createBitmap(100, 50, Config.RGB_565)); 186 bmpDrawable.setGravity(Gravity.CENTER); 187 clipDrawable = new ClipDrawable(bmpDrawable, Gravity.BOTTOM, ClipDrawable.HORIZONTAL); 188 assertEquals(PixelFormat.TRANSLUCENT, clipDrawable.getOpacity()); 189 } 190 191 @TestTargetNew( 192 level = TestLevel.COMPLETE, 193 notes = "Test getPadding(Rect)", 194 method = "getPadding", 195 args = {android.graphics.Rect.class} 196 ) 197 @ToBeFixed(bug = "1417734", explanation = "NPE is not expected.") testGetPadding()198 public void testGetPadding() { 199 MockDrawable mockDrawable = new MockDrawable(); 200 ClipDrawable clipDrawable = new ClipDrawable(mockDrawable, 201 Gravity.BOTTOM, ClipDrawable.HORIZONTAL); 202 Rect padding = new Rect(10, 10, 100, 100); 203 assertFalse(clipDrawable.getPadding(padding)); 204 assertEquals(0, padding.left); 205 assertEquals(0, padding.top); 206 assertEquals(0, padding.bottom); 207 assertEquals(0, padding.right); 208 209 try { 210 clipDrawable.getPadding(null); 211 fail("should throw NullPointerException."); 212 } catch (NullPointerException e) { 213 } 214 } 215 216 @TestTargetNew( 217 level = TestLevel.COMPLETE, 218 notes = "Test inflate(Resources, XmlPullParser, AttributeSet)", 219 method = "inflate", 220 args = {android.content.res.Resources.class, org.xmlpull.v1.XmlPullParser.class, 221 android.util.AttributeSet.class} 222 ) 223 @SuppressWarnings("deprecation") testInflate()224 public void testInflate() throws XmlPullParserException, IOException { 225 BitmapDrawable bmpDrawable = new BitmapDrawable(); 226 ClipDrawable clipDrawable = new ClipDrawable(bmpDrawable, 227 Gravity.BOTTOM, ClipDrawable.HORIZONTAL); 228 229 XmlPullParser parser = mContext.getResources().getXml(R.drawable.gradientdrawable); 230 AttributeSet attrs = Xml.asAttributeSet(parser); 231 clipDrawable.inflate(mContext.getResources(), parser, attrs); 232 } 233 234 @TestTargetNew( 235 level = TestLevel.COMPLETE, 236 notes = "Test invalidateDrawable(Drawable)", 237 method = "invalidateDrawable", 238 args = {android.graphics.drawable.Drawable.class} 239 ) testInvalidateDrawable()240 public void testInvalidateDrawable() { 241 MockDrawable mockDrawable = new MockDrawable(); 242 ClipDrawable clipDrawable = new ClipDrawable(mockDrawable, 243 Gravity.BOTTOM, ClipDrawable.HORIZONTAL); 244 MockCallback callback = new MockCallback(); 245 clipDrawable.setCallback(callback); 246 clipDrawable.invalidateDrawable(mockDrawable); 247 assertSame(clipDrawable, callback.getInvalidateDrawable()); 248 249 clipDrawable.invalidateDrawable(null); 250 } 251 252 @TestTargetNew( 253 level = TestLevel.COMPLETE, 254 notes = "Test isStateful()", 255 method = "isStateful", 256 args = {} 257 ) 258 @SuppressWarnings("deprecation") testIsStateful()259 public void testIsStateful() { 260 MockDrawable mockDrawable = new MockDrawable(); 261 ClipDrawable clipDrawable = new ClipDrawable(mockDrawable, 262 Gravity.BOTTOM, ClipDrawable.HORIZONTAL); 263 assertFalse(clipDrawable.isStateful()); 264 265 BitmapDrawable bmpDrawable = 266 new BitmapDrawable(Bitmap.createBitmap(100, 50, Config.RGB_565)); 267 clipDrawable = new ClipDrawable(bmpDrawable, Gravity.BOTTOM, ClipDrawable.HORIZONTAL); 268 assertFalse(clipDrawable.isStateful()); 269 } 270 271 @TestTargetNew( 272 level = TestLevel.COMPLETE, 273 notes = "Test onBoundsChange(Rect)", 274 method = "onBoundsChange", 275 args = {android.graphics.Rect.class} 276 ) 277 @ToBeFixed(bug = "1417734", explanation = "NPE is not expected.") testOnBoundsChange()278 public void testOnBoundsChange() { 279 MockDrawable mockDrawable = new MockDrawable(); 280 MockClipDrawable mockClipDrawable = new MockClipDrawable(mockDrawable, 281 Gravity.BOTTOM, ClipDrawable.HORIZONTAL); 282 assertEquals(0, mockDrawable.getBounds().left); 283 assertEquals(0, mockDrawable.getBounds().top); 284 assertEquals(0, mockDrawable.getBounds().bottom); 285 assertEquals(0, mockDrawable.getBounds().right); 286 mockClipDrawable.onBoundsChange(new Rect(10, 10, 100, 100)); 287 assertEquals(10, mockDrawable.getBounds().left); 288 assertEquals(10, mockDrawable.getBounds().top); 289 assertEquals(100, mockDrawable.getBounds().bottom); 290 assertEquals(100, mockDrawable.getBounds().right); 291 292 try { 293 mockClipDrawable.onBoundsChange(null); 294 fail("should throw NullPointerException."); 295 } catch (NullPointerException e) { 296 } 297 } 298 299 @TestTargetNew( 300 level = TestLevel.COMPLETE, 301 notes = "Test onLevelChange(int)", 302 method = "onLevelChange", 303 args = {int.class} 304 ) testOnLevelChange()305 public void testOnLevelChange() { 306 MockDrawable mockDrawable = new MockDrawable(); 307 MockClipDrawable mockClipDrawable = new MockClipDrawable(mockDrawable, 308 Gravity.BOTTOM, ClipDrawable.HORIZONTAL); 309 MockCallback callback = new MockCallback(); 310 mockClipDrawable.setCallback(callback); 311 312 assertEquals(0, mockDrawable.getLevel()); 313 mockClipDrawable.onLevelChange(1000); 314 assertEquals(1000, mockDrawable.getLevel()); 315 assertSame(mockClipDrawable, callback.getInvalidateDrawable()); 316 317 mockClipDrawable.onLevelChange(0); 318 assertEquals(0, mockDrawable.getLevel()); 319 320 mockClipDrawable.onLevelChange(10000); 321 assertEquals(10000, mockDrawable.getLevel()); 322 } 323 324 @TestTargetNew( 325 level = TestLevel.COMPLETE, 326 notes = "Test onStateChange(int[])", 327 method = "onStateChange", 328 args = {int[].class} 329 ) testOnStateChange()330 public void testOnStateChange() { 331 MockDrawable mockDrawable = new MockDrawable(); 332 MockClipDrawable mockClipDrawable = new MockClipDrawable(mockDrawable, 333 Gravity.BOTTOM, ClipDrawable.HORIZONTAL); 334 assertEquals(StateSet.WILD_CARD, mockDrawable.getState()); 335 336 int[] states = new int[] {1, 2, 3}; 337 assertFalse(mockClipDrawable.onStateChange(states)); 338 assertEquals(states, mockDrawable.getState()); 339 340 mockClipDrawable.onStateChange(null); 341 } 342 343 @TestTargetNew( 344 level = TestLevel.COMPLETE, 345 notes = "Test scheduleDrawable(Drawable, Runnable, long)", 346 method = "scheduleDrawable", 347 args = {android.graphics.drawable.Drawable.class, java.lang.Runnable.class, long.class} 348 ) testScheduleDrawable()349 public void testScheduleDrawable() { 350 MockDrawable mockDrawable = new MockDrawable(); 351 ClipDrawable clipDrawable = new ClipDrawable(mockDrawable, 352 Gravity.BOTTOM, ClipDrawable.HORIZONTAL); 353 MockCallback callback = new MockCallback(); 354 clipDrawable.setCallback(callback); 355 clipDrawable.scheduleDrawable(mockDrawable, null, 1000L); 356 assertEquals(clipDrawable, callback.getScheduleDrawable()); 357 assertNull(callback.getRunnable()); 358 assertEquals(1000L, callback.getWhen()); 359 } 360 361 @TestTargetNew( 362 level = TestLevel.COMPLETE, 363 notes = "Test setAlpha(int)", 364 method = "setAlpha", 365 args = {int.class} 366 ) testSetAlpha()367 public void testSetAlpha() { 368 MockDrawable mockDrawable = new MockDrawable(); 369 ClipDrawable clipDrawable = new ClipDrawable(mockDrawable, 370 Gravity.BOTTOM, ClipDrawable.HORIZONTAL); 371 372 clipDrawable.setAlpha(0); 373 assertEquals(0, mockDrawable.getAlpha()); 374 375 clipDrawable.setAlpha(128); 376 assertEquals(128, mockDrawable.getAlpha()); 377 378 clipDrawable.setAlpha(255); 379 assertEquals(255, mockDrawable.getAlpha()); 380 } 381 382 @TestTargetNew( 383 level = TestLevel.COMPLETE, 384 notes = "Test setColorFilter(ColorFilter)", 385 method = "setColorFilter", 386 args = {android.graphics.ColorFilter.class} 387 ) testSetColorFilter()388 public void testSetColorFilter() { 389 MockDrawable mockDrawable = new MockDrawable(); 390 ClipDrawable clipDrawable = new ClipDrawable(mockDrawable, 391 Gravity.BOTTOM, ClipDrawable.HORIZONTAL); 392 393 ColorFilter cf = new ColorFilter(); 394 clipDrawable.setColorFilter(cf); 395 assertSame(cf, mockDrawable.getColorFilter()); 396 397 clipDrawable.setColorFilter(null); 398 assertNull(mockDrawable.getColorFilter()); 399 } 400 401 @TestTargetNew( 402 level = TestLevel.COMPLETE, 403 notes = "Test setVisible(boolean, boolean)", 404 method = "setVisible", 405 args = {boolean.class, boolean.class} 406 ) testSetVisible()407 public void testSetVisible() { 408 MockDrawable mockDrawable = new MockDrawable(); 409 ClipDrawable clipDrawable = new ClipDrawable(mockDrawable, 410 Gravity.BOTTOM, ClipDrawable.HORIZONTAL); 411 assertTrue(clipDrawable.isVisible()); 412 413 assertTrue(clipDrawable.setVisible(false, false)); 414 assertFalse(clipDrawable.isVisible()); 415 416 assertFalse(clipDrawable.setVisible(false, false)); 417 assertFalse(clipDrawable.isVisible()); 418 419 assertTrue(clipDrawable.setVisible(true, false)); 420 assertTrue(clipDrawable.isVisible()); 421 } 422 423 @TestTargetNew( 424 level = TestLevel.COMPLETE, 425 notes = "Test unscheduleDrawable(Drawable, Runnable)", 426 method = "unscheduleDrawable", 427 args = {android.graphics.drawable.Drawable.class, java.lang.Runnable.class} 428 ) testUnscheduleDrawable()429 public void testUnscheduleDrawable() { 430 MockDrawable mockDrawable = new MockDrawable(); 431 ClipDrawable clipDrawable = new ClipDrawable(mockDrawable, 432 Gravity.BOTTOM, ClipDrawable.HORIZONTAL); 433 MockCallback callback = new MockCallback(); 434 clipDrawable.setCallback(callback); 435 clipDrawable.unscheduleDrawable(mockDrawable, null); 436 assertEquals(clipDrawable, callback.getScheduleDrawable()); 437 assertNull(callback.getRunnable()); 438 } 439 440 private class MockClipDrawable extends ClipDrawable { MockClipDrawable(Drawable drawable, int gravity, int orientation)441 public MockClipDrawable(Drawable drawable, int gravity, int orientation) { 442 super(drawable, gravity, orientation); 443 } 444 445 @Override onStateChange(int[] state)446 protected boolean onStateChange(int[] state) { 447 return super.onStateChange(state); 448 } 449 450 @Override onLevelChange(int level)451 protected boolean onLevelChange(int level) { 452 return super.onLevelChange(level); 453 } 454 455 @Override onBoundsChange(Rect bounds)456 protected void onBoundsChange(Rect bounds) { 457 super.onBoundsChange(bounds); 458 } 459 } 460 461 private class MockDrawable extends Drawable { 462 private ColorFilter mColorFilter; 463 private ConstantState mConstantState; 464 private boolean mCalledDraw = false; 465 private int mAlpha; 466 getCalledDraw()467 public boolean getCalledDraw() { 468 return mCalledDraw; 469 } 470 draw(Canvas canvas)471 public void draw(Canvas canvas) { 472 mCalledDraw = true; 473 } 474 setAlpha(int alpha)475 public void setAlpha(int alpha) { 476 mAlpha = alpha; 477 } 478 getAlpha()479 public int getAlpha() { 480 return mAlpha; 481 } 482 setColorFilter(ColorFilter cf)483 public void setColorFilter(ColorFilter cf) { 484 mColorFilter = cf; 485 } 486 getColorFilter()487 public ColorFilter getColorFilter() { 488 return mColorFilter; 489 } 490 getOpacity()491 public int getOpacity() { 492 return 0; 493 } 494 onBoundsChange(Rect bounds)495 protected void onBoundsChange(Rect bounds) { 496 super.onBoundsChange(bounds); 497 } 498 onLevelChange(int level)499 protected boolean onLevelChange(int level) { 500 return super.onLevelChange(level); 501 } 502 onStateChange(int[] state)503 protected boolean onStateChange(int[] state) { 504 return super.onStateChange(state); 505 } 506 getConstantState()507 public ConstantState getConstantState() { 508 return mConstantState; 509 } 510 setConstantState(ConstantState cs)511 public void setConstantState(ConstantState cs) { 512 mConstantState = cs; 513 } 514 } 515 516 private class MockConstantState extends ConstantState { newDrawable()517 public Drawable newDrawable() { 518 return null; 519 } 520 getChangingConfigurations()521 public int getChangingConfigurations() { 522 return 0; 523 } 524 } 525 526 private class MockCallback implements Callback { 527 private Drawable mInvalidateDrawable; 528 private Drawable mScheduleDrawable; 529 private Runnable mRunnable; 530 private long mWhen; 531 getInvalidateDrawable()532 public Drawable getInvalidateDrawable() { 533 return mInvalidateDrawable; 534 } 535 getScheduleDrawable()536 public Drawable getScheduleDrawable() { 537 return mScheduleDrawable; 538 } 539 getRunnable()540 public Runnable getRunnable() { 541 return mRunnable; 542 } 543 getWhen()544 public long getWhen() { 545 return mWhen; 546 } 547 invalidateDrawable(Drawable who)548 public void invalidateDrawable(Drawable who) { 549 mInvalidateDrawable = who; 550 } 551 scheduleDrawable(Drawable who, Runnable what, long when)552 public void scheduleDrawable(Drawable who, Runnable what, long when) { 553 mScheduleDrawable = who; 554 mRunnable = what; 555 mWhen = when; 556 } 557 unscheduleDrawable(Drawable who, Runnable what)558 public void unscheduleDrawable(Drawable who, Runnable what) { 559 mScheduleDrawable = who; 560 mRunnable = what; 561 } 562 } 563 } 564