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 package android.graphics.cts;
     17 
     18 import com.android.cts.stub.R;
     19 
     20 
     21 import android.content.res.Resources;
     22 import android.graphics.Bitmap;
     23 import android.graphics.BitmapFactory;
     24 import android.graphics.Canvas;
     25 import android.graphics.Color;
     26 import android.graphics.Matrix;
     27 import android.graphics.Paint;
     28 import android.graphics.Bitmap.CompressFormat;
     29 import android.graphics.Bitmap.Config;
     30 import android.os.Parcel;
     31 import android.test.AndroidTestCase;
     32 import android.util.DisplayMetrics;
     33 import android.widget.cts.WidgetTestUtils;
     34 
     35 import java.io.ByteArrayOutputStream;
     36 import java.nio.ByteBuffer;
     37 import java.nio.CharBuffer;
     38 import java.nio.IntBuffer;
     39 import java.nio.ShortBuffer;
     40 
     41 public class BitmapTest extends AndroidTestCase {
     42     private Resources mRes;
     43     private Bitmap mBitmap;
     44     private BitmapFactory.Options mOptions;
     45 
     46     @Override
     47     protected void setUp() throws Exception {
     48         super.setUp();
     49 
     50         mRes = getContext().getResources();
     51         mOptions = new BitmapFactory.Options();
     52         mOptions.inScaled = false;
     53         mBitmap = BitmapFactory.decodeResource(mRes, R.drawable.start, mOptions);
     54     }
     55 
     56     public void testCompress(){
     57         mBitmap.recycle();
     58 
     59         //abnormal case: the bitmap has been recycled
     60         try{
     61             mBitmap.compress(CompressFormat.JPEG, 0, null);
     62             fail("shouldn't come to here");
     63         }catch(IllegalStateException e){
     64         }
     65 
     66         mBitmap = BitmapFactory.decodeResource(mRes, R.drawable.start, mOptions);
     67 
     68         // abnormal case: out stream is null
     69         try{
     70             mBitmap.compress(CompressFormat.JPEG, 0, null);
     71             fail("shouldn't come to here");
     72         }catch(NullPointerException e){
     73         }
     74 
     75         // abnormal case: quality less than 0
     76         try{
     77             mBitmap.compress(CompressFormat.JPEG, -1, new ByteArrayOutputStream());
     78             fail("shouldn't come to here");
     79         }catch(IllegalArgumentException e){
     80         }
     81 
     82         // abnormal case: quality bigger than 100
     83         try{
     84             mBitmap.compress(CompressFormat.JPEG, 101, new ByteArrayOutputStream());
     85             fail("shouldn't come to here");
     86         }catch(IllegalArgumentException e){
     87         }
     88 
     89         //normal case
     90         assertTrue(mBitmap.compress(CompressFormat.JPEG, 50, new ByteArrayOutputStream()));
     91     }
     92 
     93     public void testCopy(){
     94         mBitmap.recycle();
     95 
     96         //abnormal case: the bitmap has been recycled
     97         try{
     98             mBitmap.copy(Config.RGB_565, false);
     99             fail("shouldn't come to here");
    100         }catch(IllegalStateException e){
    101             // expected
    102         }
    103 
    104         mBitmap = Bitmap.createBitmap(100, 100, Config.ARGB_8888);
    105         Bitmap bitmap = mBitmap.copy(Config.ARGB_8888, false);
    106         WidgetTestUtils.assertEquals(mBitmap, bitmap);
    107     }
    108 
    109     public void testCopyPixelsToBuffer(){
    110         final int pixSize = mBitmap.getRowBytes() * mBitmap.getHeight();
    111         final int tooSmall = pixSize / 2;
    112 
    113         // abnormal case: unsupported Buffer subclass
    114         try{
    115             mBitmap.copyPixelsToBuffer(CharBuffer.allocate(pixSize));
    116             fail("shouldn't come to here");
    117         }catch(RuntimeException e1){
    118         }
    119 
    120         // abnormal case: Buffer not large enough for pixels
    121         try{
    122             mBitmap.copyPixelsToBuffer(ByteBuffer.allocate(tooSmall));
    123             fail("shouldn't come to here");
    124         }catch(RuntimeException e2){
    125         }
    126 
    127         // normal case
    128         ByteBuffer byteBuf = ByteBuffer.allocate(pixSize);
    129         assertEquals(0, byteBuf.position());
    130         mBitmap.copyPixelsToBuffer(byteBuf);
    131         assertEquals(pixSize, byteBuf.position());
    132 
    133         // abnormal case: Buffer not large enough for pixels
    134         try{
    135             mBitmap.copyPixelsToBuffer(ByteBuffer.allocate(tooSmall));
    136             fail("shouldn't come to here");
    137         }catch(RuntimeException e3){
    138         }
    139 
    140         // normal case
    141         ShortBuffer shortBuf = ShortBuffer.allocate(pixSize);
    142         assertEquals(0, shortBuf.position());
    143         mBitmap.copyPixelsToBuffer(shortBuf);
    144         assertEquals(pixSize >> 1, shortBuf.position());
    145 
    146         // abnormal case: Buffer not large enough for pixels
    147         try{
    148             mBitmap.copyPixelsToBuffer(ByteBuffer.allocate(tooSmall));
    149             fail("shouldn't come to here");
    150         }catch(RuntimeException e4){
    151         }
    152 
    153         // normal case
    154         IntBuffer intBuf1 = IntBuffer.allocate(pixSize);
    155         assertEquals(0, intBuf1.position());
    156         mBitmap.copyPixelsToBuffer(intBuf1);
    157         assertEquals(pixSize >> 2, intBuf1.position());
    158 
    159         Bitmap bitmap = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(),
    160                 mBitmap.getConfig());
    161         bitmap.copyPixelsFromBuffer(intBuf1);
    162         IntBuffer intBuf2 = IntBuffer.allocate(pixSize);
    163         bitmap.copyPixelsToBuffer(intBuf2);
    164 
    165         assertEquals(intBuf1.position(), intBuf2.position());
    166         int size = intBuf1.position();
    167         intBuf1.position(0);
    168         intBuf2.position(0);
    169         for (int i = 0; i < size; i++) {
    170             assertEquals(intBuf1.get(), intBuf2.get());
    171         }
    172     }
    173 
    174     public void testCreateBitmap1(){
    175         int[] colors = createColors(100);
    176         Bitmap bitmap = Bitmap.createBitmap(colors, 10, 10, Config.RGB_565);
    177         Bitmap ret = Bitmap.createBitmap(bitmap);
    178         assertNotNull(ret);
    179         assertEquals(10, ret.getWidth());
    180         assertEquals(10, ret.getHeight());
    181         assertEquals(Config.RGB_565, ret.getConfig());
    182     }
    183 
    184     public void testCreateBitmap2(){
    185         //abnormal case: Illegal Argument
    186         try{
    187             Bitmap.createBitmap(mBitmap, -100, 50, 50, 200);
    188             fail("shouldn't come to here");
    189         }catch(IllegalArgumentException e){
    190         }
    191 
    192         // special case: output bitmap is equal to the input bitmap
    193         mBitmap = Bitmap.createBitmap(new int[100 * 100], 100, 100, Config.ARGB_8888);
    194         Bitmap ret = Bitmap.createBitmap(mBitmap, 0, 0, 100, 100);
    195         assertNotNull(ret);
    196         assertTrue(mBitmap.equals(ret));
    197 
    198         //normal case
    199         mBitmap = Bitmap.createBitmap(100, 100, Config.ARGB_8888);
    200         ret = Bitmap.createBitmap(mBitmap, 10, 10, 50, 50);
    201         assertNotNull(ret);
    202         assertFalse(mBitmap.equals(ret));
    203     }
    204 
    205     public void testCreateBitmap3(){
    206         mBitmap = Bitmap.createBitmap(100, 100, Config.ARGB_8888);
    207 
    208         //abnormal case: x and/or y less than 0
    209         try{
    210             Bitmap.createBitmap(mBitmap, -1, -1, 10, 10, null, false);
    211             fail("shouldn't come to here");
    212         }catch(IllegalArgumentException e){
    213         }
    214 
    215         //abnormal case: width and/or height less than 0
    216         try{
    217             Bitmap.createBitmap(mBitmap, 1, 1, -10, -10, null, false);
    218             fail("shouldn't come to here");
    219         }catch(IllegalArgumentException e){
    220         }
    221 
    222         //abnormal case: (x + width) bigger than source bitmap's width
    223         try{
    224             Bitmap.createBitmap(mBitmap, 10, 10, 95, 50, null, false);
    225             fail("shouldn't come to here");
    226         }catch(IllegalArgumentException e){
    227         }
    228 
    229         //abnormal case: (y + height) bigger than source bitmap's height
    230         try{
    231             Bitmap.createBitmap(mBitmap, 10, 10, 50, 95, null, false);
    232             fail("shouldn't come to here");
    233         }catch(IllegalArgumentException e){
    234         }
    235 
    236         // special case: output bitmap is equal to the input bitmap
    237         mBitmap = Bitmap.createBitmap(new int[100 * 100], 100, 100, Config.ARGB_8888);
    238         Bitmap ret = Bitmap.createBitmap(mBitmap, 0, 0, 100, 100, null, false);
    239         assertNotNull(ret);
    240         assertTrue(mBitmap.equals(ret));
    241 
    242         // normal case
    243         mBitmap = Bitmap.createBitmap(100, 100, Config.ARGB_8888);
    244         ret = Bitmap.createBitmap(mBitmap, 10, 10, 50, 50, new Matrix(), true);
    245         assertNotNull(ret);
    246         assertFalse(mBitmap.equals(ret));
    247     }
    248 
    249     public void testCreateBitmap4(){
    250         Bitmap ret = Bitmap.createBitmap(100, 200, Config.RGB_565);
    251         assertNotNull(ret);
    252         assertEquals(100, ret.getWidth());
    253         assertEquals(200, ret.getHeight());
    254         assertEquals(Config.RGB_565, ret.getConfig());
    255     }
    256 
    257     public void testCreateBitmap6(){
    258         int[] colors = createColors(100);
    259 
    260         //abnormal case: width and/or height less than 0
    261         try{
    262             Bitmap.createBitmap(colors, 0, 100, -1, 100, Config.RGB_565);
    263             fail("shouldn't come to here");
    264         }catch(IllegalArgumentException e){
    265         }
    266 
    267         //abnormal case: stride less than width and bigger than -width
    268         try{
    269             Bitmap.createBitmap(colors, 10, 10, 100, 100, Config.RGB_565);
    270             fail("shouldn't come to here");
    271         }catch(IllegalArgumentException e){
    272         }
    273 
    274         //abnormal case: offset less than 0
    275         try{
    276             Bitmap.createBitmap(colors, -10, 100, 100, 100, Config.RGB_565);
    277             fail("shouldn't come to here");
    278         }catch(ArrayIndexOutOfBoundsException e){
    279         }
    280 
    281         //abnormal case: (offset + width) bigger than colors' length
    282         try{
    283             Bitmap.createBitmap(colors, 10, 100, 100, 100, Config.RGB_565);
    284             fail("shouldn't come to here");
    285         }catch(ArrayIndexOutOfBoundsException e){
    286         }
    287 
    288         //abnormal case: (lastScanline + width) bigger than colors' length
    289         try{
    290             Bitmap.createBitmap(colors, 10, 100, 50, 100, Config.RGB_565);
    291             fail("shouldn't come to here");
    292         }catch(ArrayIndexOutOfBoundsException e){
    293         }
    294 
    295         // normal case
    296         Bitmap ret = Bitmap.createBitmap(colors, 5, 10, 10, 5, Config.RGB_565);
    297         assertNotNull(ret);
    298         assertEquals(10, ret.getWidth());
    299         assertEquals(5, ret.getHeight());
    300         assertEquals(Config.RGB_565, ret.getConfig());
    301     }
    302 
    303     public void testCreateScaledBitmap(){
    304         mBitmap = Bitmap.createBitmap(100, 200, Config.RGB_565);
    305         Bitmap ret = Bitmap.createScaledBitmap(mBitmap, 50, 100, false);
    306         assertNotNull(ret);
    307         assertEquals(50, ret.getWidth());
    308         assertEquals(100, ret.getHeight());
    309     }
    310 
    311     public void testDescribeContents(){
    312         assertEquals(0, mBitmap.describeContents());
    313     }
    314 
    315     public void testEraseColor(){
    316         mBitmap.recycle();
    317 
    318         //abnormal case: the bitmap has been recycled
    319         try{
    320             mBitmap.eraseColor(0);
    321             fail("shouldn't come to here");
    322         }catch(IllegalStateException e){
    323         }
    324 
    325         mBitmap = BitmapFactory.decodeResource(mRes, R.drawable.start, mOptions);
    326 
    327         //abnormal case: bitmap is immutable
    328         try{
    329             mBitmap.eraseColor(0);
    330             fail("shouldn't come to here");
    331         }catch(IllegalStateException e){
    332         }
    333 
    334         // normal case
    335         mBitmap = Bitmap.createBitmap(100, 100, Config.ARGB_8888);
    336         mBitmap.eraseColor(0xffff0000);
    337         assertEquals(0xffff0000, mBitmap.getPixel(10, 10));
    338         assertEquals(0xffff0000, mBitmap.getPixel(50, 50));
    339     }
    340 
    341     public void testExtractAlpha1(){
    342         mBitmap.recycle();
    343 
    344         //abnormal case: the bitmap has been recycled
    345         try{
    346             mBitmap.extractAlpha();
    347             fail("shouldn't come to here");
    348         }catch(IllegalStateException e){
    349         }
    350 
    351         // normal case
    352         mBitmap = BitmapFactory.decodeResource(mRes, R.drawable.start, mOptions);
    353         Bitmap ret = mBitmap.extractAlpha();
    354         assertNotNull(ret);
    355         int color = ret.getPixel(10, 20);
    356         assertEquals(0x00, Color.alpha(color));
    357     }
    358 
    359     public void testExtractAlpha2(){
    360         mBitmap.recycle();
    361 
    362         //abnormal case: the bitmap has been recycled
    363         try{
    364             mBitmap.extractAlpha(new Paint(), new int[]{0, 1});
    365             fail("shouldn't come to here");
    366         }catch(IllegalStateException e){
    367         }
    368 
    369         // normal case
    370         mBitmap = BitmapFactory.decodeResource(mRes, R.drawable.start, mOptions);
    371         Bitmap ret = mBitmap.extractAlpha(new Paint(), new int[]{0, 1});
    372         assertNotNull(ret);
    373         int color = ret.getPixel(10, 20);
    374         assertEquals(0x00, Color.alpha(color));
    375     }
    376 
    377     public void testGetConfig(){
    378         Bitmap bm0 = Bitmap.createBitmap(100, 200, Bitmap.Config.ALPHA_8);
    379         Bitmap bm1 = Bitmap.createBitmap(100, 200, Bitmap.Config.ARGB_8888);
    380         Bitmap bm2 = Bitmap.createBitmap(100, 200, Bitmap.Config.RGB_565);
    381         Bitmap bm3 = Bitmap.createBitmap(100, 200, Bitmap.Config.ARGB_4444);
    382 
    383         assertEquals(Bitmap.Config.ALPHA_8, bm0.getConfig());
    384         assertEquals(Bitmap.Config.ARGB_8888, bm1.getConfig());
    385         assertEquals(Bitmap.Config.RGB_565, bm2.getConfig());
    386         assertEquals(Bitmap.Config.ARGB_4444, bm3.getConfig());
    387     }
    388 
    389     public void testGetHeight(){
    390         assertEquals(31, mBitmap.getHeight());
    391         mBitmap = Bitmap.createBitmap(100, 200, Bitmap.Config.ARGB_8888);
    392         assertEquals(200, mBitmap.getHeight());
    393     }
    394 
    395     public void testGetNinePatchChunk(){
    396         assertNull(mBitmap.getNinePatchChunk());
    397     }
    398 
    399     public void testGetPixel(){
    400         mBitmap.recycle();
    401 
    402         //abnormal case: the bitmap has been recycled
    403         try{
    404             mBitmap.getPixel(10, 16);
    405             fail("shouldn't come to here");
    406         }catch(IllegalStateException e){
    407         }
    408 
    409         mBitmap = Bitmap.createBitmap(100, 200, Bitmap.Config.RGB_565);
    410 
    411         //abnormal case: x bigger than the source bitmap's width
    412         try{
    413             mBitmap.getPixel(200, 16);
    414             fail("shouldn't come to here");
    415         }catch(IllegalArgumentException e){
    416         }
    417 
    418         //abnormal case: y bigger than the source bitmap's height
    419         try{
    420             mBitmap.getPixel(10, 300);
    421             fail("shouldn't come to here");
    422         }catch(IllegalArgumentException e){
    423         }
    424 
    425         // normal case
    426         mBitmap.setPixel(10, 16, 0xFF << 24);
    427         assertEquals(0xFF << 24, mBitmap.getPixel(10, 16));
    428     }
    429 
    430     public void testGetRowBytes(){
    431         Bitmap bm0 = Bitmap.createBitmap(100, 200, Bitmap.Config.ALPHA_8);
    432         Bitmap bm1 = Bitmap.createBitmap(100, 200, Bitmap.Config.ARGB_8888);
    433         Bitmap bm2 = Bitmap.createBitmap(100, 200, Bitmap.Config.RGB_565);
    434         Bitmap bm3 = Bitmap.createBitmap(100, 200, Bitmap.Config.ARGB_4444);
    435 
    436         assertEquals(100, bm0.getRowBytes());
    437         assertEquals(400, bm1.getRowBytes());
    438         assertEquals(200, bm2.getRowBytes());
    439         assertEquals(200, bm3.getRowBytes());
    440     }
    441 
    442     public void testGetWidth(){
    443         assertEquals(31, mBitmap.getWidth());
    444         mBitmap = Bitmap.createBitmap(100, 200, Bitmap.Config.ARGB_8888);
    445         assertEquals(100, mBitmap.getWidth());
    446     }
    447 
    448     public void testHasAlpha(){
    449         assertFalse(mBitmap.hasAlpha());
    450         mBitmap = Bitmap.createBitmap(100, 200, Bitmap.Config.ARGB_8888);
    451         assertTrue(mBitmap.hasAlpha());
    452     }
    453 
    454     public void testIsMutable(){
    455         assertFalse(mBitmap.isMutable());
    456         mBitmap = Bitmap.createBitmap(100, 100, Config.ARGB_8888);
    457         assertTrue(mBitmap.isMutable());
    458     }
    459 
    460     public void testIsRecycled(){
    461         assertFalse(mBitmap.isRecycled());
    462         mBitmap.recycle();
    463         assertTrue(mBitmap.isRecycled());
    464     }
    465 
    466     public void testSetPixel(){
    467         int color = 0xff << 24;
    468 
    469         mBitmap.recycle();
    470 
    471         //abnormal case: the bitmap has been recycled
    472         try{
    473             mBitmap.setPixel(10, 16, color);
    474             fail("shouldn't come to here");
    475         }catch(IllegalStateException e){
    476         }
    477 
    478         mBitmap = BitmapFactory.decodeResource(mRes, R.drawable.start, mOptions);
    479 
    480         //abnormal case: the bitmap is immutable
    481         try{
    482             mBitmap.setPixel(10, 16, color);
    483             fail("shouldn't come to here");
    484         }catch(IllegalStateException e){
    485         }
    486 
    487         mBitmap = Bitmap.createBitmap(100, 200, Bitmap.Config.RGB_565);
    488 
    489         //abnormal case: x bigger than the source bitmap's width
    490         try{
    491             mBitmap.setPixel(200, 16, color);
    492             fail("shouldn't come to here");
    493         }catch(IllegalArgumentException e){
    494         }
    495 
    496         //abnormal case: y bigger than the source bitmap's height
    497         try{
    498             mBitmap.setPixel(10, 300, color);
    499             fail("shouldn't come to here");
    500         }catch(IllegalArgumentException e){
    501         }
    502 
    503         // normal case
    504         mBitmap.setPixel(10, 16, 0xFF << 24);
    505         assertEquals(0xFF << 24, mBitmap.getPixel(10, 16));
    506     }
    507 
    508     public void testSetPixels(){
    509         int[] colors = createColors(100);
    510 
    511         //abnormal case: the bitmap has been recycled
    512         mBitmap.recycle();
    513 
    514         try{
    515             mBitmap.setPixels(colors, 0, 0, 0, 0, 0, 0);
    516             fail("shouldn't come to here");
    517         }catch(IllegalStateException e){
    518         }
    519 
    520         mBitmap = BitmapFactory.decodeResource(mRes, R.drawable.start, mOptions);
    521 
    522         // abnormal case: the bitmap is immutable
    523         try{
    524             mBitmap.setPixels(colors, 0, 0, 0, 0, 0, 0);
    525             fail("shouldn't come to here");
    526         }catch(IllegalStateException e){
    527         }
    528 
    529         mBitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
    530 
    531         // abnormal case: x and/or y less than 0
    532         try{
    533             mBitmap.setPixels(colors, 0, 0, -1, -1, 200, 16);
    534             fail("shouldn't come to here");
    535         }catch(IllegalArgumentException e){
    536         }
    537 
    538         // abnormal case: width and/or height less than 0
    539         try{
    540             mBitmap.setPixels(colors, 0, 0, 0, 0, -1, -1);
    541             fail("shouldn't come to here");
    542         }catch(IllegalArgumentException e){
    543         }
    544 
    545         // abnormal case: (x + width) bigger than the source bitmap's width
    546         try{
    547             mBitmap.setPixels(colors, 0, 0, 10, 10, 95, 50);
    548             fail("shouldn't come to here");
    549         }catch(IllegalArgumentException e){
    550         }
    551 
    552         // abnormal case: (y + height) bigger than the source bitmap's height
    553         try{
    554             mBitmap.setPixels(colors, 0, 0, 10, 10, 50, 95);
    555             fail("shouldn't come to here");
    556         }catch(IllegalArgumentException e){
    557         }
    558 
    559         // abnormal case: stride less than width and bigger than -width
    560         try{
    561             mBitmap.setPixels(colors, 0, 10, 10, 10, 50, 50);
    562             fail("shouldn't come to here");
    563         }catch(IllegalArgumentException e){
    564         }
    565 
    566         // abnormal case: offset less than 0
    567         try{
    568             mBitmap.setPixels(colors, -1, 50, 10, 10, 50, 50);
    569             fail("shouldn't come to here");
    570         }catch(ArrayIndexOutOfBoundsException e){
    571         }
    572 
    573         // abnormal case: (offset + width) bigger than the length of colors
    574         try{
    575             mBitmap.setPixels(colors, 60, 50, 10, 10, 50, 50);
    576             fail("shouldn't come to here");
    577         }catch(ArrayIndexOutOfBoundsException e){
    578         }
    579 
    580         // abnormal case: lastScanline less than 0
    581         try{
    582             mBitmap.setPixels(colors, 10, -50, 10, 10, 50, 50);
    583             fail("shouldn't come to here");
    584         }catch(ArrayIndexOutOfBoundsException e){
    585         }
    586 
    587         // abnormal case: (lastScanline + width) bigger than the length of colors
    588         try{
    589             mBitmap.setPixels(colors, 10, 50, 10, 10, 50, 50);
    590             fail("shouldn't come to here");
    591         }catch(ArrayIndexOutOfBoundsException e){
    592         }
    593 
    594         // normal case
    595         colors = createColors(100 * 100);
    596         mBitmap.setPixels(colors, 0, 100, 0, 0, 100, 100);
    597         int[] ret = new int[100 * 100];
    598         mBitmap.getPixels(ret, 0, 100, 0, 0, 100, 100);
    599 
    600         for(int i = 0; i < 10000; i++){
    601             assertEquals(ret[i], colors[i]);
    602         }
    603     }
    604 
    605     public void testWriteToParcel(){
    606         mBitmap.recycle();
    607 
    608         // abnormal case: the bitmap to be written has been recycled
    609         try{
    610             mBitmap.writeToParcel(null, 0);
    611             fail("shouldn't come to here");
    612         }catch(IllegalStateException e){
    613         }
    614 
    615         // abnormal case: failed to unparcel Bitmap
    616         mBitmap = BitmapFactory.decodeResource(mRes, R.drawable.start, mOptions);
    617         Parcel p = Parcel.obtain();
    618         mBitmap.writeToParcel(p, 0);
    619 
    620         try{
    621             Bitmap.CREATOR.createFromParcel(p);
    622             fail("shouldn't come to here");
    623         }catch(RuntimeException e){
    624         }
    625 
    626         // normal case
    627         mBitmap = Bitmap.createBitmap(100, 100, Config.ARGB_8888);
    628         mBitmap.writeToParcel(p, 0);
    629         p.setDataPosition(0);
    630         mBitmap.equals(Bitmap.CREATOR.createFromParcel(p));
    631     }
    632 
    633     public void testGetScaledHeight1() {
    634         int dummyDensity = 5;
    635         Bitmap ret = Bitmap.createBitmap(100, 200, Config.RGB_565);
    636         int scaledHeight = scaleFromDensity(ret.getHeight(), ret.getDensity(), dummyDensity);
    637         assertNotNull(ret);
    638         assertEquals(scaledHeight, ret.getScaledHeight(dummyDensity));
    639     }
    640 
    641     public void testGetScaledHeight2() {
    642         Bitmap ret = Bitmap.createBitmap(100, 200, Config.RGB_565);
    643         DisplayMetrics metrics = new DisplayMetrics();
    644         metrics = getContext().getResources().getDisplayMetrics();
    645         int scaledHeight = scaleFromDensity(ret.getHeight(), ret.getDensity(), metrics.densityDpi);
    646         assertEquals(scaledHeight, ret.getScaledHeight(metrics));
    647     }
    648 
    649     public void testGetScaledHeight3() {
    650         Bitmap ret = Bitmap.createBitmap(100, 200, Config.RGB_565);
    651         Bitmap mMutableBitmap = Bitmap.createBitmap(100, 200, Config.ARGB_8888);
    652         Canvas mCanvas = new Canvas(mMutableBitmap);
    653         // set Density
    654         mCanvas.setDensity(DisplayMetrics.DENSITY_HIGH);
    655         int scaledHeight = scaleFromDensity(
    656                 ret.getHeight(), ret.getDensity(), mCanvas.getDensity());
    657         assertEquals(scaledHeight, ret.getScaledHeight(mCanvas));
    658     }
    659 
    660     public void testGetScaledWidth1() {
    661         int dummyDensity = 5;
    662         Bitmap ret = Bitmap.createBitmap(100, 200, Config.RGB_565);
    663         int scaledWidth = scaleFromDensity(ret.getWidth(), ret.getDensity(), dummyDensity);
    664         assertNotNull(ret);
    665         assertEquals(scaledWidth, ret.getScaledWidth(dummyDensity));
    666     }
    667 
    668     public void testGetScaledWidth2() {
    669         Bitmap ret = Bitmap.createBitmap(100, 200, Config.RGB_565);
    670         DisplayMetrics metrics = new DisplayMetrics();
    671         metrics = getContext().getResources().getDisplayMetrics();
    672         int scaledWidth = scaleFromDensity(ret.getWidth(), ret.getDensity(), metrics.densityDpi);
    673         assertEquals(scaledWidth, ret.getScaledWidth(metrics));
    674     }
    675 
    676     public void testGetScaledWidth3() {
    677         Bitmap ret = Bitmap.createBitmap(100, 200, Config.RGB_565);
    678         Bitmap mMutableBitmap = Bitmap.createBitmap(100, 200, Config.ARGB_8888);
    679         Canvas mCanvas = new Canvas(mMutableBitmap);
    680         // set Density
    681         mCanvas.setDensity(DisplayMetrics.DENSITY_HIGH);
    682         int scaledWidth = scaleFromDensity(ret.getWidth(), ret.getDensity(),  mCanvas.getDensity());
    683         assertEquals(scaledWidth, ret.getScaledWidth(mCanvas));
    684     }
    685 
    686     private int scaleFromDensity(int size, int sdensity, int tdensity) {
    687         if (sdensity == Bitmap.DENSITY_NONE || sdensity == tdensity) {
    688             return size;
    689         }
    690 
    691         // Scale by tdensity / sdensity, rounding up.
    692         return ((size * tdensity) + (sdensity >> 1)) / sdensity;
    693     }
    694 
    695     private int[] createColors(int size){
    696         int[] colors = new int[size];
    697 
    698         for (int i = 0; i < size; i++) {
    699             colors[i] = (0xFF << 24) | (i << 16) | (i << 8) | i;
    700         }
    701 
    702         return colors;
    703     }
    704 }
    705