Home | History | Annotate | Download | only in cts
      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
     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 
     60         Path path = new Path();
     61         path.moveTo(START_X, COORD_Y);
     62         path.lineTo(END_X, COORD_Y);
     63 
     64         Bitmap bitmap = Bitmap.createBitmap(BITMAP_WIDTH, BITMAP_HEIGHT, Config.ARGB_8888);
     65         bitmap.eraseColor(Color.TRANSPARENT);
     66 
     67         Canvas canvas = new Canvas(bitmap);
     68         canvas.drawPath(path, paint);
     69 
     70         // draw guide line into red channel (each segment should cross this once)
     71         paint = new Paint();
     72         paint.setColor(Color.RED);
     73         paint.setStyle(Style.STROKE);
     74         paint.setStrokeWidth(0);
     75         paint.setXfermode(new PorterDuffXfermode(Mode.SCREEN));
     76         canvas.drawPath(path, paint);
     77 
     78         // draw guide rectangle into blue channel (each segment must be completely inside this)
     79         paint.setColor(Color.BLUE);
     80         paint.setStrokeWidth(1 + 2 * DEVIATION);
     81         canvas.drawPath(path, paint);
     82 
     83         int intersect = 0;
     84         int numGreenPixels = 0;
     85         int minY = BITMAP_HEIGHT;
     86         int maxY = 0;
     87         for (int y = 0; y < BITMAP_HEIGHT; y++) {
     88             for (int x = 0; x < BITMAP_WIDTH; x++) {
     89                 int pixel = bitmap.getPixel(x, y);
     90                 if (Color.green(pixel) > 0) {
     91                     numGreenPixels += 1;
     92                     minY = Math.min(minY, y);
     93                     maxY = Math.max(maxY, y);
     94                     assertEquals(0xFF, Color.blue(pixel));
     95                     if (Color.red(pixel) > 0) {
     96                         intersect += 1;
     97                     }
     98                 }
     99             }
    100         }
    101         int lineLength = END_X - START_X;
    102         // the number of pixels in all segments must be at least the same as the line length
    103         assertTrue(numGreenPixels >= lineLength);
    104         // green line must vary in y direction
    105         assertTrue(maxY - minY > 0);
    106         // ... but not too much
    107         assertTrue(maxY - minY <= 1 + 2 * DEVIATION);
    108         // intersecting pixels must be less than line length, otherwise deviation doesn't work
    109         assertTrue(intersect < lineLength);
    110         // there must be at least as many intersecting pixels as there are full segments
    111         assertTrue(intersect >= lineLength / SEGMENT_LENGTH);
    112     }
    113 }
    114