OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/autofill/save_card_bubble_controller_impl.h" | 5 #include "chrome/browser/ui/autofill/save_card_bubble_controller_impl.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
13 #include "base/test/histogram_tester.h" | 13 #include "base/test/histogram_tester.h" |
14 #include "base/values.h" | 14 #include "base/values.h" |
15 #include "chrome/browser/ui/autofill/save_card_bubble_view.h" | 15 #include "chrome/browser/ui/autofill/save_card_bubble_view.h" |
16 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 16 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
17 #include "chrome/test/base/browser_with_test_window_test.h" | 17 #include "chrome/test/base/browser_with_test_window_test.h" |
18 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 18 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
19 #include "components/autofill/core/browser/autofill_metrics.h" | 19 #include "components/autofill/core/browser/autofill_metrics.h" |
20 #include "content/public/browser/navigation_details.h" | 20 #include "content/public/browser/navigation_details.h" |
21 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
23 | 23 |
24 using base::Bucket; | 24 using base::Bucket; |
25 using testing::ElementsAre; | 25 using testing::ElementsAre; |
26 | 26 |
27 namespace autofill { | 27 namespace autofill { |
28 | 28 |
29 typedef SaveCardBubbleController::LegalMessageLine LegalMessageLine; | |
30 typedef SaveCardBubbleController::LegalMessageLines LegalMessageLines; | |
31 | |
32 class TestSaveCardBubbleControllerImpl : public SaveCardBubbleControllerImpl { | 29 class TestSaveCardBubbleControllerImpl : public SaveCardBubbleControllerImpl { |
33 public: | 30 public: |
34 static void CreateForTesting(content::WebContents* web_contents) { | 31 static void CreateForTesting(content::WebContents* web_contents) { |
35 web_contents->SetUserData( | 32 web_contents->SetUserData( |
36 UserDataKey(), new TestSaveCardBubbleControllerImpl(web_contents)); | 33 UserDataKey(), new TestSaveCardBubbleControllerImpl(web_contents)); |
37 } | 34 } |
38 | 35 |
39 explicit TestSaveCardBubbleControllerImpl(content::WebContents* web_contents) | 36 explicit TestSaveCardBubbleControllerImpl(content::WebContents* web_contents) |
40 : SaveCardBubbleControllerImpl(web_contents) {} | 37 : SaveCardBubbleControllerImpl(web_contents) {} |
41 | 38 |
(...skipping 27 matching lines...) Expand all Loading... |
69 scoped_ptr<base::Value> value(base::JSONReader::Read(message_json)); | 66 scoped_ptr<base::Value> value(base::JSONReader::Read(message_json)); |
70 ASSERT_TRUE(value); | 67 ASSERT_TRUE(value); |
71 base::DictionaryValue* dictionary; | 68 base::DictionaryValue* dictionary; |
72 ASSERT_TRUE(value->GetAsDictionary(&dictionary)); | 69 ASSERT_TRUE(value->GetAsDictionary(&dictionary)); |
73 scoped_ptr<base::DictionaryValue> legal_message = | 70 scoped_ptr<base::DictionaryValue> legal_message = |
74 dictionary->CreateDeepCopy(); | 71 dictionary->CreateDeepCopy(); |
75 controller()->ShowBubbleForUpload(base::Bind(&SaveCardCallback), | 72 controller()->ShowBubbleForUpload(base::Bind(&SaveCardCallback), |
76 std::move(legal_message)); | 73 std::move(legal_message)); |
77 } | 74 } |
78 | 75 |
79 // Returns true if lines are the same. | |
80 bool CompareLegalMessageLines(const LegalMessageLine& a, | |
81 const LegalMessageLine& b) { | |
82 if (a.text != b.text) | |
83 return false; | |
84 if (a.links.size() != b.links.size()) | |
85 return false; | |
86 for (size_t i = 0; i < a.links.size(); ++i) { | |
87 if (a.links[i].range != b.links[i].range) | |
88 return false; | |
89 if (a.links[i].url != b.links[i].url) | |
90 return false; | |
91 } | |
92 return true; | |
93 } | |
94 | |
95 // Returns true if messages are the same. | |
96 bool CompareLegalMessages(const LegalMessageLines& a, | |
97 const LegalMessageLines& b) { | |
98 if (a.size() != b.size()) | |
99 return false; | |
100 for (size_t i = 0; i < a.size(); ++i) { | |
101 if (!CompareLegalMessageLines(a[i], b[i])) | |
102 return false; | |
103 } | |
104 return true; | |
105 } | |
106 | |
107 void ShowLocalBubble() { | 76 void ShowLocalBubble() { |
108 controller()->ShowBubbleForLocalSave(base::Bind(&SaveCardCallback)); | 77 controller()->ShowBubbleForLocalSave(base::Bind(&SaveCardCallback)); |
109 } | 78 } |
110 | 79 |
111 void ShowUploadBubble() { | 80 void ShowUploadBubble() { |
112 SetLegalMessage( | 81 SetLegalMessage( |
113 "{" | 82 "{" |
114 " \"line\" : [ {" | 83 " \"line\" : [ {" |
115 " \"template\": \"This is the entire message.\"" | 84 " \"template\": \"This is the entire message.\"" |
116 " } ]" | 85 " } ]" |
(...skipping 30 matching lines...) Expand all Loading... |
147 | 116 |
148 private: | 117 private: |
149 scoped_ptr<TestSaveCardBubbleView> save_card_bubble_view_; | 118 scoped_ptr<TestSaveCardBubbleView> save_card_bubble_view_; |
150 }; | 119 }; |
151 | 120 |
152 static void SaveCardCallback() {} | 121 static void SaveCardCallback() {} |
153 | 122 |
154 DISALLOW_COPY_AND_ASSIGN(SaveCardBubbleControllerImplTest); | 123 DISALLOW_COPY_AND_ASSIGN(SaveCardBubbleControllerImplTest); |
155 }; | 124 }; |
156 | 125 |
157 TEST_F(SaveCardBubbleControllerImplTest, LegalMessage_NoParameters) { | |
158 SetLegalMessage( | |
159 "{" | |
160 " \"line\" : [ {" | |
161 " \"template\": \"This is the entire message.\"" | |
162 " } ]" | |
163 "}"); | |
164 | |
165 LegalMessageLine expected_line; | |
166 expected_line.text = base::ASCIIToUTF16("This is the entire message."); | |
167 LegalMessageLines expected = {expected_line}; | |
168 EXPECT_TRUE( | |
169 CompareLegalMessages(expected, controller()->GetLegalMessageLines())); | |
170 } | |
171 | |
172 TEST_F(SaveCardBubbleControllerImplTest, LegalMessage_SingleParameter) { | |
173 SetLegalMessage( | |
174 "{" | |
175 " \"line\" : [ {" | |
176 " \"template\": \"Panda {0}.\"," | |
177 " \"template_parameter\": [ {" | |
178 " \"display_text\": \"bears are fuzzy\"," | |
179 " \"url\": \"http://www.example.com\"" | |
180 " } ]" | |
181 " } ]" | |
182 "}"); | |
183 | |
184 LegalMessageLine expected_line; | |
185 expected_line.text = base::ASCIIToUTF16("Panda bears are fuzzy."); | |
186 expected_line.links = { | |
187 {{6, 21}, GURL("http://www.example.com")}, | |
188 }; | |
189 LegalMessageLines expected = {expected_line}; | |
190 EXPECT_TRUE( | |
191 CompareLegalMessages(expected, controller()->GetLegalMessageLines())); | |
192 } | |
193 | |
194 TEST_F(SaveCardBubbleControllerImplTest, LegalMessage_MissingUrl) { | |
195 SetLegalMessage( | |
196 "{" | |
197 " \"line\" : [ {" | |
198 " \"template\": \"Panda {0}.\"," | |
199 " \"template_parameter\": [ {" | |
200 " \"display_text\": \"bear\"" | |
201 " } ]" | |
202 " } ]" | |
203 "}"); | |
204 // Legal message is invalid so GetLegalMessageLines() should return no lines. | |
205 EXPECT_TRUE(CompareLegalMessages(LegalMessageLines(), | |
206 controller()->GetLegalMessageLines())); | |
207 } | |
208 | |
209 TEST_F(SaveCardBubbleControllerImplTest, LegalMessage_MissingDisplayText) { | |
210 SetLegalMessage( | |
211 "{" | |
212 " \"line\" : [ {" | |
213 " \"template\": \"Panda {0}.\"," | |
214 " \"template_parameter\": [ {" | |
215 " \"url\": \"http://www.example.com\"" | |
216 " } ]" | |
217 " } ]" | |
218 "}"); | |
219 // Legal message is invalid so GetLegalMessageLines() should return no lines. | |
220 EXPECT_TRUE(CompareLegalMessages(LegalMessageLines(), | |
221 controller()->GetLegalMessageLines())); | |
222 } | |
223 | |
224 TEST_F(SaveCardBubbleControllerImplTest, LegalMessage_EscapeCharacters) { | |
225 SetLegalMessage( | |
226 "{" | |
227 " \"line\" : [ {" | |
228 " \"template\": \"Panda '{'{0}'}' '{1}' don't $1.\"," | |
229 " \"template_parameter\": [ {" | |
230 " \"display_text\": \"bears\"," | |
231 " \"url\": \"http://www.example.com\"" | |
232 " } ]" | |
233 " } ]" | |
234 "}"); | |
235 | |
236 LegalMessageLine expected_line; | |
237 expected_line.text = base::ASCIIToUTF16("Panda {bears} {1} don't $1."); | |
238 expected_line.links = { | |
239 {{7, 12}, GURL("http://www.example.com")}, | |
240 }; | |
241 LegalMessageLines expected = {expected_line}; | |
242 EXPECT_TRUE( | |
243 CompareLegalMessages(expected, controller()->GetLegalMessageLines())); | |
244 } | |
245 | |
246 TEST_F(SaveCardBubbleControllerImplTest, LegalMessage_ConsecutiveDollarSigns) { | |
247 SetLegalMessage( | |
248 "{" | |
249 " \"line\" : [ {" | |
250 " \"template\": \"$$\"" | |
251 " } ]" | |
252 "}"); | |
253 | |
254 // Consecutive dollar signs do not expand correctly (see comment in | |
255 // ReplaceTemplatePlaceholders() in save_card_bubble_controller_impl.cc). | |
256 // If this is fixed and this test starts to fail, please update the | |
257 // "Caveats" section of the SaveCardBubbleControllerImpl::SetLegalMessage() | |
258 // header file comment. | |
259 LegalMessageLine expected_line; | |
260 expected_line.text = base::ASCIIToUTF16("$$$"); | |
261 | |
262 LegalMessageLines expected = {expected_line}; | |
263 EXPECT_TRUE( | |
264 CompareLegalMessages(expected, controller()->GetLegalMessageLines())); | |
265 } | |
266 | |
267 TEST_F(SaveCardBubbleControllerImplTest, LegalMessage_DollarAndParenthesis) { | |
268 // "${" does not expand correctly (see comment in | |
269 // ReplaceTemplatePlaceholders() in save_card_bubble_controller_impl.cc). | |
270 // If this is fixed and this test starts to fail, please update the | |
271 // "Caveats" section of the SaveCardBubbleControllerImpl::SetLegalMessage() | |
272 // header file comment. | |
273 SetLegalMessage( | |
274 "{" | |
275 " \"line\" : [ {" | |
276 " \"template\": \"${0}\"," | |
277 " \"template_parameter\": [ {" | |
278 " \"display_text\": \"bears\"," | |
279 " \"url\": \"http://www.example.com\"" | |
280 " } ]" | |
281 " } ]" | |
282 "}"); | |
283 // Legal message is invalid so GetLegalMessageLines() should return no lines. | |
284 EXPECT_TRUE(CompareLegalMessages(LegalMessageLines(), | |
285 controller()->GetLegalMessageLines())); | |
286 } | |
287 | |
288 TEST_F(SaveCardBubbleControllerImplTest, LegalMessage_MultipleParameters) { | |
289 SetLegalMessage( | |
290 "{" | |
291 " \"line\" : [ {" | |
292 " \"template\": \"Panda {0} like {2} eat {1}.\"," | |
293 " \"template_parameter\": [ {" | |
294 " \"display_text\": \"bears\"," | |
295 " \"url\": \"http://www.example.com/0\"" | |
296 " }, {" | |
297 " \"display_text\": \"bamboo\"," | |
298 " \"url\": \"http://www.example.com/1\"" | |
299 " }, {" | |
300 " \"display_text\": \"to\"," | |
301 " \"url\": \"http://www.example.com/2\"" | |
302 " } ]" | |
303 " } ]" | |
304 "}"); | |
305 | |
306 LegalMessageLine expected_line; | |
307 expected_line.text = base::ASCIIToUTF16("Panda bears like to eat bamboo."); | |
308 expected_line.links = { | |
309 {{6, 11}, GURL("http://www.example.com/0")}, | |
310 {{24, 30}, GURL("http://www.example.com/1")}, | |
311 {{17, 19}, GURL("http://www.example.com/2")}, | |
312 }; | |
313 LegalMessageLines expected = {expected_line}; | |
314 EXPECT_TRUE( | |
315 CompareLegalMessages(expected, controller()->GetLegalMessageLines())); | |
316 } | |
317 | |
318 TEST_F(SaveCardBubbleControllerImplTest, LegalMessage_MultipleLineElements) { | |
319 SetLegalMessage( | |
320 "{" | |
321 " \"line\" : [ {" | |
322 " \"template\": \"Panda {0}\"," | |
323 " \"template_parameter\": [ {" | |
324 " \"display_text\": \"bears\"," | |
325 " \"url\": \"http://www.example.com/line_0_param_0\"" | |
326 " } ]" | |
327 " }, {" | |
328 " \"template\": \"like {1} eat {0}.\"," | |
329 " \"template_parameter\": [ {" | |
330 " \"display_text\": \"bamboo\"," | |
331 " \"url\": \"http://www.example.com/line_1_param_0\"" | |
332 " }, {" | |
333 " \"display_text\": \"to\"," | |
334 " \"url\": \"http://www.example.com/line_1_param_1\"" | |
335 " } ]" | |
336 " }, {" | |
337 " \"template\": \"The {0}.\"," | |
338 " \"template_parameter\": [ {" | |
339 " \"display_text\": \"end\"," | |
340 " \"url\": \"http://www.example.com/line_2_param_0\"" | |
341 " } ]" | |
342 " } ]" | |
343 "}"); | |
344 | |
345 // Line 0. | |
346 LegalMessageLine expected_line_0; | |
347 expected_line_0.text = base::ASCIIToUTF16("Panda bears"); | |
348 expected_line_0.links = { | |
349 {{6, 11}, GURL("http://www.example.com/line_0_param_0")}, | |
350 }; | |
351 | |
352 // Line 1. | |
353 LegalMessageLine expected_line_1; | |
354 expected_line_1.text = base::ASCIIToUTF16("like to eat bamboo."); | |
355 expected_line_1.links = { | |
356 {{12, 18}, GURL("http://www.example.com/line_1_param_0")}, | |
357 {{5, 7}, GURL("http://www.example.com/line_1_param_1")}, | |
358 }; | |
359 | |
360 // Line 2. | |
361 LegalMessageLine expected_line_2; | |
362 expected_line_2.text = base::ASCIIToUTF16("The end."); | |
363 expected_line_2.links = { | |
364 {{4, 7}, GURL("http://www.example.com/line_2_param_0")}, | |
365 }; | |
366 | |
367 LegalMessageLines expected = {expected_line_0, expected_line_1, | |
368 expected_line_2}; | |
369 EXPECT_TRUE( | |
370 CompareLegalMessages(expected, controller()->GetLegalMessageLines())); | |
371 } | |
372 | |
373 TEST_F(SaveCardBubbleControllerImplTest, LegalMessage_EmbeddedNewlines) { | |
374 SetLegalMessage( | |
375 "{" | |
376 " \"line\" : [ {" | |
377 " \"template\": \"Panda {0}\nlike {2} eat {1}.\nThe {3}.\"," | |
378 " \"template_parameter\": [ {" | |
379 " \"display_text\": \"bears\"," | |
380 " \"url\": \"http://www.example.com/0\"" | |
381 " }, {" | |
382 " \"display_text\": \"bamboo\"," | |
383 " \"url\": \"http://www.example.com/1\"" | |
384 " }, {" | |
385 " \"display_text\": \"to\"," | |
386 " \"url\": \"http://www.example.com/2\"" | |
387 " }, {" | |
388 " \"display_text\": \"end\"," | |
389 " \"url\": \"http://www.example.com/3\"" | |
390 " } ]" | |
391 " } ]" | |
392 "}"); | |
393 | |
394 LegalMessageLine expected_line; | |
395 expected_line.text = | |
396 base::ASCIIToUTF16("Panda bears\nlike to eat bamboo.\nThe end."); | |
397 expected_line.links = { | |
398 {{6, 11}, GURL("http://www.example.com/0")}, | |
399 {{24, 30}, GURL("http://www.example.com/1")}, | |
400 {{17, 19}, GURL("http://www.example.com/2")}, | |
401 {{36, 39}, GURL("http://www.example.com/3")}, | |
402 }; | |
403 LegalMessageLines expected = {expected_line}; | |
404 EXPECT_TRUE( | |
405 CompareLegalMessages(expected, controller()->GetLegalMessageLines())); | |
406 } | |
407 | |
408 TEST_F(SaveCardBubbleControllerImplTest, LegalMessage_MaximumPlaceholders) { | |
409 SetLegalMessage( | |
410 "{" | |
411 " \"line\" : [ {" | |
412 " \"template\": \"a{0} b{1} c{2} d{3} e{4} f{5} g{6}\"," | |
413 " \"template_parameter\": [ {" | |
414 " \"display_text\": \"A\"," | |
415 " \"url\": \"http://www.example.com/0\"" | |
416 " }, {" | |
417 " \"display_text\": \"B\"," | |
418 " \"url\": \"http://www.example.com/1\"" | |
419 " }, {" | |
420 " \"display_text\": \"C\"," | |
421 " \"url\": \"http://www.example.com/2\"" | |
422 " }, {" | |
423 " \"display_text\": \"D\"," | |
424 " \"url\": \"http://www.example.com/3\"" | |
425 " }, {" | |
426 " \"display_text\": \"E\"," | |
427 " \"url\": \"http://www.example.com/4\"" | |
428 " }, {" | |
429 " \"display_text\": \"F\"," | |
430 " \"url\": \"http://www.example.com/5\"" | |
431 " }, {" | |
432 " \"display_text\": \"G\"," | |
433 " \"url\": \"http://www.example.com/6\"" | |
434 " } ]" | |
435 " } ]" | |
436 "}"); | |
437 | |
438 LegalMessageLine expected_line; | |
439 expected_line.text = base::ASCIIToUTF16("aA bB cC dD eE fF gG"); | |
440 expected_line.links = { | |
441 {{1, 2}, GURL("http://www.example.com/0")}, | |
442 {{4, 5}, GURL("http://www.example.com/1")}, | |
443 {{7, 8}, GURL("http://www.example.com/2")}, | |
444 {{10, 11}, GURL("http://www.example.com/3")}, | |
445 {{13, 14}, GURL("http://www.example.com/4")}, | |
446 {{16, 17}, GURL("http://www.example.com/5")}, | |
447 {{19, 20}, GURL("http://www.example.com/6")}, | |
448 }; | |
449 LegalMessageLines expected = {expected_line}; | |
450 EXPECT_TRUE( | |
451 CompareLegalMessages(expected, controller()->GetLegalMessageLines())); | |
452 } | |
453 | |
454 TEST_F(SaveCardBubbleControllerImplTest, Metrics_Local_FirstShow_ShowBubble) { | 126 TEST_F(SaveCardBubbleControllerImplTest, Metrics_Local_FirstShow_ShowBubble) { |
455 base::HistogramTester histogram_tester; | 127 base::HistogramTester histogram_tester; |
456 ShowLocalBubble(); | 128 ShowLocalBubble(); |
457 | 129 |
458 EXPECT_THAT( | 130 EXPECT_THAT( |
459 histogram_tester.GetAllSamples( | 131 histogram_tester.GetAllSamples( |
460 "Autofill.SaveCreditCardPrompt.Local.FirstShow"), | 132 "Autofill.SaveCreditCardPrompt.Local.FirstShow"), |
461 ElementsAre(Bucket(AutofillMetrics::SAVE_CARD_PROMPT_SHOW_REQUESTED, 1), | 133 ElementsAre(Bucket(AutofillMetrics::SAVE_CARD_PROMPT_SHOW_REQUESTED, 1), |
462 Bucket(AutofillMetrics::SAVE_CARD_PROMPT_SHOWN, 1))); | 134 Bucket(AutofillMetrics::SAVE_CARD_PROMPT_SHOWN, 1))); |
463 } | 135 } |
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
681 EXPECT_THAT( | 353 EXPECT_THAT( |
682 histogram_tester.GetAllSamples( | 354 histogram_tester.GetAllSamples( |
683 "Autofill.SaveCreditCardPrompt.Upload.FirstShow"), | 355 "Autofill.SaveCreditCardPrompt.Upload.FirstShow"), |
684 ElementsAre( | 356 ElementsAre( |
685 Bucket(AutofillMetrics::SAVE_CARD_PROMPT_SHOW_REQUESTED, 1), | 357 Bucket(AutofillMetrics::SAVE_CARD_PROMPT_SHOW_REQUESTED, 1), |
686 Bucket(AutofillMetrics::SAVE_CARD_PROMPT_END_INVALID_LEGAL_MESSAGE, | 358 Bucket(AutofillMetrics::SAVE_CARD_PROMPT_END_INVALID_LEGAL_MESSAGE, |
687 1))); | 359 1))); |
688 } | 360 } |
689 | 361 |
690 } // namespace autofill | 362 } // namespace autofill |
OLD | NEW |