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