1 /* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.webkit.cts; 18 19 import dalvik.annotation.TestLevel; 20 import dalvik.annotation.TestTargetClass; 21 import dalvik.annotation.TestTargetNew; 22 import dalvik.annotation.TestTargets; 23 import dalvik.annotation.ToBeFixed; 24 25 import android.test.ActivityInstrumentationTestCase2; 26 import android.view.animation.cts.DelayedCheck; 27 import android.webkit.CookieManager; 28 import android.webkit.CookieSyncManager; 29 import android.webkit.WebChromeClient; 30 import android.webkit.WebView; 31 32 import java.util.Date; 33 import java.util.regex.Matcher; 34 import java.util.regex.Pattern; 35 36 @TestTargetClass(android.webkit.CookieManager.class) 37 public class CookieManagerTest extends 38 ActivityInstrumentationTestCase2<CookieSyncManagerStubActivity> { 39 40 private static final int TEST_DELAY = 5000; 41 42 private WebView mWebView; 43 private CookieManager mCookieManager; 44 45 public CookieManagerTest() { 46 super("com.android.cts.stub", CookieSyncManagerStubActivity.class); 47 } 48 49 @Override 50 protected void setUp() throws Exception { 51 super.setUp(); 52 mWebView = getActivity().getWebView(); 53 54 // Set a web chrome client in order to receive progress updates. 55 mWebView.setWebChromeClient(new WebChromeClient()); 56 57 mCookieManager = CookieManager.getInstance(); 58 assertNotNull(mCookieManager); 59 } 60 61 @TestTargetNew( 62 level = TestLevel.COMPLETE, 63 method = "getInstance", 64 args = {} 65 ) 66 public void testGetInstance() { 67 CookieManager c1 = CookieManager.getInstance(); 68 CookieManager c2 = CookieManager.getInstance(); 69 70 assertSame(c1, c2); 71 } 72 73 @TestTargetNew( 74 level = TestLevel.NOT_FEASIBLE, 75 method = "clone", 76 notes = "clone() is protected and CookieManager cannot be subclassed", 77 args = {} 78 ) 79 public void testClone() { 80 } 81 82 @TestTargets({ 83 @TestTargetNew( 84 level = TestLevel.COMPLETE, 85 method = "setAcceptCookie", 86 args = {boolean.class} 87 ), 88 @TestTargetNew( 89 level = TestLevel.COMPLETE, 90 method = "acceptCookie", 91 args = {} 92 ), 93 @TestTargetNew( 94 level = TestLevel.COMPLETE, 95 method = "setCookie", 96 args = {String.class, String.class} 97 ), 98 @TestTargetNew( 99 level = TestLevel.COMPLETE, 100 method = "getCookie", 101 args = {String.class} 102 ), 103 @TestTargetNew( 104 level = TestLevel.COMPLETE, 105 method = "removeAllCookie", 106 args = {} 107 ) 108 }) 109 public void testAcceptCookie() throws Exception { 110 mCookieManager.removeAllCookie(); 111 mCookieManager.setAcceptCookie(false); 112 assertFalse(mCookieManager.acceptCookie()); 113 assertFalse(mCookieManager.hasCookies()); 114 115 CtsTestServer server = new CtsTestServer(getActivity(), false); 116 String url = server.getCookieUrl("conquest.html"); 117 loadUrl(url); 118 assertEquals(null, mWebView.getTitle()); // no cookies passed 119 Thread.sleep(TEST_DELAY); 120 assertNull(mCookieManager.getCookie(url)); 121 122 mCookieManager.setAcceptCookie(true); 123 assertTrue(mCookieManager.acceptCookie()); 124 125 url = server.getCookieUrl("war.html"); 126 loadUrl(url); 127 assertEquals(null, mWebView.getTitle()); // no cookies passed 128 waitForCookie(url); 129 String cookie = mCookieManager.getCookie(url); 130 assertNotNull(cookie); 131 // 'count' value of the returned cookie is 0 132 final Pattern pat = Pattern.compile("count=(\\d+)"); 133 Matcher m = pat.matcher(cookie); 134 assertTrue(m.matches()); 135 assertEquals("0", m.group(1)); 136 137 url = server.getCookieUrl("famine.html"); 138 loadUrl(url); 139 assertEquals("count=0", mWebView.getTitle()); // outgoing cookie 140 waitForCookie(url); 141 cookie = mCookieManager.getCookie(url); 142 assertNotNull(cookie); 143 m = pat.matcher(cookie); 144 assertTrue(m.matches()); 145 assertEquals("1", m.group(1)); // value got incremented 146 147 url = server.getCookieUrl("death.html"); 148 mCookieManager.setCookie(url, "count=41"); 149 loadUrl(url); 150 assertEquals("count=41", mWebView.getTitle()); // outgoing cookie 151 waitForCookie(url); 152 cookie = mCookieManager.getCookie(url); 153 assertNotNull(cookie); 154 m = pat.matcher(cookie); 155 assertTrue(m.matches()); 156 assertEquals("42", m.group(1)); // value got incremented 157 158 // clean up all cookies 159 mCookieManager.removeAllCookie(); 160 } 161 162 @TestTargets({ 163 @TestTargetNew( 164 level = TestLevel.COMPLETE, 165 method = "hasCookies", 166 args = {} 167 ), 168 @TestTargetNew( 169 level = TestLevel.COMPLETE, 170 method = "removeAllCookie", 171 args = {} 172 ) 173 }) 174 @ToBeFixed(explanation = "CookieManager.hasCookies() should also count cookies in RAM cache") 175 public void testCookieManager() { 176 // enable cookie 177 mCookieManager.setAcceptCookie(true); 178 assertTrue(mCookieManager.acceptCookie()); 179 180 // first there should be no cookie stored 181 assertFalse(mCookieManager.hasCookies()); 182 183 String url = "http://www.example.com"; 184 String cookie = "name=test"; 185 mCookieManager.setCookie(url, cookie); 186 assertEquals(cookie, mCookieManager.getCookie(url)); 187 188 // sync cookie from RAM to FLASH, because hasCookies() only counts FLASH cookies 189 CookieSyncManager.getInstance().sync(); 190 new DelayedCheck(TEST_DELAY) { 191 @Override 192 protected boolean check() { 193 return mCookieManager.hasCookies(); 194 } 195 }.run(); 196 197 // clean up all cookies 198 mCookieManager.removeAllCookie(); 199 new DelayedCheck(TEST_DELAY) { 200 @Override 201 protected boolean check() { 202 return !mCookieManager.hasCookies(); 203 } 204 }.run(); 205 } 206 207 @TestTargets({ 208 @TestTargetNew( 209 level = TestLevel.COMPLETE, 210 method = "removeSessionCookie", 211 args = {} 212 ), 213 @TestTargetNew( 214 level = TestLevel.COMPLETE, 215 method = "removeExpiredCookie", 216 args = {} 217 ) 218 }) 219 @SuppressWarnings("deprecation") 220 public void testRemoveCookies() throws InterruptedException { 221 // enable cookie 222 mCookieManager.setAcceptCookie(true); 223 assertTrue(mCookieManager.acceptCookie()); 224 assertFalse(mCookieManager.hasCookies()); 225 226 final String url = "http://www.example.com"; 227 final String cookie1 = "cookie1=peter"; 228 final String cookie2 = "cookie2=sue"; 229 final String cookie3 = "cookie3=marc"; 230 231 mCookieManager.setCookie(url, cookie1); // session cookie 232 233 Date date = new Date(); 234 date.setTime(date.getTime() + 1000 * 600); 235 String value2 = cookie2 + "; expires=" + date.toGMTString(); 236 mCookieManager.setCookie(url, value2); // expires in 10min 237 238 long expiration = 3000; 239 date = new Date(); 240 date.setTime(date.getTime() + expiration); 241 String value3 = cookie3 + "; expires=" + date.toGMTString(); 242 mCookieManager.setCookie(url, value3); // expires in 3s 243 244 String allCookies = mCookieManager.getCookie(url); 245 assertTrue(allCookies.contains(cookie1)); 246 assertTrue(allCookies.contains(cookie2)); 247 assertTrue(allCookies.contains(cookie3)); 248 249 mCookieManager.removeSessionCookie(); 250 new DelayedCheck(TEST_DELAY) { 251 protected boolean check() { 252 String c = mCookieManager.getCookie(url); 253 return !c.contains(cookie1) && c.contains(cookie2) && c.contains(cookie3); 254 } 255 }.run(); 256 257 Thread.sleep(expiration + 1000); // wait for cookie to expire 258 mCookieManager.removeExpiredCookie(); 259 new DelayedCheck(TEST_DELAY) { 260 protected boolean check() { 261 String c = mCookieManager.getCookie(url); 262 return !c.contains(cookie1) && c.contains(cookie2) && !c.contains(cookie3); 263 } 264 }.run(); 265 266 mCookieManager.removeAllCookie(); 267 new DelayedCheck(TEST_DELAY) { 268 protected boolean check() { 269 return mCookieManager.getCookie(url) == null; 270 } 271 }.run(); 272 } 273 274 private void loadUrl(String url) { 275 mWebView.loadUrl(url); 276 new DelayedCheck(TEST_DELAY) { 277 protected boolean check() { 278 return mWebView.getProgress() == 100; 279 } 280 }.run(); 281 } 282 283 private void waitForCookie(final String url) { 284 new DelayedCheck(TEST_DELAY) { 285 protected boolean check() { 286 return mCookieManager.getCookie(url) != null; 287 } 288 }.run(); 289 } 290 291 public void testb3167208() throws Exception { 292 String uri = "http://host.android.com/path/"; 293 // note the space after the domain= 294 String problemCookie = "foo=bar; domain= .android.com; path=/"; 295 mCookieManager.setCookie(uri, problemCookie); 296 String cookie = mCookieManager.getCookie(uri); 297 assertNotNull(cookie); 298 assertTrue(cookie.contains("foo=bar")); 299 } 300 } 301