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 <googleurl/src/gurl.h>
34 #include <gtest/gtest.h>
35 #include <webkit/support/webkit_support.h>
36 #include "WebFrame.h"
37 #include "WebFrameClient.h"
38 #include "WebSettings.h"
39 #include "WebString.h"
40 #include "WebURL.h"
41 #include "WebURLRequest.h"
42 #include "WebURLResponse.h"
43 #include "WebView.h"
44 #include "v8.h"
45
46 using namespace WebKit;
47
48 namespace {
49
50 class WebFrameTest : public testing::Test {
51 public:
WebFrameTest()52 WebFrameTest()
53 : baseURL("http://www.test.com/")
54 {
55 }
56
TearDown()57 virtual void TearDown()
58 {
59 webkit_support::UnregisterAllMockedURLs();
60 }
61
registerMockedURLLoad(const std::string & fileName)62 void registerMockedURLLoad(const std::string& fileName)
63 {
64 WebURLResponse response;
65 response.initialize();
66 response.setMIMEType("text/html");
67
68 std::string filePath = webkit_support::GetWebKitRootDir().utf8();
69 filePath += "/Source/WebKit/chromium/tests/data/";
70 filePath += fileName;
71
72 webkit_support::RegisterMockedURL(WebURL(GURL(baseURL + fileName)), response, WebString::fromUTF8(filePath));
73 }
74
serveRequests()75 void serveRequests()
76 {
77 webkit_support::ServeAsynchronousMockedRequests();
78 }
79
loadFrame(WebFrame * frame,const std::string & fileName)80 void loadFrame(WebFrame* frame, const std::string& fileName)
81 {
82 WebURLRequest urlRequest;
83 urlRequest.initialize();
84 urlRequest.setURL(WebURL(GURL(baseURL + fileName)));
85 frame->loadRequest(urlRequest);
86 }
87
88 protected:
89 std::string baseURL;
90 };
91
92 class TestWebFrameClient : public WebFrameClient {
93 };
94
TEST_F(WebFrameTest,ContentText)95 TEST_F(WebFrameTest, ContentText)
96 {
97 registerMockedURLLoad("iframes_test.html");
98 registerMockedURLLoad("visible_iframe.html");
99 registerMockedURLLoad("invisible_iframe.html");
100 registerMockedURLLoad("zero_sized_iframe.html");
101
102 // Create and initialize the WebView.
103 TestWebFrameClient webFrameClient;
104 WebView* webView = WebView::create(0);
105 webView->initializeMainFrame(&webFrameClient);
106
107 loadFrame(webView->mainFrame(), "iframes_test.html");
108 serveRequests();
109
110 // Now retrieve the frames text and test it only includes visible elements.
111 std::string content = webView->mainFrame()->contentAsText(1024).utf8();
112 EXPECT_NE(std::string::npos, content.find(" visible paragraph"));
113 EXPECT_NE(std::string::npos, content.find(" visible iframe"));
114 EXPECT_EQ(std::string::npos, content.find(" invisible pararaph"));
115 EXPECT_EQ(std::string::npos, content.find(" invisible iframe"));
116 EXPECT_EQ(std::string::npos, content.find("iframe with zero size"));
117
118 webView->close();
119 }
120
TEST_F(WebFrameTest,FrameForEnteredContext)121 TEST_F(WebFrameTest, FrameForEnteredContext)
122 {
123 registerMockedURLLoad("iframes_test.html");
124 registerMockedURLLoad("visible_iframe.html");
125 registerMockedURLLoad("invisible_iframe.html");
126 registerMockedURLLoad("zero_sized_iframe.html");
127
128 // Create and initialize the WebView.
129 TestWebFrameClient webFrameClient;
130 WebView* webView = WebView::create(0);
131 webView->settings()->setJavaScriptEnabled(true);
132 webView->initializeMainFrame(&webFrameClient);
133
134 loadFrame(webView->mainFrame(), "iframes_test.html");
135 serveRequests();
136
137 v8::HandleScope scope;
138 EXPECT_EQ(webView->mainFrame(),
139 WebFrame::frameForContext(
140 webView->mainFrame()->mainWorldScriptContext()));
141 EXPECT_EQ(webView->mainFrame()->firstChild(),
142 WebFrame::frameForContext(
143 webView->mainFrame()->firstChild()->mainWorldScriptContext()));
144
145 webView->close();
146 }
147
148 }
149