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 static org.junit.Assert.assertEquals;
     19 import static org.junit.Assert.assertFalse;
     20 import static org.junit.Assert.assertNotNull;
     21 import static org.junit.Assert.assertTrue;
     22 
     23 import android.content.res.Resources;
     24 import android.graphics.Bitmap;
     25 import android.graphics.Bitmap.Config;
     26 import android.graphics.BitmapFactory;
     27 import android.graphics.Canvas;
     28 import android.support.test.InstrumentationRegistry;
     29 import android.support.test.filters.SmallTest;
     30 import android.support.test.runner.AndroidJUnit4;
     31 
     32 import org.junit.Before;
     33 import org.junit.Test;
     34 import org.junit.runner.RunWith;
     35 
     36 @SmallTest
     37 @RunWith(AndroidJUnit4.class)
     38 public class BitmapRGBAF16Test {
     39     private Bitmap mOpaqueBitmap;
     40     private Bitmap mTransparentBitmap;
     41     private Resources mResources;
     42 
     43     @Before
     44     public void setup() {
     45         mResources = InstrumentationRegistry.getTargetContext().getResources();
     46 
     47         BitmapFactory.Options options = new BitmapFactory.Options();
     48         options.inMutable = true;
     49 
     50         // The bitmaps are in raw-nodpi/ to guarantee aapt and framework leave them untouched
     51         mOpaqueBitmap = BitmapFactory.decodeResource(mResources, R.raw.p3_opaque, options);
     52         mTransparentBitmap = BitmapFactory.decodeResource(mResources, R.raw.p3_transparent, options);
     53     }
     54 
     55     @Test
     56     public void testDecode() {
     57         assertNotNull(mOpaqueBitmap);
     58         assertEquals(Config.RGBA_F16, mOpaqueBitmap.getConfig());
     59         assertFalse(mOpaqueBitmap.hasAlpha());
     60 
     61         assertNotNull(mTransparentBitmap);
     62         assertEquals(Config.RGBA_F16, mTransparentBitmap.getConfig());
     63         assertTrue(mTransparentBitmap.hasAlpha());
     64     }
     65 
     66     @Test
     67     public void testScaling() {
     68         Bitmap scaled = Bitmap.createScaledBitmap(mOpaqueBitmap,
     69                 mOpaqueBitmap.getWidth() / 2, mOpaqueBitmap.getHeight() / 2, true);
     70         assertNotNull(scaled);
     71         assertEquals(Config.RGBA_F16, scaled.getConfig());
     72         assertFalse(scaled.hasAlpha());
     73 
     74         scaled = Bitmap.createScaledBitmap(mTransparentBitmap,
     75                 mTransparentBitmap.getWidth() / 2, mTransparentBitmap.getHeight() / 2, true);
     76         assertNotNull(scaled);
     77         assertEquals(Config.RGBA_F16, scaled.getConfig());
     78         assertTrue(scaled.hasAlpha());
     79     }
     80 
     81     @Test
     82     public void testCopy() {
     83         Bitmap copy = Bitmap.createBitmap(mOpaqueBitmap);
     84         assertNotNull(copy);
     85         assertEquals(Config.RGBA_F16, copy.getConfig());
     86         assertFalse(copy.hasAlpha());
     87 
     88         copy = Bitmap.createBitmap(mTransparentBitmap);
     89         assertNotNull(copy);
     90         assertEquals(Config.RGBA_F16, copy.getConfig());
     91         assertTrue(copy.hasAlpha());
     92     }
     93 
     94     @Test
     95     public void testCreate() {
     96         Bitmap b = Bitmap.createBitmap(64, 64, Config.RGBA_F16, false);
     97         assertNotNull(b);
     98         assertEquals(Config.RGBA_F16, b.getConfig());
     99         assertFalse(b.hasAlpha());
    100 
    101         b = Bitmap.createBitmap(64, 64, Config.RGBA_F16);
    102         assertNotNull(b);
    103         assertEquals(Config.RGBA_F16, b.getConfig());
    104         assertTrue(b.hasAlpha());
    105     }
    106 
    107     @Test
    108     public void testGetPixel() {
    109         // Opaque pixels from opaque bitmap
    110         assertEquals(0xff0f131f, mOpaqueBitmap.getPixel(0, 0));
    111         assertEquals(0xff0f1421, mOpaqueBitmap.getPixel(1, 0));
    112         assertEquals(0xff101523, mOpaqueBitmap.getPixel(2, 0));
    113 
    114         // Opaque pixels from transparent bitmap
    115         assertEquals(0xffff0000, mTransparentBitmap.getPixel(0, 0));
    116         assertEquals(0xff00ff00, mTransparentBitmap.getPixel(1, 0));
    117         assertEquals(0xff0000ff, mTransparentBitmap.getPixel(2, 0));
    118 
    119         // Transparent pixels from transparent bitmap
    120         assertEquals(0x7fff0000, mTransparentBitmap.getPixel(61, 63));
    121         assertEquals(0x7f00ff00, mTransparentBitmap.getPixel(62, 63));
    122         assertEquals(0x7f0000ff, mTransparentBitmap.getPixel(63, 63));
    123     }
    124 
    125     @Test
    126     public void testSetPixel() {
    127         int before = mOpaqueBitmap.getPixel(5, 5);
    128         mOpaqueBitmap.setPixel(5, 5, 0x7f102030);
    129         int after = mOpaqueBitmap.getPixel(5, 5);
    130         assertTrue(before != after);
    131         assertEquals(0x7f102030, after);
    132 
    133         before = mTransparentBitmap.getPixel(5, 5);
    134         mTransparentBitmap.setPixel(5, 5, 0x7f102030);
    135         after = mTransparentBitmap.getPixel(5, 5);
    136         assertTrue(before != after);
    137         assertEquals(0x7f102030, after);
    138     }
    139 
    140     @Test
    141     public void testCopyFromA8() {
    142         Bitmap res = BitmapFactory.decodeResource(mResources, R.drawable.alpha_mask);
    143         Bitmap mask = Bitmap.createBitmap(res.getWidth(), res.getHeight(),
    144                 Bitmap.Config.ALPHA_8);
    145         Canvas c = new Canvas(mask);
    146         c.drawBitmap(res, 0, 0, null);
    147 
    148         Bitmap b = mask.copy(Config.RGBA_F16, false);
    149         assertNotNull(b);
    150     }
    151 }
    152