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
3 // found in the LICENSE file.
5 package org.chromium.android_webview.test;
7 import android.test.suitebuilder.annotation.MediumTest;
9 import org.chromium.android_webview.AwContents;
10 import org.chromium.android_webview.test.util.CommonResources;
11 import org.chromium.android_webview.test.util.JSUtils;
12 import org.chromium.base.test.util.Feature;
13 import org.chromium.content.browser.test.util.TestCallbackHelperContainer;
14 import org.chromium.net.test.util.TestWebServer;
17 * Tests for the ContentViewClient.onPageFinished() method.
19 public class ClientOnPageFinishedTest extends AwTestBase {
21 private TestAwContentsClient mContentsClient;
22 private AwContents mAwContents;
25 public void setUp() throws Exception {
27 setTestAwContentsClient(new TestAwContentsClient());
30 private void setTestAwContentsClient(TestAwContentsClient contentsClient) throws Exception {
31 mContentsClient = contentsClient;
32 final AwTestContainerView testContainerView =
33 createAwTestContainerViewOnMainSync(mContentsClient);
34 mAwContents = testContainerView.getAwContents();
38 @Feature({"AndroidWebView"})
39 public void testOnPageFinishedPassesCorrectUrl() throws Throwable {
40 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
41 mContentsClient.getOnPageFinishedHelper();
43 String html = "<html><body>Simple page.</body></html>";
44 int currentCallCount = onPageFinishedHelper.getCallCount();
45 loadDataAsync(mAwContents, html, "text/html", false);
47 onPageFinishedHelper.waitForCallback(currentCallCount);
48 assertEquals("data:text/html," + html, onPageFinishedHelper.getUrl());
52 @Feature({"AndroidWebView"})
53 public void testOnPageFinishedCalledAfterError() throws Throwable {
54 class LocalTestClient extends TestAwContentsClient {
55 private boolean mIsOnReceivedErrorCalled = false;
56 private boolean mIsOnPageFinishedCalled = false;
57 private boolean mAllowAboutBlank = false;
60 public void onReceivedError(int errorCode, String description, String failingUrl) {
61 assertEquals("onReceivedError called twice for " + failingUrl,
62 false, mIsOnReceivedErrorCalled);
63 mIsOnReceivedErrorCalled = true;
64 assertEquals("onPageFinished called before onReceivedError for " + failingUrl,
65 false, mIsOnPageFinishedCalled);
66 super.onReceivedError(errorCode, description, failingUrl);
70 public void onPageFinished(String url) {
71 if (mAllowAboutBlank && "about:blank".equals(url)) {
72 super.onPageFinished(url);
75 assertEquals("onPageFinished called twice for " + url,
76 false, mIsOnPageFinishedCalled);
77 mIsOnPageFinishedCalled = true;
78 assertEquals("onReceivedError not called before onPageFinished for " + url,
79 true, mIsOnReceivedErrorCalled);
80 super.onPageFinished(url);
83 void setAllowAboutBlank() {
84 mAllowAboutBlank = true;
87 LocalTestClient testContentsClient = new LocalTestClient();
88 setTestAwContentsClient(testContentsClient);
90 TestCallbackHelperContainer.OnReceivedErrorHelper onReceivedErrorHelper =
91 mContentsClient.getOnReceivedErrorHelper();
92 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
93 mContentsClient.getOnPageFinishedHelper();
95 String invalidUrl = "http://localhost:7/non_existent";
96 loadUrlSync(mAwContents, onPageFinishedHelper, invalidUrl);
98 assertEquals(invalidUrl, onReceivedErrorHelper.getFailingUrl());
99 assertEquals(invalidUrl, onPageFinishedHelper.getUrl());
101 // Rather than wait a fixed time to see that another onPageFinished callback isn't issued
102 // we load a valid page. Since callbacks arrive sequentially, this will ensure that
103 // any extra calls of onPageFinished / onReceivedError will arrive to our client.
104 testContentsClient.setAllowAboutBlank();
105 loadUrlSync(mAwContents, onPageFinishedHelper, "about:blank");
109 @Feature({"AndroidWebView"})
110 public void testOnPageFinishedCalledAfterRedirectedUrlIsOverridden() throws Throwable {
112 * If url1 is redirected url2, and url2 load is overridden, onPageFinished should still be
115 * 1. load url1. url1 onPageStarted
116 * 2. server redirects url1 to url2. url2 onPageStarted
117 * 3. shouldOverridedUrlLoading called for url2 and returns true
118 * 4. url2 onPageFinishedCalled
121 TestWebServer webServer = TestWebServer.start();
123 final String redirectTargetPath = "/redirect_target.html";
124 final String redirectTargetUrl = webServer.setResponse(redirectTargetPath,
125 "<html><body>hello world</body></html>", null);
126 final String redirectUrl = webServer.setRedirect("/302.html", redirectTargetUrl);
128 final TestAwContentsClient.ShouldOverrideUrlLoadingHelper urlOverrideHelper =
129 mContentsClient.getShouldOverrideUrlLoadingHelper();
130 // Override the load of redirectTargetUrl
131 urlOverrideHelper.setShouldOverrideUrlLoadingReturnValue(true);
133 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
134 mContentsClient.getOnPageFinishedHelper();
136 final int currentOnPageFinishedCallCount = onPageFinishedHelper.getCallCount();
137 loadUrlAsync(mAwContents, redirectUrl);
139 onPageFinishedHelper.waitForCallback(currentOnPageFinishedCallCount);
140 // onPageFinished needs to be called for redirectTargetUrl, but not for redirectUrl
141 assertEquals(redirectTargetUrl, onPageFinishedHelper.getUrl());
143 webServer.shutdown();
148 @Feature({"AndroidWebView"})
149 public void testOnPageFinishedNotCalledForValidSubresources() throws Throwable {
150 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
151 mContentsClient.getOnPageFinishedHelper();
153 TestWebServer webServer = TestWebServer.start();
155 final String testHtml = "<html><head>Header</head><body>Body</body></html>";
156 final String testPath = "/test.html";
157 final String syncPath = "/sync.html";
159 final String testUrl = webServer.setResponse(testPath, testHtml, null);
160 final String syncUrl = webServer.setResponse(syncPath, testHtml, null);
162 assertEquals(0, onPageFinishedHelper.getCallCount());
163 final int pageWithSubresourcesCallCount = onPageFinishedHelper.getCallCount();
164 loadDataAsync(mAwContents,
165 "<html><iframe src=\"" + testUrl + "\" /></html>",
169 onPageFinishedHelper.waitForCallback(pageWithSubresourcesCallCount);
171 // Rather than wait a fixed time to see that an onPageFinished callback isn't issued
172 // we load another valid page. Since callbacks arrive sequentially if the next callback
173 // we get is for the synchronizationUrl we know that the previous load did not schedule
174 // a callback for the iframe.
175 final int synchronizationPageCallCount = onPageFinishedHelper.getCallCount();
176 loadUrlAsync(mAwContents, syncUrl);
178 onPageFinishedHelper.waitForCallback(synchronizationPageCallCount);
179 assertEquals(syncUrl, onPageFinishedHelper.getUrl());
180 assertEquals(2, onPageFinishedHelper.getCallCount());
182 webServer.shutdown();
187 @Feature({"AndroidWebView"})
188 public void testOnPageFinishedNotCalledForHistoryApi() throws Throwable {
189 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
190 mContentsClient.getOnPageFinishedHelper();
191 enableJavaScriptOnUiThread(mAwContents);
193 TestWebServer webServer = TestWebServer.start();
195 final String testHtml = "<html><head>Header</head><body>Body</body></html>";
196 final String testPath = "/test.html";
197 final String historyPath = "/history.html";
198 final String syncPath = "/sync.html";
200 final String testUrl = webServer.setResponse(testPath, testHtml, null);
201 final String historyUrl = webServer.getResponseUrl(historyPath);
202 final String syncUrl = webServer.setResponse(syncPath, testHtml, null);
204 assertEquals(0, onPageFinishedHelper.getCallCount());
205 loadUrlSync(mAwContents, onPageFinishedHelper, testUrl);
207 executeJavaScriptAndWaitForResult(mAwContents, mContentsClient,
208 "history.pushState(null, null, '" + historyUrl + "');");
210 // Rather than wait a fixed time to see that an onPageFinished callback isn't issued
211 // we load another valid page. Since callbacks arrive sequentially if the next callback
212 // we get is for the synchronizationUrl we know that the previous load did not schedule
213 // a callback for the iframe.
214 final int synchronizationPageCallCount = onPageFinishedHelper.getCallCount();
215 loadUrlAsync(mAwContents, syncUrl);
217 onPageFinishedHelper.waitForCallback(synchronizationPageCallCount);
218 assertEquals(syncUrl, onPageFinishedHelper.getUrl());
219 assertEquals(2, onPageFinishedHelper.getCallCount());
221 webServer.shutdown();
226 @Feature({"AndroidWebView"})
227 public void testOnPageFinishedCalledForHrefNavigations() throws Throwable {
228 doTestOnPageFinishedCalledForHrefNavigations(false);
232 @Feature({"AndroidWebView"})
233 public void testOnPageFinishedCalledForHrefNavigationsWithBaseUrl() throws Throwable {
234 doTestOnPageFinishedCalledForHrefNavigations(true);
237 private void doTestOnPageFinishedCalledForHrefNavigations(boolean useBaseUrl) throws Throwable {
238 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
239 mContentsClient.getOnPageFinishedHelper();
240 TestCallbackHelperContainer.OnPageStartedHelper onPageStartedHelper =
241 mContentsClient.getOnPageStartedHelper();
242 enableJavaScriptOnUiThread(mAwContents);
244 TestWebServer webServer = TestWebServer.start();
246 final String testHtml = CommonResources.makeHtmlPageFrom("",
247 "<a href=\"#anchor\" id=\"link\">anchor</a>");
248 final String testPath = "/test.html";
249 final String testUrl = webServer.setResponse(testPath, testHtml, null);
252 loadDataWithBaseUrlSync(mAwContents, onPageFinishedHelper,
253 testHtml, "text/html", false, webServer.getBaseUrl(), null);
255 loadUrlSync(mAwContents, onPageFinishedHelper, testUrl);
258 int onPageFinishedCallCount = onPageFinishedHelper.getCallCount();
259 int onPageStartedCallCount = onPageStartedHelper.getCallCount();
261 JSUtils.clickOnLinkUsingJs(this, mAwContents,
262 mContentsClient.getOnEvaluateJavaScriptResultHelper(), "link");
264 onPageFinishedHelper.waitForCallback(onPageFinishedCallCount);
265 assertEquals(onPageStartedCallCount, onPageStartedHelper.getCallCount());
267 onPageFinishedCallCount = onPageFinishedHelper.getCallCount();
268 onPageStartedCallCount = onPageStartedHelper.getCallCount();
270 executeJavaScriptAndWaitForResult(mAwContents, mContentsClient,
271 "window.history.go(-1)");
273 onPageFinishedHelper.waitForCallback(onPageFinishedCallCount);
274 assertEquals(onPageStartedCallCount, onPageStartedHelper.getCallCount());
276 webServer.shutdown();