This source file includes following definitions.
- setUp
- setTestAwContentsClient
- Feature
- testOnPageFinishedPassesCorrectUrl
- Feature
- testOnPageFinishedCalledAfterError
- Feature
- testOnPageFinishedCalledAfterRedirectedUrlIsOverridden
- Feature
- testOnPageFinishedNotCalledForValidSubresources
- Feature
- testOnPageFinishedNotCalledForHistoryApi
- Feature
- testOnPageFinishedCalledForHrefNavigations
- Feature
- testOnPageFinishedCalledForHrefNavigationsWithBaseUrl
- doTestOnPageFinishedCalledForHrefNavigations
package org.chromium.android_webview.test;
import android.test.suitebuilder.annotation.MediumTest;
import org.chromium.android_webview.AwContents;
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.content.browser.test.util.TestCallbackHelperContainer;
import org.chromium.net.test.util.TestWebServer;
public class ClientOnPageFinishedTest extends AwTestBase {
private TestAwContentsClient mContentsClient;
private AwContents mAwContents;
@Override
public void setUp() throws Exception {
super.setUp();
setTestAwContentsClient(new TestAwContentsClient());
}
private void setTestAwContentsClient(TestAwContentsClient contentsClient) throws Exception {
mContentsClient = contentsClient;
final AwTestContainerView testContainerView =
createAwTestContainerViewOnMainSync(mContentsClient);
mAwContents = testContainerView.getAwContents();
}
@MediumTest
@Feature({"AndroidWebView"})
public void testOnPageFinishedPassesCorrectUrl() throws Throwable {
TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
mContentsClient.getOnPageFinishedHelper();
String html = "<html><body>Simple page.</body></html>";
int currentCallCount = onPageFinishedHelper.getCallCount();
loadDataAsync(mAwContents, html, "text/html", false);
onPageFinishedHelper.waitForCallback(currentCallCount);
assertEquals("data:text/html," + html, onPageFinishedHelper.getUrl());
}
@MediumTest
@Feature({"AndroidWebView"})
public void testOnPageFinishedCalledAfterError() throws Throwable {
setTestAwContentsClient(new TestAwContentsClient() {
private boolean isOnReceivedErrorCalled = false;
private boolean isOnPageFinishedCalled = false;
@Override
public void onReceivedError(int errorCode, String description, String failingUrl) {
isOnReceivedErrorCalled = true;
assertEquals(false, isOnPageFinishedCalled);
super.onReceivedError(errorCode, description, failingUrl);
}
@Override
public void onPageFinished(String url) {
isOnPageFinishedCalled = true;
assertEquals(true, isOnReceivedErrorCalled);
super.onPageFinished(url);
}
});
TestCallbackHelperContainer.OnReceivedErrorHelper onReceivedErrorHelper =
mContentsClient.getOnReceivedErrorHelper();
TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
mContentsClient.getOnPageFinishedHelper();
String invalidUrl = "http://localhost:7/non_existent";
int onReceivedErrorCallCount = onReceivedErrorHelper.getCallCount();
int onPageFinishedCallCount = onPageFinishedHelper.getCallCount();
loadUrlSync(mAwContents, onPageFinishedHelper, invalidUrl);
assertEquals(invalidUrl, onReceivedErrorHelper.getFailingUrl());
assertEquals(invalidUrl, onPageFinishedHelper.getUrl());
}
@MediumTest
@Feature({"AndroidWebView"})
public void testOnPageFinishedCalledAfterRedirectedUrlIsOverridden() throws Throwable {
TestWebServer webServer = null;
try {
webServer = new TestWebServer(false);
final String redirectTargetPath = "/redirect_target.html";
final String redirectTargetUrl = webServer.setResponse(redirectTargetPath,
"<html><body>hello world</body></html>", null);
final String redirectUrl = webServer.setRedirect("/302.html", redirectTargetUrl);
final TestAwContentsClient.ShouldOverrideUrlLoadingHelper urlOverrideHelper =
mContentsClient.getShouldOverrideUrlLoadingHelper();
urlOverrideHelper.setShouldOverrideUrlLoadingReturnValue(true);
TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
mContentsClient.getOnPageFinishedHelper();
final int currentOnPageFinishedCallCount = onPageFinishedHelper.getCallCount();
loadUrlAsync(mAwContents, redirectUrl);
onPageFinishedHelper.waitForCallback(currentOnPageFinishedCallCount);
assertEquals(redirectTargetUrl, onPageFinishedHelper.getUrl());
} finally {
if (webServer != null) webServer.shutdown();
}
}
@MediumTest
@Feature({"AndroidWebView"})
public void testOnPageFinishedNotCalledForValidSubresources() throws Throwable {
TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
mContentsClient.getOnPageFinishedHelper();
TestWebServer webServer = null;
try {
webServer = new TestWebServer(false);
final String testHtml = "<html><head>Header</head><body>Body</body></html>";
final String testPath = "/test.html";
final String syncPath = "/sync.html";
final String testUrl = webServer.setResponse(testPath, testHtml, null);
final String syncUrl = webServer.setResponse(syncPath, testHtml, null);
assertEquals(0, onPageFinishedHelper.getCallCount());
final int pageWithSubresourcesCallCount = onPageFinishedHelper.getCallCount();
loadDataAsync(mAwContents,
"<html><iframe src=\"" + testUrl + "\" /></html>",
"text/html",
false);
onPageFinishedHelper.waitForCallback(pageWithSubresourcesCallCount);
final int synchronizationPageCallCount = onPageFinishedHelper.getCallCount();
loadUrlAsync(mAwContents, syncUrl);
onPageFinishedHelper.waitForCallback(synchronizationPageCallCount);
assertEquals(syncUrl, onPageFinishedHelper.getUrl());
assertEquals(2, onPageFinishedHelper.getCallCount());
} finally {
if (webServer != null) webServer.shutdown();
}
}
@MediumTest
@Feature({"AndroidWebView"})
public void testOnPageFinishedNotCalledForHistoryApi() throws Throwable {
TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
mContentsClient.getOnPageFinishedHelper();
enableJavaScriptOnUiThread(mAwContents);
TestWebServer webServer = null;
try {
webServer = new TestWebServer(false);
final String testHtml = "<html><head>Header</head><body>Body</body></html>";
final String testPath = "/test.html";
final String historyPath = "/history.html";
final String syncPath = "/sync.html";
final String testUrl = webServer.setResponse(testPath, testHtml, null);
final String historyUrl = webServer.getResponseUrl(historyPath);
final String syncUrl = webServer.setResponse(syncPath, testHtml, null);
assertEquals(0, onPageFinishedHelper.getCallCount());
loadUrlSync(mAwContents, onPageFinishedHelper, testUrl);
executeJavaScriptAndWaitForResult(mAwContents, mContentsClient,
"history.pushState(null, null, '" + historyUrl + "');");
final int synchronizationPageCallCount = onPageFinishedHelper.getCallCount();
loadUrlAsync(mAwContents, syncUrl);
onPageFinishedHelper.waitForCallback(synchronizationPageCallCount);
assertEquals(syncUrl, onPageFinishedHelper.getUrl());
assertEquals(2, onPageFinishedHelper.getCallCount());
} finally {
if (webServer != null) webServer.shutdown();
}
}
@MediumTest
@Feature({"AndroidWebView"})
public void testOnPageFinishedCalledForHrefNavigations() throws Throwable {
doTestOnPageFinishedCalledForHrefNavigations(false);
}
@MediumTest
@Feature({"AndroidWebView"})
public void testOnPageFinishedCalledForHrefNavigationsWithBaseUrl() throws Throwable {
doTestOnPageFinishedCalledForHrefNavigations(true);
}
private void doTestOnPageFinishedCalledForHrefNavigations(boolean useBaseUrl) throws Throwable {
TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
mContentsClient.getOnPageFinishedHelper();
TestCallbackHelperContainer.OnPageStartedHelper onPageStartedHelper =
mContentsClient.getOnPageStartedHelper();
enableJavaScriptOnUiThread(mAwContents);
TestWebServer webServer = null;
try {
webServer = new TestWebServer(false);
final String testHtml = CommonResources.makeHtmlPageFrom("",
"<a href=\"#anchor\" id=\"link\">anchor</a>");
final String testPath = "/test.html";
final String testUrl = webServer.setResponse(testPath, testHtml, null);
if (useBaseUrl) {
loadDataWithBaseUrlSync(mAwContents, onPageFinishedHelper,
testHtml, "text/html", false, webServer.getBaseUrl(), null);
} else {
loadUrlSync(mAwContents, onPageFinishedHelper, testUrl);
}
int onPageFinishedCallCount = onPageFinishedHelper.getCallCount();
int onPageStartedCallCount = onPageStartedHelper.getCallCount();
JSUtils.clickOnLinkUsingJs(this, mAwContents,
mContentsClient.getOnEvaluateJavaScriptResultHelper(), "link");
onPageFinishedHelper.waitForCallback(onPageFinishedCallCount);
assertEquals(onPageStartedCallCount, onPageStartedHelper.getCallCount());
onPageFinishedCallCount = onPageFinishedHelper.getCallCount();
onPageStartedCallCount = onPageStartedHelper.getCallCount();
executeJavaScriptAndWaitForResult(mAwContents, mContentsClient,
"window.history.go(-1)");
onPageFinishedHelper.waitForCallback(onPageFinishedCallCount);
assertEquals(onPageStartedCallCount, onPageStartedHelper.getCallCount());
} finally {
if (webServer != null) webServer.shutdown();
}
}
}