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.
33 #include "core/page/Page.h"
34 #include "core/page/PageSerializer.h"
35 #include "platform/SerializedResource.h"
36 #include "public/platform/Platform.h"
37 #include "public/platform/WebString.h"
38 #include "public/platform/WebThread.h"
39 #include "public/platform/WebURL.h"
40 #include "public/platform/WebURLRequest.h"
41 #include "public/platform/WebURLResponse.h"
42 #include "public/platform/WebUnitTestSupport.h"
43 #include "public/web/WebSettings.h"
44 #include "web/WebLocalFrameImpl.h"
45 #include "web/WebViewImpl.h"
46 #include "web/tests/FrameTestHelpers.h"
47 #include "web/tests/URLTestHelpers.h"
48 #include "wtf/Vector.h"
49 #include <gtest/gtest.h>
51 using namespace WebCore;
52 using namespace blink;
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 virtual void TearDown()
75 Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
78 void setBaseUrl(const char* url)
80 m_baseUrl = toKURL(url);
83 void setBaseFolder(const char* folder)
85 m_folder = WebString::fromUTF8(folder);
88 void registerURL(const char* file, const char* mimeType)
90 registerMockedURLLoad(KURL(m_baseUrl, file), WebString::fromUTF8(file), m_folder, WebString::fromUTF8(mimeType));
93 void registerErrorURL(const char* file, int statusCode)
96 error.reason = 0xdead + statusCode;
97 error.domain = "PageSerializerTest";
99 WebURLResponse response;
100 response.initialize();
101 response.setMIMEType("text/html");
102 response.setHTTPStatusCode(statusCode);
104 Platform::current()->unitTestSupport()->registerMockedErrorURL(KURL(m_baseUrl, file), response, error);
107 void serialize(const char* url)
109 FrameTestHelpers::loadFrame(m_helper.webView()->mainFrame(), KURL(m_baseUrl, url).string().utf8().data());
110 PageSerializer serializer(&m_resources);
111 serializer.serialize(m_helper.webViewImpl()->mainFrameImpl()->frame()->page());
114 Vector<SerializedResource>& getResources()
120 const SerializedResource* getResource(const char* url, const char* mimeType)
122 KURL kURL = KURL(m_baseUrl, url);
123 String mime(mimeType);
124 for (size_t i = 0; i < m_resources.size(); ++i) {
125 const SerializedResource& resource = m_resources[i];
126 if (resource.url == kURL && !resource.data->isEmpty()
127 && (mime.isNull() || equalIgnoringCase(resource.mimeType, mime)))
133 bool isSerialized(const char* url, const char* mimeType = 0)
135 return getResource(url, mimeType);
138 String getSerializedData(const char* url, const char* mimeType = 0)
140 const SerializedResource* resource = getResource(url, mimeType);
142 return String(resource->data->data(), resource->data->size());
147 static void configureSettings(WebSettings* settings)
149 settings->setImagesEnabled(true);
150 settings->setLoadsImagesAutomatically(true);
151 settings->setJavaScriptEnabled(true);
154 FrameTestHelpers::WebViewHelper m_helper;
157 Vector<SerializedResource> m_resources;
161 TEST_F(PageSerializerTest, InputImage)
163 setBaseFolder("pageserializer/input-image/");
165 registerURL("input-image.html", "text/html");
166 registerURL("button.png", "image/png");
167 registerErrorURL("non-existing-button.png", 404);
169 serialize("input-image.html");
171 EXPECT_TRUE(isSerialized("button.png", "image/png"));
172 EXPECT_FALSE(isSerialized("non-existing-button.png", "image/png"));
175 TEST_F(PageSerializerTest, XMLDeclaration)
177 setBaseFolder("pageserializer/xmldecl/");
179 registerURL("xmldecl.xml", "text/xml");
180 serialize("xmldecl.xml");
182 String expectedStart("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
183 EXPECT_TRUE(getSerializedData("xmldecl.xml").startsWith(expectedStart));
186 TEST_F(PageSerializerTest, DTD)
188 setBaseFolder("pageserializer/dtd/");
190 registerURL("dtd.html", "text/html");
191 serialize("dtd.html");
193 String expectedStart("<!DOCTYPE html>");
194 EXPECT_TRUE(getSerializedData("dtd.html").startsWith(expectedStart));
197 TEST_F(PageSerializerTest, Font)
199 setBaseFolder("pageserializer/font/");
201 registerURL("font.html", "text/html");
202 registerURL("font.ttf", "application/octet-stream");
204 serialize("font.html");
206 EXPECT_TRUE(isSerialized("font.ttf", "application/octet-stream"));