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 = null;
79 webServer = new TestWebServer(false);
81 final String testHtml = "<html><head>Header</head><body>Body</body></html>";
82 final String testPath = "/test.html";
83 final String syncPath = "/sync.html";
85 final String testUrl = webServer.setResponse(testPath, testHtml, null);
86 final String syncUrl = webServer.setResponse(syncPath, testHtml, null);
88 assertEquals(0, onPageFinishedHelper.getCallCount());
89 final int pageWithSubresourcesCallCount = onPageFinishedHelper.getCallCount();
90 loadDataAsync(null, "<html><iframe src=\"" + testUrl + "\" /></html>",
94 onPageFinishedHelper.waitForCallback(pageWithSubresourcesCallCount);
96 // Rather than wait a fixed time to see that an onPageFinished callback isn't issued
97 // we load another valid page. Since callbacks arrive sequentially if the next callback
98 // we get is for the synchronizationUrl we know that the previous load did not schedule
99 // a callback for the iframe.
100 final int synchronizationPageCallCount = onPageFinishedHelper.getCallCount();
101 loadUrlAsync(syncUrl);
103 onPageFinishedHelper.waitForCallback(synchronizationPageCallCount);
104 assertEquals(syncUrl, onPageFinishedHelper.getUrl());
105 assertEquals(2, onPageFinishedHelper.getCallCount());
107 if (webServer != null) webServer.shutdown();
112 @Feature({"OnPageFinishedTest"})
113 public void testOnPageFinishedNotCalledForJavaScriptUrl() throws Throwable {
114 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
115 mTestHelperBridge.getOnPageFinishedHelper();
117 String html = "<html><body>Simple page.</body></html>";
118 int currentCallCount = onPageFinishedHelper.getCallCount();
119 assertEquals(0, currentCallCount);
121 loadDataAsync(null, html, "text/html", false);
122 loadJavaScriptUrl("javascript: try { console.log('foo'); } catch(e) {};");
124 onPageFinishedHelper.waitForCallback(currentCallCount);
125 assertEquals("about:blank", onPageFinishedHelper.getUrl());
126 // onPageFinished won't be called for javascript: url.
127 assertEquals(1, onPageFinishedHelper.getCallCount());