Commit c6c4b8d7 authored by Christian Dullweber's avatar Christian Dullweber Committed by Commit Bot

Add browsing data deletion tests for Android

Add tests similar to BrowsingDataRemoverBrowserTest to Android.
They verify that cookies and other data is counted and deleted
correctly by Clear Browsing Data.
To perform asynchronous calls to Javascript, this adds support for
DomAutomationController.send on Android.

Bug: 905612, 562487
Change-Id: I2c5d3929cf4e1eed704b21b55ad9988b5a09c53e
Reviewed-on: https://chromium-review.googlesource.com/c/1339864
Commit-Queue: Christian Dullweber <dullweber@chromium.org>
Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Cr-Commit-Position: refs/heads/master@{#609229}
parent f9fb6707
...@@ -743,6 +743,7 @@ android_library("chrome_test_java") { ...@@ -743,6 +743,7 @@ android_library("chrome_test_java") {
"//chrome/test/data/translate/", "//chrome/test/data/translate/",
"//chrome/test/media_router/resources/", "//chrome/test/media_router/resources/",
"//components/test/data/payments/", "//components/test/data/payments/",
"//content/test/data/browsing_data/",
"//content/test/data/android/authenticator.html", "//content/test/data/android/authenticator.html",
"//content/test/data/android/geolocation.html", "//content/test/data/android/geolocation.html",
"//content/test/data/android/installedapp.html", "//content/test/data/android/installedapp.html",
......
...@@ -2150,6 +2150,7 @@ chrome_test_java_sources = [ ...@@ -2150,6 +2150,7 @@ chrome_test_java_sources = [
"javatests/src/org/chromium/chrome/browser/preferences/developer/TracingPreferencesTest.java", "javatests/src/org/chromium/chrome/browser/preferences/developer/TracingPreferencesTest.java",
"javatests/src/org/chromium/chrome/browser/preferences/password/SavePasswordsPreferencesTest.java", "javatests/src/org/chromium/chrome/browser/preferences/password/SavePasswordsPreferencesTest.java",
"javatests/src/org/chromium/chrome/browser/preferences/privacy/BrowsingDataBridgeTest.java", "javatests/src/org/chromium/chrome/browser/preferences/privacy/BrowsingDataBridgeTest.java",
"javatests/src/org/chromium/chrome/browser/preferences/privacy/BrowsingDataTest.java",
"javatests/src/org/chromium/chrome/browser/preferences/privacy/ClearBrowsingDataPreferencesTest.java", "javatests/src/org/chromium/chrome/browser/preferences/privacy/ClearBrowsingDataPreferencesTest.java",
"javatests/src/org/chromium/chrome/browser/preferences/privacy/ClearBrowsingDataPreferencesBasicTest.java", "javatests/src/org/chromium/chrome/browser/preferences/privacy/ClearBrowsingDataPreferencesBasicTest.java",
"javatests/src/org/chromium/chrome/browser/preferences/privacy/PrivacyPreferencesManagerNativeTest.java", "javatests/src/org/chromium/chrome/browser/preferences/privacy/PrivacyPreferencesManagerNativeTest.java",
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.browser.preferences.privacy;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.ThreadUtils;
import org.chromium.base.test.util.CallbackHelper;
import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.chrome.browser.ChromeActivity;
import org.chromium.chrome.browser.ChromeSwitches;
import org.chromium.chrome.browser.browsing_data.BrowsingDataType;
import org.chromium.chrome.browser.browsing_data.ClearBrowsingDataTab;
import org.chromium.chrome.browser.browsing_data.TimePeriod;
import org.chromium.chrome.test.ChromeActivityTestRule;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.content_public.browser.test.util.JavaScriptUtils;
import org.chromium.net.test.EmbeddedTestServer;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeoutException;
/**
* Integration tests for browsing data deletion.
*/
@RunWith(ChromeJUnit4ClassRunner.class)
@CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE})
public class BrowsingDataTest {
private static final String TEST_FILE = "/content/test/data/browsing_data/site_data.html";
private EmbeddedTestServer mTestServer;
private String mUrl;
@Rule
public ChromeActivityTestRule<ChromeActivity> mActivityTestRule =
new ChromeActivityTestRule<>(ChromeActivity.class);
@Before
public void setUp() throws Exception {
mActivityTestRule.startMainActivityOnBlankPage();
mTestServer = EmbeddedTestServer.createAndStartServer(InstrumentationRegistry.getContext());
mUrl = mTestServer.getURL(TEST_FILE);
}
private void clearBrowsingData(int dataType, int timePeriod)
throws InterruptedException, TimeoutException {
CallbackHelper helper = new CallbackHelper();
ThreadUtils.runOnUiThreadBlocking(() -> {
BrowsingDataBridge.getInstance().clearBrowsingData(
helper::notifyCalled, new int[] {dataType}, timePeriod);
});
helper.waitForCallback(0);
}
private int getCookieCount() throws Exception {
String[] out = {""};
BrowsingDataCounterBridge[] counter = {null};
CallbackHelper helper = new CallbackHelper();
BrowsingDataCounterBridge.BrowsingDataCounterCallback callback = (result) -> {
out[0] = result;
helper.notifyCalled();
};
ThreadUtils.runOnUiThreadBlocking(() -> {
counter[0] = new BrowsingDataCounterBridge(
callback, BrowsingDataType.COOKIES, ClearBrowsingDataTab.ADVANCED);
});
helper.waitForCallback(0);
// The counter returns a result like "3 sites" or "None".
if (out[0].equals("None")) return 0;
return Integer.parseInt(out[0].replaceAll("[^0-9]", ""));
}
private String runJavascriptAsync(String type) throws Exception {
return JavaScriptUtils.runJavascriptWithAsyncResult(
mActivityTestRule.getWebContents(), type);
}
/**
* Test cookies deletion.
*/
@Test
@SmallTest
public void testCookiesDeleted() throws Exception {
Assert.assertEquals(0, getCookieCount());
mActivityTestRule.loadUrl(mUrl);
Assert.assertEquals("false", runJavascriptAsync("hasCookie()"));
runJavascriptAsync("setCookie()");
Assert.assertEquals("true", runJavascriptAsync("hasCookie()"));
Assert.assertEquals(1, getCookieCount());
clearBrowsingData(BrowsingDataType.COOKIES, TimePeriod.LAST_HOUR);
Assert.assertEquals("false", runJavascriptAsync("hasCookie()"));
Assert.assertEquals(0, getCookieCount());
}
/**
* Test site data deletion.
*/
@Test
@SmallTest
public void testSiteDataDeleted() throws Exception {
// TODO(dullweber): Investigate, why WebSql fails this test.
List<String> siteData = Arrays.asList("LocalStorage", "ServiceWorker", "CacheStorage",
"IndexedDb", "FileSystem" /*, "WebSql"*/);
mActivityTestRule.loadUrl(mUrl);
for (String type : siteData) {
Assert.assertEquals(type, 0, getCookieCount());
Assert.assertEquals(type, "false", runJavascriptAsync("has" + type + "()"));
runJavascriptAsync("set" + type + "()");
Assert.assertEquals(type, 1, getCookieCount());
Assert.assertEquals(type, "true", runJavascriptAsync("has" + type + "()"));
clearBrowsingData(BrowsingDataType.COOKIES, TimePeriod.LAST_HOUR);
Assert.assertEquals(type, 0, getCookieCount());
Assert.assertEquals(type, "false", runJavascriptAsync("has" + type + "()"));
// Some types create data by checking for them, so we need to do a cleanup at the end.
clearBrowsingData(BrowsingDataType.COOKIES, TimePeriod.LAST_HOUR);
}
}
/**
* Test history deletion.
*/
@Test
@SmallTest
public void testHistoryDeleted() throws Exception {
Assert.assertEquals(0, getCookieCount());
mActivityTestRule.loadUrlInNewTab(mUrl);
Assert.assertEquals("false", runJavascriptAsync("hasHistory()"));
runJavascriptAsync("setHistory()");
Assert.assertEquals("true", runJavascriptAsync("hasHistory()"));
clearBrowsingData(BrowsingDataType.HISTORY, TimePeriod.LAST_HOUR);
Assert.assertEquals("false", runJavascriptAsync("hasHistory()"));
}
}
...@@ -45,6 +45,7 @@ android_library("content_java_test_support") { ...@@ -45,6 +45,7 @@ android_library("content_java_test_support") {
"javatests/src/org/chromium/content_public/browser/test/util/EqualityCriteria.java", "javatests/src/org/chromium/content_public/browser/test/util/EqualityCriteria.java",
"javatests/src/org/chromium/content_public/browser/test/util/HistoryUtils.java", "javatests/src/org/chromium/content_public/browser/test/util/HistoryUtils.java",
"javatests/src/org/chromium/content_public/browser/test/util/JavaScriptUtils.java", "javatests/src/org/chromium/content_public/browser/test/util/JavaScriptUtils.java",
"javatests/src/org/chromium/content_public/browser/test/util/DomAutomationController.java",
"javatests/src/org/chromium/content_public/browser/test/util/KeyUtils.java", "javatests/src/org/chromium/content_public/browser/test/util/KeyUtils.java",
"javatests/src/org/chromium/content_public/browser/test/util/RenderProcessLimit.java", "javatests/src/org/chromium/content_public/browser/test/util/RenderProcessLimit.java",
"javatests/src/org/chromium/content_public/browser/test/util/TestCallbackHelperContainer.java", "javatests/src/org/chromium/content_public/browser/test/util/TestCallbackHelperContainer.java",
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.content_public.browser.test.util;
import org.chromium.content_public.browser.WebContents;
import java.util.concurrent.TimeoutException;
/**
* This class implements DomAutomationController.send by injecting Javascript into
* a page and polling for data.
*/
public class DomAutomationController {
private WebContents mWebContents;
/**
* Enables domAutomationController on the given webContents. Must be called after every
* page load.
*/
public void inject(WebContents webContents) throws TimeoutException, InterruptedException {
mWebContents = webContents;
JavaScriptUtils.executeJavaScriptAndWaitForResult(mWebContents,
"window.domAutomationController = {"
+ " data_: [],"
+ " send: function(x) { this.data_.push(x) },"
+ " hasData: function() { return this.data_.length > 0 },"
+ " getData: function() { return this.data_.shift() }"
+ "}");
}
/**
* Waits until domAutomationController.send(value) has been called and returns value in JSON
* format.
*/
public String waitForResult(String failureReason)
throws TimeoutException, InterruptedException {
assert mWebContents != null;
CriteriaHelper.pollInstrumentationThread(() -> {
String result = JavaScriptUtils.executeJavaScriptAndWaitForResult(
mWebContents, "domAutomationController.hasData()");
return result.equals("true");
}, failureReason);
return JavaScriptUtils.executeJavaScriptAndWaitForResult(
mWebContents, "domAutomationController.getData()");
}
}
...@@ -56,6 +56,19 @@ public class JavaScriptUtils { ...@@ -56,6 +56,19 @@ public class JavaScriptUtils {
return helper.getJsonResultAndClear(); return helper.getJsonResultAndClear();
} }
/**
* Executes the given snippet of JavaScript code within the given WebContents and waits for a
* call to domAutomationController.send(). Returns the result from
* domAutomationController.send() in JSON format.
*/
public static String runJavascriptWithAsyncResult(WebContents webContents, String code)
throws TimeoutException, InterruptedException {
DomAutomationController controller = new DomAutomationController();
controller.inject(webContents);
executeJavaScript(webContents, code);
return controller.waitForResult("No result for `" + code + "`");
}
/** /**
* Executes the given snippet of JavaScript code but does not wait for the result. * Executes the given snippet of JavaScript code but does not wait for the result.
*/ */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment