| 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 #include "config.h" | |
| 31 | |
| 32 #include "platform/testing/URLTestHelpers.h" | |
| 33 #include "platform/testing/UnitTestHelpers.h" | |
| 34 #include "public/platform/Platform.h" | |
| 35 #include "public/platform/WebString.h" | |
| 36 #include "public/platform/WebThread.h" | |
| 37 #include "public/platform/WebURL.h" | |
| 38 #include "public/platform/WebURLRequest.h" | |
| 39 #include "public/platform/WebURLResponse.h" | |
| 40 #include "public/platform/WebUnitTestSupport.h" | |
| 41 #include "public/web/WebDocument.h" | |
| 42 #include "public/web/WebFrame.h" | |
| 43 #include "public/web/WebPageSerializer.h" | |
| 44 #include "public/web/WebPageSerializerClient.h" | |
| 45 #include "public/web/WebScriptSource.h" | |
| 46 #include "public/web/WebSettings.h" | |
| 47 #include "public/web/WebView.h" | |
| 48 #include "web/tests/FrameTestHelpers.h" | |
| 49 #include <gtest/gtest.h> | |
| 50 | |
| 51 namespace { | |
| 52 | |
| 53 using blink::URLTestHelpers::toKURL; | |
| 54 using blink::URLTestHelpers::registerMockedURLLoad; | |
| 55 using blink::testing::runPendingTasks; | |
| 56 using namespace blink; | |
| 57 | |
| 58 class LineReader { | |
| 59 public: | |
| 60 LineReader(const std::string& text) : m_text(text), m_index(0) { } | |
| 61 bool getNextLine(std::string* line) | |
| 62 { | |
| 63 line->clear(); | |
| 64 if (m_index >= m_text.length()) | |
| 65 return false; | |
| 66 | |
| 67 size_t endOfLineIndex = m_text.find("\r\n", m_index); | |
| 68 if (endOfLineIndex == std::string::npos) { | |
| 69 *line = m_text.substr(m_index); | |
| 70 m_index = m_text.length(); | |
| 71 } else { | |
| 72 *line = m_text.substr(m_index, endOfLineIndex - m_index); | |
| 73 m_index = endOfLineIndex + 2; | |
| 74 } | |
| 75 return true; | |
| 76 } | |
| 77 | |
| 78 private: | |
| 79 std::string m_text; | |
| 80 size_t m_index; | |
| 81 }; | |
| 82 | |
| 83 class LengthCountingWebPageSerializerClient : public WebPageSerializerClient { | |
| 84 public: | |
| 85 LengthCountingWebPageSerializerClient(size_t* counter) | |
| 86 : m_counter(counter) | |
| 87 { | |
| 88 } | |
| 89 | |
| 90 virtual void didSerializeDataForFrame(const WebURL& frameURL, const WebCStri
ng& data, PageSerializationStatus status) { | |
| 91 *m_counter += data.length(); | |
| 92 } | |
| 93 | |
| 94 private: | |
| 95 size_t* m_counter; | |
| 96 }; | |
| 97 | |
| 98 class FrameDataWebPageSerializerClient : public WebPageSerializerClient { | |
| 99 public: | |
| 100 FrameDataWebPageSerializerClient(const WebURL& frameURL, WebString* serializ
ationData) | |
| 101 : m_frameURL(frameURL) | |
| 102 , m_serializationData(serializationData) | |
| 103 { | |
| 104 } | |
| 105 | |
| 106 virtual void didSerializeDataForFrame(const WebURL& frameURL, const WebCStri
ng& data, PageSerializationStatus status) | |
| 107 { | |
| 108 if (frameURL != m_frameURL) | |
| 109 return; | |
| 110 *m_serializationData = data.utf16(); | |
| 111 } | |
| 112 | |
| 113 private: | |
| 114 WebURL m_frameURL; | |
| 115 WebString* m_serializationData; | |
| 116 }; | |
| 117 | |
| 118 class WebPageNewSerializeTest : public ::testing::Test { | |
| 119 public: | |
| 120 WebPageNewSerializeTest() | |
| 121 : m_baseURL("http://internal.test/") | |
| 122 , m_htmlMimeType(WebString::fromUTF8("text/html")) | |
| 123 , m_xhtmlMimeType(WebString::fromUTF8("application/xhtml+xml")) | |
| 124 , m_cssMimeType(WebString::fromUTF8("text/css")) | |
| 125 , m_pngMimeType(WebString::fromUTF8("image/png")) | |
| 126 , m_svgMimeType(WebString::fromUTF8("image/svg+xml")) | |
| 127 { | |
| 128 } | |
| 129 | |
| 130 protected: | |
| 131 virtual void SetUp() | |
| 132 { | |
| 133 // We want the images to load and JavaScript to be on. | |
| 134 m_helper.initialize(true, 0, 0, &configureSettings); | |
| 135 } | |
| 136 | |
| 137 virtual void TearDown() | |
| 138 { | |
| 139 Platform::current()->unitTestSupport()->unregisterAllMockedURLs(); | |
| 140 } | |
| 141 | |
| 142 KURL toTestURL(std::string relativeURL) | |
| 143 { | |
| 144 return toKURL(m_baseURL + relativeURL); | |
| 145 } | |
| 146 | |
| 147 WebURL setUpCSSTestPage() | |
| 148 { | |
| 149 registerMockedURLLoad(toTestURL(""), WebString::fromUTF8("css_test_page.
html"), WebString::fromUTF8("pageserializer/"), htmlMimeType()); | |
| 150 registerMockedURLLoad(toTestURL("link_styles.css"), WebString::fromUTF8(
"link_styles.css"), WebString::fromUTF8("pageserializer/"), cssMimeType()); | |
| 151 registerMockedURLLoad(toTestURL("import_style_from_link.css"), WebString
::fromUTF8("import_style_from_link.css"), WebString::fromUTF8("pageserializer/")
, cssMimeType()); | |
| 152 registerMockedURLLoad(toTestURL("import_styles.css"), WebString::fromUTF
8("import_styles.css"), WebString::fromUTF8("pageserializer/"), cssMimeType()); | |
| 153 registerMockedURLLoad(toTestURL("red_background.png"), WebString::fromUT
F8("red_background.png"), WebString::fromUTF8("pageserializer/"), pngMimeType())
; | |
| 154 registerMockedURLLoad(toTestURL("orange_background.png"), WebString::fro
mUTF8("orange_background.png"), WebString::fromUTF8("pageserializer/"), pngMimeT
ype()); | |
| 155 registerMockedURLLoad(toTestURL("yellow_background.png"), WebString::fro
mUTF8("yellow_background.png"), WebString::fromUTF8("pageserializer/"), pngMimeT
ype()); | |
| 156 registerMockedURLLoad(toTestURL("green_background.png"), WebString::from
UTF8("green_background.png"), WebString::fromUTF8("pageserializer/"), pngMimeTyp
e()); | |
| 157 registerMockedURLLoad(toTestURL("blue_background.png"), WebString::fromU
TF8("blue_background.png"), WebString::fromUTF8("pageserializer/"), pngMimeType(
)); | |
| 158 registerMockedURLLoad(toTestURL("purple_background.png"), WebString::fro
mUTF8("purple_background.png"), WebString::fromUTF8("pageserializer/"), pngMimeT
ype()); | |
| 159 registerMockedURLLoad(toTestURL("ul-dot.png"), WebString::fromUTF8("ul-d
ot.png"), WebString::fromUTF8("pageserializer/"), pngMimeType()); | |
| 160 registerMockedURLLoad(toTestURL("ol-dot.png"), WebString::fromUTF8("ol-d
ot.png"), WebString::fromUTF8("pageserializer/"), pngMimeType()); | |
| 161 | |
| 162 return toKURL(m_baseURL); // Top frame URL. | |
| 163 } | |
| 164 | |
| 165 void loadURLInTopFrame(const WebURL& url) | |
| 166 { | |
| 167 FrameTestHelpers::loadFrame(m_helper.webView()->mainFrame(), url.string(
).utf8()); | |
| 168 } | |
| 169 | |
| 170 const WebString& htmlMimeType() const { return m_htmlMimeType; } | |
| 171 const WebString& xhtmlMimeType() const { return m_xhtmlMimeType; } | |
| 172 const WebString& cssMimeType() const { return m_cssMimeType; } | |
| 173 const WebString& pngMimeType() const { return m_pngMimeType; } | |
| 174 const WebString& svgMimeType() const { return m_svgMimeType; } | |
| 175 | |
| 176 static bool resourceVectorContains(const WebVector<WebPageSerializer::Resour
ce>& resources, std::string url, const char* mimeType) | |
| 177 { | |
| 178 WebURL webURL = WebURL(toKURL(url)); | |
| 179 for (size_t i = 0; i < resources.size(); ++i) { | |
| 180 const WebPageSerializer::Resource& resource = resources[i]; | |
| 181 if (resource.url == webURL && !resource.data.isEmpty() && !resource.
mimeType.compare(WebCString(mimeType))) | |
| 182 return true; | |
| 183 } | |
| 184 return false; | |
| 185 } | |
| 186 | |
| 187 WebView* webView() const { return m_helper.webView(); } | |
| 188 | |
| 189 std::string m_baseURL; | |
| 190 | |
| 191 private: | |
| 192 static void configureSettings(WebSettings* settings) | |
| 193 { | |
| 194 settings->setImagesEnabled(true); | |
| 195 settings->setLoadsImagesAutomatically(true); | |
| 196 settings->setJavaScriptEnabled(true); | |
| 197 } | |
| 198 | |
| 199 FrameTestHelpers::WebViewHelper m_helper; | |
| 200 | |
| 201 WebString m_htmlMimeType; | |
| 202 WebString m_xhtmlMimeType; | |
| 203 WebString m_cssMimeType; | |
| 204 WebString m_pngMimeType; | |
| 205 WebString m_svgMimeType; | |
| 206 }; | |
| 207 | |
| 208 TEST_F(WebPageNewSerializeTest, FAILS_TestMHTMLEncoding) | |
| 209 { | |
| 210 // Load a page with some CSS and some images. | |
| 211 WebURL topFrameURL = setUpCSSTestPage(); | |
| 212 loadURLInTopFrame(topFrameURL); | |
| 213 | |
| 214 WebCString mhtmlData = WebPageSerializer::serializeToMHTML(webView()); | |
| 215 ASSERT_FALSE(mhtmlData.isEmpty()); | |
| 216 | |
| 217 // Read the MHTML data line per line and do some pseudo-parsing to make sure
the right encoding is used for the different sections. | |
| 218 LineReader lineReader(std::string(mhtmlData.data())); | |
| 219 int sectionCheckedCount = 0; | |
| 220 const char* expectedEncoding = 0; | |
| 221 std::string line; | |
| 222 while (lineReader.getNextLine(&line)) { | |
| 223 if (!line.find("Content-Type:")) { | |
| 224 ASSERT_FALSE(expectedEncoding); | |
| 225 if (line.find("multipart/related;") != std::string::npos) { | |
| 226 // Skip this one, it's part of the MHTML header. | |
| 227 continue; | |
| 228 } | |
| 229 if (line.find("text/") != std::string::npos) | |
| 230 expectedEncoding = "quoted-printable"; | |
| 231 else if (line.find("image/") != std::string::npos) | |
| 232 expectedEncoding = "base64"; | |
| 233 else | |
| 234 FAIL() << "Unexpected Content-Type: " << line; | |
| 235 continue; | |
| 236 } | |
| 237 if (!line.find("Content-Transfer-Encoding:")) { | |
| 238 ASSERT_TRUE(expectedEncoding); | |
| 239 EXPECT_TRUE(line.find(expectedEncoding) != std::string::npos); | |
| 240 expectedEncoding = 0; | |
| 241 sectionCheckedCount++; | |
| 242 } | |
| 243 } | |
| 244 EXPECT_EQ(12, sectionCheckedCount); | |
| 245 } | |
| 246 | |
| 247 } | |
| OLD | NEW |