1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.chrome.browser.test.util;
7 import android.graphics.Bitmap;
8 import android.graphics.Bitmap.CompressFormat;
9 import android.graphics.BitmapFactory;
10 import android.util.Log;
12 import org.chromium.base.test.util.UrlUtils;
14 import java.io.ByteArrayOutputStream;
15 import java.io.InputStream;
16 import java.io.IOException;
18 import java.util.Arrays;
21 * Set of utility functions shared between tests managing bookmarks.
23 public class BookmarkUtils {
24 private static final String TAG = "BookmarkUtils";
27 * Checks if two byte arrays are equal. Used to compare icons.
28 * @return True if equal, false otherwise.
30 public static boolean byteArrayEqual(byte[] byte1, byte[] byte2) {
31 if (byte1 == null && byte2 != null) {
32 return byte2.length == 0;
34 if (byte2 == null && byte1 != null) {
35 return byte1.length == 0;
37 return Arrays.equals(byte1, byte2);
41 * Retrieves a byte array with the decoded image data of an icon.
42 * @return Data of the icon.
44 public static byte[] getIcon(String testPath) {
45 ByteArrayOutputStream bos = new ByteArrayOutputStream();
47 InputStream faviconStream = (InputStream) (new URL(
48 UrlUtils.getTestFileUrl(testPath))).getContent();
49 Bitmap faviconBitmap = BitmapFactory.decodeStream(faviconStream);
50 faviconBitmap.compress(CompressFormat.PNG, 0, bos);
51 } catch (IOException e) {
52 Log.e(TAG, "Error trying to get the icon '" + testPath + "': " + e.getMessage());
54 return bos.toByteArray();