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

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

Issue 2195143002: [Android] Update all Robolectric tests to Robolectric 3.0 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: jbudoricks/teds comments Created 4 years, 4 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.text.TextUtils; 13 import android.text.TextUtils;
14 14
15 import org.chromium.base.test.util.Feature; 15 import org.chromium.base.test.util.Feature;
16 import org.chromium.blink_public.platform.WebDisplayMode; 16 import org.chromium.blink_public.platform.WebDisplayMode;
17 import org.chromium.chrome.browser.ShortcutHelper; 17 import org.chromium.chrome.browser.ShortcutHelper;
18 import org.chromium.chrome.browser.browsing_data.UrlFilters; 18 import org.chromium.chrome.browser.browsing_data.UrlFilters;
19 import org.chromium.testing.local.BackgroundShadowAsyncTask; 19 import org.chromium.testing.local.BackgroundShadowAsyncTask;
20 import org.chromium.testing.local.LocalRobolectricTestRunner; 20 import org.chromium.testing.local.LocalRobolectricTestRunner;
21 import org.junit.Before; 21 import org.junit.Before;
22 import org.junit.Test; 22 import org.junit.Test;
23 import org.junit.runner.RunWith; 23 import org.junit.runner.RunWith;
24 import org.robolectric.Robolectric; 24 import org.robolectric.RuntimeEnvironment;
25 import org.robolectric.annotation.Config; 25 import org.robolectric.annotation.Config;
26 import org.robolectric.shadows.ShadowLooper;
26 27
27 import java.util.Arrays; 28 import java.util.Arrays;
28 import java.util.Collections; 29 import java.util.Collections;
29 import java.util.HashMap; 30 import java.util.HashMap;
30 import java.util.HashSet; 31 import java.util.HashSet;
31 import java.util.Map; 32 import java.util.Map;
32 import java.util.Set; 33 import java.util.Set;
33 34
34 /** 35 /**
35 * Tests the WebappRegistry class by ensuring that it persists data to 36 * Tests the WebappRegistry class by ensuring that it persists data to
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 mCallbackCalled = true; 129 mCallbackCalled = true;
129 } 130 }
130 131
131 boolean getCallbackCalled() { 132 boolean getCallbackCalled() {
132 return mCallbackCalled; 133 return mCallbackCalled;
133 } 134 }
134 } 135 }
135 136
136 @Before 137 @Before
137 public void setUp() throws Exception { 138 public void setUp() throws Exception {
138 mSharedPreferences = Robolectric.application 139 mSharedPreferences = RuntimeEnvironment.application
139 .getSharedPreferences(REGISTRY_FILE_NAME, Context.MODE_PRIVATE); 140 .getSharedPreferences(REGISTRY_FILE_NAME, Context.MODE_PRIVATE);
140 mSharedPreferences.edit().putLong(KEY_LAST_CLEANUP, INITIAL_TIME).commit (); 141 mSharedPreferences.edit().putLong(KEY_LAST_CLEANUP, INITIAL_TIME).commit ();
141 142
142 mCallbackCalled = false; 143 mCallbackCalled = false;
143 } 144 }
144 145
145 @Test 146 @Test
146 @Feature({"Webapp"}) 147 @Feature({"Webapp"})
147 public void testBackwardCompatibility() { 148 public void testBackwardCompatibility() {
148 assertEquals(REGISTRY_FILE_NAME, WebappRegistry.REGISTRY_FILE_NAME); 149 assertEquals(REGISTRY_FILE_NAME, WebappRegistry.REGISTRY_FILE_NAME);
149 assertEquals(KEY_WEBAPP_SET, WebappRegistry.KEY_WEBAPP_SET); 150 assertEquals(KEY_WEBAPP_SET, WebappRegistry.KEY_WEBAPP_SET);
150 assertEquals(KEY_LAST_CLEANUP, WebappRegistry.KEY_LAST_CLEANUP); 151 assertEquals(KEY_LAST_CLEANUP, WebappRegistry.KEY_LAST_CLEANUP);
151 } 152 }
152 153
153 @Test 154 @Test
154 @Feature({"Webapp"}) 155 @Feature({"Webapp"})
155 public void testWebappRegistrationAddsToSharedPrefs() throws Exception { 156 public void testWebappRegistrationAddsToSharedPrefs() throws Exception {
156 WebappRegistry.registerWebapp(Robolectric.application, "test", null); 157 WebappRegistry.registerWebapp(RuntimeEnvironment.application, "test", nu ll);
158
157 BackgroundShadowAsyncTask.runBackgroundTasks(); 159 BackgroundShadowAsyncTask.runBackgroundTasks();
158 160
159 Set<String> actual = mSharedPreferences.getStringSet( 161 Set<String> actual = mSharedPreferences.getStringSet(
160 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); 162 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet());
161 assertEquals(1, actual.size()); 163 assertEquals(1, actual.size());
162 assertTrue(actual.contains("test")); 164 assertTrue(actual.contains("test"));
163 } 165 }
164 166
165 @Test 167 @Test
166 @Feature({"Webapp"}) 168 @Feature({"Webapp"})
167 public void testWebappRegistrationUpdatesLastUsed() throws Exception { 169 public void testWebappRegistrationUpdatesLastUsed() throws Exception {
168 WebappRegistry.registerWebapp(Robolectric.application, "test", null); 170 WebappRegistry.registerWebapp(RuntimeEnvironment.application, "test", nu ll);
171
169 BackgroundShadowAsyncTask.runBackgroundTasks(); 172 BackgroundShadowAsyncTask.runBackgroundTasks();
170 long after = System.currentTimeMillis(); 173 long after = System.currentTimeMillis();
171 174
172 SharedPreferences webAppPrefs = Robolectric.application.getSharedPrefere nces( 175 SharedPreferences webAppPrefs = RuntimeEnvironment.application.getShared Preferences(
173 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "test", Context.MOD E_PRIVATE); 176 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "test", Context.MOD E_PRIVATE);
174 long actual = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USED, 177 long actual = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USED,
175 WebappDataStorage.LAST_USED_INVALID); 178 WebappDataStorage.LAST_USED_INVALID);
176 assertTrue("Timestamp is out of range", actual <= after); 179 assertTrue("Timestamp is out of range", actual <= after);
177 } 180 }
178 181
179 @Test 182 @Test
180 @Feature({"Webapp"}) 183 @Feature({"Webapp"})
181 public void testWebappIdsRetrieval() throws Exception { 184 public void testWebappIdsRetrieval() throws Exception {
182 final Set<String> expected = addWebappsToRegistry("first", "second"); 185 final Set<String> expected = addWebappsToRegistry("first", "second");
183 186
184 FetchCallback callback = new FetchCallback(expected); 187 FetchCallback callback = new FetchCallback(expected);
185 WebappRegistry.getRegisteredWebappIds(Robolectric.application, callback) ; 188 WebappRegistry.getRegisteredWebappIds(RuntimeEnvironment.application, ca llback);
189
186 BackgroundShadowAsyncTask.runBackgroundTasks(); 190 BackgroundShadowAsyncTask.runBackgroundTasks();
187 Robolectric.runUiThreadTasks(); 191 ShadowLooper.runUiThreadTasks();
188 192
189 assertTrue(callback.getCallbackCalled()); 193 assertTrue(callback.getCallbackCalled());
190 } 194 }
191 195
192 @Test 196 @Test
193 @Feature({"Webapp"}) 197 @Feature({"Webapp"})
194 public void testWebappIdsRetrievalRegisterRetrival() throws Exception { 198 public void testWebappIdsRetrievalRegisterRetrival() throws Exception {
195 final Set<String> expected = addWebappsToRegistry("first"); 199 final Set<String> expected = addWebappsToRegistry("first");
196 200
197 FetchCallback callback = new FetchCallback(expected); 201 FetchCallback callback = new FetchCallback(expected);
198 WebappRegistry.getRegisteredWebappIds(Robolectric.application, callback) ; 202 WebappRegistry.getRegisteredWebappIds(RuntimeEnvironment.application, ca llback);
203
199 BackgroundShadowAsyncTask.runBackgroundTasks(); 204 BackgroundShadowAsyncTask.runBackgroundTasks();
200 Robolectric.runUiThreadTasks(); 205 ShadowLooper.runUiThreadTasks();
201 206
202 assertTrue(callback.getCallbackCalled()); 207 assertTrue(callback.getCallbackCalled());
203 208
204 WebappRegistry.registerWebapp(Robolectric.application, "second", null); 209 WebappRegistry.registerWebapp(RuntimeEnvironment.application, "second", null);
210
205 BackgroundShadowAsyncTask.runBackgroundTasks(); 211 BackgroundShadowAsyncTask.runBackgroundTasks();
206 212
207 // A copy of the expected set needs to be made as the SharedPreferences is using the copy 213 // A copy of the expected set needs to be made as the SharedPreferences is using the copy
208 // that was paassed to it. 214 // that was paassed to it.
209 final Set<String> secondExpected = new HashSet<>(expected); 215 final Set<String> secondExpected = new HashSet<>(expected);
210 secondExpected.add("second"); 216 secondExpected.add("second");
211 217
212 callback = new FetchCallback(secondExpected); 218 callback = new FetchCallback(secondExpected);
213 WebappRegistry.getRegisteredWebappIds(Robolectric.application, callback) ; 219 WebappRegistry.getRegisteredWebappIds(RuntimeEnvironment.application, ca llback);
220
214 BackgroundShadowAsyncTask.runBackgroundTasks(); 221 BackgroundShadowAsyncTask.runBackgroundTasks();
215 Robolectric.runUiThreadTasks(); 222 ShadowLooper.runUiThreadTasks();
216 223
217 assertTrue(callback.getCallbackCalled()); 224 assertTrue(callback.getCallbackCalled());
218 } 225 }
219 226
220 @Test 227 @Test
221 @Feature({"Webapp"}) 228 @Feature({"Webapp"})
222 public void testUnregisterRunsCallback() throws Exception { 229 public void testUnregisterRunsCallback() throws Exception {
223 CallbackRunner callback = new CallbackRunner(); 230 CallbackRunner callback = new CallbackRunner();
224 WebappRegistry.unregisterWebappsForUrls( 231 WebappRegistry.unregisterWebappsForUrls(
225 Robolectric.application, new UrlFilters.AllUrls(), callback); 232 RuntimeEnvironment.application, new UrlFilters.AllUrls(), callba ck);
226 BackgroundShadowAsyncTask.runBackgroundTasks(); 233 BackgroundShadowAsyncTask.runBackgroundTasks();
227 Robolectric.runUiThreadTasks(); 234 ShadowLooper.runUiThreadTasks();
228 235
229 assertTrue(callback.getCallbackCalled()); 236 assertTrue(callback.getCallbackCalled());
230 } 237 }
231 238
232 @Test 239 @Test
233 @Feature({"Webapp"}) 240 @Feature({"Webapp"})
234 public void testUnregisterClearsRegistry() throws Exception { 241 public void testUnregisterClearsRegistry() throws Exception {
235 Map<String, String> apps = new HashMap<>(); 242 Map<String, String> apps = new HashMap<>();
236 apps.put("webapp1", "http://example.com/index.html"); 243 apps.put("webapp1", "http://example.com/index.html");
237 apps.put("webapp2", "https://www.google.com/foo/bar"); 244 apps.put("webapp2", "https://www.google.com/foo/bar");
238 apps.put("webapp3", "https://www.chrome.com"); 245 apps.put("webapp3", "https://www.chrome.com");
239 246
240 for (Map.Entry<String, String> app : apps.entrySet()) { 247 for (Map.Entry<String, String> app : apps.entrySet()) {
241 WebappRegistry.registerWebapp(Robolectric.application, app.getKey(), 248 WebappRegistry.registerWebapp(RuntimeEnvironment.application, app.ge tKey(),
242 new FetchStorageCallback(createShortcutIntent(app.getValue() ))); 249 new FetchStorageCallback(createShortcutIntent(app.getValue() )));
243 } 250 }
244 BackgroundShadowAsyncTask.runBackgroundTasks(); 251 BackgroundShadowAsyncTask.runBackgroundTasks();
245 Robolectric.runUiThreadTasks(); 252 ShadowLooper.runUiThreadTasks();
246 253
247 // Partial deletion. 254 // Partial deletion.
248 CallbackRunner callback = new CallbackRunner(); 255 CallbackRunner callback = new CallbackRunner();
249 WebappRegistry.unregisterWebappsForUrls( 256 WebappRegistry.unregisterWebappsForUrls(
250 Robolectric.application, 257 RuntimeEnvironment.application,
251 new UrlFilters.OneUrl("http://example.com/index.html"), callback ); 258 new UrlFilters.OneUrl("http://example.com/index.html"), callback );
252 BackgroundShadowAsyncTask.runBackgroundTasks(); 259 BackgroundShadowAsyncTask.runBackgroundTasks();
253 Robolectric.runUiThreadTasks(); 260 ShadowLooper.runUiThreadTasks();
254 261
255 assertTrue(callback.getCallbackCalled()); 262 assertTrue(callback.getCallbackCalled());
256 Set<String> registeredWebapps = getRegisteredWebapps(); 263 Set<String> registeredWebapps = getRegisteredWebapps();
257 assertEquals(2, registeredWebapps.size()); 264 assertEquals(2, registeredWebapps.size());
258 for (String appName : apps.keySet()) { 265 for (String appName : apps.keySet()) {
259 assertEquals(!TextUtils.equals(appName, "webapp1"), 266 assertEquals(!TextUtils.equals(appName, "webapp1"),
260 registeredWebapps.contains(appName)); 267 registeredWebapps.contains(appName));
261 } 268 }
262 269
263 // Full deletion. 270 // Full deletion.
264 callback = new CallbackRunner(); 271 callback = new CallbackRunner();
265 WebappRegistry.unregisterWebappsForUrls( 272 WebappRegistry.unregisterWebappsForUrls(
266 Robolectric.application, new UrlFilters.AllUrls(), callback); 273 RuntimeEnvironment.application, new UrlFilters.AllUrls(), callba ck);
267 BackgroundShadowAsyncTask.runBackgroundTasks(); 274 BackgroundShadowAsyncTask.runBackgroundTasks();
268 Robolectric.runUiThreadTasks(); 275 ShadowLooper.runUiThreadTasks();
269 276
270 assertTrue(callback.getCallbackCalled()); 277 assertTrue(callback.getCallbackCalled());
271 assertTrue(getRegisteredWebapps().isEmpty()); 278 assertTrue(getRegisteredWebapps().isEmpty());
272 } 279 }
273 280
274 @Test 281 @Test
275 @Feature({"Webapp"}) 282 @Feature({"Webapp"})
276 public void testUnregisterClearsWebappDataStorage() throws Exception { 283 public void testUnregisterClearsWebappDataStorage() throws Exception {
277 Map<String, String> apps = new HashMap<>(); 284 Map<String, String> apps = new HashMap<>();
278 apps.put("webapp1", "http://example.com/index.html"); 285 apps.put("webapp1", "http://example.com/index.html");
279 apps.put("webapp2", "https://www.google.com/foo/bar"); 286 apps.put("webapp2", "https://www.google.com/foo/bar");
280 apps.put("webapp3", "https://www.chrome.com"); 287 apps.put("webapp3", "https://www.chrome.com");
281 288
282 for (Map.Entry<String, String> app : apps.entrySet()) { 289 for (Map.Entry<String, String> app : apps.entrySet()) {
283 WebappRegistry.registerWebapp(Robolectric.application, app.getKey(), 290 WebappRegistry.registerWebapp(RuntimeEnvironment.application, app.ge tKey(),
284 new FetchStorageCallback(createShortcutIntent(app.getValue() ))); 291 new FetchStorageCallback(createShortcutIntent(app.getValue() )));
285 } 292 }
286 BackgroundShadowAsyncTask.runBackgroundTasks(); 293 BackgroundShadowAsyncTask.runBackgroundTasks();
287 Robolectric.runUiThreadTasks(); 294 ShadowLooper.runUiThreadTasks();
288 295
289 for (String appName : apps.keySet()) { 296 for (String appName : apps.keySet()) {
290 SharedPreferences webAppPrefs = Robolectric.application.getSharedPre ferences( 297 SharedPreferences webAppPrefs = RuntimeEnvironment.application.getSh aredPreferences(
291 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + appName, Contex t.MODE_PRIVATE); 298 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + appName, Contex t.MODE_PRIVATE);
292 webAppPrefs.edit().putLong(WebappDataStorage.KEY_LAST_USED, 100L).ap ply(); 299 webAppPrefs.edit().putLong(WebappDataStorage.KEY_LAST_USED, 100L).ap ply();
293 } 300 }
294 301
295 // Partial deletion. 302 // Partial deletion.
296 WebappRegistry.unregisterWebappsForUrls( 303 WebappRegistry.unregisterWebappsForUrls(
297 Robolectric.application, 304 RuntimeEnvironment.application,
298 new UrlFilters.OneUrl("http://example.com/index.html"), null); 305 new UrlFilters.OneUrl("http://example.com/index.html"), null);
306
299 BackgroundShadowAsyncTask.runBackgroundTasks(); 307 BackgroundShadowAsyncTask.runBackgroundTasks();
300 for (String appName : apps.keySet()) { 308 for (String appName : apps.keySet()) {
301 SharedPreferences webAppPrefs = Robolectric.application.getSharedPre ferences( 309 SharedPreferences webAppPrefs = RuntimeEnvironment.application.getSh aredPreferences(
302 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + appName, Contex t.MODE_PRIVATE); 310 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + appName, Contex t.MODE_PRIVATE);
303 assertEquals(TextUtils.equals(appName, "webapp1"), webAppPrefs.getAl l().isEmpty()); 311 assertEquals(TextUtils.equals(appName, "webapp1"), webAppPrefs.getAl l().isEmpty());
304 } 312 }
305 313
306 // Full deletion. 314 // Full deletion.
307 WebappRegistry.unregisterWebappsForUrls( 315 WebappRegistry.unregisterWebappsForUrls(
308 Robolectric.application, new UrlFilters.AllUrls(), null); 316 RuntimeEnvironment.application, new UrlFilters.AllUrls(), null);
309 BackgroundShadowAsyncTask.runBackgroundTasks(); 317 BackgroundShadowAsyncTask.runBackgroundTasks();
310 for (String appName : apps.keySet()) { 318 for (String appName : apps.keySet()) {
311 SharedPreferences webAppPrefs = Robolectric.application.getSharedPre ferences( 319 SharedPreferences webAppPrefs = RuntimeEnvironment.application.getSh aredPreferences(
312 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + appName, Contex t.MODE_PRIVATE); 320 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + appName, Contex t.MODE_PRIVATE);
313 assertTrue(webAppPrefs.getAll().isEmpty()); 321 assertTrue(webAppPrefs.getAll().isEmpty());
314 } 322 }
315 } 323 }
316 324
317 @Test 325 @Test
318 @Feature({"Webapp"}) 326 @Feature({"Webapp"})
319 public void testCleanupDoesNotRunTooOften() throws Exception { 327 public void testCleanupDoesNotRunTooOften() throws Exception {
320 // Put the current time to just before the task should run. 328 // Put the current time to just before the task should run.
321 long currentTime = INITIAL_TIME + WebappRegistry.FULL_CLEANUP_DURATION - 1; 329 long currentTime = INITIAL_TIME + WebappRegistry.FULL_CLEANUP_DURATION - 1;
322 330
323 addWebappsToRegistry("oldWebapp"); 331 addWebappsToRegistry("oldWebapp");
324 SharedPreferences webAppPrefs = Robolectric.application.getSharedPrefere nces( 332 SharedPreferences webAppPrefs = RuntimeEnvironment.application.getShared Preferences(
325 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "oldWebapp", Contex t.MODE_PRIVATE); 333 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "oldWebapp", Contex t.MODE_PRIVATE);
326 webAppPrefs.edit().putLong(WebappDataStorage.KEY_LAST_USED, Long.MIN_VAL UE).apply(); 334 webAppPrefs.edit().putLong(WebappDataStorage.KEY_LAST_USED, Long.MIN_VAL UE).apply();
327 335
328 WebappRegistry.unregisterOldWebapps(Robolectric.application, currentTime ); 336 WebappRegistry.unregisterOldWebapps(RuntimeEnvironment.application, curr entTime);
329 BackgroundShadowAsyncTask.runBackgroundTasks(); 337 BackgroundShadowAsyncTask.runBackgroundTasks();
330 338
331 Set<String> actual = mSharedPreferences.getStringSet( 339 Set<String> actual = mSharedPreferences.getStringSet(
332 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); 340 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet());
333 assertEquals(new HashSet<>(Arrays.asList("oldWebapp")), actual); 341 assertEquals(new HashSet<>(Arrays.asList("oldWebapp")), actual);
334 342
335 long actualLastUsed = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USE D, 343 long actualLastUsed = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USE D,
336 WebappDataStorage.LAST_USED_INVALID); 344 WebappDataStorage.LAST_USED_INVALID);
337 assertEquals(Long.MIN_VALUE, actualLastUsed); 345 assertEquals(Long.MIN_VALUE, actualLastUsed);
338 346
339 // The last cleanup time was set to 0 in setUp() so check that this hasn 't changed. 347 // The last cleanup time was set to 0 in setUp() so check that this hasn 't changed.
340 long lastCleanup = mSharedPreferences.getLong(WebappRegistry.KEY_LAST_CL EANUP, -1); 348 long lastCleanup = mSharedPreferences.getLong(WebappRegistry.KEY_LAST_CL EANUP, -1);
341 assertEquals(INITIAL_TIME, lastCleanup); 349 assertEquals(INITIAL_TIME, lastCleanup);
342 } 350 }
343 351
344 @Test 352 @Test
345 @Feature({"Webapp"}) 353 @Feature({"Webapp"})
346 public void testCleanupDoesNotRemoveRecentApps() throws Exception { 354 public void testCleanupDoesNotRemoveRecentApps() throws Exception {
347 // Put the current time such that the task runs. 355 // Put the current time such that the task runs.
348 long currentTime = INITIAL_TIME + WebappRegistry.FULL_CLEANUP_DURATION; 356 long currentTime = INITIAL_TIME + WebappRegistry.FULL_CLEANUP_DURATION;
349 357
350 // Put the last used time just inside the no-cleanup window. 358 // Put the last used time just inside the no-cleanup window.
351 addWebappsToRegistry("recentWebapp"); 359 addWebappsToRegistry("recentWebapp");
352 SharedPreferences webAppPrefs = Robolectric.application.getSharedPrefere nces( 360 SharedPreferences webAppPrefs = RuntimeEnvironment.application.getShared Preferences(
353 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "recentWebapp", Con text.MODE_PRIVATE); 361 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "recentWebapp", Con text.MODE_PRIVATE);
354 long lastUsed = currentTime - WebappRegistry.WEBAPP_UNOPENED_CLEANUP_DUR ATION + 1; 362 long lastUsed = currentTime - WebappRegistry.WEBAPP_UNOPENED_CLEANUP_DUR ATION + 1;
355 webAppPrefs.edit().putLong(WebappDataStorage.KEY_LAST_USED, lastUsed).ap ply(); 363 webAppPrefs.edit().putLong(WebappDataStorage.KEY_LAST_USED, lastUsed).ap ply();
356 364
357 // Because the time is just inside the window, there should be a cleanup but the web app 365 // Because the time is just inside the window, there should be a cleanup but the web app
358 // should not be deleted as it was used recently. The last cleanup time should also be 366 // should not be deleted as it was used recently. The last cleanup time should also be
359 // set to the current time. 367 // set to the current time.
360 WebappRegistry.unregisterOldWebapps(Robolectric.application, currentTime ); 368 WebappRegistry.unregisterOldWebapps(RuntimeEnvironment.application, curr entTime);
361 BackgroundShadowAsyncTask.runBackgroundTasks(); 369 BackgroundShadowAsyncTask.runBackgroundTasks();
362 370
363 Set<String> actual = mSharedPreferences.getStringSet( 371 Set<String> actual = mSharedPreferences.getStringSet(
364 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); 372 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet());
365 assertEquals(new HashSet<>(Arrays.asList("recentWebapp")), actual); 373 assertEquals(new HashSet<>(Arrays.asList("recentWebapp")), actual);
366 374
367 long actualLastUsed = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USE D, 375 long actualLastUsed = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USE D,
368 WebappDataStorage.LAST_USED_INVALID); 376 WebappDataStorage.LAST_USED_INVALID);
369 assertEquals(lastUsed, actualLastUsed); 377 assertEquals(lastUsed, actualLastUsed);
370 378
371 long lastCleanup = mSharedPreferences.getLong(WebappRegistry.KEY_LAST_CL EANUP, -1); 379 long lastCleanup = mSharedPreferences.getLong(WebappRegistry.KEY_LAST_CL EANUP, -1);
372 assertEquals(currentTime, lastCleanup); 380 assertEquals(currentTime, lastCleanup);
373 } 381 }
374 382
375 @Test 383 @Test
376 @Feature({"Webapp"}) 384 @Feature({"Webapp"})
377 public void testCleanupRemovesOldApps() throws Exception { 385 public void testCleanupRemovesOldApps() throws Exception {
378 // Put the current time such that the task runs. 386 // Put the current time such that the task runs.
379 long currentTime = INITIAL_TIME + WebappRegistry.FULL_CLEANUP_DURATION; 387 long currentTime = INITIAL_TIME + WebappRegistry.FULL_CLEANUP_DURATION;
380 388
381 // Put the last used time just outside the no-cleanup window. 389 // Put the last used time just outside the no-cleanup window.
382 addWebappsToRegistry("oldWebapp"); 390 addWebappsToRegistry("oldWebapp");
383 SharedPreferences webAppPrefs = Robolectric.application.getSharedPrefere nces( 391 SharedPreferences webAppPrefs = RuntimeEnvironment.application.getShared Preferences(
384 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "oldWebapp", Contex t.MODE_PRIVATE); 392 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "oldWebapp", Contex t.MODE_PRIVATE);
385 long lastUsed = currentTime - WebappRegistry.WEBAPP_UNOPENED_CLEANUP_DUR ATION; 393 long lastUsed = currentTime - WebappRegistry.WEBAPP_UNOPENED_CLEANUP_DUR ATION;
386 webAppPrefs.edit().putLong(WebappDataStorage.KEY_LAST_USED, lastUsed).ap ply(); 394 webAppPrefs.edit().putLong(WebappDataStorage.KEY_LAST_USED, lastUsed).ap ply();
387 395
388 // Because the time is just inside the window, there should be a cleanup of old web apps and 396 // Because the time is just inside the window, there should be a cleanup of old web apps and
389 // the last cleaned up time should be set to the current time. 397 // the last cleaned up time should be set to the current time.
390 WebappRegistry.unregisterOldWebapps(Robolectric.application, currentTime ); 398 WebappRegistry.unregisterOldWebapps(RuntimeEnvironment.application, curr entTime);
391 BackgroundShadowAsyncTask.runBackgroundTasks(); 399 BackgroundShadowAsyncTask.runBackgroundTasks();
392 400
393 Set<String> actual = mSharedPreferences.getStringSet( 401 Set<String> actual = mSharedPreferences.getStringSet(
394 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); 402 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet());
395 assertTrue(actual.isEmpty()); 403 assertTrue(actual.isEmpty());
396 404
397 long actualLastUsed = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USE D, 405 long actualLastUsed = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USE D,
398 WebappDataStorage.LAST_USED_INVALID); 406 WebappDataStorage.LAST_USED_INVALID);
399 assertEquals(WebappDataStorage.LAST_USED_INVALID, actualLastUsed); 407 assertEquals(WebappDataStorage.LAST_USED_INVALID, actualLastUsed);
400 408
401 long lastCleanup = mSharedPreferences.getLong(WebappRegistry.KEY_LAST_CL EANUP, -1); 409 long lastCleanup = mSharedPreferences.getLong(WebappRegistry.KEY_LAST_CL EANUP, -1);
402 assertEquals(currentTime, lastCleanup); 410 assertEquals(currentTime, lastCleanup);
403 } 411 }
404 412
405 @Test 413 @Test
406 @Feature({"WebApk"}) 414 @Feature({"WebApk"})
407 public void testCleanupRemovesUninstalledWebApks() throws Exception { 415 public void testCleanupRemovesUninstalledWebApks() throws Exception {
408 String webappId1 = "webapk:uninstalledWebApk1"; 416 String webappId1 = "webapk:uninstalledWebApk1";
409 String webApkPackage1 = "uninstalledWebApk1"; 417 String webApkPackage1 = "uninstalledWebApk1";
410 String webappId2 = "webapk:uninstalledWebApk2"; 418 String webappId2 = "webapk:uninstalledWebApk2";
411 String webApkPackage2 = "uninstalledWebApk2"; 419 String webApkPackage2 = "uninstalledWebApk2";
412 420
413 FetchStorageCallback storageCallback1 = new FetchStorageCallback( 421 FetchStorageCallback storageCallback1 = new FetchStorageCallback(
414 createWebApkIntent(webappId1, webApkPackage1)); 422 createWebApkIntent(webappId1, webApkPackage1));
415 WebappRegistry.registerWebapp(Robolectric.application, webappId1, storag eCallback1); 423 WebappRegistry.registerWebapp(RuntimeEnvironment.application, webappId1, storageCallback1);
416 BackgroundShadowAsyncTask.runBackgroundTasks(); 424 BackgroundShadowAsyncTask.runBackgroundTasks();
417 Robolectric.runUiThreadTasks(); 425 ShadowLooper.runUiThreadTasks();
418 assertTrue(storageCallback1.getCallbackCalled()); 426 assertTrue(storageCallback1.getCallbackCalled());
419 427
420 FetchStorageCallback storageCallback2 = new FetchStorageCallback( 428 FetchStorageCallback storageCallback2 = new FetchStorageCallback(
421 createWebApkIntent(webappId1, webApkPackage2)); 429 createWebApkIntent(webappId1, webApkPackage2));
422 WebappRegistry.registerWebapp(Robolectric.application, webappId2, storag eCallback2); 430 WebappRegistry.registerWebapp(RuntimeEnvironment.application, webappId2, storageCallback2);
423 BackgroundShadowAsyncTask.runBackgroundTasks(); 431 BackgroundShadowAsyncTask.runBackgroundTasks();
424 Robolectric.runUiThreadTasks(); 432 ShadowLooper.runUiThreadTasks();
425 assertTrue(storageCallback2.getCallbackCalled()); 433 assertTrue(storageCallback2.getCallbackCalled());
426 434
427 // Verify that both WebAPKs are registered. 435 // Verify that both WebAPKs are registered.
428 Set<String> actual = mSharedPreferences.getStringSet( 436 Set<String> actual = mSharedPreferences.getStringSet(
429 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); 437 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet());
430 assertEquals(2, actual.size()); 438 assertEquals(2, actual.size());
431 assertTrue(actual.contains(webappId1)); 439 assertTrue(actual.contains(webappId1));
432 assertTrue(actual.contains(webappId2)); 440 assertTrue(actual.contains(webappId2));
433 441
434 // Set the current time such that the task runs. 442 // Set the current time such that the task runs.
435 long currentTime = System.currentTimeMillis() + WebappRegistry.FULL_CLEA NUP_DURATION; 443 long currentTime = System.currentTimeMillis() + WebappRegistry.FULL_CLEA NUP_DURATION;
436 // Because the time is just inside the window, there should be a cleanup of 444 // Because the time is just inside the window, there should be a cleanup of
437 // uninstalled WebAPKs and the last cleaned up time should be set to the 445 // uninstalled WebAPKs and the last cleaned up time should be set to the
438 // current time. 446 // current time.
439 WebappRegistry.unregisterOldWebapps(Robolectric.application, currentTime ); 447 WebappRegistry.unregisterOldWebapps(RuntimeEnvironment.application, curr entTime);
440 BackgroundShadowAsyncTask.runBackgroundTasks(); 448 BackgroundShadowAsyncTask.runBackgroundTasks();
441 Robolectric.runUiThreadTasks(); 449 ShadowLooper.runUiThreadTasks();
442 450
443 actual = mSharedPreferences.getStringSet( 451 actual = mSharedPreferences.getStringSet(
444 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); 452 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet());
445 assertTrue(actual.isEmpty()); 453 assertTrue(actual.isEmpty());
446 454
447 long lastCleanup = mSharedPreferences.getLong( 455 long lastCleanup = mSharedPreferences.getLong(
448 WebappRegistry.KEY_LAST_CLEANUP, -1); 456 WebappRegistry.KEY_LAST_CLEANUP, -1);
449 assertEquals(currentTime, lastCleanup); 457 assertEquals(currentTime, lastCleanup);
450 } 458 }
451 459
452 @Test 460 @Test
453 @Feature({"WebApk"}) 461 @Feature({"WebApk"})
454 public void testCleanupDoesNotRemoveInstalledWebApks() throws Exception { 462 public void testCleanupDoesNotRemoveInstalledWebApks() throws Exception {
455 String webappId = "webapk:installedWebApk"; 463 String webappId = "webapk:installedWebApk";
456 String webApkPackage = "installedWebApk"; 464 String webApkPackage = "installedWebApk";
457 String uninstalledWebappId = "webapk:uninstalledWebApk"; 465 String uninstalledWebappId = "webapk:uninstalledWebApk";
458 String uninstalledWebApkPackage = "uninstalledWebApk"; 466 String uninstalledWebApkPackage = "uninstalledWebApk";
459 467
460 FetchStorageCallback storageCallback = new FetchStorageCallback( 468 FetchStorageCallback storageCallback = new FetchStorageCallback(
461 createWebApkIntent(webappId, webApkPackage)); 469 createWebApkIntent(webappId, webApkPackage));
462 WebappRegistry.registerWebapp(Robolectric.application, webappId, storage Callback); 470 WebappRegistry.registerWebapp(RuntimeEnvironment.application, webappId, storageCallback);
463 BackgroundShadowAsyncTask.runBackgroundTasks(); 471 BackgroundShadowAsyncTask.runBackgroundTasks();
464 Robolectric.runUiThreadTasks(); 472 ShadowLooper.runUiThreadTasks();
465 assertTrue(storageCallback.getCallbackCalled()); 473 assertTrue(storageCallback.getCallbackCalled());
466 474
467 FetchStorageCallback storageCallback2 = new FetchStorageCallback( 475 FetchStorageCallback storageCallback2 = new FetchStorageCallback(
468 createWebApkIntent(uninstalledWebappId, uninstalledWebApkPackage )); 476 createWebApkIntent(uninstalledWebappId, uninstalledWebApkPackage ));
469 WebappRegistry.registerWebapp(Robolectric.application, uninstalledWebapp Id, 477 WebappRegistry.registerWebapp(RuntimeEnvironment.application, uninstalle dWebappId,
470 storageCallback2); 478 storageCallback2);
471 BackgroundShadowAsyncTask.runBackgroundTasks(); 479 BackgroundShadowAsyncTask.runBackgroundTasks();
472 Robolectric.runUiThreadTasks(); 480 ShadowLooper.runUiThreadTasks();
473 assertTrue(storageCallback2.getCallbackCalled()); 481 assertTrue(storageCallback2.getCallbackCalled());
474 482
475 // Verify that both WebAPKs are registered. 483 // Verify that both WebAPKs are registered.
476 Set<String> actual = mSharedPreferences.getStringSet( 484 Set<String> actual = mSharedPreferences.getStringSet(
477 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); 485 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet());
478 assertEquals(2, actual.size()); 486 assertEquals(2, actual.size());
479 assertTrue(actual.contains(webappId)); 487 assertTrue(actual.contains(webappId));
480 assertTrue(actual.contains(uninstalledWebappId)); 488 assertTrue(actual.contains(uninstalledWebappId));
481 489
482 Robolectric.packageManager.addPackage(webApkPackage); 490 RuntimeEnvironment.getRobolectricPackageManager().addPackage(webApkPacka ge);
483 491
484 // Set the current time such that the task runs. 492 // Set the current time such that the task runs.
485 long currentTime = System.currentTimeMillis() + WebappRegistry.FULL_CLEA NUP_DURATION; 493 long currentTime = System.currentTimeMillis() + WebappRegistry.FULL_CLEA NUP_DURATION;
486 // Because the time is just inside the window, there should be a cleanup of 494 // Because the time is just inside the window, there should be a cleanup of
487 // uninstalled WebAPKs and the last cleaned up time should be set to the 495 // uninstalled WebAPKs and the last cleaned up time should be set to the
488 // current time. 496 // current time.
489 WebappRegistry.unregisterOldWebapps(Robolectric.application, currentTime ); 497 WebappRegistry.unregisterOldWebapps(RuntimeEnvironment.application, curr entTime);
490 BackgroundShadowAsyncTask.runBackgroundTasks(); 498 BackgroundShadowAsyncTask.runBackgroundTasks();
491 Robolectric.runUiThreadTasks(); 499 ShadowLooper.runUiThreadTasks();
492 500
493 actual = mSharedPreferences.getStringSet( 501 actual = mSharedPreferences.getStringSet(
494 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); 502 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet());
495 assertEquals(1, actual.size()); 503 assertEquals(1, actual.size());
496 assertTrue(actual.contains(webappId)); 504 assertTrue(actual.contains(webappId));
497 505
498 long lastCleanup = mSharedPreferences.getLong( 506 long lastCleanup = mSharedPreferences.getLong(
499 WebappRegistry.KEY_LAST_CLEANUP, -1); 507 WebappRegistry.KEY_LAST_CLEANUP, -1);
500 assertEquals(currentTime, lastCleanup); 508 assertEquals(currentTime, lastCleanup);
501 } 509 }
502 510
503 @Test 511 @Test
504 @Feature({"Webapp"}) 512 @Feature({"Webapp"})
505 public void testClearWebappHistoryRunsCallback() throws Exception { 513 public void testClearWebappHistoryRunsCallback() throws Exception {
506 CallbackRunner callback = new CallbackRunner(); 514 CallbackRunner callback = new CallbackRunner();
507 WebappRegistry.clearWebappHistory(Robolectric.application, callback); 515 WebappRegistry.clearWebappHistory(RuntimeEnvironment.application, callba ck);
508 BackgroundShadowAsyncTask.runBackgroundTasks(); 516 BackgroundShadowAsyncTask.runBackgroundTasks();
509 Robolectric.runUiThreadTasks(); 517 ShadowLooper.runUiThreadTasks();
510 518
511 assertTrue(callback.getCallbackCalled()); 519 assertTrue(callback.getCallbackCalled());
512 } 520 }
513 521
514 @Test 522 @Test
515 @Feature({"Webapp"}) 523 @Feature({"Webapp"})
516 public void testClearWebappHistory() throws Exception { 524 public void testClearWebappHistory() throws Exception {
517 final String webapp1Url = "https://www.google.com"; 525 final String webapp1Url = "https://www.google.com";
518 final String webapp2Url = "https://drive.google.com"; 526 final String webapp2Url = "https://drive.google.com";
519 Intent shortcutIntent1 = createShortcutIntent(webapp1Url); 527 Intent shortcutIntent1 = createShortcutIntent(webapp1Url);
520 Intent shortcutIntent2 = createShortcutIntent(webapp2Url); 528 Intent shortcutIntent2 = createShortcutIntent(webapp2Url);
521 529
522 FetchStorageCallback storageCallback1 = new FetchStorageCallback(shortcu tIntent1); 530 FetchStorageCallback storageCallback1 = new FetchStorageCallback(shortcu tIntent1);
523 WebappRegistry.registerWebapp(Robolectric.application, "webapp1", storag eCallback1); 531 WebappRegistry.registerWebapp(RuntimeEnvironment.application, "webapp1", storageCallback1);
524 BackgroundShadowAsyncTask.runBackgroundTasks(); 532 BackgroundShadowAsyncTask.runBackgroundTasks();
525 Robolectric.runUiThreadTasks(); 533 ShadowLooper.runUiThreadTasks();
526 assertTrue(storageCallback1.getCallbackCalled()); 534 assertTrue(storageCallback1.getCallbackCalled());
527 535
528 FetchStorageCallback storageCallback2 = new FetchStorageCallback(shortcu tIntent2); 536 FetchStorageCallback storageCallback2 = new FetchStorageCallback(shortcu tIntent2);
529 WebappRegistry.registerWebapp(Robolectric.application, "webapp2", storag eCallback2); 537 WebappRegistry.registerWebapp(RuntimeEnvironment.application, "webapp2", storageCallback2);
530 BackgroundShadowAsyncTask.runBackgroundTasks(); 538 BackgroundShadowAsyncTask.runBackgroundTasks();
531 Robolectric.runUiThreadTasks(); 539 ShadowLooper.runUiThreadTasks();
532 assertTrue(storageCallback2.getCallbackCalled()); 540 assertTrue(storageCallback2.getCallbackCalled());
533 541
534 SharedPreferences webapp1Prefs = Robolectric.application.getSharedPrefer ences( 542 SharedPreferences webapp1Prefs = RuntimeEnvironment.application.getShare dPreferences(
535 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "webapp1", Context. MODE_PRIVATE); 543 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "webapp1", Context. MODE_PRIVATE);
536 SharedPreferences webapp2Prefs = Robolectric.application.getSharedPrefer ences( 544 SharedPreferences webapp2Prefs = RuntimeEnvironment.application.getShare dPreferences(
537 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "webapp2", Context. MODE_PRIVATE); 545 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "webapp2", Context. MODE_PRIVATE);
538 546
539 CallbackRunner callback = new CallbackRunner(); 547 CallbackRunner callback = new CallbackRunner();
540 WebappRegistry.clearWebappHistory(Robolectric.application, callback); 548 WebappRegistry.clearWebappHistory(RuntimeEnvironment.application, callba ck);
541 BackgroundShadowAsyncTask.runBackgroundTasks(); 549 BackgroundShadowAsyncTask.runBackgroundTasks();
542 Robolectric.runUiThreadTasks(); 550 ShadowLooper.runUiThreadTasks();
543 assertTrue(callback.getCallbackCalled()); 551 assertTrue(callback.getCallbackCalled());
544 552
545 Set<String> actual = mSharedPreferences.getStringSet( 553 Set<String> actual = mSharedPreferences.getStringSet(
546 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); 554 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet());
547 assertEquals(2, actual.size()); 555 assertEquals(2, actual.size());
548 assertTrue(actual.contains("webapp1")); 556 assertTrue(actual.contains("webapp1"));
549 assertTrue(actual.contains("webapp2")); 557 assertTrue(actual.contains("webapp2"));
550 558
551 // Verify that the last used time for both web apps is WebappDataStorage .LAST_USED_UNSET. 559 // Verify that the last used time for both web apps is WebappDataStorage .LAST_USED_UNSET.
552 long actualLastUsed = webapp1Prefs.getLong( 560 long actualLastUsed = webapp1Prefs.getLong(
(...skipping 14 matching lines...) Expand all
567 WebappDataStorage.KEY_SCOPE, WebappDataStorage.URL_INVALID); 575 WebappDataStorage.KEY_SCOPE, WebappDataStorage.URL_INVALID);
568 assertEquals(WebappDataStorage.URL_INVALID, actualScope); 576 assertEquals(WebappDataStorage.URL_INVALID, actualScope);
569 actualUrl = webapp2Prefs.getString( 577 actualUrl = webapp2Prefs.getString(
570 WebappDataStorage.KEY_URL, WebappDataStorage.URL_INVALID); 578 WebappDataStorage.KEY_URL, WebappDataStorage.URL_INVALID);
571 assertEquals(WebappDataStorage.URL_INVALID, actualUrl); 579 assertEquals(WebappDataStorage.URL_INVALID, actualUrl);
572 } 580 }
573 581
574 @Test 582 @Test
575 @Feature({"Webapp"}) 583 @Feature({"Webapp"})
576 public void testGetAfterClearWebappHistory() throws Exception { 584 public void testGetAfterClearWebappHistory() throws Exception {
577 WebappRegistry.registerWebapp(Robolectric.application, "webapp", null); 585 WebappRegistry.registerWebapp(RuntimeEnvironment.application, "webapp", null);
578 BackgroundShadowAsyncTask.runBackgroundTasks(); 586 BackgroundShadowAsyncTask.runBackgroundTasks();
579 587
580 SharedPreferences webappPrefs = Robolectric.application.getSharedPrefere nces( 588 SharedPreferences webappPrefs = RuntimeEnvironment.application.getShared Preferences(
581 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "webapp", Context.M ODE_PRIVATE); 589 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "webapp", Context.M ODE_PRIVATE);
582 CallbackRunner callback = new CallbackRunner(); 590 CallbackRunner callback = new CallbackRunner();
583 WebappRegistry.clearWebappHistory(Robolectric.application, callback); 591 WebappRegistry.clearWebappHistory(RuntimeEnvironment.application, callba ck);
584 BackgroundShadowAsyncTask.runBackgroundTasks(); 592 BackgroundShadowAsyncTask.runBackgroundTasks();
585 Robolectric.runUiThreadTasks(); 593 ShadowLooper.runUiThreadTasks();
586 assertTrue(callback.getCallbackCalled()); 594 assertTrue(callback.getCallbackCalled());
587 595
588 // Open the webapp up to set the last used time. 596 // Open the webapp up to set the last used time.
589 FetchStorageCallback storageCallback = new FetchStorageCallback(null); 597 FetchStorageCallback storageCallback = new FetchStorageCallback(null);
590 WebappRegistry.getWebappDataStorage(Robolectric.application, "webapp", s torageCallback); 598 WebappRegistry.getWebappDataStorage(
599 RuntimeEnvironment.application, "webapp", storageCallback);
591 BackgroundShadowAsyncTask.runBackgroundTasks(); 600 BackgroundShadowAsyncTask.runBackgroundTasks();
592 Robolectric.runUiThreadTasks(); 601 ShadowLooper.runUiThreadTasks();
593 assertTrue(storageCallback.getCallbackCalled()); 602 assertTrue(storageCallback.getCallbackCalled());
594 603
595 // Verify that the last used time is valid. 604 // Verify that the last used time is valid.
596 long actualLastUsed = webappPrefs.getLong( 605 long actualLastUsed = webappPrefs.getLong(
597 WebappDataStorage.KEY_LAST_USED, WebappDataStorage.LAST_USED_INV ALID); 606 WebappDataStorage.KEY_LAST_USED, WebappDataStorage.LAST_USED_INV ALID);
598 assertTrue(WebappDataStorage.LAST_USED_INVALID != actualLastUsed); 607 assertTrue(WebappDataStorage.LAST_USED_INVALID != actualLastUsed);
599 assertTrue(WebappDataStorage.LAST_USED_UNSET != actualLastUsed); 608 assertTrue(WebappDataStorage.LAST_USED_UNSET != actualLastUsed);
600 } 609 }
601 610
602 @Test 611 @Test
603 @Feature({"Webapp"}) 612 @Feature({"Webapp"})
604 public void testUpdateAfterClearWebappHistory() throws Exception { 613 public void testUpdateAfterClearWebappHistory() throws Exception {
605 final String webappUrl = "http://www.google.com"; 614 final String webappUrl = "http://www.google.com";
606 final String webappScope = "http://www.google.com/"; 615 final String webappScope = "http://www.google.com/";
607 final Intent shortcutIntent = createShortcutIntent(webappUrl); 616 final Intent shortcutIntent = createShortcutIntent(webappUrl);
608 WebappRegistry.registerWebapp(Robolectric.application, "webapp", 617 WebappRegistry.registerWebapp(RuntimeEnvironment.application, "webapp",
609 new FetchStorageCallback(shortcutIntent)); 618 new FetchStorageCallback(shortcutIntent));
610 BackgroundShadowAsyncTask.runBackgroundTasks(); 619 BackgroundShadowAsyncTask.runBackgroundTasks();
611 Robolectric.runUiThreadTasks(); 620 ShadowLooper.runUiThreadTasks();
612 621
613 SharedPreferences webappPrefs = Robolectric.application.getSharedPrefere nces( 622 SharedPreferences webappPrefs = RuntimeEnvironment.application.getShared Preferences(
614 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "webapp", Context.M ODE_PRIVATE); 623 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "webapp", Context.M ODE_PRIVATE);
615 624
616 // Verify that the URL and scope match the original in the intent. 625 // Verify that the URL and scope match the original in the intent.
617 String actualUrl = webappPrefs.getString( 626 String actualUrl = webappPrefs.getString(
618 WebappDataStorage.KEY_URL, WebappDataStorage.URL_INVALID); 627 WebappDataStorage.KEY_URL, WebappDataStorage.URL_INVALID);
619 assertEquals(webappUrl, actualUrl); 628 assertEquals(webappUrl, actualUrl);
620 String actualScope = webappPrefs.getString( 629 String actualScope = webappPrefs.getString(
621 WebappDataStorage.KEY_SCOPE, WebappDataStorage.URL_INVALID); 630 WebappDataStorage.KEY_SCOPE, WebappDataStorage.URL_INVALID);
622 assertEquals(webappScope, actualScope); 631 assertEquals(webappScope, actualScope);
623 632
624 WebappRegistry.clearWebappHistory(Robolectric.application, new CallbackR unner()); 633 WebappRegistry.clearWebappHistory(RuntimeEnvironment.application, new Ca llbackRunner());
625 BackgroundShadowAsyncTask.runBackgroundTasks(); 634 BackgroundShadowAsyncTask.runBackgroundTasks();
626 Robolectric.runUiThreadTasks(); 635 ShadowLooper.runUiThreadTasks();
627 636
628 // Update the webapp from the intent again. 637 // Update the webapp from the intent again.
629 WebappRegistry.getWebappDataStorage(Robolectric.application, "webapp", 638 WebappRegistry.getWebappDataStorage(RuntimeEnvironment.application, "web app",
630 new FetchStorageCallback(shortcutIntent)); 639 new FetchStorageCallback(shortcutIntent));
631 BackgroundShadowAsyncTask.runBackgroundTasks(); 640 BackgroundShadowAsyncTask.runBackgroundTasks();
632 Robolectric.runUiThreadTasks(); 641 ShadowLooper.runUiThreadTasks();
633 642
634 // Verify that the URL and scope match the original in the intent. 643 // Verify that the URL and scope match the original in the intent.
635 actualUrl = webappPrefs.getString(WebappDataStorage.KEY_URL, WebappDataS torage.URL_INVALID); 644 actualUrl = webappPrefs.getString(WebappDataStorage.KEY_URL, WebappDataS torage.URL_INVALID);
636 assertEquals(webappUrl, actualUrl); 645 assertEquals(webappUrl, actualUrl);
637 actualScope = webappPrefs.getString( 646 actualScope = webappPrefs.getString(
638 WebappDataStorage.KEY_SCOPE, WebappDataStorage.URL_INVALID); 647 WebappDataStorage.KEY_SCOPE, WebappDataStorage.URL_INVALID);
639 assertEquals(webappScope, actualScope); 648 assertEquals(webappScope, actualScope);
640 } 649 }
641 650
642 @Test 651 @Test
(...skipping 15 matching lines...) Expand all
658 final String test4Url = "https://www.google.com/drive/docs/recent/index. html"; 667 final String test4Url = "https://www.google.com/drive/docs/recent/index. html";
659 final String test5Url = "https://drive.google.com/docs/recent/trash"; 668 final String test5Url = "https://drive.google.com/docs/recent/trash";
660 final String test6Url = "https://maps.google.com/"; 669 final String test6Url = "https://maps.google.com/";
661 670
662 Intent shortcutIntent1 = createShortcutIntent(webapp1Url); 671 Intent shortcutIntent1 = createShortcutIntent(webapp1Url);
663 Intent shortcutIntent2 = createShortcutIntent(webapp2Url); 672 Intent shortcutIntent2 = createShortcutIntent(webapp2Url);
664 Intent shortcutIntent3 = createShortcutIntent(webapp3Url); 673 Intent shortcutIntent3 = createShortcutIntent(webapp3Url);
665 Intent shortcutIntent4 = createShortcutIntent(webapp4Url); 674 Intent shortcutIntent4 = createShortcutIntent(webapp4Url);
666 675
667 // Register the four web apps. 676 // Register the four web apps.
668 WebappRegistry.registerWebapp(Robolectric.application, "webapp1", 677 WebappRegistry.registerWebapp(RuntimeEnvironment.application, "webapp1",
669 new FetchStorageCallback(shortcutIntent1)); 678 new FetchStorageCallback(shortcutIntent1));
670 BackgroundShadowAsyncTask.runBackgroundTasks(); 679 BackgroundShadowAsyncTask.runBackgroundTasks();
671 Robolectric.runUiThreadTasks(); 680 ShadowLooper.runUiThreadTasks();
672 681
673 WebappRegistry.registerWebapp(Robolectric.application, "webapp2", 682 WebappRegistry.registerWebapp(RuntimeEnvironment.application, "webapp2",
674 new FetchStorageCallback(shortcutIntent2)); 683 new FetchStorageCallback(shortcutIntent2));
675 BackgroundShadowAsyncTask.runBackgroundTasks(); 684 BackgroundShadowAsyncTask.runBackgroundTasks();
676 Robolectric.runUiThreadTasks(); 685 ShadowLooper.runUiThreadTasks();
677 686
678 WebappRegistry.registerWebapp(Robolectric.application, "webapp3", 687 WebappRegistry.registerWebapp(RuntimeEnvironment.application, "webapp3",
679 new FetchStorageCallback(shortcutIntent3)); 688 new FetchStorageCallback(shortcutIntent3));
680 BackgroundShadowAsyncTask.runBackgroundTasks(); 689 BackgroundShadowAsyncTask.runBackgroundTasks();
681 Robolectric.runUiThreadTasks(); 690 ShadowLooper.runUiThreadTasks();
682 691
683 WebappRegistry.registerWebapp(Robolectric.application, "webapp4", 692 WebappRegistry.registerWebapp(RuntimeEnvironment.application, "webapp4",
684 new FetchStorageCallback(shortcutIntent4)); 693 new FetchStorageCallback(shortcutIntent4));
685 BackgroundShadowAsyncTask.runBackgroundTasks(); 694 BackgroundShadowAsyncTask.runBackgroundTasks();
686 Robolectric.runUiThreadTasks(); 695 ShadowLooper.runUiThreadTasks();
687 696
688 // test1Url should return webapp1. 697 // test1Url should return webapp1.
689 FetchStorageByUrlCallback callback = new FetchStorageByUrlCallback(webap p1Url, webapp1Url); 698 FetchStorageByUrlCallback callback = new FetchStorageByUrlCallback(webap p1Url, webapp1Url);
690 WebappRegistry.getWebappDataStorageForUrl(Robolectric.application, test1 Url, callback); 699 WebappRegistry.getWebappDataStorageForUrl(
700 RuntimeEnvironment.application, test1Url, callback);
691 BackgroundShadowAsyncTask.runBackgroundTasks(); 701 BackgroundShadowAsyncTask.runBackgroundTasks();
692 Robolectric.runUiThreadTasks(); 702 ShadowLooper.runUiThreadTasks();
693 assertTrue(callback.getCallbackCalled()); 703 assertTrue(callback.getCallbackCalled());
694 704
695 // test2Url should return webapp3. 705 // test2Url should return webapp3.
696 callback = new FetchStorageByUrlCallback(webapp3Url, webapp3Scope); 706 callback = new FetchStorageByUrlCallback(webapp3Url, webapp3Scope);
697 WebappRegistry.getWebappDataStorageForUrl(Robolectric.application, test2 Url, callback); 707 WebappRegistry.getWebappDataStorageForUrl(
708 RuntimeEnvironment.application, test2Url, callback);
698 BackgroundShadowAsyncTask.runBackgroundTasks(); 709 BackgroundShadowAsyncTask.runBackgroundTasks();
699 Robolectric.runUiThreadTasks(); 710 ShadowLooper.runUiThreadTasks();
700 assertTrue(callback.getCallbackCalled()); 711 assertTrue(callback.getCallbackCalled());
701 712
702 // test3Url should return webapp4. 713 // test3Url should return webapp4.
703 callback = new FetchStorageByUrlCallback(webapp4Url, webapp4Scope); 714 callback = new FetchStorageByUrlCallback(webapp4Url, webapp4Scope);
704 WebappRegistry.getWebappDataStorageForUrl(Robolectric.application, test3 Url, callback); 715 WebappRegistry.getWebappDataStorageForUrl(
716 RuntimeEnvironment.application, test3Url, callback);
705 BackgroundShadowAsyncTask.runBackgroundTasks(); 717 BackgroundShadowAsyncTask.runBackgroundTasks();
706 Robolectric.runUiThreadTasks(); 718 ShadowLooper.runUiThreadTasks();
707 assertTrue(callback.getCallbackCalled()); 719 assertTrue(callback.getCallbackCalled());
708 720
709 // test4Url should return webapp4. 721 // test4Url should return webapp4.
710 callback = new FetchStorageByUrlCallback(webapp4Url, webapp4Scope); 722 callback = new FetchStorageByUrlCallback(webapp4Url, webapp4Scope);
711 WebappRegistry.getWebappDataStorageForUrl(Robolectric.application, test4 Url, callback); 723 WebappRegistry.getWebappDataStorageForUrl(
724 RuntimeEnvironment.application, test4Url, callback);
712 BackgroundShadowAsyncTask.runBackgroundTasks(); 725 BackgroundShadowAsyncTask.runBackgroundTasks();
713 Robolectric.runUiThreadTasks(); 726 ShadowLooper.runUiThreadTasks();
714 assertTrue(callback.getCallbackCalled()); 727 assertTrue(callback.getCallbackCalled());
715 728
716 // test5Url should return webapp2. 729 // test5Url should return webapp2.
717 callback = new FetchStorageByUrlCallback(webapp2Url, webapp2Url); 730 callback = new FetchStorageByUrlCallback(webapp2Url, webapp2Url);
718 WebappRegistry.getWebappDataStorageForUrl(Robolectric.application, test5 Url, callback); 731 WebappRegistry.getWebappDataStorageForUrl(
732 RuntimeEnvironment.application, test5Url, callback);
719 BackgroundShadowAsyncTask.runBackgroundTasks(); 733 BackgroundShadowAsyncTask.runBackgroundTasks();
720 Robolectric.runUiThreadTasks(); 734 ShadowLooper.runUiThreadTasks();
721 assertTrue(callback.getCallbackCalled()); 735 assertTrue(callback.getCallbackCalled());
722 736
723 // test6Url doesn't correspond to a web app, so the storage returned is null. 737 // test6Url doesn't correspond to a web app, so the storage returned is null.
724 // This must use a member variable; local variables must be final or eff ectively final to be 738 // This must use a member variable; local variables must be final or eff ectively final to be
725 // accessible inside an inner class. 739 // accessible inside an inner class.
726 mCallbackCalled = false; 740 mCallbackCalled = false;
727 WebappRegistry.getWebappDataStorageForUrl(Robolectric.application, test6 Url, 741 WebappRegistry.getWebappDataStorageForUrl(RuntimeEnvironment.application , test6Url,
728 new WebappRegistry.FetchWebappDataStorageCallback() { 742 new WebappRegistry.FetchWebappDataStorageCallback() {
729 @Override 743 @Override
730 public void onWebappDataStorageRetrieved(WebappDataStorage s torage) { 744 public void onWebappDataStorageRetrieved(WebappDataStorage s torage) {
731 assertEquals(null, storage); 745 assertEquals(null, storage);
732 mCallbackCalled = true; 746 mCallbackCalled = true;
733 } 747 }
734 } 748 }
735 ); 749 );
736 BackgroundShadowAsyncTask.runBackgroundTasks(); 750 BackgroundShadowAsyncTask.runBackgroundTasks();
737 Robolectric.runUiThreadTasks(); 751 ShadowLooper.runUiThreadTasks();
738 assertTrue(mCallbackCalled); 752 assertTrue(mCallbackCalled);
739 } 753 }
740 754
741 private Set<String> addWebappsToRegistry(String... webapps) { 755 private Set<String> addWebappsToRegistry(String... webapps) {
742 final Set<String> expected = new HashSet<>(Arrays.asList(webapps)); 756 final Set<String> expected = new HashSet<>(Arrays.asList(webapps));
743 mSharedPreferences.edit().putStringSet(WebappRegistry.KEY_WEBAPP_SET, ex pected).apply(); 757 mSharedPreferences.edit().putStringSet(WebappRegistry.KEY_WEBAPP_SET, ex pected).apply();
744 return expected; 758 return expected;
745 } 759 }
746 760
747 private Set<String> getRegisteredWebapps() { 761 private Set<String> getRegisteredWebapps() {
748 return mSharedPreferences.getStringSet( 762 return mSharedPreferences.getStringSet(
749 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); 763 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet());
750 } 764 }
751 765
752 private Intent createShortcutIntent(String url) { 766 private Intent createShortcutIntent(String url) {
753 return ShortcutHelper.createWebappShortcutIntent("id", "action", url, 767 return ShortcutHelper.createWebappShortcutIntent("id", "action", url,
754 ShortcutHelper.getScopeFromUrl(url), "name", "shortName", null, 768 ShortcutHelper.getScopeFromUrl(url), "name", "shortName", null,
755 ShortcutHelper.WEBAPP_SHORTCUT_VERSION, WebDisplayMode.Standalon e, 0, 0, 0, false); 769 ShortcutHelper.WEBAPP_SHORTCUT_VERSION, WebDisplayMode.Standalon e, 0, 0, 0, false);
756 } 770 }
757 771
758 private Intent createWebApkIntent(String webappId, String webApkPackage) { 772 private Intent createWebApkIntent(String webappId, String webApkPackage) {
759 Intent intent = new Intent(); 773 Intent intent = new Intent();
760 intent.putExtra(ShortcutHelper.EXTRA_ID, webappId) 774 intent.putExtra(ShortcutHelper.EXTRA_ID, webappId)
761 .putExtra(ShortcutHelper.EXTRA_URL, "https://foo.com") 775 .putExtra(ShortcutHelper.EXTRA_URL, "https://foo.com")
762 .putExtra(ShortcutHelper.EXTRA_WEBAPK_PACKAGE_NAME, webApkPackage) ; 776 .putExtra(ShortcutHelper.EXTRA_WEBAPK_PACKAGE_NAME, webApkPackage) ;
763 return intent; 777 return intent;
764 } 778 }
765 } 779 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698