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