1 /* 2 * Copyright (C) 2008 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.cts.util; 18 19 import org.xmlpull.v1.XmlPullParser; 20 import org.xmlpull.v1.XmlPullParserException; 21 22 import android.content.Context; 23 import android.content.res.Resources; 24 import android.graphics.Bitmap; 25 import android.graphics.BitmapFactory; 26 27 import java.io.IOException; 28 29 import junit.framework.Assert; 30 31 /** 32 * The useful methods for widget test. 33 */ 34 public class WidgetTestUtils { 35 /** 36 * Assert that two bitmaps are equal. 37 * 38 * @param Bitmap b1 the first bitmap which needs to compare. 39 * @param Bitmap b2 the second bitmap which needs to compare. 40 */ assertEquals(Bitmap b1, Bitmap b2)41 public static void assertEquals(Bitmap b1, Bitmap b2) { 42 if (b1 == b2) { 43 return; 44 } 45 46 if (b1 == null || b2 == null) { 47 Assert.fail("the bitmaps are not equal"); 48 } 49 50 // b1 and b2 are all not null. 51 if (b1.getWidth() != b2.getWidth() || b1.getHeight() != b2.getHeight() 52 || b1.getConfig() != b2.getConfig()) { 53 Assert.fail("the bitmaps are not equal"); 54 } 55 56 int w = b1.getWidth(); 57 int h = b1.getHeight(); 58 int s = w * h; 59 int[] pixels1 = new int[s]; 60 int[] pixels2 = new int[s]; 61 62 b1.getPixels(pixels1, 0, w, 0, 0, w, h); 63 b2.getPixels(pixels2, 0, w, 0, 0, w, h); 64 65 for (int i = 0; i < s; i++) { 66 if (pixels1[i] != pixels2[i]) { 67 Assert.fail("the bitmaps are not equal"); 68 } 69 } 70 } 71 72 /** 73 * Find beginning of the special element. 74 * @param parser XmlPullParser will be parsed. 75 * @param firstElementName the target element name. 76 * 77 * @throws XmlPullParserException if XML Pull Parser related faults occur. 78 * @throws IOException if I/O-related error occur when parsing. 79 */ beginDocument(XmlPullParser parser, String firstElementName)80 public static final void beginDocument(XmlPullParser parser, String firstElementName) 81 throws XmlPullParserException, IOException { 82 Assert.assertNotNull(parser); 83 Assert.assertNotNull(firstElementName); 84 85 int type; 86 while ((type = parser.next()) != XmlPullParser.START_TAG 87 && type != XmlPullParser.END_DOCUMENT) { 88 ; 89 } 90 91 if (!parser.getName().equals(firstElementName)) { 92 throw new XmlPullParserException("Unexpected start tag: found " + parser.getName() 93 + ", expected " + firstElementName); 94 } 95 } 96 97 /** 98 * Compare the expected pixels with actual, scaling for the target context density 99 * 100 * @throws AssertionFailedError 101 */ assertScaledPixels(int expected, int actual, Context context)102 public static void assertScaledPixels(int expected, int actual, Context context) { 103 Assert.assertEquals(expected * context.getResources().getDisplayMetrics().density, 104 actual, 3); 105 } 106 107 /** Converts dips into pixels using the {@link Context}'s density. */ convertDipToPixels(Context context, int dip)108 public static int convertDipToPixels(Context context, int dip) { 109 float density = context.getResources().getDisplayMetrics().density; 110 return Math.round(density * dip); 111 } 112 113 /** 114 * Retrieve a bitmap that can be used for comparison on any density 115 * @param resources 116 * @return the {@link Bitmap} or <code>null</code> 117 */ getUnscaledBitmap(Resources resources, int resId)118 public static Bitmap getUnscaledBitmap(Resources resources, int resId) { 119 BitmapFactory.Options options = new BitmapFactory.Options(); 120 options.inScaled = false; 121 return BitmapFactory.decodeResource(resources, resId, options); 122 } 123 124 /** 125 * Retrieve a dithered bitmap that can be used for comparison on any density 126 * @param resources 127 * @param config the preferred config for the returning bitmap 128 * @return the {@link Bitmap} or <code>null</code> 129 */ getUnscaledAndDitheredBitmap(Resources resources, int resId, Bitmap.Config config)130 public static Bitmap getUnscaledAndDitheredBitmap(Resources resources, 131 int resId, Bitmap.Config config) { 132 BitmapFactory.Options options = new BitmapFactory.Options(); 133 options.inDither = true; 134 options.inScaled = false; 135 options.inPreferredConfig = config; 136 return BitmapFactory.decodeResource(resources, resId, options); 137 } 138 } 139