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 android.graphics.Bitmap; 21import android.graphics.Canvas; 22import android.graphics.Color; 23import android.graphics.Rect; 24import android.graphics.drawable.Drawable; 25import android.support.annotation.ColorInt; 26import android.support.annotation.NonNull; 27import junit.framework.Assert; 28 29public class TestUtils { 30 /** 31 * Checks whether all the pixels in the specified drawable are of the same specified color. 32 * If the passed <code>Drawable</code> does not have positive intrinsic dimensions set, this 33 * method will throw an <code>IllegalArgumentException</code>. If there is a color mismatch, 34 * this method will call <code>Assert.fail</code> with detailed description of the mismatch. 35 */ 36 public static void assertAllPixelsOfColor(String failMessagePrefix, @NonNull Drawable drawable, 37 @ColorInt int color) { 38 int drawableWidth = drawable.getIntrinsicWidth(); 39 int drawableHeight = drawable.getIntrinsicHeight(); 40 41 if ((drawableWidth <= 0) || (drawableHeight <= 0)) { 42 throw new IllegalArgumentException("Drawable must be configured to have non-zero size"); 43 } 44 45 assertAllPixelsOfColor(failMessagePrefix, drawable, drawableWidth, drawableHeight, color, 46 false); 47 } 48 49 /** 50 * Checks whether all the pixels in the specified drawable are of the same specified color. 51 * 52 * In case there is a color mismatch, the behavior of this method depends on the 53 * <code>throwExceptionIfFails</code> parameter. If it is <code>true</code>, this method will 54 * throw an <code>Exception</code> describing the mismatch. Otherwise this method will call 55 * <code>Assert.fail</code> with detailed description of the mismatch. 56 */ 57 public static void assertAllPixelsOfColor(String failMessagePrefix, @NonNull Drawable drawable, 58 int drawableWidth, int drawableHeight, @ColorInt int color, 59 boolean throwExceptionIfFails) { 60 // Create a bitmap 61 Bitmap bitmap = Bitmap.createBitmap(drawableWidth, drawableHeight, Bitmap.Config.ARGB_8888); 62 // Create a canvas that wraps the bitmap 63 Canvas canvas = new Canvas(bitmap); 64 // Configure the drawable to have bounds that match its intrinsic size 65 drawable.setBounds(0, 0, drawableWidth, drawableHeight); 66 // And ask the drawable to draw itself to the canvas / bitmap 67 drawable.draw(canvas); 68 69 try { 70 int[] rowPixels = new int[drawableWidth]; 71 for (int row = 0; row < drawableHeight; row++) { 72 bitmap.getPixels(rowPixels, 0, drawableWidth, 0, row, drawableWidth, 1); 73 for (int column = 0; column < drawableWidth; column++) { 74 if (rowPixels[column] != color) { 75 String mismatchDescription = failMessagePrefix 76 + ": expected all drawable colors to be [" 77 + Color.red(color) + "," + Color.green(color) + "," 78 + Color.blue(color) 79 + "] but at position (" + row + "," + column + ") found [" 80 + Color.red(rowPixels[column]) + "," 81 + Color.green(rowPixels[column]) + "," 82 + Color.blue(rowPixels[column]) + "]"; 83 if (throwExceptionIfFails) { 84 throw new RuntimeException(mismatchDescription); 85 } else { 86 Assert.fail(mismatchDescription); 87 } 88 } 89 } 90 } 91 } finally { 92 bitmap.recycle(); 93 } 94 } 95 96 /** 97 * Checks whether the specified rectangle matches the specified left / top / right / 98 * bottom bounds. 99 */ 100 public static void assertRectangleBounds(String failMessagePrefix, @NonNull Rect rectangle, 101 int left, int top, int right, int bottom) { 102 Assert.assertEquals(failMessagePrefix + " left", rectangle.left, left); 103 Assert.assertEquals(failMessagePrefix + " top", rectangle.top, top); 104 Assert.assertEquals(failMessagePrefix + " right", rectangle.right, right); 105 Assert.assertEquals(failMessagePrefix + " bottom", rectangle.bottom, bottom); 106 } 107}