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.drawable.cts; 18 19 import com.android.cts.stub.R; 20 21 22 import org.xmlpull.v1.XmlPullParser; 23 import org.xmlpull.v1.XmlPullParserException; 24 25 import android.content.res.Resources; 26 import android.graphics.Canvas; 27 import android.graphics.ColorFilter; 28 import android.graphics.Paint; 29 import android.graphics.PixelFormat; 30 import android.graphics.Rect; 31 import android.graphics.Shader; 32 import android.graphics.drawable.ShapeDrawable; 33 import android.graphics.drawable.Drawable.ConstantState; 34 import android.graphics.drawable.ShapeDrawable.ShaderFactory; 35 import android.graphics.drawable.shapes.RectShape; 36 import android.graphics.drawable.shapes.Shape; 37 import android.test.AndroidTestCase; 38 import android.util.AttributeSet; 39 import android.util.Xml; 40 41 import java.io.IOException; 42 43 public class ShapeDrawableTest extends AndroidTestCase { testConstructors()44 public void testConstructors() { 45 new ShapeDrawable(); 46 47 new ShapeDrawable(null); 48 49 new ShapeDrawable(new RectShape()); 50 } 51 testDraw()52 public void testDraw() { 53 ShapeDrawable shapeDrawable = new ShapeDrawable(); 54 55 try { 56 shapeDrawable.draw(null); 57 fail("should throw NullPointerException."); 58 } catch (NullPointerException e) { 59 } 60 } 61 testGetChangingConfigurations()62 public void testGetChangingConfigurations() { 63 ShapeDrawable shapeDrawable = new ShapeDrawable(); 64 assertEquals(0, shapeDrawable.getChangingConfigurations()); 65 66 shapeDrawable.setChangingConfigurations(1); 67 assertEquals(1, shapeDrawable.getChangingConfigurations()); 68 69 shapeDrawable.setChangingConfigurations(Integer.MIN_VALUE); 70 assertEquals(Integer.MIN_VALUE, shapeDrawable.getChangingConfigurations()); 71 72 shapeDrawable.setChangingConfigurations(Integer.MAX_VALUE); 73 assertEquals(Integer.MAX_VALUE, shapeDrawable.getChangingConfigurations()); 74 75 shapeDrawable.setChangingConfigurations(1); 76 shapeDrawable.getConstantState(); 77 shapeDrawable.setChangingConfigurations(2); 78 assertEquals(3, shapeDrawable.getChangingConfigurations()); 79 } 80 testGetConstantState()81 public void testGetConstantState() { 82 ShapeDrawable shapeDrawable = new ShapeDrawable(); 83 84 shapeDrawable.setChangingConfigurations(1); 85 ConstantState constantState = shapeDrawable.getConstantState(); 86 assertNotNull(constantState); 87 assertEquals(1, constantState.getChangingConfigurations()); 88 } 89 testAccessIntrinsicHeight()90 public void testAccessIntrinsicHeight() { 91 ShapeDrawable shapeDrawable = new ShapeDrawable(); 92 assertEquals(0, shapeDrawable.getIntrinsicHeight()); 93 94 shapeDrawable.setIntrinsicHeight(10); 95 assertEquals(10, shapeDrawable.getIntrinsicHeight()); 96 97 shapeDrawable.setIntrinsicHeight(Integer.MIN_VALUE); 98 assertEquals(Integer.MIN_VALUE, shapeDrawable.getIntrinsicHeight()); 99 100 shapeDrawable.setIntrinsicHeight(Integer.MAX_VALUE); 101 assertEquals(Integer.MAX_VALUE, shapeDrawable.getIntrinsicHeight()); 102 } 103 testAccessIntrinsicWidth()104 public void testAccessIntrinsicWidth() { 105 ShapeDrawable shapeDrawable = new ShapeDrawable(); 106 assertEquals(0, shapeDrawable.getIntrinsicWidth()); 107 108 shapeDrawable.setIntrinsicWidth(10); 109 assertEquals(10, shapeDrawable.getIntrinsicWidth()); 110 111 shapeDrawable.setIntrinsicWidth(Integer.MIN_VALUE); 112 assertEquals(Integer.MIN_VALUE, shapeDrawable.getIntrinsicWidth()); 113 114 shapeDrawable.setIntrinsicWidth(Integer.MAX_VALUE); 115 assertEquals(Integer.MAX_VALUE, shapeDrawable.getIntrinsicWidth()); 116 } 117 testGetOpacity()118 public void testGetOpacity() { 119 ShapeDrawable shapeDrawable = new ShapeDrawable(new RectShape()); 120 assertEquals(PixelFormat.TRANSLUCENT, shapeDrawable.getOpacity()); 121 122 shapeDrawable = new ShapeDrawable(); 123 assertEquals(255, shapeDrawable.getPaint().getAlpha()); 124 assertEquals(PixelFormat.OPAQUE, shapeDrawable.getOpacity()); 125 126 shapeDrawable.getPaint().setAlpha(0); 127 assertEquals(PixelFormat.TRANSPARENT, shapeDrawable.getOpacity()); 128 129 shapeDrawable.getPaint().setAlpha(128); 130 assertEquals(PixelFormat.TRANSLUCENT, shapeDrawable.getOpacity()); 131 } 132 testAccessPadding()133 public void testAccessPadding() { 134 ShapeDrawable shapeDrawable = new ShapeDrawable(); 135 Rect padding = new Rect(); 136 assertFalse(shapeDrawable.getPadding(padding)); 137 assertEquals(0, padding.left); 138 assertEquals(0, padding.top); 139 assertEquals(0, padding.right); 140 assertEquals(0, padding.bottom); 141 142 shapeDrawable.setPadding(10, 10, 100, 100); 143 assertTrue(shapeDrawable.getPadding(padding)); 144 assertEquals(10, padding.left); 145 assertEquals(10, padding.top); 146 assertEquals(100, padding.right); 147 assertEquals(100, padding.bottom); 148 149 shapeDrawable.setPadding(0, 0, 0, 0); 150 assertFalse(shapeDrawable.getPadding(padding)); 151 assertEquals(0, padding.left); 152 assertEquals(0, padding.top); 153 assertEquals(0, padding.right); 154 assertEquals(0, padding.bottom); 155 156 shapeDrawable.setPadding(new Rect(5, 5, 80, 80)); 157 assertTrue(shapeDrawable.getPadding(padding)); 158 assertEquals(5, padding.left); 159 assertEquals(5, padding.top); 160 assertEquals(80, padding.right); 161 assertEquals(80, padding.bottom); 162 163 shapeDrawable.setPadding(null); 164 assertFalse(shapeDrawable.getPadding(padding)); 165 assertEquals(0, padding.left); 166 assertEquals(0, padding.top); 167 assertEquals(0, padding.right); 168 assertEquals(0, padding.bottom); 169 170 try { 171 shapeDrawable.getPadding(null); 172 fail("should throw NullPointerException."); 173 } catch (NullPointerException e) { 174 } 175 } 176 testGetPaint()177 public void testGetPaint() { 178 ShapeDrawable shapeDrawable = new ShapeDrawable(); 179 assertNotNull(shapeDrawable.getPaint()); 180 assertEquals(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG, 181 shapeDrawable.getPaint().getFlags()); 182 } 183 testAccessShaderFactory()184 public void testAccessShaderFactory() { 185 ShapeDrawable shapeDrawable = new ShapeDrawable(); 186 assertNull(shapeDrawable.getShaderFactory()); 187 188 MockShaderFactory mockShaderFactory = new MockShaderFactory(); 189 shapeDrawable.setShaderFactory(mockShaderFactory); 190 assertSame(mockShaderFactory, shapeDrawable.getShaderFactory()); 191 192 shapeDrawable.setShaderFactory(null); 193 assertNull(shapeDrawable.getShaderFactory()); 194 } 195 196 private static class MockShaderFactory extends ShaderFactory { resize(int width, int height)197 public Shader resize(int width, int height) { 198 return null; 199 } 200 } 201 testAccessShape()202 public void testAccessShape() { 203 ShapeDrawable shapeDrawable = new ShapeDrawable(); 204 assertNull(shapeDrawable.getShape()); 205 206 RectShape rectShape = new RectShape(); 207 shapeDrawable.setShape(rectShape); 208 assertSame(rectShape, shapeDrawable.getShape()); 209 210 shapeDrawable.setShape(null); 211 assertNull(shapeDrawable.getShape()); 212 } 213 testInflate()214 public void testInflate() throws XmlPullParserException, IOException { 215 final Resources res = mContext.getResources(); 216 217 XmlPullParser parser = res.getXml(R.drawable.shapedrawable_test); 218 while (parser.next() != XmlPullParser.START_TAG) { 219 // ignore event, just seek to first tag 220 } 221 AttributeSet attrs = Xml.asAttributeSet(parser); 222 MockShapeDrawable shapeDrawable = new MockShapeDrawable(); 223 shapeDrawable.inflate(res, parser, attrs); 224 // values from shapedrawable_test.xml 225 assertEquals(42, shapeDrawable.getIntrinsicWidth()); 226 assertEquals(63, shapeDrawable.getIntrinsicHeight()); 227 Rect padding = new Rect(); 228 assertTrue(shapeDrawable.getPadding(padding)); 229 assertEquals(1, padding.left); 230 assertEquals(2, padding.top); 231 assertEquals(3, padding.right); 232 assertEquals(4, padding.bottom); 233 assertTrue(shapeDrawable.inflateTagCalled); 234 assertTrue(shapeDrawable.extendedAttrsSet); 235 } 236 testOnBoundsChange()237 public void testOnBoundsChange() { 238 // implementation details, do not test. 239 } 240 241 private class MockShapeDrawable extends ShapeDrawable { 242 public boolean inflateTagCalled; 243 public boolean extendedAttrsSet; 244 MockShapeDrawable()245 public MockShapeDrawable() { 246 super(); 247 } 248 MockShapeDrawable(Shape s)249 public MockShapeDrawable(Shape s) { 250 super(s); 251 } 252 onDraw(Shape shape, Canvas canvas, Paint paint)253 protected void onDraw(Shape shape, Canvas canvas, Paint paint) { 254 super.onDraw(shape, canvas, paint); 255 } 256 inflateTag(String name, Resources r, XmlPullParser parser, AttributeSet attrs)257 protected boolean inflateTag(String name, Resources r, XmlPullParser parser, 258 AttributeSet attrs) { 259 inflateTagCalled = true; 260 if (name.equals("testattrs")) { 261 extendedAttrsSet = true; 262 return true; 263 } 264 return super.inflateTag(name, r, parser, attrs); 265 } 266 } 267 testOnDraw()268 public void testOnDraw() { 269 MockShape mockShape = new MockShape(); 270 MockShapeDrawable shapeDrawable = new MockShapeDrawable(mockShape); 271 assertFalse(mockShape.hasDrawCalled()); 272 shapeDrawable.onDraw(mockShape, new Canvas(), new Paint()); 273 assertTrue(mockShape.hasDrawCalled()); 274 275 try { 276 shapeDrawable.onDraw(null, null, new Paint()); 277 fail("should throw NullPointerException."); 278 } catch (NullPointerException e) { 279 } 280 } 281 282 private static class MockShape extends Shape { 283 private boolean mDrawCalled = false; 284 hasDrawCalled()285 public boolean hasDrawCalled() { 286 return mDrawCalled; 287 } 288 reset()289 public void reset() { 290 mDrawCalled = false; 291 } 292 draw(Canvas canvas, Paint paint)293 public void draw(Canvas canvas, Paint paint) { 294 mDrawCalled = true; 295 } 296 } 297 testSetAlpha()298 public void testSetAlpha() { 299 ShapeDrawable shapeDrawable = new ShapeDrawable(); 300 shapeDrawable.setAlpha(0); 301 shapeDrawable.setAlpha(255); 302 shapeDrawable.setAlpha(-1); 303 shapeDrawable.setAlpha(256); 304 } 305 testSetColorFilter()306 public void testSetColorFilter() { 307 ShapeDrawable shapeDrawable = new ShapeDrawable(); 308 309 ColorFilter cf = new ColorFilter(); 310 shapeDrawable.setColorFilter(cf); 311 assertSame(cf, shapeDrawable.getPaint().getColorFilter()); 312 313 shapeDrawable.setColorFilter(null); 314 assertNull(shapeDrawable.getPaint().getColorFilter()); 315 } 316 testSetDither()317 public void testSetDither() { 318 ShapeDrawable shapeDrawable = new ShapeDrawable(); 319 320 shapeDrawable.setDither(true); 321 assertTrue(shapeDrawable.getPaint().isDither()); 322 323 shapeDrawable.setDither(false); 324 assertFalse(shapeDrawable.getPaint().isDither()); 325 } 326 testMutate()327 public void testMutate() { 328 // How to load a ShapeDrawable from resources. 329 } 330 } 331