OLD | NEW |
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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 #include "web/WebViewImpl.h" | 49 #include "web/WebViewImpl.h" |
50 #include "web/tests/FrameTestHelpers.h" | 50 #include "web/tests/FrameTestHelpers.h" |
51 #include "wtf/Assertions.h" | 51 #include "wtf/Assertions.h" |
52 #include "wtf/Vector.h" | 52 #include "wtf/Vector.h" |
53 | 53 |
54 using blink::URLTestHelpers::toKURL; | 54 using blink::URLTestHelpers::toKURL; |
55 using blink::URLTestHelpers::registerMockedURLLoad; | 55 using blink::URLTestHelpers::registerMockedURLLoad; |
56 | 56 |
57 namespace blink { | 57 namespace blink { |
58 | 58 |
59 class PageSerializerTest : public testing::Test { | 59 class PageSerializerTest |
| 60 : public testing::Test |
| 61 , public PageSerializer::Delegate { |
60 public: | 62 public: |
61 PageSerializerTest() | 63 PageSerializerTest() |
62 : m_folder(WebString::fromUTF8("pageserializer/")) | 64 : m_folder(WebString::fromUTF8("pageserializer/")) |
63 , m_baseUrl(toKURL("http://www.test.com")) | 65 , m_baseUrl(toKURL("http://www.test.com")) |
64 { | 66 { |
65 } | 67 } |
66 | 68 |
67 protected: | 69 protected: |
68 void SetUp() override | 70 void SetUp() override |
69 { | 71 { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 } | 113 } |
112 | 114 |
113 void registerRewriteURL(const char* fromURL, const char* toURL) | 115 void registerRewriteURL(const char* fromURL, const char* toURL) |
114 { | 116 { |
115 m_rewriteURLs.add(fromURL, toURL); | 117 m_rewriteURLs.add(fromURL, toURL); |
116 } | 118 } |
117 | 119 |
118 void serialize(const char* url) | 120 void serialize(const char* url) |
119 { | 121 { |
120 FrameTestHelpers::loadFrame(m_helper.webView()->mainFrame(), KURL(m_base
Url, url).string().utf8().data()); | 122 FrameTestHelpers::loadFrame(m_helper.webView()->mainFrame(), KURL(m_base
Url, url).string().utf8().data()); |
121 PageSerializer serializer(&m_resources, nullptr); | 123 PageSerializer serializer(m_resources, this); |
122 | |
123 serializer.setRewriteURLFolder(m_rewriteFolder); | |
124 for (const auto& rewriteURL: m_rewriteURLs) | |
125 serializer.registerRewriteURL(rewriteURL.key, rewriteURL.value); | |
126 | |
127 Frame* frame = m_helper.webViewImpl()->mainFrameImpl()->frame(); | 124 Frame* frame = m_helper.webViewImpl()->mainFrameImpl()->frame(); |
128 for (; frame; frame = frame->tree().traverseNext()) { | 125 for (; frame; frame = frame->tree().traverseNext()) { |
129 // This is safe, because tests do not do cross-site navigation | 126 // This is safe, because tests do not do cross-site navigation |
130 // (and therefore don't have remote frames). | 127 // (and therefore don't have remote frames). |
131 serializer.serializeFrame(*toLocalFrame(frame)); | 128 serializer.serializeFrame(*toLocalFrame(frame)); |
132 } | 129 } |
133 } | 130 } |
134 | 131 |
135 Vector<SerializedResource>& getResources() | 132 Vector<SerializedResource>& getResources() |
136 { | 133 { |
(...skipping 27 matching lines...) Expand all Loading... |
164 } | 161 } |
165 | 162 |
166 private: | 163 private: |
167 static void configureSettings(WebSettings* settings) | 164 static void configureSettings(WebSettings* settings) |
168 { | 165 { |
169 settings->setImagesEnabled(true); | 166 settings->setImagesEnabled(true); |
170 settings->setLoadsImagesAutomatically(true); | 167 settings->setLoadsImagesAutomatically(true); |
171 settings->setJavaScriptEnabled(true); | 168 settings->setJavaScriptEnabled(true); |
172 } | 169 } |
173 | 170 |
| 171 // PageSerializer::Delegate implementation. |
| 172 bool shouldIgnoreAttribute(const Attribute&) override |
| 173 { |
| 174 return false; |
| 175 } |
| 176 |
| 177 // PageSerializer::Delegate implementation. |
| 178 bool rewriteLink(const Element& element, String& rewrittenLink) |
| 179 { |
| 180 String completeURL; |
| 181 for (const auto& attribute : element.attributes()) { |
| 182 if (element.hasLegalLinkAttribute(attribute.name())) { |
| 183 completeURL = element.document().completeURL(attribute.value()); |
| 184 break; |
| 185 } |
| 186 } |
| 187 |
| 188 if (completeURL.isNull() || !m_rewriteURLs.contains(completeURL)) |
| 189 return false; |
| 190 |
| 191 StringBuilder uriBuilder; |
| 192 uriBuilder.append(m_rewriteFolder); |
| 193 uriBuilder.appendLiteral("/"); |
| 194 uriBuilder.append(m_rewriteURLs.get(completeURL)); |
| 195 rewrittenLink = uriBuilder.toString(); |
| 196 return true; |
| 197 } |
| 198 |
174 FrameTestHelpers::WebViewHelper m_helper; | 199 FrameTestHelpers::WebViewHelper m_helper; |
175 WebString m_folder; | 200 WebString m_folder; |
176 KURL m_baseUrl; | 201 KURL m_baseUrl; |
177 Vector<SerializedResource> m_resources; | 202 Vector<SerializedResource> m_resources; |
178 HashMap<String, String> m_rewriteURLs; | 203 HashMap<String, String> m_rewriteURLs; |
179 String m_rewriteFolder; | 204 String m_rewriteFolder; |
180 }; | 205 }; |
181 | 206 |
182 TEST_F(PageSerializerTest, HTMLElements) | 207 TEST_F(PageSerializerTest, HTMLElements) |
183 { | 208 { |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
259 registerURL("embed_iframe.html", "text/html"); | 284 registerURL("embed_iframe.html", "text/html"); |
260 registerURL("encoded_iframe.html", "text/html"); | 285 registerURL("encoded_iframe.html", "text/html"); |
261 | 286 |
262 registerURL("top.png", "image.png", "image/png"); | 287 registerURL("top.png", "image.png", "image/png"); |
263 registerURL("simple.png", "image.png", "image/png"); | 288 registerURL("simple.png", "image.png", "image/png"); |
264 registerURL("object.png", "image.png", "image/png"); | 289 registerURL("object.png", "image.png", "image/png"); |
265 registerURL("embed.png", "image.png", "image/png"); | 290 registerURL("embed.png", "image.png", "image/png"); |
266 | 291 |
267 serialize("top_frame.html"); | 292 serialize("top_frame.html"); |
268 | 293 |
269 EXPECT_EQ(9U, getResources().size()); | 294 EXPECT_EQ(10U, getResources().size()); |
270 | 295 |
271 EXPECT_TRUE(isSerialized("top_frame.html", "text/html")); | 296 EXPECT_TRUE(isSerialized("top_frame.html", "text/html")); |
272 EXPECT_TRUE(isSerialized("simple_iframe.html", "text/html")); | 297 EXPECT_TRUE(isSerialized("simple_iframe.html", "text/html")); // Twice. |
273 EXPECT_TRUE(isSerialized("object_iframe.html", "text/html")); | 298 EXPECT_TRUE(isSerialized("object_iframe.html", "text/html")); |
274 EXPECT_TRUE(isSerialized("embed_iframe.html", "text/html")); | 299 EXPECT_TRUE(isSerialized("embed_iframe.html", "text/html")); |
275 EXPECT_TRUE(isSerialized("encoded_iframe.html", "text/html")); | 300 EXPECT_TRUE(isSerialized("encoded_iframe.html", "text/html")); |
276 | 301 |
277 EXPECT_TRUE(isSerialized("top.png", "image/png")); | 302 EXPECT_TRUE(isSerialized("top.png", "image/png")); |
278 EXPECT_TRUE(isSerialized("simple.png", "image/png")); | 303 EXPECT_TRUE(isSerialized("simple.png", "image/png")); |
279 EXPECT_TRUE(isSerialized("object.png", "image/png")); | 304 EXPECT_TRUE(isSerialized("object.png", "image/png")); |
280 EXPECT_TRUE(isSerialized("embed.png", "image/png")); | 305 EXPECT_TRUE(isSerialized("embed.png", "image/png")); |
281 | 306 |
282 // Ensure that page contents are not NFC-normalized before encoding. | 307 // Ensure that page contents are not NFC-normalized before encoding. |
(...skipping 13 matching lines...) Expand all Loading... |
296 registerURL("orange_background.png", "image.png", "image/png"); | 321 registerURL("orange_background.png", "image.png", "image/png"); |
297 registerURL("blue_background.png", "image.png", "image/png"); | 322 registerURL("blue_background.png", "image.png", "image/png"); |
298 | 323 |
299 serialize("blank_frames.html"); | 324 serialize("blank_frames.html"); |
300 | 325 |
301 EXPECT_EQ(7U, getResources().size()); | 326 EXPECT_EQ(7U, getResources().size()); |
302 | 327 |
303 EXPECT_TRUE(isSerialized("http://www.test.com/red_background.png", "image/pn
g")); | 328 EXPECT_TRUE(isSerialized("http://www.test.com/red_background.png", "image/pn
g")); |
304 EXPECT_TRUE(isSerialized("http://www.test.com/orange_background.png", "image
/png")); | 329 EXPECT_TRUE(isSerialized("http://www.test.com/orange_background.png", "image
/png")); |
305 EXPECT_TRUE(isSerialized("http://www.test.com/blue_background.png", "image/p
ng")); | 330 EXPECT_TRUE(isSerialized("http://www.test.com/blue_background.png", "image/p
ng")); |
306 // The blank frames should have got a magic URL. | 331 |
307 EXPECT_TRUE(isSerialized("wyciwyg://frame/0", "text/html")); | 332 // The blank frames no longer get magic URL (i.e. wyciwyg://frame/0), so we |
308 EXPECT_TRUE(isSerialized("wyciwyg://frame/1", "text/html")); | 333 // can't really assert their presence via URL. We also can't use content-id |
309 EXPECT_TRUE(isSerialized("wyciwyg://frame/2", "text/html")); | 334 // in assertions (since it is not deterministic). Therefore we need to rely |
| 335 // on getResources().size() assertion above and on browser-level tests |
| 336 // (i.e. SavePageMultiFrameBrowserTest.AboutBlank). |
310 } | 337 } |
311 | 338 |
312 TEST_F(PageSerializerTest, CSS) | 339 TEST_F(PageSerializerTest, CSS) |
313 { | 340 { |
314 setBaseFolder("pageserializer/css/"); | 341 setBaseFolder("pageserializer/css/"); |
315 | 342 |
316 registerURL("css_test_page.html", "text/html"); | 343 registerURL("css_test_page.html", "text/html"); |
317 registerURL("link_styles.css", "text/css"); | 344 registerURL("link_styles.css", "text/css"); |
318 registerURL("encoding.css", "text/css"); | 345 registerURL("encoding.css", "text/css"); |
319 registerURL("import_style_from_link.css", "text/css"); | 346 registerURL("import_style_from_link.css", "text/css"); |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
450 registerRewriteURL("http://www.test.com/absolute.png", "a.png"); | 477 registerRewriteURL("http://www.test.com/absolute.png", "a.png"); |
451 registerRewriteURL("http://www.test.com/relative.png", "b.png"); | 478 registerRewriteURL("http://www.test.com/relative.png", "b.png"); |
452 | 479 |
453 serialize("rewritelinks_simple.html"); | 480 serialize("rewritelinks_simple.html"); |
454 | 481 |
455 EXPECT_EQ(3U, getResources().size()); | 482 EXPECT_EQ(3U, getResources().size()); |
456 EXPECT_NE(getSerializedData("rewritelinks_simple.html", "text/html").find("\
"folder/a.png\""), kNotFound); | 483 EXPECT_NE(getSerializedData("rewritelinks_simple.html", "text/html").find("\
"folder/a.png\""), kNotFound); |
457 EXPECT_NE(getSerializedData("rewritelinks_simple.html", "text/html").find("\
"folder/b.png\""), kNotFound); | 484 EXPECT_NE(getSerializedData("rewritelinks_simple.html", "text/html").find("\
"folder/b.png\""), kNotFound); |
458 } | 485 } |
459 | 486 |
460 TEST_F(PageSerializerTest, RewriteLinksBase) | |
461 { | |
462 setBaseFolder("pageserializer/rewritelinks/"); | |
463 setRewriteURLFolder("folder"); | |
464 | |
465 registerURL("rewritelinks_base.html", "text/html"); | |
466 registerURL("images/here/image.png", "image.png", "image/png"); | |
467 registerURL("images/here/or/in/here/image.png", "image.png", "image/png"); | |
468 registerURL("or/absolute.png", "image.png", "image/png"); | |
469 registerRewriteURL("http://www.test.com/images/here/image.png", "a.png"); | |
470 registerRewriteURL("http://www.test.com/images/here/or/in/here/image.png", "
b.png"); | |
471 registerRewriteURL("http://www.test.com/or/absolute.png", "c.png"); | |
472 | |
473 serialize("rewritelinks_base.html"); | |
474 | |
475 EXPECT_EQ(4U, getResources().size()); | |
476 EXPECT_NE(getSerializedData("rewritelinks_base.html", "text/html").find("\"f
older/a.png\""), kNotFound); | |
477 EXPECT_NE(getSerializedData("rewritelinks_base.html", "text/html").find("\"f
older/b.png\""), kNotFound); | |
478 EXPECT_NE(getSerializedData("rewritelinks_base.html", "text/html").find("\"f
older/c.png\""), kNotFound); | |
479 } | |
480 | |
481 // Test that we don't regress https://bugs.webkit.org/show_bug.cgi?id=99105 | 487 // Test that we don't regress https://bugs.webkit.org/show_bug.cgi?id=99105 |
482 TEST_F(PageSerializerTest, SVGImageDontCrash) | 488 TEST_F(PageSerializerTest, SVGImageDontCrash) |
483 { | 489 { |
484 setBaseFolder("pageserializer/svg/"); | 490 setBaseFolder("pageserializer/svg/"); |
485 | 491 |
486 registerURL("page_with_svg_image.html", "text/html"); | 492 registerURL("page_with_svg_image.html", "text/html"); |
487 registerURL("green_rectangle.svg", "image/svg+xml"); | 493 registerURL("green_rectangle.svg", "image/svg+xml"); |
488 | 494 |
489 serialize("page_with_svg_image.html"); | 495 serialize("page_with_svg_image.html"); |
490 | 496 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
526 EXPECT_EQ("saved from url=(0015)http://foo.com/", PageSerializer::markOfTheW
ebDeclaration(KURL(ParsedURLString, "http://foo.com"))); | 532 EXPECT_EQ("saved from url=(0015)http://foo.com/", PageSerializer::markOfTheW
ebDeclaration(KURL(ParsedURLString, "http://foo.com"))); |
527 EXPECT_EQ("saved from url=(0015)http://f-o.com/", PageSerializer::markOfTheW
ebDeclaration(KURL(ParsedURLString, "http://f-o.com"))); | 533 EXPECT_EQ("saved from url=(0015)http://f-o.com/", PageSerializer::markOfTheW
ebDeclaration(KURL(ParsedURLString, "http://f-o.com"))); |
528 EXPECT_EQ("saved from url=(0019)http://foo.com-%2D/", PageSerializer::markOf
TheWebDeclaration(KURL(ParsedURLString, "http://foo.com--"))); | 534 EXPECT_EQ("saved from url=(0019)http://foo.com-%2D/", PageSerializer::markOf
TheWebDeclaration(KURL(ParsedURLString, "http://foo.com--"))); |
529 EXPECT_EQ("saved from url=(0024)http://f-%2D.com-%2D%3E/", PageSerializer::m
arkOfTheWebDeclaration(KURL(ParsedURLString, "http://f--.com-->"))); | 535 EXPECT_EQ("saved from url=(0024)http://f-%2D.com-%2D%3E/", PageSerializer::m
arkOfTheWebDeclaration(KURL(ParsedURLString, "http://f--.com-->"))); |
530 EXPECT_EQ("saved from url=(0020)http://foo.com/?-%2D", PageSerializer::markO
fTheWebDeclaration(KURL(ParsedURLString, "http://foo.com?--"))); | 536 EXPECT_EQ("saved from url=(0020)http://foo.com/?-%2D", PageSerializer::markO
fTheWebDeclaration(KURL(ParsedURLString, "http://foo.com?--"))); |
531 EXPECT_EQ("saved from url=(0020)http://foo.com/#-%2D", PageSerializer::markO
fTheWebDeclaration(KURL(ParsedURLString, "http://foo.com#--"))); | 537 EXPECT_EQ("saved from url=(0020)http://foo.com/#-%2D", PageSerializer::markO
fTheWebDeclaration(KURL(ParsedURLString, "http://foo.com#--"))); |
532 EXPECT_EQ("saved from url=(0026)http://foo.com/#bar-%2Dbaz", PageSerializer:
:markOfTheWebDeclaration(KURL(ParsedURLString, "http://foo.com#bar--baz"))); | 538 EXPECT_EQ("saved from url=(0026)http://foo.com/#bar-%2Dbaz", PageSerializer:
:markOfTheWebDeclaration(KURL(ParsedURLString, "http://foo.com#bar--baz"))); |
533 } | 539 } |
534 | 540 |
535 } // namespace blink | 541 } // namespace blink |
OLD | NEW |