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