Upstream version 8.37.180.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::URLTestHelpers::toKURL;
54 using blink::URLTestHelpers::registerMockedURLLoad;
55
56 namespace {
57
58 class PageSerializerTest : public testing::Test {
59 public:
60     PageSerializerTest()
61         : m_folder(WebString::fromUTF8("pageserializer/"))
62         , m_baseUrl(toKURL("http://www.test.com"))
63     {
64     }
65
66 protected:
67     virtual void SetUp()
68     {
69         // We want the images to load and JavaScript to be on.
70         m_helper.initialize(true, 0, 0, &configureSettings);
71     }
72
73     virtual void TearDown()
74     {
75         Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
76     }
77
78     void setBaseUrl(const char* url)
79     {
80         m_baseUrl = toKURL(url);
81     }
82
83     void setBaseFolder(const char* folder)
84     {
85         m_folder = WebString::fromUTF8(folder);
86     }
87
88     void registerURL(const char* file, const char* mimeType)
89     {
90         registerMockedURLLoad(KURL(m_baseUrl, file), WebString::fromUTF8(file), m_folder, WebString::fromUTF8(mimeType));
91     }
92
93     void registerErrorURL(const char* file, int statusCode)
94     {
95         WebURLError error;
96         error.reason = 0xdead + statusCode;
97         error.domain = "PageSerializerTest";
98
99         WebURLResponse response;
100         response.initialize();
101         response.setMIMEType("text/html");
102         response.setHTTPStatusCode(statusCode);
103
104         Platform::current()->unitTestSupport()->registerMockedErrorURL(KURL(m_baseUrl, file), response, error);
105     }
106
107     void serialize(const char* url)
108     {
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());
112     }
113
114     Vector<SerializedResource>& getResources()
115     {
116         return m_resources;
117     }
118
119
120     const SerializedResource* getResource(const char* url, const char* mimeType)
121     {
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)))
128                 return &resource;
129         }
130         return 0;
131     }
132
133     bool isSerialized(const char* url, const char* mimeType = 0)
134     {
135         return getResource(url, mimeType);
136     }
137
138     String getSerializedData(const char* url, const char* mimeType = 0)
139     {
140         const SerializedResource* resource = getResource(url, mimeType);
141         if (resource)
142             return String(resource->data->data(), resource->data->size());
143         return String();
144     }
145
146 private:
147     static void configureSettings(WebSettings* settings)
148     {
149         settings->setImagesEnabled(true);
150         settings->setLoadsImagesAutomatically(true);
151         settings->setJavaScriptEnabled(true);
152     }
153
154     FrameTestHelpers::WebViewHelper m_helper;
155     WebString m_folder;
156     KURL m_baseUrl;
157     Vector<SerializedResource> m_resources;
158 };
159
160
161 TEST_F(PageSerializerTest, InputImage)
162 {
163     setBaseFolder("pageserializer/input-image/");
164
165     registerURL("input-image.html", "text/html");
166     registerURL("button.png", "image/png");
167     registerErrorURL("non-existing-button.png", 404);
168
169     serialize("input-image.html");
170
171     EXPECT_TRUE(isSerialized("button.png", "image/png"));
172     EXPECT_FALSE(isSerialized("non-existing-button.png", "image/png"));
173 }
174
175 TEST_F(PageSerializerTest, XMLDeclaration)
176 {
177     setBaseFolder("pageserializer/xmldecl/");
178
179     registerURL("xmldecl.xml", "text/xml");
180     serialize("xmldecl.xml");
181
182     String expectedStart("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
183     EXPECT_TRUE(getSerializedData("xmldecl.xml").startsWith(expectedStart));
184 }
185
186 TEST_F(PageSerializerTest, DTD)
187 {
188     setBaseFolder("pageserializer/dtd/");
189
190     registerURL("dtd.html", "text/html");
191     serialize("dtd.html");
192
193     String expectedStart("<!DOCTYPE html>");
194     EXPECT_TRUE(getSerializedData("dtd.html").startsWith(expectedStart));
195 }
196
197 TEST_F(PageSerializerTest, Font)
198 {
199     setBaseFolder("pageserializer/font/");
200
201     registerURL("font.html", "text/html");
202     registerURL("font.ttf", "application/octet-stream");
203
204     serialize("font.html");
205
206     EXPECT_TRUE(isSerialized("font.ttf", "application/octet-stream"));
207 }
208
209 }