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