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.PaintFlagsDrawFilter; 25 import android.graphics.Rect; 26 import android.graphics.Bitmap.Config; 27 import android.graphics.Paint.Align; 28 import android.test.AndroidTestCase; 29 30 public class PaintFlagsDrawFilterTest extends AndroidTestCase { 31 32 private static final float TEXT_SIZE = 20; 33 private static final float TEXT_X = 50; 34 private static final float TEXT_Y = 50; 35 private static final String TEXT = "Test"; 36 private static final int BITMAP_WIDTH = 100; 37 private static final int BITMAP_HEIGHT = 100; 38 private float mTextWidth; 39 40 @Override setUp()41 protected void setUp() throws Exception { 42 super.setUp(); 43 } 44 testPaintFlagsDrawFilter()45 public void testPaintFlagsDrawFilter() { 46 47 Bitmap bitmapWithoutFilter = drawText(null); 48 49 PaintFlagsDrawFilter filter = new PaintFlagsDrawFilter(Paint.UNDERLINE_TEXT_FLAG, 0); 50 Bitmap bitmapWithFilter = drawText(filter); 51 52 Bitmap combined = delta(bitmapWithoutFilter, bitmapWithFilter); 53 assertUnderline(combined); 54 } 55 drawText(PaintFlagsDrawFilter filter)56 private Bitmap drawText(PaintFlagsDrawFilter filter) { 57 Paint p = new Paint(Paint.UNDERLINE_TEXT_FLAG); 58 p.setColor(Color.RED); 59 p.setTextSize(TEXT_SIZE); 60 p.setTextAlign(Align.CENTER); 61 mTextWidth = p.measureText(TEXT); 62 Bitmap b = Bitmap.createBitmap(BITMAP_WIDTH, BITMAP_HEIGHT, Config.ARGB_8888); 63 Canvas c = new Canvas(b); 64 c.setDrawFilter(filter); 65 c.drawColor(Color.BLACK); 66 c.drawText(TEXT, TEXT_X, TEXT_Y, p); 67 return b; 68 } 69 delta(Bitmap bitmapWithoutFilter, Bitmap bitmapWithFilter)70 private Bitmap delta(Bitmap bitmapWithoutFilter, Bitmap bitmapWithFilter) { 71 Bitmap combinedBitmap = Bitmap.createBitmap(BITMAP_WIDTH, BITMAP_HEIGHT, Config.ARGB_8888); 72 combinedBitmap.eraseColor(Color.BLACK); 73 int pixelWithoutFilter; 74 int pixelWithFilter; 75 for (int i = 0; i < BITMAP_WIDTH; i++) { 76 for (int j = 0; j < BITMAP_HEIGHT; j++) { 77 pixelWithoutFilter = bitmapWithoutFilter.getPixel(i, j); 78 pixelWithFilter = bitmapWithFilter.getPixel(i, j); 79 if (pixelWithoutFilter != pixelWithFilter) { 80 assertEquals(Color.RED, pixelWithoutFilter); 81 assertEquals(Color.BLACK, pixelWithFilter); 82 combinedBitmap.setPixel(i, j, Color.RED); 83 } 84 } 85 } 86 return combinedBitmap; 87 } 88 assertUnderline(Bitmap bitmap)89 private void assertUnderline(Bitmap bitmap) { 90 // Find smallest rectangle containing all RED pixels 91 Rect rect = new Rect(BITMAP_WIDTH, BITMAP_HEIGHT, 0, 0); 92 for (int y = 0; y < BITMAP_HEIGHT; y++) { 93 for (int x = 0; x < BITMAP_WIDTH; x++) { 94 int pixel = bitmap.getPixel(x, y); 95 if (pixel == Color.RED) { 96 rect.left = Math.min(rect.left, x); 97 rect.right = Math.max(rect.right, x); 98 rect.top = Math.min(rect.top, y); 99 rect.bottom = Math.max(rect.bottom, y); 100 } 101 } 102 } 103 // underline is at least one pixel high 104 assertTrue(rect.top <= rect.bottom); 105 // underline is roughly the same length at the text (5% tolerance) 106 assertEquals(mTextWidth, rect.right - rect.left, mTextWidth * 0.053); 107 // underline is under the text or at least at the bottom of it 108 assertTrue(rect.top >= TEXT_Y); 109 } 110 } 111