Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / autofill / core / browser / personal_data_manager_unittest.cc
blob0e93585cacabe0d23ec7e8bc275aec2284a28d44
1 // Copyright 2013 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 <algorithm>
6 #include <string>
7 #include <vector>
9 #include "base/basictypes.h"
10 #include "base/command_line.h"
11 #include "base/files/scoped_temp_dir.h"
12 #include "base/guid.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/prefs/pref_service.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "base/synchronization/waitable_event.h"
17 #include "base/thread_task_runner_handle.h"
18 #include "components/autofill/core/browser/autofill_experiments.h"
19 #include "components/autofill/core/browser/autofill_profile.h"
20 #include "components/autofill/core/browser/autofill_test_utils.h"
21 #include "components/autofill/core/browser/field_types.h"
22 #include "components/autofill/core/browser/form_structure.h"
23 #include "components/autofill/core/browser/personal_data_manager.h"
24 #include "components/autofill/core/browser/personal_data_manager_observer.h"
25 #include "components/autofill/core/browser/webdata/autofill_table.h"
26 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
27 #include "components/autofill/core/common/autofill_pref_names.h"
28 #include "components/autofill/core/common/autofill_switches.h"
29 #include "components/autofill/core/common/form_data.h"
30 #include "components/signin/core/browser/account_tracker_service.h"
31 #include "components/signin/core/browser/test_signin_client.h"
32 #include "components/signin/core/common/signin_pref_names.h"
33 #include "components/webdata/common/web_data_service_base.h"
34 #include "components/webdata/common/web_database_service.h"
35 #include "testing/gmock/include/gmock/gmock.h"
36 #include "testing/gtest/include/gtest/gtest.h"
38 using base::ASCIIToUTF16;
39 using base::UTF8ToUTF16;
41 namespace autofill {
42 namespace {
44 enum UserMode { USER_MODE_NORMAL, USER_MODE_INCOGNITO };
46 ACTION(QuitMainMessageLoop) { base::MessageLoop::current()->Quit(); }
48 class PersonalDataLoadedObserverMock : public PersonalDataManagerObserver {
49 public:
50 PersonalDataLoadedObserverMock() {}
51 virtual ~PersonalDataLoadedObserverMock() {}
53 MOCK_METHOD0(OnPersonalDataChanged, void());
56 template <typename T>
57 bool CompareElements(T* a, T* b) {
58 return a->Compare(*b) < 0;
61 template <typename T>
62 bool ElementsEqual(T* a, T* b) {
63 return a->Compare(*b) == 0;
66 // Verifies that two vectors have the same elements (according to T::Compare)
67 // while ignoring order. This is useful because multiple profiles or credit
68 // cards that are added to the SQLite DB within the same second will be returned
69 // in GUID (aka random) order.
70 template <typename T>
71 void ExpectSameElements(const std::vector<T*>& expectations,
72 const std::vector<T*>& results) {
73 ASSERT_EQ(expectations.size(), results.size());
75 std::vector<T*> expectations_copy = expectations;
76 std::sort(
77 expectations_copy.begin(), expectations_copy.end(), CompareElements<T>);
78 std::vector<T*> results_copy = results;
79 std::sort(results_copy.begin(), results_copy.end(), CompareElements<T>);
81 EXPECT_EQ(std::mismatch(results_copy.begin(),
82 results_copy.end(),
83 expectations_copy.begin(),
84 ElementsEqual<T>).first,
85 results_copy.end());
88 } // anonymous namespace
90 class PersonalDataManagerTest : public testing::Test {
91 protected:
92 PersonalDataManagerTest() : autofill_table_(nullptr) {}
94 void SetUp() override {
95 prefs_ = test::PrefServiceForTesting();
96 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
97 base::FilePath path = temp_dir_.path().AppendASCII("TestWebDB");
98 web_database_ =
99 new WebDatabaseService(path, base::ThreadTaskRunnerHandle::Get(),
100 base::ThreadTaskRunnerHandle::Get());
102 // Setup account tracker.
103 signin_client_.reset(new TestSigninClient(prefs_.get()));
104 account_tracker_.reset(new AccountTrackerService());
105 account_tracker_->Initialize(signin_client_.get());
107 // Hacky: hold onto a pointer but pass ownership.
108 autofill_table_ = new AutofillTable;
109 web_database_->AddTable(scoped_ptr<WebDatabaseTable>(autofill_table_));
110 web_database_->LoadDatabase();
111 autofill_database_service_ = new AutofillWebDataService(
112 web_database_, base::ThreadTaskRunnerHandle::Get(),
113 base::ThreadTaskRunnerHandle::Get(),
114 WebDataServiceBase::ProfileErrorCallback());
115 autofill_database_service_->Init();
117 test::DisableSystemServices(prefs_.get());
118 ResetPersonalDataManager(USER_MODE_NORMAL);
121 void TearDown() override {
122 // Order of destruction is important as AutofillManager relies on
123 // PersonalDataManager to be around when it gets destroyed.
124 account_tracker_->Shutdown();
125 account_tracker_.reset();
126 signin_client_.reset();
129 void ResetPersonalDataManager(UserMode user_mode) {
130 bool is_incognito = (user_mode == USER_MODE_INCOGNITO);
131 personal_data_.reset(new PersonalDataManager("en"));
132 personal_data_->Init(
133 scoped_refptr<AutofillWebDataService>(autofill_database_service_),
134 prefs_.get(),
135 account_tracker_.get(),
136 is_incognito);
137 personal_data_->AddObserver(&personal_data_observer_);
139 // Verify that the web database has been updated and the notification sent.
140 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
141 .WillOnce(QuitMainMessageLoop());
142 base::MessageLoop::current()->Run();
145 void EnableWalletCardImport() {
146 prefs_->SetBoolean(prefs::kAutofillWalletSyncExperimentEnabled, true);
147 std::string account_id =
148 account_tracker_->SeedAccountInfo("12345", "syncuser@example.com");
149 prefs_->SetString(::prefs::kGoogleServicesAccountId, account_id);
150 base::CommandLine::ForCurrentProcess()->AppendSwitch(
151 switches::kEnableOfferStoreUnmaskedWalletCards);
154 void SetupReferenceProfile() {
155 ASSERT_EQ(0U, personal_data_->GetProfiles().size());
157 AutofillProfile profile(base::GenerateGUID(), "https://www.example.com");
158 test::SetProfileInfo(&profile, "Marion", "Mitchell", "Morrison",
159 "johnwayne@me.xyz", "Fox", "123 Zoo St", "unit 5",
160 "Hollywood", "CA", "91601", "US", "12345678910");
161 personal_data_->AddProfile(profile);
163 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
164 .WillOnce(QuitMainMessageLoop());
165 base::MessageLoop::current()->Run();
167 ASSERT_EQ(1U, personal_data_->GetProfiles().size());
170 // The temporary directory should be deleted at the end to ensure that
171 // files are not used anymore and deletion succeeds.
172 base::ScopedTempDir temp_dir_;
173 base::MessageLoopForUI message_loop_;
174 scoped_ptr<PrefService> prefs_;
175 scoped_ptr<AccountTrackerService> account_tracker_;
176 scoped_ptr<TestSigninClient> signin_client_;
177 scoped_refptr<AutofillWebDataService> autofill_database_service_;
178 scoped_refptr<WebDatabaseService> web_database_;
179 AutofillTable* autofill_table_; // weak ref
180 PersonalDataLoadedObserverMock personal_data_observer_;
181 scoped_ptr<PersonalDataManager> personal_data_;
184 TEST_F(PersonalDataManagerTest, AddProfile) {
185 // Add profile0 to the database.
186 AutofillProfile profile0(test::GetFullProfile());
187 profile0.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("j@s.com"));
188 personal_data_->AddProfile(profile0);
190 // Reload the database.
191 ResetPersonalDataManager(USER_MODE_NORMAL);
193 // Verify the addition.
194 const std::vector<AutofillProfile*>& results1 = personal_data_->GetProfiles();
195 ASSERT_EQ(1U, results1.size());
196 EXPECT_EQ(0, profile0.Compare(*results1[0]));
198 // Add profile with identical values. Duplicates should not get saved.
199 AutofillProfile profile0a = profile0;
200 profile0a.set_guid(base::GenerateGUID());
201 personal_data_->AddProfile(profile0a);
203 // Reload the database.
204 ResetPersonalDataManager(USER_MODE_NORMAL);
206 // Verify the non-addition.
207 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
208 ASSERT_EQ(1U, results2.size());
209 EXPECT_EQ(0, profile0.Compare(*results2[0]));
211 // New profile with different email.
212 AutofillProfile profile1 = profile0;
213 profile1.set_guid(base::GenerateGUID());
214 profile1.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("john@smith.com"));
216 // Add the different profile. This should save as a separate profile.
217 // Note that if this same profile was "merged" it would collapse to one
218 // profile with a multi-valued entry for email.
219 personal_data_->AddProfile(profile1);
221 // Reload the database.
222 ResetPersonalDataManager(USER_MODE_NORMAL);
224 // Verify the addition.
225 std::vector<AutofillProfile*> profiles;
226 profiles.push_back(&profile0);
227 profiles.push_back(&profile1);
228 ExpectSameElements(profiles, personal_data_->GetProfiles());
231 TEST_F(PersonalDataManagerTest, DontDuplicateServerProfile) {
232 EnableWalletCardImport();
234 std::vector<AutofillProfile> server_profiles;
235 server_profiles.push_back(
236 AutofillProfile(AutofillProfile::SERVER_PROFILE, "a123"));
237 test::SetProfileInfo(&server_profiles.back(), "John", "", "Doe", "",
238 "ACME Corp", "500 Oak View", "Apt 8", "Houston", "TX",
239 "77401", "US", "");
240 // Wallet only provides a full name, so the above first and last names
241 // will be ignored when the profile is written to the DB.
242 server_profiles.back().SetRawInfo(NAME_FULL, ASCIIToUTF16("John Doe"));
243 autofill_table_->SetServerProfiles(server_profiles);
244 personal_data_->Refresh();
245 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
246 .WillOnce(QuitMainMessageLoop());
247 base::MessageLoop::current()->Run();
248 EXPECT_EQ(1U, personal_data_->GetProfiles().size());
250 // Add profile with identical values. Duplicates should not get saved.
251 AutofillProfile scraped_profile(base::GenerateGUID(),
252 "https://www.example.com");
253 test::SetProfileInfo(&scraped_profile, "John", "", "Doe", "", "ACME Corp",
254 "500 Oak View", "Apt 8", "Houston", "TX", "77401", "US",
255 "");
256 EXPECT_TRUE(scraped_profile.IsSubsetOf(server_profiles.back(), "en-US"));
257 std::string saved_guid = personal_data_->SaveImportedProfile(scraped_profile);
258 EXPECT_NE(scraped_profile.guid(), saved_guid);
260 personal_data_->Refresh();
261 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
262 .WillOnce(QuitMainMessageLoop());
263 base::MessageLoop::current()->Run();
265 // Verify the non-addition.
266 EXPECT_EQ(1U, personal_data_->GetProfiles().size());
267 EXPECT_EQ(0U, personal_data_->web_profiles().size());
270 TEST_F(PersonalDataManagerTest, AddUpdateRemoveProfiles) {
271 AutofillProfile profile0(base::GenerateGUID(), "https://www.example.com");
272 test::SetProfileInfo(&profile0,
273 "Marion", "Mitchell", "Morrison",
274 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
275 "91601", "US", "12345678910");
277 AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com");
278 test::SetProfileInfo(&profile1,
279 "Josephine", "Alicia", "Saenz",
280 "joewayne@me.xyz", "Fox", "903 Apple Ct.", NULL, "Orlando", "FL", "32801",
281 "US", "19482937549");
283 AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com");
284 test::SetProfileInfo(&profile2,
285 "Josephine", "Alicia", "Saenz",
286 "joewayne@me.xyz", "Fox", "1212 Center.", "Bld. 5", "Orlando", "FL",
287 "32801", "US", "19482937549");
289 // Add two test profiles to the database.
290 personal_data_->AddProfile(profile0);
291 personal_data_->AddProfile(profile1);
293 // Verify that the web database has been updated and the notification sent.
294 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
295 .WillOnce(QuitMainMessageLoop());
296 base::MessageLoop::current()->Run();
298 std::vector<AutofillProfile*> profiles;
299 profiles.push_back(&profile0);
300 profiles.push_back(&profile1);
301 ExpectSameElements(profiles, personal_data_->GetProfiles());
303 // Update, remove, and add.
304 profile0.SetRawInfo(NAME_FIRST, ASCIIToUTF16("John"));
305 personal_data_->UpdateProfile(profile0);
306 personal_data_->RemoveByGUID(profile1.guid());
307 personal_data_->AddProfile(profile2);
309 // Verify that the web database has been updated and the notification sent.
310 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
311 .WillOnce(QuitMainMessageLoop());
312 base::MessageLoop::current()->Run();
314 profiles.clear();
315 profiles.push_back(&profile0);
316 profiles.push_back(&profile2);
317 ExpectSameElements(profiles, personal_data_->GetProfiles());
319 // Reset the PersonalDataManager. This tests that the personal data was saved
320 // to the web database, and that we can load the profiles from the web
321 // database.
322 ResetPersonalDataManager(USER_MODE_NORMAL);
324 // Verify that we've loaded the profiles from the web database.
325 ExpectSameElements(profiles, personal_data_->GetProfiles());
328 TEST_F(PersonalDataManagerTest, AddUpdateRemoveCreditCards) {
329 CreditCard credit_card0(base::GenerateGUID(), "https://www.example.com");
330 test::SetCreditCardInfo(&credit_card0,
331 "John Dillinger", "423456789012" /* Visa */, "01", "2010");
333 CreditCard credit_card1(base::GenerateGUID(), "https://www.example.com");
334 test::SetCreditCardInfo(&credit_card1,
335 "Bonnie Parker", "518765432109" /* Mastercard */, "12", "2012");
337 CreditCard credit_card2(base::GenerateGUID(), "https://www.example.com");
338 test::SetCreditCardInfo(&credit_card2,
339 "Clyde Barrow", "347666888555" /* American Express */, "04", "2015");
341 // Add two test credit cards to the database.
342 personal_data_->AddCreditCard(credit_card0);
343 personal_data_->AddCreditCard(credit_card1);
345 // Verify that the web database has been updated and the notification sent.
346 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
347 .WillOnce(QuitMainMessageLoop());
348 base::MessageLoop::current()->Run();
350 std::vector<CreditCard*> cards;
351 cards.push_back(&credit_card0);
352 cards.push_back(&credit_card1);
353 ExpectSameElements(cards, personal_data_->GetCreditCards());
355 // Update, remove, and add.
356 credit_card0.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Joe"));
357 personal_data_->UpdateCreditCard(credit_card0);
358 personal_data_->RemoveByGUID(credit_card1.guid());
359 personal_data_->AddCreditCard(credit_card2);
361 // Verify that the web database has been updated and the notification sent.
362 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
363 .WillOnce(QuitMainMessageLoop());
364 base::MessageLoop::current()->Run();
366 cards.clear();
367 cards.push_back(&credit_card0);
368 cards.push_back(&credit_card2);
369 ExpectSameElements(cards, personal_data_->GetCreditCards());
371 // Reset the PersonalDataManager. This tests that the personal data was saved
372 // to the web database, and that we can load the credit cards from the web
373 // database.
374 ResetPersonalDataManager(USER_MODE_NORMAL);
376 // Verify that we've loaded the credit cards from the web database.
377 cards.clear();
378 cards.push_back(&credit_card0);
379 cards.push_back(&credit_card2);
380 ExpectSameElements(cards, personal_data_->GetCreditCards());
383 TEST_F(PersonalDataManagerTest, UpdateUnverifiedProfilesAndCreditCards) {
384 // Start with unverified data.
385 AutofillProfile profile(base::GenerateGUID(), "https://www.example.com/");
386 test::SetProfileInfo(&profile,
387 "Marion", "Mitchell", "Morrison",
388 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
389 "91601", "US", "12345678910");
390 EXPECT_FALSE(profile.IsVerified());
392 CreditCard credit_card(base::GenerateGUID(), "https://www.example.com/");
393 test::SetCreditCardInfo(&credit_card,
394 "John Dillinger", "423456789012" /* Visa */, "01", "2010");
395 EXPECT_FALSE(credit_card.IsVerified());
397 // Add the data to the database.
398 personal_data_->AddProfile(profile);
399 personal_data_->AddCreditCard(credit_card);
401 // Verify that the web database has been updated and the notification sent.
402 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
403 .WillOnce(QuitMainMessageLoop());
404 base::MessageLoop::current()->Run();
406 const std::vector<AutofillProfile*>& profiles1 =
407 personal_data_->GetProfiles();
408 const std::vector<CreditCard*>& cards1 = personal_data_->GetCreditCards();
409 ASSERT_EQ(1U, profiles1.size());
410 ASSERT_EQ(1U, cards1.size());
411 EXPECT_EQ(0, profile.Compare(*profiles1[0]));
412 EXPECT_EQ(0, credit_card.Compare(*cards1[0]));
414 // Try to update with just the origin changed.
415 AutofillProfile original_profile(profile);
416 CreditCard original_credit_card(credit_card);
417 profile.set_origin("Chrome settings");
418 credit_card.set_origin("Chrome settings");
420 EXPECT_TRUE(profile.IsVerified());
421 EXPECT_TRUE(credit_card.IsVerified());
423 personal_data_->UpdateProfile(profile);
424 personal_data_->UpdateCreditCard(credit_card);
426 // Note: No refresh, as no update is expected.
428 const std::vector<AutofillProfile*>& profiles2 =
429 personal_data_->GetProfiles();
430 const std::vector<CreditCard*>& cards2 = personal_data_->GetCreditCards();
431 ASSERT_EQ(1U, profiles2.size());
432 ASSERT_EQ(1U, cards2.size());
433 EXPECT_NE(profile.origin(), profiles2[0]->origin());
434 EXPECT_NE(credit_card.origin(), cards2[0]->origin());
435 EXPECT_EQ(original_profile.origin(), profiles2[0]->origin());
436 EXPECT_EQ(original_credit_card.origin(), cards2[0]->origin());
438 // Try to update with data changed as well.
439 profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("John"));
440 credit_card.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Joe"));
442 personal_data_->UpdateProfile(profile);
443 personal_data_->UpdateCreditCard(credit_card);
445 // Verify that the web database has been updated and the notification sent.
446 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
447 .WillOnce(QuitMainMessageLoop());
448 base::MessageLoop::current()->Run();
450 const std::vector<AutofillProfile*>& profiles3 =
451 personal_data_->GetProfiles();
452 const std::vector<CreditCard*>& cards3 = personal_data_->GetCreditCards();
453 ASSERT_EQ(1U, profiles3.size());
454 ASSERT_EQ(1U, cards3.size());
455 EXPECT_EQ(0, profile.Compare(*profiles3[0]));
456 EXPECT_EQ(0, credit_card.Compare(*cards3[0]));
457 EXPECT_EQ(profile.origin(), profiles3[0]->origin());
458 EXPECT_EQ(credit_card.origin(), cards3[0]->origin());
461 // Tests that server cards are ignored without the flag.
462 TEST_F(PersonalDataManagerTest, ReturnsServerCreditCards) {
463 std::vector<CreditCard> server_cards;
464 server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "a123"));
465 test::SetCreditCardInfo(&server_cards.back(), "John Dillinger",
466 "9012" /* Visa */, "01", "2010");
467 server_cards.back().SetTypeForMaskedCard(kVisaCard);
469 server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "b456"));
470 test::SetCreditCardInfo(&server_cards.back(), "Bonnie Parker",
471 "2109" /* Mastercard */, "12", "2012");
472 server_cards.back().SetTypeForMaskedCard(kMasterCard);
474 test::SetServerCreditCards(autofill_table_, server_cards);
475 personal_data_->Refresh();
477 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
478 .WillOnce(QuitMainMessageLoop());
479 base::MessageLoop::current()->Run();
481 EXPECT_EQ(0U, personal_data_->GetCreditCards().size());
484 // Makes sure that full cards are re-masked when full PAN storage is off.
485 TEST_F(PersonalDataManagerTest, RefuseToStoreFullCard) {
486 prefs_->SetBoolean(prefs::kAutofillWalletSyncExperimentEnabled, true);
488 // On Linux this should be disabled automatically. Elsewhere, only if the
489 // flag is passed.
490 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
491 EXPECT_FALSE(base::CommandLine::ForCurrentProcess()->HasSwitch(
492 switches::kDisableOfferStoreUnmaskedWalletCards));
493 #else
494 base::CommandLine::ForCurrentProcess()->AppendSwitch(
495 switches::kDisableOfferStoreUnmaskedWalletCards);
496 #endif
498 std::vector<CreditCard> server_cards;
499 server_cards.push_back(CreditCard(CreditCard::FULL_SERVER_CARD, "c789"));
500 test::SetCreditCardInfo(&server_cards.back(), "Clyde Barrow",
501 "347666888555" /* American Express */, "04", "2015");
502 test::SetServerCreditCards(autofill_table_, server_cards);
503 personal_data_->Refresh();
505 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
506 .WillOnce(QuitMainMessageLoop());
507 base::MessageLoop::current()->Run();
509 ASSERT_EQ(1U, personal_data_->GetCreditCards().size());
510 EXPECT_EQ(CreditCard::MASKED_SERVER_CARD,
511 personal_data_->GetCreditCards()[0]->record_type());
514 TEST_F(PersonalDataManagerTest, OfferStoreUnmaskedCards) {
515 #if defined(OS_CHROMEOS) || defined(OS_WIN) || defined(OS_MACOSX) || \
516 defined(OS_IOS) || defined(OS_ANDROID)
517 bool should_offer = true;
518 #elif defined(OS_LINUX)
519 bool should_offer = false;
520 #endif
521 EXPECT_EQ(should_offer, OfferStoreUnmaskedCards());
524 // Tests that UpdateServerCreditCard can be used to mask or unmask server cards.
525 TEST_F(PersonalDataManagerTest, UpdateServerCreditCards) {
526 EnableWalletCardImport();
528 std::vector<CreditCard> server_cards;
529 server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "a123"));
530 test::SetCreditCardInfo(&server_cards.back(), "John Dillinger",
531 "9012" /* Visa */, "01", "2010");
532 server_cards.back().SetTypeForMaskedCard(kVisaCard);
534 server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "b456"));
535 test::SetCreditCardInfo(&server_cards.back(), "Bonnie Parker",
536 "2109" /* Mastercard */, "12", "2012");
537 server_cards.back().SetTypeForMaskedCard(kMasterCard);
539 server_cards.push_back(CreditCard(CreditCard::FULL_SERVER_CARD, "c789"));
540 test::SetCreditCardInfo(&server_cards.back(), "Clyde Barrow",
541 "347666888555" /* American Express */, "04", "2015");
543 test::SetServerCreditCards(autofill_table_, server_cards);
544 personal_data_->Refresh();
546 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
547 .WillOnce(QuitMainMessageLoop());
548 base::MessageLoop::current()->Run();
550 ASSERT_EQ(3U, personal_data_->GetCreditCards().size());
552 if (!OfferStoreUnmaskedCards()) {
553 for (CreditCard* card : personal_data_->GetCreditCards()) {
554 EXPECT_EQ(CreditCard::MASKED_SERVER_CARD, card->record_type());
556 // The rest of this test doesn't work if we're force-masking all unmasked
557 // cards.
558 return;
561 // The GUIDs will be different, so just compare the data.
562 for (size_t i = 0; i < 3; ++i)
563 EXPECT_EQ(0, server_cards[i].Compare(*personal_data_->GetCreditCards()[i]));
565 CreditCard* unmasked_card = &server_cards.front();
566 unmasked_card->set_record_type(CreditCard::FULL_SERVER_CARD);
567 unmasked_card->SetNumber(ASCIIToUTF16("423456789012"));
568 EXPECT_NE(0, server_cards.front().Compare(
569 *personal_data_->GetCreditCards().front()));
570 personal_data_->UpdateServerCreditCard(*unmasked_card);
572 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
573 .WillOnce(QuitMainMessageLoop());
574 base::MessageLoop::current()->Run();
576 for (size_t i = 0; i < 3; ++i)
577 EXPECT_EQ(0, server_cards[i].Compare(*personal_data_->GetCreditCards()[i]));
579 CreditCard* remasked_card = &server_cards.back();
580 remasked_card->set_record_type(CreditCard::MASKED_SERVER_CARD);
581 remasked_card->SetNumber(ASCIIToUTF16("8555"));
582 EXPECT_NE(
583 0, server_cards.back().Compare(*personal_data_->GetCreditCards().back()));
584 personal_data_->UpdateServerCreditCard(*remasked_card);
586 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
587 .WillOnce(QuitMainMessageLoop());
588 base::MessageLoop::current()->Run();
590 for (size_t i = 0; i < 3; ++i)
591 EXPECT_EQ(0, server_cards[i].Compare(*personal_data_->GetCreditCards()[i]));
594 TEST_F(PersonalDataManagerTest, AddProfilesAndCreditCards) {
595 AutofillProfile profile0(base::GenerateGUID(), "https://www.example.com");
596 test::SetProfileInfo(&profile0,
597 "Marion", "Mitchell", "Morrison",
598 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
599 "91601", "US", "12345678910");
601 AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com");
602 test::SetProfileInfo(&profile1,
603 "Josephine", "Alicia", "Saenz",
604 "joewayne@me.xyz", "Fox", "903 Apple Ct.", NULL, "Orlando", "FL", "32801",
605 "US", "19482937549");
607 CreditCard credit_card0(base::GenerateGUID(), "https://www.example.com");
608 test::SetCreditCardInfo(&credit_card0,
609 "John Dillinger", "423456789012" /* Visa */, "01", "2010");
611 CreditCard credit_card1(base::GenerateGUID(), "https://www.example.com");
612 test::SetCreditCardInfo(&credit_card1,
613 "Bonnie Parker", "518765432109" /* Mastercard */, "12", "2012");
615 // Add two test profiles to the database.
616 personal_data_->AddProfile(profile0);
617 personal_data_->AddProfile(profile1);
619 // Verify that the web database has been updated and the notification sent.
620 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
621 .WillOnce(QuitMainMessageLoop());
622 base::MessageLoop::current()->Run();
624 std::vector<AutofillProfile*> profiles;
625 profiles.push_back(&profile0);
626 profiles.push_back(&profile1);
627 ExpectSameElements(profiles, personal_data_->GetProfiles());
629 // Add two test credit cards to the database.
630 personal_data_->AddCreditCard(credit_card0);
631 personal_data_->AddCreditCard(credit_card1);
633 // Verify that the web database has been updated and the notification sent.
634 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
635 .WillOnce(QuitMainMessageLoop());
636 base::MessageLoop::current()->Run();
638 std::vector<CreditCard*> cards;
639 cards.push_back(&credit_card0);
640 cards.push_back(&credit_card1);
641 ExpectSameElements(cards, personal_data_->GetCreditCards());
643 // Determine uniqueness by inserting all of the GUIDs into a set and verifying
644 // the size of the set matches the number of GUIDs.
645 std::set<std::string> guids;
646 guids.insert(profile0.guid());
647 guids.insert(profile1.guid());
648 guids.insert(credit_card0.guid());
649 guids.insert(credit_card1.guid());
650 EXPECT_EQ(4U, guids.size());
653 // Test for http://crbug.com/50047. Makes sure that guids are populated
654 // correctly on load.
655 TEST_F(PersonalDataManagerTest, PopulateUniqueIDsOnLoad) {
656 AutofillProfile profile0(base::GenerateGUID(), "https://www.example.com");
657 test::SetProfileInfo(&profile0,
658 "y", "", "", "", "", "", "", "", "", "", "", "");
660 // Add the profile0 to the db.
661 personal_data_->AddProfile(profile0);
663 // Verify that the web database has been updated and the notification sent.
664 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
665 .WillOnce(QuitMainMessageLoop());
666 base::MessageLoop::current()->Run();
668 // Verify that we've loaded the profiles from the web database.
669 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
670 ASSERT_EQ(1U, results2.size());
671 EXPECT_EQ(0, profile0.Compare(*results2[0]));
673 // Add a new profile.
674 AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com");
675 test::SetProfileInfo(&profile1,
676 "z", "", "", "", "", "", "", "", "", "", "", "");
677 personal_data_->AddProfile(profile1);
679 // Verify that the web database has been updated and the notification sent.
680 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
681 .WillOnce(QuitMainMessageLoop());
682 base::MessageLoop::current()->Run();
684 // Make sure the two profiles have different GUIDs, both valid.
685 const std::vector<AutofillProfile*>& results3 = personal_data_->GetProfiles();
686 ASSERT_EQ(2U, results3.size());
687 EXPECT_NE(results3[0]->guid(), results3[1]->guid());
688 EXPECT_TRUE(base::IsValidGUID(results3[0]->guid()));
689 EXPECT_TRUE(base::IsValidGUID(results3[1]->guid()));
692 TEST_F(PersonalDataManagerTest, SetEmptyProfile) {
693 AutofillProfile profile0(base::GenerateGUID(), "https://www.example.com");
694 test::SetProfileInfo(&profile0,
695 "", "", "", "", "", "", "", "", "", "", "", "");
697 // Add the empty profile to the database.
698 personal_data_->AddProfile(profile0);
700 // Note: no refresh here.
702 // Reset the PersonalDataManager. This tests that the personal data was saved
703 // to the web database, and that we can load the profiles from the web
704 // database.
705 ResetPersonalDataManager(USER_MODE_NORMAL);
707 // Verify that we've loaded the profiles from the web database.
708 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
709 ASSERT_EQ(0U, results2.size());
712 TEST_F(PersonalDataManagerTest, SetEmptyCreditCard) {
713 CreditCard credit_card0(base::GenerateGUID(), "https://www.example.com");
714 test::SetCreditCardInfo(&credit_card0, "", "", "", "");
716 // Add the empty credit card to the database.
717 personal_data_->AddCreditCard(credit_card0);
719 // Note: no refresh here.
721 // Reset the PersonalDataManager. This tests that the personal data was saved
722 // to the web database, and that we can load the credit cards from the web
723 // database.
724 ResetPersonalDataManager(USER_MODE_NORMAL);
726 // Verify that we've loaded the credit cards from the web database.
727 const std::vector<CreditCard*>& results2 = personal_data_->GetCreditCards();
728 ASSERT_EQ(0U, results2.size());
731 TEST_F(PersonalDataManagerTest, Refresh) {
732 AutofillProfile profile0(base::GenerateGUID(), "https://www.example.com");
733 test::SetProfileInfo(&profile0,
734 "Marion", "Mitchell", "Morrison",
735 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
736 "91601", "US", "12345678910");
738 AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com");
739 test::SetProfileInfo(&profile1,
740 "Josephine", "Alicia", "Saenz",
741 "joewayne@me.xyz", "Fox", "903 Apple Ct.", NULL, "Orlando", "FL", "32801",
742 "US", "19482937549");
744 // Add the test profiles to the database.
745 personal_data_->AddProfile(profile0);
746 personal_data_->AddProfile(profile1);
748 // Verify that the web database has been updated and the notification sent.
749 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
750 .WillOnce(QuitMainMessageLoop());
751 base::MessageLoop::current()->Run();
753 std::vector<AutofillProfile*> profiles;
754 profiles.push_back(&profile0);
755 profiles.push_back(&profile1);
756 ExpectSameElements(profiles, personal_data_->GetProfiles());
758 AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com");
759 test::SetProfileInfo(&profile2,
760 "Josephine", "Alicia", "Saenz",
761 "joewayne@me.xyz", "Fox", "1212 Center.", "Bld. 5", "Orlando", "FL",
762 "32801", "US", "19482937549");
764 autofill_database_service_->AddAutofillProfile(profile2);
766 personal_data_->Refresh();
768 // Verify that the web database has been updated and the notification sent.
769 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
770 .WillOnce(QuitMainMessageLoop());
771 base::MessageLoop::current()->Run();
773 profiles.clear();
774 profiles.push_back(&profile0);
775 profiles.push_back(&profile1);
776 profiles.push_back(&profile2);
777 ExpectSameElements(profiles, personal_data_->GetProfiles());
779 autofill_database_service_->RemoveAutofillProfile(profile1.guid());
780 autofill_database_service_->RemoveAutofillProfile(profile2.guid());
782 // Before telling the PDM to refresh, simulate an edit to one of the deleted
783 // profiles via a SetProfile update (this would happen if the Autofill window
784 // was open with a previous snapshot of the profiles, and something
785 // [e.g. sync] removed a profile from the browser. In this edge case, we will
786 // end up in a consistent state by dropping the write).
787 profile0.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Mar"));
788 profile2.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Jo"));
789 personal_data_->UpdateProfile(profile0);
790 personal_data_->AddProfile(profile1);
791 personal_data_->AddProfile(profile2);
793 // Verify that the web database has been updated and the notification sent.
794 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
795 .WillOnce(QuitMainMessageLoop());
796 base::MessageLoop::current()->Run();
798 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
799 ASSERT_EQ(1U, results.size());
800 EXPECT_EQ(profile0, *results[0]);
803 TEST_F(PersonalDataManagerTest, ImportFormData) {
804 FormData form;
805 FormFieldData field;
806 test::CreateTestFormField(
807 "First name:", "first_name", "George", "text", &field);
808 form.fields.push_back(field);
809 test::CreateTestFormField(
810 "Last name:", "last_name", "Washington", "text", &field);
811 form.fields.push_back(field);
812 test::CreateTestFormField(
813 "Email:", "email", "theprez@gmail.com", "text", &field);
814 form.fields.push_back(field);
815 test::CreateTestFormField(
816 "Address:", "address1", "21 Laussat St", "text", &field);
817 form.fields.push_back(field);
818 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
819 form.fields.push_back(field);
820 test::CreateTestFormField("State:", "state", "California", "text", &field);
821 form.fields.push_back(field);
822 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
823 form.fields.push_back(field);
824 FormStructure form_structure(form);
825 form_structure.DetermineHeuristicTypes();
826 scoped_ptr<CreditCard> imported_credit_card;
827 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
828 &imported_credit_card));
829 ASSERT_FALSE(imported_credit_card);
831 // Verify that the web database has been updated and the notification sent.
832 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
833 .WillOnce(QuitMainMessageLoop());
834 base::MessageLoop::current()->Run();
836 AutofillProfile expected(base::GenerateGUID(), "https://www.example.com");
837 test::SetProfileInfo(&expected, "George", NULL,
838 "Washington", "theprez@gmail.com", NULL, "21 Laussat St", NULL,
839 "San Francisco", "California", "94102", NULL, NULL);
840 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
841 ASSERT_EQ(1U, results.size());
842 EXPECT_EQ(0, expected.Compare(*results[0]));
845 TEST_F(PersonalDataManagerTest, ImportFormDataBadEmail) {
846 FormData form;
847 FormFieldData field;
848 test::CreateTestFormField(
849 "First name:", "first_name", "George", "text", &field);
850 form.fields.push_back(field);
851 test::CreateTestFormField(
852 "Last name:", "last_name", "Washington", "text", &field);
853 form.fields.push_back(field);
854 test::CreateTestFormField("Email:", "email", "bogus", "text", &field);
855 form.fields.push_back(field);
856 test::CreateTestFormField(
857 "Address:", "address1", "21 Laussat St", "text", &field);
858 form.fields.push_back(field);
859 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
860 form.fields.push_back(field);
861 test::CreateTestFormField("State:", "state", "California", "text", &field);
862 form.fields.push_back(field);
863 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
864 form.fields.push_back(field);
865 FormStructure form_structure(form);
866 form_structure.DetermineHeuristicTypes();
867 scoped_ptr<CreditCard> imported_credit_card;
868 EXPECT_FALSE(personal_data_->ImportFormData(form_structure,
869 &imported_credit_card));
870 ASSERT_FALSE(imported_credit_card);
872 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
873 ASSERT_EQ(0U, results.size());
876 // Tests that a 'confirm email' field does not block profile import.
877 TEST_F(PersonalDataManagerTest, ImportFormDataTwoEmails) {
878 FormData form;
879 FormFieldData field;
880 test::CreateTestFormField(
881 "Name:", "name", "George Washington", "text", &field);
882 form.fields.push_back(field);
883 test::CreateTestFormField(
884 "Address:", "address1", "21 Laussat St", "text", &field);
885 form.fields.push_back(field);
886 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
887 form.fields.push_back(field);
888 test::CreateTestFormField("State:", "state", "California", "text", &field);
889 form.fields.push_back(field);
890 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
891 form.fields.push_back(field);
892 test::CreateTestFormField(
893 "Email:", "email", "example@example.com", "text", &field);
894 form.fields.push_back(field);
895 test::CreateTestFormField(
896 "Confirm email:", "confirm_email", "example@example.com", "text", &field);
897 form.fields.push_back(field);
898 FormStructure form_structure(form);
899 form_structure.DetermineHeuristicTypes();
900 scoped_ptr<CreditCard> imported_credit_card;
901 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
902 &imported_credit_card));
903 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
904 ASSERT_EQ(1U, results.size());
907 // Tests two email fields containing different values blocks provile import.
908 TEST_F(PersonalDataManagerTest, ImportFormDataTwoDifferentEmails) {
909 FormData form;
910 FormFieldData field;
911 test::CreateTestFormField(
912 "Name:", "name", "George Washington", "text", &field);
913 form.fields.push_back(field);
914 test::CreateTestFormField(
915 "Address:", "address1", "21 Laussat St", "text", &field);
916 form.fields.push_back(field);
917 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
918 form.fields.push_back(field);
919 test::CreateTestFormField("State:", "state", "California", "text", &field);
920 form.fields.push_back(field);
921 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
922 form.fields.push_back(field);
923 test::CreateTestFormField(
924 "Email:", "email", "example@example.com", "text", &field);
925 form.fields.push_back(field);
926 test::CreateTestFormField(
927 "Email:", "email2", "example2@example.com", "text", &field);
928 form.fields.push_back(field);
929 FormStructure form_structure(form);
930 form_structure.DetermineHeuristicTypes();
931 scoped_ptr<CreditCard> imported_credit_card;
932 EXPECT_FALSE(personal_data_->ImportFormData(form_structure,
933 &imported_credit_card));
934 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
935 ASSERT_EQ(0U, results.size());
938 TEST_F(PersonalDataManagerTest, ImportFormDataNotEnoughFilledFields) {
939 FormData form;
940 FormFieldData field;
941 test::CreateTestFormField(
942 "First name:", "first_name", "George", "text", &field);
943 form.fields.push_back(field);
944 test::CreateTestFormField(
945 "Last name:", "last_name", "Washington", "text", &field);
946 form.fields.push_back(field);
947 test::CreateTestFormField(
948 "Card number:", "card_number", "4111 1111 1111 1111", "text", &field);
949 form.fields.push_back(field);
950 FormStructure form_structure(form);
951 form_structure.DetermineHeuristicTypes();
952 scoped_ptr<CreditCard> imported_credit_card;
953 EXPECT_FALSE(personal_data_->ImportFormData(form_structure,
954 &imported_credit_card));
955 ASSERT_FALSE(imported_credit_card);
957 const std::vector<AutofillProfile*>& profiles = personal_data_->GetProfiles();
958 ASSERT_EQ(0U, profiles.size());
959 const std::vector<CreditCard*>& cards = personal_data_->GetCreditCards();
960 ASSERT_EQ(0U, cards.size());
963 TEST_F(PersonalDataManagerTest, ImportFormMinimumAddressUSA) {
964 // United States addresses must specifiy one address line, a city, state and
965 // zip code.
966 FormData form;
967 FormFieldData field;
968 test::CreateTestFormField("Name:", "name", "Barack Obama", "text", &field);
969 form.fields.push_back(field);
970 test::CreateTestFormField(
971 "Address:", "address", "1600 Pennsylvania Avenue", "text", &field);
972 form.fields.push_back(field);
973 test::CreateTestFormField("City:", "city", "Washington", "text", &field);
974 form.fields.push_back(field);
975 test::CreateTestFormField("State:", "state", "DC", "text", &field);
976 form.fields.push_back(field);
977 test::CreateTestFormField("Zip:", "zip", "20500", "text", &field);
978 form.fields.push_back(field);
979 test::CreateTestFormField("Country:", "country", "USA", "text", &field);
980 form.fields.push_back(field);
981 FormStructure form_structure(form);
982 form_structure.DetermineHeuristicTypes();
983 scoped_ptr<CreditCard> imported_credit_card;
984 EXPECT_TRUE(
985 personal_data_->ImportFormData(form_structure, &imported_credit_card));
986 const std::vector<AutofillProfile*>& profiles = personal_data_->GetProfiles();
987 ASSERT_EQ(1U, profiles.size());
990 TEST_F(PersonalDataManagerTest, ImportFormMinimumAddressGB) {
991 // British addresses do not require a state/province as the county is usually
992 // not requested on forms.
993 FormData form;
994 FormFieldData field;
995 test::CreateTestFormField("Name:", "name", "David Cameron", "text", &field);
996 form.fields.push_back(field);
997 test::CreateTestFormField(
998 "Address:", "address", "10 Downing Street", "text", &field);
999 form.fields.push_back(field);
1000 test::CreateTestFormField("City:", "city", "London", "text", &field);
1001 form.fields.push_back(field);
1002 test::CreateTestFormField(
1003 "Postcode:", "postcode", "SW1A 2AA", "text", &field);
1004 form.fields.push_back(field);
1005 test::CreateTestFormField(
1006 "Country:", "country", "United Kingdom", "text", &field);
1007 form.fields.push_back(field);
1008 FormStructure form_structure(form);
1009 form_structure.DetermineHeuristicTypes();
1010 scoped_ptr<CreditCard> imported_credit_card;
1011 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
1012 &imported_credit_card));
1013 const std::vector<AutofillProfile*>& profiles = personal_data_->GetProfiles();
1014 ASSERT_EQ(1U, profiles.size());
1017 TEST_F(PersonalDataManagerTest, ImportFormMinimumAddressGI) {
1018 // Gibraltar has the most minimal set of requirements for a valid address.
1019 // There are no cities or provinces and no postal/zip code system.
1020 FormData form;
1021 FormFieldData field;
1022 test::CreateTestFormField(
1023 "Name:", "name", "Sir Adrian Johns", "text", &field);
1024 form.fields.push_back(field);
1025 test::CreateTestFormField(
1026 "Address:", "address", "The Convent, Main Street", "text", &field);
1027 form.fields.push_back(field);
1028 test::CreateTestFormField("Country:", "country", "Gibraltar", "text", &field);
1029 form.fields.push_back(field);
1030 FormStructure form_structure(form);
1031 form_structure.DetermineHeuristicTypes();
1032 scoped_ptr<CreditCard> imported_credit_card;
1033 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
1034 &imported_credit_card));
1035 const std::vector<AutofillProfile*>& profiles = personal_data_->GetProfiles();
1036 ASSERT_EQ(1U, profiles.size());
1039 TEST_F(PersonalDataManagerTest, ImportPhoneNumberSplitAcrossMultipleFields) {
1040 FormData form;
1041 FormFieldData field;
1042 test::CreateTestFormField(
1043 "First name:", "first_name", "George", "text", &field);
1044 form.fields.push_back(field);
1045 test::CreateTestFormField(
1046 "Last name:", "last_name", "Washington", "text", &field);
1047 form.fields.push_back(field);
1048 test::CreateTestFormField(
1049 "Phone #:", "home_phone_area_code", "650", "text", &field);
1050 field.max_length = 3;
1051 form.fields.push_back(field);
1052 test::CreateTestFormField(
1053 "Phone #:", "home_phone_prefix", "555", "text", &field);
1054 field.max_length = 3;
1055 form.fields.push_back(field);
1056 test::CreateTestFormField(
1057 "Phone #:", "home_phone_suffix", "0000", "text", &field);
1058 field.max_length = 4;
1059 form.fields.push_back(field);
1060 test::CreateTestFormField(
1061 "Address:", "address1", "21 Laussat St", "text", &field);
1062 form.fields.push_back(field);
1063 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
1064 form.fields.push_back(field);
1065 test::CreateTestFormField("State:", "state", "California", "text", &field);
1066 form.fields.push_back(field);
1067 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
1068 form.fields.push_back(field);
1069 FormStructure form_structure(form);
1070 form_structure.DetermineHeuristicTypes();
1071 scoped_ptr<CreditCard> imported_credit_card;
1072 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
1073 &imported_credit_card));
1074 ASSERT_FALSE(imported_credit_card);
1076 // Verify that the web database has been updated and the notification sent.
1077 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1078 .WillOnce(QuitMainMessageLoop());
1079 base::MessageLoop::current()->Run();
1081 AutofillProfile expected(base::GenerateGUID(), "https://www.example.com");
1082 test::SetProfileInfo(&expected, "George", NULL,
1083 "Washington", NULL, NULL, "21 Laussat St", NULL,
1084 "San Francisco", "California", "94102", NULL, "(650) 555-0000");
1085 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
1086 ASSERT_EQ(1U, results.size());
1087 EXPECT_EQ(0, expected.Compare(*results[0]));
1090 TEST_F(PersonalDataManagerTest, ImportFormDataMultilineAddress) {
1091 FormData form;
1092 FormFieldData field;
1093 test::CreateTestFormField(
1094 "First name:", "first_name", "George", "text", &field);
1095 form.fields.push_back(field);
1096 test::CreateTestFormField(
1097 "Last name:", "last_name", "Washington", "text", &field);
1098 form.fields.push_back(field);
1099 test::CreateTestFormField(
1100 "Email:", "email", "theprez@gmail.com", "text", &field);
1101 form.fields.push_back(field);
1102 test::CreateTestFormField(
1103 "Address:",
1104 "street_address",
1105 "21 Laussat St\n"
1106 "Apt. #42",
1107 "textarea",
1108 &field);
1109 form.fields.push_back(field);
1110 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
1111 form.fields.push_back(field);
1112 test::CreateTestFormField("State:", "state", "California", "text", &field);
1113 form.fields.push_back(field);
1114 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
1115 form.fields.push_back(field);
1116 FormStructure form_structure(form);
1117 form_structure.DetermineHeuristicTypes();
1118 scoped_ptr<CreditCard> imported_credit_card;
1119 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
1120 &imported_credit_card));
1121 ASSERT_FALSE(imported_credit_card);
1123 // Verify that the web database has been updated and the notification sent.
1124 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1125 .WillOnce(QuitMainMessageLoop());
1126 base::MessageLoop::current()->Run();
1128 AutofillProfile expected(base::GenerateGUID(), "https://www.example.com");
1129 test::SetProfileInfo(&expected, "George", NULL,
1130 "Washington", "theprez@gmail.com", NULL, "21 Laussat St", "Apt. #42",
1131 "San Francisco", "California", "94102", NULL, NULL);
1132 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
1133 ASSERT_EQ(1U, results.size());
1134 EXPECT_EQ(0, expected.Compare(*results[0]));
1137 TEST_F(PersonalDataManagerTest, SetUniqueCreditCardLabels) {
1138 CreditCard credit_card0(base::GenerateGUID(), "https://www.example.com");
1139 credit_card0.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("John"));
1140 CreditCard credit_card1(base::GenerateGUID(), "https://www.example.com");
1141 credit_card1.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Paul"));
1142 CreditCard credit_card2(base::GenerateGUID(), "https://www.example.com");
1143 credit_card2.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Ringo"));
1144 CreditCard credit_card3(base::GenerateGUID(), "https://www.example.com");
1145 credit_card3.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Other"));
1146 CreditCard credit_card4(base::GenerateGUID(), "https://www.example.com");
1147 credit_card4.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Ozzy"));
1148 CreditCard credit_card5(base::GenerateGUID(), "https://www.example.com");
1149 credit_card5.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Dio"));
1151 // Add the test credit cards to the database.
1152 personal_data_->AddCreditCard(credit_card0);
1153 personal_data_->AddCreditCard(credit_card1);
1154 personal_data_->AddCreditCard(credit_card2);
1155 personal_data_->AddCreditCard(credit_card3);
1156 personal_data_->AddCreditCard(credit_card4);
1157 personal_data_->AddCreditCard(credit_card5);
1159 // Reset the PersonalDataManager. This tests that the personal data was saved
1160 // to the web database, and that we can load the credit cards from the web
1161 // database.
1162 ResetPersonalDataManager(USER_MODE_NORMAL);
1164 std::vector<CreditCard*> cards;
1165 cards.push_back(&credit_card0);
1166 cards.push_back(&credit_card1);
1167 cards.push_back(&credit_card2);
1168 cards.push_back(&credit_card3);
1169 cards.push_back(&credit_card4);
1170 cards.push_back(&credit_card5);
1171 ExpectSameElements(cards, personal_data_->GetCreditCards());
1174 TEST_F(PersonalDataManagerTest, AggregateTwoDifferentProfiles) {
1175 FormData form1;
1176 FormFieldData field;
1177 test::CreateTestFormField(
1178 "First name:", "first_name", "George", "text", &field);
1179 form1.fields.push_back(field);
1180 test::CreateTestFormField(
1181 "Last name:", "last_name", "Washington", "text", &field);
1182 form1.fields.push_back(field);
1183 test::CreateTestFormField(
1184 "Email:", "email", "theprez@gmail.com", "text", &field);
1185 form1.fields.push_back(field);
1186 test::CreateTestFormField(
1187 "Address:", "address1", "21 Laussat St", "text", &field);
1188 form1.fields.push_back(field);
1189 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
1190 form1.fields.push_back(field);
1191 test::CreateTestFormField("State:", "state", "California", "text", &field);
1192 form1.fields.push_back(field);
1193 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
1194 form1.fields.push_back(field);
1196 FormStructure form_structure1(form1);
1197 form_structure1.DetermineHeuristicTypes();
1198 scoped_ptr<CreditCard> imported_credit_card;
1199 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1200 &imported_credit_card));
1201 ASSERT_FALSE(imported_credit_card);
1203 // Verify that the web database has been updated and the notification sent.
1204 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1205 .WillOnce(QuitMainMessageLoop());
1206 base::MessageLoop::current()->Run();
1208 AutofillProfile expected(base::GenerateGUID(), "https://www.example.com");
1209 test::SetProfileInfo(&expected, "George", NULL,
1210 "Washington", "theprez@gmail.com", NULL, "21 Laussat St", NULL,
1211 "San Francisco", "California", "94102", NULL, NULL);
1212 const std::vector<AutofillProfile*>& results1 = personal_data_->GetProfiles();
1213 ASSERT_EQ(1U, results1.size());
1214 EXPECT_EQ(0, expected.Compare(*results1[0]));
1216 // Now create a completely different profile.
1217 FormData form2;
1218 test::CreateTestFormField(
1219 "First name:", "first_name", "John", "text", &field);
1220 form2.fields.push_back(field);
1221 test::CreateTestFormField(
1222 "Last name:", "last_name", "Adams", "text", &field);
1223 form2.fields.push_back(field);
1224 test::CreateTestFormField(
1225 "Email:", "email", "second@gmail.com", "text", &field);
1226 form2.fields.push_back(field);
1227 test::CreateTestFormField(
1228 "Address:", "address1", "22 Laussat St", "text", &field);
1229 form2.fields.push_back(field);
1230 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
1231 form2.fields.push_back(field);
1232 test::CreateTestFormField("State:", "state", "California", "text", &field);
1233 form2.fields.push_back(field);
1234 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
1235 form2.fields.push_back(field);
1237 FormStructure form_structure2(form2);
1238 form_structure2.DetermineHeuristicTypes();
1239 EXPECT_TRUE(personal_data_->ImportFormData(form_structure2,
1240 &imported_credit_card));
1241 ASSERT_FALSE(imported_credit_card);
1243 // Verify that the web database has been updated and the notification sent.
1244 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1245 .WillOnce(QuitMainMessageLoop());
1246 base::MessageLoop::current()->Run();
1248 AutofillProfile expected2(base::GenerateGUID(), "https://www.example.com");
1249 test::SetProfileInfo(&expected2, "John", NULL,
1250 "Adams", "second@gmail.com", NULL, "22 Laussat St", NULL,
1251 "San Francisco", "California", "94102", NULL, NULL);
1252 std::vector<AutofillProfile*> profiles;
1253 profiles.push_back(&expected);
1254 profiles.push_back(&expected2);
1255 ExpectSameElements(profiles, personal_data_->GetProfiles());
1258 TEST_F(PersonalDataManagerTest, AggregateSameProfileWithConflict) {
1259 FormData form1;
1260 FormFieldData field;
1261 test::CreateTestFormField(
1262 "First name:", "first_name", "George", "text", &field);
1263 form1.fields.push_back(field);
1264 test::CreateTestFormField(
1265 "Last name:", "last_name", "Washington", "text", &field);
1266 form1.fields.push_back(field);
1267 test::CreateTestFormField(
1268 "Address:", "address", "1600 Pennsylvania Avenue", "text", &field);
1269 form1.fields.push_back(field);
1270 test::CreateTestFormField(
1271 "Address Line 2:", "address2", "Suite A", "text", &field);
1272 form1.fields.push_back(field);
1273 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
1274 form1.fields.push_back(field);
1275 test::CreateTestFormField("State:", "state", "California", "text", &field);
1276 form1.fields.push_back(field);
1277 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
1278 form1.fields.push_back(field);
1279 test::CreateTestFormField(
1280 "Email:", "email", "theprez@gmail.com", "text", &field);
1281 form1.fields.push_back(field);
1282 test::CreateTestFormField("Phone:", "phone", "6505556666", "text", &field);
1283 form1.fields.push_back(field);
1285 FormStructure form_structure1(form1);
1286 form_structure1.DetermineHeuristicTypes();
1287 scoped_ptr<CreditCard> imported_credit_card;
1288 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1289 &imported_credit_card));
1290 ASSERT_FALSE(imported_credit_card);
1292 // Verify that the web database has been updated and the notification sent.
1293 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1294 .WillOnce(QuitMainMessageLoop());
1295 base::MessageLoop::current()->Run();
1297 AutofillProfile expected(base::GenerateGUID(), "https://www.example.com");
1298 test::SetProfileInfo(&expected, "George", nullptr, "Washington",
1299 "theprez@gmail.com", nullptr, "1600 Pennsylvania Avenue",
1300 "Suite A", "San Francisco", "California", "94102",
1301 nullptr, "(650) 555-6666");
1302 const std::vector<AutofillProfile*>& results1 = personal_data_->GetProfiles();
1303 ASSERT_EQ(1U, results1.size());
1304 EXPECT_EQ(0, expected.Compare(*results1[0]));
1306 // Now create an updated profile.
1307 FormData form2;
1308 test::CreateTestFormField(
1309 "First name:", "first_name", "George", "text", &field);
1310 form2.fields.push_back(field);
1311 test::CreateTestFormField(
1312 "Last name:", "last_name", "Washington", "text", &field);
1313 form2.fields.push_back(field);
1314 test::CreateTestFormField(
1315 "Address:", "address", "1600 Pennsylvania Avenue", "text", &field);
1316 form2.fields.push_back(field);
1317 test::CreateTestFormField(
1318 "Address Line 2:", "address2", "Suite A", "text", &field);
1319 form2.fields.push_back(field);
1320 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
1321 form2.fields.push_back(field);
1322 test::CreateTestFormField("State:", "state", "California", "text", &field);
1323 form2.fields.push_back(field);
1324 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
1325 form2.fields.push_back(field);
1326 test::CreateTestFormField(
1327 "Email:", "email", "theprez@gmail.com", "text", &field);
1328 form2.fields.push_back(field);
1329 // Country gets added.
1330 test::CreateTestFormField("Country:", "country", "USA", "text", &field);
1331 form2.fields.push_back(field);
1332 // Same phone number with different formatting doesn't create a new profile.
1333 test::CreateTestFormField("Phone:", "phone", "650-555-6666", "text", &field);
1334 form2.fields.push_back(field);
1336 FormStructure form_structure2(form2);
1337 form_structure2.DetermineHeuristicTypes();
1338 EXPECT_TRUE(personal_data_->ImportFormData(form_structure2,
1339 &imported_credit_card));
1340 ASSERT_FALSE(imported_credit_card);
1342 // Verify that the web database has been updated and the notification sent.
1343 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1344 .WillOnce(QuitMainMessageLoop());
1345 base::MessageLoop::current()->Run();
1347 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
1349 // Phone formatting is updated. Also, country gets added.
1350 expected.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("650-555-6666"));
1351 expected.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"));
1352 ASSERT_EQ(1U, results2.size());
1353 EXPECT_EQ(0, expected.Compare(*results2[0]));
1356 TEST_F(PersonalDataManagerTest, AggregateProfileWithMissingInfoInOld) {
1357 FormData form1;
1358 FormFieldData field;
1359 test::CreateTestFormField(
1360 "First name:", "first_name", "George", "text", &field);
1361 form1.fields.push_back(field);
1362 test::CreateTestFormField(
1363 "Last name:", "last_name", "Washington", "text", &field);
1364 form1.fields.push_back(field);
1365 test::CreateTestFormField(
1366 "Address Line 1:", "address", "190 High Street", "text", &field);
1367 form1.fields.push_back(field);
1368 test::CreateTestFormField("City:", "city", "Philadelphia", "text", &field);
1369 form1.fields.push_back(field);
1370 test::CreateTestFormField("State:", "state", "Pennsylvania", "text", &field);
1371 form1.fields.push_back(field);
1372 test::CreateTestFormField("Zip:", "zipcode", "19106", "text", &field);
1373 form1.fields.push_back(field);
1375 FormStructure form_structure1(form1);
1376 form_structure1.DetermineHeuristicTypes();
1377 scoped_ptr<CreditCard> imported_credit_card;
1378 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1379 &imported_credit_card));
1380 EXPECT_FALSE(imported_credit_card);
1382 // Verify that the web database has been updated and the notification sent.
1383 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1384 .WillOnce(QuitMainMessageLoop());
1385 base::MessageLoop::current()->Run();
1387 AutofillProfile expected(base::GenerateGUID(), "https://www.example.com");
1388 test::SetProfileInfo(&expected, "George", NULL,
1389 "Washington", NULL, NULL, "190 High Street", NULL,
1390 "Philadelphia", "Pennsylvania", "19106", NULL, NULL);
1391 const std::vector<AutofillProfile*>& results1 = personal_data_->GetProfiles();
1392 ASSERT_EQ(1U, results1.size());
1393 EXPECT_EQ(0, expected.Compare(*results1[0]));
1395 // Submit a form with new data for the first profile.
1396 FormData form2;
1397 test::CreateTestFormField(
1398 "First name:", "first_name", "George", "text", &field);
1399 form2.fields.push_back(field);
1400 test::CreateTestFormField(
1401 "Last name:", "last_name", "Washington", "text", &field);
1402 form2.fields.push_back(field);
1403 test::CreateTestFormField(
1404 "Email:", "email", "theprez@gmail.com", "text", &field);
1405 form2.fields.push_back(field);
1406 test::CreateTestFormField(
1407 "Address Line 1:", "address", "190 High Street", "text", &field);
1408 form2.fields.push_back(field);
1409 test::CreateTestFormField("City:", "city", "Philadelphia", "text", &field);
1410 form2.fields.push_back(field);
1411 test::CreateTestFormField("State:", "state", "Pennsylvania", "text", &field);
1412 form2.fields.push_back(field);
1413 test::CreateTestFormField("Zip:", "zipcode", "19106", "text", &field);
1414 form2.fields.push_back(field);
1416 FormStructure form_structure2(form2);
1417 form_structure2.DetermineHeuristicTypes();
1418 EXPECT_TRUE(personal_data_->ImportFormData(form_structure2,
1419 &imported_credit_card));
1420 ASSERT_FALSE(imported_credit_card);
1422 // Verify that the web database has been updated and the notification sent.
1423 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1424 .WillOnce(QuitMainMessageLoop());
1425 base::MessageLoop::current()->Run();
1427 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
1429 AutofillProfile expected2(base::GenerateGUID(), "https://www.example.com");
1430 test::SetProfileInfo(&expected2, "George", NULL,
1431 "Washington", "theprez@gmail.com", NULL, "190 High Street", NULL,
1432 "Philadelphia", "Pennsylvania", "19106", NULL, NULL);
1433 ASSERT_EQ(1U, results2.size());
1434 EXPECT_EQ(0, expected2.Compare(*results2[0]));
1437 TEST_F(PersonalDataManagerTest, AggregateProfileWithMissingInfoInNew) {
1438 FormData form1;
1439 FormFieldData field;
1440 test::CreateTestFormField(
1441 "First name:", "first_name", "George", "text", &field);
1442 form1.fields.push_back(field);
1443 test::CreateTestFormField(
1444 "Last name:", "last_name", "Washington", "text", &field);
1445 form1.fields.push_back(field);
1446 test::CreateTestFormField(
1447 "Company:", "company", "Government", "text", &field);
1448 form1.fields.push_back(field);
1449 test::CreateTestFormField(
1450 "Email:", "email", "theprez@gmail.com", "text", &field);
1451 form1.fields.push_back(field);
1452 test::CreateTestFormField(
1453 "Address Line 1:", "address", "190 High Street", "text", &field);
1454 form1.fields.push_back(field);
1455 test::CreateTestFormField("City:", "city", "Philadelphia", "text", &field);
1456 form1.fields.push_back(field);
1457 test::CreateTestFormField("State:", "state", "Pennsylvania", "text", &field);
1458 form1.fields.push_back(field);
1459 test::CreateTestFormField("Zip:", "zipcode", "19106", "text", &field);
1460 form1.fields.push_back(field);
1462 FormStructure form_structure1(form1);
1463 form_structure1.DetermineHeuristicTypes();
1464 scoped_ptr<CreditCard> imported_credit_card;
1465 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1466 &imported_credit_card));
1467 ASSERT_FALSE(imported_credit_card);
1469 // Verify that the web database has been updated and the notification sent.
1470 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1471 .WillOnce(QuitMainMessageLoop());
1472 base::MessageLoop::current()->Run();
1474 AutofillProfile expected(base::GenerateGUID(), "https://www.example.com");
1475 test::SetProfileInfo(&expected, "George", NULL,
1476 "Washington", "theprez@gmail.com", "Government", "190 High Street", NULL,
1477 "Philadelphia", "Pennsylvania", "19106", NULL, NULL);
1478 const std::vector<AutofillProfile*>& results1 = personal_data_->GetProfiles();
1479 ASSERT_EQ(1U, results1.size());
1480 EXPECT_EQ(0, expected.Compare(*results1[0]));
1482 // Submit a form with new data for the first profile.
1483 FormData form2;
1484 test::CreateTestFormField(
1485 "First name:", "first_name", "George", "text", &field);
1486 form2.fields.push_back(field);
1487 test::CreateTestFormField(
1488 "Last name:", "last_name", "Washington", "text", &field);
1489 form2.fields.push_back(field);
1490 // Note missing Company field.
1491 test::CreateTestFormField(
1492 "Email:", "email", "theprez@gmail.com", "text", &field);
1493 form2.fields.push_back(field);
1494 test::CreateTestFormField(
1495 "Address Line 1:", "address", "190 High Street", "text", &field);
1496 form2.fields.push_back(field);
1497 test::CreateTestFormField("City:", "city", "Philadelphia", "text", &field);
1498 form2.fields.push_back(field);
1499 test::CreateTestFormField("State:", "state", "Pennsylvania", "text", &field);
1500 form2.fields.push_back(field);
1501 test::CreateTestFormField("Zip:", "zipcode", "19106", "text", &field);
1502 form2.fields.push_back(field);
1504 FormStructure form_structure2(form2);
1505 form_structure2.DetermineHeuristicTypes();
1506 EXPECT_TRUE(personal_data_->ImportFormData(form_structure2,
1507 &imported_credit_card));
1508 ASSERT_FALSE(imported_credit_card);
1510 // Verify that the web database has been updated and the notification sent.
1511 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1512 .WillOnce(QuitMainMessageLoop());
1513 base::MessageLoop::current()->Run();
1515 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
1517 // Expect no change.
1518 ASSERT_EQ(1U, results2.size());
1519 EXPECT_EQ(0, expected.Compare(*results2[0]));
1522 TEST_F(PersonalDataManagerTest, AggregateProfileWithInsufficientAddress) {
1523 FormData form1;
1524 FormFieldData field;
1525 test::CreateTestFormField(
1526 "First name:", "first_name", "George", "text", &field);
1527 form1.fields.push_back(field);
1528 test::CreateTestFormField(
1529 "Last name:", "last_name", "Washington", "text", &field);
1530 form1.fields.push_back(field);
1531 test::CreateTestFormField(
1532 "Company:", "company", "Government", "text", &field);
1533 form1.fields.push_back(field);
1534 test::CreateTestFormField(
1535 "Email:", "email", "theprez@gmail.com", "text", &field);
1536 form1.fields.push_back(field);
1537 test::CreateTestFormField(
1538 "Address Line 1:", "address", "190 High Street", "text", &field);
1539 form1.fields.push_back(field);
1540 test::CreateTestFormField("City:", "city", "Philadelphia", "text", &field);
1541 form1.fields.push_back(field);
1543 FormStructure form_structure1(form1);
1544 form_structure1.DetermineHeuristicTypes();
1545 scoped_ptr<CreditCard> imported_credit_card;
1546 EXPECT_FALSE(personal_data_->ImportFormData(form_structure1,
1547 &imported_credit_card));
1548 ASSERT_FALSE(imported_credit_card);
1550 // Since no refresh is expected, reload the data from the database to make
1551 // sure no changes were written out.
1552 ResetPersonalDataManager(USER_MODE_NORMAL);
1554 const std::vector<AutofillProfile*>& profiles = personal_data_->GetProfiles();
1555 ASSERT_EQ(0U, profiles.size());
1556 const std::vector<CreditCard*>& cards = personal_data_->GetCreditCards();
1557 ASSERT_EQ(0U, cards.size());
1560 TEST_F(PersonalDataManagerTest, AggregateTwoDifferentCreditCards) {
1561 FormData form1;
1563 // Start with a single valid credit card form.
1564 FormFieldData field;
1565 test::CreateTestFormField(
1566 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1567 form1.fields.push_back(field);
1568 test::CreateTestFormField(
1569 "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
1570 form1.fields.push_back(field);
1571 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
1572 form1.fields.push_back(field);
1573 test::CreateTestFormField("Exp Year:", "exp_year", "2011", "text", &field);
1574 form1.fields.push_back(field);
1576 FormStructure form_structure1(form1);
1577 form_structure1.DetermineHeuristicTypes();
1578 scoped_ptr<CreditCard> imported_credit_card;
1579 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1580 &imported_credit_card));
1581 ASSERT_TRUE(imported_credit_card);
1582 personal_data_->SaveImportedCreditCard(*imported_credit_card);
1584 // Verify that the web database has been updated and the notification sent.
1585 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1586 .WillOnce(QuitMainMessageLoop());
1587 base::MessageLoop::current()->Run();
1589 CreditCard expected(base::GenerateGUID(), "https://www.example.com");
1590 test::SetCreditCardInfo(&expected,
1591 "Biggie Smalls", "4111111111111111", "01", "2011");
1592 const std::vector<CreditCard*>& results = personal_data_->GetCreditCards();
1593 ASSERT_EQ(1U, results.size());
1594 EXPECT_EQ(0, expected.Compare(*results[0]));
1596 // Add a second different valid credit card.
1597 FormData form2;
1598 test::CreateTestFormField(
1599 "Name on card:", "name_on_card", "", "text", &field);
1600 form2.fields.push_back(field);
1601 test::CreateTestFormField(
1602 "Card Number:", "card_number", "5500 0000 0000 0004", "text", &field);
1603 form2.fields.push_back(field);
1604 test::CreateTestFormField("Exp Month:", "exp_month", "02", "text", &field);
1605 form2.fields.push_back(field);
1606 test::CreateTestFormField("Exp Year:", "exp_year", "2012", "text", &field);
1607 form2.fields.push_back(field);
1609 FormStructure form_structure2(form2);
1610 form_structure2.DetermineHeuristicTypes();
1611 EXPECT_TRUE(personal_data_->ImportFormData(form_structure2,
1612 &imported_credit_card));
1613 ASSERT_TRUE(imported_credit_card);
1614 personal_data_->SaveImportedCreditCard(*imported_credit_card);
1616 // Verify that the web database has been updated and the notification sent.
1617 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1618 .WillOnce(QuitMainMessageLoop());
1619 base::MessageLoop::current()->Run();
1621 CreditCard expected2(base::GenerateGUID(), "https://www.example.com");
1622 test::SetCreditCardInfo(&expected2, "", "5500000000000004", "02", "2012");
1623 std::vector<CreditCard*> cards;
1624 cards.push_back(&expected);
1625 cards.push_back(&expected2);
1626 ExpectSameElements(cards, personal_data_->GetCreditCards());
1629 TEST_F(PersonalDataManagerTest, AggregateCardsThatDuplicateServerCards) {
1630 // Add server cards.
1631 std::vector<CreditCard> server_cards;
1632 server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "a123"));
1633 test::SetCreditCardInfo(&server_cards.back(), "John Dillinger",
1634 "1111" /* Visa */, "01", "2010");
1635 server_cards.back().SetTypeForMaskedCard(kVisaCard);
1636 server_cards.push_back(CreditCard(CreditCard::FULL_SERVER_CARD, "c789"));
1637 test::SetCreditCardInfo(&server_cards.back(), "Clyde Barrow",
1638 "347666888555" /* American Express */, "04", "2015");
1639 test::SetServerCreditCards(autofill_table_, server_cards);
1641 FormData form1;
1643 // Type the same data as the masked card into a form.
1644 FormFieldData field;
1645 test::CreateTestFormField(
1646 "Name on card:", "name_on_card", "John Dillinger", "text", &field);
1647 form1.fields.push_back(field);
1648 test::CreateTestFormField(
1649 "Card Number:", "card_number", "4111111111111111", "text", &field);
1650 form1.fields.push_back(field);
1651 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
1652 form1.fields.push_back(field);
1653 test::CreateTestFormField("Exp Year:", "exp_year", "2010", "text", &field);
1654 form1.fields.push_back(field);
1656 // The card should be offered to be saved locally because it only matches the
1657 // masked card.
1658 FormStructure form_structure1(form1);
1659 form_structure1.DetermineHeuristicTypes();
1660 scoped_ptr<CreditCard> imported_credit_card;
1661 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1662 &imported_credit_card));
1663 ASSERT_TRUE(imported_credit_card);
1664 personal_data_->SaveImportedCreditCard(*imported_credit_card);
1666 // Verify that the web database has been updated and the notification sent.
1667 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1668 .WillOnce(QuitMainMessageLoop());
1669 base::MessageLoop::current()->Run();
1671 // Type the same data as the unmasked card into a form.
1672 FormData form2;
1673 test::CreateTestFormField(
1674 "Name on card:", "name_on_card", "Clyde Barrow", "text", &field);
1675 form2.fields.push_back(field);
1676 test::CreateTestFormField(
1677 "Card Number:", "card_number", "347666888555", "text", &field);
1678 form2.fields.push_back(field);
1679 test::CreateTestFormField("Exp Month:", "exp_month", "04", "text", &field);
1680 form2.fields.push_back(field);
1681 test::CreateTestFormField("Exp Year:", "exp_year", "2015", "text", &field);
1682 form2.fields.push_back(field);
1684 // The card should not be offered to be saved locally because it only matches
1685 // the masked card.
1686 FormStructure form_structure2(form2);
1687 form_structure2.DetermineHeuristicTypes();
1688 EXPECT_FALSE(personal_data_->ImportFormData(form_structure2,
1689 &imported_credit_card));
1690 ASSERT_FALSE(imported_credit_card);
1693 TEST_F(PersonalDataManagerTest, AggregateInvalidCreditCard) {
1694 FormData form1;
1696 // Start with a single valid credit card form.
1697 FormFieldData field;
1698 test::CreateTestFormField(
1699 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1700 form1.fields.push_back(field);
1701 test::CreateTestFormField(
1702 "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
1703 form1.fields.push_back(field);
1704 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
1705 form1.fields.push_back(field);
1706 test::CreateTestFormField("Exp Year:", "exp_year", "2011", "text", &field);
1707 form1.fields.push_back(field);
1709 FormStructure form_structure1(form1);
1710 form_structure1.DetermineHeuristicTypes();
1711 scoped_ptr<CreditCard> imported_credit_card;
1712 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1713 &imported_credit_card));
1714 ASSERT_TRUE(imported_credit_card);
1715 personal_data_->SaveImportedCreditCard(*imported_credit_card);
1717 // Verify that the web database has been updated and the notification sent.
1718 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1719 .WillOnce(QuitMainMessageLoop());
1720 base::MessageLoop::current()->Run();
1722 CreditCard expected(base::GenerateGUID(), "https://www.example.com");
1723 test::SetCreditCardInfo(&expected,
1724 "Biggie Smalls", "4111111111111111", "01", "2011");
1725 const std::vector<CreditCard*>& results = personal_data_->GetCreditCards();
1726 ASSERT_EQ(1U, results.size());
1727 EXPECT_EQ(0, expected.Compare(*results[0]));
1729 // Add a second different invalid credit card.
1730 FormData form2;
1731 test::CreateTestFormField(
1732 "Name on card:", "name_on_card", "Jim Johansen", "text", &field);
1733 form2.fields.push_back(field);
1734 test::CreateTestFormField(
1735 "Card Number:", "card_number", "1000000000000000", "text", &field);
1736 form2.fields.push_back(field);
1737 test::CreateTestFormField("Exp Month:", "exp_month", "02", "text", &field);
1738 form2.fields.push_back(field);
1739 test::CreateTestFormField("Exp Year:", "exp_year", "2012", "text", &field);
1740 form2.fields.push_back(field);
1742 FormStructure form_structure2(form2);
1743 form_structure2.DetermineHeuristicTypes();
1744 EXPECT_FALSE(personal_data_->ImportFormData(form_structure2,
1745 &imported_credit_card));
1746 ASSERT_FALSE(imported_credit_card);
1748 // Since no refresh is expected, reload the data from the database to make
1749 // sure no changes were written out.
1750 ResetPersonalDataManager(USER_MODE_NORMAL);
1752 const std::vector<CreditCard*>& results2 = personal_data_->GetCreditCards();
1753 ASSERT_EQ(1U, results2.size());
1754 EXPECT_EQ(0, expected.Compare(*results2[0]));
1757 TEST_F(PersonalDataManagerTest, AggregateSameCreditCardWithConflict) {
1758 FormData form1;
1760 // Start with a single valid credit card form.
1761 FormFieldData field;
1762 test::CreateTestFormField(
1763 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1764 form1.fields.push_back(field);
1765 test::CreateTestFormField(
1766 "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
1767 form1.fields.push_back(field);
1768 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
1769 form1.fields.push_back(field);
1770 test::CreateTestFormField("Exp Year:", "exp_year", "2011", "text", &field);
1771 form1.fields.push_back(field);
1773 FormStructure form_structure1(form1);
1774 form_structure1.DetermineHeuristicTypes();
1775 scoped_ptr<CreditCard> imported_credit_card;
1776 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1777 &imported_credit_card));
1778 ASSERT_TRUE(imported_credit_card);
1779 personal_data_->SaveImportedCreditCard(*imported_credit_card);
1781 // Verify that the web database has been updated and the notification sent.
1782 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1783 .WillOnce(QuitMainMessageLoop());
1784 base::MessageLoop::current()->Run();
1786 CreditCard expected(base::GenerateGUID(), "https://www.example.com");
1787 test::SetCreditCardInfo(&expected,
1788 "Biggie Smalls", "4111111111111111", "01", "2011");
1789 const std::vector<CreditCard*>& results = personal_data_->GetCreditCards();
1790 ASSERT_EQ(1U, results.size());
1791 EXPECT_EQ(0, expected.Compare(*results[0]));
1793 // Add a second different valid credit card where the year is different but
1794 // the credit card number matches.
1795 FormData form2;
1796 test::CreateTestFormField(
1797 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1798 form2.fields.push_back(field);
1799 test::CreateTestFormField(
1800 "Card Number:", "card_number", "4111 1111 1111 1111", "text", &field);
1801 form2.fields.push_back(field);
1802 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
1803 form2.fields.push_back(field);
1804 test::CreateTestFormField("Exp Year:", "exp_year", "2012", "text", &field);
1805 form2.fields.push_back(field);
1807 FormStructure form_structure2(form2);
1808 form_structure2.DetermineHeuristicTypes();
1809 EXPECT_TRUE(personal_data_->ImportFormData(form_structure2,
1810 &imported_credit_card));
1811 EXPECT_FALSE(imported_credit_card);
1813 // Verify that the web database has been updated and the notification sent.
1814 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1815 .WillOnce(QuitMainMessageLoop());
1816 base::MessageLoop::current()->Run();
1818 // Expect that the newer information is saved. In this case the year is
1819 // updated to "2012".
1820 CreditCard expected2(base::GenerateGUID(), "https://www.example.com");
1821 test::SetCreditCardInfo(&expected2,
1822 "Biggie Smalls", "4111111111111111", "01", "2012");
1823 const std::vector<CreditCard*>& results2 = personal_data_->GetCreditCards();
1824 ASSERT_EQ(1U, results2.size());
1825 EXPECT_EQ(0, expected2.Compare(*results2[0]));
1828 TEST_F(PersonalDataManagerTest, AggregateEmptyCreditCardWithConflict) {
1829 FormData form1;
1831 // Start with a single valid credit card form.
1832 FormFieldData field;
1833 test::CreateTestFormField(
1834 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1835 form1.fields.push_back(field);
1836 test::CreateTestFormField(
1837 "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
1838 form1.fields.push_back(field);
1839 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
1840 form1.fields.push_back(field);
1841 test::CreateTestFormField("Exp Year:", "exp_year", "2011", "text", &field);
1842 form1.fields.push_back(field);
1844 FormStructure form_structure1(form1);
1845 form_structure1.DetermineHeuristicTypes();
1846 scoped_ptr<CreditCard> imported_credit_card;
1847 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1848 &imported_credit_card));
1849 ASSERT_TRUE(imported_credit_card);
1850 personal_data_->SaveImportedCreditCard(*imported_credit_card);
1852 // Verify that the web database has been updated and the notification sent.
1853 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1854 .WillOnce(QuitMainMessageLoop());
1855 base::MessageLoop::current()->Run();
1857 CreditCard expected(base::GenerateGUID(), "https://www.example.com");
1858 test::SetCreditCardInfo(&expected,
1859 "Biggie Smalls", "4111111111111111", "01", "2011");
1860 const std::vector<CreditCard*>& results = personal_data_->GetCreditCards();
1861 ASSERT_EQ(1U, results.size());
1862 EXPECT_EQ(0, expected.Compare(*results[0]));
1864 // Add a second credit card with no number.
1865 FormData form2;
1866 test::CreateTestFormField(
1867 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1868 form2.fields.push_back(field);
1869 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
1870 form2.fields.push_back(field);
1871 test::CreateTestFormField("Exp Year:", "exp_year", "2012", "text", &field);
1872 form2.fields.push_back(field);
1874 FormStructure form_structure2(form2);
1875 form_structure2.DetermineHeuristicTypes();
1876 EXPECT_FALSE(personal_data_->ImportFormData(form_structure2,
1877 &imported_credit_card));
1878 EXPECT_FALSE(imported_credit_card);
1880 // Since no refresh is expected, reload the data from the database to make
1881 // sure no changes were written out.
1882 ResetPersonalDataManager(USER_MODE_NORMAL);
1884 // No change is expected.
1885 CreditCard expected2(base::GenerateGUID(), "https://www.example.com");
1886 test::SetCreditCardInfo(&expected2,
1887 "Biggie Smalls", "4111111111111111", "01", "2011");
1888 const std::vector<CreditCard*>& results2 = personal_data_->GetCreditCards();
1889 ASSERT_EQ(1U, results2.size());
1890 EXPECT_EQ(0, expected2.Compare(*results2[0]));
1893 TEST_F(PersonalDataManagerTest, AggregateCreditCardWithMissingInfoInNew) {
1894 FormData form1;
1896 // Start with a single valid credit card form.
1897 FormFieldData field;
1898 test::CreateTestFormField(
1899 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1900 form1.fields.push_back(field);
1901 test::CreateTestFormField(
1902 "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
1903 form1.fields.push_back(field);
1904 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
1905 form1.fields.push_back(field);
1906 test::CreateTestFormField("Exp Year:", "exp_year", "2011", "text", &field);
1907 form1.fields.push_back(field);
1909 FormStructure form_structure1(form1);
1910 form_structure1.DetermineHeuristicTypes();
1911 scoped_ptr<CreditCard> imported_credit_card;
1912 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1913 &imported_credit_card));
1914 ASSERT_TRUE(imported_credit_card);
1915 personal_data_->SaveImportedCreditCard(*imported_credit_card);
1917 // Verify that the web database has been updated and the notification sent.
1918 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1919 .WillOnce(QuitMainMessageLoop());
1920 base::MessageLoop::current()->Run();
1922 CreditCard expected(base::GenerateGUID(), "https://www.example.com");
1923 test::SetCreditCardInfo(&expected,
1924 "Biggie Smalls", "4111111111111111", "01", "2011");
1925 const std::vector<CreditCard*>& results = personal_data_->GetCreditCards();
1926 ASSERT_EQ(1U, results.size());
1927 EXPECT_EQ(0, expected.Compare(*results[0]));
1929 // Add a second different valid credit card where the name is missing but
1930 // the credit card number matches.
1931 FormData form2;
1932 // Note missing name.
1933 test::CreateTestFormField(
1934 "Card Number:", "card_number", "4111111111111111", "text", &field);
1935 form2.fields.push_back(field);
1936 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
1937 form2.fields.push_back(field);
1938 test::CreateTestFormField("Exp Year:", "exp_year", "2011", "text", &field);
1939 form2.fields.push_back(field);
1941 FormStructure form_structure2(form2);
1942 form_structure2.DetermineHeuristicTypes();
1943 EXPECT_TRUE(personal_data_->ImportFormData(form_structure2,
1944 &imported_credit_card));
1945 EXPECT_FALSE(imported_credit_card);
1947 // Since no refresh is expected, reload the data from the database to make
1948 // sure no changes were written out.
1949 ResetPersonalDataManager(USER_MODE_NORMAL);
1951 // No change is expected.
1952 CreditCard expected2(base::GenerateGUID(), "https://www.example.com");
1953 test::SetCreditCardInfo(&expected2,
1954 "Biggie Smalls", "4111111111111111", "01", "2011");
1955 const std::vector<CreditCard*>& results2 = personal_data_->GetCreditCards();
1956 ASSERT_EQ(1U, results2.size());
1957 EXPECT_EQ(0, expected2.Compare(*results2[0]));
1959 // Add a third credit card where the expiration date is missing.
1960 FormData form3;
1961 test::CreateTestFormField(
1962 "Name on card:", "name_on_card", "Johnny McEnroe", "text", &field);
1963 form3.fields.push_back(field);
1964 test::CreateTestFormField(
1965 "Card Number:", "card_number", "5555555555554444", "text", &field);
1966 form3.fields.push_back(field);
1967 // Note missing expiration month and year..
1969 FormStructure form_structure3(form3);
1970 form_structure3.DetermineHeuristicTypes();
1971 EXPECT_FALSE(personal_data_->ImportFormData(form_structure3,
1972 &imported_credit_card));
1973 ASSERT_FALSE(imported_credit_card);
1975 // Since no refresh is expected, reload the data from the database to make
1976 // sure no changes were written out.
1977 ResetPersonalDataManager(USER_MODE_NORMAL);
1979 // No change is expected.
1980 CreditCard expected3(base::GenerateGUID(), "https://www.example.com");
1981 test::SetCreditCardInfo(&expected3,
1982 "Biggie Smalls", "4111111111111111", "01", "2011");
1983 const std::vector<CreditCard*>& results3 = personal_data_->GetCreditCards();
1984 ASSERT_EQ(1U, results3.size());
1985 EXPECT_EQ(0, expected3.Compare(*results3[0]));
1988 TEST_F(PersonalDataManagerTest, AggregateCreditCardWithMissingInfoInOld) {
1989 // Start with a single valid credit card stored via the preferences.
1990 // Note the empty name.
1991 CreditCard saved_credit_card(base::GenerateGUID(), "https://www.example.com");
1992 test::SetCreditCardInfo(&saved_credit_card,
1993 "", "4111111111111111" /* Visa */, "01", "2011");
1994 personal_data_->AddCreditCard(saved_credit_card);
1996 // Verify that the web database has been updated and the notification sent.
1997 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1998 .WillOnce(QuitMainMessageLoop());
1999 base::MessageLoop::current()->Run();
2001 const std::vector<CreditCard*>& results1 = personal_data_->GetCreditCards();
2002 ASSERT_EQ(1U, results1.size());
2003 EXPECT_EQ(saved_credit_card, *results1[0]);
2006 // Add a second different valid credit card where the year is different but
2007 // the credit card number matches.
2008 FormData form;
2009 FormFieldData field;
2010 test::CreateTestFormField(
2011 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
2012 form.fields.push_back(field);
2013 test::CreateTestFormField(
2014 "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
2015 form.fields.push_back(field);
2016 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
2017 form.fields.push_back(field);
2018 test::CreateTestFormField("Exp Year:", "exp_year", "2012", "text", &field);
2019 form.fields.push_back(field);
2021 FormStructure form_structure(form);
2022 form_structure.DetermineHeuristicTypes();
2023 scoped_ptr<CreditCard> imported_credit_card;
2024 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
2025 &imported_credit_card));
2026 EXPECT_FALSE(imported_credit_card);
2028 // Verify that the web database has been updated and the notification sent.
2029 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2030 .WillOnce(QuitMainMessageLoop());
2031 base::MessageLoop::current()->Run();
2033 // Expect that the newer information is saved. In this case the year is
2034 // added to the existing credit card.
2035 CreditCard expected2(base::GenerateGUID(), "https://www.example.com");
2036 test::SetCreditCardInfo(&expected2,
2037 "Biggie Smalls", "4111111111111111", "01", "2012");
2038 const std::vector<CreditCard*>& results2 = personal_data_->GetCreditCards();
2039 ASSERT_EQ(1U, results2.size());
2040 EXPECT_EQ(0, expected2.Compare(*results2[0]));
2043 // We allow the user to store a credit card number with separators via the UI.
2044 // We should not try to re-aggregate the same card with the separators stripped.
2045 TEST_F(PersonalDataManagerTest, AggregateSameCreditCardWithSeparators) {
2046 // Start with a single valid credit card stored via the preferences.
2047 // Note the separators in the credit card number.
2048 CreditCard saved_credit_card(base::GenerateGUID(), "https://www.example.com");
2049 test::SetCreditCardInfo(&saved_credit_card,
2050 "Biggie Smalls", "4111 1111 1111 1111" /* Visa */, "01", "2011");
2051 personal_data_->AddCreditCard(saved_credit_card);
2053 // Verify that the web database has been updated and the notification sent.
2054 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2055 .WillOnce(QuitMainMessageLoop());
2056 base::MessageLoop::current()->Run();
2058 const std::vector<CreditCard*>& results1 = personal_data_->GetCreditCards();
2059 ASSERT_EQ(1U, results1.size());
2060 EXPECT_EQ(0, saved_credit_card.Compare(*results1[0]));
2062 // Import the same card info, but with different separators in the number.
2063 FormData form;
2064 FormFieldData field;
2065 test::CreateTestFormField(
2066 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
2067 form.fields.push_back(field);
2068 test::CreateTestFormField(
2069 "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
2070 form.fields.push_back(field);
2071 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
2072 form.fields.push_back(field);
2073 test::CreateTestFormField("Exp Year:", "exp_year", "2011", "text", &field);
2074 form.fields.push_back(field);
2076 FormStructure form_structure(form);
2077 form_structure.DetermineHeuristicTypes();
2078 scoped_ptr<CreditCard> imported_credit_card;
2079 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
2080 &imported_credit_card));
2081 EXPECT_FALSE(imported_credit_card);
2083 // Since no refresh is expected, reload the data from the database to make
2084 // sure no changes were written out.
2085 ResetPersonalDataManager(USER_MODE_NORMAL);
2087 // Expect that no new card is saved.
2088 const std::vector<CreditCard*>& results2 = personal_data_->GetCreditCards();
2089 ASSERT_EQ(1U, results2.size());
2090 EXPECT_EQ(0, saved_credit_card.Compare(*results2[0]));
2093 // Ensure that if a verified profile already exists, aggregated profiles cannot
2094 // modify it in any way. This also checks the profile merging/matching algorithm
2095 // works: if either the full name OR all the non-empty name pieces match, the
2096 // profile is a match.
2097 TEST_F(PersonalDataManagerTest, AggregateExistingVerifiedProfileWithConflict) {
2098 // Start with a verified profile.
2099 AutofillProfile profile(base::GenerateGUID(), "Chrome settings");
2100 test::SetProfileInfo(&profile,
2101 "Marion", "Mitchell", "Morrison",
2102 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
2103 "91601", "US", "12345678910");
2104 EXPECT_TRUE(profile.IsVerified());
2106 // Add the profile to the database.
2107 personal_data_->AddProfile(profile);
2109 // Verify that the web database has been updated and the notification sent.
2110 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2111 .WillOnce(QuitMainMessageLoop());
2112 base::MessageLoop::current()->Run();
2114 // Simulate a form submission with conflicting info.
2115 FormData form;
2116 FormFieldData field;
2117 test::CreateTestFormField("First name:", "first_name", "Marion Mitchell",
2118 "text", &field);
2119 form.fields.push_back(field);
2120 test::CreateTestFormField(
2121 "Last name:", "last_name", "Morrison", "text", &field);
2122 form.fields.push_back(field);
2123 test::CreateTestFormField("Email:", "email", "johnwayne@me.xyz", "text",
2124 &field);
2125 form.fields.push_back(field);
2126 test::CreateTestFormField(
2127 "Address:", "address1", "123 Zoo St.", "text", &field);
2128 form.fields.push_back(field);
2129 test::CreateTestFormField("City:", "city", "Hollywood", "text", &field);
2130 form.fields.push_back(field);
2131 test::CreateTestFormField("State:", "state", "CA", "text", &field);
2132 form.fields.push_back(field);
2133 test::CreateTestFormField("Zip:", "zip", "91601", "text", &field);
2134 form.fields.push_back(field);
2136 FormStructure form_structure(form);
2137 form_structure.DetermineHeuristicTypes();
2138 scoped_ptr<CreditCard> imported_credit_card;
2139 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
2140 &imported_credit_card));
2141 EXPECT_FALSE(imported_credit_card);
2143 // Wait for the refresh, which in this case is a no-op.
2144 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2145 .WillOnce(QuitMainMessageLoop());
2146 base::MessageLoop::current()->Run();
2148 // Expect that no new profile is saved.
2149 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
2150 ASSERT_EQ(1U, results.size());
2151 EXPECT_EQ(0, profile.Compare(*results[0]));
2153 // Try the same thing, but without "Mitchell". The profiles should still match
2154 // because the non empty name pieces (first and last) match that stored in the
2155 // profile.
2156 test::CreateTestFormField("First name:", "first_name", "Marion", "text",
2157 &field);
2158 form.fields[0] = field;
2160 FormStructure form_structure2(form);
2161 form_structure2.DetermineHeuristicTypes();
2162 EXPECT_TRUE(
2163 personal_data_->ImportFormData(form_structure2, &imported_credit_card));
2164 EXPECT_FALSE(imported_credit_card);
2166 // Wait for the refresh, which in this case is a no-op.
2167 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2168 .WillOnce(QuitMainMessageLoop());
2169 base::MessageLoop::current()->Run();
2171 // Expect that no new profile is saved.
2172 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
2173 ASSERT_EQ(1U, results2.size());
2174 EXPECT_EQ(0, profile.Compare(*results2[0]));
2177 // Ensure that if a verified credit card already exists, aggregated credit cards
2178 // cannot modify it in any way.
2179 TEST_F(PersonalDataManagerTest,
2180 AggregateExistingVerifiedCreditCardWithConflict) {
2181 // Start with a verified credit card.
2182 CreditCard credit_card(base::GenerateGUID(), "Chrome settings");
2183 test::SetCreditCardInfo(&credit_card,
2184 "Biggie Smalls", "4111 1111 1111 1111" /* Visa */, "01", "2011");
2185 EXPECT_TRUE(credit_card.IsVerified());
2187 // Add the credit card to the database.
2188 personal_data_->AddCreditCard(credit_card);
2190 // Verify that the web database has been updated and the notification sent.
2191 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2192 .WillOnce(QuitMainMessageLoop());
2193 base::MessageLoop::current()->Run();
2195 // Simulate a form submission with conflicting expiration year.
2196 FormData form;
2197 FormFieldData field;
2198 test::CreateTestFormField(
2199 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
2200 form.fields.push_back(field);
2201 test::CreateTestFormField(
2202 "Card Number:", "card_number", "4111 1111 1111 1111", "text", &field);
2203 form.fields.push_back(field);
2204 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
2205 form.fields.push_back(field);
2206 test::CreateTestFormField("Exp Year:", "exp_year", "2012", "text", &field);
2207 form.fields.push_back(field);
2209 FormStructure form_structure(form);
2210 form_structure.DetermineHeuristicTypes();
2211 scoped_ptr<CreditCard> imported_credit_card;
2212 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
2213 &imported_credit_card));
2214 ASSERT_FALSE(imported_credit_card);
2216 // Since no refresh is expected, reload the data from the database to make
2217 // sure no changes were written out.
2218 ResetPersonalDataManager(USER_MODE_NORMAL);
2220 // Expect that the saved credit card is not modified.
2221 const std::vector<CreditCard*>& results = personal_data_->GetCreditCards();
2222 ASSERT_EQ(1U, results.size());
2223 EXPECT_EQ(0, credit_card.Compare(*results[0]));
2226 // Ensure that verified profiles can be saved via SaveImportedProfile,
2227 // overwriting existing unverified profiles.
2228 TEST_F(PersonalDataManagerTest, SaveImportedProfileWithVerifiedData) {
2229 // Start with an unverified profile.
2230 AutofillProfile profile(base::GenerateGUID(), "https://www.example.com");
2231 test::SetProfileInfo(&profile,
2232 "Marion", "Mitchell", "Morrison",
2233 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
2234 "91601", "US", "12345678910");
2235 EXPECT_FALSE(profile.IsVerified());
2237 // Add the profile to the database.
2238 personal_data_->AddProfile(profile);
2240 // Verify that the web database has been updated and the notification sent.
2241 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2242 .WillOnce(QuitMainMessageLoop());
2243 base::MessageLoop::current()->Run();
2245 AutofillProfile new_verified_profile = profile;
2246 new_verified_profile.set_guid(base::GenerateGUID());
2247 new_verified_profile.set_origin("Chrome settings");
2248 new_verified_profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER,
2249 ASCIIToUTF16("1 234 567-8910"));
2250 EXPECT_TRUE(new_verified_profile.IsVerified());
2252 personal_data_->SaveImportedProfile(new_verified_profile);
2254 // Verify that the web database has been updated and the notification sent.
2255 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2256 .WillOnce(QuitMainMessageLoop());
2257 base::MessageLoop::current()->Run();
2259 // The new profile should be merged into the existing one.
2260 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
2261 ASSERT_EQ(1U, results.size());
2262 EXPECT_EQ(0, new_verified_profile.Compare(*results[0]));
2265 // Ensure that verified profiles can be saved via SaveImportedProfile,
2266 // overwriting existing verified profiles as well.
2267 TEST_F(PersonalDataManagerTest, SaveImportedProfileWithExistingVerifiedData) {
2268 // Start with a verified profile.
2269 AutofillProfile profile(base::GenerateGUID(), "Chrome settings");
2270 test::SetProfileInfo(&profile,
2271 "Marion", "Mitchell", "Morrison",
2272 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
2273 "91601", "US", "12345678910");
2274 EXPECT_TRUE(profile.IsVerified());
2276 // Add the profile to the database.
2277 personal_data_->AddProfile(profile);
2279 // Verify that the web database has been updated and the notification sent.
2280 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2281 .WillOnce(QuitMainMessageLoop());
2282 base::MessageLoop::current()->Run();
2284 AutofillProfile new_verified_profile = profile;
2285 new_verified_profile.set_guid(base::GenerateGUID());
2286 new_verified_profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER,
2287 ASCIIToUTF16("1 234 567-8910"));
2288 EXPECT_TRUE(new_verified_profile.IsVerified());
2290 personal_data_->SaveImportedProfile(new_verified_profile);
2292 // Verify that the web database has been updated and the notification sent.
2293 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2294 .WillOnce(QuitMainMessageLoop());
2295 base::MessageLoop::current()->Run();
2297 // The new profile should be merged into the existing one.
2298 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
2299 ASSERT_EQ(1U, results.size());
2300 EXPECT_EQ(0, new_verified_profile.Compare(*results[0]));
2303 // Ensure that verified credit cards can be saved via SaveImportedCreditCard.
2304 TEST_F(PersonalDataManagerTest, SaveImportedCreditCardWithVerifiedData) {
2305 // Start with a verified credit card.
2306 CreditCard credit_card(base::GenerateGUID(), "Chrome settings");
2307 test::SetCreditCardInfo(&credit_card,
2308 "Biggie Smalls", "4111 1111 1111 1111" /* Visa */, "01", "2011");
2309 EXPECT_TRUE(credit_card.IsVerified());
2311 // Add the credit card to the database.
2312 personal_data_->AddCreditCard(credit_card);
2314 // Verify that the web database has been updated and the notification sent.
2315 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2316 .WillOnce(QuitMainMessageLoop());
2317 base::MessageLoop::current()->Run();
2319 CreditCard new_verified_card = credit_card;
2320 new_verified_card.set_guid(base::GenerateGUID());
2321 new_verified_card.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("B. Small"));
2322 EXPECT_TRUE(new_verified_card.IsVerified());
2324 personal_data_->SaveImportedCreditCard(new_verified_card);
2326 // Verify that the web database has been updated and the notification sent.
2327 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2328 .WillOnce(QuitMainMessageLoop());
2329 base::MessageLoop::current()->Run();
2331 // Expect that the saved credit card is updated.
2332 const std::vector<CreditCard*>& results = personal_data_->GetCreditCards();
2333 ASSERT_EQ(1U, results.size());
2334 EXPECT_EQ(ASCIIToUTF16("B. Small"), results[0]->GetRawInfo(CREDIT_CARD_NAME));
2337 TEST_F(PersonalDataManagerTest, GetNonEmptyTypes) {
2338 // Check that there are no available types with no profiles stored.
2339 ServerFieldTypeSet non_empty_types;
2340 personal_data_->GetNonEmptyTypes(&non_empty_types);
2341 EXPECT_EQ(0U, non_empty_types.size());
2343 // Test with one profile stored.
2344 AutofillProfile profile0(base::GenerateGUID(), "https://www.example.com");
2345 test::SetProfileInfo(&profile0,
2346 "Marion", NULL, "Morrison",
2347 "johnwayne@me.xyz", NULL, "123 Zoo St.", NULL, "Hollywood", "CA",
2348 "91601", "US", "14155678910");
2350 personal_data_->AddProfile(profile0);
2352 // Verify that the web database has been updated and the notification sent.
2353 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2354 .WillOnce(QuitMainMessageLoop());
2355 base::MessageLoop::current()->Run();
2357 personal_data_->GetNonEmptyTypes(&non_empty_types);
2358 EXPECT_EQ(15U, non_empty_types.size());
2359 EXPECT_TRUE(non_empty_types.count(NAME_FIRST));
2360 EXPECT_TRUE(non_empty_types.count(NAME_LAST));
2361 EXPECT_TRUE(non_empty_types.count(NAME_FULL));
2362 EXPECT_TRUE(non_empty_types.count(EMAIL_ADDRESS));
2363 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_LINE1));
2364 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_STREET_ADDRESS));
2365 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_CITY));
2366 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_STATE));
2367 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_ZIP));
2368 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_COUNTRY));
2369 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_NUMBER));
2370 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_COUNTRY_CODE));
2371 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_CITY_CODE));
2372 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_CITY_AND_NUMBER));
2373 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_WHOLE_NUMBER));
2375 // Test with multiple profiles stored.
2376 AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com");
2377 test::SetProfileInfo(&profile1,
2378 "Josephine", "Alicia", "Saenz",
2379 "joewayne@me.xyz", "Fox", "903 Apple Ct.", NULL, "Orlando", "FL", "32801",
2380 "US", "16502937549");
2382 AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com");
2383 test::SetProfileInfo(&profile2,
2384 "Josephine", "Alicia", "Saenz",
2385 "joewayne@me.xyz", "Fox", "1212 Center.", "Bld. 5", "Orlando", "FL",
2386 "32801", "US", "16502937549");
2388 personal_data_->AddProfile(profile1);
2389 personal_data_->AddProfile(profile2);
2391 // Verify that the web database has been updated and the notification sent.
2392 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2393 .WillOnce(QuitMainMessageLoop());
2394 base::MessageLoop::current()->Run();
2396 personal_data_->GetNonEmptyTypes(&non_empty_types);
2397 EXPECT_EQ(19U, non_empty_types.size());
2398 EXPECT_TRUE(non_empty_types.count(NAME_FIRST));
2399 EXPECT_TRUE(non_empty_types.count(NAME_MIDDLE));
2400 EXPECT_TRUE(non_empty_types.count(NAME_MIDDLE_INITIAL));
2401 EXPECT_TRUE(non_empty_types.count(NAME_LAST));
2402 EXPECT_TRUE(non_empty_types.count(NAME_FULL));
2403 EXPECT_TRUE(non_empty_types.count(EMAIL_ADDRESS));
2404 EXPECT_TRUE(non_empty_types.count(COMPANY_NAME));
2405 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_LINE1));
2406 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_LINE2));
2407 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_STREET_ADDRESS));
2408 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_CITY));
2409 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_STATE));
2410 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_ZIP));
2411 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_COUNTRY));
2412 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_NUMBER));
2413 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_CITY_CODE));
2414 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_COUNTRY_CODE));
2415 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_CITY_AND_NUMBER));
2416 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_WHOLE_NUMBER));
2418 // Test with credit card information also stored.
2419 CreditCard credit_card(base::GenerateGUID(), "https://www.example.com");
2420 test::SetCreditCardInfo(&credit_card,
2421 "John Dillinger", "423456789012" /* Visa */,
2422 "01", "2010");
2423 personal_data_->AddCreditCard(credit_card);
2425 // Verify that the web database has been updated and the notification sent.
2426 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2427 .WillOnce(QuitMainMessageLoop());
2428 base::MessageLoop::current()->Run();
2430 personal_data_->GetNonEmptyTypes(&non_empty_types);
2431 EXPECT_EQ(27U, non_empty_types.size());
2432 EXPECT_TRUE(non_empty_types.count(NAME_FIRST));
2433 EXPECT_TRUE(non_empty_types.count(NAME_MIDDLE));
2434 EXPECT_TRUE(non_empty_types.count(NAME_MIDDLE_INITIAL));
2435 EXPECT_TRUE(non_empty_types.count(NAME_LAST));
2436 EXPECT_TRUE(non_empty_types.count(NAME_FULL));
2437 EXPECT_TRUE(non_empty_types.count(EMAIL_ADDRESS));
2438 EXPECT_TRUE(non_empty_types.count(COMPANY_NAME));
2439 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_LINE1));
2440 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_LINE2));
2441 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_STREET_ADDRESS));
2442 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_CITY));
2443 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_STATE));
2444 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_ZIP));
2445 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_COUNTRY));
2446 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_NUMBER));
2447 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_CITY_CODE));
2448 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_COUNTRY_CODE));
2449 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_CITY_AND_NUMBER));
2450 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_WHOLE_NUMBER));
2451 EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_NAME));
2452 EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_NUMBER));
2453 EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_TYPE));
2454 EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_EXP_MONTH));
2455 EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_EXP_2_DIGIT_YEAR));
2456 EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_EXP_4_DIGIT_YEAR));
2457 EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR));
2458 EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR));
2461 TEST_F(PersonalDataManagerTest, IncognitoReadOnly) {
2462 ASSERT_TRUE(personal_data_->GetProfiles().empty());
2463 ASSERT_TRUE(personal_data_->GetCreditCards().empty());
2465 AutofillProfile steve_jobs(base::GenerateGUID(), "https://www.example.com");
2466 test::SetProfileInfo(&steve_jobs, "Steven", "Paul", "Jobs", "sjobs@apple.com",
2467 "Apple Computer, Inc.", "1 Infinite Loop", "", "Cupertino", "CA", "95014",
2468 "US", "(800) 275-2273");
2469 personal_data_->AddProfile(steve_jobs);
2471 CreditCard bill_gates(base::GenerateGUID(), "https://www.example.com");
2472 test::SetCreditCardInfo(
2473 &bill_gates, "William H. Gates", "5555555555554444", "1", "2020");
2474 personal_data_->AddCreditCard(bill_gates);
2476 // The personal data manager should be able to read existing profiles in an
2477 // off-the-record context.
2478 ResetPersonalDataManager(USER_MODE_INCOGNITO);
2479 ASSERT_EQ(1U, personal_data_->GetProfiles().size());
2480 ASSERT_EQ(1U, personal_data_->GetCreditCards().size());
2482 // No adds, saves, or updates should take effect.
2483 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()).Times(0);
2485 // Add profiles or credit card shouldn't work.
2486 personal_data_->AddProfile(test::GetFullProfile());
2488 CreditCard larry_page(base::GenerateGUID(), "https://www.example.com");
2489 test::SetCreditCardInfo(
2490 &larry_page, "Lawrence Page", "4111111111111111", "10", "2025");
2491 personal_data_->AddCreditCard(larry_page);
2493 ResetPersonalDataManager(USER_MODE_INCOGNITO);
2494 EXPECT_EQ(1U, personal_data_->GetProfiles().size());
2495 EXPECT_EQ(1U, personal_data_->GetCreditCards().size());
2497 // Saving or creating profiles from imported profiles shouldn't work.
2498 steve_jobs.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Steve"));
2499 personal_data_->SaveImportedProfile(steve_jobs);
2501 bill_gates.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Bill Gates"));
2502 personal_data_->SaveImportedCreditCard(bill_gates);
2504 ResetPersonalDataManager(USER_MODE_INCOGNITO);
2505 EXPECT_EQ(ASCIIToUTF16("Steven"),
2506 personal_data_->GetProfiles()[0]->GetRawInfo(NAME_FIRST));
2507 EXPECT_EQ(ASCIIToUTF16("William H. Gates"),
2508 personal_data_->GetCreditCards()[0]->GetRawInfo(CREDIT_CARD_NAME));
2510 // Updating existing profiles shouldn't work.
2511 steve_jobs.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Steve"));
2512 personal_data_->UpdateProfile(steve_jobs);
2514 bill_gates.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Bill Gates"));
2515 personal_data_->UpdateCreditCard(bill_gates);
2517 ResetPersonalDataManager(USER_MODE_INCOGNITO);
2518 EXPECT_EQ(ASCIIToUTF16("Steven"),
2519 personal_data_->GetProfiles()[0]->GetRawInfo(NAME_FIRST));
2520 EXPECT_EQ(ASCIIToUTF16("William H. Gates"),
2521 personal_data_->GetCreditCards()[0]->GetRawInfo(CREDIT_CARD_NAME));
2523 // Removing shouldn't work.
2524 personal_data_->RemoveByGUID(steve_jobs.guid());
2525 personal_data_->RemoveByGUID(bill_gates.guid());
2527 ResetPersonalDataManager(USER_MODE_INCOGNITO);
2528 EXPECT_EQ(1U, personal_data_->GetProfiles().size());
2529 EXPECT_EQ(1U, personal_data_->GetCreditCards().size());
2532 TEST_F(PersonalDataManagerTest, DefaultCountryCodeIsCached) {
2533 // The return value should always be some country code, no matter what.
2534 std::string default_country =
2535 personal_data_->GetDefaultCountryCodeForNewAddress();
2536 EXPECT_EQ(2U, default_country.size());
2538 AutofillProfile moose(base::GenerateGUID(), "Chrome settings");
2539 test::SetProfileInfo(&moose, "Moose", "P", "McMahon", "mpm@example.com",
2540 "", "1 Taiga TKTR", "", "Calgary", "AB", "T2B 2K2",
2541 "CA", "(800) 555-9000");
2542 personal_data_->AddProfile(moose);
2543 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2544 .WillOnce(QuitMainMessageLoop());
2545 base::MessageLoop::current()->Run();
2546 // The value is cached and doesn't change even after adding an address.
2547 EXPECT_EQ(default_country,
2548 personal_data_->GetDefaultCountryCodeForNewAddress());
2550 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()).Times(2);
2552 // Disabling Autofill blows away this cache and shouldn't account for Autofill
2553 // profiles.
2554 prefs_->SetBoolean(prefs::kAutofillEnabled, false);
2555 EXPECT_EQ(default_country,
2556 personal_data_->GetDefaultCountryCodeForNewAddress());
2558 // Enabling Autofill blows away the cached value and should reflect the new
2559 // value (accounting for profiles).
2560 prefs_->SetBoolean(prefs::kAutofillEnabled, true);
2561 EXPECT_EQ(base::UTF16ToUTF8(moose.GetRawInfo(ADDRESS_HOME_COUNTRY)),
2562 personal_data_->GetDefaultCountryCodeForNewAddress());
2565 TEST_F(PersonalDataManagerTest, DefaultCountryCodeComesFromProfiles) {
2566 AutofillProfile moose(base::GenerateGUID(), "Chrome settings");
2567 test::SetProfileInfo(&moose, "Moose", "P", "McMahon", "mpm@example.com",
2568 "", "1 Taiga TKTR", "", "Calgary", "AB", "T2B 2K2",
2569 "CA", "(800) 555-9000");
2570 personal_data_->AddProfile(moose);
2571 ResetPersonalDataManager(USER_MODE_NORMAL);
2572 EXPECT_EQ("CA", personal_data_->GetDefaultCountryCodeForNewAddress());
2574 // Multiple profiles cast votes.
2575 AutofillProfile armadillo(base::GenerateGUID(), "Chrome settings");
2576 test::SetProfileInfo(&armadillo, "Armin", "Dill", "Oh", "ado@example.com",
2577 "", "1 Speed Bump", "", "Lubbock", "TX", "77500",
2578 "MX", "(800) 555-9000");
2579 AutofillProfile armadillo2(base::GenerateGUID(), "Chrome settings");
2580 test::SetProfileInfo(&armadillo2, "Armin", "Dill", "Oh", "ado@example.com",
2581 "", "2 Speed Bump", "", "Lubbock", "TX", "77500",
2582 "MX", "(800) 555-9000");
2583 personal_data_->AddProfile(armadillo);
2584 personal_data_->AddProfile(armadillo2);
2585 ResetPersonalDataManager(USER_MODE_NORMAL);
2586 EXPECT_EQ("MX", personal_data_->GetDefaultCountryCodeForNewAddress());
2588 personal_data_->RemoveByGUID(armadillo.guid());
2589 personal_data_->RemoveByGUID(armadillo2.guid());
2590 ResetPersonalDataManager(USER_MODE_NORMAL);
2591 // Verified profiles count more.
2592 armadillo.set_origin("http://randomwebsite.com");
2593 armadillo2.set_origin("http://randomwebsite.com");
2594 personal_data_->AddProfile(armadillo);
2595 personal_data_->AddProfile(armadillo2);
2596 ResetPersonalDataManager(USER_MODE_NORMAL);
2597 EXPECT_EQ("CA", personal_data_->GetDefaultCountryCodeForNewAddress());
2599 personal_data_->RemoveByGUID(armadillo.guid());
2600 ResetPersonalDataManager(USER_MODE_NORMAL);
2601 // But unverified profiles can be a tie breaker.
2602 armadillo.set_origin("Chrome settings");
2603 personal_data_->AddProfile(armadillo);
2604 ResetPersonalDataManager(USER_MODE_NORMAL);
2605 EXPECT_EQ("MX", personal_data_->GetDefaultCountryCodeForNewAddress());
2607 // Invalid country codes are ignored.
2608 personal_data_->RemoveByGUID(armadillo.guid());
2609 personal_data_->RemoveByGUID(moose.guid());
2610 AutofillProfile space_invader(base::GenerateGUID(), "Chrome settings");
2611 test::SetProfileInfo(&space_invader, "Marty", "", "Martian",
2612 "mm@example.com", "", "1 Flying Object", "", "Valles Marineris", "",
2613 "", "XX", "");
2614 personal_data_->AddProfile(moose);
2615 ResetPersonalDataManager(USER_MODE_NORMAL);
2616 EXPECT_EQ("MX", personal_data_->GetDefaultCountryCodeForNewAddress());
2619 TEST_F(PersonalDataManagerTest, UpdateLanguageCodeInProfile) {
2620 AutofillProfile profile(base::GenerateGUID(), "https://www.example.com");
2621 test::SetProfileInfo(&profile,
2622 "Marion", "Mitchell", "Morrison",
2623 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
2624 "91601", "US", "12345678910");
2625 personal_data_->AddProfile(profile);
2627 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2628 .WillOnce(QuitMainMessageLoop());
2629 base::MessageLoop::current()->Run();
2631 profile.set_language_code("en");
2632 personal_data_->UpdateProfile(profile);
2634 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2635 .WillOnce(QuitMainMessageLoop());
2636 base::MessageLoop::current()->Run();
2638 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
2639 ASSERT_EQ(1U, results.size());
2640 EXPECT_EQ(0, profile.Compare(*results[0]));
2641 EXPECT_EQ("en", results[0]->language_code());
2644 TEST_F(PersonalDataManagerTest, GetProfileSuggestions) {
2645 AutofillProfile profile(base::GenerateGUID(), "https://www.example.com");
2646 test::SetProfileInfo(&profile,
2647 "Marion", "Mitchell", "Morrison",
2648 "johnwayne@me.xyz", "Fox",
2649 "123 Zoo St.\nSecond Line\nThird line", "unit 5", "Hollywood", "CA",
2650 "91601", "US", "12345678910");
2651 personal_data_->AddProfile(profile);
2652 ResetPersonalDataManager(USER_MODE_NORMAL);
2654 std::vector<Suggestion> suggestions = personal_data_->GetProfileSuggestions(
2655 AutofillType(ADDRESS_HOME_STREET_ADDRESS), base::ASCIIToUTF16("123"),
2656 false, std::vector<ServerFieldType>());
2657 ASSERT_FALSE(suggestions.empty());
2658 EXPECT_EQ(suggestions[0].value,
2659 base::ASCIIToUTF16("123 Zoo St., Second Line, Third line, unit 5"));
2662 TEST_F(PersonalDataManagerTest, GetProfileSuggestionsHideSubsets) {
2663 AutofillProfile profile(base::GenerateGUID(), "https://www.example.com");
2664 test::SetProfileInfo(&profile, "Marion", "Mitchell", "Morrison",
2665 "johnwayne@me.xyz", "Fox",
2666 "123 Zoo St.\nSecond Line\nThird line", "unit 5",
2667 "Hollywood", "CA", "91601", "US", "12345678910");
2669 // Dupe profile, except different in email address (irrelevant for this form).
2670 AutofillProfile profile1 = profile;
2671 profile1.set_guid(base::GenerateGUID());
2672 profile1.SetRawInfo(EMAIL_ADDRESS, base::ASCIIToUTF16("spam_me@example.com"));
2674 // Dupe profile, except different in address state.
2675 AutofillProfile profile2 = profile;
2676 profile2.set_guid(base::GenerateGUID());
2677 profile2.SetRawInfo(ADDRESS_HOME_STATE, base::ASCIIToUTF16("TX"));
2679 // Subset profile.
2680 AutofillProfile profile3 = profile;
2681 profile3.set_guid(base::GenerateGUID());
2682 profile3.SetRawInfo(ADDRESS_HOME_STATE, base::string16());
2684 // For easier results verification, make sure |profile| is suggested first.
2685 profile.set_use_count(5);
2686 personal_data_->AddProfile(profile);
2687 personal_data_->AddProfile(profile1);
2688 personal_data_->AddProfile(profile2);
2689 personal_data_->AddProfile(profile3);
2690 ResetPersonalDataManager(USER_MODE_NORMAL);
2692 // Simulate a form with street address, city and state.
2693 std::vector<ServerFieldType> types;
2694 types.push_back(ADDRESS_HOME_CITY);
2695 types.push_back(ADDRESS_HOME_STATE);
2696 std::vector<Suggestion> suggestions = personal_data_->GetProfileSuggestions(
2697 AutofillType(ADDRESS_HOME_STREET_ADDRESS), base::ASCIIToUTF16("123"),
2698 false, types);
2699 ASSERT_EQ(2U, suggestions.size());
2700 EXPECT_EQ(base::ASCIIToUTF16("Hollywood, CA"), suggestions[0].label);
2701 EXPECT_EQ(base::ASCIIToUTF16("Hollywood, TX"), suggestions[1].label);
2704 TEST_F(PersonalDataManagerTest, GetCreditCardSuggestions) {
2705 EnableWalletCardImport();
2707 CreditCard credit_card0("287151C8-6AB1-487C-9095-28E80BE5DA15",
2708 "https://www.example.com");
2709 test::SetCreditCardInfo(&credit_card0,
2710 "Clyde Barrow", "347666888555" /* American Express */, "04", "2015");
2711 credit_card0.set_use_count(2);
2712 personal_data_->AddCreditCard(credit_card0);
2714 CreditCard credit_card1("1141084B-72D7-4B73-90CF-3D6AC154673B",
2715 "https://www.example.com");
2716 credit_card1.set_use_count(3);
2717 test::SetCreditCardInfo(&credit_card1, "John Dillinger", "", "01", "2010");
2718 personal_data_->AddCreditCard(credit_card1);
2720 CreditCard credit_card2("002149C1-EE28-4213-A3B9-DA243FFF021B",
2721 "https://www.example.com");
2722 credit_card2.set_use_count(1);
2723 test::SetCreditCardInfo(&credit_card2,
2724 "Bonnie Parker", "518765432109" /* Mastercard */, "", "");
2725 personal_data_->AddCreditCard(credit_card2);
2727 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2728 .WillOnce(QuitMainMessageLoop());
2729 base::MessageLoop::current()->Run();
2731 // Sublabel is card number when filling name (exact format depends on
2732 // the platform, but the last 4 digits should appear).
2733 std::vector<Suggestion> suggestions =
2734 personal_data_->GetCreditCardSuggestions(
2735 AutofillType(CREDIT_CARD_NAME), base::string16());
2736 ASSERT_EQ(3U, suggestions.size());
2737 // Ordered by MFU.
2738 EXPECT_EQ(ASCIIToUTF16("Clyde Barrow"), suggestions[1].value);
2739 EXPECT_TRUE(suggestions[1].label.find(ASCIIToUTF16("8555")) !=
2740 base::string16::npos);
2741 EXPECT_EQ(ASCIIToUTF16("John Dillinger"), suggestions[0].value);
2742 EXPECT_EQ(base::string16(), suggestions[0].label);
2743 EXPECT_EQ(ASCIIToUTF16("Bonnie Parker"), suggestions[2].value);
2744 EXPECT_TRUE(suggestions[2].label.find(ASCIIToUTF16("2109")) !=
2745 base::string16::npos);
2747 // Sublabel is expiration date when filling card number.
2748 suggestions = personal_data_->GetCreditCardSuggestions(
2749 AutofillType(CREDIT_CARD_NUMBER), base::string16());
2750 ASSERT_EQ(2U, suggestions.size());
2751 EXPECT_EQ(UTF8ToUTF16(
2752 "Amex\xC2\xA0\xE2\x8B\xAF"
2753 "8555"),
2754 suggestions[0].value);
2755 EXPECT_EQ(ASCIIToUTF16("04/15"), suggestions[0].label);
2756 EXPECT_EQ(UTF8ToUTF16(
2757 "MasterCard\xC2\xA0\xE2\x8B\xAF"
2758 "2109"),
2759 suggestions[1].value);
2760 EXPECT_EQ(base::string16(), suggestions[1].label);
2762 // Add some server cards. If there are local dupes, the locals should be
2763 // hidden.
2764 std::vector<CreditCard> server_cards;
2765 // This server card matches a local card, except the local card is missing the
2766 // number. This should count as a dupe. The locally saved card takes
2767 // precedence.
2768 server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "a123"));
2769 test::SetCreditCardInfo(&server_cards.back(), "John Dillinger",
2770 "9012" /* Visa */, "01", "2010");
2771 server_cards.back().SetTypeForMaskedCard(kVisaCard);
2773 // This server card is identical to a local card, but has a different
2774 // card type. Not a dupe.
2775 server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "b456"));
2776 test::SetCreditCardInfo(&server_cards.back(), "Bonnie Parker",
2777 "2109", "12", "2012");
2778 server_cards.back().SetTypeForMaskedCard(kVisaCard);
2780 // This unmasked server card is a dupe.
2781 server_cards.push_back(CreditCard(CreditCard::FULL_SERVER_CARD, "c789"));
2782 test::SetCreditCardInfo(&server_cards.back(), "Clyde Barrow",
2783 "347666888555" /* American Express */, "04", "2015");
2785 test::SetServerCreditCards(autofill_table_, server_cards);
2786 personal_data_->Refresh();
2787 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2788 .WillOnce(QuitMainMessageLoop());
2789 base::MessageLoop::current()->Run();
2791 suggestions = personal_data_->GetCreditCardSuggestions(
2792 AutofillType(CREDIT_CARD_NAME), base::string16());
2793 ASSERT_EQ(4U, suggestions.size());
2794 EXPECT_EQ(ASCIIToUTF16("John Dillinger"), suggestions[0].value);
2795 EXPECT_EQ(suggestions[0].backend_id, credit_card1.guid());
2796 EXPECT_EQ(ASCIIToUTF16("Clyde Barrow"), suggestions[1].value);
2797 EXPECT_NE(suggestions[1].backend_id, credit_card0.guid());
2798 EXPECT_EQ(ASCIIToUTF16("Bonnie Parker"), suggestions[2].value);
2799 EXPECT_EQ(suggestions[2].backend_id, credit_card2.guid());
2800 EXPECT_EQ(ASCIIToUTF16("Bonnie Parker"), suggestions[3].value);
2801 EXPECT_NE(suggestions[3].backend_id, credit_card2.guid());
2803 suggestions = personal_data_->GetCreditCardSuggestions(
2804 AutofillType(CREDIT_CARD_NUMBER), base::string16());
2805 ASSERT_EQ(4U, suggestions.size());
2806 EXPECT_EQ(UTF8ToUTF16(
2807 "Amex\xC2\xA0\xE2\x8B\xAF"
2808 "8555"),
2809 suggestions[0].value);
2810 EXPECT_EQ(UTF8ToUTF16(
2811 "MasterCard\xC2\xA0\xE2\x8B\xAF"
2812 "2109"),
2813 suggestions[1].value);
2814 EXPECT_EQ(UTF8ToUTF16(
2815 "Visa\xC2\xA0\xE2\x8B\xAF"
2816 "9012"),
2817 suggestions[2].value);
2818 EXPECT_EQ(UTF8ToUTF16(
2819 "Visa\xC2\xA0\xE2\x8B\xAF"
2820 "2109"),
2821 suggestions[3].value);
2823 // Make sure a full server card can be a dupe of more than one local card.
2824 CreditCard credit_card3("4141084B-72D7-4B73-90CF-3D6AC154673B",
2825 "https://www.example.com");
2826 test::SetCreditCardInfo(&credit_card3, "Clyde Barrow", "", "04", "");
2827 personal_data_->AddCreditCard(credit_card3);
2829 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2830 .WillOnce(QuitMainMessageLoop());
2831 base::MessageLoop::current()->Run();
2833 suggestions = personal_data_->GetCreditCardSuggestions(
2834 AutofillType(CREDIT_CARD_NAME), base::string16());
2835 ASSERT_EQ(4U, suggestions.size());
2836 EXPECT_EQ(ASCIIToUTF16("John Dillinger"), suggestions[0].value);
2837 EXPECT_EQ(ASCIIToUTF16("Clyde Barrow"), suggestions[1].value);
2838 EXPECT_EQ(ASCIIToUTF16("Bonnie Parker"), suggestions[2].value);
2839 EXPECT_EQ(ASCIIToUTF16("Bonnie Parker"), suggestions[3].value);
2842 TEST_F(PersonalDataManagerTest, RecordUseOf) {
2843 AutofillProfile profile(test::GetFullProfile());
2844 EXPECT_EQ(0U, profile.use_count());
2845 EXPECT_EQ(base::Time(), profile.use_date());
2846 EXPECT_EQ(base::Time(), profile.modification_date());
2847 personal_data_->AddProfile(profile);
2849 CreditCard credit_card(base::GenerateGUID(), "https://www.example.com");
2850 test::SetCreditCardInfo(&credit_card, "John Dillinger",
2851 "423456789012" /* Visa */, "01", "2010");
2852 EXPECT_EQ(0U, credit_card.use_count());
2853 EXPECT_EQ(base::Time(), credit_card.use_date());
2854 EXPECT_EQ(base::Time(), credit_card.modification_date());
2855 personal_data_->AddCreditCard(credit_card);
2857 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2858 .WillOnce(QuitMainMessageLoop());
2859 base::MessageLoop::current()->Run();
2861 // Notify the PDM that the profile and credit card were used.
2862 AutofillProfile* added_profile =
2863 personal_data_->GetProfileByGUID(profile.guid());
2864 ASSERT_TRUE(added_profile);
2865 EXPECT_EQ(*added_profile, profile);
2866 EXPECT_EQ(0U, added_profile->use_count());
2867 EXPECT_EQ(base::Time(), added_profile->use_date());
2868 EXPECT_NE(base::Time(), added_profile->modification_date());
2869 personal_data_->RecordUseOf(profile);
2871 CreditCard* added_card =
2872 personal_data_->GetCreditCardByGUID(credit_card.guid());
2873 ASSERT_TRUE(added_card);
2874 EXPECT_EQ(*added_card, credit_card);
2875 EXPECT_EQ(0U, added_card->use_count());
2876 EXPECT_EQ(base::Time(), added_card->use_date());
2877 EXPECT_NE(base::Time(), added_card->modification_date());
2878 personal_data_->RecordUseOf(credit_card);
2880 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2881 .WillOnce(QuitMainMessageLoop());
2882 base::MessageLoop::current()->Run();
2884 // Verify usage stats are updated.
2885 added_profile = personal_data_->GetProfileByGUID(profile.guid());
2886 ASSERT_TRUE(added_profile);
2887 EXPECT_EQ(1U, added_profile->use_count());
2888 EXPECT_NE(base::Time(), added_profile->use_date());
2889 EXPECT_NE(base::Time(), added_profile->modification_date());
2891 added_card = personal_data_->GetCreditCardByGUID(credit_card.guid());
2892 ASSERT_TRUE(added_card);
2893 EXPECT_EQ(1U, added_card->use_count());
2894 EXPECT_NE(base::Time(), added_card->use_date());
2895 EXPECT_NE(base::Time(), added_card->modification_date());
2898 TEST_F(PersonalDataManagerTest, UpdateServerCreditCardUsageStats) {
2899 EnableWalletCardImport();
2901 std::vector<CreditCard> server_cards;
2902 server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "a123"));
2903 test::SetCreditCardInfo(&server_cards.back(), "John Dillinger",
2904 "9012" /* Visa */, "01", "2010");
2905 server_cards.back().SetTypeForMaskedCard(kVisaCard);
2907 server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "b456"));
2908 test::SetCreditCardInfo(&server_cards.back(), "Bonnie Parker",
2909 "4444" /* Mastercard */, "12", "2012");
2910 server_cards.back().SetTypeForMaskedCard(kMasterCard);
2912 server_cards.push_back(CreditCard(CreditCard::FULL_SERVER_CARD, "c789"));
2913 test::SetCreditCardInfo(&server_cards.back(), "Clyde Barrow",
2914 "347666888555" /* American Express */, "04", "2015");
2916 test::SetServerCreditCards(autofill_table_, server_cards);
2917 personal_data_->Refresh();
2919 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2920 .WillOnce(QuitMainMessageLoop());
2921 base::MessageLoop::current()->Run();
2923 ASSERT_EQ(3U, personal_data_->GetCreditCards().size());
2925 if (!OfferStoreUnmaskedCards()) {
2926 for (CreditCard* card : personal_data_->GetCreditCards()) {
2927 EXPECT_EQ(CreditCard::MASKED_SERVER_CARD, card->record_type());
2929 // The rest of this test doesn't work if we're force-masking all unmasked
2930 // cards.
2931 return;
2934 // The GUIDs will be different, so just compare the data.
2935 for (size_t i = 0; i < 3; ++i)
2936 EXPECT_EQ(0, server_cards[i].Compare(*personal_data_->GetCreditCards()[i]));
2938 CreditCard* unmasked_card = &server_cards.front();
2939 unmasked_card->set_record_type(CreditCard::FULL_SERVER_CARD);
2940 unmasked_card->SetNumber(ASCIIToUTF16("423456789012"));
2941 EXPECT_NE(0, unmasked_card->Compare(
2942 *personal_data_->GetCreditCards().front()));
2943 personal_data_->UpdateServerCreditCard(*unmasked_card);
2945 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2946 .WillOnce(QuitMainMessageLoop());
2947 base::MessageLoop::current()->Run();
2948 ASSERT_EQ(3U, personal_data_->GetCreditCards().size());
2950 for (size_t i = 0; i < 3; ++i)
2951 EXPECT_EQ(0, server_cards[i].Compare(*personal_data_->GetCreditCards()[i]));
2953 // For an unmasked card, usage data starts out as 1 and Now().
2954 EXPECT_EQ(1U, personal_data_->GetCreditCards()[0]->use_count());
2955 EXPECT_NE(base::Time(), personal_data_->GetCreditCards()[0]->use_date());
2957 EXPECT_EQ(0U, personal_data_->GetCreditCards()[1]->use_count());
2958 EXPECT_EQ(base::Time(), personal_data_->GetCreditCards()[1]->use_date());
2960 // Having unmasked this card, usage stats should be 1 and Now().
2961 EXPECT_EQ(1U, personal_data_->GetCreditCards()[2]->use_count());
2962 EXPECT_NE(base::Time(), personal_data_->GetCreditCards()[2]->use_date());
2963 base::Time initial_use_date = personal_data_->GetCreditCards()[2]->use_date();
2965 server_cards.back().set_guid(personal_data_->GetCreditCards()[2]->guid());
2966 personal_data_->RecordUseOf(server_cards.back());
2967 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2968 .WillOnce(QuitMainMessageLoop());
2969 base::MessageLoop::current()->Run();
2970 ASSERT_EQ(3U, personal_data_->GetCreditCards().size());
2972 EXPECT_EQ(1U, personal_data_->GetCreditCards()[0]->use_count());
2973 EXPECT_NE(base::Time(), personal_data_->GetCreditCards()[0]->use_date());
2975 EXPECT_EQ(0U, personal_data_->GetCreditCards()[1]->use_count());
2976 EXPECT_EQ(base::Time(), personal_data_->GetCreditCards()[1]->use_date());
2978 EXPECT_EQ(2U, personal_data_->GetCreditCards()[2]->use_count());
2979 EXPECT_NE(base::Time(), personal_data_->GetCreditCards()[2]->use_date());
2980 // Time may or may not have elapsed between unmasking and RecordUseOf.
2981 EXPECT_LE(initial_use_date, personal_data_->GetCreditCards()[2]->use_date());
2983 // Can record usage stats on masked cards.
2984 server_cards[1].set_guid(personal_data_->GetCreditCards()[1]->guid());
2985 personal_data_->RecordUseOf(server_cards[1]);
2986 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2987 .WillOnce(QuitMainMessageLoop());
2988 base::MessageLoop::current()->Run();
2989 ASSERT_EQ(3U, personal_data_->GetCreditCards().size());
2990 EXPECT_EQ(1U, personal_data_->GetCreditCards()[1]->use_count());
2991 EXPECT_NE(base::Time(), personal_data_->GetCreditCards()[1]->use_date());
2993 // Upgrading to unmasked retains the usage stats (and increments them).
2994 CreditCard* unmasked_card2 = &server_cards[1];
2995 unmasked_card2->set_record_type(CreditCard::FULL_SERVER_CARD);
2996 unmasked_card2->SetNumber(ASCIIToUTF16("5555555555554444"));
2997 personal_data_->UpdateServerCreditCard(*unmasked_card2);
2999 server_cards[1].set_guid(personal_data_->GetCreditCards()[1]->guid());
3000 personal_data_->RecordUseOf(server_cards[1]);
3001 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
3002 .WillOnce(QuitMainMessageLoop());
3003 base::MessageLoop::current()->Run();
3004 ASSERT_EQ(3U, personal_data_->GetCreditCards().size());
3005 EXPECT_EQ(2U, personal_data_->GetCreditCards()[1]->use_count());
3006 EXPECT_NE(base::Time(), personal_data_->GetCreditCards()[1]->use_date());
3009 TEST_F(PersonalDataManagerTest, ClearAllServerData) {
3010 // Add a server card.
3011 std::vector<CreditCard> server_cards;
3012 server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "a123"));
3013 test::SetCreditCardInfo(&server_cards.back(), "John Dillinger",
3014 "9012" /* Visa */, "01", "2010");
3015 server_cards.back().SetTypeForMaskedCard(kVisaCard);
3016 test::SetServerCreditCards(autofill_table_, server_cards);
3017 personal_data_->Refresh();
3019 // Need to set the google services username
3020 EnableWalletCardImport();
3022 // Add a server profile.
3023 std::vector<AutofillProfile> server_profiles;
3024 server_profiles.push_back(
3025 AutofillProfile(AutofillProfile::SERVER_PROFILE, "a123"));
3026 test::SetProfileInfo(&server_profiles.back(), "John", "", "Doe", "",
3027 "ACME Corp", "500 Oak View", "Apt 8", "Houston", "TX",
3028 "77401", "US", "");
3029 autofill_table_->SetServerProfiles(server_profiles);
3031 // The card and profile should be there.
3032 ResetPersonalDataManager(USER_MODE_NORMAL);
3033 EXPECT_FALSE(personal_data_->GetCreditCards().empty());
3034 EXPECT_FALSE(personal_data_->GetProfiles().empty());
3036 personal_data_->ClearAllServerData();
3038 // Reload the database, everything should be gone.
3039 ResetPersonalDataManager(USER_MODE_NORMAL);
3040 EXPECT_TRUE(personal_data_->GetCreditCards().empty());
3041 EXPECT_TRUE(personal_data_->GetProfiles().empty());
3044 TEST_F(PersonalDataManagerTest, DontDuplicateServerCard) {
3045 EnableWalletCardImport();
3047 std::vector<CreditCard> server_cards;
3048 server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "a123"));
3049 test::SetCreditCardInfo(&server_cards.back(), "John Dillinger",
3050 "1881" /* Visa */, "01", "2017");
3051 server_cards.back().SetTypeForMaskedCard(kVisaCard);
3053 server_cards.push_back(CreditCard(CreditCard::FULL_SERVER_CARD, "c789"));
3054 test::SetCreditCardInfo(&server_cards.back(), "Clyde Barrow",
3055 "347666888555" /* American Express */, "04", "2015");
3057 test::SetServerCreditCards(autofill_table_, server_cards);
3058 personal_data_->Refresh();
3059 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
3060 .WillOnce(QuitMainMessageLoop());
3061 base::MessageLoop::current()->Run();
3063 // A valid credit card form. A user re-types one of their masked cards.
3064 // We should offer to save.
3065 FormData form1;
3066 FormFieldData field;
3067 test::CreateTestFormField("Name on card:", "name_on_card", "John Dillinger",
3068 "text", &field);
3069 form1.fields.push_back(field);
3070 test::CreateTestFormField("Card Number:", "card_number", "4012888888881881",
3071 "text", &field);
3072 form1.fields.push_back(field);
3073 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
3074 form1.fields.push_back(field);
3075 test::CreateTestFormField("Exp Year:", "exp_year", "2017", "text", &field);
3076 form1.fields.push_back(field);
3078 FormStructure form_structure1(form1);
3079 form_structure1.DetermineHeuristicTypes();
3080 scoped_ptr<CreditCard> imported_credit_card;
3081 EXPECT_TRUE(
3082 personal_data_->ImportFormData(form_structure1, &imported_credit_card));
3083 EXPECT_TRUE(imported_credit_card);
3084 imported_credit_card.reset();
3086 // A user re-types (or fills with) an unmasked card. Don't offer to save
3087 // again.
3088 FormData form2;
3089 test::CreateTestFormField("Name on card:", "name_on_card", "Clyde Barrow",
3090 "text", &field);
3091 form2.fields.push_back(field);
3092 test::CreateTestFormField("Card Number:", "card_number", "347666888555",
3093 "text", &field);
3094 form2.fields.push_back(field);
3095 test::CreateTestFormField("Exp Month:", "exp_month", "04", "text", &field);
3096 form2.fields.push_back(field);
3097 test::CreateTestFormField("Exp Year:", "exp_year", "2015", "text", &field);
3098 form2.fields.push_back(field);
3100 FormStructure form_structure2(form2);
3101 form_structure2.DetermineHeuristicTypes();
3102 EXPECT_FALSE(
3103 personal_data_->ImportFormData(form_structure2, &imported_credit_card));
3104 EXPECT_FALSE(imported_credit_card);
3107 // Tests that a profile with all the same info as the reference profile except
3108 // the name gets saved as a new profile.
3109 TEST_F(PersonalDataManagerTest, SaveImportedProfile_DifferentNames) {
3110 SetupReferenceProfile();
3112 AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com");
3113 // New profile changes the first name from "Marion" to "Marionette".
3114 test::SetProfileInfo(&profile2, "Marionette", "Mitchell", "Morrison",
3115 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5",
3116 "Hollywood", "CA", "91601", "US", "12345678910");
3118 personal_data_->SaveImportedProfile(profile2);
3120 const std::vector<AutofillProfile*>& profiles2 =
3121 personal_data_->GetProfiles();
3122 ASSERT_EQ(2U, profiles2.size());
3125 // Tests that a profile with all the same info as the reference profile except
3126 // the address line 1 gets saved as a new profile.
3127 TEST_F(PersonalDataManagerTest, SaveImportedProfile_DifferentAddresseLine1) {
3128 SetupReferenceProfile();
3130 AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com");
3131 test::SetProfileInfo(&profile2, "Marion", "Mitchell", "Morrison",
3132 "johnwayne@me.xyz", "Fox", "123 Aquarium St.", "unit 5",
3133 "Hollywood", "CA", "91601", "US", "12345678910");
3135 personal_data_->SaveImportedProfile(profile2);
3137 const std::vector<AutofillProfile*>& profiles2 =
3138 personal_data_->GetProfiles();
3139 ASSERT_EQ(2U, profiles2.size());
3142 // Tests that a profile with all the same info as the reference profile except
3143 // the address line 2 gets saved as a new profile.
3144 TEST_F(PersonalDataManagerTest, SaveImportedProfile_DifferentAddresseLine2) {
3145 SetupReferenceProfile();
3147 AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com");
3148 test::SetProfileInfo(&profile2, "Marion", "Mitchell", "Morrison",
3149 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 7",
3150 "Hollywood", "CA", "91601", "US", "12345678910");
3152 personal_data_->SaveImportedProfile(profile2);
3154 const std::vector<AutofillProfile*>& profiles2 =
3155 personal_data_->GetProfiles();
3156 ASSERT_EQ(2U, profiles2.size());
3159 // Tests that a profile with all the same info as the reference profile plus a
3160 // new piece of information gets merged with the reference profile and that the
3161 // old empty value gets overwritten by the new information.
3162 TEST_F(PersonalDataManagerTest, SaveImportedProfile_AddtionalInfoCompanyName) {
3163 SetupReferenceProfile();
3165 // Remove the zip code from the reference profile.
3166 const std::vector<AutofillProfile*>& profiles = personal_data_->GetProfiles();
3167 profiles.front()->SetRawInfo(COMPANY_NAME, base::UTF8ToUTF16(""));
3169 AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com");
3170 test::SetProfileInfo(&profile2, "Marion", "Mitchell", "Morrison",
3171 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5",
3172 "Hollywood", "CA", "91601", "US", "12345678910");
3174 personal_data_->SaveImportedProfile(profile2);
3176 const std::vector<AutofillProfile*>& profiles2 =
3177 personal_data_->GetProfiles();
3178 ASSERT_EQ(1U, profiles2.size());
3179 // Make sure the new information is saved.
3180 ASSERT_EQ(base::UTF8ToUTF16("Fox"),
3181 profiles2.front()->GetRawInfo(COMPANY_NAME));
3184 // Tests that a profile with all the same info as the reference profile except a
3185 // loss of information gets merged with the reference profile but that the value
3186 // of the reference profile is not overwritten (no information loss).
3187 TEST_F(PersonalDataManagerTest, SaveImportedProfile_LessInfoCompanyName) {
3188 SetupReferenceProfile();
3190 AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com");
3191 test::SetProfileInfo(&profile2, "Marion", "Mitchell", "Morrison",
3192 "johnwayne@me.xyz", "", "123 Zoo St.", "unit 5",
3193 "Hollywood", "CA", "91601", "US", "12345678910");
3195 personal_data_->SaveImportedProfile(profile2);
3197 const std::vector<AutofillProfile*>& profiles2 =
3198 personal_data_->GetProfiles();
3199 ASSERT_EQ(1U, profiles2.size());
3200 // Make sure there is no loss of information.
3201 ASSERT_EQ(base::UTF8ToUTF16("Fox"),
3202 profiles2.front()->GetRawInfo(COMPANY_NAME));
3205 // Tests that a profile with all the same info as the reference profile plus a
3206 // new piece of information on the address line 2 gets merged with the reference
3207 // profile and that the old empty value gets overwritten by the new information.
3208 TEST_F(PersonalDataManagerTest, SaveImportedProfile_AddtionalInfoAddressLine2) {
3209 SetupReferenceProfile();
3211 // Remove the address line 2 from the reference profile.
3212 const std::vector<AutofillProfile*>& profiles = personal_data_->GetProfiles();
3213 profiles.front()->SetRawInfo(ADDRESS_HOME_LINE2, base::UTF8ToUTF16(""));
3215 AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com");
3216 test::SetProfileInfo(&profile2, "Marion", "Mitchell", "Morrison",
3217 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5",
3218 "Hollywood", "CA", "91601", "US", "12345678910");
3220 personal_data_->SaveImportedProfile(profile2);
3222 const std::vector<AutofillProfile*>& profiles2 =
3223 personal_data_->GetProfiles();
3224 ASSERT_EQ(1U, profiles2.size());
3225 // Make sure the new information is saved.
3226 ASSERT_EQ(base::UTF8ToUTF16("unit 5"),
3227 profiles2.front()->GetRawInfo(ADDRESS_HOME_LINE2));
3230 // Tests that a profile with all the same info as the reference profile except a
3231 // loss of information on the address line 2 gets merged with the reference
3232 // profile but that the value of the reference profile is not overwritten (no
3233 // information loss).
3234 TEST_F(PersonalDataManagerTest, SaveImportedProfile_LessInfoAddressLine2) {
3235 SetupReferenceProfile();
3237 AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com");
3238 test::SetProfileInfo(&profile2, "Marion", "Mitchell", "Morrison",
3239 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "",
3240 "Hollywood", "CA", "91601", "US", "12345678910");
3242 personal_data_->SaveImportedProfile(profile2);
3244 const std::vector<AutofillProfile*>& profiles2 =
3245 personal_data_->GetProfiles();
3246 ASSERT_EQ(1U, profiles2.size());
3247 // Make sure there is no loss of information.
3248 ASSERT_EQ(base::UTF8ToUTF16("unit 5"),
3249 profiles2.front()->GetRawInfo(ADDRESS_HOME_LINE2));
3252 // Tests that a profile with all the same info as the reference profile except
3253 // additional punctuation in the two address lines gets merged with the
3254 // reference profile and that the address of the reference profile is
3255 // overwritten.
3256 TEST_F(PersonalDataManagerTest, SaveImportedProfile_SameAddressAddPunctuation) {
3257 SetupReferenceProfile();
3259 AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com");
3260 // New profile changes the first address line from "123 Zoo St" to "123, Zoo
3261 // St.".
3262 test::SetProfileInfo(&profile2, "Marion", "Mitchell", "Morrison",
3263 "johnwayne@me.xyz", "Fox", "123, Zoo St.", "unit. 5",
3264 "Hollywood", "CA", "91601", "US", "12345678910");
3266 personal_data_->SaveImportedProfile(profile2);
3268 const std::vector<AutofillProfile*>& profiles2 =
3269 personal_data_->GetProfiles();
3270 ASSERT_EQ(1U, profiles2.size());
3272 // Make sure that the new address punctuation is saved.
3273 ASSERT_EQ(base::UTF8ToUTF16("123, Zoo St."),
3274 profiles2.front()->GetRawInfo(ADDRESS_HOME_LINE1));
3275 ASSERT_EQ(base::UTF8ToUTF16("unit. 5"),
3276 profiles2.front()->GetRawInfo(ADDRESS_HOME_LINE2));
3279 // Tests that a profile with all the same info as the reference profile except
3280 // less punctuation in the two address lines gets merged with the reference
3281 // profile and that the address of the reference profile is overwritten.
3282 TEST_F(PersonalDataManagerTest,
3283 SaveImportedProfile_SameAddressRemovePunctuation) {
3284 SetupReferenceProfile();
3286 // Add punctuation to the reference profile's ADDRESS_HOME_LINE1 and 2.
3287 const std::vector<AutofillProfile*>& profiles = personal_data_->GetProfiles();
3288 profiles.front()->SetRawInfo(ADDRESS_HOME_LINE1,
3289 base::UTF8ToUTF16("123, Zoo St."));
3290 profiles.front()->SetRawInfo(ADDRESS_HOME_LINE2,
3291 base::UTF8ToUTF16("Unit. 5"));
3293 AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com");
3294 // New profile changes the first address line from "123, Zoo St." to "123 Zoo
3295 // St".
3296 test::SetProfileInfo(&profile2, "Marion", "Mitchell", "Morrison",
3297 "johnwayne@me.xyz", "Fox", "123 Zoo St", "unit 5",
3298 "Hollywood", "CA", "91601", "US", "12345678910");
3300 personal_data_->SaveImportedProfile(profile2);
3302 const std::vector<AutofillProfile*>& profiles2 =
3303 personal_data_->GetProfiles();
3304 ASSERT_EQ(1U, profiles2.size());
3306 // Make sure that the new address punctuation is saved.
3307 ASSERT_EQ(base::UTF8ToUTF16("123 Zoo St"),
3308 profiles2.front()->GetRawInfo(ADDRESS_HOME_LINE1));
3309 ASSERT_EQ(base::UTF8ToUTF16("unit 5"),
3310 profiles2.front()->GetRawInfo(ADDRESS_HOME_LINE2));
3313 // Tests that a profile with all the same info as the reference profile except
3314 // that the address line 1 is in the address line 2 gets merged with the
3315 // reference profile and that the address lines of the reference profile are
3316 // not overwritten.
3317 TEST_F(PersonalDataManagerTest, SaveImportedProfile_FromTwoAddressLinesToOne) {
3318 SetupReferenceProfile();
3320 AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com");
3321 test::SetProfileInfo(&profile2, "Marion", "Mitchell", "Morrison",
3322 "johnwayne@me.xyz", "Fox", "123 Zoo St, unit 5", "",
3323 "Hollywood", "CA", "91601", "US", "12345678910");
3325 personal_data_->SaveImportedProfile(profile2);
3327 const std::vector<AutofillProfile*>& profiles2 =
3328 personal_data_->GetProfiles();
3329 ASSERT_EQ(1U, profiles2.size());
3331 // Make sure that the address was not overwritten.
3332 ASSERT_EQ(base::UTF8ToUTF16("123 Zoo St"),
3333 profiles2.front()->GetRawInfo(ADDRESS_HOME_LINE1));
3334 ASSERT_EQ(base::UTF8ToUTF16("unit 5"),
3335 profiles2.front()->GetRawInfo(ADDRESS_HOME_LINE2));
3338 // Tests that a profile with all the same info as the reference profile except
3339 // that the address line 2 contains part of the old address line 1 gets merged
3340 // with the reference profile and that the address lines of the reference
3341 // profile are overwritten.
3342 TEST_F(PersonalDataManagerTest, SaveImportedProfile_FromOneAddressLineToTwo) {
3343 SetupReferenceProfile();
3345 // Add punctuation to the reference profile's ADDRESS_HOME_LINE1.
3346 const std::vector<AutofillProfile*>& profiles = personal_data_->GetProfiles();
3347 profiles.front()->SetRawInfo(ADDRESS_HOME_LINE1,
3348 base::UTF8ToUTF16("123, Zoo St, Unit. 5"));
3349 profiles.front()->SetRawInfo(ADDRESS_HOME_LINE2, base::UTF8ToUTF16(""));
3351 AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com");
3352 test::SetProfileInfo(&profile2, "Marion", "Mitchell", "Morrison",
3353 "johnwayne@me.xyz", "Fox", "123 Zoo St", "unit 5",
3354 "Hollywood", "CA", "91601", "US", "12345678910");
3356 personal_data_->SaveImportedProfile(profile2);
3358 const std::vector<AutofillProfile*>& profiles2 =
3359 personal_data_->GetProfiles();
3360 ASSERT_EQ(1U, profiles2.size());
3362 // Make sure that the new address layout is saved.
3363 ASSERT_EQ(base::UTF8ToUTF16("123 Zoo St"),
3364 profiles2.front()->GetRawInfo(ADDRESS_HOME_LINE1));
3365 ASSERT_EQ(base::UTF8ToUTF16("unit 5"),
3366 profiles2.front()->GetRawInfo(ADDRESS_HOME_LINE2));
3369 } // namespace autofill