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

Unified Diff: android_webview/javatests/src/org/chromium/android_webview/test/CookieManagerTest.java

Issue 273873003: Adds Asynchronous APIs to CookieManager (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Refactor cookie_manager.cc Created 6 years, 7 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: android_webview/javatests/src/org/chromium/android_webview/test/CookieManagerTest.java
diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/CookieManagerTest.java b/android_webview/javatests/src/org/chromium/android_webview/test/CookieManagerTest.java
index d1059667adb5b2645e079e3bd350aff1ab4c924a..d93440454c5063a89898ddbee4f396f1bd072fa7 100644
--- a/android_webview/javatests/src/org/chromium/android_webview/test/CookieManagerTest.java
+++ b/android_webview/javatests/src/org/chromium/android_webview/test/CookieManagerTest.java
@@ -6,11 +6,14 @@ package org.chromium.android_webview.test;
import android.test.MoreAsserts;
import android.test.suitebuilder.annotation.MediumTest;
-import android.test.suitebuilder.annotation.SmallTest;
import android.util.Pair;
+import android.webkit.ValueCallback;
+
+import static org.chromium.android_webview.test.util.CookieUtils.TestValueCallback;
import org.chromium.android_webview.AwContents;
import org.chromium.android_webview.AwCookieManager;
+import org.chromium.android_webview.test.util.CookieUtils;
import org.chromium.android_webview.test.util.JSUtils;
import org.chromium.base.test.util.Feature;
import org.chromium.net.test.util.TestWebServer;
@@ -43,16 +46,17 @@ public class CookieManagerTest extends AwTestBase {
mAwContents = testContainerView.getAwContents();
mAwContents.getSettings().setJavaScriptEnabled(true);
assertNotNull(mCookieManager);
- }
- @SmallTest
- @Feature({"AndroidWebView", "Privacy"})
- public void testAllowFileSchemeCookies() throws Throwable {
- assertFalse(mCookieManager.allowFileSchemeCookies());
- mCookieManager.setAcceptFileSchemeCookies(true);
- assertTrue(mCookieManager.allowFileSchemeCookies());
- mCookieManager.setAcceptFileSchemeCookies(false);
- assertFalse(mCookieManager.allowFileSchemeCookies());
+ // All tests start with no cookies.
+ try {
+ clearCookies();
+ } catch (Throwable e) {
+ throw new RuntimeException("Could not clear cookies.");
+ }
+
+ // But setting cookies enabled.
+ mCookieManager.setAcceptCookie(true);
+ assertTrue(mCookieManager.acceptCookie());
}
@MediumTest
@@ -67,12 +71,10 @@ public class CookieManagerTest extends AwTestBase {
String url = webServer.setResponse(path, responseStr, null);
mCookieManager.setAcceptCookie(false);
- mCookieManager.removeAllCookie();
assertFalse(mCookieManager.acceptCookie());
- assertFalse(mCookieManager.hasCookies());
loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url);
- setCookie("test1", "value1");
+ setCookieWithJavaScript("test1", "value1");
assertNull(mCookieManager.getCookie(url));
List<Pair<String, String>> responseHeaders = new ArrayList<Pair<String, String>>();
@@ -87,7 +89,7 @@ public class CookieManagerTest extends AwTestBase {
url = webServer.setResponse(path, responseStr, null);
loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url);
- setCookie("test2", "value2");
+ setCookieWithJavaScript("test2", "value2");
waitForCookie(url);
String cookie = mCookieManager.getCookie(url);
assertNotNull(cookie);
@@ -102,15 +104,12 @@ public class CookieManagerTest extends AwTestBase {
cookie = mCookieManager.getCookie(url);
assertNotNull(cookie);
validateCookies(cookie, "test2", "header-test2");
-
- // clean up all cookies
- mCookieManager.removeAllCookie();
} finally {
if (webServer != null) webServer.shutdown();
}
}
- private void setCookie(final String name, final String value)
+ private void setCookieWithJavaScript(final String name, final String value)
throws Throwable {
JSUtils.executeJavaScriptAndWaitForResult(
this, mAwContents,
@@ -121,51 +120,124 @@ public class CookieManagerTest extends AwTestBase {
"; expires=' + expirationDate.toUTCString();");
}
- private void waitForCookie(final String url) throws Exception {
- poll(new Callable<Boolean>() {
- @Override
- public Boolean call() throws Exception {
- return mCookieManager.getCookie(url) != null;
- }
- });
+ @MediumTest
+ @Feature({"AndroidWebView", "Privacy"})
+ public void testRemoveAllCookies() throws Exception {
+ mCookieManager.setCookie("http://www.example.com", "name=test");
+ assertTrue(mCookieManager.hasCookies());
+ mCookieManager.removeAllCookie();
+ assertFalse(mCookieManager.hasCookies());
}
- private void validateCookies(String responseCookie, String... expectedCookieNames) {
- String[] cookies = responseCookie.split(";");
- Set<String> foundCookieNames = new HashSet<String>();
- for (String cookie : cookies) {
- foundCookieNames.add(cookie.substring(0, cookie.indexOf("=")).trim());
- }
- MoreAsserts.assertEquals(
- foundCookieNames, new HashSet<String>(Arrays.asList(expectedCookieNames)));
+ @MediumTest
+ @Feature({"AndroidWebView", "Privacy"})
+ public void testRemoveSessionCookie() throws Exception {
+ final String url = "http://www.example.com";
+ final String sessionCookie = "cookie1=peter";
+ final String normalCookie = "cookie2=sue";
+
+ mCookieManager.setCookie(url, sessionCookie);
+ mCookieManager.setCookie(url, makeExpiringCookie(normalCookie, 600));
+
+ mCookieManager.removeSessionCookie();
+
+ String allCookies = mCookieManager.getCookie(url);
+ assertFalse(allCookies.contains(sessionCookie));
+ assertTrue(allCookies.contains(normalCookie));
}
@MediumTest
@Feature({"AndroidWebView", "Privacy"})
- public void testRemoveAllCookie() throws Exception {
- // enable cookie
- mCookieManager.setAcceptCookie(true);
- assertTrue(mCookieManager.acceptCookie());
+ public void testSetCookie() throws Throwable {
+ String url = "http://www.example.com";
+ String cookie = "name=test";
+ mCookieManager.setCookie(url, cookie);
+ assertEquals(cookie, mCookieManager.getCookie(url));
+ }
- // first there should be no cookie stored
- mCookieManager.removeAllCookie();
- mCookieManager.flushCookieStore();
+ @MediumTest
+ @Feature({"AndroidWebView", "Privacy"})
+ public void testHasCookie() throws Throwable {
assertFalse(mCookieManager.hasCookies());
+ mCookieManager.setCookie("http://www.example.com", "name=test");
+ assertTrue(mCookieManager.hasCookies());
+ }
+ @MediumTest
+ @Feature({"AndroidWebView", "Privacy"})
+ public void testSetCookieCallback() throws Throwable {
String url = "http://www.example.com";
String cookie = "name=test";
- mCookieManager.setCookie(url, cookie);
+ String brokenUrl = "foo";
+
+ final TestValueCallback<Boolean> callback = new TestValueCallback<Boolean>();
+ int callCount = callback.getOnReceiveValueHelper().getCallCount();
+
+ setCookieOnUiThread(url, cookie, callback);
+ callback.getOnReceiveValueHelper().waitForCallback(callCount);
+ assertTrue(callback.getValue());
assertEquals(cookie, mCookieManager.getCookie(url));
+ callCount = callback.getOnReceiveValueHelper().getCallCount();
+
+ setCookieOnUiThread(brokenUrl, cookie, callback);
+ callback.getOnReceiveValueHelper().waitForCallback(callCount);
+ assertFalse(callback.getValue());
+ assertEquals(null, mCookieManager.getCookie(brokenUrl));
+ }
+
+ @MediumTest
+ @Feature({"AndroidWebView", "Privacy"})
+ public void testSetCookieNullCallback() throws Throwable {
+ mCookieManager.setAcceptCookie(true);
+ assertTrue(mCookieManager.acceptCookie());
+
+ final String url = "http://www.example.com";
+ final String cookie = "name=test";
+
+ // We do not require this to be called on a thread with a Looper.
+ mCookieManager.setCookie(url, cookie, null);
+
poll(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return mCookieManager.hasCookies();
}
});
+ }
- // clean up all cookies
- mCookieManager.removeAllCookie();
+
+ @MediumTest
+ @Feature({"AndroidWebView", "Privacy"})
+ public void testRemoveAllCookiesCallback() throws Throwable {
+ TestValueCallback<Boolean> callback = new TestValueCallback<Boolean>();
+ int callCount = callback.getOnReceiveValueHelper().getCallCount();
+
+ mCookieManager.setCookie("http://www.example.com", "name=test");
+
+ // When we remove all cookies the first time some cookies are removed.
+ removeAllCookiesOnUiThread(callback);
+ callback.getOnReceiveValueHelper().waitForCallback(callCount);
+ assertTrue(callback.getValue());
+ assertFalse(mCookieManager.hasCookies());
+
+ callCount = callback.getOnReceiveValueHelper().getCallCount();
+
+ // The second time none are removed.
+ removeAllCookiesOnUiThread(callback);
+ callback.getOnReceiveValueHelper().waitForCallback(callCount);
+ assertFalse(callback.getValue());
+ }
+
+ @MediumTest
+ @Feature({"AndroidWebView", "Privacy"})
+ public void testRemoveAllCookiesNullCallback() throws Throwable {
+ mCookieManager.setCookie("http://www.example.com", "name=test");
+
+ // We do not require this to be called on a thread with a Looper.
+ mCookieManager.removeAllCookies(null);
+
+ // Eventually the cookies are removed.
poll(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
@@ -176,63 +248,84 @@ public class CookieManagerTest extends AwTestBase {
@MediumTest
@Feature({"AndroidWebView", "Privacy"})
- @SuppressWarnings("deprecation")
- public void testCookieExpiration() throws Exception {
- // enable cookie
- mCookieManager.setAcceptCookie(true);
- assertTrue(mCookieManager.acceptCookie());
- mCookieManager.removeAllCookie();
- assertFalse(mCookieManager.hasCookies());
-
+ public void testRemoveSessionCookiesCallback() throws Throwable {
final String url = "http://www.example.com";
- final String cookie1 = "cookie1=peter";
- final String cookie2 = "cookie2=sue";
- final String cookie3 = "cookie3=marc";
+ final String sessionCookie = "cookie1=peter";
+ final String normalCookie = "cookie2=sue";
- mCookieManager.setCookie(url, cookie1); // session cookie
+ TestValueCallback<Boolean> callback = new TestValueCallback<Boolean>();
+ int callCount = callback.getOnReceiveValueHelper().getCallCount();
- Date date = new Date();
- date.setTime(date.getTime() + 1000 * 600);
- String value2 = cookie2 + "; expires=" + date.toGMTString();
- mCookieManager.setCookie(url, value2); // expires in 10min
+ mCookieManager.setCookie(url, sessionCookie);
+ mCookieManager.setCookie(url, makeExpiringCookie(normalCookie, 600));
- long expiration = 3000;
- date = new Date();
- date.setTime(date.getTime() + expiration);
- String value3 = cookie3 + "; expires=" + date.toGMTString();
- mCookieManager.setCookie(url, value3); // expires in 3s
+ // When there is a session cookie then it is removed.
+ removeSessionCookiesOnUiThread(callback);
+ callback.getOnReceiveValueHelper().waitForCallback(callCount);
+ String allCookies = mCookieManager.getCookie(url);
+ assertTrue(!allCookies.contains(sessionCookie));
+ assertTrue(allCookies.contains(normalCookie));
+ assertTrue(callback.getValue());
mkosiba (inactive) 2014/05/15 12:40:35 uber-nit: I it would read better if this were imme
hjd 2014/05/15 14:11:39 Done.
+
+ callCount = callback.getOnReceiveValueHelper().getCallCount();
+ // If there are no session cookies then none are removed.
+ removeSessionCookiesOnUiThread(callback);
+ callback.getOnReceiveValueHelper().waitForCallback(callCount);
+ assertFalse(callback.getValue());
+ }
+
+ @MediumTest
+ @Feature({"AndroidWebView", "Privacy"})
+ public void testRemoveSessionCookiesNullCallback() throws Throwable {
+ final String url = "http://www.example.com";
+ final String sessionCookie = "cookie1=peter";
+ final String normalCookie = "cookie2=sue";
+
+ mCookieManager.setCookie(url, sessionCookie);
+ mCookieManager.setCookie(url, makeExpiringCookie(normalCookie, 600));
String allCookies = mCookieManager.getCookie(url);
- assertTrue(allCookies.contains(cookie1));
- assertTrue(allCookies.contains(cookie2));
- assertTrue(allCookies.contains(cookie3));
+ assertTrue(allCookies.contains(sessionCookie));
+ assertTrue(allCookies.contains(normalCookie));
- mCookieManager.removeSessionCookie();
+ // We do not require this to be called on a thread with a Looper.
mkosiba (inactive) 2014/05/15 12:40:35 I'm not sure this comment is helpful. The thread y
hjd 2014/05/15 14:11:39 Done.
+ mCookieManager.removeSessionCookies(null);
+
+ // Eventually the session cookie is removed.
poll(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
String c = mCookieManager.getCookie(url);
- return !c.contains(cookie1) && c.contains(cookie2) && c.contains(cookie3);
+ return !c.contains(sessionCookie) && c.contains(normalCookie);
}
});
+ }
- Thread.sleep(expiration + 1000); // wait for cookie to expire
+ @MediumTest
+ @Feature({"AndroidWebView", "Privacy"})
+ public void testCookieExpiration() throws Exception {
mkosiba (inactive) 2014/05/15 12:40:35 sweet! thanks for separating this into 2 test case
+ final String url = "http://www.example.com";
+ final String sessionCookie = "cookie1=peter";
+ final String quickCookie = "cookie2=sue";
+ final String longCookie = "cookie3=marc";
+
+ mCookieManager.setCookie(url, sessionCookie);
+ mCookieManager.setCookie(url, makeExpiringCookie(longCookie, 600));
+ mCookieManager.setCookie(url, makeExpiringCookie(quickCookie, -1));
+
+ String allCookies = mCookieManager.getCookie(url);
+ assertTrue(allCookies.contains(sessionCookie));
+ assertFalse(allCookies.contains(quickCookie)); // Expired cookies don't even get set.
+ assertTrue(allCookies.contains(longCookie));
+
+ // Removing expired cookies doesn't have an observable effect but since people will still
+ // be calling it for a while it shouldn't break anything either.
mCookieManager.removeExpiredCookie();
- poll(new Callable<Boolean>() {
- @Override
- public Boolean call() throws Exception {
- String c = mCookieManager.getCookie(url);
- return !c.contains(cookie1) && c.contains(cookie2) && !c.contains(cookie3);
- }
- });
- mCookieManager.removeAllCookie();
- poll(new Callable<Boolean>() {
- @Override
- public Boolean call() throws Exception {
- return mCookieManager.getCookie(url) == null;
- }
- });
+ allCookies = mCookieManager.getCookie(url);
+ assertTrue(allCookies.contains(sessionCookie));
+ assertFalse(allCookies.contains(quickCookie));
+ assertTrue(allCookies.contains(longCookie));
}
@MediumTest
@@ -254,9 +347,7 @@ public class CookieManagerTest extends AwTestBase {
// Turn global allow on.
mCookieManager.setAcceptCookie(true);
- mCookieManager.removeAllCookie();
assertTrue(mCookieManager.acceptCookie());
- assertFalse(mCookieManager.hasCookies());
// When third party cookies are disabled...
mCookieManager.setAcceptThirdPartyCookie(false);
@@ -327,9 +418,7 @@ public class CookieManagerTest extends AwTestBase {
webServer = new TestWebServer(false);
mCookieManager.setAcceptCookie(true);
- mCookieManager.removeAllCookie();
assertTrue(mCookieManager.acceptCookie());
- assertFalse(mCookieManager.hasCookies());
// When third party cookies are disabled...
mCookieManager.setAcceptThirdPartyCookie(false);
@@ -397,4 +486,67 @@ public class CookieManagerTest extends AwTestBase {
private String toThirdPartyUrl(String url) {
return url.replace("localhost", "127.0.0.1");
}
+
+ private void setCookieOnUiThread(final String url, final String cookie,
+ final ValueCallback<Boolean> callback) throws Throwable {
+ runTestOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ mCookieManager.setCookie(url, cookie, callback);
+ }
+ });
+ }
+
+ private void removeSessionCookiesOnUiThread(final ValueCallback<Boolean> callback)
+ throws Throwable {
+ runTestOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ mCookieManager.removeSessionCookies(callback);
+ }
+ });
+ }
+
+ private void removeAllCookiesOnUiThread(final ValueCallback<Boolean> callback)
+ throws Throwable {
+ runTestOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ mCookieManager.removeAllCookies(callback);
+ }
+ });
+ }
+
+ /**
+ * Clears all cookies synchronously.
+ */
+ private void clearCookies() throws Throwable {
+ CookieUtils.clearCookies(this, mCookieManager);
+ }
+
+ private void waitForCookie(final String url) throws Exception {
+ poll(new Callable<Boolean>() {
+ @Override
+ public Boolean call() throws Exception {
+ return mCookieManager.getCookie(url) != null;
+ }
+ });
+ }
+
+ private void validateCookies(String responseCookie, String... expectedCookieNames) {
+ String[] cookies = responseCookie.split(";");
+ Set<String> foundCookieNames = new HashSet<String>();
+ for (String cookie : cookies) {
+ foundCookieNames.add(cookie.substring(0, cookie.indexOf("=")).trim());
+ }
+ MoreAsserts.assertEquals(
+ foundCookieNames, new HashSet<String>(Arrays.asList(expectedCookieNames)));
+ }
+
+ @SuppressWarnings("deprecation")
+ private String makeExpiringCookie(String cookie, int secondsTillExpiry) {
+ Date date = new Date();
+ date.setTime(date.getTime() + 1000 * secondsTillExpiry);
+ return cookie + "; expires=" + date.toGMTString();
+ }
}

Powered by Google App Engine
This is Rietveld 408576698