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