Home | History | Annotate | Download | only in cts
      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.File;
     25 import java.io.FileInputStream;
     26 import java.io.FileNotFoundException;
     27 import java.io.FileOutputStream;
     28 import java.io.IOException;
     29 import java.io.InputStream;
     30 import java.io.OutputStream;
     31 
     32 import android.content.ContentResolver;
     33 import android.content.res.Resources;
     34 import android.graphics.BitmapFactory;
     35 import android.graphics.Canvas;
     36 import android.graphics.ColorFilter;
     37 import android.graphics.PixelFormat;
     38 import android.graphics.PorterDuff;
     39 import android.graphics.Rect;
     40 import android.graphics.drawable.Drawable;
     41 import android.net.Uri;
     42 import android.test.AndroidTestCase;
     43 import android.util.AttributeSet;
     44 import android.util.StateSet;
     45 import android.util.TypedValue;
     46 import android.util.Xml;
     47 
     48 public class DrawableTest extends AndroidTestCase {
     49     Resources mResources;
     50 
     51     @Override
     52     protected void setUp() throws Exception {
     53         super.setUp();
     54 
     55         mResources = mContext.getResources();
     56     }
     57 
     58     public void testClearColorFilter() {
     59         MockDrawable mockDrawable = new MockDrawable();
     60         mockDrawable.clearColorFilter();
     61         assertNull(mockDrawable.getColorFilter());
     62 
     63         ColorFilter cf = new ColorFilter();
     64         mockDrawable.setColorFilter(cf);
     65         assertEquals(cf, mockDrawable.getColorFilter());
     66 
     67         mockDrawable.clearColorFilter();
     68         assertNull(mockDrawable.getColorFilter());
     69     }
     70 
     71     public void testCopyBounds() {
     72         MockDrawable mockDrawable = new MockDrawable();
     73         Rect rect1 = mockDrawable.copyBounds();
     74         Rect r1 = new Rect();
     75         mockDrawable.copyBounds(r1);
     76         assertEquals(0, rect1.bottom);
     77         assertEquals(0, rect1.left);
     78         assertEquals(0, rect1.right);
     79         assertEquals(0, rect1.top);
     80         assertEquals(0, r1.bottom);
     81         assertEquals(0, r1.left);
     82         assertEquals(0, r1.right);
     83         assertEquals(0, r1.top);
     84 
     85         mockDrawable.setBounds(10, 10, 100, 100);
     86         Rect rect2 = mockDrawable.copyBounds();
     87         Rect r2 = new Rect();
     88         mockDrawable.copyBounds(r2);
     89         assertEquals(100, rect2.bottom);
     90         assertEquals(10, rect2.left);
     91         assertEquals(100, rect2.right);
     92         assertEquals(10, rect2.top);
     93         assertEquals(100, r2.bottom);
     94         assertEquals(10, r2.left);
     95         assertEquals(100, r2.right);
     96         assertEquals(10, r2.top);
     97 
     98         mockDrawable.setBounds(new Rect(50, 50, 500, 500));
     99         Rect rect3 = mockDrawable.copyBounds();
    100         Rect r3 = new Rect();
    101         mockDrawable.copyBounds(r3);
    102         assertEquals(500, rect3.bottom);
    103         assertEquals(50, rect3.left);
    104         assertEquals(500, rect3.right);
    105         assertEquals(50, rect3.top);
    106         assertEquals(500, r3.bottom);
    107         assertEquals(50, r3.left);
    108         assertEquals(500, r3.right);
    109         assertEquals(50, r3.top);
    110 
    111         try {
    112             mockDrawable.copyBounds(null);
    113             fail("should throw NullPointerException.");
    114         } catch (NullPointerException e) {
    115         }
    116     }
    117 
    118     public void testCreateFromPath() throws IOException {
    119         assertNull(Drawable.createFromPath(null));
    120 
    121         Uri uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
    122                 mContext.getPackageName() + R.raw.testimage);
    123         assertNull(Drawable.createFromPath(uri.getPath()));
    124 
    125         File imageFile = new File(mContext.getFilesDir(), "tempimage.jpg");
    126         assertTrue(imageFile.createNewFile());
    127         assertTrue(imageFile.exists());
    128         writeSampleImage(imageFile);
    129 
    130         final String path = imageFile.getPath();
    131         Uri u = Uri.parse(path);
    132         assertNotNull(Drawable.createFromPath(u.toString()));
    133         assertTrue(imageFile.delete());
    134     }
    135 
    136     private void writeSampleImage(File imagefile) throws IOException {
    137         InputStream source = null;
    138         OutputStream target = null;
    139 
    140         try {
    141             source = mResources.openRawResource(R.raw.testimage);
    142             target = new FileOutputStream(imagefile);
    143 
    144             byte[] buffer = new byte[1024];
    145             for (int len = source.read(buffer); len >= 0; len = source.read(buffer)) {
    146                 target.write(buffer, 0, len);
    147             }
    148         } finally {
    149             if (target != null) {
    150                 target.close();
    151             }
    152 
    153             if (source != null) {
    154                 source.close();
    155             }
    156         }
    157     }
    158 
    159     public void testCreateFromStream() throws FileNotFoundException, IOException {
    160         FileInputStream inputEmptyStream = null;
    161         FileInputStream inputStream = null;
    162         File imageFile = null;
    163         OutputStream outputEmptyStream = null;
    164 
    165         assertNull(Drawable.createFromStream(null, "test.bmp"));
    166 
    167         File emptyFile = new File(mContext.getFilesDir(), "tempemptyimage.jpg");
    168 
    169         // write some random data.
    170         try {
    171             outputEmptyStream = new FileOutputStream(emptyFile);
    172             outputEmptyStream.write(10);
    173 
    174             inputEmptyStream = new FileInputStream(emptyFile);
    175             assertNull(Drawable.createFromStream(inputEmptyStream, "Sample"));
    176 
    177             imageFile = new File(mContext.getFilesDir(), "tempimage.jpg");
    178 
    179             writeSampleImage(imageFile);
    180 
    181             inputStream = new FileInputStream(imageFile);
    182             assertNotNull(Drawable.createFromStream(inputStream, "Sample"));
    183         } finally {
    184 
    185             if (null != outputEmptyStream) {
    186                 outputEmptyStream.close();
    187             }
    188             if (null != inputEmptyStream) {
    189                 inputEmptyStream.close();
    190             }
    191             if (null != inputStream) {
    192                 inputStream.close();
    193             }
    194             if (emptyFile.exists()) {
    195                 assertTrue(emptyFile.delete());
    196             }
    197             if (imageFile.exists()) {
    198                 assertTrue(imageFile.delete());
    199             }
    200         }
    201     }
    202 
    203     public void testCreateFromResourceStream1() throws FileNotFoundException, IOException {
    204         FileInputStream inputEmptyStream = null;
    205         FileInputStream inputStream = null;
    206         File imageFile = null;
    207         OutputStream outputEmptyStream = null;
    208 
    209         assertNull(Drawable.createFromResourceStream(null, null, inputStream, "test.bmp"));
    210 
    211         File emptyFile = new File(mContext.getFilesDir(), "tempemptyimage.jpg");
    212 
    213         // write some random data.
    214         try {
    215             outputEmptyStream = new FileOutputStream(emptyFile);
    216             outputEmptyStream.write(10);
    217 
    218             inputEmptyStream = new FileInputStream(emptyFile);
    219             assertNull(Drawable.createFromResourceStream(mResources, null, inputEmptyStream,
    220                     "Sample"));
    221 
    222             imageFile = new File(mContext.getFilesDir(), "tempimage.jpg");
    223 
    224             writeSampleImage(imageFile);
    225 
    226             inputStream = new FileInputStream(imageFile);
    227             final TypedValue value = new TypedValue();
    228             assertNotNull(Drawable.createFromResourceStream(mResources, value, inputStream,
    229                     "Sample"));
    230         } finally {
    231 
    232             if (null != outputEmptyStream) {
    233                 outputEmptyStream.close();
    234             }
    235             if (null != inputEmptyStream) {
    236                 inputEmptyStream.close();
    237             }
    238             if (null != inputStream) {
    239                 inputStream.close();
    240             }
    241             if (emptyFile.exists()) {
    242                 assertTrue(emptyFile.delete());
    243             }
    244             if (imageFile.exists()) {
    245                 assertTrue(imageFile.delete());
    246             }
    247         }
    248     }
    249 
    250     public void testCreateFromResourceStream2() throws FileNotFoundException, IOException {
    251         FileInputStream inputEmptyStream = null;
    252         FileInputStream inputStream = null;
    253         File imageFile = null;
    254         OutputStream outputEmptyStream = null;
    255 
    256         BitmapFactory.Options opt = new BitmapFactory.Options();
    257         opt.inScaled = false;
    258 
    259         assertNull(Drawable.createFromResourceStream(null, null, inputStream, "test.bmp", opt));
    260 
    261         File emptyFile = new File(mContext.getFilesDir(), "tempemptyimage.jpg");
    262 
    263         // write some random data.
    264         try {
    265             outputEmptyStream = new FileOutputStream(emptyFile);
    266             outputEmptyStream.write(10);
    267 
    268             inputEmptyStream = new FileInputStream(emptyFile);
    269             assertNull(Drawable.createFromResourceStream(mResources, null, inputEmptyStream,
    270                     "Sample", opt));
    271 
    272             imageFile = new File(mContext.getFilesDir(), "tempimage.jpg");
    273 
    274             writeSampleImage(imageFile);
    275 
    276             inputStream = new FileInputStream(imageFile);
    277             final TypedValue value = new TypedValue();
    278             assertNotNull(Drawable.createFromResourceStream(mResources, value, inputStream,
    279                     "Sample", opt));
    280         } finally {
    281 
    282             if (null != outputEmptyStream) {
    283                 outputEmptyStream.close();
    284             }
    285             if (null != inputEmptyStream) {
    286                 inputEmptyStream.close();
    287             }
    288             if (null != inputStream) {
    289                 inputStream.close();
    290             }
    291             if (emptyFile.exists()) {
    292                 assertTrue(emptyFile.delete());
    293             }
    294             if (imageFile.exists()) {
    295                 assertTrue(imageFile.delete());
    296             }
    297         }
    298     }
    299 
    300     public void testCreateFromXml() throws XmlPullParserException, IOException {
    301         XmlPullParser parser = mResources.getXml(R.drawable.gradientdrawable);
    302         Drawable drawable = Drawable.createFromXml(mResources, parser);
    303         // values from gradientdrawable.xml
    304         assertEquals(42, drawable.getIntrinsicWidth());
    305         assertEquals(63, drawable.getIntrinsicHeight());
    306     }
    307 
    308     public void testCreateFromXmlInner() throws XmlPullParserException, IOException {
    309         XmlPullParser parser = mResources.getXml(R.drawable.gradientdrawable);
    310         while (parser.next() != XmlPullParser.START_TAG) {
    311             // ignore event, just seek to first tag
    312         }
    313         AttributeSet attrs = Xml.asAttributeSet(parser);
    314         Drawable drawable = Drawable.createFromXmlInner(mResources, parser, attrs);
    315         assertNotNull(drawable);
    316 
    317         Drawable expected = mResources.getDrawable(R.drawable.gradientdrawable);
    318 
    319         assertEquals(expected.getIntrinsicWidth(), drawable.getIntrinsicWidth());
    320         assertEquals(expected.getIntrinsicHeight(), drawable.getIntrinsicHeight());
    321     }
    322 
    323     public void testAccessBounds() {
    324         MockDrawable mockDrawable = new MockDrawable();
    325         mockDrawable.setBounds(0, 0, 100, 100);
    326         Rect r = mockDrawable.getBounds();
    327         assertEquals(0, r.left);
    328         assertEquals(0, r.top);
    329         assertEquals(100, r.bottom);
    330         assertEquals(100, r.right);
    331 
    332         mockDrawable.setBounds(new Rect(10, 10, 150, 150));
    333         r = mockDrawable.getBounds();
    334         assertEquals(10, r.left);
    335         assertEquals(10, r.top);
    336         assertEquals(150, r.bottom);
    337         assertEquals(150, r.right);
    338 
    339         try {
    340             mockDrawable.setBounds(null);
    341             fail("should throw NullPointerException.");
    342         } catch (NullPointerException e) {
    343         }
    344     }
    345 
    346     public void testAccessChangingConfigurations() {
    347         MockDrawable mockDrawable = new MockDrawable();
    348         assertEquals(0, mockDrawable.getChangingConfigurations());
    349 
    350         mockDrawable.setChangingConfigurations(1);
    351         assertEquals(1, mockDrawable.getChangingConfigurations());
    352 
    353         mockDrawable.setChangingConfigurations(Integer.MAX_VALUE);
    354         assertEquals(Integer.MAX_VALUE, mockDrawable.getChangingConfigurations());
    355 
    356         mockDrawable.setChangingConfigurations(Integer.MIN_VALUE);
    357         assertEquals(Integer.MIN_VALUE, mockDrawable.getChangingConfigurations());
    358     }
    359 
    360     public void testGetConstantState() {
    361         MockDrawable mockDrawable = new MockDrawable();
    362         assertNull(mockDrawable.getConstantState());
    363     }
    364 
    365     public void testGetCurrent() {
    366         MockDrawable mockDrawable = new MockDrawable();
    367         assertSame(mockDrawable, mockDrawable.getCurrent());
    368     }
    369 
    370     public void testGetIntrinsicHeight() {
    371         MockDrawable mockDrawable = new MockDrawable();
    372         assertEquals(-1, mockDrawable.getIntrinsicHeight());
    373     }
    374 
    375     public void testGetIntrinsicWidth() {
    376         MockDrawable mockDrawable = new MockDrawable();
    377         assertEquals(-1, mockDrawable.getIntrinsicWidth());
    378     }
    379 
    380     public void testAccessLevel() {
    381         MockDrawable mockDrawable = new MockDrawable();
    382         assertEquals(0, mockDrawable.getLevel());
    383 
    384         assertFalse(mockDrawable.setLevel(10));
    385         assertEquals(10, mockDrawable.getLevel());
    386 
    387         assertFalse(mockDrawable.setLevel(20));
    388         assertEquals(20, mockDrawable.getLevel());
    389 
    390         assertFalse(mockDrawable.setLevel(0));
    391         assertEquals(0, mockDrawable.getLevel());
    392 
    393         assertFalse(mockDrawable.setLevel(10000));
    394         assertEquals(10000, mockDrawable.getLevel());
    395     }
    396 
    397     public void testGetMinimumHeight() {
    398         MockDrawable mockDrawable = new MockDrawable();
    399         assertEquals(0, mockDrawable.getMinimumHeight());
    400     }
    401 
    402     public void testGetMinimumWidth() {
    403         MockDrawable mockDrawable = new MockDrawable();
    404         assertEquals(0, mockDrawable.getMinimumWidth());
    405     }
    406 
    407     public void testGetPadding() {
    408         MockDrawable mockDrawable = new MockDrawable();
    409         Rect r = new Rect(10, 10, 20, 20);
    410         assertFalse(mockDrawable.getPadding(r));
    411         assertEquals(0, r.bottom);
    412         assertEquals(0, r.top);
    413         assertEquals(0, r.left);
    414         assertEquals(0, r.right);
    415 
    416         try {
    417             mockDrawable.getPadding(null);
    418             fail("should throw NullPointerException.");
    419         } catch (NullPointerException e) {
    420         }
    421     }
    422 
    423     public void testAccessState() {
    424         MockDrawable mockDrawable = new MockDrawable();
    425         assertEquals(StateSet.WILD_CARD, mockDrawable.getState());
    426 
    427         int[] states = new int[] {1, 2, 3};
    428         assertFalse(mockDrawable.setState(states));
    429         assertEquals(states, mockDrawable.getState());
    430 
    431         mockDrawable.setState(null);
    432     }
    433 
    434     public void testGetTransparentRegion() {
    435         MockDrawable mockDrawable = new MockDrawable();
    436         assertNull(mockDrawable.getTransparentRegion());
    437     }
    438 
    439     public void testInflate() throws XmlPullParserException, IOException {
    440         MockDrawable mockDrawable = new MockDrawable();
    441 
    442         XmlPullParser parser = mResources.getXml(R.xml.drawable_test);
    443         while (parser.next() != XmlPullParser.START_TAG) {
    444             // ignore event, just seek to first tag
    445         }
    446         AttributeSet attrs = Xml.asAttributeSet(parser);
    447 
    448         mockDrawable.inflate(mResources, parser, attrs);
    449         // visibility set to false in resource
    450         assertFalse(mockDrawable.isVisible());
    451     }
    452 
    453     public void testInvalidateSelf() {
    454         MockDrawable mockDrawable = new MockDrawable();
    455         // if setCallback() is not called, invalidateSelf() would do nothing,
    456         // so just call it to check whether it throws exceptions.
    457         mockDrawable.invalidateSelf();
    458 
    459         MockCallback mockCallback = new MockCallback();
    460         mockDrawable.setCallback(mockCallback);
    461         mockDrawable.invalidateSelf();
    462         assertEquals(mockDrawable, mockCallback.getInvalidateDrawable());
    463     }
    464 
    465     public void testIsStateful() {
    466         MockDrawable mockDrawable = new MockDrawable();
    467         assertFalse(mockDrawable.isStateful());
    468     }
    469 
    470     public void testVisible() {
    471         MockDrawable mockDrawable = new MockDrawable();
    472         assertTrue(mockDrawable.isVisible());
    473 
    474         assertTrue(mockDrawable.setVisible(false, false));
    475         assertFalse(mockDrawable.isVisible());
    476 
    477         assertFalse(mockDrawable.setVisible(false, false));
    478         assertFalse(mockDrawable.isVisible());
    479 
    480         assertTrue(mockDrawable.setVisible(true, false));
    481         assertTrue(mockDrawable.isVisible());
    482     }
    483 
    484     public void testOnBoundsChange() {
    485         MockDrawable mockDrawable = new MockDrawable();
    486 
    487         // onBoundsChange is a non-operation function.
    488         mockDrawable.onBoundsChange(new Rect(0, 0, 10, 10));
    489     }
    490 
    491     public void testOnLevelChange() {
    492         MockDrawable mockDrawable = new MockDrawable();
    493         assertFalse(mockDrawable.onLevelChange(0));
    494     }
    495 
    496     public void testOnStateChange() {
    497         MockDrawable mockDrawable = new MockDrawable();
    498         assertFalse(mockDrawable.onStateChange(null));
    499     }
    500 
    501     public void testResolveOpacity() {
    502         assertEquals(PixelFormat.TRANSLUCENT,
    503                 Drawable.resolveOpacity(PixelFormat.TRANSLUCENT, PixelFormat.TRANSLUCENT));
    504         assertEquals(PixelFormat.UNKNOWN,
    505                 Drawable.resolveOpacity(PixelFormat.UNKNOWN, PixelFormat.TRANSLUCENT));
    506         assertEquals(PixelFormat.TRANSLUCENT,
    507                 Drawable.resolveOpacity(PixelFormat.OPAQUE, PixelFormat.TRANSLUCENT));
    508         assertEquals(PixelFormat.TRANSPARENT,
    509                 Drawable.resolveOpacity(PixelFormat.OPAQUE, PixelFormat.TRANSPARENT));
    510         assertEquals(PixelFormat.OPAQUE,
    511                 Drawable.resolveOpacity(PixelFormat.RGB_888, PixelFormat.RGB_565));
    512     }
    513 
    514     public void testScheduleSelf() {
    515         MockDrawable mockDrawable = new MockDrawable();
    516         MockCallback mockCallback = new MockCallback();
    517         mockDrawable.setCallback(mockCallback);
    518         mockDrawable.scheduleSelf(null, 1000L);
    519         assertEquals(mockDrawable, mockCallback.getScheduleDrawable());
    520         assertNull(mockCallback.getRunnable());
    521         assertEquals(1000L, mockCallback.getWhen());
    522     }
    523 
    524     public void testSetCallback() {
    525         MockDrawable mockDrawable = new MockDrawable();
    526 
    527         MockCallback mockCallback = new MockCallback();
    528         mockDrawable.setCallback(mockCallback);
    529         mockDrawable.scheduleSelf(null, 1000L);
    530         assertEquals(mockDrawable, mockCallback.getScheduleDrawable());
    531         assertNull(mockCallback.getRunnable());
    532         assertEquals(1000L, mockCallback.getWhen());
    533     }
    534 
    535     public void testSetColorFilter() {
    536         MockDrawable mockDrawable = new MockDrawable();
    537 
    538         mockDrawable.setColorFilter(5, PorterDuff.Mode.CLEAR);
    539     }
    540 
    541     public void testSetDither() {
    542         MockDrawable mockDrawable = new MockDrawable();
    543 
    544         // setDither is a non-operation function.
    545         mockDrawable.setDither(false);
    546     }
    547 
    548     public void testSetFilterBitmap() {
    549         MockDrawable mockDrawable = new MockDrawable();
    550 
    551         // setFilterBitmap is a non-operation function.
    552         mockDrawable.setFilterBitmap(false);
    553     }
    554 
    555     public void testUnscheduleSelf() {
    556         MockDrawable mockDrawable = new MockDrawable();
    557         MockCallback mockCallback = new MockCallback();
    558         mockDrawable.setCallback(mockCallback);
    559         mockDrawable.unscheduleSelf(null);
    560         assertEquals(mockDrawable, mockCallback.getScheduleDrawable());
    561         assertNull(mockCallback.getRunnable());
    562     }
    563 
    564     public void testMutate() {
    565         MockDrawable mockDrawable = new MockDrawable();
    566 
    567         assertSame(mockDrawable, mockDrawable.mutate());
    568     }
    569 
    570     private static class MockDrawable extends Drawable {
    571         private ColorFilter mColorFilter;
    572 
    573         public void draw(Canvas canvas) {
    574         }
    575 
    576         public void setAlpha(int alpha) {
    577         }
    578 
    579         public void setColorFilter(ColorFilter cf) {
    580             mColorFilter = cf;
    581         }
    582 
    583         public ColorFilter getColorFilter() {
    584             return mColorFilter;
    585         }
    586 
    587         public int getOpacity() {
    588             return 0;
    589         }
    590 
    591         protected void onBoundsChange(Rect bounds) {
    592             super.onBoundsChange(bounds);
    593         }
    594 
    595         protected boolean onLevelChange(int level) {
    596             return super.onLevelChange(level);
    597         }
    598 
    599         protected boolean onStateChange(int[] state) {
    600             return super.onStateChange(state);
    601         }
    602     }
    603 
    604     private static class MockCallback implements Drawable.Callback {
    605         private Drawable mInvalidateDrawable;
    606         private Drawable mScheduleDrawable;
    607         private Runnable mRunnable;
    608         private long mWhen;
    609 
    610         public MockCallback() {
    611         }
    612 
    613         public Drawable getInvalidateDrawable() {
    614             return mInvalidateDrawable;
    615         }
    616 
    617         public Drawable getScheduleDrawable() {
    618             return mScheduleDrawable;
    619         }
    620 
    621         public Runnable getRunnable() {
    622             return mRunnable;
    623         }
    624 
    625         public long getWhen() {
    626             return mWhen;
    627         }
    628 
    629         public void invalidateDrawable(Drawable who) {
    630             mInvalidateDrawable = who;
    631         }
    632 
    633         public void scheduleDrawable(Drawable who, Runnable what, long when) {
    634             mScheduleDrawable = who;
    635             mRunnable = what;
    636             mWhen = when;
    637         }
    638 
    639         public void unscheduleDrawable(Drawable who, Runnable what) {
    640             mScheduleDrawable = who;
    641             mRunnable = what;
    642         }
    643     }
    644 }
    645