Upstream version 11.40.277.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 = TestWebServer.start();
78         try {
79
80             final String testHtml = "<html><head>Header</head><body>Body</body></html>";
81             final String testPath = "/test.html";
82             final String syncPath = "/sync.html";
83
84             final String testUrl = webServer.setResponse(testPath, testHtml, null);
85             final String syncUrl = webServer.setResponse(syncPath, testHtml, null);
86
87             assertEquals(0, onPageFinishedHelper.getCallCount());
88             final int pageWithSubresourcesCallCount = onPageFinishedHelper.getCallCount();
89             loadDataAsync(null, "<html><iframe src=\"" + testUrl + "\" /></html>",
90                           "text/html",
91                           false);
92
93             onPageFinishedHelper.waitForCallback(pageWithSubresourcesCallCount);
94
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);
101
102             onPageFinishedHelper.waitForCallback(synchronizationPageCallCount);
103             assertEquals(syncUrl, onPageFinishedHelper.getUrl());
104             assertEquals(2, onPageFinishedHelper.getCallCount());
105         } finally {
106             webServer.shutdown();
107         }
108     }
109
110     @MediumTest
111     @Feature({"OnPageFinishedTest"})
112     public void testOnPageFinishedNotCalledForJavaScriptUrl() throws Throwable {
113         TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
114                 mTestHelperBridge.getOnPageFinishedHelper();
115
116         String html = "<html><body>Simple page.</body></html>";
117         int currentCallCount = onPageFinishedHelper.getCallCount();
118         assertEquals(0, currentCallCount);
119
120         loadDataAsync(null, html, "text/html", false);
121         loadJavaScriptUrl("javascript: try { console.log('foo'); } catch(e) {};");
122
123         onPageFinishedHelper.waitForCallback(currentCallCount);
124         assertEquals("about:blank", onPageFinishedHelper.getUrl());
125         // onPageFinished won't be called for javascript: url.
126         assertEquals(1, onPageFinishedHelper.getCallCount());
127     }
128 }