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