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