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