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