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.support.v4.util.Pair;
21import android.view.View;
22import android.view.ViewParent;
23import junit.framework.Assert;
24
25import android.graphics.Bitmap;
26import android.graphics.Canvas;
27import android.graphics.Color;
28import android.graphics.drawable.Drawable;
29import android.os.SystemClock;
30import android.support.annotation.ColorInt;
31import android.support.annotation.NonNull;
32
33import java.util.ArrayList;
34import java.util.List;
35
36public 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     */
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     */
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     */
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                int sourceAlpha = Color.alpha(rowPixels[column]);
152                int sourceRed = Color.red(rowPixels[column]);
153                int sourceGreen = Color.green(rowPixels[column]);
154                int sourceBlue = Color.blue(rowPixels[column]);
155
156                int expectedAlpha = Color.alpha(color);
157                int expectedRed = Color.red(color);
158                int expectedGreen = Color.green(color);
159                int expectedBlue = Color.blue(color);
160
161                int varianceAlpha = Math.abs(sourceAlpha - expectedAlpha);
162                int varianceRed = Math.abs(sourceRed - expectedRed);
163                int varianceGreen = Math.abs(sourceGreen - expectedGreen);
164                int varianceBlue = Math.abs(sourceBlue - expectedBlue);
165
166                boolean isColorMatch = (varianceAlpha <= allowedComponentVariance)
167                        && (varianceRed <= allowedComponentVariance)
168                        && (varianceGreen <= allowedComponentVariance)
169                        && (varianceBlue <= allowedComponentVariance);
170
171                if (!isColorMatch) {
172                    String mismatchDescription = failMessagePrefix
173                            + ": expected all drawable colors to be ["
174                            + expectedAlpha + "," + expectedRed + ","
175                            + expectedGreen + "," + expectedBlue
176                            + "] but at position (" + row + "," + column + ") out of ("
177                            + bitmapWidth + "," + bitmapHeight + ") found ["
178                            + sourceAlpha + "," + sourceRed + ","
179                            + sourceGreen + "," + sourceBlue + "]";
180                    if (throwExceptionIfFails) {
181                        throw new RuntimeException(mismatchDescription);
182                    } else {
183                        Assert.fail(mismatchDescription);
184                    }
185                }
186            }
187        }
188    }
189
190    public static void waitForActivityDestroyed(BaseTestActivity activity) {
191        while (!activity.isDestroyed()) {
192            SystemClock.sleep(30);
193        }
194    }
195}