| Index: android_webview/javatests/src/org/chromium/android_webview/test/PolicyUrlFilteringTest.java
|
| diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/PolicyUrlFilteringTest.java b/android_webview/javatests/src/org/chromium/android_webview/test/PolicyUrlFilteringTest.java
|
| index 7c2629e1d98a2637be0ecea13188e4d9ec0c0326..32bdae7c02dd8debf0ffe1a7a0c396137e160ad6 100644
|
| --- a/android_webview/javatests/src/org/chromium/android_webview/test/PolicyUrlFilteringTest.java
|
| +++ b/android_webview/javatests/src/org/chromium/android_webview/test/PolicyUrlFilteringTest.java
|
| @@ -4,10 +4,8 @@
|
|
|
| package org.chromium.android_webview.test;
|
|
|
| -import android.os.Bundle;
|
| import android.test.suitebuilder.annotation.MediumTest;
|
| import android.test.suitebuilder.annotation.SmallTest;
|
| -import android.text.TextUtils;
|
| import android.util.Pair;
|
|
|
| import org.chromium.android_webview.AwContents;
|
| @@ -17,9 +15,13 @@
|
| import org.chromium.base.test.util.Feature;
|
| import org.chromium.content.browser.test.util.TestCallbackHelperContainer;
|
| import org.chromium.net.test.util.TestWebServer;
|
| +import org.chromium.policy.AbstractAppRestrictionsProvider;
|
| import org.chromium.policy.CombinedPolicyProvider;
|
| +import org.chromium.policy.test.PolicyData;
|
| +import org.chromium.policy.test.annotations.Policies;
|
|
|
| import java.util.ArrayList;
|
| +import java.util.Arrays;
|
|
|
| /** Tests for the policy based URL filtering. */
|
| public class PolicyUrlFilteringTest extends AwTestBase {
|
| @@ -28,7 +30,8 @@
|
| private TestWebServer mWebServer;
|
| private String mFooTestUrl;
|
| private String mBarTestUrl;
|
| - private AwPolicyProvider mTestProvider;
|
| + private static final String sFooTestFilePath = "/foo.html";
|
| + private static final String sFooWhitelistFilter = "localhost" + sFooTestFilePath;
|
|
|
| private static final String sBlacklistPolicyName = "com.android.browser:URLBlacklist";
|
| private static final String sWhitelistPolicyName = "com.android.browser:URLWhitelist";
|
| @@ -36,21 +39,14 @@
|
| @Override
|
| public void setUp() throws Exception {
|
| super.setUp();
|
| - setTestAwContentsClient(new TestAwContentsClient());
|
| + mContentsClient = new TestAwContentsClient();
|
| + mAwContents = createAwTestContainerViewOnMainSync(mContentsClient).getAwContents();
|
| mWebServer = TestWebServer.start();
|
| - mFooTestUrl = mWebServer.setResponse("/foo.html", "<html><body>foo</body></html>",
|
| + mFooTestUrl = mWebServer.setResponse(sFooTestFilePath, "<html><body>foo</body></html>",
|
| new ArrayList<Pair<String, String>>());
|
| mBarTestUrl = mWebServer.setResponse("/bar.html", "<html><body>bar</body></html>",
|
| new ArrayList<Pair<String, String>>());
|
|
|
| - ThreadUtils.runOnUiThreadBlocking(new Runnable() {
|
| - @Override
|
| - public void run() {
|
| - mTestProvider = new AwPolicyProvider(getActivity().getApplicationContext());
|
| - CombinedPolicyProvider.get().registerProvider(mTestProvider);
|
| - }
|
| - });
|
| -
|
| getInstrumentation().waitForIdleSync();
|
| }
|
|
|
| @@ -60,97 +56,83 @@ public void tearDown() throws Exception {
|
| super.tearDown();
|
| }
|
|
|
| - private void setTestAwContentsClient(TestAwContentsClient contentsClient) throws Exception {
|
| - mContentsClient = contentsClient;
|
| - final AwTestContainerView testContainerView =
|
| - createAwTestContainerViewOnMainSync(mContentsClient);
|
| - mAwContents = testContainerView.getAwContents();
|
| - }
|
| -
|
| // Tests transforming the bundle to native policies, reloading the policies and blocking
|
| // the navigation.
|
| @MediumTest
|
| @Feature({"AndroidWebView", "Policy"})
|
| public void testBlacklistedUrl() throws Throwable {
|
| - TestCallbackHelperContainer.OnReceivedErrorHelper onReceivedErrorHelper =
|
| - mContentsClient.getOnReceivedErrorHelper();
|
| - TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
|
| - mContentsClient.getOnPageFinishedHelper();
|
| + final AwPolicyProvider testProvider =
|
| + new AwPolicyProvider(getActivity().getApplicationContext());
|
| + ThreadUtils.runOnUiThreadBlocking(new Runnable() {
|
| + @Override
|
| + public void run() {
|
| + CombinedPolicyProvider.get().registerProvider(testProvider);
|
| + }
|
| + });
|
|
|
| - loadUrlSync(mAwContents, onPageFinishedHelper, mFooTestUrl);
|
| - assertEquals(mFooTestUrl, onPageFinishedHelper.getUrl());
|
| - assertEquals(0, onReceivedErrorHelper.getCallCount());
|
| + navigateAndCheckOutcome(mFooTestUrl, 0);
|
|
|
| - setFilteringPolicy(new String[] {"localhost"}, new String[] {});
|
| + setFilteringPolicy(testProvider, new String[] {"localhost"}, new String[] {});
|
|
|
| - loadUrlSync(mAwContents, onPageFinishedHelper, mFooTestUrl);
|
| - assertEquals(mFooTestUrl, onPageFinishedHelper.getUrl());
|
| - assertEquals(1, onReceivedErrorHelper.getCallCount());
|
| - assertEquals(ErrorCodeConversionHelper.ERROR_CONNECT, onReceivedErrorHelper.getErrorCode());
|
| + navigateAndCheckOutcome(mFooTestUrl, 1);
|
| + assertEquals(ErrorCodeConversionHelper.ERROR_CONNECT,
|
| + mContentsClient.getOnReceivedErrorHelper().getErrorCode());
|
| }
|
|
|
| - // Tests transforming the bundle to native policies, reloading the policies and getting a
|
| - // successful navigation with a whitelist.
|
| + // Tests getting a successful navigation with a whitelist.
|
| @MediumTest
|
| @Feature({"AndroidWebView", "Policy"})
|
| + @Policies.Add({
|
| + @Policies.Item(key = sBlacklistPolicyName, stringArray = {"*"}),
|
| + @Policies.Item(key = sWhitelistPolicyName, stringArray = {sFooWhitelistFilter})})
|
| public void testWhitelistedUrl() throws Throwable {
|
| - TestCallbackHelperContainer.OnReceivedErrorHelper onReceivedErrorHelper =
|
| - mContentsClient.getOnReceivedErrorHelper();
|
| - TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
|
| - mContentsClient.getOnPageFinishedHelper();
|
| - setFilteringPolicy(new String[] {"*"}, new String[] {mFooTestUrl});
|
| -
|
| - loadUrlSync(mAwContents, onPageFinishedHelper, mFooTestUrl);
|
| - assertEquals(mFooTestUrl, onPageFinishedHelper.getUrl());
|
| - assertEquals(0, onReceivedErrorHelper.getCallCount());
|
| + navigateAndCheckOutcome(mFooTestUrl, 0);
|
|
|
| // Make sure it goes through the blacklist
|
| - loadUrlSync(mAwContents, onPageFinishedHelper, mBarTestUrl);
|
| - assertEquals(mBarTestUrl, onPageFinishedHelper.getUrl());
|
| - assertEquals(1, onReceivedErrorHelper.getCallCount());
|
| - assertEquals(ErrorCodeConversionHelper.ERROR_CONNECT, onReceivedErrorHelper.getErrorCode());
|
| + navigateAndCheckOutcome(mBarTestUrl, 1);
|
| + assertEquals(ErrorCodeConversionHelper.ERROR_CONNECT,
|
| + mContentsClient.getOnReceivedErrorHelper().getErrorCode());
|
| }
|
|
|
| // Tests that bad policy values are properly handled
|
| @SmallTest
|
| @Feature({"AndroidWebView", "Policy"})
|
| + @Policies.Add({
|
| + @Policies.Item(key = sBlacklistPolicyName, string = "shouldBeAJsonArrayNotAString")})
|
| public void testBadPolicyValue() throws Exception {
|
| - final Bundle newPolicies = new Bundle();
|
| - newPolicies.putString(sBlacklistPolicyName, "shouldBeAJsonArrayNotAString");
|
| - ThreadUtils.runOnUiThreadBlocking(new Runnable() {
|
| - @Override
|
| - public void run() {
|
| - mTestProvider.notifySettingsAvailable(newPolicies);
|
| - }
|
| - });
|
| - getInstrumentation().waitForIdleSync();
|
| + navigateAndCheckOutcome(mFooTestUrl, 0);
|
| + // At the moment this test is written, a failure is a crash, a success is no crash.
|
| + }
|
|
|
| + /**
|
| + * Synchronously loads the provided URL and checks that the number or reported errors for the
|
| + * current context is the expected one.
|
| + */
|
| + private void navigateAndCheckOutcome(String url, int expectedErrorCount) throws Exception {
|
| + TestCallbackHelperContainer.OnReceivedErrorHelper onReceivedErrorHelper =
|
| + mContentsClient.getOnReceivedErrorHelper();
|
| TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
|
| mContentsClient.getOnPageFinishedHelper();
|
| - loadUrlSync(mAwContents, onPageFinishedHelper, mFooTestUrl);
|
|
|
| - // At the moment this test is written, a failure is a crash, a success is no crash.
|
| + loadUrlSync(mAwContents, onPageFinishedHelper, url);
|
| + assertEquals(url, onPageFinishedHelper.getUrl());
|
| + assertEquals(expectedErrorCount, onReceivedErrorHelper.getCallCount());
|
| }
|
|
|
| - private void setFilteringPolicy(final String[] blacklistUrls, final String[] whitelistUrls) {
|
| - final Bundle newPolicies = new Bundle();
|
| -
|
| - if (blacklistUrls != null && blacklistUrls.length > 0) {
|
| - String blacklistString =
|
| - String.format("[\"%s\"]", TextUtils.join("\",\"", blacklistUrls));
|
| - newPolicies.putString(sBlacklistPolicyName, blacklistString);
|
| - }
|
| + private void setFilteringPolicy(final AwPolicyProvider testProvider,
|
| + final String[] blacklistUrls, final String[] whitelistUrls) {
|
| + final PolicyData[] policies = {
|
| + new PolicyData.StrArray(sBlacklistPolicyName, blacklistUrls),
|
| + new PolicyData.StrArray(sWhitelistPolicyName, whitelistUrls)
|
| + };
|
|
|
| - if (whitelistUrls != null && whitelistUrls.length > 0) {
|
| - String whitelistString =
|
| - String.format("[\"%s\"]", TextUtils.join("\",\"", whitelistUrls));
|
| - newPolicies.putString(sWhitelistPolicyName, whitelistString);
|
| - }
|
| + AbstractAppRestrictionsProvider.setTestRestrictions(
|
| + PolicyData.asBundle(Arrays.asList(policies)));
|
|
|
| ThreadUtils.runOnUiThreadBlocking(new Runnable() {
|
| @Override
|
| public void run() {
|
| - mTestProvider.notifySettingsAvailable(newPolicies);
|
| + testProvider.refresh();
|
| }
|
| });
|
|
|
|
|