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