EMMA Coverage Report (generated Fri Aug 23 16:39:17 PDT 2013)
[all classes][org.chromium.android_webview.test]

COVERAGE SUMMARY FOR SOURCE FILE [CookieManagerTest.java]

nameclass, %method, %block, %line, %
CookieManagerTest.java100% (7/7)100% (22/22)98%  (614/626)100% (112.5/113)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class CookieManagerTest$3100% (1/1)100% (2/2)93%  (14/15)94%  (1.9/2)
isSatisfied (): boolean 100% (1/1)89%  (8/9)88%  (0.9/1)
CookieManagerTest$3 (CookieManagerTest): void 100% (1/1)100% (6/6)100% (1/1)
     
class CookieManagerTest$6100% (1/1)100% (2/2)94%  (15/16)95%  (1.9/2)
isSatisfied (): boolean 100% (1/1)90%  (9/10)90%  (0.9/1)
CookieManagerTest$6 (CookieManagerTest): void 100% (1/1)100% (6/6)100% (1/1)
     
class CookieManagerTest$1100% (1/1)100% (2/2)95%  (19/20)95%  (1.9/2)
isSatisfied (): boolean 100% (1/1)91%  (10/11)90%  (0.9/1)
CookieManagerTest$1 (CookieManagerTest, String): void 100% (1/1)100% (9/9)100% (1/1)
     
class CookieManagerTest$4100% (1/1)100% (2/2)96%  (27/28)98%  (3/3)
isSatisfied (): boolean 100% (1/1)95%  (21/22)98%  (2/2)
CookieManagerTest$4 (CookieManagerTest): void 100% (1/1)100% (6/6)100% (1/1)
     
class CookieManagerTest$5100% (1/1)100% (2/2)96%  (27/28)98%  (3/3)
isSatisfied (): boolean 100% (1/1)95%  (21/22)98%  (2/2)
CookieManagerTest$5 (CookieManagerTest): void 100% (1/1)100% (6/6)100% (1/1)
     
class CookieManagerTest100% (1/1)100% (10/10)99%  (501/508)100% (105/105)
testAcceptCookie (): void 100% (1/1)96%  (193/200)100% (37/37)
CookieManagerTest (): void 100% (1/1)100% (3/3)100% (1/1)
access$000 (CookieManagerTest): AwCookieManager 100% (1/1)100% (3/3)100% (1/1)
setCookie (String, String): void 100% (1/1)100% (23/23)100% (2/2)
setUp (): void 100% (1/1)100% (30/30)100% (8/8)
testAllowFileSchemeCookies (): void 100% (1/1)100% (21/21)100% (6/6)
testCookieExpiration (): void 100% (1/1)100% (129/129)100% (30/30)
testRemoveAllCookie (): void 100% (1/1)100% (49/49)100% (13/13)
validateCookies (String, String []): void 100% (1/1)100% (42/42)100% (6/6)
waitForCookie (String): void 100% (1/1)100% (8/8)100% (2/2)
     
class CookieManagerTest$2100% (1/1)100% (2/2)100% (11/11)100% (2/2)
CookieManagerTest$2 (CookieManagerTest): void 100% (1/1)100% (6/6)100% (1/1)
isSatisfied (): boolean 100% (1/1)100% (5/5)100% (1/1)

1// Copyright (c) 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 
5package org.chromium.android_webview.test;
6 
7import android.test.MoreAsserts;
8import android.test.suitebuilder.annotation.MediumTest;
9import android.test.suitebuilder.annotation.SmallTest;
10import android.util.Pair;
11 
12import org.chromium.android_webview.AwContents;
13import org.chromium.android_webview.AwCookieManager;
14import org.chromium.android_webview.test.util.JSUtils;
15import org.chromium.base.test.util.Feature;
16import org.chromium.content.browser.test.util.Criteria;
17import org.chromium.content.browser.test.util.CriteriaHelper;
18import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnEvaluateJavaScriptResultHelper;
19import org.chromium.net.test.util.TestWebServer;
20 
21import java.util.ArrayList;
22import java.util.Arrays;
23import java.util.Date;
24import java.util.HashSet;
25import java.util.List;
26import java.util.Set;
27import java.util.concurrent.atomic.AtomicInteger;
28 
29/**
30 * Tests for the CookieManager.
31 */
32public class CookieManagerTest extends AwTestBase {
33 
34    private AwCookieManager mCookieManager;
35    private TestAwContentsClient mContentsClient;
36    private AwContents mAwContents;
37 
38    @Override
39    protected void setUp() throws Exception {
40        super.setUp();
41 
42        mCookieManager = new AwCookieManager();
43        mContentsClient = new TestAwContentsClient();
44        final AwTestContainerView testContainerView =
45                createAwTestContainerViewOnMainSync(mContentsClient);
46        mAwContents = testContainerView.getAwContents();
47        mAwContents.getSettings().setJavaScriptEnabled(true);
48        assertNotNull(mCookieManager);
49    }
50 
51    @SmallTest
52    @Feature({"AndroidWebView", "Privacy"})
53    public void testAllowFileSchemeCookies() throws Throwable {
54        assertFalse(mCookieManager.allowFileSchemeCookies());
55        mCookieManager.setAcceptFileSchemeCookies(true);
56        assertTrue(mCookieManager.allowFileSchemeCookies());
57        mCookieManager.setAcceptFileSchemeCookies(false);
58        assertFalse(mCookieManager.allowFileSchemeCookies());
59    }
60 
61    @MediumTest
62    @Feature({"AndroidWebView", "Privacy"})
63    public void testAcceptCookie() throws Throwable {
64        TestWebServer webServer = null;
65        try {
66            webServer = new TestWebServer(false);
67            String path = "/cookie_test.html";
68            String responseStr =
69                    "<html><head><title>TEST!</title></head><body>HELLO!</body></html>";
70            String url = webServer.setResponse(path, responseStr, null);
71 
72            mCookieManager.setAcceptCookie(false);
73            mCookieManager.removeAllCookie();
74            assertFalse(mCookieManager.acceptCookie());
75            assertFalse(mCookieManager.hasCookies());
76 
77            loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url);
78            setCookie("test1", "value1");
79            assertNull(mCookieManager.getCookie(url));
80 
81            List<Pair<String, String>> responseHeaders = new ArrayList<Pair<String, String>>();
82            responseHeaders.add(
83                    Pair.create("Set-Cookie", "header-test1=header-value1; path=" + path));
84            url = webServer.setResponse(path, responseStr, responseHeaders);
85            loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url);
86            assertNull(mCookieManager.getCookie(url));
87 
88            mCookieManager.setAcceptCookie(true);
89            assertTrue(mCookieManager.acceptCookie());
90 
91            url = webServer.setResponse(path, responseStr, null);
92            loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url);
93            setCookie("test2", "value2");
94            waitForCookie(url);
95            String cookie = mCookieManager.getCookie(url);
96            assertNotNull(cookie);
97            validateCookies(cookie, "test2");
98 
99            responseHeaders = new ArrayList<Pair<String, String>>();
100            responseHeaders.add(
101                    Pair.create("Set-Cookie", "header-test2=header-value2 path=" + path));
102            url = webServer.setResponse(path, responseStr, responseHeaders);
103            loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url);
104            waitForCookie(url);
105            cookie = mCookieManager.getCookie(url);
106            assertNotNull(cookie);
107            validateCookies(cookie, "test2", "header-test2");
108 
109            // clean up all cookies
110            mCookieManager.removeAllCookie();
111        } finally {
112            if (webServer != null) webServer.shutdown();
113        }
114    }
115 
116    private void setCookie(final String name, final String value)
117            throws Throwable {
118        JSUtils.executeJavaScriptAndWaitForResult(
119                this, mAwContents,
120                mContentsClient.getOnEvaluateJavaScriptResultHelper(),
121                "var expirationDate = new Date();" +
122                "expirationDate.setDate(expirationDate.getDate() + 5);" +
123                "document.cookie='" + name + "=" + value +
124                        "; expires=' + expirationDate.toUTCString();");
125    }
126 
127    private void waitForCookie(final String url) throws InterruptedException {
128        assertTrue(CriteriaHelper.pollForCriteria(new Criteria() {
129            @Override
130            public boolean isSatisfied() {
131                return mCookieManager.getCookie(url) != null;
132            }
133        }));
134    }
135 
136    private void validateCookies(String responseCookie, String... expectedCookieNames) {
137        String[] cookies = responseCookie.split(";");
138        Set<String> foundCookieNames = new HashSet<String>();
139        for (String cookie : cookies) {
140            foundCookieNames.add(cookie.substring(0, cookie.indexOf("=")).trim());
141        }
142        MoreAsserts.assertEquals(
143                foundCookieNames, new HashSet<String>(Arrays.asList(expectedCookieNames)));
144    }
145 
146    @MediumTest
147    @Feature({"AndroidWebView", "Privacy"})
148    public void testRemoveAllCookie() throws InterruptedException {
149        // enable cookie
150        mCookieManager.setAcceptCookie(true);
151        assertTrue(mCookieManager.acceptCookie());
152 
153        // first there should be no cookie stored
154        mCookieManager.removeAllCookie();
155        mCookieManager.flushCookieStore();
156        assertFalse(mCookieManager.hasCookies());
157 
158        String url = "http://www.example.com";
159        String cookie = "name=test";
160        mCookieManager.setCookie(url, cookie);
161        assertEquals(cookie, mCookieManager.getCookie(url));
162 
163        assertTrue(CriteriaHelper.pollForCriteria(new Criteria() {
164            @Override
165            public boolean isSatisfied() {
166                return mCookieManager.hasCookies();
167            }
168        }));
169 
170        // clean up all cookies
171        mCookieManager.removeAllCookie();
172        assertTrue(CriteriaHelper.pollForCriteria(new Criteria() {
173            @Override
174            public boolean isSatisfied() {
175                return !mCookieManager.hasCookies();
176            }
177        }));
178    }
179 
180    @MediumTest
181    @Feature({"AndroidWebView", "Privacy"})
182    @SuppressWarnings("deprecation")
183    public void testCookieExpiration() throws InterruptedException {
184        // enable cookie
185        mCookieManager.setAcceptCookie(true);
186        assertTrue(mCookieManager.acceptCookie());
187        mCookieManager.removeAllCookie();
188        assertFalse(mCookieManager.hasCookies());
189 
190        final String url = "http://www.example.com";
191        final String cookie1 = "cookie1=peter";
192        final String cookie2 = "cookie2=sue";
193        final String cookie3 = "cookie3=marc";
194 
195        mCookieManager.setCookie(url, cookie1); // session cookie
196 
197        Date date = new Date();
198        date.setTime(date.getTime() + 1000 * 600);
199        String value2 = cookie2 + "; expires=" + date.toGMTString();
200        mCookieManager.setCookie(url, value2); // expires in 10min
201 
202        long expiration = 3000;
203        date = new Date();
204        date.setTime(date.getTime() + expiration);
205        String value3 = cookie3 + "; expires=" + date.toGMTString();
206        mCookieManager.setCookie(url, value3); // expires in 3s
207 
208        String allCookies = mCookieManager.getCookie(url);
209        assertTrue(allCookies.contains(cookie1));
210        assertTrue(allCookies.contains(cookie2));
211        assertTrue(allCookies.contains(cookie3));
212 
213        mCookieManager.removeSessionCookie();
214        assertTrue(CriteriaHelper.pollForCriteria(new Criteria() {
215            @Override
216            public boolean isSatisfied() {
217                String c = mCookieManager.getCookie(url);
218                return !c.contains(cookie1) && c.contains(cookie2) && c.contains(cookie3);
219            }
220        }));
221 
222        Thread.sleep(expiration + 1000); // wait for cookie to expire
223        mCookieManager.removeExpiredCookie();
224        assertTrue(CriteriaHelper.pollForCriteria(new Criteria() {
225            @Override
226            public boolean isSatisfied() {
227                String c = mCookieManager.getCookie(url);
228                return !c.contains(cookie1) && c.contains(cookie2) && !c.contains(cookie3);
229            }
230        }));
231 
232        mCookieManager.removeAllCookie();
233        assertTrue(CriteriaHelper.pollForCriteria(new Criteria() {
234            @Override
235            public boolean isSatisfied() {
236                return mCookieManager.getCookie(url) == null;
237            }
238        }));
239    }
240}

[all classes][org.chromium.android_webview.test]
EMMA 2.0.5312 (C) Vladimir Roubtsov