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