| OLD | NEW |
| (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 | |
| 31 #include "public/web/WebPageSerializer.h" | |
| 32 | |
| 33 #include "platform/testing/URLTestHelpers.h" | |
| 34 #include "platform/weborigin/KURL.h" | |
| 35 #include "public/platform/Platform.h" | |
| 36 #include "public/platform/WebString.h" | |
| 37 #include "public/platform/WebURL.h" | |
| 38 #include "public/platform/WebUnitTestSupport.h" | |
| 39 #include "public/web/WebPageSerializerClient.h" | |
| 40 #include "testing/gtest/include/gtest/gtest.h" | |
| 41 #include "web/WebLocalFrameImpl.h" | |
| 42 #include "web/WebViewImpl.h" | |
| 43 #include "web/tests/FrameTestHelpers.h" | |
| 44 #include "wtf/text/StringBuilder.h" | |
| 45 | |
| 46 namespace blink { | |
| 47 | |
| 48 namespace { | |
| 49 class SimpleWebPageSerializerClient final : public WebPageSerializerClient { | |
| 50 public: | |
| 51 String toString() { return m_builder.toString(); } | |
| 52 | |
| 53 private: | |
| 54 void didSerializeDataForFrame(const WebCString& data, PageSerializationStatu
s) final | |
| 55 { | |
| 56 m_builder.append(data.data(), data.length()); | |
| 57 } | |
| 58 | |
| 59 StringBuilder m_builder; | |
| 60 }; | |
| 61 | |
| 62 } // namespace | |
| 63 | |
| 64 class WebPageSerializerTest : public testing::Test { | |
| 65 protected: | |
| 66 WebPageSerializerTest() | |
| 67 { | |
| 68 m_helper.initialize(); | |
| 69 } | |
| 70 | |
| 71 ~WebPageSerializerTest() override | |
| 72 { | |
| 73 Platform::current()->unitTestSupport()->unregisterAllMockedURLs(); | |
| 74 } | |
| 75 | |
| 76 void registerMockedImageURL(const String& url) | |
| 77 { | |
| 78 // Image resources need to be mocked, but irrelevant here what image the
y map to. | |
| 79 URLTestHelpers::registerMockedURLLoad(KURL(ParsedURLString, url), "pages
erialization/awesome.png"); | |
| 80 } | |
| 81 | |
| 82 String serializeFile(const String& url, const String& fileName) | |
| 83 { | |
| 84 KURL parsedURL(ParsedURLString, url); | |
| 85 URLTestHelpers::registerMockedURLLoad(parsedURL, fileName, "pageserializ
ation/", "text/html"); | |
| 86 FrameTestHelpers::loadFrame(mainFrameImpl(), url.utf8().data()); | |
| 87 std::vector<std::pair<WebURL, WebString>> urlsToLocalPaths; | |
| 88 urlsToLocalPaths.push_back(std::make_pair(parsedURL, WebString("local"))
); | |
| 89 SimpleWebPageSerializerClient serializerClient; | |
| 90 WebPageSerializer::serialize(mainFrameImpl(), &serializerClient, urlsToL
ocalPaths); | |
| 91 return serializerClient.toString(); | |
| 92 } | |
| 93 | |
| 94 WebLocalFrameImpl* mainFrameImpl() | |
| 95 { | |
| 96 return m_helper.webViewImpl()->mainFrameImpl(); | |
| 97 } | |
| 98 | |
| 99 private: | |
| 100 FrameTestHelpers::WebViewHelper m_helper; | |
| 101 }; | |
| 102 | |
| 103 TEST_F(WebPageSerializerTest, URLAttributeValues) | |
| 104 { | |
| 105 registerMockedImageURL("javascript:\""); | |
| 106 | |
| 107 const char* expectedHTML = | |
| 108 "\n<!-- saved from url=(0020)http://www.test.com/ -->\n" | |
| 109 "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; char
set=UTF-8\">\n" | |
| 110 "</head><body><img src=\"javascript:"\">\n" | |
| 111 "<a href=\"http://www.test.com/local#"\">local</a>\n" | |
| 112 "<a href=\"http://www.example.com/#"><script>alert(0)</
script>\">external</a>\n" | |
| 113 "</body></html>"; | |
| 114 String actualHTML = serializeFile("http://www.test.com", "url_attribute_valu
es.html"); | |
| 115 EXPECT_EQ(expectedHTML, actualHTML); | |
| 116 } | |
| 117 | |
| 118 TEST_F(WebPageSerializerTest, EncodingAndNormalization) | |
| 119 { | |
| 120 const char* expectedHTML = | |
| 121 "<!DOCTYPE html>\n" | |
| 122 "<!-- saved from url=(0020)http://www.test.com/ -->\n" | |
| 123 "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; char
set=EUC-KR\">\n" | |
| 124 "<title>Ensure NFC normalization is not performed by page serializer</ti
tle>\n" | |
| 125 "</head><body>\n" | |
| 126 "\xe4\xc5\xd1\xe2\n" | |
| 127 "\n</body></html>"; | |
| 128 String actualHTML = serializeFile("http://www.test.com", "encoding_normaliza
tion.html"); | |
| 129 EXPECT_EQ(expectedHTML, actualHTML); | |
| 130 } | |
| 131 | |
| 132 TEST_F(WebPageSerializerTest, FromUrlWithMinusMinus) | |
| 133 { | |
| 134 String actualHTML = serializeFile("http://www.test.com?--x--", "text_only_pa
ge.html"); | |
| 135 EXPECT_EQ("<!-- saved from url=(0030)http://www.test.com/?-%2Dx-%2D -->", ac
tualHTML.substring(1, 60)); | |
| 136 } | |
| 137 | |
| 138 } // namespace blink | |
| OLD | NEW |