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 {
30 setXWalkClient(new XWalkViewTestBase.TestXWalkClient());
31 setResourceClient(new XWalkViewTestBase.TestXWalkResourceClient());
35 @Feature({"OnPageFinishedTest"})
36 public void testOnPageFinishedPassesCorrectUrl() throws Throwable {
37 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
38 mTestHelperBridge.getOnPageFinishedHelper();
40 String html = "<html><body>Simple page.</body></html>";
41 int currentCallCount = onPageFinishedHelper.getCallCount();
42 loadDataAsync(null, html, "text/html", false);
44 onPageFinishedHelper.waitForCallback(currentCallCount);
45 assertEquals("about:blank", onPageFinishedHelper.getUrl());
49 @Feature({"OnPageFinishedTest"})
50 public void testOnPageFinishedCalledAfterError() throws Throwable {
51 TestCallbackHelperContainer.OnReceivedErrorHelper onReceivedErrorHelper =
52 mTestHelperBridge.getOnReceivedErrorHelper();
53 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
54 mTestHelperBridge.getOnPageFinishedHelper();
56 assertEquals(0, onReceivedErrorHelper.getCallCount());
58 String url = "http://localhost:7/non_existent";
59 int onReceivedErrorCallCount = onReceivedErrorHelper.getCallCount();
60 int onPageFinishedCallCount = onPageFinishedHelper.getCallCount();
63 onReceivedErrorHelper.waitForCallback(onReceivedErrorCallCount,
64 1 /* numberOfCallsToWaitFor */,
66 TimeUnit.MILLISECONDS);
67 onPageFinishedHelper.waitForCallback(onPageFinishedCallCount,
68 1 /* numberOfCallsToWaitFor */,
70 TimeUnit.MILLISECONDS);
71 assertEquals(1, onReceivedErrorHelper.getCallCount());
75 @Feature({"OnPageFinishedTest"})
76 public void testOnPageFinishedNotCalledForValidSubresources() throws Throwable {
77 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
78 mTestHelperBridge.getOnPageFinishedHelper();
80 TestWebServer webServer = null;
82 webServer = new TestWebServer(false);
84 final String testHtml = "<html><head>Header</head><body>Body</body></html>";
85 final String testPath = "/test.html";
86 final String syncPath = "/sync.html";
88 final String testUrl = webServer.setResponse(testPath, testHtml, null);
89 final String syncUrl = webServer.setResponse(syncPath, testHtml, null);
91 assertEquals(0, onPageFinishedHelper.getCallCount());
92 final int pageWithSubresourcesCallCount = onPageFinishedHelper.getCallCount();
93 loadDataAsync(null, "<html><iframe src=\"" + testUrl + "\" /></html>",
97 onPageFinishedHelper.waitForCallback(pageWithSubresourcesCallCount);
99 // Rather than wait a fixed time to see that an onPageFinished callback isn't issued
100 // we load another valid page. Since callbacks arrive sequentially if the next callback
101 // we get is for the synchronizationUrl we know that the previous load did not schedule
102 // a callback for the iframe.
103 final int synchronizationPageCallCount = onPageFinishedHelper.getCallCount();
104 loadUrlAsync(syncUrl);
106 onPageFinishedHelper.waitForCallback(synchronizationPageCallCount);
107 assertEquals(syncUrl, onPageFinishedHelper.getUrl());
108 assertEquals(2, onPageFinishedHelper.getCallCount());
110 if (webServer != null) webServer.shutdown();
115 @Feature({"OnPageFinishedTest"})
116 public void testOnPageFinishedNotCalledForJavaScriptUrl() throws Throwable {
117 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
118 mTestHelperBridge.getOnPageFinishedHelper();
120 String html = "<html><body>Simple page.</body></html>";
121 int currentCallCount = onPageFinishedHelper.getCallCount();
122 assertEquals(0, currentCallCount);
124 loadDataAsync(null, html, "text/html", false);
125 loadJavaScriptUrl("javascript: try { console.log('foo'); } catch(e) {};");
127 onPageFinishedHelper.waitForCallback(currentCallCount);
128 assertEquals("about:blank", onPageFinishedHelper.getUrl());
129 // onPageFinished won't be called for javascript: url.
130 assertEquals(1, onPageFinishedHelper.getCallCount());