Upstream version 10.39.233.0
[platform/framework/web/crosswalk.git] / src / android_webview / javatests / src / org / chromium / android_webview / test / CookieManagerStartupTest.java
1 // Copyright 2013 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.content.Context;
8 import android.test.suitebuilder.annotation.MediumTest;
9 import android.test.suitebuilder.annotation.SmallTest;
10
11 import org.chromium.android_webview.AwBrowserProcess;
12 import org.chromium.android_webview.AwContents;
13 import org.chromium.android_webview.AwCookieManager;
14 import org.chromium.android_webview.AwWebResourceResponse;
15 import org.chromium.android_webview.test.util.CommonResources;
16 import org.chromium.android_webview.test.util.CookieUtils;
17 import org.chromium.base.test.util.Feature;
18 import org.chromium.content.app.ContentMain;
19 import org.chromium.net.test.util.TestWebServer;
20
21
22 /**
23  * Tests for CookieManager/Chromium startup ordering weirdness.
24  */
25 public class CookieManagerStartupTest extends AwTestBase {
26
27     private TestAwContentsClient mContentsClient;
28     private AwContents mAwContents;
29
30     @Override
31     protected void setUp() throws Exception {
32         super.setUp();
33         ContentMain.initApplicationContext(getActivity().getApplicationContext());
34     }
35
36     @Override
37     protected boolean needsBrowserProcessStarted() {
38         return false;
39     }
40
41     private void startChromium() throws Exception {
42         startChromiumWithClient(new TestAwContentsClient());
43     }
44
45     private void startChromiumWithClient(TestAwContentsClient contentsClient) throws Exception {
46         final Context context = getActivity();
47         getInstrumentation().runOnMainSync(new Runnable() {
48             @Override
49             public void run() {
50                 AwBrowserProcess.start(context);
51             }
52         });
53
54         mContentsClient = contentsClient;
55         final AwTestContainerView testContainerView =
56                 createAwTestContainerViewOnMainSync(mContentsClient);
57         mAwContents = testContainerView.getAwContents();
58         mAwContents.getSettings().setJavaScriptEnabled(true);
59     }
60
61     @MediumTest
62     @Feature({"AndroidWebView"})
63     public void testStartup() throws Throwable {
64         TestWebServer webServer = TestWebServer.start();
65         try {
66             String path = "/cookie_test.html";
67             String url = webServer.setResponse(path, CommonResources.ABOUT_HTML, null);
68
69             AwCookieManager cookieManager = new AwCookieManager();
70             assertNotNull(cookieManager);
71
72             CookieUtils.clearCookies(this, cookieManager);
73             assertFalse(cookieManager.hasCookies());
74
75             cookieManager.setAcceptCookie(true);
76             assertTrue(cookieManager.acceptCookie());
77
78             cookieManager.setCookie(url, "count=41");
79
80             startChromium();
81             loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url);
82             executeJavaScriptAndWaitForResult(
83                     mAwContents,
84                     mContentsClient,
85                     "var c=document.cookie.split('=');document.cookie=c[0]+'='+(1+(+c[1]));");
86
87             assertEquals("count=42", cookieManager.getCookie(url));
88         } finally {
89             webServer.shutdown();
90         }
91     }
92
93     @SmallTest
94     @Feature({"AndroidWebView", "Privacy"})
95     public void testAllowFileSchemeCookies() throws Throwable {
96         AwCookieManager cookieManager = new AwCookieManager();
97         assertFalse(cookieManager.allowFileSchemeCookies());
98         cookieManager.setAcceptFileSchemeCookies(true);
99         assertTrue(cookieManager.allowFileSchemeCookies());
100         cookieManager.setAcceptFileSchemeCookies(false);
101         assertFalse(cookieManager.allowFileSchemeCookies());
102     }
103
104     @SmallTest
105     @Feature({"AndroidWebView", "Privacy"})
106     public void testAllowCookies() throws Throwable {
107         AwCookieManager cookieManager = new AwCookieManager();
108         assertTrue(cookieManager.acceptCookie());
109         cookieManager.setAcceptCookie(false);
110         assertFalse(cookieManager.acceptCookie());
111         cookieManager.setAcceptCookie(true);
112         assertTrue(cookieManager.acceptCookie());
113     }
114
115     // https://code.google.com/p/chromium/issues/detail?id=374203
116     @MediumTest
117     @Feature({"AndroidWebView"})
118     public void testShouldInterceptRequestDeadlock() throws Throwable {
119         String url = "http://www.example.com";
120         TestAwContentsClient contentsClient = new TestAwContentsClient() {
121             @Override
122             public AwWebResourceResponse shouldInterceptRequest(
123                     ShouldInterceptRequestParams params) {
124                 (new AwCookieManager()).getCookie("www.example.com");
125                 return null;
126             }
127         };
128         startChromiumWithClient(contentsClient);
129         loadUrlSync(mAwContents, contentsClient.getOnPageFinishedHelper(), url);
130     }
131 }