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.
31 #include <googleurl/src/gurl.h>
32 #include <gtest/gtest.h>
33 #include <webkit/support/webkit_support.h>
35 #include "WebFrameClient.h"
36 #include "WebString.h"
38 #include "WebURLRequest.h"
39 #include "WebURLResponse.h"
42 using namespace WebKit;
46 class WebFrameTest : public testing::Test {
50 virtual void TearDown()
52 webkit_support::UnregisterAllMockedURLs();
55 void registerMockedURLLoad(const WebURL& url, const WebURLResponse& response, const WebString& fileName)
57 std::string filePath = webkit_support::GetWebKitRootDir().utf8();
58 filePath.append("/WebKit/chromium/tests/data/");
59 filePath.append(fileName.utf8());
60 webkit_support::RegisterMockedURL(url, response, WebString::fromUTF8(filePath));
65 webkit_support::ServeAsynchronousMockedRequests();
69 class TestWebFrameClient : public WebFrameClient {
72 TEST_F(WebFrameTest, ContentText)
74 // Register our resources.
75 WebURLResponse response;
76 response.initialize();
77 response.setMIMEType("text/html");
78 std::string rootURL = "http://www.test.com/";
79 const char* files[] = { "iframes_test.html", "visible_iframe.html",
80 "invisible_iframe.html", "zero_sized_iframe.html" };
81 for (int i = 0; i < (sizeof(files) / sizeof(char*)); ++i) {
82 WebURL webURL = GURL(rootURL + files[i]);
83 registerMockedURLLoad(webURL, response, WebString::fromUTF8(files[i]));
86 // Create and initialize the WebView.
87 TestWebFrameClient webFrameClient;
88 WebView* webView = WebView::create(0);
89 webView->initializeMainFrame(&webFrameClient);
91 // Load the main frame URL.
92 WebURL testURL(GURL(rootURL + files[0]));
93 WebURLRequest urlRequest;
94 urlRequest.initialize();
95 urlRequest.setURL(testURL);
96 webView->mainFrame()->loadRequest(urlRequest);
98 // Load all pending asynchronous requests.
101 // Now retrieve the frames text and test it only includes visible elements.
102 std::string content = webView->mainFrame()->contentAsText(1024).utf8();
103 EXPECT_NE(std::string::npos, content.find(" visible paragraph"));
104 EXPECT_NE(std::string::npos, content.find(" visible iframe"));
105 EXPECT_EQ(std::string::npos, content.find(" invisible pararaph"));
106 EXPECT_EQ(std::string::npos, content.find(" invisible iframe"));
107 EXPECT_EQ(std::string::npos, content.find("iframe with zero size"));