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

Unified Diff: chrome/android/junit/src/org/chromium/chrome/browser/webapps/WebappRegistryTest.java

Issue 1845233002: Store standalone web app data in WebappDataStorage as well as the homescreen intent. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@notification-deep-linking
Patch Set: Improve comments Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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 fe5367ae1f78d68fa76a1656c2cc3ed0fbd82d62..2132e3ce82bea728c853388b337f4d71dcd72662 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
@@ -8,9 +8,11 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import android.content.Context;
+import android.content.Intent;
import android.content.SharedPreferences;
import org.chromium.base.test.util.Feature;
+import org.chromium.chrome.browser.ShortcutHelper;
import org.chromium.testing.local.BackgroundShadowAsyncTask;
import org.chromium.testing.local.LocalRobolectricTestRunner;
import org.junit.Before;
@@ -57,6 +59,17 @@ public class WebappRegistryTest {
}
}
+ private class Runner implements Runnable {
+ public Runner() {
+ mCallbackCalled = false;
+ }
+
+ @Override
+ public void run() {
+ mCallbackCalled = true;
+ }
+ }
+
@Before
public void setUp() throws Exception {
mSharedPreferences = Robolectric.application
@@ -77,7 +90,7 @@ public class WebappRegistryTest {
@Test
@Feature({"Webapp"})
public void testWebappRegistrationAddsToSharedPrefs() throws Exception {
- WebappRegistry.registerWebapp(Robolectric.application, "test", "https://www.google.com");
+ WebappRegistry.registerWebapp(Robolectric.application, "test");
BackgroundShadowAsyncTask.runBackgroundTasks();
Set<String> actual = mSharedPreferences.getStringSet(
@@ -88,11 +101,10 @@ public class WebappRegistryTest {
@Test
@Feature({"Webapp"})
- public void testWebappRegistrationUpdatesLastUsedAndSCOPE() throws Exception {
+ public void testWebappRegistrationUpdatesLastUsed() throws Exception {
long before = System.currentTimeMillis();
- final String scope = "http://drive.google.com";
- WebappRegistry.registerWebapp(Robolectric.application, "test", scope);
+ WebappRegistry.registerWebapp(Robolectric.application, "test");
BackgroundShadowAsyncTask.runBackgroundTasks();
long after = System.currentTimeMillis();
@@ -100,10 +112,7 @@ public class WebappRegistryTest {
WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "test", Context.MODE_PRIVATE);
long actual = webAppPrefs.getLong(WebappDataStorage.KEY_LAST_USED,
WebappDataStorage.LAST_USED_INVALID);
- String webAppScope = webAppPrefs.getString(WebappDataStorage.KEY_SCOPE,
- WebappDataStorage.SCOPE_INVALID);
assertTrue("Timestamp is out of range", before <= actual && actual <= after);
- assertEquals(scope, webAppScope);
}
@Test
@@ -130,7 +139,7 @@ public class WebappRegistryTest {
assertTrue(mCallbackCalled);
mCallbackCalled = false;
- WebappRegistry.registerWebapp(Robolectric.application, "second", "https://www.google.com");
+ WebappRegistry.registerWebapp(Robolectric.application, "second");
BackgroundShadowAsyncTask.runBackgroundTasks();
// A copy of the expected set needs to be made as the SharedPreferences is using the copy
@@ -149,12 +158,7 @@ public class WebappRegistryTest {
@Test
@Feature({"Webapp"})
public void testUnregisterRunsCallback() throws Exception {
- WebappRegistry.unregisterAllWebapps(Robolectric.application, new Runnable() {
- @Override
- public void run() {
- mCallbackCalled = true;
- }
- });
+ WebappRegistry.unregisterAllWebapps(Robolectric.application, new Runner());
BackgroundShadowAsyncTask.runBackgroundTasks();
Robolectric.runUiThreadTasks();
@@ -166,12 +170,7 @@ public class WebappRegistryTest {
public void testUnregisterClearsRegistry() throws Exception {
addWebappsToRegistry("test");
- WebappRegistry.unregisterAllWebapps(Robolectric.application, new Runnable() {
- @Override
- public void run() {
- mCallbackCalled = true;
- }
- });
+ WebappRegistry.unregisterAllWebapps(Robolectric.application, new Runner());
BackgroundShadowAsyncTask.runBackgroundTasks();
Robolectric.runUiThreadTasks();
@@ -293,12 +292,7 @@ public class WebappRegistryTest {
@Test
@Feature({"Webapp"})
public void testClearWebappHistoryRunsCallback() throws Exception {
- WebappRegistry.clearWebappHistory(Robolectric.application, new Runnable() {
- @Override
- public void run() {
- mCallbackCalled = true;
- }
- });
+ WebappRegistry.clearWebappHistory(Robolectric.application, new Runner());
BackgroundShadowAsyncTask.runBackgroundTasks();
Robolectric.runUiThreadTasks();
@@ -308,10 +302,21 @@ public class WebappRegistryTest {
@Test
@Feature({"Webapp"})
public void testClearWebappHistory() throws Exception {
- final String webappScope1 = "https://www.google.com";
- final String webappScope2 = "https://drive.google.com";
- WebappRegistry.registerWebapp(Robolectric.application, "webapp1", webappScope1);
- WebappRegistry.registerWebapp(Robolectric.application, "webapp2", webappScope2);
+ final String webapp1Url = "https://www.google.com";
+ final String webapp2Url = "https://drive.google.com";
+ WebappDataStorage webapp1Storage =
+ WebappRegistry.registerWebapp(Robolectric.application, "webapp1");
+ WebappDataStorage webapp2Storage =
+ WebappRegistry.registerWebapp(Robolectric.application, "webapp2");
+ BackgroundShadowAsyncTask.runBackgroundTasks();
+
+ Intent shortcutIntent1 = createShortcutIntent(webapp1Url);
+ Intent shortcutIntent2 = createShortcutIntent(webapp2Url);
+
+ webapp1Storage.updateFromShortcutIntent(shortcutIntent1, new Runner());
+ BackgroundShadowAsyncTask.runBackgroundTasks();
+
+ webapp2Storage.updateFromShortcutIntent(shortcutIntent2, new Runner());
BackgroundShadowAsyncTask.runBackgroundTasks();
SharedPreferences webapp1Prefs = Robolectric.application.getSharedPreferences(
@@ -319,12 +324,7 @@ public class WebappRegistryTest {
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;
- }
- });
+ WebappRegistry.clearWebappHistory(Robolectric.application, new Runner());
BackgroundShadowAsyncTask.runBackgroundTasks();
Robolectric.runUiThreadTasks();
@@ -344,50 +344,90 @@ public class WebappRegistryTest {
WebappDataStorage.KEY_LAST_USED, WebappDataStorage.LAST_USED_UNSET);
assertEquals(WebappDataStorage.LAST_USED_UNSET, actualLastUsed);
- // Verify that the scope for both web apps is WebappDataStorage.SCOPE_INVALID.
+ // Verify that the URL and scope for both web apps is WebappDataStorage.URL_INVALID.
String actualScope = webapp1Prefs.getString(
- WebappDataStorage.KEY_SCOPE, WebappDataStorage.SCOPE_INVALID);
- assertEquals(WebappDataStorage.SCOPE_INVALID, actualScope);
+ WebappDataStorage.KEY_SCOPE, WebappDataStorage.URL_INVALID);
+ assertEquals(WebappDataStorage.URL_INVALID, actualScope);
+ String actualUrl = webapp1Prefs.getString(
+ WebappDataStorage.KEY_URL, WebappDataStorage.URL_INVALID);
+ assertEquals(WebappDataStorage.URL_INVALID, actualUrl);
actualScope = webapp2Prefs.getString(
- WebappDataStorage.KEY_SCOPE, WebappDataStorage.SCOPE_INVALID);
- assertEquals(WebappDataStorage.SCOPE_INVALID, actualScope);
+ WebappDataStorage.KEY_SCOPE, WebappDataStorage.URL_INVALID);
+ assertEquals(WebappDataStorage.URL_INVALID, actualScope);
+ actualUrl = webapp2Prefs.getString(
+ WebappDataStorage.KEY_URL, WebappDataStorage.URL_INVALID);
+ assertEquals(WebappDataStorage.URL_INVALID, actualUrl);
}
@Test
@Feature({"Webapp"})
public void testOpenAfterClearWebappHistory() throws Exception {
- final String webappScope = "https://www.google.com";
- WebappRegistry.registerWebapp(Robolectric.application, "webapp", webappScope);
+ WebappRegistry.registerWebapp(Robolectric.application, "webapp");
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;
- }
- });
+ WebappRegistry.clearWebappHistory(Robolectric.application, new Runner());
BackgroundShadowAsyncTask.runBackgroundTasks();
+ Robolectric.runUiThreadTasks();
+ assertTrue(mCallbackCalled);
- // Open the webapp up and set the scope.
+ // Open the webapp up to set the last used time.
WebappDataStorage.open(Robolectric.application, "webapp");
- WebappDataStorage.setScope(Robolectric.application, "webapp", webappScope);
BackgroundShadowAsyncTask.runBackgroundTasks();
- Robolectric.runUiThreadTasks();
- assertTrue(mCallbackCalled);
- // Verify that the last used time is valid and the scope is updated.
+ // Verify that the last used time is valid.
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 actualScope = webappPrefs.getString(
- WebappDataStorage.KEY_SCOPE, WebappDataStorage.SCOPE_INVALID);
- assertEquals(webappScope, actualScope);
}
+ @Test
+ @Feature({"Webapp"})
+ public void testUpdateAfterClearWebappHistory() throws Exception {
+ final String webappUrl = "http://www.google.com";
+ WebappDataStorage webappStorage =
+ WebappRegistry.registerWebapp(Robolectric.application, "webapp");
+ BackgroundShadowAsyncTask.runBackgroundTasks();
+
+ Intent shortcutIntent = createShortcutIntent(webappUrl);
+ webappStorage.updateFromShortcutIntent(shortcutIntent, new Runner());
+ BackgroundShadowAsyncTask.runBackgroundTasks();
+ Robolectric.runUiThreadTasks();
+ assertTrue(mCallbackCalled);
+
+ SharedPreferences webappPrefs = Robolectric.application.getSharedPreferences(
+ WebappDataStorage.SHARED_PREFS_FILE_PREFIX + "webapp", Context.MODE_PRIVATE);
+
+ // Verify that the URL and scope match the original in the intent.
+ String actualURL = webappPrefs.getString(
+ WebappDataStorage.KEY_URL, WebappDataStorage.URL_INVALID);
+ assertEquals(webappUrl, actualURL);
+ String actualScope = webappPrefs.getString(
+ WebappDataStorage.KEY_SCOPE, WebappDataStorage.URL_INVALID);
+ assertEquals(webappUrl, actualScope);
+
+ WebappRegistry.clearWebappHistory(Robolectric.application, new Runner());
+ BackgroundShadowAsyncTask.runBackgroundTasks();
+ Robolectric.runUiThreadTasks();
+ assertTrue(mCallbackCalled);
+
+ // Update the webapp from the intent again.
+ webappStorage.updateFromShortcutIntent(shortcutIntent, new Runner());
+ BackgroundShadowAsyncTask.runBackgroundTasks();
+ Robolectric.runUiThreadTasks();
+ assertTrue(mCallbackCalled);
+
+ // Verify that the URL and scope match the original in the intent.
+ actualURL = webappPrefs.getString(
+ WebappDataStorage.KEY_URL, WebappDataStorage.URL_INVALID);
+ assertEquals(webappUrl, actualURL);
+ actualScope = webappPrefs.getString(
+ WebappDataStorage.KEY_SCOPE, WebappDataStorage.URL_INVALID);
+ assertEquals(webappUrl, actualScope);
+ }
private Set<String> addWebappsToRegistry(String... webapps) {
final Set<String> expected = new HashSet<String>(Arrays.asList(webapps));
@@ -401,4 +441,9 @@ public class WebappRegistryTest {
return mSharedPreferences.getStringSet(
WebappRegistry.KEY_WEBAPP_SET, Collections.<String>emptySet());
}
+
+ private Intent createShortcutIntent(String url) {
+ return ShortcutHelper.createWebappShortcutIntent("id", "action", url, "name", "shortName",
+ null, 0, 0, 0, false);
+ }
}

Powered by Google App Engine
This is Rietveld 408576698