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