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