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