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 package android.uirendering.cts.util;
17 
18 import static org.junit.Assert.assertTrue;
19 import static org.junit.Assert.fail;
20 
21 import android.content.Context;
22 import android.graphics.Bitmap;
23 import android.uirendering.cts.bitmapcomparers.BitmapComparer;
24 import android.uirendering.cts.bitmapverifiers.BitmapVerifier;
25 import android.uirendering.cts.differencevisualizers.DifferenceVisualizer;
26 import android.uirendering.cts.differencevisualizers.PassFailVisualizer;
27 
28 public class BitmapAsserter {
29     private DifferenceVisualizer mDifferenceVisualizer;
30     private String mClassName;
31 
BitmapAsserter(String className, String name)32     public BitmapAsserter(String className, String name) {
33         mClassName = className;
34         mDifferenceVisualizer = new PassFailVisualizer();
35 
36         // Create a location for the files to be held, if it doesn't exist already
37         BitmapDumper.createSubDirectory(mClassName);
38 
39         // If we have a test currently, let's remove the older files if they exist
40         if (name != null) {
41             BitmapDumper.deleteFileInClassFolder(mClassName, name);
42         }
43     }
44 
45     /**
46      * Compares the two bitmaps saved using the given test. If they fail, the files are saved using
47      * the test name.
48      */
assertBitmapsAreSimilar(Bitmap bitmap1, Bitmap bitmap2, BitmapComparer comparer, String testName, String debugMessage)49     public void assertBitmapsAreSimilar(Bitmap bitmap1, Bitmap bitmap2, BitmapComparer comparer,
50             String testName, String debugMessage) {
51         boolean success;
52         int width = bitmap1.getWidth();
53         int height = bitmap1.getHeight();
54 
55         if (width != bitmap2.getWidth() || height != bitmap2.getHeight()) {
56             fail("Can't compare bitmaps of different sizes");
57         }
58 
59         int[] pixels1 = new int[width * height];
60         int[] pixels2 = new int[width * height];
61         bitmap1.getPixels(pixels1, 0, width, 0, 0, width, height);
62         bitmap2.getPixels(pixels2, 0, width, 0, 0, width, height);
63         success = comparer.verifySame(pixels1, pixels2, 0, width, width, height);
64 
65         if (!success) {
66             BitmapDumper.dumpBitmaps(bitmap1, bitmap2, testName, mClassName, mDifferenceVisualizer);
67         }
68 
69         assertTrue(debugMessage, success);
70     }
71 
72     /**
73      * Tests to see if a bitmap passes a verifier's test. If it doesn't the bitmap is saved to the
74      * sdcard.
75      */
assertBitmapIsVerified(Bitmap bitmap, BitmapVerifier bitmapVerifier, String testName, String debugMessage)76     public void assertBitmapIsVerified(Bitmap bitmap, BitmapVerifier bitmapVerifier,
77             String testName, String debugMessage) {
78         int width = bitmap.getWidth();
79         int height = bitmap.getHeight();
80         boolean success = bitmapVerifier.verify(bitmap);
81         if (!success) {
82             Bitmap croppedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height);
83             BitmapDumper.dumpBitmap(croppedBitmap, testName, mClassName);
84             BitmapDumper.dumpBitmap(bitmapVerifier.getDifferenceBitmap(), testName + "_verifier",
85                     mClassName);
86         }
87         assertTrue(debugMessage, success);
88     }
89 
90 
91 }
92