2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
33 #include "ResourceError.h"
34 #include "WebDocument.h"
35 #include "WebFormElement.h"
37 #include "WebFrameClient.h"
38 #include "WebSearchableFormData.h"
39 #include "WebSecurityPolicy.h"
40 #include "WebSettings.h"
41 #include "WebString.h"
43 #include "WebURLRequest.h"
44 #include "WebURLResponse.h"
47 #include <googleurl/src/gurl.h>
48 #include <gtest/gtest.h>
49 #include <webkit/support/webkit_support.h>
51 using namespace WebKit;
55 class WebFrameTest : public testing::Test {
58 : baseURL("http://www.test.com/"),
59 chromeURL("chrome://")
63 virtual void TearDown()
65 webkit_support::UnregisterAllMockedURLs();
68 void registerMockedHttpURLLoad(const std::string& fileName)
70 registerMockedURLLoad(baseURL, fileName);
73 void registerMockedChromeURLLoad(const std::string& fileName)
75 registerMockedURLLoad(chromeURL, fileName);
80 webkit_support::ServeAsynchronousMockedRequests();
83 void loadHttpFrame(WebFrame* frame, const std::string& fileName)
85 loadFrame(frame, baseURL, fileName);
88 void loadChromeFrame(WebFrame* frame, const std::string& fileName)
90 loadFrame(frame, chromeURL, fileName);
93 void registerMockedURLLoad(const std::string& base, const std::string& fileName)
95 WebURLResponse response;
96 response.initialize();
97 response.setMIMEType("text/html");
99 std::string filePath = webkit_support::GetWebKitRootDir().utf8();
100 filePath += "/Source/WebKit/chromium/tests/data/";
101 filePath += fileName;
103 webkit_support::RegisterMockedURL(WebURL(GURL(base + fileName)), response, WebString::fromUTF8(filePath));
106 void loadFrame(WebFrame* frame, const std::string& base, const std::string& fileName)
108 WebURLRequest urlRequest;
109 urlRequest.initialize();
110 urlRequest.setURL(WebURL(GURL(base + fileName)));
111 frame->loadRequest(urlRequest);
116 std::string chromeURL;
119 class TestWebFrameClient : public WebFrameClient {
122 TEST_F(WebFrameTest, ContentText)
124 registerMockedHttpURLLoad("iframes_test.html");
125 registerMockedHttpURLLoad("visible_iframe.html");
126 registerMockedHttpURLLoad("invisible_iframe.html");
127 registerMockedHttpURLLoad("zero_sized_iframe.html");
129 // Create and initialize the WebView.
130 TestWebFrameClient webFrameClient;
131 WebView* webView = WebView::create(0);
132 webView->initializeMainFrame(&webFrameClient);
134 loadHttpFrame(webView->mainFrame(), "iframes_test.html");
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"));
148 TEST_F(WebFrameTest, FrameForEnteredContext)
150 registerMockedHttpURLLoad("iframes_test.html");
151 registerMockedHttpURLLoad("visible_iframe.html");
152 registerMockedHttpURLLoad("invisible_iframe.html");
153 registerMockedHttpURLLoad("zero_sized_iframe.html");
155 // Create and initialize the WebView.
156 TestWebFrameClient webFrameClient;
157 WebView* webView = WebView::create(0);
158 webView->settings()->setJavaScriptEnabled(true);
159 webView->initializeMainFrame(&webFrameClient);
161 loadHttpFrame(webView->mainFrame(), "iframes_test.html");
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()));
175 TEST_F(WebFrameTest, FormWithNullFrame)
177 registerMockedHttpURLLoad("form.html");
179 TestWebFrameClient webFrameClient;
180 WebView* webView = WebView::create(0);
181 webView->initializeMainFrame(&webFrameClient);
183 loadHttpFrame(webView->mainFrame(), "form.html");
186 WebVector<WebFormElement> forms;
187 webView->mainFrame()->document().forms(forms);
190 EXPECT_EQ(forms.size(), 1U);
192 // This test passes if this doesn't crash.
193 WebSearchableFormData searchableDataForm(forms[0]);
196 TEST_F(WebFrameTest, ChromePageNoJavascript)
198 registerMockedChromeURLLoad("history.html");
200 // Create and initialize the WebView.
201 TestWebFrameClient webFrameClient;
202 WebView* webView = WebView::create(0);
203 webView->settings()->setJavaScriptEnabled(true);
204 webView->initializeMainFrame(&webFrameClient);
206 loadChromeFrame(webView->mainFrame(), "history.html");
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'))");
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"));
219 class TestReloadDoesntRedirectWebFrameClient : public WebFrameClient {
221 virtual WebNavigationPolicy decidePolicyForNavigation(
222 WebFrame*, const WebURLRequest&, WebNavigationType,
223 const WebNode& originatingNode,
224 WebNavigationPolicy defaultPolicy, bool isRedirect)
226 EXPECT_EQ(false, isRedirect);
227 return WebNavigationPolicyCurrentTab;
230 virtual WebURLError cancelledError(WebFrame*, const WebURLRequest& request)
232 // Return a dummy error so the DocumentLoader doesn't assert when
233 // the reload cancels it.
234 return WebURLError(WebCore::ResourceError("", 1, "", "cancelled"));
238 TEST_F(WebFrameTest, ReloadDoesntSetRedirect)
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");
244 TestReloadDoesntRedirectWebFrameClient webFrameClient;
245 WebView* webView = WebView::create(0);
246 webView->initializeMainFrame(&webFrameClient);
248 loadHttpFrame(webView->mainFrame(), "form.html");
252 webView->mainFrame()->reload(true);
253 // start reload before request is delivered.
254 webView->mainFrame()->reload(true);