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

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

Issue 1867543002: Enable deep-linking from notifications for recently used web apps on the Android home screen. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@bulk-webappdatastorage
Patch Set: Address nits Created 4 years, 8 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;
(...skipping 27 matching lines...) Expand all
38 // These were copied from WebappRegistry for backward compatibility checking . 38 // These were copied from WebappRegistry for backward compatibility checking .
39 private static final String REGISTRY_FILE_NAME = "webapp_registry"; 39 private static final String REGISTRY_FILE_NAME = "webapp_registry";
40 private static final String KEY_WEBAPP_SET = "webapp_set"; 40 private static final String KEY_WEBAPP_SET = "webapp_set";
41 private static final String KEY_LAST_CLEANUP = "last_cleanup"; 41 private static final String KEY_LAST_CLEANUP = "last_cleanup";
42 42
43 private static final int INITIAL_TIME = 0; 43 private static final int INITIAL_TIME = 0;
44 44
45 private SharedPreferences mSharedPreferences; 45 private SharedPreferences mSharedPreferences;
46 private boolean mCallbackCalled; 46 private boolean mCallbackCalled;
47 47
48 private class FetchCallback implements WebappRegistry.FetchCallback { 48 private static class FetchCallback implements WebappRegistry.FetchCallback {
49 boolean mCallbackCalled;
50
49 Set<String> mExpected; 51 Set<String> mExpected;
50 52
51 FetchCallback(Set<String> expected) { 53 FetchCallback(Set<String> expected) {
54 mCallbackCalled = false;
52 mExpected = expected; 55 mExpected = expected;
53 } 56 }
54 57
55 @Override 58 @Override
56 public void onWebappIdsRetrieved(Set<String> actual) { 59 public void onWebappIdsRetrieved(Set<String> actual) {
57 mCallbackCalled = true; 60 mCallbackCalled = true;
58 assertEquals(mExpected, actual); 61 assertEquals(mExpected, actual);
59 } 62 }
63
64 boolean getCallbackCalled() {
65 return mCallbackCalled;
66 }
60 } 67 }
61 68
62 private class FetchStorageCallback implements WebappRegistry.FetchWebappData StorageCallback { 69 private static class FetchStorageCallback
70 implements WebappRegistry.FetchWebappDataStorageCallback {
63 Intent mShortcutIntent; 71 Intent mShortcutIntent;
72 boolean mMarkLaunched;
73 boolean mCallbackCalled;
64 74
65 FetchStorageCallback(Intent shortcutIntent) { 75 FetchStorageCallback(Intent shortcutIntent, boolean markLaunched) {
66 mCallbackCalled = false; 76 mCallbackCalled = false;
67 mShortcutIntent = shortcutIntent; 77 mShortcutIntent = shortcutIntent;
78 mMarkLaunched = markLaunched;
68 } 79 }
69 80
70 @Override 81 @Override
71 public void onWebappDataStorageRetrieved(WebappDataStorage storage) { 82 public void onWebappDataStorageRetrieved(WebappDataStorage storage) {
72 mCallbackCalled = true; 83 mCallbackCalled = true;
73 storage.updateFromShortcutIntent(mShortcutIntent); 84 storage.updateFromShortcutIntent(mShortcutIntent);
85 storage.updateLastUsedTime();
86 if (mMarkLaunched) storage.setLaunched();
87 }
88
89 boolean getCallbackCalled() {
90 return mCallbackCalled;
74 } 91 }
75 } 92 }
76 93
77 private class CallbackRunner implements Runnable { 94 private static class FetchStorageByUrlCallback
95 implements WebappRegistry.FetchWebappDataStorageCallback {
96 String mUrl;
97 String mScope;
98 boolean mCallbackCalled;
99
100 FetchStorageByUrlCallback(String url, String scope) {
101 mCallbackCalled = false;
102 mUrl = url;
103 mScope = scope;
104 }
105
106 @Override
107 public void onWebappDataStorageRetrieved(WebappDataStorage storage) {
108 mCallbackCalled = true;
109 assertEquals(mUrl, storage.getUrl());
110 assertEquals(mScope, storage.getScope());
111 }
112
113 boolean getCallbackCalled() {
114 return mCallbackCalled;
115 }
116 }
117
118 private static class CallbackRunner implements Runnable {
119 boolean mCallbackCalled;
120
78 public CallbackRunner() { 121 public CallbackRunner() {
79 mCallbackCalled = false; 122 mCallbackCalled = false;
80 } 123 }
81 124
82 @Override 125 @Override
83 public void run() { 126 public void run() {
84 mCallbackCalled = true; 127 mCallbackCalled = true;
85 } 128 }
129
130 boolean getCallbackCalled() {
131 return mCallbackCalled;
132 }
86 } 133 }
87 134
88 @Before 135 @Before
89 public void setUp() throws Exception { 136 public void setUp() throws Exception {
90 mSharedPreferences = Robolectric.application 137 mSharedPreferences = Robolectric.application
91 .getSharedPreferences(REGISTRY_FILE_NAME, Context.MODE_PRIVATE); 138 .getSharedPreferences(REGISTRY_FILE_NAME, Context.MODE_PRIVATE);
92 mSharedPreferences.edit().putLong(KEY_LAST_CLEANUP, INITIAL_TIME).commit (); 139 mSharedPreferences.edit().putLong(KEY_LAST_CLEANUP, INITIAL_TIME).commit ();
93 140
94 mCallbackCalled = false; 141 mCallbackCalled = false;
95 } 142 }
(...skipping 13 matching lines...) Expand all
109 BackgroundShadowAsyncTask.runBackgroundTasks(); 156 BackgroundShadowAsyncTask.runBackgroundTasks();
110 157
111 Set<String> actual = mSharedPreferences.getStringSet( 158 Set<String> actual = mSharedPreferences.getStringSet(
112 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); 159 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet());
113 assertEquals(1, actual.size()); 160 assertEquals(1, actual.size());
114 assertTrue(actual.contains("test")); 161 assertTrue(actual.contains("test"));
115 } 162 }
116 163
117 @Test 164 @Test
118 @Feature({"Webapp"}) 165 @Feature({"Webapp"})
119 public void testWebappRegistrationUpdatesLastUsed() throws Exception { 166 public void testWebappRegistrationUpdatesLastUsedAndDoesNotMarkLaunched() th rows Exception {
120 long before = System.currentTimeMillis();
121
122 WebappRegistry.registerWebapp(Robolectric.application, "test", null); 167 WebappRegistry.registerWebapp(Robolectric.application, "test", null);
123 BackgroundShadowAsyncTask.runBackgroundTasks(); 168 BackgroundShadowAsyncTask.runBackgroundTasks();
124 long after = System.currentTimeMillis(); 169 long after = System.currentTimeMillis();
125 170
126 SharedPreferences webAppPrefs = Robolectric.application.getSharedPrefere nces( 171 SharedPreferences webAppPrefs = Robolectric.application.getSharedPrefere nces(
127 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "test", Context.MOD E_PRIVATE); 172 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "test", Context.MOD E_PRIVATE);
128 long actual = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USED, 173 long actual = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USED,
129 WebappDataStorage.LAST_USED_INVALID); 174 WebappDataStorage.LAST_USED_INVALID);
130 assertTrue("Timestamp is out of range", before <= actual && actual <= af ter); 175 assertTrue("Timestamp is out of range", actual <= after);
176
177 assertTrue(!webAppPrefs.getBoolean(WebappDataStorage.KEY_LAUNCHED, false ));
131 } 178 }
132 179
133 @Test 180 @Test
134 @Feature({"Webapp"}) 181 @Feature({"Webapp"})
135 public void testWebappIdsRetrieval() throws Exception { 182 public void testWebappIdsRetrieval() throws Exception {
136 final Set<String> expected = addWebappsToRegistry("first", "second"); 183 final Set<String> expected = addWebappsToRegistry("first", "second");
137 184
138 WebappRegistry.getRegisteredWebappIds(Robolectric.application, new Fetch Callback(expected)); 185 FetchCallback callback = new FetchCallback(expected);
186 WebappRegistry.getRegisteredWebappIds(Robolectric.application, callback) ;
139 BackgroundShadowAsyncTask.runBackgroundTasks(); 187 BackgroundShadowAsyncTask.runBackgroundTasks();
140 Robolectric.runUiThreadTasks(); 188 Robolectric.runUiThreadTasks();
141 189
142 assertTrue(mCallbackCalled); 190 assertTrue(callback.getCallbackCalled());
143 } 191 }
144 192
145 @Test 193 @Test
146 @Feature({"Webapp"}) 194 @Feature({"Webapp"})
147 public void testWebappIdsRetrievalRegisterRetrival() throws Exception { 195 public void testWebappIdsRetrievalRegisterRetrival() throws Exception {
148 final Set<String> expected = addWebappsToRegistry("first"); 196 final Set<String> expected = addWebappsToRegistry("first");
149 197
150 WebappRegistry.getRegisteredWebappIds(Robolectric.application, new Fetch Callback(expected)); 198 FetchCallback callback = new FetchCallback(expected);
199 WebappRegistry.getRegisteredWebappIds(Robolectric.application, callback) ;
151 BackgroundShadowAsyncTask.runBackgroundTasks(); 200 BackgroundShadowAsyncTask.runBackgroundTasks();
152 Robolectric.runUiThreadTasks(); 201 Robolectric.runUiThreadTasks();
153 202
154 assertTrue(mCallbackCalled); 203 assertTrue(callback.getCallbackCalled());
155 mCallbackCalled = false;
156 204
157 WebappRegistry.registerWebapp(Robolectric.application, "second", null); 205 WebappRegistry.registerWebapp(Robolectric.application, "second", null);
158 BackgroundShadowAsyncTask.runBackgroundTasks(); 206 BackgroundShadowAsyncTask.runBackgroundTasks();
159 207
160 // A copy of the expected set needs to be made as the SharedPreferences is using the copy 208 // A copy of the expected set needs to be made as the SharedPreferences is using the copy
161 // that was paassed to it. 209 // that was paassed to it.
162 final Set<String> secondExpected = new HashSet<String>(expected); 210 final Set<String> secondExpected = new HashSet<String>(expected);
163 secondExpected.add("second"); 211 secondExpected.add("second");
164 212
165 WebappRegistry.getRegisteredWebappIds(Robolectric.application, 213 callback = new FetchCallback(secondExpected);
166 new FetchCallback(secondExpected)); 214 WebappRegistry.getRegisteredWebappIds(Robolectric.application, callback) ;
167 BackgroundShadowAsyncTask.runBackgroundTasks(); 215 BackgroundShadowAsyncTask.runBackgroundTasks();
168 Robolectric.runUiThreadTasks(); 216 Robolectric.runUiThreadTasks();
169 217
170 assertTrue(mCallbackCalled); 218 assertTrue(callback.getCallbackCalled());
171 } 219 }
172 220
173 @Test 221 @Test
174 @Feature({"Webapp"}) 222 @Feature({"Webapp"})
175 public void testUnregisterRunsCallback() throws Exception { 223 public void testUnregisterRunsCallback() throws Exception {
176 WebappRegistry.unregisterAllWebapps(Robolectric.application, new Callbac kRunner()); 224 CallbackRunner callback = new CallbackRunner();
225 WebappRegistry.unregisterAllWebapps(Robolectric.application, callback);
177 BackgroundShadowAsyncTask.runBackgroundTasks(); 226 BackgroundShadowAsyncTask.runBackgroundTasks();
178 Robolectric.runUiThreadTasks(); 227 Robolectric.runUiThreadTasks();
179 228
180 assertTrue(mCallbackCalled); 229 assertTrue(callback.getCallbackCalled());
181 } 230 }
182 231
183 @Test 232 @Test
184 @Feature({"Webapp"}) 233 @Feature({"Webapp"})
185 public void testUnregisterClearsRegistry() throws Exception { 234 public void testUnregisterClearsRegistry() throws Exception {
186 addWebappsToRegistry("test"); 235 addWebappsToRegistry("test");
187 236
188 WebappRegistry.unregisterAllWebapps(Robolectric.application, new Callbac kRunner()); 237 CallbackRunner callback = new CallbackRunner();
238 WebappRegistry.unregisterAllWebapps(Robolectric.application, callback);
189 BackgroundShadowAsyncTask.runBackgroundTasks(); 239 BackgroundShadowAsyncTask.runBackgroundTasks();
190 Robolectric.runUiThreadTasks(); 240 Robolectric.runUiThreadTasks();
191 241
192 assertTrue(mCallbackCalled); 242 assertTrue(callback.getCallbackCalled());
193 assertTrue(getRegisteredWebapps().isEmpty()); 243 assertTrue(getRegisteredWebapps().isEmpty());
194 } 244 }
195 245
196 @Test 246 @Test
197 @Feature({"Webapp"}) 247 @Feature({"Webapp"})
198 public void testUnregisterClearsWebappDataStorage() throws Exception { 248 public void testUnregisterClearsWebappDataStorage() throws Exception {
199 addWebappsToRegistry("test"); 249 addWebappsToRegistry("test");
200 SharedPreferences webAppPrefs = Robolectric.application.getSharedPrefere nces( 250 SharedPreferences webAppPrefs = Robolectric.application.getSharedPrefere nces(
201 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "test", Context.MOD E_PRIVATE); 251 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "test", Context.MOD E_PRIVATE);
202 webAppPrefs.edit() 252 webAppPrefs.edit()
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
300 WebappDataStorage.LAST_USED_INVALID); 350 WebappDataStorage.LAST_USED_INVALID);
301 assertEquals(WebappDataStorage.LAST_USED_INVALID, actualLastUsed); 351 assertEquals(WebappDataStorage.LAST_USED_INVALID, actualLastUsed);
302 352
303 long lastCleanup = mSharedPreferences.getLong(WebappRegistry.KEY_LAST_CL EANUP, -1); 353 long lastCleanup = mSharedPreferences.getLong(WebappRegistry.KEY_LAST_CL EANUP, -1);
304 assertEquals(currentTime, lastCleanup); 354 assertEquals(currentTime, lastCleanup);
305 } 355 }
306 356
307 @Test 357 @Test
308 @Feature({"Webapp"}) 358 @Feature({"Webapp"})
309 public void testClearWebappHistoryRunsCallback() throws Exception { 359 public void testClearWebappHistoryRunsCallback() throws Exception {
310 WebappRegistry.clearWebappHistory(Robolectric.application, new CallbackR unner()); 360 CallbackRunner callback = new CallbackRunner();
361 WebappRegistry.clearWebappHistory(Robolectric.application, callback);
311 BackgroundShadowAsyncTask.runBackgroundTasks(); 362 BackgroundShadowAsyncTask.runBackgroundTasks();
312 Robolectric.runUiThreadTasks(); 363 Robolectric.runUiThreadTasks();
313 364
314 assertTrue(mCallbackCalled); 365 assertTrue(callback.getCallbackCalled());
315 } 366 }
316 367
317 @Test 368 @Test
318 @Feature({"Webapp"}) 369 @Feature({"Webapp"})
319 public void testClearWebappHistory() throws Exception { 370 public void testClearWebappHistory() throws Exception {
320 final String webapp1Url = "https://www.google.com"; 371 final String webapp1Url = "https://www.google.com";
321 final String webapp2Url = "https://drive.google.com"; 372 final String webapp2Url = "https://drive.google.com";
322 Intent shortcutIntent1 = createShortcutIntent(webapp1Url); 373 Intent shortcutIntent1 = createShortcutIntent(webapp1Url);
323 Intent shortcutIntent2 = createShortcutIntent(webapp2Url); 374 Intent shortcutIntent2 = createShortcutIntent(webapp2Url);
324 375
325 WebappRegistry.registerWebapp(Robolectric.application, "webapp1", 376 FetchStorageCallback storageCallback1 = new FetchStorageCallback(shortcu tIntent1, true);
326 new FetchStorageCallback(shortcutIntent1)); 377 WebappRegistry.registerWebapp(Robolectric.application, "webapp1", storag eCallback1);
327 BackgroundShadowAsyncTask.runBackgroundTasks(); 378 BackgroundShadowAsyncTask.runBackgroundTasks();
328 Robolectric.runUiThreadTasks(); 379 Robolectric.runUiThreadTasks();
329 assertTrue(mCallbackCalled); 380 assertTrue(storageCallback1.getCallbackCalled());
330 381
331 WebappRegistry.registerWebapp(Robolectric.application, "webapp2", 382 FetchStorageCallback storageCallback2 = new FetchStorageCallback(shortcu tIntent2, false);
332 new FetchStorageCallback(shortcutIntent2)); 383 WebappRegistry.registerWebapp(Robolectric.application, "webapp2", storag eCallback2);
333 BackgroundShadowAsyncTask.runBackgroundTasks(); 384 BackgroundShadowAsyncTask.runBackgroundTasks();
334 Robolectric.runUiThreadTasks(); 385 Robolectric.runUiThreadTasks();
335 assertTrue(mCallbackCalled); 386 assertTrue(storageCallback2.getCallbackCalled());
336 387
337 SharedPreferences webapp1Prefs = Robolectric.application.getSharedPrefer ences( 388 SharedPreferences webapp1Prefs = Robolectric.application.getSharedPrefer ences(
338 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "webapp1", Context. MODE_PRIVATE); 389 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "webapp1", Context. MODE_PRIVATE);
339 SharedPreferences webapp2Prefs = Robolectric.application.getSharedPrefer ences( 390 SharedPreferences webapp2Prefs = Robolectric.application.getSharedPrefer ences(
340 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "webapp2", Context. MODE_PRIVATE); 391 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "webapp2", Context. MODE_PRIVATE);
341 392
342 WebappRegistry.clearWebappHistory(Robolectric.application, new CallbackR unner()); 393 CallbackRunner callback = new CallbackRunner();
394 WebappRegistry.clearWebappHistory(Robolectric.application, callback);
343 BackgroundShadowAsyncTask.runBackgroundTasks(); 395 BackgroundShadowAsyncTask.runBackgroundTasks();
344 Robolectric.runUiThreadTasks(); 396 Robolectric.runUiThreadTasks();
345 assertTrue(mCallbackCalled); 397 assertTrue(callback.getCallbackCalled());
346 398
347 Set<String> actual = mSharedPreferences.getStringSet( 399 Set<String> actual = mSharedPreferences.getStringSet(
348 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); 400 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet());
349 assertEquals(2, actual.size()); 401 assertEquals(2, actual.size());
350 assertTrue(actual.contains("webapp1")); 402 assertTrue(actual.contains("webapp1"));
351 assertTrue(actual.contains("webapp2")); 403 assertTrue(actual.contains("webapp2"));
352 404
353 // Verify that the last used time for both web apps is WebappDataStorage .LAST_USED_UNSET. 405 // Verify that the last used time for both web apps is WebappDataStorage .LAST_USED_UNSET.
354 long actualLastUsed = webapp1Prefs.getLong( 406 long actualLastUsed = webapp1Prefs.getLong(
355 WebappDataStorage.KEY_LAST_USED, WebappDataStorage.LAST_USED_UNS ET); 407 WebappDataStorage.KEY_LAST_USED, WebappDataStorage.LAST_USED_UNS ET);
356 assertEquals(WebappDataStorage.LAST_USED_UNSET, actualLastUsed); 408 assertEquals(WebappDataStorage.LAST_USED_UNSET, actualLastUsed);
357 actualLastUsed = webapp2Prefs.getLong( 409 actualLastUsed = webapp2Prefs.getLong(
358 WebappDataStorage.KEY_LAST_USED, WebappDataStorage.LAST_USED_UNS ET); 410 WebappDataStorage.KEY_LAST_USED, WebappDataStorage.LAST_USED_UNS ET);
359 assertEquals(WebappDataStorage.LAST_USED_UNSET, actualLastUsed); 411 assertEquals(WebappDataStorage.LAST_USED_UNSET, actualLastUsed);
360 412
413 // Verify that neither web app is marked as launched.
414 assertTrue(!webapp1Prefs.getBoolean(WebappDataStorage.KEY_LAUNCHED, fals e));
415 assertTrue(!webapp2Prefs.getBoolean(WebappDataStorage.KEY_LAUNCHED, fals e));
416
361 // Verify that the URL and scope for both web apps is WebappDataStorage. URL_INVALID. 417 // Verify that the URL and scope for both web apps is WebappDataStorage. URL_INVALID.
362 String actualScope = webapp1Prefs.getString( 418 String actualScope = webapp1Prefs.getString(
363 WebappDataStorage.KEY_SCOPE, WebappDataStorage.URL_INVALID); 419 WebappDataStorage.KEY_SCOPE, WebappDataStorage.URL_INVALID);
364 assertEquals(WebappDataStorage.URL_INVALID, actualScope); 420 assertEquals(WebappDataStorage.URL_INVALID, actualScope);
365 String actualUrl = webapp1Prefs.getString( 421 String actualUrl = webapp1Prefs.getString(
366 WebappDataStorage.KEY_URL, WebappDataStorage.URL_INVALID); 422 WebappDataStorage.KEY_URL, WebappDataStorage.URL_INVALID);
367 assertEquals(WebappDataStorage.URL_INVALID, actualUrl); 423 assertEquals(WebappDataStorage.URL_INVALID, actualUrl);
368 actualScope = webapp2Prefs.getString( 424 actualScope = webapp2Prefs.getString(
369 WebappDataStorage.KEY_SCOPE, WebappDataStorage.URL_INVALID); 425 WebappDataStorage.KEY_SCOPE, WebappDataStorage.URL_INVALID);
370 assertEquals(WebappDataStorage.URL_INVALID, actualScope); 426 assertEquals(WebappDataStorage.URL_INVALID, actualScope);
371 actualUrl = webapp2Prefs.getString( 427 actualUrl = webapp2Prefs.getString(
372 WebappDataStorage.KEY_URL, WebappDataStorage.URL_INVALID); 428 WebappDataStorage.KEY_URL, WebappDataStorage.URL_INVALID);
373 assertEquals(WebappDataStorage.URL_INVALID, actualUrl); 429 assertEquals(WebappDataStorage.URL_INVALID, actualUrl);
374 } 430 }
375 431
376 @Test 432 @Test
377 @Feature({"Webapp"}) 433 @Feature({"Webapp"})
378 public void testGetAfterClearWebappHistory() throws Exception { 434 public void testGetAfterClearWebappHistory() throws Exception {
379 WebappRegistry.registerWebapp(Robolectric.application, "webapp", null); 435 WebappRegistry.registerWebapp(Robolectric.application, "webapp", null);
380 BackgroundShadowAsyncTask.runBackgroundTasks(); 436 BackgroundShadowAsyncTask.runBackgroundTasks();
381 437
382 SharedPreferences webappPrefs = Robolectric.application.getSharedPrefere nces( 438 SharedPreferences webappPrefs = Robolectric.application.getSharedPrefere nces(
383 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "webapp", Context.M ODE_PRIVATE); 439 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "webapp", Context.M ODE_PRIVATE);
384 WebappRegistry.clearWebappHistory(Robolectric.application, new CallbackR unner()); 440 CallbackRunner callback = new CallbackRunner();
441 WebappRegistry.clearWebappHistory(Robolectric.application, callback);
385 BackgroundShadowAsyncTask.runBackgroundTasks(); 442 BackgroundShadowAsyncTask.runBackgroundTasks();
386 Robolectric.runUiThreadTasks(); 443 Robolectric.runUiThreadTasks();
387 assertTrue(mCallbackCalled); 444 assertTrue(callback.getCallbackCalled());
388 445
389 // Open the webapp up to set the last used time. 446 // Open the webapp up to set the last used time and launched.
390 WebappRegistry.getWebappDataStorage(Robolectric.application, "webapp", 447 FetchStorageCallback storageCallback = new FetchStorageCallback(null, tr ue);
391 new FetchStorageCallback(null)); 448 WebappRegistry.getWebappDataStorage(Robolectric.application, "webapp", s torageCallback);
392 BackgroundShadowAsyncTask.runBackgroundTasks(); 449 BackgroundShadowAsyncTask.runBackgroundTasks();
393 Robolectric.runUiThreadTasks(); 450 Robolectric.runUiThreadTasks();
394 assertTrue(mCallbackCalled); 451 assertTrue(storageCallback.getCallbackCalled());
395 452
396 // Verify that the last used time is valid. 453 // Verify that the last used time is valid.
397 long actualLastUsed = webappPrefs.getLong( 454 long actualLastUsed = webappPrefs.getLong(
398 WebappDataStorage.KEY_LAST_USED, WebappDataStorage.LAST_USED_INV ALID); 455 WebappDataStorage.KEY_LAST_USED, WebappDataStorage.LAST_USED_INV ALID);
399 assertTrue(WebappDataStorage.LAST_USED_INVALID != actualLastUsed); 456 assertTrue(WebappDataStorage.LAST_USED_INVALID != actualLastUsed);
400 assertTrue(WebappDataStorage.LAST_USED_UNSET != actualLastUsed); 457 assertTrue(WebappDataStorage.LAST_USED_UNSET != actualLastUsed);
458
459 // Verify that the app is marked as launched.
460 assertTrue(webappPrefs.getBoolean(WebappDataStorage.KEY_LAUNCHED, false) );
401 } 461 }
402 462
403 @Test 463 @Test
404 @Feature({"Webapp"}) 464 @Feature({"Webapp"})
405 public void testUpdateAfterClearWebappHistory() throws Exception { 465 public void testUpdateAfterClearWebappHistory() throws Exception {
406 final String webappUrl = "http://www.google.com"; 466 final String webappUrl = "http://www.google.com";
467 final String webappScope = "http://www.google.com/";
407 final Intent shortcutIntent = createShortcutIntent(webappUrl); 468 final Intent shortcutIntent = createShortcutIntent(webappUrl);
408 WebappRegistry.registerWebapp(Robolectric.application, "webapp", 469 WebappRegistry.registerWebapp(Robolectric.application, "webapp",
409 new FetchStorageCallback(shortcutIntent)); 470 new FetchStorageCallback(shortcutIntent, false));
410 BackgroundShadowAsyncTask.runBackgroundTasks(); 471 BackgroundShadowAsyncTask.runBackgroundTasks();
411 Robolectric.runUiThreadTasks(); 472 Robolectric.runUiThreadTasks();
412 assertTrue(mCallbackCalled);
413 473
414 SharedPreferences webappPrefs = Robolectric.application.getSharedPrefere nces( 474 SharedPreferences webappPrefs = Robolectric.application.getSharedPrefere nces(
415 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "webapp", Context.M ODE_PRIVATE); 475 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "webapp", Context.M ODE_PRIVATE);
416 476
477 // Verify that the app is not marked as launched.
478 assertTrue(!webappPrefs.getBoolean(WebappDataStorage.KEY_LAUNCHED, false ));
479
417 // Verify that the URL and scope match the original in the intent. 480 // Verify that the URL and scope match the original in the intent.
418 String actualURL = webappPrefs.getString( 481 String actualUrl = webappPrefs.getString(
419 WebappDataStorage.KEY_URL, WebappDataStorage.URL_INVALID); 482 WebappDataStorage.KEY_URL, WebappDataStorage.URL_INVALID);
420 assertEquals(webappUrl, actualURL); 483 assertEquals(webappUrl, actualUrl);
421 String actualScope = webappPrefs.getString( 484 String actualScope = webappPrefs.getString(
422 WebappDataStorage.KEY_SCOPE, WebappDataStorage.URL_INVALID); 485 WebappDataStorage.KEY_SCOPE, WebappDataStorage.URL_INVALID);
423 assertEquals(webappUrl, actualScope); 486 assertEquals(webappScope, actualScope);
424 487
425 WebappRegistry.clearWebappHistory(Robolectric.application, new CallbackR unner()); 488 WebappRegistry.clearWebappHistory(Robolectric.application, new CallbackR unner());
426 BackgroundShadowAsyncTask.runBackgroundTasks(); 489 BackgroundShadowAsyncTask.runBackgroundTasks();
427 Robolectric.runUiThreadTasks(); 490 Robolectric.runUiThreadTasks();
428 assertTrue(mCallbackCalled);
429 491
430 // Update the webapp from the intent again. 492 // Update the webapp from the intent again.
431 WebappRegistry.getWebappDataStorage(Robolectric.application, "webapp", 493 WebappRegistry.getWebappDataStorage(Robolectric.application, "webapp",
432 new FetchStorageCallback(shortcutIntent)); 494 new FetchStorageCallback(shortcutIntent, true));
495 BackgroundShadowAsyncTask.runBackgroundTasks();
496 Robolectric.runUiThreadTasks();
497
498 // Verify that the app is marked as launched.
499 assertTrue(webappPrefs.getBoolean(WebappDataStorage.KEY_LAUNCHED, false) );
500
501 // Verify that the URL and scope match the original in the intent.
502 actualUrl = webappPrefs.getString(WebappDataStorage.KEY_URL, WebappDataS torage.URL_INVALID);
503 assertEquals(webappUrl, actualUrl);
504 actualScope = webappPrefs.getString(
505 WebappDataStorage.KEY_SCOPE, WebappDataStorage.URL_INVALID);
506 assertEquals(webappScope, actualScope);
507 }
508
509 @Test
510 @Feature({"Webapp"})
511 public void testGetWebappDataStorageForUrl() throws Exception {
512 // Ensure that getWebappDataStorageForUrl returns the correct WebappData Storage object.
513 // URLs should return the WebappDataStorage with the longest scope that the URL starts with.
514 final String webapp1Url = "https://www.google.com/";
515 final String webapp2Url = "https://drive.google.com/";
516 final String webapp3Url = "https://www.google.com/drive/index.html";
517 final String webapp4Url = "https://www.google.com/drive/docs/index.html" ;
518
519 final String webapp3Scope = "https://www.google.com/drive/";
520 final String webapp4Scope = "https://www.google.com/drive/docs/";
521
522 final String test1Url = "https://www.google.com/index.html";
523 final String test2Url = "https://www.google.com/drive/recent.html";
524 final String test3Url = "https://www.google.com/drive/docs/recent.html";
525 final String test4Url = "https://www.google.com/drive/docs/recent/index. html";
526 final String test5Url = "https://drive.google.com/docs/recent/trash";
527 final String test6Url = "https://maps.google.com/";
528
529 Intent shortcutIntent1 = createShortcutIntent(webapp1Url);
530 Intent shortcutIntent2 = createShortcutIntent(webapp2Url);
531 Intent shortcutIntent3 = createShortcutIntent(webapp3Url);
532 Intent shortcutIntent4 = createShortcutIntent(webapp4Url);
533
534 // Register the four web apps.
535 WebappRegistry.registerWebapp(Robolectric.application, "webapp1",
536 new FetchStorageCallback(shortcutIntent1, true));
537 BackgroundShadowAsyncTask.runBackgroundTasks();
538 Robolectric.runUiThreadTasks();
539
540 WebappRegistry.registerWebapp(Robolectric.application, "webapp2",
541 new FetchStorageCallback(shortcutIntent2, false));
542 BackgroundShadowAsyncTask.runBackgroundTasks();
543 Robolectric.runUiThreadTasks();
544
545 WebappRegistry.registerWebapp(Robolectric.application, "webapp3",
546 new FetchStorageCallback(shortcutIntent3, true));
547 BackgroundShadowAsyncTask.runBackgroundTasks();
548 Robolectric.runUiThreadTasks();
549
550 WebappRegistry.registerWebapp(Robolectric.application, "webapp4",
551 new FetchStorageCallback(shortcutIntent4, false));
552 BackgroundShadowAsyncTask.runBackgroundTasks();
553 Robolectric.runUiThreadTasks();
554
555 // test1Url should return webapp1.
556 FetchStorageByUrlCallback callback = new FetchStorageByUrlCallback(webap p1Url, webapp1Url);
557 WebappRegistry.getWebappDataStorageForUrl(Robolectric.application, test1 Url, callback);
558 BackgroundShadowAsyncTask.runBackgroundTasks();
559 Robolectric.runUiThreadTasks();
560 assertTrue(callback.getCallbackCalled());
561
562 // test2Url should return webapp3.
563 callback = new FetchStorageByUrlCallback(webapp3Url, webapp3Scope);
564 WebappRegistry.getWebappDataStorageForUrl(Robolectric.application, test2 Url, callback);
565 BackgroundShadowAsyncTask.runBackgroundTasks();
566 Robolectric.runUiThreadTasks();
567 assertTrue(callback.getCallbackCalled());
568
569 // test3Url should return webapp4.
570 callback = new FetchStorageByUrlCallback(webapp4Url, webapp4Scope);
571 WebappRegistry.getWebappDataStorageForUrl(Robolectric.application, test3 Url, callback);
572 BackgroundShadowAsyncTask.runBackgroundTasks();
573 Robolectric.runUiThreadTasks();
574 assertTrue(callback.getCallbackCalled());
575
576 // test4Url should return webapp4.
577 callback = new FetchStorageByUrlCallback(webapp4Url, webapp4Scope);
578 WebappRegistry.getWebappDataStorageForUrl(Robolectric.application, test4 Url, callback);
579 BackgroundShadowAsyncTask.runBackgroundTasks();
580 Robolectric.runUiThreadTasks();
581 assertTrue(callback.getCallbackCalled());
582
583 // test5Url should return webapp2.
584 callback = new FetchStorageByUrlCallback(webapp2Url, webapp2Url);
585 WebappRegistry.getWebappDataStorageForUrl(Robolectric.application, test5 Url, callback);
586 BackgroundShadowAsyncTask.runBackgroundTasks();
587 Robolectric.runUiThreadTasks();
588 assertTrue(callback.getCallbackCalled());
589
590 // test6Url doesn't correspond to a web app, so the storage returned is null.
591 // This must use a member variable; local variables must be final or eff ectively final to be
592 // accessible inside an inner class.
593 mCallbackCalled = false;
594 WebappRegistry.getWebappDataStorageForUrl(Robolectric.application, test6 Url,
595 new WebappRegistry.FetchWebappDataStorageCallback() {
596 @Override
597 public void onWebappDataStorageRetrieved(WebappDataStorage s torage) {
598 assertEquals(null, storage);
599 mCallbackCalled = true;
600 }
601 }
602 );
433 BackgroundShadowAsyncTask.runBackgroundTasks(); 603 BackgroundShadowAsyncTask.runBackgroundTasks();
434 Robolectric.runUiThreadTasks(); 604 Robolectric.runUiThreadTasks();
435 assertTrue(mCallbackCalled); 605 assertTrue(mCallbackCalled);
436
437 // Verify that the URL and scope match the original in the intent.
438 actualURL = webappPrefs.getString(
439 WebappDataStorage.KEY_URL, WebappDataStorage.URL_INVALID);
440 assertEquals(webappUrl, actualURL);
441 actualScope = webappPrefs.getString(
442 WebappDataStorage.KEY_SCOPE, WebappDataStorage.URL_INVALID);
443 assertEquals(webappUrl, actualScope);
444 } 606 }
445 607
446 private Set<String> addWebappsToRegistry(String... webapps) { 608 private Set<String> addWebappsToRegistry(String... webapps) {
447 final Set<String> expected = new HashSet<String>(Arrays.asList(webapps)) ; 609 final Set<String> expected = new HashSet<String>(Arrays.asList(webapps)) ;
448 mSharedPreferences.edit() 610 mSharedPreferences.edit()
449 .putStringSet(WebappRegistry.KEY_WEBAPP_SET, expected) 611 .putStringSet(WebappRegistry.KEY_WEBAPP_SET, expected)
450 .commit(); 612 .commit();
451 return expected; 613 return expected;
452 } 614 }
453 615
454 private Set<String> getRegisteredWebapps() { 616 private Set<String> getRegisteredWebapps() {
455 return mSharedPreferences.getStringSet( 617 return mSharedPreferences.getStringSet(
456 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); 618 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet());
457 } 619 }
458 620
459 private Intent createShortcutIntent(String url) { 621 private Intent createShortcutIntent(String url) {
460 return ShortcutHelper.createWebappShortcutIntent("id", "action", url, ur l, "name", 622 return ShortcutHelper.createWebappShortcutIntent("id", "action", url,
461 "shortName", null, ShortcutHelper.WEBAPP_SHORTCUT_VERSION, 0, 0, 0, false); 623 ShortcutHelper.getScopeFromUrl(url), "name", "shortName", null,
624 ShortcutHelper.WEBAPP_SHORTCUT_VERSION, 0, 0, 0, false);
462 } 625 }
463 } 626 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698