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

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: 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 } else {
hajimehoshi 2015/06/10 09:02:03 nit: early return is better
Tiger (Sony Mobile) 2015/06/10 10:30:36 Done.
74 *line = m_text.substr(m_index, endOfLineIndex - m_index);
75 m_index = endOfLineIndex + 2;
76 }
77 return true;
78 }
79
80 private:
81 std::string m_text;
82 size_t m_index;
83 };
84
53 using blink::URLTestHelpers::toKURL; 85 using blink::URLTestHelpers::toKURL;
54 using namespace blink; 86 using namespace blink;
55 87
56 class MHTMLTest : public testing::Test { 88 class MHTMLTest : public testing::Test {
57 public: 89 public:
58 MHTMLTest() 90 MHTMLTest()
59 { 91 {
92 m_filePath = Platform::current()->unitTestSupport()->webKitRootDir();
93 m_filePath.append("/Source/web/tests/data/mhtml/");
60 } 94 }
61 95
62 protected: 96 protected:
63 virtual void SetUp() 97 virtual void SetUp()
64 { 98 {
65 m_helper.initialize(); 99 m_helper.initialize();
66 } 100 }
67 101
68 virtual void TearDown() 102 virtual void TearDown()
69 { 103 {
70 Platform::current()->unitTestSupport()->unregisterAllMockedURLs(); 104 Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
71 } 105 }
72 106
73 void registerMockedURLLoad(const std::string& url, const WebString& fileName ) 107 void registerMockedURLLoad(const std::string& url, const WebString& fileName )
74 { 108 {
75 URLTestHelpers::registerMockedURLLoad(toKURL(url), fileName, WebString:: fromUTF8("mhtml/"), WebString::fromUTF8("text/html")); 109 URLTestHelpers::registerMockedURLLoad(toKURL(url), fileName, WebString:: fromUTF8("mhtml/"), WebString::fromUTF8("text/html"));
76 } 110 }
77 111
78 void loadURLInTopFrame(const WebURL& url) 112 void loadURLInTopFrame(const WebURL& url)
79 { 113 {
80 FrameTestHelpers::loadFrame(m_helper.webView()->mainFrame(), url.string( ).utf8().data()); 114 FrameTestHelpers::loadFrame(m_helper.webView()->mainFrame(), url.string( ).utf8().data());
81 } 115 }
82 116
83 Page* page() const { return m_helper.webViewImpl()->page(); } 117 Page* page() const { return m_helper.webViewImpl()->page(); }
84 118
119
120 void addResource(const char* url, const char* mime, PassRefPtr<SharedBuffer> data)
121 {
122 SerializedResource resource(toKURL(url), mime, data);
123 m_resources.append(resource);
124 }
125
126 void addResource(const char* url, const char* mime, const char* fileName)
127 {
128 addResource(url, mime, readFile(fileName));
129 }
130
131 void addTestResources()
132 {
133 addResource("http://www.test.com", "text/html", "css_test_page.html");
134 addResource("http://www.test.com/link_styles.css", "text/css", "link_sty les.css");
135 addResource("http://www.test.com/import_style_from_link.css", "text/css" , "import_style_from_link.css");
136 addResource("http://www.test.com/import_styles.css", "text/css", "import _styles.css");
137 addResource("http://www.test.com/red_background.png", "image/png", "red_ background.png");
138 addResource("http://www.test.com/orange_background.png", "image/png", "o range_background.png");
139 addResource("http://www.test.com/yellow_background.png", "image/png", "y ellow_background.png");
140 addResource("http://www.test.com/green_background.png", "image/png", "gr een_background.png");
141 addResource("http://www.test.com/blue_background.png", "image/png", "blu e_background.png");
142 addResource("http://www.test.com/purple_background.png", "image/png", "p urple_background.png");
143 addResource("http://www.test.com/ul-dot.png", "image/png", "ul-dot.png") ;
144 addResource("http://www.test.com/ol-dot.png", "image/png", "ol-dot.png") ;
145 }
146
147 PassRefPtr<SharedBuffer> serialize(const char *title, const char *mime, MHT MLArchive::EncodingPolicy encodingPolicy)
148 {
149 return MHTMLArchive::generateMHTMLData(m_resources, encodingPolicy, titl e, mime);
150 }
151
152
153
85 private: 154 private:
155 PassRefPtr<SharedBuffer> readFile(const char* fileName)
156 {
157 String filePath = m_filePath + fileName;
158 return Platform::current()->unitTestSupport()->readFromFile(filePath);
159 }
160
161 String m_filePath;
162 Vector<SerializedResource> m_resources;
86 FrameTestHelpers::WebViewHelper m_helper; 163 FrameTestHelpers::WebViewHelper m_helper;
87 }; 164 };
88 165
89 // Checks that the domain is set to the actual MHTML file, not the URL it was 166 // Checks that the domain is set to the actual MHTML file, not the URL it was
90 // generated from. 167 // generated from.
91 TEST_F(MHTMLTest, CheckDomain) 168 TEST_F(MHTMLTest, CheckDomain)
92 { 169 {
93 const char kFileURL[] = "file:///simple_test.mht"; 170 const char kFileURL[] = "file:///simple_test.mht";
94 171
95 // Register the mocked frame and load it. 172 // Register the mocked frame and load it.
96 WebURL url = toKURL(kFileURL); 173 WebURL url = toKURL(kFileURL);
97 registerMockedURLLoad(kFileURL, WebString::fromUTF8("simple_test.mht")); 174 registerMockedURLLoad(kFileURL, WebString::fromUTF8("simple_test.mht"));
98 loadURLInTopFrame(url); 175 loadURLInTopFrame(url);
99 ASSERT_TRUE(page()); 176 ASSERT_TRUE(page());
100 LocalFrame* frame = toLocalFrame(page()->mainFrame()); 177 LocalFrame* frame = toLocalFrame(page()->mainFrame());
101 ASSERT_TRUE(frame); 178 ASSERT_TRUE(frame);
102 Document* document = frame->document(); 179 Document* document = frame->document();
103 ASSERT_TRUE(document); 180 ASSERT_TRUE(document);
104 181
105 EXPECT_STREQ(kFileURL, frame->domWindow()->location()->href().ascii().data() ); 182 EXPECT_STREQ(kFileURL, frame->domWindow()->location()->href().ascii().data() );
106 183
107 SecurityOrigin* origin = document->securityOrigin(); 184 SecurityOrigin* origin = document->securityOrigin();
108 EXPECT_STRNE("localhost", origin->domain().ascii().data()); 185 EXPECT_STRNE("localhost", origin->domain().ascii().data());
109 } 186 }
110 187
188 TEST_F(MHTMLTest, TestMHTMLEncoding)
189 {
190 addTestResources();
191 RefPtr<SharedBuffer> data = serialize("Test Serialization", "text/html", MH TMLArchive::UseDefaultEncoding);
192
193 // Read the MHTML data line per line and do some pseudo-parsing to make sure the right encoding is used for the different sections.
194 LineReader lineReader(std::string(data->data()));
195 int sectionCheckedCount = 0;
196 const char* expectedEncoding = 0;
197 std::string line;
198 while (lineReader.getNextLine(&line)) {
199 if (!line.find("Content-Type:")) {
200 ASSERT_FALSE(expectedEncoding);
201 if (line.find("multipart/related;") != std::string::npos) {
202 // Skip this one, it's part of the MHTML header.
203 continue;
204 }
205 if (line.find("text/") != std::string::npos)
206 expectedEncoding = "quoted-printable";
207 else if (line.find("image/") != std::string::npos)
208 expectedEncoding = "base64";
209 else
210 FAIL() << "Unexpected Content-Type: " << line;
211 continue;
212 }
213 if (!line.find("Content-Transfer-Encoding:")) {
214 ASSERT_TRUE(expectedEncoding);
215 EXPECT_TRUE(line.find(expectedEncoding) != std::string::npos);
hajimehoshi 2015/06/10 09:02:03 nit: EXPECT_NE
Tiger (Sony Mobile) 2015/06/10 10:30:36 Done.
216 expectedEncoding = 0;
217 sectionCheckedCount++;
218 }
219 }
220 EXPECT_EQ(12, sectionCheckedCount);
111 } 221 }
222 }
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