Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.graphics.cts;
     18 
     19 import com.android.cts.stub.R;
     20 
     21 import dalvik.annotation.TestLevel;
     22 import dalvik.annotation.TestTargetClass;
     23 import dalvik.annotation.TestTargetNew;
     24 import dalvik.annotation.TestTargets;
     25 
     26 import android.content.res.Resources;
     27 import android.graphics.Bitmap;
     28 import android.graphics.BitmapFactory;
     29 import android.graphics.Canvas;
     30 import android.graphics.Color;
     31 import android.graphics.NinePatch;
     32 import android.graphics.Paint;
     33 import android.graphics.Rect;
     34 import android.graphics.RectF;
     35 import android.graphics.Region;
     36 import android.test.AndroidTestCase;
     37 
     38 @TestTargetClass(NinePatch.class)
     39 public class NinePatchTest extends AndroidTestCase {
     40     private static int ALPHA_OPAQUE = 0xFF;
     41 
     42     private NinePatch mNinePatch;
     43     private Bitmap mBitmap;
     44     private BitmapFactory.Options mOptNoScale;
     45     private Resources mRes;
     46     private final String NAME = "TESTNAME";
     47     private final int WIDTH = 80;
     48     private final int HEIGTH = 120;
     49     private final int[] COLOR = new int[WIDTH * HEIGTH];
     50     private byte[] mChunk;
     51 
     52     @Override
     53     protected void setUp() throws Exception {
     54         super.setUp();
     55         mRes = getContext().getResources();
     56         mOptNoScale = new BitmapFactory.Options();
     57         mOptNoScale.inDensity = mOptNoScale.inTargetDensity = mRes.getDisplayMetrics().densityDpi;
     58         mBitmap = BitmapFactory.decodeResource(mRes, R.drawable.opaque, mOptNoScale);
     59         mChunk = mBitmap.getNinePatchChunk();
     60         assertNotNull(mChunk);
     61         mNinePatch = new NinePatch(mBitmap, mChunk, NAME);
     62     }
     63 
     64     @TestTargetNew(
     65         level = TestLevel.COMPLETE,
     66         method = "NinePatch",
     67         args = {android.graphics.Bitmap.class, byte[].class, java.lang.String.class}
     68     )
     69     public void testConstructor() {
     70         mNinePatch = null;
     71         try {
     72             mNinePatch = new NinePatch(mBitmap, new byte[2], NAME);
     73             fail("should throw exception");
     74         } catch (Exception e) {
     75         }
     76         mNinePatch = new NinePatch(mBitmap, mChunk, NAME);
     77     }
     78 
     79     @TestTargetNew(
     80         level = TestLevel.COMPLETE,
     81         method = "isNinePatchChunk",
     82         args = {byte[].class}
     83     )
     84     public void testIsNinePatchChunk() {
     85         assertTrue(NinePatch.isNinePatchChunk(mChunk));
     86         Bitmap bitmap = Bitmap.createBitmap(COLOR, 10, 10, Bitmap.Config.ARGB_4444);
     87         assertFalse(NinePatch.isNinePatchChunk(bitmap.getNinePatchChunk()));
     88         assertFalse(NinePatch.isNinePatchChunk(null));
     89 
     90     }
     91 
     92     @TestTargets(value = {
     93         @TestTargetNew(
     94             level = TestLevel.COMPLETE,
     95             method = "draw",
     96             args = {android.graphics.Canvas.class, android.graphics.RectF.class}
     97         ),
     98         @TestTargetNew(
     99             level = TestLevel.COMPLETE,
    100             method = "draw",
    101             args = {android.graphics.Canvas.class, android.graphics.Rect.class}
    102         ),
    103         @TestTargetNew(
    104             level = TestLevel.COMPLETE,
    105             method = "draw",
    106             args = {android.graphics.Canvas.class, android.graphics.Rect.class}
    107         ),
    108         @TestTargetNew(
    109             level = TestLevel.COMPLETE,
    110             method = "setPaint",
    111             args = {android.graphics.Paint.class}
    112         )
    113     })
    114     public void testDraw() {
    115         Bitmap expected = BitmapFactory.decodeResource(mRes, R.drawable.scaled1, mOptNoScale);
    116 
    117         Bitmap bitmap = Bitmap.createBitmap(expected.getWidth(), expected.getHeight(),
    118                 Bitmap.Config.ARGB_8888);
    119         Canvas c = new Canvas(bitmap);
    120         RectF rectf = new RectF(0, 0, c.getWidth(), c.getHeight());
    121         mNinePatch.draw(c, rectf);
    122         checkBitmapWithAlpha(expected, bitmap, ALPHA_OPAQUE);
    123 
    124         expected = BitmapFactory.decodeResource(mRes, R.drawable.scaled2, mOptNoScale);
    125         bitmap = Bitmap.createBitmap(expected.getWidth(), expected.getHeight(),
    126                 Bitmap.Config.ARGB_8888);
    127         c = new Canvas(bitmap);
    128         Rect rect = new Rect(0, 0, c.getWidth(), c.getHeight());
    129         mNinePatch.draw(c, rect);
    130         checkBitmapWithAlpha(expected, bitmap, ALPHA_OPAQUE);
    131 
    132         bitmap = Bitmap.createBitmap(expected.getWidth(), expected.getHeight(),
    133                 Bitmap.Config.ARGB_8888);
    134         c = new Canvas(bitmap);
    135         rect = new Rect(0, 0, c.getWidth(), c.getHeight());
    136         final int alpha = 128;
    137         Paint p = new Paint();
    138         p.setAlpha(alpha);
    139         mNinePatch.draw(c, rect, p);
    140         checkBitmapWithAlpha(expected, bitmap, alpha);
    141 
    142         bitmap = Bitmap.createBitmap(expected.getWidth(), expected.getHeight(),
    143                 Bitmap.Config.ARGB_8888);
    144         c = new Canvas(bitmap);
    145         rectf = new RectF(0, 0, c.getWidth(), c.getHeight());
    146         mNinePatch.setPaint(p);
    147         mNinePatch.draw(c, rectf);
    148         checkBitmapWithAlpha(expected, bitmap, alpha);
    149     }
    150 
    151     private void checkBitmapWithAlpha(Bitmap expected, Bitmap bitmap, int alpha) {
    152         assertEquals(expected.getWidth(), bitmap.getWidth());
    153         assertEquals(expected.getHeight(), bitmap.getHeight());
    154         int width = expected.getWidth();
    155         int height = expected.getHeight();
    156 
    157         for (int i = 0; i < width; i++) {
    158             for (int j = 0; j < height; j++) {
    159                 int expectedPixel = expected.getPixel(i, j);
    160                 int actualPixel = bitmap.getPixel(i, j);
    161                 int expectedAlpha = Color.alpha(expectedPixel);
    162                 int actualAlpha = Color.alpha(actualPixel);
    163                 expectedPixel &= 0xFFFFFF;
    164                 actualPixel &= 0xFFFFFF;
    165                 assertEquals(expectedPixel, actualPixel);
    166                 assertEquals(expectedAlpha * alpha / ALPHA_OPAQUE, actualAlpha, 1);
    167             }
    168         }
    169     }
    170 
    171     @TestTargetNew(
    172         level = TestLevel.COMPLETE,
    173         method = "hasAlpha",
    174         args = {}
    175     )
    176     public void testHasAlpha() {
    177         assertFalse(mNinePatch.hasAlpha());
    178         assertEquals(mNinePatch.hasAlpha(), mBitmap.hasAlpha());
    179 
    180         Bitmap bitmap =
    181             BitmapFactory.decodeResource(mRes, R.drawable.transparent_border, mOptNoScale);
    182         byte[] chunk = bitmap.getNinePatchChunk();
    183         NinePatch ninePatch = new NinePatch(bitmap, chunk, NAME);
    184         assertTrue(ninePatch.hasAlpha());
    185         assertEquals(ninePatch.hasAlpha(), bitmap.hasAlpha());
    186     }
    187 
    188     @TestTargetNew(
    189         level = TestLevel.COMPLETE,
    190         method = "getHeight",
    191         args = {}
    192     )
    193     public void testGetHeight() {
    194         assertEquals(5, mNinePatch.getHeight());
    195         assertEquals(mNinePatch.getHeight(), mBitmap.getHeight());
    196     }
    197 
    198     @TestTargetNew(
    199         level = TestLevel.COMPLETE,
    200         method = "getWidth",
    201         args = {}
    202     )
    203     public void testGetWidth() {
    204         assertEquals(5, mNinePatch.getHeight());
    205         assertEquals(mNinePatch.getWidth(), mBitmap.getWidth());
    206     }
    207 
    208     @TestTargetNew(
    209         level = TestLevel.COMPLETE,
    210         method = "getDensity",
    211         args = {}
    212     )
    213     public void testGetDensity() {
    214         mBitmap.setDensity(11);
    215         assertEquals(11, mNinePatch.getDensity());
    216         mNinePatch = new NinePatch(mBitmap, mChunk, NAME);
    217         assertEquals(mNinePatch.getDensity(), mBitmap.getDensity());
    218     }
    219 
    220     @TestTargetNew(
    221         level = TestLevel.COMPLETE,
    222         method = "getTransparentRegion",
    223         args = {android.graphics.Rect.class}
    224     )
    225     public void testGetTransparentRegion() {
    226         // no transparency in opaque bitmap
    227         Rect location = new Rect(0, 0, mBitmap.getWidth(), mBitmap.getHeight());
    228         Region region = mNinePatch.getTransparentRegion(location);
    229         assertNull(region);
    230 
    231         // transparent border of 1px
    232         mBitmap = BitmapFactory.decodeResource(mRes, R.drawable.transparent_border, mOptNoScale);
    233         mChunk = mBitmap.getNinePatchChunk();
    234         assertNotNull(mChunk);
    235         mNinePatch = new NinePatch(mBitmap, mChunk, NAME);
    236 
    237         location = new Rect(0, 0, mBitmap.getWidth(), mBitmap.getHeight());
    238         region = mNinePatch.getTransparentRegion(location);
    239         assertNotNull(region);
    240         Rect regionBounds = region.getBounds();
    241         assertBounds(regionBounds, 0, 0, 5, 5);
    242 
    243         location = new Rect(0, 0, mBitmap.getWidth() * 2, mBitmap.getHeight() * 2);
    244         region = mNinePatch.getTransparentRegion(location);
    245         assertNotNull(region);
    246         regionBounds = region.getBounds();
    247         assertBounds(regionBounds, 0, 0, 10, 10);
    248 
    249         // transparent padding of 1px on the right side
    250         mBitmap = BitmapFactory.decodeResource(mRes, R.drawable.transparent_right, mOptNoScale);
    251         mChunk = mBitmap.getNinePatchChunk();
    252         assertNotNull(mChunk);
    253         mNinePatch = new NinePatch(mBitmap, mChunk, NAME);
    254 
    255         location = new Rect(0, 0, mBitmap.getWidth(), mBitmap.getHeight());
    256         region = mNinePatch.getTransparentRegion(location);
    257         assertNotNull(region);
    258         regionBounds = region.getBounds();
    259         assertBounds(regionBounds, 4, 0, 5, 5);
    260 
    261         location = new Rect(0, 0, mBitmap.getWidth() * 2, mBitmap.getHeight() * 2);
    262         region = mNinePatch.getTransparentRegion(location);
    263         regionBounds = region.getBounds();
    264         assertBounds(regionBounds, 9, 0, 10, 10);
    265     }
    266 
    267     private void assertBounds(Rect regionBounds, int left, int top, int right, int bottom) {
    268         assertEquals(left, regionBounds.left);
    269         assertEquals(top, regionBounds.top);
    270         assertEquals(right, regionBounds.right);
    271         assertEquals(bottom, regionBounds.bottom);
    272     }
    273 }
    274