Include all dupe types (event when value is zero) in scan stats.
[chromium-blink-merge.git] / components / autofill / core / browser / personal_data_manager_unittest.cc
blobde7273a4e7dd072abe33468fc676ce4b3dc80eb7
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/form_structure.h"
22 #include "components/autofill/core/browser/personal_data_manager.h"
23 #include "components/autofill/core/browser/personal_data_manager_observer.h"
24 #include "components/autofill/core/browser/webdata/autofill_table.h"
25 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
26 #include "components/autofill/core/common/autofill_pref_names.h"
27 #include "components/autofill/core/common/autofill_switches.h"
28 #include "components/autofill/core/common/form_data.h"
29 #include "components/signin/core/browser/account_tracker_service.h"
30 #include "components/signin/core/browser/test_signin_client.h"
31 #include "components/signin/core/common/signin_pref_names.h"
32 #include "components/webdata/common/web_data_service_base.h"
33 #include "components/webdata/common/web_database_service.h"
34 #include "google_apis/gaia/fake_oauth2_token_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 fake_oauth2_token_service_.reset(new FakeOAuth2TokenService());
105 account_tracker_.reset(new AccountTrackerService());
106 account_tracker_->Initialize(fake_oauth2_token_service_.get(),
107 signin_client_.get());
109 // Hacky: hold onto a pointer but pass ownership.
110 autofill_table_ = new AutofillTable("en-US");
111 web_database_->AddTable(scoped_ptr<WebDatabaseTable>(autofill_table_));
112 web_database_->LoadDatabase();
113 autofill_database_service_ = new AutofillWebDataService(
114 web_database_, base::ThreadTaskRunnerHandle::Get(),
115 base::ThreadTaskRunnerHandle::Get(),
116 WebDataServiceBase::ProfileErrorCallback());
117 autofill_database_service_->Init();
119 test::DisableSystemServices(prefs_.get());
120 ResetPersonalDataManager(USER_MODE_NORMAL);
123 void TearDown() override {
124 // Order of destruction is important as AutofillManager relies on
125 // PersonalDataManager to be around when it gets destroyed.
126 account_tracker_->Shutdown();
127 fake_oauth2_token_service_.reset();
128 account_tracker_.reset();
129 signin_client_.reset();
132 void ResetPersonalDataManager(UserMode user_mode) {
133 bool is_incognito = (user_mode == USER_MODE_INCOGNITO);
134 personal_data_.reset(new PersonalDataManager("en"));
135 personal_data_->Init(
136 scoped_refptr<AutofillWebDataService>(autofill_database_service_),
137 prefs_.get(),
138 account_tracker_.get(),
139 is_incognito);
140 personal_data_->AddObserver(&personal_data_observer_);
142 // Verify that the web database has been updated and the notification sent.
143 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
144 .WillOnce(QuitMainMessageLoop());
145 base::MessageLoop::current()->Run();
148 void EnableWalletCardImport() {
149 prefs_->SetBoolean(prefs::kAutofillWalletSyncExperimentEnabled, true);
150 std::string account_id =
151 account_tracker_->SeedAccountInfo("12345", "syncuser@example.com");
152 prefs_->SetString(::prefs::kGoogleServicesAccountId, account_id);
153 base::CommandLine::ForCurrentProcess()->AppendSwitch(
154 switches::kEnableOfferStoreUnmaskedWalletCards);
157 // The temporary directory should be deleted at the end to ensure that
158 // files are not used anymore and deletion succeeds.
159 base::ScopedTempDir temp_dir_;
160 base::MessageLoopForUI message_loop_;
161 scoped_ptr<PrefService> prefs_;
162 scoped_ptr<FakeOAuth2TokenService> fake_oauth2_token_service_;
163 scoped_ptr<AccountTrackerService> account_tracker_;
164 scoped_ptr<TestSigninClient> signin_client_;
165 scoped_refptr<AutofillWebDataService> autofill_database_service_;
166 scoped_refptr<WebDatabaseService> web_database_;
167 AutofillTable* autofill_table_; // weak ref
168 PersonalDataLoadedObserverMock personal_data_observer_;
169 scoped_ptr<PersonalDataManager> personal_data_;
172 TEST_F(PersonalDataManagerTest, AddProfile) {
173 // Add profile0 to the database.
174 AutofillProfile profile0(autofill::test::GetFullProfile());
175 profile0.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("j@s.com"));
176 personal_data_->AddProfile(profile0);
178 // Reload the database.
179 ResetPersonalDataManager(USER_MODE_NORMAL);
181 // Verify the addition.
182 const std::vector<AutofillProfile*>& results1 = personal_data_->GetProfiles();
183 ASSERT_EQ(1U, results1.size());
184 EXPECT_EQ(0, profile0.Compare(*results1[0]));
186 // Add profile with identical values. Duplicates should not get saved.
187 AutofillProfile profile0a = profile0;
188 profile0a.set_guid(base::GenerateGUID());
189 personal_data_->AddProfile(profile0a);
191 // Reload the database.
192 ResetPersonalDataManager(USER_MODE_NORMAL);
194 // Verify the non-addition.
195 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
196 ASSERT_EQ(1U, results2.size());
197 EXPECT_EQ(0, profile0.Compare(*results2[0]));
199 // New profile with different email.
200 AutofillProfile profile1 = profile0;
201 profile1.set_guid(base::GenerateGUID());
202 profile1.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("john@smith.com"));
204 // Add the different profile. This should save as a separate profile.
205 // Note that if this same profile was "merged" it would collapse to one
206 // profile with a multi-valued entry for email.
207 personal_data_->AddProfile(profile1);
209 // Reload the database.
210 ResetPersonalDataManager(USER_MODE_NORMAL);
212 // Verify the addition.
213 std::vector<AutofillProfile*> profiles;
214 profiles.push_back(&profile0);
215 profiles.push_back(&profile1);
216 ExpectSameElements(profiles, personal_data_->GetProfiles());
219 TEST_F(PersonalDataManagerTest, DontDuplicateServerProfile) {
220 EnableWalletCardImport();
222 std::vector<AutofillProfile> server_profiles;
223 server_profiles.push_back(
224 AutofillProfile(AutofillProfile::SERVER_PROFILE, "a123"));
225 test::SetProfileInfo(&server_profiles.back(), "John", "", "Doe", "",
226 "ACME Corp", "500 Oak View", "Apt 8", "Houston", "TX",
227 "77401", "US", "");
228 // Wallet only provides a full name, so the above first and last names
229 // will be ignored when the profile is written to the DB.
230 server_profiles.back().SetRawInfo(NAME_FULL, ASCIIToUTF16("John Doe"));
231 autofill_table_->SetServerProfiles(server_profiles);
232 personal_data_->Refresh();
233 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
234 .WillOnce(QuitMainMessageLoop());
235 base::MessageLoop::current()->Run();
236 EXPECT_EQ(1U, personal_data_->GetProfiles().size());
238 // Add profile with identical values. Duplicates should not get saved.
239 AutofillProfile scraped_profile(base::GenerateGUID(),
240 "https://www.example.com");
241 test::SetProfileInfo(&scraped_profile, "John", "", "Doe", "", "ACME Corp",
242 "500 Oak View", "Apt 8", "Houston", "TX", "77401", "US",
243 "");
244 EXPECT_TRUE(scraped_profile.IsSubsetOf(server_profiles.back(), "en-US"));
245 std::string saved_guid = personal_data_->SaveImportedProfile(scraped_profile);
246 EXPECT_NE(scraped_profile.guid(), saved_guid);
248 personal_data_->Refresh();
249 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
250 .WillOnce(QuitMainMessageLoop());
251 base::MessageLoop::current()->Run();
253 // Verify the non-addition.
254 EXPECT_EQ(1U, personal_data_->GetProfiles().size());
255 EXPECT_EQ(0U, personal_data_->web_profiles().size());
258 TEST_F(PersonalDataManagerTest, AddUpdateRemoveProfiles) {
259 AutofillProfile profile0(base::GenerateGUID(), "https://www.example.com");
260 test::SetProfileInfo(&profile0,
261 "Marion", "Mitchell", "Morrison",
262 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
263 "91601", "US", "12345678910");
265 AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com");
266 test::SetProfileInfo(&profile1,
267 "Josephine", "Alicia", "Saenz",
268 "joewayne@me.xyz", "Fox", "903 Apple Ct.", NULL, "Orlando", "FL", "32801",
269 "US", "19482937549");
271 AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com");
272 test::SetProfileInfo(&profile2,
273 "Josephine", "Alicia", "Saenz",
274 "joewayne@me.xyz", "Fox", "1212 Center.", "Bld. 5", "Orlando", "FL",
275 "32801", "US", "19482937549");
277 // Add two test profiles to the database.
278 personal_data_->AddProfile(profile0);
279 personal_data_->AddProfile(profile1);
281 // Verify that the web database has been updated and the notification sent.
282 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
283 .WillOnce(QuitMainMessageLoop());
284 base::MessageLoop::current()->Run();
286 std::vector<AutofillProfile*> profiles;
287 profiles.push_back(&profile0);
288 profiles.push_back(&profile1);
289 ExpectSameElements(profiles, personal_data_->GetProfiles());
291 // Update, remove, and add.
292 profile0.SetRawInfo(NAME_FIRST, ASCIIToUTF16("John"));
293 personal_data_->UpdateProfile(profile0);
294 personal_data_->RemoveByGUID(profile1.guid());
295 personal_data_->AddProfile(profile2);
297 // Verify that the web database has been updated and the notification sent.
298 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
299 .WillOnce(QuitMainMessageLoop());
300 base::MessageLoop::current()->Run();
302 profiles.clear();
303 profiles.push_back(&profile0);
304 profiles.push_back(&profile2);
305 ExpectSameElements(profiles, personal_data_->GetProfiles());
307 // Reset the PersonalDataManager. This tests that the personal data was saved
308 // to the web database, and that we can load the profiles from the web
309 // database.
310 ResetPersonalDataManager(USER_MODE_NORMAL);
312 // Verify that we've loaded the profiles from the web database.
313 ExpectSameElements(profiles, personal_data_->GetProfiles());
316 TEST_F(PersonalDataManagerTest, AddUpdateRemoveCreditCards) {
317 CreditCard credit_card0(base::GenerateGUID(), "https://www.example.com");
318 test::SetCreditCardInfo(&credit_card0,
319 "John Dillinger", "423456789012" /* Visa */, "01", "2010");
321 CreditCard credit_card1(base::GenerateGUID(), "https://www.example.com");
322 test::SetCreditCardInfo(&credit_card1,
323 "Bonnie Parker", "518765432109" /* Mastercard */, "12", "2012");
325 CreditCard credit_card2(base::GenerateGUID(), "https://www.example.com");
326 test::SetCreditCardInfo(&credit_card2,
327 "Clyde Barrow", "347666888555" /* American Express */, "04", "2015");
329 // Add two test credit cards to the database.
330 personal_data_->AddCreditCard(credit_card0);
331 personal_data_->AddCreditCard(credit_card1);
333 // Verify that the web database has been updated and the notification sent.
334 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
335 .WillOnce(QuitMainMessageLoop());
336 base::MessageLoop::current()->Run();
338 std::vector<CreditCard*> cards;
339 cards.push_back(&credit_card0);
340 cards.push_back(&credit_card1);
341 ExpectSameElements(cards, personal_data_->GetCreditCards());
343 // Update, remove, and add.
344 credit_card0.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Joe"));
345 personal_data_->UpdateCreditCard(credit_card0);
346 personal_data_->RemoveByGUID(credit_card1.guid());
347 personal_data_->AddCreditCard(credit_card2);
349 // Verify that the web database has been updated and the notification sent.
350 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
351 .WillOnce(QuitMainMessageLoop());
352 base::MessageLoop::current()->Run();
354 cards.clear();
355 cards.push_back(&credit_card0);
356 cards.push_back(&credit_card2);
357 ExpectSameElements(cards, personal_data_->GetCreditCards());
359 // Reset the PersonalDataManager. This tests that the personal data was saved
360 // to the web database, and that we can load the credit cards from the web
361 // database.
362 ResetPersonalDataManager(USER_MODE_NORMAL);
364 // Verify that we've loaded the credit cards from the web database.
365 cards.clear();
366 cards.push_back(&credit_card0);
367 cards.push_back(&credit_card2);
368 ExpectSameElements(cards, personal_data_->GetCreditCards());
371 TEST_F(PersonalDataManagerTest, UpdateUnverifiedProfilesAndCreditCards) {
372 // Start with unverified data.
373 AutofillProfile profile(base::GenerateGUID(), "https://www.example.com/");
374 test::SetProfileInfo(&profile,
375 "Marion", "Mitchell", "Morrison",
376 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
377 "91601", "US", "12345678910");
378 EXPECT_FALSE(profile.IsVerified());
380 CreditCard credit_card(base::GenerateGUID(), "https://www.example.com/");
381 test::SetCreditCardInfo(&credit_card,
382 "John Dillinger", "423456789012" /* Visa */, "01", "2010");
383 EXPECT_FALSE(credit_card.IsVerified());
385 // Add the data to the database.
386 personal_data_->AddProfile(profile);
387 personal_data_->AddCreditCard(credit_card);
389 // Verify that the web database has been updated and the notification sent.
390 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
391 .WillOnce(QuitMainMessageLoop());
392 base::MessageLoop::current()->Run();
394 const std::vector<AutofillProfile*>& profiles1 =
395 personal_data_->GetProfiles();
396 const std::vector<CreditCard*>& cards1 = personal_data_->GetCreditCards();
397 ASSERT_EQ(1U, profiles1.size());
398 ASSERT_EQ(1U, cards1.size());
399 EXPECT_EQ(0, profile.Compare(*profiles1[0]));
400 EXPECT_EQ(0, credit_card.Compare(*cards1[0]));
402 // Try to update with just the origin changed.
403 AutofillProfile original_profile(profile);
404 CreditCard original_credit_card(credit_card);
405 profile.set_origin("Chrome settings");
406 credit_card.set_origin("Chrome settings");
408 EXPECT_TRUE(profile.IsVerified());
409 EXPECT_TRUE(credit_card.IsVerified());
411 personal_data_->UpdateProfile(profile);
412 personal_data_->UpdateCreditCard(credit_card);
414 // Note: No refresh, as no update is expected.
416 const std::vector<AutofillProfile*>& profiles2 =
417 personal_data_->GetProfiles();
418 const std::vector<CreditCard*>& cards2 = personal_data_->GetCreditCards();
419 ASSERT_EQ(1U, profiles2.size());
420 ASSERT_EQ(1U, cards2.size());
421 EXPECT_NE(profile.origin(), profiles2[0]->origin());
422 EXPECT_NE(credit_card.origin(), cards2[0]->origin());
423 EXPECT_EQ(original_profile.origin(), profiles2[0]->origin());
424 EXPECT_EQ(original_credit_card.origin(), cards2[0]->origin());
426 // Try to update with data changed as well.
427 profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("John"));
428 credit_card.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Joe"));
430 personal_data_->UpdateProfile(profile);
431 personal_data_->UpdateCreditCard(credit_card);
433 // Verify that the web database has been updated and the notification sent.
434 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
435 .WillOnce(QuitMainMessageLoop());
436 base::MessageLoop::current()->Run();
438 const std::vector<AutofillProfile*>& profiles3 =
439 personal_data_->GetProfiles();
440 const std::vector<CreditCard*>& cards3 = personal_data_->GetCreditCards();
441 ASSERT_EQ(1U, profiles3.size());
442 ASSERT_EQ(1U, cards3.size());
443 EXPECT_EQ(0, profile.Compare(*profiles3[0]));
444 EXPECT_EQ(0, credit_card.Compare(*cards3[0]));
445 EXPECT_EQ(profile.origin(), profiles3[0]->origin());
446 EXPECT_EQ(credit_card.origin(), cards3[0]->origin());
449 // Tests that server cards are ignored without the flag.
450 TEST_F(PersonalDataManagerTest, ReturnsServerCreditCards) {
451 std::vector<CreditCard> server_cards;
452 server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "a123"));
453 test::SetCreditCardInfo(&server_cards.back(), "John Dillinger",
454 "9012" /* Visa */, "01", "2010");
455 server_cards.back().SetTypeForMaskedCard(kVisaCard);
457 server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "b456"));
458 test::SetCreditCardInfo(&server_cards.back(), "Bonnie Parker",
459 "2109" /* Mastercard */, "12", "2012");
460 server_cards.back().SetTypeForMaskedCard(kMasterCard);
462 test::SetServerCreditCards(autofill_table_, server_cards);
463 personal_data_->Refresh();
465 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
466 .WillOnce(QuitMainMessageLoop());
467 base::MessageLoop::current()->Run();
469 EXPECT_EQ(0U, personal_data_->GetCreditCards().size());
472 // Makes sure that full cards are re-masked when full PAN storage is off.
473 TEST_F(PersonalDataManagerTest, RefuseToStoreFullCard) {
474 prefs_->SetBoolean(prefs::kAutofillWalletSyncExperimentEnabled, true);
476 // On Linux this should be disabled automatically. Elsewhere, only if the
477 // flag is passed.
478 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
479 EXPECT_FALSE(base::CommandLine::ForCurrentProcess()->HasSwitch(
480 switches::kDisableOfferStoreUnmaskedWalletCards));
481 #else
482 base::CommandLine::ForCurrentProcess()->AppendSwitch(
483 switches::kDisableOfferStoreUnmaskedWalletCards);
484 #endif
486 std::vector<CreditCard> server_cards;
487 server_cards.push_back(CreditCard(CreditCard::FULL_SERVER_CARD, "c789"));
488 test::SetCreditCardInfo(&server_cards.back(), "Clyde Barrow",
489 "347666888555" /* American Express */, "04", "2015");
490 test::SetServerCreditCards(autofill_table_, server_cards);
491 personal_data_->Refresh();
493 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
494 .WillOnce(QuitMainMessageLoop());
495 base::MessageLoop::current()->Run();
497 ASSERT_EQ(1U, personal_data_->GetCreditCards().size());
498 EXPECT_EQ(CreditCard::MASKED_SERVER_CARD,
499 personal_data_->GetCreditCards()[0]->record_type());
502 TEST_F(PersonalDataManagerTest, OfferStoreUnmaskedCards) {
503 #if defined(OS_CHROMEOS) || defined(OS_WIN) || defined(OS_MACOSX) || \
504 defined(OS_IOS) || defined(OS_ANDROID)
505 bool should_offer = true;
506 #elif defined(OS_LINUX)
507 bool should_offer = false;
508 #endif
509 EXPECT_EQ(should_offer, OfferStoreUnmaskedCards());
512 // Tests that UpdateServerCreditCard can be used to mask or unmask server cards.
513 TEST_F(PersonalDataManagerTest, UpdateServerCreditCards) {
514 EnableWalletCardImport();
516 std::vector<CreditCard> server_cards;
517 server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "a123"));
518 test::SetCreditCardInfo(&server_cards.back(), "John Dillinger",
519 "9012" /* Visa */, "01", "2010");
520 server_cards.back().SetTypeForMaskedCard(kVisaCard);
522 server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "b456"));
523 test::SetCreditCardInfo(&server_cards.back(), "Bonnie Parker",
524 "2109" /* Mastercard */, "12", "2012");
525 server_cards.back().SetTypeForMaskedCard(kMasterCard);
527 server_cards.push_back(CreditCard(CreditCard::FULL_SERVER_CARD, "c789"));
528 test::SetCreditCardInfo(&server_cards.back(), "Clyde Barrow",
529 "347666888555" /* American Express */, "04", "2015");
531 test::SetServerCreditCards(autofill_table_, server_cards);
532 personal_data_->Refresh();
534 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
535 .WillOnce(QuitMainMessageLoop());
536 base::MessageLoop::current()->Run();
538 ASSERT_EQ(3U, personal_data_->GetCreditCards().size());
540 if (!OfferStoreUnmaskedCards()) {
541 for (CreditCard* card : personal_data_->GetCreditCards()) {
542 EXPECT_EQ(CreditCard::MASKED_SERVER_CARD, card->record_type());
544 // The rest of this test doesn't work if we're force-masking all unmasked
545 // cards.
546 return;
549 // The GUIDs will be different, so just compare the data.
550 for (size_t i = 0; i < 3; ++i)
551 EXPECT_EQ(0, server_cards[i].Compare(*personal_data_->GetCreditCards()[i]));
553 CreditCard* unmasked_card = &server_cards.front();
554 unmasked_card->set_record_type(CreditCard::FULL_SERVER_CARD);
555 unmasked_card->SetNumber(ASCIIToUTF16("423456789012"));
556 EXPECT_NE(0, server_cards.front().Compare(
557 *personal_data_->GetCreditCards().front()));
558 personal_data_->UpdateServerCreditCard(*unmasked_card);
560 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
561 .WillOnce(QuitMainMessageLoop());
562 base::MessageLoop::current()->Run();
564 for (size_t i = 0; i < 3; ++i)
565 EXPECT_EQ(0, server_cards[i].Compare(*personal_data_->GetCreditCards()[i]));
567 CreditCard* remasked_card = &server_cards.back();
568 remasked_card->set_record_type(CreditCard::MASKED_SERVER_CARD);
569 remasked_card->SetNumber(ASCIIToUTF16("8555"));
570 EXPECT_NE(
571 0, server_cards.back().Compare(*personal_data_->GetCreditCards().back()));
572 personal_data_->UpdateServerCreditCard(*remasked_card);
574 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
575 .WillOnce(QuitMainMessageLoop());
576 base::MessageLoop::current()->Run();
578 for (size_t i = 0; i < 3; ++i)
579 EXPECT_EQ(0, server_cards[i].Compare(*personal_data_->GetCreditCards()[i]));
582 TEST_F(PersonalDataManagerTest, AddProfilesAndCreditCards) {
583 AutofillProfile profile0(base::GenerateGUID(), "https://www.example.com");
584 test::SetProfileInfo(&profile0,
585 "Marion", "Mitchell", "Morrison",
586 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
587 "91601", "US", "12345678910");
589 AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com");
590 test::SetProfileInfo(&profile1,
591 "Josephine", "Alicia", "Saenz",
592 "joewayne@me.xyz", "Fox", "903 Apple Ct.", NULL, "Orlando", "FL", "32801",
593 "US", "19482937549");
595 CreditCard credit_card0(base::GenerateGUID(), "https://www.example.com");
596 test::SetCreditCardInfo(&credit_card0,
597 "John Dillinger", "423456789012" /* Visa */, "01", "2010");
599 CreditCard credit_card1(base::GenerateGUID(), "https://www.example.com");
600 test::SetCreditCardInfo(&credit_card1,
601 "Bonnie Parker", "518765432109" /* Mastercard */, "12", "2012");
603 // Add two test profiles to the database.
604 personal_data_->AddProfile(profile0);
605 personal_data_->AddProfile(profile1);
607 // Verify that the web database has been updated and the notification sent.
608 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
609 .WillOnce(QuitMainMessageLoop());
610 base::MessageLoop::current()->Run();
612 std::vector<AutofillProfile*> profiles;
613 profiles.push_back(&profile0);
614 profiles.push_back(&profile1);
615 ExpectSameElements(profiles, personal_data_->GetProfiles());
617 // Add two test credit cards to the database.
618 personal_data_->AddCreditCard(credit_card0);
619 personal_data_->AddCreditCard(credit_card1);
621 // Verify that the web database has been updated and the notification sent.
622 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
623 .WillOnce(QuitMainMessageLoop());
624 base::MessageLoop::current()->Run();
626 std::vector<CreditCard*> cards;
627 cards.push_back(&credit_card0);
628 cards.push_back(&credit_card1);
629 ExpectSameElements(cards, personal_data_->GetCreditCards());
631 // Determine uniqueness by inserting all of the GUIDs into a set and verifying
632 // the size of the set matches the number of GUIDs.
633 std::set<std::string> guids;
634 guids.insert(profile0.guid());
635 guids.insert(profile1.guid());
636 guids.insert(credit_card0.guid());
637 guids.insert(credit_card1.guid());
638 EXPECT_EQ(4U, guids.size());
641 // Test for http://crbug.com/50047. Makes sure that guids are populated
642 // correctly on load.
643 TEST_F(PersonalDataManagerTest, PopulateUniqueIDsOnLoad) {
644 AutofillProfile profile0(base::GenerateGUID(), "https://www.example.com");
645 test::SetProfileInfo(&profile0,
646 "y", "", "", "", "", "", "", "", "", "", "", "");
648 // Add the profile0 to the db.
649 personal_data_->AddProfile(profile0);
651 // Verify that the web database has been updated and the notification sent.
652 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
653 .WillOnce(QuitMainMessageLoop());
654 base::MessageLoop::current()->Run();
656 // Verify that we've loaded the profiles from the web database.
657 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
658 ASSERT_EQ(1U, results2.size());
659 EXPECT_EQ(0, profile0.Compare(*results2[0]));
661 // Add a new profile.
662 AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com");
663 test::SetProfileInfo(&profile1,
664 "z", "", "", "", "", "", "", "", "", "", "", "");
665 personal_data_->AddProfile(profile1);
667 // Verify that the web database has been updated and the notification sent.
668 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
669 .WillOnce(QuitMainMessageLoop());
670 base::MessageLoop::current()->Run();
672 // Make sure the two profiles have different GUIDs, both valid.
673 const std::vector<AutofillProfile*>& results3 = personal_data_->GetProfiles();
674 ASSERT_EQ(2U, results3.size());
675 EXPECT_NE(results3[0]->guid(), results3[1]->guid());
676 EXPECT_TRUE(base::IsValidGUID(results3[0]->guid()));
677 EXPECT_TRUE(base::IsValidGUID(results3[1]->guid()));
680 TEST_F(PersonalDataManagerTest, SetEmptyProfile) {
681 AutofillProfile profile0(base::GenerateGUID(), "https://www.example.com");
682 test::SetProfileInfo(&profile0,
683 "", "", "", "", "", "", "", "", "", "", "", "");
685 // Add the empty profile to the database.
686 personal_data_->AddProfile(profile0);
688 // Note: no refresh here.
690 // Reset the PersonalDataManager. This tests that the personal data was saved
691 // to the web database, and that we can load the profiles from the web
692 // database.
693 ResetPersonalDataManager(USER_MODE_NORMAL);
695 // Verify that we've loaded the profiles from the web database.
696 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
697 ASSERT_EQ(0U, results2.size());
700 TEST_F(PersonalDataManagerTest, SetEmptyCreditCard) {
701 CreditCard credit_card0(base::GenerateGUID(), "https://www.example.com");
702 test::SetCreditCardInfo(&credit_card0, "", "", "", "");
704 // Add the empty credit card to the database.
705 personal_data_->AddCreditCard(credit_card0);
707 // Note: no refresh here.
709 // Reset the PersonalDataManager. This tests that the personal data was saved
710 // to the web database, and that we can load the credit cards from the web
711 // database.
712 ResetPersonalDataManager(USER_MODE_NORMAL);
714 // Verify that we've loaded the credit cards from the web database.
715 const std::vector<CreditCard*>& results2 = personal_data_->GetCreditCards();
716 ASSERT_EQ(0U, results2.size());
719 TEST_F(PersonalDataManagerTest, Refresh) {
720 AutofillProfile profile0(base::GenerateGUID(), "https://www.example.com");
721 test::SetProfileInfo(&profile0,
722 "Marion", "Mitchell", "Morrison",
723 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
724 "91601", "US", "12345678910");
726 AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com");
727 test::SetProfileInfo(&profile1,
728 "Josephine", "Alicia", "Saenz",
729 "joewayne@me.xyz", "Fox", "903 Apple Ct.", NULL, "Orlando", "FL", "32801",
730 "US", "19482937549");
732 // Add the test profiles to the database.
733 personal_data_->AddProfile(profile0);
734 personal_data_->AddProfile(profile1);
736 // Verify that the web database has been updated and the notification sent.
737 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
738 .WillOnce(QuitMainMessageLoop());
739 base::MessageLoop::current()->Run();
741 std::vector<AutofillProfile*> profiles;
742 profiles.push_back(&profile0);
743 profiles.push_back(&profile1);
744 ExpectSameElements(profiles, personal_data_->GetProfiles());
746 AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com");
747 test::SetProfileInfo(&profile2,
748 "Josephine", "Alicia", "Saenz",
749 "joewayne@me.xyz", "Fox", "1212 Center.", "Bld. 5", "Orlando", "FL",
750 "32801", "US", "19482937549");
752 autofill_database_service_->AddAutofillProfile(profile2);
754 personal_data_->Refresh();
756 // Verify that the web database has been updated and the notification sent.
757 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
758 .WillOnce(QuitMainMessageLoop());
759 base::MessageLoop::current()->Run();
761 profiles.clear();
762 profiles.push_back(&profile0);
763 profiles.push_back(&profile1);
764 profiles.push_back(&profile2);
765 ExpectSameElements(profiles, personal_data_->GetProfiles());
767 autofill_database_service_->RemoveAutofillProfile(profile1.guid());
768 autofill_database_service_->RemoveAutofillProfile(profile2.guid());
770 // Before telling the PDM to refresh, simulate an edit to one of the deleted
771 // profiles via a SetProfile update (this would happen if the Autofill window
772 // was open with a previous snapshot of the profiles, and something
773 // [e.g. sync] removed a profile from the browser. In this edge case, we will
774 // end up in a consistent state by dropping the write).
775 profile0.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Mar"));
776 profile2.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Jo"));
777 personal_data_->UpdateProfile(profile0);
778 personal_data_->AddProfile(profile1);
779 personal_data_->AddProfile(profile2);
781 // Verify that the web database has been updated and the notification sent.
782 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
783 .WillOnce(QuitMainMessageLoop());
784 base::MessageLoop::current()->Run();
786 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
787 ASSERT_EQ(1U, results.size());
788 EXPECT_EQ(profile0, *results[0]);
791 TEST_F(PersonalDataManagerTest, ImportFormData) {
792 FormData form;
793 FormFieldData field;
794 test::CreateTestFormField(
795 "First name:", "first_name", "George", "text", &field);
796 form.fields.push_back(field);
797 test::CreateTestFormField(
798 "Last name:", "last_name", "Washington", "text", &field);
799 form.fields.push_back(field);
800 test::CreateTestFormField(
801 "Email:", "email", "theprez@gmail.com", "text", &field);
802 form.fields.push_back(field);
803 test::CreateTestFormField(
804 "Address:", "address1", "21 Laussat St", "text", &field);
805 form.fields.push_back(field);
806 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
807 form.fields.push_back(field);
808 test::CreateTestFormField("State:", "state", "California", "text", &field);
809 form.fields.push_back(field);
810 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
811 form.fields.push_back(field);
812 FormStructure form_structure(form);
813 form_structure.DetermineHeuristicTypes();
814 scoped_ptr<CreditCard> imported_credit_card;
815 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
816 &imported_credit_card));
817 ASSERT_FALSE(imported_credit_card);
819 // Verify that the web database has been updated and the notification sent.
820 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
821 .WillOnce(QuitMainMessageLoop());
822 base::MessageLoop::current()->Run();
824 AutofillProfile expected(base::GenerateGUID(), "https://www.example.com");
825 test::SetProfileInfo(&expected, "George", NULL,
826 "Washington", "theprez@gmail.com", NULL, "21 Laussat St", NULL,
827 "San Francisco", "California", "94102", NULL, NULL);
828 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
829 ASSERT_EQ(1U, results.size());
830 EXPECT_EQ(0, expected.Compare(*results[0]));
833 TEST_F(PersonalDataManagerTest, ImportFormDataBadEmail) {
834 FormData form;
835 FormFieldData field;
836 test::CreateTestFormField(
837 "First name:", "first_name", "George", "text", &field);
838 form.fields.push_back(field);
839 test::CreateTestFormField(
840 "Last name:", "last_name", "Washington", "text", &field);
841 form.fields.push_back(field);
842 test::CreateTestFormField("Email:", "email", "bogus", "text", &field);
843 form.fields.push_back(field);
844 test::CreateTestFormField(
845 "Address:", "address1", "21 Laussat St", "text", &field);
846 form.fields.push_back(field);
847 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
848 form.fields.push_back(field);
849 test::CreateTestFormField("State:", "state", "California", "text", &field);
850 form.fields.push_back(field);
851 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
852 form.fields.push_back(field);
853 FormStructure form_structure(form);
854 form_structure.DetermineHeuristicTypes();
855 scoped_ptr<CreditCard> imported_credit_card;
856 EXPECT_FALSE(personal_data_->ImportFormData(form_structure,
857 &imported_credit_card));
858 ASSERT_EQ(static_cast<CreditCard*>(NULL), imported_credit_card.get());
860 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
861 ASSERT_EQ(0U, results.size());
864 // Tests that a 'confirm email' field does not block profile import.
865 TEST_F(PersonalDataManagerTest, ImportFormDataTwoEmails) {
866 FormData form;
867 FormFieldData field;
868 test::CreateTestFormField(
869 "Name:", "name", "George Washington", "text", &field);
870 form.fields.push_back(field);
871 test::CreateTestFormField(
872 "Address:", "address1", "21 Laussat St", "text", &field);
873 form.fields.push_back(field);
874 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
875 form.fields.push_back(field);
876 test::CreateTestFormField("State:", "state", "California", "text", &field);
877 form.fields.push_back(field);
878 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
879 form.fields.push_back(field);
880 test::CreateTestFormField(
881 "Email:", "email", "example@example.com", "text", &field);
882 form.fields.push_back(field);
883 test::CreateTestFormField(
884 "Confirm email:", "confirm_email", "example@example.com", "text", &field);
885 form.fields.push_back(field);
886 FormStructure form_structure(form);
887 form_structure.DetermineHeuristicTypes();
888 scoped_ptr<CreditCard> imported_credit_card;
889 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
890 &imported_credit_card));
891 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
892 ASSERT_EQ(1U, results.size());
895 // Tests two email fields containing different values blocks provile import.
896 TEST_F(PersonalDataManagerTest, ImportFormDataTwoDifferentEmails) {
897 FormData form;
898 FormFieldData field;
899 test::CreateTestFormField(
900 "Name:", "name", "George Washington", "text", &field);
901 form.fields.push_back(field);
902 test::CreateTestFormField(
903 "Address:", "address1", "21 Laussat St", "text", &field);
904 form.fields.push_back(field);
905 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
906 form.fields.push_back(field);
907 test::CreateTestFormField("State:", "state", "California", "text", &field);
908 form.fields.push_back(field);
909 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
910 form.fields.push_back(field);
911 test::CreateTestFormField(
912 "Email:", "email", "example@example.com", "text", &field);
913 form.fields.push_back(field);
914 test::CreateTestFormField(
915 "Email:", "email2", "example2@example.com", "text", &field);
916 form.fields.push_back(field);
917 FormStructure form_structure(form);
918 form_structure.DetermineHeuristicTypes();
919 scoped_ptr<CreditCard> imported_credit_card;
920 EXPECT_FALSE(personal_data_->ImportFormData(form_structure,
921 &imported_credit_card));
922 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
923 ASSERT_EQ(0U, results.size());
926 TEST_F(PersonalDataManagerTest, ImportFormDataNotEnoughFilledFields) {
927 FormData form;
928 FormFieldData field;
929 test::CreateTestFormField(
930 "First name:", "first_name", "George", "text", &field);
931 form.fields.push_back(field);
932 test::CreateTestFormField(
933 "Last name:", "last_name", "Washington", "text", &field);
934 form.fields.push_back(field);
935 test::CreateTestFormField(
936 "Card number:", "card_number", "4111 1111 1111 1111", "text", &field);
937 form.fields.push_back(field);
938 FormStructure form_structure(form);
939 form_structure.DetermineHeuristicTypes();
940 scoped_ptr<CreditCard> imported_credit_card;
941 EXPECT_FALSE(personal_data_->ImportFormData(form_structure,
942 &imported_credit_card));
943 ASSERT_FALSE(imported_credit_card);
945 const std::vector<AutofillProfile*>& profiles = personal_data_->GetProfiles();
946 ASSERT_EQ(0U, profiles.size());
947 const std::vector<CreditCard*>& cards = personal_data_->GetCreditCards();
948 ASSERT_EQ(0U, cards.size());
951 TEST_F(PersonalDataManagerTest, ImportFormMinimumAddressUSA) {
952 // United States addresses must specifiy one address line, a city, state and
953 // zip code.
954 FormData form;
955 FormFieldData field;
956 test::CreateTestFormField("Name:", "name", "Barack Obama", "text", &field);
957 form.fields.push_back(field);
958 test::CreateTestFormField(
959 "Address:", "address", "1600 Pennsylvania Avenue", "text", &field);
960 form.fields.push_back(field);
961 test::CreateTestFormField("City:", "city", "Washington", "text", &field);
962 form.fields.push_back(field);
963 test::CreateTestFormField("State:", "state", "DC", "text", &field);
964 form.fields.push_back(field);
965 test::CreateTestFormField("Zip:", "zip", "20500", "text", &field);
966 form.fields.push_back(field);
967 test::CreateTestFormField("Country:", "country", "USA", "text", &field);
968 form.fields.push_back(field);
969 FormStructure form_structure(form);
970 form_structure.DetermineHeuristicTypes();
971 scoped_ptr<CreditCard> imported_credit_card;
972 EXPECT_TRUE(
973 personal_data_->ImportFormData(form_structure, &imported_credit_card));
974 const std::vector<AutofillProfile*>& profiles = personal_data_->GetProfiles();
975 ASSERT_EQ(1U, profiles.size());
978 TEST_F(PersonalDataManagerTest, ImportFormMinimumAddressGB) {
979 // British addresses do not require a state/province as the county is usually
980 // not requested on forms.
981 FormData form;
982 FormFieldData field;
983 test::CreateTestFormField("Name:", "name", "David Cameron", "text", &field);
984 form.fields.push_back(field);
985 test::CreateTestFormField(
986 "Address:", "address", "10 Downing Street", "text", &field);
987 form.fields.push_back(field);
988 test::CreateTestFormField("City:", "city", "London", "text", &field);
989 form.fields.push_back(field);
990 test::CreateTestFormField(
991 "Postcode:", "postcode", "SW1A 2AA", "text", &field);
992 form.fields.push_back(field);
993 test::CreateTestFormField(
994 "Country:", "country", "United Kingdom", "text", &field);
995 form.fields.push_back(field);
996 FormStructure form_structure(form);
997 form_structure.DetermineHeuristicTypes();
998 scoped_ptr<CreditCard> imported_credit_card;
999 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
1000 &imported_credit_card));
1001 const std::vector<AutofillProfile*>& profiles = personal_data_->GetProfiles();
1002 ASSERT_EQ(1U, profiles.size());
1005 TEST_F(PersonalDataManagerTest, ImportFormMinimumAddressGI) {
1006 // Gibraltar has the most minimal set of requirements for a valid address.
1007 // There are no cities or provinces and no postal/zip code system.
1008 FormData form;
1009 FormFieldData field;
1010 test::CreateTestFormField(
1011 "Name:", "name", "Sir Adrian Johns", "text", &field);
1012 form.fields.push_back(field);
1013 test::CreateTestFormField(
1014 "Address:", "address", "The Convent, Main Street", "text", &field);
1015 form.fields.push_back(field);
1016 test::CreateTestFormField("Country:", "country", "Gibraltar", "text", &field);
1017 form.fields.push_back(field);
1018 FormStructure form_structure(form);
1019 form_structure.DetermineHeuristicTypes();
1020 scoped_ptr<CreditCard> imported_credit_card;
1021 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
1022 &imported_credit_card));
1023 const std::vector<AutofillProfile*>& profiles = personal_data_->GetProfiles();
1024 ASSERT_EQ(1U, profiles.size());
1027 TEST_F(PersonalDataManagerTest, ImportPhoneNumberSplitAcrossMultipleFields) {
1028 FormData form;
1029 FormFieldData field;
1030 test::CreateTestFormField(
1031 "First name:", "first_name", "George", "text", &field);
1032 form.fields.push_back(field);
1033 test::CreateTestFormField(
1034 "Last name:", "last_name", "Washington", "text", &field);
1035 form.fields.push_back(field);
1036 test::CreateTestFormField(
1037 "Phone #:", "home_phone_area_code", "650", "text", &field);
1038 field.max_length = 3;
1039 form.fields.push_back(field);
1040 test::CreateTestFormField(
1041 "Phone #:", "home_phone_prefix", "555", "text", &field);
1042 field.max_length = 3;
1043 form.fields.push_back(field);
1044 test::CreateTestFormField(
1045 "Phone #:", "home_phone_suffix", "0000", "text", &field);
1046 field.max_length = 4;
1047 form.fields.push_back(field);
1048 test::CreateTestFormField(
1049 "Address:", "address1", "21 Laussat St", "text", &field);
1050 form.fields.push_back(field);
1051 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
1052 form.fields.push_back(field);
1053 test::CreateTestFormField("State:", "state", "California", "text", &field);
1054 form.fields.push_back(field);
1055 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
1056 form.fields.push_back(field);
1057 FormStructure form_structure(form);
1058 form_structure.DetermineHeuristicTypes();
1059 scoped_ptr<CreditCard> imported_credit_card;
1060 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
1061 &imported_credit_card));
1062 ASSERT_FALSE(imported_credit_card);
1064 // Verify that the web database has been updated and the notification sent.
1065 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1066 .WillOnce(QuitMainMessageLoop());
1067 base::MessageLoop::current()->Run();
1069 AutofillProfile expected(base::GenerateGUID(), "https://www.example.com");
1070 test::SetProfileInfo(&expected, "George", NULL,
1071 "Washington", NULL, NULL, "21 Laussat St", NULL,
1072 "San Francisco", "California", "94102", NULL, "(650) 555-0000");
1073 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
1074 ASSERT_EQ(1U, results.size());
1075 EXPECT_EQ(0, expected.Compare(*results[0]));
1078 TEST_F(PersonalDataManagerTest, ImportFormDataMultilineAddress) {
1079 FormData form;
1080 FormFieldData field;
1081 test::CreateTestFormField(
1082 "First name:", "first_name", "George", "text", &field);
1083 form.fields.push_back(field);
1084 test::CreateTestFormField(
1085 "Last name:", "last_name", "Washington", "text", &field);
1086 form.fields.push_back(field);
1087 test::CreateTestFormField(
1088 "Email:", "email", "theprez@gmail.com", "text", &field);
1089 form.fields.push_back(field);
1090 test::CreateTestFormField(
1091 "Address:",
1092 "street_address",
1093 "21 Laussat St\n"
1094 "Apt. #42",
1095 "textarea",
1096 &field);
1097 form.fields.push_back(field);
1098 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
1099 form.fields.push_back(field);
1100 test::CreateTestFormField("State:", "state", "California", "text", &field);
1101 form.fields.push_back(field);
1102 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
1103 form.fields.push_back(field);
1104 FormStructure form_structure(form);
1105 form_structure.DetermineHeuristicTypes();
1106 scoped_ptr<CreditCard> imported_credit_card;
1107 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
1108 &imported_credit_card));
1109 ASSERT_FALSE(imported_credit_card);
1111 // Verify that the web database has been updated and the notification sent.
1112 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1113 .WillOnce(QuitMainMessageLoop());
1114 base::MessageLoop::current()->Run();
1116 AutofillProfile expected(base::GenerateGUID(), "https://www.example.com");
1117 test::SetProfileInfo(&expected, "George", NULL,
1118 "Washington", "theprez@gmail.com", NULL, "21 Laussat St", "Apt. #42",
1119 "San Francisco", "California", "94102", NULL, NULL);
1120 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
1121 ASSERT_EQ(1U, results.size());
1122 EXPECT_EQ(0, expected.Compare(*results[0]));
1125 TEST_F(PersonalDataManagerTest, SetUniqueCreditCardLabels) {
1126 CreditCard credit_card0(base::GenerateGUID(), "https://www.example.com");
1127 credit_card0.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("John"));
1128 CreditCard credit_card1(base::GenerateGUID(), "https://www.example.com");
1129 credit_card1.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Paul"));
1130 CreditCard credit_card2(base::GenerateGUID(), "https://www.example.com");
1131 credit_card2.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Ringo"));
1132 CreditCard credit_card3(base::GenerateGUID(), "https://www.example.com");
1133 credit_card3.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Other"));
1134 CreditCard credit_card4(base::GenerateGUID(), "https://www.example.com");
1135 credit_card4.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Ozzy"));
1136 CreditCard credit_card5(base::GenerateGUID(), "https://www.example.com");
1137 credit_card5.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Dio"));
1139 // Add the test credit cards to the database.
1140 personal_data_->AddCreditCard(credit_card0);
1141 personal_data_->AddCreditCard(credit_card1);
1142 personal_data_->AddCreditCard(credit_card2);
1143 personal_data_->AddCreditCard(credit_card3);
1144 personal_data_->AddCreditCard(credit_card4);
1145 personal_data_->AddCreditCard(credit_card5);
1147 // Reset the PersonalDataManager. This tests that the personal data was saved
1148 // to the web database, and that we can load the credit cards from the web
1149 // database.
1150 ResetPersonalDataManager(USER_MODE_NORMAL);
1152 std::vector<CreditCard*> cards;
1153 cards.push_back(&credit_card0);
1154 cards.push_back(&credit_card1);
1155 cards.push_back(&credit_card2);
1156 cards.push_back(&credit_card3);
1157 cards.push_back(&credit_card4);
1158 cards.push_back(&credit_card5);
1159 ExpectSameElements(cards, personal_data_->GetCreditCards());
1162 TEST_F(PersonalDataManagerTest, AggregateTwoDifferentProfiles) {
1163 FormData form1;
1164 FormFieldData field;
1165 test::CreateTestFormField(
1166 "First name:", "first_name", "George", "text", &field);
1167 form1.fields.push_back(field);
1168 test::CreateTestFormField(
1169 "Last name:", "last_name", "Washington", "text", &field);
1170 form1.fields.push_back(field);
1171 test::CreateTestFormField(
1172 "Email:", "email", "theprez@gmail.com", "text", &field);
1173 form1.fields.push_back(field);
1174 test::CreateTestFormField(
1175 "Address:", "address1", "21 Laussat St", "text", &field);
1176 form1.fields.push_back(field);
1177 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
1178 form1.fields.push_back(field);
1179 test::CreateTestFormField("State:", "state", "California", "text", &field);
1180 form1.fields.push_back(field);
1181 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
1182 form1.fields.push_back(field);
1184 FormStructure form_structure1(form1);
1185 form_structure1.DetermineHeuristicTypes();
1186 scoped_ptr<CreditCard> imported_credit_card;
1187 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1188 &imported_credit_card));
1189 ASSERT_FALSE(imported_credit_card);
1191 // Verify that the web database has been updated and the notification sent.
1192 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1193 .WillOnce(QuitMainMessageLoop());
1194 base::MessageLoop::current()->Run();
1196 AutofillProfile expected(base::GenerateGUID(), "https://www.example.com");
1197 test::SetProfileInfo(&expected, "George", NULL,
1198 "Washington", "theprez@gmail.com", NULL, "21 Laussat St", NULL,
1199 "San Francisco", "California", "94102", NULL, NULL);
1200 const std::vector<AutofillProfile*>& results1 = personal_data_->GetProfiles();
1201 ASSERT_EQ(1U, results1.size());
1202 EXPECT_EQ(0, expected.Compare(*results1[0]));
1204 // Now create a completely different profile.
1205 FormData form2;
1206 test::CreateTestFormField(
1207 "First name:", "first_name", "John", "text", &field);
1208 form2.fields.push_back(field);
1209 test::CreateTestFormField(
1210 "Last name:", "last_name", "Adams", "text", &field);
1211 form2.fields.push_back(field);
1212 test::CreateTestFormField(
1213 "Email:", "email", "second@gmail.com", "text", &field);
1214 form2.fields.push_back(field);
1215 test::CreateTestFormField(
1216 "Address:", "address1", "22 Laussat St", "text", &field);
1217 form2.fields.push_back(field);
1218 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
1219 form2.fields.push_back(field);
1220 test::CreateTestFormField("State:", "state", "California", "text", &field);
1221 form2.fields.push_back(field);
1222 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
1223 form2.fields.push_back(field);
1225 FormStructure form_structure2(form2);
1226 form_structure2.DetermineHeuristicTypes();
1227 EXPECT_TRUE(personal_data_->ImportFormData(form_structure2,
1228 &imported_credit_card));
1229 ASSERT_FALSE(imported_credit_card);
1231 // Verify that the web database has been updated and the notification sent.
1232 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1233 .WillOnce(QuitMainMessageLoop());
1234 base::MessageLoop::current()->Run();
1236 AutofillProfile expected2(base::GenerateGUID(), "https://www.example.com");
1237 test::SetProfileInfo(&expected2, "John", NULL,
1238 "Adams", "second@gmail.com", NULL, "22 Laussat St", NULL,
1239 "San Francisco", "California", "94102", NULL, NULL);
1240 std::vector<AutofillProfile*> profiles;
1241 profiles.push_back(&expected);
1242 profiles.push_back(&expected2);
1243 ExpectSameElements(profiles, personal_data_->GetProfiles());
1246 TEST_F(PersonalDataManagerTest, AggregateSameProfileWithConflict) {
1247 FormData form1;
1248 FormFieldData field;
1249 test::CreateTestFormField(
1250 "First name:", "first_name", "George", "text", &field);
1251 form1.fields.push_back(field);
1252 test::CreateTestFormField(
1253 "Last name:", "last_name", "Washington", "text", &field);
1254 form1.fields.push_back(field);
1255 test::CreateTestFormField(
1256 "Address:", "address", "1600 Pennsylvania Avenue", "text", &field);
1257 form1.fields.push_back(field);
1258 test::CreateTestFormField(
1259 "Address Line 2:", "address2", "Suite A", "text", &field);
1260 form1.fields.push_back(field);
1261 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
1262 form1.fields.push_back(field);
1263 test::CreateTestFormField("State:", "state", "California", "text", &field);
1264 form1.fields.push_back(field);
1265 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
1266 form1.fields.push_back(field);
1267 test::CreateTestFormField(
1268 "Email:", "email", "theprez@gmail.com", "text", &field);
1269 form1.fields.push_back(field);
1270 test::CreateTestFormField("Phone:", "phone", "6505556666", "text", &field);
1271 form1.fields.push_back(field);
1273 FormStructure form_structure1(form1);
1274 form_structure1.DetermineHeuristicTypes();
1275 scoped_ptr<CreditCard> imported_credit_card;
1276 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1277 &imported_credit_card));
1278 ASSERT_FALSE(imported_credit_card);
1280 // Verify that the web database has been updated and the notification sent.
1281 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1282 .WillOnce(QuitMainMessageLoop());
1283 base::MessageLoop::current()->Run();
1285 AutofillProfile expected(base::GenerateGUID(), "https://www.example.com");
1286 test::SetProfileInfo(
1287 &expected, "George", NULL, "Washington", "theprez@gmail.com", NULL,
1288 "1600 Pennsylvania Avenue", "Suite A", "San Francisco", "California",
1289 "94102", NULL, "(650) 555-6666");
1290 const std::vector<AutofillProfile*>& results1 = personal_data_->GetProfiles();
1291 ASSERT_EQ(1U, results1.size());
1292 EXPECT_EQ(0, expected.Compare(*results1[0]));
1294 // Now create an updated profile.
1295 FormData form2;
1296 test::CreateTestFormField(
1297 "First name:", "first_name", "George", "text", &field);
1298 form2.fields.push_back(field);
1299 test::CreateTestFormField(
1300 "Last name:", "last_name", "Washington", "text", &field);
1301 form2.fields.push_back(field);
1302 test::CreateTestFormField(
1303 "Address:", "address", "1600 Pennsylvania Avenue", "text", &field);
1304 form2.fields.push_back(field);
1305 test::CreateTestFormField(
1306 "Address Line 2:", "address2", "Suite A", "text", &field);
1307 form2.fields.push_back(field);
1308 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
1309 form2.fields.push_back(field);
1310 test::CreateTestFormField("State:", "state", "California", "text", &field);
1311 form2.fields.push_back(field);
1312 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
1313 form2.fields.push_back(field);
1314 test::CreateTestFormField(
1315 "Email:", "email", "theprez@gmail.com", "text", &field);
1316 form2.fields.push_back(field);
1317 // Country gets added.
1318 test::CreateTestFormField("Country:", "country", "USA", "text", &field);
1319 form2.fields.push_back(field);
1320 // Phone gets updated.
1321 test::CreateTestFormField("Phone:", "phone", "6502231234", "text", &field);
1322 form2.fields.push_back(field);
1324 FormStructure form_structure2(form2);
1325 form_structure2.DetermineHeuristicTypes();
1326 EXPECT_TRUE(personal_data_->ImportFormData(form_structure2,
1327 &imported_credit_card));
1328 ASSERT_FALSE(imported_credit_card);
1330 // Verify that the web database has been updated and the notification sent.
1331 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1332 .WillOnce(QuitMainMessageLoop());
1333 base::MessageLoop::current()->Run();
1335 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
1337 // Add multi-valued phone number to expectation. Also, country gets added.
1338 std::vector<base::string16> values;
1339 expected.GetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, &values);
1340 values.push_back(ASCIIToUTF16("(650) 223-1234"));
1341 expected.SetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, values);
1342 expected.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"));
1343 ASSERT_EQ(1U, results2.size());
1344 EXPECT_EQ(0, expected.Compare(*results2[0]));
1347 TEST_F(PersonalDataManagerTest, AggregateProfileWithMissingInfoInOld) {
1348 FormData form1;
1349 FormFieldData field;
1350 test::CreateTestFormField(
1351 "First name:", "first_name", "George", "text", &field);
1352 form1.fields.push_back(field);
1353 test::CreateTestFormField(
1354 "Last name:", "last_name", "Washington", "text", &field);
1355 form1.fields.push_back(field);
1356 test::CreateTestFormField(
1357 "Address Line 1:", "address", "190 High Street", "text", &field);
1358 form1.fields.push_back(field);
1359 test::CreateTestFormField("City:", "city", "Philadelphia", "text", &field);
1360 form1.fields.push_back(field);
1361 test::CreateTestFormField("State:", "state", "Pennsylvania", "text", &field);
1362 form1.fields.push_back(field);
1363 test::CreateTestFormField("Zip:", "zipcode", "19106", "text", &field);
1364 form1.fields.push_back(field);
1366 FormStructure form_structure1(form1);
1367 form_structure1.DetermineHeuristicTypes();
1368 scoped_ptr<CreditCard> imported_credit_card;
1369 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1370 &imported_credit_card));
1371 EXPECT_FALSE(imported_credit_card);
1373 // Verify that the web database has been updated and the notification sent.
1374 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1375 .WillOnce(QuitMainMessageLoop());
1376 base::MessageLoop::current()->Run();
1378 AutofillProfile expected(base::GenerateGUID(), "https://www.example.com");
1379 test::SetProfileInfo(&expected, "George", NULL,
1380 "Washington", NULL, NULL, "190 High Street", NULL,
1381 "Philadelphia", "Pennsylvania", "19106", NULL, NULL);
1382 const std::vector<AutofillProfile*>& results1 = personal_data_->GetProfiles();
1383 ASSERT_EQ(1U, results1.size());
1384 EXPECT_EQ(0, expected.Compare(*results1[0]));
1386 // Submit a form with new data for the first profile.
1387 FormData form2;
1388 test::CreateTestFormField(
1389 "First name:", "first_name", "George", "text", &field);
1390 form2.fields.push_back(field);
1391 test::CreateTestFormField(
1392 "Last name:", "last_name", "Washington", "text", &field);
1393 form2.fields.push_back(field);
1394 test::CreateTestFormField(
1395 "Email:", "email", "theprez@gmail.com", "text", &field);
1396 form2.fields.push_back(field);
1397 test::CreateTestFormField(
1398 "Address Line 1:", "address", "190 High Street", "text", &field);
1399 form2.fields.push_back(field);
1400 test::CreateTestFormField("City:", "city", "Philadelphia", "text", &field);
1401 form2.fields.push_back(field);
1402 test::CreateTestFormField("State:", "state", "Pennsylvania", "text", &field);
1403 form2.fields.push_back(field);
1404 test::CreateTestFormField("Zip:", "zipcode", "19106", "text", &field);
1405 form2.fields.push_back(field);
1407 FormStructure form_structure2(form2);
1408 form_structure2.DetermineHeuristicTypes();
1409 EXPECT_TRUE(personal_data_->ImportFormData(form_structure2,
1410 &imported_credit_card));
1411 ASSERT_FALSE(imported_credit_card);
1413 // Verify that the web database has been updated and the notification sent.
1414 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1415 .WillOnce(QuitMainMessageLoop());
1416 base::MessageLoop::current()->Run();
1418 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
1420 AutofillProfile expected2(base::GenerateGUID(), "https://www.example.com");
1421 test::SetProfileInfo(&expected2, "George", NULL,
1422 "Washington", "theprez@gmail.com", NULL, "190 High Street", NULL,
1423 "Philadelphia", "Pennsylvania", "19106", NULL, NULL);
1424 ASSERT_EQ(1U, results2.size());
1425 EXPECT_EQ(0, expected2.Compare(*results2[0]));
1428 TEST_F(PersonalDataManagerTest, AggregateProfileWithMissingInfoInNew) {
1429 FormData form1;
1430 FormFieldData field;
1431 test::CreateTestFormField(
1432 "First name:", "first_name", "George", "text", &field);
1433 form1.fields.push_back(field);
1434 test::CreateTestFormField(
1435 "Last name:", "last_name", "Washington", "text", &field);
1436 form1.fields.push_back(field);
1437 test::CreateTestFormField(
1438 "Company:", "company", "Government", "text", &field);
1439 form1.fields.push_back(field);
1440 test::CreateTestFormField(
1441 "Email:", "email", "theprez@gmail.com", "text", &field);
1442 form1.fields.push_back(field);
1443 test::CreateTestFormField(
1444 "Address Line 1:", "address", "190 High Street", "text", &field);
1445 form1.fields.push_back(field);
1446 test::CreateTestFormField("City:", "city", "Philadelphia", "text", &field);
1447 form1.fields.push_back(field);
1448 test::CreateTestFormField("State:", "state", "Pennsylvania", "text", &field);
1449 form1.fields.push_back(field);
1450 test::CreateTestFormField("Zip:", "zipcode", "19106", "text", &field);
1451 form1.fields.push_back(field);
1453 FormStructure form_structure1(form1);
1454 form_structure1.DetermineHeuristicTypes();
1455 scoped_ptr<CreditCard> imported_credit_card;
1456 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1457 &imported_credit_card));
1458 ASSERT_FALSE(imported_credit_card);
1460 // Verify that the web database has been updated and the notification sent.
1461 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1462 .WillOnce(QuitMainMessageLoop());
1463 base::MessageLoop::current()->Run();
1465 AutofillProfile expected(base::GenerateGUID(), "https://www.example.com");
1466 test::SetProfileInfo(&expected, "George", NULL,
1467 "Washington", "theprez@gmail.com", "Government", "190 High Street", NULL,
1468 "Philadelphia", "Pennsylvania", "19106", NULL, NULL);
1469 const std::vector<AutofillProfile*>& results1 = personal_data_->GetProfiles();
1470 ASSERT_EQ(1U, results1.size());
1471 EXPECT_EQ(0, expected.Compare(*results1[0]));
1473 // Submit a form with new data for the first profile.
1474 FormData form2;
1475 test::CreateTestFormField(
1476 "First name:", "first_name", "George", "text", &field);
1477 form2.fields.push_back(field);
1478 test::CreateTestFormField(
1479 "Last name:", "last_name", "Washington", "text", &field);
1480 form2.fields.push_back(field);
1481 // Note missing Company field.
1482 test::CreateTestFormField(
1483 "Email:", "email", "theprez@gmail.com", "text", &field);
1484 form2.fields.push_back(field);
1485 test::CreateTestFormField(
1486 "Address Line 1:", "address", "190 High Street", "text", &field);
1487 form2.fields.push_back(field);
1488 test::CreateTestFormField("City:", "city", "Philadelphia", "text", &field);
1489 form2.fields.push_back(field);
1490 test::CreateTestFormField("State:", "state", "Pennsylvania", "text", &field);
1491 form2.fields.push_back(field);
1492 test::CreateTestFormField("Zip:", "zipcode", "19106", "text", &field);
1493 form2.fields.push_back(field);
1495 FormStructure form_structure2(form2);
1496 form_structure2.DetermineHeuristicTypes();
1497 EXPECT_TRUE(personal_data_->ImportFormData(form_structure2,
1498 &imported_credit_card));
1499 ASSERT_FALSE(imported_credit_card);
1501 // Verify that the web database has been updated and the notification sent.
1502 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1503 .WillOnce(QuitMainMessageLoop());
1504 base::MessageLoop::current()->Run();
1506 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
1508 // Expect no change.
1509 ASSERT_EQ(1U, results2.size());
1510 EXPECT_EQ(0, expected.Compare(*results2[0]));
1513 TEST_F(PersonalDataManagerTest, AggregateProfileWithInsufficientAddress) {
1514 FormData form1;
1515 FormFieldData field;
1516 test::CreateTestFormField(
1517 "First name:", "first_name", "George", "text", &field);
1518 form1.fields.push_back(field);
1519 test::CreateTestFormField(
1520 "Last name:", "last_name", "Washington", "text", &field);
1521 form1.fields.push_back(field);
1522 test::CreateTestFormField(
1523 "Company:", "company", "Government", "text", &field);
1524 form1.fields.push_back(field);
1525 test::CreateTestFormField(
1526 "Email:", "email", "theprez@gmail.com", "text", &field);
1527 form1.fields.push_back(field);
1528 test::CreateTestFormField(
1529 "Address Line 1:", "address", "190 High Street", "text", &field);
1530 form1.fields.push_back(field);
1531 test::CreateTestFormField("City:", "city", "Philadelphia", "text", &field);
1532 form1.fields.push_back(field);
1534 FormStructure form_structure1(form1);
1535 form_structure1.DetermineHeuristicTypes();
1536 scoped_ptr<CreditCard> imported_credit_card;
1537 EXPECT_FALSE(personal_data_->ImportFormData(form_structure1,
1538 &imported_credit_card));
1539 ASSERT_FALSE(imported_credit_card);
1541 // Since no refresh is expected, reload the data from the database to make
1542 // sure no changes were written out.
1543 ResetPersonalDataManager(USER_MODE_NORMAL);
1545 const std::vector<AutofillProfile*>& profiles = personal_data_->GetProfiles();
1546 ASSERT_EQ(0U, profiles.size());
1547 const std::vector<CreditCard*>& cards = personal_data_->GetCreditCards();
1548 ASSERT_EQ(0U, cards.size());
1551 TEST_F(PersonalDataManagerTest, AggregateExistingAuxiliaryProfile) {
1552 // Simulate having access to an auxiliary profile.
1553 // |auxiliary_profile| will be owned by |personal_data_|.
1554 AutofillProfile* auxiliary_profile =
1555 new AutofillProfile(base::GenerateGUID(), "https://www.example.com");
1556 test::SetProfileInfo(auxiliary_profile,
1557 "Tester", "Frederick", "McAddressBookTesterson",
1558 "tester@example.com", "Acme Inc.", "1 Main", "Apt A", "San Francisco",
1559 "CA", "94102", "US", "1.415.888.9999");
1560 ScopedVector<AutofillProfile>& auxiliary_profiles =
1561 personal_data_->auxiliary_profiles_;
1562 auxiliary_profiles.push_back(auxiliary_profile);
1564 // Simulate a form submission with a subset of the info.
1565 // Note that the phone number format is different from the saved format.
1566 FormData form;
1567 FormFieldData field;
1568 test::CreateTestFormField(
1569 "First name:", "first_name", "Tester", "text", &field);
1570 form.fields.push_back(field);
1571 test::CreateTestFormField(
1572 "Last name:", "last_name", "McAddressBookTesterson", "text", &field);
1573 form.fields.push_back(field);
1574 test::CreateTestFormField(
1575 "Email:", "email", "tester@example.com", "text", &field);
1576 form.fields.push_back(field);
1577 test::CreateTestFormField("Address:", "address1", "1 Main", "text", &field);
1578 form.fields.push_back(field);
1579 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
1580 form.fields.push_back(field);
1581 test::CreateTestFormField("State:", "state", "CA", "text", &field);
1582 form.fields.push_back(field);
1583 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
1584 form.fields.push_back(field);
1585 test::CreateTestFormField("Phone:", "phone", "4158889999", "text", &field);
1586 form.fields.push_back(field);
1588 FormStructure form_structure(form);
1589 form_structure.DetermineHeuristicTypes();
1590 scoped_ptr<CreditCard> imported_credit_card;
1591 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
1592 &imported_credit_card));
1593 EXPECT_FALSE(imported_credit_card);
1595 // Note: No refresh.
1597 // Expect no change.
1598 const std::vector<AutofillProfile*>& web_profiles =
1599 personal_data_->web_profiles();
1600 EXPECT_EQ(0U, web_profiles.size());
1601 ASSERT_EQ(1U, auxiliary_profiles.size());
1602 EXPECT_EQ(0, auxiliary_profile->Compare(*auxiliary_profiles[0]));
1605 TEST_F(PersonalDataManagerTest, AggregateTwoDifferentCreditCards) {
1606 FormData form1;
1608 // Start with a single valid credit card form.
1609 FormFieldData field;
1610 test::CreateTestFormField(
1611 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1612 form1.fields.push_back(field);
1613 test::CreateTestFormField(
1614 "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
1615 form1.fields.push_back(field);
1616 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
1617 form1.fields.push_back(field);
1618 test::CreateTestFormField("Exp Year:", "exp_year", "2011", "text", &field);
1619 form1.fields.push_back(field);
1621 FormStructure form_structure1(form1);
1622 form_structure1.DetermineHeuristicTypes();
1623 scoped_ptr<CreditCard> imported_credit_card;
1624 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1625 &imported_credit_card));
1626 ASSERT_TRUE(imported_credit_card);
1627 personal_data_->SaveImportedCreditCard(*imported_credit_card);
1629 // Verify that the web database has been updated and the notification sent.
1630 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1631 .WillOnce(QuitMainMessageLoop());
1632 base::MessageLoop::current()->Run();
1634 CreditCard expected(base::GenerateGUID(), "https://www.example.com");
1635 test::SetCreditCardInfo(&expected,
1636 "Biggie Smalls", "4111111111111111", "01", "2011");
1637 const std::vector<CreditCard*>& results = personal_data_->GetCreditCards();
1638 ASSERT_EQ(1U, results.size());
1639 EXPECT_EQ(0, expected.Compare(*results[0]));
1641 // Add a second different valid credit card.
1642 FormData form2;
1643 test::CreateTestFormField(
1644 "Name on card:", "name_on_card", "", "text", &field);
1645 form2.fields.push_back(field);
1646 test::CreateTestFormField(
1647 "Card Number:", "card_number", "5500 0000 0000 0004", "text", &field);
1648 form2.fields.push_back(field);
1649 test::CreateTestFormField("Exp Month:", "exp_month", "02", "text", &field);
1650 form2.fields.push_back(field);
1651 test::CreateTestFormField("Exp Year:", "exp_year", "2012", "text", &field);
1652 form2.fields.push_back(field);
1654 FormStructure form_structure2(form2);
1655 form_structure2.DetermineHeuristicTypes();
1656 EXPECT_TRUE(personal_data_->ImportFormData(form_structure2,
1657 &imported_credit_card));
1658 ASSERT_TRUE(imported_credit_card);
1659 personal_data_->SaveImportedCreditCard(*imported_credit_card);
1661 // Verify that the web database has been updated and the notification sent.
1662 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1663 .WillOnce(QuitMainMessageLoop());
1664 base::MessageLoop::current()->Run();
1666 CreditCard expected2(base::GenerateGUID(), "https://www.example.com");
1667 test::SetCreditCardInfo(&expected2,"", "5500000000000004", "02", "2012");
1668 std::vector<CreditCard*> cards;
1669 cards.push_back(&expected);
1670 cards.push_back(&expected2);
1671 ExpectSameElements(cards, personal_data_->GetCreditCards());
1674 TEST_F(PersonalDataManagerTest, AggregateCardsThatDuplicateServerCards) {
1675 // Add server cards.
1676 std::vector<CreditCard> server_cards;
1677 server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "a123"));
1678 test::SetCreditCardInfo(&server_cards.back(), "John Dillinger",
1679 "1111" /* Visa */, "01", "2010");
1680 server_cards.back().SetTypeForMaskedCard(kVisaCard);
1681 server_cards.push_back(CreditCard(CreditCard::FULL_SERVER_CARD, "c789"));
1682 test::SetCreditCardInfo(&server_cards.back(), "Clyde Barrow",
1683 "347666888555" /* American Express */, "04", "2015");
1684 test::SetServerCreditCards(autofill_table_, server_cards);
1686 FormData form1;
1688 // Type the same data as the masked card into a form.
1689 FormFieldData field;
1690 test::CreateTestFormField(
1691 "Name on card:", "name_on_card", "John Dillinger", "text", &field);
1692 form1.fields.push_back(field);
1693 test::CreateTestFormField(
1694 "Card Number:", "card_number", "4111111111111111", "text", &field);
1695 form1.fields.push_back(field);
1696 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
1697 form1.fields.push_back(field);
1698 test::CreateTestFormField("Exp Year:", "exp_year", "2010", "text", &field);
1699 form1.fields.push_back(field);
1701 // The card should be offered to be saved locally because it only matches the
1702 // masked card.
1703 FormStructure form_structure1(form1);
1704 form_structure1.DetermineHeuristicTypes();
1705 scoped_ptr<CreditCard> imported_credit_card;
1706 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1707 &imported_credit_card));
1708 ASSERT_TRUE(imported_credit_card);
1709 personal_data_->SaveImportedCreditCard(*imported_credit_card);
1711 // Verify that the web database has been updated and the notification sent.
1712 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1713 .WillOnce(QuitMainMessageLoop());
1714 base::MessageLoop::current()->Run();
1716 // Type the same data as the unmasked card into a form.
1717 FormData form2;
1718 test::CreateTestFormField(
1719 "Name on card:", "name_on_card", "Clyde Barrow", "text", &field);
1720 form2.fields.push_back(field);
1721 test::CreateTestFormField(
1722 "Card Number:", "card_number", "347666888555", "text", &field);
1723 form2.fields.push_back(field);
1724 test::CreateTestFormField("Exp Month:", "exp_month", "04", "text", &field);
1725 form2.fields.push_back(field);
1726 test::CreateTestFormField("Exp Year:", "exp_year", "2015", "text", &field);
1727 form2.fields.push_back(field);
1729 // The card should not be offered to be saved locally because it only matches
1730 // the masked card.
1731 FormStructure form_structure2(form2);
1732 form_structure2.DetermineHeuristicTypes();
1733 EXPECT_FALSE(personal_data_->ImportFormData(form_structure2,
1734 &imported_credit_card));
1735 ASSERT_FALSE(imported_credit_card);
1738 TEST_F(PersonalDataManagerTest, AggregateInvalidCreditCard) {
1739 FormData form1;
1741 // Start with a single valid credit card form.
1742 FormFieldData field;
1743 test::CreateTestFormField(
1744 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1745 form1.fields.push_back(field);
1746 test::CreateTestFormField(
1747 "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
1748 form1.fields.push_back(field);
1749 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
1750 form1.fields.push_back(field);
1751 test::CreateTestFormField("Exp Year:", "exp_year", "2011", "text", &field);
1752 form1.fields.push_back(field);
1754 FormStructure form_structure1(form1);
1755 form_structure1.DetermineHeuristicTypes();
1756 scoped_ptr<CreditCard> imported_credit_card;
1757 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1758 &imported_credit_card));
1759 ASSERT_TRUE(imported_credit_card);
1760 personal_data_->SaveImportedCreditCard(*imported_credit_card);
1762 // Verify that the web database has been updated and the notification sent.
1763 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1764 .WillOnce(QuitMainMessageLoop());
1765 base::MessageLoop::current()->Run();
1767 CreditCard expected(base::GenerateGUID(), "https://www.example.com");
1768 test::SetCreditCardInfo(&expected,
1769 "Biggie Smalls", "4111111111111111", "01", "2011");
1770 const std::vector<CreditCard*>& results = personal_data_->GetCreditCards();
1771 ASSERT_EQ(1U, results.size());
1772 EXPECT_EQ(0, expected.Compare(*results[0]));
1774 // Add a second different invalid credit card.
1775 FormData form2;
1776 test::CreateTestFormField(
1777 "Name on card:", "name_on_card", "Jim Johansen", "text", &field);
1778 form2.fields.push_back(field);
1779 test::CreateTestFormField(
1780 "Card Number:", "card_number", "1000000000000000", "text", &field);
1781 form2.fields.push_back(field);
1782 test::CreateTestFormField("Exp Month:", "exp_month", "02", "text", &field);
1783 form2.fields.push_back(field);
1784 test::CreateTestFormField("Exp Year:", "exp_year", "2012", "text", &field);
1785 form2.fields.push_back(field);
1787 FormStructure form_structure2(form2);
1788 form_structure2.DetermineHeuristicTypes();
1789 EXPECT_FALSE(personal_data_->ImportFormData(form_structure2,
1790 &imported_credit_card));
1791 ASSERT_FALSE(imported_credit_card);
1793 // Since no refresh is expected, reload the data from the database to make
1794 // sure no changes were written out.
1795 ResetPersonalDataManager(USER_MODE_NORMAL);
1797 const std::vector<CreditCard*>& results2 = personal_data_->GetCreditCards();
1798 ASSERT_EQ(1U, results2.size());
1799 EXPECT_EQ(0, expected.Compare(*results2[0]));
1802 TEST_F(PersonalDataManagerTest, AggregateSameCreditCardWithConflict) {
1803 FormData form1;
1805 // Start with a single valid credit card form.
1806 FormFieldData field;
1807 test::CreateTestFormField(
1808 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1809 form1.fields.push_back(field);
1810 test::CreateTestFormField(
1811 "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
1812 form1.fields.push_back(field);
1813 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
1814 form1.fields.push_back(field);
1815 test::CreateTestFormField("Exp Year:", "exp_year", "2011", "text", &field);
1816 form1.fields.push_back(field);
1818 FormStructure form_structure1(form1);
1819 form_structure1.DetermineHeuristicTypes();
1820 scoped_ptr<CreditCard> imported_credit_card;
1821 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1822 &imported_credit_card));
1823 ASSERT_TRUE(imported_credit_card);
1824 personal_data_->SaveImportedCreditCard(*imported_credit_card);
1826 // Verify that the web database has been updated and the notification sent.
1827 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1828 .WillOnce(QuitMainMessageLoop());
1829 base::MessageLoop::current()->Run();
1831 CreditCard expected(base::GenerateGUID(), "https://www.example.com");
1832 test::SetCreditCardInfo(&expected,
1833 "Biggie Smalls", "4111111111111111", "01", "2011");
1834 const std::vector<CreditCard*>& results = personal_data_->GetCreditCards();
1835 ASSERT_EQ(1U, results.size());
1836 EXPECT_EQ(0, expected.Compare(*results[0]));
1838 // Add a second different valid credit card where the year is different but
1839 // the credit card number matches.
1840 FormData form2;
1841 test::CreateTestFormField(
1842 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1843 form2.fields.push_back(field);
1844 test::CreateTestFormField(
1845 "Card Number:", "card_number", "4111 1111 1111 1111", "text", &field);
1846 form2.fields.push_back(field);
1847 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
1848 form2.fields.push_back(field);
1849 test::CreateTestFormField("Exp Year:", "exp_year", "2012", "text", &field);
1850 form2.fields.push_back(field);
1852 FormStructure form_structure2(form2);
1853 form_structure2.DetermineHeuristicTypes();
1854 EXPECT_TRUE(personal_data_->ImportFormData(form_structure2,
1855 &imported_credit_card));
1856 EXPECT_FALSE(imported_credit_card);
1858 // Verify that the web database has been updated and the notification sent.
1859 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1860 .WillOnce(QuitMainMessageLoop());
1861 base::MessageLoop::current()->Run();
1863 // Expect that the newer information is saved. In this case the year is
1864 // updated to "2012".
1865 CreditCard expected2(base::GenerateGUID(), "https://www.example.com");
1866 test::SetCreditCardInfo(&expected2,
1867 "Biggie Smalls", "4111111111111111", "01", "2012");
1868 const std::vector<CreditCard*>& results2 = personal_data_->GetCreditCards();
1869 ASSERT_EQ(1U, results2.size());
1870 EXPECT_EQ(0, expected2.Compare(*results2[0]));
1873 TEST_F(PersonalDataManagerTest, AggregateEmptyCreditCardWithConflict) {
1874 FormData form1;
1876 // Start with a single valid credit card form.
1877 FormFieldData field;
1878 test::CreateTestFormField(
1879 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1880 form1.fields.push_back(field);
1881 test::CreateTestFormField(
1882 "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
1883 form1.fields.push_back(field);
1884 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
1885 form1.fields.push_back(field);
1886 test::CreateTestFormField("Exp Year:", "exp_year", "2011", "text", &field);
1887 form1.fields.push_back(field);
1889 FormStructure form_structure1(form1);
1890 form_structure1.DetermineHeuristicTypes();
1891 scoped_ptr<CreditCard> imported_credit_card;
1892 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1893 &imported_credit_card));
1894 ASSERT_TRUE(imported_credit_card);
1895 personal_data_->SaveImportedCreditCard(*imported_credit_card);
1897 // Verify that the web database has been updated and the notification sent.
1898 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1899 .WillOnce(QuitMainMessageLoop());
1900 base::MessageLoop::current()->Run();
1902 CreditCard expected(base::GenerateGUID(), "https://www.example.com");
1903 test::SetCreditCardInfo(&expected,
1904 "Biggie Smalls", "4111111111111111", "01", "2011");
1905 const std::vector<CreditCard*>& results = personal_data_->GetCreditCards();
1906 ASSERT_EQ(1U, results.size());
1907 EXPECT_EQ(0, expected.Compare(*results[0]));
1909 // Add a second credit card with no number.
1910 FormData form2;
1911 test::CreateTestFormField(
1912 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1913 form2.fields.push_back(field);
1914 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
1915 form2.fields.push_back(field);
1916 test::CreateTestFormField("Exp Year:", "exp_year", "2012", "text", &field);
1917 form2.fields.push_back(field);
1919 FormStructure form_structure2(form2);
1920 form_structure2.DetermineHeuristicTypes();
1921 EXPECT_FALSE(personal_data_->ImportFormData(form_structure2,
1922 &imported_credit_card));
1923 EXPECT_FALSE(imported_credit_card);
1925 // Since no refresh is expected, reload the data from the database to make
1926 // sure no changes were written out.
1927 ResetPersonalDataManager(USER_MODE_NORMAL);
1929 // No change is expected.
1930 CreditCard expected2(base::GenerateGUID(), "https://www.example.com");
1931 test::SetCreditCardInfo(&expected2,
1932 "Biggie Smalls", "4111111111111111", "01", "2011");
1933 const std::vector<CreditCard*>& results2 = personal_data_->GetCreditCards();
1934 ASSERT_EQ(1U, results2.size());
1935 EXPECT_EQ(0, expected2.Compare(*results2[0]));
1938 TEST_F(PersonalDataManagerTest, AggregateCreditCardWithMissingInfoInNew) {
1939 FormData form1;
1941 // Start with a single valid credit card form.
1942 FormFieldData field;
1943 test::CreateTestFormField(
1944 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1945 form1.fields.push_back(field);
1946 test::CreateTestFormField(
1947 "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
1948 form1.fields.push_back(field);
1949 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
1950 form1.fields.push_back(field);
1951 test::CreateTestFormField("Exp Year:", "exp_year", "2011", "text", &field);
1952 form1.fields.push_back(field);
1954 FormStructure form_structure1(form1);
1955 form_structure1.DetermineHeuristicTypes();
1956 scoped_ptr<CreditCard> imported_credit_card;
1957 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1958 &imported_credit_card));
1959 ASSERT_TRUE(imported_credit_card);
1960 personal_data_->SaveImportedCreditCard(*imported_credit_card);
1962 // Verify that the web database has been updated and the notification sent.
1963 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1964 .WillOnce(QuitMainMessageLoop());
1965 base::MessageLoop::current()->Run();
1967 CreditCard expected(base::GenerateGUID(), "https://www.example.com");
1968 test::SetCreditCardInfo(&expected,
1969 "Biggie Smalls", "4111111111111111", "01", "2011");
1970 const std::vector<CreditCard*>& results = personal_data_->GetCreditCards();
1971 ASSERT_EQ(1U, results.size());
1972 EXPECT_EQ(0, expected.Compare(*results[0]));
1974 // Add a second different valid credit card where the name is missing but
1975 // the credit card number matches.
1976 FormData form2;
1977 // Note missing name.
1978 test::CreateTestFormField(
1979 "Card Number:", "card_number", "4111111111111111", "text", &field);
1980 form2.fields.push_back(field);
1981 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
1982 form2.fields.push_back(field);
1983 test::CreateTestFormField("Exp Year:", "exp_year", "2011", "text", &field);
1984 form2.fields.push_back(field);
1986 FormStructure form_structure2(form2);
1987 form_structure2.DetermineHeuristicTypes();
1988 EXPECT_TRUE(personal_data_->ImportFormData(form_structure2,
1989 &imported_credit_card));
1990 EXPECT_FALSE(imported_credit_card);
1992 // Since no refresh is expected, reload the data from the database to make
1993 // sure no changes were written out.
1994 ResetPersonalDataManager(USER_MODE_NORMAL);
1996 // No change is expected.
1997 CreditCard expected2(base::GenerateGUID(), "https://www.example.com");
1998 test::SetCreditCardInfo(&expected2,
1999 "Biggie Smalls", "4111111111111111", "01", "2011");
2000 const std::vector<CreditCard*>& results2 = personal_data_->GetCreditCards();
2001 ASSERT_EQ(1U, results2.size());
2002 EXPECT_EQ(0, expected2.Compare(*results2[0]));
2004 // Add a third credit card where the expiration date is missing.
2005 FormData form3;
2006 test::CreateTestFormField(
2007 "Name on card:", "name_on_card", "Johnny McEnroe", "text", &field);
2008 form3.fields.push_back(field);
2009 test::CreateTestFormField(
2010 "Card Number:", "card_number", "5555555555554444", "text", &field);
2011 form3.fields.push_back(field);
2012 // Note missing expiration month and year..
2014 FormStructure form_structure3(form3);
2015 form_structure3.DetermineHeuristicTypes();
2016 EXPECT_FALSE(personal_data_->ImportFormData(form_structure3,
2017 &imported_credit_card));
2018 ASSERT_FALSE(imported_credit_card);
2020 // Since no refresh is expected, reload the data from the database to make
2021 // sure no changes were written out.
2022 ResetPersonalDataManager(USER_MODE_NORMAL);
2024 // No change is expected.
2025 CreditCard expected3(base::GenerateGUID(), "https://www.example.com");
2026 test::SetCreditCardInfo(&expected3,
2027 "Biggie Smalls", "4111111111111111", "01", "2011");
2028 const std::vector<CreditCard*>& results3 = personal_data_->GetCreditCards();
2029 ASSERT_EQ(1U, results3.size());
2030 EXPECT_EQ(0, expected3.Compare(*results3[0]));
2033 TEST_F(PersonalDataManagerTest, AggregateCreditCardWithMissingInfoInOld) {
2034 // Start with a single valid credit card stored via the preferences.
2035 // Note the empty name.
2036 CreditCard saved_credit_card(base::GenerateGUID(), "https://www.example.com");
2037 test::SetCreditCardInfo(&saved_credit_card,
2038 "", "4111111111111111" /* Visa */, "01", "2011");
2039 personal_data_->AddCreditCard(saved_credit_card);
2041 // Verify that the web database has been updated and the notification sent.
2042 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2043 .WillOnce(QuitMainMessageLoop());
2044 base::MessageLoop::current()->Run();
2046 const std::vector<CreditCard*>& results1 = personal_data_->GetCreditCards();
2047 ASSERT_EQ(1U, results1.size());
2048 EXPECT_EQ(saved_credit_card, *results1[0]);
2051 // Add a second different valid credit card where the year is different but
2052 // the credit card number matches.
2053 FormData form;
2054 FormFieldData field;
2055 test::CreateTestFormField(
2056 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
2057 form.fields.push_back(field);
2058 test::CreateTestFormField(
2059 "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
2060 form.fields.push_back(field);
2061 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
2062 form.fields.push_back(field);
2063 test::CreateTestFormField("Exp Year:", "exp_year", "2012", "text", &field);
2064 form.fields.push_back(field);
2066 FormStructure form_structure(form);
2067 form_structure.DetermineHeuristicTypes();
2068 scoped_ptr<CreditCard> imported_credit_card;
2069 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
2070 &imported_credit_card));
2071 EXPECT_FALSE(imported_credit_card);
2073 // Verify that the web database has been updated and the notification sent.
2074 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2075 .WillOnce(QuitMainMessageLoop());
2076 base::MessageLoop::current()->Run();
2078 // Expect that the newer information is saved. In this case the year is
2079 // added to the existing credit card.
2080 CreditCard expected2(base::GenerateGUID(), "https://www.example.com");
2081 test::SetCreditCardInfo(&expected2,
2082 "Biggie Smalls", "4111111111111111", "01", "2012");
2083 const std::vector<CreditCard*>& results2 = personal_data_->GetCreditCards();
2084 ASSERT_EQ(1U, results2.size());
2085 EXPECT_EQ(0, expected2.Compare(*results2[0]));
2088 // We allow the user to store a credit card number with separators via the UI.
2089 // We should not try to re-aggregate the same card with the separators stripped.
2090 TEST_F(PersonalDataManagerTest, AggregateSameCreditCardWithSeparators) {
2091 // Start with a single valid credit card stored via the preferences.
2092 // Note the separators in the credit card number.
2093 CreditCard saved_credit_card(base::GenerateGUID(), "https://www.example.com");
2094 test::SetCreditCardInfo(&saved_credit_card,
2095 "Biggie Smalls", "4111 1111 1111 1111" /* Visa */, "01", "2011");
2096 personal_data_->AddCreditCard(saved_credit_card);
2098 // Verify that the web database has been updated and the notification sent.
2099 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2100 .WillOnce(QuitMainMessageLoop());
2101 base::MessageLoop::current()->Run();
2103 const std::vector<CreditCard*>& results1 = personal_data_->GetCreditCards();
2104 ASSERT_EQ(1U, results1.size());
2105 EXPECT_EQ(0, saved_credit_card.Compare(*results1[0]));
2107 // Import the same card info, but with different separators in the number.
2108 FormData form;
2109 FormFieldData field;
2110 test::CreateTestFormField(
2111 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
2112 form.fields.push_back(field);
2113 test::CreateTestFormField(
2114 "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
2115 form.fields.push_back(field);
2116 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
2117 form.fields.push_back(field);
2118 test::CreateTestFormField("Exp Year:", "exp_year", "2011", "text", &field);
2119 form.fields.push_back(field);
2121 FormStructure form_structure(form);
2122 form_structure.DetermineHeuristicTypes();
2123 scoped_ptr<CreditCard> imported_credit_card;
2124 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
2125 &imported_credit_card));
2126 EXPECT_FALSE(imported_credit_card);
2128 // Since no refresh is expected, reload the data from the database to make
2129 // sure no changes were written out.
2130 ResetPersonalDataManager(USER_MODE_NORMAL);
2132 // Expect that no new card is saved.
2133 const std::vector<CreditCard*>& results2 = personal_data_->GetCreditCards();
2134 ASSERT_EQ(1U, results2.size());
2135 EXPECT_EQ(0, saved_credit_card.Compare(*results2[0]));
2138 // Ensure that if a verified profile already exists, aggregated profiles cannot
2139 // modify it in any way.
2140 TEST_F(PersonalDataManagerTest, AggregateExistingVerifiedProfileWithConflict) {
2141 // Start with a verified profile.
2142 AutofillProfile profile(base::GenerateGUID(), "Chrome settings");
2143 test::SetProfileInfo(&profile,
2144 "Marion", "Mitchell", "Morrison",
2145 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
2146 "91601", "US", "12345678910");
2147 EXPECT_TRUE(profile.IsVerified());
2149 // Add the profile to the database.
2150 personal_data_->AddProfile(profile);
2152 // Verify that the web database has been updated and the notification sent.
2153 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2154 .WillOnce(QuitMainMessageLoop());
2155 base::MessageLoop::current()->Run();
2157 // Simulate a form submission with conflicting info.
2158 FormData form;
2159 FormFieldData field;
2160 test::CreateTestFormField(
2161 "First name:", "first_name", "Marion", "text", &field);
2162 form.fields.push_back(field);
2163 test::CreateTestFormField(
2164 "Last name:", "last_name", "Morrison", "text", &field);
2165 form.fields.push_back(field);
2166 test::CreateTestFormField(
2167 "Email:", "email", "other.email@example.com", "text", &field);
2168 form.fields.push_back(field);
2169 test::CreateTestFormField(
2170 "Address:", "address1", "123 Zoo St.", "text", &field);
2171 form.fields.push_back(field);
2172 test::CreateTestFormField("City:", "city", "Hollywood", "text", &field);
2173 form.fields.push_back(field);
2174 test::CreateTestFormField("State:", "state", "CA", "text", &field);
2175 form.fields.push_back(field);
2176 test::CreateTestFormField("Zip:", "zip", "91601", "text", &field);
2177 form.fields.push_back(field);
2179 FormStructure form_structure(form);
2180 form_structure.DetermineHeuristicTypes();
2181 scoped_ptr<CreditCard> imported_credit_card;
2182 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
2183 &imported_credit_card));
2184 EXPECT_FALSE(imported_credit_card);
2186 // Wait for the refresh, which in this case is a no-op.
2187 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2188 .WillOnce(QuitMainMessageLoop());
2189 base::MessageLoop::current()->Run();
2191 // Expect that no new profile is saved.
2192 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
2193 ASSERT_EQ(1U, results.size());
2194 EXPECT_EQ(0, profile.Compare(*results[0]));
2197 // Ensure that if a verified credit card already exists, aggregated credit cards
2198 // cannot modify it in any way.
2199 TEST_F(PersonalDataManagerTest,
2200 AggregateExistingVerifiedCreditCardWithConflict) {
2201 // Start with a verified credit card.
2202 CreditCard credit_card(base::GenerateGUID(), "Chrome settings");
2203 test::SetCreditCardInfo(&credit_card,
2204 "Biggie Smalls", "4111 1111 1111 1111" /* Visa */, "01", "2011");
2205 EXPECT_TRUE(credit_card.IsVerified());
2207 // Add the credit card to the database.
2208 personal_data_->AddCreditCard(credit_card);
2210 // Verify that the web database has been updated and the notification sent.
2211 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2212 .WillOnce(QuitMainMessageLoop());
2213 base::MessageLoop::current()->Run();
2215 // Simulate a form submission with conflicting expiration year.
2216 FormData form;
2217 FormFieldData field;
2218 test::CreateTestFormField(
2219 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
2220 form.fields.push_back(field);
2221 test::CreateTestFormField(
2222 "Card Number:", "card_number", "4111 1111 1111 1111", "text", &field);
2223 form.fields.push_back(field);
2224 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
2225 form.fields.push_back(field);
2226 test::CreateTestFormField("Exp Year:", "exp_year", "2012", "text", &field);
2227 form.fields.push_back(field);
2229 FormStructure form_structure(form);
2230 form_structure.DetermineHeuristicTypes();
2231 scoped_ptr<CreditCard> imported_credit_card;
2232 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
2233 &imported_credit_card));
2234 ASSERT_FALSE(imported_credit_card);
2236 // Since no refresh is expected, reload the data from the database to make
2237 // sure no changes were written out.
2238 ResetPersonalDataManager(USER_MODE_NORMAL);
2240 // Expect that the saved credit card is not modified.
2241 const std::vector<CreditCard*>& results = personal_data_->GetCreditCards();
2242 ASSERT_EQ(1U, results.size());
2243 EXPECT_EQ(0, credit_card.Compare(*results[0]));
2246 // Ensure that verified profiles can be saved via SaveImportedProfile,
2247 // overwriting existing unverified profiles.
2248 TEST_F(PersonalDataManagerTest, SaveImportedProfileWithVerifiedData) {
2249 // Start with an unverified profile.
2250 AutofillProfile profile(base::GenerateGUID(), "https://www.example.com");
2251 test::SetProfileInfo(&profile,
2252 "Marion", "Mitchell", "Morrison",
2253 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
2254 "91601", "US", "12345678910");
2255 EXPECT_FALSE(profile.IsVerified());
2257 // Add the profile to the database.
2258 personal_data_->AddProfile(profile);
2260 // Verify that the web database has been updated and the notification sent.
2261 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2262 .WillOnce(QuitMainMessageLoop());
2263 base::MessageLoop::current()->Run();
2265 AutofillProfile new_verified_profile = profile;
2266 new_verified_profile.set_guid(base::GenerateGUID());
2267 new_verified_profile.set_origin("Chrome settings");
2268 new_verified_profile.SetRawInfo(COMPANY_NAME, ASCIIToUTF16("Fizzbang, Inc."));
2269 EXPECT_TRUE(new_verified_profile.IsVerified());
2271 personal_data_->SaveImportedProfile(new_verified_profile);
2273 // Verify that the web database has been updated and the notification sent.
2274 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2275 .WillOnce(QuitMainMessageLoop());
2276 base::MessageLoop::current()->Run();
2278 // Expect that the existing profile is not modified, and instead the new
2279 // profile is added.
2280 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
2281 ASSERT_EQ(1U, results.size());
2282 EXPECT_EQ(0, new_verified_profile.Compare(*results[0]));
2285 // Ensure that verified profiles can be saved via SaveImportedProfile,
2286 // overwriting existing verified profiles as well.
2287 TEST_F(PersonalDataManagerTest, SaveImportedProfileWithExistingVerifiedData) {
2288 // Start with a verified profile.
2289 AutofillProfile profile(base::GenerateGUID(), "Chrome settings");
2290 test::SetProfileInfo(&profile,
2291 "Marion", "Mitchell", "Morrison",
2292 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
2293 "91601", "US", "12345678910");
2294 EXPECT_TRUE(profile.IsVerified());
2296 // Add the profile to the database.
2297 personal_data_->AddProfile(profile);
2299 // Verify that the web database has been updated and the notification sent.
2300 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2301 .WillOnce(QuitMainMessageLoop());
2302 base::MessageLoop::current()->Run();
2304 AutofillProfile new_verified_profile = profile;
2305 new_verified_profile.set_guid(base::GenerateGUID());
2306 new_verified_profile.SetRawInfo(COMPANY_NAME, ASCIIToUTF16("Fizzbang, Inc."));
2307 EXPECT_TRUE(new_verified_profile.IsVerified());
2309 personal_data_->SaveImportedProfile(new_verified_profile);
2311 // Verify that the web database has been updated and the notification sent.
2312 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2313 .WillOnce(QuitMainMessageLoop());
2314 base::MessageLoop::current()->Run();
2316 // The new profile should be merged into the existing one.
2317 AutofillProfile expected_profile = profile;
2318 expected_profile.SetRawInfo(COMPANY_NAME, ASCIIToUTF16("Fizzbang, Inc."));
2319 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
2320 ASSERT_EQ(1U, results.size());
2321 EXPECT_EQ(expected_profile, *results[0]);
2324 // Ensure that verified credit cards can be saved via SaveImportedCreditCard.
2325 TEST_F(PersonalDataManagerTest, SaveImportedCreditCardWithVerifiedData) {
2326 // Start with a verified credit card.
2327 CreditCard credit_card(base::GenerateGUID(), "Chrome settings");
2328 test::SetCreditCardInfo(&credit_card,
2329 "Biggie Smalls", "4111 1111 1111 1111" /* Visa */, "01", "2011");
2330 EXPECT_TRUE(credit_card.IsVerified());
2332 // Add the credit card to the database.
2333 personal_data_->AddCreditCard(credit_card);
2335 // Verify that the web database has been updated and the notification sent.
2336 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2337 .WillOnce(QuitMainMessageLoop());
2338 base::MessageLoop::current()->Run();
2340 CreditCard new_verified_card = credit_card;
2341 new_verified_card.set_guid(base::GenerateGUID());
2342 new_verified_card.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("B. Small"));
2343 EXPECT_TRUE(new_verified_card.IsVerified());
2345 personal_data_->SaveImportedCreditCard(new_verified_card);
2347 // Verify that the web database has been updated and the notification sent.
2348 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2349 .WillOnce(QuitMainMessageLoop());
2350 base::MessageLoop::current()->Run();
2352 // Expect that the saved credit card is updated.
2353 const std::vector<CreditCard*>& results = personal_data_->GetCreditCards();
2354 ASSERT_EQ(1U, results.size());
2355 EXPECT_EQ(ASCIIToUTF16("B. Small"), results[0]->GetRawInfo(CREDIT_CARD_NAME));
2358 TEST_F(PersonalDataManagerTest, GetNonEmptyTypes) {
2359 // Check that there are no available types with no profiles stored.
2360 ServerFieldTypeSet non_empty_types;
2361 personal_data_->GetNonEmptyTypes(&non_empty_types);
2362 EXPECT_EQ(0U, non_empty_types.size());
2364 // Test with one profile stored.
2365 AutofillProfile profile0(base::GenerateGUID(), "https://www.example.com");
2366 test::SetProfileInfo(&profile0,
2367 "Marion", NULL, "Morrison",
2368 "johnwayne@me.xyz", NULL, "123 Zoo St.", NULL, "Hollywood", "CA",
2369 "91601", "US", "14155678910");
2371 personal_data_->AddProfile(profile0);
2373 // Verify that the web database has been updated and the notification sent.
2374 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2375 .WillOnce(QuitMainMessageLoop());
2376 base::MessageLoop::current()->Run();
2378 personal_data_->GetNonEmptyTypes(&non_empty_types);
2379 EXPECT_EQ(15U, non_empty_types.size());
2380 EXPECT_TRUE(non_empty_types.count(NAME_FIRST));
2381 EXPECT_TRUE(non_empty_types.count(NAME_LAST));
2382 EXPECT_TRUE(non_empty_types.count(NAME_FULL));
2383 EXPECT_TRUE(non_empty_types.count(EMAIL_ADDRESS));
2384 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_LINE1));
2385 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_STREET_ADDRESS));
2386 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_CITY));
2387 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_STATE));
2388 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_ZIP));
2389 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_COUNTRY));
2390 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_NUMBER));
2391 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_COUNTRY_CODE));
2392 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_CITY_CODE));
2393 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_CITY_AND_NUMBER));
2394 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_WHOLE_NUMBER));
2396 // Test with multiple profiles stored.
2397 AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com");
2398 test::SetProfileInfo(&profile1,
2399 "Josephine", "Alicia", "Saenz",
2400 "joewayne@me.xyz", "Fox", "903 Apple Ct.", NULL, "Orlando", "FL", "32801",
2401 "US", "16502937549");
2403 AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com");
2404 test::SetProfileInfo(&profile2,
2405 "Josephine", "Alicia", "Saenz",
2406 "joewayne@me.xyz", "Fox", "1212 Center.", "Bld. 5", "Orlando", "FL",
2407 "32801", "US", "16502937549");
2409 personal_data_->AddProfile(profile1);
2410 personal_data_->AddProfile(profile2);
2412 // Verify that the web database has been updated and the notification sent.
2413 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2414 .WillOnce(QuitMainMessageLoop());
2415 base::MessageLoop::current()->Run();
2417 personal_data_->GetNonEmptyTypes(&non_empty_types);
2418 EXPECT_EQ(19U, non_empty_types.size());
2419 EXPECT_TRUE(non_empty_types.count(NAME_FIRST));
2420 EXPECT_TRUE(non_empty_types.count(NAME_MIDDLE));
2421 EXPECT_TRUE(non_empty_types.count(NAME_MIDDLE_INITIAL));
2422 EXPECT_TRUE(non_empty_types.count(NAME_LAST));
2423 EXPECT_TRUE(non_empty_types.count(NAME_FULL));
2424 EXPECT_TRUE(non_empty_types.count(EMAIL_ADDRESS));
2425 EXPECT_TRUE(non_empty_types.count(COMPANY_NAME));
2426 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_LINE1));
2427 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_LINE2));
2428 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_STREET_ADDRESS));
2429 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_CITY));
2430 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_STATE));
2431 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_ZIP));
2432 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_COUNTRY));
2433 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_NUMBER));
2434 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_CITY_CODE));
2435 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_COUNTRY_CODE));
2436 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_CITY_AND_NUMBER));
2437 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_WHOLE_NUMBER));
2439 // Test with credit card information also stored.
2440 CreditCard credit_card(base::GenerateGUID(), "https://www.example.com");
2441 test::SetCreditCardInfo(&credit_card,
2442 "John Dillinger", "423456789012" /* Visa */,
2443 "01", "2010");
2444 personal_data_->AddCreditCard(credit_card);
2446 // Verify that the web database has been updated and the notification sent.
2447 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2448 .WillOnce(QuitMainMessageLoop());
2449 base::MessageLoop::current()->Run();
2451 personal_data_->GetNonEmptyTypes(&non_empty_types);
2452 EXPECT_EQ(27U, non_empty_types.size());
2453 EXPECT_TRUE(non_empty_types.count(NAME_FIRST));
2454 EXPECT_TRUE(non_empty_types.count(NAME_MIDDLE));
2455 EXPECT_TRUE(non_empty_types.count(NAME_MIDDLE_INITIAL));
2456 EXPECT_TRUE(non_empty_types.count(NAME_LAST));
2457 EXPECT_TRUE(non_empty_types.count(NAME_FULL));
2458 EXPECT_TRUE(non_empty_types.count(EMAIL_ADDRESS));
2459 EXPECT_TRUE(non_empty_types.count(COMPANY_NAME));
2460 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_LINE1));
2461 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_LINE2));
2462 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_STREET_ADDRESS));
2463 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_CITY));
2464 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_STATE));
2465 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_ZIP));
2466 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_COUNTRY));
2467 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_NUMBER));
2468 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_CITY_CODE));
2469 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_COUNTRY_CODE));
2470 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_CITY_AND_NUMBER));
2471 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_WHOLE_NUMBER));
2472 EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_NAME));
2473 EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_NUMBER));
2474 EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_TYPE));
2475 EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_EXP_MONTH));
2476 EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_EXP_2_DIGIT_YEAR));
2477 EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_EXP_4_DIGIT_YEAR));
2478 EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR));
2479 EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR));
2482 TEST_F(PersonalDataManagerTest, IncognitoReadOnly) {
2483 ASSERT_TRUE(personal_data_->GetProfiles().empty());
2484 ASSERT_TRUE(personal_data_->GetCreditCards().empty());
2486 AutofillProfile steve_jobs(base::GenerateGUID(), "https://www.example.com");
2487 test::SetProfileInfo(&steve_jobs, "Steven", "Paul", "Jobs", "sjobs@apple.com",
2488 "Apple Computer, Inc.", "1 Infinite Loop", "", "Cupertino", "CA", "95014",
2489 "US", "(800) 275-2273");
2490 personal_data_->AddProfile(steve_jobs);
2492 CreditCard bill_gates(base::GenerateGUID(), "https://www.example.com");
2493 test::SetCreditCardInfo(
2494 &bill_gates, "William H. Gates", "5555555555554444", "1", "2020");
2495 personal_data_->AddCreditCard(bill_gates);
2497 // The personal data manager should be able to read existing profiles in an
2498 // off-the-record context.
2499 ResetPersonalDataManager(USER_MODE_INCOGNITO);
2500 ASSERT_EQ(1U, personal_data_->GetProfiles().size());
2501 ASSERT_EQ(1U, personal_data_->GetCreditCards().size());
2503 // No adds, saves, or updates should take effect.
2504 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()).Times(0);
2506 // Add profiles or credit card shouldn't work.
2507 personal_data_->AddProfile(test::GetFullProfile());
2509 CreditCard larry_page(base::GenerateGUID(), "https://www.example.com");
2510 test::SetCreditCardInfo(
2511 &larry_page, "Lawrence Page", "4111111111111111", "10", "2025");
2512 personal_data_->AddCreditCard(larry_page);
2514 ResetPersonalDataManager(USER_MODE_INCOGNITO);
2515 EXPECT_EQ(1U, personal_data_->GetProfiles().size());
2516 EXPECT_EQ(1U, personal_data_->GetCreditCards().size());
2518 // Saving or creating profiles from imported profiles shouldn't work.
2519 steve_jobs.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Steve"));
2520 personal_data_->SaveImportedProfile(steve_jobs);
2522 bill_gates.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Bill Gates"));
2523 personal_data_->SaveImportedCreditCard(bill_gates);
2525 ResetPersonalDataManager(USER_MODE_INCOGNITO);
2526 EXPECT_EQ(ASCIIToUTF16("Steven"),
2527 personal_data_->GetProfiles()[0]->GetRawInfo(NAME_FIRST));
2528 EXPECT_EQ(ASCIIToUTF16("William H. Gates"),
2529 personal_data_->GetCreditCards()[0]->GetRawInfo(CREDIT_CARD_NAME));
2531 // Updating existing profiles shouldn't work.
2532 steve_jobs.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Steve"));
2533 personal_data_->UpdateProfile(steve_jobs);
2535 bill_gates.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Bill Gates"));
2536 personal_data_->UpdateCreditCard(bill_gates);
2538 ResetPersonalDataManager(USER_MODE_INCOGNITO);
2539 EXPECT_EQ(ASCIIToUTF16("Steven"),
2540 personal_data_->GetProfiles()[0]->GetRawInfo(NAME_FIRST));
2541 EXPECT_EQ(ASCIIToUTF16("William H. Gates"),
2542 personal_data_->GetCreditCards()[0]->GetRawInfo(CREDIT_CARD_NAME));
2544 // Removing shouldn't work.
2545 personal_data_->RemoveByGUID(steve_jobs.guid());
2546 personal_data_->RemoveByGUID(bill_gates.guid());
2548 ResetPersonalDataManager(USER_MODE_INCOGNITO);
2549 EXPECT_EQ(1U, personal_data_->GetProfiles().size());
2550 EXPECT_EQ(1U, personal_data_->GetCreditCards().size());
2553 TEST_F(PersonalDataManagerTest, DefaultCountryCodeIsCached) {
2554 // The return value should always be some country code, no matter what.
2555 std::string default_country =
2556 personal_data_->GetDefaultCountryCodeForNewAddress();
2557 EXPECT_EQ(2U, default_country.size());
2559 AutofillProfile moose(base::GenerateGUID(), "Chrome settings");
2560 test::SetProfileInfo(&moose, "Moose", "P", "McMahon", "mpm@example.com",
2561 "", "1 Taiga TKTR", "", "Calgary", "AB", "T2B 2K2",
2562 "CA", "(800) 555-9000");
2563 personal_data_->AddProfile(moose);
2564 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2565 .WillOnce(QuitMainMessageLoop());
2566 base::MessageLoop::current()->Run();
2567 // The value is cached and doesn't change even after adding an address.
2568 EXPECT_EQ(default_country,
2569 personal_data_->GetDefaultCountryCodeForNewAddress());
2571 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()).Times(2);
2573 // Disabling Autofill blows away this cache and shouldn't account for Autofill
2574 // profiles.
2575 prefs_->SetBoolean(prefs::kAutofillEnabled, false);
2576 EXPECT_EQ(default_country,
2577 personal_data_->GetDefaultCountryCodeForNewAddress());
2579 // Enabling Autofill blows away the cached value and should reflect the new
2580 // value (accounting for profiles).
2581 prefs_->SetBoolean(prefs::kAutofillEnabled, true);
2582 EXPECT_EQ(base::UTF16ToUTF8(moose.GetRawInfo(ADDRESS_HOME_COUNTRY)),
2583 personal_data_->GetDefaultCountryCodeForNewAddress());
2586 TEST_F(PersonalDataManagerTest, DefaultCountryCodeComesFromProfiles) {
2587 AutofillProfile moose(base::GenerateGUID(), "Chrome settings");
2588 test::SetProfileInfo(&moose, "Moose", "P", "McMahon", "mpm@example.com",
2589 "", "1 Taiga TKTR", "", "Calgary", "AB", "T2B 2K2",
2590 "CA", "(800) 555-9000");
2591 personal_data_->AddProfile(moose);
2592 ResetPersonalDataManager(USER_MODE_NORMAL);
2593 EXPECT_EQ("CA", personal_data_->GetDefaultCountryCodeForNewAddress());
2595 // Multiple profiles cast votes.
2596 AutofillProfile armadillo(base::GenerateGUID(), "Chrome settings");
2597 test::SetProfileInfo(&armadillo, "Armin", "Dill", "Oh", "ado@example.com",
2598 "", "1 Speed Bump", "", "Lubbock", "TX", "77500",
2599 "MX", "(800) 555-9000");
2600 AutofillProfile armadillo2(base::GenerateGUID(), "Chrome settings");
2601 test::SetProfileInfo(&armadillo2, "Armin", "Dill", "Oh", "ado@example.com",
2602 "", "2 Speed Bump", "", "Lubbock", "TX", "77500",
2603 "MX", "(800) 555-9000");
2604 personal_data_->AddProfile(armadillo);
2605 personal_data_->AddProfile(armadillo2);
2606 ResetPersonalDataManager(USER_MODE_NORMAL);
2607 EXPECT_EQ("MX", personal_data_->GetDefaultCountryCodeForNewAddress());
2609 personal_data_->RemoveByGUID(armadillo.guid());
2610 personal_data_->RemoveByGUID(armadillo2.guid());
2611 ResetPersonalDataManager(USER_MODE_NORMAL);
2612 // Verified profiles count more.
2613 armadillo.set_origin("http://randomwebsite.com");
2614 armadillo2.set_origin("http://randomwebsite.com");
2615 personal_data_->AddProfile(armadillo);
2616 personal_data_->AddProfile(armadillo2);
2617 ResetPersonalDataManager(USER_MODE_NORMAL);
2618 EXPECT_EQ("CA", personal_data_->GetDefaultCountryCodeForNewAddress());
2620 personal_data_->RemoveByGUID(armadillo.guid());
2621 ResetPersonalDataManager(USER_MODE_NORMAL);
2622 // But unverified profiles can be a tie breaker.
2623 armadillo.set_origin("Chrome settings");
2624 personal_data_->AddProfile(armadillo);
2625 ResetPersonalDataManager(USER_MODE_NORMAL);
2626 EXPECT_EQ("MX", personal_data_->GetDefaultCountryCodeForNewAddress());
2628 // Invalid country codes are ignored.
2629 personal_data_->RemoveByGUID(armadillo.guid());
2630 personal_data_->RemoveByGUID(moose.guid());
2631 AutofillProfile space_invader(base::GenerateGUID(), "Chrome settings");
2632 test::SetProfileInfo(&space_invader, "Marty", "", "Martian",
2633 "mm@example.com", "", "1 Flying Object", "", "Valles Marineris", "",
2634 "", "XX", "");
2635 personal_data_->AddProfile(moose);
2636 ResetPersonalDataManager(USER_MODE_NORMAL);
2637 EXPECT_EQ("MX", personal_data_->GetDefaultCountryCodeForNewAddress());
2640 TEST_F(PersonalDataManagerTest, UpdateLanguageCodeInProfile) {
2641 AutofillProfile profile(base::GenerateGUID(), "https://www.example.com");
2642 test::SetProfileInfo(&profile,
2643 "Marion", "Mitchell", "Morrison",
2644 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
2645 "91601", "US", "12345678910");
2646 personal_data_->AddProfile(profile);
2648 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2649 .WillOnce(QuitMainMessageLoop());
2650 base::MessageLoop::current()->Run();
2652 profile.set_language_code("en");
2653 personal_data_->UpdateProfile(profile);
2655 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2656 .WillOnce(QuitMainMessageLoop());
2657 base::MessageLoop::current()->Run();
2659 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
2660 ASSERT_EQ(1U, results.size());
2661 EXPECT_EQ(0, profile.Compare(*results[0]));
2662 EXPECT_EQ("en", results[0]->language_code());
2665 TEST_F(PersonalDataManagerTest, GetProfileSuggestions) {
2666 AutofillProfile profile(base::GenerateGUID(), "https://www.example.com");
2667 test::SetProfileInfo(&profile,
2668 "Marion", "Mitchell", "Morrison",
2669 "johnwayne@me.xyz", "Fox",
2670 "123 Zoo St.\nSecond Line\nThird line", "unit 5", "Hollywood", "CA",
2671 "91601", "US", "12345678910");
2672 personal_data_->AddProfile(profile);
2673 ResetPersonalDataManager(USER_MODE_NORMAL);
2675 std::vector<Suggestion> suggestions = personal_data_->GetProfileSuggestions(
2676 AutofillType(ADDRESS_HOME_STREET_ADDRESS), base::ASCIIToUTF16("123"),
2677 false, std::vector<ServerFieldType>());
2678 ASSERT_FALSE(suggestions.empty());
2679 EXPECT_EQ(suggestions[0].value,
2680 base::ASCIIToUTF16("123 Zoo St., Second Line, Third line, unit 5"));
2683 TEST_F(PersonalDataManagerTest, GetProfileSuggestionsHideSubsets) {
2684 AutofillProfile profile(base::GenerateGUID(), "https://www.example.com");
2685 test::SetProfileInfo(&profile, "Marion", "Mitchell", "Morrison",
2686 "johnwayne@me.xyz", "Fox",
2687 "123 Zoo St.\nSecond Line\nThird line", "unit 5",
2688 "Hollywood", "CA", "91601", "US", "12345678910");
2690 // Dupe profile, except different in email address (irrelevant for this form).
2691 AutofillProfile profile1 = profile;
2692 profile1.set_guid(base::GenerateGUID());
2693 profile1.SetRawInfo(EMAIL_ADDRESS, base::ASCIIToUTF16("spam_me@example.com"));
2695 // Dupe profile, except different in address state.
2696 AutofillProfile profile2 = profile;
2697 profile2.set_guid(base::GenerateGUID());
2698 profile2.SetRawInfo(ADDRESS_HOME_STATE, base::ASCIIToUTF16("TX"));
2700 // Subset profile.
2701 AutofillProfile profile3 = profile;
2702 profile3.set_guid(base::GenerateGUID());
2703 profile3.SetRawInfo(ADDRESS_HOME_STATE, base::string16());
2705 // For easier results verification, make sure |profile| is suggested first.
2706 profile.set_use_count(5);
2707 personal_data_->AddProfile(profile);
2708 personal_data_->AddProfile(profile1);
2709 personal_data_->AddProfile(profile2);
2710 personal_data_->AddProfile(profile3);
2711 ResetPersonalDataManager(USER_MODE_NORMAL);
2713 // Simulate a form with street address, city and state.
2714 std::vector<ServerFieldType> types;
2715 types.push_back(ADDRESS_HOME_CITY);
2716 types.push_back(ADDRESS_HOME_STATE);
2717 std::vector<Suggestion> suggestions = personal_data_->GetProfileSuggestions(
2718 AutofillType(ADDRESS_HOME_STREET_ADDRESS), base::ASCIIToUTF16("123"),
2719 false, types);
2720 ASSERT_EQ(2U, suggestions.size());
2721 EXPECT_EQ(base::ASCIIToUTF16("Hollywood, CA"), suggestions[0].label);
2722 EXPECT_EQ(base::ASCIIToUTF16("Hollywood, TX"), suggestions[1].label);
2725 TEST_F(PersonalDataManagerTest, GetCreditCardSuggestions) {
2726 EnableWalletCardImport();
2728 CreditCard credit_card0("287151C8-6AB1-487C-9095-28E80BE5DA15",
2729 "https://www.example.com");
2730 test::SetCreditCardInfo(&credit_card0,
2731 "Clyde Barrow", "347666888555" /* American Express */, "04", "2015");
2732 credit_card0.set_use_count(2);
2733 personal_data_->AddCreditCard(credit_card0);
2735 CreditCard credit_card1("1141084B-72D7-4B73-90CF-3D6AC154673B",
2736 "https://www.example.com");
2737 credit_card1.set_use_count(3);
2738 test::SetCreditCardInfo(&credit_card1, "John Dillinger", "", "01", "2010");
2739 personal_data_->AddCreditCard(credit_card1);
2741 CreditCard credit_card2("002149C1-EE28-4213-A3B9-DA243FFF021B",
2742 "https://www.example.com");
2743 credit_card2.set_use_count(1);
2744 test::SetCreditCardInfo(&credit_card2,
2745 "Bonnie Parker", "518765432109" /* Mastercard */, "", "");
2746 personal_data_->AddCreditCard(credit_card2);
2748 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2749 .WillOnce(QuitMainMessageLoop());
2750 base::MessageLoop::current()->Run();
2752 // Sublabel is card number when filling name (exact format depends on
2753 // the platform, but the last 4 digits should appear).
2754 std::vector<Suggestion> suggestions =
2755 personal_data_->GetCreditCardSuggestions(
2756 AutofillType(CREDIT_CARD_NAME), base::string16());
2757 ASSERT_EQ(3U, suggestions.size());
2758 // Ordered by MFU.
2759 EXPECT_EQ(ASCIIToUTF16("Clyde Barrow"), suggestions[1].value);
2760 EXPECT_TRUE(suggestions[1].label.find(ASCIIToUTF16("8555")) !=
2761 base::string16::npos);
2762 EXPECT_EQ(ASCIIToUTF16("John Dillinger"), suggestions[0].value);
2763 EXPECT_EQ(base::string16(), suggestions[0].label);
2764 EXPECT_EQ(ASCIIToUTF16("Bonnie Parker"), suggestions[2].value);
2765 EXPECT_TRUE(suggestions[2].label.find(ASCIIToUTF16("2109")) !=
2766 base::string16::npos);
2768 // Sublabel is expiration date when filling card number.
2769 suggestions = personal_data_->GetCreditCardSuggestions(
2770 AutofillType(CREDIT_CARD_NUMBER), base::string16());
2771 ASSERT_EQ(2U, suggestions.size());
2772 EXPECT_EQ(UTF8ToUTF16(
2773 "Amex\xC2\xA0\xE2\x8B\xAF"
2774 "8555"),
2775 suggestions[0].value);
2776 EXPECT_EQ(ASCIIToUTF16("04/15"), suggestions[0].label);
2777 EXPECT_EQ(UTF8ToUTF16(
2778 "MasterCard\xC2\xA0\xE2\x8B\xAF"
2779 "2109"),
2780 suggestions[1].value);
2781 EXPECT_EQ(base::string16(), suggestions[1].label);
2783 // Add some server cards. If there are local dupes, the locals should be
2784 // hidden.
2785 std::vector<CreditCard> server_cards;
2786 // This server card matches a local card, except the local card is missing the
2787 // number. This should count as a dupe. The locally saved card takes
2788 // precedence.
2789 server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "a123"));
2790 test::SetCreditCardInfo(&server_cards.back(), "John Dillinger",
2791 "9012" /* Visa */, "01", "2010");
2792 server_cards.back().SetTypeForMaskedCard(kVisaCard);
2794 // This server card is identical to a local card, but has a different
2795 // card type. Not a dupe.
2796 server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "b456"));
2797 test::SetCreditCardInfo(&server_cards.back(), "Bonnie Parker",
2798 "2109", "12", "2012");
2799 server_cards.back().SetTypeForMaskedCard(kVisaCard);
2801 // This unmasked server card is a dupe.
2802 server_cards.push_back(CreditCard(CreditCard::FULL_SERVER_CARD, "c789"));
2803 test::SetCreditCardInfo(&server_cards.back(), "Clyde Barrow",
2804 "347666888555" /* American Express */, "04", "2015");
2806 test::SetServerCreditCards(autofill_table_, server_cards);
2807 personal_data_->Refresh();
2808 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2809 .WillOnce(QuitMainMessageLoop());
2810 base::MessageLoop::current()->Run();
2812 suggestions = personal_data_->GetCreditCardSuggestions(
2813 AutofillType(CREDIT_CARD_NAME), base::string16());
2814 ASSERT_EQ(4U, suggestions.size());
2815 EXPECT_EQ(ASCIIToUTF16("John Dillinger"), suggestions[0].value);
2816 EXPECT_EQ(suggestions[0].backend_id, credit_card1.guid());
2817 EXPECT_EQ(ASCIIToUTF16("Clyde Barrow"), suggestions[1].value);
2818 EXPECT_NE(suggestions[1].backend_id, credit_card0.guid());
2819 EXPECT_EQ(ASCIIToUTF16("Bonnie Parker"), suggestions[2].value);
2820 EXPECT_EQ(suggestions[2].backend_id, credit_card2.guid());
2821 EXPECT_EQ(ASCIIToUTF16("Bonnie Parker"), suggestions[3].value);
2822 EXPECT_NE(suggestions[3].backend_id, credit_card2.guid());
2824 suggestions = personal_data_->GetCreditCardSuggestions(
2825 AutofillType(CREDIT_CARD_NUMBER), base::string16());
2826 ASSERT_EQ(4U, suggestions.size());
2827 EXPECT_EQ(UTF8ToUTF16(
2828 "Amex\xC2\xA0\xE2\x8B\xAF"
2829 "8555"),
2830 suggestions[0].value);
2831 EXPECT_EQ(UTF8ToUTF16(
2832 "MasterCard\xC2\xA0\xE2\x8B\xAF"
2833 "2109"),
2834 suggestions[1].value);
2835 EXPECT_EQ(UTF8ToUTF16(
2836 "Visa\xC2\xA0\xE2\x8B\xAF"
2837 "9012"),
2838 suggestions[2].value);
2839 EXPECT_EQ(UTF8ToUTF16(
2840 "Visa\xC2\xA0\xE2\x8B\xAF"
2841 "2109"),
2842 suggestions[3].value);
2844 // Make sure a full server card can be a dupe of more than one local card.
2845 CreditCard credit_card3("4141084B-72D7-4B73-90CF-3D6AC154673B",
2846 "https://www.example.com");
2847 test::SetCreditCardInfo(&credit_card3, "Clyde Barrow", "", "04", "");
2848 personal_data_->AddCreditCard(credit_card3);
2850 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2851 .WillOnce(QuitMainMessageLoop());
2852 base::MessageLoop::current()->Run();
2854 suggestions = personal_data_->GetCreditCardSuggestions(
2855 AutofillType(CREDIT_CARD_NAME), base::string16());
2856 ASSERT_EQ(4U, suggestions.size());
2857 EXPECT_EQ(ASCIIToUTF16("John Dillinger"), suggestions[0].value);
2858 EXPECT_EQ(ASCIIToUTF16("Clyde Barrow"), suggestions[1].value);
2859 EXPECT_EQ(ASCIIToUTF16("Bonnie Parker"), suggestions[2].value);
2860 EXPECT_EQ(ASCIIToUTF16("Bonnie Parker"), suggestions[3].value);
2863 #if defined(OS_MACOSX) && !defined(OS_IOS)
2864 TEST_F(PersonalDataManagerTest, ShowAddressBookPrompt) {
2865 // TODO(erikchen): After Address Book integration has been disabled for 6
2866 // weeks, and there are no major problems, rip out all the code. Expected
2867 // removal date: 07/15/2015. http://crbug.com/488146.
2868 return;
2870 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()).Times(2);
2872 AutofillType type(ADDRESS_HOME_STREET_ADDRESS);
2874 prefs_->SetBoolean(prefs::kAutofillEnabled, false);
2875 EXPECT_FALSE(personal_data_->ShouldShowAccessAddressBookSuggestion(type));
2877 prefs_->SetBoolean(prefs::kAutofillEnabled, true);
2878 EXPECT_TRUE(personal_data_->ShouldShowAccessAddressBookSuggestion(type));
2880 // Adding an Autofill Profile should prevent the prompt from appearing.
2881 AutofillProfile profile(base::GenerateGUID(), "https://www.example.com/");
2882 test::SetProfileInfo(&profile,
2883 "Marion", "Mitchell", "Morrison",
2884 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
2885 "91601", "US", "12345678910");
2886 personal_data_->AddProfile(profile);
2888 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2889 .WillOnce(QuitMainMessageLoop());
2890 base::MessageLoop::current()->Run();
2892 EXPECT_FALSE(personal_data_->ShouldShowAccessAddressBookSuggestion(type));
2895 // Tests that the logic to show the access Address Book prompt respects the
2896 // preference that indicates the total number of times the prompt has already
2897 // been shown.
2898 TEST_F(PersonalDataManagerTest, MaxTimesToShowAddressBookPrompt) {
2899 // TODO(erikchen): After Address Book integration has been disabled for 6
2900 // weeks, and there are no major problems, rip out all the code. Expected
2901 // removal date: 07/15/2015. http://crbug.com/488146.
2902 return;
2903 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()).Times(1);
2905 AutofillType type(ADDRESS_HOME_STREET_ADDRESS);
2907 prefs_->SetBoolean(prefs::kAutofillEnabled, true);
2908 EXPECT_TRUE(personal_data_->ShouldShowAccessAddressBookSuggestion(type));
2910 prefs_->SetInteger(prefs::kAutofillMacAddressBookShowedCount, 4);
2911 EXPECT_TRUE(personal_data_->ShouldShowAccessAddressBookSuggestion(type));
2913 prefs_->SetInteger(prefs::kAutofillMacAddressBookShowedCount, 6);
2914 EXPECT_FALSE(personal_data_->ShouldShowAccessAddressBookSuggestion(type));
2916 #endif // defined(OS_MACOSX) && !defined(OS_IOS)
2918 TEST_F(PersonalDataManagerTest, RecordUseOf) {
2919 AutofillProfile profile(autofill::test::GetFullProfile());
2920 EXPECT_EQ(0U, profile.use_count());
2921 EXPECT_EQ(base::Time(), profile.use_date());
2922 EXPECT_EQ(base::Time(), profile.modification_date());
2923 personal_data_->AddProfile(profile);
2925 CreditCard credit_card(base::GenerateGUID(), "https://www.example.com");
2926 test::SetCreditCardInfo(&credit_card, "John Dillinger",
2927 "423456789012" /* Visa */, "01", "2010");
2928 EXPECT_EQ(0U, credit_card.use_count());
2929 EXPECT_EQ(base::Time(), credit_card.use_date());
2930 EXPECT_EQ(base::Time(), credit_card.modification_date());
2931 personal_data_->AddCreditCard(credit_card);
2933 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2934 .WillOnce(QuitMainMessageLoop());
2935 base::MessageLoop::current()->Run();
2937 // Notify the PDM that the profile and credit card were used.
2938 AutofillProfile* added_profile =
2939 personal_data_->GetProfileByGUID(profile.guid());
2940 ASSERT_TRUE(added_profile);
2941 EXPECT_EQ(*added_profile, profile);
2942 EXPECT_EQ(0U, added_profile->use_count());
2943 EXPECT_EQ(base::Time(), added_profile->use_date());
2944 EXPECT_NE(base::Time(), added_profile->modification_date());
2945 personal_data_->RecordUseOf(profile);
2947 CreditCard* added_card =
2948 personal_data_->GetCreditCardByGUID(credit_card.guid());
2949 ASSERT_TRUE(added_card);
2950 EXPECT_EQ(*added_card, credit_card);
2951 EXPECT_EQ(0U, added_card->use_count());
2952 EXPECT_EQ(base::Time(), added_card->use_date());
2953 EXPECT_NE(base::Time(), added_card->modification_date());
2954 personal_data_->RecordUseOf(credit_card);
2956 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2957 .WillOnce(QuitMainMessageLoop());
2958 base::MessageLoop::current()->Run();
2960 // Verify usage stats are updated.
2961 added_profile = personal_data_->GetProfileByGUID(profile.guid());
2962 ASSERT_TRUE(added_profile);
2963 EXPECT_EQ(1U, added_profile->use_count());
2964 EXPECT_NE(base::Time(), added_profile->use_date());
2965 EXPECT_NE(base::Time(), added_profile->modification_date());
2967 added_card = personal_data_->GetCreditCardByGUID(credit_card.guid());
2968 ASSERT_TRUE(added_card);
2969 EXPECT_EQ(1U, added_card->use_count());
2970 EXPECT_NE(base::Time(), added_card->use_date());
2971 EXPECT_NE(base::Time(), added_card->modification_date());
2974 TEST_F(PersonalDataManagerTest, UpdateServerCreditCardUsageStats) {
2975 EnableWalletCardImport();
2977 std::vector<CreditCard> server_cards;
2978 server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "a123"));
2979 test::SetCreditCardInfo(&server_cards.back(), "John Dillinger",
2980 "9012" /* Visa */, "01", "2010");
2981 server_cards.back().SetTypeForMaskedCard(kVisaCard);
2983 server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "b456"));
2984 test::SetCreditCardInfo(&server_cards.back(), "Bonnie Parker",
2985 "4444" /* Mastercard */, "12", "2012");
2986 server_cards.back().SetTypeForMaskedCard(kMasterCard);
2988 server_cards.push_back(CreditCard(CreditCard::FULL_SERVER_CARD, "c789"));
2989 test::SetCreditCardInfo(&server_cards.back(), "Clyde Barrow",
2990 "347666888555" /* American Express */, "04", "2015");
2992 test::SetServerCreditCards(autofill_table_, server_cards);
2993 personal_data_->Refresh();
2995 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2996 .WillOnce(QuitMainMessageLoop());
2997 base::MessageLoop::current()->Run();
2999 ASSERT_EQ(3U, personal_data_->GetCreditCards().size());
3001 if (!OfferStoreUnmaskedCards()) {
3002 for (CreditCard* card : personal_data_->GetCreditCards()) {
3003 EXPECT_EQ(CreditCard::MASKED_SERVER_CARD, card->record_type());
3005 // The rest of this test doesn't work if we're force-masking all unmasked
3006 // cards.
3007 return;
3010 // The GUIDs will be different, so just compare the data.
3011 for (size_t i = 0; i < 3; ++i)
3012 EXPECT_EQ(0, server_cards[i].Compare(*personal_data_->GetCreditCards()[i]));
3014 CreditCard* unmasked_card = &server_cards.front();
3015 unmasked_card->set_record_type(CreditCard::FULL_SERVER_CARD);
3016 unmasked_card->SetNumber(ASCIIToUTF16("423456789012"));
3017 EXPECT_NE(0, unmasked_card->Compare(
3018 *personal_data_->GetCreditCards().front()));
3019 personal_data_->UpdateServerCreditCard(*unmasked_card);
3021 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
3022 .WillOnce(QuitMainMessageLoop());
3023 base::MessageLoop::current()->Run();
3024 ASSERT_EQ(3U, personal_data_->GetCreditCards().size());
3026 for (size_t i = 0; i < 3; ++i)
3027 EXPECT_EQ(0, server_cards[i].Compare(*personal_data_->GetCreditCards()[i]));
3029 // For an unmasked card, usage data starts out as 1 and Now().
3030 EXPECT_EQ(1U, personal_data_->GetCreditCards()[0]->use_count());
3031 EXPECT_NE(base::Time(), personal_data_->GetCreditCards()[0]->use_date());
3033 EXPECT_EQ(0U, personal_data_->GetCreditCards()[1]->use_count());
3034 EXPECT_EQ(base::Time(), personal_data_->GetCreditCards()[1]->use_date());
3036 // Having unmasked this card, usage stats should be 1 and Now().
3037 EXPECT_EQ(1U, personal_data_->GetCreditCards()[2]->use_count());
3038 EXPECT_NE(base::Time(), personal_data_->GetCreditCards()[2]->use_date());
3039 base::Time initial_use_date = personal_data_->GetCreditCards()[2]->use_date();
3041 server_cards.back().set_guid(personal_data_->GetCreditCards()[2]->guid());
3042 personal_data_->RecordUseOf(server_cards.back());
3043 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
3044 .WillOnce(QuitMainMessageLoop());
3045 base::MessageLoop::current()->Run();
3046 ASSERT_EQ(3U, personal_data_->GetCreditCards().size());
3048 EXPECT_EQ(1U, personal_data_->GetCreditCards()[0]->use_count());
3049 EXPECT_NE(base::Time(), personal_data_->GetCreditCards()[0]->use_date());
3051 EXPECT_EQ(0U, personal_data_->GetCreditCards()[1]->use_count());
3052 EXPECT_EQ(base::Time(), personal_data_->GetCreditCards()[1]->use_date());
3054 EXPECT_EQ(2U, personal_data_->GetCreditCards()[2]->use_count());
3055 EXPECT_NE(base::Time(), personal_data_->GetCreditCards()[2]->use_date());
3056 // Time may or may not have elapsed between unmasking and RecordUseOf.
3057 EXPECT_LE(initial_use_date, personal_data_->GetCreditCards()[2]->use_date());
3059 // Can record usage stats on masked cards.
3060 server_cards[1].set_guid(personal_data_->GetCreditCards()[1]->guid());
3061 personal_data_->RecordUseOf(server_cards[1]);
3062 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
3063 .WillOnce(QuitMainMessageLoop());
3064 base::MessageLoop::current()->Run();
3065 ASSERT_EQ(3U, personal_data_->GetCreditCards().size());
3066 EXPECT_EQ(1U, personal_data_->GetCreditCards()[1]->use_count());
3067 EXPECT_NE(base::Time(), personal_data_->GetCreditCards()[1]->use_date());
3069 // Upgrading to unmasked retains the usage stats (and increments them).
3070 CreditCard* unmasked_card2 = &server_cards[1];
3071 unmasked_card2->set_record_type(CreditCard::FULL_SERVER_CARD);
3072 unmasked_card2->SetNumber(ASCIIToUTF16("5555555555554444"));
3073 personal_data_->UpdateServerCreditCard(*unmasked_card2);
3075 server_cards[1].set_guid(personal_data_->GetCreditCards()[1]->guid());
3076 personal_data_->RecordUseOf(server_cards[1]);
3077 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
3078 .WillOnce(QuitMainMessageLoop());
3079 base::MessageLoop::current()->Run();
3080 ASSERT_EQ(3U, personal_data_->GetCreditCards().size());
3081 EXPECT_EQ(2U, personal_data_->GetCreditCards()[1]->use_count());
3082 EXPECT_NE(base::Time(), personal_data_->GetCreditCards()[1]->use_date());
3085 TEST_F(PersonalDataManagerTest, ClearAllServerData) {
3086 // Add a server card.
3087 std::vector<CreditCard> server_cards;
3088 server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "a123"));
3089 test::SetCreditCardInfo(&server_cards.back(), "John Dillinger",
3090 "9012" /* Visa */, "01", "2010");
3091 server_cards.back().SetTypeForMaskedCard(kVisaCard);
3092 test::SetServerCreditCards(autofill_table_, server_cards);
3093 personal_data_->Refresh();
3095 // Need to set the google services username
3096 EnableWalletCardImport();
3098 // Add a server profile.
3099 std::vector<AutofillProfile> server_profiles;
3100 server_profiles.push_back(
3101 AutofillProfile(AutofillProfile::SERVER_PROFILE, "a123"));
3102 test::SetProfileInfo(&server_profiles.back(), "John", "", "Doe", "",
3103 "ACME Corp", "500 Oak View", "Apt 8", "Houston", "TX",
3104 "77401", "US", "");
3105 autofill_table_->SetServerProfiles(server_profiles);
3107 // The card and profile should be there.
3108 ResetPersonalDataManager(USER_MODE_NORMAL);
3109 EXPECT_FALSE(personal_data_->GetCreditCards().empty());
3110 EXPECT_FALSE(personal_data_->GetProfiles().empty());
3112 personal_data_->ClearAllServerData();
3114 // Reload the database, everything should be gone.
3115 ResetPersonalDataManager(USER_MODE_NORMAL);
3116 EXPECT_TRUE(personal_data_->GetCreditCards().empty());
3117 EXPECT_TRUE(personal_data_->GetProfiles().empty());
3120 TEST_F(PersonalDataManagerTest, DontDuplicateServerCard) {
3121 EnableWalletCardImport();
3123 std::vector<CreditCard> server_cards;
3124 server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "a123"));
3125 test::SetCreditCardInfo(&server_cards.back(), "John Dillinger",
3126 "1881" /* Visa */, "01", "2017");
3127 server_cards.back().SetTypeForMaskedCard(kVisaCard);
3129 server_cards.push_back(CreditCard(CreditCard::FULL_SERVER_CARD, "c789"));
3130 test::SetCreditCardInfo(&server_cards.back(), "Clyde Barrow",
3131 "347666888555" /* American Express */, "04", "2015");
3133 test::SetServerCreditCards(autofill_table_, server_cards);
3134 personal_data_->Refresh();
3135 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
3136 .WillOnce(QuitMainMessageLoop());
3137 base::MessageLoop::current()->Run();
3139 // A valid credit card form. A user re-types one of their masked cards.
3140 // We should offer to save.
3141 FormData form1;
3142 FormFieldData field;
3143 test::CreateTestFormField("Name on card:", "name_on_card", "John Dillinger",
3144 "text", &field);
3145 form1.fields.push_back(field);
3146 test::CreateTestFormField("Card Number:", "card_number", "4012888888881881",
3147 "text", &field);
3148 form1.fields.push_back(field);
3149 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
3150 form1.fields.push_back(field);
3151 test::CreateTestFormField("Exp Year:", "exp_year", "2017", "text", &field);
3152 form1.fields.push_back(field);
3154 FormStructure form_structure1(form1);
3155 form_structure1.DetermineHeuristicTypes();
3156 scoped_ptr<CreditCard> imported_credit_card;
3157 EXPECT_TRUE(
3158 personal_data_->ImportFormData(form_structure1, &imported_credit_card));
3159 EXPECT_TRUE(imported_credit_card);
3160 imported_credit_card.reset();
3162 // A user re-types (or fills with) an unmasked card. Don't offer to save
3163 // again.
3164 FormData form2;
3165 test::CreateTestFormField("Name on card:", "name_on_card", "Clyde Barrow",
3166 "text", &field);
3167 form2.fields.push_back(field);
3168 test::CreateTestFormField("Card Number:", "card_number", "347666888555",
3169 "text", &field);
3170 form2.fields.push_back(field);
3171 test::CreateTestFormField("Exp Month:", "exp_month", "04", "text", &field);
3172 form2.fields.push_back(field);
3173 test::CreateTestFormField("Exp Year:", "exp_year", "2015", "text", &field);
3174 form2.fields.push_back(field);
3176 FormStructure form_structure2(form2);
3177 form_structure2.DetermineHeuristicTypes();
3178 EXPECT_FALSE(
3179 personal_data_->ImportFormData(form_structure2, &imported_credit_card));
3180 EXPECT_FALSE(imported_credit_card);
3183 } // namespace autofill