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.cts;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertTrue;
21 
22 import android.graphics.Bitmap;
23 import android.graphics.Bitmap.Config;
24 import android.graphics.Canvas;
25 import android.graphics.Color;
26 import android.graphics.DiscretePathEffect;
27 import android.graphics.Paint;
28 import android.graphics.Paint.Style;
29 import android.graphics.Path;
30 import android.graphics.PorterDuff.Mode;
31 import android.graphics.PorterDuffXfermode;
32 import android.support.test.filters.SmallTest;
33 import android.support.test.runner.AndroidJUnit4;
34 
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 
38 @SmallTest
39 @RunWith(AndroidJUnit4.class)
40 public class DiscretePathEffectTest {
41     private static final int BITMAP_WIDTH = 200;
42     private static final int BITMAP_HEIGHT = 100;
43     private static final int START_X = 10;
44     private static final int END_X = BITMAP_WIDTH - START_X;
45     private static final int COORD_Y = BITMAP_HEIGHT / 2;
46     private static final int SEGMENT_LENGTH = 10; // must be < BITMAP_WIDTH
47     private static final int DEVIATION = 10; // must be < BITMAP_HEIGHT
48 
49     @Test
testDiscretePathEffect()50     public void testDiscretePathEffect() {
51         DiscretePathEffect effect = new DiscretePathEffect(SEGMENT_LENGTH, DEVIATION);
52 
53         Paint paint = new Paint();
54         paint.setColor(Color.GREEN);
55         paint.setStyle(Style.STROKE);
56         paint.setStrokeWidth(0);
57         paint.setPathEffect(effect);
58 
59         Path path = new Path();
60         path.moveTo(START_X, COORD_Y);
61         path.lineTo(END_X, COORD_Y);
62 
63         Bitmap bitmap = Bitmap.createBitmap(BITMAP_WIDTH, BITMAP_HEIGHT, Config.ARGB_8888);
64         bitmap.eraseColor(Color.TRANSPARENT);
65 
66         Canvas canvas = new Canvas(bitmap);
67         canvas.drawPath(path, paint);
68 
69         // draw guide line into red channel (each segment should cross this once)
70         paint = new Paint();
71         paint.setColor(Color.RED);
72         paint.setStyle(Style.STROKE);
73         paint.setStrokeWidth(0);
74         paint.setXfermode(new PorterDuffXfermode(Mode.SCREEN));
75         canvas.drawPath(path, paint);
76 
77         // draw guide rectangle into blue channel (each segment must be completely inside this)
78         paint.setColor(Color.BLUE);
79         paint.setStrokeWidth(1 + 2 * DEVIATION);
80         canvas.drawPath(path, paint);
81 
82         int intersect = 0;
83         int numGreenPixels = 0;
84         int minY = BITMAP_HEIGHT;
85         int maxY = 0;
86         for (int y = 0; y < BITMAP_HEIGHT; y++) {
87             for (int x = 0; x < BITMAP_WIDTH; x++) {
88                 int pixel = bitmap.getPixel(x, y);
89                 if (Color.green(pixel) > 0) {
90                     numGreenPixels += 1;
91                     minY = Math.min(minY, y);
92                     maxY = Math.max(maxY, y);
93                     assertEquals(0xFF, Color.blue(pixel));
94                     if (Color.red(pixel) > 0) {
95                         intersect += 1;
96                     }
97                 }
98             }
99         }
100         int lineLength = END_X - START_X;
101         // the number of pixels in all segments must be at least the same as the line length
102         assertTrue(numGreenPixels >= lineLength);
103         // green line must vary in y direction
104         assertTrue(maxY - minY > 0);
105         // ... but not too much
106         assertTrue(maxY - minY <= 1 + 2 * DEVIATION);
107         // intersecting pixels must be less than line length, otherwise deviation doesn't work
108         assertTrue(intersect < lineLength);
109         // there must be at least as many intersecting pixels as there are full segments
110         assertTrue(intersect >= lineLength / SEGMENT_LENGTH);
111     }
112 }
113