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..f6ee29c20e9c4fe7e972d83a561efa308ea9e426 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 |
| @@ -9,6 +9,7 @@ import static org.junit.Assert.assertTrue; |
| import android.content.Context; |
| import android.content.SharedPreferences; |
| +import android.os.SystemClock; |
| import org.chromium.base.test.util.Feature; |
| import org.chromium.testing.local.BackgroundShadowAsyncTask; |
| @@ -184,6 +185,89 @@ public class WebappRegistryTest { |
| assertTrue(actual.isEmpty()); |
| } |
| + @Test |
| + @Feature({"Webapp"}) |
| + public void testCleanupDoesNotRunTooOften() throws Exception { |
| + // Set the system clock to just before the task should run. |
| + SystemClock.setCurrentTimeMillis(WebappRegistry.FULL_CLEANUP_DURATION - 1); |
|
gone
2015/09/24 10:27:12
instead of mucking about with the clock, add some
Lalit Maganti
2015/09/24 12:35:44
Done.
|
| + |
| + addWebappsToRegistry("test"); |
|
gone
2015/09/24 10:27:12
nit: can you use something other than "test"? it'
Lalit Maganti
2015/09/24 12:35:44
Done.
|
| + SharedPreferences webAppPrefs = Robolectric.application.getSharedPreferences( |
| + WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "test", Context.MODE_PRIVATE); |
| + webAppPrefs.edit() |
| + .putLong(WebappDataStorage.KEY_LAST_USED, Long.MIN_VALUE) |
| + .commit(); |
| + |
| + WebappRegistry.unregisterOldWebapps(Robolectric.application); |
| + BackgroundShadowAsyncTask.runBackgroundTasks(); |
| + |
| + Set<String> actual = mSharedPreferences.getStringSet( |
| + WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); |
| + assertEquals(new HashSet<String>(Arrays.asList("test")), 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 { |
| + // Set the system clock to just before the task should run. |
| + SystemClock.setCurrentTimeMillis(WebappRegistry.FULL_CLEANUP_DURATION); |
| + |
| + addWebappsToRegistry("test"); |
| + SharedPreferences webAppPrefs = Robolectric.application.getSharedPreferences( |
| + WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "test", Context.MODE_PRIVATE); |
| + |
| + // Put the last used time just inside the no-cleanup window. |
| + long lastUsed = |
| + System.currentTimeMillis() - WebappRegistry.WEBAPP_NON_OPEN_CLEANUP_DURATION + 1; |
| + webAppPrefs.edit() |
| + .putLong(WebappDataStorage.KEY_LAST_USED, lastUsed) |
| + .commit(); |
| + |
| + WebappRegistry.unregisterOldWebapps(Robolectric.application); |
| + BackgroundShadowAsyncTask.runBackgroundTasks(); |
| + |
| + Set<String> actual = mSharedPreferences.getStringSet( |
| + WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); |
| + assertEquals(new HashSet<String>(Arrays.asList("test")), actual); |
| + |
| + long actualLastUsed = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USED, |
| + WebappDataStorage.INVALID_LAST_USED); |
| + assertEquals(lastUsed, actualLastUsed); |
| + } |
| + |
| + @Test |
| + @Feature({"Webapp"}) |
| + public void testCleanupRemovesOldApps() throws Exception { |
| + // Set the system clock to just before the task should run. |
| + SystemClock.setCurrentTimeMillis(WebappRegistry.FULL_CLEANUP_DURATION); |
| + |
| + addWebappsToRegistry("test"); |
| + SharedPreferences webAppPrefs = Robolectric.application.getSharedPreferences( |
| + WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "test", Context.MODE_PRIVATE); |
| + |
| + // Put the last used time just outside the no-cleanup window. |
| + long lastUsed = System.currentTimeMillis() |
| + - WebappRegistry.WEBAPP_NON_OPEN_CLEANUP_DURATION; |
| + webAppPrefs.edit() |
| + .putLong(WebappDataStorage.KEY_LAST_USED, lastUsed) |
| + .commit(); |
| + |
| + WebappRegistry.unregisterOldWebapps(Robolectric.application); |
| + 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); |
| + } |
| + |
| private Set<String> addWebappsToRegistry(String... webapps) { |
| final Set<String> expected = new HashSet<String>(Arrays.asList(webapps)); |
| mSharedPreferences.edit() |