| 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. |
| 4 | |
| 5 | package org.chromium.chrome.browser.test.util; |
| 6 | |
| 7 | import android.graphics.Bitmap; |
| 8 | import android.graphics.Bitmap.CompressFormat; |
| 9 | import android.graphics.BitmapFactory; |
| 10 | import android.util.Log; |
| 11 | |
| 12 | import org.chromium.base.test.util.UrlUtils; |
| 13 | |
| 14 | import java.io.ByteArrayOutputStream; |
| 15 | import java.io.InputStream; |
| 16 | import java.io.IOException; |
| 17 | import java.net.URL; |
| 18 | import java.util.Arrays; |
| 19 | |
| 20 | /** |
| 21 | * Set of utility functions shared between tests managing bookmarks. |
| 22 | */ |
| 23 | public class BookmarkUtils { |
| 24 | private static final String TAG = "BookmarkUtils"; |
| 25 | |
| 26 | /** |
| 27 | * Checks if two byte arrays are equal. Used to compare icons. |
| 28 | * @return True if equal, false otherwise. |
| 29 | */ |
| 30 | public static boolean byteArrayEqual(byte[] byte1, byte[] byte2) { |
| 31 | if (byte1 == null && byte2 != null) { |
| 32 | return byte2.length == 0; |
| 33 | } |
| 34 | if (byte2 == null && byte1 != null) { |
| 35 | return byte1.length == 0; |
| 36 | } |
| 37 | return Arrays.equals(byte1, byte2); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Retrieves a byte array with the decoded image data of an icon. |
| 42 | * @return Data of the icon. |
| 43 | */ |
| 44 | public static byte[] getIcon(String testPath) { |
| 45 | ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 46 | try { |
| 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()); |
| 53 | } |
| 54 | return bos.toByteArray(); |
| 55 | } |
| 56 | } |