Add more checks to investigate SupervisedUserPrefStore crash at startup.
[chromium-blink-merge.git] / chrome / browser / autofill / autofill_browsertest.cc
blob1aa5e686f7ff8341d9011d036edb33d6cdf33ad0
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.
5 #include <string>
7 #include "base/basictypes.h"
8 #include "base/command_line.h"
9 #include "base/files/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/infobars/infobar_service.h"
20 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/browser/ui/browser.h"
22 #include "chrome/browser/ui/browser_window.h"
23 #include "chrome/browser/ui/tabs/tab_strip_model.h"
24 #include "chrome/common/render_messages.h"
25 #include "chrome/test/base/in_process_browser_test.h"
26 #include "chrome/test/base/test_switches.h"
27 #include "chrome/test/base/ui_test_utils.h"
28 #include "components/autofill/content/browser/content_autofill_driver.h"
29 #include "components/autofill/content/browser/content_autofill_driver_factory.h"
30 #include "components/autofill/core/browser/autofill_profile.h"
31 #include "components/autofill/core/browser/autofill_test_utils.h"
32 #include "components/autofill/core/browser/credit_card.h"
33 #include "components/autofill/core/browser/personal_data_manager.h"
34 #include "components/autofill/core/browser/personal_data_manager_observer.h"
35 #include "components/autofill/core/browser/validation.h"
36 #include "components/infobars/core/confirm_infobar_delegate.h"
37 #include "components/infobars/core/infobar.h"
38 #include "components/infobars/core/infobar_manager.h"
39 #include "content/public/browser/navigation_controller.h"
40 #include "content/public/browser/render_view_host.h"
41 #include "content/public/browser/web_contents.h"
42 #include "content/public/test/browser_test_utils.h"
43 #include "content/public/test/test_renderer_host.h"
44 #include "content/public/test/test_utils.h"
45 #include "net/url_request/test_url_fetcher_factory.h"
46 #include "testing/gmock/include/gmock/gmock.h"
47 #include "testing/gtest/include/gtest/gtest.h"
48 #include "ui/events/keycodes/keyboard_codes.h"
50 using base::ASCIIToUTF16;
51 using base::UTF16ToASCII;
52 using base::WideToUTF16;
54 namespace autofill {
56 // TODO(bondd): PdmChangeWaiter in autofill_uitest_util.cc is a replacement for
57 // this class. Remove this class and use helper functions in that file instead.
58 class WindowedPersonalDataManagerObserver
59 : public PersonalDataManagerObserver,
60 public infobars::InfoBarManager::Observer {
61 public:
62 explicit WindowedPersonalDataManagerObserver(Browser* browser)
63 : alerted_(false),
64 has_run_message_loop_(false),
65 browser_(browser),
66 infobar_service_(InfoBarService::FromWebContents(
67 browser_->tab_strip_model()->GetActiveWebContents())) {
68 PersonalDataManagerFactory::GetForProfile(browser_->profile())->
69 AddObserver(this);
70 infobar_service_->AddObserver(this);
73 ~WindowedPersonalDataManagerObserver() override {
74 infobar_service_->RemoveObserver(this);
76 if (infobar_service_->infobar_count() > 0) {
77 infobar_service_->RemoveInfoBar(infobar_service_->infobar_at(0));
81 void Wait() {
82 if (!alerted_) {
83 has_run_message_loop_ = true;
84 content::RunMessageLoop();
86 PersonalDataManagerFactory::GetForProfile(browser_->profile())->
87 RemoveObserver(this);
90 // PersonalDataManagerObserver:
91 void OnPersonalDataChanged() override {
92 if (has_run_message_loop_) {
93 base::MessageLoopForUI::current()->Quit();
94 has_run_message_loop_ = false;
96 alerted_ = true;
99 void OnInsufficientFormData() override { OnPersonalDataChanged(); }
101 // infobars::InfoBarManager::Observer:
102 void OnInfoBarAdded(infobars::InfoBar* infobar) override {
103 ConfirmInfoBarDelegate* infobar_delegate =
104 infobar_service_->infobar_at(0)->delegate()->AsConfirmInfoBarDelegate();
105 ASSERT_TRUE(infobar_delegate);
106 infobar_delegate->Accept();
109 private:
110 bool alerted_;
111 bool has_run_message_loop_;
112 Browser* browser_;
113 InfoBarService* infobar_service_;
116 class AutofillTest : public InProcessBrowserTest {
117 protected:
118 AutofillTest() {}
120 void SetUpOnMainThread() override {
121 // Don't want Keychain coming up on Mac.
122 test::DisableSystemServices(browser()->profile()->GetPrefs());
125 void TearDownOnMainThread() override {
126 // Make sure to close any showing popups prior to tearing down the UI.
127 content::WebContents* web_contents =
128 browser()->tab_strip_model()->GetActiveWebContents();
129 AutofillManager* autofill_manager =
130 ContentAutofillDriverFactory::FromWebContents(web_contents)
131 ->DriverForFrame(web_contents->GetMainFrame())
132 ->autofill_manager();
133 autofill_manager->client()->HideAutofillPopup();
136 PersonalDataManager* personal_data_manager() {
137 return PersonalDataManagerFactory::GetForProfile(browser()->profile());
140 void SetProfiles(std::vector<AutofillProfile>* profiles) {
141 WindowedPersonalDataManagerObserver observer(browser());
142 personal_data_manager()->SetProfiles(profiles);
143 observer.Wait();
146 void SetProfile(const AutofillProfile& profile) {
147 std::vector<AutofillProfile> profiles;
148 profiles.push_back(profile);
149 SetProfiles(&profiles);
152 void SetCards(std::vector<CreditCard>* cards) {
153 WindowedPersonalDataManagerObserver observer(browser());
154 personal_data_manager()->SetCreditCards(cards);
155 observer.Wait();
158 void SetCard(const CreditCard& card) {
159 std::vector<CreditCard> cards;
160 cards.push_back(card);
161 SetCards(&cards);
164 typedef std::map<std::string, std::string> FormMap;
165 // Navigate to the form, input values into the fields, and submit the form.
166 // The function returns after the PersonalDataManager is updated.
167 void FillFormAndSubmit(const std::string& filename, const FormMap& data) {
168 GURL url = test_server()->GetURL("files/autofill/" + filename);
169 chrome::NavigateParams params(browser(), url,
170 ui::PAGE_TRANSITION_LINK);
171 params.disposition = NEW_FOREGROUND_TAB;
172 ui_test_utils::NavigateToURL(&params);
174 std::string js;
175 for (FormMap::const_iterator i = data.begin(); i != data.end(); ++i) {
176 js += "document.getElementById('" + i->first + "').value = '" +
177 i->second + "';";
179 js += "document.onclick = function() {"
180 " document.getElementById('testform').submit();"
181 "};";
183 WindowedPersonalDataManagerObserver observer(browser());
184 ASSERT_TRUE(content::ExecuteScript(render_view_host(), js));
185 // Simulate a mouse click to submit the form because form submissions not
186 // triggered by user gestures are ignored.
187 content::SimulateMouseClick(
188 browser()->tab_strip_model()->GetActiveWebContents(), 0,
189 blink::WebMouseEvent::ButtonLeft);
190 observer.Wait();
193 void SubmitCreditCard(const char* name,
194 const char* number,
195 const char* exp_month,
196 const char* exp_year) {
197 FormMap data;
198 data["CREDIT_CARD_NAME"] = name;
199 data["CREDIT_CARD_NUMBER"] = number;
200 data["CREDIT_CARD_EXP_MONTH"] = exp_month;
201 data["CREDIT_CARD_EXP_4_DIGIT_YEAR"] = exp_year;
202 FillFormAndSubmit("autofill_creditcard_form.html", data);
205 // Aggregate profiles from forms into Autofill preferences. Returns the number
206 // of parsed profiles.
207 int AggregateProfilesIntoAutofillPrefs(const std::string& filename) {
208 CHECK(test_server()->Start());
210 std::string data;
211 base::FilePath data_file =
212 ui_test_utils::GetTestFilePath(base::FilePath().AppendASCII("autofill"),
213 base::FilePath().AppendASCII(filename));
214 CHECK(base::ReadFileToString(data_file, &data));
215 std::vector<std::string> lines;
216 base::SplitString(data, '\n', &lines);
217 int parsed_profiles = 0;
218 for (size_t i = 0; i < lines.size(); ++i) {
219 if (StartsWithASCII(lines[i], "#", false))
220 continue;
222 std::vector<std::string> fields;
223 base::SplitString(lines[i], '|', &fields);
224 if (fields.empty())
225 continue; // Blank line.
227 ++parsed_profiles;
228 CHECK_EQ(12u, fields.size());
230 FormMap data;
231 data["NAME_FIRST"] = fields[0];
232 data["NAME_MIDDLE"] = fields[1];
233 data["NAME_LAST"] = fields[2];
234 data["EMAIL_ADDRESS"] = fields[3];
235 data["COMPANY_NAME"] = fields[4];
236 data["ADDRESS_HOME_LINE1"] = fields[5];
237 data["ADDRESS_HOME_LINE2"] = fields[6];
238 data["ADDRESS_HOME_CITY"] = fields[7];
239 data["ADDRESS_HOME_STATE"] = fields[8];
240 data["ADDRESS_HOME_ZIP"] = fields[9];
241 data["ADDRESS_HOME_COUNTRY"] = fields[10];
242 data["PHONE_HOME_WHOLE_NUMBER"] = fields[11];
244 FillFormAndSubmit("duplicate_profiles_test.html", data);
246 return parsed_profiles;
249 void ExpectFieldValue(const std::string& field_name,
250 const std::string& expected_value) {
251 std::string value;
252 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
253 browser()->tab_strip_model()->GetActiveWebContents(),
254 "window.domAutomationController.send("
255 " document.getElementById('" + field_name + "').value);",
256 &value));
257 EXPECT_EQ(expected_value, value);
260 content::RenderViewHost* render_view_host() {
261 return browser()->tab_strip_model()->GetActiveWebContents()->
262 GetRenderViewHost();
265 void ExpectFilledTestForm() {
266 ExpectFieldValue("firstname", "Milton");
267 ExpectFieldValue("lastname", "Waddams");
268 ExpectFieldValue("address1", "4120 Freidrich Lane");
269 ExpectFieldValue("address2", "Basement");
270 ExpectFieldValue("city", "Austin");
271 ExpectFieldValue("state", "TX");
272 ExpectFieldValue("zip", "78744");
273 ExpectFieldValue("country", "US");
274 ExpectFieldValue("phone", "5125551234");
277 private:
278 net::TestURLFetcherFactory url_fetcher_factory_;
281 // Test filling profiles with unicode strings and crazy characters.
282 // TODO(isherman): rewrite as unit test under PersonalDataManagerTest.
283 IN_PROC_BROWSER_TEST_F(AutofillTest, FillProfileCrazyCharacters) {
284 std::vector<AutofillProfile> profiles;
285 AutofillProfile profile1;
286 profile1.SetRawInfo(NAME_FIRST,
287 WideToUTF16(L"\u0623\u0648\u0628\u0627\u0645\u0627 "
288 L"\u064a\u0639\u062a\u0630\u0631 "
289 L"\u0647\u0627\u062a\u0641\u064a\u0627 "
290 L"\u0644\u0645\u0648\u0638\u0641\u0629 "
291 L"\u0633\u0648\u062f\u0627\u0621 "
292 L"\u0627\u0633\u062a\u0642\u0627\u0644\u062a "
293 L"\u0628\u0633\u0628\u0628 "
294 L"\u062a\u0635\u0631\u064a\u062d\u0627\u062a "
295 L"\u0645\u062c\u062a\u0632\u0623\u0629"));
296 profile1.SetRawInfo(NAME_MIDDLE, WideToUTF16(L"BANK\xcBERF\xc4LLE"));
297 profile1.SetRawInfo(EMAIL_ADDRESS,
298 WideToUTF16(L"\uacbd\uc81c \ub274\uc2a4 "
299 L"\ub354\ubcf4\uae30@google.com"));
300 profile1.SetRawInfo(ADDRESS_HOME_LINE1,
301 WideToUTF16(L"\uad6d\uc815\uc6d0\xb7\uac80\ucc30, "
302 L"\ub178\ubb34\ud604\uc815\ubd80 "
303 L"\ub300\ubd81\uc811\ucd09 \ub2f4\ub2f9 "
304 L"\uc778\uc0ac\ub4e4 \uc870\uc0ac"));
305 profile1.SetRawInfo(ADDRESS_HOME_CITY,
306 WideToUTF16(L"\u653f\u5e9c\u4e0d\u6392\u9664\u7acb\u6cd5"
307 L"\u898f\u7ba1\u5c0e\u904a"));
308 profile1.SetRawInfo(ADDRESS_HOME_ZIP, WideToUTF16(L"YOHO_54676"));
309 profile1.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, WideToUTF16(L"861088828000"));
310 profile1.SetInfo(
311 AutofillType(ADDRESS_HOME_COUNTRY), WideToUTF16(L"India"), "en-US");
312 profiles.push_back(profile1);
314 AutofillProfile profile2;
315 profile2.SetRawInfo(NAME_FIRST,
316 WideToUTF16(L"\u4e0a\u6d77\u5e02\u91d1\u5c71\u533a "
317 L"\u677e\u9690\u9547\u4ead\u67ab\u516c"
318 L"\u8def1915\u53f7"));
319 profile2.SetRawInfo(NAME_LAST, WideToUTF16(L"aguantó"));
320 profile2.SetRawInfo(ADDRESS_HOME_ZIP, WideToUTF16(L"HOME 94043"));
321 profiles.push_back(profile2);
323 AutofillProfile profile3;
324 profile3.SetRawInfo(EMAIL_ADDRESS, WideToUTF16(L"sue@example.com"));
325 profile3.SetRawInfo(COMPANY_NAME, WideToUTF16(L"Company X"));
326 profiles.push_back(profile3);
328 AutofillProfile profile4;
329 profile4.SetRawInfo(NAME_FIRST, WideToUTF16(L"Joe 3254"));
330 profile4.SetRawInfo(NAME_LAST, WideToUTF16(L"\u8bb0\u8d262\u5e74\u591a"));
331 profile4.SetRawInfo(ADDRESS_HOME_ZIP,
332 WideToUTF16(L"\uff08\u90ae\u7f16\uff1a201504\uff09"));
333 profile4.SetRawInfo(EMAIL_ADDRESS, WideToUTF16(L"télévision@example.com"));
334 profile4.SetRawInfo(COMPANY_NAME,
335 WideToUTF16(L"\u0907\u0932\u0947\u0915\u093f\u091f\u094d"
336 L"\u0930\u0928\u093f\u0915\u094d\u0938, "
337 L"\u0905\u092a\u094b\u0932\u094b "
338 L"\u091f\u093e\u092f\u0930\u094d\u0938 "
339 L"\u0906\u0926\u093f"));
340 profiles.push_back(profile4);
342 AutofillProfile profile5;
343 profile5.SetRawInfo(NAME_FIRST, WideToUTF16(L"Larry"));
344 profile5.SetRawInfo(NAME_LAST,
345 WideToUTF16(L"\u0938\u094d\u091f\u093e\u0902\u092a "
346 L"\u0921\u094d\u092f\u0942\u091f\u0940"));
347 profile5.SetRawInfo(ADDRESS_HOME_ZIP,
348 WideToUTF16(L"111111111111110000GOOGLE"));
349 profile5.SetRawInfo(EMAIL_ADDRESS, WideToUTF16(L"page@000000.com"));
350 profile5.SetRawInfo(COMPANY_NAME, WideToUTF16(L"Google"));
351 profiles.push_back(profile5);
353 AutofillProfile profile6;
354 profile6.SetRawInfo(NAME_FIRST,
355 WideToUTF16(L"\u4e0a\u6d77\u5e02\u91d1\u5c71\u533a "
356 L"\u677e\u9690\u9547\u4ead\u67ab\u516c"
357 L"\u8def1915\u53f7"));
358 profile6.SetRawInfo(NAME_LAST,
359 WideToUTF16(L"\u0646\u062c\u0627\u0645\u064a\u0646\u0627 "
360 L"\u062f\u0639\u0645\u0647\u0627 "
361 L"\u0644\u0644\u0631\u0626\u064a\u0633 "
362 L"\u0627\u0644\u0633\u0648\u062f\u0627\u0646"
363 L"\u064a \u0639\u0645\u0631 "
364 L"\u0627\u0644\u0628\u0634\u064a\u0631"));
365 profile6.SetRawInfo(ADDRESS_HOME_ZIP, WideToUTF16(L"HOME 94043"));
366 profiles.push_back(profile6);
368 AutofillProfile profile7;
369 profile7.SetRawInfo(NAME_FIRST, WideToUTF16(L"&$%$$$ TESTO *&*&^&^& MOKO"));
370 profile7.SetRawInfo(NAME_MIDDLE, WideToUTF16(L"WOHOOOO$$$$$$$$****"));
371 profile7.SetRawInfo(EMAIL_ADDRESS, WideToUTF16(L"yuvu@example.com"));
372 profile7.SetRawInfo(ADDRESS_HOME_LINE1,
373 WideToUTF16(L"34544, anderson ST.(120230)"));
374 profile7.SetRawInfo(ADDRESS_HOME_CITY, WideToUTF16(L"Sunnyvale"));
375 profile7.SetRawInfo(ADDRESS_HOME_STATE, WideToUTF16(L"CA"));
376 profile7.SetRawInfo(ADDRESS_HOME_ZIP, WideToUTF16(L"94086"));
377 profile7.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, WideToUTF16(L"15466784565"));
378 profile7.SetInfo(
379 AutofillType(ADDRESS_HOME_COUNTRY), WideToUTF16(L"United States"),
380 "en-US");
381 profiles.push_back(profile7);
383 SetProfiles(&profiles);
384 ASSERT_EQ(profiles.size(), personal_data_manager()->GetProfiles().size());
385 for (size_t i = 0; i < profiles.size(); ++i) {
386 EXPECT_TRUE(std::find(profiles.begin(),
387 profiles.end(),
388 *personal_data_manager()->GetProfiles()[i]) !=
389 profiles.end());
392 std::vector<CreditCard> cards;
393 CreditCard card1;
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);
402 CreditCard card2;
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);
409 CreditCard card3;
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);
425 CreditCard card4;
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);
438 SetCards(&cards);
439 ASSERT_EQ(cards.size(), personal_data_manager()->GetCreditCards().size());
440 for (size_t i = 0; i < cards.size(); ++i) {
441 EXPECT_TRUE(std::find(cards.begin(),
442 cards.end(),
443 *personal_data_manager()->GetCreditCards()[i]) !=
444 cards.end());
448 // Test filling in invalid values for profiles are saved as-is. Phone
449 // information entered into the prefs UI is not validated or rejected except for
450 // duplicates.
451 // TODO(isherman): rewrite as WebUI test?
452 IN_PROC_BROWSER_TEST_F(AutofillTest, Invalid) {
453 // First try profiles with invalid ZIP input.
454 AutofillProfile without_invalid;
455 without_invalid.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Will"));
456 without_invalid.SetRawInfo(ADDRESS_HOME_CITY, ASCIIToUTF16("Sunnyvale"));
457 without_invalid.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("CA"));
458 without_invalid.SetRawInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("my_zip"));
459 without_invalid.SetInfo(
460 AutofillType(ADDRESS_HOME_COUNTRY), ASCIIToUTF16("United States"),
461 "en-US");
463 AutofillProfile with_invalid = without_invalid;
464 with_invalid.SetRawInfo(PHONE_HOME_WHOLE_NUMBER,
465 ASCIIToUTF16("Invalid_Phone_Number"));
466 SetProfile(with_invalid);
468 ASSERT_EQ(1u, personal_data_manager()->GetProfiles().size());
469 AutofillProfile profile = *personal_data_manager()->GetProfiles()[0];
470 ASSERT_NE(without_invalid.GetRawInfo(PHONE_HOME_WHOLE_NUMBER),
471 profile.GetRawInfo(PHONE_HOME_WHOLE_NUMBER));
474 // Test invalid credit card numbers typed in prefs should be saved as-is.
475 // TODO(isherman): rewrite as WebUI test?
476 IN_PROC_BROWSER_TEST_F(AutofillTest, PrefsStringSavedAsIs) {
477 CreditCard card;
478 card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("Not_0123-5Checked"));
479 SetCard(card);
481 ASSERT_EQ(1u, personal_data_manager()->GetCreditCards().size());
482 ASSERT_EQ(card, *personal_data_manager()->GetCreditCards()[0]);
485 // Test credit card info with an invalid number is not aggregated.
486 // When filling out a form with an invalid credit card number (one that does not
487 // pass the Luhn test) the credit card info should not be saved into Autofill
488 // preferences.
489 IN_PROC_BROWSER_TEST_F(AutofillTest, InvalidCreditCardNumberIsNotAggregated) {
490 #if defined(OS_WIN) && defined(USE_ASH)
491 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
492 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
493 switches::kAshBrowserTests))
494 return;
495 #endif
497 ASSERT_TRUE(test_server()->Start());
498 std::string card("4408 0412 3456 7890");
499 ASSERT_FALSE(autofill::IsValidCreditCardNumber(ASCIIToUTF16(card)));
500 SubmitCreditCard("Bob Smith", card.c_str(), "12", "2014");
501 InfoBarService* infobar_service = InfoBarService::FromWebContents(
502 browser()->tab_strip_model()->GetActiveWebContents());
503 ASSERT_EQ(0u, infobar_service->infobar_count());
506 // Test whitespaces and separator chars are stripped for valid CC numbers.
507 // The credit card numbers used in this test pass the Luhn test. For reference:
508 // http://www.merriampark.com/anatomycc.htm
509 IN_PROC_BROWSER_TEST_F(AutofillTest,
510 WhitespacesAndSeparatorCharsStrippedForValidCCNums) {
511 #if defined(OS_WIN) && defined(USE_ASH)
512 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
513 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
514 switches::kAshBrowserTests))
515 return;
516 #endif
518 ASSERT_TRUE(test_server()->Start());
519 SubmitCreditCard("Bob Smith", "4408 0412 3456 7893", "12", "2014");
520 SubmitCreditCard("Jane Doe", "4417-1234-5678-9113", "10", "2013");
522 ASSERT_EQ(2u, personal_data_manager()->GetCreditCards().size());
523 base::string16 cc1 = personal_data_manager()->GetCreditCards()[0]->GetRawInfo(
524 CREDIT_CARD_NUMBER);
525 ASSERT_TRUE(autofill::IsValidCreditCardNumber(cc1));
526 base::string16 cc2 = personal_data_manager()->GetCreditCards()[1]->GetRawInfo(
527 CREDIT_CARD_NUMBER);
528 ASSERT_TRUE(autofill::IsValidCreditCardNumber(cc2));
531 // Test that Autofill aggregates a minimum valid profile.
532 // The minimum required address fields must be specified: First Name, Last Name,
533 // Address Line 1, City, Zip Code, and State.
534 IN_PROC_BROWSER_TEST_F(AutofillTest, AggregatesMinValidProfile) {
535 ASSERT_TRUE(test_server()->Start());
536 FormMap data;
537 data["NAME_FIRST"] = "Bob";
538 data["NAME_LAST"] = "Smith";
539 data["ADDRESS_HOME_LINE1"] = "1234 H St.";
540 data["ADDRESS_HOME_CITY"] = "Mountain View";
541 data["ADDRESS_HOME_STATE"] = "CA";
542 data["ADDRESS_HOME_ZIP"] = "94043";
543 FillFormAndSubmit("duplicate_profiles_test.html", data);
545 ASSERT_EQ(1u, personal_data_manager()->GetProfiles().size());
548 // Test Autofill does not aggregate profiles with no address info.
549 // The minimum required address fields must be specified: First Name, Last Name,
550 // Address Line 1, City, Zip Code, and State.
551 IN_PROC_BROWSER_TEST_F(AutofillTest, ProfilesNotAggregatedWithNoAddress) {
552 ASSERT_TRUE(test_server()->Start());
553 FormMap data;
554 data["NAME_FIRST"] = "Bob";
555 data["NAME_LAST"] = "Smith";
556 data["EMAIL_ADDRESS"] = "bsmith@example.com";
557 data["COMPANY_NAME"] = "Mountain View";
558 data["ADDRESS_HOME_CITY"] = "Mountain View";
559 data["PHONE_HOME_WHOLE_NUMBER"] = "650-555-4567";
560 FillFormAndSubmit("duplicate_profiles_test.html", data);
562 ASSERT_TRUE(personal_data_manager()->GetProfiles().empty());
565 // Test Autofill does not aggregate profiles with an invalid email.
566 IN_PROC_BROWSER_TEST_F(AutofillTest, ProfilesNotAggregatedWithInvalidEmail) {
567 ASSERT_TRUE(test_server()->Start());
568 FormMap data;
569 data["NAME_FIRST"] = "Bob";
570 data["NAME_LAST"] = "Smith";
571 data["EMAIL_ADDRESS"] = "garbage";
572 data["ADDRESS_HOME_LINE1"] = "1234 H St.";
573 data["ADDRESS_HOME_CITY"] = "San Jose";
574 data["ADDRESS_HOME_STATE"] = "CA";
575 data["ADDRESS_HOME_ZIP"] = "95110";
576 data["COMPANY_NAME"] = "Company X";
577 data["PHONE_HOME_WHOLE_NUMBER"] = "408-871-4567";
578 FillFormAndSubmit("duplicate_profiles_test.html", data);
580 ASSERT_TRUE(personal_data_manager()->GetProfiles().empty());
583 // Test profile is saved if phone number is valid in selected country.
584 // The data file contains two profiles with valid phone numbers and two
585 // profiles with invalid phone numbers from their respective country.
586 IN_PROC_BROWSER_TEST_F(AutofillTest, ProfileSavedWithValidCountryPhone) {
587 ASSERT_TRUE(test_server()->Start());
588 std::vector<FormMap> profiles;
590 FormMap data1;
591 data1["NAME_FIRST"] = "Bob";
592 data1["NAME_LAST"] = "Smith";
593 data1["ADDRESS_HOME_LINE1"] = "123 Cherry Ave";
594 data1["ADDRESS_HOME_CITY"] = "Mountain View";
595 data1["ADDRESS_HOME_STATE"] = "CA";
596 data1["ADDRESS_HOME_ZIP"] = "94043";
597 data1["ADDRESS_HOME_COUNTRY"] = "United States";
598 data1["PHONE_HOME_WHOLE_NUMBER"] = "408-871-4567";
599 profiles.push_back(data1);
601 FormMap data2;
602 data2["NAME_FIRST"] = "John";
603 data2["NAME_LAST"] = "Doe";
604 data2["ADDRESS_HOME_LINE1"] = "987 H St";
605 data2["ADDRESS_HOME_CITY"] = "San Jose";
606 data2["ADDRESS_HOME_STATE"] = "CA";
607 data2["ADDRESS_HOME_ZIP"] = "95510";
608 data2["ADDRESS_HOME_COUNTRY"] = "United States";
609 data2["PHONE_HOME_WHOLE_NUMBER"] = "408-123-456";
610 profiles.push_back(data2);
612 FormMap data3;
613 data3["NAME_FIRST"] = "Jane";
614 data3["NAME_LAST"] = "Doe";
615 data3["ADDRESS_HOME_LINE1"] = "1523 Garcia St";
616 data3["ADDRESS_HOME_CITY"] = "Mountain View";
617 data3["ADDRESS_HOME_STATE"] = "CA";
618 data3["ADDRESS_HOME_ZIP"] = "94043";
619 data3["ADDRESS_HOME_COUNTRY"] = "Germany";
620 data3["PHONE_HOME_WHOLE_NUMBER"] = "+49 40-80-81-79-000";
621 profiles.push_back(data3);
623 FormMap data4;
624 data4["NAME_FIRST"] = "Bonnie";
625 data4["NAME_LAST"] = "Smith";
626 data4["ADDRESS_HOME_LINE1"] = "6723 Roadway Rd";
627 data4["ADDRESS_HOME_CITY"] = "San Jose";
628 data4["ADDRESS_HOME_STATE"] = "CA";
629 data4["ADDRESS_HOME_ZIP"] = "95510";
630 data4["ADDRESS_HOME_COUNTRY"] = "Germany";
631 data4["PHONE_HOME_WHOLE_NUMBER"] = "+21 08450 777 777";
632 profiles.push_back(data4);
634 for (size_t i = 0; i < profiles.size(); ++i)
635 FillFormAndSubmit("autofill_test_form.html", profiles[i]);
637 ASSERT_EQ(2u, personal_data_manager()->GetProfiles().size());
638 int us_address_index =
639 personal_data_manager()->GetProfiles()[0]->GetRawInfo(
640 ADDRESS_HOME_LINE1) == ASCIIToUTF16("123 Cherry Ave")
642 : 1;
644 EXPECT_EQ(
645 ASCIIToUTF16("408-871-4567"),
646 personal_data_manager()->GetProfiles()[us_address_index]->GetRawInfo(
647 PHONE_HOME_WHOLE_NUMBER));
648 ASSERT_EQ(
649 ASCIIToUTF16("+49 40-80-81-79-000"),
650 personal_data_manager()->GetProfiles()[1 - us_address_index]->GetRawInfo(
651 PHONE_HOME_WHOLE_NUMBER));
654 // Prepend country codes when formatting phone numbers, but only if the user
655 // provided one in the first place.
656 IN_PROC_BROWSER_TEST_F(AutofillTest, AppendCountryCodeForAggregatedPhones) {
657 ASSERT_TRUE(test_server()->Start());
658 FormMap data;
659 data["NAME_FIRST"] = "Bob";
660 data["NAME_LAST"] = "Smith";
661 data["ADDRESS_HOME_LINE1"] = "1234 H St.";
662 data["ADDRESS_HOME_CITY"] = "San Jose";
663 data["ADDRESS_HOME_STATE"] = "CA";
664 data["ADDRESS_HOME_ZIP"] = "95110";
665 data["ADDRESS_HOME_COUNTRY"] = "Germany";
666 data["PHONE_HOME_WHOLE_NUMBER"] = "+4908450777777";
667 FillFormAndSubmit("autofill_test_form.html", data);
669 data["ADDRESS_HOME_LINE1"] = "4321 H St.";
670 data["PHONE_HOME_WHOLE_NUMBER"] = "08450777777";
671 FillFormAndSubmit("autofill_test_form.html", data);
673 ASSERT_EQ(2u, personal_data_manager()->GetProfiles().size());
674 int second_address_index =
675 personal_data_manager()->GetProfiles()[0]->GetRawInfo(
676 ADDRESS_HOME_LINE1) == ASCIIToUTF16("4321 H St.")
678 : 1;
680 EXPECT_EQ(ASCIIToUTF16("+49 8450 777777"),
681 personal_data_manager()
682 ->GetProfiles()[1 - second_address_index]
683 ->GetRawInfo(PHONE_HOME_WHOLE_NUMBER));
685 EXPECT_EQ(
686 ASCIIToUTF16("08450 777777"),
687 personal_data_manager()->GetProfiles()[second_address_index]->GetRawInfo(
688 PHONE_HOME_WHOLE_NUMBER));
691 // Test that Autofill uses '+' sign for international numbers.
692 // This applies to the following cases:
693 // The phone number has a leading '+'.
694 // The phone number does not have a leading '+'.
695 // The phone number has a leading international direct dialing (IDD) code.
696 // This does not apply to US numbers. For US numbers, '+' is removed.
697 IN_PROC_BROWSER_TEST_F(AutofillTest, UsePlusSignForInternationalNumber) {
698 ASSERT_TRUE(test_server()->Start());
699 std::vector<FormMap> profiles;
701 FormMap data1;
702 data1["NAME_FIRST"] = "Bonnie";
703 data1["NAME_LAST"] = "Smith";
704 data1["ADDRESS_HOME_LINE1"] = "6723 Roadway Rd";
705 data1["ADDRESS_HOME_CITY"] = "Reading";
706 data1["ADDRESS_HOME_STATE"] = "Berkshire";
707 data1["ADDRESS_HOME_ZIP"] = "RG12 3BR";
708 data1["ADDRESS_HOME_COUNTRY"] = "United Kingdom";
709 data1["PHONE_HOME_WHOLE_NUMBER"] = "+44 7624-123456";
710 profiles.push_back(data1);
712 FormMap data2;
713 data2["NAME_FIRST"] = "John";
714 data2["NAME_LAST"] = "Doe";
715 data2["ADDRESS_HOME_LINE1"] = "987 H St";
716 data2["ADDRESS_HOME_CITY"] = "Reading";
717 data2["ADDRESS_HOME_STATE"] = "BerkShire";
718 data2["ADDRESS_HOME_ZIP"] = "RG12 3BR";
719 data2["ADDRESS_HOME_COUNTRY"] = "United Kingdom";
720 data2["PHONE_HOME_WHOLE_NUMBER"] = "44 7624 123456";
721 profiles.push_back(data2);
723 FormMap data3;
724 data3["NAME_FIRST"] = "Jane";
725 data3["NAME_LAST"] = "Doe";
726 data3["ADDRESS_HOME_LINE1"] = "1523 Garcia St";
727 data3["ADDRESS_HOME_CITY"] = "Reading";
728 data3["ADDRESS_HOME_STATE"] = "BerkShire";
729 data3["ADDRESS_HOME_ZIP"] = "RG12 3BR";
730 data3["ADDRESS_HOME_COUNTRY"] = "United Kingdom";
731 data3["PHONE_HOME_WHOLE_NUMBER"] = "0044 7624 123456";
732 profiles.push_back(data3);
734 FormMap data4;
735 data4["NAME_FIRST"] = "Bob";
736 data4["NAME_LAST"] = "Smith";
737 data4["ADDRESS_HOME_LINE1"] = "123 Cherry Ave";
738 data4["ADDRESS_HOME_CITY"] = "Mountain View";
739 data4["ADDRESS_HOME_STATE"] = "CA";
740 data4["ADDRESS_HOME_ZIP"] = "94043";
741 data4["ADDRESS_HOME_COUNTRY"] = "United States";
742 data4["PHONE_HOME_WHOLE_NUMBER"] = "+1 (408) 871-4567";
743 profiles.push_back(data4);
745 for (size_t i = 0; i < profiles.size(); ++i)
746 FillFormAndSubmit("autofill_test_form.html", profiles[i]);
748 ASSERT_EQ(4u, personal_data_manager()->GetProfiles().size());
750 for (size_t i = 0; i < personal_data_manager()->GetProfiles().size(); ++i) {
751 AutofillProfile* profile = personal_data_manager()->GetProfiles()[i];
752 std::string expectation;
753 std::string name = UTF16ToASCII(profile->GetRawInfo(NAME_FIRST));
755 if (name == "Bonnie")
756 expectation = "+447624123456";
757 else if (name == "John")
758 expectation = "+447624123456";
759 else if (name == "Jane")
760 expectation = "+447624123456";
761 else if (name == "Bob")
762 expectation = "14088714567";
764 EXPECT_EQ(ASCIIToUTF16(expectation),
765 profile->GetInfo(AutofillType(PHONE_HOME_WHOLE_NUMBER), ""));
769 // Test CC info not offered to be saved when autocomplete=off for CC field.
770 // If the credit card number field has autocomplete turned off, then the credit
771 // card infobar should not offer to save the credit card info. The credit card
772 // number must be a valid Luhn number.
773 IN_PROC_BROWSER_TEST_F(AutofillTest, CCInfoNotStoredWhenAutocompleteOff) {
774 #if defined(OS_WIN) && defined(USE_ASH)
775 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
776 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
777 switches::kAshBrowserTests))
778 return;
779 #endif
781 ASSERT_TRUE(test_server()->Start());
782 FormMap data;
783 data["CREDIT_CARD_NAME"] = "Bob Smith";
784 data["CREDIT_CARD_NUMBER"] = "4408041234567893";
785 data["CREDIT_CARD_EXP_MONTH"] = "12";
786 data["CREDIT_CARD_EXP_4_DIGIT_YEAR"] = "2014";
787 FillFormAndSubmit("cc_autocomplete_off_test.html", data);
789 InfoBarService* infobar_service = InfoBarService::FromWebContents(
790 browser()->tab_strip_model()->GetActiveWebContents());
791 ASSERT_EQ(0u, infobar_service->infobar_count());
794 // Test profile not aggregated if email found in non-email field.
795 IN_PROC_BROWSER_TEST_F(AutofillTest, ProfileWithEmailInOtherFieldNotSaved) {
796 ASSERT_TRUE(test_server()->Start());
798 FormMap data;
799 data["NAME_FIRST"] = "Bob";
800 data["NAME_LAST"] = "Smith";
801 data["ADDRESS_HOME_LINE1"] = "bsmith@gmail.com";
802 data["ADDRESS_HOME_CITY"] = "San Jose";
803 data["ADDRESS_HOME_STATE"] = "CA";
804 data["ADDRESS_HOME_ZIP"] = "95110";
805 data["COMPANY_NAME"] = "Company X";
806 data["PHONE_HOME_WHOLE_NUMBER"] = "408-871-4567";
807 FillFormAndSubmit("duplicate_profiles_test.html", data);
809 ASSERT_EQ(0u, personal_data_manager()->GetProfiles().size());
812 // Test that profiles merge for aggregated data with same address.
813 // The criterion for when two profiles are expected to be merged is when their
814 // 'Address Line 1' and 'City' data match. When two profiles are merged, any
815 // remaining address fields are expected to be overwritten. Any non-address
816 // fields should accumulate multi-valued data.
817 // DISABLED: http://crbug.com/281541
818 IN_PROC_BROWSER_TEST_F(AutofillTest,
819 DISABLED_MergeAggregatedProfilesWithSameAddress) {
820 AggregateProfilesIntoAutofillPrefs("dataset_same_address.txt");
822 ASSERT_EQ(3u, personal_data_manager()->GetProfiles().size());
825 // Test profiles are not merged without minimum address values.
826 // Mininum address values needed during aggregation are: address line 1, city,
827 // state, and zip code.
828 // Profiles are merged when data for address line 1 and city match.
829 // DISABLED: http://crbug.com/281541
830 IN_PROC_BROWSER_TEST_F(AutofillTest,
831 DISABLED_ProfilesNotMergedWhenNoMinAddressData) {
832 AggregateProfilesIntoAutofillPrefs("dataset_no_address.txt");
834 ASSERT_EQ(0u, personal_data_manager()->GetProfiles().size());
837 // Test Autofill ability to merge duplicate profiles and throw away junk.
838 // TODO(isherman): this looks redundant, consider removing.
839 // DISABLED: http://crbug.com/281541
840 IN_PROC_BROWSER_TEST_F(AutofillTest,
841 DISABLED_MergeAggregatedDuplicatedProfiles) {
842 int num_of_profiles =
843 AggregateProfilesIntoAutofillPrefs("dataset_duplicated_profiles.txt");
845 ASSERT_GT(num_of_profiles,
846 static_cast<int>(personal_data_manager()->GetProfiles().size()));
849 } // namespace autofill