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.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; |
(...skipping 933 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
957 @Override | 958 @Override |
958 public Boolean call() { | 959 public Boolean call() { |
959 return getActivity().getLastSentIntent() != null; | 960 return getActivity().getLastSentIntent() != null; |
960 } | 961 } |
961 }); | 962 }); |
962 assertEquals(testUrl, getActivity().getLastSentIntent().getData().to String()); | 963 assertEquals(testUrl, getActivity().getLastSentIntent().getData().to String()); |
963 } finally { | 964 } finally { |
964 getActivity().setIgnoreStartActivity(false); | 965 getActivity().setIgnoreStartActivity(false); |
965 } | 966 } |
966 } | 967 } |
968 | |
969 @SmallTest | |
970 @Feature({"AndroidWebView"}) | |
971 public void testXhrInLink() throws Throwable { | |
972 final TestAwContentsClient contentsClient = new TestAwContentsClient(); | |
973 final AwTestContainerView testContainerView = | |
974 createAwTestContainerViewOnMainSync(contentsClient); | |
975 final AwContents awContents = testContainerView.getAwContents(); | |
976 TestAwContentsClient.ShouldOverrideUrlLoadingHelper shouldOverrideUrlLoa dingHelper = | |
977 contentsClient.getShouldOverrideUrlLoadingHelper(); | |
978 | |
979 final String xhrPath = "/xhrPath.html"; | |
980 final String xhrUrl = | |
981 addPageToTestServer(mWebServer, xhrPath, CommonResources.makeHtm lPageFrom("", "")); | |
982 | |
983 final String xhrJs = "function xhrFunction() {" | |
984 + " var xhr = new XMLHttpRequest();" | |
985 + " xhr.onload=function() {" | |
986 + " console.info('xhr loaded');" | |
987 + " window.jsInterface.setValue(true);" | |
988 + " };" | |
989 + " xhr.onerror=function() {" | |
990 + " console.info('xhr failed, status ' + xhr.status);" | |
991 + " window.jsInterface.setValue(false);" | |
992 + " };" | |
993 + " xhr.open('GET', '" + xhrUrl + "', true);" | |
994 + " xhr.send();" | |
995 + "};"; | |
996 | |
997 String pageWithXhrLink = makeHtmlPageFrom( | |
998 "<script>" + xhrJs + "</script>", | |
999 "<img onclick=\"xhrFunction(); location.href='" | |
1000 + "thiswillbe://intercepted/" | |
1001 + "'\" class=\"big\" id=\"link\" />"); | |
1002 | |
1003 final String startPath = "/startPath.html"; | |
1004 final String startUrl = addPageToTestServer(mWebServer, startPath, pageW ithXhrLink); | |
1005 | |
1006 enableJavaScriptOnUiThread(awContents); | |
1007 final BooleanValueJavascriptObserver jsInterface = new BooleanValueJavas criptObserver(); | |
1008 | |
1009 // add javascript interface | |
1010 getInstrumentation().runOnMainSync(new Runnable() { | |
1011 @Override | |
1012 public void run() { | |
1013 jsInterface.register(awContents.getContentViewCore(), "jsInterfa ce"); | |
1014 } | |
1015 }); | |
1016 | |
1017 loadUrlSync(awContents, contentsClient.getOnPageFinishedHelper(), startU rl); | |
1018 | |
1019 // TODO is the following line an OK way of delaying the xhr request? | |
1020 // (we just call Thread.sleep() in mWebServer.getResponse...) | |
1021 mWebServer.setResponseDelay(WAIT_TIMEOUT_MS / 5); | |
mnaganov (inactive)
2015/06/23 19:06:08
No, that's an awfully flaky way. You can use TestW
gsennton
2015/06/24 11:14:16
Done.
| |
1022 | |
1023 setShouldOverrideUrlLoadingReturnValueOnUiThread(shouldOverrideUrlLoadin gHelper, true); | |
1024 | |
1025 clickOnLinkUsingJs(awContents, contentsClient); | |
1026 | |
1027 jsInterface.waitForEvent(WAIT_TIMEOUT_MS); | |
1028 assertTrue(jsInterface.getValue()); | |
1029 assertEquals(1, mWebServer.getRequestCount(xhrPath)); | |
1030 } | |
1031 | |
1032 private class BooleanValueJavascriptObserver extends JavascriptEventObserver { | |
1033 private boolean mValue = false; | |
1034 | |
1035 public void setValue(boolean value) { | |
1036 mValue = value; | |
1037 notifyJava(); | |
1038 } | |
1039 | |
1040 public boolean getValue() { | |
1041 return mValue; | |
1042 } | |
1043 } | |
967 } | 1044 } |
OLD | NEW |