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 20 import android.graphics.Bitmap; 21 import android.graphics.Bitmap.Config; 22 import android.graphics.Canvas; 23 import android.graphics.Color; 24 import android.graphics.DashPathEffect; 25 import android.graphics.Paint; 26 import android.graphics.Paint.Style; 27 import android.graphics.Path; 28 import android.graphics.PathEffect; 29 import android.util.Log; 30 31 import androidx.test.filters.SmallTest; 32 import androidx.test.runner.AndroidJUnit4; 33 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 37 @SmallTest 38 @RunWith(AndroidJUnit4.class) 39 public class DashPathEffectTest { 40 private static final int BITMAP_WIDTH = 200; 41 private static final int BITMAP_HEIGHT = 20; 42 private static final int START_X = 10; 43 private static final int END_X = BITMAP_WIDTH - START_X; 44 private static final int COORD_Y = BITMAP_HEIGHT / 2; 45 private static final float[] PATTERN = new float[] { 15, 5, 10, 5 }; 46 private static final int OFFSET = 5; 47 private static final int BACKGROUND = Color.TRANSPARENT; 48 private static final int FOREGROUND = Color.GREEN; 49 50 @Test 51 public void testDashPathEffect() { 52 PathEffect effect = new DashPathEffect(PATTERN, OFFSET); 53 Bitmap bitmap = Bitmap.createBitmap(BITMAP_WIDTH, BITMAP_HEIGHT, Config.ARGB_8888); 54 bitmap.eraseColor(BACKGROUND); 55 56 Path path = new Path(); 57 path.moveTo(START_X, COORD_Y); 58 path.lineTo(END_X, COORD_Y); 59 60 Paint paint = new Paint(); 61 paint.setStyle(Style.STROKE); 62 paint.setStrokeWidth(0); 63 paint.setColor(FOREGROUND); 64 paint.setPathEffect(effect); 65 66 Canvas canvas = new Canvas(bitmap); 67 canvas.drawPath(path, paint); 68 69 PatternIterator iterator = new PatternIterator(PATTERN, OFFSET); 70 for (int y = 0; y < BITMAP_HEIGHT; y++) { 71 for (int x = 0; x < BITMAP_WIDTH; x++) { 72 try { 73 if (y == COORD_Y && x >= START_X && x < END_X) { 74 if (iterator.next()) { 75 assertEquals(FOREGROUND, bitmap.getPixel(x, y)); 76 } else { 77 assertEquals(BACKGROUND, bitmap.getPixel(x, y)); 78 } 79 } else { 80 assertEquals(BACKGROUND, bitmap.getPixel(x, y)); 81 } 82 } catch (Error e) { 83 Log.w(getClass().getName(), "Failed at (" + x + "," + y + ")"); 84 throw e; 85 } 86 } 87 } 88 } 89 90 private static class PatternIterator { 91 private int mPatternOffset; 92 private int mLength; 93 private final float[] mPattern; 94 95 /** 96 * Create an instance that iterates through the given pattern starting at the given offset. 97 */ 98 PatternIterator(final float[] pattern, int offset) { 99 mPattern = pattern; 100 while (offset-- > 0) { 101 next(); 102 } 103 } 104 105 /** 106 * Determine whether to draw the current pixel and move on to the next. 107 */ 108 boolean next() { 109 int oldPatternOffset = mPatternOffset; 110 mLength += 1; 111 if (mLength == mPattern[mPatternOffset]) { 112 mLength = 0; 113 mPatternOffset += 1; 114 mPatternOffset %= mPattern.length; 115 } 116 // even offsets are 'on' 117 return (oldPatternOffset & 1) == 0; 118 } 119 } 120 } 121