rAc: change how enabling/disabling Autofill affects requestAutocomplete().
[chromium-blink-merge.git] / components / autofill / core / browser / personal_data_manager_unittest.cc
blobf0d32ea86fed7d3384b05deeb4901474fbf438ed
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 <string>
7 #include "base/basictypes.h"
8 #include "base/guid.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/synchronization/waitable_event.h"
14 #include "chrome/test/base/testing_profile.h"
15 #include "components/autofill/core/browser/autofill_metrics.h"
16 #include "components/autofill/core/browser/autofill_profile.h"
17 #include "components/autofill/core/browser/autofill_test_utils.h"
18 #include "components/autofill/core/browser/form_structure.h"
19 #include "components/autofill/core/browser/personal_data_manager.h"
20 #include "components/autofill/core/browser/personal_data_manager_observer.h"
21 #include "components/autofill/core/browser/webdata/autofill_table.h"
22 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
23 #include "components/autofill/core/common/autofill_pref_names.h"
24 #include "components/autofill/core/common/form_data.h"
25 #include "components/webdata/common/web_data_service_base.h"
26 #include "components/webdata/common/web_database_service.h"
27 #include "components/webdata/encryptor/encryptor.h"
28 #include "content/public/test/test_browser_thread.h"
29 #include "testing/gmock/include/gmock/gmock.h"
30 #include "testing/gtest/include/gtest/gtest.h"
32 using base::ASCIIToUTF16;
34 using content::BrowserThread;
36 namespace autofill {
37 namespace {
39 ACTION(QuitUIMessageLoop) {
40 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
41 base::MessageLoop::current()->Quit();
44 class PersonalDataLoadedObserverMock : public PersonalDataManagerObserver {
45 public:
46 PersonalDataLoadedObserverMock() {}
47 virtual ~PersonalDataLoadedObserverMock() {}
49 MOCK_METHOD0(OnPersonalDataChanged, void());
52 // Unlike the base AutofillMetrics, exposes copy and assignment constructors,
53 // which are handy for briefer test code. The AutofillMetrics class is
54 // stateless, so this is safe.
55 class TestAutofillMetrics : public AutofillMetrics {
56 public:
57 TestAutofillMetrics() {}
58 virtual ~TestAutofillMetrics() {}
61 } // anonymous namespace
63 class PersonalDataManagerTest : public testing::Test {
64 protected:
65 PersonalDataManagerTest()
66 : ui_thread_(BrowserThread::UI, &message_loop_),
67 db_thread_(BrowserThread::DB) {
70 virtual void SetUp() {
71 db_thread_.Start();
73 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
74 base::FilePath path = temp_dir_.path().AppendASCII("TestWebDB");
75 web_database_ = new WebDatabaseService(
76 path,
77 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
78 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB));
79 web_database_->AddTable(
80 scoped_ptr<WebDatabaseTable>(new AutofillTable("en-US")));
81 web_database_->LoadDatabase();
82 autofill_database_service_ = new AutofillWebDataService(
83 web_database_,
84 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
85 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB),
86 WebDataServiceBase::ProfileErrorCallback());
87 autofill_database_service_->Init();
89 profile_.reset(new TestingProfile);
90 profile_->CreateWebDataService();
92 test::DisableSystemServices(profile_.get());
93 ResetPersonalDataManager();
96 virtual void TearDown() {
97 // Destruction order is imposed explicitly here.
98 personal_data_.reset(NULL);
99 profile_.reset(NULL);
101 autofill_database_service_->ShutdownOnUIThread();
102 web_database_->ShutdownDatabase();
103 autofill_database_service_ = NULL;
104 web_database_ = NULL;
106 // Schedule another task on the DB thread to notify us that it's safe to
107 // stop the thread.
108 base::WaitableEvent done(false, false);
109 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
110 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done)));
111 done.Wait();
112 base::MessageLoop::current()->PostTask(FROM_HERE,
113 base::MessageLoop::QuitClosure());
114 base::MessageLoop::current()->Run();
115 db_thread_.Stop();
118 void ResetPersonalDataManager() {
119 personal_data_.reset(new PersonalDataManager("en-US"));
120 personal_data_->Init(
121 scoped_refptr<AutofillWebDataService>(autofill_database_service_),
122 profile_->GetPrefs(),
123 profile_->IsOffTheRecord());
124 personal_data_->AddObserver(&personal_data_observer_);
126 // Verify that the web database has been updated and the notification sent.
127 EXPECT_CALL(personal_data_observer_,
128 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
129 base::MessageLoop::current()->Run();
132 void MakeProfileIncognito() {
133 // Switch to an incognito profile.
134 profile_->ForceIncognito(true);
135 DCHECK(profile_->IsOffTheRecord());
138 base::MessageLoopForUI message_loop_;
139 content::TestBrowserThread ui_thread_;
140 content::TestBrowserThread db_thread_;
141 scoped_ptr<TestingProfile> profile_;
142 scoped_refptr<AutofillWebDataService> autofill_database_service_;
143 scoped_refptr<WebDatabaseService> web_database_;
144 base::ScopedTempDir temp_dir_;
145 scoped_ptr<PersonalDataManager> personal_data_;
146 PersonalDataLoadedObserverMock personal_data_observer_;
149 TEST_F(PersonalDataManagerTest, AddProfile) {
150 // Add profile0 to the database.
151 AutofillProfile profile0(autofill::test::GetFullProfile());
152 profile0.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("j@s.com"));
153 personal_data_->AddProfile(profile0);
155 // Reload the database.
156 ResetPersonalDataManager();
158 // Verify the addition.
159 const std::vector<AutofillProfile*>& results1 = personal_data_->GetProfiles();
160 ASSERT_EQ(1U, results1.size());
161 EXPECT_EQ(0, profile0.Compare(*results1[0]));
163 // Add profile with identical values. Duplicates should not get saved.
164 AutofillProfile profile0a = profile0;
165 profile0a.set_guid(base::GenerateGUID());
166 personal_data_->AddProfile(profile0a);
168 // Reload the database.
169 ResetPersonalDataManager();
171 // Verify the non-addition.
172 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
173 ASSERT_EQ(1U, results2.size());
174 EXPECT_EQ(0, profile0.Compare(*results2[0]));
176 // New profile with different email.
177 AutofillProfile profile1 = profile0;
178 profile1.set_guid(base::GenerateGUID());
179 profile1.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("john@smith.com"));
181 // Add the different profile. This should save as a separate profile.
182 // Note that if this same profile was "merged" it would collapse to one
183 // profile with a multi-valued entry for email.
184 personal_data_->AddProfile(profile1);
186 // Reload the database.
187 ResetPersonalDataManager();
189 // Verify the addition.
190 const std::vector<AutofillProfile*>& results3 = personal_data_->GetProfiles();
191 ASSERT_EQ(2U, results3.size());
192 EXPECT_EQ(0, profile0.Compare(*results3[0]));
193 EXPECT_EQ(0, profile1.Compare(*results3[1]));
196 TEST_F(PersonalDataManagerTest, AddUpdateRemoveProfiles) {
197 AutofillProfile profile0(base::GenerateGUID(), "https://www.example.com");
198 test::SetProfileInfo(&profile0,
199 "Marion", "Mitchell", "Morrison",
200 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
201 "91601", "US", "12345678910");
203 AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com");
204 test::SetProfileInfo(&profile1,
205 "Josephine", "Alicia", "Saenz",
206 "joewayne@me.xyz", "Fox", "903 Apple Ct.", NULL, "Orlando", "FL", "32801",
207 "US", "19482937549");
209 AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com");
210 test::SetProfileInfo(&profile2,
211 "Josephine", "Alicia", "Saenz",
212 "joewayne@me.xyz", "Fox", "1212 Center.", "Bld. 5", "Orlando", "FL",
213 "32801", "US", "19482937549");
215 // Add two test profiles to the database.
216 personal_data_->AddProfile(profile0);
217 personal_data_->AddProfile(profile1);
219 // Verify that the web database has been updated and the notification sent.
220 EXPECT_CALL(personal_data_observer_,
221 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
222 base::MessageLoop::current()->Run();
224 const std::vector<AutofillProfile*>& results1 = personal_data_->GetProfiles();
225 ASSERT_EQ(2U, results1.size());
226 EXPECT_EQ(0, profile0.Compare(*results1[0]));
227 EXPECT_EQ(0, profile1.Compare(*results1[1]));
229 // Update, remove, and add.
230 profile0.SetRawInfo(NAME_FIRST, ASCIIToUTF16("John"));
231 personal_data_->UpdateProfile(profile0);
232 personal_data_->RemoveByGUID(profile1.guid());
233 personal_data_->AddProfile(profile2);
235 // Verify that the web database has been updated and the notification sent.
236 EXPECT_CALL(personal_data_observer_,
237 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
238 base::MessageLoop::current()->Run();
240 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
241 ASSERT_EQ(2U, results2.size());
242 EXPECT_EQ(0, profile0.Compare(*results2[0]));
243 EXPECT_EQ(0, profile2.Compare(*results2[1]));
245 // Reset the PersonalDataManager. This tests that the personal data was saved
246 // to the web database, and that we can load the profiles from the web
247 // database.
248 ResetPersonalDataManager();
250 // Verify that we've loaded the profiles from the web database.
251 const std::vector<AutofillProfile*>& results3 = personal_data_->GetProfiles();
252 ASSERT_EQ(2U, results3.size());
253 EXPECT_EQ(0, profile0.Compare(*results3[0]));
254 EXPECT_EQ(0, profile2.Compare(*results3[1]));
257 TEST_F(PersonalDataManagerTest, AddUpdateRemoveCreditCards) {
258 CreditCard credit_card0(base::GenerateGUID(), "https://www.example.com");
259 test::SetCreditCardInfo(&credit_card0,
260 "John Dillinger", "423456789012" /* Visa */, "01", "2010");
262 CreditCard credit_card1(base::GenerateGUID(), "https://www.example.com");
263 test::SetCreditCardInfo(&credit_card1,
264 "Bonnie Parker", "518765432109" /* Mastercard */, "12", "2012");
266 CreditCard credit_card2(base::GenerateGUID(), "https://www.example.com");
267 test::SetCreditCardInfo(&credit_card2,
268 "Clyde Barrow", "347666888555" /* American Express */, "04", "2015");
270 // Add two test credit cards to the database.
271 personal_data_->AddCreditCard(credit_card0);
272 personal_data_->AddCreditCard(credit_card1);
274 // Verify that the web database has been updated and the notification sent.
275 EXPECT_CALL(personal_data_observer_,
276 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
277 base::MessageLoop::current()->Run();
279 const std::vector<CreditCard*>& results1 = personal_data_->GetCreditCards();
280 ASSERT_EQ(2U, results1.size());
281 EXPECT_EQ(0, credit_card0.Compare(*results1[0]));
282 EXPECT_EQ(0, credit_card1.Compare(*results1[1]));
284 // Update, remove, and add.
285 credit_card0.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Joe"));
286 personal_data_->UpdateCreditCard(credit_card0);
287 personal_data_->RemoveByGUID(credit_card1.guid());
288 personal_data_->AddCreditCard(credit_card2);
290 // Verify that the web database has been updated and the notification sent.
291 EXPECT_CALL(personal_data_observer_,
292 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
293 base::MessageLoop::current()->Run();
295 const std::vector<CreditCard*>& results2 = personal_data_->GetCreditCards();
296 ASSERT_EQ(2U, results2.size());
297 EXPECT_EQ(credit_card0, *results2[0]);
298 EXPECT_EQ(credit_card2, *results2[1]);
300 // Reset the PersonalDataManager. This tests that the personal data was saved
301 // to the web database, and that we can load the credit cards from the web
302 // database.
303 ResetPersonalDataManager();
305 // Verify that we've loaded the credit cards from the web database.
306 const std::vector<CreditCard*>& results3 = personal_data_->GetCreditCards();
307 ASSERT_EQ(2U, results3.size());
308 EXPECT_EQ(credit_card0, *results3[0]);
309 EXPECT_EQ(credit_card2, *results3[1]);
312 TEST_F(PersonalDataManagerTest, UpdateUnverifiedProfilesAndCreditCards) {
313 // Start with unverified data.
314 AutofillProfile profile(base::GenerateGUID(), "https://www.example.com/");
315 test::SetProfileInfo(&profile,
316 "Marion", "Mitchell", "Morrison",
317 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
318 "91601", "US", "12345678910");
319 EXPECT_FALSE(profile.IsVerified());
321 CreditCard credit_card(base::GenerateGUID(), "https://www.example.com/");
322 test::SetCreditCardInfo(&credit_card,
323 "John Dillinger", "423456789012" /* Visa */, "01", "2010");
324 EXPECT_FALSE(credit_card.IsVerified());
326 // Add the data to the database.
327 personal_data_->AddProfile(profile);
328 personal_data_->AddCreditCard(credit_card);
330 // Verify that the web database has been updated and the notification sent.
331 EXPECT_CALL(personal_data_observer_,
332 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
333 base::MessageLoop::current()->Run();
335 const std::vector<AutofillProfile*>& profiles1 =
336 personal_data_->GetProfiles();
337 const std::vector<CreditCard*>& cards1 = personal_data_->GetCreditCards();
338 ASSERT_EQ(1U, profiles1.size());
339 ASSERT_EQ(1U, cards1.size());
340 EXPECT_EQ(0, profile.Compare(*profiles1[0]));
341 EXPECT_EQ(0, credit_card.Compare(*cards1[0]));
343 // Try to update with just the origin changed.
344 AutofillProfile original_profile(profile);
345 CreditCard original_credit_card(credit_card);
346 profile.set_origin("Chrome settings");
347 credit_card.set_origin("Chrome settings");
349 EXPECT_TRUE(profile.IsVerified());
350 EXPECT_TRUE(credit_card.IsVerified());
352 personal_data_->UpdateProfile(profile);
353 personal_data_->UpdateCreditCard(credit_card);
355 // Note: No refresh, as no update is expected.
357 const std::vector<AutofillProfile*>& profiles2 =
358 personal_data_->GetProfiles();
359 const std::vector<CreditCard*>& cards2 = personal_data_->GetCreditCards();
360 ASSERT_EQ(1U, profiles2.size());
361 ASSERT_EQ(1U, cards2.size());
362 EXPECT_NE(profile.origin(), profiles2[0]->origin());
363 EXPECT_NE(credit_card.origin(), cards2[0]->origin());
364 EXPECT_EQ(original_profile.origin(), profiles2[0]->origin());
365 EXPECT_EQ(original_credit_card.origin(), cards2[0]->origin());
367 // Try to update with data changed as well.
368 profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("John"));
369 credit_card.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Joe"));
371 personal_data_->UpdateProfile(profile);
372 personal_data_->UpdateCreditCard(credit_card);
374 // Verify that the web database has been updated and the notification sent.
375 EXPECT_CALL(personal_data_observer_,
376 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
377 base::MessageLoop::current()->Run();
379 const std::vector<AutofillProfile*>& profiles3 =
380 personal_data_->GetProfiles();
381 const std::vector<CreditCard*>& cards3 = personal_data_->GetCreditCards();
382 ASSERT_EQ(1U, profiles3.size());
383 ASSERT_EQ(1U, cards3.size());
384 EXPECT_EQ(0, profile.Compare(*profiles3[0]));
385 EXPECT_EQ(0, credit_card.Compare(*cards3[0]));
386 EXPECT_EQ(profile.origin(), profiles3[0]->origin());
387 EXPECT_EQ(credit_card.origin(), cards3[0]->origin());
390 TEST_F(PersonalDataManagerTest, AddProfilesAndCreditCards) {
391 AutofillProfile profile0(base::GenerateGUID(), "https://www.example.com");
392 test::SetProfileInfo(&profile0,
393 "Marion", "Mitchell", "Morrison",
394 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
395 "91601", "US", "12345678910");
397 AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com");
398 test::SetProfileInfo(&profile1,
399 "Josephine", "Alicia", "Saenz",
400 "joewayne@me.xyz", "Fox", "903 Apple Ct.", NULL, "Orlando", "FL", "32801",
401 "US", "19482937549");
403 CreditCard credit_card0(base::GenerateGUID(), "https://www.example.com");
404 test::SetCreditCardInfo(&credit_card0,
405 "John Dillinger", "423456789012" /* Visa */, "01", "2010");
407 CreditCard credit_card1(base::GenerateGUID(), "https://www.example.com");
408 test::SetCreditCardInfo(&credit_card1,
409 "Bonnie Parker", "518765432109" /* Mastercard */, "12", "2012");
411 // Add two test profiles to the database.
412 personal_data_->AddProfile(profile0);
413 personal_data_->AddProfile(profile1);
415 // Verify that the web database has been updated and the notification sent.
416 EXPECT_CALL(personal_data_observer_,
417 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
418 base::MessageLoop::current()->Run();
420 const std::vector<AutofillProfile*>& results1 = personal_data_->GetProfiles();
421 ASSERT_EQ(2U, results1.size());
422 EXPECT_EQ(0, profile0.Compare(*results1[0]));
423 EXPECT_EQ(0, profile1.Compare(*results1[1]));
425 // Add two test credit cards to the database.
426 personal_data_->AddCreditCard(credit_card0);
427 personal_data_->AddCreditCard(credit_card1);
429 // Verify that the web database has been updated and the notification sent.
430 EXPECT_CALL(personal_data_observer_,
431 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
432 base::MessageLoop::current()->Run();
434 const std::vector<CreditCard*>& results2 = personal_data_->GetCreditCards();
435 ASSERT_EQ(2U, results2.size());
436 EXPECT_EQ(credit_card0, *results2[0]);
437 EXPECT_EQ(credit_card1, *results2[1]);
439 // Determine uniqueness by inserting all of the GUIDs into a set and verifying
440 // the size of the set matches the number of GUIDs.
441 std::set<std::string> guids;
442 guids.insert(profile0.guid());
443 guids.insert(profile1.guid());
444 guids.insert(credit_card0.guid());
445 guids.insert(credit_card1.guid());
446 EXPECT_EQ(4U, guids.size());
449 // Test for http://crbug.com/50047. Makes sure that guids are populated
450 // correctly on load.
451 TEST_F(PersonalDataManagerTest, PopulateUniqueIDsOnLoad) {
452 AutofillProfile profile0(base::GenerateGUID(), "https://www.example.com");
453 test::SetProfileInfo(&profile0,
454 "y", "", "", "", "", "", "", "", "", "", "", "");
456 // Add the profile0 to the db.
457 personal_data_->AddProfile(profile0);
459 // Verify that the web database has been updated and the notification sent.
460 EXPECT_CALL(personal_data_observer_,
461 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
462 base::MessageLoop::current()->Run();
464 // Verify that we've loaded the profiles from the web database.
465 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
466 ASSERT_EQ(1U, results2.size());
467 EXPECT_EQ(0, profile0.Compare(*results2[0]));
469 // Add a new profile.
470 AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com");
471 test::SetProfileInfo(&profile1,
472 "z", "", "", "", "", "", "", "", "", "", "", "");
473 personal_data_->AddProfile(profile1);
475 // Verify that the web database has been updated and the notification sent.
476 EXPECT_CALL(personal_data_observer_,
477 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
478 base::MessageLoop::current()->Run();
480 // Make sure the two profiles have different GUIDs, both valid.
481 const std::vector<AutofillProfile*>& results3 = personal_data_->GetProfiles();
482 ASSERT_EQ(2U, results3.size());
483 EXPECT_NE(results3[0]->guid(), results3[1]->guid());
484 EXPECT_TRUE(base::IsValidGUID(results3[0]->guid()));
485 EXPECT_TRUE(base::IsValidGUID(results3[1]->guid()));
488 TEST_F(PersonalDataManagerTest, SetEmptyProfile) {
489 AutofillProfile profile0(base::GenerateGUID(), "https://www.example.com");
490 test::SetProfileInfo(&profile0,
491 "", "", "", "", "", "", "", "", "", "", "", "");
493 // Add the empty profile to the database.
494 personal_data_->AddProfile(profile0);
496 // Note: no refresh here.
498 // Reset the PersonalDataManager. This tests that the personal data was saved
499 // to the web database, and that we can load the profiles from the web
500 // database.
501 ResetPersonalDataManager();
503 // Verify that we've loaded the profiles from the web database.
504 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
505 ASSERT_EQ(0U, results2.size());
508 TEST_F(PersonalDataManagerTest, SetEmptyCreditCard) {
509 CreditCard credit_card0(base::GenerateGUID(), "https://www.example.com");
510 test::SetCreditCardInfo(&credit_card0, "", "", "", "");
512 // Add the empty credit card to the database.
513 personal_data_->AddCreditCard(credit_card0);
515 // Note: no refresh here.
517 // Reset the PersonalDataManager. This tests that the personal data was saved
518 // to the web database, and that we can load the credit cards from the web
519 // database.
520 ResetPersonalDataManager();
522 // Verify that we've loaded the credit cards from the web database.
523 const std::vector<CreditCard*>& results2 = personal_data_->GetCreditCards();
524 ASSERT_EQ(0U, results2.size());
527 TEST_F(PersonalDataManagerTest, Refresh) {
528 AutofillProfile profile0(base::GenerateGUID(), "https://www.example.com");
529 test::SetProfileInfo(&profile0,
530 "Marion", "Mitchell", "Morrison",
531 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
532 "91601", "US", "12345678910");
534 AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com");
535 test::SetProfileInfo(&profile1,
536 "Josephine", "Alicia", "Saenz",
537 "joewayne@me.xyz", "Fox", "903 Apple Ct.", NULL, "Orlando", "FL", "32801",
538 "US", "19482937549");
540 // Add the test profiles to the database.
541 personal_data_->AddProfile(profile0);
542 personal_data_->AddProfile(profile1);
544 // Verify that the web database has been updated and the notification sent.
545 EXPECT_CALL(personal_data_observer_,
546 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
547 base::MessageLoop::current()->Run();
549 const std::vector<AutofillProfile*>& results1 = personal_data_->GetProfiles();
550 ASSERT_EQ(2U, results1.size());
551 EXPECT_EQ(profile0, *results1[0]);
552 EXPECT_EQ(profile1, *results1[1]);
554 AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com");
555 test::SetProfileInfo(&profile2,
556 "Josephine", "Alicia", "Saenz",
557 "joewayne@me.xyz", "Fox", "1212 Center.", "Bld. 5", "Orlando", "FL",
558 "32801", "US", "19482937549");
560 autofill_database_service_->AddAutofillProfile(profile2);
562 personal_data_->Refresh();
564 // Verify that the web database has been updated and the notification sent.
565 EXPECT_CALL(personal_data_observer_,
566 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
567 base::MessageLoop::current()->Run();
569 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
570 ASSERT_EQ(3U, results2.size());
571 EXPECT_EQ(profile0, *results2[0]);
572 EXPECT_EQ(profile1, *results2[1]);
573 EXPECT_EQ(profile2, *results2[2]);
575 autofill_database_service_->RemoveAutofillProfile(profile1.guid());
576 autofill_database_service_->RemoveAutofillProfile(profile2.guid());
578 // Before telling the PDM to refresh, simulate an edit to one of the deleted
579 // profiles via a SetProfile update (this would happen if the Autofill window
580 // was open with a previous snapshot of the profiles, and something
581 // [e.g. sync] removed a profile from the browser. In this edge case, we will
582 // end up in a consistent state by dropping the write).
583 profile0.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Mar"));
584 profile2.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Jo"));
585 personal_data_->UpdateProfile(profile0);
586 personal_data_->AddProfile(profile1);
587 personal_data_->AddProfile(profile2);
589 // Verify that the web database has been updated and the notification sent.
590 EXPECT_CALL(personal_data_observer_,
591 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
592 base::MessageLoop::current()->Run();
594 const std::vector<AutofillProfile*>& results3 = personal_data_->GetProfiles();
595 ASSERT_EQ(1U, results3.size());
596 EXPECT_EQ(profile0, *results2[0]);
599 TEST_F(PersonalDataManagerTest, ImportFormData) {
600 FormData form;
601 FormFieldData field;
602 test::CreateTestFormField(
603 "First name:", "first_name", "George", "text", &field);
604 form.fields.push_back(field);
605 test::CreateTestFormField(
606 "Last name:", "last_name", "Washington", "text", &field);
607 form.fields.push_back(field);
608 test::CreateTestFormField(
609 "Email:", "email", "theprez@gmail.com", "text", &field);
610 form.fields.push_back(field);
611 test::CreateTestFormField(
612 "Address:", "address1", "21 Laussat St", "text", &field);
613 form.fields.push_back(field);
614 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
615 form.fields.push_back(field);
616 test::CreateTestFormField("State:", "state", "California", "text", &field);
617 form.fields.push_back(field);
618 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
619 form.fields.push_back(field);
620 FormStructure form_structure(form);
621 form_structure.DetermineHeuristicTypes(TestAutofillMetrics());
622 scoped_ptr<CreditCard> imported_credit_card;
623 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
624 &imported_credit_card));
625 ASSERT_FALSE(imported_credit_card);
627 // Verify that the web database has been updated and the notification sent.
628 EXPECT_CALL(personal_data_observer_,
629 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
630 base::MessageLoop::current()->Run();
632 AutofillProfile expected(base::GenerateGUID(), "https://www.example.com");
633 test::SetProfileInfo(&expected, "George", NULL,
634 "Washington", "theprez@gmail.com", NULL, "21 Laussat St", NULL,
635 "San Francisco", "California", "94102", NULL, NULL);
636 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
637 ASSERT_EQ(1U, results.size());
638 EXPECT_EQ(0, expected.Compare(*results[0]));
641 TEST_F(PersonalDataManagerTest, ImportFormDataBadEmail) {
642 FormData form;
643 FormFieldData field;
644 test::CreateTestFormField(
645 "First name:", "first_name", "George", "text", &field);
646 form.fields.push_back(field);
647 test::CreateTestFormField(
648 "Last name:", "last_name", "Washington", "text", &field);
649 form.fields.push_back(field);
650 test::CreateTestFormField("Email:", "email", "bogus", "text", &field);
651 form.fields.push_back(field);
652 test::CreateTestFormField(
653 "Address:", "address1", "21 Laussat St", "text", &field);
654 form.fields.push_back(field);
655 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
656 form.fields.push_back(field);
657 test::CreateTestFormField("State:", "state", "California", "text", &field);
658 form.fields.push_back(field);
659 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
660 form.fields.push_back(field);
661 FormStructure form_structure(form);
662 form_structure.DetermineHeuristicTypes(TestAutofillMetrics());
663 scoped_ptr<CreditCard> imported_credit_card;
664 EXPECT_FALSE(personal_data_->ImportFormData(form_structure,
665 &imported_credit_card));
666 ASSERT_EQ(static_cast<CreditCard*>(NULL), imported_credit_card.get());
668 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
669 ASSERT_EQ(0U, results.size());
672 // Tests that a 'confirm email' field does not block profile import.
673 TEST_F(PersonalDataManagerTest, ImportFormDataTwoEmails) {
674 FormData form;
675 FormFieldData field;
676 test::CreateTestFormField(
677 "Name:", "name", "George Washington", "text", &field);
678 form.fields.push_back(field);
679 test::CreateTestFormField(
680 "Address:", "address1", "21 Laussat St", "text", &field);
681 form.fields.push_back(field);
682 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
683 form.fields.push_back(field);
684 test::CreateTestFormField("State:", "state", "California", "text", &field);
685 form.fields.push_back(field);
686 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
687 form.fields.push_back(field);
688 test::CreateTestFormField(
689 "Email:", "email", "example@example.com", "text", &field);
690 form.fields.push_back(field);
691 test::CreateTestFormField(
692 "Confirm email:", "confirm_email", "example@example.com", "text", &field);
693 form.fields.push_back(field);
694 FormStructure form_structure(form);
695 form_structure.DetermineHeuristicTypes(TestAutofillMetrics());
696 scoped_ptr<CreditCard> imported_credit_card;
697 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
698 &imported_credit_card));
699 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
700 ASSERT_EQ(1U, results.size());
703 // Tests two email fields containing different values blocks provile import.
704 TEST_F(PersonalDataManagerTest, ImportFormDataTwoDifferentEmails) {
705 FormData form;
706 FormFieldData field;
707 test::CreateTestFormField(
708 "Name:", "name", "George Washington", "text", &field);
709 form.fields.push_back(field);
710 test::CreateTestFormField(
711 "Address:", "address1", "21 Laussat St", "text", &field);
712 form.fields.push_back(field);
713 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
714 form.fields.push_back(field);
715 test::CreateTestFormField("State:", "state", "California", "text", &field);
716 form.fields.push_back(field);
717 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
718 form.fields.push_back(field);
719 test::CreateTestFormField(
720 "Email:", "email", "example@example.com", "text", &field);
721 form.fields.push_back(field);
722 test::CreateTestFormField(
723 "Email:", "email2", "example2@example.com", "text", &field);
724 form.fields.push_back(field);
725 FormStructure form_structure(form);
726 form_structure.DetermineHeuristicTypes(TestAutofillMetrics());
727 scoped_ptr<CreditCard> imported_credit_card;
728 EXPECT_FALSE(personal_data_->ImportFormData(form_structure,
729 &imported_credit_card));
730 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
731 ASSERT_EQ(0U, results.size());
734 TEST_F(PersonalDataManagerTest, ImportFormDataNotEnoughFilledFields) {
735 FormData form;
736 FormFieldData field;
737 test::CreateTestFormField(
738 "First name:", "first_name", "George", "text", &field);
739 form.fields.push_back(field);
740 test::CreateTestFormField(
741 "Last name:", "last_name", "Washington", "text", &field);
742 form.fields.push_back(field);
743 test::CreateTestFormField(
744 "Card number:", "card_number", "4111 1111 1111 1111", "text", &field);
745 form.fields.push_back(field);
746 FormStructure form_structure(form);
747 form_structure.DetermineHeuristicTypes(TestAutofillMetrics());
748 scoped_ptr<CreditCard> imported_credit_card;
749 EXPECT_FALSE(personal_data_->ImportFormData(form_structure,
750 &imported_credit_card));
751 ASSERT_FALSE(imported_credit_card);
753 const std::vector<AutofillProfile*>& profiles = personal_data_->GetProfiles();
754 ASSERT_EQ(0U, profiles.size());
755 const std::vector<CreditCard*>& cards = personal_data_->GetCreditCards();
756 ASSERT_EQ(0U, cards.size());
759 TEST_F(PersonalDataManagerTest, ImportFormMinimumAddressUSA) {
760 // United States addresses must specifiy one address line, a city, state and
761 // zip code.
762 FormData form;
763 FormFieldData field;
764 test::CreateTestFormField("Name:", "name", "Barack Obama", "text", &field);
765 form.fields.push_back(field);
766 test::CreateTestFormField(
767 "Address:", "address", "1600 Pennsylvania Avenue", "text", &field);
768 form.fields.push_back(field);
769 test::CreateTestFormField("City:", "city", "Washington", "text", &field);
770 form.fields.push_back(field);
771 test::CreateTestFormField("State:", "state", "DC", "text", &field);
772 form.fields.push_back(field);
773 test::CreateTestFormField("Zip:", "zip", "20500", "text", &field);
774 form.fields.push_back(field);
775 test::CreateTestFormField("Country:", "country", "USA", "text", &field);
776 form.fields.push_back(field);
777 FormStructure form_structure(form);
778 form_structure.DetermineHeuristicTypes(TestAutofillMetrics());
779 scoped_ptr<CreditCard> imported_credit_card;
780 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
781 &imported_credit_card));
782 const std::vector<AutofillProfile*>& profiles = personal_data_->GetProfiles();
783 ASSERT_EQ(1U, profiles.size());
786 TEST_F(PersonalDataManagerTest, ImportFormMinimumAddressGB) {
787 // British addresses do not require a state/province as the county is usually
788 // not requested on forms.
789 FormData form;
790 FormFieldData field;
791 test::CreateTestFormField("Name:", "name", "David Cameron", "text", &field);
792 form.fields.push_back(field);
793 test::CreateTestFormField(
794 "Address:", "address", "10 Downing Street", "text", &field);
795 form.fields.push_back(field);
796 test::CreateTestFormField("City:", "city", "London", "text", &field);
797 form.fields.push_back(field);
798 test::CreateTestFormField(
799 "Postcode:", "postcode", "SW1A 2AA", "text", &field);
800 form.fields.push_back(field);
801 test::CreateTestFormField(
802 "Country:", "country", "United Kingdom", "text", &field);
803 form.fields.push_back(field);
804 FormStructure form_structure(form);
805 form_structure.DetermineHeuristicTypes(TestAutofillMetrics());
806 scoped_ptr<CreditCard> imported_credit_card;
807 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
808 &imported_credit_card));
809 const std::vector<AutofillProfile*>& profiles = personal_data_->GetProfiles();
810 ASSERT_EQ(1U, profiles.size());
813 TEST_F(PersonalDataManagerTest, ImportFormMinimumAddressGI) {
814 // Gibraltar has the most minimal set of requirements for a valid address.
815 // There are no cities or provinces and no postal/zip code system.
816 FormData form;
817 FormFieldData field;
818 test::CreateTestFormField(
819 "Name:", "name", "Sir Adrian Johns", "text", &field);
820 form.fields.push_back(field);
821 test::CreateTestFormField(
822 "Address:", "address", "The Convent, Main Street", "text", &field);
823 form.fields.push_back(field);
824 test::CreateTestFormField("Country:", "country", "Gibraltar", "text", &field);
825 form.fields.push_back(field);
826 FormStructure form_structure(form);
827 form_structure.DetermineHeuristicTypes(TestAutofillMetrics());
828 scoped_ptr<CreditCard> imported_credit_card;
829 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
830 &imported_credit_card));
831 const std::vector<AutofillProfile*>& profiles = personal_data_->GetProfiles();
832 ASSERT_EQ(1U, profiles.size());
835 TEST_F(PersonalDataManagerTest, ImportPhoneNumberSplitAcrossMultipleFields) {
836 FormData form;
837 FormFieldData field;
838 test::CreateTestFormField(
839 "First name:", "first_name", "George", "text", &field);
840 form.fields.push_back(field);
841 test::CreateTestFormField(
842 "Last name:", "last_name", "Washington", "text", &field);
843 form.fields.push_back(field);
844 test::CreateTestFormField(
845 "Phone #:", "home_phone_area_code", "650", "text", &field);
846 field.max_length = 3;
847 form.fields.push_back(field);
848 test::CreateTestFormField(
849 "Phone #:", "home_phone_prefix", "555", "text", &field);
850 field.max_length = 3;
851 form.fields.push_back(field);
852 test::CreateTestFormField(
853 "Phone #:", "home_phone_suffix", "0000", "text", &field);
854 field.max_length = 4;
855 form.fields.push_back(field);
856 test::CreateTestFormField(
857 "Address:", "address1", "21 Laussat St", "text", &field);
858 form.fields.push_back(field);
859 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
860 form.fields.push_back(field);
861 test::CreateTestFormField("State:", "state", "California", "text", &field);
862 form.fields.push_back(field);
863 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
864 form.fields.push_back(field);
865 FormStructure form_structure(form);
866 form_structure.DetermineHeuristicTypes(TestAutofillMetrics());
867 scoped_ptr<CreditCard> imported_credit_card;
868 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
869 &imported_credit_card));
870 ASSERT_FALSE(imported_credit_card);
872 // Verify that the web database has been updated and the notification sent.
873 EXPECT_CALL(personal_data_observer_,
874 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
875 base::MessageLoop::current()->Run();
877 AutofillProfile expected(base::GenerateGUID(), "https://www.example.com");
878 test::SetProfileInfo(&expected, "George", NULL,
879 "Washington", NULL, NULL, "21 Laussat St", NULL,
880 "San Francisco", "California", "94102", NULL, "(650) 555-0000");
881 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
882 ASSERT_EQ(1U, results.size());
883 EXPECT_EQ(0, expected.Compare(*results[0]));
886 TEST_F(PersonalDataManagerTest, ImportFormDataMultilineAddress) {
887 FormData form;
888 FormFieldData field;
889 test::CreateTestFormField(
890 "First name:", "first_name", "George", "text", &field);
891 form.fields.push_back(field);
892 test::CreateTestFormField(
893 "Last name:", "last_name", "Washington", "text", &field);
894 form.fields.push_back(field);
895 test::CreateTestFormField(
896 "Email:", "email", "theprez@gmail.com", "text", &field);
897 form.fields.push_back(field);
898 test::CreateTestFormField(
899 "Address:",
900 "street_address",
901 "21 Laussat St\n"
902 "Apt. #42",
903 "textarea",
904 &field);
905 form.fields.push_back(field);
906 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
907 form.fields.push_back(field);
908 test::CreateTestFormField("State:", "state", "California", "text", &field);
909 form.fields.push_back(field);
910 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
911 form.fields.push_back(field);
912 FormStructure form_structure(form);
913 form_structure.DetermineHeuristicTypes(TestAutofillMetrics());
914 scoped_ptr<CreditCard> imported_credit_card;
915 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
916 &imported_credit_card));
917 ASSERT_FALSE(imported_credit_card);
919 // Verify that the web database has been updated and the notification sent.
920 EXPECT_CALL(personal_data_observer_,
921 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
922 base::MessageLoop::current()->Run();
924 AutofillProfile expected(base::GenerateGUID(), "https://www.example.com");
925 test::SetProfileInfo(&expected, "George", NULL,
926 "Washington", "theprez@gmail.com", NULL, "21 Laussat St", "Apt. #42",
927 "San Francisco", "California", "94102", NULL, NULL);
928 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
929 ASSERT_EQ(1U, results.size());
930 EXPECT_EQ(0, expected.Compare(*results[0]));
933 TEST_F(PersonalDataManagerTest, SetUniqueCreditCardLabels) {
934 CreditCard credit_card0(base::GenerateGUID(), "https://www.example.com");
935 credit_card0.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("John"));
936 CreditCard credit_card1(base::GenerateGUID(), "https://www.example.com");
937 credit_card1.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Paul"));
938 CreditCard credit_card2(base::GenerateGUID(), "https://www.example.com");
939 credit_card2.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Ringo"));
940 CreditCard credit_card3(base::GenerateGUID(), "https://www.example.com");
941 credit_card3.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Other"));
942 CreditCard credit_card4(base::GenerateGUID(), "https://www.example.com");
943 credit_card4.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Ozzy"));
944 CreditCard credit_card5(base::GenerateGUID(), "https://www.example.com");
945 credit_card5.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Dio"));
947 // Add the test credit cards to the database.
948 personal_data_->AddCreditCard(credit_card0);
949 personal_data_->AddCreditCard(credit_card1);
950 personal_data_->AddCreditCard(credit_card2);
951 personal_data_->AddCreditCard(credit_card3);
952 personal_data_->AddCreditCard(credit_card4);
953 personal_data_->AddCreditCard(credit_card5);
955 // Reset the PersonalDataManager. This tests that the personal data was saved
956 // to the web database, and that we can load the credit cards from the web
957 // database.
958 ResetPersonalDataManager();
960 const std::vector<CreditCard*>& results = personal_data_->GetCreditCards();
961 ASSERT_EQ(6U, results.size());
962 EXPECT_EQ(credit_card0.guid(), results[0]->guid());
963 EXPECT_EQ(credit_card1.guid(), results[1]->guid());
964 EXPECT_EQ(credit_card2.guid(), results[2]->guid());
965 EXPECT_EQ(credit_card3.guid(), results[3]->guid());
966 EXPECT_EQ(credit_card4.guid(), results[4]->guid());
967 EXPECT_EQ(credit_card5.guid(), results[5]->guid());
970 TEST_F(PersonalDataManagerTest, AggregateTwoDifferentProfiles) {
971 FormData form1;
972 FormFieldData field;
973 test::CreateTestFormField(
974 "First name:", "first_name", "George", "text", &field);
975 form1.fields.push_back(field);
976 test::CreateTestFormField(
977 "Last name:", "last_name", "Washington", "text", &field);
978 form1.fields.push_back(field);
979 test::CreateTestFormField(
980 "Email:", "email", "theprez@gmail.com", "text", &field);
981 form1.fields.push_back(field);
982 test::CreateTestFormField(
983 "Address:", "address1", "21 Laussat St", "text", &field);
984 form1.fields.push_back(field);
985 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
986 form1.fields.push_back(field);
987 test::CreateTestFormField("State:", "state", "California", "text", &field);
988 form1.fields.push_back(field);
989 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
990 form1.fields.push_back(field);
992 FormStructure form_structure1(form1);
993 form_structure1.DetermineHeuristicTypes(TestAutofillMetrics());
994 scoped_ptr<CreditCard> imported_credit_card;
995 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
996 &imported_credit_card));
997 ASSERT_FALSE(imported_credit_card);
999 // Verify that the web database has been updated and the notification sent.
1000 EXPECT_CALL(personal_data_observer_,
1001 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
1002 base::MessageLoop::current()->Run();
1004 AutofillProfile expected(base::GenerateGUID(), "https://www.example.com");
1005 test::SetProfileInfo(&expected, "George", NULL,
1006 "Washington", "theprez@gmail.com", NULL, "21 Laussat St", NULL,
1007 "San Francisco", "California", "94102", NULL, NULL);
1008 const std::vector<AutofillProfile*>& results1 = personal_data_->GetProfiles();
1009 ASSERT_EQ(1U, results1.size());
1010 EXPECT_EQ(0, expected.Compare(*results1[0]));
1012 // Now create a completely different profile.
1013 FormData form2;
1014 test::CreateTestFormField(
1015 "First name:", "first_name", "John", "text", &field);
1016 form2.fields.push_back(field);
1017 test::CreateTestFormField(
1018 "Last name:", "last_name", "Adams", "text", &field);
1019 form2.fields.push_back(field);
1020 test::CreateTestFormField(
1021 "Email:", "email", "second@gmail.com", "text", &field);
1022 form2.fields.push_back(field);
1023 test::CreateTestFormField(
1024 "Address:", "address1", "22 Laussat St", "text", &field);
1025 form2.fields.push_back(field);
1026 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
1027 form2.fields.push_back(field);
1028 test::CreateTestFormField("State:", "state", "California", "text", &field);
1029 form2.fields.push_back(field);
1030 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
1031 form2.fields.push_back(field);
1033 FormStructure form_structure2(form2);
1034 form_structure2.DetermineHeuristicTypes(TestAutofillMetrics());
1035 EXPECT_TRUE(personal_data_->ImportFormData(form_structure2,
1036 &imported_credit_card));
1037 ASSERT_FALSE(imported_credit_card);
1039 // Verify that the web database has been updated and the notification sent.
1040 EXPECT_CALL(personal_data_observer_,
1041 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
1042 base::MessageLoop::current()->Run();
1044 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
1046 AutofillProfile expected2(base::GenerateGUID(), "https://www.example.com");
1047 test::SetProfileInfo(&expected2, "John", NULL,
1048 "Adams", "second@gmail.com", NULL, "22 Laussat St", NULL,
1049 "San Francisco", "California", "94102", NULL, NULL);
1050 ASSERT_EQ(2U, results2.size());
1051 EXPECT_EQ(0, expected.Compare(*results2[0]));
1052 EXPECT_EQ(0, expected2.Compare(*results2[1]));
1055 TEST_F(PersonalDataManagerTest, AggregateTwoProfilesWithMultiValue) {
1056 FormData form1;
1057 FormFieldData field;
1058 test::CreateTestFormField(
1059 "First name:", "first_name", "George", "text", &field);
1060 form1.fields.push_back(field);
1061 test::CreateTestFormField(
1062 "Last name:", "last_name", "Washington", "text", &field);
1063 form1.fields.push_back(field);
1064 test::CreateTestFormField(
1065 "Email:", "email", "theprez@gmail.com", "text", &field);
1066 form1.fields.push_back(field);
1067 test::CreateTestFormField(
1068 "Address:", "address1", "21 Laussat St", "text", &field);
1069 form1.fields.push_back(field);
1070 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
1071 form1.fields.push_back(field);
1072 test::CreateTestFormField("State:", "state", "California", "text", &field);
1073 form1.fields.push_back(field);
1074 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
1075 form1.fields.push_back(field);
1077 FormStructure form_structure1(form1);
1078 form_structure1.DetermineHeuristicTypes(TestAutofillMetrics());
1079 scoped_ptr<CreditCard> imported_credit_card;
1080 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1081 &imported_credit_card));
1082 ASSERT_FALSE(imported_credit_card);
1084 // Verify that the web database has been updated and the notification sent.
1085 EXPECT_CALL(personal_data_observer_,
1086 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
1087 base::MessageLoop::current()->Run();
1089 AutofillProfile expected(base::GenerateGUID(), "https://www.example.com");
1090 test::SetProfileInfo(&expected, "George", NULL,
1091 "Washington", "theprez@gmail.com", NULL, "21 Laussat St", NULL,
1092 "San Francisco", "California", "94102", NULL, NULL);
1093 const std::vector<AutofillProfile*>& results1 = personal_data_->GetProfiles();
1094 ASSERT_EQ(1U, results1.size());
1095 EXPECT_EQ(0, expected.Compare(*results1[0]));
1097 // Now create a completely different profile.
1098 FormData form2;
1099 test::CreateTestFormField(
1100 "First name:", "first_name", "John", "text", &field);
1101 form2.fields.push_back(field);
1102 test::CreateTestFormField("Last name:", "last_name", "Adams", "text", &field);
1103 form2.fields.push_back(field);
1104 test::CreateTestFormField(
1105 "Email:", "email", "second@gmail.com", "text", &field);
1106 form2.fields.push_back(field);
1107 test::CreateTestFormField(
1108 "Address:", "address1", "21 Laussat St", "text", &field);
1109 form2.fields.push_back(field);
1110 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
1111 form2.fields.push_back(field);
1112 test::CreateTestFormField("State:", "state", "California", "text", &field);
1113 form2.fields.push_back(field);
1114 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
1115 form2.fields.push_back(field);
1117 FormStructure form_structure2(form2);
1118 form_structure2.DetermineHeuristicTypes(TestAutofillMetrics());
1119 EXPECT_TRUE(personal_data_->ImportFormData(form_structure2,
1120 &imported_credit_card));
1121 ASSERT_FALSE(imported_credit_card);
1123 // Verify that the web database has been updated and the notification sent.
1124 EXPECT_CALL(personal_data_observer_,
1125 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
1126 base::MessageLoop::current()->Run();
1128 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
1130 // Modify expected to include multi-valued fields.
1131 std::vector<base::string16> values;
1132 expected.GetRawMultiInfo(NAME_FULL, &values);
1133 values.push_back(ASCIIToUTF16("John Adams"));
1134 expected.SetRawMultiInfo(NAME_FULL, values);
1135 expected.GetRawMultiInfo(EMAIL_ADDRESS, &values);
1136 values.push_back(ASCIIToUTF16("second@gmail.com"));
1137 expected.SetRawMultiInfo(EMAIL_ADDRESS, values);
1139 ASSERT_EQ(1U, results2.size());
1140 EXPECT_EQ(0, expected.Compare(*results2[0]));
1143 TEST_F(PersonalDataManagerTest, AggregateSameProfileWithConflict) {
1144 FormData form1;
1145 FormFieldData field;
1146 test::CreateTestFormField(
1147 "First name:", "first_name", "George", "text", &field);
1148 form1.fields.push_back(field);
1149 test::CreateTestFormField(
1150 "Last name:", "last_name", "Washington", "text", &field);
1151 form1.fields.push_back(field);
1152 test::CreateTestFormField(
1153 "Address:", "address", "1600 Pennsylvania Avenue", "text", &field);
1154 form1.fields.push_back(field);
1155 test::CreateTestFormField(
1156 "Address Line 2:", "address2", "Suite A", "text", &field);
1157 form1.fields.push_back(field);
1158 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
1159 form1.fields.push_back(field);
1160 test::CreateTestFormField("State:", "state", "California", "text", &field);
1161 form1.fields.push_back(field);
1162 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
1163 form1.fields.push_back(field);
1164 test::CreateTestFormField(
1165 "Email:", "email", "theprez@gmail.com", "text", &field);
1166 form1.fields.push_back(field);
1167 test::CreateTestFormField("Phone:", "phone", "6505556666", "text", &field);
1168 form1.fields.push_back(field);
1170 FormStructure form_structure1(form1);
1171 form_structure1.DetermineHeuristicTypes(TestAutofillMetrics());
1172 scoped_ptr<CreditCard> imported_credit_card;
1173 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1174 &imported_credit_card));
1175 ASSERT_FALSE(imported_credit_card);
1177 // Verify that the web database has been updated and the notification sent.
1178 EXPECT_CALL(personal_data_observer_,
1179 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
1180 base::MessageLoop::current()->Run();
1182 AutofillProfile expected(base::GenerateGUID(), "https://www.example.com");
1183 test::SetProfileInfo(
1184 &expected, "George", NULL, "Washington", "theprez@gmail.com", NULL,
1185 "1600 Pennsylvania Avenue", "Suite A", "San Francisco", "California",
1186 "94102", NULL, "(650) 555-6666");
1187 const std::vector<AutofillProfile*>& results1 = personal_data_->GetProfiles();
1188 ASSERT_EQ(1U, results1.size());
1189 EXPECT_EQ(0, expected.Compare(*results1[0]));
1191 // Now create an updated profile.
1192 FormData form2;
1193 test::CreateTestFormField(
1194 "First name:", "first_name", "George", "text", &field);
1195 form2.fields.push_back(field);
1196 test::CreateTestFormField(
1197 "Last name:", "last_name", "Washington", "text", &field);
1198 form2.fields.push_back(field);
1199 test::CreateTestFormField(
1200 "Address:", "address", "1600 Pennsylvania Avenue", "text", &field);
1201 form2.fields.push_back(field);
1202 test::CreateTestFormField(
1203 "Address Line 2:", "address2", "Suite A", "text", &field);
1204 form2.fields.push_back(field);
1205 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
1206 form2.fields.push_back(field);
1207 test::CreateTestFormField("State:", "state", "California", "text", &field);
1208 form2.fields.push_back(field);
1209 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
1210 form2.fields.push_back(field);
1211 test::CreateTestFormField(
1212 "Email:", "email", "theprez@gmail.com", "text", &field);
1213 form2.fields.push_back(field);
1214 // Country gets added.
1215 test::CreateTestFormField("Country:", "country", "USA", "text", &field);
1216 form2.fields.push_back(field);
1217 // Phone gets updated.
1218 test::CreateTestFormField("Phone:", "phone", "6502231234", "text", &field);
1219 form2.fields.push_back(field);
1221 FormStructure form_structure2(form2);
1222 form_structure2.DetermineHeuristicTypes(TestAutofillMetrics());
1223 EXPECT_TRUE(personal_data_->ImportFormData(form_structure2,
1224 &imported_credit_card));
1225 ASSERT_FALSE(imported_credit_card);
1227 // Verify that the web database has been updated and the notification sent.
1228 EXPECT_CALL(personal_data_observer_,
1229 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
1230 base::MessageLoop::current()->Run();
1232 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
1234 // Add multi-valued phone number to expectation. Also, country gets added.
1235 std::vector<base::string16> values;
1236 expected.GetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, &values);
1237 values.push_back(ASCIIToUTF16("(650) 223-1234"));
1238 expected.SetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, values);
1239 expected.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"));
1240 ASSERT_EQ(1U, results2.size());
1241 EXPECT_EQ(0, expected.Compare(*results2[0]));
1244 TEST_F(PersonalDataManagerTest, AggregateProfileWithMissingInfoInOld) {
1245 FormData form1;
1246 FormFieldData field;
1247 test::CreateTestFormField(
1248 "First name:", "first_name", "George", "text", &field);
1249 form1.fields.push_back(field);
1250 test::CreateTestFormField(
1251 "Last name:", "last_name", "Washington", "text", &field);
1252 form1.fields.push_back(field);
1253 test::CreateTestFormField(
1254 "Address Line 1:", "address", "190 High Street", "text", &field);
1255 form1.fields.push_back(field);
1256 test::CreateTestFormField("City:", "city", "Philadelphia", "text", &field);
1257 form1.fields.push_back(field);
1258 test::CreateTestFormField("State:", "state", "Pennsylvania", "text", &field);
1259 form1.fields.push_back(field);
1260 test::CreateTestFormField("Zip:", "zipcode", "19106", "text", &field);
1261 form1.fields.push_back(field);
1263 FormStructure form_structure1(form1);
1264 form_structure1.DetermineHeuristicTypes(TestAutofillMetrics());
1265 scoped_ptr<CreditCard> imported_credit_card;
1266 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1267 &imported_credit_card));
1268 EXPECT_FALSE(imported_credit_card);
1270 // Verify that the web database has been updated and the notification sent.
1271 EXPECT_CALL(personal_data_observer_,
1272 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
1273 base::MessageLoop::current()->Run();
1275 AutofillProfile expected(base::GenerateGUID(), "https://www.example.com");
1276 test::SetProfileInfo(&expected, "George", NULL,
1277 "Washington", NULL, NULL, "190 High Street", NULL,
1278 "Philadelphia", "Pennsylvania", "19106", NULL, NULL);
1279 const std::vector<AutofillProfile*>& results1 = personal_data_->GetProfiles();
1280 ASSERT_EQ(1U, results1.size());
1281 EXPECT_EQ(0, expected.Compare(*results1[0]));
1283 // Submit a form with new data for the first profile.
1284 FormData form2;
1285 test::CreateTestFormField(
1286 "First name:", "first_name", "George", "text", &field);
1287 form2.fields.push_back(field);
1288 test::CreateTestFormField(
1289 "Last name:", "last_name", "Washington", "text", &field);
1290 form2.fields.push_back(field);
1291 test::CreateTestFormField(
1292 "Email:", "email", "theprez@gmail.com", "text", &field);
1293 form2.fields.push_back(field);
1294 test::CreateTestFormField(
1295 "Address Line 1:", "address", "190 High Street", "text", &field);
1296 form2.fields.push_back(field);
1297 test::CreateTestFormField("City:", "city", "Philadelphia", "text", &field);
1298 form2.fields.push_back(field);
1299 test::CreateTestFormField("State:", "state", "Pennsylvania", "text", &field);
1300 form2.fields.push_back(field);
1301 test::CreateTestFormField("Zip:", "zipcode", "19106", "text", &field);
1302 form2.fields.push_back(field);
1304 FormStructure form_structure2(form2);
1305 form_structure2.DetermineHeuristicTypes(TestAutofillMetrics());
1306 EXPECT_TRUE(personal_data_->ImportFormData(form_structure2,
1307 &imported_credit_card));
1308 ASSERT_FALSE(imported_credit_card);
1310 // Verify that the web database has been updated and the notification sent.
1311 EXPECT_CALL(personal_data_observer_,
1312 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
1313 base::MessageLoop::current()->Run();
1315 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
1317 AutofillProfile expected2(base::GenerateGUID(), "https://www.example.com");
1318 test::SetProfileInfo(&expected2, "George", NULL,
1319 "Washington", "theprez@gmail.com", NULL, "190 High Street", NULL,
1320 "Philadelphia", "Pennsylvania", "19106", NULL, NULL);
1321 ASSERT_EQ(1U, results2.size());
1322 EXPECT_EQ(0, expected2.Compare(*results2[0]));
1325 TEST_F(PersonalDataManagerTest, AggregateProfileWithMissingInfoInNew) {
1326 FormData form1;
1327 FormFieldData field;
1328 test::CreateTestFormField(
1329 "First name:", "first_name", "George", "text", &field);
1330 form1.fields.push_back(field);
1331 test::CreateTestFormField(
1332 "Last name:", "last_name", "Washington", "text", &field);
1333 form1.fields.push_back(field);
1334 test::CreateTestFormField(
1335 "Company:", "company", "Government", "text", &field);
1336 form1.fields.push_back(field);
1337 test::CreateTestFormField(
1338 "Email:", "email", "theprez@gmail.com", "text", &field);
1339 form1.fields.push_back(field);
1340 test::CreateTestFormField(
1341 "Address Line 1:", "address", "190 High Street", "text", &field);
1342 form1.fields.push_back(field);
1343 test::CreateTestFormField("City:", "city", "Philadelphia", "text", &field);
1344 form1.fields.push_back(field);
1345 test::CreateTestFormField("State:", "state", "Pennsylvania", "text", &field);
1346 form1.fields.push_back(field);
1347 test::CreateTestFormField("Zip:", "zipcode", "19106", "text", &field);
1348 form1.fields.push_back(field);
1350 FormStructure form_structure1(form1);
1351 form_structure1.DetermineHeuristicTypes(TestAutofillMetrics());
1352 scoped_ptr<CreditCard> imported_credit_card;
1353 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1354 &imported_credit_card));
1355 ASSERT_FALSE(imported_credit_card);
1357 // Verify that the web database has been updated and the notification sent.
1358 EXPECT_CALL(personal_data_observer_,
1359 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
1360 base::MessageLoop::current()->Run();
1362 AutofillProfile expected(base::GenerateGUID(), "https://www.example.com");
1363 test::SetProfileInfo(&expected, "George", NULL,
1364 "Washington", "theprez@gmail.com", "Government", "190 High Street", NULL,
1365 "Philadelphia", "Pennsylvania", "19106", NULL, NULL);
1366 const std::vector<AutofillProfile*>& results1 = personal_data_->GetProfiles();
1367 ASSERT_EQ(1U, results1.size());
1368 EXPECT_EQ(0, expected.Compare(*results1[0]));
1370 // Submit a form with new data for the first profile.
1371 FormData form2;
1372 test::CreateTestFormField(
1373 "First name:", "first_name", "George", "text", &field);
1374 form2.fields.push_back(field);
1375 test::CreateTestFormField(
1376 "Last name:", "last_name", "Washington", "text", &field);
1377 form2.fields.push_back(field);
1378 // Note missing Company field.
1379 test::CreateTestFormField(
1380 "Email:", "email", "theprez@gmail.com", "text", &field);
1381 form2.fields.push_back(field);
1382 test::CreateTestFormField(
1383 "Address Line 1:", "address", "190 High Street", "text", &field);
1384 form2.fields.push_back(field);
1385 test::CreateTestFormField("City:", "city", "Philadelphia", "text", &field);
1386 form2.fields.push_back(field);
1387 test::CreateTestFormField("State:", "state", "Pennsylvania", "text", &field);
1388 form2.fields.push_back(field);
1389 test::CreateTestFormField("Zip:", "zipcode", "19106", "text", &field);
1390 form2.fields.push_back(field);
1392 FormStructure form_structure2(form2);
1393 form_structure2.DetermineHeuristicTypes(TestAutofillMetrics());
1394 EXPECT_TRUE(personal_data_->ImportFormData(form_structure2,
1395 &imported_credit_card));
1396 ASSERT_FALSE(imported_credit_card);
1398 // Verify that the web database has been updated and the notification sent.
1399 EXPECT_CALL(personal_data_observer_,
1400 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
1401 base::MessageLoop::current()->Run();
1403 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
1405 // Expect no change.
1406 ASSERT_EQ(1U, results2.size());
1407 EXPECT_EQ(0, expected.Compare(*results2[0]));
1410 TEST_F(PersonalDataManagerTest, AggregateProfileWithInsufficientAddress) {
1411 FormData form1;
1412 FormFieldData field;
1413 test::CreateTestFormField(
1414 "First name:", "first_name", "George", "text", &field);
1415 form1.fields.push_back(field);
1416 test::CreateTestFormField(
1417 "Last name:", "last_name", "Washington", "text", &field);
1418 form1.fields.push_back(field);
1419 test::CreateTestFormField(
1420 "Company:", "company", "Government", "text", &field);
1421 form1.fields.push_back(field);
1422 test::CreateTestFormField(
1423 "Email:", "email", "theprez@gmail.com", "text", &field);
1424 form1.fields.push_back(field);
1425 test::CreateTestFormField(
1426 "Address Line 1:", "address", "190 High Street", "text", &field);
1427 form1.fields.push_back(field);
1428 test::CreateTestFormField("City:", "city", "Philadelphia", "text", &field);
1429 form1.fields.push_back(field);
1431 FormStructure form_structure1(form1);
1432 form_structure1.DetermineHeuristicTypes(TestAutofillMetrics());
1433 scoped_ptr<CreditCard> imported_credit_card;
1434 EXPECT_FALSE(personal_data_->ImportFormData(form_structure1,
1435 &imported_credit_card));
1436 ASSERT_FALSE(imported_credit_card);
1438 // Since no refresh is expected, reload the data from the database to make
1439 // sure no changes were written out.
1440 ResetPersonalDataManager();
1442 const std::vector<AutofillProfile*>& profiles = personal_data_->GetProfiles();
1443 ASSERT_EQ(0U, profiles.size());
1444 const std::vector<CreditCard*>& cards = personal_data_->GetCreditCards();
1445 ASSERT_EQ(0U, cards.size());
1448 TEST_F(PersonalDataManagerTest, AggregateExistingAuxiliaryProfile) {
1449 // Simulate having access to an auxiliary profile.
1450 // |auxiliary_profile| will be owned by |personal_data_|.
1451 AutofillProfile* auxiliary_profile =
1452 new AutofillProfile(base::GenerateGUID(), "https://www.example.com");
1453 test::SetProfileInfo(auxiliary_profile,
1454 "Tester", "Frederick", "McAddressBookTesterson",
1455 "tester@example.com", "Acme Inc.", "1 Main", "Apt A", "San Francisco",
1456 "CA", "94102", "US", "1.415.888.9999");
1457 ScopedVector<AutofillProfile>& auxiliary_profiles =
1458 personal_data_->auxiliary_profiles_;
1459 auxiliary_profiles.push_back(auxiliary_profile);
1461 // Simulate a form submission with a subset of the info.
1462 // Note that the phone number format is different from the saved format.
1463 FormData form;
1464 FormFieldData field;
1465 test::CreateTestFormField(
1466 "First name:", "first_name", "Tester", "text", &field);
1467 form.fields.push_back(field);
1468 test::CreateTestFormField(
1469 "Last name:", "last_name", "McAddressBookTesterson", "text", &field);
1470 form.fields.push_back(field);
1471 test::CreateTestFormField(
1472 "Email:", "email", "tester@example.com", "text", &field);
1473 form.fields.push_back(field);
1474 test::CreateTestFormField("Address:", "address1", "1 Main", "text", &field);
1475 form.fields.push_back(field);
1476 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
1477 form.fields.push_back(field);
1478 test::CreateTestFormField("State:", "state", "CA", "text", &field);
1479 form.fields.push_back(field);
1480 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
1481 form.fields.push_back(field);
1482 test::CreateTestFormField("Phone:", "phone", "4158889999", "text", &field);
1483 form.fields.push_back(field);
1485 FormStructure form_structure(form);
1486 form_structure.DetermineHeuristicTypes(TestAutofillMetrics());
1487 scoped_ptr<CreditCard> imported_credit_card;
1488 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
1489 &imported_credit_card));
1490 EXPECT_FALSE(imported_credit_card);
1492 // Note: No refresh.
1494 // Expect no change.
1495 const std::vector<AutofillProfile*>& web_profiles =
1496 personal_data_->web_profiles();
1497 EXPECT_EQ(0U, web_profiles.size());
1498 ASSERT_EQ(1U, auxiliary_profiles.size());
1499 EXPECT_EQ(0, auxiliary_profile->Compare(*auxiliary_profiles[0]));
1502 TEST_F(PersonalDataManagerTest, AggregateTwoDifferentCreditCards) {
1503 FormData form1;
1505 // Start with a single valid credit card form.
1506 FormFieldData field;
1507 test::CreateTestFormField(
1508 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1509 form1.fields.push_back(field);
1510 test::CreateTestFormField(
1511 "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
1512 form1.fields.push_back(field);
1513 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
1514 form1.fields.push_back(field);
1515 test::CreateTestFormField("Exp Year:", "exp_year", "2011", "text", &field);
1516 form1.fields.push_back(field);
1518 FormStructure form_structure1(form1);
1519 form_structure1.DetermineHeuristicTypes(TestAutofillMetrics());
1520 scoped_ptr<CreditCard> imported_credit_card;
1521 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1522 &imported_credit_card));
1523 ASSERT_TRUE(imported_credit_card);
1524 personal_data_->SaveImportedCreditCard(*imported_credit_card);
1526 // Verify that the web database has been updated and the notification sent.
1527 EXPECT_CALL(personal_data_observer_,
1528 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
1529 base::MessageLoop::current()->Run();
1531 CreditCard expected(base::GenerateGUID(), "https://www.example.com");
1532 test::SetCreditCardInfo(&expected,
1533 "Biggie Smalls", "4111111111111111", "01", "2011");
1534 const std::vector<CreditCard*>& results = personal_data_->GetCreditCards();
1535 ASSERT_EQ(1U, results.size());
1536 EXPECT_EQ(0, expected.Compare(*results[0]));
1538 // Add a second different valid credit card.
1539 FormData form2;
1540 test::CreateTestFormField(
1541 "Name on card:", "name_on_card", "", "text", &field);
1542 form2.fields.push_back(field);
1543 test::CreateTestFormField(
1544 "Card Number:", "card_number", "5500 0000 0000 0004", "text", &field);
1545 form2.fields.push_back(field);
1546 test::CreateTestFormField("Exp Month:", "exp_month", "02", "text", &field);
1547 form2.fields.push_back(field);
1548 test::CreateTestFormField("Exp Year:", "exp_year", "2012", "text", &field);
1549 form2.fields.push_back(field);
1551 FormStructure form_structure2(form2);
1552 form_structure2.DetermineHeuristicTypes(TestAutofillMetrics());
1553 EXPECT_TRUE(personal_data_->ImportFormData(form_structure2,
1554 &imported_credit_card));
1555 ASSERT_TRUE(imported_credit_card);
1556 personal_data_->SaveImportedCreditCard(*imported_credit_card);
1558 // Verify that the web database has been updated and the notification sent.
1559 EXPECT_CALL(personal_data_observer_,
1560 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
1561 base::MessageLoop::current()->Run();
1563 CreditCard expected2(base::GenerateGUID(), "https://www.example.com");
1564 test::SetCreditCardInfo(&expected2,"", "5500000000000004", "02", "2012");
1565 const std::vector<CreditCard*>& results2 = personal_data_->GetCreditCards();
1566 ASSERT_EQ(2U, results2.size());
1567 EXPECT_EQ(0, expected.Compare(*results2[0]));
1568 EXPECT_EQ(0, expected2.Compare(*results2[1]));
1571 TEST_F(PersonalDataManagerTest, AggregateInvalidCreditCard) {
1572 FormData form1;
1574 // Start with a single valid credit card form.
1575 FormFieldData field;
1576 test::CreateTestFormField(
1577 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1578 form1.fields.push_back(field);
1579 test::CreateTestFormField(
1580 "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
1581 form1.fields.push_back(field);
1582 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
1583 form1.fields.push_back(field);
1584 test::CreateTestFormField("Exp Year:", "exp_year", "2011", "text", &field);
1585 form1.fields.push_back(field);
1587 FormStructure form_structure1(form1);
1588 form_structure1.DetermineHeuristicTypes(TestAutofillMetrics());
1589 scoped_ptr<CreditCard> imported_credit_card;
1590 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1591 &imported_credit_card));
1592 ASSERT_TRUE(imported_credit_card);
1593 personal_data_->SaveImportedCreditCard(*imported_credit_card);
1595 // Verify that the web database has been updated and the notification sent.
1596 EXPECT_CALL(personal_data_observer_,
1597 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
1598 base::MessageLoop::current()->Run();
1600 CreditCard expected(base::GenerateGUID(), "https://www.example.com");
1601 test::SetCreditCardInfo(&expected,
1602 "Biggie Smalls", "4111111111111111", "01", "2011");
1603 const std::vector<CreditCard*>& results = personal_data_->GetCreditCards();
1604 ASSERT_EQ(1U, results.size());
1605 EXPECT_EQ(0, expected.Compare(*results[0]));
1607 // Add a second different invalid credit card.
1608 FormData form2;
1609 test::CreateTestFormField(
1610 "Name on card:", "name_on_card", "Jim Johansen", "text", &field);
1611 form2.fields.push_back(field);
1612 test::CreateTestFormField(
1613 "Card Number:", "card_number", "1000000000000000", "text", &field);
1614 form2.fields.push_back(field);
1615 test::CreateTestFormField("Exp Month:", "exp_month", "02", "text", &field);
1616 form2.fields.push_back(field);
1617 test::CreateTestFormField("Exp Year:", "exp_year", "2012", "text", &field);
1618 form2.fields.push_back(field);
1620 FormStructure form_structure2(form2);
1621 form_structure2.DetermineHeuristicTypes(TestAutofillMetrics());
1622 EXPECT_FALSE(personal_data_->ImportFormData(form_structure2,
1623 &imported_credit_card));
1624 ASSERT_FALSE(imported_credit_card);
1626 // Since no refresh is expected, reload the data from the database to make
1627 // sure no changes were written out.
1628 ResetPersonalDataManager();
1630 const std::vector<CreditCard*>& results2 = personal_data_->GetCreditCards();
1631 ASSERT_EQ(1U, results2.size());
1632 EXPECT_EQ(0, expected.Compare(*results2[0]));
1635 TEST_F(PersonalDataManagerTest, AggregateSameCreditCardWithConflict) {
1636 FormData form1;
1638 // Start with a single valid credit card form.
1639 FormFieldData field;
1640 test::CreateTestFormField(
1641 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1642 form1.fields.push_back(field);
1643 test::CreateTestFormField(
1644 "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
1645 form1.fields.push_back(field);
1646 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
1647 form1.fields.push_back(field);
1648 test::CreateTestFormField("Exp Year:", "exp_year", "2011", "text", &field);
1649 form1.fields.push_back(field);
1651 FormStructure form_structure1(form1);
1652 form_structure1.DetermineHeuristicTypes(TestAutofillMetrics());
1653 scoped_ptr<CreditCard> imported_credit_card;
1654 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1655 &imported_credit_card));
1656 ASSERT_TRUE(imported_credit_card);
1657 personal_data_->SaveImportedCreditCard(*imported_credit_card);
1659 // Verify that the web database has been updated and the notification sent.
1660 EXPECT_CALL(personal_data_observer_,
1661 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
1662 base::MessageLoop::current()->Run();
1664 CreditCard expected(base::GenerateGUID(), "https://www.example.com");
1665 test::SetCreditCardInfo(&expected,
1666 "Biggie Smalls", "4111111111111111", "01", "2011");
1667 const std::vector<CreditCard*>& results = personal_data_->GetCreditCards();
1668 ASSERT_EQ(1U, results.size());
1669 EXPECT_EQ(0, expected.Compare(*results[0]));
1671 // Add a second different valid credit card where the year is different but
1672 // the credit card number matches.
1673 FormData form2;
1674 test::CreateTestFormField(
1675 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1676 form2.fields.push_back(field);
1677 test::CreateTestFormField(
1678 "Card Number:", "card_number", "4111 1111 1111 1111", "text", &field);
1679 form2.fields.push_back(field);
1680 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
1681 form2.fields.push_back(field);
1682 test::CreateTestFormField("Exp Year:", "exp_year", "2012", "text", &field);
1683 form2.fields.push_back(field);
1685 FormStructure form_structure2(form2);
1686 form_structure2.DetermineHeuristicTypes(TestAutofillMetrics());
1687 EXPECT_TRUE(personal_data_->ImportFormData(form_structure2,
1688 &imported_credit_card));
1689 EXPECT_FALSE(imported_credit_card);
1691 // Verify that the web database has been updated and the notification sent.
1692 EXPECT_CALL(personal_data_observer_,
1693 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
1694 base::MessageLoop::current()->Run();
1696 // Expect that the newer information is saved. In this case the year is
1697 // updated to "2012".
1698 CreditCard expected2(base::GenerateGUID(), "https://www.example.com");
1699 test::SetCreditCardInfo(&expected2,
1700 "Biggie Smalls", "4111111111111111", "01", "2012");
1701 const std::vector<CreditCard*>& results2 = personal_data_->GetCreditCards();
1702 ASSERT_EQ(1U, results2.size());
1703 EXPECT_EQ(0, expected2.Compare(*results2[0]));
1706 TEST_F(PersonalDataManagerTest, AggregateEmptyCreditCardWithConflict) {
1707 FormData form1;
1709 // Start with a single valid credit card form.
1710 FormFieldData field;
1711 test::CreateTestFormField(
1712 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1713 form1.fields.push_back(field);
1714 test::CreateTestFormField(
1715 "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
1716 form1.fields.push_back(field);
1717 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
1718 form1.fields.push_back(field);
1719 test::CreateTestFormField("Exp Year:", "exp_year", "2011", "text", &field);
1720 form1.fields.push_back(field);
1722 FormStructure form_structure1(form1);
1723 form_structure1.DetermineHeuristicTypes(TestAutofillMetrics());
1724 scoped_ptr<CreditCard> imported_credit_card;
1725 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1726 &imported_credit_card));
1727 ASSERT_TRUE(imported_credit_card);
1728 personal_data_->SaveImportedCreditCard(*imported_credit_card);
1730 // Verify that the web database has been updated and the notification sent.
1731 EXPECT_CALL(personal_data_observer_,
1732 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
1733 base::MessageLoop::current()->Run();
1735 CreditCard expected(base::GenerateGUID(), "https://www.example.com");
1736 test::SetCreditCardInfo(&expected,
1737 "Biggie Smalls", "4111111111111111", "01", "2011");
1738 const std::vector<CreditCard*>& results = personal_data_->GetCreditCards();
1739 ASSERT_EQ(1U, results.size());
1740 EXPECT_EQ(0, expected.Compare(*results[0]));
1742 // Add a second credit card with no number.
1743 FormData form2;
1744 test::CreateTestFormField(
1745 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1746 form2.fields.push_back(field);
1747 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
1748 form2.fields.push_back(field);
1749 test::CreateTestFormField("Exp Year:", "exp_year", "2012", "text", &field);
1750 form2.fields.push_back(field);
1752 FormStructure form_structure2(form2);
1753 form_structure2.DetermineHeuristicTypes(TestAutofillMetrics());
1754 EXPECT_FALSE(personal_data_->ImportFormData(form_structure2,
1755 &imported_credit_card));
1756 EXPECT_FALSE(imported_credit_card);
1758 // Since no refresh is expected, reload the data from the database to make
1759 // sure no changes were written out.
1760 ResetPersonalDataManager();
1762 // No change is expected.
1763 CreditCard expected2(base::GenerateGUID(), "https://www.example.com");
1764 test::SetCreditCardInfo(&expected2,
1765 "Biggie Smalls", "4111111111111111", "01", "2011");
1766 const std::vector<CreditCard*>& results2 = personal_data_->GetCreditCards();
1767 ASSERT_EQ(1U, results2.size());
1768 EXPECT_EQ(0, expected2.Compare(*results2[0]));
1771 TEST_F(PersonalDataManagerTest, AggregateCreditCardWithMissingInfoInNew) {
1772 FormData form1;
1774 // Start with a single valid credit card form.
1775 FormFieldData field;
1776 test::CreateTestFormField(
1777 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1778 form1.fields.push_back(field);
1779 test::CreateTestFormField(
1780 "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
1781 form1.fields.push_back(field);
1782 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
1783 form1.fields.push_back(field);
1784 test::CreateTestFormField("Exp Year:", "exp_year", "2011", "text", &field);
1785 form1.fields.push_back(field);
1787 FormStructure form_structure1(form1);
1788 form_structure1.DetermineHeuristicTypes(TestAutofillMetrics());
1789 scoped_ptr<CreditCard> imported_credit_card;
1790 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1791 &imported_credit_card));
1792 ASSERT_TRUE(imported_credit_card);
1793 personal_data_->SaveImportedCreditCard(*imported_credit_card);
1795 // Verify that the web database has been updated and the notification sent.
1796 EXPECT_CALL(personal_data_observer_,
1797 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
1798 base::MessageLoop::current()->Run();
1800 CreditCard expected(base::GenerateGUID(), "https://www.example.com");
1801 test::SetCreditCardInfo(&expected,
1802 "Biggie Smalls", "4111111111111111", "01", "2011");
1803 const std::vector<CreditCard*>& results = personal_data_->GetCreditCards();
1804 ASSERT_EQ(1U, results.size());
1805 EXPECT_EQ(0, expected.Compare(*results[0]));
1807 // Add a second different valid credit card where the name is missing but
1808 // the credit card number matches.
1809 FormData form2;
1810 // Note missing name.
1811 test::CreateTestFormField(
1812 "Card Number:", "card_number", "4111111111111111", "text", &field);
1813 form2.fields.push_back(field);
1814 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
1815 form2.fields.push_back(field);
1816 test::CreateTestFormField("Exp Year:", "exp_year", "2011", "text", &field);
1817 form2.fields.push_back(field);
1819 FormStructure form_structure2(form2);
1820 form_structure2.DetermineHeuristicTypes(TestAutofillMetrics());
1821 EXPECT_TRUE(personal_data_->ImportFormData(form_structure2,
1822 &imported_credit_card));
1823 EXPECT_FALSE(imported_credit_card);
1825 // Since no refresh is expected, reload the data from the database to make
1826 // sure no changes were written out.
1827 ResetPersonalDataManager();
1829 // No change is expected.
1830 CreditCard expected2(base::GenerateGUID(), "https://www.example.com");
1831 test::SetCreditCardInfo(&expected2,
1832 "Biggie Smalls", "4111111111111111", "01", "2011");
1833 const std::vector<CreditCard*>& results2 = personal_data_->GetCreditCards();
1834 ASSERT_EQ(1U, results2.size());
1835 EXPECT_EQ(0, expected2.Compare(*results2[0]));
1837 // Add a third credit card where the expiration date is missing.
1838 FormData form3;
1839 test::CreateTestFormField(
1840 "Name on card:", "name_on_card", "Johnny McEnroe", "text", &field);
1841 form3.fields.push_back(field);
1842 test::CreateTestFormField(
1843 "Card Number:", "card_number", "5555555555554444", "text", &field);
1844 form3.fields.push_back(field);
1845 // Note missing expiration month and year..
1847 FormStructure form_structure3(form3);
1848 form_structure3.DetermineHeuristicTypes(TestAutofillMetrics());
1849 EXPECT_FALSE(personal_data_->ImportFormData(form_structure3,
1850 &imported_credit_card));
1851 ASSERT_FALSE(imported_credit_card);
1853 // Since no refresh is expected, reload the data from the database to make
1854 // sure no changes were written out.
1855 ResetPersonalDataManager();
1857 // No change is expected.
1858 CreditCard expected3(base::GenerateGUID(), "https://www.example.com");
1859 test::SetCreditCardInfo(&expected3,
1860 "Biggie Smalls", "4111111111111111", "01", "2011");
1861 const std::vector<CreditCard*>& results3 = personal_data_->GetCreditCards();
1862 ASSERT_EQ(1U, results3.size());
1863 EXPECT_EQ(0, expected3.Compare(*results3[0]));
1866 TEST_F(PersonalDataManagerTest, AggregateCreditCardWithMissingInfoInOld) {
1867 // Start with a single valid credit card stored via the preferences.
1868 // Note the empty name.
1869 CreditCard saved_credit_card(base::GenerateGUID(), "https://www.example.com");
1870 test::SetCreditCardInfo(&saved_credit_card,
1871 "", "4111111111111111" /* Visa */, "01", "2011");
1872 personal_data_->AddCreditCard(saved_credit_card);
1874 // Verify that the web database has been updated and the notification sent.
1875 EXPECT_CALL(personal_data_observer_,
1876 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
1877 base::MessageLoop::current()->Run();
1879 const std::vector<CreditCard*>& results1 = personal_data_->GetCreditCards();
1880 ASSERT_EQ(1U, results1.size());
1881 EXPECT_EQ(saved_credit_card, *results1[0]);
1884 // Add a second different valid credit card where the year is different but
1885 // the credit card number matches.
1886 FormData form;
1887 FormFieldData field;
1888 test::CreateTestFormField(
1889 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1890 form.fields.push_back(field);
1891 test::CreateTestFormField(
1892 "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
1893 form.fields.push_back(field);
1894 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
1895 form.fields.push_back(field);
1896 test::CreateTestFormField("Exp Year:", "exp_year", "2012", "text", &field);
1897 form.fields.push_back(field);
1899 FormStructure form_structure(form);
1900 form_structure.DetermineHeuristicTypes(TestAutofillMetrics());
1901 scoped_ptr<CreditCard> imported_credit_card;
1902 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
1903 &imported_credit_card));
1904 EXPECT_FALSE(imported_credit_card);
1906 // Verify that the web database has been updated and the notification sent.
1907 EXPECT_CALL(personal_data_observer_,
1908 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
1909 base::MessageLoop::current()->Run();
1911 // Expect that the newer information is saved. In this case the year is
1912 // added to the existing credit card.
1913 CreditCard expected2(base::GenerateGUID(), "https://www.example.com");
1914 test::SetCreditCardInfo(&expected2,
1915 "Biggie Smalls", "4111111111111111", "01", "2012");
1916 const std::vector<CreditCard*>& results2 = personal_data_->GetCreditCards();
1917 ASSERT_EQ(1U, results2.size());
1918 EXPECT_EQ(0, expected2.Compare(*results2[0]));
1921 // We allow the user to store a credit card number with separators via the UI.
1922 // We should not try to re-aggregate the same card with the separators stripped.
1923 TEST_F(PersonalDataManagerTest, AggregateSameCreditCardWithSeparators) {
1924 // Start with a single valid credit card stored via the preferences.
1925 // Note the separators in the credit card number.
1926 CreditCard saved_credit_card(base::GenerateGUID(), "https://www.example.com");
1927 test::SetCreditCardInfo(&saved_credit_card,
1928 "Biggie Smalls", "4111 1111 1111 1111" /* Visa */, "01", "2011");
1929 personal_data_->AddCreditCard(saved_credit_card);
1931 // Verify that the web database has been updated and the notification sent.
1932 EXPECT_CALL(personal_data_observer_,
1933 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
1934 base::MessageLoop::current()->Run();
1936 const std::vector<CreditCard*>& results1 = personal_data_->GetCreditCards();
1937 ASSERT_EQ(1U, results1.size());
1938 EXPECT_EQ(0, saved_credit_card.Compare(*results1[0]));
1940 // Import the same card info, but with different separators in the number.
1941 FormData form;
1942 FormFieldData field;
1943 test::CreateTestFormField(
1944 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1945 form.fields.push_back(field);
1946 test::CreateTestFormField(
1947 "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
1948 form.fields.push_back(field);
1949 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
1950 form.fields.push_back(field);
1951 test::CreateTestFormField("Exp Year:", "exp_year", "2011", "text", &field);
1952 form.fields.push_back(field);
1954 FormStructure form_structure(form);
1955 form_structure.DetermineHeuristicTypes(TestAutofillMetrics());
1956 scoped_ptr<CreditCard> imported_credit_card;
1957 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
1958 &imported_credit_card));
1959 EXPECT_FALSE(imported_credit_card);
1961 // Since no refresh is expected, reload the data from the database to make
1962 // sure no changes were written out.
1963 ResetPersonalDataManager();
1965 // Expect that no new card is saved.
1966 const std::vector<CreditCard*>& results2 = personal_data_->GetCreditCards();
1967 ASSERT_EQ(1U, results2.size());
1968 EXPECT_EQ(0, saved_credit_card.Compare(*results2[0]));
1971 // Ensure that if a verified profile already exists, aggregated profiles cannot
1972 // modify it in any way.
1973 TEST_F(PersonalDataManagerTest, AggregateExistingVerifiedProfileWithConflict) {
1974 // Start with a verified profile.
1975 AutofillProfile profile(base::GenerateGUID(), "Chrome settings");
1976 test::SetProfileInfo(&profile,
1977 "Marion", "Mitchell", "Morrison",
1978 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
1979 "91601", "US", "12345678910");
1980 EXPECT_TRUE(profile.IsVerified());
1982 // Add the profile to the database.
1983 personal_data_->AddProfile(profile);
1985 // Verify that the web database has been updated and the notification sent.
1986 EXPECT_CALL(personal_data_observer_,
1987 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
1988 base::MessageLoop::current()->Run();
1990 // Simulate a form submission with conflicting info.
1991 FormData form;
1992 FormFieldData field;
1993 test::CreateTestFormField(
1994 "First name:", "first_name", "Marion", "text", &field);
1995 form.fields.push_back(field);
1996 test::CreateTestFormField(
1997 "Last name:", "last_name", "Morrison", "text", &field);
1998 form.fields.push_back(field);
1999 test::CreateTestFormField(
2000 "Email:", "email", "other.email@example.com", "text", &field);
2001 form.fields.push_back(field);
2002 test::CreateTestFormField(
2003 "Address:", "address1", "123 Zoo St.", "text", &field);
2004 form.fields.push_back(field);
2005 test::CreateTestFormField("City:", "city", "Hollywood", "text", &field);
2006 form.fields.push_back(field);
2007 test::CreateTestFormField("State:", "state", "CA", "text", &field);
2008 form.fields.push_back(field);
2009 test::CreateTestFormField("Zip:", "zip", "91601", "text", &field);
2010 form.fields.push_back(field);
2012 FormStructure form_structure(form);
2013 form_structure.DetermineHeuristicTypes(TestAutofillMetrics());
2014 scoped_ptr<CreditCard> imported_credit_card;
2015 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
2016 &imported_credit_card));
2017 EXPECT_FALSE(imported_credit_card);
2019 // Wait for the refresh, which in this case is a no-op.
2020 EXPECT_CALL(personal_data_observer_,
2021 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
2022 base::MessageLoop::current()->Run();
2024 // Expect that no new profile is saved.
2025 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
2026 ASSERT_EQ(1U, results.size());
2027 EXPECT_EQ(0, profile.Compare(*results[0]));
2030 // Ensure that if a verified credit card already exists, aggregated credit cards
2031 // cannot modify it in any way.
2032 TEST_F(PersonalDataManagerTest,
2033 AggregateExistingVerifiedCreditCardWithConflict) {
2034 // Start with a verified credit card.
2035 CreditCard credit_card(base::GenerateGUID(), "Chrome settings");
2036 test::SetCreditCardInfo(&credit_card,
2037 "Biggie Smalls", "4111 1111 1111 1111" /* Visa */, "01", "2011");
2038 EXPECT_TRUE(credit_card.IsVerified());
2040 // Add the credit card to the database.
2041 personal_data_->AddCreditCard(credit_card);
2043 // Verify that the web database has been updated and the notification sent.
2044 EXPECT_CALL(personal_data_observer_,
2045 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
2046 base::MessageLoop::current()->Run();
2048 // Simulate a form submission with conflicting expiration year.
2049 FormData form;
2050 FormFieldData field;
2051 test::CreateTestFormField(
2052 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
2053 form.fields.push_back(field);
2054 test::CreateTestFormField(
2055 "Card Number:", "card_number", "4111 1111 1111 1111", "text", &field);
2056 form.fields.push_back(field);
2057 test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
2058 form.fields.push_back(field);
2059 test::CreateTestFormField("Exp Year:", "exp_year", "2012", "text", &field);
2060 form.fields.push_back(field);
2062 FormStructure form_structure(form);
2063 form_structure.DetermineHeuristicTypes(TestAutofillMetrics());
2064 scoped_ptr<CreditCard> imported_credit_card;
2065 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
2066 &imported_credit_card));
2067 ASSERT_FALSE(imported_credit_card);
2069 // Since no refresh is expected, reload the data from the database to make
2070 // sure no changes were written out.
2071 ResetPersonalDataManager();
2073 // Expect that the saved credit card is not modified.
2074 const std::vector<CreditCard*>& results = personal_data_->GetCreditCards();
2075 ASSERT_EQ(1U, results.size());
2076 EXPECT_EQ(0, credit_card.Compare(*results[0]));
2079 // Ensure that verified profiles can be saved via SaveImportedProfile,
2080 // overwriting existing unverified profiles.
2081 TEST_F(PersonalDataManagerTest, SaveImportedProfileWithVerifiedData) {
2082 // Start with an unverified profile.
2083 AutofillProfile profile(base::GenerateGUID(), "https://www.example.com");
2084 test::SetProfileInfo(&profile,
2085 "Marion", "Mitchell", "Morrison",
2086 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
2087 "91601", "US", "12345678910");
2088 EXPECT_FALSE(profile.IsVerified());
2090 // Add the profile to the database.
2091 personal_data_->AddProfile(profile);
2093 // Verify that the web database has been updated and the notification sent.
2094 EXPECT_CALL(personal_data_observer_,
2095 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
2096 base::MessageLoop::current()->Run();
2098 AutofillProfile new_verified_profile = profile;
2099 new_verified_profile.set_guid(base::GenerateGUID());
2100 new_verified_profile.set_origin("Chrome settings");
2101 new_verified_profile.SetRawInfo(COMPANY_NAME, ASCIIToUTF16("Fizzbang, Inc."));
2102 EXPECT_TRUE(new_verified_profile.IsVerified());
2104 personal_data_->SaveImportedProfile(new_verified_profile);
2106 // Verify that the web database has been updated and the notification sent.
2107 EXPECT_CALL(personal_data_observer_,
2108 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
2109 base::MessageLoop::current()->Run();
2111 // Expect that the existing profile is not modified, and instead the new
2112 // profile is added.
2113 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
2114 ASSERT_EQ(1U, results.size());
2115 EXPECT_EQ(0, new_verified_profile.Compare(*results[0]));
2118 // Ensure that verified profiles can be saved via SaveImportedProfile,
2119 // overwriting existing verified profiles as well.
2120 TEST_F(PersonalDataManagerTest, SaveImportedProfileWithExistingVerifiedData) {
2121 // Start with a verified profile.
2122 AutofillProfile profile(base::GenerateGUID(), "Chrome settings");
2123 test::SetProfileInfo(&profile,
2124 "Marion", "Mitchell", "Morrison",
2125 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
2126 "91601", "US", "12345678910");
2127 EXPECT_TRUE(profile.IsVerified());
2129 // Add the profile to the database.
2130 personal_data_->AddProfile(profile);
2132 // Verify that the web database has been updated and the notification sent.
2133 EXPECT_CALL(personal_data_observer_,
2134 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
2135 base::MessageLoop::current()->Run();
2137 AutofillProfile new_verified_profile = profile;
2138 new_verified_profile.set_guid(base::GenerateGUID());
2139 new_verified_profile.SetRawInfo(COMPANY_NAME, ASCIIToUTF16("Fizzbang, Inc."));
2140 new_verified_profile.SetRawInfo(NAME_MIDDLE, base::string16());
2141 EXPECT_TRUE(new_verified_profile.IsVerified());
2143 personal_data_->SaveImportedProfile(new_verified_profile);
2145 // Verify that the web database has been updated and the notification sent.
2146 EXPECT_CALL(personal_data_observer_,
2147 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
2148 base::MessageLoop::current()->Run();
2150 // The new profile should be merged into the existing one.
2151 AutofillProfile expected_profile = new_verified_profile;
2152 expected_profile.set_guid(profile.guid());
2153 std::vector<base::string16> names;
2154 expected_profile.GetRawMultiInfo(NAME_FULL, &names);
2155 names.insert(names.begin(), ASCIIToUTF16("Marion Mitchell Morrison"));
2156 expected_profile.SetRawMultiInfo(NAME_FULL, names);
2158 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
2159 ASSERT_EQ(1U, results.size());
2160 EXPECT_EQ(expected_profile, *results[0]);
2163 // Ensure that verified credit cards can be saved via SaveImportedCreditCard.
2164 TEST_F(PersonalDataManagerTest, SaveImportedCreditCardWithVerifiedData) {
2165 // Start with a verified credit card.
2166 CreditCard credit_card(base::GenerateGUID(), "Chrome settings");
2167 test::SetCreditCardInfo(&credit_card,
2168 "Biggie Smalls", "4111 1111 1111 1111" /* Visa */, "01", "2011");
2169 EXPECT_TRUE(credit_card.IsVerified());
2171 // Add the credit card to the database.
2172 personal_data_->AddCreditCard(credit_card);
2174 // Verify that the web database has been updated and the notification sent.
2175 EXPECT_CALL(personal_data_observer_,
2176 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
2177 base::MessageLoop::current()->Run();
2179 CreditCard new_verified_card = credit_card;
2180 new_verified_card.set_guid(base::GenerateGUID());
2181 new_verified_card.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("B. Small"));
2182 EXPECT_TRUE(new_verified_card.IsVerified());
2184 personal_data_->SaveImportedCreditCard(new_verified_card);
2186 // Verify that the web database has been updated and the notification sent.
2187 EXPECT_CALL(personal_data_observer_,
2188 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
2189 base::MessageLoop::current()->Run();
2191 // Expect that the saved credit card is updated.
2192 const std::vector<CreditCard*>& results = personal_data_->GetCreditCards();
2193 ASSERT_EQ(1U, results.size());
2194 EXPECT_EQ(ASCIIToUTF16("B. Small"), results[0]->GetRawInfo(CREDIT_CARD_NAME));
2197 TEST_F(PersonalDataManagerTest, GetNonEmptyTypes) {
2198 // Check that there are no available types with no profiles stored.
2199 ServerFieldTypeSet non_empty_types;
2200 personal_data_->GetNonEmptyTypes(&non_empty_types);
2201 EXPECT_EQ(0U, non_empty_types.size());
2203 // Test with one profile stored.
2204 AutofillProfile profile0(base::GenerateGUID(), "https://www.example.com");
2205 test::SetProfileInfo(&profile0,
2206 "Marion", NULL, "Morrison",
2207 "johnwayne@me.xyz", NULL, "123 Zoo St.", NULL, "Hollywood", "CA",
2208 "91601", "US", "14155678910");
2210 personal_data_->AddProfile(profile0);
2212 // Verify that the web database has been updated and the notification sent.
2213 EXPECT_CALL(personal_data_observer_,
2214 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
2215 base::MessageLoop::current()->Run();
2217 personal_data_->GetNonEmptyTypes(&non_empty_types);
2218 EXPECT_EQ(15U, non_empty_types.size());
2219 EXPECT_TRUE(non_empty_types.count(NAME_FIRST));
2220 EXPECT_TRUE(non_empty_types.count(NAME_LAST));
2221 EXPECT_TRUE(non_empty_types.count(NAME_FULL));
2222 EXPECT_TRUE(non_empty_types.count(EMAIL_ADDRESS));
2223 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_LINE1));
2224 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_STREET_ADDRESS));
2225 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_CITY));
2226 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_STATE));
2227 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_ZIP));
2228 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_COUNTRY));
2229 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_NUMBER));
2230 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_COUNTRY_CODE));
2231 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_CITY_CODE));
2232 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_CITY_AND_NUMBER));
2233 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_WHOLE_NUMBER));
2235 // Test with multiple profiles stored.
2236 AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com");
2237 test::SetProfileInfo(&profile1,
2238 "Josephine", "Alicia", "Saenz",
2239 "joewayne@me.xyz", "Fox", "903 Apple Ct.", NULL, "Orlando", "FL", "32801",
2240 "US", "16502937549");
2242 AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com");
2243 test::SetProfileInfo(&profile2,
2244 "Josephine", "Alicia", "Saenz",
2245 "joewayne@me.xyz", "Fox", "1212 Center.", "Bld. 5", "Orlando", "FL",
2246 "32801", "US", "16502937549");
2248 personal_data_->AddProfile(profile1);
2249 personal_data_->AddProfile(profile2);
2251 // Verify that the web database has been updated and the notification sent.
2252 EXPECT_CALL(personal_data_observer_,
2253 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
2254 base::MessageLoop::current()->Run();
2256 personal_data_->GetNonEmptyTypes(&non_empty_types);
2257 EXPECT_EQ(19U, non_empty_types.size());
2258 EXPECT_TRUE(non_empty_types.count(NAME_FIRST));
2259 EXPECT_TRUE(non_empty_types.count(NAME_MIDDLE));
2260 EXPECT_TRUE(non_empty_types.count(NAME_MIDDLE_INITIAL));
2261 EXPECT_TRUE(non_empty_types.count(NAME_LAST));
2262 EXPECT_TRUE(non_empty_types.count(NAME_FULL));
2263 EXPECT_TRUE(non_empty_types.count(EMAIL_ADDRESS));
2264 EXPECT_TRUE(non_empty_types.count(COMPANY_NAME));
2265 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_LINE1));
2266 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_LINE2));
2267 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_STREET_ADDRESS));
2268 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_CITY));
2269 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_STATE));
2270 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_ZIP));
2271 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_COUNTRY));
2272 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_NUMBER));
2273 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_CITY_CODE));
2274 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_COUNTRY_CODE));
2275 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_CITY_AND_NUMBER));
2276 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_WHOLE_NUMBER));
2278 // Test with credit card information also stored.
2279 CreditCard credit_card(base::GenerateGUID(), "https://www.example.com");
2280 test::SetCreditCardInfo(&credit_card,
2281 "John Dillinger", "423456789012" /* Visa */,
2282 "01", "2010");
2283 personal_data_->AddCreditCard(credit_card);
2285 // Verify that the web database has been updated and the notification sent.
2286 EXPECT_CALL(personal_data_observer_,
2287 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
2288 base::MessageLoop::current()->Run();
2290 personal_data_->GetNonEmptyTypes(&non_empty_types);
2291 EXPECT_EQ(27U, non_empty_types.size());
2292 EXPECT_TRUE(non_empty_types.count(NAME_FIRST));
2293 EXPECT_TRUE(non_empty_types.count(NAME_MIDDLE));
2294 EXPECT_TRUE(non_empty_types.count(NAME_MIDDLE_INITIAL));
2295 EXPECT_TRUE(non_empty_types.count(NAME_LAST));
2296 EXPECT_TRUE(non_empty_types.count(NAME_FULL));
2297 EXPECT_TRUE(non_empty_types.count(EMAIL_ADDRESS));
2298 EXPECT_TRUE(non_empty_types.count(COMPANY_NAME));
2299 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_LINE1));
2300 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_LINE2));
2301 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_STREET_ADDRESS));
2302 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_CITY));
2303 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_STATE));
2304 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_ZIP));
2305 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_COUNTRY));
2306 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_NUMBER));
2307 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_CITY_CODE));
2308 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_COUNTRY_CODE));
2309 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_CITY_AND_NUMBER));
2310 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_WHOLE_NUMBER));
2311 EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_NAME));
2312 EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_NUMBER));
2313 EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_TYPE));
2314 EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_EXP_MONTH));
2315 EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_EXP_2_DIGIT_YEAR));
2316 EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_EXP_4_DIGIT_YEAR));
2317 EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR));
2318 EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR));
2321 TEST_F(PersonalDataManagerTest, CaseInsensitiveMultiValueAggregation) {
2322 FormData form1;
2323 FormFieldData field;
2324 test::CreateTestFormField(
2325 "First name:", "first_name", "George", "text", &field);
2326 form1.fields.push_back(field);
2327 test::CreateTestFormField(
2328 "Last name:", "last_name", "Washington", "text", &field);
2329 form1.fields.push_back(field);
2330 test::CreateTestFormField(
2331 "Email:", "email", "theprez@gmail.com", "text", &field);
2332 form1.fields.push_back(field);
2333 test::CreateTestFormField(
2334 "Address:", "address1", "21 Laussat St", "text", &field);
2335 form1.fields.push_back(field);
2336 test::CreateTestFormField(
2337 "City:", "city", "San Francisco", "text", &field);
2338 form1.fields.push_back(field);
2339 test::CreateTestFormField("State:", "state", "California", "text", &field);
2340 form1.fields.push_back(field);
2341 test::CreateTestFormField(
2342 "Zip:", "zip", "94102", "text", &field);
2343 form1.fields.push_back(field);
2344 test::CreateTestFormField(
2345 "Phone number:", "phone_number", "817-555-6789", "text", &field);
2346 form1.fields.push_back(field);
2348 FormStructure form_structure1(form1);
2349 form_structure1.DetermineHeuristicTypes(TestAutofillMetrics());
2350 scoped_ptr<CreditCard> imported_credit_card;
2351 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
2352 &imported_credit_card));
2353 ASSERT_FALSE(imported_credit_card);
2355 // Verify that the web database has been updated and the notification sent.
2356 EXPECT_CALL(personal_data_observer_,
2357 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
2358 base::MessageLoop::current()->Run();
2360 AutofillProfile expected(base::GenerateGUID(), "https://www.example.com");
2361 test::SetProfileInfo(&expected, "George", NULL,
2362 "Washington", "theprez@gmail.com", NULL, "21 Laussat St", NULL,
2363 "San Francisco", "California", "94102", NULL, "(817) 555-6789");
2364 const std::vector<AutofillProfile*>& results1 = personal_data_->GetProfiles();
2365 ASSERT_EQ(1U, results1.size());
2366 EXPECT_EQ(0, expected.Compare(*results1[0]));
2368 // Upper-case the first name and change the phone number.
2369 FormData form2;
2370 test::CreateTestFormField(
2371 "First name:", "first_name", "GEORGE", "text", &field);
2372 form2.fields.push_back(field);
2373 test::CreateTestFormField(
2374 "Last name:", "last_name", "Washington", "text", &field);
2375 form2.fields.push_back(field);
2376 test::CreateTestFormField(
2377 "Email:", "email", "theprez@gmail.com", "text", &field);
2378 form2.fields.push_back(field);
2379 test::CreateTestFormField(
2380 "Address:", "address1", "21 Laussat St", "text", &field);
2381 form2.fields.push_back(field);
2382 test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
2383 form2.fields.push_back(field);
2384 test::CreateTestFormField("State:", "state", "California", "text", &field);
2385 form2.fields.push_back(field);
2386 test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
2387 form2.fields.push_back(field);
2388 test::CreateTestFormField(
2389 "Phone number:", "phone_number", "214-555-1234", "text", &field);
2390 form2.fields.push_back(field);
2392 FormStructure form_structure2(form2);
2393 form_structure2.DetermineHeuristicTypes(TestAutofillMetrics());
2394 EXPECT_TRUE(personal_data_->ImportFormData(form_structure2,
2395 &imported_credit_card));
2396 ASSERT_FALSE(imported_credit_card);
2398 // Verify that the web database has been updated and the notification sent.
2399 EXPECT_CALL(personal_data_observer_,
2400 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
2401 base::MessageLoop::current()->Run();
2403 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
2405 // Modify expected to include multi-valued fields.
2406 std::vector<base::string16> values;
2407 expected.GetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, &values);
2408 values.push_back(ASCIIToUTF16("(214) 555-1234"));
2409 expected.SetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, values);
2411 ASSERT_EQ(1U, results2.size());
2412 EXPECT_EQ(0, expected.Compare(*results2[0]));
2415 TEST_F(PersonalDataManagerTest, IncognitoReadOnly) {
2416 ASSERT_TRUE(personal_data_->GetProfiles().empty());
2417 ASSERT_TRUE(personal_data_->GetCreditCards().empty());
2419 AutofillProfile steve_jobs(base::GenerateGUID(), "https://www.example.com");
2420 test::SetProfileInfo(&steve_jobs, "Steven", "Paul", "Jobs", "sjobs@apple.com",
2421 "Apple Computer, Inc.", "1 Infinite Loop", "", "Cupertino", "CA", "95014",
2422 "US", "(800) 275-2273");
2423 personal_data_->AddProfile(steve_jobs);
2425 CreditCard bill_gates(base::GenerateGUID(), "https://www.example.com");
2426 test::SetCreditCardInfo(
2427 &bill_gates, "William H. Gates", "5555555555554444", "1", "2020");
2428 personal_data_->AddCreditCard(bill_gates);
2430 MakeProfileIncognito();
2432 // The personal data manager should be able to read existing profiles in an
2433 // off-the-record context.
2434 ResetPersonalDataManager();
2435 ASSERT_EQ(1U, personal_data_->GetProfiles().size());
2436 ASSERT_EQ(1U, personal_data_->GetCreditCards().size());
2438 // No adds, saves, or updates should take effect.
2439 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()).Times(0);
2441 // Add profiles or credit card shouldn't work.
2442 personal_data_->AddProfile(test::GetFullProfile());
2444 CreditCard larry_page(base::GenerateGUID(), "https://www.example.com");
2445 test::SetCreditCardInfo(
2446 &larry_page, "Lawrence Page", "4111111111111111", "10", "2025");
2447 personal_data_->AddCreditCard(larry_page);
2449 ResetPersonalDataManager();
2450 EXPECT_EQ(1U, personal_data_->GetProfiles().size());
2451 EXPECT_EQ(1U, personal_data_->GetCreditCards().size());
2453 // Saving or creating profiles from imported profiles shouldn't work.
2454 steve_jobs.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Steve"));
2455 personal_data_->SaveImportedProfile(steve_jobs);
2457 bill_gates.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Bill Gates"));
2458 personal_data_->SaveImportedCreditCard(bill_gates);
2460 ResetPersonalDataManager();
2461 EXPECT_EQ(ASCIIToUTF16("Steven"),
2462 personal_data_->GetProfiles()[0]->GetRawInfo(NAME_FIRST));
2463 EXPECT_EQ(ASCIIToUTF16("William H. Gates"),
2464 personal_data_->GetCreditCards()[0]->GetRawInfo(CREDIT_CARD_NAME));
2466 // Updating existing profiles shouldn't work.
2467 steve_jobs.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Steve"));
2468 personal_data_->UpdateProfile(steve_jobs);
2470 bill_gates.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Bill Gates"));
2471 personal_data_->UpdateCreditCard(bill_gates);
2473 ResetPersonalDataManager();
2474 EXPECT_EQ(ASCIIToUTF16("Steven"),
2475 personal_data_->GetProfiles()[0]->GetRawInfo(NAME_FIRST));
2476 EXPECT_EQ(ASCIIToUTF16("William H. Gates"),
2477 personal_data_->GetCreditCards()[0]->GetRawInfo(CREDIT_CARD_NAME));
2479 // Removing shouldn't work.
2480 personal_data_->RemoveByGUID(steve_jobs.guid());
2481 personal_data_->RemoveByGUID(bill_gates.guid());
2483 ResetPersonalDataManager();
2484 EXPECT_EQ(1U, personal_data_->GetProfiles().size());
2485 EXPECT_EQ(1U, personal_data_->GetCreditCards().size());
2488 TEST_F(PersonalDataManagerTest, DefaultCountryCodeIsCached) {
2489 // The return value should always be some country code, no matter what.
2490 std::string default_country =
2491 personal_data_->GetDefaultCountryCodeForNewAddress();
2492 EXPECT_EQ(2U, default_country.size());
2494 AutofillProfile moose(base::GenerateGUID(), "Chrome settings");
2495 test::SetProfileInfo(&moose, "Moose", "P", "McMahon", "mpm@example.com",
2496 "", "1 Taiga TKTR", "", "Calgary", "AB", "T2B 2K2",
2497 "CA", "(800) 555-9000");
2498 personal_data_->AddProfile(moose);
2499 EXPECT_CALL(personal_data_observer_,
2500 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
2501 base::MessageLoop::current()->Run();
2502 // The value is cached and doesn't change even after adding an address.
2503 EXPECT_EQ(default_country,
2504 personal_data_->GetDefaultCountryCodeForNewAddress());
2506 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()).Times(2);
2508 // Disabling Autofill blows away this cache and shouldn't account for Autofill
2509 // profiles.
2510 profile_->GetPrefs()->SetBoolean(prefs::kAutofillEnabled, false);
2511 EXPECT_EQ(default_country,
2512 personal_data_->GetDefaultCountryCodeForNewAddress());
2514 // Enabling Autofill blows away the cached value and should reflect the new
2515 // value (accounting for profiles).
2516 profile_->GetPrefs()->SetBoolean(prefs::kAutofillEnabled, true);
2517 EXPECT_EQ(base::UTF16ToUTF8(moose.GetRawInfo(ADDRESS_HOME_COUNTRY)),
2518 personal_data_->GetDefaultCountryCodeForNewAddress());
2521 TEST_F(PersonalDataManagerTest, DefaultCountryCodeComesFromProfiles) {
2522 AutofillProfile moose(base::GenerateGUID(), "Chrome settings");
2523 test::SetProfileInfo(&moose, "Moose", "P", "McMahon", "mpm@example.com",
2524 "", "1 Taiga TKTR", "", "Calgary", "AB", "T2B 2K2",
2525 "CA", "(800) 555-9000");
2526 personal_data_->AddProfile(moose);
2527 ResetPersonalDataManager();
2528 EXPECT_EQ("CA", personal_data_->GetDefaultCountryCodeForNewAddress());
2530 // Multiple profiles cast votes.
2531 AutofillProfile armadillo(base::GenerateGUID(), "Chrome settings");
2532 test::SetProfileInfo(&armadillo, "Armin", "Dill", "Oh", "ado@example.com",
2533 "", "1 Speed Bump", "", "Lubbock", "TX", "77500",
2534 "MX", "(800) 555-9000");
2535 AutofillProfile armadillo2(base::GenerateGUID(), "Chrome settings");
2536 test::SetProfileInfo(&armadillo2, "Armin", "Dill", "Oh", "ado@example.com",
2537 "", "2 Speed Bump", "", "Lubbock", "TX", "77500",
2538 "MX", "(800) 555-9000");
2539 personal_data_->AddProfile(armadillo);
2540 personal_data_->AddProfile(armadillo2);
2541 ResetPersonalDataManager();
2542 EXPECT_EQ("MX", personal_data_->GetDefaultCountryCodeForNewAddress());
2544 personal_data_->RemoveByGUID(armadillo.guid());
2545 personal_data_->RemoveByGUID(armadillo2.guid());
2546 ResetPersonalDataManager();
2547 // Verified profiles count more.
2548 armadillo.set_origin("http://randomwebsite.com");
2549 armadillo2.set_origin("http://randomwebsite.com");
2550 personal_data_->AddProfile(armadillo);
2551 personal_data_->AddProfile(armadillo2);
2552 ResetPersonalDataManager();
2553 EXPECT_EQ("CA", personal_data_->GetDefaultCountryCodeForNewAddress());
2555 personal_data_->RemoveByGUID(armadillo.guid());
2556 ResetPersonalDataManager();
2557 // But unverified profiles can be a tie breaker.
2558 armadillo.set_origin("Chrome settings");
2559 personal_data_->AddProfile(armadillo);
2560 ResetPersonalDataManager();
2561 EXPECT_EQ("MX", personal_data_->GetDefaultCountryCodeForNewAddress());
2563 // Invalid country codes are ignored.
2564 personal_data_->RemoveByGUID(armadillo.guid());
2565 personal_data_->RemoveByGUID(moose.guid());
2566 AutofillProfile space_invader(base::GenerateGUID(), "Chrome settings");
2567 test::SetProfileInfo(&space_invader, "Marty", "", "Martian",
2568 "mm@example.com", "", "1 Flying Object", "", "Valles Marineris", "",
2569 "", "XX", "");
2570 personal_data_->AddProfile(moose);
2571 ResetPersonalDataManager();
2572 EXPECT_EQ("MX", personal_data_->GetDefaultCountryCodeForNewAddress());
2575 } // namespace autofill