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