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 package android.support.v7.testutils; 18 19 import android.content.Context; 20 import android.graphics.Bitmap; 21 import android.graphics.Canvas; 22 import android.graphics.Color; 23 import android.graphics.drawable.Drawable; 24 import android.os.SystemClock; 25 import android.support.annotation.ColorInt; 26 import android.support.annotation.NonNull; 27 import android.support.v4.util.Pair; 28 import android.support.v7.widget.TintTypedArray; 29 import android.view.View; 30 import android.view.ViewParent; 31 import junit.framework.Assert; 32 33 import java.util.ArrayList; 34 import java.util.List; 35 36 public class TestUtils { 37 /** 38 * This method takes a view and returns a single bitmap that is the layered combination 39 * of background drawables of this view and all its ancestors. It can be used to abstract 40 * away the specific implementation of a view hierarchy that is not exposed via class APIs 41 * or a view hierarchy that depends on the platform version. Instead of hard-coded lookups 42 * of particular inner implementations of such a view hierarchy that can break during 43 * refactoring or on newer platform versions, calling this API returns a "combined" background 44 * of the view. 45 * 46 * For example, it is useful to get the combined background of a popup / dropdown without 47 * delving into the inner implementation details of how that popup is implemented on a 48 * particular platform version. 49 */ getCombinedBackgroundBitmap(View view)50 public static Bitmap getCombinedBackgroundBitmap(View view) { 51 final int bitmapWidth = view.getWidth(); 52 final int bitmapHeight = view.getHeight(); 53 54 // Create a bitmap 55 final Bitmap bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, 56 Bitmap.Config.ARGB_8888); 57 // Create a canvas that wraps the bitmap 58 final Canvas canvas = new Canvas(bitmap); 59 60 // As the draw pass starts at the top of view hierarchy, our first step is to traverse 61 // the ancestor hierarchy of our view and collect a list of all ancestors with non-null 62 // and visible backgrounds. At each step we're keeping track of the combined offsets 63 // so that we can properly combine all of the visuals together in the next pass. 64 List<View> ancestorsWithBackgrounds = new ArrayList<>(); 65 List<Pair<Integer, Integer>> ancestorOffsets = new ArrayList<>(); 66 int offsetX = 0; 67 int offsetY = 0; 68 while (true) { 69 final Drawable backgroundDrawable = view.getBackground(); 70 if ((backgroundDrawable != null) && backgroundDrawable.isVisible()) { 71 ancestorsWithBackgrounds.add(view); 72 ancestorOffsets.add(Pair.create(offsetX, offsetY)); 73 } 74 // Go to the parent 75 ViewParent parent = view.getParent(); 76 if (!(parent instanceof View)) { 77 // We're done traversing the ancestor chain 78 break; 79 } 80 81 // Update the offsets based on the location of current view in its parent's bounds 82 offsetX += view.getLeft(); 83 offsetY += view.getTop(); 84 85 view = (View) parent; 86 } 87 88 // Now we're going to iterate over the collected ancestors in reverse order (starting from 89 // the topmost ancestor) and draw their backgrounds into our combined bitmap. At each step 90 // we are respecting the offsets of our original view in the coordinate system of the 91 // currently drawn ancestor. 92 final int layerCount = ancestorsWithBackgrounds.size(); 93 for (int i = layerCount - 1; i >= 0; i--) { 94 View ancestor = ancestorsWithBackgrounds.get(i); 95 Pair<Integer, Integer> offsets = ancestorOffsets.get(i); 96 97 canvas.translate(offsets.first, offsets.second); 98 ancestor.getBackground().draw(canvas); 99 canvas.translate(-offsets.first, -offsets.second); 100 } 101 102 return bitmap; 103 } 104 105 /** 106 * Checks whether all the pixels in the specified drawable are of the same specified color. 107 * 108 * In case there is a color mismatch, the behavior of this method depends on the 109 * <code>throwExceptionIfFails</code> parameter. If it is <code>true</code>, this method will 110 * throw an <code>Exception</code> describing the mismatch. Otherwise this method will call 111 * <code>Assert.fail</code> with detailed description of the mismatch. 112 */ assertAllPixelsOfColor(String failMessagePrefix, @NonNull Drawable drawable, int drawableWidth, int drawableHeight, boolean callSetBounds, @ColorInt int color, int allowedComponentVariance, boolean throwExceptionIfFails)113 public static void assertAllPixelsOfColor(String failMessagePrefix, @NonNull Drawable drawable, 114 int drawableWidth, int drawableHeight, boolean callSetBounds, @ColorInt int color, 115 int allowedComponentVariance, boolean throwExceptionIfFails) { 116 // Create a bitmap 117 Bitmap bitmap = Bitmap.createBitmap(drawableWidth, drawableHeight, 118 Bitmap.Config.ARGB_8888); 119 // Create a canvas that wraps the bitmap 120 Canvas canvas = new Canvas(bitmap); 121 if (callSetBounds) { 122 // Configure the drawable to have bounds that match the passed size 123 drawable.setBounds(0, 0, drawableWidth, drawableHeight); 124 } 125 // And ask the drawable to draw itself to the canvas / bitmap 126 drawable.draw(canvas); 127 128 try { 129 assertAllPixelsOfColor(failMessagePrefix, bitmap, drawableWidth, drawableHeight, color, 130 allowedComponentVariance, throwExceptionIfFails); 131 } finally { 132 bitmap.recycle(); 133 } 134 } 135 136 /** 137 * Checks whether all the pixels in the specified bitmap are of the same specified color. 138 * 139 * In case there is a color mismatch, the behavior of this method depends on the 140 * <code>throwExceptionIfFails</code> parameter. If it is <code>true</code>, this method will 141 * throw an <code>Exception</code> describing the mismatch. Otherwise this method will call 142 * <code>Assert.fail</code> with detailed description of the mismatch. 143 */ assertAllPixelsOfColor(String failMessagePrefix, @NonNull Bitmap bitmap, int bitmapWidth, int bitmapHeight, @ColorInt int color, int allowedComponentVariance, boolean throwExceptionIfFails)144 public static void assertAllPixelsOfColor(String failMessagePrefix, @NonNull Bitmap bitmap, 145 int bitmapWidth, int bitmapHeight, @ColorInt int color, 146 int allowedComponentVariance, boolean throwExceptionIfFails) { 147 int[] rowPixels = new int[bitmapWidth]; 148 for (int row = 0; row < bitmapHeight; row++) { 149 bitmap.getPixels(rowPixels, 0, bitmapWidth, 0, row, bitmapWidth, 1); 150 for (int column = 0; column < bitmapWidth; column++) { 151 @ColorInt int colorAtCurrPixel = rowPixels[column]; 152 if (!areColorsTheSameWithTolerance(color, colorAtCurrPixel, 153 allowedComponentVariance)) { 154 String mismatchDescription = failMessagePrefix 155 + ": expected all drawable colors to be " 156 + formatColorToHex(color) 157 + " but at position (" + row + "," + column + ") out of (" 158 + bitmapWidth + "," + bitmapHeight + ") found " 159 + formatColorToHex(colorAtCurrPixel); 160 if (throwExceptionIfFails) { 161 throw new RuntimeException(mismatchDescription); 162 } else { 163 Assert.fail(mismatchDescription); 164 } 165 } 166 } 167 } 168 } 169 170 /** 171 * Checks whether the center pixel in the specified bitmap is of the same specified color. 172 * 173 * In case there is a color mismatch, the behavior of this method depends on the 174 * <code>throwExceptionIfFails</code> parameter. If it is <code>true</code>, this method will 175 * throw an <code>Exception</code> describing the mismatch. Otherwise this method will call 176 * <code>Assert.fail</code> with detailed description of the mismatch. 177 */ assertCenterPixelOfColor(String failMessagePrefix, @NonNull Bitmap bitmap, @ColorInt int color, int allowedComponentVariance, boolean throwExceptionIfFails)178 public static void assertCenterPixelOfColor(String failMessagePrefix, @NonNull Bitmap bitmap, 179 @ColorInt int color, 180 int allowedComponentVariance, boolean throwExceptionIfFails) { 181 final int centerX = bitmap.getWidth() / 2; 182 final int centerY = bitmap.getHeight() / 2; 183 final @ColorInt int colorAtCenterPixel = bitmap.getPixel(centerX, centerY); 184 if (!areColorsTheSameWithTolerance(color, colorAtCenterPixel, 185 allowedComponentVariance)) { 186 String mismatchDescription = failMessagePrefix 187 + ": expected all drawable colors to be " 188 + formatColorToHex(color) 189 + " but at position (" + centerX + "," + centerY + ") out of (" 190 + bitmap.getWidth() + "," + bitmap.getHeight() + ") found" 191 + formatColorToHex(colorAtCenterPixel); 192 if (throwExceptionIfFails) { 193 throw new RuntimeException(mismatchDescription); 194 } else { 195 Assert.fail(mismatchDescription); 196 } 197 } 198 } 199 200 /** 201 * Formats the passed integer-packed color into the #AARRGGBB format. 202 */ formatColorToHex(@olorInt int color)203 private static String formatColorToHex(@ColorInt int color) { 204 return String.format("#%08X", (0xFFFFFFFF & color)); 205 } 206 207 /** 208 * Compares two integer-packed colors to be equal, each component within the specified 209 * allowed variance. Returns <code>true</code> if the two colors are sufficiently equal 210 * and <code>false</code> otherwise. 211 */ areColorsTheSameWithTolerance(@olorInt int expectedColor, @ColorInt int actualColor, int allowedComponentVariance)212 private static boolean areColorsTheSameWithTolerance(@ColorInt int expectedColor, 213 @ColorInt int actualColor, int allowedComponentVariance) { 214 int sourceAlpha = Color.alpha(actualColor); 215 int sourceRed = Color.red(actualColor); 216 int sourceGreen = Color.green(actualColor); 217 int sourceBlue = Color.blue(actualColor); 218 219 int expectedAlpha = Color.alpha(expectedColor); 220 int expectedRed = Color.red(expectedColor); 221 int expectedGreen = Color.green(expectedColor); 222 int expectedBlue = Color.blue(expectedColor); 223 224 int varianceAlpha = Math.abs(sourceAlpha - expectedAlpha); 225 int varianceRed = Math.abs(sourceRed - expectedRed); 226 int varianceGreen = Math.abs(sourceGreen - expectedGreen); 227 int varianceBlue = Math.abs(sourceBlue - expectedBlue); 228 229 boolean isColorMatch = (varianceAlpha <= allowedComponentVariance) 230 && (varianceRed <= allowedComponentVariance) 231 && (varianceGreen <= allowedComponentVariance) 232 && (varianceBlue <= allowedComponentVariance); 233 234 return isColorMatch; 235 } 236 waitForActivityDestroyed(BaseTestActivity activity)237 public static void waitForActivityDestroyed(BaseTestActivity activity) { 238 while (!activity.isDestroyed()) { 239 SystemClock.sleep(30); 240 } 241 } 242 getThemeAttrColor(Context context, int attr)243 public static int getThemeAttrColor(Context context, int attr) { 244 final int[] attrs = { attr }; 245 TintTypedArray a = TintTypedArray.obtainStyledAttributes(context, null, attrs); 246 try { 247 return a.getColor(0, 0); 248 } finally { 249 a.recycle(); 250 } 251 } 252 }