Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / web / 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 #include "config.h"
31
32 #include "core/dom/Document.h"
33 #include "public/platform/Platform.h"
34 #include "public/platform/WebString.h"
35 #include "public/platform/WebThread.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/WebPageSerializer.h"
43 #include "public/web/WebPageSerializerClient.h"
44 #include "public/web/WebScriptSource.h"
45 #include "public/web/WebSettings.h"
46 #include "public/web/WebView.h"
47 #include "web/tests/FrameTestHelpers.h"
48 #include "web/tests/URLTestHelpers.h"
49 #include <gtest/gtest.h>
50
51 using namespace blink;
52 using WebCore::Document;
53 using blink::FrameTestHelpers::runPendingTasks;
54 using blink::URLTestHelpers::toKURL;
55 using blink::URLTestHelpers::registerMockedURLLoad;
56
57 namespace {
58
59 class LineReader {
60 public:
61     LineReader(const std::string& text) : m_text(text), m_index(0) { }
62     bool getNextLine(std::string* line)
63     {
64         line->clear();
65         if (m_index >= m_text.length())
66             return false;
67
68         size_t endOfLineIndex = m_text.find("\r\n", m_index);
69         if (endOfLineIndex == std::string::npos) {
70             *line = m_text.substr(m_index);
71             m_index = m_text.length();
72         } else {
73             *line = m_text.substr(m_index, endOfLineIndex - m_index);
74             m_index = endOfLineIndex + 2;
75         }
76         return true;
77     }
78
79 private:
80     std::string m_text;
81     size_t m_index;
82 };
83
84 class LengthCountingWebPageSerializerClient : public WebPageSerializerClient {
85 public:
86     LengthCountingWebPageSerializerClient(size_t* counter)
87         : m_counter(counter)
88     {
89     }
90
91     virtual void didSerializeDataForFrame(const WebURL& frameURL, const WebCString& data, PageSerializationStatus status) {
92         *m_counter += data.length();
93     }
94
95 private:
96     size_t* m_counter;
97 };
98
99 class FrameDataWebPageSerializerClient : public WebPageSerializerClient {
100 public:
101     FrameDataWebPageSerializerClient(const WebURL& frameURL, WebString* serializationData)
102         : m_frameURL(frameURL)
103         , m_serializationData(serializationData)
104     {
105     }
106
107     virtual void didSerializeDataForFrame(const WebURL& frameURL, const WebCString& data, PageSerializationStatus status)
108     {
109         if (frameURL != m_frameURL)
110             return;
111         *m_serializationData = data.utf16();
112     }
113
114 private:
115     WebURL m_frameURL;
116     WebString* m_serializationData;
117 };
118
119 class WebPageNewSerializeTest : public testing::Test {
120 public:
121     WebPageNewSerializeTest()
122         : m_htmlMimeType(WebString::fromUTF8("text/html"))
123         , m_xhtmlMimeType(WebString::fromUTF8("application/xhtml+xml"))
124         , m_cssMimeType(WebString::fromUTF8("text/css"))
125         , m_pngMimeType(WebString::fromUTF8("image/png"))
126         , m_svgMimeType(WebString::fromUTF8("image/svg+xml"))
127     {
128     }
129
130 protected:
131     virtual void SetUp()
132     {
133         // We want the images to load and JavaScript to be on.
134         m_helper.initialize(true, 0, 0, &configureSettings);
135     }
136
137     virtual void TearDown()
138     {
139         Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
140     }
141
142     WebURL setUpCSSTestPage()
143     {
144         WebURL topFrameURL = toKURL("http://www.test.com");
145         registerMockedURLLoad(topFrameURL, WebString::fromUTF8("css_test_page.html"), WebString::fromUTF8("pageserializer/"), htmlMimeType());
146         registerMockedURLLoad(toKURL("http://www.test.com/link_styles.css"), WebString::fromUTF8("link_styles.css"), WebString::fromUTF8("pageserializer/"), cssMimeType());
147         registerMockedURLLoad(toKURL("http://www.test.com/import_style_from_link.css"), WebString::fromUTF8("import_style_from_link.css"), WebString::fromUTF8("pageserializer/"), cssMimeType());
148         registerMockedURLLoad(toKURL("http://www.test.com/import_styles.css"), WebString::fromUTF8("import_styles.css"), WebString::fromUTF8("pageserializer/"), cssMimeType());
149         registerMockedURLLoad(toKURL("http://www.test.com/red_background.png"), WebString::fromUTF8("red_background.png"), WebString::fromUTF8("pageserializer/"), pngMimeType());
150         registerMockedURLLoad(toKURL("http://www.test.com/orange_background.png"), WebString::fromUTF8("orange_background.png"), WebString::fromUTF8("pageserializer/"), pngMimeType());
151         registerMockedURLLoad(toKURL("http://www.test.com/yellow_background.png"), WebString::fromUTF8("yellow_background.png"), WebString::fromUTF8("pageserializer/"), pngMimeType());
152         registerMockedURLLoad(toKURL("http://www.test.com/green_background.png"), WebString::fromUTF8("green_background.png"), WebString::fromUTF8("pageserializer/"), pngMimeType());
153         registerMockedURLLoad(toKURL("http://www.test.com/blue_background.png"), WebString::fromUTF8("blue_background.png"), WebString::fromUTF8("pageserializer/"), pngMimeType());
154         registerMockedURLLoad(toKURL("http://www.test.com/purple_background.png"), WebString::fromUTF8("purple_background.png"), WebString::fromUTF8("pageserializer/"), pngMimeType());
155         registerMockedURLLoad(toKURL("http://www.test.com/ul-dot.png"), WebString::fromUTF8("ul-dot.png"), WebString::fromUTF8("pageserializer/"), pngMimeType());
156         registerMockedURLLoad(toKURL("http://www.test.com/ol-dot.png"), WebString::fromUTF8("ol-dot.png"), WebString::fromUTF8("pageserializer/"), pngMimeType());
157         return topFrameURL;
158     }
159
160     void loadURLInTopFrame(const WebURL& url)
161     {
162         WebURLRequest urlRequest;
163         urlRequest.initialize();
164         urlRequest.setURL(url);
165         m_helper.webView()->mainFrame()->loadRequest(urlRequest);
166         // Make sure any pending request get served.
167         Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests();
168         // Some requests get delayed, run the timer.
169         runPendingTasks();
170         // Server the delayed resources.
171         Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests();
172     }
173
174     const WebString& htmlMimeType() const { return m_htmlMimeType; }
175     const WebString& xhtmlMimeType() const { return m_xhtmlMimeType; }
176     const WebString& cssMimeType() const { return m_cssMimeType; }
177     const WebString& pngMimeType() const { return m_pngMimeType; }
178     const WebString& svgMimeType() const { return m_svgMimeType; }
179
180     static bool resourceVectorContains(const WebVector<WebPageSerializer::Resource>& resources, const char* url, const char* mimeType)
181     {
182         WebURL webURL = WebURL(toKURL(url));
183         for (size_t i = 0; i < resources.size(); ++i) {
184             const WebPageSerializer::Resource& resource = resources[i];
185             if (resource.url == webURL && !resource.data.isEmpty() && !resource.mimeType.compare(WebCString(mimeType)))
186                 return true;
187         }
188         return false;
189     }
190
191     WebView* webView() const { return m_helper.webView(); }
192
193 private:
194     static void configureSettings(WebSettings* settings)
195     {
196         settings->setImagesEnabled(true);
197         settings->setLoadsImagesAutomatically(true);
198         settings->setJavaScriptEnabled(true);
199     }
200
201     FrameTestHelpers::WebViewHelper m_helper;
202     WebString m_htmlMimeType;
203     WebString m_xhtmlMimeType;
204     WebString m_cssMimeType;
205     WebString m_pngMimeType;
206     WebString m_svgMimeType;
207 };
208
209 // Tests that a page with resources and sub-frame is reported with all its resources.
210 TEST_F(WebPageNewSerializeTest, PageWithFrames)
211 {
212     // Register the mocked frames.
213     WebURL topFrameURL = toKURL("http://www.test.com");
214     registerMockedURLLoad(topFrameURL, WebString::fromUTF8("top_frame.html"), WebString::fromUTF8("pageserializer/"), htmlMimeType());
215     registerMockedURLLoad(toKURL("http://www.test.com/iframe.html"), WebString::fromUTF8("iframe.html"), WebString::fromUTF8("pageserializer/"), htmlMimeType());
216     registerMockedURLLoad(toKURL("http://www.test.com/iframe2.html"), WebString::fromUTF8("iframe2.html"), WebString::fromUTF8("pageserializer/"), htmlMimeType());
217     registerMockedURLLoad(toKURL("http://www.test.com/red_background.png"), WebString::fromUTF8("red_background.png"), WebString::fromUTF8("pageserializer/"), pngMimeType());
218     registerMockedURLLoad(toKURL("http://www.test.com/green_background.png"), WebString::fromUTF8("green_background.png"), WebString::fromUTF8("pageserializer/"), pngMimeType());
219     registerMockedURLLoad(toKURL("http://www.test.com/blue_background.png"), WebString::fromUTF8("blue_background.png"), WebString::fromUTF8("pageserializer/"), pngMimeType());
220
221     loadURLInTopFrame(topFrameURL);
222     // OBJECT/EMBED have some delay to start to load their content. The first
223     // serveAsynchronousMockedRequests call in loadURLInTopFrame() finishes
224     // before the start.
225     RefPtrWillBeRawPtr<Document> document = static_cast<PassRefPtrWillBeRawPtr<Document> >(webView()->mainFrame()->document());
226     document->updateLayoutIgnorePendingStylesheets(Document::RunPostLayoutTasksSynchronously);
227     Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests();
228
229     WebVector<WebPageSerializer::Resource> resources;
230     WebPageSerializer::serialize(webView(), &resources);
231     ASSERT_FALSE(resources.isEmpty());
232
233     // The first resource should be the main-frame.
234     const WebPageSerializer::Resource& resource = resources[0];
235     EXPECT_TRUE(resource.url == WebURL(toKURL("http://www.test.com")));
236     EXPECT_EQ(0, resource.mimeType.compare(WebCString("text/html")));
237     EXPECT_FALSE(resource.data.isEmpty());
238
239     EXPECT_EQ(6U, resources.size()); // There should be no duplicates.
240     EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/red_background.png", "image/png"));
241     EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/green_background.png", "image/png"));
242     EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/blue_background.png", "image/png"));
243     EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/iframe.html", "text/html"));
244     EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/iframe2.html", "text/html"));
245 }
246
247 // Test that when serializing a page, all CSS resources are reported, including url()'s
248 // and imports and links. Note that we don't test the resources contents, we only make sure
249 // they are all reported with the right mime type and that they contain some data.
250 TEST_F(WebPageNewSerializeTest, FAILS_CSSResources)
251 {
252     // Register the mocked frame and load it.
253     WebURL topFrameURL = setUpCSSTestPage();
254     loadURLInTopFrame(topFrameURL);
255
256     WebVector<WebPageSerializer::Resource> resources;
257     WebPageSerializer::serialize(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(12U, resources.size()); // There should be no duplicates.
267     EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/link_styles.css", "text/css"));
268     EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/import_styles.css", "text/css"));
269     EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/import_style_from_link.css", "text/css"));
270     EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/red_background.png", "image/png"));
271     EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/orange_background.png", "image/png"));
272     EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/yellow_background.png", "image/png"));
273     EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/green_background.png", "image/png"));
274     EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/blue_background.png", "image/png"));
275     EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/purple_background.png", "image/png"));
276     EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/ul-dot.png", "image/png"));
277     EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/ol-dot.png", "image/png"));
278 }
279
280 // Tests that when serializing a page with blank frames these are reported with their resources.
281 TEST_F(WebPageNewSerializeTest, BlankFrames)
282 {
283     // Register the mocked frame and load it.
284     WebURL topFrameURL = toKURL("http://www.test.com");
285     registerMockedURLLoad(topFrameURL, WebString::fromUTF8("blank_frames.html"), WebString::fromUTF8("pageserializer/"), htmlMimeType());
286     registerMockedURLLoad(toKURL("http://www.test.com/red_background.png"), WebString::fromUTF8("red_background.png"), WebString::fromUTF8("pageserializer/"), pngMimeType());
287     registerMockedURLLoad(toKURL("http://www.test.com/orange_background.png"), WebString::fromUTF8("orange_background.png"), WebString::fromUTF8("pageserializer/"), pngMimeType());
288     registerMockedURLLoad(toKURL("http://www.test.com/blue_background.png"), WebString::fromUTF8("blue_background.png"), WebString::fromUTF8("pageserializer/"), pngMimeType());
289
290     loadURLInTopFrame(topFrameURL);
291
292     WebVector<WebPageSerializer::Resource> resources;
293     WebPageSerializer::serialize(webView(), &resources);
294     ASSERT_FALSE(resources.isEmpty());
295
296     // The first resource should be the main-frame.
297     const WebPageSerializer::Resource& resource = resources[0];
298     EXPECT_TRUE(resource.url == WebURL(toKURL("http://www.test.com")));
299     EXPECT_EQ(0, resource.mimeType.compare(WebCString("text/html")));
300     EXPECT_FALSE(resource.data.isEmpty());
301
302     EXPECT_EQ(7U, resources.size()); // There should be no duplicates.
303     EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/red_background.png", "image/png"));
304     EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/orange_background.png", "image/png"));
305     EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/blue_background.png", "image/png"));
306     // The blank frames should have got a magic URL.
307     EXPECT_TRUE(resourceVectorContains(resources, "wyciwyg://frame/0", "text/html"));
308     EXPECT_TRUE(resourceVectorContains(resources, "wyciwyg://frame/1", "text/html"));
309     EXPECT_TRUE(resourceVectorContains(resources, "wyciwyg://frame/2", "text/html"));
310 }
311
312 TEST_F(WebPageNewSerializeTest, SerializeXMLHasRightDeclaration)
313 {
314     WebURL topFrameURL = toKURL("http://www.test.com/simple.xhtml");
315     registerMockedURLLoad(topFrameURL, WebString::fromUTF8("simple.xhtml"), WebString::fromUTF8("pageserializer/"), xhtmlMimeType());
316
317     loadURLInTopFrame(topFrameURL);
318
319     WebVector<WebPageSerializer::Resource> resources;
320     WebPageSerializer::serialize(webView(), &resources);
321     ASSERT_FALSE(resources.isEmpty());
322
323     // We expect only one resource, the XML.
324     ASSERT_EQ(1U, resources.size());
325     std::string xml = std::string(resources[0].data.data());
326
327     // We should have one and only one instance of the XML declaration.
328     size_t pos = xml.find("<?xml version=");
329     ASSERT_TRUE(pos != std::string::npos);
330
331     pos = xml.find("<?xml version=", pos + 1);
332     ASSERT_TRUE(pos == std::string::npos);
333 }
334
335 TEST_F(WebPageNewSerializeTest, FAILS_TestMHTMLEncoding)
336 {
337     // Load a page with some CSS and some images.
338     WebURL topFrameURL = setUpCSSTestPage();
339     loadURLInTopFrame(topFrameURL);
340
341     WebCString mhtmlData = WebPageSerializer::serializeToMHTML(webView());
342     ASSERT_FALSE(mhtmlData.isEmpty());
343
344     // Read the MHTML data line per line and do some pseudo-parsing to make sure the right encoding is used for the different sections.
345     LineReader lineReader(std::string(mhtmlData.data()));
346     int sectionCheckedCount = 0;
347     const char* expectedEncoding = 0;
348     std::string line;
349     while (lineReader.getNextLine(&line)) {
350         if (!line.find("Content-Type:")) {
351             ASSERT_FALSE(expectedEncoding);
352             if (line.find("multipart/related;") != std::string::npos) {
353                 // Skip this one, it's part of the MHTML header.
354                 continue;
355             }
356             if (line.find("text/") != std::string::npos)
357                 expectedEncoding = "quoted-printable";
358             else if (line.find("image/") != std::string::npos)
359                 expectedEncoding = "base64";
360             else
361                 FAIL() << "Unexpected Content-Type: " << line;
362             continue;
363         }
364         if (!line.find("Content-Transfer-Encoding:")) {
365            ASSERT_TRUE(expectedEncoding);
366            EXPECT_TRUE(line.find(expectedEncoding) != std::string::npos);
367            expectedEncoding = 0;
368            sectionCheckedCount++;
369         }
370     }
371     EXPECT_EQ(12, sectionCheckedCount);
372 }
373
374 // Test that we don't regress https://bugs.webkit.org/show_bug.cgi?id=99105
375 TEST_F(WebPageNewSerializeTest, SVGImageDontCrash)
376 {
377     WebURL pageUrl = toKURL("http://www.test.com");
378     WebURL imageUrl = toKURL("http://www.test.com/green_rectangle.svg");
379
380     registerMockedURLLoad(pageUrl, WebString::fromUTF8("page_with_svg_image.html"), WebString::fromUTF8("pageserializer/"), htmlMimeType());
381     registerMockedURLLoad(imageUrl, WebString::fromUTF8("green_rectangle.svg"), WebString::fromUTF8("pageserializer/"), svgMimeType());
382
383     loadURLInTopFrame(pageUrl);
384
385     WebCString mhtml = WebPageSerializer::serializeToMHTML(webView());
386     // We expect some data to be generated.
387     EXPECT_GT(mhtml.length(), 50U);
388 }
389
390
391 TEST_F(WebPageNewSerializeTest, DontIncludeErrorImage)
392 {
393     WebURL pageUrl = toKURL("http://www.test.com");
394     WebURL imageUrl = toKURL("http://www.test.com/error_image.png");
395
396     registerMockedURLLoad(pageUrl, WebString::fromUTF8("page_with_img_error.html"), WebString::fromUTF8("pageserializer/"), htmlMimeType());
397     registerMockedURLLoad(imageUrl, WebString::fromUTF8("error_image.png"), WebString::fromUTF8("pageserializer/"), pngMimeType());
398
399     loadURLInTopFrame(pageUrl);
400
401     WebCString mhtmlData = WebPageSerializer::serializeToMHTML(webView());
402     ASSERT_FALSE(mhtmlData.isEmpty());
403
404     // Sniff the MHTML data to make sure image content is excluded.
405     LineReader lineReader(std::string(mhtmlData.data()));
406     std::string line;
407     while (lineReader.getNextLine(&line)) {
408         if (line.find("image/") != std::string::npos)
409             FAIL() << "Error Image was not excluded " << line;
410     }
411 }
412
413
414 TEST_F(WebPageNewSerializeTest, NamespaceElementsDontCrash)
415 {
416     WebURL pageUrl = toKURL("http://www.test.com");
417     registerMockedURLLoad(pageUrl, WebString::fromUTF8("namespace_element.html"), WebString::fromUTF8("pageserializer/"), htmlMimeType());
418
419     loadURLInTopFrame(pageUrl);
420
421     WebVector<WebURL> localLinks(static_cast<size_t>(1));
422     WebVector<WebString> localPaths(static_cast<size_t>(1));
423     localLinks[0] = pageUrl;
424     localPaths[0] = WebString("/");
425
426     size_t counter = 0;
427     LengthCountingWebPageSerializerClient client(&counter);
428
429     // We just want to make sure nothing crazy happens, namely that no
430     // assertions are hit. As a sanity check, we also make sure that some data
431     // was returned.
432     WebPageSerializer::serialize(webView()->mainFrame()->toWebLocalFrame(), true, &client, localLinks, localPaths, WebString(""));
433
434     EXPECT_GT(counter, 0U);
435 }
436
437 TEST_F(WebPageNewSerializeTest, SubFrameSerialization)
438 {
439     WebURL pageUrl = toKURL("http://www.test.com");
440     registerMockedURLLoad(pageUrl, WebString::fromUTF8("top_frame.html"), WebString::fromUTF8("pageserializer/"), htmlMimeType());
441     registerMockedURLLoad(toKURL("http://www.test.com/iframe.html"), WebString::fromUTF8("iframe.html"), WebString::fromUTF8("pageserializer/"), htmlMimeType());
442     registerMockedURLLoad(toKURL("http://www.test.com/iframe2.html"), WebString::fromUTF8("iframe2.html"), WebString::fromUTF8("pageserializer/"), htmlMimeType());
443     registerMockedURLLoad(toKURL("http://www.test.com/red_background.png"), WebString::fromUTF8("red_background.png"), WebString::fromUTF8("pageserializer/"), pngMimeType());
444     registerMockedURLLoad(toKURL("http://www.test.com/green_background.png"), WebString::fromUTF8("green_background.png"), WebString::fromUTF8("pageserializer/"), pngMimeType());
445     registerMockedURLLoad(toKURL("http://www.test.com/blue_background.png"), WebString::fromUTF8("blue_background.png"), WebString::fromUTF8("pageserializer/"), pngMimeType());
446
447     loadURLInTopFrame(pageUrl);
448
449     // OBJECT/EMBED have some delay to start to load their content. The first
450     // serveAsynchronousMockedRequests call in loadURLInTopFrame() finishes
451     // before the start.
452     RefPtrWillBeRawPtr<Document> document = static_cast<PassRefPtrWillBeRawPtr<Document> >(webView()->mainFrame()->document());
453     document->updateLayoutIgnorePendingStylesheets(Document::RunPostLayoutTasksSynchronously);
454     Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests();
455
456     WebVector<WebURL> localLinks(static_cast<size_t>(2));
457     WebVector<WebString> localPaths(static_cast<size_t>(2));
458     localLinks[0] = pageUrl;
459     localPaths[0] = WebString("/");
460     localLinks[1] = toKURL("http://www.test.com/iframe.html");
461     localPaths[1] = WebString("SavedFiles/iframe.html");
462
463     WebString serializedData;
464     FrameDataWebPageSerializerClient client(pageUrl, &serializedData);
465
466     // We just want to make sure nothing crazy happens, namely that no
467     // assertions are hit. As a sanity check, we also make sure that some data
468     // was returned.
469     WebPageSerializer::serialize(webView()->mainFrame()->toWebLocalFrame(), true, &client, localLinks, localPaths, WebString(""));
470
471     // Subframe src
472     EXPECT_TRUE(static_cast<String>(serializedData).contains("src=\"SavedFiles/iframe.html\""));
473 }
474
475 }
476
477 TEST_F(WebPageNewSerializeTest, TestMHTMLEncodingWithDataURL)
478 {
479     // Load a page with some data urls.
480     WebURL topFrameURL = toKURL("http://www.test.com");
481     registerMockedURLLoad(topFrameURL, WebString::fromUTF8("page_with_data.html"), WebString::fromUTF8("pageserializer/"), htmlMimeType());
482     loadURLInTopFrame(topFrameURL);
483
484     WebCString mhtmlData = WebPageSerializer::serializeToMHTML(webView());
485     ASSERT_FALSE(mhtmlData.isEmpty());
486
487     // Read the MHTML data line and check that the string data:image is found
488     // exactly one time.
489     size_t nbDataURLs = 0;
490     LineReader lineReader(std::string(mhtmlData.data()));
491     std::string line;
492     while (lineReader.getNextLine(&line)) {
493         if (line.find("data:image") != std::string::npos)
494             nbDataURLs++;
495     }
496     EXPECT_EQ(1u, nbDataURLs);
497 }
498
499
500 TEST_F(WebPageNewSerializeTest, TestMHTMLEncodingWithMorphingDataURL)
501 {
502     // Load a page with some data urls.
503     WebURL topFrameURL = toKURL("http://www.test.com");
504     registerMockedURLLoad(topFrameURL, WebString::fromUTF8("page_with_morphing_data.html"), WebString::fromUTF8("pageserializer/"), htmlMimeType());
505     loadURLInTopFrame(topFrameURL);
506
507     WebCString mhtmlData = WebPageSerializer::serializeToMHTML(webView());
508     ASSERT_FALSE(mhtmlData.isEmpty());
509
510     // Read the MHTML data line and check that the string data:image is found
511     // exactly two times.
512     size_t nbDataURLs = 0;
513     LineReader lineReader(std::string(mhtmlData.data()));
514     std::string line;
515     while (lineReader.getNextLine(&line)) {
516         if (line.find("data:text") != std::string::npos)
517             nbDataURLs++;
518     }
519     EXPECT_EQ(2u, nbDataURLs);
520 }