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..f9687ee17916d8b9b1fab18fb08fe34f29f3ae37 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,41 +120,82 @@ 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; |
- } |
- }); |
- } |
- |
- 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 testRemoveAllCookies() throws Exception { |
+ mCookieManager.setCookie("http://www.example.com", "name=test"); |
+ assertTrue(mCookieManager.hasCookies()); |
+ mCookieManager.removeAllCookies(); |
+ assertFalse(mCookieManager.hasCookies()); |
} |
@MediumTest |
@Feature({"AndroidWebView", "Privacy"}) |
- public void testRemoveAllCookie() throws Exception { |
- // enable cookie |
- mCookieManager.setAcceptCookie(true); |
- assertTrue(mCookieManager.acceptCookie()); |
+ public void testRemoveSessionCookies() throws Exception { |
+ final String url = "http://www.example.com"; |
+ final String sessionCookie = "cookie1=peter"; |
+ final String normalCookie = "cookie2=sue"; |
- // first there should be no cookie stored |
- mCookieManager.removeAllCookie(); |
- mCookieManager.flushCookieStore(); |
- assertFalse(mCookieManager.hasCookies()); |
+ mCookieManager.setCookie(url, sessionCookie); |
+ mCookieManager.setCookie(url, makeExpiringCookie(normalCookie, 600)); |
+ mCookieManager.removeSessionCookies(); |
+ |
+ String allCookies = mCookieManager.getCookie(url); |
+ assertFalse(allCookies.contains(sessionCookie)); |
+ assertTrue(allCookies.contains(normalCookie)); |
+ } |
+ |
+ @MediumTest |
+ @Feature({"AndroidWebView", "Privacy"}) |
+ public void testSetCookie() throws Throwable { |
String url = "http://www.example.com"; |
String cookie = "name=test"; |
mCookieManager.setCookie(url, cookie); |
assertEquals(cookie, mCookieManager.getCookie(url)); |
+ } |
+ |
+ @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 { |
+ final String url = "http://www.example.com"; |
+ final String cookie = "name=test"; |
+ final 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"; |
+ |
+ mCookieManager.setCookie(url, cookie, null); |
poll(new Callable<Boolean>() { |
@Override |
@@ -163,9 +203,38 @@ public class CookieManagerTest extends AwTestBase { |
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"); |
+ |
+ mCookieManager.removeAllCookies(null); |
+ |
+ // Eventually the cookies are removed. |
poll(new Callable<Boolean>() { |
@Override |
public Boolean call() throws Exception { |
@@ -176,67 +245,113 @@ 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); |
+ assertTrue(callback.getValue()); |
+ String allCookies = mCookieManager.getCookie(url); |
+ assertTrue(!allCookies.contains(sessionCookie)); |
+ assertTrue(allCookies.contains(normalCookie)); |
+ 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(); |
- 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.removeSessionCookies(null); |
- Thread.sleep(expiration + 1000); // wait for cookie to expire |
- mCookieManager.removeExpiredCookie(); |
+ // 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); |
} |
}); |
+ } |
+ |
+ @MediumTest |
+ @Feature({"AndroidWebView", "Privacy"}) |
+ public void testExpiredCookiesAreNotSet() throws Exception { |
+ final String url = "http://www.example.com"; |
+ final String cookie = "cookie1=peter"; |
+ |
+ mCookieManager.setCookie(url, makeExpiringCookie(cookie, -1)); |
+ assertNull(mCookieManager.getCookie(url)); |
+ } |
+ |
+ @MediumTest |
+ @Feature({"AndroidWebView", "Privacy"}) |
+ public void testCookiesExpire() throws Exception { |
+ final String url = "http://www.example.com"; |
+ final String cookie = "cookie1=peter"; |
- mCookieManager.removeAllCookie(); |
+ mCookieManager.setCookie(url, makeExpiringCookieMs(cookie, 1200)); |
+ |
+ // The cookie exists: |
+ assertTrue(mCookieManager.hasCookies()); |
+ |
+ // But eventually expires: |
poll(new Callable<Boolean>() { |
@Override |
public Boolean call() throws Exception { |
- return mCookieManager.getCookie(url) == null; |
+ return !mCookieManager.hasCookies(); |
} |
}); |
} |
@MediumTest |
@Feature({"AndroidWebView", "Privacy"}) |
+ public void testCookieExpiration() throws Exception { |
+ final String url = "http://www.example.com"; |
+ final String sessionCookie = "cookie1=peter"; |
+ final String longCookie = "cookie2=marc"; |
+ |
+ mCookieManager.setCookie(url, sessionCookie); |
+ mCookieManager.setCookie(url, makeExpiringCookie(longCookie, 600)); |
+ |
+ String allCookies = mCookieManager.getCookie(url); |
+ assertTrue(allCookies.contains(sessionCookie)); |
+ 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.removeExpiredCookies(); |
+ |
+ allCookies = mCookieManager.getCookie(url); |
+ assertTrue(allCookies.contains(sessionCookie)); |
+ assertTrue(allCookies.contains(longCookie)); |
+ } |
+ |
+ @MediumTest |
+ @Feature({"AndroidWebView", "Privacy"}) |
public void testThirdPartyCookie() throws Throwable { |
TestWebServer webServer = null; |
try { |
@@ -254,9 +369,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 +440,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 +508,71 @@ 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))); |
+ } |
+ |
+ private String makeExpiringCookie(String cookie, int secondsTillExpiry) { |
+ return makeExpiringCookieMs(cookie, secondsTillExpiry * 1000); |
+ } |
+ |
+ @SuppressWarnings("deprecation") |
+ private String makeExpiringCookieMs(String cookie, int millisecondsTillExpiry) { |
+ Date date = new Date(); |
+ date.setTime(date.getTime() + millisecondsTillExpiry); |
+ return cookie + "; expires=" + date.toGMTString(); |
+ } |
} |