Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(450)

Side by Side Diff: chrome/android/junit/src/org/chromium/chrome/browser/webapps/WebappDataStorageTest.java

Issue 2351113005: [Reland] Refactor WebappRegistry into a singleton instance. (Closed)
Patch Set: Checkstyle import order has changed overnight argh Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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.Intent;
12 import android.content.SharedPreferences; 12 import android.content.SharedPreferences;
13 import android.graphics.Bitmap; 13 import android.graphics.Bitmap;
14 import android.os.AsyncTask; 14 import android.os.AsyncTask;
15 15
16 import org.junit.After;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20
21 import org.robolectric.RuntimeEnvironment;
22 import org.robolectric.annotation.Config;
23 import org.robolectric.shadows.ShadowLooper;
24
16 import org.chromium.base.ContextUtils; 25 import org.chromium.base.ContextUtils;
17 import org.chromium.base.test.util.Feature; 26 import org.chromium.base.test.util.Feature;
18 import org.chromium.blink_public.platform.WebDisplayMode; 27 import org.chromium.blink_public.platform.WebDisplayMode;
19 import org.chromium.chrome.browser.ShortcutHelper; 28 import org.chromium.chrome.browser.ShortcutHelper;
20 import org.chromium.testing.local.BackgroundShadowAsyncTask; 29 import org.chromium.testing.local.BackgroundShadowAsyncTask;
21 import org.chromium.testing.local.LocalRobolectricTestRunner; 30 import org.chromium.testing.local.LocalRobolectricTestRunner;
22 import org.junit.After;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.robolectric.RuntimeEnvironment;
27 import org.robolectric.annotation.Config;
28 import org.robolectric.shadows.ShadowLooper;
29 31
30 import java.util.concurrent.TimeUnit; 32 import java.util.concurrent.TimeUnit;
31 33
32 /** 34 /**
33 * Tests the WebappDataStorage class by ensuring that it persists data to 35 * Tests the WebappDataStorage class by ensuring that it persists data to
34 * SharedPreferences as expected. 36 * SharedPreferences as expected.
35 */ 37 */
36 @RunWith(LocalRobolectricTestRunner.class) 38 @RunWith(LocalRobolectricTestRunner.class)
37 @Config(manifest = Config.NONE, shadows = {BackgroundShadowAsyncTask.class}) 39 @Config(manifest = Config.NONE, shadows = {BackgroundShadowAsyncTask.class})
38 public class WebappDataStorageTest { 40 public class WebappDataStorageTest {
(...skipping 29 matching lines...) Expand all
68 @Override 70 @Override
69 public long currentTimeMillis() { 71 public long currentTimeMillis() {
70 return mCurrentTime; 72 return mCurrentTime;
71 } 73 }
72 } 74 }
73 75
74 @Before 76 @Before
75 public void setUp() throws Exception { 77 public void setUp() throws Exception {
76 ContextUtils.initApplicationContextForTests(RuntimeEnvironment.applicati on); 78 ContextUtils.initApplicationContextForTests(RuntimeEnvironment.applicati on);
77 mSharedPreferences = ContextUtils.getApplicationContext().getSharedPrefe rences( 79 mSharedPreferences = ContextUtils.getApplicationContext().getSharedPrefe rences(
78 "webapp_test", Context.MODE_PRIVATE); 80 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "test", Context.MOD E_PRIVATE);
79 81
80 // Set the last_used as if the web app had been registered by WebappRegi stry. 82 // Set the last_used as if the web app had been registered by WebappRegi stry.
81 mSharedPreferences.edit().putLong("last_used", 0).apply(); 83 mSharedPreferences.edit().putLong(WebappDataStorage.KEY_LAST_USED, 0).ap ply();
82 84
83 mCallbackCalled = false; 85 mCallbackCalled = false;
84 } 86 }
85 87
86 @After 88 @After
87 public void tearDown() { 89 public void tearDown() {
88 mSharedPreferences.edit().clear().apply(); 90 mSharedPreferences.edit().clear().apply();
89 } 91 }
90 92
91 @Test 93 @Test
(...skipping 12 matching lines...) Expand all
104 assertEquals("background_color", WebappDataStorage.KEY_BACKGROUND_COLOR) ; 106 assertEquals("background_color", WebappDataStorage.KEY_BACKGROUND_COLOR) ;
105 assertEquals("source", WebappDataStorage.KEY_SOURCE); 107 assertEquals("source", WebappDataStorage.KEY_SOURCE);
106 assertEquals("action", WebappDataStorage.KEY_ACTION); 108 assertEquals("action", WebappDataStorage.KEY_ACTION);
107 assertEquals("is_icon_generated", WebappDataStorage.KEY_IS_ICON_GENERATE D); 109 assertEquals("is_icon_generated", WebappDataStorage.KEY_IS_ICON_GENERATE D);
108 assertEquals("version", WebappDataStorage.KEY_VERSION); 110 assertEquals("version", WebappDataStorage.KEY_VERSION);
109 } 111 }
110 112
111 @Test 113 @Test
112 @Feature({"Webapp"}) 114 @Feature({"Webapp"})
113 public void testLastUsedRetrieval() throws Exception { 115 public void testLastUsedRetrieval() throws Exception {
114 mSharedPreferences.edit().putLong(WebappDataStorage.KEY_LAST_USED, 100L) .apply(); 116 long lastUsed = 100;
115 117 mSharedPreferences.edit().putLong(WebappDataStorage.KEY_LAST_USED, lastU sed).apply();
116 WebappDataStorage.getLastUsedTime("test", new FetchCallback<Long>(new Lo ng(100L))); 118 assertEquals(lastUsed, new WebappDataStorage("test").getLastUsedTime());
117 BackgroundShadowAsyncTask.runBackgroundTasks();
118 ShadowLooper.runUiThreadTasks();
119
120 assertTrue(mCallbackCalled);
121 } 119 }
122 120
123 @Test 121 @Test
124 @Feature({"Webapp"}) 122 @Feature({"Webapp"})
125 public void testSplashImageRetrieval() throws Exception { 123 public void testSplashImageRetrieval() throws Exception {
126 final Bitmap expected = createBitmap(); 124 final Bitmap expected = createBitmap();
127 mSharedPreferences.edit() 125 mSharedPreferences.edit()
128 .putString(WebappDataStorage.KEY_SPLASH_ICON, 126 .putString(WebappDataStorage.KEY_SPLASH_ICON,
129 ShortcutHelper.encodeBitmapAsString(expected)) 127 ShortcutHelper.encodeBitmapAsString(expected))
130 .commit(); 128 .apply();
131 WebappDataStorage.open("test") 129 WebappDataStorage.open("test").getSplashScreenImage(
132 .getSplashScreenImage(new WebappDataStorage.FetchCallback<Bitmap >() { 130 new WebappDataStorage.FetchCallback<Bitmap>() {
133 @Override 131 @Override
134 public void onDataRetrieved(Bitmap actual) { 132 public void onDataRetrieved(Bitmap actual) {
135 mCallbackCalled = true; 133 mCallbackCalled = true;
136 134
137 // TODO(lalitm) - once the Robolectric bug is fixed chan ge to 135 // TODO(lalitm) - once the Robolectric bug is fixed chan ge to
138 // assertTrue(expected.sameAs(actual)). 136 // assertTrue(expected.sameAs(actual)).
139 // See bitmapEquals(Bitmap, Bitmap) for more information . 137 // See bitmapEquals(Bitmap, Bitmap) for more information .
140 assertTrue(bitmapEquals(expected, actual)); 138 assertTrue(bitmapEquals(expected, actual));
141 } 139 }
142 }); 140 });
143 BackgroundShadowAsyncTask.runBackgroundTasks(); 141 BackgroundShadowAsyncTask.runBackgroundTasks();
144 ShadowLooper.runUiThreadTasks(); 142 ShadowLooper.runUiThreadTasks();
145 143
146 assertTrue(mCallbackCalled); 144 assertTrue(mCallbackCalled);
147 } 145 }
148 146
149 @Test 147 @Test
150 @Feature({"Webapp"}) 148 @Feature({"Webapp"})
151 public void testSplashImageUpdate() throws Exception { 149 public void testSplashImageUpdate() throws Exception {
152 final Bitmap expectedImage = createBitmap(); 150 final Bitmap expectedImage = createBitmap();
153 WebappDataStorage.open("test").updateSplashScreenImage(expectedImage); 151 WebappDataStorage.open("test").updateSplashScreenImage(expectedImage);
154 BackgroundShadowAsyncTask.runBackgroundTasks(); 152 BackgroundShadowAsyncTask.runBackgroundTasks();
153 ShadowLooper.runUiThreadTasks();
155 154
156 assertEquals(ShortcutHelper.encodeBitmapAsString(expectedImage), 155 assertEquals(ShortcutHelper.encodeBitmapAsString(expectedImage),
157 mSharedPreferences.getString(WebappDataStorage.KEY_SPLASH_ICON, null)); 156 mSharedPreferences.getString(WebappDataStorage.KEY_SPLASH_ICON, null));
158 } 157 }
159 158
160 @Test 159 @Test
161 @Feature({"Webapp"}) 160 @Feature({"Webapp"})
162 public void testScopeRetrieval() throws Exception { 161 public void testScopeRetrieval() throws Exception {
163 final String scope = "http://drive.google.com"; 162 String scope = "http://drive.google.com";
164 mSharedPreferences.edit().putString(WebappDataStorage.KEY_SCOPE, scope). apply(); 163 mSharedPreferences.edit().putString(WebappDataStorage.KEY_SCOPE, scope). apply();
165 164 assertEquals(scope, new WebappDataStorage("test").getScope());
166 WebappDataStorage.getScope("test", new FetchCallback<String>(scope));
167 BackgroundShadowAsyncTask.runBackgroundTasks();
168 ShadowLooper.runUiThreadTasks();
169
170 assertTrue(mCallbackCalled);
171 } 165 }
172 166
173 @Test 167 @Test
174 @Feature({"Webapp"}) 168 @Feature({"Webapp"})
175 public void testUrlRetrieval() throws Exception { 169 public void testUrlRetrieval() throws Exception {
176 final String url = "https://www.google.com"; 170 String url = "https://www.google.com";
177 mSharedPreferences.edit().putString(WebappDataStorage.KEY_URL, url).appl y(); 171 mSharedPreferences.edit().putString(WebappDataStorage.KEY_URL, url).appl y();
178 172 assertEquals(url, new WebappDataStorage("test").getUrl());
179 WebappDataStorage.getUrl("test", new FetchCallback<String>(url));
180 BackgroundShadowAsyncTask.runBackgroundTasks();
181 ShadowLooper.runUiThreadTasks();
182
183 assertTrue(mCallbackCalled);
184 } 173 }
185 174
186 @Test 175 @Test
187 @Feature({"Webapp"}) 176 @Feature({"Webapp"})
188 public void testWasLaunchedRecently() throws Exception { 177 public void testWasLaunchedRecently() throws Exception {
189 final TestClock clock = new TestClock(System.currentTimeMillis()); 178 final TestClock clock = new TestClock(System.currentTimeMillis());
190 WebappDataStorage.setClockForTests(clock); 179 WebappDataStorage.setClockForTests(clock);
191 180
192 // Opening a data storage doesn't count as a launch. 181 // Opening a data storage doesn't count as a launch.
193 WebappDataStorage storage = WebappDataStorage.open("test"); 182 WebappDataStorage storage = WebappDataStorage.open("test");
194 BackgroundShadowAsyncTask.runBackgroundTasks();
195 ShadowLooper.runUiThreadTasks();
196 assertTrue(!storage.wasLaunchedRecently()); 183 assertTrue(!storage.wasLaunchedRecently());
197 184
198 // When the last used time is updated, then it is a launch. 185 // When the last used time is updated, then it is a launch.
199 storage.updateLastUsedTime(); 186 storage.updateLastUsedTime();
200 BackgroundShadowAsyncTask.runBackgroundTasks();
201 ShadowLooper.runUiThreadTasks();
202 assertTrue(storage.wasLaunchedRecently()); 187 assertTrue(storage.wasLaunchedRecently());
203 188
204 long lastUsedTime = mSharedPreferences.getLong(WebappDataStorage.KEY_LAS T_USED, 189 long lastUsedTime = mSharedPreferences.getLong(WebappDataStorage.KEY_LAS T_USED,
205 WebappDataStorage.LAST_USED_INVALID); 190 WebappDataStorage.LAST_USED_INVALID);
206 191
207 assertTrue(lastUsedTime != WebappDataStorage.LAST_USED_UNSET); 192 assertTrue(lastUsedTime != WebappDataStorage.LAST_USED_UNSET);
208 assertTrue(lastUsedTime != WebappDataStorage.LAST_USED_INVALID); 193 assertTrue(lastUsedTime != WebappDataStorage.LAST_USED_INVALID);
209 194
210 // Move the last used time one day in the past. 195 // Move the last used time one day in the past.
211 mSharedPreferences.edit() 196 mSharedPreferences.edit()
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 protected Intent doInBackground(Void... nothing) { 241 protected Intent doInBackground(Void... nothing) {
257 return ShortcutHelper.createWebappShortcutIntent(id, action, url , scope, name, 242 return ShortcutHelper.createWebappShortcutIntent(id, action, url , scope, name,
258 shortName, icon, ShortcutHelper.WEBAPP_SHORTCUT_VERSION, displayMode, 243 shortName, icon, ShortcutHelper.WEBAPP_SHORTCUT_VERSION, displayMode,
259 orientation, themeColor, backgroundColor, isIconGenerate d); 244 orientation, themeColor, backgroundColor, isIconGenerate d);
260 } 245 }
261 }; 246 };
262 Intent shortcutIntent = shortcutIntentTask.execute().get(); 247 Intent shortcutIntent = shortcutIntentTask.execute().get();
263 248
264 WebappDataStorage storage = WebappDataStorage.open("test"); 249 WebappDataStorage storage = WebappDataStorage.open("test");
265 storage.updateFromShortcutIntent(shortcutIntent); 250 storage.updateFromShortcutIntent(shortcutIntent);
266 BackgroundShadowAsyncTask.runBackgroundTasks();
267 ShadowLooper.runUiThreadTasks();
268 251
269 assertEquals(action, mSharedPreferences.getString(WebappDataStorage.KEY_ ACTION, null)); 252 assertEquals(action, mSharedPreferences.getString(WebappDataStorage.KEY_ ACTION, null));
270 assertEquals(url, mSharedPreferences.getString(WebappDataStorage.KEY_URL , null)); 253 assertEquals(url, mSharedPreferences.getString(WebappDataStorage.KEY_URL , null));
271 assertEquals(scope, mSharedPreferences.getString(WebappDataStorage.KEY_S COPE, null)); 254 assertEquals(scope, mSharedPreferences.getString(WebappDataStorage.KEY_S COPE, null));
272 assertEquals(name, mSharedPreferences.getString(WebappDataStorage.KEY_NA ME, null)); 255 assertEquals(name, mSharedPreferences.getString(WebappDataStorage.KEY_NA ME, null));
273 assertEquals(shortName, 256 assertEquals(shortName,
274 mSharedPreferences.getString(WebappDataStorage.KEY_SHORT_NAME, n ull)); 257 mSharedPreferences.getString(WebappDataStorage.KEY_SHORT_NAME, n ull));
275 assertEquals(ShortcutHelper.encodeBitmapAsString(icon), 258 assertEquals(ShortcutHelper.encodeBitmapAsString(icon),
276 mSharedPreferences.getString(WebappDataStorage.KEY_ICON, null)); 259 mSharedPreferences.getString(WebappDataStorage.KEY_ICON, null));
277 assertEquals(ShortcutHelper.WEBAPP_SHORTCUT_VERSION, 260 assertEquals(ShortcutHelper.WEBAPP_SHORTCUT_VERSION,
(...skipping 27 matching lines...) Expand all
305 assertEquals(null, mSharedPreferences.getString(WebappDataStorage.KEY_IC ON, null)); 288 assertEquals(null, mSharedPreferences.getString(WebappDataStorage.KEY_IC ON, null));
306 assertEquals(0, mSharedPreferences.getInt(WebappDataStorage.KEY_VERSION, 0)); 289 assertEquals(0, mSharedPreferences.getInt(WebappDataStorage.KEY_VERSION, 0));
307 assertEquals(0, mSharedPreferences.getInt(WebappDataStorage.KEY_ORIENTAT ION, 0)); 290 assertEquals(0, mSharedPreferences.getInt(WebappDataStorage.KEY_ORIENTAT ION, 0));
308 assertEquals(0, mSharedPreferences.getLong(WebappDataStorage.KEY_THEME_C OLOR, 0)); 291 assertEquals(0, mSharedPreferences.getLong(WebappDataStorage.KEY_THEME_C OLOR, 0));
309 assertEquals(0, mSharedPreferences.getLong(WebappDataStorage.KEY_BACKGRO UND_COLOR, 0)); 292 assertEquals(0, mSharedPreferences.getLong(WebappDataStorage.KEY_BACKGRO UND_COLOR, 0));
310 assertEquals(true, 293 assertEquals(true,
311 mSharedPreferences.getBoolean(WebappDataStorage.KEY_IS_ICON_GENE RATED, true)); 294 mSharedPreferences.getBoolean(WebappDataStorage.KEY_IS_ICON_GENE RATED, true));
312 295
313 // Update again from the intent and ensure that the data is restored. 296 // Update again from the intent and ensure that the data is restored.
314 storage.updateFromShortcutIntent(shortcutIntent); 297 storage.updateFromShortcutIntent(shortcutIntent);
315 BackgroundShadowAsyncTask.runBackgroundTasks();
316 ShadowLooper.runUiThreadTasks();
317 298
318 assertEquals(action, mSharedPreferences.getString(WebappDataStorage.KEY_ ACTION, null)); 299 assertEquals(action, mSharedPreferences.getString(WebappDataStorage.KEY_ ACTION, null));
319 assertEquals(url, mSharedPreferences.getString(WebappDataStorage.KEY_URL , null)); 300 assertEquals(url, mSharedPreferences.getString(WebappDataStorage.KEY_URL , null));
320 assertEquals(scope, mSharedPreferences.getString(WebappDataStorage.KEY_S COPE, null)); 301 assertEquals(scope, mSharedPreferences.getString(WebappDataStorage.KEY_S COPE, null));
321 assertEquals(name, mSharedPreferences.getString(WebappDataStorage.KEY_NA ME, null)); 302 assertEquals(name, mSharedPreferences.getString(WebappDataStorage.KEY_NA ME, null));
322 assertEquals(shortName, 303 assertEquals(shortName,
323 mSharedPreferences.getString(WebappDataStorage.KEY_SHORT_NAME, n ull)); 304 mSharedPreferences.getString(WebappDataStorage.KEY_SHORT_NAME, n ull));
324 assertEquals(ShortcutHelper.encodeBitmapAsString(icon), 305 assertEquals(ShortcutHelper.encodeBitmapAsString(icon),
325 mSharedPreferences.getString(WebappDataStorage.KEY_ICON, null)); 306 mSharedPreferences.getString(WebappDataStorage.KEY_ICON, null));
326 assertEquals(ShortcutHelper.WEBAPP_SHORTCUT_VERSION, 307 assertEquals(ShortcutHelper.WEBAPP_SHORTCUT_VERSION,
(...skipping 20 matching lines...) Expand all
347 if (actual.getPixel(i, j) != 0) return false; 328 if (actual.getPixel(i, j) != 0) return false;
348 } 329 }
349 } 330 }
350 return true; 331 return true;
351 } 332 }
352 333
353 private static Bitmap createBitmap() { 334 private static Bitmap createBitmap() {
354 return Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_4444); 335 return Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_4444);
355 } 336 }
356 } 337 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698