Upstream version 5.34.92.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
10 import org.chromium.android_webview.AwBrowserProcess;
11 import org.chromium.android_webview.AwContents;
12 import org.chromium.android_webview.AwCookieManager;
13 import org.chromium.android_webview.test.util.CommonResources;
14 import org.chromium.base.test.util.Feature;
15 import org.chromium.content.app.ContentMain;
16 import org.chromium.net.test.util.TestWebServer;
17
18 /**
19  * Test for the CookieManager in the case where it's used before Chromium is started.
20  */
21 public class CookieManagerStartupTest extends AwTestBase {
22
23     private AwCookieManager mCookieManager;
24     private TestAwContentsClient mContentsClient;
25     private AwContents mAwContents;
26
27     @Override
28     protected void setUp() throws Exception {
29         super.setUp();
30
31         mCookieManager = new AwCookieManager();
32         assertNotNull(mCookieManager);
33
34         ContentMain.initApplicationContext(getActivity().getApplicationContext());
35     }
36
37     @Override
38     protected boolean needsBrowserProcessStarted() {
39         return false;
40     }
41
42     private void startChromium() throws Exception {
43         final Context context = getActivity();
44         getInstrumentation().runOnMainSync(new Runnable() {
45             @Override
46             public void run() {
47                 AwBrowserProcess.start(context);
48             }
49         });
50
51         mContentsClient = new TestAwContentsClient();
52         final AwTestContainerView testContainerView =
53                 createAwTestContainerViewOnMainSync(mContentsClient);
54         mAwContents = testContainerView.getAwContents();
55         mAwContents.getSettings().setJavaScriptEnabled(true);
56     }
57
58     @MediumTest
59     @Feature({"AndroidWebView"})
60     public void testStartup() throws Throwable {
61         TestWebServer webServer = null;
62         try {
63             webServer = new TestWebServer(false);
64             String path = "/cookie_test.html";
65             String url = webServer.setResponse(path, CommonResources.ABOUT_HTML, null);
66
67             mCookieManager.setAcceptCookie(true);
68             mCookieManager.removeAllCookie();
69             assertTrue(mCookieManager.acceptCookie());
70             assertFalse(mCookieManager.hasCookies());
71             mCookieManager.setCookie(url, "count=41");
72
73             startChromium();
74             loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url);
75             executeJavaScriptAndWaitForResult(
76                     mAwContents,
77                     mContentsClient,
78                     "var c=document.cookie.split('=');document.cookie=c[0]+'='+(1+(+c[1]));");
79
80             assertEquals("count=42", mCookieManager.getCookie(url));
81         } finally {
82             if (webServer != null) webServer.shutdown();
83         }
84     }
85
86 }