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