1 // Copyright (c) 2012 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.
7 #include "base/basictypes.h"
8 #include "base/command_line.h"
9 #include "base/file_util.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/rand_util.h"
13 #include "base/strings/string16.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_split.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "base/time/time.h"
18 #include "chrome/browser/autofill/personal_data_manager_factory.h"
19 #include "chrome/browser/chrome_notification_types.h"
20 #include "chrome/browser/infobars/confirm_infobar_delegate.h"
21 #include "chrome/browser/infobars/infobar.h"
22 #include "chrome/browser/infobars/infobar_manager.h"
23 #include "chrome/browser/infobars/infobar_service.h"
24 #include "chrome/browser/profiles/profile.h"
25 #include "chrome/browser/ui/browser.h"
26 #include "chrome/browser/ui/browser_window.h"
27 #include "chrome/browser/ui/tabs/tab_strip_model.h"
28 #include "chrome/common/render_messages.h"
29 #include "chrome/test/base/in_process_browser_test.h"
30 #include "chrome/test/base/test_switches.h"
31 #include "chrome/test/base/ui_test_utils.h"
32 #include "components/autofill/content/browser/content_autofill_driver.h"
33 #include "components/autofill/core/browser/autofill_profile.h"
34 #include "components/autofill/core/browser/autofill_test_utils.h"
35 #include "components/autofill/core/browser/credit_card.h"
36 #include "components/autofill/core/browser/personal_data_manager.h"
37 #include "components/autofill/core/browser/personal_data_manager_observer.h"
38 #include "components/autofill/core/browser/validation.h"
39 #include "content/public/browser/navigation_controller.h"
40 #include "content/public/browser/notification_observer.h"
41 #include "content/public/browser/notification_registrar.h"
42 #include "content/public/browser/notification_service.h"
43 #include "content/public/browser/render_view_host.h"
44 #include "content/public/browser/web_contents.h"
45 #include "content/public/test/browser_test_utils.h"
46 #include "content/public/test/test_renderer_host.h"
47 #include "content/public/test/test_utils.h"
48 #include "net/url_request/test_url_fetcher_factory.h"
49 #include "testing/gmock/include/gmock/gmock.h"
50 #include "testing/gtest/include/gtest/gtest.h"
51 #include "ui/events/keycodes/keyboard_codes.h"
53 using base::ASCIIToUTF16
;
54 using base::WideToUTF16
;
58 class WindowedPersonalDataManagerObserver
59 : public PersonalDataManagerObserver
,
60 public content::NotificationObserver
{
62 explicit WindowedPersonalDataManagerObserver(Browser
* browser
)
64 has_run_message_loop_(false),
66 infobar_service_(NULL
) {
67 PersonalDataManagerFactory::GetForProfile(browser_
->profile())->
69 registrar_
.Add(this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED
,
70 content::NotificationService::AllSources());
73 virtual ~WindowedPersonalDataManagerObserver() {
74 if (infobar_service_
&&
75 (infobar_service_
->infobar_manager()->infobar_count() > 0)) {
76 InfoBarManager
* infobar_manager
= infobar_service_
->infobar_manager();
77 infobar_manager
->RemoveInfoBar(infobar_manager
->infobar_at(0));
83 has_run_message_loop_
= true;
84 content::RunMessageLoop();
86 PersonalDataManagerFactory::GetForProfile(browser_
->profile())->
90 // PersonalDataManagerObserver:
91 virtual void OnPersonalDataChanged() OVERRIDE
{
92 if (has_run_message_loop_
) {
93 base::MessageLoopForUI::current()->Quit();
94 has_run_message_loop_
= false;
99 virtual void OnInsufficientFormData() OVERRIDE
{
100 OnPersonalDataChanged();
103 // content::NotificationObserver:
104 virtual void Observe(int type
,
105 const content::NotificationSource
& source
,
106 const content::NotificationDetails
& details
) OVERRIDE
{
107 EXPECT_EQ(chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED
, type
);
108 infobar_service_
= InfoBarService::FromWebContents(
109 browser_
->tab_strip_model()->GetActiveWebContents());
110 InfoBarManager
* infobar_manager
= infobar_service_
->infobar_manager();
111 ConfirmInfoBarDelegate
* infobar_delegate
=
112 infobar_manager
->infobar_at(0)->delegate()->AsConfirmInfoBarDelegate();
113 ASSERT_TRUE(infobar_delegate
);
114 infobar_delegate
->Accept();
119 bool has_run_message_loop_
;
121 content::NotificationRegistrar registrar_
;
122 InfoBarService
* infobar_service_
;
125 class AutofillTest
: public InProcessBrowserTest
{
129 virtual void SetUpOnMainThread() OVERRIDE
{
130 // Don't want Keychain coming up on Mac.
131 test::DisableSystemServices(browser()->profile()->GetPrefs());
134 virtual void CleanUpOnMainThread() OVERRIDE
{
135 // Make sure to close any showing popups prior to tearing down the UI.
136 content::WebContents
* web_contents
=
137 browser()->tab_strip_model()->GetActiveWebContents();
138 AutofillManager
* autofill_manager
= ContentAutofillDriver::FromWebContents(
139 web_contents
)->autofill_manager();
140 autofill_manager
->delegate()->HideAutofillPopup();
143 PersonalDataManager
* personal_data_manager() {
144 return PersonalDataManagerFactory::GetForProfile(browser()->profile());
147 void SetProfiles(std::vector
<AutofillProfile
>* profiles
) {
148 WindowedPersonalDataManagerObserver
observer(browser());
149 personal_data_manager()->SetProfiles(profiles
);
153 void SetProfile(const AutofillProfile
& profile
) {
154 std::vector
<AutofillProfile
> profiles
;
155 profiles
.push_back(profile
);
156 SetProfiles(&profiles
);
159 void SetCards(std::vector
<CreditCard
>* cards
) {
160 WindowedPersonalDataManagerObserver
observer(browser());
161 personal_data_manager()->SetCreditCards(cards
);
165 void SetCard(const CreditCard
& card
) {
166 std::vector
<CreditCard
> cards
;
167 cards
.push_back(card
);
171 typedef std::map
<std::string
, std::string
> FormMap
;
172 // Navigate to the form, input values into the fields, and submit the form.
173 // The function returns after the PersonalDataManager is updated.
174 void FillFormAndSubmit(const std::string
& filename
, const FormMap
& data
) {
175 GURL url
= test_server()->GetURL("files/autofill/" + filename
);
176 ui_test_utils::NavigateToURL(browser(), url
);
179 for (FormMap::const_iterator i
= data
.begin(); i
!= data
.end(); ++i
) {
180 js
+= "document.getElementById('" + i
->first
+ "').value = '" +
183 js
+= "document.onclick = function() {"
184 " document.getElementById('testform').submit();"
187 WindowedPersonalDataManagerObserver
observer(browser());
188 ASSERT_TRUE(content::ExecuteScript(render_view_host(), js
));
189 // Simulate a mouse click to submit the form because form submissions not
190 // triggered by user gestures are ignored.
191 content::SimulateMouseClick(
192 browser()->tab_strip_model()->GetActiveWebContents(), 0,
193 blink::WebMouseEvent::ButtonLeft
);
197 void SubmitCreditCard(const char* name
,
199 const char* exp_month
,
200 const char* exp_year
) {
202 data
["CREDIT_CARD_NAME"] = name
;
203 data
["CREDIT_CARD_NUMBER"] = number
;
204 data
["CREDIT_CARD_EXP_MONTH"] = exp_month
;
205 data
["CREDIT_CARD_EXP_4_DIGIT_YEAR"] = exp_year
;
206 FillFormAndSubmit("autofill_creditcard_form.html", data
);
209 // Aggregate profiles from forms into Autofill preferences. Returns the number
210 // of parsed profiles.
211 int AggregateProfilesIntoAutofillPrefs(const std::string
& filename
) {
212 CHECK(test_server()->Start());
215 base::FilePath data_file
=
216 ui_test_utils::GetTestFilePath(base::FilePath().AppendASCII("autofill"),
217 base::FilePath().AppendASCII(filename
));
218 CHECK(base::ReadFileToString(data_file
, &data
));
219 std::vector
<std::string
> lines
;
220 base::SplitString(data
, '\n', &lines
);
221 int parsed_profiles
= 0;
222 for (size_t i
= 0; i
< lines
.size(); ++i
) {
223 if (StartsWithASCII(lines
[i
], "#", false))
226 std::vector
<std::string
> fields
;
227 base::SplitString(lines
[i
], '|', &fields
);
229 continue; // Blank line.
232 CHECK_EQ(12u, fields
.size());
235 data
["NAME_FIRST"] = fields
[0];
236 data
["NAME_MIDDLE"] = fields
[1];
237 data
["NAME_LAST"] = fields
[2];
238 data
["EMAIL_ADDRESS"] = fields
[3];
239 data
["COMPANY_NAME"] = fields
[4];
240 data
["ADDRESS_HOME_LINE1"] = fields
[5];
241 data
["ADDRESS_HOME_LINE2"] = fields
[6];
242 data
["ADDRESS_HOME_CITY"] = fields
[7];
243 data
["ADDRESS_HOME_STATE"] = fields
[8];
244 data
["ADDRESS_HOME_ZIP"] = fields
[9];
245 data
["ADDRESS_HOME_COUNTRY"] = fields
[10];
246 data
["PHONE_HOME_WHOLE_NUMBER"] = fields
[11];
248 FillFormAndSubmit("duplicate_profiles_test.html", data
);
250 return parsed_profiles
;
253 void ExpectFieldValue(const std::string
& field_name
,
254 const std::string
& expected_value
) {
256 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
257 browser()->tab_strip_model()->GetActiveWebContents(),
258 "window.domAutomationController.send("
259 " document.getElementById('" + field_name
+ "').value);",
261 EXPECT_EQ(expected_value
, value
);
264 content::RenderViewHost
* render_view_host() {
265 return browser()->tab_strip_model()->GetActiveWebContents()->
269 void ExpectFilledTestForm() {
270 ExpectFieldValue("firstname", "Milton");
271 ExpectFieldValue("lastname", "Waddams");
272 ExpectFieldValue("address1", "4120 Freidrich Lane");
273 ExpectFieldValue("address2", "Basement");
274 ExpectFieldValue("city", "Austin");
275 ExpectFieldValue("state", "TX");
276 ExpectFieldValue("zip", "78744");
277 ExpectFieldValue("country", "US");
278 ExpectFieldValue("phone", "5125551234");
282 net::TestURLFetcherFactory url_fetcher_factory_
;
285 // Test filling profiles with unicode strings and crazy characters.
286 // TODO(isherman): rewrite as unit test under PersonalDataManagerTest.
287 IN_PROC_BROWSER_TEST_F(AutofillTest
, FillProfileCrazyCharacters
) {
288 std::vector
<AutofillProfile
> profiles
;
289 AutofillProfile profile1
;
290 profile1
.SetRawInfo(NAME_FIRST
,
291 WideToUTF16(L
"\u0623\u0648\u0628\u0627\u0645\u0627 "
292 L
"\u064a\u0639\u062a\u0630\u0631 "
293 L
"\u0647\u0627\u062a\u0641\u064a\u0627 "
294 L
"\u0644\u0645\u0648\u0638\u0641\u0629 "
295 L
"\u0633\u0648\u062f\u0627\u0621 "
296 L
"\u0627\u0633\u062a\u0642\u0627\u0644\u062a "
297 L
"\u0628\u0633\u0628\u0628 "
298 L
"\u062a\u0635\u0631\u064a\u062d\u0627\u062a "
299 L
"\u0645\u062c\u062a\u0632\u0623\u0629"));
300 profile1
.SetRawInfo(NAME_MIDDLE
, WideToUTF16(L
"BANK\xcBERF\xc4LLE"));
301 profile1
.SetRawInfo(EMAIL_ADDRESS
,
302 WideToUTF16(L
"\uacbd\uc81c \ub274\uc2a4 "
303 L
"\ub354\ubcf4\uae30@google.com"));
304 profile1
.SetRawInfo(ADDRESS_HOME_LINE1
,
305 WideToUTF16(L
"\uad6d\uc815\uc6d0\xb7\uac80\ucc30, "
306 L
"\ub178\ubb34\ud604\uc815\ubd80 "
307 L
"\ub300\ubd81\uc811\ucd09 \ub2f4\ub2f9 "
308 L
"\uc778\uc0ac\ub4e4 \uc870\uc0ac"));
309 profile1
.SetRawInfo(ADDRESS_HOME_CITY
,
310 WideToUTF16(L
"\u653f\u5e9c\u4e0d\u6392\u9664\u7acb\u6cd5"
311 L
"\u898f\u7ba1\u5c0e\u904a"));
312 profile1
.SetRawInfo(ADDRESS_HOME_ZIP
, WideToUTF16(L
"YOHO_54676"));
313 profile1
.SetRawInfo(PHONE_HOME_WHOLE_NUMBER
, WideToUTF16(L
"861088828000"));
315 AutofillType(ADDRESS_HOME_COUNTRY
), WideToUTF16(L
"India"), "en-US");
316 profiles
.push_back(profile1
);
318 AutofillProfile profile2
;
319 profile2
.SetRawInfo(NAME_FIRST
,
320 WideToUTF16(L
"\u4e0a\u6d77\u5e02\u91d1\u5c71\u533a "
321 L
"\u677e\u9690\u9547\u4ead\u67ab\u516c"
322 L
"\u8def1915\u53f7"));
323 profile2
.SetRawInfo(NAME_LAST
, WideToUTF16(L
"aguantó"));
324 profile2
.SetRawInfo(ADDRESS_HOME_ZIP
, WideToUTF16(L
"HOME 94043"));
325 profiles
.push_back(profile2
);
327 AutofillProfile profile3
;
328 profile3
.SetRawInfo(EMAIL_ADDRESS
, WideToUTF16(L
"sue@example.com"));
329 profile3
.SetRawInfo(COMPANY_NAME
, WideToUTF16(L
"Company X"));
330 profiles
.push_back(profile3
);
332 AutofillProfile profile4
;
333 profile4
.SetRawInfo(NAME_FIRST
, WideToUTF16(L
"Joe 3254"));
334 profile4
.SetRawInfo(NAME_LAST
, WideToUTF16(L
"\u8bb0\u8d262\u5e74\u591a"));
335 profile4
.SetRawInfo(ADDRESS_HOME_ZIP
,
336 WideToUTF16(L
"\uff08\u90ae\u7f16\uff1a201504\uff09"));
337 profile4
.SetRawInfo(EMAIL_ADDRESS
, WideToUTF16(L
"télévision@example.com"));
338 profile4
.SetRawInfo(COMPANY_NAME
,
339 WideToUTF16(L
"\u0907\u0932\u0947\u0915\u093f\u091f\u094d"
340 L
"\u0930\u0928\u093f\u0915\u094d\u0938, "
341 L
"\u0905\u092a\u094b\u0932\u094b "
342 L
"\u091f\u093e\u092f\u0930\u094d\u0938 "
343 L
"\u0906\u0926\u093f"));
344 profiles
.push_back(profile4
);
346 AutofillProfile profile5
;
347 profile5
.SetRawInfo(NAME_FIRST
, WideToUTF16(L
"Larry"));
348 profile5
.SetRawInfo(NAME_LAST
,
349 WideToUTF16(L
"\u0938\u094d\u091f\u093e\u0902\u092a "
350 L
"\u0921\u094d\u092f\u0942\u091f\u0940"));
351 profile5
.SetRawInfo(ADDRESS_HOME_ZIP
,
352 WideToUTF16(L
"111111111111110000GOOGLE"));
353 profile5
.SetRawInfo(EMAIL_ADDRESS
, WideToUTF16(L
"page@000000.com"));
354 profile5
.SetRawInfo(COMPANY_NAME
, WideToUTF16(L
"Google"));
355 profiles
.push_back(profile5
);
357 AutofillProfile profile6
;
358 profile6
.SetRawInfo(NAME_FIRST
,
359 WideToUTF16(L
"\u4e0a\u6d77\u5e02\u91d1\u5c71\u533a "
360 L
"\u677e\u9690\u9547\u4ead\u67ab\u516c"
361 L
"\u8def1915\u53f7"));
362 profile6
.SetRawInfo(NAME_LAST
,
363 WideToUTF16(L
"\u0646\u062c\u0627\u0645\u064a\u0646\u0627 "
364 L
"\u062f\u0639\u0645\u0647\u0627 "
365 L
"\u0644\u0644\u0631\u0626\u064a\u0633 "
366 L
"\u0627\u0644\u0633\u0648\u062f\u0627\u0646"
367 L
"\u064a \u0639\u0645\u0631 "
368 L
"\u0627\u0644\u0628\u0634\u064a\u0631"));
369 profile6
.SetRawInfo(ADDRESS_HOME_ZIP
, WideToUTF16(L
"HOME 94043"));
370 profiles
.push_back(profile6
);
372 AutofillProfile profile7
;
373 profile7
.SetRawInfo(NAME_FIRST
, WideToUTF16(L
"&$%$$$ TESTO *&*&^&^& MOKO"));
374 profile7
.SetRawInfo(NAME_MIDDLE
, WideToUTF16(L
"WOHOOOO$$$$$$$$****"));
375 profile7
.SetRawInfo(EMAIL_ADDRESS
, WideToUTF16(L
"yuvu@example.com"));
376 profile7
.SetRawInfo(ADDRESS_HOME_LINE1
,
377 WideToUTF16(L
"34544, anderson ST.(120230)"));
378 profile7
.SetRawInfo(ADDRESS_HOME_CITY
, WideToUTF16(L
"Sunnyvale"));
379 profile7
.SetRawInfo(ADDRESS_HOME_STATE
, WideToUTF16(L
"CA"));
380 profile7
.SetRawInfo(ADDRESS_HOME_ZIP
, WideToUTF16(L
"94086"));
381 profile7
.SetRawInfo(PHONE_HOME_WHOLE_NUMBER
, WideToUTF16(L
"15466784565"));
383 AutofillType(ADDRESS_HOME_COUNTRY
), WideToUTF16(L
"United States"),
385 profiles
.push_back(profile7
);
387 SetProfiles(&profiles
);
388 ASSERT_EQ(profiles
.size(), personal_data_manager()->GetProfiles().size());
389 for (size_t i
= 0; i
< profiles
.size(); ++i
)
390 ASSERT_EQ(profiles
[i
], *personal_data_manager()->GetProfiles()[i
]);
392 std::vector
<CreditCard
> cards
;
394 card1
.SetRawInfo(CREDIT_CARD_NAME
,
395 WideToUTF16(L
"\u751f\u6d3b\u5f88\u6709\u89c4\u5f8b "
396 L
"\u4ee5\u73a9\u4e3a\u4e3b"));
397 card1
.SetRawInfo(CREDIT_CARD_NUMBER
, WideToUTF16(L
"6011111111111117"));
398 card1
.SetRawInfo(CREDIT_CARD_EXP_MONTH
, WideToUTF16(L
"12"));
399 card1
.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR
, WideToUTF16(L
"2011"));
400 cards
.push_back(card1
);
403 card2
.SetRawInfo(CREDIT_CARD_NAME
, WideToUTF16(L
"John Williams"));
404 card2
.SetRawInfo(CREDIT_CARD_NUMBER
, WideToUTF16(L
"WokoAwesome12345"));
405 card2
.SetRawInfo(CREDIT_CARD_EXP_MONTH
, WideToUTF16(L
"10"));
406 card2
.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR
, WideToUTF16(L
"2015"));
407 cards
.push_back(card2
);
410 card3
.SetRawInfo(CREDIT_CARD_NAME
,
411 WideToUTF16(L
"\u0623\u062d\u0645\u062f\u064a "
412 L
"\u0646\u062c\u0627\u062f "
413 L
"\u0644\u0645\u062d\u0627\u0648\u0644\u0647 "
414 L
"\u0627\u063a\u062a\u064a\u0627\u0644 "
415 L
"\u0641\u064a \u0645\u062f\u064a\u0646\u0629 "
416 L
"\u0647\u0645\u062f\u0627\u0646 "));
417 card3
.SetRawInfo(CREDIT_CARD_NUMBER
,
418 WideToUTF16(L
"\u092a\u0941\u0928\u0930\u094d\u091c\u0940"
419 L
"\u0935\u093f\u0924 \u0939\u094b\u0917\u093e "
420 L
"\u0928\u093e\u0932\u0902\u0926\u093e"));
421 card3
.SetRawInfo(CREDIT_CARD_EXP_MONTH
, WideToUTF16(L
"10"));
422 card3
.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR
, WideToUTF16(L
"2015"));
423 cards
.push_back(card3
);
426 card4
.SetRawInfo(CREDIT_CARD_NAME
,
427 WideToUTF16(L
"\u039d\u03ad\u03b5\u03c2 "
428 L
"\u03c3\u03c5\u03b3\u03c7\u03c9\u03bd\u03b5"
429 L
"\u03cd\u03c3\u03b5\u03b9\u03c2 "
430 L
"\u03ba\u03b1\u03b9 "
431 L
"\u03ba\u03b1\u03c4\u03b1\u03c1\u03b3\u03ae"
432 L
"\u03c3\u03b5\u03b9\u03c2"));
433 card4
.SetRawInfo(CREDIT_CARD_NUMBER
, WideToUTF16(L
"00000000000000000000000"));
434 card4
.SetRawInfo(CREDIT_CARD_EXP_MONTH
, WideToUTF16(L
"01"));
435 card4
.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR
, WideToUTF16(L
"2016"));
436 cards
.push_back(card4
);
439 ASSERT_EQ(cards
.size(), personal_data_manager()->GetCreditCards().size());
440 for (size_t i
= 0; i
< cards
.size(); ++i
)
441 ASSERT_EQ(cards
[i
], *personal_data_manager()->GetCreditCards()[i
]);
444 // Test filling in invalid values for profiles are saved as-is. Phone
445 // information entered into the prefs UI is not validated or rejected except for
447 // TODO(isherman): rewrite as WebUI test?
448 IN_PROC_BROWSER_TEST_F(AutofillTest
, Invalid
) {
449 // First try profiles with invalid ZIP input.
450 AutofillProfile without_invalid
;
451 without_invalid
.SetRawInfo(NAME_FIRST
, ASCIIToUTF16("Will"));
452 without_invalid
.SetRawInfo(ADDRESS_HOME_CITY
, ASCIIToUTF16("Sunnyvale"));
453 without_invalid
.SetRawInfo(ADDRESS_HOME_STATE
, ASCIIToUTF16("CA"));
454 without_invalid
.SetRawInfo(ADDRESS_HOME_ZIP
, ASCIIToUTF16("my_zip"));
455 without_invalid
.SetInfo(
456 AutofillType(ADDRESS_HOME_COUNTRY
), ASCIIToUTF16("United States"),
459 AutofillProfile with_invalid
= without_invalid
;
460 with_invalid
.SetRawInfo(PHONE_HOME_WHOLE_NUMBER
,
461 ASCIIToUTF16("Invalid_Phone_Number"));
462 SetProfile(with_invalid
);
464 ASSERT_EQ(1u, personal_data_manager()->GetProfiles().size());
465 AutofillProfile profile
= *personal_data_manager()->GetProfiles()[0];
466 ASSERT_NE(without_invalid
.GetRawInfo(PHONE_HOME_WHOLE_NUMBER
),
467 profile
.GetRawInfo(PHONE_HOME_WHOLE_NUMBER
));
470 // Test invalid credit card numbers typed in prefs should be saved as-is.
471 // TODO(isherman): rewrite as WebUI test?
472 IN_PROC_BROWSER_TEST_F(AutofillTest
, PrefsStringSavedAsIs
) {
474 card
.SetRawInfo(CREDIT_CARD_NUMBER
, ASCIIToUTF16("Not_0123-5Checked"));
477 ASSERT_EQ(1u, personal_data_manager()->GetCreditCards().size());
478 ASSERT_EQ(card
, *personal_data_manager()->GetCreditCards()[0]);
481 // Test credit card info with an invalid number is not aggregated.
482 // When filling out a form with an invalid credit card number (one that does not
483 // pass the Luhn test) the credit card info should not be saved into Autofill
485 IN_PROC_BROWSER_TEST_F(AutofillTest
, InvalidCreditCardNumberIsNotAggregated
) {
486 #if defined(OS_WIN) && defined(USE_ASH)
487 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
488 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
492 ASSERT_TRUE(test_server()->Start());
493 std::string
card("4408 0412 3456 7890");
494 ASSERT_FALSE(autofill::IsValidCreditCardNumber(ASCIIToUTF16(card
)));
495 SubmitCreditCard("Bob Smith", card
.c_str(), "12", "2014");
496 InfoBarService
* infobar_service
= InfoBarService::FromWebContents(
497 browser()->tab_strip_model()->GetActiveWebContents());
498 ASSERT_EQ(0u, infobar_service
->infobar_manager()->infobar_count());
501 // Test whitespaces and separator chars are stripped for valid CC numbers.
502 // The credit card numbers used in this test pass the Luhn test. For reference:
503 // http://www.merriampark.com/anatomycc.htm
504 IN_PROC_BROWSER_TEST_F(AutofillTest
,
505 WhitespacesAndSeparatorCharsStrippedForValidCCNums
) {
506 #if defined(OS_WIN) && defined(USE_ASH)
507 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
508 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
512 ASSERT_TRUE(test_server()->Start());
513 SubmitCreditCard("Bob Smith", "4408 0412 3456 7893", "12", "2014");
514 SubmitCreditCard("Jane Doe", "4417-1234-5678-9113", "10", "2013");
516 ASSERT_EQ(2u, personal_data_manager()->GetCreditCards().size());
517 base::string16 cc1
= personal_data_manager()->GetCreditCards()[0]->GetRawInfo(
519 ASSERT_TRUE(autofill::IsValidCreditCardNumber(cc1
));
520 base::string16 cc2
= personal_data_manager()->GetCreditCards()[1]->GetRawInfo(
522 ASSERT_TRUE(autofill::IsValidCreditCardNumber(cc2
));
525 // Test that Autofill aggregates a minimum valid profile.
526 // The minimum required address fields must be specified: First Name, Last Name,
527 // Address Line 1, City, Zip Code, and State.
528 IN_PROC_BROWSER_TEST_F(AutofillTest
, AggregatesMinValidProfile
) {
529 ASSERT_TRUE(test_server()->Start());
531 data
["NAME_FIRST"] = "Bob";
532 data
["NAME_LAST"] = "Smith";
533 data
["ADDRESS_HOME_LINE1"] = "1234 H St.";
534 data
["ADDRESS_HOME_CITY"] = "Mountain View";
535 data
["ADDRESS_HOME_STATE"] = "CA";
536 data
["ADDRESS_HOME_ZIP"] = "94043";
537 FillFormAndSubmit("duplicate_profiles_test.html", data
);
539 ASSERT_EQ(1u, personal_data_manager()->GetProfiles().size());
542 // Test Autofill does not aggregate profiles with no address info.
543 // The minimum required address fields must be specified: First Name, Last Name,
544 // Address Line 1, City, Zip Code, and State.
545 IN_PROC_BROWSER_TEST_F(AutofillTest
, ProfilesNotAggregatedWithNoAddress
) {
546 ASSERT_TRUE(test_server()->Start());
548 data
["NAME_FIRST"] = "Bob";
549 data
["NAME_LAST"] = "Smith";
550 data
["EMAIL_ADDRESS"] = "bsmith@example.com";
551 data
["COMPANY_NAME"] = "Mountain View";
552 data
["ADDRESS_HOME_CITY"] = "Mountain View";
553 data
["PHONE_HOME_WHOLE_NUMBER"] = "650-555-4567";
554 FillFormAndSubmit("duplicate_profiles_test.html", data
);
556 ASSERT_TRUE(personal_data_manager()->GetProfiles().empty());
559 // Test Autofill does not aggregate profiles with an invalid email.
560 IN_PROC_BROWSER_TEST_F(AutofillTest
, ProfilesNotAggregatedWithInvalidEmail
) {
561 ASSERT_TRUE(test_server()->Start());
563 data
["NAME_FIRST"] = "Bob";
564 data
["NAME_LAST"] = "Smith";
565 data
["EMAIL_ADDRESS"] = "garbage";
566 data
["ADDRESS_HOME_LINE1"] = "1234 H St.";
567 data
["ADDRESS_HOME_CITY"] = "San Jose";
568 data
["ADDRESS_HOME_STATE"] = "CA";
569 data
["ADDRESS_HOME_ZIP"] = "95110";
570 data
["COMPANY_NAME"] = "Company X";
571 data
["PHONE_HOME_WHOLE_NUMBER"] = "408-871-4567";
572 FillFormAndSubmit("duplicate_profiles_test.html", data
);
574 ASSERT_TRUE(personal_data_manager()->GetProfiles().empty());
577 // Test profile is saved if phone number is valid in selected country.
578 // The data file contains two profiles with valid phone numbers and two
579 // profiles with invalid phone numbers from their respective country.
580 IN_PROC_BROWSER_TEST_F(AutofillTest
, ProfileSavedWithValidCountryPhone
) {
581 ASSERT_TRUE(test_server()->Start());
582 std::vector
<FormMap
> profiles
;
585 data1
["NAME_FIRST"] = "Bob";
586 data1
["NAME_LAST"] = "Smith";
587 data1
["ADDRESS_HOME_LINE1"] = "123 Cherry Ave";
588 data1
["ADDRESS_HOME_CITY"] = "Mountain View";
589 data1
["ADDRESS_HOME_STATE"] = "CA";
590 data1
["ADDRESS_HOME_ZIP"] = "94043";
591 data1
["ADDRESS_HOME_COUNTRY"] = "United States";
592 data1
["PHONE_HOME_WHOLE_NUMBER"] = "408-871-4567";
593 profiles
.push_back(data1
);
596 data2
["NAME_FIRST"] = "John";
597 data2
["NAME_LAST"] = "Doe";
598 data2
["ADDRESS_HOME_LINE1"] = "987 H St";
599 data2
["ADDRESS_HOME_CITY"] = "San Jose";
600 data2
["ADDRESS_HOME_STATE"] = "CA";
601 data2
["ADDRESS_HOME_ZIP"] = "95510";
602 data2
["ADDRESS_HOME_COUNTRY"] = "United States";
603 data2
["PHONE_HOME_WHOLE_NUMBER"] = "408-123-456";
604 profiles
.push_back(data2
);
607 data3
["NAME_FIRST"] = "Jane";
608 data3
["NAME_LAST"] = "Doe";
609 data3
["ADDRESS_HOME_LINE1"] = "1523 Garcia St";
610 data3
["ADDRESS_HOME_CITY"] = "Mountain View";
611 data3
["ADDRESS_HOME_STATE"] = "CA";
612 data3
["ADDRESS_HOME_ZIP"] = "94043";
613 data3
["ADDRESS_HOME_COUNTRY"] = "Germany";
614 data3
["PHONE_HOME_WHOLE_NUMBER"] = "+49 40-80-81-79-000";
615 profiles
.push_back(data3
);
618 data4
["NAME_FIRST"] = "Bonnie";
619 data4
["NAME_LAST"] = "Smith";
620 data4
["ADDRESS_HOME_LINE1"] = "6723 Roadway Rd";
621 data4
["ADDRESS_HOME_CITY"] = "San Jose";
622 data4
["ADDRESS_HOME_STATE"] = "CA";
623 data4
["ADDRESS_HOME_ZIP"] = "95510";
624 data4
["ADDRESS_HOME_COUNTRY"] = "Germany";
625 data4
["PHONE_HOME_WHOLE_NUMBER"] = "+21 08450 777 777";
626 profiles
.push_back(data4
);
628 for (size_t i
= 0; i
< profiles
.size(); ++i
)
629 FillFormAndSubmit("autofill_test_form.html", profiles
[i
]);
631 ASSERT_EQ(2u, personal_data_manager()->GetProfiles().size());
632 ASSERT_EQ(ASCIIToUTF16("(408) 871-4567"),
633 personal_data_manager()->GetProfiles()[0]->GetRawInfo(
634 PHONE_HOME_WHOLE_NUMBER
));
635 ASSERT_EQ(ASCIIToUTF16("+49 40 808179000"),
636 personal_data_manager()->GetProfiles()[1]->GetRawInfo(
637 PHONE_HOME_WHOLE_NUMBER
));
640 // Test Autofill appends country codes to aggregated phone numbers.
641 // The country code is added for the following case:
642 // The phone number contains the correct national number size and
643 // is a valid format.
644 IN_PROC_BROWSER_TEST_F(AutofillTest
, AppendCountryCodeForAggregatedPhones
) {
645 ASSERT_TRUE(test_server()->Start());
647 data
["NAME_FIRST"] = "Bob";
648 data
["NAME_LAST"] = "Smith";
649 data
["ADDRESS_HOME_LINE1"] = "1234 H St.";
650 data
["ADDRESS_HOME_CITY"] = "San Jose";
651 data
["ADDRESS_HOME_STATE"] = "CA";
652 data
["ADDRESS_HOME_ZIP"] = "95110";
653 data
["ADDRESS_HOME_COUNTRY"] = "Germany";
654 data
["PHONE_HOME_WHOLE_NUMBER"] = "(08) 450 777-777";
655 FillFormAndSubmit("autofill_test_form.html", data
);
657 ASSERT_EQ(1u, personal_data_manager()->GetProfiles().size());
658 base::string16 phone
= personal_data_manager()->GetProfiles()[0]->GetRawInfo(
659 PHONE_HOME_WHOLE_NUMBER
);
660 ASSERT_TRUE(StartsWith(phone
, ASCIIToUTF16("+49"), true));
663 // Test CC info not offered to be saved when autocomplete=off for CC field.
664 // If the credit card number field has autocomplete turned off, then the credit
665 // card infobar should not offer to save the credit card info. The credit card
666 // number must be a valid Luhn number.
667 IN_PROC_BROWSER_TEST_F(AutofillTest
, CCInfoNotStoredWhenAutocompleteOff
) {
668 #if defined(OS_WIN) && defined(USE_ASH)
669 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
670 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
674 ASSERT_TRUE(test_server()->Start());
676 data
["CREDIT_CARD_NAME"] = "Bob Smith";
677 data
["CREDIT_CARD_NUMBER"] = "4408041234567893";
678 data
["CREDIT_CARD_EXP_MONTH"] = "12";
679 data
["CREDIT_CARD_EXP_4_DIGIT_YEAR"] = "2014";
680 FillFormAndSubmit("cc_autocomplete_off_test.html", data
);
682 InfoBarService
* infobar_service
= InfoBarService::FromWebContents(
683 browser()->tab_strip_model()->GetActiveWebContents());
684 ASSERT_EQ(0u, infobar_service
->infobar_manager()->infobar_count());
687 // Test profile not aggregated if email found in non-email field.
688 IN_PROC_BROWSER_TEST_F(AutofillTest
, ProfileWithEmailInOtherFieldNotSaved
) {
689 ASSERT_TRUE(test_server()->Start());
692 data
["NAME_FIRST"] = "Bob";
693 data
["NAME_LAST"] = "Smith";
694 data
["ADDRESS_HOME_LINE1"] = "bsmith@gmail.com";
695 data
["ADDRESS_HOME_CITY"] = "San Jose";
696 data
["ADDRESS_HOME_STATE"] = "CA";
697 data
["ADDRESS_HOME_ZIP"] = "95110";
698 data
["COMPANY_NAME"] = "Company X";
699 data
["PHONE_HOME_WHOLE_NUMBER"] = "408-871-4567";
700 FillFormAndSubmit("duplicate_profiles_test.html", data
);
702 ASSERT_EQ(0u, personal_data_manager()->GetProfiles().size());
705 // Test that profiles merge for aggregated data with same address.
706 // The criterion for when two profiles are expected to be merged is when their
707 // 'Address Line 1' and 'City' data match. When two profiles are merged, any
708 // remaining address fields are expected to be overwritten. Any non-address
709 // fields should accumulate multi-valued data.
710 // DISABLED: http://crbug.com/281541
711 IN_PROC_BROWSER_TEST_F(AutofillTest
,
712 DISABLED_MergeAggregatedProfilesWithSameAddress
) {
713 AggregateProfilesIntoAutofillPrefs("dataset_same_address.txt");
715 ASSERT_EQ(3u, personal_data_manager()->GetProfiles().size());
718 // Test profiles are not merged without mininum address values.
719 // Mininum address values needed during aggregation are: address line 1, city,
720 // state, and zip code.
721 // Profiles are merged when data for address line 1 and city match.
722 // DISABLED: http://crbug.com/281541
723 IN_PROC_BROWSER_TEST_F(AutofillTest
,
724 DISABLED_ProfilesNotMergedWhenNoMinAddressData
) {
725 AggregateProfilesIntoAutofillPrefs("dataset_no_address.txt");
727 ASSERT_EQ(0u, personal_data_manager()->GetProfiles().size());
730 // Test Autofill ability to merge duplicate profiles and throw away junk.
731 // TODO(isherman): this looks redundant, consider removing.
732 // DISABLED: http://crbug.com/281541
733 IN_PROC_BROWSER_TEST_F(AutofillTest
,
734 DISABLED_MergeAggregatedDuplicatedProfiles
) {
735 int num_of_profiles
=
736 AggregateProfilesIntoAutofillPrefs("dataset_duplicated_profiles.txt");
738 ASSERT_GT(num_of_profiles
,
739 static_cast<int>(personal_data_manager()->GetProfiles().size()));
742 } // namespace autofill