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

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: Created 5 years, 2 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
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;
11 import android.content.SharedPreferences; 11 import android.content.SharedPreferences;
12 import android.os.SystemClock;
12 13
13 import org.chromium.base.test.util.Feature; 14 import org.chromium.base.test.util.Feature;
14 import org.chromium.testing.local.BackgroundShadowAsyncTask; 15 import org.chromium.testing.local.BackgroundShadowAsyncTask;
15 import org.chromium.testing.local.LocalRobolectricTestRunner; 16 import org.chromium.testing.local.LocalRobolectricTestRunner;
16 import org.junit.Before; 17 import org.junit.Before;
17 import org.junit.Test; 18 import org.junit.Test;
18 import org.junit.runner.RunWith; 19 import org.junit.runner.RunWith;
19 import org.robolectric.Robolectric; 20 import org.robolectric.Robolectric;
20 import org.robolectric.annotation.Config; 21 import org.robolectric.annotation.Config;
21 22
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 .putLong(WebappDataStorage.KEY_LAST_USED, 100L) 178 .putLong(WebappDataStorage.KEY_LAST_USED, 100L)
178 .commit(); 179 .commit();
179 180
180 WebappRegistry.unregisterAllWebapps(Robolectric.application, null); 181 WebappRegistry.unregisterAllWebapps(Robolectric.application, null);
181 BackgroundShadowAsyncTask.runBackgroundTasks(); 182 BackgroundShadowAsyncTask.runBackgroundTasks();
182 183
183 Map<String, ?> actual = webAppPrefs.getAll(); 184 Map<String, ?> actual = webAppPrefs.getAll();
184 assertTrue(actual.isEmpty()); 185 assertTrue(actual.isEmpty());
185 } 186 }
186 187
188 @Test
189 @Feature({"Webapp"})
190 public void testCleanupDoesNotRunTooOften() throws Exception {
191 // Set the system clock to just before the task should run.
192 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.
193
194 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.
195 SharedPreferences webAppPrefs = Robolectric.application.getSharedPrefere nces(
196 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "test", Context.MOD E_PRIVATE);
197 webAppPrefs.edit()
198 .putLong(WebappDataStorage.KEY_LAST_USED, Long.MIN_VALUE)
199 .commit();
200
201 WebappRegistry.unregisterOldWebapps(Robolectric.application);
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("test")), 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 // Set the system clock to just before the task should run.
217 SystemClock.setCurrentTimeMillis(WebappRegistry.FULL_CLEANUP_DURATION);
218
219 addWebappsToRegistry("test");
220 SharedPreferences webAppPrefs = Robolectric.application.getSharedPrefere nces(
221 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "test", Context.MOD E_PRIVATE);
222
223 // Put the last used time just inside the no-cleanup window.
224 long lastUsed =
225 System.currentTimeMillis() - WebappRegistry.WEBAPP_NON_OPEN_CLEA NUP_DURATION + 1;
226 webAppPrefs.edit()
227 .putLong(WebappDataStorage.KEY_LAST_USED, lastUsed)
228 .commit();
229
230 WebappRegistry.unregisterOldWebapps(Robolectric.application);
231 BackgroundShadowAsyncTask.runBackgroundTasks();
232
233 Set<String> actual = mSharedPreferences.getStringSet(
234 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet());
235 assertEquals(new HashSet<String>(Arrays.asList("test")), actual);
236
237 long actualLastUsed = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USE D,
238 WebappDataStorage.INVALID_LAST_USED);
239 assertEquals(lastUsed, actualLastUsed);
240 }
241
242 @Test
243 @Feature({"Webapp"})
244 public void testCleanupRemovesOldApps() throws Exception {
245 // Set the system clock to just before the task should run.
246 SystemClock.setCurrentTimeMillis(WebappRegistry.FULL_CLEANUP_DURATION);
247
248 addWebappsToRegistry("test");
249 SharedPreferences webAppPrefs = Robolectric.application.getSharedPrefere nces(
250 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "test", Context.MOD E_PRIVATE);
251
252 // Put the last used time just outside the no-cleanup window.
253 long lastUsed = System.currentTimeMillis()
254 - WebappRegistry.WEBAPP_NON_OPEN_CLEANUP_DURATION;
255 webAppPrefs.edit()
256 .putLong(WebappDataStorage.KEY_LAST_USED, lastUsed)
257 .commit();
258
259 WebappRegistry.unregisterOldWebapps(Robolectric.application);
260 BackgroundShadowAsyncTask.runBackgroundTasks();
261
262 Set<String> actual = mSharedPreferences.getStringSet(
263 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet());
264 assertTrue(actual.isEmpty());
265
266 long actualLastUsed = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USE D,
267 WebappDataStorage.INVALID_LAST_USED);
268 assertEquals(WebappDataStorage.INVALID_LAST_USED, actualLastUsed);
269 }
270
187 private Set<String> addWebappsToRegistry(String... webapps) { 271 private Set<String> addWebappsToRegistry(String... webapps) {
188 final Set<String> expected = new HashSet<String>(Arrays.asList(webapps)) ; 272 final Set<String> expected = new HashSet<String>(Arrays.asList(webapps)) ;
189 mSharedPreferences.edit() 273 mSharedPreferences.edit()
190 .putStringSet(WebappRegistry.KEY_WEBAPP_SET, expected) 274 .putStringSet(WebappRegistry.KEY_WEBAPP_SET, expected)
191 .commit(); 275 .commit();
192 return expected; 276 return expected;
193 } 277 }
194 278
195 private Set<String> getRegisteredWebapps() { 279 private Set<String> getRegisteredWebapps() {
196 return mSharedPreferences.getStringSet( 280 return mSharedPreferences.getStringSet(
197 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); 281 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet());
198 } 282 }
199 } 283 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698