Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2008 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 
     20 import android.graphics.Bitmap;
     21 import android.graphics.Canvas;
     22 import android.graphics.Color;
     23 import android.graphics.Paint;
     24 import android.graphics.Path;
     25 import android.graphics.PathDashPathEffect;
     26 import android.graphics.RectF;
     27 import android.graphics.Bitmap.Config;
     28 import android.graphics.Path.Direction;
     29 
     30 import junit.framework.TestCase;
     31 
     32 public class PathDashPathEffectTest extends TestCase {
     33 
     34     private static final int SQUARE = 10;
     35     private static final int ADVANCE = 30;
     36     private static final int WIDTH = 100;
     37     private static final int HEIGHT = 100;
     38 
     39     public void testPathDashPathEffect() {
     40         Bitmap b = Bitmap.createBitmap(WIDTH, HEIGHT, Config.ARGB_8888);
     41         b.eraseColor(Color.BLACK);
     42         PathDashPathEffect effect = new PathDashPathEffect(shape(), ADVANCE, 0,
     43                 PathDashPathEffect.Style.TRANSLATE);
     44         Canvas canvas = new Canvas(b);
     45         Paint p = new Paint();
     46         p.setPathEffect(effect);
     47         p.setColor(Color.RED);
     48         canvas.drawPath(path(), p);
     49 
     50         Bitmap expected = Bitmap.createBitmap(WIDTH, HEIGHT, Config.ARGB_8888);
     51         expected.eraseColor(Color.BLACK);
     52         canvas = new Canvas(expected);
     53         p = new Paint();
     54         p.setColor(Color.RED);
     55         RectF rect = new RectF(0, HEIGHT / 2 - SQUARE, 0, HEIGHT / 2 + SQUARE);
     56         for (int i = 0; i <= WIDTH + SQUARE; i += ADVANCE) {
     57             rect.left = i - SQUARE;
     58             rect.right = i + SQUARE;
     59             canvas.drawRect(rect, p);
     60         }
     61 
     62         int diffCount = 0;
     63         for (int y = 0; y < HEIGHT; y++) {
     64             for (int x = 0; x < WIDTH; x++) {
     65                 if (expected.getPixel(x, y) != b.getPixel(x, y)) {
     66                     diffCount += 1;
     67                 }
     68             }
     69         }
     70         assertEquals(0, diffCount);
     71     }
     72 
     73     private static Path path() {
     74         Path p = new Path();
     75         p.moveTo(0, HEIGHT / 2);
     76         p.lineTo(WIDTH, HEIGHT / 2);
     77         return p;
     78     }
     79 
     80     private static Path shape() {
     81         Path p = new Path();
     82         p.addRect(new RectF(-SQUARE, -SQUARE, SQUARE, SQUARE), Direction.CCW);
     83         return p;
     84     }
     85 }
     86