1 /*
2  * Copyright (C) 2016 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.view.cts.util;
18 
19 import android.graphics.Bitmap;
20 import android.graphics.Canvas;
21 import android.graphics.Color;
22 import android.graphics.Paint;
23 import android.graphics.Rect;
24 import android.util.Pair;
25 import android.view.View;
26 
27 import junit.framework.Assert;
28 
29 import java.util.List;
30 
31 public class DrawingUtils {
32     /**
33      * Checks the pixels in the specified {@link View}. This methods draws the view into an
34      * offscreen bitmap and checks each pixel to match the expected color. The expected color
35      * of each pixel is either the color associated with the {@link Rect} that contains it,
36      * or the default color in case none of the rectangles cover that pixel.
37      *
38      * In case of a mismatch this method will call {@link Assert#fail(String)}
39      * with detailed description of the mismatch.
40      */
assertAllPixelsOfColor(String failMessagePrefix, View view, int defaultColor, List<Pair<Rect, Integer>> colorRectangles)41     public static void assertAllPixelsOfColor(String failMessagePrefix, View view,
42             int defaultColor, List<Pair<Rect, Integer>> colorRectangles) {
43         final int viewWidth = view.getWidth();
44         final int viewHeight = view.getHeight();
45         // Create a bitmap that matches the size of our view
46         final Bitmap bitmap = Bitmap.createBitmap(viewWidth, viewHeight, Bitmap.Config.ARGB_8888);
47         // Create a canvas that wraps the bitmap
48         final Canvas canvas = new Canvas(bitmap);
49         // And ask the view to draw itself to the canvas / bitmap
50         view.draw(canvas);
51 
52         // Now create a golden bitmap based on the colors passed to us
53         final Bitmap goldenBitmap = Bitmap.createBitmap(viewWidth, viewHeight,
54                 Bitmap.Config.ARGB_8888);
55         // Create a canvas that wraps the bitmap
56         final Canvas goldenCanvas = new Canvas(goldenBitmap);
57         // Fill it with default color
58         final Paint goldenPaint = new Paint();
59         goldenPaint.setStyle(Paint.Style.FILL_AND_STROKE);
60         goldenPaint.setColor(defaultColor);
61         goldenCanvas.drawRect(0, 0, viewWidth, viewHeight, goldenPaint);
62         if (colorRectangles != null) {
63             for (Pair<Rect, Integer> colorRectangle : colorRectangles) {
64                 goldenPaint.setColor(colorRectangle.second);
65                 goldenCanvas.drawRect(colorRectangle.first, goldenPaint);
66             }
67         }
68 
69         // Compare bitmap pixels (row-wise)
70         try {
71             int[] rowPixels = new int[viewWidth];
72             int[] rowGoldenPixels = new int[viewWidth];
73             for (int row = 0; row < viewHeight; row++) {
74                 bitmap.getPixels(rowPixels, 0, viewWidth, 0, row, viewWidth, 1);
75                 goldenBitmap.getPixels(rowGoldenPixels, 0, viewWidth, 0, row, viewWidth, 1);
76                 for (int column = 0; column < viewWidth; column++) {
77                     int expectedPixelColor = rowGoldenPixels[column];
78                     int actualPixelColor = rowPixels[column];
79                     if (rowPixels[column] != expectedPixelColor) {
80                         String mismatchDescription = failMessagePrefix
81                                 + ": expected all drawable colors to be ["
82                                 + Color.alpha(expectedPixelColor) + ","
83                                 + Color.red(expectedPixelColor) + ","
84                                 + Color.green(expectedPixelColor) + ","
85                                 + Color.blue(expectedPixelColor)
86                                 + "] but at position (" + row + "," + column + ") found ["
87                                 + Color.alpha(actualPixelColor) + ","
88                                 + Color.red(actualPixelColor) + ","
89                                 + Color.green(actualPixelColor) + ","
90                                 + Color.blue(actualPixelColor) + "]";
91                         Assert.fail(mismatchDescription);
92                     }
93                 }
94             }
95         } finally {
96             bitmap.recycle();
97             goldenBitmap.recycle();
98         }
99     }
100 }
101