TestUtils.java revision 3ac77bf186f87ecad4bf0063b2f6c4384efbd56a
1/* 2 * Copyright (C) 2015 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 18package android.support.v4.testutils; 19 20import java.lang.IllegalArgumentException; 21import java.lang.RuntimeException; 22 23import android.graphics.Bitmap; 24import android.graphics.Canvas; 25import android.graphics.Color; 26import android.graphics.Rect; 27import android.graphics.drawable.Drawable; 28import android.support.annotation.ColorInt; 29import android.support.annotation.NonNull; 30import android.util.DisplayMetrics; 31import android.util.TypedValue; 32 33import junit.framework.Assert; 34 35public class TestUtils { 36 /** 37 * Converts the specified value from dips to pixels for use as view size. 38 */ 39 public static int convertSizeDipsToPixels(DisplayMetrics displayMetrics, int dipValue) { 40 // Round to the nearest int value. This follows the logic in 41 // TypedValue.complexToDimensionPixelSize 42 final int res = (int) (dipValue * displayMetrics.density + 0.5f); 43 if (res != 0) { 44 return res; 45 } 46 if (dipValue == 0) { 47 return 0; 48 } 49 if (dipValue > 0) { 50 return 1; 51 } 52 return -1; 53 } 54 55 /** 56 * Converts the specified value from dips to pixels for use as view offset. 57 */ 58 public static int convertOffsetDipsToPixels(DisplayMetrics displayMetrics, int dipValue) { 59 // Round to the nearest int value. 60 return (int) (dipValue * displayMetrics.density); 61 } 62 63 64 /** 65 * Checks whether all the pixels in the specified drawable are of the same specified color. 66 * If the passed <code>Drawable</code> does not have positive intrinsic dimensions set, this 67 * method will throw an <code>IllegalArgumentException</code>. If there is a color mismatch, 68 * this method will call <code>Assert.fail</code> with detailed description of the mismatch. 69 */ 70 public static void assertAllPixelsOfColor(String failMessagePrefix, @NonNull Drawable drawable, 71 @ColorInt int color) { 72 int drawableWidth = drawable.getIntrinsicWidth(); 73 int drawableHeight = drawable.getIntrinsicHeight(); 74 75 if ((drawableWidth <= 0) || (drawableHeight <= 0)) { 76 throw new IllegalArgumentException("Drawable must be configured to have non-zero size"); 77 } 78 79 assertAllPixelsOfColor(failMessagePrefix, drawable, drawableWidth, drawableHeight, color, 80 false); 81 } 82 83 /** 84 * Checks whether all the pixels in the specified drawable are of the same specified color. 85 * 86 * In case there is a color mismatch, the behavior of this method depends on the 87 * <code>throwExceptionIfFails</code> parameter. If it is <code>true</code>, this method will 88 * throw an <code>Exception</code> describing the mismatch. Otherwise this method will call 89 * <code>Assert.fail</code> with detailed description of the mismatch. 90 */ 91 public static void assertAllPixelsOfColor(String failMessagePrefix, @NonNull Drawable drawable, 92 int drawableWidth, int drawableHeight, @ColorInt int color, 93 boolean throwExceptionIfFails) { 94 // Create a bitmap 95 Bitmap bitmap = Bitmap.createBitmap(drawableWidth, drawableHeight, Bitmap.Config.ARGB_8888); 96 // Create a canvas that wraps the bitmap 97 Canvas canvas = new Canvas(bitmap); 98 // Configure the drawable to have bounds that match its intrinsic size 99 drawable.setBounds(0, 0, drawableWidth, drawableHeight); 100 // And ask the drawable to draw itself to the canvas / bitmap 101 drawable.draw(canvas); 102 103 try { 104 int[] rowPixels = new int[drawableWidth]; 105 for (int row = 0; row < drawableHeight; row++) { 106 bitmap.getPixels(rowPixels, 0, drawableWidth, 0, row, drawableWidth, 1); 107 for (int column = 0; column < drawableWidth; column++) { 108 if (rowPixels[column] != color) { 109 String mismatchDescription = failMessagePrefix 110 + ": expected all drawable colors to be [" 111 + Color.red(color) + "," + Color.green(color) + "," 112 + Color.blue(color) 113 + "] but at position (" + row + "," + column + ") found [" 114 + Color.red(rowPixels[column]) + "," 115 + Color.green(rowPixels[column]) + "," 116 + Color.blue(rowPixels[column]) + "]"; 117 if (throwExceptionIfFails) { 118 throw new RuntimeException(mismatchDescription); 119 } else { 120 Assert.fail(mismatchDescription); 121 } 122 } 123 } 124 } 125 } finally { 126 bitmap.recycle(); 127 } 128 } 129 130 /** 131 * Checks whether the specified rectangle matches the specified left / top / right / 132 * bottom bounds. 133 */ 134 public static void assertRectangleBounds(String failMessagePrefix, @NonNull Rect rectangle, 135 int left, int top, int right, int bottom) { 136 Assert.assertEquals(failMessagePrefix + " left", rectangle.left, left); 137 Assert.assertEquals(failMessagePrefix + " top", rectangle.top, top); 138 Assert.assertEquals(failMessagePrefix + " right", rectangle.right, right); 139 Assert.assertEquals(failMessagePrefix + " bottom", rectangle.bottom, bottom); 140 } 141}