Index: android_webview/javatests/src/org/chromium/android_webview/test/AwContentsClientShouldInterceptRequestTest.java |
diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsClientShouldInterceptRequestTest.java b/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsClientShouldInterceptRequestTest.java |
index fff12d289854403df3be5bb6d3172fd467ae2266..97cd530d12ab47da1bfa7d6503ca696e0123aa85 100644 |
--- a/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsClientShouldInterceptRequestTest.java |
+++ b/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsClientShouldInterceptRequestTest.java |
@@ -8,8 +8,10 @@ import android.test.suitebuilder.annotation.SmallTest; |
import android.util.Pair; |
import org.chromium.android_webview.AwContents; |
+import org.chromium.android_webview.AwContentsClient.ShouldInterceptRequestParams; |
import org.chromium.android_webview.InterceptedRequestData; |
import org.chromium.android_webview.test.util.CommonResources; |
+import org.chromium.android_webview.test.util.JSUtils; |
import org.chromium.base.test.util.Feature; |
import org.chromium.base.test.util.TestFileUtil; |
import org.chromium.content.browser.test.util.CallbackHelper; |
@@ -20,7 +22,9 @@ import java.io.ByteArrayInputStream; |
import java.io.IOException; |
import java.io.InputStream; |
import java.util.ArrayList; |
+import java.util.HashMap; |
import java.util.List; |
+import java.util.Map; |
import java.util.concurrent.Callable; |
import java.util.concurrent.ConcurrentHashMap; |
import java.util.concurrent.CountDownLatch; |
@@ -37,6 +41,8 @@ public class AwContentsClientShouldInterceptRequestTest extends AwTestBase { |
private List<String> mShouldInterceptRequestUrls = new ArrayList<String>(); |
private ConcurrentHashMap<String, InterceptedRequestData> mReturnValuesByUrls |
= new ConcurrentHashMap<String, InterceptedRequestData>(); |
+ private ConcurrentHashMap<String, ShouldInterceptRequestParams> mParamsByUrls |
+ = new ConcurrentHashMap<String, ShouldInterceptRequestParams>(); |
// This is read from the IO thread, so needs to be marked volatile. |
private volatile InterceptedRequestData mShouldInterceptRequestReturnValue = null; |
void setReturnValue(InterceptedRequestData value) { |
@@ -54,8 +60,14 @@ public class AwContentsClientShouldInterceptRequestTest extends AwTestBase { |
if (value != null) return value; |
return mShouldInterceptRequestReturnValue; |
} |
- public void notifyCalled(String url) { |
- mShouldInterceptRequestUrls.add(url); |
+ public ShouldInterceptRequestParams getParamsForUrl(String url) { |
+ assert getCallCount() > 0; |
+ assert mParamsByUrls.containsKey(url); |
+ return mParamsByUrls.get(url); |
+ } |
+ public void notifyCalled(ShouldInterceptRequestParams params) { |
+ mShouldInterceptRequestUrls.add(params.url); |
+ mParamsByUrls.put(params.url, params); |
notifyCalled(); |
} |
} |
@@ -75,9 +87,10 @@ public class AwContentsClientShouldInterceptRequestTest extends AwTestBase { |
} |
@Override |
- public InterceptedRequestData shouldInterceptRequest(String url) { |
- InterceptedRequestData returnValue = mShouldInterceptRequestHelper.getReturnValue(url); |
- mShouldInterceptRequestHelper.notifyCalled(url); |
+ public InterceptedRequestData shouldInterceptRequest(ShouldInterceptRequestParams params) { |
+ InterceptedRequestData returnValue = |
+ mShouldInterceptRequestHelper.getReturnValue(params.url); |
+ mShouldInterceptRequestHelper.notifyCalled(params); |
return returnValue; |
} |
@@ -104,6 +117,9 @@ public class AwContentsClientShouldInterceptRequestTest extends AwTestBase { |
} |
} |
+ final int teapotStatusCode = 418; |
+ final String teapotResponsePhrase = "I'm a teapot"; |
+ |
private String addPageToTestServer(TestWebServer webServer, String httpPath, String html) { |
List<Pair<String, String>> headers = new ArrayList<Pair<String, String>>(); |
headers.add(Pair.create("Content-Type", "text/html")); |
@@ -150,14 +166,13 @@ public class AwContentsClientShouldInterceptRequestTest extends AwTestBase { |
@SmallTest |
@Feature({"AndroidWebView"}) |
- public void testCalledWithCorrectUrl() throws Throwable { |
+ public void testCalledWithCorrectUrlParam() throws Throwable { |
final String aboutPageUrl = addAboutPageToTestServer(mWebServer); |
- int callCount = mShouldInterceptRequestHelper.getCallCount(); |
int onPageFinishedCallCount = mContentsClient.getOnPageFinishedHelper().getCallCount(); |
+ int callCount = mShouldInterceptRequestHelper.getCallCount(); |
loadUrlAsync(mAwContents, aboutPageUrl); |
- |
mShouldInterceptRequestHelper.waitForCallback(callCount); |
assertEquals(1, mShouldInterceptRequestHelper.getUrls().size()); |
assertEquals(aboutPageUrl, |
@@ -169,6 +184,95 @@ public class AwContentsClientShouldInterceptRequestTest extends AwTestBase { |
@SmallTest |
@Feature({"AndroidWebView"}) |
+ public void testCalledWithCorrectIsMainFrameParam() throws Throwable { |
+ final String subframeUrl = addAboutPageToTestServer(mWebServer); |
+ final String pageWithIframeUrl = addPageToTestServer(mWebServer, "/page_with_iframe.html", |
+ CommonResources.makeHtmlPageFrom("", |
+ "<iframe src=\"" + subframeUrl + "\"/>")); |
+ |
+ int callCount = mShouldInterceptRequestHelper.getCallCount(); |
+ loadUrlAsync(mAwContents, pageWithIframeUrl); |
+ mShouldInterceptRequestHelper.waitForCallback(callCount, 2); |
+ assertEquals(2, mShouldInterceptRequestHelper.getUrls().size()); |
+ assertEquals(false, |
+ mShouldInterceptRequestHelper.getParamsForUrl(subframeUrl).isMainFrame); |
+ assertEquals(true, |
+ mShouldInterceptRequestHelper.getParamsForUrl(pageWithIframeUrl).isMainFrame); |
+ } |
+ |
+ @SmallTest |
+ @Feature({"AndroidWebView"}) |
+ public void testCalledWithCorrectMethodParam() throws Throwable { |
+ final String pageToPostToUrl = addAboutPageToTestServer(mWebServer); |
+ final String pageWithFormUrl = addPageToTestServer(mWebServer, "/page_with_form.html", |
+ CommonResources.makeHtmlPageWithSimplePostFormTo(pageToPostToUrl)); |
+ enableJavaScriptOnUiThread(mAwContents); |
+ |
+ int callCount = mShouldInterceptRequestHelper.getCallCount(); |
+ loadUrlAsync(mAwContents, pageWithFormUrl); |
+ mShouldInterceptRequestHelper.waitForCallback(callCount); |
+ assertEquals("GET", |
+ mShouldInterceptRequestHelper.getParamsForUrl(pageWithFormUrl).method); |
+ |
+ callCount = mShouldInterceptRequestHelper.getCallCount(); |
+ JSUtils.clickOnLinkUsingJs(this, mAwContents, |
+ mContentsClient.getOnEvaluateJavaScriptResultHelper(), "link"); |
+ mShouldInterceptRequestHelper.waitForCallback(callCount); |
+ assertEquals("POST", |
+ mShouldInterceptRequestHelper.getParamsForUrl(pageToPostToUrl).method); |
+ } |
+ |
+ @SmallTest |
+ @Feature({"AndroidWebView"}) |
+ public void testCalledWithCorrectHasUserGestureParam() throws Throwable { |
+ final String aboutPageUrl = addAboutPageToTestServer(mWebServer); |
+ final String pageWithLinkUrl = addPageToTestServer(mWebServer, "/page_with_link.html", |
+ CommonResources.makeHtmlPageWithSimpleLinkTo(aboutPageUrl)); |
+ enableJavaScriptOnUiThread(mAwContents); |
+ |
+ int callCount = mShouldInterceptRequestHelper.getCallCount(); |
+ loadUrlAsync(mAwContents, pageWithLinkUrl); |
+ mShouldInterceptRequestHelper.waitForCallback(callCount); |
+ assertEquals(false, |
+ mShouldInterceptRequestHelper.getParamsForUrl(pageWithLinkUrl).hasUserGesture); |
+ |
+ callCount = mShouldInterceptRequestHelper.getCallCount(); |
+ JSUtils.clickOnLinkUsingJs(this, mAwContents, |
+ mContentsClient.getOnEvaluateJavaScriptResultHelper(), "link"); |
+ mShouldInterceptRequestHelper.waitForCallback(callCount); |
+ assertEquals(true, |
+ mShouldInterceptRequestHelper.getParamsForUrl(aboutPageUrl).hasUserGesture); |
+ } |
+ |
+ @SmallTest |
+ @Feature({"AndroidWebView"}) |
+ public void testCalledWithCorrectHeadersParam() throws Throwable { |
+ final String headerName = "X-Test-Header-Name"; |
+ final String headerValue = "TestHeaderValue"; |
+ final String syncGetUrl = addPageToTestServer(mWebServer, "/intercept_me", |
+ CommonResources.ABOUT_HTML); |
+ final String mainPageUrl = addPageToTestServer(mWebServer, "/main", |
+ CommonResources.makeHtmlPageFrom("", |
+ "<script>" + |
+ " var xhr = new XMLHttpRequest();" + |
+ " xhr.open('GET', '" + syncGetUrl + "', false);" + |
+ " xhr.setRequestHeader('" + headerName + "', '" + headerValue + "'); " + |
+ " xhr.send(null);" + |
+ "</script>")); |
+ enableJavaScriptOnUiThread(mAwContents); |
+ |
+ int callCount = mShouldInterceptRequestHelper.getCallCount(); |
+ loadUrlAsync(mAwContents, mainPageUrl); |
+ mShouldInterceptRequestHelper.waitForCallback(callCount, 2); |
+ |
+ Map<String, String> headers = |
+ mShouldInterceptRequestHelper.getParamsForUrl(syncGetUrl).headers; |
+ assertTrue(headers.containsKey(headerName)); |
+ assertEquals(headerValue, headers.get(headerName)); |
+ } |
+ |
+ @SmallTest |
+ @Feature({"AndroidWebView"}) |
public void testOnLoadResourceCalledWithCorrectUrl() throws Throwable { |
final String aboutPageUrl = addAboutPageToTestServer(mWebServer); |
final TestAwContentsClient.OnLoadResourceHelper onLoadResourceHelper = |
@@ -317,7 +421,7 @@ public class AwContentsClientShouldInterceptRequestTest extends AwTestBase { |
@SmallTest |
@Feature({"AndroidWebView"}) |
- public void testHttpStatusField() throws Throwable { |
+ public void testHttpStatusCodeAndText() throws Throwable { |
final String syncGetUrl = mWebServer.getResponseUrl("/intercept_me"); |
final String syncGetJs = |
"(function() {" + |
@@ -325,7 +429,8 @@ public class AwContentsClientShouldInterceptRequestTest extends AwTestBase { |
" xhr.open('GET', '" + syncGetUrl + "', false);" + |
" xhr.send(null);" + |
" console.info('xhr.status = ' + xhr.status);" + |
- " return xhr.status;" + |
+ " console.info('xhr.statusText = ' + xhr.statusText);" + |
+ " return '[' + xhr.status + '][' + xhr.statusText + ']';" + |
"})();"; |
enableJavaScriptOnUiThread(mAwContents); |
@@ -334,31 +439,46 @@ public class AwContentsClientShouldInterceptRequestTest extends AwTestBase { |
mShouldInterceptRequestHelper.setReturnValue( |
new InterceptedRequestData("text/html", "UTF-8", null)); |
- assertEquals("404", |
+ assertEquals("\"[404][Not Found]\"", |
executeJavaScriptAndWaitForResult(mAwContents, mContentsClient, syncGetJs)); |
mShouldInterceptRequestHelper.setReturnValue( |
new InterceptedRequestData("text/html", "UTF-8", new EmptyInputStream())); |
- assertEquals("200", |
+ assertEquals("\"[200][OK]\"", |
+ executeJavaScriptAndWaitForResult(mAwContents, mContentsClient, syncGetJs)); |
+ |
+ mShouldInterceptRequestHelper.setReturnValue( |
+ new InterceptedRequestData("text/html", "UTF-8", new EmptyInputStream(), |
+ teapotStatusCode, teapotResponsePhrase, new HashMap<String, String>())); |
+ assertEquals("\"[" + teapotStatusCode + "][" + teapotResponsePhrase + "]\"", |
executeJavaScriptAndWaitForResult(mAwContents, mContentsClient, syncGetJs)); |
} |
- @SmallTest |
- @Feature({"AndroidWebView"}) |
- public void testHttpResponseClientHeader() throws Throwable { |
- final String clientResponseHeaderName = "Client-Via"; |
- // JSON stringification applied by executeJavaScriptAndWaitForResult adds quotes |
- // around returned strings. |
- final String clientResponseHeaderValue = "\"shouldInterceptRequest\""; |
- final String syncGetUrl = mWebServer.getResponseUrl("/intercept_me"); |
+ private String getHeaderValue(AwContents awContents, TestAwContentsClient contentsClient, |
+ String url, String headerName) throws Exception { |
final String syncGetJs = |
"(function() {" + |
" var xhr = new XMLHttpRequest();" + |
- " xhr.open('GET', '" + syncGetUrl + "', false);" + |
+ " xhr.open('GET', '" + url + "', false);" + |
" xhr.send(null);" + |
" console.info(xhr.getAllResponseHeaders());" + |
- " return xhr.getResponseHeader('" + clientResponseHeaderName + "');" + |
+ " return xhr.getResponseHeader('" + headerName + "');" + |
"})();"; |
+ String header = executeJavaScriptAndWaitForResult(awContents, contentsClient, syncGetJs); |
+ // JSON stringification applied by executeJavaScriptAndWaitForResult adds quotes |
+ // around returned strings. |
+ assertTrue(header.length() > 2); |
+ assertEquals('"', header.charAt(0)); |
+ assertEquals('"', header.charAt(header.length() - 1)); |
+ return header.substring(1, header.length() - 1); |
+ } |
+ |
+ @SmallTest |
+ @Feature({"AndroidWebView"}) |
+ public void testHttpResponseClientViaHeader() throws Throwable { |
+ final String clientResponseHeaderName = "Client-Via"; |
+ final String clientResponseHeaderValue = "shouldInterceptRequest"; |
+ final String syncGetUrl = mWebServer.getResponseUrl("/intercept_me"); |
enableJavaScriptOnUiThread(mAwContents); |
final String aboutPageUrl = addAboutPageToTestServer(mWebServer); |
@@ -369,13 +489,32 @@ public class AwContentsClientShouldInterceptRequestTest extends AwTestBase { |
mShouldInterceptRequestHelper.setReturnValue( |
new InterceptedRequestData("text/html", "UTF-8", null)); |
assertEquals(clientResponseHeaderValue, |
- executeJavaScriptAndWaitForResult(mAwContents, mContentsClient, syncGetJs)); |
+ getHeaderValue(mAwContents, mContentsClient, syncGetUrl, clientResponseHeaderName)); |
mShouldInterceptRequestHelper.setReturnValue( |
new InterceptedRequestData("text/html", "UTF-8", new EmptyInputStream())); |
assertEquals(clientResponseHeaderValue, |
- executeJavaScriptAndWaitForResult(mAwContents, mContentsClient, syncGetJs)); |
+ getHeaderValue(mAwContents, mContentsClient, syncGetUrl, clientResponseHeaderName)); |
+ |
} |
+ @SmallTest |
+ @Feature({"AndroidWebView"}) |
+ public void testHttpResponseHeader() throws Throwable { |
+ final String clientResponseHeaderName = "X-Test-Header-Name"; |
+ final String clientResponseHeaderValue = "TestHeaderValue"; |
+ final String syncGetUrl = mWebServer.getResponseUrl("/intercept_me"); |
+ final Map<String, String> headers = new HashMap<String, String>(); |
+ headers.put(clientResponseHeaderName, clientResponseHeaderValue); |
+ enableJavaScriptOnUiThread(mAwContents); |
+ |
+ final String aboutPageUrl = addAboutPageToTestServer(mWebServer); |
+ loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), aboutPageUrl); |
+ |
+ mShouldInterceptRequestHelper.setReturnValue( |
+ new InterceptedRequestData("text/html", "UTF-8", null, 0, null, headers)); |
+ assertEquals(clientResponseHeaderValue, |
+ getHeaderValue(mAwContents, mContentsClient, syncGetUrl, clientResponseHeaderName)); |
+ } |
private String makePageWithTitle(String title) { |
return CommonResources.makeHtmlPageFrom("<title>" + title + "</title>", |
@@ -479,12 +618,12 @@ public class AwContentsClientShouldInterceptRequestTest extends AwTestBase { |
@Feature({"AndroidWebView"}) |
public void testCalledForIframe() throws Throwable { |
final String aboutPageUrl = addAboutPageToTestServer(mWebServer); |
- final String pageWithIframe = addPageToTestServer(mWebServer, "/page_with_iframe.html", |
+ final String pageWithIframeUrl = addPageToTestServer(mWebServer, "/page_with_iframe.html", |
CommonResources.makeHtmlPageFrom("", |
"<iframe src=\"" + aboutPageUrl + "\"/>")); |
int callCount = mShouldInterceptRequestHelper.getCallCount(); |
- loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), pageWithIframe); |
+ loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), pageWithIframeUrl); |
mShouldInterceptRequestHelper.waitForCallback(callCount, 2); |
assertEquals(2, mShouldInterceptRequestHelper.getUrls().size()); |
assertEquals(aboutPageUrl, mShouldInterceptRequestHelper.getUrls().get(1)); |