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