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