Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / web / tests / PageSerializerTest.cpp
1 /*
2  * Copyright (c) 2013, Opera Software ASA. 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
6  * are met:
7  *
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.
16  *
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.
29  */
30
31 #include "config.h"
32
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>
50
51 using namespace WebCore;
52 using namespace blink;
53 using blink::FrameTestHelpers::runPendingTasks;
54 using blink::URLTestHelpers::toKURL;
55 using blink::URLTestHelpers::registerMockedURLLoad;
56
57 namespace {
58
59 class PageSerializerTest : public testing::Test {
60 public:
61     PageSerializerTest()
62         : m_folder(WebString::fromUTF8("pageserializer/"))
63         , m_baseUrl(toKURL("http://www.test.com"))
64     {
65     }
66
67 protected:
68     virtual void SetUp()
69     {
70         // We want the images to load and JavaScript to be on.
71         m_helper.initialize(true, 0, 0, &configureSettings);
72     }
73
74     virtual void TearDown()
75     {
76         Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
77     }
78
79     void setBaseUrl(const char* url)
80     {
81         m_baseUrl = toKURL(url);
82     }
83
84     void setBaseFolder(const char* folder)
85     {
86         m_folder = WebString::fromUTF8(folder);
87     }
88
89     void registerURL(const char* file, const char* mimeType)
90     {
91         registerMockedURLLoad(KURL(m_baseUrl, file), WebString::fromUTF8(file), m_folder, WebString::fromUTF8(mimeType));
92     }
93
94     void registerErrorURL(const char* file, int statusCode)
95     {
96         WebURLError error;
97         error.reason = 0xdead + statusCode;
98         error.domain = "PageSerializerTest";
99
100         WebURLResponse response;
101         response.initialize();
102         response.setMIMEType("text/html");
103         response.setHTTPStatusCode(statusCode);
104
105         Platform::current()->unitTestSupport()->registerMockedErrorURL(KURL(m_baseUrl, file), response, error);
106     }
107
108     void serialize(const char* url)
109     {
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.
117         runPendingTasks();
118         // Server the delayed resources.
119         Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests();
120
121         PageSerializer serializer(&m_resources);
122         serializer.serialize(m_helper.webViewImpl()->mainFrameImpl()->frame()->page());
123     }
124
125     Vector<SerializedResource>& getResources()
126     {
127         return m_resources;
128     }
129
130
131     const SerializedResource* getResource(const char* url, const char* mimeType)
132     {
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)))
139                 return &resource;
140         }
141         return 0;
142     }
143
144     bool isSerialized(const char* url, const char* mimeType = 0)
145     {
146         return getResource(url, mimeType);
147     }
148
149     String getSerializedData(const char* url, const char* mimeType = 0)
150     {
151         const SerializedResource* resource = getResource(url, mimeType);
152         if (resource)
153             return String(resource->data->data(), resource->data->size());
154         return String();
155     }
156
157 private:
158     static void configureSettings(WebSettings* settings)
159     {
160         settings->setImagesEnabled(true);
161         settings->setLoadsImagesAutomatically(true);
162         settings->setJavaScriptEnabled(true);
163     }
164
165     FrameTestHelpers::WebViewHelper m_helper;
166     WebString m_folder;
167     KURL m_baseUrl;
168     Vector<SerializedResource> m_resources;
169 };
170
171
172 TEST_F(PageSerializerTest, InputImage)
173 {
174     setBaseFolder("pageserializer/input-image/");
175
176     registerURL("input-image.html", "text/html");
177     registerURL("button.png", "image/png");
178     registerErrorURL("non-existing-button.png", 404);
179
180     serialize("input-image.html");
181
182     EXPECT_TRUE(isSerialized("button.png", "image/png"));
183     EXPECT_FALSE(isSerialized("non-existing-button.png", "image/png"));
184 }
185
186 TEST_F(PageSerializerTest, XMLDeclaration)
187 {
188     setBaseFolder("pageserializer/xmldecl/");
189
190     registerURL("xmldecl.xml", "text/xml");
191     serialize("xmldecl.xml");
192
193     String expectedStart("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
194     EXPECT_TRUE(getSerializedData("xmldecl.xml").startsWith(expectedStart));
195 }
196
197 TEST_F(PageSerializerTest, DTD)
198 {
199     setBaseFolder("pageserializer/dtd/");
200
201     registerURL("dtd.html", "text/html");
202     serialize("dtd.html");
203
204     String expectedStart("<!DOCTYPE html>");
205     EXPECT_TRUE(getSerializedData("dtd.html").startsWith(expectedStart));
206 }
207
208 TEST_F(PageSerializerTest, Font)
209 {
210     setBaseFolder("pageserializer/font/");
211
212     registerURL("font.html", "text/html");
213     registerURL("font.ttf", "application/octet-stream");
214
215     serialize("font.html");
216
217     EXPECT_TRUE(isSerialized("font.ttf", "application/octet-stream"));
218 }
219
220 }