| 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..2d07c7afc6018b61bf11668a17315ad1f00fa8a9 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
|
| @@ -36,6 +36,9 @@ public class WebappRegistryTest {
|
| // These were copied from WebappRegistry for backward compatibility checking.
|
| private static final String REGISTRY_FILE_NAME = "webapp_registry";
|
| private static final String KEY_WEBAPP_SET = "webapp_set";
|
| + private static final String KEY_LAST_CLEANUP = "last_cleanup";
|
| +
|
| + private static final int INITIAL_TIME = 0;
|
|
|
| private SharedPreferences mSharedPreferences;
|
| private boolean mCallbackCalled;
|
| @@ -44,6 +47,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, INITIAL_TIME).commit();
|
| +
|
| mCallbackCalled = false;
|
| }
|
|
|
| @@ -52,6 +57,7 @@ public class WebappRegistryTest {
|
| public void testBackwardCompatibility() {
|
| assertEquals(REGISTRY_FILE_NAME, WebappRegistry.REGISTRY_FILE_NAME);
|
| assertEquals(KEY_WEBAPP_SET, WebappRegistry.KEY_WEBAPP_SET);
|
| + assertEquals(KEY_LAST_CLEANUP, WebappRegistry.KEY_LAST_CLEANUP);
|
| }
|
|
|
| @Test
|
| @@ -184,6 +190,100 @@ public class WebappRegistryTest {
|
| assertTrue(actual.isEmpty());
|
| }
|
|
|
| + @Test
|
| + @Feature({"Webapp"})
|
| + public void testCleanupDoesNotRunTooOften() throws Exception {
|
| + // Put the current time to just before the task should run.
|
| + long currentTime = INITIAL_TIME + WebappRegistry.FULL_CLEANUP_DURATION - 1;
|
| +
|
| + 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();
|
| +
|
| + 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("oldWebapp")), actual);
|
| +
|
| + long actualLastUsed = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USED,
|
| + WebappDataStorage.INVALID_LAST_USED);
|
| + assertEquals(Long.MIN_VALUE, 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(INITIAL_TIME, lastCleanup);
|
| + }
|
| +
|
| + @Test
|
| + @Feature({"Webapp"})
|
| + public void testCleanupDoesNotRemoveRecentApps() throws Exception {
|
| + // Put the current time such that the task runs.
|
| + long currentTime = INITIAL_TIME + 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 a cleanup but the web app
|
| + // should not be deleted as it was used recently. The last cleanup time should also 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());
|
| + assertEquals(new HashSet<String>(Arrays.asList("recentWebapp")), actual);
|
| +
|
| + long actualLastUsed = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USED,
|
| + WebappDataStorage.INVALID_LAST_USED);
|
| + assertEquals(lastUsed, actualLastUsed);
|
| +
|
| + long lastCleanup = mSharedPreferences.getLong(WebappRegistry.KEY_LAST_CLEANUP, -1);
|
| + assertEquals(currentTime, lastCleanup);
|
| + }
|
| +
|
| + @Test
|
| + @Feature({"Webapp"})
|
| + public void testCleanupRemovesOldApps() throws Exception {
|
| + // Put the current time such that the task runs.
|
| + long currentTime = INITIAL_TIME + 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);
|
| +
|
| + 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()
|
|
|