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 static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertFalse; 21 import static org.junit.Assert.assertNotNull; 22 import static org.junit.Assert.assertNull; 23 import static org.junit.Assert.assertTrue; 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.NinePatch; 31 import android.graphics.Paint; 32 import android.graphics.Rect; 33 import android.graphics.RectF; 34 import android.graphics.Region; 35 import android.support.test.InstrumentationRegistry; 36 import android.support.test.filters.SmallTest; 37 import android.support.test.runner.AndroidJUnit4; 38 39 import org.junit.Before; 40 import org.junit.Test; 41 import org.junit.runner.RunWith; 42 43 @SmallTest 44 @RunWith(AndroidJUnit4.class) 45 public class NinePatchTest { 46 private static final int ALPHA_OPAQUE = 0xFF; 47 private static final String NAME = "TESTNAME"; 48 private static final int WIDTH = 80; 49 private static final int HEIGTH = 120; 50 private static final int[] COLOR = new int[WIDTH * HEIGTH]; 51 52 private NinePatch mNinePatch; 53 private Bitmap mBitmap; 54 private BitmapFactory.Options mOptNoScale; 55 private Resources mRes; 56 private byte[] mChunk; 57 58 @Before 59 public void setup() { 60 mRes = InstrumentationRegistry.getTargetContext().getResources(); 61 mOptNoScale = new BitmapFactory.Options(); 62 mOptNoScale.inDensity = mOptNoScale.inTargetDensity = mRes.getDisplayMetrics().densityDpi; 63 mBitmap = BitmapFactory.decodeResource(mRes, R.drawable.opaque, mOptNoScale); 64 mChunk = mBitmap.getNinePatchChunk(); 65 assertNotNull(mChunk); 66 mNinePatch = new NinePatch(mBitmap, mChunk, NAME); 67 } 68 69 @Test(expected=Exception.class) 70 public void testConstructorTooShort() { 71 mNinePatch = new NinePatch(mBitmap, new byte[2]); 72 } 73 74 @Test(expected=Exception.class) 75 public void testConstructorNamedTooShort() { 76 mNinePatch = new NinePatch(mBitmap, new byte[2], NAME); 77 } 78 79 @Test 80 public void testConstructor() { 81 mNinePatch = new NinePatch(mBitmap, mChunk); 82 assertEquals(mBitmap, mNinePatch.getBitmap()); 83 assertEquals(null, mNinePatch.getName()); 84 85 mNinePatch = new NinePatch(mBitmap, mChunk, NAME); 86 assertEquals(mBitmap, mNinePatch.getBitmap()); 87 assertEquals(NAME, mNinePatch.getName()); 88 } 89 90 @Test 91 public void testPaintAccessors() { 92 Paint p = new Paint(); 93 mNinePatch = new NinePatch(mBitmap, mChunk, NAME); 94 assertNull(mNinePatch.getPaint()); 95 96 mNinePatch.setPaint(p); 97 assertEquals(p, mNinePatch.getPaint()); 98 99 mNinePatch.setPaint(null); 100 assertNull(mNinePatch.getPaint()); 101 } 102 103 @Test 104 public void testIsNinePatchChunk() { 105 assertTrue(NinePatch.isNinePatchChunk(mChunk)); 106 Bitmap bitmap = Bitmap.createBitmap(COLOR, 10, 10, Bitmap.Config.ARGB_4444); 107 assertFalse(NinePatch.isNinePatchChunk(bitmap.getNinePatchChunk())); 108 assertFalse(NinePatch.isNinePatchChunk(null)); 109 } 110 111 @Test 112 public void testDraw() { 113 Bitmap expected = BitmapFactory.decodeResource(mRes, R.drawable.scaled1, mOptNoScale); 114 115 Bitmap bitmap = Bitmap.createBitmap(expected.getWidth(), expected.getHeight(), 116 Bitmap.Config.ARGB_8888); 117 Canvas c = new Canvas(bitmap); 118 RectF rectf = new RectF(0, 0, c.getWidth(), c.getHeight()); 119 mNinePatch.draw(c, rectf); 120 verifyBitmapWithAlpha(expected, bitmap, ALPHA_OPAQUE); 121 122 expected = BitmapFactory.decodeResource(mRes, R.drawable.scaled2, mOptNoScale); 123 bitmap = Bitmap.createBitmap(expected.getWidth(), expected.getHeight(), 124 Bitmap.Config.ARGB_8888); 125 c = new Canvas(bitmap); 126 Rect rect = new Rect(0, 0, c.getWidth(), c.getHeight()); 127 mNinePatch.draw(c, rect); 128 verifyBitmapWithAlpha(expected, bitmap, ALPHA_OPAQUE); 129 130 bitmap = Bitmap.createBitmap(expected.getWidth(), expected.getHeight(), 131 Bitmap.Config.ARGB_8888); 132 c = new Canvas(bitmap); 133 rect = new Rect(0, 0, c.getWidth(), c.getHeight()); 134 final int alpha = 128; 135 Paint p = new Paint(); 136 p.setAlpha(alpha); 137 mNinePatch.draw(c, rect, p); 138 verifyBitmapWithAlpha(expected, bitmap, alpha); 139 140 bitmap = Bitmap.createBitmap(expected.getWidth(), expected.getHeight(), 141 Bitmap.Config.ARGB_8888); 142 c = new Canvas(bitmap); 143 rectf = new RectF(0, 0, c.getWidth(), c.getHeight()); 144 mNinePatch.setPaint(p); 145 mNinePatch.draw(c, rectf); 146 verifyBitmapWithAlpha(expected, bitmap, alpha); 147 } 148 149 private void verifyBitmapWithAlpha(Bitmap expected, Bitmap bitmap, int alpha) { 150 assertEquals(expected.getWidth(), bitmap.getWidth()); 151 assertEquals(expected.getHeight(), bitmap.getHeight()); 152 int width = expected.getWidth(); 153 int height = expected.getHeight(); 154 155 for (int i = 0; i < width; i++) { 156 for (int j = 0; j < height; j++) { 157 int expectedPixel = expected.getPixel(i, j); 158 int actualPixel = bitmap.getPixel(i, j); 159 int expectedAlpha = Color.alpha(expectedPixel); 160 int actualAlpha = Color.alpha(actualPixel); 161 expectedPixel &= 0xFFFFFF; 162 actualPixel &= 0xFFFFFF; 163 assertEquals(expectedPixel, actualPixel); 164 assertEquals(expectedAlpha * alpha / ALPHA_OPAQUE, actualAlpha, 1); 165 } 166 } 167 } 168 169 @Test 170 public void testHasAlpha() { 171 assertFalse(mNinePatch.hasAlpha()); 172 assertEquals(mNinePatch.hasAlpha(), mBitmap.hasAlpha()); 173 174 Bitmap bitmap = 175 BitmapFactory.decodeResource(mRes, R.drawable.transparent_border, mOptNoScale); 176 byte[] chunk = bitmap.getNinePatchChunk(); 177 NinePatch ninePatch = new NinePatch(bitmap, chunk, NAME); 178 assertTrue(ninePatch.hasAlpha()); 179 assertEquals(ninePatch.hasAlpha(), bitmap.hasAlpha()); 180 } 181 182 @Test 183 public void testGetHeight() { 184 assertEquals(5, mNinePatch.getHeight()); 185 assertEquals(mNinePatch.getHeight(), mBitmap.getHeight()); 186 } 187 188 @Test 189 public void testGetWidth() { 190 assertEquals(5, mNinePatch.getHeight()); 191 assertEquals(mNinePatch.getWidth(), mBitmap.getWidth()); 192 } 193 194 @Test 195 public void testGetDensity() { 196 mBitmap.setDensity(11); 197 assertEquals(11, mNinePatch.getDensity()); 198 mNinePatch = new NinePatch(mBitmap, mChunk, NAME); 199 assertEquals(mNinePatch.getDensity(), mBitmap.getDensity()); 200 } 201 202 @Test 203 public void testGetTransparentRegion() { 204 // no transparency in opaque bitmap 205 Rect location = new Rect(0, 0, mBitmap.getWidth(), mBitmap.getHeight()); 206 Region region = mNinePatch.getTransparentRegion(location); 207 assertNull(region); 208 209 // transparent border of 1px 210 mBitmap = BitmapFactory.decodeResource(mRes, R.drawable.transparent_border, mOptNoScale); 211 mChunk = mBitmap.getNinePatchChunk(); 212 assertNotNull(mChunk); 213 mNinePatch = new NinePatch(mBitmap, mChunk, NAME); 214 215 location = new Rect(0, 0, mBitmap.getWidth(), mBitmap.getHeight()); 216 region = mNinePatch.getTransparentRegion(location); 217 assertNotNull(region); 218 Rect regionBounds = region.getBounds(); 219 verifyBounds(regionBounds, 0, 0, 5, 5); 220 221 location = new Rect(0, 0, mBitmap.getWidth() * 2, mBitmap.getHeight() * 2); 222 region = mNinePatch.getTransparentRegion(location); 223 assertNotNull(region); 224 regionBounds = region.getBounds(); 225 verifyBounds(regionBounds, 0, 0, 10, 10); 226 227 // transparent padding of 1px on the right side 228 mBitmap = BitmapFactory.decodeResource(mRes, R.drawable.transparent_right, mOptNoScale); 229 mChunk = mBitmap.getNinePatchChunk(); 230 assertNotNull(mChunk); 231 mNinePatch = new NinePatch(mBitmap, mChunk, NAME); 232 233 location = new Rect(0, 0, mBitmap.getWidth(), mBitmap.getHeight()); 234 region = mNinePatch.getTransparentRegion(location); 235 assertNotNull(region); 236 regionBounds = region.getBounds(); 237 verifyBounds(regionBounds, 4, 0, 5, 5); 238 239 location = new Rect(0, 0, mBitmap.getWidth() * 2, mBitmap.getHeight() * 2); 240 region = mNinePatch.getTransparentRegion(location); 241 regionBounds = region.getBounds(); 242 verifyBounds(regionBounds, 9, 0, 10, 10); 243 } 244 245 private void verifyBounds(Rect regionBounds, int left, int top, int right, int bottom) { 246 assertEquals(left, regionBounds.left); 247 assertEquals(top, regionBounds.top); 248 assertEquals(right, regionBounds.right); 249 assertEquals(bottom, regionBounds.bottom); 250 } 251 } 252