Chromium Code Reviews| Index: chrome/android/junit/src/org/chromium/chrome/browser/webapps/WebappRegistryTest.java |
| diff --git a/chrome/android/junit/src/org/chromium/chrome/browser/webapps/WebappRegistryTest.java b/chrome/android/junit/src/org/chromium/chrome/browser/webapps/WebappRegistryTest.java |
| index 073e24bcb7e0eacededf84b348b0c6670987dd01..981a9a800cc62663fab44465232ea0ff5b3afc82 100644 |
| --- a/chrome/android/junit/src/org/chromium/chrome/browser/webapps/WebappRegistryTest.java |
| +++ b/chrome/android/junit/src/org/chromium/chrome/browser/webapps/WebappRegistryTest.java |
| @@ -44,6 +44,8 @@ public class WebappRegistryTest { |
| public void setUp() throws Exception { |
| mSharedPreferences = Robolectric.application |
| .getSharedPreferences(REGISTRY_FILE_NAME, Context.MODE_PRIVATE); |
| + mSharedPreferences.edit().putLong(KEY_LAST_CLEANUP, 0).commit(); |
|
gone
2015/09/24 14:08:30
nit: make a private static final int INITIAL_TIME
Lalit Maganti
2015/09/24 14:30:20
Done.
|
| + |
| mCallbackCalled = false; |
| } |
| @@ -184,6 +186,94 @@ public class WebappRegistryTest { |
| assertTrue(actual.isEmpty()); |
| } |
| + @Test |
| + @Feature({"Webapp"}) |
| + public void testCleanupDoesNotRunTooOften() throws Exception { |
| + addWebappsToRegistry("oldWebapp"); |
| + SharedPreferences webAppPrefs = Robolectric.application.getSharedPreferences( |
| + WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "oldWebapp", Context.MODE_PRIVATE); |
| + webAppPrefs.edit() |
| + .putLong(WebappDataStorage.KEY_LAST_USED, Long.MIN_VALUE) |
| + .commit(); |
| + |
| + // Time just before the task should run. |
| + WebappRegistry.unregisterOldWebapps(Robolectric.application, |
| + WebappRegistry.FULL_CLEANUP_DURATION - 1); |
| + BackgroundShadowAsyncTask.runBackgroundTasks(); |
| + |
| + Set<String> actual = mSharedPreferences.getStringSet( |
| + WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); |
| + assertEquals(new HashSet<String>(Arrays.asList("oldWebapp")), actual); |
| + |
| + long actualLastUsed = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USED, |
| + WebappDataStorage.INVALID_LAST_USED); |
| + assertEquals(Long.MIN_VALUE, actualLastUsed); |
| + } |
| + |
| + @Test |
| + @Feature({"Webapp"}) |
| + public void testCleanupDoesNotRemoveRecentApps() throws Exception { |
| + long currentTime = WebappRegistry.FULL_CLEANUP_DURATION; |
| + |
| + // Put the last used time just inside the no-cleanup window. |
| + addWebappsToRegistry("recentWebapp"); |
| + SharedPreferences webAppPrefs = Robolectric.application.getSharedPreferences( |
| + WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "recentWebapp", Context.MODE_PRIVATE); |
| + long lastUsed = currentTime - WebappRegistry.WEBAPP_UNOPENED_CLEANUP_DURATION + 1; |
| + webAppPrefs.edit() |
| + .putLong(WebappDataStorage.KEY_LAST_USED, lastUsed) |
| + .commit(); |
| + |
| + // Because the time is just inside the window, there should be no cleanup and no update |
| + // of the last cleaned up time. |
| + WebappRegistry.unregisterOldWebapps(Robolectric.application, currentTime); |
| + BackgroundShadowAsyncTask.runBackgroundTasks(); |
| + |
| + Set<String> actual = mSharedPreferences.getStringSet( |
| + WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); |
| + assertEquals(new HashSet<String>(Arrays.asList("recentWebapp")), actual); |
| + |
| + long actualLastUsed = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USED, |
| + WebappDataStorage.INVALID_LAST_USED); |
| + assertEquals(lastUsed, actualLastUsed); |
| + |
| + // The last cleanup time was set to 0 in setUp() so check that this hasn't changed. |
| + long lastCleanup = mSharedPreferences.getLong(WebappRegistry.KEY_LAST_CLEANUP, -1); |
| + assertEquals(0, lastCleanup); |
|
gone
2015/09/24 14:08:30
INITIAL_TIME
Lalit Maganti
2015/09/24 14:30:20
Done.
|
| + } |
| + |
| + @Test |
| + @Feature({"Webapp"}) |
| + public void testCleanupRemovesOldApps() throws Exception { |
| + long currentTime = WebappRegistry.FULL_CLEANUP_DURATION; |
| + |
| + // Put the last used time just outside the no-cleanup window. |
| + addWebappsToRegistry("oldWebapp"); |
| + SharedPreferences webAppPrefs = Robolectric.application.getSharedPreferences( |
| + WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "oldWebapp", Context.MODE_PRIVATE); |
| + long lastUsed = currentTime - WebappRegistry.WEBAPP_UNOPENED_CLEANUP_DURATION; |
| + webAppPrefs.edit() |
| + .putLong(WebappDataStorage.KEY_LAST_USED, lastUsed) |
| + .commit(); |
| + |
| + // Because the time is just inside the window, there should be a cleanup of old web apps and |
| + // the last cleaned up time should be set to the current time. |
| + WebappRegistry.unregisterOldWebapps(Robolectric.application, currentTime); |
| + BackgroundShadowAsyncTask.runBackgroundTasks(); |
| + |
| + Set<String> actual = mSharedPreferences.getStringSet( |
| + WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); |
| + assertTrue(actual.isEmpty()); |
| + |
| + long actualLastUsed = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USED, |
| + WebappDataStorage.INVALID_LAST_USED); |
| + assertEquals(WebappDataStorage.INVALID_LAST_USED, actualLastUsed); |
| + |
| + // The last cleanup time should be set to the current time. |
| + long lastCleanup = mSharedPreferences.getLong(WebappRegistry.KEY_LAST_CLEANUP, -1); |
| + assertEquals(currentTime, lastCleanup); |
| + } |
| + |
| private Set<String> addWebappsToRegistry(String... webapps) { |
| final Set<String> expected = new HashSet<String>(Arrays.asList(webapps)); |
| mSharedPreferences.edit() |