Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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.webapps; | |
| 6 | |
| 7 import static org.junit.Assert.assertEquals; | |
| 8 import static org.junit.Assert.assertTrue; | |
| 9 | |
| 10 import android.content.Context; | |
| 11 import android.content.SharedPreferences; | |
| 12 import android.graphics.Bitmap; | |
| 13 | |
| 14 import org.chromium.base.test.util.Feature; | |
| 15 import org.chromium.chrome.browser.ShortcutHelper; | |
| 16 import org.chromium.testing.local.LocalRobolectricTestRunner; | |
| 17 import org.junit.Before; | |
| 18 import org.junit.Test; | |
| 19 import org.junit.runner.RunWith; | |
| 20 import org.robolectric.Robolectric; | |
| 21 import org.robolectric.annotation.Config; | |
| 22 | |
| 23 import java.util.concurrent.atomic.AtomicBoolean; | |
| 24 | |
| 25 /** | |
| 26 * Tests the WebappDataStorage class by ensuring that it persists data to | |
| 27 * SharedPreferences as expected. | |
| 28 */ | |
| 29 @RunWith(LocalRobolectricTestRunner.class) | |
| 30 @Config(manifest = Config.NONE) | |
| 31 public class WebappDataStorageTest { | |
| 32 | |
| 33 private SharedPreferences mSharedPreferences; | |
| 34 private AtomicBoolean mCallbackCalled; | |
| 35 | |
| 36 @Before | |
| 37 public void setUp() throws Exception { | |
| 38 mSharedPreferences = Robolectric.application | |
| 39 .getSharedPreferences("webapp_test", Context.MODE_PRIVATE); | |
| 40 mCallbackCalled = new AtomicBoolean(false); | |
| 41 } | |
| 42 | |
| 43 @Test | |
| 44 @Feature("{Webapp}") | |
| 45 public void testBackwardCompat() { | |
| 46 assertEquals("webapp_", WebappDataStorage.SHARED_PREFS_FILE_PREFIX); | |
| 47 assertEquals("splash_icon", WebappDataStorage.KEY_SPLASH_ICON); | |
| 48 assertEquals("last_used", WebappDataStorage.KEY_LAST_USED); | |
| 49 } | |
| 50 | |
| 51 @Test | |
| 52 @Feature("{Webapp}") | |
| 53 public void testLastUsedRetrieval() throws InterruptedException { | |
| 54 mSharedPreferences.edit() | |
| 55 .putLong(WebappDataStorage.KEY_LAST_USED, 100L) | |
| 56 .commit(); | |
| 57 | |
| 58 WebappDataStorage.getLastUsedTime(Robolectric.application, "test", | |
| 59 new WebappDataStorage.FetchCallback<Long>() { | |
| 60 @Override | |
| 61 public void onDataRetrieved(Long readObject) { | |
| 62 mCallbackCalled.set(true); | |
| 63 assertEquals(100L, (long) readObject); | |
| 64 } | |
| 65 }); | |
| 66 Robolectric.runBackgroundTasks(); | |
| 67 assertTrue(mCallbackCalled.get()); | |
| 68 } | |
| 69 | |
| 70 @Test | |
| 71 @Feature("{Webapp}") | |
| 72 public void testOpenUpdatesLastUsed() throws InterruptedException { | |
| 73 long before = System.currentTimeMillis(); | |
| 74 | |
| 75 WebappDataStorage.open(Robolectric.application, "test"); | |
| 76 Robolectric.runBackgroundTasks(); | |
| 77 | |
| 78 long after = System.currentTimeMillis(); | |
| 79 long value = mSharedPreferences.getLong(WebappDataStorage.KEY_LAST_USED, -1L); | |
| 80 assertTrue("Timestamp is out of range", before <= value && value <= afte r); | |
| 81 } | |
| 82 | |
| 83 @Test | |
| 84 @Feature("{Webapp}") | |
| 85 public void testSplashImageRetrieval() throws InterruptedException { | |
| 86 Bitmap expected = createBitmap(); | |
| 87 System.out.println(ShortcutHelper.encodeBitmapAsString(expected)); | |
|
mlamouri (slow - plz ping)
2015/08/27 16:18:23
nit: remove
Lalit Maganti
2015/08/27 16:24:34
And this :/
Done.
| |
| 88 | |
| 89 mSharedPreferences.edit() | |
| 90 .putString(WebappDataStorage.KEY_SPLASH_ICON, | |
| 91 ShortcutHelper.encodeBitmapAsString(expected)) | |
| 92 .commit(); | |
| 93 WebappDataStorage.open(Robolectric.application, "test") | |
| 94 .getSplashScreenImage(new WebappDataStorage.FetchCallback<Bitmap >() { | |
| 95 @Override | |
| 96 public void onDataRetrieved(Bitmap actual) { | |
| 97 mCallbackCalled.set(true); | |
| 98 | |
| 99 // TODO(lalitm) - once the Robolectric bug is fixed chan ge to | |
| 100 // assertTrue(expected.sameAs(actual)). | |
| 101 // See bitmapEquals(Bitmap, Bitmap) for more information . | |
| 102 assertTrue(bitmapEquals(expected, actual)); | |
| 103 } | |
| 104 }); | |
| 105 Robolectric.runBackgroundTasks(); | |
| 106 assertTrue(mCallbackCalled.get()); | |
| 107 } | |
| 108 | |
| 109 @Test | |
| 110 @Feature("{Webapp}") | |
| 111 public void testSplashImageUpdate() throws InterruptedException { | |
| 112 final Bitmap expectedImage = createBitmap(); | |
| 113 WebappDataStorage.open(Robolectric.application, "test") | |
| 114 .updateSplashScreenImage(expectedImage); | |
| 115 Robolectric.runBackgroundTasks(); | |
| 116 assertEquals(ShortcutHelper.encodeBitmapAsString(expectedImage), | |
| 117 mSharedPreferences.getString(WebappDataStorage.KEY_SPLASH_ICON, null)); | |
| 118 } | |
| 119 | |
| 120 // TODO(lalitm) - There seems to be a bug in Robolectric where a Bitmap | |
| 121 // produced from a byte stream is hardcoded to be a 100x100 bitmap with | |
| 122 // ARGB_8888 pixel format. Because of this, we need to work around the | |
| 123 // equality check of bitmaps. Remove this once the bug is fixed. | |
| 124 private static boolean bitmapEquals(Bitmap expected, Bitmap actual) { | |
| 125 if (actual.getWidth() != 100) return false; | |
| 126 if (actual.getHeight() != 100) return false; | |
| 127 if (!actual.getConfig().equals(Bitmap.Config.ARGB_8888)) return false; | |
| 128 | |
| 129 for (int i = 0; i < actual.getWidth(); i++) { | |
| 130 for (int j = 0; j < actual.getHeight(); j++) { | |
| 131 if (actual.getPixel(i, j) != 0) return false; | |
| 132 } | |
| 133 } | |
| 134 return true; | |
| 135 } | |
| 136 | |
| 137 private static Bitmap createBitmap() { | |
| 138 return Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_4444); | |
| 139 } | |
| 140 } | |
| OLD | NEW |