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 6b8f54f0af805ca944e46dd7a0e25d2233bd882a..7be71ccd88e44b470d573ca5fa6ccb165e701e6d 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 |
@@ -52,6 +52,27 @@ |
private SharedPreferences mSharedPreferences; |
private boolean mCallbackCalled; |
+ private static class FetchCallback implements WebappRegistry.FetchCallback { |
+ boolean mCallbackCalled; |
+ |
+ Set<String> mExpected; |
+ |
+ FetchCallback(Set<String> expected) { |
+ mCallbackCalled = false; |
+ mExpected = expected; |
+ } |
+ |
+ @Override |
+ public void onWebappIdsRetrieved(Set<String> actual) { |
+ mCallbackCalled = true; |
+ assertEquals(mExpected, actual); |
+ } |
+ |
+ boolean getCallbackCalled() { |
+ return mCallbackCalled; |
+ } |
+ } |
+ |
private static class FetchStorageCallback |
implements WebappRegistry.FetchWebappDataStorageCallback { |
Intent mShortcutIntent; |
@@ -66,6 +87,48 @@ |
public void onWebappDataStorageRetrieved(WebappDataStorage storage) { |
mCallbackCalled = true; |
storage.updateFromShortcutIntent(mShortcutIntent); |
+ storage.updateLastUsedTime(); |
+ } |
+ |
+ boolean getCallbackCalled() { |
+ return mCallbackCalled; |
+ } |
+ } |
+ |
+ private static class FetchStorageByUrlCallback |
+ implements WebappRegistry.FetchWebappDataStorageCallback { |
+ String mUrl; |
+ String mScope; |
+ boolean mCallbackCalled; |
+ |
+ FetchStorageByUrlCallback(String url, String scope) { |
+ mCallbackCalled = false; |
+ mUrl = url; |
+ mScope = scope; |
+ } |
+ |
+ @Override |
+ public void onWebappDataStorageRetrieved(WebappDataStorage storage) { |
+ mCallbackCalled = true; |
+ assertEquals(mUrl, storage.getUrl()); |
+ assertEquals(mScope, storage.getScope()); |
+ } |
+ |
+ boolean getCallbackCalled() { |
+ return mCallbackCalled; |
+ } |
+ } |
+ |
+ private static class CallbackRunner implements Runnable { |
+ boolean mCallbackCalled; |
+ |
+ public CallbackRunner() { |
+ mCallbackCalled = false; |
+ } |
+ |
+ @Override |
+ public void run() { |
+ mCallbackCalled = true; |
} |
boolean getCallbackCalled() { |
@@ -76,7 +139,6 @@ |
@Before |
public void setUp() throws Exception { |
ContextUtils.initApplicationContextForTests(RuntimeEnvironment.application); |
- WebappRegistry.refreshSharedPrefsForTesting(); |
mSharedPreferences = ContextUtils.getApplicationContext().getSharedPreferences( |
REGISTRY_FILE_NAME, Context.MODE_PRIVATE); |
mSharedPreferences.edit().putLong(KEY_LAST_CLEANUP, INITIAL_TIME).commit(); |
@@ -87,16 +149,6 @@ |
@After |
public void tearDown() { |
mSharedPreferences.edit().clear().apply(); |
- } |
- |
- private void registerWebapp(String webappId, |
- WebappRegistry.FetchWebappDataStorageCallback callback) throws Exception { |
- WebappRegistry.getInstance().register(webappId, callback); |
- |
- // Run background tasks to make sure the data is committed. Run UI thread tasks to make sure |
- // the last used time is updated. |
- BackgroundShadowAsyncTask.runBackgroundTasks(); |
- ShadowLooper.runUiThreadTasks(); |
} |
@Test |
@@ -110,8 +162,12 @@ |
@Test |
@Feature({"Webapp"}) |
public void testWebappRegistrationAddsToSharedPrefs() throws Exception { |
- registerWebapp("test", null); |
- Set<String> actual = getRegisteredWebapps(); |
+ WebappRegistry.registerWebapp("test", null); |
+ |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
+ |
+ Set<String> actual = mSharedPreferences.getStringSet( |
+ WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); |
assertEquals(1, actual.size()); |
assertTrue(actual.contains("test")); |
} |
@@ -119,9 +175,11 @@ |
@Test |
@Feature({"Webapp"}) |
public void testWebappRegistrationUpdatesLastUsed() throws Exception { |
- registerWebapp("test", null); |
- |
+ WebappRegistry.registerWebapp("test", null); |
+ |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
long after = System.currentTimeMillis(); |
+ |
SharedPreferences webAppPrefs = ContextUtils.getApplicationContext().getSharedPreferences( |
WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "test", Context.MODE_PRIVATE); |
long actual = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USED, |
@@ -133,24 +191,56 @@ |
@Feature({"Webapp"}) |
public void testWebappIdsRetrieval() throws Exception { |
final Set<String> expected = addWebappsToRegistry("first", "second"); |
- assertEquals(expected, WebappRegistry.getRegisteredWebappIdsForTesting()); |
+ |
+ FetchCallback callback = new FetchCallback(expected); |
+ WebappRegistry.getRegisteredWebappIds(callback); |
+ |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
+ ShadowLooper.runUiThreadTasks(); |
+ |
+ assertTrue(callback.getCallbackCalled()); |
} |
@Test |
@Feature({"Webapp"}) |
public void testWebappIdsRetrievalRegisterRetrival() throws Exception { |
final Set<String> expected = addWebappsToRegistry("first"); |
- assertEquals(expected, WebappRegistry.getRegisteredWebappIdsForTesting()); |
- |
- // Force a re-read of the preferences from disk. Add a new web app via the registry. |
- WebappRegistry.refreshSharedPrefsForTesting(); |
- registerWebapp("second", null); |
+ |
+ FetchCallback callback = new FetchCallback(expected); |
+ WebappRegistry.getRegisteredWebappIds(callback); |
+ |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
+ ShadowLooper.runUiThreadTasks(); |
+ |
+ assertTrue(callback.getCallbackCalled()); |
+ |
+ WebappRegistry.registerWebapp("second", null); |
+ |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
// A copy of the expected set needs to be made as the SharedPreferences is using the copy |
- // that was passed to it. |
+ // that was paassed to it. |
final Set<String> secondExpected = new HashSet<>(expected); |
secondExpected.add("second"); |
- assertEquals(secondExpected, WebappRegistry.getRegisteredWebappIdsForTesting()); |
+ |
+ callback = new FetchCallback(secondExpected); |
+ WebappRegistry.getRegisteredWebappIds(callback); |
+ |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
+ ShadowLooper.runUiThreadTasks(); |
+ |
+ assertTrue(callback.getCallbackCalled()); |
+ } |
+ |
+ @Test |
+ @Feature({"Webapp"}) |
+ public void testUnregisterRunsCallback() throws Exception { |
+ CallbackRunner callback = new CallbackRunner(); |
+ WebappRegistry.unregisterWebappsForUrls(new UrlFilters.AllUrls(), callback); |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
+ ShadowLooper.runUiThreadTasks(); |
+ |
+ assertTrue(callback.getCallbackCalled()); |
} |
@Test |
@@ -162,14 +252,20 @@ |
apps.put("webapp3", "https://www.chrome.com"); |
for (Map.Entry<String, String> app : apps.entrySet()) { |
- registerWebapp( |
+ WebappRegistry.registerWebapp( |
app.getKey(), new FetchStorageCallback(createShortcutIntent(app.getValue()))); |
} |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
+ ShadowLooper.runUiThreadTasks(); |
// Partial deletion. |
- WebappRegistry.getInstance().unregisterWebappsForUrlsImpl( |
- new UrlFilters.OneUrl("http://example.com/index.html")); |
- |
+ CallbackRunner callback = new CallbackRunner(); |
+ WebappRegistry.unregisterWebappsForUrls( |
+ new UrlFilters.OneUrl("http://example.com/index.html"), callback); |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
+ ShadowLooper.runUiThreadTasks(); |
+ |
+ assertTrue(callback.getCallbackCalled()); |
Set<String> registeredWebapps = getRegisteredWebapps(); |
assertEquals(2, registeredWebapps.size()); |
for (String appName : apps.keySet()) { |
@@ -178,7 +274,12 @@ |
} |
// Full deletion. |
- WebappRegistry.getInstance().unregisterWebappsForUrlsImpl(new UrlFilters.AllUrls()); |
+ callback = new CallbackRunner(); |
+ WebappRegistry.unregisterWebappsForUrls(new UrlFilters.AllUrls(), callback); |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
+ ShadowLooper.runUiThreadTasks(); |
+ |
+ assertTrue(callback.getCallbackCalled()); |
assertTrue(getRegisteredWebapps().isEmpty()); |
} |
@@ -191,9 +292,11 @@ |
apps.put("webapp3", "https://www.chrome.com"); |
for (Map.Entry<String, String> app : apps.entrySet()) { |
- registerWebapp( |
+ WebappRegistry.registerWebapp( |
app.getKey(), new FetchStorageCallback(createShortcutIntent(app.getValue()))); |
} |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
+ ShadowLooper.runUiThreadTasks(); |
for (String appName : apps.keySet()) { |
SharedPreferences webAppPrefs = |
@@ -204,9 +307,10 @@ |
} |
// Partial deletion. |
- WebappRegistry.getInstance().unregisterWebappsForUrlsImpl( |
- new UrlFilters.OneUrl("http://example.com/index.html")); |
- |
+ WebappRegistry.unregisterWebappsForUrls( |
+ new UrlFilters.OneUrl("http://example.com/index.html"), null); |
+ |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
for (String appName : apps.keySet()) { |
SharedPreferences webAppPrefs = |
ContextUtils.getApplicationContext().getSharedPreferences( |
@@ -216,7 +320,8 @@ |
} |
// Full deletion. |
- WebappRegistry.getInstance().unregisterWebappsForUrlsImpl(new UrlFilters.AllUrls()); |
+ WebappRegistry.unregisterWebappsForUrls(new UrlFilters.AllUrls(), null); |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
for (String appName : apps.keySet()) { |
SharedPreferences webAppPrefs = |
ContextUtils.getApplicationContext().getSharedPreferences( |
@@ -237,11 +342,11 @@ |
WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "oldWebapp", Context.MODE_PRIVATE); |
webAppPrefs.edit().putLong(WebappDataStorage.KEY_LAST_USED, Long.MIN_VALUE).apply(); |
- // Force a re-read of the preferences from disk. |
- WebappRegistry.refreshSharedPrefsForTesting(); |
- WebappRegistry.getInstance().unregisterOldWebapps(currentTime); |
- |
- Set<String> actual = getRegisteredWebapps(); |
+ WebappRegistry.unregisterOldWebapps(currentTime); |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
+ |
+ Set<String> actual = mSharedPreferences.getStringSet( |
+ WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); |
assertEquals(new HashSet<>(Arrays.asList("oldWebapp")), actual); |
long actualLastUsed = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USED, |
@@ -266,15 +371,14 @@ |
long lastUsed = currentTime - WebappRegistry.WEBAPP_UNOPENED_CLEANUP_DURATION + 1; |
webAppPrefs.edit().putLong(WebappDataStorage.KEY_LAST_USED, lastUsed).apply(); |
- // Force a re-read of the preferences from disk. |
- WebappRegistry.refreshSharedPrefsForTesting(); |
- |
// Because the time is just inside the window, there should be a cleanup but the web app |
// should not be deleted as it was used recently. The last cleanup time should also be |
// set to the current time. |
- WebappRegistry.getInstance().unregisterOldWebapps(currentTime); |
- |
- Set<String> actual = getRegisteredWebapps(); |
+ WebappRegistry.unregisterOldWebapps(currentTime); |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
+ |
+ Set<String> actual = mSharedPreferences.getStringSet( |
+ WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); |
assertEquals(new HashSet<>(Arrays.asList("recentWebapp")), actual); |
long actualLastUsed = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USED, |
@@ -298,14 +402,13 @@ |
long lastUsed = currentTime - WebappRegistry.WEBAPP_UNOPENED_CLEANUP_DURATION; |
webAppPrefs.edit().putLong(WebappDataStorage.KEY_LAST_USED, lastUsed).apply(); |
- // Force a re-read of the preferences from disk. |
- WebappRegistry.refreshSharedPrefsForTesting(); |
- |
// Because the time is just inside the window, there should be a cleanup of old web apps and |
// the last cleaned up time should be set to the current time. |
- WebappRegistry.getInstance().unregisterOldWebapps(currentTime); |
- |
- Set<String> actual = getRegisteredWebapps(); |
+ WebappRegistry.unregisterOldWebapps(currentTime); |
+ 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, |
@@ -326,16 +429,21 @@ |
FetchStorageCallback storageCallback1 = new FetchStorageCallback( |
createWebApkIntent(webappId1, webApkPackage1)); |
- registerWebapp(webappId1, storageCallback1); |
+ WebappRegistry.registerWebapp(webappId1, storageCallback1); |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
+ ShadowLooper.runUiThreadTasks(); |
assertTrue(storageCallback1.getCallbackCalled()); |
FetchStorageCallback storageCallback2 = new FetchStorageCallback( |
createWebApkIntent(webappId1, webApkPackage2)); |
- registerWebapp(webappId2, storageCallback2); |
+ WebappRegistry.registerWebapp(webappId2, storageCallback2); |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
+ ShadowLooper.runUiThreadTasks(); |
assertTrue(storageCallback2.getCallbackCalled()); |
// Verify that both WebAPKs are registered. |
- Set<String> actual = getRegisteredWebapps(); |
+ Set<String> actual = mSharedPreferences.getStringSet( |
+ WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); |
assertEquals(2, actual.size()); |
assertTrue(actual.contains(webappId1)); |
assertTrue(actual.contains(webappId2)); |
@@ -345,9 +453,13 @@ |
// Because the time is just inside the window, there should be a cleanup of |
// uninstalled WebAPKs and the last cleaned up time should be set to the |
// current time. |
- WebappRegistry.getInstance().unregisterOldWebapps(currentTime); |
- |
- assertTrue(getRegisteredWebapps().isEmpty()); |
+ WebappRegistry.unregisterOldWebapps(currentTime); |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
+ ShadowLooper.runUiThreadTasks(); |
+ |
+ actual = mSharedPreferences.getStringSet( |
+ WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); |
+ assertTrue(actual.isEmpty()); |
long lastCleanup = mSharedPreferences.getLong( |
WebappRegistry.KEY_LAST_CLEANUP, -1); |
@@ -364,16 +476,21 @@ |
FetchStorageCallback storageCallback = new FetchStorageCallback( |
createWebApkIntent(webappId, webApkPackage)); |
- registerWebapp(webappId, storageCallback); |
+ WebappRegistry.registerWebapp(webappId, storageCallback); |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
+ ShadowLooper.runUiThreadTasks(); |
assertTrue(storageCallback.getCallbackCalled()); |
FetchStorageCallback storageCallback2 = new FetchStorageCallback( |
createWebApkIntent(uninstalledWebappId, uninstalledWebApkPackage)); |
- registerWebapp(uninstalledWebappId, storageCallback2); |
+ WebappRegistry.registerWebapp(uninstalledWebappId, storageCallback2); |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
+ ShadowLooper.runUiThreadTasks(); |
assertTrue(storageCallback2.getCallbackCalled()); |
// Verify that both WebAPKs are registered. |
- Set<String> actual = getRegisteredWebapps(); |
+ Set<String> actual = mSharedPreferences.getStringSet( |
+ WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); |
assertEquals(2, actual.size()); |
assertTrue(actual.contains(webappId)); |
assertTrue(actual.contains(uninstalledWebappId)); |
@@ -385,15 +502,29 @@ |
// Because the time is just inside the window, there should be a cleanup of |
// uninstalled WebAPKs and the last cleaned up time should be set to the |
// current time. |
- WebappRegistry.getInstance().unregisterOldWebapps(currentTime); |
- |
- actual = getRegisteredWebapps(); |
+ WebappRegistry.unregisterOldWebapps(currentTime); |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
+ ShadowLooper.runUiThreadTasks(); |
+ |
+ actual = mSharedPreferences.getStringSet( |
+ WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); |
assertEquals(1, actual.size()); |
assertTrue(actual.contains(webappId)); |
long lastCleanup = mSharedPreferences.getLong( |
WebappRegistry.KEY_LAST_CLEANUP, -1); |
assertEquals(currentTime, lastCleanup); |
+ } |
+ |
+ @Test |
+ @Feature({"Webapp"}) |
+ public void testClearWebappHistoryRunsCallback() throws Exception { |
+ CallbackRunner callback = new CallbackRunner(); |
+ WebappRegistry.clearWebappHistoryForUrls(new UrlFilters.AllUrls(), callback); |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
+ ShadowLooper.runUiThreadTasks(); |
+ |
+ assertTrue(callback.getCallbackCalled()); |
} |
@Test |
@@ -405,11 +536,15 @@ |
Intent shortcutIntent2 = createShortcutIntent(webapp2Url); |
FetchStorageCallback storageCallback1 = new FetchStorageCallback(shortcutIntent1); |
- registerWebapp("webapp1", storageCallback1); |
+ WebappRegistry.registerWebapp("webapp1", storageCallback1); |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
+ ShadowLooper.runUiThreadTasks(); |
assertTrue(storageCallback1.getCallbackCalled()); |
FetchStorageCallback storageCallback2 = new FetchStorageCallback(shortcutIntent2); |
- registerWebapp("webapp2", storageCallback2); |
+ WebappRegistry.registerWebapp("webapp2", storageCallback2); |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
+ ShadowLooper.runUiThreadTasks(); |
assertTrue(storageCallback2.getCallbackCalled()); |
SharedPreferences webapp1Prefs = ContextUtils.getApplicationContext().getSharedPreferences( |
@@ -425,10 +560,14 @@ |
assertTrue(webapp2OriginalLastUsed != WebappDataStorage.LAST_USED_UNSET); |
// Clear data for |webapp1Url|. |
- WebappRegistry.getInstance().clearWebappHistoryForUrlsImpl( |
- new UrlFilters.OneUrl(webapp1Url)); |
- |
- Set<String> actual = getRegisteredWebapps(); |
+ CallbackRunner callback = new CallbackRunner(); |
+ WebappRegistry.clearWebappHistoryForUrls(new UrlFilters.OneUrl(webapp1Url), callback); |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
+ ShadowLooper.runUiThreadTasks(); |
+ assertTrue(callback.getCallbackCalled()); |
+ |
+ Set<String> actual = mSharedPreferences.getStringSet( |
+ WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); |
assertEquals(2, actual.size()); |
assertTrue(actual.contains("webapp1")); |
assertTrue(actual.contains("webapp2")); |
@@ -458,7 +597,11 @@ |
assertEquals(webapp2Url, actualUrl); |
// Clear data for all urls. |
- WebappRegistry.getInstance().clearWebappHistoryForUrlsImpl(new UrlFilters.AllUrls()); |
+ callback = new CallbackRunner(); |
+ WebappRegistry.clearWebappHistoryForUrls(new UrlFilters.AllUrls(), callback); |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
+ ShadowLooper.runUiThreadTasks(); |
+ assertTrue(callback.getCallbackCalled()); |
// Verify that the last used time for both web apps is WebappDataStorage.LAST_USED_UNSET. |
actualLastUsed = webapp1Prefs.getLong( |
@@ -486,14 +629,23 @@ |
@Test |
@Feature({"Webapp"}) |
public void testGetAfterClearWebappHistory() throws Exception { |
- registerWebapp("webapp", null); |
+ WebappRegistry.registerWebapp("webapp", null); |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
SharedPreferences webappPrefs = ContextUtils.getApplicationContext().getSharedPreferences( |
WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "webapp", Context.MODE_PRIVATE); |
- WebappRegistry.getInstance().clearWebappHistoryForUrlsImpl(new UrlFilters.AllUrls()); |
- |
- // Open the webapp up and set the last used time. |
- WebappRegistry.getInstance().getWebappDataStorage("webapp").updateLastUsedTime(); |
+ CallbackRunner callback = new CallbackRunner(); |
+ WebappRegistry.clearWebappHistoryForUrls(new UrlFilters.AllUrls(), callback); |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
+ ShadowLooper.runUiThreadTasks(); |
+ assertTrue(callback.getCallbackCalled()); |
+ |
+ // Open the webapp up to set the last used time. |
+ FetchStorageCallback storageCallback = new FetchStorageCallback(null); |
+ WebappRegistry.getWebappDataStorage("webapp", storageCallback); |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
+ ShadowLooper.runUiThreadTasks(); |
+ assertTrue(storageCallback.getCallbackCalled()); |
// Verify that the last used time is valid. |
long actualLastUsed = webappPrefs.getLong( |
@@ -508,7 +660,9 @@ |
final String webappUrl = "http://www.google.com"; |
final String webappScope = "http://www.google.com/"; |
final Intent shortcutIntent = createShortcutIntent(webappUrl); |
- registerWebapp("webapp", new FetchStorageCallback(shortcutIntent)); |
+ WebappRegistry.registerWebapp("webapp", new FetchStorageCallback(shortcutIntent)); |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
+ ShadowLooper.runUiThreadTasks(); |
SharedPreferences webappPrefs = ContextUtils.getApplicationContext().getSharedPreferences( |
WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "webapp", Context.MODE_PRIVATE); |
@@ -521,11 +675,14 @@ |
WebappDataStorage.KEY_SCOPE, WebappDataStorage.URL_INVALID); |
assertEquals(webappScope, actualScope); |
- WebappRegistry.getInstance().clearWebappHistoryForUrlsImpl(new UrlFilters.AllUrls()); |
+ WebappRegistry.clearWebappHistoryForUrls(new UrlFilters.AllUrls(), new CallbackRunner()); |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
+ ShadowLooper.runUiThreadTasks(); |
// Update the webapp from the intent again. |
- WebappDataStorage storage = WebappRegistry.getInstance().getWebappDataStorage("webapp"); |
- storage.updateFromShortcutIntent(shortcutIntent); |
+ WebappRegistry.getWebappDataStorage("webapp", new FetchStorageCallback(shortcutIntent)); |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
+ ShadowLooper.runUiThreadTasks(); |
// Verify that the URL and scope match the original in the intent. |
actualUrl = webappPrefs.getString(WebappDataStorage.KEY_URL, WebappDataStorage.URL_INVALID); |
@@ -561,45 +718,72 @@ |
Intent shortcutIntent4 = createShortcutIntent(webapp4Url); |
// Register the four web apps. |
- registerWebapp("webapp1", new FetchStorageCallback(shortcutIntent1)); |
- registerWebapp("webapp2", new FetchStorageCallback(shortcutIntent2)); |
- registerWebapp("webapp3", new FetchStorageCallback(shortcutIntent3)); |
- registerWebapp("webapp4", new FetchStorageCallback(shortcutIntent4)); |
+ WebappRegistry.registerWebapp("webapp1", new FetchStorageCallback(shortcutIntent1)); |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
+ ShadowLooper.runUiThreadTasks(); |
+ |
+ WebappRegistry.registerWebapp("webapp2", new FetchStorageCallback(shortcutIntent2)); |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
+ ShadowLooper.runUiThreadTasks(); |
+ |
+ WebappRegistry.registerWebapp("webapp3", new FetchStorageCallback(shortcutIntent3)); |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
+ ShadowLooper.runUiThreadTasks(); |
+ |
+ WebappRegistry.registerWebapp("webapp4", new FetchStorageCallback(shortcutIntent4)); |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
+ ShadowLooper.runUiThreadTasks(); |
// test1Url should return webapp1. |
- WebappDataStorage storage1 = |
- WebappRegistry.getInstance().getWebappDataStorageForUrl(test1Url); |
- assertEquals(webapp1Url, storage1.getUrl()); |
- assertEquals(webapp1Url, storage1.getScope()); |
+ FetchStorageByUrlCallback callback = new FetchStorageByUrlCallback(webapp1Url, webapp1Url); |
+ WebappRegistry.getWebappDataStorageForUrl(test1Url, callback); |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
+ ShadowLooper.runUiThreadTasks(); |
+ assertTrue(callback.getCallbackCalled()); |
// test2Url should return webapp3. |
- WebappDataStorage storage2 = |
- WebappRegistry.getInstance().getWebappDataStorageForUrl(test2Url); |
- assertEquals(webapp3Url, storage2.getUrl()); |
- assertEquals(webapp3Scope, storage2.getScope()); |
+ callback = new FetchStorageByUrlCallback(webapp3Url, webapp3Scope); |
+ WebappRegistry.getWebappDataStorageForUrl(test2Url, callback); |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
+ ShadowLooper.runUiThreadTasks(); |
+ assertTrue(callback.getCallbackCalled()); |
// test3Url should return webapp4. |
- WebappDataStorage storage3 = |
- WebappRegistry.getInstance().getWebappDataStorageForUrl(test3Url); |
- assertEquals(webapp4Url, storage3.getUrl()); |
- assertEquals(webapp4Scope, storage3.getScope()); |
+ callback = new FetchStorageByUrlCallback(webapp4Url, webapp4Scope); |
+ WebappRegistry.getWebappDataStorageForUrl(test3Url, callback); |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
+ ShadowLooper.runUiThreadTasks(); |
+ assertTrue(callback.getCallbackCalled()); |
// test4Url should return webapp4. |
- WebappDataStorage storage4 = |
- WebappRegistry.getInstance().getWebappDataStorageForUrl(test4Url); |
- assertEquals(webapp4Url, storage4.getUrl()); |
- assertEquals(webapp4Scope, storage4.getScope()); |
+ callback = new FetchStorageByUrlCallback(webapp4Url, webapp4Scope); |
+ WebappRegistry.getWebappDataStorageForUrl(test4Url, callback); |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
+ ShadowLooper.runUiThreadTasks(); |
+ assertTrue(callback.getCallbackCalled()); |
// test5Url should return webapp2. |
- WebappDataStorage storage5 = |
- WebappRegistry.getInstance().getWebappDataStorageForUrl(test5Url); |
- assertEquals(webapp2Url, storage5.getUrl()); |
- assertEquals(webapp2Url, storage5.getScope()); |
+ callback = new FetchStorageByUrlCallback(webapp2Url, webapp2Url); |
+ WebappRegistry.getWebappDataStorageForUrl(test5Url, callback); |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
+ ShadowLooper.runUiThreadTasks(); |
+ assertTrue(callback.getCallbackCalled()); |
// test6Url doesn't correspond to a web app, so the storage returned is null. |
- WebappDataStorage storage6 = |
- WebappRegistry.getInstance().getWebappDataStorageForUrl(test6Url); |
- assertEquals(null, storage6); |
+ // This must use a member variable; local variables must be final or effectively final to be |
+ // accessible inside an inner class. |
+ mCallbackCalled = false; |
+ WebappRegistry.getWebappDataStorageForUrl( |
+ test6Url, new WebappRegistry.FetchWebappDataStorageCallback() { |
+ @Override |
+ public void onWebappDataStorageRetrieved(WebappDataStorage storage) { |
+ assertEquals(null, storage); |
+ mCallbackCalled = true; |
+ } |
+ }); |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
+ ShadowLooper.runUiThreadTasks(); |
+ assertTrue(mCallbackCalled); |
} |
private Set<String> addWebappsToRegistry(String... webapps) { |