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.BlurMaskFilter; 23 import android.graphics.BlurMaskFilter.Blur; 24 import android.graphics.Canvas; 25 import android.graphics.Color; 26 import android.graphics.Paint; 27 28 import androidx.test.filters.SmallTest; 29 import androidx.test.runner.AndroidJUnit4; 30 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 34 @SmallTest 35 @RunWith(AndroidJUnit4.class) 36 public class BlurMaskFilterTest { 37 private static final int OFFSET = 10; 38 private static final int RADIUS = 5; 39 private static final int CHECK_RADIUS = 8; 40 private static final int BITMAP_WIDTH = 100; 41 private static final int BITMAP_HEIGHT = 100; 42 private static final int CENTER = BITMAP_HEIGHT / 2; 43 44 @Test 45 public void testBlurMaskFilter() { 46 BlurMaskFilter filter = new BlurMaskFilter(RADIUS, Blur.NORMAL); 47 Paint paint = new Paint(); 48 paint.setMaskFilter(filter); 49 paint.setColor(Color.RED); 50 Bitmap b = Bitmap.createBitmap(BITMAP_WIDTH, BITMAP_HEIGHT, Config.ARGB_8888); 51 b.eraseColor(Color.TRANSPARENT); 52 Canvas canvas = new Canvas(b); 53 canvas.drawRect(CENTER - OFFSET, CENTER - OFFSET, CENTER + OFFSET, CENTER + OFFSET, paint); 54 for (int x = 0; x < CENTER; x++) { 55 for (int y = 0; y < CENTER; y++) { 56 if (x < CENTER - OFFSET - CHECK_RADIUS || y < CENTER - OFFSET - CHECK_RADIUS) { 57 // check that color didn't bleed (much) beyond radius 58 verifyQuadrants(Color.TRANSPARENT, b, x, y, 5); 59 } else if (x > CENTER - OFFSET + RADIUS && y > CENTER - OFFSET + RADIUS) { 60 // check that color didn't wash out (much) in the center 61 verifyQuadrants(Color.RED, b, x, y, 8); 62 } else if (x > CENTER - OFFSET - RADIUS && y > CENTER - OFFSET - RADIUS) { 63 // check blur zone, color should remain, alpha varies 64 verifyQuadrants(Color.RED, b, x, y, 255); 65 } 66 } 67 } 68 } 69 70 private void verifyQuadrants(int color, Bitmap bitmap, int x, int y, int alphaTolerance) { 71 int right = bitmap.getWidth() - 1; 72 int bottom = bitmap.getHeight() - 1; 73 74 verifyColor(color, bitmap.getPixel(x, y), alphaTolerance); 75 verifyColor(color, bitmap.getPixel(right - x, y), alphaTolerance); 76 verifyColor(color, bitmap.getPixel(x, bottom - y), alphaTolerance); 77 verifyColor(color, bitmap.getPixel(right - x, bottom - y), alphaTolerance); 78 } 79 80 private void verifyColor(int expected, int actual, int alphaTolerance) { 81 assertEquals(Color.red(expected), Color.red(actual)); 82 assertEquals(Color.green(expected), Color.green(actual)); 83 assertEquals(Color.blue(expected), Color.blue(actual)); 84 assertEquals(Color.alpha(expected), Color.alpha(actual), alphaTolerance); 85 } 86 } 87