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