1 /* 2 * Copyright (C) 2009 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.shapes.cts; 18 19 import junit.framework.TestCase; 20 21 import android.graphics.Bitmap; 22 import android.graphics.Bitmap.Config; 23 import android.graphics.Canvas; 24 import android.graphics.Outline; 25 import android.graphics.Paint; 26 import android.graphics.Paint.Style; 27 import android.graphics.Path; 28 import android.graphics.drawable.shapes.PathShape; 29 import android.test.suitebuilder.annotation.SmallTest; 30 31 public class PathShapeTest extends TestCase { 32 private static final int TEST_COLOR_1 = 0xFF00FF00; 33 private static final int TEST_COLOR_2 = 0xFFFF0000; 34 35 private static final int TOLERANCE = 4; 36 37 @SmallTest testConstructor()38 public void testConstructor() { 39 new PathShape(new Path(), 1f, 5f); 40 41 new PathShape(new Path(), -1f, -1f); 42 43 new PathShape(null, 0f, 0f); 44 } 45 46 @SmallTest testDraw()47 public void testDraw() { 48 final int SHAPE_SIZE = 200; 49 50 // draw a square rotated by 45 degrees centered on (50, 50) 51 Path path = new Path(); 52 path.moveTo(50, 0); 53 path.lineTo(0, 50); 54 path.lineTo(50, 100); 55 path.lineTo(100, 50); 56 path.close(); 57 PathShape pathShape = new PathShape(path, SHAPE_SIZE, SHAPE_SIZE); 58 Bitmap bitmap = Bitmap.createBitmap(SHAPE_SIZE, SHAPE_SIZE, Config.ARGB_8888); 59 Canvas canvas = new Canvas(bitmap); 60 Paint paint = new Paint(); 61 paint.setStyle(Style.FILL); 62 paint.setColor(TEST_COLOR_1); 63 pathShape.resize(SHAPE_SIZE, SHAPE_SIZE); 64 65 pathShape.draw(canvas, paint); 66 // check center point 67 assertEquals(TEST_COLOR_1, bitmap.getPixel(50, 50)); 68 69 paint.setColor(TEST_COLOR_2); 70 // scale down to half size; diagonal is now 50px 71 pathShape.resize(SHAPE_SIZE / 2, SHAPE_SIZE / 2); 72 pathShape.draw(canvas, paint); 73 // count number of pixels with TEST_COLOR_2 horizontally, vertically and diagonally 74 int horizontal = 0; 75 int vertical = 0; 76 int diagonal = 0; 77 for (int i = 0; i < 50; i++) { 78 if (bitmap.getPixel(25, i) == TEST_COLOR_2) { 79 vertical += 1; 80 } 81 if (bitmap.getPixel(i, 25) == TEST_COLOR_2) { 82 horizontal += 1; 83 } 84 if (bitmap.getPixel(i, i) == TEST_COLOR_2) { 85 diagonal += 1; 86 } 87 } 88 assertEquals(50, horizontal, TOLERANCE); 89 assertEquals(50, vertical, TOLERANCE); 90 assertEquals(25, diagonal, TOLERANCE); 91 } 92 93 @SmallTest testClone()94 public void testClone() throws CloneNotSupportedException { 95 PathShape pathShape = new PathShape(new Path(), 1f, 5f); 96 pathShape.resize(100f, 200f); 97 PathShape clonedShape = pathShape.clone(); 98 assertEquals(100f, pathShape.getWidth()); 99 assertEquals(200f, pathShape.getHeight()); 100 101 assertNotSame(pathShape, clonedShape); 102 assertEquals(pathShape.getWidth(), clonedShape.getWidth()); 103 assertEquals(pathShape.getHeight(), clonedShape.getHeight()); 104 } 105 106 @SmallTest testGetOutline()107 public void testGetOutline() { 108 Outline outline = new Outline(); 109 PathShape shape; 110 111 // This is a no-op. Just make sure it doesn't crash. 112 outline.setEmpty(); 113 shape = new PathShape(new Path(), 0, 0); 114 shape.getOutline(outline); 115 assertTrue(outline.isEmpty()); 116 } 117 } 118