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