2 * Copyright (c) 2013, Opera Software ASA. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Opera Software ASA nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "core/page/PageSerializer.h"
34 #include "bindings/core/v8/V8Binding.h"
35 #include "bindings/core/v8/V8BindingForTesting.h"
36 #include "core/page/Page.h"
37 #include "platform/SerializedResource.h"
38 #include "platform/testing/URLTestHelpers.h"
39 #include "public/platform/Platform.h"
40 #include "public/platform/WebString.h"
41 #include "public/platform/WebThread.h"
42 #include "public/platform/WebURL.h"
43 #include "public/platform/WebURLRequest.h"
44 #include "public/platform/WebURLResponse.h"
45 #include "public/platform/WebUnitTestSupport.h"
46 #include "public/web/WebSettings.h"
47 #include "web/WebLocalFrameImpl.h"
48 #include "web/WebViewImpl.h"
49 #include "web/tests/FrameTestHelpers.h"
50 #include "wtf/Vector.h"
51 #include <gtest/gtest.h>
53 using blink::URLTestHelpers::toKURL
;
54 using blink::URLTestHelpers::registerMockedURLLoad
;
58 class PageSerializerTest
: public testing::Test
{
61 : m_folder(WebString::fromUTF8("pageserializer/"))
62 , m_baseUrl(toKURL("http://www.test.com"))
69 // We want the images to load and JavaScript to be on.
70 m_helper
.initialize(true, 0, 0, &configureSettings
);
73 void TearDown() override
75 Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
78 void setBaseFolder(const char* folder
)
80 m_folder
= WebString::fromUTF8(folder
);
83 void setRewriteURLFolder(const char* folder
)
85 m_rewriteFolder
= folder
;
88 void registerURL(const char* url
, const char* file
, const char* mimeType
)
90 registerMockedURLLoad(KURL(m_baseUrl
, url
), WebString::fromUTF8(file
), m_folder
, WebString::fromUTF8(mimeType
));
93 void registerURL(const char* file
, const char* mimeType
)
95 registerURL(file
, file
, mimeType
);
98 void registerErrorURL(const char* file
, int statusCode
)
101 error
.reason
= 0xdead + statusCode
;
102 error
.domain
= "PageSerializerTest";
104 WebURLResponse response
;
105 response
.initialize();
106 response
.setMIMEType("text/html");
107 response
.setHTTPStatusCode(statusCode
);
109 Platform::current()->unitTestSupport()->registerMockedErrorURL(KURL(m_baseUrl
, file
), response
, error
);
112 void registerRewriteURL(const char* fromURL
, const char* toURL
)
114 m_rewriteURLs
.add(fromURL
, toURL
);
117 void serialize(const char* url
)
119 FrameTestHelpers::loadFrame(m_helper
.webView()->mainFrame(), KURL(m_baseUrl
, url
).string().utf8().data());
120 PageSerializer
serializer(&m_resources
, nullptr);
122 serializer
.setRewriteURLFolder(m_rewriteFolder
);
123 for (const auto& rewriteURL
: m_rewriteURLs
)
124 serializer
.registerRewriteURL(rewriteURL
.key
, rewriteURL
.value
);
126 serializer
.serialize(m_helper
.webViewImpl()->mainFrameImpl()->frame()->page());
129 Vector
<SerializedResource
>& getResources()
135 const SerializedResource
* getResource(const char* url
, const char* mimeType
)
137 KURL kURL
= KURL(m_baseUrl
, url
);
138 String
mime(mimeType
);
139 for (size_t i
= 0; i
< m_resources
.size(); ++i
) {
140 const SerializedResource
& resource
= m_resources
[i
];
141 if (resource
.url
== kURL
&& !resource
.data
->isEmpty()
142 && (mime
.isNull() || equalIgnoringCase(resource
.mimeType
, mime
)))
148 bool isSerialized(const char* url
, const char* mimeType
= 0)
150 return getResource(url
, mimeType
);
153 String
getSerializedData(const char* url
, const char* mimeType
= 0)
155 const SerializedResource
* resource
= getResource(url
, mimeType
);
157 return String(resource
->data
->data(), resource
->data
->size());
162 static void configureSettings(WebSettings
* settings
)
164 settings
->setImagesEnabled(true);
165 settings
->setLoadsImagesAutomatically(true);
166 settings
->setJavaScriptEnabled(true);
169 FrameTestHelpers::WebViewHelper m_helper
;
172 Vector
<SerializedResource
> m_resources
;
173 HashMap
<String
, String
> m_rewriteURLs
;
174 String m_rewriteFolder
;
177 TEST_F(PageSerializerTest
, HTMLElements
)
179 setBaseFolder("pageserializer/elements/");
181 registerURL("elements.html", "text/html");
182 registerURL("style.css", "style.css", "text/css");
183 registerURL("copyright.html", "empty.txt", "text/html");
184 registerURL("script.js", "empty.txt", "text/javascript");
186 registerURL("bodyBackground.png", "image.png", "image/png");
188 registerURL("imageSrc.png", "image.png", "image/png");
190 registerURL("inputImage.png", "image.png", "image/png");
192 registerURL("tableBackground.png", "image.png", "image/png");
193 registerURL("trBackground.png", "image.png", "image/png");
194 registerURL("tdBackground.png", "image.png", "image/png");
196 registerURL("blockquoteCite.html", "empty.txt", "text/html");
197 registerURL("qCite.html", "empty.txt", "text/html");
198 registerURL("delCite.html", "empty.txt", "text/html");
199 registerURL("insCite.html", "empty.txt", "text/html");
201 registerErrorURL("nonExisting.png", 404);
203 serialize("elements.html");
205 EXPECT_EQ(8U, getResources().size());
207 EXPECT_TRUE(isSerialized("elements.html", "text/html"));
208 EXPECT_TRUE(isSerialized("style.css", "text/css"));
209 EXPECT_TRUE(isSerialized("bodyBackground.png", "image/png"));
210 EXPECT_TRUE(isSerialized("imageSrc.png", "image/png"));
211 EXPECT_TRUE(isSerialized("inputImage.png", "image/png"));
212 EXPECT_TRUE(isSerialized("tableBackground.png", "image/png"));
213 EXPECT_TRUE(isSerialized("trBackground.png", "image/png"));
214 EXPECT_TRUE(isSerialized("tdBackground.png", "image/png"));
215 EXPECT_FALSE(isSerialized("nonExisting.png", "image/png"));
218 TEST_F(PageSerializerTest
, Frames
)
220 setBaseFolder("pageserializer/frames/");
222 registerURL("simple_frames.html", "text/html");
223 registerURL("simple_frames_top.html", "text/html");
224 registerURL("simple_frames_1.html", "text/html");
225 registerURL("simple_frames_3.html", "text/html");
227 registerURL("frame_1.png", "image.png", "image/png");
228 registerURL("frame_2.png", "image.png", "image/png");
229 registerURL("frame_3.png", "image.png", "image/png");
230 registerURL("frame_4.png", "image.png", "image/png");
232 serialize("simple_frames.html");
234 EXPECT_EQ(8U, getResources().size());
236 EXPECT_TRUE(isSerialized("simple_frames.html", "text/html"));
237 EXPECT_TRUE(isSerialized("simple_frames_top.html", "text/html"));
238 EXPECT_TRUE(isSerialized("simple_frames_1.html", "text/html"));
239 EXPECT_TRUE(isSerialized("simple_frames_3.html", "text/html"));
241 EXPECT_TRUE(isSerialized("frame_1.png", "image/png"));
242 EXPECT_TRUE(isSerialized("frame_2.png", "image/png"));
243 EXPECT_TRUE(isSerialized("frame_3.png", "image/png"));
244 EXPECT_TRUE(isSerialized("frame_4.png", "image/png"));
247 TEST_F(PageSerializerTest
, IFrames
)
249 setBaseFolder("pageserializer/frames/");
251 registerURL("top_frame.html", "text/html");
252 registerURL("simple_iframe.html", "text/html");
253 registerURL("object_iframe.html", "text/html");
254 registerURL("embed_iframe.html", "text/html");
256 registerURL("top.png", "image.png", "image/png");
257 registerURL("simple.png", "image.png", "image/png");
258 registerURL("object.png", "image.png", "image/png");
259 registerURL("embed.png", "image.png", "image/png");
261 serialize("top_frame.html");
263 EXPECT_EQ(8U, getResources().size());
265 EXPECT_TRUE(isSerialized("top_frame.html", "text/html"));
266 EXPECT_TRUE(isSerialized("simple_iframe.html", "text/html"));
267 EXPECT_TRUE(isSerialized("object_iframe.html", "text/html"));
268 EXPECT_TRUE(isSerialized("embed_iframe.html", "text/html"));
270 EXPECT_TRUE(isSerialized("top.png", "image/png"));
271 EXPECT_TRUE(isSerialized("simple.png", "image/png"));
272 EXPECT_TRUE(isSerialized("object.png", "image/png"));
273 EXPECT_TRUE(isSerialized("embed.png", "image/png"));
276 // Tests that when serializing a page with blank frames these are reported with their resources.
277 TEST_F(PageSerializerTest
, BlankFrames
)
279 setBaseFolder("pageserializer/frames/");
281 registerURL("blank_frames.html", "text/html");
282 registerURL("red_background.png", "image.png", "image/png");
283 registerURL("orange_background.png", "image.png", "image/png");
284 registerURL("blue_background.png", "image.png", "image/png");
286 serialize("blank_frames.html");
288 EXPECT_EQ(7U, getResources().size());
290 EXPECT_TRUE(isSerialized("http://www.test.com/red_background.png", "image/png"));
291 EXPECT_TRUE(isSerialized("http://www.test.com/orange_background.png", "image/png"));
292 EXPECT_TRUE(isSerialized("http://www.test.com/blue_background.png", "image/png"));
293 // The blank frames should have got a magic URL.
294 EXPECT_TRUE(isSerialized("wyciwyg://frame/0", "text/html"));
295 EXPECT_TRUE(isSerialized("wyciwyg://frame/1", "text/html"));
296 EXPECT_TRUE(isSerialized("wyciwyg://frame/2", "text/html"));
299 TEST_F(PageSerializerTest
, CSS
)
301 setBaseFolder("pageserializer/css/");
303 registerURL("css_test_page.html", "text/html");
304 registerURL("link_styles.css", "text/css");
305 registerURL("import_style_from_link.css", "text/css");
306 registerURL("import_styles.css", "text/css");
307 registerURL("do_not_serialize.png", "image.png", "image/png");
308 registerURL("red_background.png", "image.png", "image/png");
309 registerURL("orange_background.png", "image.png", "image/png");
310 registerURL("yellow_background.png", "image.png", "image/png");
311 registerURL("green_background.png", "image.png", "image/png");
312 registerURL("blue_background.png", "image.png", "image/png");
313 registerURL("purple_background.png", "image.png", "image/png");
314 registerURL("pink_background.png", "image.png", "image/png");
315 registerURL("brown_background.png", "image.png", "image/png");
316 registerURL("ul-dot.png", "image.png", "image/png");
317 registerURL("ol-dot.png", "image.png", "image/png");
319 serialize("css_test_page.html");
321 EXPECT_EQ(14U, getResources().size());
323 EXPECT_FALSE(isSerialized("do_not_serialize.png", "image/png"));
325 EXPECT_TRUE(isSerialized("css_test_page.html", "text/html"));
326 EXPECT_TRUE(isSerialized("link_styles.css", "text/css"));
327 EXPECT_TRUE(isSerialized("import_styles.css", "text/css"));
328 EXPECT_TRUE(isSerialized("import_style_from_link.css", "text/css"));
329 EXPECT_TRUE(isSerialized("red_background.png", "image/png"));
330 EXPECT_TRUE(isSerialized("orange_background.png", "image/png"));
331 EXPECT_TRUE(isSerialized("yellow_background.png", "image/png"));
332 EXPECT_TRUE(isSerialized("green_background.png", "image/png"));
333 EXPECT_TRUE(isSerialized("blue_background.png", "image/png"));
334 EXPECT_TRUE(isSerialized("purple_background.png", "image/png"));
335 EXPECT_TRUE(isSerialized("pink_background.png", "image/png"));
336 EXPECT_TRUE(isSerialized("brown_background.png", "image/png"));
337 EXPECT_TRUE(isSerialized("ul-dot.png", "image/png"));
338 EXPECT_TRUE(isSerialized("ol-dot.png", "image/png"));
341 TEST_F(PageSerializerTest
, CSSImport
)
343 setBaseFolder("pageserializer/css/");
345 registerURL("import.html", "text/html");
346 registerURL("import/base.css", "text/css");
347 registerURL("import/relative/red-background.css", "text/css");
348 registerURL("import/absolute/green-header.css", "text/css");
350 serialize("import.html");
352 EXPECT_TRUE(isSerialized("import.html", "text/html"));
353 EXPECT_TRUE(isSerialized("import/base.css", "text/css"));
354 EXPECT_TRUE(isSerialized("import/relative/red-background.css", "text/css"));
355 EXPECT_TRUE(isSerialized("import/absolute/green-header.css", "text/css"));
358 TEST_F(PageSerializerTest
, XMLDeclaration
)
360 V8TestingScope
scope(v8::Isolate::GetCurrent());
361 setBaseFolder("pageserializer/xml/");
363 registerURL("xmldecl.xml", "text/xml");
364 serialize("xmldecl.xml");
366 String
expectedStart("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
367 EXPECT_TRUE(getSerializedData("xmldecl.xml").startsWith(expectedStart
));
370 TEST_F(PageSerializerTest
, DTD
)
372 setBaseFolder("pageserializer/dtd/");
374 registerURL("html5.html", "text/html");
375 serialize("html5.html");
377 String
expectedStart("<!DOCTYPE html>");
378 EXPECT_TRUE(getSerializedData("html5.html").startsWith(expectedStart
));
381 TEST_F(PageSerializerTest
, Font
)
383 setBaseFolder("pageserializer/font/");
385 registerURL("font.html", "text/html");
386 registerURL("font.ttf", "application/octet-stream");
388 serialize("font.html");
390 EXPECT_TRUE(isSerialized("font.ttf", "application/octet-stream"));
393 TEST_F(PageSerializerTest
, DataURI
)
395 setBaseFolder("pageserializer/datauri/");
397 registerURL("page_with_data.html", "text/html");
399 serialize("page_with_data.html");
401 EXPECT_EQ(1U, getResources().size());
402 EXPECT_TRUE(isSerialized("page_with_data.html", "text/html"));
405 TEST_F(PageSerializerTest
, DataURIMorphing
)
407 setBaseFolder("pageserializer/datauri/");
409 registerURL("page_with_morphing_data.html", "text/html");
411 serialize("page_with_morphing_data.html");
413 EXPECT_EQ(2U, getResources().size());
414 EXPECT_TRUE(isSerialized("page_with_morphing_data.html", "text/html"));
417 TEST_F(PageSerializerTest
, RewriteLinksSimple
)
419 setBaseFolder("pageserializer/rewritelinks/");
420 setRewriteURLFolder("folder");
422 registerURL("rewritelinks_simple.html", "text/html");
423 registerURL("absolute.png", "image.png", "image/png");
424 registerURL("relative.png", "image.png", "image/png");
425 registerRewriteURL("http://www.test.com/absolute.png", "a.png");
426 registerRewriteURL("http://www.test.com/relative.png", "b.png");
428 serialize("rewritelinks_simple.html");
430 EXPECT_EQ(3U, getResources().size());
431 EXPECT_NE(getSerializedData("rewritelinks_simple.html", "text/html").find("\"folder/a.png\""), kNotFound
);
432 EXPECT_NE(getSerializedData("rewritelinks_simple.html", "text/html").find("\"folder/b.png\""), kNotFound
);
435 TEST_F(PageSerializerTest
, RewriteLinksBase
)
437 setBaseFolder("pageserializer/rewritelinks/");
438 setRewriteURLFolder("folder");
440 registerURL("rewritelinks_base.html", "text/html");
441 registerURL("images/here/image.png", "image.png", "image/png");
442 registerURL("images/here/or/in/here/image.png", "image.png", "image/png");
443 registerURL("or/absolute.png", "image.png", "image/png");
444 registerRewriteURL("http://www.test.com/images/here/image.png", "a.png");
445 registerRewriteURL("http://www.test.com/images/here/or/in/here/image.png", "b.png");
446 registerRewriteURL("http://www.test.com/or/absolute.png", "c.png");
448 serialize("rewritelinks_base.html");
450 EXPECT_EQ(4U, getResources().size());
451 EXPECT_NE(getSerializedData("rewritelinks_base.html", "text/html").find("\"folder/a.png\""), kNotFound
);
452 EXPECT_NE(getSerializedData("rewritelinks_base.html", "text/html").find("\"folder/b.png\""), kNotFound
);
453 EXPECT_NE(getSerializedData("rewritelinks_base.html", "text/html").find("\"folder/c.png\""), kNotFound
);
456 // Test that we don't regress https://bugs.webkit.org/show_bug.cgi?id=99105
457 TEST_F(PageSerializerTest
, SVGImageDontCrash
)
459 setBaseFolder("pageserializer/svg/");
461 registerURL("page_with_svg_image.html", "text/html");
462 registerURL("green_rectangle.svg", "image/svg+xml");
464 serialize("page_with_svg_image.html");
466 EXPECT_EQ(2U, getResources().size());
468 EXPECT_TRUE(isSerialized("green_rectangle.svg", "image/svg+xml"));
469 EXPECT_GT(getSerializedData("green_rectangle.svg", "image/svg+xml").length(), 250U);
472 TEST_F(PageSerializerTest
, DontIncludeErrorImage
)
474 setBaseFolder("pageserializer/image/");
476 registerURL("page_with_img_error.html", "text/html");
477 registerURL("error_image.png", "image/png");
479 serialize("page_with_img_error.html");
481 EXPECT_EQ(1U, getResources().size());
482 EXPECT_TRUE(isSerialized("page_with_img_error.html", "text/html"));
483 EXPECT_FALSE(isSerialized("error_image.png", "image/png"));
486 TEST_F(PageSerializerTest
, NamespaceElementsDontCrash
)
488 setBaseFolder("pageserializer/namespace/");
490 registerURL("namespace_element.html", "text/html");
492 serialize("namespace_element.html");
494 EXPECT_EQ(1U, getResources().size());
495 EXPECT_TRUE(isSerialized("namespace_element.html", "text/html"));
496 EXPECT_GT(getSerializedData("namespace_element.html", "text/html").length(), 0U);