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.widget.cts; 18 19 import org.xmlpull.v1.XmlPullParser; 20 import org.xmlpull.v1.XmlPullParserException; 21 22 import android.graphics.Bitmap; 23 24 import java.io.IOException; 25 26 import junit.framework.Assert; 27 28 /** 29 * The useful methods for widget test. 30 */ 31 public class WidgetTestUtils { 32 /** 33 * Assert that two bitmaps are equal. 34 * 35 * @param Bitmap b1 the first bitmap which needs to compare. 36 * @param Bitmap b2 the second bitmap which needs to compare. 37 */ assertEquals(Bitmap b1, Bitmap b2)38 public static void assertEquals(Bitmap b1, Bitmap b2) { 39 if (b1 == b2) { 40 return; 41 } 42 43 if (b1 == null || b2 == null) { 44 Assert.fail("the bitmaps are not equal"); 45 } 46 47 // b1 and b2 are all not null. 48 if (b1.getWidth() != b2.getWidth() || b1.getHeight() != b2.getHeight()) { 49 Assert.fail("the bitmaps are not equal"); 50 } 51 52 int w = b1.getWidth(); 53 int h = b1.getHeight(); 54 int s = w * h; 55 int[] pixels1 = new int[s]; 56 int[] pixels2 = new int[s]; 57 58 b1.getPixels(pixels1, 0, w, 0, 0, w, h); 59 b2.getPixels(pixels2, 0, w, 0, 0, w, h); 60 61 for (int i = 0; i < s; i++) { 62 if (pixels1[i] != pixels2[i]) { 63 Assert.fail("the bitmaps are not equal"); 64 } 65 } 66 } 67 68 /** 69 * Find beginning of the special element. 70 * @param parser XmlPullParser will be parsed. 71 * @param firstElementName the target element name. 72 * 73 * @throws XmlPullParserException if XML Pull Parser related faults occur. 74 * @throws IOException if I/O-related error occur when parsing. 75 */ beginDocument(XmlPullParser parser, String firstElementName)76 public static final void beginDocument(XmlPullParser parser, String firstElementName) 77 throws XmlPullParserException, IOException { 78 Assert.assertNotNull(parser); 79 Assert.assertNotNull(firstElementName); 80 81 int type; 82 while ((type = parser.next()) != XmlPullParser.START_TAG 83 && type != XmlPullParser.END_DOCUMENT) { 84 ; 85 } 86 87 if (!parser.getName().equals(firstElementName)) { 88 throw new XmlPullParserException("Unexpected start tag: found " + parser.getName() 89 + ", expected " + firstElementName); 90 } 91 } 92 } 93