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::FrameTestHelpers::runPendingTasks;
54 using blink::URLTestHelpers::toKURL;
55 using blink::URLTestHelpers::registerMockedURLLoad;
59 class PageSerializerTest : public testing::Test {
62 : m_folder(WebString::fromUTF8("pageserializer/"))
63 , m_baseUrl(toKURL("http://www.test.com"))
70 // We want the images to load and JavaScript to be on.
71 m_helper.initialize(true, 0, 0, &configureSettings);
74 virtual void TearDown()
76 Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
79 void setBaseUrl(const char* url)
81 m_baseUrl = toKURL(url);
84 void setBaseFolder(const char* folder)
86 m_folder = WebString::fromUTF8(folder);
89 void registerURL(const char* file, const char* mimeType)
91 registerMockedURLLoad(KURL(m_baseUrl, file), WebString::fromUTF8(file), m_folder, WebString::fromUTF8(mimeType));
94 void registerErrorURL(const char* file, int statusCode)
97 error.reason = 0xdead + statusCode;
98 error.domain = "PageSerializerTest";
100 WebURLResponse response;
101 response.initialize();
102 response.setMIMEType("text/html");
103 response.setHTTPStatusCode(statusCode);
105 Platform::current()->unitTestSupport()->registerMockedErrorURL(KURL(m_baseUrl, file), response, error);
108 void serialize(const char* url)
110 WebURLRequest urlRequest;
111 urlRequest.initialize();
112 urlRequest.setURL(KURL(m_baseUrl, url));
113 m_helper.webView()->mainFrame()->loadRequest(urlRequest);
114 // Make sure any pending request get served.
115 Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests();
116 // Some requests get delayed, run the timer.
118 // Server the delayed resources.
119 Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests();
121 PageSerializer serializer(&m_resources);
122 serializer.serialize(m_helper.webViewImpl()->mainFrameImpl()->frame()->page());
125 Vector<SerializedResource>& getResources()
131 const SerializedResource* getResource(const char* url, const char* mimeType)
133 KURL kURL = KURL(m_baseUrl, url);
134 String mime(mimeType);
135 for (size_t i = 0; i < m_resources.size(); ++i) {
136 const SerializedResource& resource = m_resources[i];
137 if (resource.url == kURL && !resource.data->isEmpty()
138 && (mime.isNull() || equalIgnoringCase(resource.mimeType, mime)))
144 bool isSerialized(const char* url, const char* mimeType = 0)
146 return getResource(url, mimeType);
149 String getSerializedData(const char* url, const char* mimeType = 0)
151 const SerializedResource* resource = getResource(url, mimeType);
153 return String(resource->data->data(), resource->data->size());
158 static void configureSettings(WebSettings* settings)
160 settings->setImagesEnabled(true);
161 settings->setLoadsImagesAutomatically(true);
162 settings->setJavaScriptEnabled(true);
165 FrameTestHelpers::WebViewHelper m_helper;
168 Vector<SerializedResource> m_resources;
172 TEST_F(PageSerializerTest, InputImage)
174 setBaseFolder("pageserializer/input-image/");
176 registerURL("input-image.html", "text/html");
177 registerURL("button.png", "image/png");
178 registerErrorURL("non-existing-button.png", 404);
180 serialize("input-image.html");
182 EXPECT_TRUE(isSerialized("button.png", "image/png"));
183 EXPECT_FALSE(isSerialized("non-existing-button.png", "image/png"));
186 TEST_F(PageSerializerTest, XMLDeclaration)
188 setBaseFolder("pageserializer/xmldecl/");
190 registerURL("xmldecl.xml", "text/xml");
191 serialize("xmldecl.xml");
193 String expectedStart("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
194 EXPECT_TRUE(getSerializedData("xmldecl.xml").startsWith(expectedStart));
197 TEST_F(PageSerializerTest, DTD)
199 setBaseFolder("pageserializer/dtd/");
201 registerURL("dtd.html", "text/html");
202 serialize("dtd.html");
204 String expectedStart("<!DOCTYPE html>");
205 EXPECT_TRUE(getSerializedData("dtd.html").startsWith(expectedStart));
208 TEST_F(PageSerializerTest, Font)
210 setBaseFolder("pageserializer/font/");
212 registerURL("font.html", "text/html");
213 registerURL("font.ttf", "application/octet-stream");
215 serialize("font.html");
217 EXPECT_TRUE(isSerialized("font.ttf", "application/octet-stream"));