ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / components / autofill / core / browser / personal_data_manager_unittest.cc
blob5955670e45af1c6ffef1dc67ab177441a787f97a
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/message_loop/message_loop.h"
15 #include "base/prefs/pref_service.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "base/synchronization/waitable_event.h"
18 #include "components/autofill/core/browser/autofill_profile.h"
19 #include "components/autofill/core/browser/autofill_test_utils.h"
20 #include "components/autofill/core/browser/form_structure.h"
21 #include "components/autofill/core/browser/personal_data_manager.h"
22 #include "components/autofill/core/browser/personal_data_manager_observer.h"
23 #include "components/autofill/core/browser/webdata/autofill_table.h"
24 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
25 #include "components/autofill/core/common/autofill_pref_names.h"
26 #include "components/autofill/core/common/autofill_switches.h"
27 #include "components/autofill/core/common/form_data.h"
28 #include "components/webdata/common/web_data_service_base.h"
29 #include "components/webdata/common/web_database_service.h"
30 #include "testing/gmock/include/gmock/gmock.h"
31 #include "testing/gtest/include/gtest/gtest.h"
33 using base::ASCIIToUTF16;
35 namespace autofill {
36 namespace {
38 enum UserMode { USER_MODE_NORMAL, USER_MODE_INCOGNITO };
40 ACTION(QuitMainMessageLoop) { base::MessageLoop::current()->Quit(); }
42 class PersonalDataLoadedObserverMock : public PersonalDataManagerObserver {
43 public:
44 PersonalDataLoadedObserverMock() {}
45 virtual ~PersonalDataLoadedObserverMock() {}
47 MOCK_METHOD0(OnPersonalDataChanged, void());
50 template <typename T>
51 bool CompareElements(T* a, T* b) {
52 return a->Compare(*b) < 0;
55 template <typename T>
56 bool ElementsEqual(T* a, T* b) {
57 return a->Compare(*b) == 0;
60 // Verifies that two vectors have the same elements (according to T::Compare)
61 // while ignoring order. This is useful because multiple profiles or credit
62 // cards that are added to the SQLite DB within the same second will be returned
63 // in GUID (aka random) order.
64 template <typename T>
65 void ExpectSameElements(const std::vector<T*>& expectations,
66 const std::vector<T*>& results) {
67 ASSERT_EQ(expectations.size(), results.size());
69 std::vector<T*> expectations_copy = expectations;
70 std::sort(
71 expectations_copy.begin(), expectations_copy.end(), CompareElements<T>);
72 std::vector<T*> results_copy = results;
73 std::sort(results_copy.begin(), results_copy.end(), CompareElements<T>);
75 EXPECT_EQ(std::mismatch(results_copy.begin(),
76 results_copy.end(),
77 expectations_copy.begin(),
78 ElementsEqual<T>).first,
79 results_copy.end());
82 } // anonymous namespace
84 class PersonalDataManagerTest : public testing::Test {
85 protected:
86 PersonalDataManagerTest() : autofill_table_(nullptr) {}
88 void SetUp() override {
89 prefs_ = test::PrefServiceForTesting();
90 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
91 base::FilePath path = temp_dir_.path().AppendASCII("TestWebDB");
92 web_database_ = new WebDatabaseService(path,
93 base::MessageLoopProxy::current(),
94 base::MessageLoopProxy::current());
95 // Hacky: hold onto a pointer but pass ownership.
96 autofill_table_ = new AutofillTable("en-US");
97 web_database_->AddTable(scoped_ptr<WebDatabaseTable>(autofill_table_));
98 web_database_->LoadDatabase();
99 autofill_database_service_ =
100 new AutofillWebDataService(web_database_,
101 base::MessageLoopProxy::current(),
102 base::MessageLoopProxy::current(),
103 WebDataServiceBase::ProfileErrorCallback());
104 autofill_database_service_->Init();
106 test::DisableSystemServices(prefs_.get());
107 ResetPersonalDataManager(USER_MODE_NORMAL);
110 void ResetPersonalDataManager(UserMode user_mode) {
111 bool is_incognito = (user_mode == USER_MODE_INCOGNITO);
112 personal_data_.reset(new PersonalDataManager("en"));
113 personal_data_->Init(
114 scoped_refptr<AutofillWebDataService>(autofill_database_service_),
115 prefs_.get(),
116 is_incognito);
117 personal_data_->AddObserver(&personal_data_observer_);
119 // Verify that the web database has been updated and the notification sent.
120 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
121 .WillOnce(QuitMainMessageLoop());
122 base::MessageLoop::current()->Run();
125 void EnableWalletCardImport() {
126 prefs_->SetBoolean(prefs::kAutofillWalletSyncExperimentEnabled, true);
129 // The temporary directory should be deleted at the end to ensure that
130 // files are not used anymore and deletion succeeds.
131 base::ScopedTempDir temp_dir_;
132 base::MessageLoopForUI message_loop_;
133 scoped_ptr<PrefService> prefs_;
134 scoped_refptr<AutofillWebDataService> autofill_database_service_;
135 scoped_refptr<WebDatabaseService> web_database_;
136 AutofillTable* autofill_table_; // weak ref
137 PersonalDataLoadedObserverMock personal_data_observer_;
138 scoped_ptr<PersonalDataManager> personal_data_;
141 TEST_F(PersonalDataManagerTest, AddProfile) {
142 // Add profile0 to the database.
143 AutofillProfile profile0(autofill::test::GetFullProfile());
144 profile0.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("j@s.com"));
145 personal_data_->AddProfile(profile0);
147 // Reload the database.
148 ResetPersonalDataManager(USER_MODE_NORMAL);
150 // Verify the addition.
151 const std::vector<AutofillProfile*>& results1 = personal_data_->GetProfiles();
152 ASSERT_EQ(1U, results1.size());
153 EXPECT_EQ(0, profile0.Compare(*results1[0]));
155 // Add profile with identical values. Duplicates should not get saved.
156 AutofillProfile profile0a = profile0;
157 profile0a.set_guid(base::GenerateGUID());
158 personal_data_->AddProfile(profile0a);
160 // Reload the database.
161 ResetPersonalDataManager(USER_MODE_NORMAL);
163 // Verify the non-addition.
164 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
165 ASSERT_EQ(1U, results2.size());
166 EXPECT_EQ(0, profile0.Compare(*results2[0]));
168 // New profile with different email.
169 AutofillProfile profile1 = profile0;
170 profile1.set_guid(base::GenerateGUID());
171 profile1.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("john@smith.com"));
173 // Add the different profile. This should save as a separate profile.
174 // Note that if this same profile was "merged" it would collapse to one
175 // profile with a multi-valued entry for email.
176 personal_data_->AddProfile(profile1);
178 // Reload the database.
179 ResetPersonalDataManager(USER_MODE_NORMAL);
181 // Verify the addition.
182 std::vector<AutofillProfile*> profiles;
183 profiles.push_back(&profile0);
184 profiles.push_back(&profile1);
185 ExpectSameElements(profiles, personal_data_->GetProfiles());
188 TEST_F(PersonalDataManagerTest, AddUpdateRemoveProfiles) {
189 AutofillProfile profile0(base::GenerateGUID(), "https://www.example.com");
190 test::SetProfileInfo(&profile0,
191 "Marion", "Mitchell", "Morrison",
192 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
193 "91601", "US", "12345678910");
195 AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com");
196 test::SetProfileInfo(&profile1,
197 "Josephine", "Alicia", "Saenz",
198 "joewayne@me.xyz", "Fox", "903 Apple Ct.", NULL, "Orlando", "FL", "32801",
199 "US", "19482937549");
201 AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com");
202 test::SetProfileInfo(&profile2,
203 "Josephine", "Alicia", "Saenz",
204 "joewayne@me.xyz", "Fox", "1212 Center.", "Bld. 5", "Orlando", "FL",
205 "32801", "US", "19482937549");
207 // Add two test profiles to the database.
208 personal_data_->AddProfile(profile0);
209 personal_data_->AddProfile(profile1);
211 // Verify that the web database has been updated and the notification sent.
212 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
213 .WillOnce(QuitMainMessageLoop());
214 base::MessageLoop::current()->Run();
216 std::vector<AutofillProfile*> profiles;
217 profiles.push_back(&profile0);
218 profiles.push_back(&profile1);
219 ExpectSameElements(profiles, personal_data_->GetProfiles());
221 // Update, remove, and add.
222 profile0.SetRawInfo(NAME_FIRST, ASCIIToUTF16("John"));
223 personal_data_->UpdateProfile(profile0);
224 personal_data_->RemoveByGUID(profile1.guid());
225 personal_data_->AddProfile(profile2);
227 // Verify that the web database has been updated and the notification sent.
228 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
229 .WillOnce(QuitMainMessageLoop());
230 base::MessageLoop::current()->Run();
232 profiles.clear();
233 profiles.push_back(&profile0);
234 profiles.push_back(&profile2);
235 ExpectSameElements(profiles, personal_data_->GetProfiles());
237 // Reset the PersonalDataManager. This tests that the personal data was saved
238 // to the web database, and that we can load the profiles from the web
239 // database.
240 ResetPersonalDataManager(USER_MODE_NORMAL);
242 // Verify that we've loaded the profiles from the web database.
243 ExpectSameElements(profiles, personal_data_->GetProfiles());
246 TEST_F(PersonalDataManagerTest, AddUpdateRemoveCreditCards) {
247 CreditCard credit_card0(base::GenerateGUID(), "https://www.example.com");
248 test::SetCreditCardInfo(&credit_card0,
249 "John Dillinger", "423456789012" /* Visa */, "01", "2010");
251 CreditCard credit_card1(base::GenerateGUID(), "https://www.example.com");
252 test::SetCreditCardInfo(&credit_card1,
253 "Bonnie Parker", "518765432109" /* Mastercard */, "12", "2012");
255 CreditCard credit_card2(base::GenerateGUID(), "https://www.example.com");
256 test::SetCreditCardInfo(&credit_card2,
257 "Clyde Barrow", "347666888555" /* American Express */, "04", "2015");
259 // Add two test credit cards to the database.
260 personal_data_->AddCreditCard(credit_card0);
261 personal_data_->AddCreditCard(credit_card1);
263 // Verify that the web database has been updated and the notification sent.
264 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
265 .WillOnce(QuitMainMessageLoop());
266 base::MessageLoop::current()->Run();
268 std::vector<CreditCard*> cards;
269 cards.push_back(&credit_card0);
270 cards.push_back(&credit_card1);
271 ExpectSameElements(cards, personal_data_->GetCreditCards());
273 // Update, remove, and add.
274 credit_card0.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Joe"));
275 personal_data_->UpdateCreditCard(credit_card0);
276 personal_data_->RemoveByGUID(credit_card1.guid());
277 personal_data_->AddCreditCard(credit_card2);
279 // Verify that the web database has been updated and the notification sent.
280 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
281 .WillOnce(QuitMainMessageLoop());
282 base::MessageLoop::current()->Run();
284 cards.clear();
285 cards.push_back(&credit_card0);
286 cards.push_back(&credit_card2);
287 ExpectSameElements(cards, personal_data_->GetCreditCards());
289 // Reset the PersonalDataManager. This tests that the personal data was saved
290 // to the web database, and that we can load the credit cards from the web
291 // database.
292 ResetPersonalDataManager(USER_MODE_NORMAL);
294 // Verify that we've loaded the credit cards from the web database.
295 cards.clear();
296 cards.push_back(&credit_card0);
297 cards.push_back(&credit_card2);
298 ExpectSameElements(cards, personal_data_->GetCreditCards());
301 TEST_F(PersonalDataManagerTest, UpdateUnverifiedProfilesAndCreditCards) {
302 // Start with unverified data.
303 AutofillProfile profile(base::GenerateGUID(), "https://www.example.com/");
304 test::SetProfileInfo(&profile,
305 "Marion", "Mitchell", "Morrison",
306 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
307 "91601", "US", "12345678910");
308 EXPECT_FALSE(profile.IsVerified());
310 CreditCard credit_card(base::GenerateGUID(), "https://www.example.com/");
311 test::SetCreditCardInfo(&credit_card,
312 "John Dillinger", "423456789012" /* Visa */, "01", "2010");
313 EXPECT_FALSE(credit_card.IsVerified());
315 // Add the data to the database.
316 personal_data_->AddProfile(profile);
317 personal_data_->AddCreditCard(credit_card);
319 // Verify that the web database has been updated and the notification sent.
320 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
321 .WillOnce(QuitMainMessageLoop());
322 base::MessageLoop::current()->Run();
324 const std::vector<AutofillProfile*>& profiles1 =
325 personal_data_->GetProfiles();
326 const std::vector<CreditCard*>& cards1 = personal_data_->GetCreditCards();
327 ASSERT_EQ(1U, profiles1.size());
328 ASSERT_EQ(1U, cards1.size());
329 EXPECT_EQ(0, profile.Compare(*profiles1[0]));
330 EXPECT_EQ(0, credit_card.Compare(*cards1[0]));
332 // Try to update with just the origin changed.
333 AutofillProfile original_profile(profile);
334 CreditCard original_credit_card(credit_card);
335 profile.set_origin("Chrome settings");
336 credit_card.set_origin("Chrome settings");
338 EXPECT_TRUE(profile.IsVerified());
339 EXPECT_TRUE(credit_card.IsVerified());
341 personal_data_->UpdateProfile(profile);
342 personal_data_->UpdateCreditCard(credit_card);
344 // Note: No refresh, as no update is expected.
346 const std::vector<AutofillProfile*>& profiles2 =
347 personal_data_->GetProfiles();
348 const std::vector<CreditCard*>& cards2 = personal_data_->GetCreditCards();
349 ASSERT_EQ(1U, profiles2.size());
350 ASSERT_EQ(1U, cards2.size());
351 EXPECT_NE(profile.origin(), profiles2[0]->origin());
352 EXPECT_NE(credit_card.origin(), cards2[0]->origin());
353 EXPECT_EQ(original_profile.origin(), profiles2[0]->origin());
354 EXPECT_EQ(original_credit_card.origin(), cards2[0]->origin());
356 // Try to update with data changed as well.
357 profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("John"));
358 credit_card.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Joe"));
360 personal_data_->UpdateProfile(profile);
361 personal_data_->UpdateCreditCard(credit_card);
363 // Verify that the web database has been updated and the notification sent.
364 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
365 .WillOnce(QuitMainMessageLoop());
366 base::MessageLoop::current()->Run();
368 const std::vector<AutofillProfile*>& profiles3 =
369 personal_data_->GetProfiles();
370 const std::vector<CreditCard*>& cards3 = personal_data_->GetCreditCards();
371 ASSERT_EQ(1U, profiles3.size());
372 ASSERT_EQ(1U, cards3.size());
373 EXPECT_EQ(0, profile.Compare(*profiles3[0]));
374 EXPECT_EQ(0, credit_card.Compare(*cards3[0]));
375 EXPECT_EQ(profile.origin(), profiles3[0]->origin());
376 EXPECT_EQ(credit_card.origin(), cards3[0]->origin());
379 // Tests that server cards are ignored without the flag.
380 TEST_F(PersonalDataManagerTest, ReturnsServerCreditCards) {
381 std::vector<CreditCard> server_cards;
382 server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "a123"));
383 test::SetCreditCardInfo(&server_cards.back(), "John Dillinger",
384 "9012" /* Visa */, "01", "2010");
385 server_cards.back().SetTypeForMaskedCard(kVisaCard);
387 server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "b456"));
388 test::SetCreditCardInfo(&server_cards.back(), "Bonnie Parker",
389 "2109" /* Mastercard */, "12", "2012");
390 server_cards.back().SetTypeForMaskedCard(kMasterCard);
392 autofill_table_->SetServerCreditCards(server_cards);
393 personal_data_->Refresh();
395 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
396 .WillOnce(QuitMainMessageLoop());
397 base::MessageLoop::current()->Run();
399 EXPECT_EQ(0U, personal_data_->GetCreditCards().size());
402 // Tests that UpdateServerCreditCard can be used to mask or unmask server cards.
403 TEST_F(PersonalDataManagerTest, UpdateServerCreditCards) {
404 EnableWalletCardImport();
406 std::vector<CreditCard> server_cards;
407 server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "a123"));
408 test::SetCreditCardInfo(&server_cards.back(), "John Dillinger",
409 "9012" /* Visa */, "01", "2010");
410 server_cards.back().SetTypeForMaskedCard(kVisaCard);
412 server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "b456"));
413 test::SetCreditCardInfo(&server_cards.back(), "Bonnie Parker",
414 "2109" /* Mastercard */, "12", "2012");
415 server_cards.back().SetTypeForMaskedCard(kMasterCard);
417 server_cards.push_back(CreditCard(CreditCard::FULL_SERVER_CARD, "c789"));
418 test::SetCreditCardInfo(&server_cards.back(), "Clyde Barrow",
419 "347666888555" /* American Express */, "04", "2015");
421 autofill_table_->SetServerCreditCards(server_cards);
422 personal_data_->Refresh();
424 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
425 .WillOnce(QuitMainMessageLoop());
426 base::MessageLoop::current()->Run();
428 ASSERT_EQ(3U, personal_data_->GetCreditCards().size());
429 // The GUIDs will be different, so just compare the data.
430 for (size_t i = 0; i < 3; ++i)
431 EXPECT_EQ(0, server_cards[i].Compare(*personal_data_->GetCreditCards()[i]));
433 CreditCard* unmasked_card = &server_cards.front();
434 unmasked_card->set_record_type(CreditCard::FULL_SERVER_CARD);
435 unmasked_card->SetNumber(ASCIIToUTF16("423456789012"));
436 EXPECT_NE(0, server_cards.front().Compare(
437 *personal_data_->GetCreditCards().front()));
438 personal_data_->UpdateServerCreditCard(*unmasked_card);
440 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
441 .WillOnce(QuitMainMessageLoop());
442 base::MessageLoop::current()->Run();
444 for (size_t i = 0; i < 3; ++i)
445 EXPECT_EQ(0, server_cards[i].Compare(*personal_data_->GetCreditCards()[i]));
447 CreditCard* remasked_card = &server_cards.back();
448 remasked_card->set_record_type(CreditCard::MASKED_SERVER_CARD);
449 remasked_card->SetNumber(ASCIIToUTF16("8555"));
450 EXPECT_NE(
451 0, server_cards.back().Compare(*personal_data_->GetCreditCards().back()));
452 personal_data_->UpdateServerCreditCard(*remasked_card);
454 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
455 .WillOnce(QuitMainMessageLoop());
456 base::MessageLoop::current()->Run();
458 for (size_t i = 0; i < 3; ++i)
459 EXPECT_EQ(0, server_cards[i].Compare(*personal_data_->GetCreditCards()[i]));
462 TEST_F(PersonalDataManagerTest, AddProfilesAndCreditCards) {
463 AutofillProfile profile0(base::GenerateGUID(), "https://www.example.com");
464 test::SetProfileInfo(&profile0,
465 "Marion", "Mitchell", "Morrison",
466 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
467 "91601", "US", "12345678910");
469 AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com");
470 test::SetProfileInfo(&profile1,
471 "Josephine", "Alicia", "Saenz",
472 "joewayne@me.xyz", "Fox", "903 Apple Ct.", NULL, "Orlando", "FL", "32801",
473 "US", "19482937549");
475 CreditCard credit_card0(base::GenerateGUID(), "https://www.example.com");
476 test::SetCreditCardInfo(&credit_card0,
477 "John Dillinger", "423456789012" /* Visa */, "01", "2010");
479 CreditCard credit_card1(base::GenerateGUID(), "https://www.example.com");
480 test::SetCreditCardInfo(&credit_card1,
481 "Bonnie Parker", "518765432109" /* Mastercard */, "12", "2012");
483 // Add two test profiles to the database.
484 personal_data_->AddProfile(profile0);
485 personal_data_->AddProfile(profile1);
487 // Verify that the web database has been updated and the notification sent.
488 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
489 .WillOnce(QuitMainMessageLoop());
490 base::MessageLoop::current()->Run();
492 std::vector<AutofillProfile*> profiles;
493 profiles.push_back(&profile0);
494 profiles.push_back(&profile1);
495 ExpectSameElements(profiles, personal_data_->GetProfiles());
497 // Add two test credit cards to the database.
498 personal_data_->AddCreditCard(credit_card0);
499 personal_data_->AddCreditCard(credit_card1);
501 // Verify that the web database has been updated and the notification sent.
502 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
503 .WillOnce(QuitMainMessageLoop());
504 base::MessageLoop::current()->Run();
506 std::vector<CreditCard*> cards;
507 cards.push_back(&credit_card0);
508 cards.push_back(&credit_card1);
509 ExpectSameElements(cards, personal_data_->GetCreditCards());
511 // Determine uniqueness by inserting all of the GUIDs into a set and verifying
512 // the size of the set matches the number of GUIDs.
513 std::set<std::string> guids;
514 guids.insert(profile0.guid());
515 guids.insert(profile1.guid());
516 guids.insert(credit_card0.guid());
517 guids.insert(credit_card1.guid());
518 EXPECT_EQ(4U, guids.size());
521 // Test for http://crbug.com/50047. Makes sure that guids are populated
522 // correctly on load.
523 TEST_F(PersonalDataManagerTest, PopulateUniqueIDsOnLoad) {
524 AutofillProfile profile0(base::GenerateGUID(), "https://www.example.com");
525 test::SetProfileInfo(&profile0,
526 "y", "", "", "", "", "", "", "", "", "", "", "");
528 // Add the profile0 to the db.
529 personal_data_->AddProfile(profile0);
531 // Verify that the web database has been updated and the notification sent.
532 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
533 .WillOnce(QuitMainMessageLoop());
534 base::MessageLoop::current()->Run();
536 // Verify that we've loaded the profiles from the web database.
537 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
538 ASSERT_EQ(1U, results2.size());
539 EXPECT_EQ(0, profile0.Compare(*results2[0]));
541 // Add a new profile.
542 AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com");
543 test::SetProfileInfo(&profile1,
544 "z", "", "", "", "", "", "", "", "", "", "", "");
545 personal_data_->AddProfile(profile1);
547 // Verify that the web database has been updated and the notification sent.
548 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
549 .WillOnce(QuitMainMessageLoop());
550 base::MessageLoop::current()->Run();
552 // Make sure the two profiles have different GUIDs, both valid.
553 const std::vector<AutofillProfile*>& results3 = personal_data_->GetProfiles();
554 ASSERT_EQ(2U, results3.size());
555 EXPECT_NE(results3[0]->guid(), results3[1]->guid());
556 EXPECT_TRUE(base::IsValidGUID(results3[0]->guid()));
557 EXPECT_TRUE(base::IsValidGUID(results3[1]->guid()));
560 TEST_F(PersonalDataManagerTest, SetEmptyProfile) {
561 AutofillProfile profile0(base::GenerateGUID(), "https://www.example.com");
562 test::SetProfileInfo(&profile0,
563 "", "", "", "", "", "", "", "", "", "", "", "");
565 // Add the empty profile to the database.
566 personal_data_->AddProfile(profile0);
568 // Note: no refresh here.
570 // Reset the PersonalDataManager. This tests that the personal data was saved
571 // to the web database, and that we can load the profiles from the web
572 // database.
573 ResetPersonalDataManager(USER_MODE_NORMAL);
575 // Verify that we've loaded the profiles from the web database.
576 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
577 ASSERT_EQ(0U, results2.size());
580 TEST_F(PersonalDataManagerTest, SetEmptyCreditCard) {
581 CreditCard credit_card0(base::GenerateGUID(), "https://www.example.com");
582 test::SetCreditCardInfo(&credit_card0, "", "", "", "");
584 // Add the empty credit card to the database.
585 personal_data_->AddCreditCard(credit_card0);
587 // Note: no refresh here.
589 // Reset the PersonalDataManager. This tests that the personal data was saved
590 // to the web database, and that we can load the credit cards from the web
591 // database.
592 ResetPersonalDataManager(USER_MODE_NORMAL);
594 // Verify that we've loaded the credit cards from the web database.
595 const std::vector<CreditCard*>& results2 = personal_data_->GetCreditCards();
596 ASSERT_EQ(0U, results2.size());
599 TEST_F(PersonalDataManagerTest, Refresh) {
600 AutofillProfile profile0(base::GenerateGUID(), "https://www.example.com");
601 test::SetProfileInfo(&profile0,
602 "Marion", "Mitchell", "Morrison",
603 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
604 "91601", "US", "12345678910");
606 AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com");
607 test::SetProfileInfo(&profile1,
608 "Josephine", "Alicia", "Saenz",
609 "joewayne@me.xyz", "Fox", "903 Apple Ct.", NULL, "Orlando", "FL", "32801",
610 "US", "19482937549");
612 // Add the test profiles to the database.
613 personal_data_->AddProfile(profile0);
614 personal_data_->AddProfile(profile1);
616 // Verify that the web database has been updated and the notification sent.
617 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
618 .WillOnce(QuitMainMessageLoop());
619 base::MessageLoop::current()->Run();
621 std::vector<AutofillProfile*> profiles;
622 profiles.push_back(&profile0);
623 profiles.push_back(&profile1);
624 ExpectSameElements(profiles, personal_data_->GetProfiles());
626 AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com");
627 test::SetProfileInfo(&profile2,
628 "Josephine", "Alicia", "Saenz",
629 "joewayne@me.xyz", "Fox", "1212 Center.", "Bld. 5", "Orlando", "FL",
630 "32801", "US", "19482937549");
632 autofill_database_service_->AddAutofillProfile(profile2);
634 personal_data_->Refresh();
636 // Verify that the web database has been updated and the notification sent.
637 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
638 .WillOnce(QuitMainMessageLoop());
639 base::MessageLoop::current()->Run();
641 profiles.clear();
642 profiles.push_back(&profile0);
643 profiles.push_back(&profile1);
644 profiles.push_back(&profile2);
645 ExpectSameElements(profiles, personal_data_->GetProfiles());
647 autofill_database_service_->RemoveAutofillProfile(profile1.guid());
648 autofill_database_service_->RemoveAutofillProfile(profile2.guid());
650 // Before telling the PDM to refresh, simulate an edit to one of the deleted
651 // profiles via a SetProfile update (this would happen if the Autofill window
652 // was open with a previous snapshot of the profiles, and something
653 // [e.g. sync] removed a profile from the browser. In this edge case, we will
654 // end up in a consistent state by dropping the write).
655 profile0.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Mar"));
656 profile2.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Jo"));
657 personal_data_->UpdateProfile(profile0);
658 personal_data_->AddProfile(profile1);
659 personal_data_->AddProfile(profile2);
661 // Verify that the web database has been updated and the notification sent.
662 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
663 .WillOnce(QuitMainMessageLoop());
664 base::MessageLoop::current()->Run();
666 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
667 ASSERT_EQ(1U, results.size());
668 EXPECT_EQ(profile0, *results[0]);
671 TEST_F(PersonalDataManagerTest, ImportFormData) {
672 FormData form;
673 FormFieldData field;
674 test::CreateTestFormField(
675 "First name:", "first_name", "George", "text", &field);
676 form.fields.push_back(field);
677 test::CreateTestFormField(
678 "Last name:", "last_name", "Washington", "text", &field);
679 form.fields.push_back(field);
680 test::CreateTestFormField(
681 "Email:", "email", "theprez@gmail.com", "text", &field);
682 form.fields.push_back(field);
683 test::CreateTestFormField(
684 "Address:", "address1", "21 Laussat St", "text", &field);
685 form.fields.push_back(field);
686 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
687 form.fields.push_back(field);
688 test::CreateTestFormField("State:", "state", "California", "text", &field);
689 form.fields.push_back(field);
690 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
691 form.fields.push_back(field);
692 FormStructure form_structure(form);
693 form_structure.DetermineHeuristicTypes();
694 scoped_ptr<CreditCard> imported_credit_card;
695 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
696 &imported_credit_card));
697 ASSERT_FALSE(imported_credit_card);
699 // Verify that the web database has been updated and the notification sent.
700 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
701 .WillOnce(QuitMainMessageLoop());
702 base::MessageLoop::current()->Run();
704 AutofillProfile expected(base::GenerateGUID(), "https://www.example.com");
705 test::SetProfileInfo(&expected, "George", NULL,
706 "Washington", "theprez@gmail.com", NULL, "21 Laussat St", NULL,
707 "San Francisco", "California", "94102", NULL, NULL);
708 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
709 ASSERT_EQ(1U, results.size());
710 EXPECT_EQ(0, expected.Compare(*results[0]));
713 TEST_F(PersonalDataManagerTest, ImportFormDataBadEmail) {
714 FormData form;
715 FormFieldData field;
716 test::CreateTestFormField(
717 "First name:", "first_name", "George", "text", &field);
718 form.fields.push_back(field);
719 test::CreateTestFormField(
720 "Last name:", "last_name", "Washington", "text", &field);
721 form.fields.push_back(field);
722 test::CreateTestFormField("Email:", "email", "bogus", "text", &field);
723 form.fields.push_back(field);
724 test::CreateTestFormField(
725 "Address:", "address1", "21 Laussat St", "text", &field);
726 form.fields.push_back(field);
727 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
728 form.fields.push_back(field);
729 test::CreateTestFormField("State:", "state", "California", "text", &field);
730 form.fields.push_back(field);
731 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
732 form.fields.push_back(field);
733 FormStructure form_structure(form);
734 form_structure.DetermineHeuristicTypes();
735 scoped_ptr<CreditCard> imported_credit_card;
736 EXPECT_FALSE(personal_data_->ImportFormData(form_structure,
737 &imported_credit_card));
738 ASSERT_EQ(static_cast<CreditCard*>(NULL), imported_credit_card.get());
740 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
741 ASSERT_EQ(0U, results.size());
744 // Tests that a 'confirm email' field does not block profile import.
745 TEST_F(PersonalDataManagerTest, ImportFormDataTwoEmails) {
746 FormData form;
747 FormFieldData field;
748 test::CreateTestFormField(
749 "Name:", "name", "George Washington", "text", &field);
750 form.fields.push_back(field);
751 test::CreateTestFormField(
752 "Address:", "address1", "21 Laussat St", "text", &field);
753 form.fields.push_back(field);
754 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
755 form.fields.push_back(field);
756 test::CreateTestFormField("State:", "state", "California", "text", &field);
757 form.fields.push_back(field);
758 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
759 form.fields.push_back(field);
760 test::CreateTestFormField(
761 "Email:", "email", "example@example.com", "text", &field);
762 form.fields.push_back(field);
763 test::CreateTestFormField(
764 "Confirm email:", "confirm_email", "example@example.com", "text", &field);
765 form.fields.push_back(field);
766 FormStructure form_structure(form);
767 form_structure.DetermineHeuristicTypes();
768 scoped_ptr<CreditCard> imported_credit_card;
769 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
770 &imported_credit_card));
771 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
772 ASSERT_EQ(1U, results.size());
775 // Tests two email fields containing different values blocks provile import.
776 TEST_F(PersonalDataManagerTest, ImportFormDataTwoDifferentEmails) {
777 FormData form;
778 FormFieldData field;
779 test::CreateTestFormField(
780 "Name:", "name", "George Washington", "text", &field);
781 form.fields.push_back(field);
782 test::CreateTestFormField(
783 "Address:", "address1", "21 Laussat St", "text", &field);
784 form.fields.push_back(field);
785 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
786 form.fields.push_back(field);
787 test::CreateTestFormField("State:", "state", "California", "text", &field);
788 form.fields.push_back(field);
789 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
790 form.fields.push_back(field);
791 test::CreateTestFormField(
792 "Email:", "email", "example@example.com", "text", &field);
793 form.fields.push_back(field);
794 test::CreateTestFormField(
795 "Email:", "email2", "example2@example.com", "text", &field);
796 form.fields.push_back(field);
797 FormStructure form_structure(form);
798 form_structure.DetermineHeuristicTypes();
799 scoped_ptr<CreditCard> imported_credit_card;
800 EXPECT_FALSE(personal_data_->ImportFormData(form_structure,
801 &imported_credit_card));
802 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
803 ASSERT_EQ(0U, results.size());
806 TEST_F(PersonalDataManagerTest, ImportFormDataNotEnoughFilledFields) {
807 FormData form;
808 FormFieldData field;
809 test::CreateTestFormField(
810 "First name:", "first_name", "George", "text", &field);
811 form.fields.push_back(field);
812 test::CreateTestFormField(
813 "Last name:", "last_name", "Washington", "text", &field);
814 form.fields.push_back(field);
815 test::CreateTestFormField(
816 "Card number:", "card_number", "4111 1111 1111 1111", "text", &field);
817 form.fields.push_back(field);
818 FormStructure form_structure(form);
819 form_structure.DetermineHeuristicTypes();
820 scoped_ptr<CreditCard> imported_credit_card;
821 EXPECT_FALSE(personal_data_->ImportFormData(form_structure,
822 &imported_credit_card));
823 ASSERT_FALSE(imported_credit_card);
825 const std::vector<AutofillProfile*>& profiles = personal_data_->GetProfiles();
826 ASSERT_EQ(0U, profiles.size());
827 const std::vector<CreditCard*>& cards = personal_data_->GetCreditCards();
828 ASSERT_EQ(0U, cards.size());
831 TEST_F(PersonalDataManagerTest, ImportFormMinimumAddressUSA) {
832 // United States addresses must specifiy one address line, a city, state and
833 // zip code.
834 FormData form;
835 FormFieldData field;
836 test::CreateTestFormField("Name:", "name", "Barack Obama", "text", &field);
837 form.fields.push_back(field);
838 test::CreateTestFormField(
839 "Address:", "address", "1600 Pennsylvania Avenue", "text", &field);
840 form.fields.push_back(field);
841 test::CreateTestFormField("City:", "city", "Washington", "text", &field);
842 form.fields.push_back(field);
843 test::CreateTestFormField("State:", "state", "DC", "text", &field);
844 form.fields.push_back(field);
845 test::CreateTestFormField("Zip:", "zip", "20500", "text", &field);
846 form.fields.push_back(field);
847 test::CreateTestFormField("Country:", "country", "USA", "text", &field);
848 form.fields.push_back(field);
849 FormStructure form_structure(form);
850 form_structure.DetermineHeuristicTypes();
851 scoped_ptr<CreditCard> imported_credit_card;
852 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
853 &imported_credit_card));
854 const std::vector<AutofillProfile*>& profiles = personal_data_->GetProfiles();
855 ASSERT_EQ(1U, profiles.size());
858 TEST_F(PersonalDataManagerTest, ImportFormMinimumAddressGB) {
859 // British addresses do not require a state/province as the county is usually
860 // not requested on forms.
861 FormData form;
862 FormFieldData field;
863 test::CreateTestFormField("Name:", "name", "David Cameron", "text", &field);
864 form.fields.push_back(field);
865 test::CreateTestFormField(
866 "Address:", "address", "10 Downing Street", "text", &field);
867 form.fields.push_back(field);
868 test::CreateTestFormField("City:", "city", "London", "text", &field);
869 form.fields.push_back(field);
870 test::CreateTestFormField(
871 "Postcode:", "postcode", "SW1A 2AA", "text", &field);
872 form.fields.push_back(field);
873 test::CreateTestFormField(
874 "Country:", "country", "United Kingdom", "text", &field);
875 form.fields.push_back(field);
876 FormStructure form_structure(form);
877 form_structure.DetermineHeuristicTypes();
878 scoped_ptr<CreditCard> imported_credit_card;
879 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
880 &imported_credit_card));
881 const std::vector<AutofillProfile*>& profiles = personal_data_->GetProfiles();
882 ASSERT_EQ(1U, profiles.size());
885 TEST_F(PersonalDataManagerTest, ImportFormMinimumAddressGI) {
886 // Gibraltar has the most minimal set of requirements for a valid address.
887 // There are no cities or provinces and no postal/zip code system.
888 FormData form;
889 FormFieldData field;
890 test::CreateTestFormField(
891 "Name:", "name", "Sir Adrian Johns", "text", &field);
892 form.fields.push_back(field);
893 test::CreateTestFormField(
894 "Address:", "address", "The Convent, Main Street", "text", &field);
895 form.fields.push_back(field);
896 test::CreateTestFormField("Country:", "country", "Gibraltar", "text", &field);
897 form.fields.push_back(field);
898 FormStructure form_structure(form);
899 form_structure.DetermineHeuristicTypes();
900 scoped_ptr<CreditCard> imported_credit_card;
901 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
902 &imported_credit_card));
903 const std::vector<AutofillProfile*>& profiles = personal_data_->GetProfiles();
904 ASSERT_EQ(1U, profiles.size());
907 TEST_F(PersonalDataManagerTest, ImportPhoneNumberSplitAcrossMultipleFields) {
908 FormData form;
909 FormFieldData field;
910 test::CreateTestFormField(
911 "First name:", "first_name", "George", "text", &field);
912 form.fields.push_back(field);
913 test::CreateTestFormField(
914 "Last name:", "last_name", "Washington", "text", &field);
915 form.fields.push_back(field);
916 test::CreateTestFormField(
917 "Phone #:", "home_phone_area_code", "650", "text", &field);
918 field.max_length = 3;
919 form.fields.push_back(field);
920 test::CreateTestFormField(
921 "Phone #:", "home_phone_prefix", "555", "text", &field);
922 field.max_length = 3;
923 form.fields.push_back(field);
924 test::CreateTestFormField(
925 "Phone #:", "home_phone_suffix", "0000", "text", &field);
926 field.max_length = 4;
927 form.fields.push_back(field);
928 test::CreateTestFormField(
929 "Address:", "address1", "21 Laussat St", "text", &field);
930 form.fields.push_back(field);
931 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
932 form.fields.push_back(field);
933 test::CreateTestFormField("State:", "state", "California", "text", &field);
934 form.fields.push_back(field);
935 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
936 form.fields.push_back(field);
937 FormStructure form_structure(form);
938 form_structure.DetermineHeuristicTypes();
939 scoped_ptr<CreditCard> imported_credit_card;
940 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
941 &imported_credit_card));
942 ASSERT_FALSE(imported_credit_card);
944 // Verify that the web database has been updated and the notification sent.
945 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
946 .WillOnce(QuitMainMessageLoop());
947 base::MessageLoop::current()->Run();
949 AutofillProfile expected(base::GenerateGUID(), "https://www.example.com");
950 test::SetProfileInfo(&expected, "George", NULL,
951 "Washington", NULL, NULL, "21 Laussat St", NULL,
952 "San Francisco", "California", "94102", NULL, "(650) 555-0000");
953 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
954 ASSERT_EQ(1U, results.size());
955 EXPECT_EQ(0, expected.Compare(*results[0]));
958 TEST_F(PersonalDataManagerTest, ImportFormDataMultilineAddress) {
959 FormData form;
960 FormFieldData field;
961 test::CreateTestFormField(
962 "First name:", "first_name", "George", "text", &field);
963 form.fields.push_back(field);
964 test::CreateTestFormField(
965 "Last name:", "last_name", "Washington", "text", &field);
966 form.fields.push_back(field);
967 test::CreateTestFormField(
968 "Email:", "email", "theprez@gmail.com", "text", &field);
969 form.fields.push_back(field);
970 test::CreateTestFormField(
971 "Address:",
972 "street_address",
973 "21 Laussat St\n"
974 "Apt. #42",
975 "textarea",
976 &field);
977 form.fields.push_back(field);
978 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
979 form.fields.push_back(field);
980 test::CreateTestFormField("State:", "state", "California", "text", &field);
981 form.fields.push_back(field);
982 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
983 form.fields.push_back(field);
984 FormStructure form_structure(form);
985 form_structure.DetermineHeuristicTypes();
986 scoped_ptr<CreditCard> imported_credit_card;
987 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
988 &imported_credit_card));
989 ASSERT_FALSE(imported_credit_card);
991 // Verify that the web database has been updated and the notification sent.
992 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
993 .WillOnce(QuitMainMessageLoop());
994 base::MessageLoop::current()->Run();
996 AutofillProfile expected(base::GenerateGUID(), "https://www.example.com");
997 test::SetProfileInfo(&expected, "George", NULL,
998 "Washington", "theprez@gmail.com", NULL, "21 Laussat St", "Apt. #42",
999 "San Francisco", "California", "94102", NULL, NULL);
1000 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
1001 ASSERT_EQ(1U, results.size());
1002 EXPECT_EQ(0, expected.Compare(*results[0]));
1005 TEST_F(PersonalDataManagerTest, SetUniqueCreditCardLabels) {
1006 CreditCard credit_card0(base::GenerateGUID(), "https://www.example.com");
1007 credit_card0.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("John"));
1008 CreditCard credit_card1(base::GenerateGUID(), "https://www.example.com");
1009 credit_card1.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Paul"));
1010 CreditCard credit_card2(base::GenerateGUID(), "https://www.example.com");
1011 credit_card2.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Ringo"));
1012 CreditCard credit_card3(base::GenerateGUID(), "https://www.example.com");
1013 credit_card3.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Other"));
1014 CreditCard credit_card4(base::GenerateGUID(), "https://www.example.com");
1015 credit_card4.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Ozzy"));
1016 CreditCard credit_card5(base::GenerateGUID(), "https://www.example.com");
1017 credit_card5.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Dio"));
1019 // Add the test credit cards to the database.
1020 personal_data_->AddCreditCard(credit_card0);
1021 personal_data_->AddCreditCard(credit_card1);
1022 personal_data_->AddCreditCard(credit_card2);
1023 personal_data_->AddCreditCard(credit_card3);
1024 personal_data_->AddCreditCard(credit_card4);
1025 personal_data_->AddCreditCard(credit_card5);
1027 // Reset the PersonalDataManager. This tests that the personal data was saved
1028 // to the web database, and that we can load the credit cards from the web
1029 // database.
1030 ResetPersonalDataManager(USER_MODE_NORMAL);
1032 std::vector<CreditCard*> cards;
1033 cards.push_back(&credit_card0);
1034 cards.push_back(&credit_card1);
1035 cards.push_back(&credit_card2);
1036 cards.push_back(&credit_card3);
1037 cards.push_back(&credit_card4);
1038 cards.push_back(&credit_card5);
1039 ExpectSameElements(cards, personal_data_->GetCreditCards());
1042 TEST_F(PersonalDataManagerTest, AggregateTwoDifferentProfiles) {
1043 FormData form1;
1044 FormFieldData field;
1045 test::CreateTestFormField(
1046 "First name:", "first_name", "George", "text", &field);
1047 form1.fields.push_back(field);
1048 test::CreateTestFormField(
1049 "Last name:", "last_name", "Washington", "text", &field);
1050 form1.fields.push_back(field);
1051 test::CreateTestFormField(
1052 "Email:", "email", "theprez@gmail.com", "text", &field);
1053 form1.fields.push_back(field);
1054 test::CreateTestFormField(
1055 "Address:", "address1", "21 Laussat St", "text", &field);
1056 form1.fields.push_back(field);
1057 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
1058 form1.fields.push_back(field);
1059 test::CreateTestFormField("State:", "state", "California", "text", &field);
1060 form1.fields.push_back(field);
1061 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
1062 form1.fields.push_back(field);
1064 FormStructure form_structure1(form1);
1065 form_structure1.DetermineHeuristicTypes();
1066 scoped_ptr<CreditCard> imported_credit_card;
1067 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1068 &imported_credit_card));
1069 ASSERT_FALSE(imported_credit_card);
1071 // Verify that the web database has been updated and the notification sent.
1072 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1073 .WillOnce(QuitMainMessageLoop());
1074 base::MessageLoop::current()->Run();
1076 AutofillProfile expected(base::GenerateGUID(), "https://www.example.com");
1077 test::SetProfileInfo(&expected, "George", NULL,
1078 "Washington", "theprez@gmail.com", NULL, "21 Laussat St", NULL,
1079 "San Francisco", "California", "94102", NULL, NULL);
1080 const std::vector<AutofillProfile*>& results1 = personal_data_->GetProfiles();
1081 ASSERT_EQ(1U, results1.size());
1082 EXPECT_EQ(0, expected.Compare(*results1[0]));
1084 // Now create a completely different profile.
1085 FormData form2;
1086 test::CreateTestFormField(
1087 "First name:", "first_name", "John", "text", &field);
1088 form2.fields.push_back(field);
1089 test::CreateTestFormField(
1090 "Last name:", "last_name", "Adams", "text", &field);
1091 form2.fields.push_back(field);
1092 test::CreateTestFormField(
1093 "Email:", "email", "second@gmail.com", "text", &field);
1094 form2.fields.push_back(field);
1095 test::CreateTestFormField(
1096 "Address:", "address1", "22 Laussat St", "text", &field);
1097 form2.fields.push_back(field);
1098 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
1099 form2.fields.push_back(field);
1100 test::CreateTestFormField("State:", "state", "California", "text", &field);
1101 form2.fields.push_back(field);
1102 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
1103 form2.fields.push_back(field);
1105 FormStructure form_structure2(form2);
1106 form_structure2.DetermineHeuristicTypes();
1107 EXPECT_TRUE(personal_data_->ImportFormData(form_structure2,
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 expected2(base::GenerateGUID(), "https://www.example.com");
1117 test::SetProfileInfo(&expected2, "John", NULL,
1118 "Adams", "second@gmail.com", NULL, "22 Laussat St", NULL,
1119 "San Francisco", "California", "94102", NULL, NULL);
1120 std::vector<AutofillProfile*> profiles;
1121 profiles.push_back(&expected);
1122 profiles.push_back(&expected2);
1123 ExpectSameElements(profiles, personal_data_->GetProfiles());
1126 TEST_F(PersonalDataManagerTest, AggregateTwoProfilesWithMultiValue) {
1127 FormData form1;
1128 FormFieldData field;
1129 test::CreateTestFormField(
1130 "First name:", "first_name", "George", "text", &field);
1131 form1.fields.push_back(field);
1132 test::CreateTestFormField(
1133 "Last name:", "last_name", "Washington", "text", &field);
1134 form1.fields.push_back(field);
1135 test::CreateTestFormField(
1136 "Email:", "email", "theprez@gmail.com", "text", &field);
1137 form1.fields.push_back(field);
1138 test::CreateTestFormField(
1139 "Address:", "address1", "21 Laussat St", "text", &field);
1140 form1.fields.push_back(field);
1141 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
1142 form1.fields.push_back(field);
1143 test::CreateTestFormField("State:", "state", "California", "text", &field);
1144 form1.fields.push_back(field);
1145 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
1146 form1.fields.push_back(field);
1148 FormStructure form_structure1(form1);
1149 form_structure1.DetermineHeuristicTypes();
1150 scoped_ptr<CreditCard> imported_credit_card;
1151 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1152 &imported_credit_card));
1153 ASSERT_FALSE(imported_credit_card);
1155 // Verify that the web database has been updated and the notification sent.
1156 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1157 .WillOnce(QuitMainMessageLoop());
1158 base::MessageLoop::current()->Run();
1160 AutofillProfile expected(base::GenerateGUID(), "https://www.example.com");
1161 test::SetProfileInfo(&expected, "George", NULL,
1162 "Washington", "theprez@gmail.com", NULL, "21 Laussat St", NULL,
1163 "San Francisco", "California", "94102", NULL, NULL);
1164 const std::vector<AutofillProfile*>& results1 = personal_data_->GetProfiles();
1165 ASSERT_EQ(1U, results1.size());
1166 EXPECT_EQ(0, expected.Compare(*results1[0]));
1168 // Now create a completely different profile.
1169 FormData form2;
1170 test::CreateTestFormField(
1171 "First name:", "first_name", "John", "text", &field);
1172 form2.fields.push_back(field);
1173 test::CreateTestFormField("Last name:", "last_name", "Adams", "text", &field);
1174 form2.fields.push_back(field);
1175 test::CreateTestFormField(
1176 "Email:", "email", "second@gmail.com", "text", &field);
1177 form2.fields.push_back(field);
1178 test::CreateTestFormField(
1179 "Address:", "address1", "21 Laussat St", "text", &field);
1180 form2.fields.push_back(field);
1181 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
1182 form2.fields.push_back(field);
1183 test::CreateTestFormField("State:", "state", "California", "text", &field);
1184 form2.fields.push_back(field);
1185 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
1186 form2.fields.push_back(field);
1188 FormStructure form_structure2(form2);
1189 form_structure2.DetermineHeuristicTypes();
1190 EXPECT_TRUE(personal_data_->ImportFormData(form_structure2,
1191 &imported_credit_card));
1192 ASSERT_FALSE(imported_credit_card);
1194 // Verify that the web database has been updated and the notification sent.
1195 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1196 .WillOnce(QuitMainMessageLoop());
1197 base::MessageLoop::current()->Run();
1199 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
1201 // Modify expected to include multi-valued fields.
1202 std::vector<base::string16> first_names, last_names, emails;
1203 expected.GetRawMultiInfo(NAME_FIRST, &first_names);
1204 first_names.push_back(ASCIIToUTF16("John"));
1205 expected.GetRawMultiInfo(NAME_LAST, &last_names);
1206 last_names.push_back(ASCIIToUTF16("Adams"));
1207 expected.SetRawMultiInfo(NAME_FIRST, first_names);
1208 expected.SetRawMultiInfo(NAME_LAST, last_names);
1210 expected.GetRawMultiInfo(EMAIL_ADDRESS, &emails);
1211 emails.push_back(ASCIIToUTF16("second@gmail.com"));
1212 expected.SetRawMultiInfo(EMAIL_ADDRESS, emails);
1214 ASSERT_EQ(1U, results2.size());
1215 EXPECT_EQ(0, expected.Compare(*results2[0]));
1218 TEST_F(PersonalDataManagerTest, AggregateSameProfileWithConflict) {
1219 FormData form1;
1220 FormFieldData field;
1221 test::CreateTestFormField(
1222 "First name:", "first_name", "George", "text", &field);
1223 form1.fields.push_back(field);
1224 test::CreateTestFormField(
1225 "Last name:", "last_name", "Washington", "text", &field);
1226 form1.fields.push_back(field);
1227 test::CreateTestFormField(
1228 "Address:", "address", "1600 Pennsylvania Avenue", "text", &field);
1229 form1.fields.push_back(field);
1230 test::CreateTestFormField(
1231 "Address Line 2:", "address2", "Suite A", "text", &field);
1232 form1.fields.push_back(field);
1233 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
1234 form1.fields.push_back(field);
1235 test::CreateTestFormField("State:", "state", "California", "text", &field);
1236 form1.fields.push_back(field);
1237 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
1238 form1.fields.push_back(field);
1239 test::CreateTestFormField(
1240 "Email:", "email", "theprez@gmail.com", "text", &field);
1241 form1.fields.push_back(field);
1242 test::CreateTestFormField("Phone:", "phone", "6505556666", "text", &field);
1243 form1.fields.push_back(field);
1245 FormStructure form_structure1(form1);
1246 form_structure1.DetermineHeuristicTypes();
1247 scoped_ptr<CreditCard> imported_credit_card;
1248 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1249 &imported_credit_card));
1250 ASSERT_FALSE(imported_credit_card);
1252 // Verify that the web database has been updated and the notification sent.
1253 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1254 .WillOnce(QuitMainMessageLoop());
1255 base::MessageLoop::current()->Run();
1257 AutofillProfile expected(base::GenerateGUID(), "https://www.example.com");
1258 test::SetProfileInfo(
1259 &expected, "George", NULL, "Washington", "theprez@gmail.com", NULL,
1260 "1600 Pennsylvania Avenue", "Suite A", "San Francisco", "California",
1261 "94102", NULL, "(650) 555-6666");
1262 const std::vector<AutofillProfile*>& results1 = personal_data_->GetProfiles();
1263 ASSERT_EQ(1U, results1.size());
1264 EXPECT_EQ(0, expected.Compare(*results1[0]));
1266 // Now create an updated profile.
1267 FormData form2;
1268 test::CreateTestFormField(
1269 "First name:", "first_name", "George", "text", &field);
1270 form2.fields.push_back(field);
1271 test::CreateTestFormField(
1272 "Last name:", "last_name", "Washington", "text", &field);
1273 form2.fields.push_back(field);
1274 test::CreateTestFormField(
1275 "Address:", "address", "1600 Pennsylvania Avenue", "text", &field);
1276 form2.fields.push_back(field);
1277 test::CreateTestFormField(
1278 "Address Line 2:", "address2", "Suite A", "text", &field);
1279 form2.fields.push_back(field);
1280 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
1281 form2.fields.push_back(field);
1282 test::CreateTestFormField("State:", "state", "California", "text", &field);
1283 form2.fields.push_back(field);
1284 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
1285 form2.fields.push_back(field);
1286 test::CreateTestFormField(
1287 "Email:", "email", "theprez@gmail.com", "text", &field);
1288 form2.fields.push_back(field);
1289 // Country gets added.
1290 test::CreateTestFormField("Country:", "country", "USA", "text", &field);
1291 form2.fields.push_back(field);
1292 // Phone gets updated.
1293 test::CreateTestFormField("Phone:", "phone", "6502231234", "text", &field);
1294 form2.fields.push_back(field);
1296 FormStructure form_structure2(form2);
1297 form_structure2.DetermineHeuristicTypes();
1298 EXPECT_TRUE(personal_data_->ImportFormData(form_structure2,
1299 &imported_credit_card));
1300 ASSERT_FALSE(imported_credit_card);
1302 // Verify that the web database has been updated and the notification sent.
1303 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1304 .WillOnce(QuitMainMessageLoop());
1305 base::MessageLoop::current()->Run();
1307 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
1309 // Add multi-valued phone number to expectation. Also, country gets added.
1310 std::vector<base::string16> values;
1311 expected.GetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, &values);
1312 values.push_back(ASCIIToUTF16("(650) 223-1234"));
1313 expected.SetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, values);
1314 expected.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"));
1315 ASSERT_EQ(1U, results2.size());
1316 EXPECT_EQ(0, expected.Compare(*results2[0]));
1319 TEST_F(PersonalDataManagerTest, AggregateProfileWithMissingInfoInOld) {
1320 FormData form1;
1321 FormFieldData field;
1322 test::CreateTestFormField(
1323 "First name:", "first_name", "George", "text", &field);
1324 form1.fields.push_back(field);
1325 test::CreateTestFormField(
1326 "Last name:", "last_name", "Washington", "text", &field);
1327 form1.fields.push_back(field);
1328 test::CreateTestFormField(
1329 "Address Line 1:", "address", "190 High Street", "text", &field);
1330 form1.fields.push_back(field);
1331 test::CreateTestFormField("City:", "city", "Philadelphia", "text", &field);
1332 form1.fields.push_back(field);
1333 test::CreateTestFormField("State:", "state", "Pennsylvania", "text", &field);
1334 form1.fields.push_back(field);
1335 test::CreateTestFormField("Zip:", "zipcode", "19106", "text", &field);
1336 form1.fields.push_back(field);
1338 FormStructure form_structure1(form1);
1339 form_structure1.DetermineHeuristicTypes();
1340 scoped_ptr<CreditCard> imported_credit_card;
1341 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1342 &imported_credit_card));
1343 EXPECT_FALSE(imported_credit_card);
1345 // Verify that the web database has been updated and the notification sent.
1346 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1347 .WillOnce(QuitMainMessageLoop());
1348 base::MessageLoop::current()->Run();
1350 AutofillProfile expected(base::GenerateGUID(), "https://www.example.com");
1351 test::SetProfileInfo(&expected, "George", NULL,
1352 "Washington", NULL, NULL, "190 High Street", NULL,
1353 "Philadelphia", "Pennsylvania", "19106", NULL, NULL);
1354 const std::vector<AutofillProfile*>& results1 = personal_data_->GetProfiles();
1355 ASSERT_EQ(1U, results1.size());
1356 EXPECT_EQ(0, expected.Compare(*results1[0]));
1358 // Submit a form with new data for the first profile.
1359 FormData form2;
1360 test::CreateTestFormField(
1361 "First name:", "first_name", "George", "text", &field);
1362 form2.fields.push_back(field);
1363 test::CreateTestFormField(
1364 "Last name:", "last_name", "Washington", "text", &field);
1365 form2.fields.push_back(field);
1366 test::CreateTestFormField(
1367 "Email:", "email", "theprez@gmail.com", "text", &field);
1368 form2.fields.push_back(field);
1369 test::CreateTestFormField(
1370 "Address Line 1:", "address", "190 High Street", "text", &field);
1371 form2.fields.push_back(field);
1372 test::CreateTestFormField("City:", "city", "Philadelphia", "text", &field);
1373 form2.fields.push_back(field);
1374 test::CreateTestFormField("State:", "state", "Pennsylvania", "text", &field);
1375 form2.fields.push_back(field);
1376 test::CreateTestFormField("Zip:", "zipcode", "19106", "text", &field);
1377 form2.fields.push_back(field);
1379 FormStructure form_structure2(form2);
1380 form_structure2.DetermineHeuristicTypes();
1381 EXPECT_TRUE(personal_data_->ImportFormData(form_structure2,
1382 &imported_credit_card));
1383 ASSERT_FALSE(imported_credit_card);
1385 // Verify that the web database has been updated and the notification sent.
1386 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1387 .WillOnce(QuitMainMessageLoop());
1388 base::MessageLoop::current()->Run();
1390 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
1392 AutofillProfile expected2(base::GenerateGUID(), "https://www.example.com");
1393 test::SetProfileInfo(&expected2, "George", NULL,
1394 "Washington", "theprez@gmail.com", NULL, "190 High Street", NULL,
1395 "Philadelphia", "Pennsylvania", "19106", NULL, NULL);
1396 ASSERT_EQ(1U, results2.size());
1397 EXPECT_EQ(0, expected2.Compare(*results2[0]));
1400 TEST_F(PersonalDataManagerTest, AggregateProfileWithMissingInfoInNew) {
1401 FormData form1;
1402 FormFieldData field;
1403 test::CreateTestFormField(
1404 "First name:", "first_name", "George", "text", &field);
1405 form1.fields.push_back(field);
1406 test::CreateTestFormField(
1407 "Last name:", "last_name", "Washington", "text", &field);
1408 form1.fields.push_back(field);
1409 test::CreateTestFormField(
1410 "Company:", "company", "Government", "text", &field);
1411 form1.fields.push_back(field);
1412 test::CreateTestFormField(
1413 "Email:", "email", "theprez@gmail.com", "text", &field);
1414 form1.fields.push_back(field);
1415 test::CreateTestFormField(
1416 "Address Line 1:", "address", "190 High Street", "text", &field);
1417 form1.fields.push_back(field);
1418 test::CreateTestFormField("City:", "city", "Philadelphia", "text", &field);
1419 form1.fields.push_back(field);
1420 test::CreateTestFormField("State:", "state", "Pennsylvania", "text", &field);
1421 form1.fields.push_back(field);
1422 test::CreateTestFormField("Zip:", "zipcode", "19106", "text", &field);
1423 form1.fields.push_back(field);
1425 FormStructure form_structure1(form1);
1426 form_structure1.DetermineHeuristicTypes();
1427 scoped_ptr<CreditCard> imported_credit_card;
1428 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1429 &imported_credit_card));
1430 ASSERT_FALSE(imported_credit_card);
1432 // Verify that the web database has been updated and the notification sent.
1433 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1434 .WillOnce(QuitMainMessageLoop());
1435 base::MessageLoop::current()->Run();
1437 AutofillProfile expected(base::GenerateGUID(), "https://www.example.com");
1438 test::SetProfileInfo(&expected, "George", NULL,
1439 "Washington", "theprez@gmail.com", "Government", "190 High Street", NULL,
1440 "Philadelphia", "Pennsylvania", "19106", NULL, NULL);
1441 const std::vector<AutofillProfile*>& results1 = personal_data_->GetProfiles();
1442 ASSERT_EQ(1U, results1.size());
1443 EXPECT_EQ(0, expected.Compare(*results1[0]));
1445 // Submit a form with new data for the first profile.
1446 FormData form2;
1447 test::CreateTestFormField(
1448 "First name:", "first_name", "George", "text", &field);
1449 form2.fields.push_back(field);
1450 test::CreateTestFormField(
1451 "Last name:", "last_name", "Washington", "text", &field);
1452 form2.fields.push_back(field);
1453 // Note missing Company field.
1454 test::CreateTestFormField(
1455 "Email:", "email", "theprez@gmail.com", "text", &field);
1456 form2.fields.push_back(field);
1457 test::CreateTestFormField(
1458 "Address Line 1:", "address", "190 High Street", "text", &field);
1459 form2.fields.push_back(field);
1460 test::CreateTestFormField("City:", "city", "Philadelphia", "text", &field);
1461 form2.fields.push_back(field);
1462 test::CreateTestFormField("State:", "state", "Pennsylvania", "text", &field);
1463 form2.fields.push_back(field);
1464 test::CreateTestFormField("Zip:", "zipcode", "19106", "text", &field);
1465 form2.fields.push_back(field);
1467 FormStructure form_structure2(form2);
1468 form_structure2.DetermineHeuristicTypes();
1469 EXPECT_TRUE(personal_data_->ImportFormData(form_structure2,
1470 &imported_credit_card));
1471 ASSERT_FALSE(imported_credit_card);
1473 // Verify that the web database has been updated and the notification sent.
1474 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1475 .WillOnce(QuitMainMessageLoop());
1476 base::MessageLoop::current()->Run();
1478 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
1480 // Expect no change.
1481 ASSERT_EQ(1U, results2.size());
1482 EXPECT_EQ(0, expected.Compare(*results2[0]));
1485 TEST_F(PersonalDataManagerTest, AggregateProfileWithInsufficientAddress) {
1486 FormData form1;
1487 FormFieldData field;
1488 test::CreateTestFormField(
1489 "First name:", "first_name", "George", "text", &field);
1490 form1.fields.push_back(field);
1491 test::CreateTestFormField(
1492 "Last name:", "last_name", "Washington", "text", &field);
1493 form1.fields.push_back(field);
1494 test::CreateTestFormField(
1495 "Company:", "company", "Government", "text", &field);
1496 form1.fields.push_back(field);
1497 test::CreateTestFormField(
1498 "Email:", "email", "theprez@gmail.com", "text", &field);
1499 form1.fields.push_back(field);
1500 test::CreateTestFormField(
1501 "Address Line 1:", "address", "190 High Street", "text", &field);
1502 form1.fields.push_back(field);
1503 test::CreateTestFormField("City:", "city", "Philadelphia", "text", &field);
1504 form1.fields.push_back(field);
1506 FormStructure form_structure1(form1);
1507 form_structure1.DetermineHeuristicTypes();
1508 scoped_ptr<CreditCard> imported_credit_card;
1509 EXPECT_FALSE(personal_data_->ImportFormData(form_structure1,
1510 &imported_credit_card));
1511 ASSERT_FALSE(imported_credit_card);
1513 // Since no refresh is expected, reload the data from the database to make
1514 // sure no changes were written out.
1515 ResetPersonalDataManager(USER_MODE_NORMAL);
1517 const std::vector<AutofillProfile*>& profiles = personal_data_->GetProfiles();
1518 ASSERT_EQ(0U, profiles.size());
1519 const std::vector<CreditCard*>& cards = personal_data_->GetCreditCards();
1520 ASSERT_EQ(0U, cards.size());
1523 TEST_F(PersonalDataManagerTest, AggregateExistingAuxiliaryProfile) {
1524 // Simulate having access to an auxiliary profile.
1525 // |auxiliary_profile| will be owned by |personal_data_|.
1526 AutofillProfile* auxiliary_profile =
1527 new AutofillProfile(base::GenerateGUID(), "https://www.example.com");
1528 test::SetProfileInfo(auxiliary_profile,
1529 "Tester", "Frederick", "McAddressBookTesterson",
1530 "tester@example.com", "Acme Inc.", "1 Main", "Apt A", "San Francisco",
1531 "CA", "94102", "US", "1.415.888.9999");
1532 ScopedVector<AutofillProfile>& auxiliary_profiles =
1533 personal_data_->auxiliary_profiles_;
1534 auxiliary_profiles.push_back(auxiliary_profile);
1536 // Simulate a form submission with a subset of the info.
1537 // Note that the phone number format is different from the saved format.
1538 FormData form;
1539 FormFieldData field;
1540 test::CreateTestFormField(
1541 "First name:", "first_name", "Tester", "text", &field);
1542 form.fields.push_back(field);
1543 test::CreateTestFormField(
1544 "Last name:", "last_name", "McAddressBookTesterson", "text", &field);
1545 form.fields.push_back(field);
1546 test::CreateTestFormField(
1547 "Email:", "email", "tester@example.com", "text", &field);
1548 form.fields.push_back(field);
1549 test::CreateTestFormField("Address:", "address1", "1 Main", "text", &field);
1550 form.fields.push_back(field);
1551 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
1552 form.fields.push_back(field);
1553 test::CreateTestFormField("State:", "state", "CA", "text", &field);
1554 form.fields.push_back(field);
1555 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
1556 form.fields.push_back(field);
1557 test::CreateTestFormField("Phone:", "phone", "4158889999", "text", &field);
1558 form.fields.push_back(field);
1560 FormStructure form_structure(form);
1561 form_structure.DetermineHeuristicTypes();
1562 scoped_ptr<CreditCard> imported_credit_card;
1563 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
1564 &imported_credit_card));
1565 EXPECT_FALSE(imported_credit_card);
1567 // Note: No refresh.
1569 // Expect no change.
1570 const std::vector<AutofillProfile*>& web_profiles =
1571 personal_data_->web_profiles();
1572 EXPECT_EQ(0U, web_profiles.size());
1573 ASSERT_EQ(1U, auxiliary_profiles.size());
1574 EXPECT_EQ(0, auxiliary_profile->Compare(*auxiliary_profiles[0]));
1577 TEST_F(PersonalDataManagerTest, AggregateTwoDifferentCreditCards) {
1578 FormData form1;
1580 // Start with a single valid credit card form.
1581 FormFieldData field;
1582 test::CreateTestFormField(
1583 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1584 form1.fields.push_back(field);
1585 test::CreateTestFormField(
1586 "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
1587 form1.fields.push_back(field);
1588 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
1589 form1.fields.push_back(field);
1590 test::CreateTestFormField("Exp Year:", "exp_year", "2011", "text", &field);
1591 form1.fields.push_back(field);
1593 FormStructure form_structure1(form1);
1594 form_structure1.DetermineHeuristicTypes();
1595 scoped_ptr<CreditCard> imported_credit_card;
1596 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1597 &imported_credit_card));
1598 ASSERT_TRUE(imported_credit_card);
1599 personal_data_->SaveImportedCreditCard(*imported_credit_card);
1601 // Verify that the web database has been updated and the notification sent.
1602 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1603 .WillOnce(QuitMainMessageLoop());
1604 base::MessageLoop::current()->Run();
1606 CreditCard expected(base::GenerateGUID(), "https://www.example.com");
1607 test::SetCreditCardInfo(&expected,
1608 "Biggie Smalls", "4111111111111111", "01", "2011");
1609 const std::vector<CreditCard*>& results = personal_data_->GetCreditCards();
1610 ASSERT_EQ(1U, results.size());
1611 EXPECT_EQ(0, expected.Compare(*results[0]));
1613 // Add a second different valid credit card.
1614 FormData form2;
1615 test::CreateTestFormField(
1616 "Name on card:", "name_on_card", "", "text", &field);
1617 form2.fields.push_back(field);
1618 test::CreateTestFormField(
1619 "Card Number:", "card_number", "5500 0000 0000 0004", "text", &field);
1620 form2.fields.push_back(field);
1621 test::CreateTestFormField("Exp Month:", "exp_month", "02", "text", &field);
1622 form2.fields.push_back(field);
1623 test::CreateTestFormField("Exp Year:", "exp_year", "2012", "text", &field);
1624 form2.fields.push_back(field);
1626 FormStructure form_structure2(form2);
1627 form_structure2.DetermineHeuristicTypes();
1628 EXPECT_TRUE(personal_data_->ImportFormData(form_structure2,
1629 &imported_credit_card));
1630 ASSERT_TRUE(imported_credit_card);
1631 personal_data_->SaveImportedCreditCard(*imported_credit_card);
1633 // Verify that the web database has been updated and the notification sent.
1634 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1635 .WillOnce(QuitMainMessageLoop());
1636 base::MessageLoop::current()->Run();
1638 CreditCard expected2(base::GenerateGUID(), "https://www.example.com");
1639 test::SetCreditCardInfo(&expected2,"", "5500000000000004", "02", "2012");
1640 std::vector<CreditCard*> cards;
1641 cards.push_back(&expected);
1642 cards.push_back(&expected2);
1643 ExpectSameElements(cards, personal_data_->GetCreditCards());
1646 TEST_F(PersonalDataManagerTest, AggregateCardsThatDuplicateServerCards) {
1647 // Add server cards.
1648 std::vector<CreditCard> server_cards;
1649 server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "a123"));
1650 test::SetCreditCardInfo(&server_cards.back(), "John Dillinger",
1651 "1111" /* Visa */, "01", "2010");
1652 server_cards.back().SetTypeForMaskedCard(kVisaCard);
1653 server_cards.push_back(CreditCard(CreditCard::FULL_SERVER_CARD, "c789"));
1654 test::SetCreditCardInfo(&server_cards.back(), "Clyde Barrow",
1655 "347666888555" /* American Express */, "04", "2015");
1656 autofill_table_->SetServerCreditCards(server_cards);
1658 FormData form1;
1660 // Type the same data as the masked card into a form.
1661 FormFieldData field;
1662 test::CreateTestFormField(
1663 "Name on card:", "name_on_card", "John Dillinger", "text", &field);
1664 form1.fields.push_back(field);
1665 test::CreateTestFormField(
1666 "Card Number:", "card_number", "4111111111111111", "text", &field);
1667 form1.fields.push_back(field);
1668 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
1669 form1.fields.push_back(field);
1670 test::CreateTestFormField("Exp Year:", "exp_year", "2010", "text", &field);
1671 form1.fields.push_back(field);
1673 // The card should be offered to be saved locally because it only matches the
1674 // masked card.
1675 FormStructure form_structure1(form1);
1676 form_structure1.DetermineHeuristicTypes();
1677 scoped_ptr<CreditCard> imported_credit_card;
1678 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1679 &imported_credit_card));
1680 ASSERT_TRUE(imported_credit_card);
1681 personal_data_->SaveImportedCreditCard(*imported_credit_card);
1683 // Verify that the web database has been updated and the notification sent.
1684 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1685 .WillOnce(QuitMainMessageLoop());
1686 base::MessageLoop::current()->Run();
1688 // Type the same data as the unmasked card into a form.
1689 FormData form2;
1690 test::CreateTestFormField(
1691 "Name on card:", "name_on_card", "Clyde Barrow", "text", &field);
1692 form2.fields.push_back(field);
1693 test::CreateTestFormField(
1694 "Card Number:", "card_number", "347666888555", "text", &field);
1695 form2.fields.push_back(field);
1696 test::CreateTestFormField("Exp Month:", "exp_month", "04", "text", &field);
1697 form2.fields.push_back(field);
1698 test::CreateTestFormField("Exp Year:", "exp_year", "2015", "text", &field);
1699 form2.fields.push_back(field);
1701 // The card should not be offered to be saved locally because it only matches
1702 // the masked card.
1703 FormStructure form_structure2(form2);
1704 form_structure2.DetermineHeuristicTypes();
1705 EXPECT_FALSE(personal_data_->ImportFormData(form_structure2,
1706 &imported_credit_card));
1707 ASSERT_FALSE(imported_credit_card);
1710 TEST_F(PersonalDataManagerTest, AggregateInvalidCreditCard) {
1711 FormData form1;
1713 // Start with a single valid credit card form.
1714 FormFieldData field;
1715 test::CreateTestFormField(
1716 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1717 form1.fields.push_back(field);
1718 test::CreateTestFormField(
1719 "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
1720 form1.fields.push_back(field);
1721 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
1722 form1.fields.push_back(field);
1723 test::CreateTestFormField("Exp Year:", "exp_year", "2011", "text", &field);
1724 form1.fields.push_back(field);
1726 FormStructure form_structure1(form1);
1727 form_structure1.DetermineHeuristicTypes();
1728 scoped_ptr<CreditCard> imported_credit_card;
1729 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1730 &imported_credit_card));
1731 ASSERT_TRUE(imported_credit_card);
1732 personal_data_->SaveImportedCreditCard(*imported_credit_card);
1734 // Verify that the web database has been updated and the notification sent.
1735 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1736 .WillOnce(QuitMainMessageLoop());
1737 base::MessageLoop::current()->Run();
1739 CreditCard expected(base::GenerateGUID(), "https://www.example.com");
1740 test::SetCreditCardInfo(&expected,
1741 "Biggie Smalls", "4111111111111111", "01", "2011");
1742 const std::vector<CreditCard*>& results = personal_data_->GetCreditCards();
1743 ASSERT_EQ(1U, results.size());
1744 EXPECT_EQ(0, expected.Compare(*results[0]));
1746 // Add a second different invalid credit card.
1747 FormData form2;
1748 test::CreateTestFormField(
1749 "Name on card:", "name_on_card", "Jim Johansen", "text", &field);
1750 form2.fields.push_back(field);
1751 test::CreateTestFormField(
1752 "Card Number:", "card_number", "1000000000000000", "text", &field);
1753 form2.fields.push_back(field);
1754 test::CreateTestFormField("Exp Month:", "exp_month", "02", "text", &field);
1755 form2.fields.push_back(field);
1756 test::CreateTestFormField("Exp Year:", "exp_year", "2012", "text", &field);
1757 form2.fields.push_back(field);
1759 FormStructure form_structure2(form2);
1760 form_structure2.DetermineHeuristicTypes();
1761 EXPECT_FALSE(personal_data_->ImportFormData(form_structure2,
1762 &imported_credit_card));
1763 ASSERT_FALSE(imported_credit_card);
1765 // Since no refresh is expected, reload the data from the database to make
1766 // sure no changes were written out.
1767 ResetPersonalDataManager(USER_MODE_NORMAL);
1769 const std::vector<CreditCard*>& results2 = personal_data_->GetCreditCards();
1770 ASSERT_EQ(1U, results2.size());
1771 EXPECT_EQ(0, expected.Compare(*results2[0]));
1774 TEST_F(PersonalDataManagerTest, AggregateSameCreditCardWithConflict) {
1775 FormData form1;
1777 // Start with a single valid credit card form.
1778 FormFieldData field;
1779 test::CreateTestFormField(
1780 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1781 form1.fields.push_back(field);
1782 test::CreateTestFormField(
1783 "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
1784 form1.fields.push_back(field);
1785 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
1786 form1.fields.push_back(field);
1787 test::CreateTestFormField("Exp Year:", "exp_year", "2011", "text", &field);
1788 form1.fields.push_back(field);
1790 FormStructure form_structure1(form1);
1791 form_structure1.DetermineHeuristicTypes();
1792 scoped_ptr<CreditCard> imported_credit_card;
1793 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1794 &imported_credit_card));
1795 ASSERT_TRUE(imported_credit_card);
1796 personal_data_->SaveImportedCreditCard(*imported_credit_card);
1798 // Verify that the web database has been updated and the notification sent.
1799 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1800 .WillOnce(QuitMainMessageLoop());
1801 base::MessageLoop::current()->Run();
1803 CreditCard expected(base::GenerateGUID(), "https://www.example.com");
1804 test::SetCreditCardInfo(&expected,
1805 "Biggie Smalls", "4111111111111111", "01", "2011");
1806 const std::vector<CreditCard*>& results = personal_data_->GetCreditCards();
1807 ASSERT_EQ(1U, results.size());
1808 EXPECT_EQ(0, expected.Compare(*results[0]));
1810 // Add a second different valid credit card where the year is different but
1811 // the credit card number matches.
1812 FormData form2;
1813 test::CreateTestFormField(
1814 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1815 form2.fields.push_back(field);
1816 test::CreateTestFormField(
1817 "Card Number:", "card_number", "4111 1111 1111 1111", "text", &field);
1818 form2.fields.push_back(field);
1819 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
1820 form2.fields.push_back(field);
1821 test::CreateTestFormField("Exp Year:", "exp_year", "2012", "text", &field);
1822 form2.fields.push_back(field);
1824 FormStructure form_structure2(form2);
1825 form_structure2.DetermineHeuristicTypes();
1826 EXPECT_TRUE(personal_data_->ImportFormData(form_structure2,
1827 &imported_credit_card));
1828 EXPECT_FALSE(imported_credit_card);
1830 // Verify that the web database has been updated and the notification sent.
1831 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1832 .WillOnce(QuitMainMessageLoop());
1833 base::MessageLoop::current()->Run();
1835 // Expect that the newer information is saved. In this case the year is
1836 // updated to "2012".
1837 CreditCard expected2(base::GenerateGUID(), "https://www.example.com");
1838 test::SetCreditCardInfo(&expected2,
1839 "Biggie Smalls", "4111111111111111", "01", "2012");
1840 const std::vector<CreditCard*>& results2 = personal_data_->GetCreditCards();
1841 ASSERT_EQ(1U, results2.size());
1842 EXPECT_EQ(0, expected2.Compare(*results2[0]));
1845 TEST_F(PersonalDataManagerTest, AggregateEmptyCreditCardWithConflict) {
1846 FormData form1;
1848 // Start with a single valid credit card form.
1849 FormFieldData field;
1850 test::CreateTestFormField(
1851 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1852 form1.fields.push_back(field);
1853 test::CreateTestFormField(
1854 "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
1855 form1.fields.push_back(field);
1856 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
1857 form1.fields.push_back(field);
1858 test::CreateTestFormField("Exp Year:", "exp_year", "2011", "text", &field);
1859 form1.fields.push_back(field);
1861 FormStructure form_structure1(form1);
1862 form_structure1.DetermineHeuristicTypes();
1863 scoped_ptr<CreditCard> imported_credit_card;
1864 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1865 &imported_credit_card));
1866 ASSERT_TRUE(imported_credit_card);
1867 personal_data_->SaveImportedCreditCard(*imported_credit_card);
1869 // Verify that the web database has been updated and the notification sent.
1870 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1871 .WillOnce(QuitMainMessageLoop());
1872 base::MessageLoop::current()->Run();
1874 CreditCard expected(base::GenerateGUID(), "https://www.example.com");
1875 test::SetCreditCardInfo(&expected,
1876 "Biggie Smalls", "4111111111111111", "01", "2011");
1877 const std::vector<CreditCard*>& results = personal_data_->GetCreditCards();
1878 ASSERT_EQ(1U, results.size());
1879 EXPECT_EQ(0, expected.Compare(*results[0]));
1881 // Add a second credit card with no number.
1882 FormData form2;
1883 test::CreateTestFormField(
1884 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1885 form2.fields.push_back(field);
1886 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
1887 form2.fields.push_back(field);
1888 test::CreateTestFormField("Exp Year:", "exp_year", "2012", "text", &field);
1889 form2.fields.push_back(field);
1891 FormStructure form_structure2(form2);
1892 form_structure2.DetermineHeuristicTypes();
1893 EXPECT_FALSE(personal_data_->ImportFormData(form_structure2,
1894 &imported_credit_card));
1895 EXPECT_FALSE(imported_credit_card);
1897 // Since no refresh is expected, reload the data from the database to make
1898 // sure no changes were written out.
1899 ResetPersonalDataManager(USER_MODE_NORMAL);
1901 // No change is expected.
1902 CreditCard expected2(base::GenerateGUID(), "https://www.example.com");
1903 test::SetCreditCardInfo(&expected2,
1904 "Biggie Smalls", "4111111111111111", "01", "2011");
1905 const std::vector<CreditCard*>& results2 = personal_data_->GetCreditCards();
1906 ASSERT_EQ(1U, results2.size());
1907 EXPECT_EQ(0, expected2.Compare(*results2[0]));
1910 TEST_F(PersonalDataManagerTest, AggregateCreditCardWithMissingInfoInNew) {
1911 FormData form1;
1913 // Start with a single valid credit card form.
1914 FormFieldData field;
1915 test::CreateTestFormField(
1916 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1917 form1.fields.push_back(field);
1918 test::CreateTestFormField(
1919 "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
1920 form1.fields.push_back(field);
1921 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
1922 form1.fields.push_back(field);
1923 test::CreateTestFormField("Exp Year:", "exp_year", "2011", "text", &field);
1924 form1.fields.push_back(field);
1926 FormStructure form_structure1(form1);
1927 form_structure1.DetermineHeuristicTypes();
1928 scoped_ptr<CreditCard> imported_credit_card;
1929 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1930 &imported_credit_card));
1931 ASSERT_TRUE(imported_credit_card);
1932 personal_data_->SaveImportedCreditCard(*imported_credit_card);
1934 // Verify that the web database has been updated and the notification sent.
1935 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
1936 .WillOnce(QuitMainMessageLoop());
1937 base::MessageLoop::current()->Run();
1939 CreditCard expected(base::GenerateGUID(), "https://www.example.com");
1940 test::SetCreditCardInfo(&expected,
1941 "Biggie Smalls", "4111111111111111", "01", "2011");
1942 const std::vector<CreditCard*>& results = personal_data_->GetCreditCards();
1943 ASSERT_EQ(1U, results.size());
1944 EXPECT_EQ(0, expected.Compare(*results[0]));
1946 // Add a second different valid credit card where the name is missing but
1947 // the credit card number matches.
1948 FormData form2;
1949 // Note missing name.
1950 test::CreateTestFormField(
1951 "Card Number:", "card_number", "4111111111111111", "text", &field);
1952 form2.fields.push_back(field);
1953 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
1954 form2.fields.push_back(field);
1955 test::CreateTestFormField("Exp Year:", "exp_year", "2011", "text", &field);
1956 form2.fields.push_back(field);
1958 FormStructure form_structure2(form2);
1959 form_structure2.DetermineHeuristicTypes();
1960 EXPECT_TRUE(personal_data_->ImportFormData(form_structure2,
1961 &imported_credit_card));
1962 EXPECT_FALSE(imported_credit_card);
1964 // Since no refresh is expected, reload the data from the database to make
1965 // sure no changes were written out.
1966 ResetPersonalDataManager(USER_MODE_NORMAL);
1968 // No change is expected.
1969 CreditCard expected2(base::GenerateGUID(), "https://www.example.com");
1970 test::SetCreditCardInfo(&expected2,
1971 "Biggie Smalls", "4111111111111111", "01", "2011");
1972 const std::vector<CreditCard*>& results2 = personal_data_->GetCreditCards();
1973 ASSERT_EQ(1U, results2.size());
1974 EXPECT_EQ(0, expected2.Compare(*results2[0]));
1976 // Add a third credit card where the expiration date is missing.
1977 FormData form3;
1978 test::CreateTestFormField(
1979 "Name on card:", "name_on_card", "Johnny McEnroe", "text", &field);
1980 form3.fields.push_back(field);
1981 test::CreateTestFormField(
1982 "Card Number:", "card_number", "5555555555554444", "text", &field);
1983 form3.fields.push_back(field);
1984 // Note missing expiration month and year..
1986 FormStructure form_structure3(form3);
1987 form_structure3.DetermineHeuristicTypes();
1988 EXPECT_FALSE(personal_data_->ImportFormData(form_structure3,
1989 &imported_credit_card));
1990 ASSERT_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 expected3(base::GenerateGUID(), "https://www.example.com");
1998 test::SetCreditCardInfo(&expected3,
1999 "Biggie Smalls", "4111111111111111", "01", "2011");
2000 const std::vector<CreditCard*>& results3 = personal_data_->GetCreditCards();
2001 ASSERT_EQ(1U, results3.size());
2002 EXPECT_EQ(0, expected3.Compare(*results3[0]));
2005 TEST_F(PersonalDataManagerTest, AggregateCreditCardWithMissingInfoInOld) {
2006 // Start with a single valid credit card stored via the preferences.
2007 // Note the empty name.
2008 CreditCard saved_credit_card(base::GenerateGUID(), "https://www.example.com");
2009 test::SetCreditCardInfo(&saved_credit_card,
2010 "", "4111111111111111" /* Visa */, "01", "2011");
2011 personal_data_->AddCreditCard(saved_credit_card);
2013 // Verify that the web database has been updated and the notification sent.
2014 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2015 .WillOnce(QuitMainMessageLoop());
2016 base::MessageLoop::current()->Run();
2018 const std::vector<CreditCard*>& results1 = personal_data_->GetCreditCards();
2019 ASSERT_EQ(1U, results1.size());
2020 EXPECT_EQ(saved_credit_card, *results1[0]);
2023 // Add a second different valid credit card where the year is different but
2024 // the credit card number matches.
2025 FormData form;
2026 FormFieldData field;
2027 test::CreateTestFormField(
2028 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
2029 form.fields.push_back(field);
2030 test::CreateTestFormField(
2031 "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
2032 form.fields.push_back(field);
2033 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
2034 form.fields.push_back(field);
2035 test::CreateTestFormField("Exp Year:", "exp_year", "2012", "text", &field);
2036 form.fields.push_back(field);
2038 FormStructure form_structure(form);
2039 form_structure.DetermineHeuristicTypes();
2040 scoped_ptr<CreditCard> imported_credit_card;
2041 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
2042 &imported_credit_card));
2043 EXPECT_FALSE(imported_credit_card);
2045 // Verify that the web database has been updated and the notification sent.
2046 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2047 .WillOnce(QuitMainMessageLoop());
2048 base::MessageLoop::current()->Run();
2050 // Expect that the newer information is saved. In this case the year is
2051 // added to the existing credit card.
2052 CreditCard expected2(base::GenerateGUID(), "https://www.example.com");
2053 test::SetCreditCardInfo(&expected2,
2054 "Biggie Smalls", "4111111111111111", "01", "2012");
2055 const std::vector<CreditCard*>& results2 = personal_data_->GetCreditCards();
2056 ASSERT_EQ(1U, results2.size());
2057 EXPECT_EQ(0, expected2.Compare(*results2[0]));
2060 // We allow the user to store a credit card number with separators via the UI.
2061 // We should not try to re-aggregate the same card with the separators stripped.
2062 TEST_F(PersonalDataManagerTest, AggregateSameCreditCardWithSeparators) {
2063 // Start with a single valid credit card stored via the preferences.
2064 // Note the separators in the credit card number.
2065 CreditCard saved_credit_card(base::GenerateGUID(), "https://www.example.com");
2066 test::SetCreditCardInfo(&saved_credit_card,
2067 "Biggie Smalls", "4111 1111 1111 1111" /* Visa */, "01", "2011");
2068 personal_data_->AddCreditCard(saved_credit_card);
2070 // Verify that the web database has been updated and the notification sent.
2071 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2072 .WillOnce(QuitMainMessageLoop());
2073 base::MessageLoop::current()->Run();
2075 const std::vector<CreditCard*>& results1 = personal_data_->GetCreditCards();
2076 ASSERT_EQ(1U, results1.size());
2077 EXPECT_EQ(0, saved_credit_card.Compare(*results1[0]));
2079 // Import the same card info, but with different separators in the number.
2080 FormData form;
2081 FormFieldData field;
2082 test::CreateTestFormField(
2083 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
2084 form.fields.push_back(field);
2085 test::CreateTestFormField(
2086 "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
2087 form.fields.push_back(field);
2088 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
2089 form.fields.push_back(field);
2090 test::CreateTestFormField("Exp Year:", "exp_year", "2011", "text", &field);
2091 form.fields.push_back(field);
2093 FormStructure form_structure(form);
2094 form_structure.DetermineHeuristicTypes();
2095 scoped_ptr<CreditCard> imported_credit_card;
2096 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
2097 &imported_credit_card));
2098 EXPECT_FALSE(imported_credit_card);
2100 // Since no refresh is expected, reload the data from the database to make
2101 // sure no changes were written out.
2102 ResetPersonalDataManager(USER_MODE_NORMAL);
2104 // Expect that no new card is saved.
2105 const std::vector<CreditCard*>& results2 = personal_data_->GetCreditCards();
2106 ASSERT_EQ(1U, results2.size());
2107 EXPECT_EQ(0, saved_credit_card.Compare(*results2[0]));
2110 // Ensure that if a verified profile already exists, aggregated profiles cannot
2111 // modify it in any way.
2112 TEST_F(PersonalDataManagerTest, AggregateExistingVerifiedProfileWithConflict) {
2113 // Start with a verified profile.
2114 AutofillProfile profile(base::GenerateGUID(), "Chrome settings");
2115 test::SetProfileInfo(&profile,
2116 "Marion", "Mitchell", "Morrison",
2117 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
2118 "91601", "US", "12345678910");
2119 EXPECT_TRUE(profile.IsVerified());
2121 // Add the profile to the database.
2122 personal_data_->AddProfile(profile);
2124 // Verify that the web database has been updated and the notification sent.
2125 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2126 .WillOnce(QuitMainMessageLoop());
2127 base::MessageLoop::current()->Run();
2129 // Simulate a form submission with conflicting info.
2130 FormData form;
2131 FormFieldData field;
2132 test::CreateTestFormField(
2133 "First name:", "first_name", "Marion", "text", &field);
2134 form.fields.push_back(field);
2135 test::CreateTestFormField(
2136 "Last name:", "last_name", "Morrison", "text", &field);
2137 form.fields.push_back(field);
2138 test::CreateTestFormField(
2139 "Email:", "email", "other.email@example.com", "text", &field);
2140 form.fields.push_back(field);
2141 test::CreateTestFormField(
2142 "Address:", "address1", "123 Zoo St.", "text", &field);
2143 form.fields.push_back(field);
2144 test::CreateTestFormField("City:", "city", "Hollywood", "text", &field);
2145 form.fields.push_back(field);
2146 test::CreateTestFormField("State:", "state", "CA", "text", &field);
2147 form.fields.push_back(field);
2148 test::CreateTestFormField("Zip:", "zip", "91601", "text", &field);
2149 form.fields.push_back(field);
2151 FormStructure form_structure(form);
2152 form_structure.DetermineHeuristicTypes();
2153 scoped_ptr<CreditCard> imported_credit_card;
2154 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
2155 &imported_credit_card));
2156 EXPECT_FALSE(imported_credit_card);
2158 // Wait for the refresh, which in this case is a no-op.
2159 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2160 .WillOnce(QuitMainMessageLoop());
2161 base::MessageLoop::current()->Run();
2163 // Expect that no new profile is saved.
2164 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
2165 ASSERT_EQ(1U, results.size());
2166 EXPECT_EQ(0, profile.Compare(*results[0]));
2169 // Ensure that if a verified credit card already exists, aggregated credit cards
2170 // cannot modify it in any way.
2171 TEST_F(PersonalDataManagerTest,
2172 AggregateExistingVerifiedCreditCardWithConflict) {
2173 // Start with a verified credit card.
2174 CreditCard credit_card(base::GenerateGUID(), "Chrome settings");
2175 test::SetCreditCardInfo(&credit_card,
2176 "Biggie Smalls", "4111 1111 1111 1111" /* Visa */, "01", "2011");
2177 EXPECT_TRUE(credit_card.IsVerified());
2179 // Add the credit card to the database.
2180 personal_data_->AddCreditCard(credit_card);
2182 // Verify that the web database has been updated and the notification sent.
2183 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2184 .WillOnce(QuitMainMessageLoop());
2185 base::MessageLoop::current()->Run();
2187 // Simulate a form submission with conflicting expiration year.
2188 FormData form;
2189 FormFieldData field;
2190 test::CreateTestFormField(
2191 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
2192 form.fields.push_back(field);
2193 test::CreateTestFormField(
2194 "Card Number:", "card_number", "4111 1111 1111 1111", "text", &field);
2195 form.fields.push_back(field);
2196 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
2197 form.fields.push_back(field);
2198 test::CreateTestFormField("Exp Year:", "exp_year", "2012", "text", &field);
2199 form.fields.push_back(field);
2201 FormStructure form_structure(form);
2202 form_structure.DetermineHeuristicTypes();
2203 scoped_ptr<CreditCard> imported_credit_card;
2204 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
2205 &imported_credit_card));
2206 ASSERT_FALSE(imported_credit_card);
2208 // Since no refresh is expected, reload the data from the database to make
2209 // sure no changes were written out.
2210 ResetPersonalDataManager(USER_MODE_NORMAL);
2212 // Expect that the saved credit card is not modified.
2213 const std::vector<CreditCard*>& results = personal_data_->GetCreditCards();
2214 ASSERT_EQ(1U, results.size());
2215 EXPECT_EQ(0, credit_card.Compare(*results[0]));
2218 // Ensure that verified profiles can be saved via SaveImportedProfile,
2219 // overwriting existing unverified profiles.
2220 TEST_F(PersonalDataManagerTest, SaveImportedProfileWithVerifiedData) {
2221 // Start with an unverified profile.
2222 AutofillProfile profile(base::GenerateGUID(), "https://www.example.com");
2223 test::SetProfileInfo(&profile,
2224 "Marion", "Mitchell", "Morrison",
2225 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
2226 "91601", "US", "12345678910");
2227 EXPECT_FALSE(profile.IsVerified());
2229 // Add the profile to the database.
2230 personal_data_->AddProfile(profile);
2232 // Verify that the web database has been updated and the notification sent.
2233 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2234 .WillOnce(QuitMainMessageLoop());
2235 base::MessageLoop::current()->Run();
2237 AutofillProfile new_verified_profile = profile;
2238 new_verified_profile.set_guid(base::GenerateGUID());
2239 new_verified_profile.set_origin("Chrome settings");
2240 new_verified_profile.SetRawInfo(COMPANY_NAME, ASCIIToUTF16("Fizzbang, Inc."));
2241 EXPECT_TRUE(new_verified_profile.IsVerified());
2243 personal_data_->SaveImportedProfile(new_verified_profile);
2245 // Verify that the web database has been updated and the notification sent.
2246 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2247 .WillOnce(QuitMainMessageLoop());
2248 base::MessageLoop::current()->Run();
2250 // Expect that the existing profile is not modified, and instead the new
2251 // profile is added.
2252 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
2253 ASSERT_EQ(1U, results.size());
2254 EXPECT_EQ(0, new_verified_profile.Compare(*results[0]));
2257 // Ensure that verified profiles can be saved via SaveImportedProfile,
2258 // overwriting existing verified profiles as well.
2259 TEST_F(PersonalDataManagerTest, SaveImportedProfileWithExistingVerifiedData) {
2260 // Start with a verified profile.
2261 AutofillProfile profile(base::GenerateGUID(), "Chrome settings");
2262 test::SetProfileInfo(&profile,
2263 "Marion", "Mitchell", "Morrison",
2264 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
2265 "91601", "US", "12345678910");
2266 EXPECT_TRUE(profile.IsVerified());
2268 // Add the profile to the database.
2269 personal_data_->AddProfile(profile);
2271 // Verify that the web database has been updated and the notification sent.
2272 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2273 .WillOnce(QuitMainMessageLoop());
2274 base::MessageLoop::current()->Run();
2276 AutofillProfile new_verified_profile = profile;
2277 new_verified_profile.set_guid(base::GenerateGUID());
2278 new_verified_profile.SetRawInfo(COMPANY_NAME, ASCIIToUTF16("Fizzbang, Inc."));
2279 new_verified_profile.SetRawInfo(NAME_MIDDLE, base::string16());
2280 EXPECT_TRUE(new_verified_profile.IsVerified());
2282 personal_data_->SaveImportedProfile(new_verified_profile);
2284 // Verify that the web database has been updated and the notification sent.
2285 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2286 .WillOnce(QuitMainMessageLoop());
2287 base::MessageLoop::current()->Run();
2289 // The new profile should be merged into the existing one.
2290 AutofillProfile expected_profile = new_verified_profile;
2291 expected_profile.set_guid(profile.guid());
2292 std::vector<base::string16> first_names, middle_names, last_names;
2293 expected_profile.GetRawMultiInfo(NAME_FIRST, &first_names);
2294 expected_profile.GetRawMultiInfo(NAME_MIDDLE, &middle_names);
2295 expected_profile.GetRawMultiInfo(NAME_LAST, &last_names);
2296 first_names.insert(first_names.begin(), ASCIIToUTF16("Marion"));
2297 middle_names.insert(middle_names.begin(), ASCIIToUTF16("Mitchell"));
2298 last_names.insert(last_names.begin(), ASCIIToUTF16("Morrison"));
2299 expected_profile.SetRawMultiInfo(NAME_FIRST, first_names);
2300 expected_profile.SetRawMultiInfo(NAME_MIDDLE, middle_names);
2301 expected_profile.SetRawMultiInfo(NAME_LAST, last_names);
2303 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
2304 ASSERT_EQ(1U, results.size());
2305 EXPECT_EQ(expected_profile, *results[0]);
2308 // Ensure that verified credit cards can be saved via SaveImportedCreditCard.
2309 TEST_F(PersonalDataManagerTest, SaveImportedCreditCardWithVerifiedData) {
2310 // Start with a verified credit card.
2311 CreditCard credit_card(base::GenerateGUID(), "Chrome settings");
2312 test::SetCreditCardInfo(&credit_card,
2313 "Biggie Smalls", "4111 1111 1111 1111" /* Visa */, "01", "2011");
2314 EXPECT_TRUE(credit_card.IsVerified());
2316 // Add the credit card to the database.
2317 personal_data_->AddCreditCard(credit_card);
2319 // Verify that the web database has been updated and the notification sent.
2320 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2321 .WillOnce(QuitMainMessageLoop());
2322 base::MessageLoop::current()->Run();
2324 CreditCard new_verified_card = credit_card;
2325 new_verified_card.set_guid(base::GenerateGUID());
2326 new_verified_card.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("B. Small"));
2327 EXPECT_TRUE(new_verified_card.IsVerified());
2329 personal_data_->SaveImportedCreditCard(new_verified_card);
2331 // Verify that the web database has been updated and the notification sent.
2332 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2333 .WillOnce(QuitMainMessageLoop());
2334 base::MessageLoop::current()->Run();
2336 // Expect that the saved credit card is updated.
2337 const std::vector<CreditCard*>& results = personal_data_->GetCreditCards();
2338 ASSERT_EQ(1U, results.size());
2339 EXPECT_EQ(ASCIIToUTF16("B. Small"), results[0]->GetRawInfo(CREDIT_CARD_NAME));
2342 TEST_F(PersonalDataManagerTest, GetNonEmptyTypes) {
2343 // Check that there are no available types with no profiles stored.
2344 ServerFieldTypeSet non_empty_types;
2345 personal_data_->GetNonEmptyTypes(&non_empty_types);
2346 EXPECT_EQ(0U, non_empty_types.size());
2348 // Test with one profile stored.
2349 AutofillProfile profile0(base::GenerateGUID(), "https://www.example.com");
2350 test::SetProfileInfo(&profile0,
2351 "Marion", NULL, "Morrison",
2352 "johnwayne@me.xyz", NULL, "123 Zoo St.", NULL, "Hollywood", "CA",
2353 "91601", "US", "14155678910");
2355 personal_data_->AddProfile(profile0);
2357 // Verify that the web database has been updated and the notification sent.
2358 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2359 .WillOnce(QuitMainMessageLoop());
2360 base::MessageLoop::current()->Run();
2362 personal_data_->GetNonEmptyTypes(&non_empty_types);
2363 EXPECT_EQ(15U, non_empty_types.size());
2364 EXPECT_TRUE(non_empty_types.count(NAME_FIRST));
2365 EXPECT_TRUE(non_empty_types.count(NAME_LAST));
2366 EXPECT_TRUE(non_empty_types.count(NAME_FULL));
2367 EXPECT_TRUE(non_empty_types.count(EMAIL_ADDRESS));
2368 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_LINE1));
2369 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_STREET_ADDRESS));
2370 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_CITY));
2371 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_STATE));
2372 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_ZIP));
2373 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_COUNTRY));
2374 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_NUMBER));
2375 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_COUNTRY_CODE));
2376 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_CITY_CODE));
2377 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_CITY_AND_NUMBER));
2378 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_WHOLE_NUMBER));
2380 // Test with multiple profiles stored.
2381 AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com");
2382 test::SetProfileInfo(&profile1,
2383 "Josephine", "Alicia", "Saenz",
2384 "joewayne@me.xyz", "Fox", "903 Apple Ct.", NULL, "Orlando", "FL", "32801",
2385 "US", "16502937549");
2387 AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com");
2388 test::SetProfileInfo(&profile2,
2389 "Josephine", "Alicia", "Saenz",
2390 "joewayne@me.xyz", "Fox", "1212 Center.", "Bld. 5", "Orlando", "FL",
2391 "32801", "US", "16502937549");
2393 personal_data_->AddProfile(profile1);
2394 personal_data_->AddProfile(profile2);
2396 // Verify that the web database has been updated and the notification sent.
2397 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2398 .WillOnce(QuitMainMessageLoop());
2399 base::MessageLoop::current()->Run();
2401 personal_data_->GetNonEmptyTypes(&non_empty_types);
2402 EXPECT_EQ(19U, non_empty_types.size());
2403 EXPECT_TRUE(non_empty_types.count(NAME_FIRST));
2404 EXPECT_TRUE(non_empty_types.count(NAME_MIDDLE));
2405 EXPECT_TRUE(non_empty_types.count(NAME_MIDDLE_INITIAL));
2406 EXPECT_TRUE(non_empty_types.count(NAME_LAST));
2407 EXPECT_TRUE(non_empty_types.count(NAME_FULL));
2408 EXPECT_TRUE(non_empty_types.count(EMAIL_ADDRESS));
2409 EXPECT_TRUE(non_empty_types.count(COMPANY_NAME));
2410 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_LINE1));
2411 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_LINE2));
2412 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_STREET_ADDRESS));
2413 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_CITY));
2414 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_STATE));
2415 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_ZIP));
2416 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_COUNTRY));
2417 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_NUMBER));
2418 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_CITY_CODE));
2419 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_COUNTRY_CODE));
2420 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_CITY_AND_NUMBER));
2421 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_WHOLE_NUMBER));
2423 // Test with credit card information also stored.
2424 CreditCard credit_card(base::GenerateGUID(), "https://www.example.com");
2425 test::SetCreditCardInfo(&credit_card,
2426 "John Dillinger", "423456789012" /* Visa */,
2427 "01", "2010");
2428 personal_data_->AddCreditCard(credit_card);
2430 // Verify that the web database has been updated and the notification sent.
2431 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2432 .WillOnce(QuitMainMessageLoop());
2433 base::MessageLoop::current()->Run();
2435 personal_data_->GetNonEmptyTypes(&non_empty_types);
2436 EXPECT_EQ(27U, non_empty_types.size());
2437 EXPECT_TRUE(non_empty_types.count(NAME_FIRST));
2438 EXPECT_TRUE(non_empty_types.count(NAME_MIDDLE));
2439 EXPECT_TRUE(non_empty_types.count(NAME_MIDDLE_INITIAL));
2440 EXPECT_TRUE(non_empty_types.count(NAME_LAST));
2441 EXPECT_TRUE(non_empty_types.count(NAME_FULL));
2442 EXPECT_TRUE(non_empty_types.count(EMAIL_ADDRESS));
2443 EXPECT_TRUE(non_empty_types.count(COMPANY_NAME));
2444 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_LINE1));
2445 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_LINE2));
2446 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_STREET_ADDRESS));
2447 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_CITY));
2448 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_STATE));
2449 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_ZIP));
2450 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_COUNTRY));
2451 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_NUMBER));
2452 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_CITY_CODE));
2453 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_COUNTRY_CODE));
2454 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_CITY_AND_NUMBER));
2455 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_WHOLE_NUMBER));
2456 EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_NAME));
2457 EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_NUMBER));
2458 EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_TYPE));
2459 EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_EXP_MONTH));
2460 EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_EXP_2_DIGIT_YEAR));
2461 EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_EXP_4_DIGIT_YEAR));
2462 EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR));
2463 EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR));
2466 TEST_F(PersonalDataManagerTest, CaseInsensitiveMultiValueAggregation) {
2467 FormData form1;
2468 FormFieldData field;
2469 test::CreateTestFormField(
2470 "First name:", "first_name", "George", "text", &field);
2471 form1.fields.push_back(field);
2472 test::CreateTestFormField(
2473 "Last name:", "last_name", "Washington", "text", &field);
2474 form1.fields.push_back(field);
2475 test::CreateTestFormField(
2476 "Email:", "email", "theprez@gmail.com", "text", &field);
2477 form1.fields.push_back(field);
2478 test::CreateTestFormField(
2479 "Address:", "address1", "21 Laussat St", "text", &field);
2480 form1.fields.push_back(field);
2481 test::CreateTestFormField(
2482 "City:", "city", "San Francisco", "text", &field);
2483 form1.fields.push_back(field);
2484 test::CreateTestFormField("State:", "state", "California", "text", &field);
2485 form1.fields.push_back(field);
2486 test::CreateTestFormField(
2487 "Zip:", "zip", "94102", "text", &field);
2488 form1.fields.push_back(field);
2489 test::CreateTestFormField(
2490 "Phone number:", "phone_number", "817-555-6789", "text", &field);
2491 form1.fields.push_back(field);
2493 FormStructure form_structure1(form1);
2494 form_structure1.DetermineHeuristicTypes();
2495 scoped_ptr<CreditCard> imported_credit_card;
2496 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
2497 &imported_credit_card));
2498 ASSERT_FALSE(imported_credit_card);
2500 // Verify that the web database has been updated and the notification sent.
2501 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2502 .WillOnce(QuitMainMessageLoop());
2503 base::MessageLoop::current()->Run();
2505 AutofillProfile expected(base::GenerateGUID(), "https://www.example.com");
2506 test::SetProfileInfo(&expected,
2507 "George",
2508 NULL,
2509 "Washington",
2510 "theprez@gmail.com",
2511 NULL,
2512 "21 Laussat St",
2513 NULL,
2514 "San Francisco",
2515 "California",
2516 "94102",
2517 NULL,
2518 "817-555-6789");
2519 const std::vector<AutofillProfile*>& results1 = personal_data_->GetProfiles();
2520 ASSERT_EQ(1U, results1.size());
2521 EXPECT_EQ(0, expected.Compare(*results1[0]));
2523 // Upper-case the first name and change the phone number.
2524 FormData form2;
2525 test::CreateTestFormField(
2526 "First name:", "first_name", "GEORGE", "text", &field);
2527 form2.fields.push_back(field);
2528 test::CreateTestFormField(
2529 "Last name:", "last_name", "Washington", "text", &field);
2530 form2.fields.push_back(field);
2531 test::CreateTestFormField(
2532 "Email:", "email", "theprez@gmail.com", "text", &field);
2533 form2.fields.push_back(field);
2534 test::CreateTestFormField(
2535 "Address:", "address1", "21 Laussat St", "text", &field);
2536 form2.fields.push_back(field);
2537 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
2538 form2.fields.push_back(field);
2539 test::CreateTestFormField("State:", "state", "California", "text", &field);
2540 form2.fields.push_back(field);
2541 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
2542 form2.fields.push_back(field);
2543 test::CreateTestFormField(
2544 "Phone number:", "phone_number", "214-555-1234", "text", &field);
2545 form2.fields.push_back(field);
2547 FormStructure form_structure2(form2);
2548 form_structure2.DetermineHeuristicTypes();
2549 EXPECT_TRUE(personal_data_->ImportFormData(form_structure2,
2550 &imported_credit_card));
2551 ASSERT_FALSE(imported_credit_card);
2553 // Verify that the web database has been updated and the notification sent.
2554 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2555 .WillOnce(QuitMainMessageLoop());
2556 base::MessageLoop::current()->Run();
2558 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
2560 // Modify expected to include multi-valued fields.
2561 std::vector<base::string16> values;
2562 expected.GetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, &values);
2563 values.push_back(ASCIIToUTF16("214-555-1234"));
2564 expected.SetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, values);
2566 ASSERT_EQ(1U, results2.size());
2567 EXPECT_EQ(0, expected.Compare(*results2[0]));
2570 TEST_F(PersonalDataManagerTest, IncognitoReadOnly) {
2571 ASSERT_TRUE(personal_data_->GetProfiles().empty());
2572 ASSERT_TRUE(personal_data_->GetCreditCards().empty());
2574 AutofillProfile steve_jobs(base::GenerateGUID(), "https://www.example.com");
2575 test::SetProfileInfo(&steve_jobs, "Steven", "Paul", "Jobs", "sjobs@apple.com",
2576 "Apple Computer, Inc.", "1 Infinite Loop", "", "Cupertino", "CA", "95014",
2577 "US", "(800) 275-2273");
2578 personal_data_->AddProfile(steve_jobs);
2580 CreditCard bill_gates(base::GenerateGUID(), "https://www.example.com");
2581 test::SetCreditCardInfo(
2582 &bill_gates, "William H. Gates", "5555555555554444", "1", "2020");
2583 personal_data_->AddCreditCard(bill_gates);
2585 // The personal data manager should be able to read existing profiles in an
2586 // off-the-record context.
2587 ResetPersonalDataManager(USER_MODE_INCOGNITO);
2588 ASSERT_EQ(1U, personal_data_->GetProfiles().size());
2589 ASSERT_EQ(1U, personal_data_->GetCreditCards().size());
2591 // No adds, saves, or updates should take effect.
2592 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()).Times(0);
2594 // Add profiles or credit card shouldn't work.
2595 personal_data_->AddProfile(test::GetFullProfile());
2597 CreditCard larry_page(base::GenerateGUID(), "https://www.example.com");
2598 test::SetCreditCardInfo(
2599 &larry_page, "Lawrence Page", "4111111111111111", "10", "2025");
2600 personal_data_->AddCreditCard(larry_page);
2602 ResetPersonalDataManager(USER_MODE_INCOGNITO);
2603 EXPECT_EQ(1U, personal_data_->GetProfiles().size());
2604 EXPECT_EQ(1U, personal_data_->GetCreditCards().size());
2606 // Saving or creating profiles from imported profiles shouldn't work.
2607 steve_jobs.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Steve"));
2608 personal_data_->SaveImportedProfile(steve_jobs);
2610 bill_gates.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Bill Gates"));
2611 personal_data_->SaveImportedCreditCard(bill_gates);
2613 ResetPersonalDataManager(USER_MODE_INCOGNITO);
2614 EXPECT_EQ(ASCIIToUTF16("Steven"),
2615 personal_data_->GetProfiles()[0]->GetRawInfo(NAME_FIRST));
2616 EXPECT_EQ(ASCIIToUTF16("William H. Gates"),
2617 personal_data_->GetCreditCards()[0]->GetRawInfo(CREDIT_CARD_NAME));
2619 // Updating existing profiles shouldn't work.
2620 steve_jobs.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Steve"));
2621 personal_data_->UpdateProfile(steve_jobs);
2623 bill_gates.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Bill Gates"));
2624 personal_data_->UpdateCreditCard(bill_gates);
2626 ResetPersonalDataManager(USER_MODE_INCOGNITO);
2627 EXPECT_EQ(ASCIIToUTF16("Steven"),
2628 personal_data_->GetProfiles()[0]->GetRawInfo(NAME_FIRST));
2629 EXPECT_EQ(ASCIIToUTF16("William H. Gates"),
2630 personal_data_->GetCreditCards()[0]->GetRawInfo(CREDIT_CARD_NAME));
2632 // Removing shouldn't work.
2633 personal_data_->RemoveByGUID(steve_jobs.guid());
2634 personal_data_->RemoveByGUID(bill_gates.guid());
2636 ResetPersonalDataManager(USER_MODE_INCOGNITO);
2637 EXPECT_EQ(1U, personal_data_->GetProfiles().size());
2638 EXPECT_EQ(1U, personal_data_->GetCreditCards().size());
2641 TEST_F(PersonalDataManagerTest, DefaultCountryCodeIsCached) {
2642 // The return value should always be some country code, no matter what.
2643 std::string default_country =
2644 personal_data_->GetDefaultCountryCodeForNewAddress();
2645 EXPECT_EQ(2U, default_country.size());
2647 AutofillProfile moose(base::GenerateGUID(), "Chrome settings");
2648 test::SetProfileInfo(&moose, "Moose", "P", "McMahon", "mpm@example.com",
2649 "", "1 Taiga TKTR", "", "Calgary", "AB", "T2B 2K2",
2650 "CA", "(800) 555-9000");
2651 personal_data_->AddProfile(moose);
2652 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2653 .WillOnce(QuitMainMessageLoop());
2654 base::MessageLoop::current()->Run();
2655 // The value is cached and doesn't change even after adding an address.
2656 EXPECT_EQ(default_country,
2657 personal_data_->GetDefaultCountryCodeForNewAddress());
2659 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()).Times(2);
2661 // Disabling Autofill blows away this cache and shouldn't account for Autofill
2662 // profiles.
2663 prefs_->SetBoolean(prefs::kAutofillEnabled, false);
2664 EXPECT_EQ(default_country,
2665 personal_data_->GetDefaultCountryCodeForNewAddress());
2667 // Enabling Autofill blows away the cached value and should reflect the new
2668 // value (accounting for profiles).
2669 prefs_->SetBoolean(prefs::kAutofillEnabled, true);
2670 EXPECT_EQ(base::UTF16ToUTF8(moose.GetRawInfo(ADDRESS_HOME_COUNTRY)),
2671 personal_data_->GetDefaultCountryCodeForNewAddress());
2674 TEST_F(PersonalDataManagerTest, DefaultCountryCodeComesFromProfiles) {
2675 AutofillProfile moose(base::GenerateGUID(), "Chrome settings");
2676 test::SetProfileInfo(&moose, "Moose", "P", "McMahon", "mpm@example.com",
2677 "", "1 Taiga TKTR", "", "Calgary", "AB", "T2B 2K2",
2678 "CA", "(800) 555-9000");
2679 personal_data_->AddProfile(moose);
2680 ResetPersonalDataManager(USER_MODE_NORMAL);
2681 EXPECT_EQ("CA", personal_data_->GetDefaultCountryCodeForNewAddress());
2683 // Multiple profiles cast votes.
2684 AutofillProfile armadillo(base::GenerateGUID(), "Chrome settings");
2685 test::SetProfileInfo(&armadillo, "Armin", "Dill", "Oh", "ado@example.com",
2686 "", "1 Speed Bump", "", "Lubbock", "TX", "77500",
2687 "MX", "(800) 555-9000");
2688 AutofillProfile armadillo2(base::GenerateGUID(), "Chrome settings");
2689 test::SetProfileInfo(&armadillo2, "Armin", "Dill", "Oh", "ado@example.com",
2690 "", "2 Speed Bump", "", "Lubbock", "TX", "77500",
2691 "MX", "(800) 555-9000");
2692 personal_data_->AddProfile(armadillo);
2693 personal_data_->AddProfile(armadillo2);
2694 ResetPersonalDataManager(USER_MODE_NORMAL);
2695 EXPECT_EQ("MX", personal_data_->GetDefaultCountryCodeForNewAddress());
2697 personal_data_->RemoveByGUID(armadillo.guid());
2698 personal_data_->RemoveByGUID(armadillo2.guid());
2699 ResetPersonalDataManager(USER_MODE_NORMAL);
2700 // Verified profiles count more.
2701 armadillo.set_origin("http://randomwebsite.com");
2702 armadillo2.set_origin("http://randomwebsite.com");
2703 personal_data_->AddProfile(armadillo);
2704 personal_data_->AddProfile(armadillo2);
2705 ResetPersonalDataManager(USER_MODE_NORMAL);
2706 EXPECT_EQ("CA", personal_data_->GetDefaultCountryCodeForNewAddress());
2708 personal_data_->RemoveByGUID(armadillo.guid());
2709 ResetPersonalDataManager(USER_MODE_NORMAL);
2710 // But unverified profiles can be a tie breaker.
2711 armadillo.set_origin("Chrome settings");
2712 personal_data_->AddProfile(armadillo);
2713 ResetPersonalDataManager(USER_MODE_NORMAL);
2714 EXPECT_EQ("MX", personal_data_->GetDefaultCountryCodeForNewAddress());
2716 // Invalid country codes are ignored.
2717 personal_data_->RemoveByGUID(armadillo.guid());
2718 personal_data_->RemoveByGUID(moose.guid());
2719 AutofillProfile space_invader(base::GenerateGUID(), "Chrome settings");
2720 test::SetProfileInfo(&space_invader, "Marty", "", "Martian",
2721 "mm@example.com", "", "1 Flying Object", "", "Valles Marineris", "",
2722 "", "XX", "");
2723 personal_data_->AddProfile(moose);
2724 ResetPersonalDataManager(USER_MODE_NORMAL);
2725 EXPECT_EQ("MX", personal_data_->GetDefaultCountryCodeForNewAddress());
2728 TEST_F(PersonalDataManagerTest, UpdateLanguageCodeInProfile) {
2729 AutofillProfile profile(base::GenerateGUID(), "https://www.example.com");
2730 test::SetProfileInfo(&profile,
2731 "Marion", "Mitchell", "Morrison",
2732 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
2733 "91601", "US", "12345678910");
2734 personal_data_->AddProfile(profile);
2736 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2737 .WillOnce(QuitMainMessageLoop());
2738 base::MessageLoop::current()->Run();
2740 profile.set_language_code("en");
2741 personal_data_->UpdateProfile(profile);
2743 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2744 .WillOnce(QuitMainMessageLoop());
2745 base::MessageLoop::current()->Run();
2747 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
2748 ASSERT_EQ(1U, results.size());
2749 EXPECT_EQ(0, profile.Compare(*results[0]));
2750 EXPECT_EQ("en", results[0]->language_code());
2753 TEST_F(PersonalDataManagerTest, GetProfileSuggestions) {
2754 AutofillProfile profile(base::GenerateGUID(), "https://www.example.com");
2755 test::SetProfileInfo(&profile,
2756 "Marion", "Mitchell", "Morrison",
2757 "johnwayne@me.xyz", "Fox",
2758 "123 Zoo St.\nSecond Line\nThird line", "unit 5", "Hollywood", "CA",
2759 "91601", "US", "12345678910");
2760 personal_data_->AddProfile(profile);
2761 ResetPersonalDataManager(USER_MODE_NORMAL);
2763 std::vector<Suggestion> suggestions = personal_data_->GetProfileSuggestions(
2764 AutofillType(ADDRESS_HOME_STREET_ADDRESS),
2765 base::UTF8ToUTF16("123"),
2766 false,
2767 std::vector<ServerFieldType>());
2768 ASSERT_FALSE(suggestions.empty());
2769 EXPECT_EQ(suggestions[0].value,
2770 base::UTF8ToUTF16("123 Zoo St., Second Line, Third line, unit 5"));
2773 TEST_F(PersonalDataManagerTest, GetCreditCardSuggestions) {
2774 EnableWalletCardImport();
2776 CreditCard credit_card0("287151C8-6AB1-487C-9095-28E80BE5DA15",
2777 "https://www.example.com");
2778 test::SetCreditCardInfo(&credit_card0,
2779 "Clyde Barrow", "347666888555" /* American Express */, "04", "2015");
2780 credit_card0.set_use_count(2);
2781 personal_data_->AddCreditCard(credit_card0);
2783 CreditCard credit_card1("1141084B-72D7-4B73-90CF-3D6AC154673B",
2784 "https://www.example.com");
2785 credit_card1.set_use_count(3);
2786 test::SetCreditCardInfo(&credit_card1, "John Dillinger", "", "01", "2010");
2787 personal_data_->AddCreditCard(credit_card1);
2789 CreditCard credit_card2("002149C1-EE28-4213-A3B9-DA243FFF021B",
2790 "https://www.example.com");
2791 credit_card2.set_use_count(1);
2792 test::SetCreditCardInfo(&credit_card2,
2793 "Bonnie Parker", "518765432109" /* Mastercard */, "", "");
2794 personal_data_->AddCreditCard(credit_card2);
2796 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2797 .WillOnce(QuitMainMessageLoop());
2798 base::MessageLoop::current()->Run();
2800 // Sublabel is card number when filling name (exact format depends on
2801 // the platform, but the last 4 digits should appear).
2802 std::vector<Suggestion> suggestions =
2803 personal_data_->GetCreditCardSuggestions(
2804 AutofillType(CREDIT_CARD_NAME), base::string16());
2805 ASSERT_EQ(3U, suggestions.size());
2806 // Ordered by MFU.
2807 EXPECT_EQ(ASCIIToUTF16("Clyde Barrow"), suggestions[1].value);
2808 EXPECT_TRUE(suggestions[1].label.find(ASCIIToUTF16("8555")) !=
2809 base::string16::npos);
2810 EXPECT_EQ(ASCIIToUTF16("John Dillinger"), suggestions[0].value);
2811 EXPECT_EQ(base::string16(), suggestions[0].label);
2812 EXPECT_EQ(ASCIIToUTF16("Bonnie Parker"), suggestions[2].value);
2813 EXPECT_TRUE(suggestions[2].label.find(ASCIIToUTF16("2109")) !=
2814 base::string16::npos);
2816 // Sublabel is expiration date when filling card number.
2817 suggestions = personal_data_->GetCreditCardSuggestions(
2818 AutofillType(CREDIT_CARD_NUMBER), base::string16());
2819 ASSERT_EQ(2U, suggestions.size());
2820 EXPECT_EQ(ASCIIToUTF16("Amex - 8555"), suggestions[0].value);
2821 EXPECT_EQ(ASCIIToUTF16("04/15"), suggestions[0].label);
2822 EXPECT_EQ(ASCIIToUTF16("MasterCard - 2109"), suggestions[1].value);
2823 EXPECT_EQ(base::string16(), suggestions[1].label);
2825 // Add some server cards. If there are local dupes, the locals should be
2826 // hidden.
2827 std::vector<CreditCard> server_cards;
2828 // This server card matches a local card, except the local card is missing the
2829 // number. This should count as a dupe.
2830 server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "a123"));
2831 test::SetCreditCardInfo(&server_cards.back(), "John Dillinger",
2832 "9012" /* Visa */, "01", "2010");
2833 server_cards.back().SetTypeForMaskedCard(kVisaCard);
2835 // This server card is identical to a local card, but has a different
2836 // card type. Not a dupe.
2837 server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "b456"));
2838 test::SetCreditCardInfo(&server_cards.back(), "Bonnie Parker",
2839 "2109", "12", "2012");
2840 server_cards.back().SetTypeForMaskedCard(kVisaCard);
2842 // This unmasked server card is a dupe.
2843 server_cards.push_back(CreditCard(CreditCard::FULL_SERVER_CARD, "c789"));
2844 test::SetCreditCardInfo(&server_cards.back(), "Clyde Barrow",
2845 "347666888555" /* American Express */, "04", "2015");
2847 autofill_table_->SetServerCreditCards(server_cards);
2848 personal_data_->Refresh();
2849 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2850 .WillOnce(QuitMainMessageLoop());
2851 base::MessageLoop::current()->Run();
2853 suggestions = personal_data_->GetCreditCardSuggestions(
2854 AutofillType(CREDIT_CARD_NAME), base::string16());
2855 ASSERT_EQ(4U, suggestions.size());
2856 EXPECT_EQ(ASCIIToUTF16("Bonnie Parker"), suggestions[0].value);
2857 EXPECT_EQ(suggestions[0].backend_id.guid, credit_card2.guid());
2858 EXPECT_EQ(ASCIIToUTF16("John Dillinger"), suggestions[1].value);
2859 EXPECT_NE(suggestions[1].backend_id.guid, credit_card1.guid());
2860 EXPECT_EQ(ASCIIToUTF16("Bonnie Parker"), suggestions[2].value);
2861 EXPECT_NE(suggestions[2].backend_id.guid, credit_card2.guid());
2862 EXPECT_EQ(ASCIIToUTF16("Clyde Barrow"), suggestions[3].value);
2863 EXPECT_NE(suggestions[0].backend_id.guid, credit_card0.guid());
2865 suggestions = personal_data_->GetCreditCardSuggestions(
2866 AutofillType(CREDIT_CARD_NUMBER), base::string16());
2867 ASSERT_EQ(4U, suggestions.size());
2868 EXPECT_EQ(ASCIIToUTF16("MasterCard - 2109"), suggestions[0].value);
2869 EXPECT_EQ(ASCIIToUTF16("Visa - 9012"), suggestions[1].value);
2870 EXPECT_EQ(ASCIIToUTF16("Visa - 2109"), suggestions[2].value);
2871 EXPECT_EQ(ASCIIToUTF16("Amex - 8555"), suggestions[3].value);
2873 // Make sure a server card can be a dupe of more than one local card.
2874 CreditCard credit_card3("4141084B-72D7-4B73-90CF-3D6AC154673B",
2875 "https://www.example.com");
2876 test::SetCreditCardInfo(&credit_card3, "John Dillinger", "", "01", "");
2877 personal_data_->AddCreditCard(credit_card3);
2879 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2880 .WillOnce(QuitMainMessageLoop());
2881 base::MessageLoop::current()->Run();
2883 suggestions = personal_data_->GetCreditCardSuggestions(
2884 AutofillType(CREDIT_CARD_NAME), base::string16());
2885 ASSERT_EQ(4U, suggestions.size());
2886 EXPECT_EQ(ASCIIToUTF16("Bonnie Parker"), suggestions[0].value);
2887 EXPECT_EQ(ASCIIToUTF16("John Dillinger"), suggestions[1].value);
2888 EXPECT_EQ(ASCIIToUTF16("Bonnie Parker"), suggestions[2].value);
2889 EXPECT_EQ(ASCIIToUTF16("Clyde Barrow"), suggestions[3].value);
2892 #if defined(OS_MACOSX) && !defined(OS_IOS)
2893 TEST_F(PersonalDataManagerTest, ShowAddressBookPrompt) {
2894 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()).Times(2);
2896 AutofillType type(ADDRESS_HOME_STREET_ADDRESS);
2898 prefs_->SetBoolean(prefs::kAutofillEnabled, false);
2899 EXPECT_FALSE(personal_data_->ShouldShowAccessAddressBookSuggestion(type));
2901 prefs_->SetBoolean(prefs::kAutofillEnabled, true);
2902 EXPECT_TRUE(personal_data_->ShouldShowAccessAddressBookSuggestion(type));
2904 // Adding an Autofill Profile should prevent the prompt from appearing.
2905 AutofillProfile profile(base::GenerateGUID(), "https://www.example.com/");
2906 test::SetProfileInfo(&profile,
2907 "Marion", "Mitchell", "Morrison",
2908 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
2909 "91601", "US", "12345678910");
2910 personal_data_->AddProfile(profile);
2912 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2913 .WillOnce(QuitMainMessageLoop());
2914 base::MessageLoop::current()->Run();
2916 EXPECT_FALSE(personal_data_->ShouldShowAccessAddressBookSuggestion(type));
2919 // Tests that the logic to show the access Address Book prompt respects the
2920 // preference that indicates the total number of times the prompt has already
2921 // been shown.
2922 TEST_F(PersonalDataManagerTest, MaxTimesToShowAddressBookPrompt) {
2923 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()).Times(1);
2925 AutofillType type(ADDRESS_HOME_STREET_ADDRESS);
2927 prefs_->SetBoolean(prefs::kAutofillEnabled, true);
2928 EXPECT_TRUE(personal_data_->ShouldShowAccessAddressBookSuggestion(type));
2930 prefs_->SetInteger(prefs::kAutofillMacAddressBookShowedCount, 4);
2931 EXPECT_TRUE(personal_data_->ShouldShowAccessAddressBookSuggestion(type));
2933 prefs_->SetInteger(prefs::kAutofillMacAddressBookShowedCount, 6);
2934 EXPECT_FALSE(personal_data_->ShouldShowAccessAddressBookSuggestion(type));
2936 #endif // defined(OS_MACOSX) && !defined(OS_IOS)
2938 TEST_F(PersonalDataManagerTest, RecordUseOf) {
2939 AutofillProfile profile(autofill::test::GetFullProfile());
2940 EXPECT_EQ(0U, profile.use_count());
2941 EXPECT_EQ(base::Time(), profile.use_date());
2942 EXPECT_EQ(base::Time(), profile.modification_date());
2943 personal_data_->AddProfile(profile);
2945 CreditCard credit_card(base::GenerateGUID(), "https://www.example.com");
2946 test::SetCreditCardInfo(&credit_card, "John Dillinger",
2947 "423456789012" /* Visa */, "01", "2010");
2948 EXPECT_EQ(0U, credit_card.use_count());
2949 EXPECT_EQ(base::Time(), credit_card.use_date());
2950 EXPECT_EQ(base::Time(), credit_card.modification_date());
2951 personal_data_->AddCreditCard(credit_card);
2953 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2954 .WillOnce(QuitMainMessageLoop());
2955 base::MessageLoop::current()->Run();
2957 // Notify the PDM that the profile and credit card were used.
2958 AutofillProfile* added_profile =
2959 personal_data_->GetProfileByGUID(profile.guid());
2960 ASSERT_TRUE(added_profile);
2961 EXPECT_EQ(*added_profile, profile);
2962 EXPECT_EQ(0U, added_profile->use_count());
2963 EXPECT_EQ(base::Time(), added_profile->use_date());
2964 EXPECT_NE(base::Time(), added_profile->modification_date());
2965 personal_data_->RecordUseOf(profile);
2967 CreditCard* added_card =
2968 personal_data_->GetCreditCardByGUID(credit_card.guid());
2969 ASSERT_TRUE(added_card);
2970 EXPECT_EQ(*added_card, credit_card);
2971 EXPECT_EQ(0U, added_card->use_count());
2972 EXPECT_EQ(base::Time(), added_card->use_date());
2973 EXPECT_NE(base::Time(), added_card->modification_date());
2974 personal_data_->RecordUseOf(credit_card);
2976 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
2977 .WillOnce(QuitMainMessageLoop());
2978 base::MessageLoop::current()->Run();
2980 // Verify usage stats are updated.
2981 added_profile = personal_data_->GetProfileByGUID(profile.guid());
2982 ASSERT_TRUE(added_profile);
2983 EXPECT_EQ(1U, added_profile->use_count());
2984 EXPECT_NE(base::Time(), added_profile->use_date());
2985 EXPECT_NE(base::Time(), added_profile->modification_date());
2987 added_card = personal_data_->GetCreditCardByGUID(credit_card.guid());
2988 ASSERT_TRUE(added_card);
2989 EXPECT_EQ(1U, added_card->use_count());
2990 EXPECT_NE(base::Time(), added_card->use_date());
2991 EXPECT_NE(base::Time(), added_card->modification_date());
2994 TEST_F(PersonalDataManagerTest, UpdateServerCreditCardUsageStats) {
2995 EnableWalletCardImport();
2997 std::vector<CreditCard> server_cards;
2998 server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "a123"));
2999 test::SetCreditCardInfo(&server_cards.back(), "John Dillinger",
3000 "9012" /* Visa */, "01", "2010");
3001 server_cards.back().SetTypeForMaskedCard(kVisaCard);
3003 server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "b456"));
3004 test::SetCreditCardInfo(&server_cards.back(), "Bonnie Parker",
3005 "2109" /* Mastercard */, "12", "2012");
3006 server_cards.back().SetTypeForMaskedCard(kMasterCard);
3008 server_cards.push_back(CreditCard(CreditCard::FULL_SERVER_CARD, "c789"));
3009 test::SetCreditCardInfo(&server_cards.back(), "Clyde Barrow",
3010 "347666888555" /* American Express */, "04", "2015");
3012 autofill_table_->SetServerCreditCards(server_cards);
3013 personal_data_->Refresh();
3015 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
3016 .WillOnce(QuitMainMessageLoop());
3017 base::MessageLoop::current()->Run();
3019 ASSERT_EQ(3U, personal_data_->GetCreditCards().size());
3020 // The GUIDs will be different, so just compare the data.
3021 for (size_t i = 0; i < 3; ++i)
3022 EXPECT_EQ(0, server_cards[i].Compare(*personal_data_->GetCreditCards()[i]));
3024 CreditCard* unmasked_card = &server_cards.front();
3025 unmasked_card->set_record_type(CreditCard::FULL_SERVER_CARD);
3026 unmasked_card->SetNumber(ASCIIToUTF16("423456789012"));
3027 EXPECT_NE(0, unmasked_card->Compare(
3028 *personal_data_->GetCreditCards().front()));
3029 personal_data_->UpdateServerCreditCard(*unmasked_card);
3031 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
3032 .WillOnce(QuitMainMessageLoop());
3033 base::MessageLoop::current()->Run();
3035 for (size_t i = 0; i < 3; ++i)
3036 EXPECT_EQ(0, server_cards[i].Compare(*personal_data_->GetCreditCards()[i]));
3038 // For an unmasked card, usage data starts out as 1 and Now().
3039 EXPECT_EQ(1U, personal_data_->GetCreditCards()[0]->use_count());
3040 EXPECT_NE(base::Time(), personal_data_->GetCreditCards()[0]->use_date());
3042 EXPECT_EQ(0U, personal_data_->GetCreditCards()[1]->use_count());
3043 EXPECT_EQ(base::Time(), personal_data_->GetCreditCards()[1]->use_date());
3045 EXPECT_EQ(0U, personal_data_->GetCreditCards()[2]->use_count());
3046 EXPECT_EQ(base::Time(), personal_data_->GetCreditCards()[2]->use_date());
3048 server_cards.back().set_guid(personal_data_->GetCreditCards()[2]->guid());
3049 personal_data_->RecordUseOf(server_cards.back());
3050 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
3051 .WillOnce(QuitMainMessageLoop());
3052 base::MessageLoop::current()->Run();
3054 EXPECT_EQ(1U, personal_data_->GetCreditCards()[0]->use_count());
3055 EXPECT_NE(base::Time(), personal_data_->GetCreditCards()[0]->use_date());
3057 EXPECT_EQ(0U, personal_data_->GetCreditCards()[1]->use_count());
3058 EXPECT_EQ(base::Time(), personal_data_->GetCreditCards()[1]->use_date());
3060 EXPECT_EQ(1U, personal_data_->GetCreditCards()[2]->use_count());
3061 EXPECT_NE(base::Time(), personal_data_->GetCreditCards()[2]->use_date());
3064 } // namespace autofill