| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.android_webview.test; | 5 package org.chromium.android_webview.test; |
| 6 | 6 |
| 7 import android.test.MoreAsserts; | 7 import android.test.MoreAsserts; |
| 8 import android.test.suitebuilder.annotation.MediumTest; | 8 import android.test.suitebuilder.annotation.MediumTest; |
| 9 import android.test.suitebuilder.annotation.SmallTest; | 9 import android.test.suitebuilder.annotation.SmallTest; |
| 10 import android.util.Pair; | 10 import android.util.Pair; |
| 11 | 11 |
| 12 import org.chromium.android_webview.AwContents; | 12 import org.chromium.android_webview.AwContents; |
| 13 import org.chromium.android_webview.AwCookieManager; | 13 import org.chromium.android_webview.AwCookieManager; |
| 14 import org.chromium.android_webview.test.util.JSUtils; | 14 import org.chromium.android_webview.test.util.JSUtils; |
| 15 import org.chromium.base.test.util.Feature; | 15 import org.chromium.base.test.util.Feature; |
| 16 import org.chromium.content.browser.test.util.Criteria; | |
| 17 import org.chromium.content.browser.test.util.CriteriaHelper; | |
| 18 import org.chromium.net.test.util.TestWebServer; | 16 import org.chromium.net.test.util.TestWebServer; |
| 19 | 17 |
| 20 import java.util.ArrayList; | 18 import java.util.ArrayList; |
| 21 import java.util.Arrays; | 19 import java.util.Arrays; |
| 22 import java.util.Date; | 20 import java.util.Date; |
| 23 import java.util.HashSet; | 21 import java.util.HashSet; |
| 24 import java.util.List; | 22 import java.util.List; |
| 25 import java.util.Set; | 23 import java.util.Set; |
| 24 import java.util.concurrent.Callable; |
| 26 | 25 |
| 27 /** | 26 /** |
| 28 * Tests for the CookieManager. | 27 * Tests for the CookieManager. |
| 29 */ | 28 */ |
| 30 public class CookieManagerTest extends AwTestBase { | 29 public class CookieManagerTest extends AwTestBase { |
| 31 | 30 |
| 32 private AwCookieManager mCookieManager; | 31 private AwCookieManager mCookieManager; |
| 33 private TestAwContentsClient mContentsClient; | 32 private TestAwContentsClient mContentsClient; |
| 34 private AwContents mAwContents; | 33 private AwContents mAwContents; |
| 35 | 34 |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 throws Throwable { | 114 throws Throwable { |
| 116 JSUtils.executeJavaScriptAndWaitForResult( | 115 JSUtils.executeJavaScriptAndWaitForResult( |
| 117 this, mAwContents, | 116 this, mAwContents, |
| 118 mContentsClient.getOnEvaluateJavaScriptResultHelper(), | 117 mContentsClient.getOnEvaluateJavaScriptResultHelper(), |
| 119 "var expirationDate = new Date();" + | 118 "var expirationDate = new Date();" + |
| 120 "expirationDate.setDate(expirationDate.getDate() + 5);" + | 119 "expirationDate.setDate(expirationDate.getDate() + 5);" + |
| 121 "document.cookie='" + name + "=" + value + | 120 "document.cookie='" + name + "=" + value + |
| 122 "; expires=' + expirationDate.toUTCString();"); | 121 "; expires=' + expirationDate.toUTCString();"); |
| 123 } | 122 } |
| 124 | 123 |
| 125 private void waitForCookie(final String url) throws InterruptedException { | 124 private void waitForCookie(final String url) throws Exception { |
| 126 assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { | 125 poll(new Callable<Boolean>() { |
| 127 @Override | 126 @Override |
| 128 public boolean isSatisfied() { | 127 public Boolean call() throws Exception { |
| 129 return mCookieManager.getCookie(url) != null; | 128 return mCookieManager.getCookie(url) != null; |
| 130 } | 129 } |
| 131 })); | 130 }); |
| 132 } | 131 } |
| 133 | 132 |
| 134 private void validateCookies(String responseCookie, String... expectedCookie
Names) { | 133 private void validateCookies(String responseCookie, String... expectedCookie
Names) { |
| 135 String[] cookies = responseCookie.split(";"); | 134 String[] cookies = responseCookie.split(";"); |
| 136 Set<String> foundCookieNames = new HashSet<String>(); | 135 Set<String> foundCookieNames = new HashSet<String>(); |
| 137 for (String cookie : cookies) { | 136 for (String cookie : cookies) { |
| 138 foundCookieNames.add(cookie.substring(0, cookie.indexOf("=")).trim()
); | 137 foundCookieNames.add(cookie.substring(0, cookie.indexOf("=")).trim()
); |
| 139 } | 138 } |
| 140 MoreAsserts.assertEquals( | 139 MoreAsserts.assertEquals( |
| 141 foundCookieNames, new HashSet<String>(Arrays.asList(expectedCook
ieNames))); | 140 foundCookieNames, new HashSet<String>(Arrays.asList(expectedCook
ieNames))); |
| 142 } | 141 } |
| 143 | 142 |
| 144 @MediumTest | 143 @MediumTest |
| 145 @Feature({"AndroidWebView", "Privacy"}) | 144 @Feature({"AndroidWebView", "Privacy"}) |
| 146 public void testRemoveAllCookie() throws InterruptedException { | 145 public void testRemoveAllCookie() throws Exception { |
| 147 // enable cookie | 146 // enable cookie |
| 148 mCookieManager.setAcceptCookie(true); | 147 mCookieManager.setAcceptCookie(true); |
| 149 assertTrue(mCookieManager.acceptCookie()); | 148 assertTrue(mCookieManager.acceptCookie()); |
| 150 | 149 |
| 151 // first there should be no cookie stored | 150 // first there should be no cookie stored |
| 152 mCookieManager.removeAllCookie(); | 151 mCookieManager.removeAllCookie(); |
| 153 mCookieManager.flushCookieStore(); | 152 mCookieManager.flushCookieStore(); |
| 154 assertFalse(mCookieManager.hasCookies()); | 153 assertFalse(mCookieManager.hasCookies()); |
| 155 | 154 |
| 156 String url = "http://www.example.com"; | 155 String url = "http://www.example.com"; |
| 157 String cookie = "name=test"; | 156 String cookie = "name=test"; |
| 158 mCookieManager.setCookie(url, cookie); | 157 mCookieManager.setCookie(url, cookie); |
| 159 assertEquals(cookie, mCookieManager.getCookie(url)); | 158 assertEquals(cookie, mCookieManager.getCookie(url)); |
| 160 | 159 |
| 161 assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { | 160 poll(new Callable<Boolean>() { |
| 162 @Override | 161 @Override |
| 163 public boolean isSatisfied() { | 162 public Boolean call() throws Exception { |
| 164 return mCookieManager.hasCookies(); | 163 return mCookieManager.hasCookies(); |
| 165 } | 164 } |
| 166 })); | 165 }); |
| 167 | 166 |
| 168 // clean up all cookies | 167 // clean up all cookies |
| 169 mCookieManager.removeAllCookie(); | 168 mCookieManager.removeAllCookie(); |
| 170 assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { | 169 poll(new Callable<Boolean>() { |
| 171 @Override | 170 @Override |
| 172 public boolean isSatisfied() { | 171 public Boolean call() throws Exception { |
| 173 return !mCookieManager.hasCookies(); | 172 return !mCookieManager.hasCookies(); |
| 174 } | 173 } |
| 175 })); | 174 }); |
| 176 } | 175 } |
| 177 | 176 |
| 178 @MediumTest | 177 @MediumTest |
| 179 @Feature({"AndroidWebView", "Privacy"}) | 178 @Feature({"AndroidWebView", "Privacy"}) |
| 180 @SuppressWarnings("deprecation") | 179 @SuppressWarnings("deprecation") |
| 181 public void testCookieExpiration() throws InterruptedException { | 180 public void testCookieExpiration() throws Exception { |
| 182 // enable cookie | 181 // enable cookie |
| 183 mCookieManager.setAcceptCookie(true); | 182 mCookieManager.setAcceptCookie(true); |
| 184 assertTrue(mCookieManager.acceptCookie()); | 183 assertTrue(mCookieManager.acceptCookie()); |
| 185 mCookieManager.removeAllCookie(); | 184 mCookieManager.removeAllCookie(); |
| 186 assertFalse(mCookieManager.hasCookies()); | 185 assertFalse(mCookieManager.hasCookies()); |
| 187 | 186 |
| 188 final String url = "http://www.example.com"; | 187 final String url = "http://www.example.com"; |
| 189 final String cookie1 = "cookie1=peter"; | 188 final String cookie1 = "cookie1=peter"; |
| 190 final String cookie2 = "cookie2=sue"; | 189 final String cookie2 = "cookie2=sue"; |
| 191 final String cookie3 = "cookie3=marc"; | 190 final String cookie3 = "cookie3=marc"; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 202 date.setTime(date.getTime() + expiration); | 201 date.setTime(date.getTime() + expiration); |
| 203 String value3 = cookie3 + "; expires=" + date.toGMTString(); | 202 String value3 = cookie3 + "; expires=" + date.toGMTString(); |
| 204 mCookieManager.setCookie(url, value3); // expires in 3s | 203 mCookieManager.setCookie(url, value3); // expires in 3s |
| 205 | 204 |
| 206 String allCookies = mCookieManager.getCookie(url); | 205 String allCookies = mCookieManager.getCookie(url); |
| 207 assertTrue(allCookies.contains(cookie1)); | 206 assertTrue(allCookies.contains(cookie1)); |
| 208 assertTrue(allCookies.contains(cookie2)); | 207 assertTrue(allCookies.contains(cookie2)); |
| 209 assertTrue(allCookies.contains(cookie3)); | 208 assertTrue(allCookies.contains(cookie3)); |
| 210 | 209 |
| 211 mCookieManager.removeSessionCookie(); | 210 mCookieManager.removeSessionCookie(); |
| 212 assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { | 211 poll(new Callable<Boolean>() { |
| 213 @Override | 212 @Override |
| 214 public boolean isSatisfied() { | 213 public Boolean call() throws Exception { |
| 215 String c = mCookieManager.getCookie(url); | 214 String c = mCookieManager.getCookie(url); |
| 216 return !c.contains(cookie1) && c.contains(cookie2) && c.contains
(cookie3); | 215 return !c.contains(cookie1) && c.contains(cookie2) && c.contains
(cookie3); |
| 217 } | 216 } |
| 218 })); | 217 }); |
| 219 | 218 |
| 220 Thread.sleep(expiration + 1000); // wait for cookie to expire | 219 Thread.sleep(expiration + 1000); // wait for cookie to expire |
| 221 mCookieManager.removeExpiredCookie(); | 220 mCookieManager.removeExpiredCookie(); |
| 222 assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { | 221 poll(new Callable<Boolean>() { |
| 223 @Override | 222 @Override |
| 224 public boolean isSatisfied() { | 223 public Boolean call() throws Exception { |
| 225 String c = mCookieManager.getCookie(url); | 224 String c = mCookieManager.getCookie(url); |
| 226 return !c.contains(cookie1) && c.contains(cookie2) && !c.contain
s(cookie3); | 225 return !c.contains(cookie1) && c.contains(cookie2) && !c.contain
s(cookie3); |
| 227 } | 226 } |
| 228 })); | 227 }); |
| 229 | 228 |
| 230 mCookieManager.removeAllCookie(); | 229 mCookieManager.removeAllCookie(); |
| 231 assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { | 230 poll(new Callable<Boolean>() { |
| 232 @Override | 231 @Override |
| 233 public boolean isSatisfied() { | 232 public Boolean call() throws Exception { |
| 234 return mCookieManager.getCookie(url) == null; | 233 return mCookieManager.getCookie(url) == null; |
| 235 } | 234 } |
| 236 })); | 235 }); |
| 237 } | 236 } |
| 238 } | 237 } |
| OLD | NEW |