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.xwalk.core.xwview.test;
7 import android.test.suitebuilder.annotation.MediumTest;
9 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout;
11 import org.chromium.base.test.util.Feature;
12 import org.chromium.content.browser.test.util.TestCallbackHelperContainer;
13 import org.chromium.net.test.util.TestWebServer;
15 import org.xwalk.core.XWalkView;
16 import org.xwalk.core.internal.XWalkClient;
18 import java.util.concurrent.TimeUnit;
21 * Tests for the XWalkClient.onPageFinished() method.
23 public class OnPageFinishedTest extends XWalkViewTestBase {
24 private static final long WAIT_TIMEOUT_MS = scaleTimeout(2000);
27 public void setUp() throws Exception {
32 @Feature({"OnPageFinishedTest"})
33 public void testOnPageFinishedPassesCorrectUrl() throws Throwable {
34 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
35 mTestHelperBridge.getOnPageFinishedHelper();
37 String html = "<html><body>Simple page.</body></html>";
38 int currentCallCount = onPageFinishedHelper.getCallCount();
39 loadDataAsync(null, html, "text/html", false);
41 onPageFinishedHelper.waitForCallback(currentCallCount);
42 assertEquals("about:blank", onPageFinishedHelper.getUrl());
46 @Feature({"OnPageFinishedTest"})
47 public void testOnPageFinishedCalledAfterError() throws Throwable {
48 TestCallbackHelperContainer.OnReceivedErrorHelper onReceivedErrorHelper =
49 mTestHelperBridge.getOnReceivedErrorHelper();
50 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
51 mTestHelperBridge.getOnPageFinishedHelper();
53 assertEquals(0, onReceivedErrorHelper.getCallCount());
55 String url = "http://localhost:7/non_existent";
56 int onReceivedErrorCallCount = onReceivedErrorHelper.getCallCount();
57 int onPageFinishedCallCount = onPageFinishedHelper.getCallCount();
60 onReceivedErrorHelper.waitForCallback(onReceivedErrorCallCount,
61 1 /* numberOfCallsToWaitFor */,
63 TimeUnit.MILLISECONDS);
64 onPageFinishedHelper.waitForCallback(onPageFinishedCallCount,
65 1 /* numberOfCallsToWaitFor */,
67 TimeUnit.MILLISECONDS);
68 assertEquals(1, onReceivedErrorHelper.getCallCount());
72 @Feature({"OnPageFinishedTest"})
73 public void testOnPageFinishedNotCalledForValidSubresources() throws Throwable {
74 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
75 mTestHelperBridge.getOnPageFinishedHelper();
77 TestWebServer webServer = TestWebServer.start();
80 final String testHtml = "<html><head>Header</head><body>Body</body></html>";
81 final String testPath = "/test.html";
82 final String syncPath = "/sync.html";
84 final String testUrl = webServer.setResponse(testPath, testHtml, null);
85 final String syncUrl = webServer.setResponse(syncPath, testHtml, null);
87 assertEquals(0, onPageFinishedHelper.getCallCount());
88 final int pageWithSubresourcesCallCount = onPageFinishedHelper.getCallCount();
89 loadDataAsync(null, "<html><iframe src=\"" + testUrl + "\" /></html>",
93 onPageFinishedHelper.waitForCallback(pageWithSubresourcesCallCount);
95 // Rather than wait a fixed time to see that an onPageFinished callback isn't issued
96 // we load another valid page. Since callbacks arrive sequentially if the next callback
97 // we get is for the synchronizationUrl we know that the previous load did not schedule
98 // a callback for the iframe.
99 final int synchronizationPageCallCount = onPageFinishedHelper.getCallCount();
100 loadUrlAsync(syncUrl);
102 onPageFinishedHelper.waitForCallback(synchronizationPageCallCount);
103 assertEquals(syncUrl, onPageFinishedHelper.getUrl());
104 assertEquals(2, onPageFinishedHelper.getCallCount());
106 webServer.shutdown();
111 @Feature({"OnPageFinishedTest"})
112 public void testOnPageFinishedNotCalledForJavaScriptUrl() throws Throwable {
113 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
114 mTestHelperBridge.getOnPageFinishedHelper();
116 String html = "<html><body>Simple page.</body></html>";
117 int currentCallCount = onPageFinishedHelper.getCallCount();
118 assertEquals(0, currentCallCount);
120 loadDataAsync(null, html, "text/html", false);
121 loadJavaScriptUrl("javascript: try { console.log('foo'); } catch(e) {};");
123 onPageFinishedHelper.waitForCallback(currentCallCount);
124 assertEquals("about:blank", onPageFinishedHelper.getUrl());
125 // onPageFinished won't be called for javascript: url.
126 assertEquals(1, onPageFinishedHelper.getCallCount());