| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.android_webview.test; |
| 6 |
| 7 import android.test.MoreAsserts; |
| 8 import android.test.suitebuilder.annotation.MediumTest; |
| 9 import android.test.suitebuilder.annotation.SmallTest; |
| 10 import android.util.Pair; |
| 11 |
| 12 import org.chromium.android_webview.CookieManager; |
| 13 import org.chromium.base.test.Feature; |
| 14 import org.chromium.content.browser.ContentViewCore; |
| 15 import org.chromium.content.browser.test.Criteria; |
| 16 import org.chromium.content.browser.test.CriteriaHelper; |
| 17 import org.chromium.content.browser.test.TestContentViewClient.OnEvaluateJavaScr
iptResultHelper; |
| 18 |
| 19 import java.util.ArrayList; |
| 20 import java.util.Arrays; |
| 21 import java.util.Date; |
| 22 import java.util.HashSet; |
| 23 import java.util.List; |
| 24 import java.util.Set; |
| 25 import java.util.concurrent.atomic.AtomicInteger; |
| 26 |
| 27 /** |
| 28 * Tests for the CookieManager. |
| 29 */ |
| 30 public class CookieManagerTest extends AndroidWebViewTestBase { |
| 31 |
| 32 private CookieManager mCookieManager; |
| 33 private TestAwContentsClient mContentViewClient; |
| 34 private ContentViewCore mContentViewCore; |
| 35 |
| 36 @Override |
| 37 protected void setUp() throws Exception { |
| 38 super.setUp(); |
| 39 |
| 40 mCookieManager = new CookieManager(); |
| 41 mContentViewClient = new TestAwContentsClient(); |
| 42 mContentViewCore = |
| 43 createAwTestContainerViewOnMainSync(mContentViewClient).getConte
ntViewCore(); |
| 44 mContentViewCore.getContentSettings().setJavaScriptEnabled(true); |
| 45 assertNotNull(mCookieManager); |
| 46 } |
| 47 |
| 48 @SmallTest |
| 49 @Feature({"Android-WebView", "Privacy"}) |
| 50 public void testAllowFileSchemeCookies() throws Throwable { |
| 51 assertFalse(CookieManager.allowFileSchemeCookies()); |
| 52 CookieManager.setAcceptFileSchemeCookies(true); |
| 53 assertTrue(CookieManager.allowFileSchemeCookies()); |
| 54 CookieManager.setAcceptFileSchemeCookies(false); |
| 55 assertFalse(CookieManager.allowFileSchemeCookies()); |
| 56 } |
| 57 |
| 58 @MediumTest |
| 59 @Feature({"Android-WebView", "Privacy"}) |
| 60 public void testAcceptCookie() throws Throwable { |
| 61 TestWebServer webServer = null; |
| 62 try { |
| 63 webServer = new TestWebServer(false); |
| 64 String path = "/cookie_test.html"; |
| 65 String responseStr = |
| 66 "<html><head><title>TEST!</title></head><body>HELLO!</body><
/html>"; |
| 67 String url = webServer.setResponse(path, responseStr, null); |
| 68 |
| 69 mCookieManager.setAcceptCookie(false); |
| 70 mCookieManager.removeAllCookie(); |
| 71 assertFalse(mCookieManager.acceptCookie()); |
| 72 assertFalse(mCookieManager.hasCookies()); |
| 73 |
| 74 loadUrlSync(mContentViewCore, mContentViewClient.getOnPageFinishedHe
lper(), url); |
| 75 setCookie("test1", "value1"); |
| 76 assertNull(mCookieManager.getCookie(url)); |
| 77 |
| 78 List<Pair<String, String>> responseHeaders = new ArrayList<Pair<Stri
ng, String>>(); |
| 79 responseHeaders.add( |
| 80 Pair.create("Set-Cookie", "header-test1=header-value1; path=
" + path)); |
| 81 url = webServer.setResponse(path, responseStr, responseHeaders); |
| 82 loadUrlSync(mContentViewCore, mContentViewClient.getOnPageFinishedHe
lper(), url); |
| 83 assertNull(mCookieManager.getCookie(url)); |
| 84 |
| 85 mCookieManager.setAcceptCookie(true); |
| 86 assertTrue(mCookieManager.acceptCookie()); |
| 87 |
| 88 url = webServer.setResponse(path, responseStr, null); |
| 89 loadUrlSync(mContentViewCore, mContentViewClient.getOnPageFinishedHe
lper(), url); |
| 90 setCookie("test2", "value2"); |
| 91 waitForCookie(url); |
| 92 String cookie = mCookieManager.getCookie(url); |
| 93 assertNotNull(cookie); |
| 94 validateCookies(cookie, "test2"); |
| 95 |
| 96 responseHeaders = new ArrayList<Pair<String, String>>(); |
| 97 responseHeaders.add( |
| 98 Pair.create("Set-Cookie", "header-test2=header-value2 path="
+ path)); |
| 99 url = webServer.setResponse(path, responseStr, responseHeaders); |
| 100 loadUrlSync(mContentViewCore, mContentViewClient.getOnPageFinishedHe
lper(), url); |
| 101 waitForCookie(url); |
| 102 cookie = mCookieManager.getCookie(url); |
| 103 assertNotNull(cookie); |
| 104 validateCookies(cookie, "test2", "header-test2"); |
| 105 |
| 106 // clean up all cookies |
| 107 mCookieManager.removeAllCookie(); |
| 108 } finally { |
| 109 if (webServer != null) webServer.shutdown(); |
| 110 } |
| 111 } |
| 112 |
| 113 private void setCookie(final String name, final String value) |
| 114 throws Throwable { |
| 115 OnEvaluateJavaScriptResultHelper onEvaluateJavaScriptResultHelper = |
| 116 mContentViewClient.getOnEvaluateJavaScriptResultHelper(); |
| 117 int currentCallCount = onEvaluateJavaScriptResultHelper.getCallCount(); |
| 118 final AtomicInteger requestId = new AtomicInteger(); |
| 119 runTestOnUiThread(new Runnable() { |
| 120 @Override |
| 121 public void run() { |
| 122 requestId.set(mContentViewCore.evaluateJavaScript( |
| 123 "var expirationDate = new Date();" + |
| 124 "expirationDate.setDate(expirationDate.getDate() + 5);"
+ |
| 125 "document.cookie='" + name + "=" + value + |
| 126 "; expires=' + expirationDate.toUTCString();")); |
| 127 } |
| 128 }); |
| 129 onEvaluateJavaScriptResultHelper.waitForCallback(currentCallCount); |
| 130 assertEquals("Response ID mismatch when evaluating JavaScript.", |
| 131 requestId.get(), onEvaluateJavaScriptResultHelper.getId()); |
| 132 } |
| 133 |
| 134 private void waitForCookie(final String url) throws InterruptedException { |
| 135 assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { |
| 136 @Override |
| 137 public boolean isSatisfied() { |
| 138 return mCookieManager.getCookie(url) != null; |
| 139 } |
| 140 })); |
| 141 } |
| 142 |
| 143 private void validateCookies(String responseCookie, String... expectedCookie
Names) { |
| 144 String[] cookies = responseCookie.split(";"); |
| 145 Set<String> foundCookieNames = new HashSet<String>(); |
| 146 for (String cookie : cookies) { |
| 147 foundCookieNames.add(cookie.substring(0, cookie.indexOf("=")).trim()
); |
| 148 } |
| 149 MoreAsserts.assertEquals( |
| 150 foundCookieNames, new HashSet<String>(Arrays.asList(expectedCook
ieNames))); |
| 151 } |
| 152 |
| 153 @MediumTest |
| 154 @Feature({"Android-WebView", "Privacy"}) |
| 155 public void testRemoveAllCookie() throws InterruptedException { |
| 156 // enable cookie |
| 157 mCookieManager.setAcceptCookie(true); |
| 158 assertTrue(mCookieManager.acceptCookie()); |
| 159 |
| 160 // first there should be no cookie stored |
| 161 mCookieManager.removeAllCookie(); |
| 162 assertFalse(mCookieManager.hasCookies()); |
| 163 |
| 164 String url = "http://www.example.com"; |
| 165 String cookie = "name=test"; |
| 166 mCookieManager.setCookie(url, cookie); |
| 167 assertEquals(cookie, mCookieManager.getCookie(url)); |
| 168 |
| 169 assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { |
| 170 @Override |
| 171 public boolean isSatisfied() { |
| 172 return mCookieManager.hasCookies(); |
| 173 } |
| 174 })); |
| 175 |
| 176 // clean up all cookies |
| 177 mCookieManager.removeAllCookie(); |
| 178 assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { |
| 179 @Override |
| 180 public boolean isSatisfied() { |
| 181 return !mCookieManager.hasCookies(); |
| 182 } |
| 183 })); |
| 184 } |
| 185 |
| 186 @MediumTest |
| 187 @Feature({"Android-WebView", "Privacy"}) |
| 188 @SuppressWarnings("deprecation") |
| 189 public void testCookieExpiration() throws InterruptedException { |
| 190 // enable cookie |
| 191 mCookieManager.setAcceptCookie(true); |
| 192 assertTrue(mCookieManager.acceptCookie()); |
| 193 mCookieManager.removeAllCookie(); |
| 194 assertFalse(mCookieManager.hasCookies()); |
| 195 |
| 196 final String url = "http://www.example.com"; |
| 197 final String cookie1 = "cookie1=peter"; |
| 198 final String cookie2 = "cookie2=sue"; |
| 199 final String cookie3 = "cookie3=marc"; |
| 200 |
| 201 mCookieManager.setCookie(url, cookie1); // session cookie |
| 202 |
| 203 Date date = new Date(); |
| 204 date.setTime(date.getTime() + 1000 * 600); |
| 205 String value2 = cookie2 + "; expires=" + date.toGMTString(); |
| 206 mCookieManager.setCookie(url, value2); // expires in 10min |
| 207 |
| 208 long expiration = 3000; |
| 209 date = new Date(); |
| 210 date.setTime(date.getTime() + expiration); |
| 211 String value3 = cookie3 + "; expires=" + date.toGMTString(); |
| 212 mCookieManager.setCookie(url, value3); // expires in 3s |
| 213 |
| 214 String allCookies = mCookieManager.getCookie(url); |
| 215 assertTrue(allCookies.contains(cookie1)); |
| 216 assertTrue(allCookies.contains(cookie2)); |
| 217 assertTrue(allCookies.contains(cookie3)); |
| 218 |
| 219 mCookieManager.removeSessionCookie(); |
| 220 assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { |
| 221 @Override |
| 222 public boolean isSatisfied() { |
| 223 String c = mCookieManager.getCookie(url); |
| 224 return !c.contains(cookie1) && c.contains(cookie2) && c.contains
(cookie3); |
| 225 } |
| 226 })); |
| 227 |
| 228 Thread.sleep(expiration + 1000); // wait for cookie to expire |
| 229 mCookieManager.removeExpiredCookie(); |
| 230 assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { |
| 231 @Override |
| 232 public boolean isSatisfied() { |
| 233 String c = mCookieManager.getCookie(url); |
| 234 return !c.contains(cookie1) && c.contains(cookie2) && !c.contain
s(cookie3); |
| 235 } |
| 236 })); |
| 237 |
| 238 mCookieManager.removeAllCookie(); |
| 239 assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { |
| 240 @Override |
| 241 public boolean isSatisfied() { |
| 242 return mCookieManager.getCookie(url) == null; |
| 243 } |
| 244 })); |
| 245 } |
| 246 } |
| OLD | NEW |