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.test.suitebuilder.annotation.SmallTest; | 7 import android.test.suitebuilder.annotation.SmallTest; |
8 import android.util.Pair; | 8 import android.util.Pair; |
9 | 9 |
10 import org.chromium.android_webview.AwContents; | 10 import org.chromium.android_webview.AwContents; |
11 import org.chromium.android_webview.AwContentsClient; | 11 import org.chromium.android_webview.AwContentsClient; |
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.content.browser.test.util.CallbackHelper; | 18 import org.chromium.content.browser.test.util.CallbackHelper; |
18 import org.chromium.content.browser.test.util.DOMUtils; | 19 import org.chromium.content.browser.test.util.DOMUtils; |
19 import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnEval
uateJavaScriptResultHelper; | 20 import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnEval
uateJavaScriptResultHelper; |
20 import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnPage
StartedHelper; | 21 import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnPage
StartedHelper; |
21 import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnRece
ivedErrorHelper; | 22 import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnRece
ivedErrorHelper; |
22 import org.chromium.content_public.browser.LoadUrlParams; | 23 import org.chromium.content_public.browser.LoadUrlParams; |
23 import org.chromium.net.test.util.TestWebServer; | 24 import org.chromium.net.test.util.TestWebServer; |
24 | 25 |
25 import java.util.ArrayList; | 26 import java.util.ArrayList; |
26 import java.util.List; | 27 import java.util.List; |
27 import java.util.concurrent.Callable; | 28 import java.util.concurrent.Callable; |
| 29 import java.util.concurrent.CountDownLatch; |
28 import java.util.concurrent.TimeUnit; | 30 import java.util.concurrent.TimeUnit; |
29 | 31 |
30 /** | 32 /** |
31 * Tests for the WebViewClient.shouldOverrideUrlLoading() method. | 33 * Tests for the WebViewClient.shouldOverrideUrlLoading() method. |
32 */ | 34 */ |
33 public class AwContentsClientShouldOverrideUrlLoadingTest extends AwTestBase { | 35 public class AwContentsClientShouldOverrideUrlLoadingTest extends AwTestBase { |
34 private static final String ABOUT_BLANK_URL = "about:blank"; | 36 private static final String ABOUT_BLANK_URL = "about:blank"; |
35 private static final String DATA_URL = "data:text/html,<div/>"; | 37 private static final String DATA_URL = "data:text/html,<div/>"; |
36 private static final String REDIRECT_TARGET_PATH = "/redirect_target.html"; | 38 private static final String REDIRECT_TARGET_PATH = "/redirect_target.html"; |
37 private static final String TITLE = "TITLE"; | 39 private static final String TITLE = "TITLE"; |
(...skipping 675 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
713 mShouldOverrideUrlLoadingHelper.waitForCallback(indirectLoadCallCount, 1
); | 715 mShouldOverrideUrlLoadingHelper.waitForCallback(indirectLoadCallCount, 1
); |
714 assertEquals(redirectUrl, | 716 assertEquals(redirectUrl, |
715 mShouldOverrideUrlLoadingHelper.getShouldOverrideUrlLoadingUrl()
); | 717 mShouldOverrideUrlLoadingHelper.getShouldOverrideUrlLoadingUrl()
); |
716 assertFalse(mShouldOverrideUrlLoadingHelper.isRedirect()); | 718 assertFalse(mShouldOverrideUrlLoadingHelper.isRedirect()); |
717 assertTrue(mShouldOverrideUrlLoadingHelper.hasUserGesture()); | 719 assertTrue(mShouldOverrideUrlLoadingHelper.hasUserGesture()); |
718 assertTrue(mShouldOverrideUrlLoadingHelper.isMainFrame()); | 720 assertTrue(mShouldOverrideUrlLoadingHelper.isMainFrame()); |
719 mShouldOverrideUrlLoadingHelper.waitForCallback(indirectLoadCallCount +
1, 1); | 721 mShouldOverrideUrlLoadingHelper.waitForCallback(indirectLoadCallCount +
1, 1); |
720 assertEquals(redirectTarget, | 722 assertEquals(redirectTarget, |
721 mShouldOverrideUrlLoadingHelper.getShouldOverrideUrlLoadingUrl()
); | 723 mShouldOverrideUrlLoadingHelper.getShouldOverrideUrlLoadingUrl()
); |
722 assertEquals(serverSideRedirect, mShouldOverrideUrlLoadingHelper.isRedir
ect()); | 724 assertEquals(serverSideRedirect, mShouldOverrideUrlLoadingHelper.isRedir
ect()); |
723 assertFalse(mShouldOverrideUrlLoadingHelper.hasUserGesture()); | 725 // We keep the user gesture from the initial navigation for serverside r
edirects but drop |
| 726 // the user gesture for browser initiated redirects. |
| 727 assertEquals(serverSideRedirect, mShouldOverrideUrlLoadingHelper.hasUser
Gesture()); |
724 assertTrue(mShouldOverrideUrlLoadingHelper.isMainFrame()); | 728 assertTrue(mShouldOverrideUrlLoadingHelper.isMainFrame()); |
725 } | 729 } |
726 | 730 |
727 @SmallTest | 731 @SmallTest |
728 @Feature({"AndroidWebView", "Navigation"}) | 732 @Feature({"AndroidWebView", "Navigation"}) |
729 public void testCalledOn302Redirect() throws Throwable { | 733 public void testCalledOn302Redirect() throws Throwable { |
730 final String redirectTargetUrl = createRedirectTargetPage(); | 734 final String redirectTargetUrl = createRedirectTargetPage(); |
731 final String redirectUrl = mWebServer.setRedirect("/302.html", redirectT
argetUrl); | 735 final String redirectUrl = mWebServer.setRedirect("/302.html", redirectT
argetUrl); |
732 doTestCalledOnRedirect(redirectUrl, redirectTargetUrl, true); | 736 doTestCalledOnRedirect(redirectUrl, redirectTargetUrl, true); |
733 } | 737 } |
(...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1042 int callCount = mShouldOverrideUrlLoadingHelper.getCallCount(); | 1046 int callCount = mShouldOverrideUrlLoadingHelper.getCallCount(); |
1043 DOMUtils.clickNodeByJs(this, mAwContents.getContentViewCore(), | 1047 DOMUtils.clickNodeByJs(this, mAwContents.getContentViewCore(), |
1044 "window.frames[0].document.getElementById('email')"); | 1048 "window.frames[0].document.getElementById('email')"); |
1045 mShouldOverrideUrlLoadingHelper.waitForCallback(callCount); | 1049 mShouldOverrideUrlLoadingHelper.waitForCallback(callCount); |
1046 assertEquals("mailto:" + testEmail.replace("@", "%40"), | 1050 assertEquals("mailto:" + testEmail.replace("@", "%40"), |
1047 mShouldOverrideUrlLoadingHelper.getShouldOverrideUrlLoadingUrl()
); | 1051 mShouldOverrideUrlLoadingHelper.getShouldOverrideUrlLoadingUrl()
); |
1048 assertFalse(mShouldOverrideUrlLoadingHelper.isRedirect()); | 1052 assertFalse(mShouldOverrideUrlLoadingHelper.isRedirect()); |
1049 assertTrue(mShouldOverrideUrlLoadingHelper.hasUserGesture()); | 1053 assertTrue(mShouldOverrideUrlLoadingHelper.hasUserGesture()); |
1050 assertFalse(mShouldOverrideUrlLoadingHelper.isMainFrame()); | 1054 assertFalse(mShouldOverrideUrlLoadingHelper.isMainFrame()); |
1051 } | 1055 } |
| 1056 |
| 1057 @SmallTest |
| 1058 @Feature({"AndroidWebView"}) |
| 1059 public void testXhrInLink() throws Throwable { |
| 1060 standardSetup(); |
| 1061 final CountDownLatch shouldOverrideUrlLoadingSignal = new CountDownLatch
(1); |
| 1062 |
| 1063 final String xhrPath = "/xhrPath.html"; |
| 1064 final String xhrUrl = mWebServer.setResponseWithRunnableAction( |
| 1065 xhrPath, CommonResources.makeHtmlPageFrom("", ""), null, new Run
nable() { |
| 1066 @Override |
| 1067 public void run() { |
| 1068 try { |
| 1069 shouldOverrideUrlLoadingSignal.await(); |
| 1070 } catch (InterruptedException e) { |
| 1071 throw new RuntimeException(e); |
| 1072 } |
| 1073 } |
| 1074 }); |
| 1075 |
| 1076 final String xhrJs = "function xhrFunction() {" |
| 1077 + " var xhr = new XMLHttpRequest();" |
| 1078 + " xhr.onload=function() {" |
| 1079 + " console.info('xhr loaded');" |
| 1080 + " window.jsInterface.setValue(true);" |
| 1081 + " };" |
| 1082 + " xhr.onerror=function() {" |
| 1083 + " console.info('xhr failed, status ' + xhr.status);" |
| 1084 + " window.jsInterface.setValue(false);" |
| 1085 + " };" |
| 1086 + " xhr.open('GET', '" + xhrUrl + "', true);" |
| 1087 + " xhr.send();" |
| 1088 + "};"; |
| 1089 |
| 1090 String pageWithXhrLink = makeHtmlPageFrom( |
| 1091 "<script>" + xhrJs + "</script>", "<img onclick=\"xhrFunction();
location.href='" |
| 1092 + "thiswillbe://intercepted/" |
| 1093 + "'\" class=\"big\" id=\"link\" />"); |
| 1094 |
| 1095 final String startPath = "/startPath.html"; |
| 1096 final String startUrl = addPageToTestServer(startPath, pageWithXhrLink); |
| 1097 |
| 1098 enableJavaScriptOnUiThread(mAwContents); |
| 1099 final BooleanValueJavascriptObserver jsInterface = new BooleanValueJavas
criptObserver(); |
| 1100 |
| 1101 // add javascript interface |
| 1102 getInstrumentation().runOnMainSync(new Runnable() { |
| 1103 @Override |
| 1104 public void run() { |
| 1105 jsInterface.register(mAwContents.getContentViewCore(), "jsInterf
ace"); |
| 1106 } |
| 1107 }); |
| 1108 |
| 1109 loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), star
tUrl); |
| 1110 |
| 1111 setShouldOverrideUrlLoadingReturnValueOnUiThread(true); |
| 1112 final int shouldOverrideUrlLoadingCallCount = |
| 1113 mShouldOverrideUrlLoadingHelper.getCallCount(); |
| 1114 |
| 1115 assertEquals(0, mWebServer.getRequestCount(xhrPath)); |
| 1116 |
| 1117 clickOnLinkUsingJs(); |
| 1118 |
| 1119 // Make the server xhr response wait until the navigation request is int
ercepted. |
| 1120 mShouldOverrideUrlLoadingHelper.waitForCallback(shouldOverrideUrlLoading
CallCount); |
| 1121 shouldOverrideUrlLoadingSignal.countDown(); |
| 1122 |
| 1123 jsInterface.waitForEvent(WAIT_TIMEOUT_MS); |
| 1124 assertTrue(jsInterface.getValue()); |
| 1125 assertEquals(1, mWebServer.getRequestCount(xhrPath)); |
| 1126 } |
| 1127 |
| 1128 private static class BooleanValueJavascriptObserver extends JavascriptEventO
bserver { |
| 1129 private boolean mValue = false; |
| 1130 |
| 1131 public void setValue(boolean value) { |
| 1132 mValue = value; |
| 1133 notifyJava(); |
| 1134 } |
| 1135 |
| 1136 public boolean getValue() { |
| 1137 return mValue; |
| 1138 } |
| 1139 } |
1052 } | 1140 } |
OLD | NEW |