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         intBuf1.position(0); // copyPixelsToBuffer adjusted the position, so rewind to start
    162         bitmap.copyPixelsFromBuffer(intBuf1);
    163         IntBuffer intBuf2 = IntBuffer.allocate(pixSize);
    164         bitmap.copyPixelsToBuffer(intBuf2);
    165 
    166         assertEquals(pixSize >> 2, intBuf2.position());
    167         assertEquals(intBuf1.position(), intBuf2.position());
    168         int size = intBuf1.position();
    169         intBuf1.position(0);
    170         intBuf2.position(0);
    171         for (int i = 0; i < size; i++) {
    172             assertEquals("mismatching pixels at position " + i, intBuf1.get(), intBuf2.get());
    173         }
    174     }
    175 
    176     public void testCreateBitmap1(){
    177         int[] colors = createColors(100);
    178         Bitmap bitmap = Bitmap.createBitmap(colors, 10, 10, Config.RGB_565);
    179         Bitmap ret = Bitmap.createBitmap(bitmap);
    180         assertNotNull(ret);
    181         assertEquals(10, ret.getWidth());
    182         assertEquals(10, ret.getHeight());
    183         assertEquals(Config.RGB_565, ret.getConfig());
    184     }
    185 
    186     public void testCreateBitmap2(){
    187         //abnormal case: Illegal Argument
    188         try{
    189             Bitmap.createBitmap(mBitmap, -100, 50, 50, 200);
    190             fail("shouldn't come to here");
    191         }catch(IllegalArgumentException e){
    192         }
    193 
    194         // special case: output bitmap is equal to the input bitmap
    195         mBitmap = Bitmap.createBitmap(new int[100 * 100], 100, 100, Config.ARGB_8888);
    196         Bitmap ret = Bitmap.createBitmap(mBitmap, 0, 0, 100, 100);
    197         assertNotNull(ret);
    198         assertTrue(mBitmap.equals(ret));
    199 
    200         //normal case
    201         mBitmap = Bitmap.createBitmap(100, 100, Config.ARGB_8888);
    202         ret = Bitmap.createBitmap(mBitmap, 10, 10, 50, 50);
    203         assertNotNull(ret);
    204         assertFalse(mBitmap.equals(ret));
    205     }
    206 
    207     public void testCreateBitmap3(){
    208         mBitmap = Bitmap.createBitmap(100, 100, Config.ARGB_8888);
    209 
    210         //abnormal case: x and/or y less than 0
    211         try{
    212             Bitmap.createBitmap(mBitmap, -1, -1, 10, 10, null, false);
    213             fail("shouldn't come to here");
    214         }catch(IllegalArgumentException e){
    215         }
    216 
    217         //abnormal case: width and/or height less than 0
    218         try{
    219             Bitmap.createBitmap(mBitmap, 1, 1, -10, -10, null, false);
    220             fail("shouldn't come to here");
    221         }catch(IllegalArgumentException e){
    222         }
    223 
    224         //abnormal case: (x + width) bigger than source bitmap's width
    225         try{
    226             Bitmap.createBitmap(mBitmap, 10, 10, 95, 50, null, false);
    227             fail("shouldn't come to here");
    228         }catch(IllegalArgumentException e){
    229         }
    230 
    231         //abnormal case: (y + height) bigger than source bitmap's height
    232         try{
    233             Bitmap.createBitmap(mBitmap, 10, 10, 50, 95, null, false);
    234             fail("shouldn't come to here");
    235         }catch(IllegalArgumentException e){
    236         }
    237 
    238         // special case: output bitmap is equal to the input bitmap
    239         mBitmap = Bitmap.createBitmap(new int[100 * 100], 100, 100, Config.ARGB_8888);
    240         Bitmap ret = Bitmap.createBitmap(mBitmap, 0, 0, 100, 100, null, false);
    241         assertNotNull(ret);
    242         assertTrue(mBitmap.equals(ret));
    243 
    244         // normal case
    245         mBitmap = Bitmap.createBitmap(100, 100, Config.ARGB_8888);
    246         ret = Bitmap.createBitmap(mBitmap, 10, 10, 50, 50, new Matrix(), true);
    247         assertNotNull(ret);
    248         assertFalse(mBitmap.equals(ret));
    249     }
    250 
    251     public void testCreateBitmap4(){
    252         Bitmap ret = Bitmap.createBitmap(100, 200, Config.RGB_565);
    253         assertNotNull(ret);
    254         assertEquals(100, ret.getWidth());
    255         assertEquals(200, ret.getHeight());
    256         assertEquals(Config.RGB_565, ret.getConfig());
    257     }
    258 
    259     public void testCreateBitmap6(){
    260         int[] colors = createColors(100);
    261 
    262         //abnormal case: width and/or height less than 0
    263         try{
    264             Bitmap.createBitmap(colors, 0, 100, -1, 100, Config.RGB_565);
    265             fail("shouldn't come to here");
    266         }catch(IllegalArgumentException e){
    267         }
    268 
    269         //abnormal case: stride less than width and bigger than -width
    270         try{
    271             Bitmap.createBitmap(colors, 10, 10, 100, 100, Config.RGB_565);
    272             fail("shouldn't come to here");
    273         }catch(IllegalArgumentException e){
    274         }
    275 
    276         //abnormal case: offset less than 0
    277         try{
    278             Bitmap.createBitmap(colors, -10, 100, 100, 100, Config.RGB_565);
    279             fail("shouldn't come to here");
    280         }catch(ArrayIndexOutOfBoundsException e){
    281         }
    282 
    283         //abnormal case: (offset + width) bigger than colors' length
    284         try{
    285             Bitmap.createBitmap(colors, 10, 100, 100, 100, Config.RGB_565);
    286             fail("shouldn't come to here");
    287         }catch(ArrayIndexOutOfBoundsException e){
    288         }
    289 
    290         //abnormal case: (lastScanline + width) bigger than colors' length
    291         try{
    292             Bitmap.createBitmap(colors, 10, 100, 50, 100, Config.RGB_565);
    293             fail("shouldn't come to here");
    294         }catch(ArrayIndexOutOfBoundsException e){
    295         }
    296 
    297         // normal case
    298         Bitmap ret = Bitmap.createBitmap(colors, 5, 10, 10, 5, Config.RGB_565);
    299         assertNotNull(ret);
    300         assertEquals(10, ret.getWidth());
    301         assertEquals(5, ret.getHeight());
    302         assertEquals(Config.RGB_565, ret.getConfig());
    303     }
    304 
    305     public void testCreateScaledBitmap(){
    306         mBitmap = Bitmap.createBitmap(100, 200, Config.RGB_565);
    307         Bitmap ret = Bitmap.createScaledBitmap(mBitmap, 50, 100, false);
    308         assertNotNull(ret);
    309         assertEquals(50, ret.getWidth());
    310         assertEquals(100, ret.getHeight());
    311     }
    312 
    313     public void testDescribeContents(){
    314         assertEquals(0, mBitmap.describeContents());
    315     }
    316 
    317     public void testEraseColor(){
    318         mBitmap.recycle();
    319 
    320         //abnormal case: the bitmap has been recycled
    321         try{
    322             mBitmap.eraseColor(0);
    323             fail("shouldn't come to here");
    324         }catch(IllegalStateException e){
    325         }
    326 
    327         mBitmap = BitmapFactory.decodeResource(mRes, R.drawable.start, mOptions);
    328 
    329         //abnormal case: bitmap is immutable
    330         try{
    331             mBitmap.eraseColor(0);
    332             fail("shouldn't come to here");
    333         }catch(IllegalStateException e){
    334         }
    335 
    336         // normal case
    337         mBitmap = Bitmap.createBitmap(100, 100, Config.ARGB_8888);
    338         mBitmap.eraseColor(0xffff0000);
    339         assertEquals(0xffff0000, mBitmap.getPixel(10, 10));
    340         assertEquals(0xffff0000, mBitmap.getPixel(50, 50));
    341     }
    342 
    343     public void testExtractAlpha1(){
    344         mBitmap.recycle();
    345 
    346         //abnormal case: the bitmap has been recycled
    347         try{
    348             mBitmap.extractAlpha();
    349             fail("shouldn't come to here");
    350         }catch(IllegalStateException e){
    351         }
    352 
    353         // normal case
    354         mBitmap = BitmapFactory.decodeResource(mRes, R.drawable.start, mOptions);
    355         Bitmap ret = mBitmap.extractAlpha();
    356         assertNotNull(ret);
    357         int color = ret.getPixel(10, 20);
    358         assertEquals(0x00, Color.alpha(color));
    359     }
    360 
    361     public void testExtractAlpha2(){
    362         mBitmap.recycle();
    363 
    364         //abnormal case: the bitmap has been recycled
    365         try{
    366             mBitmap.extractAlpha(new Paint(), new int[]{0, 1});
    367             fail("shouldn't come to here");
    368         }catch(IllegalStateException e){
    369         }
    370 
    371         // normal case
    372         mBitmap = BitmapFactory.decodeResource(mRes, R.drawable.start, mOptions);
    373         Bitmap ret = mBitmap.extractAlpha(new Paint(), new int[]{0, 1});
    374         assertNotNull(ret);
    375         int color = ret.getPixel(10, 20);
    376         assertEquals(0x00, Color.alpha(color));
    377     }
    378 
    379     public void testGetConfig(){
    380         Bitmap bm0 = Bitmap.createBitmap(100, 200, Bitmap.Config.ALPHA_8);
    381         Bitmap bm1 = Bitmap.createBitmap(100, 200, Bitmap.Config.ARGB_8888);
    382         Bitmap bm2 = Bitmap.createBitmap(100, 200, Bitmap.Config.RGB_565);
    383         Bitmap bm3 = Bitmap.createBitmap(100, 200, Bitmap.Config.ARGB_4444);
    384 
    385         assertEquals(Bitmap.Config.ALPHA_8, bm0.getConfig());
    386         assertEquals(Bitmap.Config.ARGB_8888, bm1.getConfig());
    387         assertEquals(Bitmap.Config.RGB_565, bm2.getConfig());
    388         assertEquals(Bitmap.Config.ARGB_4444, bm3.getConfig());
    389     }
    390 
    391     public void testGetHeight(){
    392         assertEquals(31, mBitmap.getHeight());
    393         mBitmap = Bitmap.createBitmap(100, 200, Bitmap.Config.ARGB_8888);
    394         assertEquals(200, mBitmap.getHeight());
    395     }
    396 
    397     public void testGetNinePatchChunk(){
    398         assertNull(mBitmap.getNinePatchChunk());
    399     }
    400 
    401     public void testGetPixel(){
    402         mBitmap.recycle();
    403 
    404         //abnormal case: the bitmap has been recycled
    405         try{
    406             mBitmap.getPixel(10, 16);
    407             fail("shouldn't come to here");
    408         }catch(IllegalStateException e){
    409         }
    410 
    411         mBitmap = Bitmap.createBitmap(100, 200, Bitmap.Config.RGB_565);
    412 
    413         //abnormal case: x bigger than the source bitmap's width
    414         try{
    415             mBitmap.getPixel(200, 16);
    416             fail("shouldn't come to here");
    417         }catch(IllegalArgumentException e){
    418         }
    419 
    420         //abnormal case: y bigger than the source bitmap's height
    421         try{
    422             mBitmap.getPixel(10, 300);
    423             fail("shouldn't come to here");
    424         }catch(IllegalArgumentException e){
    425         }
    426 
    427         // normal case
    428         mBitmap.setPixel(10, 16, 0xFF << 24);
    429         assertEquals(0xFF << 24, mBitmap.getPixel(10, 16));
    430     }
    431 
    432     public void testGetRowBytes(){
    433         Bitmap bm0 = Bitmap.createBitmap(100, 200, Bitmap.Config.ALPHA_8);
    434         Bitmap bm1 = Bitmap.createBitmap(100, 200, Bitmap.Config.ARGB_8888);
    435         Bitmap bm2 = Bitmap.createBitmap(100, 200, Bitmap.Config.RGB_565);
    436         Bitmap bm3 = Bitmap.createBitmap(100, 200, Bitmap.Config.ARGB_4444);
    437 
    438         assertEquals(100, bm0.getRowBytes());
    439         assertEquals(400, bm1.getRowBytes());
    440         assertEquals(200, bm2.getRowBytes());
    441         assertEquals(200, bm3.getRowBytes());
    442     }
    443 
    444     public void testGetWidth(){
    445         assertEquals(31, mBitmap.getWidth());
    446         mBitmap = Bitmap.createBitmap(100, 200, Bitmap.Config.ARGB_8888);
    447         assertEquals(100, mBitmap.getWidth());
    448     }
    449 
    450     public void testHasAlpha(){
    451         assertFalse(mBitmap.hasAlpha());
    452         mBitmap = Bitmap.createBitmap(100, 200, Bitmap.Config.ARGB_8888);
    453         assertTrue(mBitmap.hasAlpha());
    454     }
    455 
    456     public void testIsMutable(){
    457         assertFalse(mBitmap.isMutable());
    458         mBitmap = Bitmap.createBitmap(100, 100, Config.ARGB_8888);
    459         assertTrue(mBitmap.isMutable());
    460     }
    461 
    462     public void testIsRecycled(){
    463         assertFalse(mBitmap.isRecycled());
    464         mBitmap.recycle();
    465         assertTrue(mBitmap.isRecycled());
    466     }
    467 
    468     public void testSetPixel(){
    469         int color = 0xff << 24;
    470 
    471         mBitmap.recycle();
    472 
    473         //abnormal case: the bitmap has been recycled
    474         try{
    475             mBitmap.setPixel(10, 16, color);
    476             fail("shouldn't come to here");
    477         }catch(IllegalStateException e){
    478         }
    479 
    480         mBitmap = BitmapFactory.decodeResource(mRes, R.drawable.start, mOptions);
    481 
    482         //abnormal case: the bitmap is immutable
    483         try{
    484             mBitmap.setPixel(10, 16, color);
    485             fail("shouldn't come to here");
    486         }catch(IllegalStateException e){
    487         }
    488 
    489         mBitmap = Bitmap.createBitmap(100, 200, Bitmap.Config.RGB_565);
    490 
    491         //abnormal case: x bigger than the source bitmap's width
    492         try{
    493             mBitmap.setPixel(200, 16, color);
    494             fail("shouldn't come to here");
    495         }catch(IllegalArgumentException e){
    496         }
    497 
    498         //abnormal case: y bigger than the source bitmap's height
    499         try{
    500             mBitmap.setPixel(10, 300, color);
    501             fail("shouldn't come to here");
    502         }catch(IllegalArgumentException e){
    503         }
    504 
    505         // normal case
    506         mBitmap.setPixel(10, 16, 0xFF << 24);
    507         assertEquals(0xFF << 24, mBitmap.getPixel(10, 16));
    508     }
    509 
    510     public void testSetPixels(){
    511         int[] colors = createColors(100);
    512 
    513         //abnormal case: the bitmap has been recycled
    514         mBitmap.recycle();
    515 
    516         try{
    517             mBitmap.setPixels(colors, 0, 0, 0, 0, 0, 0);
    518             fail("shouldn't come to here");
    519         }catch(IllegalStateException e){
    520         }
    521 
    522         mBitmap = BitmapFactory.decodeResource(mRes, R.drawable.start, mOptions);
    523 
    524         // abnormal case: the bitmap is immutable
    525         try{
    526             mBitmap.setPixels(colors, 0, 0, 0, 0, 0, 0);
    527             fail("shouldn't come to here");
    528         }catch(IllegalStateException e){
    529         }
    530 
    531         mBitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
    532 
    533         // abnormal case: x and/or y less than 0
    534         try{
    535             mBitmap.setPixels(colors, 0, 0, -1, -1, 200, 16);
    536             fail("shouldn't come to here");
    537         }catch(IllegalArgumentException e){
    538         }
    539 
    540         // abnormal case: width and/or height less than 0
    541         try{
    542             mBitmap.setPixels(colors, 0, 0, 0, 0, -1, -1);
    543             fail("shouldn't come to here");
    544         }catch(IllegalArgumentException e){
    545         }
    546 
    547         // abnormal case: (x + width) bigger than the source bitmap's width
    548         try{
    549             mBitmap.setPixels(colors, 0, 0, 10, 10, 95, 50);
    550             fail("shouldn't come to here");
    551         }catch(IllegalArgumentException e){
    552         }
    553 
    554         // abnormal case: (y + height) bigger than the source bitmap's height
    555         try{
    556             mBitmap.setPixels(colors, 0, 0, 10, 10, 50, 95);
    557             fail("shouldn't come to here");
    558         }catch(IllegalArgumentException e){
    559         }
    560 
    561         // abnormal case: stride less than width and bigger than -width
    562         try{
    563             mBitmap.setPixels(colors, 0, 10, 10, 10, 50, 50);
    564             fail("shouldn't come to here");
    565         }catch(IllegalArgumentException e){
    566         }
    567 
    568         // abnormal case: offset less than 0
    569         try{
    570             mBitmap.setPixels(colors, -1, 50, 10, 10, 50, 50);
    571             fail("shouldn't come to here");
    572         }catch(ArrayIndexOutOfBoundsException e){
    573         }
    574 
    575         // abnormal case: (offset + width) bigger than the length of colors
    576         try{
    577             mBitmap.setPixels(colors, 60, 50, 10, 10, 50, 50);
    578             fail("shouldn't come to here");
    579         }catch(ArrayIndexOutOfBoundsException e){
    580         }
    581 
    582         // abnormal case: lastScanline less than 0
    583         try{
    584             mBitmap.setPixels(colors, 10, -50, 10, 10, 50, 50);
    585             fail("shouldn't come to here");
    586         }catch(ArrayIndexOutOfBoundsException e){
    587         }
    588 
    589         // abnormal case: (lastScanline + width) bigger than the length of colors
    590         try{
    591             mBitmap.setPixels(colors, 10, 50, 10, 10, 50, 50);
    592             fail("shouldn't come to here");
    593         }catch(ArrayIndexOutOfBoundsException e){
    594         }
    595 
    596         // normal case
    597         colors = createColors(100 * 100);
    598         mBitmap.setPixels(colors, 0, 100, 0, 0, 100, 100);
    599         int[] ret = new int[100 * 100];
    600         mBitmap.getPixels(ret, 0, 100, 0, 0, 100, 100);
    601 
    602         for(int i = 0; i < 10000; i++){
    603             assertEquals(ret[i], colors[i]);
    604         }
    605     }
    606 
    607     public void testWriteToParcel(){
    608         mBitmap.recycle();
    609 
    610         // abnormal case: the bitmap to be written has been recycled
    611         try{
    612             mBitmap.writeToParcel(null, 0);
    613             fail("shouldn't come to here");
    614         }catch(IllegalStateException e){
    615         }
    616 
    617         // abnormal case: failed to unparcel Bitmap
    618         mBitmap = BitmapFactory.decodeResource(mRes, R.drawable.start, mOptions);
    619         Parcel p = Parcel.obtain();
    620         mBitmap.writeToParcel(p, 0);
    621 
    622         try{
    623             Bitmap.CREATOR.createFromParcel(p);
    624             fail("shouldn't come to here");
    625         }catch(RuntimeException e){
    626         }
    627 
    628         // normal case
    629         mBitmap = Bitmap.createBitmap(100, 100, Config.ARGB_8888);
    630         mBitmap.writeToParcel(p, 0);
    631         p.setDataPosition(0);
    632         mBitmap.equals(Bitmap.CREATOR.createFromParcel(p));
    633     }
    634 
    635     public void testGetScaledHeight1() {
    636         int dummyDensity = 5;
    637         Bitmap ret = Bitmap.createBitmap(100, 200, Config.RGB_565);
    638         int scaledHeight = scaleFromDensity(ret.getHeight(), ret.getDensity(), dummyDensity);
    639         assertNotNull(ret);
    640         assertEquals(scaledHeight, ret.getScaledHeight(dummyDensity));
    641     }
    642 
    643     public void testGetScaledHeight2() {
    644         Bitmap ret = Bitmap.createBitmap(100, 200, Config.RGB_565);
    645         DisplayMetrics metrics = new DisplayMetrics();
    646         metrics = getContext().getResources().getDisplayMetrics();
    647         int scaledHeight = scaleFromDensity(ret.getHeight(), ret.getDensity(), metrics.densityDpi);
    648         assertEquals(scaledHeight, ret.getScaledHeight(metrics));
    649     }
    650 
    651     public void testGetScaledHeight3() {
    652         Bitmap ret = Bitmap.createBitmap(100, 200, Config.RGB_565);
    653         Bitmap mMutableBitmap = Bitmap.createBitmap(100, 200, Config.ARGB_8888);
    654         Canvas mCanvas = new Canvas(mMutableBitmap);
    655         // set Density
    656         mCanvas.setDensity(DisplayMetrics.DENSITY_HIGH);
    657         int scaledHeight = scaleFromDensity(
    658                 ret.getHeight(), ret.getDensity(), mCanvas.getDensity());
    659         assertEquals(scaledHeight, ret.getScaledHeight(mCanvas));
    660     }
    661 
    662     public void testGetScaledWidth1() {
    663         int dummyDensity = 5;
    664         Bitmap ret = Bitmap.createBitmap(100, 200, Config.RGB_565);
    665         int scaledWidth = scaleFromDensity(ret.getWidth(), ret.getDensity(), dummyDensity);
    666         assertNotNull(ret);
    667         assertEquals(scaledWidth, ret.getScaledWidth(dummyDensity));
    668     }
    669 
    670     public void testGetScaledWidth2() {
    671         Bitmap ret = Bitmap.createBitmap(100, 200, Config.RGB_565);
    672         DisplayMetrics metrics = new DisplayMetrics();
    673         metrics = getContext().getResources().getDisplayMetrics();
    674         int scaledWidth = scaleFromDensity(ret.getWidth(), ret.getDensity(), metrics.densityDpi);
    675         assertEquals(scaledWidth, ret.getScaledWidth(metrics));
    676     }
    677 
    678     public void testGetScaledWidth3() {
    679         Bitmap ret = Bitmap.createBitmap(100, 200, Config.RGB_565);
    680         Bitmap mMutableBitmap = Bitmap.createBitmap(100, 200, Config.ARGB_8888);
    681         Canvas mCanvas = new Canvas(mMutableBitmap);
    682         // set Density
    683         mCanvas.setDensity(DisplayMetrics.DENSITY_HIGH);
    684         int scaledWidth = scaleFromDensity(ret.getWidth(), ret.getDensity(),  mCanvas.getDensity());
    685         assertEquals(scaledWidth, ret.getScaledWidth(mCanvas));
    686     }
    687 
    688     private int scaleFromDensity(int size, int sdensity, int tdensity) {
    689         if (sdensity == Bitmap.DENSITY_NONE || sdensity == tdensity) {
    690             return size;
    691         }
    692 
    693         // Scale by tdensity / sdensity, rounding up.
    694         return ((size * tdensity) + (sdensity >> 1)) / sdensity;
    695     }
    696 
    697     private int[] createColors(int size){
    698         int[] colors = new int[size];
    699 
    700         for (int i = 0; i < size; i++) {
    701             colors[i] = (0xFF << 24) | (i << 16) | (i << 8) | i;
    702         }
    703 
    704         return colors;
    705     }
    706 }
    707