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