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