TestUtils.java revision ee9519c17254b5e992164ff278173c4b2c7c5fce
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.v7.testutils;
19
20import android.graphics.Bitmap;
21import android.graphics.Canvas;
22import android.graphics.Color;
23import android.graphics.drawable.Drawable;
24import android.support.annotation.ColorInt;
25import android.support.annotation.NonNull;
26import junit.framework.Assert;
27
28public class TestUtils {
29    /**
30     * Checks whether all the pixels in the specified drawable are of the same specified color.
31     *
32     * In case there is a color mismatch, the behavior of this method depends on the
33     * <code>throwExceptionIfFails</code> parameter. If it is <code>true</code>, this method will
34     * throw an <code>Exception</code> describing the mismatch. Otherwise this method will call
35     * <code>Assert.fail</code> with detailed description of the mismatch.
36     */
37    public static void assertAllPixelsOfColor(String failMessagePrefix, @NonNull Drawable drawable,
38            int drawableWidth, int drawableHeight, boolean callSetBounds, @ColorInt int color,
39            boolean throwExceptionIfFails) {
40        // Create a bitmap
41        Bitmap bitmap = Bitmap.createBitmap(drawableWidth, drawableHeight, Bitmap.Config.ARGB_8888);
42        // Create a canvas that wraps the bitmap
43        Canvas canvas = new Canvas(bitmap);
44        if (callSetBounds) {
45            // Configure the drawable to have bounds that match the passed size
46            drawable.setBounds(0, 0, drawableWidth, drawableHeight);
47        }
48        // And ask the drawable to draw itself to the canvas / bitmap
49        drawable.draw(canvas);
50
51        try {
52            int[] rowPixels = new int[drawableWidth];
53            for (int row = 0; row < drawableHeight; row++) {
54                bitmap.getPixels(rowPixels, 0, drawableWidth, 0, row, drawableWidth, 1);
55                for (int column = 0; column < drawableWidth; column++) {
56                    if (rowPixels[column] != color) {
57                        String mismatchDescription = failMessagePrefix
58                                + ": expected all drawable colors to be ["
59                                + Color.red(color) + "," + Color.green(color) + ","
60                                + Color.blue(color)
61                                + "] but at position (" + row + "," + column + ") found ["
62                                + Color.red(rowPixels[column]) + ","
63                                + Color.green(rowPixels[column]) + ","
64                                + Color.blue(rowPixels[column]) + "]";
65                        if (throwExceptionIfFails) {
66                            throw new RuntimeException(mismatchDescription);
67                        } else {
68                            Assert.fail(mismatchDescription);
69                        }
70                    }
71                }
72            }
73        } finally {
74            bitmap.recycle();
75        }
76    }
77}