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