Update mojo surfaces bindings and mojo/cc/ glue
[chromium-blink-merge.git] / chrome / browser / autofill / autofill_browsertest.cc
blobf5bb63b938e661c7c194c2f24ac9141ffb5efed8
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include <string>
7 #include "base/basictypes.h"
8 #include "base/command_line.h"
9 #include "base/file_util.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/rand_util.h"
13 #include "base/strings/string16.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_split.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "base/time/time.h"
18 #include "chrome/browser/autofill/personal_data_manager_factory.h"
19 #include "chrome/browser/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"
49 using base::ASCIIToUTF16;
50 using base::WideToUTF16;
52 namespace autofill {
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);
69 virtual ~WindowedPersonalDataManagerObserver() {
70 infobar_service_->RemoveObserver(this);
72 if (infobar_service_->infobar_count() > 0) {
73 infobar_service_->RemoveInfoBar(infobar_service_->infobar_at(0));
77 void Wait() {
78 if (!alerted_) {
79 has_run_message_loop_ = true;
80 content::RunMessageLoop();
82 PersonalDataManagerFactory::GetForProfile(browser_->profile())->
83 RemoveObserver(this);
86 // PersonalDataManagerObserver:
87 virtual void OnPersonalDataChanged() OVERRIDE {
88 if (has_run_message_loop_) {
89 base::MessageLoopForUI::current()->Quit();
90 has_run_message_loop_ = false;
92 alerted_ = true;
95 virtual void OnInsufficientFormData() OVERRIDE {
96 OnPersonalDataChanged();
99 // 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();
107 private:
108 bool alerted_;
109 bool has_run_message_loop_;
110 Browser* browser_;
111 InfoBarService* infobar_service_;
114 class AutofillTest : public InProcessBrowserTest {
115 protected:
116 AutofillTest() {}
118 virtual void SetUpOnMainThread() OVERRIDE {
119 // Don't want Keychain coming up on Mac.
120 test::DisableSystemServices(browser()->profile()->GetPrefs());
123 virtual void TearDownOnMainThread() 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();
132 PersonalDataManager* personal_data_manager() {
133 return PersonalDataManagerFactory::GetForProfile(browser()->profile());
136 void SetProfiles(std::vector<AutofillProfile>* profiles) {
137 WindowedPersonalDataManagerObserver observer(browser());
138 personal_data_manager()->SetProfiles(profiles);
139 observer.Wait();
142 void SetProfile(const AutofillProfile& profile) {
143 std::vector<AutofillProfile> profiles;
144 profiles.push_back(profile);
145 SetProfiles(&profiles);
148 void SetCards(std::vector<CreditCard>* cards) {
149 WindowedPersonalDataManagerObserver observer(browser());
150 personal_data_manager()->SetCreditCards(cards);
151 observer.Wait();
154 void SetCard(const CreditCard& card) {
155 std::vector<CreditCard> cards;
156 cards.push_back(card);
157 SetCards(&cards);
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);
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 + "';";
175 js += "document.onclick = function() {"
176 " document.getElementById('testform').submit();"
177 "};";
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();
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);
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());
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;
218 std::vector<std::string> fields;
219 base::SplitString(lines[i], '|', &fields);
220 if (fields.empty())
221 continue; // Blank line.
223 ++parsed_profiles;
224 CHECK_EQ(12u, fields.size());
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];
240 FillFormAndSubmit("duplicate_profiles_test.html", data);
242 return parsed_profiles;
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);
256 content::RenderViewHost* render_view_host() {
257 return browser()->tab_strip_model()->GetActiveWebContents()->
258 GetRenderViewHost();
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");
273 private:
274 net::TestURLFetcherFactory url_fetcher_factory_;
277 // Test filling profiles with unicode strings and crazy characters.
278 // TODO(isherman): rewrite as unit test under PersonalDataManagerTest.
279 IN_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);
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);
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);
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);
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);
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);
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);
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]);
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);
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);
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);
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);
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]);
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?
440 IN_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");
451 AutofillProfile with_invalid = without_invalid;
452 with_invalid.SetRawInfo(PHONE_HOME_WHOLE_NUMBER,
453 ASCIIToUTF16("Invalid_Phone_Number"));
454 SetProfile(with_invalid);
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));
462 // Test invalid credit card numbers typed in prefs should be saved as-is.
463 // TODO(isherman): rewrite as WebUI test?
464 IN_PROC_BROWSER_TEST_F(AutofillTest, PrefsStringSavedAsIs) {
465 CreditCard card;
466 card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("Not_0123-5Checked"));
467 SetCard(card);
469 ASSERT_EQ(1u, personal_data_manager()->GetCreditCards().size());
470 ASSERT_EQ(card, *personal_data_manager()->GetCreditCards()[0]);
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.
477 IN_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
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());
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
496 IN_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
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");
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));
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.
520 IN_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);
531 ASSERT_EQ(1u, personal_data_manager()->GetProfiles().size());
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.
537 IN_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);
548 ASSERT_TRUE(personal_data_manager()->GetProfiles().empty());
551 // Test Autofill does not aggregate profiles with an invalid email.
552 IN_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);
566 ASSERT_TRUE(personal_data_manager()->GetProfiles().empty());
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.
572 IN_PROC_BROWSER_TEST_F(AutofillTest, ProfileSavedWithValidCountryPhone) {
573 ASSERT_TRUE(test_server()->Start());
574 std::vector<FormMap> profiles;
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);
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);
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);
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);
620 for (size_t i = 0; i < profiles.size(); ++i)
621 FillFormAndSubmit("autofill_test_form.html", profiles[i]);
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));
632 // Prepend country codes when formatting phone numbers, but only if the user
633 // provided one in the first place.
634 IN_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);
647 data["ADDRESS_HOME_LINE1"] = "4321 H St.";
648 data["PHONE_HOME_WHOLE_NUMBER"] = "08450777777";
649 FillFormAndSubmit("autofill_test_form.html", data);
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));
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));
662 // Test that Autofill uses '+' sign for international numbers.
663 // This applies to the following cases:
664 // The phone number has a leading '+'.
665 // The phone number does not have a leading '+'.
666 // The phone number has a leading international direct dialing (IDD) code.
667 // This does not apply to US numbers. For US numbers, '+' is removed.
668 IN_PROC_BROWSER_TEST_F(AutofillTest, UsePlusSignForInternaltionalNumber) {
669 ASSERT_TRUE(test_server()->Start());
670 std::vector<FormMap> profiles;
672 FormMap data1;
673 data1["NAME_FIRST"] = "Bonnie";
674 data1["NAME_LAST"] = "Smith";
675 data1["ADDRESS_HOME_LINE1"] = "6723 Roadway Rd";
676 data1["ADDRESS_HOME_CITY"] = "Reading";
677 data1["ADDRESS_HOME_STATE"] = "Berkshire";
678 data1["ADDRESS_HOME_ZIP"] = "RG12 3BR";
679 data1["ADDRESS_HOME_COUNTRY"] = "United Kingdom";
680 data1["PHONE_HOME_WHOLE_NUMBER"] = "+44 7624-123456";
681 profiles.push_back(data1);
683 FormMap data2;
684 data2["NAME_FIRST"] = "John";
685 data2["NAME_LAST"] = "Doe";
686 data2["ADDRESS_HOME_LINE1"] = "987 H St";
687 data2["ADDRESS_HOME_CITY"] = "Reading";
688 data2["ADDRESS_HOME_STATE"] = "BerkShire";
689 data2["ADDRESS_HOME_ZIP"] = "RG12 3BR";
690 data2["ADDRESS_HOME_COUNTRY"] = "United Kingdom";
691 data2["PHONE_HOME_WHOLE_NUMBER"] = "44 7624 123456";
692 profiles.push_back(data2);
694 FormMap data3;
695 data3["NAME_FIRST"] = "Jane";
696 data3["NAME_LAST"] = "Doe";
697 data3["ADDRESS_HOME_LINE1"] = "1523 Garcia St";
698 data3["ADDRESS_HOME_CITY"] = "Reading";
699 data3["ADDRESS_HOME_STATE"] = "BerkShire";
700 data3["ADDRESS_HOME_ZIP"] = "RG12 3BR";
701 data3["ADDRESS_HOME_COUNTRY"] = "United Kingdom";
702 data3["PHONE_HOME_WHOLE_NUMBER"] = "0044 7624 123456";
703 profiles.push_back(data3);
705 FormMap data4;
706 data4["NAME_FIRST"] = "Bob";
707 data4["NAME_LAST"] = "Smith";
708 data4["ADDRESS_HOME_LINE1"] = "123 Cherry Ave";
709 data4["ADDRESS_HOME_CITY"] = "Mountain View";
710 data4["ADDRESS_HOME_STATE"] = "CA";
711 data4["ADDRESS_HOME_ZIP"] = "94043";
712 data4["ADDRESS_HOME_COUNTRY"] = "United States";
713 data4["PHONE_HOME_WHOLE_NUMBER"] = "+1 (408) 871-4567";
714 profiles.push_back(data4);
716 for (size_t i = 0; i < profiles.size(); ++i)
717 FillFormAndSubmit("autofill_test_form.html", profiles[i]);
719 ASSERT_EQ(4u, personal_data_manager()->GetProfiles().size());
720 ASSERT_EQ(ASCIIToUTF16("+447624123456"),
721 personal_data_manager()->GetProfiles()[0]->GetInfo(
722 AutofillType(PHONE_HOME_WHOLE_NUMBER), ""));
723 ASSERT_EQ(ASCIIToUTF16("+447624123456"),
724 personal_data_manager()->GetProfiles()[1]->GetInfo(
725 AutofillType(PHONE_HOME_WHOLE_NUMBER), ""));
726 ASSERT_EQ(ASCIIToUTF16("+447624123456"),
727 personal_data_manager()->GetProfiles()[2]->GetInfo(
728 AutofillType(PHONE_HOME_WHOLE_NUMBER), ""));
729 ASSERT_EQ(ASCIIToUTF16("14088714567"),
730 personal_data_manager()->GetProfiles()[3]->GetInfo(
731 AutofillType(PHONE_HOME_WHOLE_NUMBER), ""));
734 // Test CC info not offered to be saved when autocomplete=off for CC field.
735 // If the credit card number field has autocomplete turned off, then the credit
736 // card infobar should not offer to save the credit card info. The credit card
737 // number must be a valid Luhn number.
738 IN_PROC_BROWSER_TEST_F(AutofillTest, CCInfoNotStoredWhenAutocompleteOff) {
739 #if defined(OS_WIN) && defined(USE_ASH)
740 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
741 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
742 return;
743 #endif
745 ASSERT_TRUE(test_server()->Start());
746 FormMap data;
747 data["CREDIT_CARD_NAME"] = "Bob Smith";
748 data["CREDIT_CARD_NUMBER"] = "4408041234567893";
749 data["CREDIT_CARD_EXP_MONTH"] = "12";
750 data["CREDIT_CARD_EXP_4_DIGIT_YEAR"] = "2014";
751 FillFormAndSubmit("cc_autocomplete_off_test.html", data);
753 InfoBarService* infobar_service = InfoBarService::FromWebContents(
754 browser()->tab_strip_model()->GetActiveWebContents());
755 ASSERT_EQ(0u, infobar_service->infobar_count());
758 // Test profile not aggregated if email found in non-email field.
759 IN_PROC_BROWSER_TEST_F(AutofillTest, ProfileWithEmailInOtherFieldNotSaved) {
760 ASSERT_TRUE(test_server()->Start());
762 FormMap data;
763 data["NAME_FIRST"] = "Bob";
764 data["NAME_LAST"] = "Smith";
765 data["ADDRESS_HOME_LINE1"] = "bsmith@gmail.com";
766 data["ADDRESS_HOME_CITY"] = "San Jose";
767 data["ADDRESS_HOME_STATE"] = "CA";
768 data["ADDRESS_HOME_ZIP"] = "95110";
769 data["COMPANY_NAME"] = "Company X";
770 data["PHONE_HOME_WHOLE_NUMBER"] = "408-871-4567";
771 FillFormAndSubmit("duplicate_profiles_test.html", data);
773 ASSERT_EQ(0u, personal_data_manager()->GetProfiles().size());
776 // Test that profiles merge for aggregated data with same address.
777 // The criterion for when two profiles are expected to be merged is when their
778 // 'Address Line 1' and 'City' data match. When two profiles are merged, any
779 // remaining address fields are expected to be overwritten. Any non-address
780 // fields should accumulate multi-valued data.
781 // DISABLED: http://crbug.com/281541
782 IN_PROC_BROWSER_TEST_F(AutofillTest,
783 DISABLED_MergeAggregatedProfilesWithSameAddress) {
784 AggregateProfilesIntoAutofillPrefs("dataset_same_address.txt");
786 ASSERT_EQ(3u, personal_data_manager()->GetProfiles().size());
789 // Test profiles are not merged without minimum address values.
790 // Mininum address values needed during aggregation are: address line 1, city,
791 // state, and zip code.
792 // Profiles are merged when data for address line 1 and city match.
793 // DISABLED: http://crbug.com/281541
794 IN_PROC_BROWSER_TEST_F(AutofillTest,
795 DISABLED_ProfilesNotMergedWhenNoMinAddressData) {
796 AggregateProfilesIntoAutofillPrefs("dataset_no_address.txt");
798 ASSERT_EQ(0u, personal_data_manager()->GetProfiles().size());
801 // Test Autofill ability to merge duplicate profiles and throw away junk.
802 // TODO(isherman): this looks redundant, consider removing.
803 // DISABLED: http://crbug.com/281541
804 IN_PROC_BROWSER_TEST_F(AutofillTest,
805 DISABLED_MergeAggregatedDuplicatedProfiles) {
806 int num_of_profiles =
807 AggregateProfilesIntoAutofillPrefs("dataset_duplicated_profiles.txt");
809 ASSERT_GT(num_of_profiles,
810 static_cast<int>(personal_data_manager()->GetProfiles().size()));
813 } // namespace autofill