2 * Copyright (C) 2011 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.
32 #include "public/web/WebPageSerializer.h"
34 #include "public/platform/Platform.h"
35 #include "public/platform/WebString.h"
36 #include "public/platform/WebURL.h"
37 #include "public/platform/WebURLRequest.h"
38 #include "public/platform/WebURLResponse.h"
39 #include "public/platform/WebUnitTestSupport.h"
40 #include "public/web/WebDocument.h"
41 #include "public/web/WebFrame.h"
42 #include "public/web/WebView.h"
43 #include "web/tests/FrameTestHelpers.h"
44 #include "web/tests/URLTestHelpers.h"
46 #include <gtest/gtest.h>
48 using namespace blink;
49 using blink::Document;
50 using blink::URLTestHelpers::toKURL;
54 class WebPageSerializerTest : public testing::Test {
56 WebPageSerializerTest() : m_supportedSchemes(static_cast<size_t>(3))
58 m_supportedSchemes[0] = "http";
59 m_supportedSchemes[1] = "https";
60 m_supportedSchemes[2] = "file";
66 m_helper.initialize();
69 virtual void TearDown()
71 Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
74 void registerMockedURLLoad(const std::string& url, const WebString& fileName)
76 URLTestHelpers::registerMockedURLLoad(toKURL(url), fileName, WebString::fromUTF8("pageserialization/"), WebString::fromUTF8("text/html"));
79 void loadURLInTopFrame(const WebURL& url)
81 FrameTestHelpers::loadFrame(m_helper.webView()->mainFrame(), url.string().utf8());
84 static bool webVectorContains(const WebVector<WebURL>& vector, const char* url)
86 return vector.contains(WebURL(toKURL(std::string(url))));
89 // Useful for debugging.
90 static void printWebURLs(const WebVector<WebURL>& urls)
92 for (size_t i = 0; i < urls.size(); i++)
93 printf("%s\n", urls[i].spec().data());
96 WebView* webView() const { return m_helper.webView(); }
98 WebVector<WebCString> m_supportedSchemes;
101 FrameTestHelpers::WebViewHelper m_helper;
104 TEST_F(WebPageSerializerTest, HTMLNodes)
106 // Register the mocked frame and load it.
107 WebURL topFrameURL = toKURL("http://www.test.com");
108 registerMockedURLLoad("http://www.test.com", WebString::fromUTF8("simple_page.html"));
109 registerMockedURLLoad("http://www.example.com/beautifull.css", WebString::fromUTF8("beautifull.css"));
110 loadURLInTopFrame(topFrameURL);
112 // Retrieve all resources.
113 WebVector<WebURL> frames;
114 WebVector<WebURL> resources;
115 ASSERT_TRUE(WebPageSerializer::retrieveAllResources(
116 webView(), m_supportedSchemes, &resources, &frames));
118 // Tests that all resources from the frame have been retrieved.
119 EXPECT_EQ(1U, frames.size()); // There should be no duplicates.
120 EXPECT_TRUE(webVectorContains(frames, "http://www.test.com"));
122 EXPECT_EQ(14U, resources.size()); // There should be no duplicates.
123 EXPECT_TRUE(webVectorContains(resources, "http://www.example.com/beautifull.css"));
124 EXPECT_TRUE(webVectorContains(resources, "http://www.test.com/awesome.js"));
125 EXPECT_TRUE(webVectorContains(resources, "http://www.test.com/bodyBackground.jpg"));
126 EXPECT_TRUE(webVectorContains(resources, "http://www.test.com/awesome.png"));
127 EXPECT_TRUE(webVectorContains(resources, "http://www.test.com/imageButton.png"));
128 EXPECT_TRUE(webVectorContains(resources, "http://www.test.com/tableBackground.png"));
129 EXPECT_TRUE(webVectorContains(resources, "http://www.test.com/trBackground.png"));
130 EXPECT_TRUE(webVectorContains(resources, "http://www.test.com/tdBackground.png"));
131 EXPECT_TRUE(webVectorContains(resources, "http://www.evene.fr/citations/auteur.php?ida=46"));
132 EXPECT_TRUE(webVectorContains(resources, "http://www.brainyquote.com/quotes/authors/c/charles_darwin.html"));
133 EXPECT_TRUE(webVectorContains(resources, "http://www.test.com/why_deleted.html"));
134 EXPECT_TRUE(webVectorContains(resources, "http://www.test.com/why_inserted.html"));
135 EXPECT_TRUE(webVectorContains(resources, "https://www.secure.com/https.gif"));
136 EXPECT_TRUE(webVectorContains(resources, "file://c/my_folder/file.gif"));
139 TEST_F(WebPageSerializerTest, MultipleFrames)
141 // Register the mocked frames.
142 WebURL topFrameURL = toKURL("http://www.test.com");
143 registerMockedURLLoad("http://www.test.com", WebString::fromUTF8("top_frame.html"));
144 registerMockedURLLoad("http://www.test.com/simple_iframe.html",
145 WebString::fromUTF8("simple_iframe.html"));
146 registerMockedURLLoad("http://www.test.com/object_iframe.html",
147 WebString::fromUTF8("object_iframe.html"));
148 registerMockedURLLoad("http://www.test.com/embed_iframe.html",
149 WebString::fromUTF8("embed_iframe.html"));
150 // If we don't register a mocked resource for awesome.png, it causes the
151 // document loader of the iframe that has it as its src to assert on close,
153 registerMockedURLLoad("http://www.test.com/awesome.png",
154 WebString::fromUTF8("awesome.png"));
156 loadURLInTopFrame(topFrameURL);
158 // Retrieve all resources.
159 WebVector<WebURL> frames;
160 WebVector<WebURL> resources;
161 ASSERT_TRUE(WebPageSerializer::retrieveAllResources(
162 webView(), m_supportedSchemes, &resources, &frames));
164 // Tests that all resources from the frame have been retrieved.
165 EXPECT_EQ(4U, frames.size()); // There should be no duplicates.
166 EXPECT_TRUE(webVectorContains(frames, "http://www.test.com"));
167 EXPECT_TRUE(webVectorContains(frames, "http://www.test.com/simple_iframe.html"));
168 EXPECT_TRUE(webVectorContains(frames, "http://www.test.com/object_iframe.html"));
169 EXPECT_TRUE(webVectorContains(frames, "http://www.test.com/embed_iframe.html"));
171 EXPECT_EQ(5U, resources.size()); // There should be no duplicates.
172 EXPECT_TRUE(webVectorContains(resources, "http://www.test.com/awesome.png"));
173 EXPECT_TRUE(webVectorContains(resources, "http://www.test.com/innerFrame.png"));
174 EXPECT_TRUE(webVectorContains(resources, "http://www.test.com/flash.swf"));
175 // FIXME: for some reason the following resources is missing on one of the bot
176 // causing the test to fail. Probably a plugin issue.
177 // EXPECT_TRUE(webVectorContains(resources, "http://www.test.com/music.mid"));
178 EXPECT_TRUE(webVectorContains(resources, "http://www.test.com/object.png"));
179 EXPECT_TRUE(webVectorContains(resources, "http://www.test.com/embed.png"));