Chromium Code Reviews| 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 26 matching lines...) Expand all Loading... | |
| 37 private static final String REGISTRY_FILE_NAME = "webapp_registry"; | 37 private static final String REGISTRY_FILE_NAME = "webapp_registry"; |
| 38 private static final String KEY_WEBAPP_SET = "webapp_set"; | 38 private static final String KEY_WEBAPP_SET = "webapp_set"; |
| 39 | 39 |
| 40 private SharedPreferences mSharedPreferences; | 40 private SharedPreferences mSharedPreferences; |
| 41 private boolean mCallbackCalled; | 41 private boolean mCallbackCalled; |
| 42 | 42 |
| 43 @Before | 43 @Before |
| 44 public void setUp() throws Exception { | 44 public void setUp() throws Exception { |
| 45 mSharedPreferences = Robolectric.application | 45 mSharedPreferences = Robolectric.application |
| 46 .getSharedPreferences(REGISTRY_FILE_NAME, Context.MODE_PRIVATE); | 46 .getSharedPreferences(REGISTRY_FILE_NAME, Context.MODE_PRIVATE); |
| 47 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.
| |
| 48 | |
| 47 mCallbackCalled = false; | 49 mCallbackCalled = false; |
| 48 } | 50 } |
| 49 | 51 |
| 50 @Test | 52 @Test |
| 51 @Feature({"Webapp"}) | 53 @Feature({"Webapp"}) |
| 52 public void testBackwardCompatibility() { | 54 public void testBackwardCompatibility() { |
| 53 assertEquals(REGISTRY_FILE_NAME, WebappRegistry.REGISTRY_FILE_NAME); | 55 assertEquals(REGISTRY_FILE_NAME, WebappRegistry.REGISTRY_FILE_NAME); |
| 54 assertEquals(KEY_WEBAPP_SET, WebappRegistry.KEY_WEBAPP_SET); | 56 assertEquals(KEY_WEBAPP_SET, WebappRegistry.KEY_WEBAPP_SET); |
| 55 } | 57 } |
| 56 | 58 |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 177 .putLong(WebappDataStorage.KEY_LAST_USED, 100L) | 179 .putLong(WebappDataStorage.KEY_LAST_USED, 100L) |
| 178 .commit(); | 180 .commit(); |
| 179 | 181 |
| 180 WebappRegistry.unregisterAllWebapps(Robolectric.application, null); | 182 WebappRegistry.unregisterAllWebapps(Robolectric.application, null); |
| 181 BackgroundShadowAsyncTask.runBackgroundTasks(); | 183 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 182 | 184 |
| 183 Map<String, ?> actual = webAppPrefs.getAll(); | 185 Map<String, ?> actual = webAppPrefs.getAll(); |
| 184 assertTrue(actual.isEmpty()); | 186 assertTrue(actual.isEmpty()); |
| 185 } | 187 } |
| 186 | 188 |
| 189 @Test | |
| 190 @Feature({"Webapp"}) | |
| 191 public void testCleanupDoesNotRunTooOften() throws Exception { | |
| 192 addWebappsToRegistry("oldWebapp"); | |
| 193 SharedPreferences webAppPrefs = Robolectric.application.getSharedPrefere nces( | |
| 194 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "oldWebapp", Contex t.MODE_PRIVATE); | |
| 195 webAppPrefs.edit() | |
| 196 .putLong(WebappDataStorage.KEY_LAST_USED, Long.MIN_VALUE) | |
| 197 .commit(); | |
| 198 | |
| 199 // Time just before the task should run. | |
| 200 WebappRegistry.unregisterOldWebapps(Robolectric.application, | |
| 201 WebappRegistry.FULL_CLEANUP_DURATION - 1); | |
| 202 BackgroundShadowAsyncTask.runBackgroundTasks(); | |
| 203 | |
| 204 Set<String> actual = mSharedPreferences.getStringSet( | |
| 205 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); | |
| 206 assertEquals(new HashSet<String>(Arrays.asList("oldWebapp")), actual); | |
| 207 | |
| 208 long actualLastUsed = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USE D, | |
| 209 WebappDataStorage.INVALID_LAST_USED); | |
| 210 assertEquals(Long.MIN_VALUE, actualLastUsed); | |
| 211 } | |
| 212 | |
| 213 @Test | |
| 214 @Feature({"Webapp"}) | |
| 215 public void testCleanupDoesNotRemoveRecentApps() throws Exception { | |
| 216 long currentTime = WebappRegistry.FULL_CLEANUP_DURATION; | |
| 217 | |
| 218 // Put the last used time just inside the no-cleanup window. | |
| 219 addWebappsToRegistry("recentWebapp"); | |
| 220 SharedPreferences webAppPrefs = Robolectric.application.getSharedPrefere nces( | |
| 221 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "recentWebapp", Con text.MODE_PRIVATE); | |
| 222 long lastUsed = currentTime - WebappRegistry.WEBAPP_UNOPENED_CLEANUP_DUR ATION + 1; | |
| 223 webAppPrefs.edit() | |
| 224 .putLong(WebappDataStorage.KEY_LAST_USED, lastUsed) | |
| 225 .commit(); | |
| 226 | |
| 227 // Because the time is just inside the window, there should be no cleanu p and no update | |
| 228 // of the last cleaned up time. | |
| 229 WebappRegistry.unregisterOldWebapps(Robolectric.application, currentTime ); | |
| 230 BackgroundShadowAsyncTask.runBackgroundTasks(); | |
| 231 | |
| 232 Set<String> actual = mSharedPreferences.getStringSet( | |
| 233 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); | |
| 234 assertEquals(new HashSet<String>(Arrays.asList("recentWebapp")), actual) ; | |
| 235 | |
| 236 long actualLastUsed = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USE D, | |
| 237 WebappDataStorage.INVALID_LAST_USED); | |
| 238 assertEquals(lastUsed, actualLastUsed); | |
| 239 | |
| 240 // The last cleanup time was set to 0 in setUp() so check that this hasn 't changed. | |
| 241 long lastCleanup = mSharedPreferences.getLong(WebappRegistry.KEY_LAST_CL EANUP, -1); | |
| 242 assertEquals(0, lastCleanup); | |
|
gone
2015/09/24 14:08:30
INITIAL_TIME
Lalit Maganti
2015/09/24 14:30:20
Done.
| |
| 243 } | |
| 244 | |
| 245 @Test | |
| 246 @Feature({"Webapp"}) | |
| 247 public void testCleanupRemovesOldApps() throws Exception { | |
| 248 long currentTime = WebappRegistry.FULL_CLEANUP_DURATION; | |
| 249 | |
| 250 // Put the last used time just outside the no-cleanup window. | |
| 251 addWebappsToRegistry("oldWebapp"); | |
| 252 SharedPreferences webAppPrefs = Robolectric.application.getSharedPrefere nces( | |
| 253 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "oldWebapp", Contex t.MODE_PRIVATE); | |
| 254 long lastUsed = currentTime - WebappRegistry.WEBAPP_UNOPENED_CLEANUP_DUR ATION; | |
| 255 webAppPrefs.edit() | |
| 256 .putLong(WebappDataStorage.KEY_LAST_USED, lastUsed) | |
| 257 .commit(); | |
| 258 | |
| 259 // Because the time is just inside the window, there should be a cleanup of old web apps and | |
| 260 // the last cleaned up time should be set to the current time. | |
| 261 WebappRegistry.unregisterOldWebapps(Robolectric.application, currentTime ); | |
| 262 BackgroundShadowAsyncTask.runBackgroundTasks(); | |
| 263 | |
| 264 Set<String> actual = mSharedPreferences.getStringSet( | |
| 265 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); | |
| 266 assertTrue(actual.isEmpty()); | |
| 267 | |
| 268 long actualLastUsed = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USE D, | |
| 269 WebappDataStorage.INVALID_LAST_USED); | |
| 270 assertEquals(WebappDataStorage.INVALID_LAST_USED, actualLastUsed); | |
| 271 | |
| 272 // The last cleanup time should be set to the current time. | |
| 273 long lastCleanup = mSharedPreferences.getLong(WebappRegistry.KEY_LAST_CL EANUP, -1); | |
| 274 assertEquals(currentTime, lastCleanup); | |
| 275 } | |
| 276 | |
| 187 private Set<String> addWebappsToRegistry(String... webapps) { | 277 private Set<String> addWebappsToRegistry(String... webapps) { |
| 188 final Set<String> expected = new HashSet<String>(Arrays.asList(webapps)) ; | 278 final Set<String> expected = new HashSet<String>(Arrays.asList(webapps)) ; |
| 189 mSharedPreferences.edit() | 279 mSharedPreferences.edit() |
| 190 .putStringSet(WebappRegistry.KEY_WEBAPP_SET, expected) | 280 .putStringSet(WebappRegistry.KEY_WEBAPP_SET, expected) |
| 191 .commit(); | 281 .commit(); |
| 192 return expected; | 282 return expected; |
| 193 } | 283 } |
| 194 | 284 |
| 195 private Set<String> getRegisteredWebapps() { | 285 private Set<String> getRegisteredWebapps() { |
| 196 return mSharedPreferences.getStringSet( | 286 return mSharedPreferences.getStringSet( |
| 197 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); | 287 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); |
| 198 } | 288 } |
| 199 } | 289 } |
| OLD | NEW |