Fire an error if a pref used in the UI is missing once all prefs are fetched.
[chromium-blink-merge.git] / chrome / browser / autofill / autofill_browsertest.cc
blobc280cb8e5bcb9902da1405474d2966b6b5514bc5
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/run_loop.h"
14 #include "base/strings/string16.h"
15 #include "base/strings/string_number_conversions.h"
16 #include "base/strings/string_split.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "base/time/time.h"
19 #include "chrome/browser/autofill/personal_data_manager_factory.h"
20 #include "chrome/browser/infobars/infobar_service.h"
21 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/browser/ui/browser.h"
23 #include "chrome/browser/ui/browser_window.h"
24 #include "chrome/browser/ui/tabs/tab_strip_model.h"
25 #include "chrome/common/render_messages.h"
26 #include "chrome/test/base/in_process_browser_test.h"
27 #include "chrome/test/base/test_switches.h"
28 #include "chrome/test/base/ui_test_utils.h"
29 #include "components/autofill/content/browser/content_autofill_driver.h"
30 #include "components/autofill/content/browser/content_autofill_driver_factory.h"
31 #include "components/autofill/core/browser/autofill_profile.h"
32 #include "components/autofill/core/browser/autofill_test_utils.h"
33 #include "components/autofill/core/browser/credit_card.h"
34 #include "components/autofill/core/browser/personal_data_manager.h"
35 #include "components/autofill/core/browser/personal_data_manager_observer.h"
36 #include "components/autofill/core/browser/validation.h"
37 #include "components/infobars/core/confirm_infobar_delegate.h"
38 #include "components/infobars/core/infobar.h"
39 #include "components/infobars/core/infobar_manager.h"
40 #include "content/public/browser/navigation_controller.h"
41 #include "content/public/browser/render_view_host.h"
42 #include "content/public/browser/web_contents.h"
43 #include "content/public/test/browser_test_utils.h"
44 #include "content/public/test/test_renderer_host.h"
45 #include "content/public/test/test_utils.h"
46 #include "net/url_request/test_url_fetcher_factory.h"
47 #include "testing/gmock/include/gmock/gmock.h"
48 #include "testing/gtest/include/gtest/gtest.h"
49 #include "ui/events/keycodes/keyboard_codes.h"
51 using base::ASCIIToUTF16;
52 using base::UTF16ToASCII;
53 using base::WideToUTF16;
55 namespace autofill {
57 // Default JavaScript code used to submit the forms.
58 const char kDocumentClickHandlerSubmitJS[] =
59 "document.onclick = function() {"
60 " document.getElementById('testform').submit();"
61 "};";
63 // TODO(bondd): PdmChangeWaiter in autofill_uitest_util.cc is a replacement for
64 // this class. Remove this class and use helper functions in that file instead.
65 class WindowedPersonalDataManagerObserver
66 : public PersonalDataManagerObserver,
67 public infobars::InfoBarManager::Observer {
68 public:
69 explicit WindowedPersonalDataManagerObserver(Browser* browser)
70 : alerted_(false),
71 has_run_message_loop_(false),
72 browser_(browser),
73 infobar_service_(InfoBarService::FromWebContents(
74 browser_->tab_strip_model()->GetActiveWebContents())) {
75 PersonalDataManagerFactory::GetForProfile(browser_->profile())->
76 AddObserver(this);
77 infobar_service_->AddObserver(this);
80 ~WindowedPersonalDataManagerObserver() override {
81 infobar_service_->RemoveObserver(this);
83 if (infobar_service_->infobar_count() > 0) {
84 infobar_service_->RemoveInfoBar(infobar_service_->infobar_at(0));
88 void Wait() {
89 if (!alerted_) {
90 has_run_message_loop_ = true;
91 content::RunMessageLoop();
93 PersonalDataManagerFactory::GetForProfile(browser_->profile())->
94 RemoveObserver(this);
97 // PersonalDataManagerObserver:
98 void OnPersonalDataChanged() override {
99 if (has_run_message_loop_) {
100 base::MessageLoopForUI::current()->Quit();
101 has_run_message_loop_ = false;
103 alerted_ = true;
106 void OnInsufficientFormData() override { OnPersonalDataChanged(); }
108 // infobars::InfoBarManager::Observer:
109 void OnInfoBarAdded(infobars::InfoBar* infobar) override {
110 ConfirmInfoBarDelegate* infobar_delegate =
111 infobar_service_->infobar_at(0)->delegate()->AsConfirmInfoBarDelegate();
112 ASSERT_TRUE(infobar_delegate);
113 infobar_delegate->Accept();
116 private:
117 bool alerted_;
118 bool has_run_message_loop_;
119 Browser* browser_;
120 InfoBarService* infobar_service_;
123 class AutofillTest : public InProcessBrowserTest {
124 protected:
125 AutofillTest() {}
127 void SetUpOnMainThread() override {
128 // Don't want Keychain coming up on Mac.
129 test::DisableSystemServices(browser()->profile()->GetPrefs());
132 void TearDownOnMainThread() override {
133 // Make sure to close any showing popups prior to tearing down the UI.
134 content::WebContents* web_contents =
135 browser()->tab_strip_model()->GetActiveWebContents();
136 AutofillManager* autofill_manager =
137 ContentAutofillDriverFactory::FromWebContents(web_contents)
138 ->DriverForFrame(web_contents->GetMainFrame())
139 ->autofill_manager();
140 autofill_manager->client()->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);
150 observer.Wait();
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);
162 observer.Wait();
165 void SetCard(const CreditCard& card) {
166 std::vector<CreditCard> cards;
167 cards.push_back(card);
168 SetCards(&cards);
171 typedef std::map<std::string, std::string> FormMap;
173 // Helper function to obtain the Javascript required to update a form.
174 std::string GetJSToFillForm(const FormMap& data) {
175 std::string js;
176 for (const auto& entry : data) {
177 js += "document.getElementById('" + entry.first + "').value = '" +
178 entry.second + "';";
180 return js;
183 // Navigate to the form, input values into the fields, and submit the form.
184 // The function returns after the PersonalDataManager is updated.
185 void FillFormAndSubmit(const std::string& filename, const FormMap& data) {
186 FillFormAndSubmitWithHandler(filename, data, kDocumentClickHandlerSubmitJS,
187 true, true);
190 // Helper where the actual submit JS code can be specified, as well as whether
191 // the test should |simulate_click| on the document.
192 void FillFormAndSubmitWithHandler(const std::string& filename,
193 const FormMap& data,
194 const std::string& submit_js,
195 bool simulate_click,
196 bool expect_personal_data_change) {
197 GURL url = test_server()->GetURL("files/autofill/" + filename);
198 chrome::NavigateParams params(browser(), url,
199 ui::PAGE_TRANSITION_LINK);
200 params.disposition = NEW_FOREGROUND_TAB;
201 ui_test_utils::NavigateToURL(&params);
203 scoped_ptr<WindowedPersonalDataManagerObserver> observer;
204 if (expect_personal_data_change)
205 observer.reset(new WindowedPersonalDataManagerObserver(browser()));
207 std::string js = GetJSToFillForm(data) + submit_js;
208 ASSERT_TRUE(content::ExecuteScript(render_view_host(), js));
209 if (simulate_click) {
210 // Simulate a mouse click to submit the form because form submissions not
211 // triggered by user gestures are ignored.
212 content::SimulateMouseClick(
213 browser()->tab_strip_model()->GetActiveWebContents(), 0,
214 blink::WebMouseEvent::ButtonLeft);
216 // We may not always be expecting changes in Personal data.
217 if (observer.get())
218 observer->Wait();
219 else
220 base::RunLoop().RunUntilIdle();
223 void SubmitCreditCard(const char* name,
224 const char* number,
225 const char* exp_month,
226 const char* exp_year) {
227 FormMap data;
228 data["CREDIT_CARD_NAME"] = name;
229 data["CREDIT_CARD_NUMBER"] = number;
230 data["CREDIT_CARD_EXP_MONTH"] = exp_month;
231 data["CREDIT_CARD_EXP_4_DIGIT_YEAR"] = exp_year;
232 FillFormAndSubmit("autofill_creditcard_form.html", data);
235 // Aggregate profiles from forms into Autofill preferences. Returns the number
236 // of parsed profiles.
237 int AggregateProfilesIntoAutofillPrefs(const std::string& filename) {
238 CHECK(test_server()->Start());
240 std::string data;
241 base::FilePath data_file =
242 ui_test_utils::GetTestFilePath(base::FilePath().AppendASCII("autofill"),
243 base::FilePath().AppendASCII(filename));
244 CHECK(base::ReadFileToString(data_file, &data));
245 std::vector<std::string> lines;
246 base::SplitString(data, '\n', &lines);
247 int parsed_profiles = 0;
248 for (size_t i = 0; i < lines.size(); ++i) {
249 if (StartsWithASCII(lines[i], "#", false))
250 continue;
252 std::vector<std::string> fields;
253 base::SplitString(lines[i], '|', &fields);
254 if (fields.empty())
255 continue; // Blank line.
257 ++parsed_profiles;
258 CHECK_EQ(12u, fields.size());
260 FormMap data;
261 data["NAME_FIRST"] = fields[0];
262 data["NAME_MIDDLE"] = fields[1];
263 data["NAME_LAST"] = fields[2];
264 data["EMAIL_ADDRESS"] = fields[3];
265 data["COMPANY_NAME"] = fields[4];
266 data["ADDRESS_HOME_LINE1"] = fields[5];
267 data["ADDRESS_HOME_LINE2"] = fields[6];
268 data["ADDRESS_HOME_CITY"] = fields[7];
269 data["ADDRESS_HOME_STATE"] = fields[8];
270 data["ADDRESS_HOME_ZIP"] = fields[9];
271 data["ADDRESS_HOME_COUNTRY"] = fields[10];
272 data["PHONE_HOME_WHOLE_NUMBER"] = fields[11];
274 FillFormAndSubmit("duplicate_profiles_test.html", data);
276 return parsed_profiles;
279 void ExpectFieldValue(const std::string& field_name,
280 const std::string& expected_value) {
281 std::string value;
282 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
283 browser()->tab_strip_model()->GetActiveWebContents(),
284 "window.domAutomationController.send("
285 " document.getElementById('" + field_name + "').value);",
286 &value));
287 EXPECT_EQ(expected_value, value);
290 content::RenderViewHost* render_view_host() {
291 return browser()->tab_strip_model()->GetActiveWebContents()->
292 GetRenderViewHost();
295 void ExpectFilledTestForm() {
296 ExpectFieldValue("firstname", "Milton");
297 ExpectFieldValue("lastname", "Waddams");
298 ExpectFieldValue("address1", "4120 Freidrich Lane");
299 ExpectFieldValue("address2", "Basement");
300 ExpectFieldValue("city", "Austin");
301 ExpectFieldValue("state", "TX");
302 ExpectFieldValue("zip", "78744");
303 ExpectFieldValue("country", "US");
304 ExpectFieldValue("phone", "5125551234");
307 private:
308 net::TestURLFetcherFactory url_fetcher_factory_;
311 // Test filling profiles with unicode strings and crazy characters.
312 // TODO(isherman): rewrite as unit test under PersonalDataManagerTest.
313 IN_PROC_BROWSER_TEST_F(AutofillTest, FillProfileCrazyCharacters) {
314 std::vector<AutofillProfile> profiles;
315 AutofillProfile profile1;
316 profile1.SetRawInfo(NAME_FIRST,
317 WideToUTF16(L"\u0623\u0648\u0628\u0627\u0645\u0627 "
318 L"\u064a\u0639\u062a\u0630\u0631 "
319 L"\u0647\u0627\u062a\u0641\u064a\u0627 "
320 L"\u0644\u0645\u0648\u0638\u0641\u0629 "
321 L"\u0633\u0648\u062f\u0627\u0621 "
322 L"\u0627\u0633\u062a\u0642\u0627\u0644\u062a "
323 L"\u0628\u0633\u0628\u0628 "
324 L"\u062a\u0635\u0631\u064a\u062d\u0627\u062a "
325 L"\u0645\u062c\u062a\u0632\u0623\u0629"));
326 profile1.SetRawInfo(NAME_MIDDLE, WideToUTF16(L"BANK\xcBERF\xc4LLE"));
327 profile1.SetRawInfo(EMAIL_ADDRESS,
328 WideToUTF16(L"\uacbd\uc81c \ub274\uc2a4 "
329 L"\ub354\ubcf4\uae30@google.com"));
330 profile1.SetRawInfo(ADDRESS_HOME_LINE1,
331 WideToUTF16(L"\uad6d\uc815\uc6d0\xb7\uac80\ucc30, "
332 L"\ub178\ubb34\ud604\uc815\ubd80 "
333 L"\ub300\ubd81\uc811\ucd09 \ub2f4\ub2f9 "
334 L"\uc778\uc0ac\ub4e4 \uc870\uc0ac"));
335 profile1.SetRawInfo(ADDRESS_HOME_CITY,
336 WideToUTF16(L"\u653f\u5e9c\u4e0d\u6392\u9664\u7acb\u6cd5"
337 L"\u898f\u7ba1\u5c0e\u904a"));
338 profile1.SetRawInfo(ADDRESS_HOME_ZIP, WideToUTF16(L"YOHO_54676"));
339 profile1.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, WideToUTF16(L"861088828000"));
340 profile1.SetInfo(
341 AutofillType(ADDRESS_HOME_COUNTRY), WideToUTF16(L"India"), "en-US");
342 profiles.push_back(profile1);
344 AutofillProfile profile2;
345 profile2.SetRawInfo(NAME_FIRST,
346 WideToUTF16(L"\u4e0a\u6d77\u5e02\u91d1\u5c71\u533a "
347 L"\u677e\u9690\u9547\u4ead\u67ab\u516c"
348 L"\u8def1915\u53f7"));
349 profile2.SetRawInfo(NAME_LAST, WideToUTF16(L"aguantó"));
350 profile2.SetRawInfo(ADDRESS_HOME_ZIP, WideToUTF16(L"HOME 94043"));
351 profiles.push_back(profile2);
353 AutofillProfile profile3;
354 profile3.SetRawInfo(EMAIL_ADDRESS, WideToUTF16(L"sue@example.com"));
355 profile3.SetRawInfo(COMPANY_NAME, WideToUTF16(L"Company X"));
356 profiles.push_back(profile3);
358 AutofillProfile profile4;
359 profile4.SetRawInfo(NAME_FIRST, WideToUTF16(L"Joe 3254"));
360 profile4.SetRawInfo(NAME_LAST, WideToUTF16(L"\u8bb0\u8d262\u5e74\u591a"));
361 profile4.SetRawInfo(ADDRESS_HOME_ZIP,
362 WideToUTF16(L"\uff08\u90ae\u7f16\uff1a201504\uff09"));
363 profile4.SetRawInfo(EMAIL_ADDRESS, WideToUTF16(L"télévision@example.com"));
364 profile4.SetRawInfo(COMPANY_NAME,
365 WideToUTF16(L"\u0907\u0932\u0947\u0915\u093f\u091f\u094d"
366 L"\u0930\u0928\u093f\u0915\u094d\u0938, "
367 L"\u0905\u092a\u094b\u0932\u094b "
368 L"\u091f\u093e\u092f\u0930\u094d\u0938 "
369 L"\u0906\u0926\u093f"));
370 profiles.push_back(profile4);
372 AutofillProfile profile5;
373 profile5.SetRawInfo(NAME_FIRST, WideToUTF16(L"Larry"));
374 profile5.SetRawInfo(NAME_LAST,
375 WideToUTF16(L"\u0938\u094d\u091f\u093e\u0902\u092a "
376 L"\u0921\u094d\u092f\u0942\u091f\u0940"));
377 profile5.SetRawInfo(ADDRESS_HOME_ZIP,
378 WideToUTF16(L"111111111111110000GOOGLE"));
379 profile5.SetRawInfo(EMAIL_ADDRESS, WideToUTF16(L"page@000000.com"));
380 profile5.SetRawInfo(COMPANY_NAME, WideToUTF16(L"Google"));
381 profiles.push_back(profile5);
383 AutofillProfile profile6;
384 profile6.SetRawInfo(NAME_FIRST,
385 WideToUTF16(L"\u4e0a\u6d77\u5e02\u91d1\u5c71\u533a "
386 L"\u677e\u9690\u9547\u4ead\u67ab\u516c"
387 L"\u8def1915\u53f7"));
388 profile6.SetRawInfo(NAME_LAST,
389 WideToUTF16(L"\u0646\u062c\u0627\u0645\u064a\u0646\u0627 "
390 L"\u062f\u0639\u0645\u0647\u0627 "
391 L"\u0644\u0644\u0631\u0626\u064a\u0633 "
392 L"\u0627\u0644\u0633\u0648\u062f\u0627\u0646"
393 L"\u064a \u0639\u0645\u0631 "
394 L"\u0627\u0644\u0628\u0634\u064a\u0631"));
395 profile6.SetRawInfo(ADDRESS_HOME_ZIP, WideToUTF16(L"HOME 94043"));
396 profiles.push_back(profile6);
398 AutofillProfile profile7;
399 profile7.SetRawInfo(NAME_FIRST, WideToUTF16(L"&$%$$$ TESTO *&*&^&^& MOKO"));
400 profile7.SetRawInfo(NAME_MIDDLE, WideToUTF16(L"WOHOOOO$$$$$$$$****"));
401 profile7.SetRawInfo(EMAIL_ADDRESS, WideToUTF16(L"yuvu@example.com"));
402 profile7.SetRawInfo(ADDRESS_HOME_LINE1,
403 WideToUTF16(L"34544, anderson ST.(120230)"));
404 profile7.SetRawInfo(ADDRESS_HOME_CITY, WideToUTF16(L"Sunnyvale"));
405 profile7.SetRawInfo(ADDRESS_HOME_STATE, WideToUTF16(L"CA"));
406 profile7.SetRawInfo(ADDRESS_HOME_ZIP, WideToUTF16(L"94086"));
407 profile7.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, WideToUTF16(L"15466784565"));
408 profile7.SetInfo(
409 AutofillType(ADDRESS_HOME_COUNTRY), WideToUTF16(L"United States"),
410 "en-US");
411 profiles.push_back(profile7);
413 SetProfiles(&profiles);
414 ASSERT_EQ(profiles.size(), personal_data_manager()->GetProfiles().size());
415 for (size_t i = 0; i < profiles.size(); ++i) {
416 EXPECT_TRUE(std::find(profiles.begin(),
417 profiles.end(),
418 *personal_data_manager()->GetProfiles()[i]) !=
419 profiles.end());
422 std::vector<CreditCard> cards;
423 CreditCard card1;
424 card1.SetRawInfo(CREDIT_CARD_NAME,
425 WideToUTF16(L"\u751f\u6d3b\u5f88\u6709\u89c4\u5f8b "
426 L"\u4ee5\u73a9\u4e3a\u4e3b"));
427 card1.SetRawInfo(CREDIT_CARD_NUMBER, WideToUTF16(L"6011111111111117"));
428 card1.SetRawInfo(CREDIT_CARD_EXP_MONTH, WideToUTF16(L"12"));
429 card1.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, WideToUTF16(L"2011"));
430 cards.push_back(card1);
432 CreditCard card2;
433 card2.SetRawInfo(CREDIT_CARD_NAME, WideToUTF16(L"John Williams"));
434 card2.SetRawInfo(CREDIT_CARD_NUMBER, WideToUTF16(L"WokoAwesome12345"));
435 card2.SetRawInfo(CREDIT_CARD_EXP_MONTH, WideToUTF16(L"10"));
436 card2.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, WideToUTF16(L"2015"));
437 cards.push_back(card2);
439 CreditCard card3;
440 card3.SetRawInfo(CREDIT_CARD_NAME,
441 WideToUTF16(L"\u0623\u062d\u0645\u062f\u064a "
442 L"\u0646\u062c\u0627\u062f "
443 L"\u0644\u0645\u062d\u0627\u0648\u0644\u0647 "
444 L"\u0627\u063a\u062a\u064a\u0627\u0644 "
445 L"\u0641\u064a \u0645\u062f\u064a\u0646\u0629 "
446 L"\u0647\u0645\u062f\u0627\u0646 "));
447 card3.SetRawInfo(CREDIT_CARD_NUMBER,
448 WideToUTF16(L"\u092a\u0941\u0928\u0930\u094d\u091c\u0940"
449 L"\u0935\u093f\u0924 \u0939\u094b\u0917\u093e "
450 L"\u0928\u093e\u0932\u0902\u0926\u093e"));
451 card3.SetRawInfo(CREDIT_CARD_EXP_MONTH, WideToUTF16(L"10"));
452 card3.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, WideToUTF16(L"2015"));
453 cards.push_back(card3);
455 CreditCard card4;
456 card4.SetRawInfo(CREDIT_CARD_NAME,
457 WideToUTF16(L"\u039d\u03ad\u03b5\u03c2 "
458 L"\u03c3\u03c5\u03b3\u03c7\u03c9\u03bd\u03b5"
459 L"\u03cd\u03c3\u03b5\u03b9\u03c2 "
460 L"\u03ba\u03b1\u03b9 "
461 L"\u03ba\u03b1\u03c4\u03b1\u03c1\u03b3\u03ae"
462 L"\u03c3\u03b5\u03b9\u03c2"));
463 card4.SetRawInfo(CREDIT_CARD_NUMBER, WideToUTF16(L"00000000000000000000000"));
464 card4.SetRawInfo(CREDIT_CARD_EXP_MONTH, WideToUTF16(L"01"));
465 card4.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, WideToUTF16(L"2016"));
466 cards.push_back(card4);
468 SetCards(&cards);
469 ASSERT_EQ(cards.size(), personal_data_manager()->GetCreditCards().size());
470 for (size_t i = 0; i < cards.size(); ++i) {
471 EXPECT_TRUE(std::find(cards.begin(),
472 cards.end(),
473 *personal_data_manager()->GetCreditCards()[i]) !=
474 cards.end());
478 // Test filling in invalid values for profiles are saved as-is. Phone
479 // information entered into the prefs UI is not validated or rejected except for
480 // duplicates.
481 // TODO(isherman): rewrite as WebUI test?
482 IN_PROC_BROWSER_TEST_F(AutofillTest, Invalid) {
483 // First try profiles with invalid ZIP input.
484 AutofillProfile without_invalid;
485 without_invalid.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Will"));
486 without_invalid.SetRawInfo(ADDRESS_HOME_CITY, ASCIIToUTF16("Sunnyvale"));
487 without_invalid.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("CA"));
488 without_invalid.SetRawInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("my_zip"));
489 without_invalid.SetInfo(
490 AutofillType(ADDRESS_HOME_COUNTRY), ASCIIToUTF16("United States"),
491 "en-US");
493 AutofillProfile with_invalid = without_invalid;
494 with_invalid.SetRawInfo(PHONE_HOME_WHOLE_NUMBER,
495 ASCIIToUTF16("Invalid_Phone_Number"));
496 SetProfile(with_invalid);
498 ASSERT_EQ(1u, personal_data_manager()->GetProfiles().size());
499 AutofillProfile profile = *personal_data_manager()->GetProfiles()[0];
500 ASSERT_NE(without_invalid.GetRawInfo(PHONE_HOME_WHOLE_NUMBER),
501 profile.GetRawInfo(PHONE_HOME_WHOLE_NUMBER));
504 // Test invalid credit card numbers typed in prefs should be saved as-is.
505 // TODO(isherman): rewrite as WebUI test?
506 IN_PROC_BROWSER_TEST_F(AutofillTest, PrefsStringSavedAsIs) {
507 CreditCard card;
508 card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("Not_0123-5Checked"));
509 SetCard(card);
511 ASSERT_EQ(1u, personal_data_manager()->GetCreditCards().size());
512 ASSERT_EQ(card, *personal_data_manager()->GetCreditCards()[0]);
515 // Test credit card info with an invalid number is not aggregated.
516 // When filling out a form with an invalid credit card number (one that does not
517 // pass the Luhn test) the credit card info should not be saved into Autofill
518 // preferences.
519 IN_PROC_BROWSER_TEST_F(AutofillTest, InvalidCreditCardNumberIsNotAggregated) {
520 #if defined(OS_WIN) && defined(USE_ASH)
521 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
522 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
523 switches::kAshBrowserTests))
524 return;
525 #endif
527 ASSERT_TRUE(test_server()->Start());
528 std::string card("4408 0412 3456 7890");
529 ASSERT_FALSE(autofill::IsValidCreditCardNumber(ASCIIToUTF16(card)));
530 SubmitCreditCard("Bob Smith", card.c_str(), "12", "2014");
531 InfoBarService* infobar_service = InfoBarService::FromWebContents(
532 browser()->tab_strip_model()->GetActiveWebContents());
533 ASSERT_EQ(0u, infobar_service->infobar_count());
536 // Test whitespaces and separator chars are stripped for valid CC numbers.
537 // The credit card numbers used in this test pass the Luhn test. For reference:
538 // http://www.merriampark.com/anatomycc.htm
539 IN_PROC_BROWSER_TEST_F(AutofillTest,
540 WhitespacesAndSeparatorCharsStrippedForValidCCNums) {
541 #if defined(OS_WIN) && defined(USE_ASH)
542 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
543 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
544 switches::kAshBrowserTests))
545 return;
546 #endif
548 ASSERT_TRUE(test_server()->Start());
549 SubmitCreditCard("Bob Smith", "4408 0412 3456 7893", "12", "2014");
550 SubmitCreditCard("Jane Doe", "4417-1234-5678-9113", "10", "2013");
552 ASSERT_EQ(2u, personal_data_manager()->GetCreditCards().size());
553 base::string16 cc1 = personal_data_manager()->GetCreditCards()[0]->GetRawInfo(
554 CREDIT_CARD_NUMBER);
555 ASSERT_TRUE(autofill::IsValidCreditCardNumber(cc1));
556 base::string16 cc2 = personal_data_manager()->GetCreditCards()[1]->GetRawInfo(
557 CREDIT_CARD_NUMBER);
558 ASSERT_TRUE(autofill::IsValidCreditCardNumber(cc2));
561 // Test that Autofill aggregates a minimum valid profile.
562 // The minimum required address fields must be specified: First Name, Last Name,
563 // Address Line 1, City, Zip Code, and State.
564 IN_PROC_BROWSER_TEST_F(AutofillTest, AggregatesMinValidProfile) {
565 ASSERT_TRUE(test_server()->Start());
566 FormMap data;
567 data["NAME_FIRST"] = "Bob";
568 data["NAME_LAST"] = "Smith";
569 data["ADDRESS_HOME_LINE1"] = "1234 H St.";
570 data["ADDRESS_HOME_CITY"] = "Mountain View";
571 data["ADDRESS_HOME_STATE"] = "CA";
572 data["ADDRESS_HOME_ZIP"] = "94043";
573 FillFormAndSubmit("duplicate_profiles_test.html", data);
575 ASSERT_EQ(1u, personal_data_manager()->GetProfiles().size());
578 // Different Javascript to submit the form.
579 IN_PROC_BROWSER_TEST_F(AutofillTest, AggregatesMinValidProfileDifferentJS) {
580 ASSERT_TRUE(test_server()->Start());
581 FormMap data;
582 data["NAME_FIRST"] = "Bob";
583 data["NAME_LAST"] = "Smith";
584 data["ADDRESS_HOME_LINE1"] = "1234 H St.";
585 data["ADDRESS_HOME_CITY"] = "Mountain View";
586 data["ADDRESS_HOME_STATE"] = "CA";
587 data["ADDRESS_HOME_ZIP"] = "94043";
589 std::string submit("document.forms[0].submit();");
590 FillFormAndSubmitWithHandler("duplicate_profiles_test.html", data, submit,
591 false, true);
593 ASSERT_EQ(1u, personal_data_manager()->GetProfiles().size());
596 // Form submitted via JavaScript, with an event handler on the submit event
597 // which prevents submission of the form. Will not update the user's personal
598 // data.
599 IN_PROC_BROWSER_TEST_F(AutofillTest, ProfilesNotAggregatedWithSubmitHandler) {
600 ASSERT_TRUE(test_server()->Start());
601 FormMap data;
602 data["NAME_FIRST"] = "Bob";
603 data["NAME_LAST"] = "Smith";
604 data["ADDRESS_HOME_LINE1"] = "1234 H St.";
605 data["ADDRESS_HOME_CITY"] = "Mountain View";
606 data["ADDRESS_HOME_STATE"] = "CA";
607 data["ADDRESS_HOME_ZIP"] = "94043";
609 std::string submit(
610 "var preventFunction = function(event) { event.preventDefault(); };"
611 "document.forms[0].addEventListener('submit', preventFunction);"
612 "document.querySelector('input[type=submit]').click();");
613 FillFormAndSubmitWithHandler("duplicate_profiles_test.html", data, submit,
614 false, false);
616 // The AutofillManager will NOT update the user's profile.
617 EXPECT_EQ(0u, personal_data_manager()->GetProfiles().size());
619 // We remove the submit handler and resubmit the form. This time the profile
620 // will be updated. This is to guard against the underlying mechanics changing
621 // and to try to avoid flakiness if this happens. We submit slightly different
622 // data to make sure the expected data is saved.
623 data["NAME_FIRST"] = "John";
624 data["NAME_LAST"] = "Doe";
625 std::string change_and_resubmit =
626 GetJSToFillForm(data) +
627 "document.forms[0].removeEventListener('submit', preventFunction);"
628 "document.querySelector('input[type=submit]').click();";
629 WindowedPersonalDataManagerObserver observer(browser());
630 ASSERT_TRUE(content::ExecuteScript(render_view_host(), change_and_resubmit));
631 observer.Wait();
633 // The AutofillManager will update the user's profile this time.
634 ASSERT_EQ(1u, personal_data_manager()->GetProfiles().size());
635 EXPECT_EQ(ASCIIToUTF16("John"),
636 personal_data_manager()->GetProfiles()[0]->GetRawInfo(NAME_FIRST));
637 EXPECT_EQ(ASCIIToUTF16("Doe"),
638 personal_data_manager()->GetProfiles()[0]->GetRawInfo(NAME_LAST));
641 // Test Autofill does not aggregate profiles with no address info.
642 // The minimum required address fields must be specified: First Name, Last Name,
643 // Address Line 1, City, Zip Code, and State.
644 IN_PROC_BROWSER_TEST_F(AutofillTest, ProfilesNotAggregatedWithNoAddress) {
645 ASSERT_TRUE(test_server()->Start());
646 FormMap data;
647 data["NAME_FIRST"] = "Bob";
648 data["NAME_LAST"] = "Smith";
649 data["EMAIL_ADDRESS"] = "bsmith@example.com";
650 data["COMPANY_NAME"] = "Mountain View";
651 data["ADDRESS_HOME_CITY"] = "Mountain View";
652 data["PHONE_HOME_WHOLE_NUMBER"] = "650-555-4567";
653 FillFormAndSubmit("duplicate_profiles_test.html", data);
655 ASSERT_TRUE(personal_data_manager()->GetProfiles().empty());
658 // Test Autofill does not aggregate profiles with an invalid email.
659 IN_PROC_BROWSER_TEST_F(AutofillTest, ProfilesNotAggregatedWithInvalidEmail) {
660 ASSERT_TRUE(test_server()->Start());
661 FormMap data;
662 data["NAME_FIRST"] = "Bob";
663 data["NAME_LAST"] = "Smith";
664 data["EMAIL_ADDRESS"] = "garbage";
665 data["ADDRESS_HOME_LINE1"] = "1234 H St.";
666 data["ADDRESS_HOME_CITY"] = "San Jose";
667 data["ADDRESS_HOME_STATE"] = "CA";
668 data["ADDRESS_HOME_ZIP"] = "95110";
669 data["COMPANY_NAME"] = "Company X";
670 data["PHONE_HOME_WHOLE_NUMBER"] = "408-871-4567";
671 FillFormAndSubmit("duplicate_profiles_test.html", data);
673 ASSERT_TRUE(personal_data_manager()->GetProfiles().empty());
676 // Test profile is saved if phone number is valid in selected country.
677 // The data file contains two profiles with valid phone numbers and two
678 // profiles with invalid phone numbers from their respective country.
679 IN_PROC_BROWSER_TEST_F(AutofillTest, ProfileSavedWithValidCountryPhone) {
680 ASSERT_TRUE(test_server()->Start());
681 std::vector<FormMap> profiles;
683 FormMap data1;
684 data1["NAME_FIRST"] = "Bob";
685 data1["NAME_LAST"] = "Smith";
686 data1["ADDRESS_HOME_LINE1"] = "123 Cherry Ave";
687 data1["ADDRESS_HOME_CITY"] = "Mountain View";
688 data1["ADDRESS_HOME_STATE"] = "CA";
689 data1["ADDRESS_HOME_ZIP"] = "94043";
690 data1["ADDRESS_HOME_COUNTRY"] = "United States";
691 data1["PHONE_HOME_WHOLE_NUMBER"] = "408-871-4567";
692 profiles.push_back(data1);
694 FormMap data2;
695 data2["NAME_FIRST"] = "John";
696 data2["NAME_LAST"] = "Doe";
697 data2["ADDRESS_HOME_LINE1"] = "987 H St";
698 data2["ADDRESS_HOME_CITY"] = "San Jose";
699 data2["ADDRESS_HOME_STATE"] = "CA";
700 data2["ADDRESS_HOME_ZIP"] = "95510";
701 data2["ADDRESS_HOME_COUNTRY"] = "United States";
702 data2["PHONE_HOME_WHOLE_NUMBER"] = "408-123-456";
703 profiles.push_back(data2);
705 FormMap data3;
706 data3["NAME_FIRST"] = "Jane";
707 data3["NAME_LAST"] = "Doe";
708 data3["ADDRESS_HOME_LINE1"] = "1523 Garcia St";
709 data3["ADDRESS_HOME_CITY"] = "Mountain View";
710 data3["ADDRESS_HOME_STATE"] = "CA";
711 data3["ADDRESS_HOME_ZIP"] = "94043";
712 data3["ADDRESS_HOME_COUNTRY"] = "Germany";
713 data3["PHONE_HOME_WHOLE_NUMBER"] = "+49 40-80-81-79-000";
714 profiles.push_back(data3);
716 FormMap data4;
717 data4["NAME_FIRST"] = "Bonnie";
718 data4["NAME_LAST"] = "Smith";
719 data4["ADDRESS_HOME_LINE1"] = "6723 Roadway Rd";
720 data4["ADDRESS_HOME_CITY"] = "San Jose";
721 data4["ADDRESS_HOME_STATE"] = "CA";
722 data4["ADDRESS_HOME_ZIP"] = "95510";
723 data4["ADDRESS_HOME_COUNTRY"] = "Germany";
724 data4["PHONE_HOME_WHOLE_NUMBER"] = "+21 08450 777 777";
725 profiles.push_back(data4);
727 for (size_t i = 0; i < profiles.size(); ++i)
728 FillFormAndSubmit("autofill_test_form.html", profiles[i]);
730 ASSERT_EQ(2u, personal_data_manager()->GetProfiles().size());
731 int us_address_index =
732 personal_data_manager()->GetProfiles()[0]->GetRawInfo(
733 ADDRESS_HOME_LINE1) == ASCIIToUTF16("123 Cherry Ave")
735 : 1;
737 EXPECT_EQ(
738 ASCIIToUTF16("408-871-4567"),
739 personal_data_manager()->GetProfiles()[us_address_index]->GetRawInfo(
740 PHONE_HOME_WHOLE_NUMBER));
741 ASSERT_EQ(
742 ASCIIToUTF16("+49 40-80-81-79-000"),
743 personal_data_manager()->GetProfiles()[1 - us_address_index]->GetRawInfo(
744 PHONE_HOME_WHOLE_NUMBER));
747 // Prepend country codes when formatting phone numbers, but only if the user
748 // provided one in the first place.
749 IN_PROC_BROWSER_TEST_F(AutofillTest, AppendCountryCodeForAggregatedPhones) {
750 ASSERT_TRUE(test_server()->Start());
751 FormMap data;
752 data["NAME_FIRST"] = "Bob";
753 data["NAME_LAST"] = "Smith";
754 data["ADDRESS_HOME_LINE1"] = "1234 H St.";
755 data["ADDRESS_HOME_CITY"] = "San Jose";
756 data["ADDRESS_HOME_STATE"] = "CA";
757 data["ADDRESS_HOME_ZIP"] = "95110";
758 data["ADDRESS_HOME_COUNTRY"] = "Germany";
759 data["PHONE_HOME_WHOLE_NUMBER"] = "+4908450777777";
760 FillFormAndSubmit("autofill_test_form.html", data);
762 data["ADDRESS_HOME_LINE1"] = "4321 H St.";
763 data["PHONE_HOME_WHOLE_NUMBER"] = "08450777777";
764 FillFormAndSubmit("autofill_test_form.html", data);
766 ASSERT_EQ(2u, personal_data_manager()->GetProfiles().size());
767 int second_address_index =
768 personal_data_manager()->GetProfiles()[0]->GetRawInfo(
769 ADDRESS_HOME_LINE1) == ASCIIToUTF16("4321 H St.")
771 : 1;
773 EXPECT_EQ(ASCIIToUTF16("+49 8450 777777"),
774 personal_data_manager()
775 ->GetProfiles()[1 - second_address_index]
776 ->GetRawInfo(PHONE_HOME_WHOLE_NUMBER));
778 EXPECT_EQ(
779 ASCIIToUTF16("08450 777777"),
780 personal_data_manager()->GetProfiles()[second_address_index]->GetRawInfo(
781 PHONE_HOME_WHOLE_NUMBER));
784 // Test that Autofill uses '+' sign for international numbers.
785 // This applies to the following cases:
786 // The phone number has a leading '+'.
787 // The phone number does not have a leading '+'.
788 // The phone number has a leading international direct dialing (IDD) code.
789 // This does not apply to US numbers. For US numbers, '+' is removed.
790 IN_PROC_BROWSER_TEST_F(AutofillTest, UsePlusSignForInternationalNumber) {
791 ASSERT_TRUE(test_server()->Start());
792 std::vector<FormMap> profiles;
794 FormMap data1;
795 data1["NAME_FIRST"] = "Bonnie";
796 data1["NAME_LAST"] = "Smith";
797 data1["ADDRESS_HOME_LINE1"] = "6723 Roadway Rd";
798 data1["ADDRESS_HOME_CITY"] = "Reading";
799 data1["ADDRESS_HOME_STATE"] = "Berkshire";
800 data1["ADDRESS_HOME_ZIP"] = "RG12 3BR";
801 data1["ADDRESS_HOME_COUNTRY"] = "United Kingdom";
802 data1["PHONE_HOME_WHOLE_NUMBER"] = "+44 7624-123456";
803 profiles.push_back(data1);
805 FormMap data2;
806 data2["NAME_FIRST"] = "John";
807 data2["NAME_LAST"] = "Doe";
808 data2["ADDRESS_HOME_LINE1"] = "987 H St";
809 data2["ADDRESS_HOME_CITY"] = "Reading";
810 data2["ADDRESS_HOME_STATE"] = "BerkShire";
811 data2["ADDRESS_HOME_ZIP"] = "RG12 3BR";
812 data2["ADDRESS_HOME_COUNTRY"] = "United Kingdom";
813 data2["PHONE_HOME_WHOLE_NUMBER"] = "44 7624 123456";
814 profiles.push_back(data2);
816 FormMap data3;
817 data3["NAME_FIRST"] = "Jane";
818 data3["NAME_LAST"] = "Doe";
819 data3["ADDRESS_HOME_LINE1"] = "1523 Garcia St";
820 data3["ADDRESS_HOME_CITY"] = "Reading";
821 data3["ADDRESS_HOME_STATE"] = "BerkShire";
822 data3["ADDRESS_HOME_ZIP"] = "RG12 3BR";
823 data3["ADDRESS_HOME_COUNTRY"] = "United Kingdom";
824 data3["PHONE_HOME_WHOLE_NUMBER"] = "0044 7624 123456";
825 profiles.push_back(data3);
827 FormMap data4;
828 data4["NAME_FIRST"] = "Bob";
829 data4["NAME_LAST"] = "Smith";
830 data4["ADDRESS_HOME_LINE1"] = "123 Cherry Ave";
831 data4["ADDRESS_HOME_CITY"] = "Mountain View";
832 data4["ADDRESS_HOME_STATE"] = "CA";
833 data4["ADDRESS_HOME_ZIP"] = "94043";
834 data4["ADDRESS_HOME_COUNTRY"] = "United States";
835 data4["PHONE_HOME_WHOLE_NUMBER"] = "+1 (408) 871-4567";
836 profiles.push_back(data4);
838 for (size_t i = 0; i < profiles.size(); ++i)
839 FillFormAndSubmit("autofill_test_form.html", profiles[i]);
841 ASSERT_EQ(4u, personal_data_manager()->GetProfiles().size());
843 for (size_t i = 0; i < personal_data_manager()->GetProfiles().size(); ++i) {
844 AutofillProfile* profile = personal_data_manager()->GetProfiles()[i];
845 std::string expectation;
846 std::string name = UTF16ToASCII(profile->GetRawInfo(NAME_FIRST));
848 if (name == "Bonnie")
849 expectation = "+447624123456";
850 else if (name == "John")
851 expectation = "+447624123456";
852 else if (name == "Jane")
853 expectation = "+447624123456";
854 else if (name == "Bob")
855 expectation = "14088714567";
857 EXPECT_EQ(ASCIIToUTF16(expectation),
858 profile->GetInfo(AutofillType(PHONE_HOME_WHOLE_NUMBER), ""));
862 // Test CC info not offered to be saved when autocomplete=off for CC field.
863 // If the credit card number field has autocomplete turned off, then the credit
864 // card infobar should not offer to save the credit card info. The credit card
865 // number must be a valid Luhn number.
866 IN_PROC_BROWSER_TEST_F(AutofillTest, CCInfoNotStoredWhenAutocompleteOff) {
867 #if defined(OS_WIN) && defined(USE_ASH)
868 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
869 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
870 switches::kAshBrowserTests))
871 return;
872 #endif
874 ASSERT_TRUE(test_server()->Start());
875 FormMap data;
876 data["CREDIT_CARD_NAME"] = "Bob Smith";
877 data["CREDIT_CARD_NUMBER"] = "4408041234567893";
878 data["CREDIT_CARD_EXP_MONTH"] = "12";
879 data["CREDIT_CARD_EXP_4_DIGIT_YEAR"] = "2014";
880 FillFormAndSubmit("cc_autocomplete_off_test.html", data);
882 InfoBarService* infobar_service = InfoBarService::FromWebContents(
883 browser()->tab_strip_model()->GetActiveWebContents());
884 ASSERT_EQ(0u, infobar_service->infobar_count());
887 // Test profile not aggregated if email found in non-email field.
888 IN_PROC_BROWSER_TEST_F(AutofillTest, ProfileWithEmailInOtherFieldNotSaved) {
889 ASSERT_TRUE(test_server()->Start());
891 FormMap data;
892 data["NAME_FIRST"] = "Bob";
893 data["NAME_LAST"] = "Smith";
894 data["ADDRESS_HOME_LINE1"] = "bsmith@gmail.com";
895 data["ADDRESS_HOME_CITY"] = "San Jose";
896 data["ADDRESS_HOME_STATE"] = "CA";
897 data["ADDRESS_HOME_ZIP"] = "95110";
898 data["COMPANY_NAME"] = "Company X";
899 data["PHONE_HOME_WHOLE_NUMBER"] = "408-871-4567";
900 FillFormAndSubmit("duplicate_profiles_test.html", data);
902 ASSERT_EQ(0u, personal_data_manager()->GetProfiles().size());
905 // Test that profiles merge for aggregated data with same address.
906 // The criterion for when two profiles are expected to be merged is when their
907 // 'Address Line 1' and 'City' data match. When two profiles are merged, any
908 // remaining address fields are expected to be overwritten. Any non-address
909 // fields should accumulate multi-valued data.
910 // DISABLED: http://crbug.com/281541
911 IN_PROC_BROWSER_TEST_F(AutofillTest,
912 DISABLED_MergeAggregatedProfilesWithSameAddress) {
913 AggregateProfilesIntoAutofillPrefs("dataset_same_address.txt");
915 ASSERT_EQ(3u, personal_data_manager()->GetProfiles().size());
918 // Test profiles are not merged without minimum address values.
919 // Mininum address values needed during aggregation are: address line 1, city,
920 // state, and zip code.
921 // Profiles are merged when data for address line 1 and city match.
922 // DISABLED: http://crbug.com/281541
923 IN_PROC_BROWSER_TEST_F(AutofillTest,
924 DISABLED_ProfilesNotMergedWhenNoMinAddressData) {
925 AggregateProfilesIntoAutofillPrefs("dataset_no_address.txt");
927 ASSERT_EQ(0u, personal_data_manager()->GetProfiles().size());
930 // Test Autofill ability to merge duplicate profiles and throw away junk.
931 // TODO(isherman): this looks redundant, consider removing.
932 // DISABLED: http://crbug.com/281541
933 IN_PROC_BROWSER_TEST_F(AutofillTest,
934 DISABLED_MergeAggregatedDuplicatedProfiles) {
935 int num_of_profiles =
936 AggregateProfilesIntoAutofillPrefs("dataset_duplicated_profiles.txt");
938 ASSERT_GT(num_of_profiles,
939 static_cast<int>(personal_data_manager()->GetProfiles().size()));
942 } // namespace autofill