Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(104)

Side by Side Diff: Source/web/tests/MHTMLTest.cpp

Issue 1172993002: Merge page serializers [7/12] (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Fix nits Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | Source/web/tests/WebPageNewSerializerTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2014 Google Inc. All rights reserved. 2 * Copyright (C) 2014 Google Inc. 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 are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * 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 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 16 matching lines...) Expand all
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 #include "config.h" 31 #include "config.h"
32 32
33 #include "core/dom/Document.h" 33 #include "core/dom/Document.h"
34 #include "core/frame/LocalFrame.h" 34 #include "core/frame/LocalFrame.h"
35 #include "core/frame/Location.h" 35 #include "core/frame/Location.h"
36 #include "core/page/Page.h" 36 #include "core/page/Page.h"
37 #include "platform/SerializedResource.h"
38 #include "platform/SharedBuffer.h"
39 #include "platform/mhtml/MHTMLArchive.h"
37 #include "platform/testing/URLTestHelpers.h" 40 #include "platform/testing/URLTestHelpers.h"
38 #include "platform/weborigin/KURL.h" 41 #include "platform/weborigin/KURL.h"
39 #include "public/platform/Platform.h" 42 #include "public/platform/Platform.h"
40 #include "public/platform/WebString.h" 43 #include "public/platform/WebString.h"
41 #include "public/platform/WebURL.h" 44 #include "public/platform/WebURL.h"
42 #include "public/platform/WebURLRequest.h" 45 #include "public/platform/WebURLRequest.h"
43 #include "public/platform/WebURLResponse.h" 46 #include "public/platform/WebURLResponse.h"
44 #include "public/platform/WebUnitTestSupport.h" 47 #include "public/platform/WebUnitTestSupport.h"
45 #include "public/web/WebDocument.h" 48 #include "public/web/WebDocument.h"
46 #include "public/web/WebFrame.h" 49 #include "public/web/WebFrame.h"
47 #include "public/web/WebView.h" 50 #include "public/web/WebView.h"
48 #include "web/tests/FrameTestHelpers.h" 51 #include "web/tests/FrameTestHelpers.h"
49 #include <gtest/gtest.h> 52 #include <gtest/gtest.h>
50 53
54 using namespace blink;
55
56 using blink::URLTestHelpers::toKURL;
57
51 namespace { 58 namespace {
52 59
60 class LineReader {
61 public:
62 LineReader(const std::string& text) : m_text(text), m_index(0) { }
63 bool getNextLine(std::string* line)
64 {
65 line->clear();
66 if (m_index >= m_text.length())
67 return false;
68
69 size_t endOfLineIndex = m_text.find("\r\n", m_index);
70 if (endOfLineIndex == std::string::npos) {
71 *line = m_text.substr(m_index);
72 m_index = m_text.length();
73 return true;
74 }
75
76 *line = m_text.substr(m_index, endOfLineIndex - m_index);
77 m_index = endOfLineIndex + 2;
78 return true;
79 }
80
81 private:
82 std::string m_text;
83 size_t m_index;
84 };
85
53 using blink::URLTestHelpers::toKURL; 86 using blink::URLTestHelpers::toKURL;
54 using namespace blink; 87 using namespace blink;
55 88
56 class MHTMLTest : public testing::Test { 89 class MHTMLTest : public testing::Test {
57 public: 90 public:
58 MHTMLTest() 91 MHTMLTest()
59 { 92 {
93 m_filePath = Platform::current()->unitTestSupport()->webKitRootDir();
94 m_filePath.append("/Source/web/tests/data/mhtml/");
60 } 95 }
61 96
62 protected: 97 protected:
63 virtual void SetUp() 98 virtual void SetUp()
64 { 99 {
65 m_helper.initialize(); 100 m_helper.initialize();
66 } 101 }
67 102
68 virtual void TearDown() 103 virtual void TearDown()
69 { 104 {
70 Platform::current()->unitTestSupport()->unregisterAllMockedURLs(); 105 Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
71 } 106 }
72 107
73 void registerMockedURLLoad(const std::string& url, const WebString& fileName ) 108 void registerMockedURLLoad(const std::string& url, const WebString& fileName )
74 { 109 {
75 URLTestHelpers::registerMockedURLLoad(toKURL(url), fileName, WebString:: fromUTF8("mhtml/"), WebString::fromUTF8("text/html")); 110 URLTestHelpers::registerMockedURLLoad(toKURL(url), fileName, WebString:: fromUTF8("mhtml/"), WebString::fromUTF8("text/html"));
76 } 111 }
77 112
78 void loadURLInTopFrame(const WebURL& url) 113 void loadURLInTopFrame(const WebURL& url)
79 { 114 {
80 FrameTestHelpers::loadFrame(m_helper.webView()->mainFrame(), url.string( ).utf8().data()); 115 FrameTestHelpers::loadFrame(m_helper.webView()->mainFrame(), url.string( ).utf8().data());
81 } 116 }
82 117
83 Page* page() const { return m_helper.webViewImpl()->page(); } 118 Page* page() const { return m_helper.webViewImpl()->page(); }
84 119
120
121 void addResource(const char* url, const char* mime, PassRefPtr<SharedBuffer> data)
122 {
123 SerializedResource resource(toKURL(url), mime, data);
124 m_resources.append(resource);
125 }
126
127 void addResource(const char* url, const char* mime, const char* fileName)
128 {
129 addResource(url, mime, readFile(fileName));
130 }
131
132 void addTestResources()
133 {
134 addResource("http://www.test.com", "text/html", "css_test_page.html");
135 addResource("http://www.test.com/link_styles.css", "text/css", "link_sty les.css");
136 addResource("http://www.test.com/import_style_from_link.css", "text/css" , "import_style_from_link.css");
137 addResource("http://www.test.com/import_styles.css", "text/css", "import _styles.css");
138 addResource("http://www.test.com/red_background.png", "image/png", "red_ background.png");
139 addResource("http://www.test.com/orange_background.png", "image/png", "o range_background.png");
140 addResource("http://www.test.com/yellow_background.png", "image/png", "y ellow_background.png");
141 addResource("http://www.test.com/green_background.png", "image/png", "gr een_background.png");
142 addResource("http://www.test.com/blue_background.png", "image/png", "blu e_background.png");
143 addResource("http://www.test.com/purple_background.png", "image/png", "p urple_background.png");
144 addResource("http://www.test.com/ul-dot.png", "image/png", "ul-dot.png") ;
145 addResource("http://www.test.com/ol-dot.png", "image/png", "ol-dot.png") ;
146 }
147
148 PassRefPtr<SharedBuffer> serialize(const char *title, const char *mime, MHT MLArchive::EncodingPolicy encodingPolicy)
149 {
150 return MHTMLArchive::generateMHTMLData(m_resources, encodingPolicy, titl e, mime);
151 }
152
153
154
85 private: 155 private:
156 PassRefPtr<SharedBuffer> readFile(const char* fileName)
157 {
158 String filePath = m_filePath + fileName;
159 return Platform::current()->unitTestSupport()->readFromFile(filePath);
160 }
161
162 String m_filePath;
163 Vector<SerializedResource> m_resources;
86 FrameTestHelpers::WebViewHelper m_helper; 164 FrameTestHelpers::WebViewHelper m_helper;
87 }; 165 };
88 166
89 // Checks that the domain is set to the actual MHTML file, not the URL it was 167 // Checks that the domain is set to the actual MHTML file, not the URL it was
90 // generated from. 168 // generated from.
91 TEST_F(MHTMLTest, CheckDomain) 169 TEST_F(MHTMLTest, CheckDomain)
92 { 170 {
93 const char kFileURL[] = "file:///simple_test.mht"; 171 const char kFileURL[] = "file:///simple_test.mht";
94 172
95 // Register the mocked frame and load it. 173 // Register the mocked frame and load it.
96 WebURL url = toKURL(kFileURL); 174 WebURL url = toKURL(kFileURL);
97 registerMockedURLLoad(kFileURL, WebString::fromUTF8("simple_test.mht")); 175 registerMockedURLLoad(kFileURL, WebString::fromUTF8("simple_test.mht"));
98 loadURLInTopFrame(url); 176 loadURLInTopFrame(url);
99 ASSERT_TRUE(page()); 177 ASSERT_TRUE(page());
100 LocalFrame* frame = toLocalFrame(page()->mainFrame()); 178 LocalFrame* frame = toLocalFrame(page()->mainFrame());
101 ASSERT_TRUE(frame); 179 ASSERT_TRUE(frame);
102 Document* document = frame->document(); 180 Document* document = frame->document();
103 ASSERT_TRUE(document); 181 ASSERT_TRUE(document);
104 182
105 EXPECT_STREQ(kFileURL, frame->domWindow()->location()->href().ascii().data() ); 183 EXPECT_STREQ(kFileURL, frame->domWindow()->location()->href().ascii().data() );
106 184
107 SecurityOrigin* origin = document->securityOrigin(); 185 SecurityOrigin* origin = document->securityOrigin();
108 EXPECT_STRNE("localhost", origin->domain().ascii().data()); 186 EXPECT_STRNE("localhost", origin->domain().ascii().data());
109 } 187 }
110 188
189 TEST_F(MHTMLTest, TestMHTMLEncoding)
190 {
191 addTestResources();
192 RefPtr<SharedBuffer> data = serialize("Test Serialization", "text/html", MH TMLArchive::UseDefaultEncoding);
193
194 // Read the MHTML data line per line and do some pseudo-parsing to make sure the right encoding is used for the different sections.
195 LineReader lineReader(std::string(data->data()));
196 int sectionCheckedCount = 0;
197 const char* expectedEncoding = 0;
198 std::string line;
199 while (lineReader.getNextLine(&line)) {
200 if (!line.find("Content-Type:")) {
philipj_slow 2015/06/10 12:10:07 Because std::string::find returns 0 if the match i
Tiger (Sony Mobile) 2015/06/10 13:50:08 Done.
201 ASSERT_FALSE(expectedEncoding);
202 if (line.find("multipart/related;") != std::string::npos) {
203 // Skip this one, it's part of the MHTML header.
204 continue;
205 }
206 if (line.find("text/") != std::string::npos)
207 expectedEncoding = "quoted-printable";
208 else if (line.find("image/") != std::string::npos)
209 expectedEncoding = "base64";
210 else
211 FAIL() << "Unexpected Content-Type: " << line;
212 continue;
213 }
214 if (!line.find("Content-Transfer-Encoding:")) {
philipj_slow 2015/06/10 12:10:07 Ditto
Tiger (Sony Mobile) 2015/06/10 13:50:08 Done.
215 ASSERT_TRUE(expectedEncoding);
216 EXPECT_NE(line.find(expectedEncoding), std::string::npos);
217 expectedEncoding = 0;
218 sectionCheckedCount++;
219 }
220 }
221 EXPECT_EQ(12, sectionCheckedCount);
111 } 222 }
223 }
OLDNEW
« no previous file with comments | « no previous file | Source/web/tests/WebPageNewSerializerTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698