| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chrome.browser.webapps; | 5 package org.chromium.chrome.browser.webapps; |
| 6 | 6 |
| 7 import static org.junit.Assert.assertEquals; | 7 import static org.junit.Assert.assertEquals; |
| 8 import static org.junit.Assert.assertTrue; | 8 import static org.junit.Assert.assertTrue; |
| 9 | 9 |
| 10 import android.content.Context; | 10 import android.content.Context; |
| (...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 assertTrue(callback.getCallbackCalled()); | 238 assertTrue(callback.getCallbackCalled()); |
| 239 assertTrue(getRegisteredWebapps().isEmpty()); | 239 assertTrue(getRegisteredWebapps().isEmpty()); |
| 240 } | 240 } |
| 241 | 241 |
| 242 @Test | 242 @Test |
| 243 @Feature({"Webapp"}) | 243 @Feature({"Webapp"}) |
| 244 public void testUnregisterClearsWebappDataStorage() throws Exception { | 244 public void testUnregisterClearsWebappDataStorage() throws Exception { |
| 245 addWebappsToRegistry("test"); | 245 addWebappsToRegistry("test"); |
| 246 SharedPreferences webAppPrefs = Robolectric.application.getSharedPrefere
nces( | 246 SharedPreferences webAppPrefs = Robolectric.application.getSharedPrefere
nces( |
| 247 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "test", Context.MOD
E_PRIVATE); | 247 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "test", Context.MOD
E_PRIVATE); |
| 248 webAppPrefs.edit() | 248 webAppPrefs.edit().putLong(WebappDataStorage.KEY_LAST_USED, 100L).apply(
); |
| 249 .putLong(WebappDataStorage.KEY_LAST_USED, 100L) | |
| 250 .commit(); | |
| 251 | 249 |
| 252 WebappRegistry.unregisterAllWebapps(Robolectric.application, null); | 250 WebappRegistry.unregisterAllWebapps(Robolectric.application, null); |
| 253 BackgroundShadowAsyncTask.runBackgroundTasks(); | 251 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 254 | 252 |
| 255 Map<String, ?> actual = webAppPrefs.getAll(); | 253 Map<String, ?> actual = webAppPrefs.getAll(); |
| 256 assertTrue(actual.isEmpty()); | 254 assertTrue(actual.isEmpty()); |
| 257 } | 255 } |
| 258 | 256 |
| 259 @Test | 257 @Test |
| 260 @Feature({"Webapp"}) | 258 @Feature({"Webapp"}) |
| 261 public void testCleanupDoesNotRunTooOften() throws Exception { | 259 public void testCleanupDoesNotRunTooOften() throws Exception { |
| 262 // Put the current time to just before the task should run. | 260 // Put the current time to just before the task should run. |
| 263 long currentTime = INITIAL_TIME + WebappRegistry.FULL_CLEANUP_DURATION -
1; | 261 long currentTime = INITIAL_TIME + WebappRegistry.FULL_CLEANUP_DURATION -
1; |
| 264 | 262 |
| 265 addWebappsToRegistry("oldWebapp"); | 263 addWebappsToRegistry("oldWebapp"); |
| 266 SharedPreferences webAppPrefs = Robolectric.application.getSharedPrefere
nces( | 264 SharedPreferences webAppPrefs = Robolectric.application.getSharedPrefere
nces( |
| 267 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "oldWebapp", Contex
t.MODE_PRIVATE); | 265 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "oldWebapp", Contex
t.MODE_PRIVATE); |
| 268 webAppPrefs.edit() | 266 webAppPrefs.edit().putLong(WebappDataStorage.KEY_LAST_USED, Long.MIN_VAL
UE).apply(); |
| 269 .putLong(WebappDataStorage.KEY_LAST_USED, Long.MIN_VALUE) | |
| 270 .commit(); | |
| 271 | 267 |
| 272 WebappRegistry.unregisterOldWebapps(Robolectric.application, currentTime
); | 268 WebappRegistry.unregisterOldWebapps(Robolectric.application, currentTime
); |
| 273 BackgroundShadowAsyncTask.runBackgroundTasks(); | 269 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 274 | 270 |
| 275 Set<String> actual = mSharedPreferences.getStringSet( | 271 Set<String> actual = mSharedPreferences.getStringSet( |
| 276 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); | 272 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); |
| 277 assertEquals(new HashSet<String>(Arrays.asList("oldWebapp")), actual); | 273 assertEquals(new HashSet<String>(Arrays.asList("oldWebapp")), actual); |
| 278 | 274 |
| 279 long actualLastUsed = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USE
D, | 275 long actualLastUsed = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USE
D, |
| 280 WebappDataStorage.LAST_USED_INVALID); | 276 WebappDataStorage.LAST_USED_INVALID); |
| 281 assertEquals(Long.MIN_VALUE, actualLastUsed); | 277 assertEquals(Long.MIN_VALUE, actualLastUsed); |
| 282 | 278 |
| 283 // The last cleanup time was set to 0 in setUp() so check that this hasn
't changed. | 279 // The last cleanup time was set to 0 in setUp() so check that this hasn
't changed. |
| 284 long lastCleanup = mSharedPreferences.getLong(WebappRegistry.KEY_LAST_CL
EANUP, -1); | 280 long lastCleanup = mSharedPreferences.getLong(WebappRegistry.KEY_LAST_CL
EANUP, -1); |
| 285 assertEquals(INITIAL_TIME, lastCleanup); | 281 assertEquals(INITIAL_TIME, lastCleanup); |
| 286 } | 282 } |
| 287 | 283 |
| 288 @Test | 284 @Test |
| 289 @Feature({"Webapp"}) | 285 @Feature({"Webapp"}) |
| 290 public void testCleanupDoesNotRemoveRecentApps() throws Exception { | 286 public void testCleanupDoesNotRemoveRecentApps() throws Exception { |
| 291 // Put the current time such that the task runs. | 287 // Put the current time such that the task runs. |
| 292 long currentTime = INITIAL_TIME + WebappRegistry.FULL_CLEANUP_DURATION; | 288 long currentTime = INITIAL_TIME + WebappRegistry.FULL_CLEANUP_DURATION; |
| 293 | 289 |
| 294 // Put the last used time just inside the no-cleanup window. | 290 // Put the last used time just inside the no-cleanup window. |
| 295 addWebappsToRegistry("recentWebapp"); | 291 addWebappsToRegistry("recentWebapp"); |
| 296 SharedPreferences webAppPrefs = Robolectric.application.getSharedPrefere
nces( | 292 SharedPreferences webAppPrefs = Robolectric.application.getSharedPrefere
nces( |
| 297 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "recentWebapp", Con
text.MODE_PRIVATE); | 293 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "recentWebapp", Con
text.MODE_PRIVATE); |
| 298 long lastUsed = currentTime - WebappRegistry.WEBAPP_UNOPENED_CLEANUP_DUR
ATION + 1; | 294 long lastUsed = currentTime - WebappRegistry.WEBAPP_UNOPENED_CLEANUP_DUR
ATION + 1; |
| 299 webAppPrefs.edit() | 295 webAppPrefs.edit().putLong(WebappDataStorage.KEY_LAST_USED, lastUsed).ap
ply(); |
| 300 .putLong(WebappDataStorage.KEY_LAST_USED, lastUsed) | |
| 301 .commit(); | |
| 302 | 296 |
| 303 // Because the time is just inside the window, there should be a cleanup
but the web app | 297 // Because the time is just inside the window, there should be a cleanup
but the web app |
| 304 // should not be deleted as it was used recently. The last cleanup time
should also be | 298 // should not be deleted as it was used recently. The last cleanup time
should also be |
| 305 // set to the current time. | 299 // set to the current time. |
| 306 WebappRegistry.unregisterOldWebapps(Robolectric.application, currentTime
); | 300 WebappRegistry.unregisterOldWebapps(Robolectric.application, currentTime
); |
| 307 BackgroundShadowAsyncTask.runBackgroundTasks(); | 301 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 308 | 302 |
| 309 Set<String> actual = mSharedPreferences.getStringSet( | 303 Set<String> actual = mSharedPreferences.getStringSet( |
| 310 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); | 304 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); |
| 311 assertEquals(new HashSet<String>(Arrays.asList("recentWebapp")), actual)
; | 305 assertEquals(new HashSet<String>(Arrays.asList("recentWebapp")), actual)
; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 322 @Feature({"Webapp"}) | 316 @Feature({"Webapp"}) |
| 323 public void testCleanupRemovesOldApps() throws Exception { | 317 public void testCleanupRemovesOldApps() throws Exception { |
| 324 // Put the current time such that the task runs. | 318 // Put the current time such that the task runs. |
| 325 long currentTime = INITIAL_TIME + WebappRegistry.FULL_CLEANUP_DURATION; | 319 long currentTime = INITIAL_TIME + WebappRegistry.FULL_CLEANUP_DURATION; |
| 326 | 320 |
| 327 // Put the last used time just outside the no-cleanup window. | 321 // Put the last used time just outside the no-cleanup window. |
| 328 addWebappsToRegistry("oldWebapp"); | 322 addWebappsToRegistry("oldWebapp"); |
| 329 SharedPreferences webAppPrefs = Robolectric.application.getSharedPrefere
nces( | 323 SharedPreferences webAppPrefs = Robolectric.application.getSharedPrefere
nces( |
| 330 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "oldWebapp", Contex
t.MODE_PRIVATE); | 324 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "oldWebapp", Contex
t.MODE_PRIVATE); |
| 331 long lastUsed = currentTime - WebappRegistry.WEBAPP_UNOPENED_CLEANUP_DUR
ATION; | 325 long lastUsed = currentTime - WebappRegistry.WEBAPP_UNOPENED_CLEANUP_DUR
ATION; |
| 332 webAppPrefs.edit() | 326 webAppPrefs.edit().putLong(WebappDataStorage.KEY_LAST_USED, lastUsed).ap
ply(); |
| 333 .putLong(WebappDataStorage.KEY_LAST_USED, lastUsed) | |
| 334 .commit(); | |
| 335 | 327 |
| 336 // Because the time is just inside the window, there should be a cleanup
of old web apps and | 328 // Because the time is just inside the window, there should be a cleanup
of old web apps and |
| 337 // the last cleaned up time should be set to the current time. | 329 // the last cleaned up time should be set to the current time. |
| 338 WebappRegistry.unregisterOldWebapps(Robolectric.application, currentTime
); | 330 WebappRegistry.unregisterOldWebapps(Robolectric.application, currentTime
); |
| 339 BackgroundShadowAsyncTask.runBackgroundTasks(); | 331 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 340 | 332 |
| 341 Set<String> actual = mSharedPreferences.getStringSet( | 333 Set<String> actual = mSharedPreferences.getStringSet( |
| 342 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); | 334 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); |
| 343 assertTrue(actual.isEmpty()); | 335 assertTrue(actual.isEmpty()); |
| 344 | 336 |
| (...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 681 } | 673 } |
| 682 } | 674 } |
| 683 ); | 675 ); |
| 684 BackgroundShadowAsyncTask.runBackgroundTasks(); | 676 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 685 Robolectric.runUiThreadTasks(); | 677 Robolectric.runUiThreadTasks(); |
| 686 assertTrue(mCallbackCalled); | 678 assertTrue(mCallbackCalled); |
| 687 } | 679 } |
| 688 | 680 |
| 689 private Set<String> addWebappsToRegistry(String... webapps) { | 681 private Set<String> addWebappsToRegistry(String... webapps) { |
| 690 final Set<String> expected = new HashSet<String>(Arrays.asList(webapps))
; | 682 final Set<String> expected = new HashSet<String>(Arrays.asList(webapps))
; |
| 691 mSharedPreferences.edit() | 683 mSharedPreferences.edit().putStringSet(WebappRegistry.KEY_WEBAPP_SET, ex
pected).apply(); |
| 692 .putStringSet(WebappRegistry.KEY_WEBAPP_SET, expected) | |
| 693 .commit(); | |
| 694 return expected; | 684 return expected; |
| 695 } | 685 } |
| 696 | 686 |
| 697 private Set<String> getRegisteredWebapps() { | 687 private Set<String> getRegisteredWebapps() { |
| 698 return mSharedPreferences.getStringSet( | 688 return mSharedPreferences.getStringSet( |
| 699 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); | 689 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); |
| 700 } | 690 } |
| 701 | 691 |
| 702 private Intent createShortcutIntent(String url) { | 692 private Intent createShortcutIntent(String url) { |
| 703 return ShortcutHelper.createWebappShortcutIntent("id", "action", url, | 693 return ShortcutHelper.createWebappShortcutIntent("id", "action", url, |
| 704 ShortcutHelper.getScopeFromUrl(url), "name", "shortName", null, | 694 ShortcutHelper.getScopeFromUrl(url), "name", "shortName", null, |
| 705 ShortcutHelper.WEBAPP_SHORTCUT_VERSION, WebDisplayMode.Standalon
e, 0, 0, 0, false); | 695 ShortcutHelper.WEBAPP_SHORTCUT_VERSION, WebDisplayMode.Standalon
e, 0, 0, 0, false); |
| 706 } | 696 } |
| 707 | 697 |
| 708 private Intent createWebApkIntent(String webappId, String webApkPackage) { | 698 private Intent createWebApkIntent(String webappId, String webApkPackage) { |
| 709 Intent intent = new Intent(); | 699 Intent intent = new Intent(); |
| 710 intent.putExtra(ShortcutHelper.EXTRA_ID, webappId) | 700 intent.putExtra(ShortcutHelper.EXTRA_ID, webappId) |
| 711 .putExtra(ShortcutHelper.EXTRA_URL, "https://foo.com") | 701 .putExtra(ShortcutHelper.EXTRA_URL, "https://foo.com") |
| 712 .putExtra(ShortcutHelper.EXTRA_WEBAPK_PACKAGE_NAME, webApkPackage)
; | 702 .putExtra(ShortcutHelper.EXTRA_WEBAPK_PACKAGE_NAME, webApkPackage)
; |
| 713 return intent; | 703 return intent; |
| 714 } | 704 } |
| 715 } | 705 } |
| OLD | NEW |