Upstream version 8.36.169.0
[platform/framework/web/crosswalk.git] / src / xwalk / test / android / core / javatests / src / org / xwalk / core / xwview / test / OnPageFinishedTest.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.xwalk.core.xwview.test;
6
7 import android.test.suitebuilder.annotation.MediumTest;
8
9 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout;
10
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;
14
15 import org.xwalk.core.XWalkView;
16 import org.xwalk.core.internal.XWalkClient;
17
18 import java.util.concurrent.TimeUnit;
19
20 /**
21  * Tests for the XWalkClient.onPageFinished() method.
22  */
23 public class OnPageFinishedTest extends XWalkViewTestBase {
24     private static final long WAIT_TIMEOUT_MS = scaleTimeout(2000);
25
26     @Override
27     public void setUp() throws Exception {
28         super.setUp();
29     }
30
31     @MediumTest
32     @Feature({"OnPageFinishedTest"})
33     public void testOnPageFinishedPassesCorrectUrl() throws Throwable {
34         TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
35                 mTestHelperBridge.getOnPageFinishedHelper();
36
37         String html = "<html><body>Simple page.</body></html>";
38         int currentCallCount = onPageFinishedHelper.getCallCount();
39         loadDataAsync(null, html, "text/html", false);
40
41         onPageFinishedHelper.waitForCallback(currentCallCount);
42         assertEquals("about:blank", onPageFinishedHelper.getUrl());
43     }
44
45     @MediumTest
46     @Feature({"OnPageFinishedTest"})
47     public void testOnPageFinishedCalledAfterError() throws Throwable {
48         TestCallbackHelperContainer.OnReceivedErrorHelper onReceivedErrorHelper =
49                 mTestHelperBridge.getOnReceivedErrorHelper();
50         TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
51                 mTestHelperBridge.getOnPageFinishedHelper();
52
53         assertEquals(0, onReceivedErrorHelper.getCallCount());
54
55         String url = "http://localhost:7/non_existent";
56         int onReceivedErrorCallCount = onReceivedErrorHelper.getCallCount();
57         int onPageFinishedCallCount = onPageFinishedHelper.getCallCount();
58         loadUrlAsync(url);
59
60         onReceivedErrorHelper.waitForCallback(onReceivedErrorCallCount,
61                                               1 /* numberOfCallsToWaitFor */,
62                                               WAIT_TIMEOUT_MS,
63                                               TimeUnit.MILLISECONDS);
64         onPageFinishedHelper.waitForCallback(onPageFinishedCallCount,
65                                              1 /* numberOfCallsToWaitFor */,
66                                              WAIT_TIMEOUT_MS,
67                                              TimeUnit.MILLISECONDS);
68         assertEquals(1, onReceivedErrorHelper.getCallCount());
69     }
70
71     @MediumTest
72     @Feature({"OnPageFinishedTest"})
73     public void testOnPageFinishedNotCalledForValidSubresources() throws Throwable {
74         TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
75                 mTestHelperBridge.getOnPageFinishedHelper();
76
77         TestWebServer webServer = null;
78         try {
79             webServer = new TestWebServer(false);
80
81             final String testHtml = "<html><head>Header</head><body>Body</body></html>";
82             final String testPath = "/test.html";
83             final String syncPath = "/sync.html";
84
85             final String testUrl = webServer.setResponse(testPath, testHtml, null);
86             final String syncUrl = webServer.setResponse(syncPath, testHtml, null);
87
88             assertEquals(0, onPageFinishedHelper.getCallCount());
89             final int pageWithSubresourcesCallCount = onPageFinishedHelper.getCallCount();
90             loadDataAsync(null, "<html><iframe src=\"" + testUrl + "\" /></html>",
91                           "text/html",
92                           false);
93
94             onPageFinishedHelper.waitForCallback(pageWithSubresourcesCallCount);
95
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);
102
103             onPageFinishedHelper.waitForCallback(synchronizationPageCallCount);
104             assertEquals(syncUrl, onPageFinishedHelper.getUrl());
105             assertEquals(2, onPageFinishedHelper.getCallCount());
106         } finally {
107             if (webServer != null) webServer.shutdown();
108         }
109     }
110
111     @MediumTest
112     @Feature({"OnPageFinishedTest"})
113     public void testOnPageFinishedNotCalledForJavaScriptUrl() throws Throwable {
114         TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
115                 mTestHelperBridge.getOnPageFinishedHelper();
116
117         String html = "<html><body>Simple page.</body></html>";
118         int currentCallCount = onPageFinishedHelper.getCallCount();
119         assertEquals(0, currentCallCount);
120
121         loadDataAsync(null, html, "text/html", false);
122         loadJavaScriptUrl("javascript: try { console.log('foo'); } catch(e) {};");
123
124         onPageFinishedHelper.waitForCallback(currentCallCount);
125         assertEquals("about:blank", onPageFinishedHelper.getUrl());
126         // onPageFinished won't be called for javascript: url.
127         assertEquals(1, onPageFinishedHelper.getCallCount());
128     }
129 }