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 d57dbae637fbe4e18cbc2222ad1b76d54fe6cbe1..0148c8b8b5b17ab42c302c05b9e541360fe9d808 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 |
@@ -49,6 +49,7 @@ public class WebappRegistryTest { |
Set<String> mExpected; |
FetchCallback(Set<String> expected) { |
+ mCallbackCalled = false; |
mExpected = expected; |
} |
@@ -61,16 +62,39 @@ public class WebappRegistryTest { |
private class FetchStorageCallback implements WebappRegistry.FetchWebappDataStorageCallback { |
Intent mShortcutIntent; |
+ boolean mMarkLaunched; |
- FetchStorageCallback(Intent shortcutIntent) { |
+ FetchStorageCallback(Intent shortcutIntent, boolean markLaunched) { |
mCallbackCalled = false; |
mShortcutIntent = shortcutIntent; |
+ mMarkLaunched = markLaunched; |
} |
@Override |
public void onWebappDataStorageRetrieved(WebappDataStorage storage) { |
mCallbackCalled = true; |
storage.updateFromShortcutIntent(mShortcutIntent); |
+ storage.updateLastUsedTime(); |
+ if (mMarkLaunched) storage.setLaunched(); |
+ } |
+ } |
+ |
+ private class FetchStorageByUrlCallback |
+ implements WebappRegistry.FetchWebappDataStorageCallback { |
+ String mUrl; |
+ String mScope; |
+ |
+ FetchStorageByUrlCallback(String url, String scope) { |
+ mCallbackCalled = false; |
+ mUrl = url; |
+ mScope = scope; |
+ } |
+ |
+ @Override |
+ public void onWebappDataStorageRetrieved(WebappDataStorage storage) { |
+ mCallbackCalled = true; |
gone
2016/04/07 19:10:44
This really should be a member variable of this ca
dominickn
2016/04/08 01:10:46
Done.
|
+ assertEquals(mUrl, storage.getUrl()); |
+ assertEquals(mScope, storage.getScope()); |
} |
} |
@@ -116,9 +140,7 @@ public class WebappRegistryTest { |
@Test |
@Feature({"Webapp"}) |
- public void testWebappRegistrationUpdatesLastUsed() throws Exception { |
- long before = System.currentTimeMillis(); |
dominickn
2016/04/06 13:07:01
The before check started being flaky for me, so I
|
- |
+ public void testWebappRegistrationUpdatesLastUsedAndDoesNotMarkLaunched() throws Exception { |
WebappRegistry.registerWebapp(Robolectric.application, "test", null); |
BackgroundShadowAsyncTask.runBackgroundTasks(); |
long after = System.currentTimeMillis(); |
@@ -127,7 +149,10 @@ public class WebappRegistryTest { |
WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "test", Context.MODE_PRIVATE); |
long actual = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USED, |
WebappDataStorage.LAST_USED_INVALID); |
- assertTrue("Timestamp is out of range", before <= actual && actual <= after); |
+ assertTrue("Timestamp is out of range", actual <= after); |
+ |
+ boolean launched = webAppPrefs.getBoolean(WebappDataStorage.KEY_LAUNCHED, false); |
+ assertTrue(!launched); |
gone
2016/04/07 19:10:45
can't just assertTrue(!webAppPref....) here?
dominickn
2016/04/08 01:10:46
Done.
|
} |
@Test |
@@ -323,13 +348,13 @@ public class WebappRegistryTest { |
Intent shortcutIntent2 = createShortcutIntent(webapp2Url); |
WebappRegistry.registerWebapp(Robolectric.application, "webapp1", |
- new FetchStorageCallback(shortcutIntent1)); |
+ new FetchStorageCallback(shortcutIntent1, true)); |
BackgroundShadowAsyncTask.runBackgroundTasks(); |
Robolectric.runUiThreadTasks(); |
assertTrue(mCallbackCalled); |
WebappRegistry.registerWebapp(Robolectric.application, "webapp2", |
- new FetchStorageCallback(shortcutIntent2)); |
+ new FetchStorageCallback(shortcutIntent2, false)); |
BackgroundShadowAsyncTask.runBackgroundTasks(); |
Robolectric.runUiThreadTasks(); |
assertTrue(mCallbackCalled); |
@@ -358,6 +383,12 @@ public class WebappRegistryTest { |
WebappDataStorage.KEY_LAST_USED, WebappDataStorage.LAST_USED_UNSET); |
assertEquals(WebappDataStorage.LAST_USED_UNSET, actualLastUsed); |
+ // Verify that neither web app is marked as launched. |
+ boolean actualLaunched = webapp1Prefs.getBoolean(WebappDataStorage.KEY_LAUNCHED, false); |
+ assertTrue(!actualLaunched); |
+ actualLaunched = webapp2Prefs.getBoolean(WebappDataStorage.KEY_LAUNCHED, false); |
+ assertTrue(!actualLaunched); |
+ |
// Verify that the URL and scope for both web apps is WebappDataStorage.URL_INVALID. |
String actualScope = webapp1Prefs.getString( |
WebappDataStorage.KEY_SCOPE, WebappDataStorage.URL_INVALID); |
@@ -386,9 +417,9 @@ public class WebappRegistryTest { |
Robolectric.runUiThreadTasks(); |
assertTrue(mCallbackCalled); |
- // Open the webapp up to set the last used time. |
+ // Open the webapp up to set the last used time and launched. |
WebappRegistry.getWebappDataStorage(Robolectric.application, "webapp", |
- new FetchStorageCallback(null)); |
+ new FetchStorageCallback(null, true)); |
BackgroundShadowAsyncTask.runBackgroundTasks(); |
Robolectric.runUiThreadTasks(); |
assertTrue(mCallbackCalled); |
@@ -398,15 +429,20 @@ public class WebappRegistryTest { |
WebappDataStorage.KEY_LAST_USED, WebappDataStorage.LAST_USED_INVALID); |
assertTrue(WebappDataStorage.LAST_USED_INVALID != actualLastUsed); |
assertTrue(WebappDataStorage.LAST_USED_UNSET != actualLastUsed); |
+ |
+ // Verify that the app is marked as launched. |
+ boolean actualLaunched = webappPrefs.getBoolean(WebappDataStorage.KEY_LAUNCHED, false); |
+ assertTrue(actualLaunched); |
} |
@Test |
@Feature({"Webapp"}) |
public void testUpdateAfterClearWebappHistory() throws Exception { |
- final String webappUrl = "http://www.google.com"; |
+ final String webappUrl = "http://www.google.com"; |
+ final String webappScope = "http://www.google.com/"; |
final Intent shortcutIntent = createShortcutIntent(webappUrl); |
WebappRegistry.registerWebapp(Robolectric.application, "webapp", |
- new FetchStorageCallback(shortcutIntent)); |
+ new FetchStorageCallback(shortcutIntent, false)); |
BackgroundShadowAsyncTask.runBackgroundTasks(); |
Robolectric.runUiThreadTasks(); |
assertTrue(mCallbackCalled); |
@@ -414,13 +450,17 @@ public class WebappRegistryTest { |
SharedPreferences webappPrefs = Robolectric.application.getSharedPreferences( |
WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "webapp", Context.MODE_PRIVATE); |
+ // Verify that the app is not marked as launched. |
+ boolean actualLaunched = webappPrefs.getBoolean(WebappDataStorage.KEY_LAUNCHED, true); |
+ assertTrue(actualLaunched); |
+ |
// Verify that the URL and scope match the original in the intent. |
- String actualURL = webappPrefs.getString( |
+ String actualUrl = webappPrefs.getString( |
WebappDataStorage.KEY_URL, WebappDataStorage.URL_INVALID); |
- assertEquals(webappUrl, actualURL); |
+ assertEquals(webappUrl, actualUrl); |
String actualScope = webappPrefs.getString( |
WebappDataStorage.KEY_SCOPE, WebappDataStorage.URL_INVALID); |
- assertEquals(webappUrl, actualScope); |
+ assertEquals(webappScope, actualScope); |
WebappRegistry.clearWebappHistory(Robolectric.application, new CallbackRunner()); |
BackgroundShadowAsyncTask.runBackgroundTasks(); |
@@ -429,18 +469,107 @@ public class WebappRegistryTest { |
// Update the webapp from the intent again. |
WebappRegistry.getWebappDataStorage(Robolectric.application, "webapp", |
- new FetchStorageCallback(shortcutIntent)); |
+ new FetchStorageCallback(shortcutIntent, true)); |
BackgroundShadowAsyncTask.runBackgroundTasks(); |
Robolectric.runUiThreadTasks(); |
assertTrue(mCallbackCalled); |
+ // Verify that the app is marked as launched. |
+ actualLaunched = webappPrefs.getBoolean(WebappDataStorage.KEY_LAUNCHED, true); |
+ assertTrue(actualLaunched); |
+ |
// Verify that the URL and scope match the original in the intent. |
- actualURL = webappPrefs.getString( |
+ actualUrl = webappPrefs.getString( |
WebappDataStorage.KEY_URL, WebappDataStorage.URL_INVALID); |
- assertEquals(webappUrl, actualURL); |
+ assertEquals(webappUrl, actualUrl); |
actualScope = webappPrefs.getString( |
WebappDataStorage.KEY_SCOPE, WebappDataStorage.URL_INVALID); |
- assertEquals(webappUrl, actualScope); |
+ assertEquals(webappScope, actualScope); |
+ } |
+ |
+ @Test |
+ @Feature({"Webapp"}) |
+ public void testGetWebappDataStorageForUrl() throws Exception { |
gone
2016/04/07 19:10:45
Can you add comments here? I'm not sure what I'm
dominickn
2016/04/08 01:10:46
Done.
|
+ final String webapp1Url = "https://www.google.com/"; |
+ final String webapp2Url = "https://drive.google.com/"; |
+ final String webapp3Url = "https://www.google.com/drive/index.html"; |
+ final String webapp4Url = "https://www.google.com/drive/docs/index.html"; |
+ |
+ final String webapp3Scope = "https://www.google.com/drive/"; |
+ final String webapp4Scope = "https://www.google.com/drive/docs/"; |
+ |
+ final String test1Url = "https://www.google.com/index.html"; |
+ final String test2Url = "https://www.google.com/drive/recent.html"; |
+ final String test3Url = "https://www.google.com/drive/docs/recent.html"; |
+ final String test4Url = "https://www.google.com/drive/docs/recent/index.html"; |
+ final String test5Url = "https://maps.google.com/"; |
+ |
+ Intent shortcutIntent1 = createShortcutIntent(webapp1Url); |
+ Intent shortcutIntent2 = createShortcutIntent(webapp2Url); |
+ Intent shortcutIntent3 = createShortcutIntent(webapp3Url); |
+ Intent shortcutIntent4 = createShortcutIntent(webapp4Url); |
+ |
+ WebappRegistry.registerWebapp(Robolectric.application, "webapp1", |
+ new FetchStorageCallback(shortcutIntent1, true)); |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
+ Robolectric.runUiThreadTasks(); |
+ assertTrue(mCallbackCalled); |
+ |
+ WebappRegistry.registerWebapp(Robolectric.application, "webapp2", |
+ new FetchStorageCallback(shortcutIntent2, false)); |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
+ Robolectric.runUiThreadTasks(); |
+ assertTrue(mCallbackCalled); |
+ |
+ WebappRegistry.registerWebapp(Robolectric.application, "webapp3", |
+ new FetchStorageCallback(shortcutIntent3, true)); |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
+ Robolectric.runUiThreadTasks(); |
+ assertTrue(mCallbackCalled); |
+ |
+ WebappRegistry.registerWebapp(Robolectric.application, "webapp4", |
+ new FetchStorageCallback(shortcutIntent4, false)); |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
+ Robolectric.runUiThreadTasks(); |
+ assertTrue(mCallbackCalled); |
+ |
+ WebappRegistry.getWebappDataStorageForUrl(Robolectric.application, test1Url, |
+ new FetchStorageByUrlCallback(webapp1Url, webapp1Url)); |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
+ Robolectric.runUiThreadTasks(); |
+ assertTrue(mCallbackCalled); |
+ |
+ WebappRegistry.getWebappDataStorageForUrl(Robolectric.application, test2Url, |
+ new FetchStorageByUrlCallback(webapp3Url, webapp3Scope)); |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
+ Robolectric.runUiThreadTasks(); |
+ assertTrue(mCallbackCalled); |
+ |
+ WebappRegistry.getWebappDataStorageForUrl(Robolectric.application, test3Url, |
+ new FetchStorageByUrlCallback(webapp4Url, webapp4Scope)); |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
+ Robolectric.runUiThreadTasks(); |
+ assertTrue(mCallbackCalled); |
+ |
+ WebappRegistry.getWebappDataStorageForUrl(Robolectric.application, test4Url, |
+ new FetchStorageByUrlCallback(webapp4Url, webapp4Scope)); |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
+ Robolectric.runUiThreadTasks(); |
+ assertTrue(mCallbackCalled); |
+ |
+ mCallbackCalled = false; |
+ WebappRegistry.getWebappDataStorageForUrl(Robolectric.application, test5Url, |
+ new WebappRegistry.FetchWebappDataStorageCallback() { |
+ @Override |
+ public void onWebappDataStorageRetrieved(WebappDataStorage storage) { |
+ assertEquals(null, storage); |
+ mCallbackCalled = true; |
+ } |
+ } |
+ ); |
+ BackgroundShadowAsyncTask.runBackgroundTasks(); |
+ Robolectric.runUiThreadTasks(); |
+ assertTrue(mCallbackCalled); |
} |
private Set<String> addWebappsToRegistry(String... webapps) { |
@@ -457,7 +586,8 @@ public class WebappRegistryTest { |
} |
private Intent createShortcutIntent(String url) { |
- return ShortcutHelper.createWebappShortcutIntent("id", "action", url, url, "name", |
- "shortName", null, ShortcutHelper.WEBAPP_SHORTCUT_VERSION, 0, 0, 0, false); |
+ return ShortcutHelper.createWebappShortcutIntent("id", "action", url, |
+ ShortcutHelper.getScopeFromUrl(url), "name", "shortName", null, |
+ ShortcutHelper.WEBAPP_SHORTCUT_VERSION, 0, 0, 0, false); |
} |
} |