Commit c57fb6ef authored by Miyoung Shin's avatar Miyoung Shin Committed by Commit Bot

Replace use of std containers with WTF's equivalents in mhtml_archive_test.cc

Replace the use of std::map and std::string of std containers with
WTF::HashMap and WTF::String in mhtml_archive_test.cc and
unit_test_helpers.cc/h.

Bug: 952716
Change-Id: I89a22163c5aafc64fb4f885721129b17cefb965f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1580979Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Commit-Queue: Miyoung Shin <myid.shin@igalia.com>
Cr-Commit-Position: refs/heads/master@{#653489}
parent a1cb4455
...@@ -43,6 +43,7 @@ ...@@ -43,6 +43,7 @@
#include "third_party/blink/renderer/platform/testing/url_test_helpers.h" #include "third_party/blink/renderer/platform/testing/url_test_helpers.h"
#include "third_party/blink/renderer/platform/weborigin/kurl.h" #include "third_party/blink/renderer/platform/weborigin/kurl.h"
#include "third_party/blink/renderer/platform/weborigin/scheme_registry.h" #include "third_party/blink/renderer/platform/weborigin/scheme_registry.h"
#include "third_party/blink/renderer/platform/wtf/text/string_builder.h"
#include "third_party/blink/renderer/platform/wtf/time.h" #include "third_party/blink/renderer/platform/wtf/time.h"
using blink::mojom::MHTMLLoadResult; using blink::mojom::MHTMLLoadResult;
...@@ -106,39 +107,43 @@ class MHTMLArchiveTest : public testing::Test { ...@@ -106,39 +107,43 @@ class MHTMLArchiveTest : public testing::Test {
AddResource("http://www.test.com/ol-dot.png", "image/png", "ol-dot.png"); AddResource("http://www.test.com/ol-dot.png", "image/png", "ol-dot.png");
} }
std::map<std::string, std::string> ExtractHeaders(LineReader& line_reader) { HashMap<String, String> ExtractHeaders(LineReader& line_reader) {
// Read the data per line until reaching the empty line. // Read the data per line until reaching the empty line.
std::map<std::string, std::string> mhtml_headers; HashMap<String, String> mhtml_headers;
std::string line; String line;
line_reader.GetNextLine(&line); line_reader.GetNextLine(&line);
while (line.length()) { while (line.length()) {
StringBuilder builder;
builder.Append(line);
// Peek next line to see if it starts with soft line break. If yes, append // Peek next line to see if it starts with soft line break. If yes, append
// to current line. // to current line.
std::string next_line; String next_line;
while (true) { while (true) {
line_reader.GetNextLine(&next_line); line_reader.GetNextLine(&next_line);
if (next_line.length() > 1 && if (next_line.length() > 1 &&
(next_line[0] == ' ' || next_line[0] == '\t')) { (next_line[0] == ' ' || next_line[0] == '\t')) {
line += &(next_line.at(1)); builder.Append(next_line, 1, next_line.length() - 1);
continue; continue;
} }
break; break;
} }
std::string::size_type pos = line.find(':'); line = builder.ToString();
if (pos == std::string::npos) wtf_size_t pos = line.Find(":");
if (pos == kNotFound)
continue; continue;
std::string key = line.substr(0, pos); String key = line.Substring(0, pos);
std::string value = line.substr(pos + 2); String value = line.Substring(pos + 2);
mhtml_headers.emplace(key, value); mhtml_headers.insert(key, value);
line = next_line; line = next_line;
} }
return mhtml_headers; return mhtml_headers;
} }
std::map<std::string, std::string> ExtractMHTMLHeaders() { HashMap<String, String> ExtractMHTMLHeaders() {
LineReader line_reader(std::string(mhtml_data_.data(), mhtml_data_.size())); LineReader line_reader(String(mhtml_data_.data(), mhtml_data_.size()));
return ExtractHeaders(line_reader); return ExtractHeaders(line_reader);
} }
...@@ -220,15 +225,15 @@ TEST_F(MHTMLArchiveTest, ...@@ -220,15 +225,15 @@ TEST_F(MHTMLArchiveTest,
Serialize(ToKURL(kURL), String::FromUTF8(kTitle), "text/html", Serialize(ToKURL(kURL), String::FromUTF8(kTitle), "text/html",
MHTMLArchive::kUseDefaultEncoding); MHTMLArchive::kUseDefaultEncoding);
std::map<std::string, std::string> mhtml_headers = ExtractMHTMLHeaders(); HashMap<String, String> mhtml_headers = ExtractMHTMLHeaders();
EXPECT_EQ("<Saved by Blink>", mhtml_headers["From"]); EXPECT_EQ("<Saved by Blink>", mhtml_headers.find("From")->value);
EXPECT_FALSE(mhtml_headers["Date"].empty()); EXPECT_FALSE(mhtml_headers.find("Date")->value.IsEmpty());
EXPECT_EQ( EXPECT_EQ(
"multipart/related;type=\"text/html\";boundary=\"boundary-example\"", "multipart/related;type=\"text/html\";boundary=\"boundary-example\"",
mhtml_headers["Content-Type"]); mhtml_headers.find("Content-Type")->value);
EXPECT_EQ("abc", mhtml_headers["Subject"]); EXPECT_EQ("abc", mhtml_headers.find("Subject")->value);
EXPECT_EQ(kURL, mhtml_headers["Snapshot-Content-Location"]); EXPECT_EQ(kURL, mhtml_headers.find("Snapshot-Content-Location")->value);
} }
TEST_F(MHTMLArchiveTest, TEST_F(MHTMLArchiveTest,
...@@ -239,16 +244,16 @@ TEST_F(MHTMLArchiveTest, ...@@ -239,16 +244,16 @@ TEST_F(MHTMLArchiveTest,
Serialize(ToKURL(kURL), String::FromUTF8(kTitle), "text/html", Serialize(ToKURL(kURL), String::FromUTF8(kTitle), "text/html",
MHTMLArchive::kUseDefaultEncoding); MHTMLArchive::kUseDefaultEncoding);
std::map<std::string, std::string> mhtml_headers = ExtractMHTMLHeaders(); HashMap<String, String> mhtml_headers = ExtractMHTMLHeaders();
EXPECT_EQ("<Saved by Blink>", mhtml_headers["From"]); EXPECT_EQ("<Saved by Blink>", mhtml_headers.find("From")->value);
EXPECT_FALSE(mhtml_headers["Date"].empty()); EXPECT_FALSE(mhtml_headers.find("Date")->value.IsEmpty());
EXPECT_EQ( EXPECT_EQ(
"multipart/related;type=\"text/html\";boundary=\"boundary-example\"", "multipart/related;type=\"text/html\";boundary=\"boundary-example\"",
mhtml_headers["Content-Type"]); mhtml_headers.find("Content-Type")->value);
EXPECT_EQ("=?utf-8?Q?abc=20=09=3D=E2=98=9D=F0=9F=8F=BB?=", EXPECT_EQ("=?utf-8?Q?abc=20=09=3D=E2=98=9D=F0=9F=8F=BB?=",
mhtml_headers["Subject"]); mhtml_headers.find("Subject")->value);
EXPECT_EQ(kURL, mhtml_headers["Snapshot-Content-Location"]); EXPECT_EQ(kURL, mhtml_headers.find("Snapshot-Content-Location")->value);
} }
TEST_F(MHTMLArchiveTest, TEST_F(MHTMLArchiveTest,
...@@ -262,21 +267,21 @@ TEST_F(MHTMLArchiveTest, ...@@ -262,21 +267,21 @@ TEST_F(MHTMLArchiveTest,
Serialize(ToKURL(kURL), String::FromUTF8(kTitle), "text/html", Serialize(ToKURL(kURL), String::FromUTF8(kTitle), "text/html",
MHTMLArchive::kUseDefaultEncoding); MHTMLArchive::kUseDefaultEncoding);
std::map<std::string, std::string> mhtml_headers = ExtractMHTMLHeaders(); HashMap<String, String> mhtml_headers = ExtractMHTMLHeaders();
EXPECT_EQ("<Saved by Blink>", mhtml_headers["From"]); EXPECT_EQ("<Saved by Blink>", mhtml_headers.find("From")->value);
EXPECT_FALSE(mhtml_headers["Date"].empty()); EXPECT_FALSE(mhtml_headers.find("Date")->value.IsEmpty());
EXPECT_EQ( EXPECT_EQ(
"multipart/related;type=\"text/html\";boundary=\"boundary-example\"", "multipart/related;type=\"text/html\";boundary=\"boundary-example\"",
mhtml_headers["Content-Type"]); mhtml_headers.find("Content-Type")->value);
EXPECT_EQ( EXPECT_EQ(
"=?utf-8?Q?012345678901234567890123456789" "=?utf-8?Q?012345678901234567890123456789"
"012345678901234567890123456789012?=" "012345678901234567890123456789012?="
"=?utf-8?Q?345678901234567890123456789" "=?utf-8?Q?345678901234567890123456789"
"0123456789=20=09=3D=E2=98=9D=F0=9F?=" "0123456789=20=09=3D=E2=98=9D=F0=9F?="
"=?utf-8?Q?=8F=BB?=", "=?utf-8?Q?=8F=BB?=",
mhtml_headers["Subject"]); mhtml_headers.find("Subject")->value);
EXPECT_EQ(kURL, mhtml_headers["Snapshot-Content-Location"]); EXPECT_EQ(kURL, mhtml_headers.find("Snapshot-Content-Location")->value);
} }
TEST_F(MHTMLArchiveTest, TestMHTMLPartsWithBinaryEncoding) { TEST_F(MHTMLArchiveTest, TestMHTMLPartsWithBinaryEncoding) {
...@@ -287,20 +292,19 @@ TEST_F(MHTMLArchiveTest, TestMHTMLPartsWithBinaryEncoding) { ...@@ -287,20 +292,19 @@ TEST_F(MHTMLArchiveTest, TestMHTMLPartsWithBinaryEncoding) {
// Read the MHTML data line per line and do some pseudo-parsing to make sure // Read the MHTML data line per line and do some pseudo-parsing to make sure
// the right encoding is used for the different sections. // the right encoding is used for the different sections.
LineReader line_reader(std::string(mhtml_data().data(), mhtml_data().size())); LineReader line_reader(String(mhtml_data().data(), mhtml_data().size()));
int part_count = 0; int part_count = 0;
std::string line, last_line; String line, last_line;
while (line_reader.GetNextLine(&line)) { while (line_reader.GetNextLine(&line)) {
last_line = line; last_line = line;
if (line != kEndOfPartBoundary) if (line != kEndOfPartBoundary)
continue; continue;
part_count++; part_count++;
std::map<std::string, std::string> part_headers = HashMap<String, String> part_headers = ExtractHeaders(line_reader);
ExtractHeaders(line_reader); EXPECT_FALSE(part_headers.find("Content-Type")->value.IsEmpty());
EXPECT_FALSE(part_headers["Content-Type"].empty()); EXPECT_EQ("binary", part_headers.find("Content-Transfer-Encoding")->value);
EXPECT_EQ("binary", part_headers["Content-Transfer-Encoding"]); EXPECT_FALSE(part_headers.find("Content-Location")->value.IsEmpty());
EXPECT_FALSE(part_headers["Content-Location"].empty());
} }
EXPECT_EQ(12, part_count); EXPECT_EQ(12, part_count);
...@@ -316,27 +320,26 @@ TEST_F(MHTMLArchiveTest, TestMHTMLPartsWithDefaultEncoding) { ...@@ -316,27 +320,26 @@ TEST_F(MHTMLArchiveTest, TestMHTMLPartsWithDefaultEncoding) {
// Read the MHTML data line per line and do some pseudo-parsing to make sure // Read the MHTML data line per line and do some pseudo-parsing to make sure
// the right encoding is used for the different sections. // the right encoding is used for the different sections.
LineReader line_reader(std::string(mhtml_data().data(), mhtml_data().size())); LineReader line_reader(String(mhtml_data().data(), mhtml_data().size()));
int part_count = 0; int part_count = 0;
std::string line, last_line; String line, last_line;
while (line_reader.GetNextLine(&line)) { while (line_reader.GetNextLine(&line)) {
last_line = line; last_line = line;
if (line != kEndOfPartBoundary) if (line != kEndOfPartBoundary)
continue; continue;
part_count++; part_count++;
std::map<std::string, std::string> part_headers = HashMap<String, String> part_headers = ExtractHeaders(line_reader);
ExtractHeaders(line_reader);
std::string content_type = part_headers["Content-Type"]; String content_type = part_headers.find("Content-Type")->value;
EXPECT_FALSE(content_type.empty()); EXPECT_FALSE(content_type.IsEmpty());
std::string encoding = part_headers["Content-Transfer-Encoding"]; String encoding = part_headers.find("Content-Transfer-Encoding")->value;
EXPECT_FALSE(encoding.empty()); EXPECT_FALSE(encoding.IsEmpty());
if (content_type.compare(0, 5, "text/") == 0) if (content_type.StartsWith("text/"))
EXPECT_EQ("quoted-printable", encoding); EXPECT_EQ("quoted-printable", encoding);
else if (content_type.compare(0, 6, "image/") == 0) else if (content_type.StartsWith("image/"))
EXPECT_EQ("base64", encoding); EXPECT_EQ("base64", encoding);
else else
FAIL() << "Unexpected Content-Type: " << content_type; FAIL() << "Unexpected Content-Type: " << content_type;
...@@ -384,9 +387,8 @@ TEST_F(MHTMLArchiveTest, MHTMLDate) { ...@@ -384,9 +387,8 @@ TEST_F(MHTMLArchiveTest, MHTMLDate) {
MHTMLArchive::kUseDefaultEncoding); MHTMLArchive::kUseDefaultEncoding);
// The serialization process should have added a date header corresponding to // The serialization process should have added a date header corresponding to
// mhtml_date(). // mhtml_date().
std::map<std::string, std::string> mhtml_headers = ExtractMHTMLHeaders(); HashMap<String, String> mhtml_headers = ExtractMHTMLHeaders();
ASSERT_EQ(mhtml_date_header(), ASSERT_EQ(mhtml_date_header(), mhtml_headers.find("Date")->value);
String::FromUTF8(mhtml_headers["Date"].c_str()));
scoped_refptr<SharedBuffer> data = scoped_refptr<SharedBuffer> data =
SharedBuffer::Create(mhtml_data().data(), mhtml_data().size()); SharedBuffer::Create(mhtml_data().data(), mhtml_data().size());
......
...@@ -132,21 +132,20 @@ scoped_refptr<SharedBuffer> ReadFromFile(const String& path) { ...@@ -132,21 +132,20 @@ scoped_refptr<SharedBuffer> ReadFromFile(const String& path) {
return SharedBuffer::Create(buffer.data(), buffer.size()); return SharedBuffer::Create(buffer.data(), buffer.size());
} }
LineReader::LineReader(const std::string& text) : text_(text), index_(0) {} LineReader::LineReader(const String& text) : text_(text), index_(0) {}
bool LineReader::GetNextLine(std::string* line) { bool LineReader::GetNextLine(String* line) {
line->clear();
if (index_ >= text_.length()) if (index_ >= text_.length())
return false; return false;
size_t end_of_line_index = text_.find("\r\n", index_); wtf_size_t end_of_line_index = text_.Find("\r\n", index_);
if (end_of_line_index == std::string::npos) { if (end_of_line_index == kNotFound) {
*line = text_.substr(index_); *line = text_.Substring(index_);
index_ = text_.length(); index_ = text_.length();
return true; return true;
} }
*line = text_.substr(index_, end_of_line_index - index_); *line = text_.Substring(index_, end_of_line_index - index_);
index_ = end_of_line_index + 2; index_ = end_of_line_index + 2;
return true; return true;
} }
......
...@@ -83,11 +83,11 @@ class LineReader { ...@@ -83,11 +83,11 @@ class LineReader {
DISALLOW_NEW(); DISALLOW_NEW();
public: public:
LineReader(const std::string& text); LineReader(const String& text);
bool GetNextLine(std::string* line); bool GetNextLine(String* line);
private: private:
std::string text_; String text_;
size_t index_; size_t index_;
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment