| 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 String rewriteLink(const Element& element) |
| 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 String(); |
| 190 |
| 191 StringBuilder uriBuilder; |
| 192 uriBuilder.append(m_rewriteFolder); |
| 193 uriBuilder.appendLiteral("/"); |
| 194 uriBuilder.append(m_rewriteURLs.get(completeURL)); |
| 195 return uriBuilder.toString(); |
| 196 } |
| 197 |
| 174 FrameTestHelpers::WebViewHelper m_helper; | 198 FrameTestHelpers::WebViewHelper m_helper; |
| 175 WebString m_folder; | 199 WebString m_folder; |
| 176 KURL m_baseUrl; | 200 KURL m_baseUrl; |
| 177 Vector<SerializedResource> m_resources; | 201 Vector<SerializedResource> m_resources; |
| 178 HashMap<String, String> m_rewriteURLs; | 202 HashMap<String, String> m_rewriteURLs; |
| 179 String m_rewriteFolder; | 203 String m_rewriteFolder; |
| 180 }; | 204 }; |
| 181 | 205 |
| 182 TEST_F(PageSerializerTest, HTMLElements) | 206 TEST_F(PageSerializerTest, HTMLElements) |
| 183 { | 207 { |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 registerURL("embed_iframe.html", "text/html"); | 283 registerURL("embed_iframe.html", "text/html"); |
| 260 registerURL("encoded_iframe.html", "text/html"); | 284 registerURL("encoded_iframe.html", "text/html"); |
| 261 | 285 |
| 262 registerURL("top.png", "image.png", "image/png"); | 286 registerURL("top.png", "image.png", "image/png"); |
| 263 registerURL("simple.png", "image.png", "image/png"); | 287 registerURL("simple.png", "image.png", "image/png"); |
| 264 registerURL("object.png", "image.png", "image/png"); | 288 registerURL("object.png", "image.png", "image/png"); |
| 265 registerURL("embed.png", "image.png", "image/png"); | 289 registerURL("embed.png", "image.png", "image/png"); |
| 266 | 290 |
| 267 serialize("top_frame.html"); | 291 serialize("top_frame.html"); |
| 268 | 292 |
| 269 EXPECT_EQ(9U, getResources().size()); | 293 EXPECT_EQ(10U, getResources().size()); |
| 270 | 294 |
| 271 EXPECT_TRUE(isSerialized("top_frame.html", "text/html")); | 295 EXPECT_TRUE(isSerialized("top_frame.html", "text/html")); |
| 272 EXPECT_TRUE(isSerialized("simple_iframe.html", "text/html")); | 296 EXPECT_TRUE(isSerialized("simple_iframe.html", "text/html")); // Twice. |
| 273 EXPECT_TRUE(isSerialized("object_iframe.html", "text/html")); | 297 EXPECT_TRUE(isSerialized("object_iframe.html", "text/html")); |
| 274 EXPECT_TRUE(isSerialized("embed_iframe.html", "text/html")); | 298 EXPECT_TRUE(isSerialized("embed_iframe.html", "text/html")); |
| 275 EXPECT_TRUE(isSerialized("encoded_iframe.html", "text/html")); | 299 EXPECT_TRUE(isSerialized("encoded_iframe.html", "text/html")); |
| 276 | 300 |
| 277 EXPECT_TRUE(isSerialized("top.png", "image/png")); | 301 EXPECT_TRUE(isSerialized("top.png", "image/png")); |
| 278 EXPECT_TRUE(isSerialized("simple.png", "image/png")); | 302 EXPECT_TRUE(isSerialized("simple.png", "image/png")); |
| 279 EXPECT_TRUE(isSerialized("object.png", "image/png")); | 303 EXPECT_TRUE(isSerialized("object.png", "image/png")); |
| 280 EXPECT_TRUE(isSerialized("embed.png", "image/png")); | 304 EXPECT_TRUE(isSerialized("embed.png", "image/png")); |
| 281 | 305 |
| 282 // Ensure that page contents are not NFC-normalized before encoding. | 306 // 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"); | 320 registerURL("orange_background.png", "image.png", "image/png"); |
| 297 registerURL("blue_background.png", "image.png", "image/png"); | 321 registerURL("blue_background.png", "image.png", "image/png"); |
| 298 | 322 |
| 299 serialize("blank_frames.html"); | 323 serialize("blank_frames.html"); |
| 300 | 324 |
| 301 EXPECT_EQ(7U, getResources().size()); | 325 EXPECT_EQ(7U, getResources().size()); |
| 302 | 326 |
| 303 EXPECT_TRUE(isSerialized("http://www.test.com/red_background.png", "image/pn
g")); | 327 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")); | 328 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")); | 329 EXPECT_TRUE(isSerialized("http://www.test.com/blue_background.png", "image/p
ng")); |
| 306 // The blank frames should have got a magic URL. | 330 |
| 307 EXPECT_TRUE(isSerialized("wyciwyg://frame/0", "text/html")); | 331 // The blank frames no longer get magic URL (i.e. wyciwyg://frame/0), so we |
| 308 EXPECT_TRUE(isSerialized("wyciwyg://frame/1", "text/html")); | 332 // can't really assert their presence via URL. We also can't use content-id |
| 309 EXPECT_TRUE(isSerialized("wyciwyg://frame/2", "text/html")); | 333 // in assertions (since it is not deterministic). Therefore we need to rely |
| 334 // on getResources().size() assertion above and on browser-level tests |
| 335 // (i.e. SavePageMultiFrameBrowserTest.AboutBlank). |
| 310 } | 336 } |
| 311 | 337 |
| 312 TEST_F(PageSerializerTest, CSS) | 338 TEST_F(PageSerializerTest, CSS) |
| 313 { | 339 { |
| 314 setBaseFolder("pageserializer/css/"); | 340 setBaseFolder("pageserializer/css/"); |
| 315 | 341 |
| 316 registerURL("css_test_page.html", "text/html"); | 342 registerURL("css_test_page.html", "text/html"); |
| 317 registerURL("link_styles.css", "text/css"); | 343 registerURL("link_styles.css", "text/css"); |
| 318 registerURL("encoding.css", "text/css"); | 344 registerURL("encoding.css", "text/css"); |
| 319 registerURL("import_style_from_link.css", "text/css"); | 345 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"); | 476 registerRewriteURL("http://www.test.com/absolute.png", "a.png"); |
| 451 registerRewriteURL("http://www.test.com/relative.png", "b.png"); | 477 registerRewriteURL("http://www.test.com/relative.png", "b.png"); |
| 452 | 478 |
| 453 serialize("rewritelinks_simple.html"); | 479 serialize("rewritelinks_simple.html"); |
| 454 | 480 |
| 455 EXPECT_EQ(3U, getResources().size()); | 481 EXPECT_EQ(3U, getResources().size()); |
| 456 EXPECT_NE(getSerializedData("rewritelinks_simple.html", "text/html").find("\
"folder/a.png\""), kNotFound); | 482 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); | 483 EXPECT_NE(getSerializedData("rewritelinks_simple.html", "text/html").find("\
"folder/b.png\""), kNotFound); |
| 458 } | 484 } |
| 459 | 485 |
| 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 | 486 // Test that we don't regress https://bugs.webkit.org/show_bug.cgi?id=99105 |
| 482 TEST_F(PageSerializerTest, SVGImageDontCrash) | 487 TEST_F(PageSerializerTest, SVGImageDontCrash) |
| 483 { | 488 { |
| 484 setBaseFolder("pageserializer/svg/"); | 489 setBaseFolder("pageserializer/svg/"); |
| 485 | 490 |
| 486 registerURL("page_with_svg_image.html", "text/html"); | 491 registerURL("page_with_svg_image.html", "text/html"); |
| 487 registerURL("green_rectangle.svg", "image/svg+xml"); | 492 registerURL("green_rectangle.svg", "image/svg+xml"); |
| 488 | 493 |
| 489 serialize("page_with_svg_image.html"); | 494 serialize("page_with_svg_image.html"); |
| 490 | 495 |
| (...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"))); | 531 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"))); | 532 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--"))); | 533 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-->"))); | 534 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?--"))); | 535 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#--"))); | 536 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"))); | 537 EXPECT_EQ("saved from url=(0026)http://foo.com/#bar-%2Dbaz", PageSerializer:
:markOfTheWebDeclaration(KURL(ParsedURLString, "http://foo.com#bar--baz"))); |
| 533 } | 538 } |
| 534 | 539 |
| 535 } // namespace blink | 540 } // namespace blink |
| OLD | NEW |