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