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