OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "base/json/json_reader.h" |
| 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "base/values.h" |
| 10 #include "testing/gmock/include/gmock/gmock.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 namespace autofill { |
| 14 |
| 15 class LegalMessageLineTest : public ::testing::Test { |
| 16 public: |
| 17 LegalMessageLineTest() {} |
| 18 ~LegalMessageLineTest() override {} |
| 19 |
| 20 void SetLegalMessage(const std::string& message_json) { |
| 21 scoped_ptr<base::Value> value(base::JSONReader::Read(message_json)); |
| 22 ASSERT_TRUE(value); |
| 23 base::DictionaryValue* dictionary = nullptr; |
| 24 ASSERT_TRUE(value->GetAsDictionary(&dictionary)); |
| 25 ASSERT_TRUE(dictionary); |
| 26 LegalMessageLine::Parse(*dictionary, &actual_lines_); |
| 27 } |
| 28 |
| 29 // Returns true if lines are the same. |
| 30 bool CompareLegalMessageLines(const LegalMessageLine& a, |
| 31 const LegalMessageLine& b) { |
| 32 if (a.text() != b.text()) |
| 33 return false; |
| 34 if (a.links().size() != b.links().size()) |
| 35 return false; |
| 36 for (size_t i = 0; i < a.links().size(); ++i) { |
| 37 if (a.links()[i].range != b.links()[i].range) |
| 38 return false; |
| 39 if (a.links()[i].url != b.links()[i].url) |
| 40 return false; |
| 41 } |
| 42 return true; |
| 43 } |
| 44 |
| 45 // Returns true if messages are the same. |
| 46 bool CompareLegalMessages(const LegalMessageLines& a, |
| 47 const LegalMessageLines& b) { |
| 48 if (a.size() != b.size()) |
| 49 return false; |
| 50 for (size_t i = 0; i < a.size(); ++i) { |
| 51 if (!CompareLegalMessageLines(a[i], b[i])) |
| 52 return false; |
| 53 } |
| 54 return true; |
| 55 } |
| 56 |
| 57 const LegalMessageLines& actual_lines() const { |
| 58 return actual_lines_; |
| 59 } |
| 60 |
| 61 private: |
| 62 LegalMessageLines actual_lines_; |
| 63 |
| 64 DISALLOW_COPY_AND_ASSIGN(LegalMessageLineTest); |
| 65 }; |
| 66 |
| 67 TEST_F(LegalMessageLineTest, NoParameters) { |
| 68 SetLegalMessage( |
| 69 "{" |
| 70 " \"line\" : [ {" |
| 71 " \"template\": \"This is the entire message.\"" |
| 72 " } ]" |
| 73 "}"); |
| 74 |
| 75 LegalMessageLine expected_line; |
| 76 expected_line.text_ = base::ASCIIToUTF16("This is the entire message."); |
| 77 LegalMessageLines expected = {expected_line}; |
| 78 EXPECT_TRUE(CompareLegalMessages(expected, actual_lines())); |
| 79 } |
| 80 |
| 81 TEST_F(LegalMessageLineTest, SingleParameter) { |
| 82 SetLegalMessage( |
| 83 "{" |
| 84 " \"line\" : [ {" |
| 85 " \"template\": \"Panda {0}.\"," |
| 86 " \"template_parameter\": [ {" |
| 87 " \"display_text\": \"bears are fuzzy\"," |
| 88 " \"url\": \"http://www.example.com\"" |
| 89 " } ]" |
| 90 " } ]" |
| 91 "}"); |
| 92 |
| 93 LegalMessageLine expected_line; |
| 94 expected_line.text_ = base::ASCIIToUTF16("Panda bears are fuzzy."); |
| 95 expected_line.links_ = { |
| 96 {{6, 21}, GURL("http://www.example.com")}, |
| 97 }; |
| 98 LegalMessageLines expected = {expected_line}; |
| 99 EXPECT_TRUE(CompareLegalMessages(expected, actual_lines())); |
| 100 } |
| 101 |
| 102 TEST_F(LegalMessageLineTest, MissingUrl) { |
| 103 SetLegalMessage( |
| 104 "{" |
| 105 " \"line\" : [ {" |
| 106 " \"template\": \"Panda {0}.\"," |
| 107 " \"template_parameter\": [ {" |
| 108 " \"display_text\": \"bear\"" |
| 109 " } ]" |
| 110 " } ]" |
| 111 "}"); |
| 112 // Legal message is invalid so actual_lines() should return no lines. |
| 113 EXPECT_TRUE( |
| 114 CompareLegalMessages(LegalMessageLines(), actual_lines())); |
| 115 } |
| 116 |
| 117 TEST_F(LegalMessageLineTest, MissingDisplayText) { |
| 118 SetLegalMessage( |
| 119 "{" |
| 120 " \"line\" : [ {" |
| 121 " \"template\": \"Panda {0}.\"," |
| 122 " \"template_parameter\": [ {" |
| 123 " \"url\": \"http://www.example.com\"" |
| 124 " } ]" |
| 125 " } ]" |
| 126 "}"); |
| 127 // Legal message is invalid so actual_lines() should return no lines. |
| 128 EXPECT_TRUE( |
| 129 CompareLegalMessages(LegalMessageLines(), actual_lines())); |
| 130 } |
| 131 |
| 132 TEST_F(LegalMessageLineTest, EscapeCharacters) { |
| 133 SetLegalMessage( |
| 134 "{" |
| 135 " \"line\" : [ {" |
| 136 " \"template\": \"Panda '{'{0}'}' '{1}' don't $1.\"," |
| 137 " \"template_parameter\": [ {" |
| 138 " \"display_text\": \"bears\"," |
| 139 " \"url\": \"http://www.example.com\"" |
| 140 " } ]" |
| 141 " } ]" |
| 142 "}"); |
| 143 |
| 144 LegalMessageLine expected_line; |
| 145 expected_line.text_ = base::ASCIIToUTF16("Panda {bears} {1} don't $1."); |
| 146 expected_line.links_ = { |
| 147 {{7, 12}, GURL("http://www.example.com")}, |
| 148 }; |
| 149 LegalMessageLines expected = {expected_line}; |
| 150 EXPECT_TRUE(CompareLegalMessages(expected, actual_lines())); |
| 151 } |
| 152 |
| 153 TEST_F(LegalMessageLineTest, ConsecutiveDollarSigns) { |
| 154 SetLegalMessage( |
| 155 "{" |
| 156 " \"line\" : [ {" |
| 157 " \"template\": \"$$\"" |
| 158 " } ]" |
| 159 "}"); |
| 160 |
| 161 // Consecutive dollar signs do not expand correctly (see comment in |
| 162 // ReplaceTemplatePlaceholders() in save_card_bubble_controller_impl.cc). |
| 163 // If this is fixed and this test starts to fail, please update the |
| 164 // "Caveats" section of the SaveCardBubbleControllerImpl::SetLegalMessage() |
| 165 // header file comment. |
| 166 LegalMessageLine expected_line; |
| 167 expected_line.text_ = base::ASCIIToUTF16("$$$"); |
| 168 |
| 169 LegalMessageLines expected = {expected_line}; |
| 170 EXPECT_TRUE(CompareLegalMessages(expected, actual_lines())); |
| 171 } |
| 172 |
| 173 TEST_F(LegalMessageLineTest, DollarAndParenthesis) { |
| 174 // "${" does not expand correctly (see comment in |
| 175 // ReplaceTemplatePlaceholders() in save_card_bubble_controller_impl.cc). |
| 176 // If this is fixed and this test starts to fail, please update the |
| 177 // "Caveats" section of the SaveCardBubbleControllerImpl::SetLegalMessage() |
| 178 // header file comment. |
| 179 SetLegalMessage( |
| 180 "{" |
| 181 " \"line\" : [ {" |
| 182 " \"template\": \"${0}\"," |
| 183 " \"template_parameter\": [ {" |
| 184 " \"display_text\": \"bears\"," |
| 185 " \"url\": \"http://www.example.com\"" |
| 186 " } ]" |
| 187 " } ]" |
| 188 "}"); |
| 189 // Legal message is invalid so actual_lines() should return no lines. |
| 190 EXPECT_TRUE( |
| 191 CompareLegalMessages(LegalMessageLines(), actual_lines())); |
| 192 } |
| 193 |
| 194 TEST_F(LegalMessageLineTest, MultipleParameters) { |
| 195 SetLegalMessage( |
| 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 |
| 212 LegalMessageLine expected_line; |
| 213 expected_line.text_ = base::ASCIIToUTF16("Panda bears like to eat bamboo."); |
| 214 expected_line.links_ = { |
| 215 {{6, 11}, GURL("http://www.example.com/0")}, |
| 216 {{24, 30}, GURL("http://www.example.com/1")}, |
| 217 {{17, 19}, GURL("http://www.example.com/2")}, |
| 218 }; |
| 219 LegalMessageLines expected = {expected_line}; |
| 220 EXPECT_TRUE(CompareLegalMessages(expected, actual_lines())); |
| 221 } |
| 222 |
| 223 TEST_F(LegalMessageLineTest, MultipleLineElements) { |
| 224 SetLegalMessage( |
| 225 "{" |
| 226 " \"line\" : [ {" |
| 227 " \"template\": \"Panda {0}\"," |
| 228 " \"template_parameter\": [ {" |
| 229 " \"display_text\": \"bears\"," |
| 230 " \"url\": \"http://www.example.com/line_0_param_0\"" |
| 231 " } ]" |
| 232 " }, {" |
| 233 " \"template\": \"like {1} eat {0}.\"," |
| 234 " \"template_parameter\": [ {" |
| 235 " \"display_text\": \"bamboo\"," |
| 236 " \"url\": \"http://www.example.com/line_1_param_0\"" |
| 237 " }, {" |
| 238 " \"display_text\": \"to\"," |
| 239 " \"url\": \"http://www.example.com/line_1_param_1\"" |
| 240 " } ]" |
| 241 " }, {" |
| 242 " \"template\": \"The {0}.\"," |
| 243 " \"template_parameter\": [ {" |
| 244 " \"display_text\": \"end\"," |
| 245 " \"url\": \"http://www.example.com/line_2_param_0\"" |
| 246 " } ]" |
| 247 " } ]" |
| 248 "}"); |
| 249 |
| 250 // Line 0. |
| 251 LegalMessageLine expected_line_0; |
| 252 expected_line_0.text_ = base::ASCIIToUTF16("Panda bears"); |
| 253 expected_line_0.links_ = { |
| 254 {{6, 11}, GURL("http://www.example.com/line_0_param_0")}, |
| 255 }; |
| 256 |
| 257 // Line 1. |
| 258 LegalMessageLine expected_line_1; |
| 259 expected_line_1.text_ = base::ASCIIToUTF16("like to eat bamboo."); |
| 260 expected_line_1.links_ = { |
| 261 {{12, 18}, GURL("http://www.example.com/line_1_param_0")}, |
| 262 {{5, 7}, GURL("http://www.example.com/line_1_param_1")}, |
| 263 }; |
| 264 |
| 265 // Line 2. |
| 266 LegalMessageLine expected_line_2; |
| 267 expected_line_2.text_ = base::ASCIIToUTF16("The end."); |
| 268 expected_line_2.links_ = { |
| 269 {{4, 7}, GURL("http://www.example.com/line_2_param_0")}, |
| 270 }; |
| 271 |
| 272 LegalMessageLines expected = {expected_line_0, expected_line_1, |
| 273 expected_line_2}; |
| 274 EXPECT_TRUE(CompareLegalMessages(expected, actual_lines())); |
| 275 } |
| 276 |
| 277 TEST_F(LegalMessageLineTest, EmbeddedNewlines) { |
| 278 SetLegalMessage( |
| 279 "{" |
| 280 " \"line\" : [ {" |
| 281 " \"template\": \"Panda {0}\nlike {2} eat {1}.\nThe {3}.\"," |
| 282 " \"template_parameter\": [ {" |
| 283 " \"display_text\": \"bears\"," |
| 284 " \"url\": \"http://www.example.com/0\"" |
| 285 " }, {" |
| 286 " \"display_text\": \"bamboo\"," |
| 287 " \"url\": \"http://www.example.com/1\"" |
| 288 " }, {" |
| 289 " \"display_text\": \"to\"," |
| 290 " \"url\": \"http://www.example.com/2\"" |
| 291 " }, {" |
| 292 " \"display_text\": \"end\"," |
| 293 " \"url\": \"http://www.example.com/3\"" |
| 294 " } ]" |
| 295 " } ]" |
| 296 "}"); |
| 297 |
| 298 LegalMessageLine expected_line; |
| 299 expected_line.text_ = |
| 300 base::ASCIIToUTF16("Panda bears\nlike to eat bamboo.\nThe end."); |
| 301 expected_line.links_ = { |
| 302 {{6, 11}, GURL("http://www.example.com/0")}, |
| 303 {{24, 30}, GURL("http://www.example.com/1")}, |
| 304 {{17, 19}, GURL("http://www.example.com/2")}, |
| 305 {{36, 39}, GURL("http://www.example.com/3")}, |
| 306 }; |
| 307 LegalMessageLines expected = {expected_line}; |
| 308 EXPECT_TRUE(CompareLegalMessages(expected, actual_lines())); |
| 309 } |
| 310 |
| 311 TEST_F(LegalMessageLineTest, MaximumPlaceholders) { |
| 312 SetLegalMessage( |
| 313 "{" |
| 314 " \"line\" : [ {" |
| 315 " \"template\": \"a{0} b{1} c{2} d{3} e{4} f{5} g{6}\"," |
| 316 " \"template_parameter\": [ {" |
| 317 " \"display_text\": \"A\"," |
| 318 " \"url\": \"http://www.example.com/0\"" |
| 319 " }, {" |
| 320 " \"display_text\": \"B\"," |
| 321 " \"url\": \"http://www.example.com/1\"" |
| 322 " }, {" |
| 323 " \"display_text\": \"C\"," |
| 324 " \"url\": \"http://www.example.com/2\"" |
| 325 " }, {" |
| 326 " \"display_text\": \"D\"," |
| 327 " \"url\": \"http://www.example.com/3\"" |
| 328 " }, {" |
| 329 " \"display_text\": \"E\"," |
| 330 " \"url\": \"http://www.example.com/4\"" |
| 331 " }, {" |
| 332 " \"display_text\": \"F\"," |
| 333 " \"url\": \"http://www.example.com/5\"" |
| 334 " }, {" |
| 335 " \"display_text\": \"G\"," |
| 336 " \"url\": \"http://www.example.com/6\"" |
| 337 " } ]" |
| 338 " } ]" |
| 339 "}"); |
| 340 |
| 341 LegalMessageLine expected_line; |
| 342 expected_line.text_ = base::ASCIIToUTF16("aA bB cC dD eE fF gG"); |
| 343 expected_line.links_ = { |
| 344 {{1, 2}, GURL("http://www.example.com/0")}, |
| 345 {{4, 5}, GURL("http://www.example.com/1")}, |
| 346 {{7, 8}, GURL("http://www.example.com/2")}, |
| 347 {{10, 11}, GURL("http://www.example.com/3")}, |
| 348 {{13, 14}, GURL("http://www.example.com/4")}, |
| 349 {{16, 17}, GURL("http://www.example.com/5")}, |
| 350 {{19, 20}, GURL("http://www.example.com/6")}, |
| 351 }; |
| 352 LegalMessageLines expected = {expected_line}; |
| 353 EXPECT_TRUE(CompareLegalMessages(expected, actual_lines())); |
| 354 } |
| 355 |
| 356 } // namespace autofill |
OLD | NEW |