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