CookieManagerStartupTest.java revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
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
5package org.chromium.android_webview.test;
6
7import android.content.Context;
8import android.test.suitebuilder.annotation.MediumTest;
9
10import org.chromium.android_webview.AwBrowserProcess;
11import org.chromium.android_webview.AwContents;
12import org.chromium.android_webview.AwCookieManager;
13import org.chromium.android_webview.test.util.CommonResources;
14import org.chromium.base.test.util.Feature;
15import org.chromium.content.app.ContentMain;
16import 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 */
21public 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}
87