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