| 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 "URLTestHelpers.h" | |
| 33 #include "platform/SerializedResource.h" | |
| 34 #include "platform/SharedBuffer.h" | |
| 35 #include "platform/mhtml/MHTMLArchive.h" | |
| 36 #include "public/platform/Platform.h" | |
| 37 #include "public/platform/WebUnitTestSupport.h" | |
| 38 #include <gtest/gtest.h> | |
| 39 | |
| 40 using namespace WebCore; | |
| 41 using namespace blink; | |
| 42 | |
| 43 using blink::URLTestHelpers::toKURL; | |
| 44 | |
| 45 namespace { | |
| 46 | |
| 47 class LineReader { | |
| 48 public: | |
| 49 LineReader(const std::string& text) : m_text(text), m_index(0) { } | |
| 50 bool getNextLine(std::string* line) | |
| 51 { | |
| 52 line->clear(); | |
| 53 if (m_index >= m_text.length()) | |
| 54 return false; | |
| 55 | |
| 56 size_t endOfLineIndex = m_text.find("\r\n", m_index); | |
| 57 if (endOfLineIndex == std::string::npos) { | |
| 58 *line = m_text.substr(m_index); | |
| 59 m_index = m_text.length(); | |
| 60 } else { | |
| 61 *line = m_text.substr(m_index, endOfLineIndex - m_index); | |
| 62 m_index = endOfLineIndex + 2; | |
| 63 } | |
| 64 return true; | |
| 65 } | |
| 66 | |
| 67 private: | |
| 68 std::string m_text; | |
| 69 size_t m_index; | |
| 70 }; | |
| 71 | |
| 72 | |
| 73 class MHTMLTest : public testing::Test { | |
| 74 public: | |
| 75 MHTMLTest() | |
| 76 { | |
| 77 m_filePath = Platform::current()->unitTestSupport()->webKitRootDir(); | |
| 78 m_filePath.append("/Source/web/tests/data/mhtml/"); | |
| 79 } | |
| 80 | |
| 81 protected: | |
| 82 virtual void SetUp() | |
| 83 { | |
| 84 } | |
| 85 | |
| 86 virtual void TearDown() | |
| 87 { | |
| 88 } | |
| 89 | |
| 90 void addResource(const char* url, const char* mime, PassRefPtr<SharedBuffer>
data) | |
| 91 { | |
| 92 SerializedResource resource(toKURL(url), mime, data); | |
| 93 m_resources.append(resource); | |
| 94 } | |
| 95 | |
| 96 void addResource(const char* url, const char* mime, const char* fileName) | |
| 97 { | |
| 98 addResource(url, mime, readFile(fileName)); | |
| 99 } | |
| 100 | |
| 101 void addTestResources() | |
| 102 { | |
| 103 addResource("http://www.test.com", "text/html", "css_test_page.html"); | |
| 104 addResource("http://www.test.com/link_styles.css", "text/css", "link_sty
les.css"); | |
| 105 addResource("http://www.test.com/import_style_from_link.css", "text/css"
, "import_style_from_link.css"); | |
| 106 addResource("http://www.test.com/import_styles.css", "text/css", "import
_styles.css"); | |
| 107 addResource("http://www.test.com/red_background.png", "image/png", "red_
background.png"); | |
| 108 addResource("http://www.test.com/orange_background.png", "image/png", "o
range_background.png"); | |
| 109 addResource("http://www.test.com/yellow_background.png", "image/png", "y
ellow_background.png"); | |
| 110 addResource("http://www.test.com/green_background.png", "image/png", "gr
een_background.png"); | |
| 111 addResource("http://www.test.com/blue_background.png", "image/png", "blu
e_background.png"); | |
| 112 addResource("http://www.test.com/purple_background.png", "image/png", "p
urple_background.png"); | |
| 113 addResource("http://www.test.com/ul-dot.png", "image/png", "ul-dot.png")
; | |
| 114 addResource("http://www.test.com/ol-dot.png", "image/png", "ol-dot.png")
; | |
| 115 } | |
| 116 | |
| 117 PassRefPtr<SharedBuffer> serialize(const char *title, const char *mime, MHT
MLArchive::EncodingPolicy encodingPolicy) | |
| 118 { | |
| 119 return MHTMLArchive::generateMHTMLData(m_resources, encodingPolicy, titl
e, mime); | |
| 120 } | |
| 121 | |
| 122 | |
| 123 private: | |
| 124 | |
| 125 PassRefPtr<SharedBuffer> readFile(const char* fileName) | |
| 126 { | |
| 127 String filePath = m_filePath + fileName; | |
| 128 return Platform::current()->unitTestSupport()->readFromFile(filePath); | |
| 129 } | |
| 130 | |
| 131 String m_filePath; | |
| 132 Vector<SerializedResource> m_resources; | |
| 133 }; | |
| 134 | |
| 135 TEST_F(MHTMLTest, FAILS_TestMHTMLEncoding) | |
| 136 { | |
| 137 addTestResources(); | |
| 138 RefPtr<SharedBuffer> data = serialize("Test Serialization", "text/html", MH
TMLArchive::UseDefaultEncoding); | |
| 139 | |
| 140 // Read the MHTML data line per line and do some pseudo-parsing to make sure
the right encoding is used for the different sections. | |
| 141 LineReader lineReader(std::string(data->data())); | |
| 142 int sectionCheckedCount = 0; | |
| 143 const char* expectedEncoding = 0; | |
| 144 std::string line; | |
| 145 while (lineReader.getNextLine(&line)) { | |
| 146 if (!line.find("Content-Type:")) { | |
| 147 ASSERT_FALSE(expectedEncoding); | |
| 148 if (line.find("multipart/related;") != std::string::npos) { | |
| 149 // Skip this one, it's part of the MHTML header. | |
| 150 continue; | |
| 151 } | |
| 152 if (line.find("text/") != std::string::npos) | |
| 153 expectedEncoding = "quoted-printable"; | |
| 154 else if (line.find("image/") != std::string::npos) | |
| 155 expectedEncoding = "base64"; | |
| 156 else | |
| 157 FAIL() << "Unexpected Content-Type: " << line; | |
| 158 continue; | |
| 159 } | |
| 160 if (!line.find("Content-Transfer-Encoding:")) { | |
| 161 ASSERT_TRUE(expectedEncoding); | |
| 162 EXPECT_TRUE(line.find(expectedEncoding) != std::string::npos); | |
| 163 expectedEncoding = 0; | |
| 164 sectionCheckedCount++; | |
| 165 } | |
| 166 } | |
| 167 EXPECT_EQ(12, sectionCheckedCount); | |
| 168 } | |
| 169 } | |
| OLD | NEW |