OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/autofill/core/browser/legal_message_line.h" |
| 6 |
| 7 #include <string> |
| 8 #include <utility> |
| 9 |
| 10 #include "base/json/json_reader.h" |
| 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "base/values.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 namespace autofill { |
| 18 |
| 19 // A legal message line that allows for modifications. |
| 20 class TestLegalMessageLine : public LegalMessageLine { |
| 21 public: |
| 22 TestLegalMessageLine() {} |
| 23 |
| 24 TestLegalMessageLine(const std::string& ascii_text) { set_text(ascii_text); } |
| 25 |
| 26 TestLegalMessageLine(const std::string& ascii_text, |
| 27 const std::vector<Link>& links) { |
| 28 set_text(ascii_text); |
| 29 set_links(links); |
| 30 } |
| 31 |
| 32 ~TestLegalMessageLine() override {} |
| 33 |
| 34 void set_text(const std::string& ascii_text) { |
| 35 text_ = base::ASCIIToUTF16(ascii_text); |
| 36 } |
| 37 |
| 38 void set_links(const std::vector<Link>& links) { links_ = links; } |
| 39 |
| 40 private: |
| 41 DISALLOW_COPY_AND_ASSIGN(TestLegalMessageLine); |
| 42 }; |
| 43 |
| 44 // A conveniently constructable link. |
| 45 struct TestLink : public LegalMessageLine::Link { |
| 46 TestLink(int start, int end, const std::string& url_spec) { |
| 47 range.set_start(start); |
| 48 range.set_end(end); |
| 49 url = GURL(url_spec); |
| 50 } |
| 51 }; |
| 52 |
| 53 struct TestCase { |
| 54 std::string message_json; |
| 55 LegalMessageLines expected_lines; |
| 56 }; |
| 57 |
| 58 std::ostream& operator<<(std::ostream& os, const LegalMessageLine& obj) { |
| 59 os << "{text: '" << obj.text() << "', links: ["; |
| 60 for (const LegalMessageLine::Link& link : obj.links()) { |
| 61 os << "{range: (" << link.range.start() << ", " << link.range.end() |
| 62 << "), url: '" << link.url << "'}"; |
| 63 } |
| 64 |
| 65 os << "]}"; |
| 66 return os; |
| 67 } |
| 68 |
| 69 std::ostream& operator<<(std::ostream& os, const LegalMessageLines& lines) { |
| 70 os << "["; |
| 71 for (const LegalMessageLine& line : lines) |
| 72 os << line; |
| 73 |
| 74 os << "]"; |
| 75 return os; |
| 76 } |
| 77 |
| 78 std::ostream& operator<<(std::ostream& os, const TestCase& test_case) { |
| 79 os << "{message_json: '" << test_case.message_json |
| 80 << "', expected_lines: " << test_case.expected_lines << "}"; |
| 81 return os; |
| 82 } |
| 83 |
| 84 class LegalMessageLineTest : public ::testing::TestWithParam<TestCase> { |
| 85 public: |
| 86 LegalMessageLineTest() {} |
| 87 ~LegalMessageLineTest() override {} |
| 88 }; |
| 89 |
| 90 bool operator==(const LegalMessageLine& lhs, const LegalMessageLine& rhs) { |
| 91 if (lhs.text() != rhs.text() || lhs.links().size() != rhs.links().size()) |
| 92 return false; |
| 93 |
| 94 for (size_t i = 0; i < lhs.links().size(); ++i) { |
| 95 if (lhs.links()[i].range != rhs.links()[i].range) |
| 96 return false; |
| 97 |
| 98 if (lhs.links()[i].url != rhs.links()[i].url) |
| 99 return false; |
| 100 } |
| 101 |
| 102 return true; |
| 103 } |
| 104 |
| 105 TEST_P(LegalMessageLineTest, Parsing) { |
| 106 scoped_ptr<base::Value> value( |
| 107 base::JSONReader::Read(GetParam().message_json)); |
| 108 ASSERT_TRUE(value); |
| 109 base::DictionaryValue* dictionary = nullptr; |
| 110 EXPECT_TRUE(value->GetAsDictionary(&dictionary)); |
| 111 ASSERT_TRUE(dictionary); |
| 112 LegalMessageLines actual_lines; |
| 113 LegalMessageLine::Parse(*dictionary, &actual_lines); |
| 114 |
| 115 EXPECT_EQ(GetParam().expected_lines, actual_lines); |
| 116 }; |
| 117 |
| 118 INSTANTIATE_TEST_CASE_P( |
| 119 TestCases, |
| 120 LegalMessageLineTest, |
| 121 testing::Values( |
| 122 TestCase{"{" |
| 123 " \"line\" : [ {" |
| 124 " \"template\": \"This is the entire message.\"" |
| 125 " } ]" |
| 126 "}", |
| 127 {TestLegalMessageLine("This is the entire message.")}}, |
| 128 TestCase{"{" |
| 129 " \"line\" : [ {" |
| 130 " \"template\": \"Panda {0}.\"," |
| 131 " \"template_parameter\": [ {" |
| 132 " \"display_text\": \"bears are fuzzy\"," |
| 133 " \"url\": \"http://www.example.com\"" |
| 134 " } ]" |
| 135 " } ]" |
| 136 "}", |
| 137 {TestLegalMessageLine{ |
| 138 "Panda bears are fuzzy.", |
| 139 {TestLink(6, 21, "http://www.example.com")}}}}, |
| 140 // Legal message is invalid, so lines should be empty. |
| 141 TestCase{"{" |
| 142 " \"line\" : [ {" |
| 143 " \"template\": \"Panda {0}.\"," |
| 144 " \"template_parameter\": [ {" |
| 145 " \"display_text\": \"bear\"" |
| 146 " } ]" |
| 147 " } ]" |
| 148 "}", |
| 149 LegalMessageLines()}, |
| 150 TestCase{"{" |
| 151 " \"line\" : [ {" |
| 152 " \"template\": \"Panda {0}.\"," |
| 153 " \"template_parameter\": [ {" |
| 154 " \"url\": \"http://www.example.com\"" |
| 155 " } ]" |
| 156 " } ]" |
| 157 "}", |
| 158 // Legal message is invalid, so lines should be empty. |
| 159 LegalMessageLines()}, |
| 160 TestCase{"{" |
| 161 " \"line\" : [ {" |
| 162 " \"template\": \"Panda '{'{0}'}' '{1}' don't $1.\"," |
| 163 " \"template_parameter\": [ {" |
| 164 " \"display_text\": \"bears\"," |
| 165 " \"url\": \"http://www.example.com\"" |
| 166 " } ]" |
| 167 " } ]" |
| 168 "}", |
| 169 {TestLegalMessageLine( |
| 170 "Panda {bears} {1} don't $1.", |
| 171 {TestLink(7, 12, "http://www.example.com")})}}, |
| 172 // Consecutive dollar signs do not expand correctly (see comment in |
| 173 // ReplaceTemplatePlaceholders() in legal_message_line.cc). If this is |
| 174 // fixed and this test starts to fail, please update the "Caveats" |
| 175 // section of the LegalMessageLine::Parse() header file comment. |
| 176 TestCase{"{" |
| 177 " \"line\" : [ {" |
| 178 " \"template\": \"$$\"" |
| 179 " } ]" |
| 180 "}", |
| 181 {TestLegalMessageLine("$$$")}}, |
| 182 // "${" does not expand correctly (see comment in |
| 183 // ReplaceTemplatePlaceholders() in legal_message_line.cc). If this is |
| 184 // fixed and this test starts to fail, please update the "Caveats" |
| 185 // section of the LegalMessageLine::Parse() header file comment. |
| 186 TestCase{"{" |
| 187 " \"line\" : [ {" |
| 188 " \"template\": \"${0}\"," |
| 189 " \"template_parameter\": [ {" |
| 190 " \"display_text\": \"bears\"," |
| 191 " \"url\": \"http://www.example.com\"" |
| 192 " } ]" |
| 193 " } ]" |
| 194 "}", |
| 195 LegalMessageLines()}, |
| 196 TestCase{"{" |
| 197 " \"line\" : [ {" |
| 198 " \"template\": \"Panda {0} like {2} eat {1}.\"," |
| 199 " \"template_parameter\": [ {" |
| 200 " \"display_text\": \"bears\"," |
| 201 " \"url\": \"http://www.example.com/0\"" |
| 202 " }, {" |
| 203 " \"display_text\": \"bamboo\"," |
| 204 " \"url\": \"http://www.example.com/1\"" |
| 205 " }, {" |
| 206 " \"display_text\": \"to\"," |
| 207 " \"url\": \"http://www.example.com/2\"" |
| 208 " } ]" |
| 209 " } ]" |
| 210 "}", |
| 211 {TestLegalMessageLine( |
| 212 "Panda bears like to eat bamboo.", |
| 213 {TestLink(6, 11, "http://www.example.com/0"), |
| 214 TestLink(24, 30, "http://www.example.com/1"), |
| 215 TestLink(17, 19, "http://www.example.com/2")})}}, |
| 216 TestCase{ |
| 217 "{" |
| 218 " \"line\" : [ {" |
| 219 " \"template\": \"Panda {0}\"," |
| 220 " \"template_parameter\": [ {" |
| 221 " \"display_text\": \"bears\"," |
| 222 " \"url\": \"http://www.example.com/line_0_param_0\"" |
| 223 " } ]" |
| 224 " }, {" |
| 225 " \"template\": \"like {1} eat {0}.\"," |
| 226 " \"template_parameter\": [ {" |
| 227 " \"display_text\": \"bamboo\"," |
| 228 " \"url\": \"http://www.example.com/line_1_param_0\"" |
| 229 " }, {" |
| 230 " \"display_text\": \"to\"," |
| 231 " \"url\": \"http://www.example.com/line_1_param_1\"" |
| 232 " } ]" |
| 233 " }, {" |
| 234 " \"template\": \"The {0}.\"," |
| 235 " \"template_parameter\": [ {" |
| 236 " \"display_text\": \"end\"," |
| 237 " \"url\": \"http://www.example.com/line_2_param_0\"" |
| 238 " } ]" |
| 239 " } ]" |
| 240 "}", |
| 241 {TestLegalMessageLine( |
| 242 "Panda bears", |
| 243 {TestLink(6, 11, "http://www.example.com/line_0_param_0")}), |
| 244 TestLegalMessageLine( |
| 245 "like to eat bamboo.", |
| 246 {TestLink(12, 18, "http://www.example.com/line_1_param_0"), |
| 247 TestLink(5, 7, "http://www.example.com/line_1_param_1")}), |
| 248 TestLegalMessageLine( |
| 249 "The end.", |
| 250 {TestLink(4, 7, "http://www.example.com/line_2_param_0")}) |
| 251 |
| 252 }}, |
| 253 TestCase{"{" |
| 254 " \"line\" : [ {" |
| 255 " \"template\": \"Panda {0}\nlike {2} eat {1}.\nThe {3}.\"," |
| 256 " \"template_parameter\": [ {" |
| 257 " \"display_text\": \"bears\"," |
| 258 " \"url\": \"http://www.example.com/0\"" |
| 259 " }, {" |
| 260 " \"display_text\": \"bamboo\"," |
| 261 " \"url\": \"http://www.example.com/1\"" |
| 262 " }, {" |
| 263 " \"display_text\": \"to\"," |
| 264 " \"url\": \"http://www.example.com/2\"" |
| 265 " }, {" |
| 266 " \"display_text\": \"end\"," |
| 267 " \"url\": \"http://www.example.com/3\"" |
| 268 " } ]" |
| 269 " } ]" |
| 270 "}", |
| 271 {TestLegalMessageLine( |
| 272 "Panda bears\nlike to eat bamboo.\nThe end.", |
| 273 {TestLink(6, 11, "http://www.example.com/0"), |
| 274 TestLink(24, 30, "http://www.example.com/1"), |
| 275 TestLink(17, 19, "http://www.example.com/2"), |
| 276 TestLink(36, 39, "http://www.example.com/3")})}}, |
| 277 TestCase{"{" |
| 278 " \"line\" : [ {" |
| 279 " \"template\": \"a{0} b{1} c{2} d{3} e{4} f{5} g{6}\"," |
| 280 " \"template_parameter\": [ {" |
| 281 " \"display_text\": \"A\"," |
| 282 " \"url\": \"http://www.example.com/0\"" |
| 283 " }, {" |
| 284 " \"display_text\": \"B\"," |
| 285 " \"url\": \"http://www.example.com/1\"" |
| 286 " }, {" |
| 287 " \"display_text\": \"C\"," |
| 288 " \"url\": \"http://www.example.com/2\"" |
| 289 " }, {" |
| 290 " \"display_text\": \"D\"," |
| 291 " \"url\": \"http://www.example.com/3\"" |
| 292 " }, {" |
| 293 " \"display_text\": \"E\"," |
| 294 " \"url\": \"http://www.example.com/4\"" |
| 295 " }, {" |
| 296 " \"display_text\": \"F\"," |
| 297 " \"url\": \"http://www.example.com/5\"" |
| 298 " }, {" |
| 299 " \"display_text\": \"G\"," |
| 300 " \"url\": \"http://www.example.com/6\"" |
| 301 " } ]" |
| 302 " } ]" |
| 303 "}", |
| 304 {TestLegalMessageLine( |
| 305 "aA bB cC dD eE fF gG", |
| 306 {TestLink(1, 2, "http://www.example.com/0"), |
| 307 TestLink(4, 5, "http://www.example.com/1"), |
| 308 TestLink(7, 8, "http://www.example.com/2"), |
| 309 TestLink(10, 11, "http://www.example.com/3"), |
| 310 TestLink(13, 14, "http://www.example.com/4"), |
| 311 TestLink(16, 17, "http://www.example.com/5"), |
| 312 TestLink(19, 20, "http://www.example.com/6")})}})); |
| 313 |
| 314 } // namespace autofill |
OLD | NEW |