1 // Copyright 2018 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.chrome.browser.browsing_data;
6 
7 import android.support.test.InstrumentationRegistry;
8 
9 import androidx.test.filters.SmallTest;
10 
11 import org.junit.Assert;
12 import org.junit.Before;
13 import org.junit.Rule;
14 import org.junit.Test;
15 import org.junit.runner.RunWith;
16 
17 import org.chromium.base.test.util.CallbackHelper;
18 import org.chromium.base.test.util.CommandLineFlags;
19 import org.chromium.chrome.browser.flags.ChromeSwitches;
20 import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
21 import org.chromium.chrome.test.ChromeTabbedActivityTestRule;
22 import org.chromium.content_public.browser.test.util.JavaScriptUtils;
23 import org.chromium.content_public.browser.test.util.TestThreadUtils;
24 import org.chromium.net.test.EmbeddedTestServer;
25 
26 import java.util.Arrays;
27 import java.util.List;
28 import java.util.concurrent.TimeoutException;
29 
30 /**
31  * Integration tests for browsing data deletion.
32  */
33 @RunWith(ChromeJUnit4ClassRunner.class)
34 @CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE})
35 public class BrowsingDataTest {
36     private static final String TEST_FILE = "/content/test/data/browsing_data/site_data.html";
37 
38     private EmbeddedTestServer mTestServer;
39     private String mUrl;
40 
41     @Rule
42     public ChromeTabbedActivityTestRule mActivityTestRule = new ChromeTabbedActivityTestRule();
43 
44     @Before
setUp()45     public void setUp() throws Exception {
46         mActivityTestRule.startMainActivityOnBlankPage();
47         mTestServer = EmbeddedTestServer.createAndStartServer(InstrumentationRegistry.getContext());
48         mUrl = mTestServer.getURL(TEST_FILE);
49     }
50 
clearBrowsingData(int dataType, int timePeriod)51     private void clearBrowsingData(int dataType, int timePeriod) throws TimeoutException {
52         CallbackHelper helper = new CallbackHelper();
53         TestThreadUtils.runOnUiThreadBlocking(() -> {
54             BrowsingDataBridge.getInstance().clearBrowsingData(
55                     helper::notifyCalled, new int[] {dataType}, timePeriod);
56         });
57         helper.waitForCallback(0);
58     }
59 
getCookieCount()60     private int getCookieCount() throws Exception {
61         String[] out = {""};
62         BrowsingDataCounterBridge[] counter = {null};
63         CallbackHelper helper = new CallbackHelper();
64         BrowsingDataCounterBridge.BrowsingDataCounterCallback callback = (result) -> {
65             if (result.equals("Calculating…")) return;
66             out[0] = result;
67             helper.notifyCalled();
68         };
69         TestThreadUtils.runOnUiThreadBlocking(() -> {
70             counter[0] = new BrowsingDataCounterBridge(
71                     callback, BrowsingDataType.COOKIES, ClearBrowsingDataTab.ADVANCED);
72         });
73         helper.waitForCallback(0);
74         // The counter returns a result like "3 sites" or "None".
75         if (out[0].equals("None")) return 0;
76         String cookieCount = out[0].replaceAll("[^0-9]", "");
77         Assert.assertFalse("Result should contain a number: " + out[0], cookieCount.isEmpty());
78         return Integer.parseInt(cookieCount);
79     }
80 
runJavascriptAsync(String type)81     private String runJavascriptAsync(String type) throws Exception {
82         return JavaScriptUtils.runJavascriptWithAsyncResult(
83                 mActivityTestRule.getWebContents(), type);
84     }
85 
86     /**
87      * Test cookies deletion.
88      */
89     @Test
90     @SmallTest
testCookiesDeleted()91     public void testCookiesDeleted() throws Exception {
92         Assert.assertEquals(0, getCookieCount());
93         mActivityTestRule.loadUrl(mUrl);
94         Assert.assertEquals("false", runJavascriptAsync("hasCookie()"));
95 
96         runJavascriptAsync("setCookie()");
97         Assert.assertEquals("true", runJavascriptAsync("hasCookie()"));
98         Assert.assertEquals(1, getCookieCount());
99 
100         clearBrowsingData(BrowsingDataType.COOKIES, TimePeriod.LAST_HOUR);
101         Assert.assertEquals("false", runJavascriptAsync("hasCookie()"));
102         Assert.assertEquals(0, getCookieCount());
103     }
104 
105     /**
106      * Test site data deletion.
107      */
108     @Test
109     @SmallTest
testSiteDataDeleted()110     public void testSiteDataDeleted() throws Exception {
111         // TODO(dullweber): Investigate, why WebSql fails this test.
112         List<String> siteData = Arrays.asList("LocalStorage", "ServiceWorker", "CacheStorage",
113                 "IndexedDb", "FileSystem" /*, "WebSql"*/);
114         mActivityTestRule.loadUrl(mUrl);
115 
116         for (String type : siteData) {
117             Assert.assertEquals(type, 0, getCookieCount());
118             Assert.assertEquals(type, "false", runJavascriptAsync("has" + type + "()"));
119 
120             runJavascriptAsync("set" + type + "()");
121             Assert.assertEquals(type, 1, getCookieCount());
122             Assert.assertEquals(type, "true", runJavascriptAsync("has" + type + "()"));
123 
124             clearBrowsingData(BrowsingDataType.COOKIES, TimePeriod.LAST_HOUR);
125             Assert.assertEquals(type, 0, getCookieCount());
126             Assert.assertEquals(type, "false", runJavascriptAsync("has" + type + "()"));
127 
128             // Some types create data by checking for them, so we need to do a cleanup at the end.
129             clearBrowsingData(BrowsingDataType.COOKIES, TimePeriod.LAST_HOUR);
130         }
131     }
132 
133     /**
134      * Test all data deletion for incognito profile. This only checks to see if an android specific
135      * code crashes or not. For details see, crbug.com/990624.
136      */
137     @Test
138     @SmallTest
testAllDataDeletedForIncognito()139     public void testAllDataDeletedForIncognito() throws Exception {
140         // TODO(roagarwal) : Crashes on BrowsingDataType.SITE_SETTINGS, BrowsingDataType.BOOKMARKS
141         // data types.
142         CallbackHelper helper = new CallbackHelper();
143         TestThreadUtils.runOnUiThreadBlocking(() -> {
144             BrowsingDataBridge.getInstance().clearBrowsingDataIncognitoForTesting(
145                     helper::notifyCalled,
146                     new int[] {BrowsingDataType.HISTORY, BrowsingDataType.CACHE,
147                             BrowsingDataType.COOKIES, BrowsingDataType.PASSWORDS,
148                             BrowsingDataType.FORM_DATA},
149                     TimePeriod.LAST_HOUR);
150         });
151         helper.waitForCallback(0);
152     }
153 
154     /**
155      * Test history deletion.
156      */
157     @Test
158     @SmallTest
testHistoryDeleted()159     public void testHistoryDeleted() throws Exception {
160         Assert.assertEquals(0, getCookieCount());
161         mActivityTestRule.loadUrlInNewTab(mUrl);
162         Assert.assertEquals("false", runJavascriptAsync("hasHistory()"));
163 
164         runJavascriptAsync("setHistory()");
165         Assert.assertEquals("true", runJavascriptAsync("hasHistory()"));
166 
167         clearBrowsingData(BrowsingDataType.HISTORY, TimePeriod.LAST_HOUR);
168         Assert.assertEquals("false", runJavascriptAsync("hasHistory()"));
169     }
170 }
171