Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / android_webview / javatests / src / org / chromium / android_webview / test / ClientOnPageFinishedTest.java
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.
4
5 package org.chromium.android_webview.test;
6
7 import android.test.suitebuilder.annotation.MediumTest;
8
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;
15
16 /**
17  * Tests for the ContentViewClient.onPageFinished() method.
18  */
19 public class ClientOnPageFinishedTest extends AwTestBase {
20
21     private TestAwContentsClient mContentsClient;
22     private AwContents mAwContents;
23
24     @Override
25     public void setUp() throws Exception {
26         super.setUp();
27         setTestAwContentsClient(new TestAwContentsClient());
28     }
29
30     private void setTestAwContentsClient(TestAwContentsClient contentsClient) throws Exception {
31         mContentsClient = contentsClient;
32         final AwTestContainerView testContainerView =
33                 createAwTestContainerViewOnMainSync(mContentsClient);
34         mAwContents = testContainerView.getAwContents();
35     }
36
37     @MediumTest
38     @Feature({"AndroidWebView"})
39     public void testOnPageFinishedPassesCorrectUrl() throws Throwable {
40         TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
41                 mContentsClient.getOnPageFinishedHelper();
42
43         String html = "<html><body>Simple page.</body></html>";
44         int currentCallCount = onPageFinishedHelper.getCallCount();
45         loadDataAsync(mAwContents, html, "text/html", false);
46
47         onPageFinishedHelper.waitForCallback(currentCallCount);
48         assertEquals("data:text/html," + html, onPageFinishedHelper.getUrl());
49     }
50
51     @MediumTest
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;
58
59             @Override
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);
67             }
68
69             @Override
70             public void onPageFinished(String url) {
71                 if (mAllowAboutBlank && "about:blank".equals(url)) {
72                     super.onPageFinished(url);
73                     return;
74                 }
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);
81             }
82
83             void setAllowAboutBlank() {
84                 mAllowAboutBlank = true;
85             }
86         }
87         LocalTestClient testContentsClient = new LocalTestClient();
88         setTestAwContentsClient(testContentsClient);
89
90         TestCallbackHelperContainer.OnReceivedErrorHelper onReceivedErrorHelper =
91                 mContentsClient.getOnReceivedErrorHelper();
92         TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
93                 mContentsClient.getOnPageFinishedHelper();
94
95         String invalidUrl = "http://localhost:7/non_existent";
96         loadUrlSync(mAwContents, onPageFinishedHelper, invalidUrl);
97
98         assertEquals(invalidUrl, onReceivedErrorHelper.getFailingUrl());
99         assertEquals(invalidUrl, onPageFinishedHelper.getUrl());
100
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");
106     }
107
108     @MediumTest
109     @Feature({"AndroidWebView"})
110     public void testOnPageFinishedCalledAfterRedirectedUrlIsOverridden() throws Throwable {
111         /*
112          * If url1 is redirected url2, and url2 load is overridden, onPageFinished should still be
113          * called for url2.
114          * Steps:
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
119          */
120
121         TestWebServer webServer = TestWebServer.start();
122         try {
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);
127
128             final TestAwContentsClient.ShouldOverrideUrlLoadingHelper urlOverrideHelper =
129                     mContentsClient.getShouldOverrideUrlLoadingHelper();
130             // Override the load of redirectTargetUrl
131             urlOverrideHelper.setShouldOverrideUrlLoadingReturnValue(true);
132
133             TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
134                     mContentsClient.getOnPageFinishedHelper();
135
136             final int currentOnPageFinishedCallCount = onPageFinishedHelper.getCallCount();
137             loadUrlAsync(mAwContents, redirectUrl);
138
139             onPageFinishedHelper.waitForCallback(currentOnPageFinishedCallCount);
140             // onPageFinished needs to be called for redirectTargetUrl, but not for redirectUrl
141             assertEquals(redirectTargetUrl, onPageFinishedHelper.getUrl());
142         } finally {
143             webServer.shutdown();
144         }
145     }
146
147     @MediumTest
148     @Feature({"AndroidWebView"})
149     public void testOnPageFinishedNotCalledForValidSubresources() throws Throwable {
150         TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
151                 mContentsClient.getOnPageFinishedHelper();
152
153         TestWebServer webServer = TestWebServer.start();
154         try {
155             final String testHtml = "<html><head>Header</head><body>Body</body></html>";
156             final String testPath = "/test.html";
157             final String syncPath = "/sync.html";
158
159             final String testUrl = webServer.setResponse(testPath, testHtml, null);
160             final String syncUrl = webServer.setResponse(syncPath, testHtml, null);
161
162             assertEquals(0, onPageFinishedHelper.getCallCount());
163             final int pageWithSubresourcesCallCount = onPageFinishedHelper.getCallCount();
164             loadDataAsync(mAwContents,
165                           "<html><iframe src=\"" + testUrl + "\" /></html>",
166                           "text/html",
167                           false);
168
169             onPageFinishedHelper.waitForCallback(pageWithSubresourcesCallCount);
170
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);
177
178             onPageFinishedHelper.waitForCallback(synchronizationPageCallCount);
179             assertEquals(syncUrl, onPageFinishedHelper.getUrl());
180             assertEquals(2, onPageFinishedHelper.getCallCount());
181         } finally {
182             webServer.shutdown();
183         }
184     }
185
186     @MediumTest
187     @Feature({"AndroidWebView"})
188     public void testOnPageFinishedNotCalledForHistoryApi() throws Throwable {
189         TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
190                 mContentsClient.getOnPageFinishedHelper();
191         enableJavaScriptOnUiThread(mAwContents);
192
193         TestWebServer webServer = TestWebServer.start();
194         try {
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";
199
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);
203
204             assertEquals(0, onPageFinishedHelper.getCallCount());
205             loadUrlSync(mAwContents, onPageFinishedHelper, testUrl);
206
207             executeJavaScriptAndWaitForResult(mAwContents, mContentsClient,
208                     "history.pushState(null, null, '" + historyUrl + "');");
209
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);
216
217             onPageFinishedHelper.waitForCallback(synchronizationPageCallCount);
218             assertEquals(syncUrl, onPageFinishedHelper.getUrl());
219             assertEquals(2, onPageFinishedHelper.getCallCount());
220         } finally {
221             webServer.shutdown();
222         }
223     }
224
225     @MediumTest
226     @Feature({"AndroidWebView"})
227     public void testOnPageFinishedCalledForHrefNavigations() throws Throwable {
228         doTestOnPageFinishedCalledForHrefNavigations(false);
229     }
230
231     @MediumTest
232     @Feature({"AndroidWebView"})
233     public void testOnPageFinishedCalledForHrefNavigationsWithBaseUrl() throws Throwable {
234         doTestOnPageFinishedCalledForHrefNavigations(true);
235     }
236
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);
243
244         TestWebServer webServer = TestWebServer.start();
245         try {
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);
250
251             if (useBaseUrl) {
252                 loadDataWithBaseUrlSync(mAwContents, onPageFinishedHelper,
253                         testHtml, "text/html", false, webServer.getBaseUrl(), null);
254             } else {
255                 loadUrlSync(mAwContents, onPageFinishedHelper, testUrl);
256             }
257
258             int onPageFinishedCallCount = onPageFinishedHelper.getCallCount();
259             int onPageStartedCallCount = onPageStartedHelper.getCallCount();
260
261             JSUtils.clickOnLinkUsingJs(this, mAwContents,
262                     mContentsClient.getOnEvaluateJavaScriptResultHelper(), "link");
263
264             onPageFinishedHelper.waitForCallback(onPageFinishedCallCount);
265             assertEquals(onPageStartedCallCount, onPageStartedHelper.getCallCount());
266
267             onPageFinishedCallCount = onPageFinishedHelper.getCallCount();
268             onPageStartedCallCount = onPageStartedHelper.getCallCount();
269
270             executeJavaScriptAndWaitForResult(mAwContents, mContentsClient,
271                     "window.history.go(-1)");
272
273             onPageFinishedHelper.waitForCallback(onPageFinishedCallCount);
274             assertEquals(onPageStartedCallCount, onPageStartedHelper.getCallCount());
275         } finally {
276             webServer.shutdown();
277         }
278     }
279 }