Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(384)

Side by Side Diff: chrome/android/junit/src/org/chromium/chrome/browser/webapps/WebappRegistryTest.java

Issue 1359383002: webapps: Add cleanup task when opening up WebappActivity to clean old web apps (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@webapp-cleanup
Patch Set: Address Mounir's comments Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/android/java/src/org/chromium/chrome/browser/webapps/WebappRegistry.java ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 18 matching lines...) Expand all
29 * Tests the WebappRegistry class by ensuring that it persists data to 29 * Tests the WebappRegistry class by ensuring that it persists data to
30 * SharedPreferences as expected. 30 * SharedPreferences as expected.
31 */ 31 */
32 @RunWith(LocalRobolectricTestRunner.class) 32 @RunWith(LocalRobolectricTestRunner.class)
33 @Config(manifest = Config.NONE, shadows = {BackgroundShadowAsyncTask.class}) 33 @Config(manifest = Config.NONE, shadows = {BackgroundShadowAsyncTask.class})
34 public class WebappRegistryTest { 34 public class WebappRegistryTest {
35 35
36 // These were copied from WebappRegistry for backward compatibility checking . 36 // These were copied from WebappRegistry for backward compatibility checking .
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 private static final String KEY_LAST_CLEANUP = "last_cleanup";
40
41 private static final int INITIAL_TIME = 0;
39 42
40 private SharedPreferences mSharedPreferences; 43 private SharedPreferences mSharedPreferences;
41 private boolean mCallbackCalled; 44 private boolean mCallbackCalled;
42 45
43 @Before 46 @Before
44 public void setUp() throws Exception { 47 public void setUp() throws Exception {
45 mSharedPreferences = Robolectric.application 48 mSharedPreferences = Robolectric.application
46 .getSharedPreferences(REGISTRY_FILE_NAME, Context.MODE_PRIVATE); 49 .getSharedPreferences(REGISTRY_FILE_NAME, Context.MODE_PRIVATE);
50 mSharedPreferences.edit().putLong(KEY_LAST_CLEANUP, INITIAL_TIME).commit ();
51
47 mCallbackCalled = false; 52 mCallbackCalled = false;
48 } 53 }
49 54
50 @Test 55 @Test
51 @Feature({"Webapp"}) 56 @Feature({"Webapp"})
52 public void testBackwardCompatibility() { 57 public void testBackwardCompatibility() {
53 assertEquals(REGISTRY_FILE_NAME, WebappRegistry.REGISTRY_FILE_NAME); 58 assertEquals(REGISTRY_FILE_NAME, WebappRegistry.REGISTRY_FILE_NAME);
54 assertEquals(KEY_WEBAPP_SET, WebappRegistry.KEY_WEBAPP_SET); 59 assertEquals(KEY_WEBAPP_SET, WebappRegistry.KEY_WEBAPP_SET);
60 assertEquals(KEY_LAST_CLEANUP, WebappRegistry.KEY_LAST_CLEANUP);
55 } 61 }
56 62
57 @Test 63 @Test
58 @Feature({"Webapp"}) 64 @Feature({"Webapp"})
59 public void testWebappRegistrationAddsToSharedPrefs() throws Exception { 65 public void testWebappRegistrationAddsToSharedPrefs() throws Exception {
60 WebappRegistry.registerWebapp(Robolectric.application, "test"); 66 WebappRegistry.registerWebapp(Robolectric.application, "test");
61 BackgroundShadowAsyncTask.runBackgroundTasks(); 67 BackgroundShadowAsyncTask.runBackgroundTasks();
62 68
63 Set<String> actual = mSharedPreferences.getStringSet( 69 Set<String> actual = mSharedPreferences.getStringSet(
64 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); 70 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet());
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 .putLong(WebappDataStorage.KEY_LAST_USED, 100L) 183 .putLong(WebappDataStorage.KEY_LAST_USED, 100L)
178 .commit(); 184 .commit();
179 185
180 WebappRegistry.unregisterAllWebapps(Robolectric.application, null); 186 WebappRegistry.unregisterAllWebapps(Robolectric.application, null);
181 BackgroundShadowAsyncTask.runBackgroundTasks(); 187 BackgroundShadowAsyncTask.runBackgroundTasks();
182 188
183 Map<String, ?> actual = webAppPrefs.getAll(); 189 Map<String, ?> actual = webAppPrefs.getAll();
184 assertTrue(actual.isEmpty()); 190 assertTrue(actual.isEmpty());
185 } 191 }
186 192
193 @Test
194 @Feature({"Webapp"})
195 public void testCleanupDoesNotRunTooOften() throws Exception {
196 // Put the current time to just before the task should run.
197 long currentTime = INITIAL_TIME + WebappRegistry.FULL_CLEANUP_DURATION - 1;
198
199 addWebappsToRegistry("oldWebapp");
200 SharedPreferences webAppPrefs = Robolectric.application.getSharedPrefere nces(
201 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "oldWebapp", Contex t.MODE_PRIVATE);
202 webAppPrefs.edit()
203 .putLong(WebappDataStorage.KEY_LAST_USED, Long.MIN_VALUE)
204 .commit();
205
206 WebappRegistry.unregisterOldWebapps(Robolectric.application, currentTime );
207 BackgroundShadowAsyncTask.runBackgroundTasks();
208
209 Set<String> actual = mSharedPreferences.getStringSet(
210 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet());
211 assertEquals(new HashSet<String>(Arrays.asList("oldWebapp")), actual);
212
213 long actualLastUsed = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USE D,
214 WebappDataStorage.INVALID_LAST_USED);
215 assertEquals(Long.MIN_VALUE, actualLastUsed);
216
217 // The last cleanup time was set to 0 in setUp() so check that this hasn 't changed.
218 long lastCleanup = mSharedPreferences.getLong(WebappRegistry.KEY_LAST_CL EANUP, -1);
219 assertEquals(INITIAL_TIME, lastCleanup);
220 }
221
222 @Test
223 @Feature({"Webapp"})
224 public void testCleanupDoesNotRemoveRecentApps() throws Exception {
225 // Put the current time such that the task runs.
226 long currentTime = INITIAL_TIME + WebappRegistry.FULL_CLEANUP_DURATION;
227
228 // Put the last used time just inside the no-cleanup window.
229 addWebappsToRegistry("recentWebapp");
230 SharedPreferences webAppPrefs = Robolectric.application.getSharedPrefere nces(
231 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "recentWebapp", Con text.MODE_PRIVATE);
232 long lastUsed = currentTime - WebappRegistry.WEBAPP_UNOPENED_CLEANUP_DUR ATION + 1;
233 webAppPrefs.edit()
234 .putLong(WebappDataStorage.KEY_LAST_USED, lastUsed)
235 .commit();
236
237 // Because the time is just inside the window, there should be a cleanup but the web app
238 // should not be deleted as it was used recently. The last cleanup time should also be
239 // set to the current time.
240 WebappRegistry.unregisterOldWebapps(Robolectric.application, currentTime );
241 BackgroundShadowAsyncTask.runBackgroundTasks();
242
243 Set<String> actual = mSharedPreferences.getStringSet(
244 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet());
245 assertEquals(new HashSet<String>(Arrays.asList("recentWebapp")), actual) ;
246
247 long actualLastUsed = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USE D,
248 WebappDataStorage.INVALID_LAST_USED);
249 assertEquals(lastUsed, actualLastUsed);
250
251 long lastCleanup = mSharedPreferences.getLong(WebappRegistry.KEY_LAST_CL EANUP, -1);
252 assertEquals(currentTime, lastCleanup);
253 }
254
255 @Test
256 @Feature({"Webapp"})
257 public void testCleanupRemovesOldApps() throws Exception {
258 // Put the current time such that the task runs.
259 long currentTime = INITIAL_TIME + WebappRegistry.FULL_CLEANUP_DURATION;
260
261 // Put the last used time just outside the no-cleanup window.
262 addWebappsToRegistry("oldWebapp");
263 SharedPreferences webAppPrefs = Robolectric.application.getSharedPrefere nces(
264 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "oldWebapp", Contex t.MODE_PRIVATE);
265 long lastUsed = currentTime - WebappRegistry.WEBAPP_UNOPENED_CLEANUP_DUR ATION;
266 webAppPrefs.edit()
267 .putLong(WebappDataStorage.KEY_LAST_USED, lastUsed)
268 .commit();
269
270 // Because the time is just inside the window, there should be a cleanup of old web apps and
271 // the last cleaned up time should be set to the current time.
272 WebappRegistry.unregisterOldWebapps(Robolectric.application, currentTime );
273 BackgroundShadowAsyncTask.runBackgroundTasks();
274
275 Set<String> actual = mSharedPreferences.getStringSet(
276 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet());
277 assertTrue(actual.isEmpty());
278
279 long actualLastUsed = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USE D,
280 WebappDataStorage.INVALID_LAST_USED);
281 assertEquals(WebappDataStorage.INVALID_LAST_USED, actualLastUsed);
282
283 long lastCleanup = mSharedPreferences.getLong(WebappRegistry.KEY_LAST_CL EANUP, -1);
284 assertEquals(currentTime, lastCleanup);
285 }
286
187 private Set<String> addWebappsToRegistry(String... webapps) { 287 private Set<String> addWebappsToRegistry(String... webapps) {
188 final Set<String> expected = new HashSet<String>(Arrays.asList(webapps)) ; 288 final Set<String> expected = new HashSet<String>(Arrays.asList(webapps)) ;
189 mSharedPreferences.edit() 289 mSharedPreferences.edit()
190 .putStringSet(WebappRegistry.KEY_WEBAPP_SET, expected) 290 .putStringSet(WebappRegistry.KEY_WEBAPP_SET, expected)
191 .commit(); 291 .commit();
192 return expected; 292 return expected;
193 } 293 }
194 294
195 private Set<String> getRegisteredWebapps() { 295 private Set<String> getRegisteredWebapps() {
196 return mSharedPreferences.getStringSet( 296 return mSharedPreferences.getStringSet(
197 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); 297 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet());
198 } 298 }
199 } 299 }
OLDNEW
« no previous file with comments | « chrome/android/java/src/org/chromium/chrome/browser/webapps/WebappRegistry.java ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698