OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.webapps; | 5 package org.chromium.chrome.browser.webapps; |
6 | 6 |
7 import static org.junit.Assert.assertEquals; | 7 import static org.junit.Assert.assertEquals; |
8 import static org.junit.Assert.assertTrue; | 8 import static org.junit.Assert.assertTrue; |
9 | 9 |
10 import android.content.Context; | 10 import android.content.Context; |
| 11 import android.content.Intent; |
11 import android.content.SharedPreferences; | 12 import android.content.SharedPreferences; |
12 import android.graphics.Bitmap; | 13 import android.graphics.Bitmap; |
13 | 14 |
14 import org.chromium.base.test.util.Feature; | 15 import org.chromium.base.test.util.Feature; |
15 import org.chromium.chrome.browser.ShortcutHelper; | 16 import org.chromium.chrome.browser.ShortcutHelper; |
16 import org.chromium.testing.local.BackgroundShadowAsyncTask; | 17 import org.chromium.testing.local.BackgroundShadowAsyncTask; |
17 import org.chromium.testing.local.LocalRobolectricTestRunner; | 18 import org.chromium.testing.local.LocalRobolectricTestRunner; |
18 import org.junit.Before; | 19 import org.junit.Before; |
19 import org.junit.Test; | 20 import org.junit.Test; |
20 import org.junit.runner.RunWith; | 21 import org.junit.runner.RunWith; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 | 57 |
57 mCallbackCalled = false; | 58 mCallbackCalled = false; |
58 } | 59 } |
59 | 60 |
60 @Test | 61 @Test |
61 @Feature({"Webapp"}) | 62 @Feature({"Webapp"}) |
62 public void testBackwardCompat() { | 63 public void testBackwardCompat() { |
63 assertEquals("webapp_", WebappDataStorage.SHARED_PREFS_FILE_PREFIX); | 64 assertEquals("webapp_", WebappDataStorage.SHARED_PREFS_FILE_PREFIX); |
64 assertEquals("splash_icon", WebappDataStorage.KEY_SPLASH_ICON); | 65 assertEquals("splash_icon", WebappDataStorage.KEY_SPLASH_ICON); |
65 assertEquals("last_used", WebappDataStorage.KEY_LAST_USED); | 66 assertEquals("last_used", WebappDataStorage.KEY_LAST_USED); |
| 67 assertEquals("url", WebappDataStorage.KEY_URL); |
66 assertEquals("scope", WebappDataStorage.KEY_SCOPE); | 68 assertEquals("scope", WebappDataStorage.KEY_SCOPE); |
| 69 assertEquals("icon", WebappDataStorage.KEY_ICON); |
| 70 assertEquals("name", WebappDataStorage.KEY_NAME); |
| 71 assertEquals("short_name", WebappDataStorage.KEY_SHORT_NAME); |
| 72 assertEquals("orientation", WebappDataStorage.KEY_ORIENTATION); |
| 73 assertEquals("theme_color", WebappDataStorage.KEY_THEME_COLOR); |
| 74 assertEquals("background_color", WebappDataStorage.KEY_BACKGROUND_COLOR)
; |
| 75 assertEquals("source", WebappDataStorage.KEY_SOURCE); |
| 76 assertEquals("action", WebappDataStorage.KEY_ACTION); |
| 77 assertEquals("is_icon_generated", WebappDataStorage.KEY_IS_ICON_GENERATE
D); |
| 78 assertEquals("version", WebappDataStorage.KEY_VERSION); |
67 } | 79 } |
68 | 80 |
69 @Test | 81 @Test |
70 @Feature({"Webapp"}) | 82 @Feature({"Webapp"}) |
71 public void testLastUsedRetrieval() throws Exception { | 83 public void testLastUsedRetrieval() throws Exception { |
72 mSharedPreferences.edit() | 84 mSharedPreferences.edit() |
73 .putLong(WebappDataStorage.KEY_LAST_USED, 100L) | 85 .putLong(WebappDataStorage.KEY_LAST_USED, 100L) |
74 .commit(); | 86 .commit(); |
75 | 87 |
76 WebappDataStorage.getLastUsedTime(Robolectric.application, "test", | 88 WebappDataStorage.getLastUsedTime(Robolectric.application, "test", |
77 new FetchCallback<Long>(new Long(100L))); | 89 new FetchCallback<Long>(new Long(100L))); |
78 BackgroundShadowAsyncTask.runBackgroundTasks(); | 90 BackgroundShadowAsyncTask.runBackgroundTasks(); |
79 Robolectric.runUiThreadTasks(); | 91 Robolectric.runUiThreadTasks(); |
80 | 92 |
81 assertTrue(mCallbackCalled); | 93 assertTrue(mCallbackCalled); |
82 } | 94 } |
83 | 95 |
84 @Test | 96 @Test |
85 @Feature({"Webapp"}) | 97 @Feature({"Webapp"}) |
86 public void testOpenUpdatesLastUsed() throws Exception { | |
87 long before = System.currentTimeMillis(); | |
88 | |
89 WebappDataStorage.open(Robolectric.application, "test"); | |
90 BackgroundShadowAsyncTask.runBackgroundTasks(); | |
91 | |
92 long after = System.currentTimeMillis(); | |
93 long value = mSharedPreferences.getLong(WebappDataStorage.KEY_LAST_USED,
-1L); | |
94 assertTrue("Timestamp is out of range", before <= value && value <= afte
r); | |
95 } | |
96 | |
97 @Test | |
98 @Feature({"Webapp"}) | |
99 public void testSplashImageRetrieval() throws Exception { | 98 public void testSplashImageRetrieval() throws Exception { |
100 final Bitmap expected = createBitmap(); | 99 final Bitmap expected = createBitmap(); |
101 mSharedPreferences.edit() | 100 mSharedPreferences.edit() |
102 .putString(WebappDataStorage.KEY_SPLASH_ICON, | 101 .putString(WebappDataStorage.KEY_SPLASH_ICON, |
103 ShortcutHelper.encodeBitmapAsString(expected)) | 102 ShortcutHelper.encodeBitmapAsString(expected)) |
104 .commit(); | 103 .commit(); |
105 WebappDataStorage.open(Robolectric.application, "test") | 104 WebappDataStorage.open(Robolectric.application, "test") |
106 .getSplashScreenImage(new WebappDataStorage.FetchCallback<Bitmap
>() { | 105 .getSplashScreenImage(new WebappDataStorage.FetchCallback<Bitmap
>() { |
107 @Override | 106 @Override |
108 public void onDataRetrieved(Bitmap actual) { | 107 public void onDataRetrieved(Bitmap actual) { |
(...skipping 16 matching lines...) Expand all Loading... |
125 public void testSplashImageUpdate() throws Exception { | 124 public void testSplashImageUpdate() throws Exception { |
126 final Bitmap expectedImage = createBitmap(); | 125 final Bitmap expectedImage = createBitmap(); |
127 WebappDataStorage.open(Robolectric.application, "test") | 126 WebappDataStorage.open(Robolectric.application, "test") |
128 .updateSplashScreenImage(expectedImage); | 127 .updateSplashScreenImage(expectedImage); |
129 BackgroundShadowAsyncTask.runBackgroundTasks(); | 128 BackgroundShadowAsyncTask.runBackgroundTasks(); |
130 | 129 |
131 assertEquals(ShortcutHelper.encodeBitmapAsString(expectedImage), | 130 assertEquals(ShortcutHelper.encodeBitmapAsString(expectedImage), |
132 mSharedPreferences.getString(WebappDataStorage.KEY_SPLASH_ICON,
null)); | 131 mSharedPreferences.getString(WebappDataStorage.KEY_SPLASH_ICON,
null)); |
133 } | 132 } |
134 | 133 |
135 // TODO(lalitm) - There seems to be a bug in Robolectric where a Bitmap | |
136 // produced from a byte stream is hardcoded to be a 100x100 bitmap with | |
137 // ARGB_8888 pixel format. Because of this, we need to work around the | |
138 // equality check of bitmaps. Remove this once the bug is fixed. | |
139 private static boolean bitmapEquals(Bitmap expected, Bitmap actual) { | |
140 if (actual.getWidth() != 100) return false; | |
141 if (actual.getHeight() != 100) return false; | |
142 if (!actual.getConfig().equals(Bitmap.Config.ARGB_8888)) return false; | |
143 | |
144 for (int i = 0; i < actual.getWidth(); i++) { | |
145 for (int j = 0; j < actual.getHeight(); j++) { | |
146 if (actual.getPixel(i, j) != 0) return false; | |
147 } | |
148 } | |
149 return true; | |
150 } | |
151 | |
152 private static Bitmap createBitmap() { | |
153 return Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_4444); | |
154 } | |
155 | |
156 @Test | 134 @Test |
157 @Feature({"Webapp"}) | 135 @Feature({"Webapp"}) |
158 public void testScopeRetrieval() throws Exception { | 136 public void testScopeRetrieval() throws Exception { |
159 final String scope = "http://drive.google.com"; | 137 final String scope = "http://drive.google.com"; |
160 mSharedPreferences.edit() | 138 mSharedPreferences.edit() |
161 .putString(WebappDataStorage.KEY_SCOPE, scope) | 139 .putString(WebappDataStorage.KEY_SCOPE, scope) |
162 .commit(); | 140 .commit(); |
163 | 141 |
164 WebappDataStorage.getScope(Robolectric.application, "test", | 142 WebappDataStorage.getScope(Robolectric.application, "test", |
165 new FetchCallback<String>(scope)); | 143 new FetchCallback<String>(scope)); |
166 BackgroundShadowAsyncTask.runBackgroundTasks(); | 144 BackgroundShadowAsyncTask.runBackgroundTasks(); |
167 Robolectric.runUiThreadTasks(); | 145 Robolectric.runUiThreadTasks(); |
168 | 146 |
169 assertTrue(mCallbackCalled); | 147 assertTrue(mCallbackCalled); |
170 } | 148 } |
171 | 149 |
172 @Test | 150 @Test |
173 @Feature({"Webapp"}) | 151 @Feature({"Webapp"}) |
174 public void testScopeUpdate() throws Exception { | 152 public void testURLRetrieval() throws Exception { |
175 final String scope1 = "http://maps.google.com"; | 153 final String url = "https://www.google.com"; |
176 final String scope2 = "http://drive.google.com"; | 154 mSharedPreferences.edit() |
| 155 .putString(WebappDataStorage.KEY_URL, url) |
| 156 .commit(); |
177 | 157 |
178 WebappDataStorage.setScope(Robolectric.application, "test", scope1); | 158 WebappDataStorage.getURL(Robolectric.application, "test", |
| 159 new FetchCallback<String>(url)); |
179 BackgroundShadowAsyncTask.runBackgroundTasks(); | 160 BackgroundShadowAsyncTask.runBackgroundTasks(); |
180 Robolectric.runUiThreadTasks(); | 161 Robolectric.runUiThreadTasks(); |
181 | 162 |
182 assertEquals(scope1, mSharedPreferences.getString(WebappDataStorage.KEY_
SCOPE, null)); | 163 assertTrue(mCallbackCalled); |
| 164 } |
183 | 165 |
184 // Ensure that calling setScope with a different URL after it has been s
et | 166 @Test |
185 // doesn't change the scope stored. | 167 @Feature({"Webapp"}) |
186 WebappDataStorage.setScope(Robolectric.application, "test", scope2); | 168 public void testIntentUpdate() throws Exception { |
| 169 final String id = "id"; |
| 170 final String action = "action"; |
| 171 final String url = "url"; |
| 172 final String scope = "scope"; |
| 173 final String name = "name"; |
| 174 final String shortName = "shortName"; |
| 175 final Bitmap icon = createBitmap(); |
| 176 final int orientation = 1; |
| 177 final long themeColor = 2; |
| 178 final long backgroundColor = 3; |
| 179 final boolean isIconGenerated = false; |
| 180 Intent shortcutIntent = ShortcutHelper.createWebappShortcutIntent(id, ac
tion, url, scope, |
| 181 name, shortName, icon, ShortcutHelper.WEBAPP_SHORTCUT_VERSION, o
rientation, |
| 182 themeColor, backgroundColor, isIconGenerated); |
| 183 |
| 184 WebappDataStorage storage = WebappDataStorage.open(Robolectric.applicati
on, "test"); |
| 185 storage.updateFromShortcutIntent(shortcutIntent); |
187 BackgroundShadowAsyncTask.runBackgroundTasks(); | 186 BackgroundShadowAsyncTask.runBackgroundTasks(); |
188 Robolectric.runUiThreadTasks(); | 187 Robolectric.runUiThreadTasks(); |
189 | 188 |
190 assertEquals(scope1, mSharedPreferences.getString(WebappDataStorage.KEY_
SCOPE, null)); | 189 assertEquals(action, mSharedPreferences.getString(WebappDataStorage.KEY_
ACTION, null)); |
| 190 assertEquals(url, mSharedPreferences.getString(WebappDataStorage.KEY_URL
, null)); |
| 191 assertEquals(scope, mSharedPreferences.getString(WebappDataStorage.KEY_S
COPE, null)); |
| 192 assertEquals(name, mSharedPreferences.getString(WebappDataStorage.KEY_NA
ME, null)); |
| 193 assertEquals(shortName, |
| 194 mSharedPreferences.getString(WebappDataStorage.KEY_SHORT_NAME, n
ull)); |
| 195 assertEquals(ShortcutHelper.encodeBitmapAsString(icon), |
| 196 mSharedPreferences.getString(WebappDataStorage.KEY_ICON, null)); |
| 197 assertEquals(ShortcutHelper.WEBAPP_SHORTCUT_VERSION, |
| 198 mSharedPreferences.getInt(WebappDataStorage.KEY_VERSION, 0)); |
| 199 assertEquals(orientation, mSharedPreferences.getInt(WebappDataStorage.KE
Y_ORIENTATION, 0)); |
| 200 assertEquals(themeColor, mSharedPreferences.getLong(WebappDataStorage.KE
Y_THEME_COLOR, 0)); |
| 201 assertEquals(backgroundColor, |
| 202 mSharedPreferences.getLong(WebappDataStorage.KEY_BACKGROUND_COLO
R, 0)); |
| 203 assertEquals(isIconGenerated, |
| 204 mSharedPreferences.getBoolean(WebappDataStorage.KEY_IS_ICON_GENE
RATED, true)); |
| 205 |
| 206 // Wipe out the data and ensure that it is all gone. |
| 207 mSharedPreferences.edit().remove(WebappDataStorage.KEY_ACTION) |
| 208 .remove(WebappDataStorage.KEY_URL) |
| 209 .remove(WebappDataStorage.KEY_SCOPE) |
| 210 .remove(WebappDataStorage.KEY_NAME) |
| 211 .remove(WebappDataStorage.KEY_SHORT_NAME) |
| 212 .remove(WebappDataStorage.KEY_ICON) |
| 213 .remove(WebappDataStorage.KEY_VERSION) |
| 214 .remove(WebappDataStorage.KEY_ORIENTATION) |
| 215 .remove(WebappDataStorage.KEY_THEME_COLOR) |
| 216 .remove(WebappDataStorage.KEY_BACKGROUND_COLOR) |
| 217 .remove(WebappDataStorage.KEY_IS_ICON_GENERATED) |
| 218 .commit(); |
| 219 |
| 220 assertEquals(null, mSharedPreferences.getString(WebappDataStorage.KEY_AC
TION, null)); |
| 221 assertEquals(null, mSharedPreferences.getString(WebappDataStorage.KEY_UR
L, null)); |
| 222 assertEquals(null, mSharedPreferences.getString(WebappDataStorage.KEY_SC
OPE, null)); |
| 223 assertEquals(null, mSharedPreferences.getString(WebappDataStorage.KEY_NA
ME, null)); |
| 224 assertEquals(null, mSharedPreferences.getString(WebappDataStorage.KEY_SH
ORT_NAME, null)); |
| 225 assertEquals(null, mSharedPreferences.getString(WebappDataStorage.KEY_IC
ON, null)); |
| 226 assertEquals(0, mSharedPreferences.getInt(WebappDataStorage.KEY_VERSION,
0)); |
| 227 assertEquals(0, mSharedPreferences.getInt(WebappDataStorage.KEY_ORIENTAT
ION, 0)); |
| 228 assertEquals(0, mSharedPreferences.getLong(WebappDataStorage.KEY_THEME_C
OLOR, 0)); |
| 229 assertEquals(0, mSharedPreferences.getLong(WebappDataStorage.KEY_BACKGRO
UND_COLOR, 0)); |
| 230 assertEquals(true, |
| 231 mSharedPreferences.getBoolean(WebappDataStorage.KEY_IS_ICON_GENE
RATED, true)); |
| 232 |
| 233 // Update again from the intent and ensure that the data is restored. |
| 234 storage.updateFromShortcutIntent(shortcutIntent); |
| 235 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 236 Robolectric.runUiThreadTasks(); |
| 237 |
| 238 assertEquals(action, mSharedPreferences.getString(WebappDataStorage.KEY_
ACTION, null)); |
| 239 assertEquals(url, mSharedPreferences.getString(WebappDataStorage.KEY_URL
, null)); |
| 240 assertEquals(scope, mSharedPreferences.getString(WebappDataStorage.KEY_S
COPE, null)); |
| 241 assertEquals(name, mSharedPreferences.getString(WebappDataStorage.KEY_NA
ME, null)); |
| 242 assertEquals(shortName, |
| 243 mSharedPreferences.getString(WebappDataStorage.KEY_SHORT_NAME, n
ull)); |
| 244 assertEquals(ShortcutHelper.encodeBitmapAsString(icon), |
| 245 mSharedPreferences.getString(WebappDataStorage.KEY_ICON, null)); |
| 246 assertEquals(ShortcutHelper.WEBAPP_SHORTCUT_VERSION, |
| 247 mSharedPreferences.getInt(WebappDataStorage.KEY_VERSION, 0)); |
| 248 assertEquals(orientation, mSharedPreferences.getInt(WebappDataStorage.KE
Y_ORIENTATION, 0)); |
| 249 assertEquals(themeColor, mSharedPreferences.getLong(WebappDataStorage.KE
Y_THEME_COLOR, 0)); |
| 250 assertEquals(backgroundColor, |
| 251 mSharedPreferences.getLong(WebappDataStorage.KEY_BACKGROUND_COLO
R, 0)); |
| 252 assertEquals(isIconGenerated, |
| 253 mSharedPreferences.getBoolean(WebappDataStorage.KEY_IS_ICON_GENE
RATED, true)); |
| 254 } |
| 255 |
| 256 // TODO(lalitm) - There seems to be a bug in Robolectric where a Bitmap |
| 257 // produced from a byte stream is hardcoded to be a 100x100 bitmap with |
| 258 // ARGB_8888 pixel format. Because of this, we need to work around the |
| 259 // equality check of bitmaps. Remove this once the bug is fixed. |
| 260 private static boolean bitmapEquals(Bitmap expected, Bitmap actual) { |
| 261 if (actual.getWidth() != 100) return false; |
| 262 if (actual.getHeight() != 100) return false; |
| 263 if (!actual.getConfig().equals(Bitmap.Config.ARGB_8888)) return false; |
| 264 |
| 265 for (int i = 0; i < actual.getWidth(); i++) { |
| 266 for (int j = 0; j < actual.getHeight(); j++) { |
| 267 if (actual.getPixel(i, j) != 0) return false; |
| 268 } |
| 269 } |
| 270 return true; |
| 271 } |
| 272 |
| 273 private static Bitmap createBitmap() { |
| 274 return Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_4444); |
191 } | 275 } |
192 } | 276 } |
OLD | NEW |