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