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