Chromium Code Reviews| 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..931dba1ad845ad9246d123385ea3dcd67ed8b11d 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,12 @@ 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 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; |
| @@ -22,6 +23,10 @@ import java.util.HashSet; |
| import java.util.List; |
| import java.util.Set; |
| import java.util.concurrent.Callable; |
| +import java.util.concurrent.Semaphore; |
| +import java.util.concurrent.TimeUnit; |
| +import java.util.concurrent.atomic.AtomicBoolean; |
| +import java.util.concurrent.atomic.AtomicInteger; |
| /** |
| * Tests for the CookieManager. |
| @@ -43,16 +48,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 +73,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 +91,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 +106,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, |
| @@ -142,20 +143,91 @@ public class CookieManagerTest extends AwTestBase { |
| @MediumTest |
| @Feature({"AndroidWebView", "Privacy"}) |
| - public void testRemoveAllCookie() throws Exception { |
| - // enable cookie |
| - mCookieManager.setAcceptCookie(true); |
| - assertTrue(mCookieManager.acceptCookie()); |
| - |
| - // first there should be no cookie stored |
| + public void testRemoveAllCookies() throws Exception { |
| + mCookieManager.setCookie("http://www.example.com", "name=test"); |
| + assertTrue(mCookieManager.hasCookies()); |
| mCookieManager.removeAllCookie(); |
| - mCookieManager.flushCookieStore(); |
| - assertFalse(mCookieManager.hasCookies()); |
| + assertTrue(!mCookieManager.hasCookies()); |
| + } |
| + |
| + @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)); |
| + String allCookies = mCookieManager.getCookie(url); |
| + assertTrue(allCookies.contains(sessionCookie)); |
| + assertTrue(allCookies.contains(normalCookie)); |
| + |
| + mCookieManager.removeSessionCookie(); |
| + |
| + allCookies = mCookieManager.getCookie(url); |
| + assertTrue(!allCookies.contains(sessionCookie)); |
| + assertTrue(allCookies.contains(normalCookie)); |
| + } |
| + |
| + @SuppressWarnings("deprecation") |
| + private String makeExpiringCookie(String cookie, int secondsTillExpiry) { |
| + Date date = new Date(); |
| + date.setTime(date.getTime() + 1000 * secondsTillExpiry); |
| + return cookie + "; expires=" + date.toGMTString(); |
| + } |
| + |
| + @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 testSetCookieCallback() throws Throwable { |
| + mCookieManager.setAcceptCookie(true); |
| + assertTrue(mCookieManager.acceptCookie()); |
| + |
| + String url = "http://www.example.com"; |
| + String cookie = "name=test"; |
| + String brokenUrl = "foo"; |
| + |
| + final Semaphore s = new Semaphore(0); |
| + final AtomicBoolean result = new AtomicBoolean(); |
| + final ValueCallback<Boolean> callback = new ValueCallback<Boolean>() { |
|
mkosiba (inactive)
2014/05/13 09:37:17
you have the same pattern (semaphore, atomicXXX, v
hjd_google
2014/05/15 10:52:07
Done.
|
| + @Override |
| + public void onReceiveValue(Boolean success) { |
| + result.set(success); |
| + s.release(); |
| + } |
| + }; |
| + |
| + setCookieOnUiThread(url, cookie, callback); |
| + s.acquire(); |
| + assertTrue(result.get()); |
| + assertEquals(cookie, mCookieManager.getCookie(url)); |
| + |
| + setCookieOnUiThread(brokenUrl, cookie, callback); |
| + s.acquire(); |
|
mkosiba (inactive)
2014/05/13 09:37:17
nit: could use CallbackHelper for this.
hjd_google
2014/05/15 10:52:07
I feel like adding a comment to CallbackHelper, "T
|
| + assertFalse(result.get()); |
| + 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 |
| @@ -163,9 +235,42 @@ public class CookieManagerTest extends AwTestBase { |
| return mCookieManager.hasCookies(); |
| } |
| }); |
| + } |
| - // clean up all cookies |
| - mCookieManager.removeAllCookie(); |
| + @MediumTest |
| + @Feature({"AndroidWebView", "Privacy"}) |
| + public void testRemoveAllCookiesCallback() throws Throwable { |
| + mCookieManager.setCookie("http://www.example.com", "name=test"); |
| + assertTrue(mCookieManager.hasCookies()); |
| + |
| + final Semaphore s = new Semaphore(0); |
| + final AtomicInteger numDeleted = new AtomicInteger(); |
| + final ValueCallback<Integer> callback = new ValueCallback<Integer>() { |
| + @Override |
| + public void onReceiveValue(Integer n) { |
| + numDeleted.set(n); |
| + s.release(); |
| + } |
| + }; |
| + |
| + removeAllCookiesOnUiThread(callback); |
| + |
| + assertTrue(s.tryAcquire(WAIT_TIMEOUT_MS, TimeUnit.MILLISECONDS)); |
| + |
| + assertEquals(1, numDeleted.get()); |
| + assertFalse(mCookieManager.hasCookies()); |
| + } |
| + |
| + @MediumTest |
| + @Feature({"AndroidWebView", "Privacy"}) |
| + public void testRemoveAllCookiesNullCallback() throws Throwable { |
| + mCookieManager.setCookie("http://www.example.com", "name=test"); |
| + assertTrue(mCookieManager.hasCookies()); |
| + |
| + // 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 +281,88 @@ 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 |
| + mCookieManager.setCookie(url, sessionCookie); |
| + mCookieManager.setCookie(url, makeExpiringCookie(normalCookie, 600)); |
| + String allCookies = mCookieManager.getCookie(url); |
| + assertTrue(allCookies.contains(sessionCookie)); |
| + assertTrue(allCookies.contains(normalCookie)); |
| - Date date = new Date(); |
| - date.setTime(date.getTime() + 1000 * 600); |
| - String value2 = cookie2 + "; expires=" + date.toGMTString(); |
| - mCookieManager.setCookie(url, value2); // expires in 10min |
| + final Semaphore s = new Semaphore(0); |
| + final AtomicInteger numDeleted = new AtomicInteger(); |
| + final ValueCallback<Integer> callback = new ValueCallback<Integer>() { |
| + @Override |
| + public void onReceiveValue(Integer n) { |
| + numDeleted.set(n); |
| + s.release(); |
| + } |
| + }; |
| + |
| + removeSessionCookiesOnUiThread(callback); |
| + |
| + assertTrue(s.tryAcquire(WAIT_TIMEOUT_MS, TimeUnit.MILLISECONDS)); |
| + |
| + allCookies = mCookieManager.getCookie(url); |
| + assertTrue(!allCookies.contains(sessionCookie)); |
| + assertTrue(allCookies.contains(normalCookie)); |
| + assertEquals(1, numDeleted.get()); |
| + } |
| - long expiration = 3000; |
| - date = new Date(); |
| - date.setTime(date.getTime() + expiration); |
| - String value3 = cookie3 + "; expires=" + date.toGMTString(); |
| - mCookieManager.setCookie(url, value3); // expires in 3s |
| + @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. |
| + 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); |
| } |
| }); |
| + } |
| + |
| + @MediumTest |
| + @Feature({"AndroidWebView", "Privacy"}) |
| + public void testCookieExpiration() throws Exception { |
| + 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)); // expires in 10min |
| + mCookieManager.setCookie(url, makeExpiringCookie(quickCookie, 3)); // expires in 3sec |
| + |
| + String allCookies = mCookieManager.getCookie(url); |
| + assertTrue(allCookies.contains(sessionCookie)); |
| + assertTrue(allCookies.contains(quickCookie)); |
| + assertTrue(allCookies.contains(longCookie)); |
| + |
| + Thread.sleep(3500); // wait for cookie to expire |
|
mkosiba (inactive)
2014/05/13 09:37:17
ouch! could we set one that had already expired an
hjd_google
2014/05/15 10:52:07
That wouldn't test the same thing since expired co
|
| - Thread.sleep(expiration + 1000); // wait for cookie to expire |
| 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 +384,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 +455,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 +523,42 @@ 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<Integer> callback) |
| + throws Throwable { |
| + runTestOnUiThread(new Runnable() { |
| + @Override |
| + public void run() { |
| + mCookieManager.removeSessionCookies(callback); |
| + } |
| + }); |
| + } |
| + |
| + private void removeAllCookiesOnUiThread(final ValueCallback<Integer> 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); |
| + } |
| + |
| } |