| 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; |
| 11 import android.content.Intent; | 11 import android.content.Intent; |
| 12 import android.content.SharedPreferences; | 12 import android.content.SharedPreferences; |
| 13 import android.text.TextUtils; | 13 import android.text.TextUtils; |
| 14 | 14 |
| 15 import org.chromium.base.test.util.Feature; | 15 import org.chromium.base.test.util.Feature; |
| 16 import org.chromium.blink_public.platform.WebDisplayMode; | 16 import org.chromium.blink_public.platform.WebDisplayMode; |
| 17 import org.chromium.chrome.browser.ShortcutHelper; | 17 import org.chromium.chrome.browser.ShortcutHelper; |
| 18 import org.chromium.chrome.browser.browsing_data.UrlFilters; | 18 import org.chromium.chrome.browser.browsing_data.UrlFilters; |
| 19 import org.chromium.testing.local.BackgroundShadowAsyncTask; | 19 import org.chromium.testing.local.BackgroundShadowAsyncTask; |
| 20 import org.chromium.testing.local.LocalRobolectricTestRunner; | 20 import org.chromium.testing.local.LocalRobolectricTestRunner; |
| 21 import org.junit.Before; | 21 import org.junit.Before; |
| 22 import org.junit.Test; | 22 import org.junit.Test; |
| 23 import org.junit.runner.RunWith; | 23 import org.junit.runner.RunWith; |
| 24 import org.robolectric.RuntimeEnvironment; | 24 import org.robolectric.Robolectric; |
| 25 import org.robolectric.annotation.Config; | 25 import org.robolectric.annotation.Config; |
| 26 import org.robolectric.shadows.ShadowLooper; | |
| 27 | 26 |
| 28 import java.util.Arrays; | 27 import java.util.Arrays; |
| 29 import java.util.Collections; | 28 import java.util.Collections; |
| 30 import java.util.HashMap; | 29 import java.util.HashMap; |
| 31 import java.util.HashSet; | 30 import java.util.HashSet; |
| 32 import java.util.Map; | 31 import java.util.Map; |
| 33 import java.util.Set; | 32 import java.util.Set; |
| 34 | 33 |
| 35 /** | 34 /** |
| 36 * Tests the WebappRegistry class by ensuring that it persists data to | 35 * Tests the WebappRegistry class by ensuring that it persists data to |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 mCallbackCalled = true; | 128 mCallbackCalled = true; |
| 130 } | 129 } |
| 131 | 130 |
| 132 boolean getCallbackCalled() { | 131 boolean getCallbackCalled() { |
| 133 return mCallbackCalled; | 132 return mCallbackCalled; |
| 134 } | 133 } |
| 135 } | 134 } |
| 136 | 135 |
| 137 @Before | 136 @Before |
| 138 public void setUp() throws Exception { | 137 public void setUp() throws Exception { |
| 139 mSharedPreferences = RuntimeEnvironment.application | 138 mSharedPreferences = Robolectric.application |
| 140 .getSharedPreferences(REGISTRY_FILE_NAME, Context.MODE_PRIVATE); | 139 .getSharedPreferences(REGISTRY_FILE_NAME, Context.MODE_PRIVATE); |
| 141 mSharedPreferences.edit().putLong(KEY_LAST_CLEANUP, INITIAL_TIME).commit
(); | 140 mSharedPreferences.edit().putLong(KEY_LAST_CLEANUP, INITIAL_TIME).commit
(); |
| 142 | 141 |
| 143 mCallbackCalled = false; | 142 mCallbackCalled = false; |
| 144 } | 143 } |
| 145 | 144 |
| 146 @Test | 145 @Test |
| 147 @Feature({"Webapp"}) | 146 @Feature({"Webapp"}) |
| 148 public void testBackwardCompatibility() { | 147 public void testBackwardCompatibility() { |
| 149 assertEquals(REGISTRY_FILE_NAME, WebappRegistry.REGISTRY_FILE_NAME); | 148 assertEquals(REGISTRY_FILE_NAME, WebappRegistry.REGISTRY_FILE_NAME); |
| 150 assertEquals(KEY_WEBAPP_SET, WebappRegistry.KEY_WEBAPP_SET); | 149 assertEquals(KEY_WEBAPP_SET, WebappRegistry.KEY_WEBAPP_SET); |
| 151 assertEquals(KEY_LAST_CLEANUP, WebappRegistry.KEY_LAST_CLEANUP); | 150 assertEquals(KEY_LAST_CLEANUP, WebappRegistry.KEY_LAST_CLEANUP); |
| 152 } | 151 } |
| 153 | 152 |
| 154 @Test | 153 @Test |
| 155 @Feature({"Webapp"}) | 154 @Feature({"Webapp"}) |
| 156 public void testWebappRegistrationAddsToSharedPrefs() throws Exception { | 155 public void testWebappRegistrationAddsToSharedPrefs() throws Exception { |
| 157 WebappRegistry.registerWebapp(RuntimeEnvironment.application, "test", nu
ll); | 156 WebappRegistry.registerWebapp(Robolectric.application, "test", null); |
| 158 | |
| 159 BackgroundShadowAsyncTask.runBackgroundTasks(); | 157 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 160 | 158 |
| 161 Set<String> actual = mSharedPreferences.getStringSet( | 159 Set<String> actual = mSharedPreferences.getStringSet( |
| 162 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); | 160 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); |
| 163 assertEquals(1, actual.size()); | 161 assertEquals(1, actual.size()); |
| 164 assertTrue(actual.contains("test")); | 162 assertTrue(actual.contains("test")); |
| 165 } | 163 } |
| 166 | 164 |
| 167 @Test | 165 @Test |
| 168 @Feature({"Webapp"}) | 166 @Feature({"Webapp"}) |
| 169 public void testWebappRegistrationUpdatesLastUsed() throws Exception { | 167 public void testWebappRegistrationUpdatesLastUsed() throws Exception { |
| 170 WebappRegistry.registerWebapp(RuntimeEnvironment.application, "test", nu
ll); | 168 WebappRegistry.registerWebapp(Robolectric.application, "test", null); |
| 171 | |
| 172 BackgroundShadowAsyncTask.runBackgroundTasks(); | 169 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 173 long after = System.currentTimeMillis(); | 170 long after = System.currentTimeMillis(); |
| 174 | 171 |
| 175 SharedPreferences webAppPrefs = RuntimeEnvironment.application.getShared
Preferences( | 172 SharedPreferences webAppPrefs = Robolectric.application.getSharedPrefere
nces( |
| 176 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "test", Context.MOD
E_PRIVATE); | 173 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "test", Context.MOD
E_PRIVATE); |
| 177 long actual = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USED, | 174 long actual = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USED, |
| 178 WebappDataStorage.LAST_USED_INVALID); | 175 WebappDataStorage.LAST_USED_INVALID); |
| 179 assertTrue("Timestamp is out of range", actual <= after); | 176 assertTrue("Timestamp is out of range", actual <= after); |
| 180 } | 177 } |
| 181 | 178 |
| 182 @Test | 179 @Test |
| 183 @Feature({"Webapp"}) | 180 @Feature({"Webapp"}) |
| 184 public void testWebappIdsRetrieval() throws Exception { | 181 public void testWebappIdsRetrieval() throws Exception { |
| 185 final Set<String> expected = addWebappsToRegistry("first", "second"); | 182 final Set<String> expected = addWebappsToRegistry("first", "second"); |
| 186 | 183 |
| 187 FetchCallback callback = new FetchCallback(expected); | 184 FetchCallback callback = new FetchCallback(expected); |
| 188 WebappRegistry.getRegisteredWebappIds(RuntimeEnvironment.application, ca
llback); | 185 WebappRegistry.getRegisteredWebappIds(Robolectric.application, callback)
; |
| 189 | |
| 190 BackgroundShadowAsyncTask.runBackgroundTasks(); | 186 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 191 ShadowLooper.runUiThreadTasks(); | 187 Robolectric.runUiThreadTasks(); |
| 192 | 188 |
| 193 assertTrue(callback.getCallbackCalled()); | 189 assertTrue(callback.getCallbackCalled()); |
| 194 } | 190 } |
| 195 | 191 |
| 196 @Test | 192 @Test |
| 197 @Feature({"Webapp"}) | 193 @Feature({"Webapp"}) |
| 198 public void testWebappIdsRetrievalRegisterRetrival() throws Exception { | 194 public void testWebappIdsRetrievalRegisterRetrival() throws Exception { |
| 199 final Set<String> expected = addWebappsToRegistry("first"); | 195 final Set<String> expected = addWebappsToRegistry("first"); |
| 200 | 196 |
| 201 FetchCallback callback = new FetchCallback(expected); | 197 FetchCallback callback = new FetchCallback(expected); |
| 202 WebappRegistry.getRegisteredWebappIds(RuntimeEnvironment.application, ca
llback); | 198 WebappRegistry.getRegisteredWebappIds(Robolectric.application, callback)
; |
| 203 | |
| 204 BackgroundShadowAsyncTask.runBackgroundTasks(); | 199 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 205 ShadowLooper.runUiThreadTasks(); | 200 Robolectric.runUiThreadTasks(); |
| 206 | 201 |
| 207 assertTrue(callback.getCallbackCalled()); | 202 assertTrue(callback.getCallbackCalled()); |
| 208 | 203 |
| 209 WebappRegistry.registerWebapp(RuntimeEnvironment.application, "second",
null); | 204 WebappRegistry.registerWebapp(Robolectric.application, "second", null); |
| 210 | |
| 211 BackgroundShadowAsyncTask.runBackgroundTasks(); | 205 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 212 | 206 |
| 213 // A copy of the expected set needs to be made as the SharedPreferences
is using the copy | 207 // A copy of the expected set needs to be made as the SharedPreferences
is using the copy |
| 214 // that was paassed to it. | 208 // that was paassed to it. |
| 215 final Set<String> secondExpected = new HashSet<>(expected); | 209 final Set<String> secondExpected = new HashSet<>(expected); |
| 216 secondExpected.add("second"); | 210 secondExpected.add("second"); |
| 217 | 211 |
| 218 callback = new FetchCallback(secondExpected); | 212 callback = new FetchCallback(secondExpected); |
| 219 WebappRegistry.getRegisteredWebappIds(RuntimeEnvironment.application, ca
llback); | 213 WebappRegistry.getRegisteredWebappIds(Robolectric.application, callback)
; |
| 220 | |
| 221 BackgroundShadowAsyncTask.runBackgroundTasks(); | 214 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 222 ShadowLooper.runUiThreadTasks(); | 215 Robolectric.runUiThreadTasks(); |
| 223 | 216 |
| 224 assertTrue(callback.getCallbackCalled()); | 217 assertTrue(callback.getCallbackCalled()); |
| 225 } | 218 } |
| 226 | 219 |
| 227 @Test | 220 @Test |
| 228 @Feature({"Webapp"}) | 221 @Feature({"Webapp"}) |
| 229 public void testUnregisterRunsCallback() throws Exception { | 222 public void testUnregisterRunsCallback() throws Exception { |
| 230 CallbackRunner callback = new CallbackRunner(); | 223 CallbackRunner callback = new CallbackRunner(); |
| 231 WebappRegistry.unregisterWebappsForUrls( | 224 WebappRegistry.unregisterWebappsForUrls( |
| 232 RuntimeEnvironment.application, new UrlFilters.AllUrls(), callba
ck); | 225 Robolectric.application, new UrlFilters.AllUrls(), callback); |
| 233 BackgroundShadowAsyncTask.runBackgroundTasks(); | 226 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 234 ShadowLooper.runUiThreadTasks(); | 227 Robolectric.runUiThreadTasks(); |
| 235 | 228 |
| 236 assertTrue(callback.getCallbackCalled()); | 229 assertTrue(callback.getCallbackCalled()); |
| 237 } | 230 } |
| 238 | 231 |
| 239 @Test | 232 @Test |
| 240 @Feature({"Webapp"}) | 233 @Feature({"Webapp"}) |
| 241 public void testUnregisterClearsRegistry() throws Exception { | 234 public void testUnregisterClearsRegistry() throws Exception { |
| 242 Map<String, String> apps = new HashMap<>(); | 235 Map<String, String> apps = new HashMap<>(); |
| 243 apps.put("webapp1", "http://example.com/index.html"); | 236 apps.put("webapp1", "http://example.com/index.html"); |
| 244 apps.put("webapp2", "https://www.google.com/foo/bar"); | 237 apps.put("webapp2", "https://www.google.com/foo/bar"); |
| 245 apps.put("webapp3", "https://www.chrome.com"); | 238 apps.put("webapp3", "https://www.chrome.com"); |
| 246 | 239 |
| 247 for (Map.Entry<String, String> app : apps.entrySet()) { | 240 for (Map.Entry<String, String> app : apps.entrySet()) { |
| 248 WebappRegistry.registerWebapp(RuntimeEnvironment.application, app.ge
tKey(), | 241 WebappRegistry.registerWebapp(Robolectric.application, app.getKey(), |
| 249 new FetchStorageCallback(createShortcutIntent(app.getValue()
))); | 242 new FetchStorageCallback(createShortcutIntent(app.getValue()
))); |
| 250 } | 243 } |
| 251 BackgroundShadowAsyncTask.runBackgroundTasks(); | 244 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 252 ShadowLooper.runUiThreadTasks(); | 245 Robolectric.runUiThreadTasks(); |
| 253 | 246 |
| 254 // Partial deletion. | 247 // Partial deletion. |
| 255 CallbackRunner callback = new CallbackRunner(); | 248 CallbackRunner callback = new CallbackRunner(); |
| 256 WebappRegistry.unregisterWebappsForUrls( | 249 WebappRegistry.unregisterWebappsForUrls( |
| 257 RuntimeEnvironment.application, | 250 Robolectric.application, |
| 258 new UrlFilters.OneUrl("http://example.com/index.html"), callback
); | 251 new UrlFilters.OneUrl("http://example.com/index.html"), callback
); |
| 259 BackgroundShadowAsyncTask.runBackgroundTasks(); | 252 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 260 ShadowLooper.runUiThreadTasks(); | 253 Robolectric.runUiThreadTasks(); |
| 261 | 254 |
| 262 assertTrue(callback.getCallbackCalled()); | 255 assertTrue(callback.getCallbackCalled()); |
| 263 Set<String> registeredWebapps = getRegisteredWebapps(); | 256 Set<String> registeredWebapps = getRegisteredWebapps(); |
| 264 assertEquals(2, registeredWebapps.size()); | 257 assertEquals(2, registeredWebapps.size()); |
| 265 for (String appName : apps.keySet()) { | 258 for (String appName : apps.keySet()) { |
| 266 assertEquals(!TextUtils.equals(appName, "webapp1"), | 259 assertEquals(!TextUtils.equals(appName, "webapp1"), |
| 267 registeredWebapps.contains(appName)); | 260 registeredWebapps.contains(appName)); |
| 268 } | 261 } |
| 269 | 262 |
| 270 // Full deletion. | 263 // Full deletion. |
| 271 callback = new CallbackRunner(); | 264 callback = new CallbackRunner(); |
| 272 WebappRegistry.unregisterWebappsForUrls( | 265 WebappRegistry.unregisterWebappsForUrls( |
| 273 RuntimeEnvironment.application, new UrlFilters.AllUrls(), callba
ck); | 266 Robolectric.application, new UrlFilters.AllUrls(), callback); |
| 274 BackgroundShadowAsyncTask.runBackgroundTasks(); | 267 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 275 ShadowLooper.runUiThreadTasks(); | 268 Robolectric.runUiThreadTasks(); |
| 276 | 269 |
| 277 assertTrue(callback.getCallbackCalled()); | 270 assertTrue(callback.getCallbackCalled()); |
| 278 assertTrue(getRegisteredWebapps().isEmpty()); | 271 assertTrue(getRegisteredWebapps().isEmpty()); |
| 279 } | 272 } |
| 280 | 273 |
| 281 @Test | 274 @Test |
| 282 @Feature({"Webapp"}) | 275 @Feature({"Webapp"}) |
| 283 public void testUnregisterClearsWebappDataStorage() throws Exception { | 276 public void testUnregisterClearsWebappDataStorage() throws Exception { |
| 284 Map<String, String> apps = new HashMap<>(); | 277 Map<String, String> apps = new HashMap<>(); |
| 285 apps.put("webapp1", "http://example.com/index.html"); | 278 apps.put("webapp1", "http://example.com/index.html"); |
| 286 apps.put("webapp2", "https://www.google.com/foo/bar"); | 279 apps.put("webapp2", "https://www.google.com/foo/bar"); |
| 287 apps.put("webapp3", "https://www.chrome.com"); | 280 apps.put("webapp3", "https://www.chrome.com"); |
| 288 | 281 |
| 289 for (Map.Entry<String, String> app : apps.entrySet()) { | 282 for (Map.Entry<String, String> app : apps.entrySet()) { |
| 290 WebappRegistry.registerWebapp(RuntimeEnvironment.application, app.ge
tKey(), | 283 WebappRegistry.registerWebapp(Robolectric.application, app.getKey(), |
| 291 new FetchStorageCallback(createShortcutIntent(app.getValue()
))); | 284 new FetchStorageCallback(createShortcutIntent(app.getValue()
))); |
| 292 } | 285 } |
| 293 BackgroundShadowAsyncTask.runBackgroundTasks(); | 286 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 294 ShadowLooper.runUiThreadTasks(); | 287 Robolectric.runUiThreadTasks(); |
| 295 | 288 |
| 296 for (String appName : apps.keySet()) { | 289 for (String appName : apps.keySet()) { |
| 297 SharedPreferences webAppPrefs = RuntimeEnvironment.application.getSh
aredPreferences( | 290 SharedPreferences webAppPrefs = Robolectric.application.getSharedPre
ferences( |
| 298 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + appName, Contex
t.MODE_PRIVATE); | 291 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + appName, Contex
t.MODE_PRIVATE); |
| 299 webAppPrefs.edit().putLong(WebappDataStorage.KEY_LAST_USED, 100L).ap
ply(); | 292 webAppPrefs.edit().putLong(WebappDataStorage.KEY_LAST_USED, 100L).ap
ply(); |
| 300 } | 293 } |
| 301 | 294 |
| 302 // Partial deletion. | 295 // Partial deletion. |
| 303 WebappRegistry.unregisterWebappsForUrls( | 296 WebappRegistry.unregisterWebappsForUrls( |
| 304 RuntimeEnvironment.application, | 297 Robolectric.application, |
| 305 new UrlFilters.OneUrl("http://example.com/index.html"), null); | 298 new UrlFilters.OneUrl("http://example.com/index.html"), null); |
| 306 | |
| 307 BackgroundShadowAsyncTask.runBackgroundTasks(); | 299 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 308 for (String appName : apps.keySet()) { | 300 for (String appName : apps.keySet()) { |
| 309 SharedPreferences webAppPrefs = RuntimeEnvironment.application.getSh
aredPreferences( | 301 SharedPreferences webAppPrefs = Robolectric.application.getSharedPre
ferences( |
| 310 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + appName, Contex
t.MODE_PRIVATE); | 302 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + appName, Contex
t.MODE_PRIVATE); |
| 311 assertEquals(TextUtils.equals(appName, "webapp1"), webAppPrefs.getAl
l().isEmpty()); | 303 assertEquals(TextUtils.equals(appName, "webapp1"), webAppPrefs.getAl
l().isEmpty()); |
| 312 } | 304 } |
| 313 | 305 |
| 314 // Full deletion. | 306 // Full deletion. |
| 315 WebappRegistry.unregisterWebappsForUrls( | 307 WebappRegistry.unregisterWebappsForUrls( |
| 316 RuntimeEnvironment.application, new UrlFilters.AllUrls(), null); | 308 Robolectric.application, new UrlFilters.AllUrls(), null); |
| 317 BackgroundShadowAsyncTask.runBackgroundTasks(); | 309 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 318 for (String appName : apps.keySet()) { | 310 for (String appName : apps.keySet()) { |
| 319 SharedPreferences webAppPrefs = RuntimeEnvironment.application.getSh
aredPreferences( | 311 SharedPreferences webAppPrefs = Robolectric.application.getSharedPre
ferences( |
| 320 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + appName, Contex
t.MODE_PRIVATE); | 312 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + appName, Contex
t.MODE_PRIVATE); |
| 321 assertTrue(webAppPrefs.getAll().isEmpty()); | 313 assertTrue(webAppPrefs.getAll().isEmpty()); |
| 322 } | 314 } |
| 323 } | 315 } |
| 324 | 316 |
| 325 @Test | 317 @Test |
| 326 @Feature({"Webapp"}) | 318 @Feature({"Webapp"}) |
| 327 public void testCleanupDoesNotRunTooOften() throws Exception { | 319 public void testCleanupDoesNotRunTooOften() throws Exception { |
| 328 // Put the current time to just before the task should run. | 320 // Put the current time to just before the task should run. |
| 329 long currentTime = INITIAL_TIME + WebappRegistry.FULL_CLEANUP_DURATION -
1; | 321 long currentTime = INITIAL_TIME + WebappRegistry.FULL_CLEANUP_DURATION -
1; |
| 330 | 322 |
| 331 addWebappsToRegistry("oldWebapp"); | 323 addWebappsToRegistry("oldWebapp"); |
| 332 SharedPreferences webAppPrefs = RuntimeEnvironment.application.getShared
Preferences( | 324 SharedPreferences webAppPrefs = Robolectric.application.getSharedPrefere
nces( |
| 333 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "oldWebapp", Contex
t.MODE_PRIVATE); | 325 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "oldWebapp", Contex
t.MODE_PRIVATE); |
| 334 webAppPrefs.edit().putLong(WebappDataStorage.KEY_LAST_USED, Long.MIN_VAL
UE).apply(); | 326 webAppPrefs.edit().putLong(WebappDataStorage.KEY_LAST_USED, Long.MIN_VAL
UE).apply(); |
| 335 | 327 |
| 336 WebappRegistry.unregisterOldWebapps(RuntimeEnvironment.application, curr
entTime); | 328 WebappRegistry.unregisterOldWebapps(Robolectric.application, currentTime
); |
| 337 BackgroundShadowAsyncTask.runBackgroundTasks(); | 329 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 338 | 330 |
| 339 Set<String> actual = mSharedPreferences.getStringSet( | 331 Set<String> actual = mSharedPreferences.getStringSet( |
| 340 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); | 332 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); |
| 341 assertEquals(new HashSet<>(Arrays.asList("oldWebapp")), actual); | 333 assertEquals(new HashSet<>(Arrays.asList("oldWebapp")), actual); |
| 342 | 334 |
| 343 long actualLastUsed = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USE
D, | 335 long actualLastUsed = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USE
D, |
| 344 WebappDataStorage.LAST_USED_INVALID); | 336 WebappDataStorage.LAST_USED_INVALID); |
| 345 assertEquals(Long.MIN_VALUE, actualLastUsed); | 337 assertEquals(Long.MIN_VALUE, actualLastUsed); |
| 346 | 338 |
| 347 // The last cleanup time was set to 0 in setUp() so check that this hasn
't changed. | 339 // The last cleanup time was set to 0 in setUp() so check that this hasn
't changed. |
| 348 long lastCleanup = mSharedPreferences.getLong(WebappRegistry.KEY_LAST_CL
EANUP, -1); | 340 long lastCleanup = mSharedPreferences.getLong(WebappRegistry.KEY_LAST_CL
EANUP, -1); |
| 349 assertEquals(INITIAL_TIME, lastCleanup); | 341 assertEquals(INITIAL_TIME, lastCleanup); |
| 350 } | 342 } |
| 351 | 343 |
| 352 @Test | 344 @Test |
| 353 @Feature({"Webapp"}) | 345 @Feature({"Webapp"}) |
| 354 public void testCleanupDoesNotRemoveRecentApps() throws Exception { | 346 public void testCleanupDoesNotRemoveRecentApps() throws Exception { |
| 355 // Put the current time such that the task runs. | 347 // Put the current time such that the task runs. |
| 356 long currentTime = INITIAL_TIME + WebappRegistry.FULL_CLEANUP_DURATION; | 348 long currentTime = INITIAL_TIME + WebappRegistry.FULL_CLEANUP_DURATION; |
| 357 | 349 |
| 358 // Put the last used time just inside the no-cleanup window. | 350 // Put the last used time just inside the no-cleanup window. |
| 359 addWebappsToRegistry("recentWebapp"); | 351 addWebappsToRegistry("recentWebapp"); |
| 360 SharedPreferences webAppPrefs = RuntimeEnvironment.application.getShared
Preferences( | 352 SharedPreferences webAppPrefs = Robolectric.application.getSharedPrefere
nces( |
| 361 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "recentWebapp", Con
text.MODE_PRIVATE); | 353 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "recentWebapp", Con
text.MODE_PRIVATE); |
| 362 long lastUsed = currentTime - WebappRegistry.WEBAPP_UNOPENED_CLEANUP_DUR
ATION + 1; | 354 long lastUsed = currentTime - WebappRegistry.WEBAPP_UNOPENED_CLEANUP_DUR
ATION + 1; |
| 363 webAppPrefs.edit().putLong(WebappDataStorage.KEY_LAST_USED, lastUsed).ap
ply(); | 355 webAppPrefs.edit().putLong(WebappDataStorage.KEY_LAST_USED, lastUsed).ap
ply(); |
| 364 | 356 |
| 365 // Because the time is just inside the window, there should be a cleanup
but the web app | 357 // Because the time is just inside the window, there should be a cleanup
but the web app |
| 366 // should not be deleted as it was used recently. The last cleanup time
should also be | 358 // should not be deleted as it was used recently. The last cleanup time
should also be |
| 367 // set to the current time. | 359 // set to the current time. |
| 368 WebappRegistry.unregisterOldWebapps(RuntimeEnvironment.application, curr
entTime); | 360 WebappRegistry.unregisterOldWebapps(Robolectric.application, currentTime
); |
| 369 BackgroundShadowAsyncTask.runBackgroundTasks(); | 361 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 370 | 362 |
| 371 Set<String> actual = mSharedPreferences.getStringSet( | 363 Set<String> actual = mSharedPreferences.getStringSet( |
| 372 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); | 364 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); |
| 373 assertEquals(new HashSet<>(Arrays.asList("recentWebapp")), actual); | 365 assertEquals(new HashSet<>(Arrays.asList("recentWebapp")), actual); |
| 374 | 366 |
| 375 long actualLastUsed = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USE
D, | 367 long actualLastUsed = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USE
D, |
| 376 WebappDataStorage.LAST_USED_INVALID); | 368 WebappDataStorage.LAST_USED_INVALID); |
| 377 assertEquals(lastUsed, actualLastUsed); | 369 assertEquals(lastUsed, actualLastUsed); |
| 378 | 370 |
| 379 long lastCleanup = mSharedPreferences.getLong(WebappRegistry.KEY_LAST_CL
EANUP, -1); | 371 long lastCleanup = mSharedPreferences.getLong(WebappRegistry.KEY_LAST_CL
EANUP, -1); |
| 380 assertEquals(currentTime, lastCleanup); | 372 assertEquals(currentTime, lastCleanup); |
| 381 } | 373 } |
| 382 | 374 |
| 383 @Test | 375 @Test |
| 384 @Feature({"Webapp"}) | 376 @Feature({"Webapp"}) |
| 385 public void testCleanupRemovesOldApps() throws Exception { | 377 public void testCleanupRemovesOldApps() throws Exception { |
| 386 // Put the current time such that the task runs. | 378 // Put the current time such that the task runs. |
| 387 long currentTime = INITIAL_TIME + WebappRegistry.FULL_CLEANUP_DURATION; | 379 long currentTime = INITIAL_TIME + WebappRegistry.FULL_CLEANUP_DURATION; |
| 388 | 380 |
| 389 // Put the last used time just outside the no-cleanup window. | 381 // Put the last used time just outside the no-cleanup window. |
| 390 addWebappsToRegistry("oldWebapp"); | 382 addWebappsToRegistry("oldWebapp"); |
| 391 SharedPreferences webAppPrefs = RuntimeEnvironment.application.getShared
Preferences( | 383 SharedPreferences webAppPrefs = Robolectric.application.getSharedPrefere
nces( |
| 392 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "oldWebapp", Contex
t.MODE_PRIVATE); | 384 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "oldWebapp", Contex
t.MODE_PRIVATE); |
| 393 long lastUsed = currentTime - WebappRegistry.WEBAPP_UNOPENED_CLEANUP_DUR
ATION; | 385 long lastUsed = currentTime - WebappRegistry.WEBAPP_UNOPENED_CLEANUP_DUR
ATION; |
| 394 webAppPrefs.edit().putLong(WebappDataStorage.KEY_LAST_USED, lastUsed).ap
ply(); | 386 webAppPrefs.edit().putLong(WebappDataStorage.KEY_LAST_USED, lastUsed).ap
ply(); |
| 395 | 387 |
| 396 // Because the time is just inside the window, there should be a cleanup
of old web apps and | 388 // Because the time is just inside the window, there should be a cleanup
of old web apps and |
| 397 // the last cleaned up time should be set to the current time. | 389 // the last cleaned up time should be set to the current time. |
| 398 WebappRegistry.unregisterOldWebapps(RuntimeEnvironment.application, curr
entTime); | 390 WebappRegistry.unregisterOldWebapps(Robolectric.application, currentTime
); |
| 399 BackgroundShadowAsyncTask.runBackgroundTasks(); | 391 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 400 | 392 |
| 401 Set<String> actual = mSharedPreferences.getStringSet( | 393 Set<String> actual = mSharedPreferences.getStringSet( |
| 402 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); | 394 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); |
| 403 assertTrue(actual.isEmpty()); | 395 assertTrue(actual.isEmpty()); |
| 404 | 396 |
| 405 long actualLastUsed = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USE
D, | 397 long actualLastUsed = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USE
D, |
| 406 WebappDataStorage.LAST_USED_INVALID); | 398 WebappDataStorage.LAST_USED_INVALID); |
| 407 assertEquals(WebappDataStorage.LAST_USED_INVALID, actualLastUsed); | 399 assertEquals(WebappDataStorage.LAST_USED_INVALID, actualLastUsed); |
| 408 | 400 |
| 409 long lastCleanup = mSharedPreferences.getLong(WebappRegistry.KEY_LAST_CL
EANUP, -1); | 401 long lastCleanup = mSharedPreferences.getLong(WebappRegistry.KEY_LAST_CL
EANUP, -1); |
| 410 assertEquals(currentTime, lastCleanup); | 402 assertEquals(currentTime, lastCleanup); |
| 411 } | 403 } |
| 412 | 404 |
| 413 @Test | 405 @Test |
| 414 @Feature({"WebApk"}) | 406 @Feature({"WebApk"}) |
| 415 public void testCleanupRemovesUninstalledWebApks() throws Exception { | 407 public void testCleanupRemovesUninstalledWebApks() throws Exception { |
| 416 String webappId1 = "webapk:uninstalledWebApk1"; | 408 String webappId1 = "webapk:uninstalledWebApk1"; |
| 417 String webApkPackage1 = "uninstalledWebApk1"; | 409 String webApkPackage1 = "uninstalledWebApk1"; |
| 418 String webappId2 = "webapk:uninstalledWebApk2"; | 410 String webappId2 = "webapk:uninstalledWebApk2"; |
| 419 String webApkPackage2 = "uninstalledWebApk2"; | 411 String webApkPackage2 = "uninstalledWebApk2"; |
| 420 | 412 |
| 421 FetchStorageCallback storageCallback1 = new FetchStorageCallback( | 413 FetchStorageCallback storageCallback1 = new FetchStorageCallback( |
| 422 createWebApkIntent(webappId1, webApkPackage1)); | 414 createWebApkIntent(webappId1, webApkPackage1)); |
| 423 WebappRegistry.registerWebapp(RuntimeEnvironment.application, webappId1,
storageCallback1); | 415 WebappRegistry.registerWebapp(Robolectric.application, webappId1, storag
eCallback1); |
| 424 BackgroundShadowAsyncTask.runBackgroundTasks(); | 416 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 425 ShadowLooper.runUiThreadTasks(); | 417 Robolectric.runUiThreadTasks(); |
| 426 assertTrue(storageCallback1.getCallbackCalled()); | 418 assertTrue(storageCallback1.getCallbackCalled()); |
| 427 | 419 |
| 428 FetchStorageCallback storageCallback2 = new FetchStorageCallback( | 420 FetchStorageCallback storageCallback2 = new FetchStorageCallback( |
| 429 createWebApkIntent(webappId1, webApkPackage2)); | 421 createWebApkIntent(webappId1, webApkPackage2)); |
| 430 WebappRegistry.registerWebapp(RuntimeEnvironment.application, webappId2,
storageCallback2); | 422 WebappRegistry.registerWebapp(Robolectric.application, webappId2, storag
eCallback2); |
| 431 BackgroundShadowAsyncTask.runBackgroundTasks(); | 423 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 432 ShadowLooper.runUiThreadTasks(); | 424 Robolectric.runUiThreadTasks(); |
| 433 assertTrue(storageCallback2.getCallbackCalled()); | 425 assertTrue(storageCallback2.getCallbackCalled()); |
| 434 | 426 |
| 435 // Verify that both WebAPKs are registered. | 427 // Verify that both WebAPKs are registered. |
| 436 Set<String> actual = mSharedPreferences.getStringSet( | 428 Set<String> actual = mSharedPreferences.getStringSet( |
| 437 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); | 429 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); |
| 438 assertEquals(2, actual.size()); | 430 assertEquals(2, actual.size()); |
| 439 assertTrue(actual.contains(webappId1)); | 431 assertTrue(actual.contains(webappId1)); |
| 440 assertTrue(actual.contains(webappId2)); | 432 assertTrue(actual.contains(webappId2)); |
| 441 | 433 |
| 442 // Set the current time such that the task runs. | 434 // Set the current time such that the task runs. |
| 443 long currentTime = System.currentTimeMillis() + WebappRegistry.FULL_CLEA
NUP_DURATION; | 435 long currentTime = System.currentTimeMillis() + WebappRegistry.FULL_CLEA
NUP_DURATION; |
| 444 // Because the time is just inside the window, there should be a cleanup
of | 436 // Because the time is just inside the window, there should be a cleanup
of |
| 445 // uninstalled WebAPKs and the last cleaned up time should be set to the | 437 // uninstalled WebAPKs and the last cleaned up time should be set to the |
| 446 // current time. | 438 // current time. |
| 447 WebappRegistry.unregisterOldWebapps(RuntimeEnvironment.application, curr
entTime); | 439 WebappRegistry.unregisterOldWebapps(Robolectric.application, currentTime
); |
| 448 BackgroundShadowAsyncTask.runBackgroundTasks(); | 440 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 449 ShadowLooper.runUiThreadTasks(); | 441 Robolectric.runUiThreadTasks(); |
| 450 | 442 |
| 451 actual = mSharedPreferences.getStringSet( | 443 actual = mSharedPreferences.getStringSet( |
| 452 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); | 444 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); |
| 453 assertTrue(actual.isEmpty()); | 445 assertTrue(actual.isEmpty()); |
| 454 | 446 |
| 455 long lastCleanup = mSharedPreferences.getLong( | 447 long lastCleanup = mSharedPreferences.getLong( |
| 456 WebappRegistry.KEY_LAST_CLEANUP, -1); | 448 WebappRegistry.KEY_LAST_CLEANUP, -1); |
| 457 assertEquals(currentTime, lastCleanup); | 449 assertEquals(currentTime, lastCleanup); |
| 458 } | 450 } |
| 459 | 451 |
| 460 @Test | 452 @Test |
| 461 @Feature({"WebApk"}) | 453 @Feature({"WebApk"}) |
| 462 public void testCleanupDoesNotRemoveInstalledWebApks() throws Exception { | 454 public void testCleanupDoesNotRemoveInstalledWebApks() throws Exception { |
| 463 String webappId = "webapk:installedWebApk"; | 455 String webappId = "webapk:installedWebApk"; |
| 464 String webApkPackage = "installedWebApk"; | 456 String webApkPackage = "installedWebApk"; |
| 465 String uninstalledWebappId = "webapk:uninstalledWebApk"; | 457 String uninstalledWebappId = "webapk:uninstalledWebApk"; |
| 466 String uninstalledWebApkPackage = "uninstalledWebApk"; | 458 String uninstalledWebApkPackage = "uninstalledWebApk"; |
| 467 | 459 |
| 468 FetchStorageCallback storageCallback = new FetchStorageCallback( | 460 FetchStorageCallback storageCallback = new FetchStorageCallback( |
| 469 createWebApkIntent(webappId, webApkPackage)); | 461 createWebApkIntent(webappId, webApkPackage)); |
| 470 WebappRegistry.registerWebapp(RuntimeEnvironment.application, webappId,
storageCallback); | 462 WebappRegistry.registerWebapp(Robolectric.application, webappId, storage
Callback); |
| 471 BackgroundShadowAsyncTask.runBackgroundTasks(); | 463 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 472 ShadowLooper.runUiThreadTasks(); | 464 Robolectric.runUiThreadTasks(); |
| 473 assertTrue(storageCallback.getCallbackCalled()); | 465 assertTrue(storageCallback.getCallbackCalled()); |
| 474 | 466 |
| 475 FetchStorageCallback storageCallback2 = new FetchStorageCallback( | 467 FetchStorageCallback storageCallback2 = new FetchStorageCallback( |
| 476 createWebApkIntent(uninstalledWebappId, uninstalledWebApkPackage
)); | 468 createWebApkIntent(uninstalledWebappId, uninstalledWebApkPackage
)); |
| 477 WebappRegistry.registerWebapp(RuntimeEnvironment.application, uninstalle
dWebappId, | 469 WebappRegistry.registerWebapp(Robolectric.application, uninstalledWebapp
Id, |
| 478 storageCallback2); | 470 storageCallback2); |
| 479 BackgroundShadowAsyncTask.runBackgroundTasks(); | 471 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 480 ShadowLooper.runUiThreadTasks(); | 472 Robolectric.runUiThreadTasks(); |
| 481 assertTrue(storageCallback2.getCallbackCalled()); | 473 assertTrue(storageCallback2.getCallbackCalled()); |
| 482 | 474 |
| 483 // Verify that both WebAPKs are registered. | 475 // Verify that both WebAPKs are registered. |
| 484 Set<String> actual = mSharedPreferences.getStringSet( | 476 Set<String> actual = mSharedPreferences.getStringSet( |
| 485 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); | 477 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); |
| 486 assertEquals(2, actual.size()); | 478 assertEquals(2, actual.size()); |
| 487 assertTrue(actual.contains(webappId)); | 479 assertTrue(actual.contains(webappId)); |
| 488 assertTrue(actual.contains(uninstalledWebappId)); | 480 assertTrue(actual.contains(uninstalledWebappId)); |
| 489 | 481 |
| 490 RuntimeEnvironment.getRobolectricPackageManager().addPackage(webApkPacka
ge); | 482 Robolectric.packageManager.addPackage(webApkPackage); |
| 491 | 483 |
| 492 // Set the current time such that the task runs. | 484 // Set the current time such that the task runs. |
| 493 long currentTime = System.currentTimeMillis() + WebappRegistry.FULL_CLEA
NUP_DURATION; | 485 long currentTime = System.currentTimeMillis() + WebappRegistry.FULL_CLEA
NUP_DURATION; |
| 494 // Because the time is just inside the window, there should be a cleanup
of | 486 // Because the time is just inside the window, there should be a cleanup
of |
| 495 // uninstalled WebAPKs and the last cleaned up time should be set to the | 487 // uninstalled WebAPKs and the last cleaned up time should be set to the |
| 496 // current time. | 488 // current time. |
| 497 WebappRegistry.unregisterOldWebapps(RuntimeEnvironment.application, curr
entTime); | 489 WebappRegistry.unregisterOldWebapps(Robolectric.application, currentTime
); |
| 498 BackgroundShadowAsyncTask.runBackgroundTasks(); | 490 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 499 ShadowLooper.runUiThreadTasks(); | 491 Robolectric.runUiThreadTasks(); |
| 500 | 492 |
| 501 actual = mSharedPreferences.getStringSet( | 493 actual = mSharedPreferences.getStringSet( |
| 502 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); | 494 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); |
| 503 assertEquals(1, actual.size()); | 495 assertEquals(1, actual.size()); |
| 504 assertTrue(actual.contains(webappId)); | 496 assertTrue(actual.contains(webappId)); |
| 505 | 497 |
| 506 long lastCleanup = mSharedPreferences.getLong( | 498 long lastCleanup = mSharedPreferences.getLong( |
| 507 WebappRegistry.KEY_LAST_CLEANUP, -1); | 499 WebappRegistry.KEY_LAST_CLEANUP, -1); |
| 508 assertEquals(currentTime, lastCleanup); | 500 assertEquals(currentTime, lastCleanup); |
| 509 } | 501 } |
| 510 | 502 |
| 511 @Test | 503 @Test |
| 512 @Feature({"Webapp"}) | 504 @Feature({"Webapp"}) |
| 513 public void testClearWebappHistoryRunsCallback() throws Exception { | 505 public void testClearWebappHistoryRunsCallback() throws Exception { |
| 514 CallbackRunner callback = new CallbackRunner(); | 506 CallbackRunner callback = new CallbackRunner(); |
| 515 WebappRegistry.clearWebappHistory(RuntimeEnvironment.application, callba
ck); | 507 WebappRegistry.clearWebappHistory(Robolectric.application, callback); |
| 516 BackgroundShadowAsyncTask.runBackgroundTasks(); | 508 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 517 ShadowLooper.runUiThreadTasks(); | 509 Robolectric.runUiThreadTasks(); |
| 518 | 510 |
| 519 assertTrue(callback.getCallbackCalled()); | 511 assertTrue(callback.getCallbackCalled()); |
| 520 } | 512 } |
| 521 | 513 |
| 522 @Test | 514 @Test |
| 523 @Feature({"Webapp"}) | 515 @Feature({"Webapp"}) |
| 524 public void testClearWebappHistory() throws Exception { | 516 public void testClearWebappHistory() throws Exception { |
| 525 final String webapp1Url = "https://www.google.com"; | 517 final String webapp1Url = "https://www.google.com"; |
| 526 final String webapp2Url = "https://drive.google.com"; | 518 final String webapp2Url = "https://drive.google.com"; |
| 527 Intent shortcutIntent1 = createShortcutIntent(webapp1Url); | 519 Intent shortcutIntent1 = createShortcutIntent(webapp1Url); |
| 528 Intent shortcutIntent2 = createShortcutIntent(webapp2Url); | 520 Intent shortcutIntent2 = createShortcutIntent(webapp2Url); |
| 529 | 521 |
| 530 FetchStorageCallback storageCallback1 = new FetchStorageCallback(shortcu
tIntent1); | 522 FetchStorageCallback storageCallback1 = new FetchStorageCallback(shortcu
tIntent1); |
| 531 WebappRegistry.registerWebapp(RuntimeEnvironment.application, "webapp1",
storageCallback1); | 523 WebappRegistry.registerWebapp(Robolectric.application, "webapp1", storag
eCallback1); |
| 532 BackgroundShadowAsyncTask.runBackgroundTasks(); | 524 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 533 ShadowLooper.runUiThreadTasks(); | 525 Robolectric.runUiThreadTasks(); |
| 534 assertTrue(storageCallback1.getCallbackCalled()); | 526 assertTrue(storageCallback1.getCallbackCalled()); |
| 535 | 527 |
| 536 FetchStorageCallback storageCallback2 = new FetchStorageCallback(shortcu
tIntent2); | 528 FetchStorageCallback storageCallback2 = new FetchStorageCallback(shortcu
tIntent2); |
| 537 WebappRegistry.registerWebapp(RuntimeEnvironment.application, "webapp2",
storageCallback2); | 529 WebappRegistry.registerWebapp(Robolectric.application, "webapp2", storag
eCallback2); |
| 538 BackgroundShadowAsyncTask.runBackgroundTasks(); | 530 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 539 ShadowLooper.runUiThreadTasks(); | 531 Robolectric.runUiThreadTasks(); |
| 540 assertTrue(storageCallback2.getCallbackCalled()); | 532 assertTrue(storageCallback2.getCallbackCalled()); |
| 541 | 533 |
| 542 SharedPreferences webapp1Prefs = RuntimeEnvironment.application.getShare
dPreferences( | 534 SharedPreferences webapp1Prefs = Robolectric.application.getSharedPrefer
ences( |
| 543 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "webapp1", Context.
MODE_PRIVATE); | 535 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "webapp1", Context.
MODE_PRIVATE); |
| 544 SharedPreferences webapp2Prefs = RuntimeEnvironment.application.getShare
dPreferences( | 536 SharedPreferences webapp2Prefs = Robolectric.application.getSharedPrefer
ences( |
| 545 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "webapp2", Context.
MODE_PRIVATE); | 537 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "webapp2", Context.
MODE_PRIVATE); |
| 546 | 538 |
| 547 CallbackRunner callback = new CallbackRunner(); | 539 CallbackRunner callback = new CallbackRunner(); |
| 548 WebappRegistry.clearWebappHistory(RuntimeEnvironment.application, callba
ck); | 540 WebappRegistry.clearWebappHistory(Robolectric.application, callback); |
| 549 BackgroundShadowAsyncTask.runBackgroundTasks(); | 541 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 550 ShadowLooper.runUiThreadTasks(); | 542 Robolectric.runUiThreadTasks(); |
| 551 assertTrue(callback.getCallbackCalled()); | 543 assertTrue(callback.getCallbackCalled()); |
| 552 | 544 |
| 553 Set<String> actual = mSharedPreferences.getStringSet( | 545 Set<String> actual = mSharedPreferences.getStringSet( |
| 554 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); | 546 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); |
| 555 assertEquals(2, actual.size()); | 547 assertEquals(2, actual.size()); |
| 556 assertTrue(actual.contains("webapp1")); | 548 assertTrue(actual.contains("webapp1")); |
| 557 assertTrue(actual.contains("webapp2")); | 549 assertTrue(actual.contains("webapp2")); |
| 558 | 550 |
| 559 // Verify that the last used time for both web apps is WebappDataStorage
.LAST_USED_UNSET. | 551 // Verify that the last used time for both web apps is WebappDataStorage
.LAST_USED_UNSET. |
| 560 long actualLastUsed = webapp1Prefs.getLong( | 552 long actualLastUsed = webapp1Prefs.getLong( |
| (...skipping 14 matching lines...) Expand all Loading... |
| 575 WebappDataStorage.KEY_SCOPE, WebappDataStorage.URL_INVALID); | 567 WebappDataStorage.KEY_SCOPE, WebappDataStorage.URL_INVALID); |
| 576 assertEquals(WebappDataStorage.URL_INVALID, actualScope); | 568 assertEquals(WebappDataStorage.URL_INVALID, actualScope); |
| 577 actualUrl = webapp2Prefs.getString( | 569 actualUrl = webapp2Prefs.getString( |
| 578 WebappDataStorage.KEY_URL, WebappDataStorage.URL_INVALID); | 570 WebappDataStorage.KEY_URL, WebappDataStorage.URL_INVALID); |
| 579 assertEquals(WebappDataStorage.URL_INVALID, actualUrl); | 571 assertEquals(WebappDataStorage.URL_INVALID, actualUrl); |
| 580 } | 572 } |
| 581 | 573 |
| 582 @Test | 574 @Test |
| 583 @Feature({"Webapp"}) | 575 @Feature({"Webapp"}) |
| 584 public void testGetAfterClearWebappHistory() throws Exception { | 576 public void testGetAfterClearWebappHistory() throws Exception { |
| 585 WebappRegistry.registerWebapp(RuntimeEnvironment.application, "webapp",
null); | 577 WebappRegistry.registerWebapp(Robolectric.application, "webapp", null); |
| 586 BackgroundShadowAsyncTask.runBackgroundTasks(); | 578 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 587 | 579 |
| 588 SharedPreferences webappPrefs = RuntimeEnvironment.application.getShared
Preferences( | 580 SharedPreferences webappPrefs = Robolectric.application.getSharedPrefere
nces( |
| 589 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "webapp", Context.M
ODE_PRIVATE); | 581 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "webapp", Context.M
ODE_PRIVATE); |
| 590 CallbackRunner callback = new CallbackRunner(); | 582 CallbackRunner callback = new CallbackRunner(); |
| 591 WebappRegistry.clearWebappHistory(RuntimeEnvironment.application, callba
ck); | 583 WebappRegistry.clearWebappHistory(Robolectric.application, callback); |
| 592 BackgroundShadowAsyncTask.runBackgroundTasks(); | 584 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 593 ShadowLooper.runUiThreadTasks(); | 585 Robolectric.runUiThreadTasks(); |
| 594 assertTrue(callback.getCallbackCalled()); | 586 assertTrue(callback.getCallbackCalled()); |
| 595 | 587 |
| 596 // Open the webapp up to set the last used time. | 588 // Open the webapp up to set the last used time. |
| 597 FetchStorageCallback storageCallback = new FetchStorageCallback(null); | 589 FetchStorageCallback storageCallback = new FetchStorageCallback(null); |
| 598 WebappRegistry.getWebappDataStorage( | 590 WebappRegistry.getWebappDataStorage(Robolectric.application, "webapp", s
torageCallback); |
| 599 RuntimeEnvironment.application, "webapp", storageCallback); | |
| 600 BackgroundShadowAsyncTask.runBackgroundTasks(); | 591 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 601 ShadowLooper.runUiThreadTasks(); | 592 Robolectric.runUiThreadTasks(); |
| 602 assertTrue(storageCallback.getCallbackCalled()); | 593 assertTrue(storageCallback.getCallbackCalled()); |
| 603 | 594 |
| 604 // Verify that the last used time is valid. | 595 // Verify that the last used time is valid. |
| 605 long actualLastUsed = webappPrefs.getLong( | 596 long actualLastUsed = webappPrefs.getLong( |
| 606 WebappDataStorage.KEY_LAST_USED, WebappDataStorage.LAST_USED_INV
ALID); | 597 WebappDataStorage.KEY_LAST_USED, WebappDataStorage.LAST_USED_INV
ALID); |
| 607 assertTrue(WebappDataStorage.LAST_USED_INVALID != actualLastUsed); | 598 assertTrue(WebappDataStorage.LAST_USED_INVALID != actualLastUsed); |
| 608 assertTrue(WebappDataStorage.LAST_USED_UNSET != actualLastUsed); | 599 assertTrue(WebappDataStorage.LAST_USED_UNSET != actualLastUsed); |
| 609 } | 600 } |
| 610 | 601 |
| 611 @Test | 602 @Test |
| 612 @Feature({"Webapp"}) | 603 @Feature({"Webapp"}) |
| 613 public void testUpdateAfterClearWebappHistory() throws Exception { | 604 public void testUpdateAfterClearWebappHistory() throws Exception { |
| 614 final String webappUrl = "http://www.google.com"; | 605 final String webappUrl = "http://www.google.com"; |
| 615 final String webappScope = "http://www.google.com/"; | 606 final String webappScope = "http://www.google.com/"; |
| 616 final Intent shortcutIntent = createShortcutIntent(webappUrl); | 607 final Intent shortcutIntent = createShortcutIntent(webappUrl); |
| 617 WebappRegistry.registerWebapp(RuntimeEnvironment.application, "webapp", | 608 WebappRegistry.registerWebapp(Robolectric.application, "webapp", |
| 618 new FetchStorageCallback(shortcutIntent)); | 609 new FetchStorageCallback(shortcutIntent)); |
| 619 BackgroundShadowAsyncTask.runBackgroundTasks(); | 610 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 620 ShadowLooper.runUiThreadTasks(); | 611 Robolectric.runUiThreadTasks(); |
| 621 | 612 |
| 622 SharedPreferences webappPrefs = RuntimeEnvironment.application.getShared
Preferences( | 613 SharedPreferences webappPrefs = Robolectric.application.getSharedPrefere
nces( |
| 623 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "webapp", Context.M
ODE_PRIVATE); | 614 WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "webapp", Context.M
ODE_PRIVATE); |
| 624 | 615 |
| 625 // Verify that the URL and scope match the original in the intent. | 616 // Verify that the URL and scope match the original in the intent. |
| 626 String actualUrl = webappPrefs.getString( | 617 String actualUrl = webappPrefs.getString( |
| 627 WebappDataStorage.KEY_URL, WebappDataStorage.URL_INVALID); | 618 WebappDataStorage.KEY_URL, WebappDataStorage.URL_INVALID); |
| 628 assertEquals(webappUrl, actualUrl); | 619 assertEquals(webappUrl, actualUrl); |
| 629 String actualScope = webappPrefs.getString( | 620 String actualScope = webappPrefs.getString( |
| 630 WebappDataStorage.KEY_SCOPE, WebappDataStorage.URL_INVALID); | 621 WebappDataStorage.KEY_SCOPE, WebappDataStorage.URL_INVALID); |
| 631 assertEquals(webappScope, actualScope); | 622 assertEquals(webappScope, actualScope); |
| 632 | 623 |
| 633 WebappRegistry.clearWebappHistory(RuntimeEnvironment.application, new Ca
llbackRunner()); | 624 WebappRegistry.clearWebappHistory(Robolectric.application, new CallbackR
unner()); |
| 634 BackgroundShadowAsyncTask.runBackgroundTasks(); | 625 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 635 ShadowLooper.runUiThreadTasks(); | 626 Robolectric.runUiThreadTasks(); |
| 636 | 627 |
| 637 // Update the webapp from the intent again. | 628 // Update the webapp from the intent again. |
| 638 WebappRegistry.getWebappDataStorage(RuntimeEnvironment.application, "web
app", | 629 WebappRegistry.getWebappDataStorage(Robolectric.application, "webapp", |
| 639 new FetchStorageCallback(shortcutIntent)); | 630 new FetchStorageCallback(shortcutIntent)); |
| 640 BackgroundShadowAsyncTask.runBackgroundTasks(); | 631 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 641 ShadowLooper.runUiThreadTasks(); | 632 Robolectric.runUiThreadTasks(); |
| 642 | 633 |
| 643 // Verify that the URL and scope match the original in the intent. | 634 // Verify that the URL and scope match the original in the intent. |
| 644 actualUrl = webappPrefs.getString(WebappDataStorage.KEY_URL, WebappDataS
torage.URL_INVALID); | 635 actualUrl = webappPrefs.getString(WebappDataStorage.KEY_URL, WebappDataS
torage.URL_INVALID); |
| 645 assertEquals(webappUrl, actualUrl); | 636 assertEquals(webappUrl, actualUrl); |
| 646 actualScope = webappPrefs.getString( | 637 actualScope = webappPrefs.getString( |
| 647 WebappDataStorage.KEY_SCOPE, WebappDataStorage.URL_INVALID); | 638 WebappDataStorage.KEY_SCOPE, WebappDataStorage.URL_INVALID); |
| 648 assertEquals(webappScope, actualScope); | 639 assertEquals(webappScope, actualScope); |
| 649 } | 640 } |
| 650 | 641 |
| 651 @Test | 642 @Test |
| (...skipping 15 matching lines...) Expand all Loading... |
| 667 final String test4Url = "https://www.google.com/drive/docs/recent/index.
html"; | 658 final String test4Url = "https://www.google.com/drive/docs/recent/index.
html"; |
| 668 final String test5Url = "https://drive.google.com/docs/recent/trash"; | 659 final String test5Url = "https://drive.google.com/docs/recent/trash"; |
| 669 final String test6Url = "https://maps.google.com/"; | 660 final String test6Url = "https://maps.google.com/"; |
| 670 | 661 |
| 671 Intent shortcutIntent1 = createShortcutIntent(webapp1Url); | 662 Intent shortcutIntent1 = createShortcutIntent(webapp1Url); |
| 672 Intent shortcutIntent2 = createShortcutIntent(webapp2Url); | 663 Intent shortcutIntent2 = createShortcutIntent(webapp2Url); |
| 673 Intent shortcutIntent3 = createShortcutIntent(webapp3Url); | 664 Intent shortcutIntent3 = createShortcutIntent(webapp3Url); |
| 674 Intent shortcutIntent4 = createShortcutIntent(webapp4Url); | 665 Intent shortcutIntent4 = createShortcutIntent(webapp4Url); |
| 675 | 666 |
| 676 // Register the four web apps. | 667 // Register the four web apps. |
| 677 WebappRegistry.registerWebapp(RuntimeEnvironment.application, "webapp1", | 668 WebappRegistry.registerWebapp(Robolectric.application, "webapp1", |
| 678 new FetchStorageCallback(shortcutIntent1)); | 669 new FetchStorageCallback(shortcutIntent1)); |
| 679 BackgroundShadowAsyncTask.runBackgroundTasks(); | 670 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 680 ShadowLooper.runUiThreadTasks(); | 671 Robolectric.runUiThreadTasks(); |
| 681 | 672 |
| 682 WebappRegistry.registerWebapp(RuntimeEnvironment.application, "webapp2", | 673 WebappRegistry.registerWebapp(Robolectric.application, "webapp2", |
| 683 new FetchStorageCallback(shortcutIntent2)); | 674 new FetchStorageCallback(shortcutIntent2)); |
| 684 BackgroundShadowAsyncTask.runBackgroundTasks(); | 675 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 685 ShadowLooper.runUiThreadTasks(); | 676 Robolectric.runUiThreadTasks(); |
| 686 | 677 |
| 687 WebappRegistry.registerWebapp(RuntimeEnvironment.application, "webapp3", | 678 WebappRegistry.registerWebapp(Robolectric.application, "webapp3", |
| 688 new FetchStorageCallback(shortcutIntent3)); | 679 new FetchStorageCallback(shortcutIntent3)); |
| 689 BackgroundShadowAsyncTask.runBackgroundTasks(); | 680 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 690 ShadowLooper.runUiThreadTasks(); | 681 Robolectric.runUiThreadTasks(); |
| 691 | 682 |
| 692 WebappRegistry.registerWebapp(RuntimeEnvironment.application, "webapp4", | 683 WebappRegistry.registerWebapp(Robolectric.application, "webapp4", |
| 693 new FetchStorageCallback(shortcutIntent4)); | 684 new FetchStorageCallback(shortcutIntent4)); |
| 694 BackgroundShadowAsyncTask.runBackgroundTasks(); | 685 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 695 ShadowLooper.runUiThreadTasks(); | 686 Robolectric.runUiThreadTasks(); |
| 696 | 687 |
| 697 // test1Url should return webapp1. | 688 // test1Url should return webapp1. |
| 698 FetchStorageByUrlCallback callback = new FetchStorageByUrlCallback(webap
p1Url, webapp1Url); | 689 FetchStorageByUrlCallback callback = new FetchStorageByUrlCallback(webap
p1Url, webapp1Url); |
| 699 WebappRegistry.getWebappDataStorageForUrl( | 690 WebappRegistry.getWebappDataStorageForUrl(Robolectric.application, test1
Url, callback); |
| 700 RuntimeEnvironment.application, test1Url, callback); | |
| 701 BackgroundShadowAsyncTask.runBackgroundTasks(); | 691 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 702 ShadowLooper.runUiThreadTasks(); | 692 Robolectric.runUiThreadTasks(); |
| 703 assertTrue(callback.getCallbackCalled()); | 693 assertTrue(callback.getCallbackCalled()); |
| 704 | 694 |
| 705 // test2Url should return webapp3. | 695 // test2Url should return webapp3. |
| 706 callback = new FetchStorageByUrlCallback(webapp3Url, webapp3Scope); | 696 callback = new FetchStorageByUrlCallback(webapp3Url, webapp3Scope); |
| 707 WebappRegistry.getWebappDataStorageForUrl( | 697 WebappRegistry.getWebappDataStorageForUrl(Robolectric.application, test2
Url, callback); |
| 708 RuntimeEnvironment.application, test2Url, callback); | |
| 709 BackgroundShadowAsyncTask.runBackgroundTasks(); | 698 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 710 ShadowLooper.runUiThreadTasks(); | 699 Robolectric.runUiThreadTasks(); |
| 711 assertTrue(callback.getCallbackCalled()); | 700 assertTrue(callback.getCallbackCalled()); |
| 712 | 701 |
| 713 // test3Url should return webapp4. | 702 // test3Url should return webapp4. |
| 714 callback = new FetchStorageByUrlCallback(webapp4Url, webapp4Scope); | 703 callback = new FetchStorageByUrlCallback(webapp4Url, webapp4Scope); |
| 715 WebappRegistry.getWebappDataStorageForUrl( | 704 WebappRegistry.getWebappDataStorageForUrl(Robolectric.application, test3
Url, callback); |
| 716 RuntimeEnvironment.application, test3Url, callback); | |
| 717 BackgroundShadowAsyncTask.runBackgroundTasks(); | 705 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 718 ShadowLooper.runUiThreadTasks(); | 706 Robolectric.runUiThreadTasks(); |
| 719 assertTrue(callback.getCallbackCalled()); | 707 assertTrue(callback.getCallbackCalled()); |
| 720 | 708 |
| 721 // test4Url should return webapp4. | 709 // test4Url should return webapp4. |
| 722 callback = new FetchStorageByUrlCallback(webapp4Url, webapp4Scope); | 710 callback = new FetchStorageByUrlCallback(webapp4Url, webapp4Scope); |
| 723 WebappRegistry.getWebappDataStorageForUrl( | 711 WebappRegistry.getWebappDataStorageForUrl(Robolectric.application, test4
Url, callback); |
| 724 RuntimeEnvironment.application, test4Url, callback); | |
| 725 BackgroundShadowAsyncTask.runBackgroundTasks(); | 712 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 726 ShadowLooper.runUiThreadTasks(); | 713 Robolectric.runUiThreadTasks(); |
| 727 assertTrue(callback.getCallbackCalled()); | 714 assertTrue(callback.getCallbackCalled()); |
| 728 | 715 |
| 729 // test5Url should return webapp2. | 716 // test5Url should return webapp2. |
| 730 callback = new FetchStorageByUrlCallback(webapp2Url, webapp2Url); | 717 callback = new FetchStorageByUrlCallback(webapp2Url, webapp2Url); |
| 731 WebappRegistry.getWebappDataStorageForUrl( | 718 WebappRegistry.getWebappDataStorageForUrl(Robolectric.application, test5
Url, callback); |
| 732 RuntimeEnvironment.application, test5Url, callback); | |
| 733 BackgroundShadowAsyncTask.runBackgroundTasks(); | 719 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 734 ShadowLooper.runUiThreadTasks(); | 720 Robolectric.runUiThreadTasks(); |
| 735 assertTrue(callback.getCallbackCalled()); | 721 assertTrue(callback.getCallbackCalled()); |
| 736 | 722 |
| 737 // test6Url doesn't correspond to a web app, so the storage returned is
null. | 723 // test6Url doesn't correspond to a web app, so the storage returned is
null. |
| 738 // This must use a member variable; local variables must be final or eff
ectively final to be | 724 // This must use a member variable; local variables must be final or eff
ectively final to be |
| 739 // accessible inside an inner class. | 725 // accessible inside an inner class. |
| 740 mCallbackCalled = false; | 726 mCallbackCalled = false; |
| 741 WebappRegistry.getWebappDataStorageForUrl(RuntimeEnvironment.application
, test6Url, | 727 WebappRegistry.getWebappDataStorageForUrl(Robolectric.application, test6
Url, |
| 742 new WebappRegistry.FetchWebappDataStorageCallback() { | 728 new WebappRegistry.FetchWebappDataStorageCallback() { |
| 743 @Override | 729 @Override |
| 744 public void onWebappDataStorageRetrieved(WebappDataStorage s
torage) { | 730 public void onWebappDataStorageRetrieved(WebappDataStorage s
torage) { |
| 745 assertEquals(null, storage); | 731 assertEquals(null, storage); |
| 746 mCallbackCalled = true; | 732 mCallbackCalled = true; |
| 747 } | 733 } |
| 748 } | 734 } |
| 749 ); | 735 ); |
| 750 BackgroundShadowAsyncTask.runBackgroundTasks(); | 736 BackgroundShadowAsyncTask.runBackgroundTasks(); |
| 751 ShadowLooper.runUiThreadTasks(); | 737 Robolectric.runUiThreadTasks(); |
| 752 assertTrue(mCallbackCalled); | 738 assertTrue(mCallbackCalled); |
| 753 } | 739 } |
| 754 | 740 |
| 755 private Set<String> addWebappsToRegistry(String... webapps) { | 741 private Set<String> addWebappsToRegistry(String... webapps) { |
| 756 final Set<String> expected = new HashSet<>(Arrays.asList(webapps)); | 742 final Set<String> expected = new HashSet<>(Arrays.asList(webapps)); |
| 757 mSharedPreferences.edit().putStringSet(WebappRegistry.KEY_WEBAPP_SET, ex
pected).apply(); | 743 mSharedPreferences.edit().putStringSet(WebappRegistry.KEY_WEBAPP_SET, ex
pected).apply(); |
| 758 return expected; | 744 return expected; |
| 759 } | 745 } |
| 760 | 746 |
| 761 private Set<String> getRegisteredWebapps() { | 747 private Set<String> getRegisteredWebapps() { |
| 762 return mSharedPreferences.getStringSet( | 748 return mSharedPreferences.getStringSet( |
| 763 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); | 749 WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); |
| 764 } | 750 } |
| 765 | 751 |
| 766 private Intent createShortcutIntent(String url) { | 752 private Intent createShortcutIntent(String url) { |
| 767 return ShortcutHelper.createWebappShortcutIntent("id", "action", url, | 753 return ShortcutHelper.createWebappShortcutIntent("id", "action", url, |
| 768 ShortcutHelper.getScopeFromUrl(url), "name", "shortName", null, | 754 ShortcutHelper.getScopeFromUrl(url), "name", "shortName", null, |
| 769 ShortcutHelper.WEBAPP_SHORTCUT_VERSION, WebDisplayMode.Standalon
e, 0, 0, 0, false); | 755 ShortcutHelper.WEBAPP_SHORTCUT_VERSION, WebDisplayMode.Standalon
e, 0, 0, 0, false); |
| 770 } | 756 } |
| 771 | 757 |
| 772 private Intent createWebApkIntent(String webappId, String webApkPackage) { | 758 private Intent createWebApkIntent(String webappId, String webApkPackage) { |
| 773 Intent intent = new Intent(); | 759 Intent intent = new Intent(); |
| 774 intent.putExtra(ShortcutHelper.EXTRA_ID, webappId) | 760 intent.putExtra(ShortcutHelper.EXTRA_ID, webappId) |
| 775 .putExtra(ShortcutHelper.EXTRA_URL, "https://foo.com") | 761 .putExtra(ShortcutHelper.EXTRA_URL, "https://foo.com") |
| 776 .putExtra(ShortcutHelper.EXTRA_WEBAPK_PACKAGE_NAME, webApkPackage)
; | 762 .putExtra(ShortcutHelper.EXTRA_WEBAPK_PACKAGE_NAME, webApkPackage)
; |
| 777 return intent; | 763 return intent; |
| 778 } | 764 } |
| 779 } | 765 } |
| OLD | NEW |