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 package android.graphics.cts;
     17 
     18 import static org.junit.Assert.assertEquals;
     19 import static org.junit.Assert.assertTrue;
     20 
     21 import android.graphics.Bitmap;
     22 import android.graphics.Bitmap.Config;
     23 import android.graphics.Canvas;
     24 import android.graphics.Color;
     25 import android.graphics.CornerPathEffect;
     26 import android.graphics.Paint;
     27 import android.graphics.Paint.Style;
     28 import android.graphics.Path;
     29 import android.graphics.PathEffect;
     30 import android.graphics.PorterDuff.Mode;
     31 import android.graphics.PorterDuffXfermode;
     32 import android.graphics.RectF;
     33 import android.support.test.filters.SmallTest;
     34 import android.support.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 CornerPathEffectTest {
     42     private static final int BITMAP_WIDTH = 100;
     43     private static final int BITMAP_HEIGHT = 100;
     44     private static final int PADDING = 10;
     45     private static final int RADIUS = 20;
     46     private static final int TOLERANCE = 5;
     47 
     48     @Test
     49     public void testCornerPathEffect() {
     50         Path path = new Path();
     51         path.moveTo(0, PADDING);
     52         path.lineTo(BITMAP_WIDTH - PADDING, PADDING);
     53         path.lineTo(BITMAP_WIDTH - PADDING, BITMAP_HEIGHT);
     54 
     55         PathEffect effect = new CornerPathEffect(RADIUS);
     56 
     57         Paint pathPaint = new Paint();
     58         pathPaint.setColor(Color.GREEN);
     59         pathPaint.setStyle(Style.STROKE);
     60         pathPaint.setStrokeWidth(0);
     61         pathPaint.setPathEffect(effect);
     62 
     63         Bitmap bitmap = Bitmap.createBitmap(BITMAP_WIDTH, BITMAP_HEIGHT, Config.ARGB_8888);
     64         Canvas canvas = new Canvas(bitmap);
     65 
     66         // draw the path using the corner path effect
     67         canvas.drawPath(path, pathPaint);
     68 
     69         // create a path that describes the expected shape after the corner is rounded
     70         Path expectedPath = new Path();
     71         RectF oval = new RectF(BITMAP_WIDTH - PADDING - 2 * RADIUS, PADDING,
     72                 BITMAP_WIDTH - PADDING, PADDING + 2 * RADIUS);
     73         expectedPath.moveTo(0, PADDING);
     74         expectedPath.arcTo(oval, 270, 90);
     75         expectedPath.lineTo(BITMAP_WIDTH - PADDING, BITMAP_HEIGHT);
     76 
     77         // A paint that draws the expected path with a tolerance width into the red channel
     78         Paint expectedPaint = new Paint();
     79         expectedPaint.setColor(Color.RED);
     80         expectedPaint.setStyle(Style.STROKE);
     81         expectedPaint.setStrokeWidth(TOLERANCE);
     82         expectedPaint.setXfermode(new PorterDuffXfermode(Mode.SCREEN));
     83 
     84         canvas.drawPath(expectedPath, expectedPaint);
     85 
     86         int numPixels = 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                     numPixels += 1;
     92                     // green path must overlap with red guide line
     93                     assertEquals(Color.YELLOW, pixel);
     94                 }
     95             }
     96         }
     97         // number of pixels that should be on a straight line
     98         int straightLines = BITMAP_WIDTH - PADDING - RADIUS + BITMAP_HEIGHT - PADDING - RADIUS;
     99         // number of pixels forming the corner
    100         int cornerPixels = numPixels - straightLines;
    101         // rounded corner must have less pixels than a sharp corner
    102         assertTrue(cornerPixels < 2 * RADIUS);
    103         // ... but not as few as a diagonal
    104         assertTrue(cornerPixels > Math.sqrt(2 * Math.pow(RADIUS, 2)));
    105     }
    106 }
    107