WinCairo build fails to link.
[WebKit-https.git] / Source / WebKit / chromium / tests / WebPageNewSerializerTest.cpp
1 /*
2  * Copyright (C) 2011 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 "FrameTestHelpers.h"
34 #include "URLTestHelpers.h"
35 #include "WebFrame.h"
36 #include "WebFrameClient.h"
37 #include "WebPageSerializer.h"
38 #include "WebScriptSource.h"
39 #include "WebSettings.h"
40 #include "WebView.h"
41 #include <gtest/gtest.h>
42 #include <public/Platform.h>
43 #include <public/WebString.h>
44 #include <public/WebThread.h>
45 #include <public/WebURL.h>
46 #include <public/WebURLRequest.h>
47 #include <public/WebURLResponse.h>
48 #include <public/WebUnitTestSupport.h>
49
50 using namespace WebKit;
51 using WebKit::FrameTestHelpers::runPendingTasks;
52 using WebKit::URLTestHelpers::toKURL;
53 using WebKit::URLTestHelpers::registerMockedURLLoad;
54
55 namespace {
56
57 class LineReader {
58 public:
59     LineReader(const std::string& text) : m_text(text), m_index(0) { }
60     bool getNextLine(std::string* line)
61     {
62         line->clear();
63         if (m_index >= m_text.length())
64             return false;
65
66         size_t endOfLineIndex = m_text.find("\r\n", m_index);
67         if (endOfLineIndex == std::string::npos) {
68             *line = m_text.substr(m_index);
69             m_index = m_text.length();
70         } else {
71             *line = m_text.substr(m_index, endOfLineIndex - m_index);
72             m_index = endOfLineIndex + 2;
73         }
74         return true;
75     }
76
77 private:
78     std::string m_text;
79     size_t m_index;
80 };
81
82 class TestWebFrameClient : public WebFrameClient {
83 public:
84     virtual ~TestWebFrameClient() { }
85 };
86
87 class WebPageNewSerializeTest : public testing::Test {
88 public:
89     WebPageNewSerializeTest()
90         : m_htmlMimeType(WebString::fromUTF8("text/html"))
91         , m_xhtmlMimeType(WebString::fromUTF8("application/xhtml+xml"))
92         , m_cssMimeType(WebString::fromUTF8("text/css"))
93         , m_pngMimeType(WebString::fromUTF8("image/png"))
94         , m_svgMimeType(WebString::fromUTF8("image/svg+xml"))
95     {
96     }
97
98 protected:
99     virtual void SetUp()
100     {
101         // Create and initialize the WebView.
102         m_webView = WebView::create(0);
103
104         // We want the images to load and JavaScript to be on.
105         WebSettings* settings = m_webView->settings();
106         settings->setImagesEnabled(true);
107         settings->setLoadsImagesAutomatically(true);
108         settings->setJavaScriptEnabled(true);
109
110         m_webView->initializeMainFrame(&m_webFrameClient);
111     }
112
113     virtual void TearDown()
114     {
115         Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
116         m_webView->close();
117     }
118
119     WebURL setUpCSSTestPage()
120     {
121         WebURL topFrameURL = toKURL("http://www.test.com");
122         registerMockedURLLoad(topFrameURL, WebString::fromUTF8("css_test_page.html"), WebString::fromUTF8("pageserializer/"), htmlMimeType());
123         registerMockedURLLoad(toKURL("http://www.test.com/link_styles.css"), WebString::fromUTF8("link_styles.css"), WebString::fromUTF8("pageserializer/"), cssMimeType());
124         registerMockedURLLoad(toKURL("http://www.test.com/import_style_from_link.css"), WebString::fromUTF8("import_style_from_link.css"), WebString::fromUTF8("pageserializer/"), cssMimeType());
125         registerMockedURLLoad(toKURL("http://www.test.com/import_styles.css"), WebString::fromUTF8("import_styles.css"), WebString::fromUTF8("pageserializer/"), cssMimeType());
126         registerMockedURLLoad(toKURL("http://www.test.com/red_background.png"), WebString::fromUTF8("red_background.png"), WebString::fromUTF8("pageserializer/"), pngMimeType());
127         registerMockedURLLoad(toKURL("http://www.test.com/orange_background.png"), WebString::fromUTF8("orange_background.png"), WebString::fromUTF8("pageserializer/"), pngMimeType());
128         registerMockedURLLoad(toKURL("http://www.test.com/yellow_background.png"), WebString::fromUTF8("yellow_background.png"), WebString::fromUTF8("pageserializer/"), pngMimeType());
129         registerMockedURLLoad(toKURL("http://www.test.com/green_background.png"), WebString::fromUTF8("green_background.png"), WebString::fromUTF8("pageserializer/"), pngMimeType());
130         registerMockedURLLoad(toKURL("http://www.test.com/blue_background.png"), WebString::fromUTF8("blue_background.png"), WebString::fromUTF8("pageserializer/"), pngMimeType());
131         registerMockedURLLoad(toKURL("http://www.test.com/purple_background.png"), WebString::fromUTF8("purple_background.png"), WebString::fromUTF8("pageserializer/"), pngMimeType());
132         registerMockedURLLoad(toKURL("http://www.test.com/ul-dot.png"), WebString::fromUTF8("ul-dot.png"), WebString::fromUTF8("pageserializer/"), pngMimeType());
133         registerMockedURLLoad(toKURL("http://www.test.com/ol-dot.png"), WebString::fromUTF8("ol-dot.png"), WebString::fromUTF8("pageserializer/"), pngMimeType());
134         return topFrameURL;
135     }
136
137     void loadURLInTopFrame(const WebURL& url)
138     {
139         WebURLRequest urlRequest;
140         urlRequest.initialize();
141         urlRequest.setURL(url);
142         m_webView->mainFrame()->loadRequest(urlRequest);
143         // Make sure any pending request get served.
144         Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests();
145         // Some requests get delayed, run the timer.
146         runPendingTasks();
147         // Server the delayed resources.
148         Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests();
149     }
150
151     const WebString& htmlMimeType() const { return m_htmlMimeType; }
152     const WebString& xhtmlMimeType() const { return m_xhtmlMimeType; }
153     const WebString& cssMimeType() const { return m_cssMimeType; }
154     const WebString& pngMimeType() const { return m_pngMimeType; }
155     const WebString& svgMimeType() const { return m_svgMimeType; }
156
157     static bool resourceVectorContains(const WebVector<WebPageSerializer::Resource>& resources, const char* url, const char* mimeType)
158     {
159         WebURL webURL = WebURL(toKURL(url));
160         for (size_t i = 0; i < resources.size(); ++i) {
161             const WebPageSerializer::Resource& resource = resources[i];
162             if (resource.url == webURL && !resource.data.isEmpty() && !resource.mimeType.compare(WebCString(mimeType)))
163                 return true;
164         }
165         return false;
166     }
167
168     WebView* m_webView;
169
170 private:
171     WebString m_htmlMimeType;
172     WebString m_xhtmlMimeType;
173     WebString m_cssMimeType;
174     WebString m_pngMimeType;
175     WebString m_svgMimeType;
176     TestWebFrameClient m_webFrameClient;
177 };
178
179 // Tests that a page with resources and sub-frame is reported with all its resources.
180 TEST_F(WebPageNewSerializeTest, PageWithFrames)
181 {
182     // Register the mocked frames.
183     WebURL topFrameURL = toKURL("http://www.test.com");
184     registerMockedURLLoad(topFrameURL, WebString::fromUTF8("top_frame.html"), WebString::fromUTF8("pageserializer/"), htmlMimeType());
185     registerMockedURLLoad(toKURL("http://www.test.com/iframe.html"), WebString::fromUTF8("iframe.html"), WebString::fromUTF8("pageserializer/"), htmlMimeType());
186     registerMockedURLLoad(toKURL("http://www.test.com/iframe2.html"), WebString::fromUTF8("iframe2.html"), WebString::fromUTF8("pageserializer/"), htmlMimeType());
187     registerMockedURLLoad(toKURL("http://www.test.com/red_background.png"), WebString::fromUTF8("red_background.png"), WebString::fromUTF8("pageserializer/"), pngMimeType());
188     registerMockedURLLoad(toKURL("http://www.test.com/green_background.png"), WebString::fromUTF8("green_background.png"), WebString::fromUTF8("pageserializer/"), pngMimeType());
189     registerMockedURLLoad(toKURL("http://www.test.com/blue_background.png"), WebString::fromUTF8("blue_background.png"), WebString::fromUTF8("pageserializer/"), pngMimeType());
190
191     loadURLInTopFrame(topFrameURL);
192
193     WebVector<WebPageSerializer::Resource> resources;
194     WebPageSerializer::serialize(m_webView, &resources);
195     ASSERT_FALSE(resources.isEmpty());
196
197     // The first resource should be the main-frame.
198     const WebPageSerializer::Resource& resource = resources[0];
199     EXPECT_TRUE(resource.url == WebURL(toKURL("http://www.test.com")));
200     EXPECT_EQ(0, resource.mimeType.compare(WebCString("text/html")));
201     EXPECT_FALSE(resource.data.isEmpty());
202
203     EXPECT_EQ(6U, resources.size()); // There should be no duplicates.
204     EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/red_background.png", "image/png"));
205     EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/green_background.png", "image/png"));
206     EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/blue_background.png", "image/png"));
207     EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/iframe.html", "text/html"));
208     EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/iframe2.html", "text/html"));
209 }
210
211 // Test that when serializing a page, all CSS resources are reported, including url()'s
212 // and imports and links. Note that we don't test the resources contents, we only make sure
213 // they are all reported with the right mime type and that they contain some data.
214 TEST_F(WebPageNewSerializeTest, FAILS_CSSResources)
215 {
216     // Register the mocked frame and load it.
217     WebURL topFrameURL = setUpCSSTestPage();
218     loadURLInTopFrame(topFrameURL);
219
220     WebVector<WebPageSerializer::Resource> resources;
221     WebPageSerializer::serialize(m_webView, &resources);
222     ASSERT_FALSE(resources.isEmpty());
223
224     // The first resource should be the main-frame.
225     const WebPageSerializer::Resource& resource = resources[0];
226     EXPECT_TRUE(resource.url == WebURL(toKURL("http://www.test.com")));
227     EXPECT_EQ(0, resource.mimeType.compare(WebCString("text/html")));
228     EXPECT_FALSE(resource.data.isEmpty());
229
230     EXPECT_EQ(12U, resources.size()); // There should be no duplicates.
231     EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/link_styles.css", "text/css"));
232     EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/import_styles.css", "text/css"));
233     EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/import_style_from_link.css", "text/css"));
234     EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/red_background.png", "image/png"));
235     EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/orange_background.png", "image/png"));
236     EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/yellow_background.png", "image/png"));
237     EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/green_background.png", "image/png"));
238     EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/blue_background.png", "image/png"));
239     EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/purple_background.png", "image/png"));
240     EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/ul-dot.png", "image/png"));
241     EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/ol-dot.png", "image/png"));
242 }
243
244 // Tests that when serializing a page with blank frames these are reported with their resources.
245 TEST_F(WebPageNewSerializeTest, BlankFrames)
246 {
247     // Register the mocked frame and load it.
248     WebURL topFrameURL = toKURL("http://www.test.com");
249     registerMockedURLLoad(topFrameURL, WebString::fromUTF8("blank_frames.html"), WebString::fromUTF8("pageserializer/"), htmlMimeType());
250     registerMockedURLLoad(toKURL("http://www.test.com/red_background.png"), WebString::fromUTF8("red_background.png"), WebString::fromUTF8("pageserializer/"), pngMimeType());
251     registerMockedURLLoad(toKURL("http://www.test.com/orange_background.png"), WebString::fromUTF8("orange_background.png"), WebString::fromUTF8("pageserializer/"), pngMimeType());
252     registerMockedURLLoad(toKURL("http://www.test.com/blue_background.png"), WebString::fromUTF8("blue_background.png"), WebString::fromUTF8("pageserializer/"), pngMimeType());
253
254     loadURLInTopFrame(topFrameURL);
255
256     WebVector<WebPageSerializer::Resource> resources;
257     WebPageSerializer::serialize(m_webView, &resources);
258     ASSERT_FALSE(resources.isEmpty());
259
260     // The first resource should be the main-frame.
261     const WebPageSerializer::Resource& resource = resources[0];
262     EXPECT_TRUE(resource.url == WebURL(toKURL("http://www.test.com")));
263     EXPECT_EQ(0, resource.mimeType.compare(WebCString("text/html")));
264     EXPECT_FALSE(resource.data.isEmpty());
265
266     EXPECT_EQ(7U, resources.size()); // There should be no duplicates.
267     EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/red_background.png", "image/png"));
268     EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/orange_background.png", "image/png"));
269     EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/blue_background.png", "image/png"));
270     // The blank frames should have got a magic URL.
271     EXPECT_TRUE(resourceVectorContains(resources, "wyciwyg://frame/0", "text/html"));
272     EXPECT_TRUE(resourceVectorContains(resources, "wyciwyg://frame/1", "text/html"));
273     EXPECT_TRUE(resourceVectorContains(resources, "wyciwyg://frame/2", "text/html"));
274 }
275
276 TEST_F(WebPageNewSerializeTest, SerializeXMLHasRightDeclaration)
277 {
278     WebURL topFrameURL = toKURL("http://www.test.com/simple.xhtml");
279     registerMockedURLLoad(topFrameURL, WebString::fromUTF8("simple.xhtml"), WebString::fromUTF8("pageserializer/"), xhtmlMimeType());
280
281     loadURLInTopFrame(topFrameURL);
282
283     WebVector<WebPageSerializer::Resource> resources;
284     WebPageSerializer::serialize(m_webView, &resources);
285     ASSERT_FALSE(resources.isEmpty());
286
287     // We expect only one resource, the XML.
288     ASSERT_EQ(1U, resources.size());
289     std::string xml = std::string(resources[0].data.data());
290
291     // We should have one and only one instance of the XML declaration.
292     size_t pos = xml.find("<?xml version=");
293     ASSERT_TRUE(pos != std::string::npos);
294
295     pos = xml.find("<?xml version=", pos + 1);
296     ASSERT_TRUE(pos == std::string::npos);
297 }
298
299 TEST_F(WebPageNewSerializeTest, FAILS_TestMHTMLEncoding)
300 {
301     // Load a page with some CSS and some images.
302     WebURL topFrameURL = setUpCSSTestPage();
303     loadURLInTopFrame(topFrameURL);
304
305     WebCString mhtmlData = WebPageSerializer::serializeToMHTML(m_webView);
306     ASSERT_FALSE(mhtmlData.isEmpty());
307
308     // Read the MHTML data line per line and do some pseudo-parsing to make sure the right encoding is used for the different sections.
309     LineReader lineReader(std::string(mhtmlData.data()));
310     int sectionCheckedCount = 0;
311     const char* expectedEncoding = 0;
312     std::string line;
313     while (lineReader.getNextLine(&line)) {
314         if (!line.find("Content-Type:")) {
315             ASSERT_FALSE(expectedEncoding);
316             if (line.find("multipart/related;") != std::string::npos) {
317                 // Skip this one, it's part of the MHTML header.
318                 continue;
319             }
320             if (line.find("text/") != std::string::npos)
321                 expectedEncoding = "quoted-printable";
322             else if (line.find("image/") != std::string::npos)
323                 expectedEncoding = "base64";
324             else
325                 FAIL() << "Unexpected Content-Type: " << line;
326             continue;
327         }
328         if (!line.find("Content-Transfer-Encoding:")) {
329            ASSERT_TRUE(expectedEncoding);
330            EXPECT_TRUE(line.find(expectedEncoding) != std::string::npos);
331            expectedEncoding = 0;
332            sectionCheckedCount++;
333         }
334     }
335     EXPECT_EQ(12, sectionCheckedCount);
336 }
337
338 // Test that we don't regress https://bugs.webkit.org/show_bug.cgi?id=99105
339 TEST_F(WebPageNewSerializeTest, SVGImageDontCrash)
340 {
341     WebURL pageUrl = toKURL("http://www.test.com");
342     WebURL imageUrl = toKURL("http://www.test.com/green_rectangle.svg");
343
344     registerMockedURLLoad(pageUrl, WebString::fromUTF8("page_with_svg_image.html"), WebString::fromUTF8("pageserializer/"), htmlMimeType());
345     registerMockedURLLoad(imageUrl, WebString::fromUTF8("green_rectangle.svg"), WebString::fromUTF8("pageserializer/"), svgMimeType());
346
347     loadURLInTopFrame(pageUrl);
348
349     WebCString mhtml = WebPageSerializer::serializeToMHTML(m_webView);
350     // We expect some data to be generated.
351     EXPECT_GT(mhtml.length(), 50U);
352 }
353
354 }