Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / android_webview / javatests / src / org / chromium / android_webview / test / CookieManagerTest.java
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
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.util.Pair;
10 import android.webkit.ValueCallback;
11
12 import static org.chromium.android_webview.test.util.CookieUtils.TestValueCallback;
13
14 import org.chromium.android_webview.AwContents;
15 import org.chromium.android_webview.AwCookieManager;
16 import org.chromium.android_webview.AwSettings;
17 import org.chromium.android_webview.test.util.CookieUtils;
18 import org.chromium.android_webview.test.util.JSUtils;
19 import org.chromium.base.test.util.Feature;
20 import org.chromium.net.test.util.TestWebServer;
21
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.Date;
25 import java.util.HashSet;
26 import java.util.List;
27 import java.util.Set;
28 import java.util.concurrent.Callable;
29
30 /**
31  * Tests for the CookieManager.
32  */
33 public class CookieManagerTest extends AwTestBase {
34
35     private AwCookieManager mCookieManager;
36     private TestAwContentsClient mContentsClient;
37     private AwContents mAwContents;
38
39     @Override
40     protected void setUp() throws Exception {
41         super.setUp();
42
43         mCookieManager = new AwCookieManager();
44         mContentsClient = new TestAwContentsClient();
45         final AwTestContainerView testContainerView =
46                 createAwTestContainerViewOnMainSync(mContentsClient);
47         mAwContents = testContainerView.getAwContents();
48         mAwContents.getSettings().setJavaScriptEnabled(true);
49         assertNotNull(mCookieManager);
50
51         mCookieManager.setAcceptCookie(true);
52         assertTrue(mCookieManager.acceptCookie());
53     }
54
55     @Override
56     protected void tearDown() throws Exception {
57         try {
58             clearCookies();
59         } catch (Throwable e) {
60             throw new RuntimeException("Could not clear cookies.");
61         }
62         super.tearDown();
63     }
64
65     @MediumTest
66     @Feature({"AndroidWebView", "Privacy"})
67     public void testAcceptCookie() throws Throwable {
68         TestWebServer webServer = TestWebServer.start();
69         try {
70             String path = "/cookie_test.html";
71             String responseStr =
72                     "<html><head><title>TEST!</title></head><body>HELLO!</body></html>";
73             String url = webServer.setResponse(path, responseStr, null);
74
75             mCookieManager.setAcceptCookie(false);
76             assertFalse(mCookieManager.acceptCookie());
77
78             loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url);
79             setCookieWithJavaScript("test1", "value1");
80             assertNull(mCookieManager.getCookie(url));
81
82             List<Pair<String, String>> responseHeaders = new ArrayList<Pair<String, String>>();
83             responseHeaders.add(
84                     Pair.create("Set-Cookie", "header-test1=header-value1; path=" + path));
85             url = webServer.setResponse(path, responseStr, responseHeaders);
86             loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url);
87             assertNull(mCookieManager.getCookie(url));
88
89             mCookieManager.setAcceptCookie(true);
90             assertTrue(mCookieManager.acceptCookie());
91
92             url = webServer.setResponse(path, responseStr, null);
93             loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url);
94             setCookieWithJavaScript("test2", "value2");
95             waitForCookie(url);
96             String cookie = mCookieManager.getCookie(url);
97             assertNotNull(cookie);
98             validateCookies(cookie, "test2");
99
100             responseHeaders = new ArrayList<Pair<String, String>>();
101             responseHeaders.add(
102                     Pair.create("Set-Cookie", "header-test2=header-value2 path=" + path));
103             url = webServer.setResponse(path, responseStr, responseHeaders);
104             loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url);
105             waitForCookie(url);
106             cookie = mCookieManager.getCookie(url);
107             assertNotNull(cookie);
108             validateCookies(cookie, "test2", "header-test2");
109         } finally {
110             webServer.shutdown();
111         }
112     }
113
114     private void setCookieWithJavaScript(final String name, final String value)
115             throws Throwable {
116         JSUtils.executeJavaScriptAndWaitForResult(
117                 this, mAwContents,
118                 mContentsClient.getOnEvaluateJavaScriptResultHelper(),
119                 "var expirationDate = new Date();"
120                 + "expirationDate.setDate(expirationDate.getDate() + 5);"
121                 + "document.cookie='" + name + "=" + value
122                 + "; expires=' + expirationDate.toUTCString();");
123     }
124
125     @MediumTest
126     @Feature({"AndroidWebView", "Privacy"})
127     public void testRemoveAllCookies() throws Exception {
128         mCookieManager.setCookie("http://www.example.com", "name=test");
129         assertTrue(mCookieManager.hasCookies());
130         mCookieManager.removeAllCookies();
131         assertFalse(mCookieManager.hasCookies());
132     }
133
134     @MediumTest
135     @Feature({"AndroidWebView", "Privacy"})
136     public void testRemoveSessionCookies() throws Exception {
137         final String url = "http://www.example.com";
138         final String sessionCookie = "cookie1=peter";
139         final String normalCookie = "cookie2=sue";
140
141         mCookieManager.setCookie(url, sessionCookie);
142         mCookieManager.setCookie(url, makeExpiringCookie(normalCookie, 600));
143
144         mCookieManager.removeSessionCookies();
145
146         String allCookies = mCookieManager.getCookie(url);
147         assertFalse(allCookies.contains(sessionCookie));
148         assertTrue(allCookies.contains(normalCookie));
149     }
150
151     @MediumTest
152     @Feature({"AndroidWebView", "Privacy"})
153     public void testSetCookie() throws Throwable {
154         String url = "http://www.example.com";
155         String cookie = "name=test";
156         mCookieManager.setCookie(url, cookie);
157         assertEquals(cookie, mCookieManager.getCookie(url));
158     }
159
160     @MediumTest
161     @Feature({"AndroidWebView", "Privacy"})
162     public void testHasCookie() throws Throwable {
163         assertFalse(mCookieManager.hasCookies());
164         mCookieManager.setCookie("http://www.example.com", "name=test");
165         assertTrue(mCookieManager.hasCookies());
166     }
167
168     @MediumTest
169     @Feature({"AndroidWebView", "Privacy"})
170     public void testSetCookieCallback() throws Throwable {
171         final String url = "http://www.example.com";
172         final String cookie = "name=test";
173         final String brokenUrl = "foo";
174
175         final TestValueCallback<Boolean> callback = new TestValueCallback<Boolean>();
176         int callCount = callback.getOnReceiveValueHelper().getCallCount();
177
178         setCookieOnUiThread(url, cookie, callback);
179         callback.getOnReceiveValueHelper().waitForCallback(callCount);
180         assertTrue(callback.getValue());
181         assertEquals(cookie, mCookieManager.getCookie(url));
182
183         callCount = callback.getOnReceiveValueHelper().getCallCount();
184
185         setCookieOnUiThread(brokenUrl, cookie, callback);
186         callback.getOnReceiveValueHelper().waitForCallback(callCount);
187         assertFalse(callback.getValue());
188         assertEquals(null, mCookieManager.getCookie(brokenUrl));
189     }
190
191     @MediumTest
192     @Feature({"AndroidWebView", "Privacy"})
193     public void testSetCookieNullCallback() throws Throwable {
194         mCookieManager.setAcceptCookie(true);
195         assertTrue(mCookieManager.acceptCookie());
196
197         final String url = "http://www.example.com";
198         final String cookie = "name=test";
199
200         mCookieManager.setCookie(url, cookie, null);
201
202         poll(new Callable<Boolean>() {
203             @Override
204             public Boolean call() throws Exception {
205                 return mCookieManager.hasCookies();
206             }
207         });
208     }
209
210     @MediumTest
211     @Feature({"AndroidWebView", "Privacy"})
212     public void testRemoveAllCookiesCallback() throws Throwable {
213         TestValueCallback<Boolean> callback = new TestValueCallback<Boolean>();
214         int callCount = callback.getOnReceiveValueHelper().getCallCount();
215
216         mCookieManager.setCookie("http://www.example.com", "name=test");
217
218         // When we remove all cookies the first time some cookies are removed.
219         removeAllCookiesOnUiThread(callback);
220         callback.getOnReceiveValueHelper().waitForCallback(callCount);
221         assertTrue(callback.getValue());
222         assertFalse(mCookieManager.hasCookies());
223
224         callCount = callback.getOnReceiveValueHelper().getCallCount();
225
226         // The second time none are removed.
227         removeAllCookiesOnUiThread(callback);
228         callback.getOnReceiveValueHelper().waitForCallback(callCount);
229         assertFalse(callback.getValue());
230     }
231
232     @MediumTest
233     @Feature({"AndroidWebView", "Privacy"})
234     public void testRemoveAllCookiesNullCallback() throws Throwable {
235         mCookieManager.setCookie("http://www.example.com", "name=test");
236
237         mCookieManager.removeAllCookies(null);
238
239         // Eventually the cookies are removed.
240         poll(new Callable<Boolean>() {
241             @Override
242             public Boolean call() throws Exception {
243                 return !mCookieManager.hasCookies();
244             }
245         });
246     }
247
248     @MediumTest
249     @Feature({"AndroidWebView", "Privacy"})
250     public void testRemoveSessionCookiesCallback() throws Throwable {
251         final String url = "http://www.example.com";
252         final String sessionCookie = "cookie1=peter";
253         final String normalCookie = "cookie2=sue";
254
255         TestValueCallback<Boolean> callback = new TestValueCallback<Boolean>();
256         int callCount = callback.getOnReceiveValueHelper().getCallCount();
257
258         mCookieManager.setCookie(url, sessionCookie);
259         mCookieManager.setCookie(url, makeExpiringCookie(normalCookie, 600));
260
261         // When there is a session cookie then it is removed.
262         removeSessionCookiesOnUiThread(callback);
263         callback.getOnReceiveValueHelper().waitForCallback(callCount);
264         assertTrue(callback.getValue());
265         String allCookies = mCookieManager.getCookie(url);
266         assertTrue(!allCookies.contains(sessionCookie));
267         assertTrue(allCookies.contains(normalCookie));
268
269         callCount = callback.getOnReceiveValueHelper().getCallCount();
270
271         // If there are no session cookies then none are removed.
272         removeSessionCookiesOnUiThread(callback);
273         callback.getOnReceiveValueHelper().waitForCallback(callCount);
274         assertFalse(callback.getValue());
275     }
276
277     @MediumTest
278     @Feature({"AndroidWebView", "Privacy"})
279     public void testRemoveSessionCookiesNullCallback() throws Throwable {
280         final String url = "http://www.example.com";
281         final String sessionCookie = "cookie1=peter";
282         final String normalCookie = "cookie2=sue";
283
284         mCookieManager.setCookie(url, sessionCookie);
285         mCookieManager.setCookie(url, makeExpiringCookie(normalCookie, 600));
286         String allCookies = mCookieManager.getCookie(url);
287         assertTrue(allCookies.contains(sessionCookie));
288         assertTrue(allCookies.contains(normalCookie));
289
290         mCookieManager.removeSessionCookies(null);
291
292         // Eventually the session cookie is removed.
293         poll(new Callable<Boolean>() {
294             @Override
295             public Boolean call() throws Exception {
296                 String c = mCookieManager.getCookie(url);
297                 return !c.contains(sessionCookie) && c.contains(normalCookie);
298             }
299         });
300     }
301
302     @MediumTest
303     @Feature({"AndroidWebView", "Privacy"})
304     public void testExpiredCookiesAreNotSet() throws Exception {
305         final String url = "http://www.example.com";
306         final String cookie = "cookie1=peter";
307
308         mCookieManager.setCookie(url, makeExpiringCookie(cookie, -1));
309         assertNull(mCookieManager.getCookie(url));
310     }
311
312     @MediumTest
313     @Feature({"AndroidWebView", "Privacy"})
314     public void testCookiesExpire() throws Exception {
315         final String url = "http://www.example.com";
316         final String cookie = "cookie1=peter";
317
318         mCookieManager.setCookie(url, makeExpiringCookieMs(cookie, 1200));
319
320         // The cookie exists:
321         assertTrue(mCookieManager.hasCookies());
322
323         // But eventually expires:
324         poll(new Callable<Boolean>() {
325             @Override
326             public Boolean call() throws Exception {
327                 return !mCookieManager.hasCookies();
328             }
329         });
330     }
331
332     @MediumTest
333     @Feature({"AndroidWebView", "Privacy"})
334     public void testCookieExpiration() throws Exception {
335         final String url = "http://www.example.com";
336         final String sessionCookie = "cookie1=peter";
337         final String longCookie = "cookie2=marc";
338
339         mCookieManager.setCookie(url, sessionCookie);
340         mCookieManager.setCookie(url, makeExpiringCookie(longCookie, 600));
341
342         String allCookies = mCookieManager.getCookie(url);
343         assertTrue(allCookies.contains(sessionCookie));
344         assertTrue(allCookies.contains(longCookie));
345
346         // Removing expired cookies doesn't have an observable effect but since people will still
347         // be calling it for a while it shouldn't break anything either.
348         mCookieManager.removeExpiredCookies();
349
350         allCookies = mCookieManager.getCookie(url);
351         assertTrue(allCookies.contains(sessionCookie));
352         assertTrue(allCookies.contains(longCookie));
353     }
354
355     @MediumTest
356     @Feature({"AndroidWebView", "Privacy"})
357     public void testThirdPartyCookie() throws Throwable {
358         // In theory we need two servers to test this, one server ('the first party') which returns
359         // a response with a link to a second server ('the third party') at different origin. This
360         // second server attempts to set a cookie which should fail if AcceptThirdPartyCookie() is
361         // false.
362         // Strictly according to the letter of RFC6454 it should be possible to set this situation
363         // up with two TestServers on different ports (these count as having different origins) but
364         // Chrome is not strict about this and does not check the port. Instead we cheat making some
365         // of the urls come from localhost and some from 127.0.0.1 which count (both in theory and
366         // pratice) as having different origins.
367         TestWebServer webServer = TestWebServer.start();
368         try {
369             // Turn global allow on.
370             mCookieManager.setAcceptCookie(true);
371             assertTrue(mCookieManager.acceptCookie());
372
373             // When third party cookies are disabled...
374             mAwContents.getSettings().setAcceptThirdPartyCookies(false);
375             assertFalse(mAwContents.getSettings().getAcceptThirdPartyCookies());
376
377             // ...we can't set third party cookies.
378             // First on the third party server we create a url which tries to set a cookie.
379             String cookieUrl = toThirdPartyUrl(
380                     makeCookieUrl(webServer, "/cookie_1.js", "test1", "value1"));
381             // Then we create a url on the first party server which links to the first url.
382             String url = makeScriptLinkUrl(webServer, "/content_1.html", cookieUrl);
383             loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url);
384             assertNull(mCookieManager.getCookie(cookieUrl));
385
386             // When third party cookies are enabled...
387             mAwContents.getSettings().setAcceptThirdPartyCookies(true);
388             assertTrue(mAwContents.getSettings().getAcceptThirdPartyCookies());
389
390             // ...we can set third party cookies.
391             cookieUrl = toThirdPartyUrl(
392                     makeCookieUrl(webServer, "/cookie_2.js", "test2", "value2"));
393             url = makeScriptLinkUrl(webServer, "/content_2.html", cookieUrl);
394             loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url);
395             waitForCookie(cookieUrl);
396             String cookie = mCookieManager.getCookie(cookieUrl);
397             assertNotNull(cookie);
398             validateCookies(cookie, "test2");
399         } finally {
400             webServer.shutdown();
401         }
402     }
403
404     /**
405      * Creates a response on the TestWebServer which attempts to set a cookie when fetched.
406      * @param  webServer  the webServer on which to create the response
407      * @param  path the path component of the url (e.g "/cookie_test.html")
408      * @param  key the key of the cookie
409      * @param  value the value of the cookie
410      * @return  the url which gets the response
411      */
412     private String makeCookieUrl(TestWebServer webServer, String path, String key, String value) {
413         String response = "";
414         List<Pair<String, String>> responseHeaders = new ArrayList<Pair<String, String>>();
415         responseHeaders.add(
416                 Pair.create("Set-Cookie", key + "=" + value + "; path=" + path));
417         return webServer.setResponse(path, response, responseHeaders);
418     }
419
420     /**
421      * Creates a response on the TestWebServer which contains a script tag with an external src.
422      * @param  webServer  the webServer on which to create the response
423      * @param  path the path component of the url (e.g "/my_thing_with_script.html")
424      * @param  url the url which which should appear as the src of the script tag.
425      * @return  the url which gets the response
426      */
427     private String makeScriptLinkUrl(TestWebServer webServer, String path, String url) {
428         String responseStr = "<html><head><title>Content!</title></head>"
429                 + "<body><script src=" + url + "></script></body></html>";
430         return webServer.setResponse(path, responseStr, null);
431     }
432
433     @MediumTest
434     @Feature({"AndroidWebView", "Privacy"})
435     public void testThirdPartyJavascriptCookie() throws Throwable {
436         TestWebServer webServer = TestWebServer.start();
437         try {
438             // This test again uses 127.0.0.1/localhost trick to simulate a third party.
439             ThirdPartyCookiesTestHelper thirdParty =
440                     new ThirdPartyCookiesTestHelper(webServer);
441
442             mCookieManager.setAcceptCookie(true);
443             assertTrue(mCookieManager.acceptCookie());
444
445             // When third party cookies are disabled...
446             thirdParty.getSettings().setAcceptThirdPartyCookies(false);
447             assertFalse(thirdParty.getSettings().getAcceptThirdPartyCookies());
448
449             // ...we can't set third party cookies.
450             thirdParty.assertThirdPartyIFrameCookieResult("1", false);
451
452             // When third party cookies are enabled...
453             thirdParty.getSettings().setAcceptThirdPartyCookies(true);
454             assertTrue(thirdParty.getSettings().getAcceptThirdPartyCookies());
455
456             // ...we can set third party cookies.
457             thirdParty.assertThirdPartyIFrameCookieResult("2", true);
458         } finally {
459             webServer.shutdown();
460         }
461     }
462
463     @MediumTest
464     @Feature({"AndroidWebView", "Privacy"})
465     public void testThirdPartyCookiesArePerWebview() throws Throwable {
466         TestWebServer webServer = TestWebServer.start();
467         try {
468             mCookieManager.setAcceptCookie(true);
469             mCookieManager.removeAllCookie();
470             assertTrue(mCookieManager.acceptCookie());
471             assertFalse(mCookieManager.hasCookies());
472
473             ThirdPartyCookiesTestHelper helperOne = new ThirdPartyCookiesTestHelper(webServer);
474             ThirdPartyCookiesTestHelper helperTwo = new ThirdPartyCookiesTestHelper(webServer);
475
476             helperOne.getSettings().setAcceptThirdPartyCookies(false);
477             helperTwo.getSettings().setAcceptThirdPartyCookies(false);
478             assertFalse(helperOne.getSettings().getAcceptThirdPartyCookies());
479             assertFalse(helperTwo.getSettings().getAcceptThirdPartyCookies());
480             helperOne.assertThirdPartyIFrameCookieResult("1", false);
481             helperTwo.assertThirdPartyIFrameCookieResult("2", false);
482
483             helperTwo.getSettings().setAcceptThirdPartyCookies(true);
484             assertFalse(helperOne.getSettings().getAcceptThirdPartyCookies());
485             assertTrue(helperTwo.getSettings().getAcceptThirdPartyCookies());
486             helperOne.assertThirdPartyIFrameCookieResult("3", false);
487             helperTwo.assertThirdPartyIFrameCookieResult("4", true);
488
489             helperOne.getSettings().setAcceptThirdPartyCookies(true);
490             assertTrue(helperOne.getSettings().getAcceptThirdPartyCookies());
491             assertTrue(helperTwo.getSettings().getAcceptThirdPartyCookies());
492             helperOne.assertThirdPartyIFrameCookieResult("5", true);
493             helperTwo.assertThirdPartyIFrameCookieResult("6", true);
494
495             helperTwo.getSettings().setAcceptThirdPartyCookies(false);
496             assertTrue(helperOne.getSettings().getAcceptThirdPartyCookies());
497             assertFalse(helperTwo.getSettings().getAcceptThirdPartyCookies());
498             helperOne.assertThirdPartyIFrameCookieResult("7", true);
499             helperTwo.assertThirdPartyIFrameCookieResult("8", false);
500         } finally {
501             webServer.shutdown();
502         }
503     }
504
505     @MediumTest
506     @Feature({"AndroidWebView", "Privacy"})
507     public void testAcceptFileSchemeCookies() throws Throwable {
508         mCookieManager.setAcceptFileSchemeCookies(true);
509         mAwContents.getSettings().setAllowFileAccess(true);
510
511         mAwContents.getSettings().setAcceptThirdPartyCookies(true);
512         assertTrue(fileURLCanSetCookie("1"));
513         mAwContents.getSettings().setAcceptThirdPartyCookies(false);
514         assertTrue(fileURLCanSetCookie("2"));
515     }
516
517     @MediumTest
518     @Feature({"AndroidWebView", "Privacy"})
519     public void testRejectFileSchemeCookies() throws Throwable {
520         mCookieManager.setAcceptFileSchemeCookies(false);
521         mAwContents.getSettings().setAllowFileAccess(true);
522
523         mAwContents.getSettings().setAcceptThirdPartyCookies(true);
524         assertFalse(fileURLCanSetCookie("3"));
525         mAwContents.getSettings().setAcceptThirdPartyCookies(false);
526         assertFalse(fileURLCanSetCookie("4"));
527     }
528
529     private boolean fileURLCanSetCookie(String suffix) throws Throwable {
530         String value = "value" + suffix;
531         String url = "file:///android_asset/cookie_test.html?value=" + value;
532         loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url);
533         String cookie = mCookieManager.getCookie(url);
534         return cookie != null && cookie.contains("test=" + value);
535     }
536
537     class ThirdPartyCookiesTestHelper {
538         protected final AwContents mAwContents;
539         protected final TestAwContentsClient mContentsClient;
540         protected final TestWebServer mWebServer;
541
542         ThirdPartyCookiesTestHelper(TestWebServer webServer) throws Throwable {
543             mWebServer = webServer;
544             mContentsClient = new TestAwContentsClient();
545             final AwTestContainerView containerView =
546                     createAwTestContainerViewOnMainSync(mContentsClient);
547             mAwContents = containerView.getAwContents();
548             mAwContents.getSettings().setJavaScriptEnabled(true);
549         }
550
551         AwContents getAwContents() {
552             return mAwContents;
553         }
554
555         AwSettings getSettings() {
556             return mAwContents.getSettings();
557         }
558
559         TestWebServer getWebServer() {
560             return mWebServer;
561         }
562
563         void assertThirdPartyIFrameCookieResult(String suffix, boolean expectedResult)
564                 throws Throwable {
565             String key = "test" + suffix;
566             String value = "value" + suffix;
567             String iframePath = "/iframe_" + suffix + ".html";
568             String pagePath = "/content_" + suffix + ".html";
569
570             // We create a script which tries to set a cookie on a third party.
571             String cookieUrl = toThirdPartyUrl(
572                     makeCookieScriptUrl(getWebServer(), iframePath, key, value));
573
574             // Then we load it as an iframe.
575             String url = makeIframeUrl(getWebServer(), pagePath, cookieUrl);
576             loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url);
577
578             if (expectedResult) {
579                 String cookie = mCookieManager.getCookie(cookieUrl);
580                 assertNotNull(cookie);
581                 validateCookies(cookie, key);
582             } else {
583                 assertNull(mCookieManager.getCookie(cookieUrl));
584             }
585
586             // Clear the cookies.
587             clearCookies();
588             assertFalse(mCookieManager.hasCookies());
589         }
590     }
591
592     /**
593      * Creates a response on the TestWebServer which attempts to set a cookie when fetched.
594      * @param  webServer  the webServer on which to create the response
595      * @param  path the path component of the url (e.g "/my_thing_with_iframe.html")
596      * @param  url the url which which should appear as the src of the iframe.
597      * @return  the url which gets the response
598      */
599     private String makeIframeUrl(TestWebServer webServer, String path, String url) {
600         String responseStr = "<html><head><title>Content!</title></head>"
601                 + "<body><iframe src=" + url + "></iframe></body></html>";
602         return webServer.setResponse(path, responseStr, null);
603     }
604
605     /**
606      * Creates a response on the TestWebServer with a script that attempts to set a cookie.
607      * @param  webServer  the webServer on which to create the response
608      * @param  path the path component of the url (e.g "/cookie_test.html")
609      * @param  key the key of the cookie
610      * @param  value the value of the cookie
611      * @return  the url which gets the response
612      */
613     private String makeCookieScriptUrl(TestWebServer webServer, String path, String key,
614             String value) {
615         String response = "<html><head></head><body>"
616                 + "<script>document.cookie = \"" + key + "=" + value + "\";</script></body></html>";
617         return webServer.setResponse(path, response, null);
618     }
619
620     /**
621      * Makes a url look as if it comes from a different host.
622      * @param  url the url to fake.
623      * @return  the resulting url after faking.
624      */
625     private String toThirdPartyUrl(String url) {
626         return url.replace("localhost", "127.0.0.1");
627     }
628
629     private void setCookieOnUiThread(final String url, final String cookie,
630             final ValueCallback<Boolean> callback) throws Throwable {
631         runTestOnUiThread(new Runnable() {
632             @Override
633             public void run() {
634                 mCookieManager.setCookie(url, cookie, callback);
635             }
636         });
637     }
638
639     private void removeSessionCookiesOnUiThread(final ValueCallback<Boolean> callback)
640             throws Throwable {
641         runTestOnUiThread(new Runnable() {
642             @Override
643             public void run() {
644                 mCookieManager.removeSessionCookies(callback);
645             }
646         });
647     }
648
649     private void removeAllCookiesOnUiThread(final ValueCallback<Boolean> callback)
650             throws Throwable {
651         runTestOnUiThread(new Runnable() {
652             @Override
653             public void run() {
654                 mCookieManager.removeAllCookies(callback);
655             }
656         });
657     }
658
659     /**
660      * Clears all cookies synchronously.
661      */
662     private void clearCookies() throws Throwable {
663         CookieUtils.clearCookies(this, mCookieManager);
664     }
665
666     private void waitForCookie(final String url) throws Exception {
667         poll(new Callable<Boolean>() {
668             @Override
669             public Boolean call() throws Exception {
670                 return mCookieManager.getCookie(url) != null;
671             }
672         });
673     }
674
675     private void validateCookies(String responseCookie, String... expectedCookieNames) {
676         String[] cookies = responseCookie.split(";");
677         Set<String> foundCookieNames = new HashSet<String>();
678         for (String cookie : cookies) {
679             foundCookieNames.add(cookie.substring(0, cookie.indexOf("=")).trim());
680         }
681         MoreAsserts.assertEquals(
682                 foundCookieNames, new HashSet<String>(Arrays.asList(expectedCookieNames)));
683     }
684
685     private String makeExpiringCookie(String cookie, int secondsTillExpiry) {
686         return makeExpiringCookieMs(cookie, secondsTillExpiry * 1000);
687     }
688
689     @SuppressWarnings("deprecation")
690     private String makeExpiringCookieMs(String cookie, int millisecondsTillExpiry) {
691         Date date = new Date();
692         date.setTime(date.getTime() + millisecondsTillExpiry);
693         return cookie + "; expires=" + date.toGMTString();
694     }
695 }