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

Side by Side Diff: Source/web/tests/PageSerializerTest.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
1 /* 1 /*
2 * Copyright (c) 2013, Opera Software ASA. All rights reserved. 2 * Copyright (c) 2013, Opera Software ASA. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 7 *
8 * 1. Redistributions of source code must retain the above copyright 8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright 10 * 2. Redistributions in binary form must reproduce the above copyright
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 void setBaseUrl(const char* url) 98 void setBaseUrl(const char* url)
99 { 99 {
100 m_baseUrl = toKURL(url); 100 m_baseUrl = toKURL(url);
101 } 101 }
102 102
103 void setBaseFolder(const char* folder) 103 void setBaseFolder(const char* folder)
104 { 104 {
105 m_folder = WebString::fromUTF8(folder); 105 m_folder = WebString::fromUTF8(folder);
106 } 106 }
107 107
108 void setRewriteURLFolder(const char* folder)
109 {
110 m_rewriteFolder = folder;
111 }
112
113 void registerURL(const char* url, const char* file, const char* mimeType)
114 {
115 registerMockedURLLoad(KURL(m_baseUrl, url), WebString::fromUTF8(file), m _folder, WebString::fromUTF8(mimeType));
116 }
117
108 void registerURL(const char* file, const char* mimeType) 118 void registerURL(const char* file, const char* mimeType)
109 { 119 {
110 registerMockedURLLoad(KURL(m_baseUrl, file), WebString::fromUTF8(file), m_folder, WebString::fromUTF8(mimeType)); 120 registerURL(file, file, mimeType);
111 } 121 }
112 122
113 void registerErrorURL(const char* file, int statusCode) 123 void registerErrorURL(const char* file, int statusCode)
114 { 124 {
115 WebURLError error; 125 WebURLError error;
116 error.reason = 0xdead + statusCode; 126 error.reason = 0xdead + statusCode;
117 error.domain = "PageSerializerTest"; 127 error.domain = "PageSerializerTest";
118 128
119 WebURLResponse response; 129 WebURLResponse response;
120 response.initialize(); 130 response.initialize();
121 response.setMIMEType("text/html"); 131 response.setMIMEType("text/html");
122 response.setHTTPStatusCode(statusCode); 132 response.setHTTPStatusCode(statusCode);
123 133
124 Platform::current()->unitTestSupport()->registerMockedErrorURL(KURL(m_ba seUrl, file), response, error); 134 Platform::current()->unitTestSupport()->registerMockedErrorURL(KURL(m_ba seUrl, file), response, error);
125 } 135 }
126 136
137 void registerRewriteURL(const char* fromURL, const char* toURL)
138 {
139 m_rewriteURLs.add(fromURL, toURL);
140 }
141
127 void serialize(const char* url) 142 void serialize(const char* url)
128 { 143 {
129 WebURLRequest urlRequest; 144 WebURLRequest urlRequest;
130 urlRequest.initialize(); 145 urlRequest.initialize();
131 urlRequest.setURL(KURL(m_baseUrl, url)); 146 urlRequest.setURL(KURL(m_baseUrl, url));
132 m_webViewImpl->mainFrame()->loadRequest(urlRequest); 147 m_webViewImpl->mainFrame()->loadRequest(urlRequest);
133 // Make sure any pending request get served. 148 // Make sure any pending request get served.
134 Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests( ); 149 Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests( );
135 // Some requests get delayed, run the timer. 150 // Some requests get delayed, run the timer.
136 runPendingTasks(); 151 runPendingTasks();
137 // Server the delayed resources. 152 // Server the delayed resources.
138 Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests( ); 153 Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests( );
139 154
140 PageSerializer serializer(&m_resources); 155 PageSerializer serializer(&m_resources,
156 m_rewriteURLs.isEmpty() ? 0: &m_rewriteURLs, m_rewriteFolder);
141 serializer.serialize(m_webViewImpl->mainFrameImpl()->frame()->page()); 157 serializer.serialize(m_webViewImpl->mainFrameImpl()->frame()->page());
142 } 158 }
143 159
144 Vector<SerializedResource>& getResources() 160 Vector<SerializedResource>& getResources()
145 { 161 {
146 return m_resources; 162 return m_resources;
147 } 163 }
148 164
149 165
150 const SerializedResource* getResource(const char* url, const char* mimeType) 166 const SerializedResource* getResource(const char* url, const char* mimeType)
(...skipping 15 matching lines...) Expand all
166 } 182 }
167 183
168 String getSerializedData(const char* url, const char* mimeType = 0) 184 String getSerializedData(const char* url, const char* mimeType = 0)
169 { 185 {
170 const SerializedResource* resource = getResource(url, mimeType); 186 const SerializedResource* resource = getResource(url, mimeType);
171 if (resource) 187 if (resource)
172 return String(resource->data->data(), resource->data->size()); 188 return String(resource->data->data(), resource->data->size());
173 return String(); 189 return String();
174 } 190 }
175 191
192 // For debugging
193 void printResources()
194 {
195 printf("Printing resources (%zu resources in total)\n", m_resources.size ());
196 for (size_t i = 0; i < m_resources.size(); ++i) {
197 printf("%zu. '%s', '%s'\n", i, m_resources[i].url.string().utf8().da ta(),
198 m_resources[i].mimeType.utf8().data());
199 }
200 }
201
176 WebViewImpl* m_webViewImpl; 202 WebViewImpl* m_webViewImpl;
177 203
178 private: 204 private:
179 TestWebFrameClient m_webFrameClient; 205 TestWebFrameClient m_webFrameClient;
180 WebFrame* m_mainFrame; 206 WebFrame* m_mainFrame;
181 WebString m_folder; 207 WebString m_folder;
182 KURL m_baseUrl; 208 KURL m_baseUrl;
183 Vector<SerializedResource> m_resources; 209 Vector<SerializedResource> m_resources;
210 LinkLocalPathMap m_rewriteURLs;
211 String m_rewriteFolder;
184 }; 212 };
185 213
214 TEST_F(PageSerializerTest, HTMLElements)
215 {
216 setBaseFolder("pageserializer/elements/");
186 217
187 TEST_F(PageSerializerTest, InputImage) 218 registerURL("elements.html", "text/html");
219 registerURL("style.css", "style.css", "text/css");
220 registerURL("copyright.html", "text.txt", "text/html");
221 registerURL("script.js", "text.txt", "text/javascript");
222
223 registerURL("bodyBackground.png", "image.png", "image/png");
224
225 registerURL("imageSrc.png", "image.png", "image/png");
226
227 registerURL("inputImage.png", "image.png", "image/png");
228
229 registerURL("tableBackground.png", "image.png", "image/png");
230 registerURL("trBackground.png", "image.png", "image/png");
231 registerURL("tdBackground.png", "image.png", "image/png");
232
233 registerURL("blockquoteCite.html", "text.txt", "text/html");
234 registerURL("qCite.html", "text.txt", "text/html");
235 registerURL("delCite.html", "text.txt", "text/html");
236 registerURL("insCite.html", "text.txt", "text/html");
237
238 registerErrorURL("nonExisting.png", 404);
239
240 serialize("elements.html");
241
242 EXPECT_EQ(8U, getResources().size());
243
244 EXPECT_TRUE(isSerialized("elements.html", "text/html"));
245 EXPECT_TRUE(isSerialized("style.css", "text/css"));
246 EXPECT_TRUE(isSerialized("bodyBackground.png", "image/png"));
247 EXPECT_TRUE(isSerialized("imageSrc.png", "image/png"));
248 EXPECT_TRUE(isSerialized("inputImage.png", "image/png"));
249 EXPECT_TRUE(isSerialized("tableBackground.png", "image/png"));
250 EXPECT_TRUE(isSerialized("trBackground.png", "image/png"));
251 EXPECT_TRUE(isSerialized("tdBackground.png", "image/png"));
252 EXPECT_FALSE(isSerialized("nonExisting.png", "image/png"));
253 }
254
255 TEST_F(PageSerializerTest, Frames)
188 { 256 {
189 setBaseFolder("pageserializer/input-image/"); 257 setBaseFolder("pageserializer/frames/");
190 258
191 registerURL("input-image.html", "text/html"); 259 registerURL("simple_frames.html", "text/html");
192 registerURL("button.png", "image/png"); 260 registerURL("simple_frames_top.html", "text/html");
193 registerErrorURL("non-existing-button.png", 404); 261 registerURL("simple_frames_1.html", "text/html");
262 registerURL("simple_frames_3.html", "text/html");
194 263
195 serialize("input-image.html"); 264 registerURL("frame_1.png", "image.png", "image/png");
265 registerURL("frame_2.png", "image.png", "image/png");
266 registerURL("frame_3.png", "image.png", "image/png");
267 registerURL("frame_4.png", "image.png", "image/png");
196 268
197 EXPECT_TRUE(isSerialized("button.png", "image/png")); 269 serialize("simple_frames.html");
198 EXPECT_FALSE(isSerialized("non-existing-button.png", "image/png")); 270
271 EXPECT_EQ(8U, getResources().size());
272
273 EXPECT_TRUE(isSerialized("simple_frames.html", "text/html"));
274 EXPECT_TRUE(isSerialized("simple_frames_top.html", "text/html"));
275 EXPECT_TRUE(isSerialized("simple_frames_1.html", "text/html"));
276 EXPECT_TRUE(isSerialized("simple_frames_3.html", "text/html"));
277
278 EXPECT_TRUE(isSerialized("frame_1.png", "image/png"));
279 EXPECT_TRUE(isSerialized("frame_2.png", "image/png"));
280 EXPECT_TRUE(isSerialized("frame_3.png", "image/png"));
281 EXPECT_TRUE(isSerialized("frame_4.png", "image/png"));
282 }
283
284 TEST_F(PageSerializerTest, IFrames)
285 {
286 setBaseFolder("pageserializer/frames/");
287
288 registerURL("top_frame.html", "text/html");
289 registerURL("simple_iframe.html", "text/html");
290 registerURL("object_iframe.html", "text/html");
291 registerURL("embed_iframe.html", "text/html");
292
293 registerURL("top.png", "image.png", "image/png");
294 registerURL("simple.png", "image.png", "image/png");
295 registerURL("object.png", "image.png", "image/png");
296 registerURL("embed.png", "image.png", "image/png");
297
298 serialize("top_frame.html");
299
300 EXPECT_EQ(8U, getResources().size());
301
302 EXPECT_TRUE(isSerialized("top_frame.html", "text/html"));
303 EXPECT_TRUE(isSerialized("simple_iframe.html", "text/html"));
304 EXPECT_TRUE(isSerialized("object_iframe.html", "text/html"));
305 EXPECT_TRUE(isSerialized("embed_iframe.html", "text/html"));
306
307 EXPECT_TRUE(isSerialized("top.png", "image/png"));
308 EXPECT_TRUE(isSerialized("simple.png", "image/png"));
309 EXPECT_TRUE(isSerialized("object.png", "image/png"));
310 EXPECT_TRUE(isSerialized("embed.png", "image/png"));
311 }
312
313 // Tests that when serializing a page with blank frames these are reported with their resources.
314 TEST_F(PageSerializerTest, BlankFrames)
315 {
316 setBaseFolder("pageserializer/frames/");
317
318 registerURL("blank_frames.html", "text/html");
319 registerURL("red_background.png", "image.png", "image/png");
320 registerURL("orange_background.png", "image.png", "image/png");
321 registerURL("blue_background.png", "image.png", "image/png");
322
323 serialize("blank_frames.html");
324
325 EXPECT_EQ(7U, getResources().size());
326
327 EXPECT_TRUE(isSerialized("http://www.test.com/red_background.png", "image/pn g"));
328 EXPECT_TRUE(isSerialized("http://www.test.com/orange_background.png", "image /png"));
329 EXPECT_TRUE(isSerialized("http://www.test.com/blue_background.png", "image/p ng"));
330 // The blank frames should have got a magic URL.
331 EXPECT_TRUE(isSerialized("wyciwyg://frame/0", "text/html"));
332 EXPECT_TRUE(isSerialized("wyciwyg://frame/1", "text/html"));
333 EXPECT_TRUE(isSerialized("wyciwyg://frame/2", "text/html"));
334 }
335
336 TEST_F(PageSerializerTest, CSS)
337 {
338 setBaseFolder("pageserializer/css/");
339
340 registerURL("css_test_page.html", "text/html");
341 registerURL("link_styles.css", "text/css");
342 registerURL("import_style_from_link.css", "text/css");
343 registerURL("import_styles.css", "text/css");
344 registerURL("red_background.png", "image.png", "image/png");
345 registerURL("orange_background.png", "image.png", "image/png");
346 registerURL("yellow_background.png", "image.png", "image/png");
347 registerURL("green_background.png", "image.png", "image/png");
348 registerURL("blue_background.png", "image.png", "image/png");
349 registerURL("purple_background.png", "image.png", "image/png");
350 registerURL("ul-dot.png", "image.png", "image/png");
351 registerURL("ol-dot.png", "image.png", "image/png");
352
353 serialize("css_test_page.html");
354
355 EXPECT_EQ(12U, getResources().size());
356
357 EXPECT_TRUE(isSerialized("css_test_page.html", "text/html"));
358 EXPECT_TRUE(isSerialized("link_styles.css", "text/css"));
359 EXPECT_TRUE(isSerialized("import_styles.css", "text/css"));
360 EXPECT_TRUE(isSerialized("import_style_from_link.css", "text/css"));
361 EXPECT_TRUE(isSerialized("red_background.png", "image/png"));
362 EXPECT_TRUE(isSerialized("orange_background.png", "image/png"));
363 EXPECT_TRUE(isSerialized("yellow_background.png", "image/png"));
364 EXPECT_TRUE(isSerialized("green_background.png", "image/png"));
365 EXPECT_TRUE(isSerialized("blue_background.png", "image/png"));
366 EXPECT_TRUE(isSerialized("purple_background.png", "image/png"));
367 EXPECT_TRUE(isSerialized("ul-dot.png", "image/png"));
368 EXPECT_TRUE(isSerialized("ol-dot.png", "image/png"));
199 } 369 }
200 370
201 TEST_F(PageSerializerTest, XMLDeclaration) 371 TEST_F(PageSerializerTest, XMLDeclaration)
202 { 372 {
203 setBaseFolder("pageserializer/xmldecl/"); 373 setBaseFolder("pageserializer/xml/");
204 374
205 registerURL("xmldecl.xml", "text/xml"); 375 registerURL("xmldecl.xml", "text/xml");
206 serialize("xmldecl.xml"); 376 serialize("xmldecl.xml");
207 377
208 String expectedStart("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); 378 String expectedStart("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
209 EXPECT_TRUE(getSerializedData("xmldecl.xml").startsWith(expectedStart)); 379 EXPECT_TRUE(getSerializedData("xmldecl.xml").startsWith(expectedStart));
210 } 380 }
211 381
212 TEST_F(PageSerializerTest, DTD) 382 TEST_F(PageSerializerTest, DTD)
213 { 383 {
214 setBaseFolder("pageserializer/dtd/"); 384 setBaseFolder("pageserializer/dtd/");
215 385
216 registerURL("dtd.html", "text/html"); 386 registerURL("html5.html", "text/html");
217 serialize("dtd.html"); 387 serialize("html5.html");
218 388
219 String expectedStart("<!DOCTYPE html>"); 389 String expectedStart("<!DOCTYPE html>");
220 EXPECT_TRUE(getSerializedData("dtd.html").startsWith(expectedStart)); 390 EXPECT_TRUE(getSerializedData("html5.html").startsWith(expectedStart));
221 } 391 }
222 392
223 TEST_F(PageSerializerTest, Font) 393 TEST_F(PageSerializerTest, Font)
224 { 394 {
225 setBaseFolder("pageserializer/font/"); 395 setBaseFolder("pageserializer/font/");
226 396
227 registerURL("font.html", "text/html"); 397 registerURL("font.html", "text/html");
228 registerURL("font.ttf", "application/octet-stream"); 398 registerURL("font.ttf", "application/octet-stream");
229 399
230 serialize("font.html"); 400 serialize("font.html");
231 401
232 EXPECT_TRUE(isSerialized("font.ttf", "application/octet-stream")); 402 EXPECT_TRUE(isSerialized("font.ttf", "application/octet-stream"));
233 } 403 }
234 404
405 TEST_F(PageSerializerTest, DataURI)
406 {
407 setBaseFolder("pageserializer/datauri/");
408
409 registerURL("page_with_data.html", "text/html");
410
411 serialize("page_with_data.html");
412
413 EXPECT_EQ(1U, getResources().size());
414 EXPECT_TRUE(isSerialized("page_with_data.html", "text/html"));
235 } 415 }
416
417 TEST_F(PageSerializerTest, DataURIMorphing)
418 {
419 setBaseFolder("pageserializer/datauri/");
420
421 registerURL("page_with_morphing_data.html", "text/html");
422
423 serialize("page_with_morphing_data.html");
424
425 EXPECT_EQ(2U, getResources().size());
426 EXPECT_TRUE(isSerialized("page_with_morphing_data.html", "text/html"));
427 }
428
429 TEST_F(PageSerializerTest, RewriteLinksSimple)
430 {
431 setBaseFolder("pageserializer/rewritelinks/");
432 setRewriteURLFolder("folder");
433
434 registerURL("rewritelinks_simple.html", "text/html");
435 registerURL("absolute.png", "image.png", "image/png");
436 registerURL("relative.png", "image.png", "image/png");
437 registerRewriteURL("http://www.test.com/absolute.png", "a.png");
438 registerRewriteURL("http://www.test.com/relative.png", "b.png");
439
440 serialize("rewritelinks_simple.html");
441
442 EXPECT_EQ(3U, getResources().size());
443 EXPECT_NE(getSerializedData("rewritelinks_simple.html", "text/html").find(". /folder/a.png"), kNotFound);
444 EXPECT_NE(getSerializedData("rewritelinks_simple.html", "text/html").find(". /folder/b.png"), kNotFound);
445 }
446
447 TEST_F(PageSerializerTest, RewriteLinksBase)
448 {
449 setBaseFolder("pageserializer/rewritelinks/");
450 setRewriteURLFolder("folder");
451
452 registerURL("rewritelinks_base.html", "text/html");
453 registerURL("images/here/image.png", "image.png", "image/png");
454 registerURL("images/here/or/in/here/image.png", "image.png", "image/png");
455 registerURL("or/absolute.png", "image.png", "image/png");
456 registerRewriteURL("http://www.test.com/images/here/image.png", "a.png");
457 registerRewriteURL("http://www.test.com/images/here/or/in/here/image.png", " b.png");
458 registerRewriteURL("http://www.test.com/or/absolute.png", "c.png");
459
460 serialize("rewritelinks_base.html");
461
462 EXPECT_EQ(4U, getResources().size());
463 EXPECT_NE(getSerializedData("rewritelinks_base.html", "text/html").find("./f older/a.png"), kNotFound);
464 EXPECT_NE(getSerializedData("rewritelinks_base.html", "text/html").find("./f older/b.png"), kNotFound);
465 EXPECT_NE(getSerializedData("rewritelinks_base.html", "text/html").find("./f older/c.png"), kNotFound);
466 }
467
468 // Test that we don't regress https://bugs.webkit.org/show_bug.cgi?id=99105
469 TEST_F(PageSerializerTest, SVGImageDontCrash)
470 {
471 setBaseFolder("pageserializer/svg/");
472
473 registerURL("page_with_svg_image.html", "text/html");
474 registerURL("green_rectangle.svg", "image/svg+xml");
475
476 serialize("page_with_svg_image.html");
477
478 EXPECT_EQ(2U, getResources().size());
479
480 EXPECT_TRUE(isSerialized("green_rectangle.svg", "image/svg+xml"));
481 EXPECT_GT(getSerializedData("green_rectangle.svg", "image/svg+xml").length() , 250U);
482 }
483
484 TEST_F(PageSerializerTest, NamespaceElementsDontCrash)
485 {
486 setBaseFolder("pageserializer/namespace/");
487
488 registerURL("namespace_element.html", "text/html");
489
490 serialize("namespace_element.html");
491
492 EXPECT_EQ(1U, getResources().size());
493 EXPECT_TRUE(isSerialized("namespace_element.html", "text/html"));
494 EXPECT_GT(getSerializedData("namespace_element.html", "text/html").length(), 0U);
495 }
496
497 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698