OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.android_webview.test; | 5 package org.chromium.android_webview.test; |
6 | 6 |
7 import android.os.Build; | 7 import android.os.Build; |
8 import android.test.suitebuilder.annotation.SmallTest; | 8 import android.test.suitebuilder.annotation.SmallTest; |
9 import android.util.Pair; | 9 import android.util.Pair; |
10 | 10 |
11 import org.chromium.android_webview.AwContents; | 11 import org.chromium.android_webview.AwContents; |
12 import org.chromium.android_webview.test.util.CommonResources; | 12 import org.chromium.android_webview.test.util.CommonResources; |
13 import org.chromium.android_webview.test.util.JSUtils; | 13 import org.chromium.android_webview.test.util.JSUtils; |
| 14 import org.chromium.android_webview.test.util.JavascriptEventObserver; |
14 import org.chromium.base.annotations.SuppressFBWarnings; | 15 import org.chromium.base.annotations.SuppressFBWarnings; |
15 import org.chromium.base.test.util.DisabledTest; | 16 import org.chromium.base.test.util.DisabledTest; |
16 import org.chromium.base.test.util.Feature; | 17 import org.chromium.base.test.util.Feature; |
17 import org.chromium.base.test.util.MinAndroidSdkLevel; | 18 import org.chromium.base.test.util.MinAndroidSdkLevel; |
18 import org.chromium.content.browser.test.util.CallbackHelper; | 19 import org.chromium.content.browser.test.util.CallbackHelper; |
19 import org.chromium.content.browser.test.util.DOMUtils; | 20 import org.chromium.content.browser.test.util.DOMUtils; |
20 import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnEval
uateJavaScriptResultHelper; | 21 import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnEval
uateJavaScriptResultHelper; |
21 import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnPage
StartedHelper; | 22 import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnPage
StartedHelper; |
22 import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnRece
ivedErrorHelper; | 23 import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnRece
ivedErrorHelper; |
23 import org.chromium.content_public.browser.LoadUrlParams; | 24 import org.chromium.content_public.browser.LoadUrlParams; |
24 import org.chromium.net.test.util.TestWebServer; | 25 import org.chromium.net.test.util.TestWebServer; |
25 | 26 |
26 import java.util.ArrayList; | 27 import java.util.ArrayList; |
27 import java.util.List; | 28 import java.util.List; |
28 import java.util.concurrent.Callable; | 29 import java.util.concurrent.Callable; |
| 30 import java.util.concurrent.CountDownLatch; |
29 import java.util.concurrent.TimeUnit; | 31 import java.util.concurrent.TimeUnit; |
30 | 32 |
31 /** | 33 /** |
32 * Tests for the WebViewClient.shouldOverrideUrlLoading() method. | 34 * Tests for the WebViewClient.shouldOverrideUrlLoading() method. |
33 */ | 35 */ |
34 @MinAndroidSdkLevel(Build.VERSION_CODES.KITKAT) | 36 @MinAndroidSdkLevel(Build.VERSION_CODES.KITKAT) |
35 public class AwContentsClientShouldOverrideUrlLoadingTest extends AwTestBase { | 37 public class AwContentsClientShouldOverrideUrlLoadingTest extends AwTestBase { |
36 private static final String ABOUT_BLANK_URL = "about:blank"; | 38 private static final String ABOUT_BLANK_URL = "about:blank"; |
37 private static final String DATA_URL = "data:text/html,<div/>"; | 39 private static final String DATA_URL = "data:text/html,<div/>"; |
38 private static final String REDIRECT_TARGET_PATH = "/redirect_target.html"; | 40 private static final String REDIRECT_TARGET_PATH = "/redirect_target.html"; |
(...skipping 974 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1013 public Boolean call() { | 1015 public Boolean call() { |
1014 return getActivity().getLastSentIntent() != null; | 1016 return getActivity().getLastSentIntent() != null; |
1015 } | 1017 } |
1016 }); | 1018 }); |
1017 assertEquals("mailto:" + testEmail.replace("@", "%40"), | 1019 assertEquals("mailto:" + testEmail.replace("@", "%40"), |
1018 getActivity().getLastSentIntent().getData().toString()); | 1020 getActivity().getLastSentIntent().getData().toString()); |
1019 } finally { | 1021 } finally { |
1020 getActivity().setIgnoreStartActivity(false); | 1022 getActivity().setIgnoreStartActivity(false); |
1021 } | 1023 } |
1022 } | 1024 } |
| 1025 |
| 1026 @SmallTest |
| 1027 @Feature({"AndroidWebView"}) |
| 1028 public void testXhrInLink() throws Throwable { |
| 1029 final TestAwContentsClient contentsClient = new TestAwContentsClient(); |
| 1030 final AwTestContainerView testContainerView = |
| 1031 createAwTestContainerViewOnMainSync(contentsClient); |
| 1032 final AwContents awContents = testContainerView.getAwContents(); |
| 1033 TestAwContentsClient.ShouldOverrideUrlLoadingHelper shouldOverrideUrlLoa
dingHelper = |
| 1034 contentsClient.getShouldOverrideUrlLoadingHelper(); |
| 1035 |
| 1036 final CountDownLatch shouldOverrideUrlLoadingSignal = new CountDownLatch
(1); |
| 1037 |
| 1038 final String xhrPath = "/xhrPath.html"; |
| 1039 final String xhrUrl = mWebServer.setResponseWithRunnableAction( |
| 1040 xhrPath, CommonResources.makeHtmlPageFrom("", ""), null, new Run
nable() { |
| 1041 @Override |
| 1042 public void run() { |
| 1043 try { |
| 1044 shouldOverrideUrlLoadingSignal.await(); |
| 1045 } catch (InterruptedException e) { |
| 1046 throw new RuntimeException(e); |
| 1047 } |
| 1048 } |
| 1049 }); |
| 1050 |
| 1051 final String xhrJs = "function xhrFunction() {" |
| 1052 + " var xhr = new XMLHttpRequest();" |
| 1053 + " xhr.onload=function() {" |
| 1054 + " console.info('xhr loaded');" |
| 1055 + " window.jsInterface.setValue(true);" |
| 1056 + " };" |
| 1057 + " xhr.onerror=function() {" |
| 1058 + " console.info('xhr failed, status ' + xhr.status);" |
| 1059 + " window.jsInterface.setValue(false);" |
| 1060 + " };" |
| 1061 + " xhr.open('GET', '" + xhrUrl + "', true);" |
| 1062 + " xhr.send();" |
| 1063 + "};"; |
| 1064 |
| 1065 String pageWithXhrLink = makeHtmlPageFrom( |
| 1066 "<script>" + xhrJs + "</script>", |
| 1067 "<img onclick=\"xhrFunction(); location.href='" |
| 1068 + "thiswillbe://intercepted/" |
| 1069 + "'\" class=\"big\" id=\"link\" />"); |
| 1070 |
| 1071 final String startPath = "/startPath.html"; |
| 1072 final String startUrl = addPageToTestServer(mWebServer, startPath, pageW
ithXhrLink); |
| 1073 |
| 1074 enableJavaScriptOnUiThread(awContents); |
| 1075 final BooleanValueJavascriptObserver jsInterface = new BooleanValueJavas
criptObserver(); |
| 1076 |
| 1077 // add javascript interface |
| 1078 getInstrumentation().runOnMainSync(new Runnable() { |
| 1079 @Override |
| 1080 public void run() { |
| 1081 jsInterface.register(awContents.getContentViewCore(), "jsInterfa
ce"); |
| 1082 } |
| 1083 }); |
| 1084 |
| 1085 loadUrlSync(awContents, contentsClient.getOnPageFinishedHelper(), startU
rl); |
| 1086 |
| 1087 setShouldOverrideUrlLoadingReturnValueOnUiThread(shouldOverrideUrlLoadin
gHelper, true); |
| 1088 final int shouldOverrideUrlLoadingCallCount = shouldOverrideUrlLoadingHe
lper.getCallCount(); |
| 1089 |
| 1090 clickOnLinkUsingJs(awContents, contentsClient); |
| 1091 |
| 1092 // Make the server xhr response wait until the navigation request is int
ercepted. |
| 1093 shouldOverrideUrlLoadingHelper.waitForCallback(shouldOverrideUrlLoadingC
allCount); |
| 1094 shouldOverrideUrlLoadingSignal.countDown(); |
| 1095 |
| 1096 jsInterface.waitForEvent(WAIT_TIMEOUT_MS); |
| 1097 assertTrue(jsInterface.getValue()); |
| 1098 assertEquals(1, mWebServer.getRequestCount(xhrPath)); |
| 1099 } |
| 1100 |
| 1101 private static class BooleanValueJavascriptObserver extends JavascriptEventO
bserver { |
| 1102 private boolean mValue = false; |
| 1103 |
| 1104 public void setValue(boolean value) { |
| 1105 mValue = value; |
| 1106 notifyJava(); |
| 1107 } |
| 1108 |
| 1109 public boolean getValue() { |
| 1110 return mValue; |
| 1111 } |
| 1112 } |
1023 } | 1113 } |
OLD | NEW |