1 /* 2 * Copyright (C) 2014 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.bitmapverifiers; 17 18 import android.graphics.Bitmap; 19 import android.graphics.Point; 20 import android.uirendering.cts.testinfrastructure.ActivityTestBase; 21 import android.uirendering.cts.util.CompareUtils; 22 import android.util.Log; 23 24 import java.util.Arrays; 25 26 /** 27 * This class will test specific points, and ensure that they match up perfectly with the input colors 28 */ 29 public class SamplePointVerifier extends BitmapVerifier { 30 private static final String TAG = "SamplePoint"; 31 public static final int DEFAULT_TOLERANCE = 20; 32 private final Point[] mTestPoints; 33 private final int[] mExpectedColors; 34 protected final int mTolerance; 35 36 public SamplePointVerifier(Point[] testPoints, int[] expectedColors) { 37 this(testPoints, expectedColors, DEFAULT_TOLERANCE); 38 } 39 40 public SamplePointVerifier(Point[] testPoints, int[] expectedColors, int tolerance) { 41 mTestPoints = testPoints; 42 mExpectedColors = expectedColors; 43 mTolerance = tolerance; 44 } 45 46 @Override 47 public boolean verify(int[] bitmap, int offset, int stride, int width, int height) { 48 boolean success = true; 49 int[] differenceMap = new int[bitmap.length]; 50 Arrays.fill(differenceMap, PASS_COLOR); 51 for (int i = 0 ; i < mTestPoints.length ; i++) { 52 int x = mTestPoints[i].x; 53 int y = mTestPoints[i].y; 54 int index = indexFromXAndY(x, y, stride, offset); 55 if (!verifyPixel(bitmap[index], mExpectedColors[i])) { 56 Log.d(TAG, "Expected : " + Integer.toHexString(mExpectedColors[i]) + 57 " at position x = " + x + " y = " + y + " , tested color : " + 58 Integer.toHexString(bitmap[index])); 59 differenceMap[index] = FAIL_COLOR; 60 success = false; 61 } else { 62 differenceMap[index] = PASS_COLOR; 63 } 64 } 65 if (!success) { 66 mDifferenceBitmap = Bitmap.createBitmap(ActivityTestBase.TEST_WIDTH, 67 ActivityTestBase.TEST_HEIGHT, Bitmap.Config.ARGB_8888); 68 mDifferenceBitmap.setPixels(differenceMap, offset, stride, 0, 0, 69 ActivityTestBase.TEST_WIDTH, ActivityTestBase.TEST_HEIGHT); 70 } 71 return success; 72 } 73 74 protected boolean verifyPixel(int color, int expectedColor) { 75 return CompareUtils.verifyPixelWithThreshold(color, expectedColor, mTolerance); 76 } 77 78 public interface VerifyPixelColor { 79 boolean verifyPixel(int color); 80 } 81 82 public static SamplePointVerifier create(Point[] testPoints, int[] expectedColors, 83 int tolerance, VerifyPixelColor verifier) { 84 return new SamplePointVerifier(testPoints, expectedColors, tolerance) { 85 @Override 86 protected boolean verifyPixel(int color, int expectedColor) { 87 return super.verifyPixel(color, expectedColor) && verifier.verifyPixel(color); 88 } 89 }; 90 } 91 } 92