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 22 import android.content.res.Resources; 23 import android.graphics.Bitmap; 24 import android.graphics.BitmapFactory; 25 import android.graphics.Canvas; 26 import android.graphics.Color; 27 import android.graphics.NinePatch; 28 import android.graphics.Paint; 29 import android.graphics.Rect; 30 import android.graphics.RectF; 31 import android.graphics.Region; 32 import android.test.AndroidTestCase; 33 34 public class NinePatchTest extends AndroidTestCase { 35 private static int ALPHA_OPAQUE = 0xFF; 36 37 private NinePatch mNinePatch; 38 private Bitmap mBitmap; 39 private BitmapFactory.Options mOptNoScale; 40 private Resources mRes; 41 private final String NAME = "TESTNAME"; 42 private final int WIDTH = 80; 43 private final int HEIGTH = 120; 44 private final int[] COLOR = new int[WIDTH * HEIGTH]; 45 private byte[] mChunk; 46 47 @Override 48 protected void setUp() throws Exception { 49 super.setUp(); 50 mRes = getContext().getResources(); 51 mOptNoScale = new BitmapFactory.Options(); 52 mOptNoScale.inDensity = mOptNoScale.inTargetDensity = mRes.getDisplayMetrics().densityDpi; 53 mBitmap = BitmapFactory.decodeResource(mRes, R.drawable.opaque, mOptNoScale); 54 mChunk = mBitmap.getNinePatchChunk(); 55 assertNotNull(mChunk); 56 mNinePatch = new NinePatch(mBitmap, mChunk, NAME); 57 } 58 59 public void testConstructor() { 60 mNinePatch = null; 61 try { 62 mNinePatch = new NinePatch(mBitmap, new byte[2], NAME); 63 fail("should throw exception"); 64 } catch (Exception e) { 65 } 66 mNinePatch = new NinePatch(mBitmap, mChunk, NAME); 67 } 68 69 public void testIsNinePatchChunk() { 70 assertTrue(NinePatch.isNinePatchChunk(mChunk)); 71 Bitmap bitmap = Bitmap.createBitmap(COLOR, 10, 10, Bitmap.Config.ARGB_4444); 72 assertFalse(NinePatch.isNinePatchChunk(bitmap.getNinePatchChunk())); 73 assertFalse(NinePatch.isNinePatchChunk(null)); 74 75 } 76 77 public void testDraw() { 78 Bitmap expected = BitmapFactory.decodeResource(mRes, R.drawable.scaled1, mOptNoScale); 79 80 Bitmap bitmap = Bitmap.createBitmap(expected.getWidth(), expected.getHeight(), 81 Bitmap.Config.ARGB_8888); 82 Canvas c = new Canvas(bitmap); 83 RectF rectf = new RectF(0, 0, c.getWidth(), c.getHeight()); 84 mNinePatch.draw(c, rectf); 85 checkBitmapWithAlpha(expected, bitmap, ALPHA_OPAQUE); 86 87 expected = BitmapFactory.decodeResource(mRes, R.drawable.scaled2, mOptNoScale); 88 bitmap = Bitmap.createBitmap(expected.getWidth(), expected.getHeight(), 89 Bitmap.Config.ARGB_8888); 90 c = new Canvas(bitmap); 91 Rect rect = new Rect(0, 0, c.getWidth(), c.getHeight()); 92 mNinePatch.draw(c, rect); 93 checkBitmapWithAlpha(expected, bitmap, ALPHA_OPAQUE); 94 95 bitmap = Bitmap.createBitmap(expected.getWidth(), expected.getHeight(), 96 Bitmap.Config.ARGB_8888); 97 c = new Canvas(bitmap); 98 rect = new Rect(0, 0, c.getWidth(), c.getHeight()); 99 final int alpha = 128; 100 Paint p = new Paint(); 101 p.setAlpha(alpha); 102 mNinePatch.draw(c, rect, p); 103 checkBitmapWithAlpha(expected, bitmap, alpha); 104 105 bitmap = Bitmap.createBitmap(expected.getWidth(), expected.getHeight(), 106 Bitmap.Config.ARGB_8888); 107 c = new Canvas(bitmap); 108 rectf = new RectF(0, 0, c.getWidth(), c.getHeight()); 109 mNinePatch.setPaint(p); 110 mNinePatch.draw(c, rectf); 111 checkBitmapWithAlpha(expected, bitmap, alpha); 112 } 113 114 private void checkBitmapWithAlpha(Bitmap expected, Bitmap bitmap, int alpha) { 115 assertEquals(expected.getWidth(), bitmap.getWidth()); 116 assertEquals(expected.getHeight(), bitmap.getHeight()); 117 int width = expected.getWidth(); 118 int height = expected.getHeight(); 119 120 for (int i = 0; i < width; i++) { 121 for (int j = 0; j < height; j++) { 122 int expectedPixel = expected.getPixel(i, j); 123 int actualPixel = bitmap.getPixel(i, j); 124 int expectedAlpha = Color.alpha(expectedPixel); 125 int actualAlpha = Color.alpha(actualPixel); 126 expectedPixel &= 0xFFFFFF; 127 actualPixel &= 0xFFFFFF; 128 assertEquals(expectedPixel, actualPixel); 129 assertEquals(expectedAlpha * alpha / ALPHA_OPAQUE, actualAlpha, 1); 130 } 131 } 132 } 133 134 public void testHasAlpha() { 135 assertFalse(mNinePatch.hasAlpha()); 136 assertEquals(mNinePatch.hasAlpha(), mBitmap.hasAlpha()); 137 138 Bitmap bitmap = 139 BitmapFactory.decodeResource(mRes, R.drawable.transparent_border, mOptNoScale); 140 byte[] chunk = bitmap.getNinePatchChunk(); 141 NinePatch ninePatch = new NinePatch(bitmap, chunk, NAME); 142 assertTrue(ninePatch.hasAlpha()); 143 assertEquals(ninePatch.hasAlpha(), bitmap.hasAlpha()); 144 } 145 146 public void testGetHeight() { 147 assertEquals(5, mNinePatch.getHeight()); 148 assertEquals(mNinePatch.getHeight(), mBitmap.getHeight()); 149 } 150 151 public void testGetWidth() { 152 assertEquals(5, mNinePatch.getHeight()); 153 assertEquals(mNinePatch.getWidth(), mBitmap.getWidth()); 154 } 155 156 public void testGetDensity() { 157 mBitmap.setDensity(11); 158 assertEquals(11, mNinePatch.getDensity()); 159 mNinePatch = new NinePatch(mBitmap, mChunk, NAME); 160 assertEquals(mNinePatch.getDensity(), mBitmap.getDensity()); 161 } 162 163 public void testGetTransparentRegion() { 164 // no transparency in opaque bitmap 165 Rect location = new Rect(0, 0, mBitmap.getWidth(), mBitmap.getHeight()); 166 Region region = mNinePatch.getTransparentRegion(location); 167 assertNull(region); 168 169 // transparent border of 1px 170 mBitmap = BitmapFactory.decodeResource(mRes, R.drawable.transparent_border, mOptNoScale); 171 mChunk = mBitmap.getNinePatchChunk(); 172 assertNotNull(mChunk); 173 mNinePatch = new NinePatch(mBitmap, mChunk, NAME); 174 175 location = new Rect(0, 0, mBitmap.getWidth(), mBitmap.getHeight()); 176 region = mNinePatch.getTransparentRegion(location); 177 assertNotNull(region); 178 Rect regionBounds = region.getBounds(); 179 assertBounds(regionBounds, 0, 0, 5, 5); 180 181 location = new Rect(0, 0, mBitmap.getWidth() * 2, mBitmap.getHeight() * 2); 182 region = mNinePatch.getTransparentRegion(location); 183 assertNotNull(region); 184 regionBounds = region.getBounds(); 185 assertBounds(regionBounds, 0, 0, 10, 10); 186 187 // transparent padding of 1px on the right side 188 mBitmap = BitmapFactory.decodeResource(mRes, R.drawable.transparent_right, mOptNoScale); 189 mChunk = mBitmap.getNinePatchChunk(); 190 assertNotNull(mChunk); 191 mNinePatch = new NinePatch(mBitmap, mChunk, NAME); 192 193 location = new Rect(0, 0, mBitmap.getWidth(), mBitmap.getHeight()); 194 region = mNinePatch.getTransparentRegion(location); 195 assertNotNull(region); 196 regionBounds = region.getBounds(); 197 assertBounds(regionBounds, 4, 0, 5, 5); 198 199 location = new Rect(0, 0, mBitmap.getWidth() * 2, mBitmap.getHeight() * 2); 200 region = mNinePatch.getTransparentRegion(location); 201 regionBounds = region.getBounds(); 202 assertBounds(regionBounds, 9, 0, 10, 10); 203 } 204 205 private void assertBounds(Rect regionBounds, int left, int top, int right, int bottom) { 206 assertEquals(left, regionBounds.left); 207 assertEquals(top, regionBounds.top); 208 assertEquals(right, regionBounds.right); 209 assertEquals(bottom, regionBounds.bottom); 210 } 211 } 212