Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(459)

Side by Side Diff: Source/web/tests/WebPageNewSerializerTest.cpp

Issue 68613003: Merges the two different page serializers (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 "FrameTestHelpers.h"
33 #include "URLTestHelpers.h"
34 #include "WebFrame.h"
35 #include "WebFrameClient.h"
36 #include "WebPageSerializer.h"
37 #include "WebPageSerializerClient.h"
38 #include "WebScriptSource.h"
39 #include "WebSettings.h"
40 #include "WebView.h"
41 #include <gtest/gtest.h>
42 #include "public/platform/Platform.h"
43 #include "public/platform/WebString.h"
44 #include "public/platform/WebThread.h"
45 #include "public/platform/WebURL.h"
46 #include "public/platform/WebURLRequest.h"
47 #include "public/platform/WebURLResponse.h"
48 #include "public/platform/WebUnitTestSupport.h"
49
50 using namespace blink;
51 using blink::FrameTestHelpers::runPendingTasks;
52 using blink::URLTestHelpers::toKURL;
53 using blink::URLTestHelpers::registerMockedURLLoad;
54
55 namespace {
56
57 class LineReader {
58 public:
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
77 private:
78 std::string m_text;
79 size_t m_index;
80 };
81
82 class TestWebFrameClient : public WebFrameClient {
83 public:
84 virtual ~TestWebFrameClient() { }
85 };
86
87 class LengthCountingWebPageSerializerClient : public WebPageSerializerClient {
88 public:
89 LengthCountingWebPageSerializerClient(size_t* counter)
90 : m_counter(counter)
91 {
92 }
93
94 virtual void didSerializeDataForFrame(const WebURL& frameURL, const WebCStri ng& data, PageSerializationStatus status) {
95 *m_counter += data.length();
96 }
97
98 private:
99 size_t* m_counter;
100 };
101
102 class WebPageNewSerializeTest : public testing::Test {
103 public:
104 WebPageNewSerializeTest()
105 : m_htmlMimeType(WebString::fromUTF8("text/html"))
106 , m_xhtmlMimeType(WebString::fromUTF8("application/xhtml+xml"))
107 , m_cssMimeType(WebString::fromUTF8("text/css"))
108 , m_pngMimeType(WebString::fromUTF8("image/png"))
109 , m_svgMimeType(WebString::fromUTF8("image/svg+xml"))
110 {
111 }
112
113 protected:
114 virtual void SetUp()
115 {
116 // Create and initialize the WebView.
117 m_webView = WebView::create(0);
118 m_mainFrame = WebFrame::create(&m_webFrameClient);
119
120 // We want the images to load and JavaScript to be on.
121 WebSettings* settings = m_webView->settings();
122 settings->setImagesEnabled(true);
123 settings->setLoadsImagesAutomatically(true);
124 settings->setJavaScriptEnabled(true);
125
126 m_webView->setMainFrame(m_mainFrame);
127 }
128
129 virtual void TearDown()
130 {
131 Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
132 m_webView->close();
133 m_mainFrame->close();
134 }
135
136 WebURL setUpCSSTestPage()
137 {
138 WebURL topFrameURL = toKURL("http://www.test.com");
139 registerMockedURLLoad(topFrameURL, WebString::fromUTF8("css_test_page.ht ml"), WebString::fromUTF8("pageserializer/"), htmlMimeType());
140 registerMockedURLLoad(toKURL("http://www.test.com/link_styles.css"), Web String::fromUTF8("link_styles.css"), WebString::fromUTF8("pageserializer/"), css MimeType());
141 registerMockedURLLoad(toKURL("http://www.test.com/import_style_from_link .css"), WebString::fromUTF8("import_style_from_link.css"), WebString::fromUTF8(" pageserializer/"), cssMimeType());
142 registerMockedURLLoad(toKURL("http://www.test.com/import_styles.css"), W ebString::fromUTF8("import_styles.css"), WebString::fromUTF8("pageserializer/"), cssMimeType());
143 registerMockedURLLoad(toKURL("http://www.test.com/red_background.png"), WebString::fromUTF8("red_background.png"), WebString::fromUTF8("pageserializer/" ), pngMimeType());
144 registerMockedURLLoad(toKURL("http://www.test.com/orange_background.png" ), WebString::fromUTF8("orange_background.png"), WebString::fromUTF8("pageserial izer/"), pngMimeType());
145 registerMockedURLLoad(toKURL("http://www.test.com/yellow_background.png" ), WebString::fromUTF8("yellow_background.png"), WebString::fromUTF8("pageserial izer/"), pngMimeType());
146 registerMockedURLLoad(toKURL("http://www.test.com/green_background.png") , WebString::fromUTF8("green_background.png"), WebString::fromUTF8("pageserializ er/"), pngMimeType());
147 registerMockedURLLoad(toKURL("http://www.test.com/blue_background.png"), WebString::fromUTF8("blue_background.png"), WebString::fromUTF8("pageserializer /"), pngMimeType());
148 registerMockedURLLoad(toKURL("http://www.test.com/purple_background.png" ), WebString::fromUTF8("purple_background.png"), WebString::fromUTF8("pageserial izer/"), pngMimeType());
149 registerMockedURLLoad(toKURL("http://www.test.com/ul-dot.png"), WebStrin g::fromUTF8("ul-dot.png"), WebString::fromUTF8("pageserializer/"), pngMimeType() );
150 registerMockedURLLoad(toKURL("http://www.test.com/ol-dot.png"), WebStrin g::fromUTF8("ol-dot.png"), WebString::fromUTF8("pageserializer/"), pngMimeType() );
151 return topFrameURL;
152 }
153
154 void loadURLInTopFrame(const WebURL& url)
155 {
156 WebURLRequest urlRequest;
157 urlRequest.initialize();
158 urlRequest.setURL(url);
159 m_webView->mainFrame()->loadRequest(urlRequest);
160 // Make sure any pending request get served.
161 Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests( );
162 // Some requests get delayed, run the timer.
163 runPendingTasks();
164 // Server the delayed resources.
165 Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests( );
166 }
167
168 const WebString& htmlMimeType() const { return m_htmlMimeType; }
169 const WebString& xhtmlMimeType() const { return m_xhtmlMimeType; }
170 const WebString& cssMimeType() const { return m_cssMimeType; }
171 const WebString& pngMimeType() const { return m_pngMimeType; }
172 const WebString& svgMimeType() const { return m_svgMimeType; }
173
174 static bool resourceVectorContains(const WebVector<WebPageSerializer::Resour ce>& resources, const char* url, const char* mimeType)
175 {
176 WebURL webURL = WebURL(toKURL(url));
177 for (size_t i = 0; i < resources.size(); ++i) {
178 const WebPageSerializer::Resource& resource = resources[i];
179 if (resource.url == webURL && !resource.data.isEmpty() && !resource. mimeType.compare(WebCString(mimeType)))
180 return true;
181 }
182 return false;
183 }
184
185 WebView* m_webView;
186
187 private:
188 WebString m_htmlMimeType;
189 WebString m_xhtmlMimeType;
190 WebString m_cssMimeType;
191 WebString m_pngMimeType;
192 WebString m_svgMimeType;
193 TestWebFrameClient m_webFrameClient;
194 WebFrame* m_mainFrame;
195 };
196
197 // Tests that a page with resources and sub-frame is reported with all its resou rces.
198 TEST_F(WebPageNewSerializeTest, PageWithFrames)
199 {
200 // Register the mocked frames.
201 WebURL topFrameURL = toKURL("http://www.test.com");
202 registerMockedURLLoad(topFrameURL, WebString::fromUTF8("top_frame.html"), We bString::fromUTF8("pageserializer/"), htmlMimeType());
203 registerMockedURLLoad(toKURL("http://www.test.com/iframe.html"), WebString:: fromUTF8("iframe.html"), WebString::fromUTF8("pageserializer/"), htmlMimeType()) ;
204 registerMockedURLLoad(toKURL("http://www.test.com/iframe2.html"), WebString: :fromUTF8("iframe2.html"), WebString::fromUTF8("pageserializer/"), htmlMimeType( ));
205 registerMockedURLLoad(toKURL("http://www.test.com/red_background.png"), WebS tring::fromUTF8("red_background.png"), WebString::fromUTF8("pageserializer/"), p ngMimeType());
206 registerMockedURLLoad(toKURL("http://www.test.com/green_background.png"), We bString::fromUTF8("green_background.png"), WebString::fromUTF8("pageserializer/" ), pngMimeType());
207 registerMockedURLLoad(toKURL("http://www.test.com/blue_background.png"), Web String::fromUTF8("blue_background.png"), WebString::fromUTF8("pageserializer/"), pngMimeType());
208
209 loadURLInTopFrame(topFrameURL);
210
211 WebVector<WebPageSerializer::Resource> resources;
212 WebPageSerializer::serialize(m_webView, &resources);
213 ASSERT_FALSE(resources.isEmpty());
214
215 // The first resource should be the main-frame.
216 const WebPageSerializer::Resource& resource = resources[0];
217 EXPECT_TRUE(resource.url == WebURL(toKURL("http://www.test.com")));
218 EXPECT_EQ(0, resource.mimeType.compare(WebCString("text/html")));
219 EXPECT_FALSE(resource.data.isEmpty());
220
221 EXPECT_EQ(6U, resources.size()); // There should be no duplicates.
222 EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/red_backg round.png", "image/png"));
223 EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/green_bac kground.png", "image/png"));
224 EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/blue_back ground.png", "image/png"));
225 EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/iframe.ht ml", "text/html"));
226 EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/iframe2.h tml", "text/html"));
227 }
228
229 // Test that when serializing a page, all CSS resources are reported, including url()'s
230 // and imports and links. Note that we don't test the resources contents, we onl y make sure
231 // they are all reported with the right mime type and that they contain some dat a.
232 TEST_F(WebPageNewSerializeTest, FAILS_CSSResources)
233 {
234 // Register the mocked frame and load it.
235 WebURL topFrameURL = setUpCSSTestPage();
236 loadURLInTopFrame(topFrameURL);
237
238 WebVector<WebPageSerializer::Resource> resources;
239 WebPageSerializer::serialize(m_webView, &resources);
240 ASSERT_FALSE(resources.isEmpty());
241
242 // The first resource should be the main-frame.
243 const WebPageSerializer::Resource& resource = resources[0];
244 EXPECT_TRUE(resource.url == WebURL(toKURL("http://www.test.com")));
245 EXPECT_EQ(0, resource.mimeType.compare(WebCString("text/html")));
246 EXPECT_FALSE(resource.data.isEmpty());
247
248 EXPECT_EQ(12U, resources.size()); // There should be no duplicates.
249 EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/link_styl es.css", "text/css"));
250 EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/import_st yles.css", "text/css"));
251 EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/import_st yle_from_link.css", "text/css"));
252 EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/red_backg round.png", "image/png"));
253 EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/orange_ba ckground.png", "image/png"));
254 EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/yellow_ba ckground.png", "image/png"));
255 EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/green_bac kground.png", "image/png"));
256 EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/blue_back ground.png", "image/png"));
257 EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/purple_ba ckground.png", "image/png"));
258 EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/ul-dot.pn g", "image/png"));
259 EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/ol-dot.pn g", "image/png"));
260 }
261
262 // Tests that when serializing a page with blank frames these are reported with their resources.
263 TEST_F(WebPageNewSerializeTest, BlankFrames)
264 {
265 // Register the mocked frame and load it.
266 WebURL topFrameURL = toKURL("http://www.test.com");
267 registerMockedURLLoad(topFrameURL, WebString::fromUTF8("blank_frames.html"), WebString::fromUTF8("pageserializer/"), htmlMimeType());
268 registerMockedURLLoad(toKURL("http://www.test.com/red_background.png"), WebS tring::fromUTF8("red_background.png"), WebString::fromUTF8("pageserializer/"), p ngMimeType());
269 registerMockedURLLoad(toKURL("http://www.test.com/orange_background.png"), W ebString::fromUTF8("orange_background.png"), WebString::fromUTF8("pageserializer /"), pngMimeType());
270 registerMockedURLLoad(toKURL("http://www.test.com/blue_background.png"), Web String::fromUTF8("blue_background.png"), WebString::fromUTF8("pageserializer/"), pngMimeType());
271
272 loadURLInTopFrame(topFrameURL);
273
274 WebVector<WebPageSerializer::Resource> resources;
275 WebPageSerializer::serialize(m_webView, &resources);
276 ASSERT_FALSE(resources.isEmpty());
277
278 // The first resource should be the main-frame.
279 const WebPageSerializer::Resource& resource = resources[0];
280 EXPECT_TRUE(resource.url == WebURL(toKURL("http://www.test.com")));
281 EXPECT_EQ(0, resource.mimeType.compare(WebCString("text/html")));
282 EXPECT_FALSE(resource.data.isEmpty());
283
284 EXPECT_EQ(7U, resources.size()); // There should be no duplicates.
285 EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/red_backg round.png", "image/png"));
286 EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/orange_ba ckground.png", "image/png"));
287 EXPECT_TRUE(resourceVectorContains(resources, "http://www.test.com/blue_back ground.png", "image/png"));
288 // The blank frames should have got a magic URL.
289 EXPECT_TRUE(resourceVectorContains(resources, "wyciwyg://frame/0", "text/htm l"));
290 EXPECT_TRUE(resourceVectorContains(resources, "wyciwyg://frame/1", "text/htm l"));
291 EXPECT_TRUE(resourceVectorContains(resources, "wyciwyg://frame/2", "text/htm l"));
292 }
293
294 TEST_F(WebPageNewSerializeTest, SerializeXMLHasRightDeclaration)
295 {
296 WebURL topFrameURL = toKURL("http://www.test.com/simple.xhtml");
297 registerMockedURLLoad(topFrameURL, WebString::fromUTF8("simple.xhtml"), WebS tring::fromUTF8("pageserializer/"), xhtmlMimeType());
298
299 loadURLInTopFrame(topFrameURL);
300
301 WebVector<WebPageSerializer::Resource> resources;
302 WebPageSerializer::serialize(m_webView, &resources);
303 ASSERT_FALSE(resources.isEmpty());
304
305 // We expect only one resource, the XML.
306 ASSERT_EQ(1U, resources.size());
307 std::string xml = std::string(resources[0].data.data());
308
309 // We should have one and only one instance of the XML declaration.
310 size_t pos = xml.find("<?xml version=");
311 ASSERT_TRUE(pos != std::string::npos);
312
313 pos = xml.find("<?xml version=", pos + 1);
314 ASSERT_TRUE(pos == std::string::npos);
315 }
316
317 TEST_F(WebPageNewSerializeTest, FAILS_TestMHTMLEncoding)
318 {
319 // Load a page with some CSS and some images.
320 WebURL topFrameURL = setUpCSSTestPage();
321 loadURLInTopFrame(topFrameURL);
322
323 WebCString mhtmlData = WebPageSerializer::serializeToMHTML(m_webView);
324 ASSERT_FALSE(mhtmlData.isEmpty());
325
326 // Read the MHTML data line per line and do some pseudo-parsing to make sure the right encoding is used for the different sections.
327 LineReader lineReader(std::string(mhtmlData.data()));
328 int sectionCheckedCount = 0;
329 const char* expectedEncoding = 0;
330 std::string line;
331 while (lineReader.getNextLine(&line)) {
332 if (!line.find("Content-Type:")) {
333 ASSERT_FALSE(expectedEncoding);
334 if (line.find("multipart/related;") != std::string::npos) {
335 // Skip this one, it's part of the MHTML header.
336 continue;
337 }
338 if (line.find("text/") != std::string::npos)
339 expectedEncoding = "quoted-printable";
340 else if (line.find("image/") != std::string::npos)
341 expectedEncoding = "base64";
342 else
343 FAIL() << "Unexpected Content-Type: " << line;
344 continue;
345 }
346 if (!line.find("Content-Transfer-Encoding:")) {
347 ASSERT_TRUE(expectedEncoding);
348 EXPECT_TRUE(line.find(expectedEncoding) != std::string::npos);
349 expectedEncoding = 0;
350 sectionCheckedCount++;
351 }
352 }
353 EXPECT_EQ(12, sectionCheckedCount);
354 }
355
356 // Test that we don't regress https://bugs.webkit.org/show_bug.cgi?id=99105
357 TEST_F(WebPageNewSerializeTest, SVGImageDontCrash)
358 {
359 WebURL pageUrl = toKURL("http://www.test.com");
360 WebURL imageUrl = toKURL("http://www.test.com/green_rectangle.svg");
361
362 registerMockedURLLoad(pageUrl, WebString::fromUTF8("page_with_svg_image.html "), WebString::fromUTF8("pageserializer/"), htmlMimeType());
363 registerMockedURLLoad(imageUrl, WebString::fromUTF8("green_rectangle.svg"), WebString::fromUTF8("pageserializer/"), svgMimeType());
364
365 loadURLInTopFrame(pageUrl);
366
367 WebCString mhtml = WebPageSerializer::serializeToMHTML(m_webView);
368 // We expect some data to be generated.
369 EXPECT_GT(mhtml.length(), 50U);
370 }
371
372 TEST_F(WebPageNewSerializeTest, NamespaceElementsDontCrash)
373 {
374 WebURL pageUrl = toKURL("http://www.test.com");
375 registerMockedURLLoad(pageUrl, WebString::fromUTF8("namespace_element.html") , WebString::fromUTF8("pageserializer/"), htmlMimeType());
376
377 loadURLInTopFrame(pageUrl);
378
379 WebVector<WebURL> localLinks(static_cast<size_t>(1));
380 WebVector<WebString> localPaths(static_cast<size_t>(1));
381 localLinks[0] = pageUrl;
382 localPaths[0] = WebString("/");
383
384 size_t counter = 0;
385 LengthCountingWebPageSerializerClient client(&counter);
386
387 // We just want to make sure nothing crazy happens, namely that no
388 // assertions are hit. As a sanity check, we also make sure that some data
389 // was returned.
390 WebPageSerializer::serialize(m_webView->mainFrame(), true, &client, localLin ks, localPaths, WebString(""));
391
392 EXPECT_GT(counter, 0U);
393 }
394
395 }
396
397 TEST_F(WebPageNewSerializeTest, TestMHTMLEncodingWithDataURL)
398 {
399 // Load a page with some data urls.
400 WebURL topFrameURL = toKURL("http://www.test.com");
401 registerMockedURLLoad(topFrameURL, WebString::fromUTF8("page_with_data.html" ), WebString::fromUTF8("pageserializer/"), htmlMimeType());
402 loadURLInTopFrame(topFrameURL);
403
404 WebCString mhtmlData = WebPageSerializer::serializeToMHTML(m_webView);
405 ASSERT_FALSE(mhtmlData.isEmpty());
406
407 // Read the MHTML data line and check that the string data:image is found
408 // exactly one time.
409 size_t nbDataURLs = 0;
410 LineReader lineReader(std::string(mhtmlData.data()));
411 std::string line;
412 while (lineReader.getNextLine(&line)) {
413 if (line.find("data:image") != std::string::npos)
414 nbDataURLs++;
415 }
416 EXPECT_EQ(1u, nbDataURLs);
417 }
418
419
420 TEST_F(WebPageNewSerializeTest, TestMHTMLEncodingWithMorphingDataURL)
421 {
422 // Load a page with some data urls.
423 WebURL topFrameURL = toKURL("http://www.test.com");
424 registerMockedURLLoad(topFrameURL, WebString::fromUTF8("page_with_morphing_d ata.html"), WebString::fromUTF8("pageserializer/"), htmlMimeType());
425 loadURLInTopFrame(topFrameURL);
426
427 WebCString mhtmlData = WebPageSerializer::serializeToMHTML(m_webView);
428 ASSERT_FALSE(mhtmlData.isEmpty());
429
430 // Read the MHTML data line and check that the string data:image is found
431 // exactly two times.
432 size_t nbDataURLs = 0;
433 LineReader lineReader(std::string(mhtmlData.data()));
434 std::string line;
435 while (lineReader.getNextLine(&line)) {
436 if (line.find("data:text") != std::string::npos)
437 nbDataURLs++;
438 }
439 EXPECT_EQ(2u, nbDataURLs);
440 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698