Chromium Code Reviews| 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 2d07c7afc6018b61bf11668a17315ad1f00fa8a9..33ffefec122b04292da1e7f84294e2254fa10df6 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 |
| @@ -43,6 +43,20 @@ public class WebappRegistryTest { |
| private SharedPreferences mSharedPreferences; |
| private boolean mCallbackCalled; |
| + private class FetchCallback implements WebappRegistry.FetchCallback { |
| + FetchCallback(Set<String> expected) { |
| + mExpected = expected; |
| + } |
| + |
| + @Override |
| + public void onWebappIdsRetrieved(Set<String> actual) { |
| + mCallbackCalled = true; |
| + assertEquals(mExpected, actual); |
| + } |
| + |
| + Set<String> mExpected; |
|
gone
2016/03/10 23:27:22
members go at the top
dominickn
2016/03/11 05:14:43
Done.
|
| + } |
| + |
| @Before |
| public void setUp() throws Exception { |
| mSharedPreferences = Robolectric.application |
| @@ -63,7 +77,7 @@ public class WebappRegistryTest { |
| @Test |
| @Feature({"Webapp"}) |
| public void testWebappRegistrationAddsToSharedPrefs() throws Exception { |
| - WebappRegistry.registerWebapp(Robolectric.application, "test"); |
| + WebappRegistry.registerWebapp(Robolectric.application, "test", "https://www.google.com"); |
| BackgroundShadowAsyncTask.runBackgroundTasks(); |
| Set<String> actual = mSharedPreferences.getStringSet( |
| @@ -74,17 +88,22 @@ public class WebappRegistryTest { |
| @Test |
| @Feature({"Webapp"}) |
| - public void testWebappRegistrationUpdatesLastUsed() throws Exception { |
| + public void testWebappRegistrationUpdatesLastUsedAndOrigin() throws Exception { |
| long before = System.currentTimeMillis(); |
| - WebappRegistry.registerWebapp(Robolectric.application, "test"); |
| + final String origin = "http://drive.google.com"; |
| + |
| + WebappRegistry.registerWebapp(Robolectric.application, "test", origin); |
| BackgroundShadowAsyncTask.runBackgroundTasks(); |
| long after = System.currentTimeMillis(); |
| SharedPreferences webAppPrefs = Robolectric.application.getSharedPreferences( |
| WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "test", Context.MODE_PRIVATE); |
| long actual = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USED, |
| - WebappDataStorage.INVALID_LAST_USED); |
| + WebappDataStorage.LAST_USED_INVALID); |
| + String webAppOrigin = webAppPrefs.getString(WebappDataStorage.KEY_ORIGIN_URL, |
| + WebappDataStorage.ORIGIN_URL_INVALID); |
| assertTrue("Timestamp is out of range", before <= actual && actual <= after); |
| + assertEquals(origin, webAppOrigin); |
| } |
| @Test |
| @@ -92,14 +111,7 @@ public class WebappRegistryTest { |
| public void testWebappIdsRetrieval() throws Exception { |
| final Set<String> expected = addWebappsToRegistry("first", "second"); |
| - WebappRegistry.getRegisteredWebappIds(Robolectric.application, |
| - new WebappRegistry.FetchCallback() { |
| - @Override |
| - public void onWebappIdsRetrieved(Set<String> actual) { |
| - mCallbackCalled = true; |
| - assertEquals(expected, actual); |
| - } |
| - }); |
| + WebappRegistry.getRegisteredWebappIds(Robolectric.application, new FetchCallback(expected)); |
| BackgroundShadowAsyncTask.runBackgroundTasks(); |
| Robolectric.runUiThreadTasks(); |
| @@ -111,21 +123,14 @@ public class WebappRegistryTest { |
| public void testWebappIdsRetrievalRegisterRetrival() throws Exception { |
| final Set<String> expected = addWebappsToRegistry("first"); |
| - WebappRegistry.getRegisteredWebappIds(Robolectric.application, |
| - new WebappRegistry.FetchCallback() { |
| - @Override |
| - public void onWebappIdsRetrieved(Set<String> actual) { |
| - mCallbackCalled = true; |
| - assertEquals(expected, actual); |
| - } |
| - }); |
| + WebappRegistry.getRegisteredWebappIds(Robolectric.application, new FetchCallback(expected)); |
| BackgroundShadowAsyncTask.runBackgroundTasks(); |
| Robolectric.runUiThreadTasks(); |
| assertTrue(mCallbackCalled); |
| mCallbackCalled = false; |
| - WebappRegistry.registerWebapp(Robolectric.application, "second"); |
| + WebappRegistry.registerWebapp(Robolectric.application, "second", "https://www.google.com"); |
| BackgroundShadowAsyncTask.runBackgroundTasks(); |
| // A copy of the expected set needs to be made as the SharedPreferences is using the copy |
| @@ -134,13 +139,7 @@ public class WebappRegistryTest { |
| secondExpected.add("second"); |
| WebappRegistry.getRegisteredWebappIds(Robolectric.application, |
| - new WebappRegistry.FetchCallback() { |
| - @Override |
| - public void onWebappIdsRetrieved(Set<String> actual) { |
| - mCallbackCalled = true; |
| - assertEquals(secondExpected, actual); |
| - } |
| - }); |
| + new FetchCallback(secondExpected)); |
| BackgroundShadowAsyncTask.runBackgroundTasks(); |
| Robolectric.runUiThreadTasks(); |
| @@ -167,9 +166,16 @@ public class WebappRegistryTest { |
| public void testUnregisterClearsRegistry() throws Exception { |
| addWebappsToRegistry("test"); |
| - WebappRegistry.unregisterAllWebapps(Robolectric.application, null); |
| + WebappRegistry.unregisterAllWebapps(Robolectric.application, new Runnable() { |
| + @Override |
| + public void run() { |
| + mCallbackCalled = true; |
| + } |
| + }); |
| BackgroundShadowAsyncTask.runBackgroundTasks(); |
| + Robolectric.runUiThreadTasks(); |
| + assertTrue(mCallbackCalled); |
| assertTrue(getRegisteredWebapps().isEmpty()); |
| } |
| @@ -211,7 +217,7 @@ public class WebappRegistryTest { |
| assertEquals(new HashSet<String>(Arrays.asList("oldWebapp")), actual); |
| long actualLastUsed = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USED, |
| - WebappDataStorage.INVALID_LAST_USED); |
| + WebappDataStorage.LAST_USED_INVALID); |
| assertEquals(Long.MIN_VALUE, actualLastUsed); |
| // The last cleanup time was set to 0 in setUp() so check that this hasn't changed. |
| @@ -245,7 +251,7 @@ public class WebappRegistryTest { |
| assertEquals(new HashSet<String>(Arrays.asList("recentWebapp")), actual); |
| long actualLastUsed = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USED, |
| - WebappDataStorage.INVALID_LAST_USED); |
| + WebappDataStorage.LAST_USED_INVALID); |
| assertEquals(lastUsed, actualLastUsed); |
| long lastCleanup = mSharedPreferences.getLong(WebappRegistry.KEY_LAST_CLEANUP, -1); |
| @@ -277,13 +283,112 @@ public class WebappRegistryTest { |
| assertTrue(actual.isEmpty()); |
| long actualLastUsed = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USED, |
| - WebappDataStorage.INVALID_LAST_USED); |
| - assertEquals(WebappDataStorage.INVALID_LAST_USED, actualLastUsed); |
| + WebappDataStorage.LAST_USED_INVALID); |
| + assertEquals(WebappDataStorage.LAST_USED_INVALID, actualLastUsed); |
| long lastCleanup = mSharedPreferences.getLong(WebappRegistry.KEY_LAST_CLEANUP, -1); |
| assertEquals(currentTime, lastCleanup); |
| } |
| + @Test |
| + @Feature({"Webapp"}) |
| + public void testClearWebappHistoryRunsCallback() throws Exception { |
| + WebappRegistry.clearWebappHistory(Robolectric.application, new Runnable() { |
| + @Override |
| + public void run() { |
| + mCallbackCalled = true; |
| + } |
| + }); |
| + BackgroundShadowAsyncTask.runBackgroundTasks(); |
| + Robolectric.runUiThreadTasks(); |
| + |
| + assertTrue(mCallbackCalled); |
| + } |
| + |
| + @Test |
| + @Feature({"Webapp"}) |
| + public void testClearWebappHistory() throws Exception { |
| + final String webappOrigin1 = "https://www.google.com"; |
| + final String webappOrigin2 = "https://drive.google.com"; |
| + WebappRegistry.registerWebapp(Robolectric.application, "webapp1", webappOrigin1); |
| + WebappRegistry.registerWebapp(Robolectric.application, "webapp2", webappOrigin2); |
| + BackgroundShadowAsyncTask.runBackgroundTasks(); |
| + |
| + SharedPreferences webapp1Prefs = Robolectric.application.getSharedPreferences( |
| + WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "webapp1", Context.MODE_PRIVATE); |
| + SharedPreferences webapp2Prefs = Robolectric.application.getSharedPreferences( |
| + WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "webapp2", Context.MODE_PRIVATE); |
| + |
| + WebappRegistry.clearWebappHistory(Robolectric.application, new Runnable() { |
| + @Override |
| + public void run() { |
| + mCallbackCalled = true; |
| + } |
| + }); |
| + BackgroundShadowAsyncTask.runBackgroundTasks(); |
| + Robolectric.runUiThreadTasks(); |
| + |
| + assertTrue(mCallbackCalled); |
| + |
| + Set<String> actual = mSharedPreferences.getStringSet( |
| + WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); |
| + assertEquals(2, actual.size()); |
| + assertTrue(actual.contains("webapp1")); |
| + assertTrue(actual.contains("webapp2")); |
| + |
| + // Verify that the last used time for both web apps is WebappDataStorage.LAST_USED_UNSET. |
| + long actualLastUsed = webapp1Prefs.getLong( |
| + WebappDataStorage.KEY_LAST_USED, WebappDataStorage.LAST_USED_UNSET); |
| + assertEquals(WebappDataStorage.LAST_USED_UNSET, actualLastUsed); |
| + actualLastUsed = webapp2Prefs.getLong( |
| + WebappDataStorage.KEY_LAST_USED, WebappDataStorage.LAST_USED_UNSET); |
| + assertEquals(WebappDataStorage.LAST_USED_UNSET, actualLastUsed); |
| + |
| + // Verify that the origin URL for both web apps is WebappDataStorage.ORIGIN_URL_INVALID. |
| + String actualOriginUrl = webapp1Prefs.getString( |
| + WebappDataStorage.KEY_ORIGIN_URL, WebappDataStorage.ORIGIN_URL_INVALID); |
| + assertEquals(WebappDataStorage.ORIGIN_URL_INVALID, actualOriginUrl); |
| + actualOriginUrl = webapp2Prefs.getString( |
| + WebappDataStorage.KEY_ORIGIN_URL, WebappDataStorage.ORIGIN_URL_INVALID); |
| + assertEquals(WebappDataStorage.ORIGIN_URL_INVALID, actualOriginUrl); |
| + } |
| + |
| + @Test |
| + @Feature({"Webapp"}) |
| + public void testOpenAfterClearWebappHistory() throws Exception { |
| + final String webappOrigin = "https://www.google.com"; |
| + WebappRegistry.registerWebapp(Robolectric.application, "webapp", webappOrigin); |
| + BackgroundShadowAsyncTask.runBackgroundTasks(); |
| + |
| + SharedPreferences webappPrefs = Robolectric.application.getSharedPreferences( |
| + WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "webapp", Context.MODE_PRIVATE); |
| + |
| + WebappRegistry.clearWebappHistory(Robolectric.application, new Runnable() { |
| + @Override |
| + public void run() { |
| + mCallbackCalled = true; |
| + } |
| + }); |
| + BackgroundShadowAsyncTask.runBackgroundTasks(); |
| + |
| + // Open the webapp up and set the origin. |
| + WebappDataStorage.open(Robolectric.application, "webapp"); |
| + WebappDataStorage.updateOriginUrl(Robolectric.application, "webapp", webappOrigin); |
| + BackgroundShadowAsyncTask.runBackgroundTasks(); |
| + Robolectric.runUiThreadTasks(); |
| + assertTrue(mCallbackCalled); |
| + |
| + // Verify that the last used time is valid and the origin URL is updated. |
| + long actualLastUsed = webappPrefs.getLong( |
| + WebappDataStorage.KEY_LAST_USED, WebappDataStorage.LAST_USED_INVALID); |
| + assertTrue(WebappDataStorage.LAST_USED_INVALID != actualLastUsed); |
| + assertTrue(WebappDataStorage.LAST_USED_UNSET != actualLastUsed); |
| + String actualOriginUrl = webappPrefs.getString( |
| + WebappDataStorage.KEY_ORIGIN_URL, WebappDataStorage.ORIGIN_URL_INVALID); |
| + assertEquals(webappOrigin, actualOriginUrl); |
| + } |
| + |
| + |
| private Set<String> addWebappsToRegistry(String... webapps) { |
| final Set<String> expected = new HashSet<String>(Arrays.asList(webapps)); |
| mSharedPreferences.edit() |
| @@ -296,4 +401,4 @@ public class WebappRegistryTest { |
| return mSharedPreferences.getStringSet( |
| WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet()); |
| } |
| -} |
| +} |