initial import
[vuplus_webkit] / Source / WebKit / chromium / tests / WebFrameTest.cpp
1 /*
2  * Copyright (C) 2010 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32
33 #include "ResourceError.h"
34 #include "WebDocument.h"
35 #include "WebFormElement.h"
36 #include "WebFrame.h"
37 #include "WebFrameClient.h"
38 #include "WebSearchableFormData.h"
39 #include "WebSecurityPolicy.h"
40 #include "WebSettings.h"
41 #include "WebString.h"
42 #include "WebURL.h"
43 #include "WebURLRequest.h"
44 #include "WebURLResponse.h"
45 #include "WebView.h"
46 #include "v8.h"
47 #include <googleurl/src/gurl.h>
48 #include <gtest/gtest.h>
49 #include <webkit/support/webkit_support.h>
50
51 using namespace WebKit;
52
53 namespace {
54
55 class WebFrameTest : public testing::Test {
56 public:
57     WebFrameTest()
58         : baseURL("http://www.test.com/"),
59           chromeURL("chrome://")
60     {
61     }
62
63     virtual void TearDown()
64     {
65         webkit_support::UnregisterAllMockedURLs();
66     }
67
68     void registerMockedHttpURLLoad(const std::string& fileName)
69     {
70         registerMockedURLLoad(baseURL, fileName);
71     }
72
73     void registerMockedChromeURLLoad(const std::string& fileName)
74     {
75         registerMockedURLLoad(chromeURL, fileName);
76     }
77
78     void serveRequests()
79     {
80         webkit_support::ServeAsynchronousMockedRequests();
81     }
82
83     void loadHttpFrame(WebFrame* frame, const std::string& fileName)
84     {
85         loadFrame(frame, baseURL, fileName);
86     }
87
88     void loadChromeFrame(WebFrame* frame, const std::string& fileName)
89     {
90         loadFrame(frame, chromeURL, fileName);
91     }
92
93     void registerMockedURLLoad(const std::string& base, const std::string& fileName)
94     {
95         WebURLResponse response;
96         response.initialize();
97         response.setMIMEType("text/html");
98
99         std::string filePath = webkit_support::GetWebKitRootDir().utf8();
100         filePath += "/Source/WebKit/chromium/tests/data/";
101         filePath += fileName;
102
103         webkit_support::RegisterMockedURL(WebURL(GURL(base + fileName)), response, WebString::fromUTF8(filePath));
104     }
105
106     void loadFrame(WebFrame* frame, const std::string& base, const std::string& fileName)
107     {
108         WebURLRequest urlRequest;
109         urlRequest.initialize();
110         urlRequest.setURL(WebURL(GURL(base + fileName)));
111         frame->loadRequest(urlRequest);
112     }
113
114 protected:
115     std::string baseURL;
116     std::string chromeURL;
117 };
118
119 class TestWebFrameClient : public WebFrameClient {
120 };
121
122 TEST_F(WebFrameTest, ContentText)
123 {
124     registerMockedHttpURLLoad("iframes_test.html");
125     registerMockedHttpURLLoad("visible_iframe.html");
126     registerMockedHttpURLLoad("invisible_iframe.html");
127     registerMockedHttpURLLoad("zero_sized_iframe.html");
128
129     // Create and initialize the WebView.
130     TestWebFrameClient webFrameClient;
131     WebView* webView = WebView::create(0);
132     webView->initializeMainFrame(&webFrameClient);
133
134     loadHttpFrame(webView->mainFrame(), "iframes_test.html");
135     serveRequests();
136
137     // Now retrieve the frames text and test it only includes visible elements.
138     std::string content = webView->mainFrame()->contentAsText(1024).utf8();
139     EXPECT_NE(std::string::npos, content.find(" visible paragraph"));
140     EXPECT_NE(std::string::npos, content.find(" visible iframe"));
141     EXPECT_EQ(std::string::npos, content.find(" invisible pararaph"));
142     EXPECT_EQ(std::string::npos, content.find(" invisible iframe"));
143     EXPECT_EQ(std::string::npos, content.find("iframe with zero size"));
144
145     webView->close();
146 }
147
148 TEST_F(WebFrameTest, FrameForEnteredContext)
149 {
150     registerMockedHttpURLLoad("iframes_test.html");
151     registerMockedHttpURLLoad("visible_iframe.html");
152     registerMockedHttpURLLoad("invisible_iframe.html");
153     registerMockedHttpURLLoad("zero_sized_iframe.html");
154
155     // Create and initialize the WebView.
156      TestWebFrameClient webFrameClient;
157     WebView* webView = WebView::create(0);
158     webView->settings()->setJavaScriptEnabled(true);
159     webView->initializeMainFrame(&webFrameClient);
160
161     loadHttpFrame(webView->mainFrame(), "iframes_test.html");
162     serveRequests();
163
164     v8::HandleScope scope;
165     EXPECT_EQ(webView->mainFrame(),
166               WebFrame::frameForContext(
167                   webView->mainFrame()->mainWorldScriptContext()));
168     EXPECT_EQ(webView->mainFrame()->firstChild(),
169               WebFrame::frameForContext(
170                   webView->mainFrame()->firstChild()->mainWorldScriptContext()));
171
172     webView->close();
173 }
174
175 TEST_F(WebFrameTest, FormWithNullFrame)
176 {
177     registerMockedHttpURLLoad("form.html");
178
179     TestWebFrameClient webFrameClient;
180     WebView* webView = WebView::create(0);
181     webView->initializeMainFrame(&webFrameClient);
182
183     loadHttpFrame(webView->mainFrame(), "form.html");
184     serveRequests();
185
186     WebVector<WebFormElement> forms;
187     webView->mainFrame()->document().forms(forms);
188     webView->close();
189
190     EXPECT_EQ(forms.size(), 1U);
191
192     // This test passes if this doesn't crash.
193     WebSearchableFormData searchableDataForm(forms[0]);
194 }
195
196 TEST_F(WebFrameTest, ChromePageNoJavascript)
197 {
198     registerMockedChromeURLLoad("history.html");
199
200     // Create and initialize the WebView.
201     TestWebFrameClient webFrameClient;
202     WebView* webView = WebView::create(0);
203     webView->settings()->setJavaScriptEnabled(true);
204     webView->initializeMainFrame(&webFrameClient);
205
206     loadChromeFrame(webView->mainFrame(), "history.html");
207     serveRequests();
208
209     // Try to run JS against the chrome-style URL.
210     WebSecurityPolicy::registerURLSchemeAsNotAllowingJavascriptURLs("chrome");
211     loadFrame(webView->mainFrame(), "javascript:", "document.body.appendChild(document.createTextNode('Clobbered'))");
212
213     // Now retrieve the frames text and see if it was clobbered.
214     std::string content = webView->mainFrame()->contentAsText(1024).utf8();
215     EXPECT_NE(std::string::npos, content.find("Simulated Chromium History Page"));
216     EXPECT_EQ(std::string::npos, content.find("Clobbered"));
217 }
218
219 class TestReloadDoesntRedirectWebFrameClient : public WebFrameClient {
220 public:
221     virtual WebNavigationPolicy decidePolicyForNavigation(
222         WebFrame*, const WebURLRequest&, WebNavigationType,
223         const WebNode& originatingNode,
224         WebNavigationPolicy defaultPolicy, bool isRedirect)
225     {
226         EXPECT_EQ(false, isRedirect);
227         return WebNavigationPolicyCurrentTab;
228     }
229
230     virtual WebURLError cancelledError(WebFrame*, const WebURLRequest& request)
231     {
232         // Return a dummy error so the DocumentLoader doesn't assert when
233         // the reload cancels it.
234         return WebURLError(WebCore::ResourceError("", 1, "", "cancelled"));
235     }
236 };
237
238 TEST_F(WebFrameTest, ReloadDoesntSetRedirect)
239 {
240     // Test for case in http://crbug.com/73104. Reloading a frame very quickly
241     // would sometimes call decidePolicyForNavigation with isRedirect=true
242     registerMockedHttpURLLoad("form.html");
243
244     TestReloadDoesntRedirectWebFrameClient webFrameClient;
245     WebView* webView = WebView::create(0);
246     webView->initializeMainFrame(&webFrameClient);
247
248     loadHttpFrame(webView->mainFrame(), "form.html");
249     serveRequests();
250     // Frame is loaded.
251
252     webView->mainFrame()->reload(true);
253     // start reload before request is delivered.
254     webView->mainFrame()->reload(true);
255     serveRequests();
256 }
257
258 } // namespace