personal_data_manager_unittest.cc revision 72a454cd3513ac24fbdd0e0cb9ad70b86a99b801
1// Copyright (c) 2010 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/message_loop.h"
9#include "base/ref_counted.h"
10#include "base/scoped_ptr.h"
11#include "base/utf_string_conversions.h"
12#include "chrome/browser/autofill/autofill_common_test.h"
13#include "chrome/browser/autofill/autofill_profile.h"
14#include "chrome/browser/autofill/form_structure.h"
15#include "chrome/browser/autofill/personal_data_manager.h"
16#include "chrome/browser/browser_thread.h"
17#include "chrome/browser/password_manager/encryptor.h"
18#include "chrome/common/guid.h"
19#include "chrome/common/notification_details.h"
20#include "chrome/common/notification_observer_mock.h"
21#include "chrome/common/notification_registrar.h"
22#include "chrome/common/notification_source.h"
23#include "chrome/common/notification_type.h"
24#include "chrome/test/testing_profile.h"
25#include "webkit/glue/form_data.h"
26#include "testing/gmock/include/gmock/gmock.h"
27#include "testing/gtest/include/gtest/gtest.h"
28
29using webkit_glue::FormData;
30
31ACTION(QuitUIMessageLoop) {
32  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
33  MessageLoop::current()->Quit();
34}
35
36class PersonalDataLoadedObserverMock : public PersonalDataManager::Observer {
37 public:
38  PersonalDataLoadedObserverMock() {}
39  virtual ~PersonalDataLoadedObserverMock() {}
40
41  MOCK_METHOD0(OnPersonalDataLoaded, void());
42};
43
44class PersonalDataManagerTest : public testing::Test {
45 protected:
46  PersonalDataManagerTest()
47      : ui_thread_(BrowserThread::UI, &message_loop_),
48        db_thread_(BrowserThread::DB) {
49  }
50
51  virtual void SetUp() {
52    db_thread_.Start();
53
54    profile_.reset(new TestingProfile);
55    profile_->CreateWebDataService(false);
56
57    autofill_test::DisableSystemServices(profile_.get());
58    ResetPersonalDataManager();
59  }
60
61  virtual void TearDown() {
62    personal_data_ = NULL;
63    if (profile_.get())
64      profile_.reset(NULL);
65
66    db_thread_.Stop();
67    MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask);
68    MessageLoop::current()->Run();
69  }
70
71  void ResetPersonalDataManager() {
72    personal_data_ = new PersonalDataManager();
73    personal_data_->Init(profile_.get());
74    personal_data_->SetObserver(&personal_data_observer_);
75  }
76
77  MessageLoopForUI message_loop_;
78  BrowserThread ui_thread_;
79  BrowserThread db_thread_;
80  scoped_ptr<TestingProfile> profile_;
81  scoped_refptr<PersonalDataManager> personal_data_;
82  NotificationRegistrar registrar_;
83  NotificationObserverMock observer_;
84  PersonalDataLoadedObserverMock personal_data_observer_;
85};
86
87// TODO(jhawkins): Test SetProfiles w/out a WebDataService in the profile.
88TEST_F(PersonalDataManagerTest, SetProfiles) {
89  AutoFillProfile profile0;
90  autofill_test::SetProfileInfo(&profile0,
91      "Billing", "Marion", "Mitchell", "Morrison",
92      "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
93      "91601", "US", "12345678910", "01987654321");
94
95  AutoFillProfile profile1;
96  autofill_test::SetProfileInfo(&profile1,
97      "Home", "Josephine", "Alicia", "Saenz",
98      "joewayne@me.xyz", "Fox", "903 Apple Ct.", NULL, "Orlando", "FL", "32801",
99      "US", "19482937549", "13502849239");
100
101  AutoFillProfile profile2;
102  autofill_test::SetProfileInfo(&profile2,
103      "Work", "Josephine", "Alicia", "Saenz",
104      "joewayne@me.xyz", "Fox", "1212 Center.", "Bld. 5", "Orlando", "FL",
105      "32801", "US", "19482937549", "13502849239");
106
107  // This will verify that the web database has been loaded and the notification
108  // sent out.
109  EXPECT_CALL(personal_data_observer_,
110              OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
111
112  // The message loop will exit when the mock observer is notified.
113  MessageLoop::current()->Run();
114
115  // Add two test profiles to the database.
116  std::vector<AutoFillProfile> update;
117  update.push_back(profile0);
118  update.push_back(profile1);
119  personal_data_->SetProfiles(&update);
120
121  const std::vector<AutoFillProfile*>& results1 = personal_data_->profiles();
122  ASSERT_EQ(2U, results1.size());
123  EXPECT_EQ(0, profile0.Compare(*results1.at(0)));
124  EXPECT_EQ(0, profile1.Compare(*results1.at(1)));
125
126  // Three operations in one:
127  //  - Update profile0
128  //  - Remove profile1
129  //  - Add profile2
130  profile0.SetInfo(AutoFillType(NAME_FIRST), ASCIIToUTF16("John"));
131  update.clear();
132  update.push_back(profile0);
133  update.push_back(profile2);
134  personal_data_->SetProfiles(&update);
135
136  const std::vector<AutoFillProfile*>& results2 = personal_data_->profiles();
137  ASSERT_EQ(2U, results2.size());
138  EXPECT_EQ(0, profile0.Compare(*results2.at(0)));
139  EXPECT_EQ(0, profile2.Compare(*results2.at(1)));
140
141  // Reset the PersonalDataManager.  This tests that the personal data was saved
142  // to the web database, and that we can load the profiles from the web
143  // database.
144  ResetPersonalDataManager();
145
146  // This will verify that the web database has been loaded and the notification
147  // sent out.
148  EXPECT_CALL(personal_data_observer_,
149              OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
150
151  // The message loop will exit when the PersonalDataLoadedObserver is notified.
152  MessageLoop::current()->Run();
153
154  // Verify that we've loaded the profiles from the web database.
155  const std::vector<AutoFillProfile*>& results3 = personal_data_->profiles();
156  ASSERT_EQ(2U, results3.size());
157  EXPECT_EQ(0, profile0.Compare(*results3.at(0)));
158  EXPECT_EQ(0, profile2.Compare(*results3.at(1)));
159}
160
161// TODO(jhawkins): Test SetCreditCards w/out a WebDataService in the profile.
162TEST_F(PersonalDataManagerTest, SetCreditCards) {
163  CreditCard creditcard0;
164  autofill_test::SetCreditCardInfo(&creditcard0, "Corporate",
165      "John Dillinger", "423456789012" /* Visa */, "01", "2010");
166
167  CreditCard creditcard1;
168  autofill_test::SetCreditCardInfo(&creditcard1, "Personal",
169      "Bonnie Parker", "518765432109" /* Mastercard */, "12", "2012");
170
171  CreditCard creditcard2;
172  autofill_test::SetCreditCardInfo(&creditcard2, "Savings",
173      "Clyde Barrow", "347666888555" /* American Express */, "04", "2015");
174
175  // This will verify that the web database has been loaded and the notification
176  // sent out.
177  EXPECT_CALL(personal_data_observer_,
178              OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
179
180  // The message loop will exit when the mock observer is notified.
181  MessageLoop::current()->Run();
182
183  // Add two test credit cards to the database.
184  std::vector<CreditCard> update;
185  update.push_back(creditcard0);
186  update.push_back(creditcard1);
187  personal_data_->SetCreditCards(&update);
188
189  const std::vector<CreditCard*>& results1 = personal_data_->credit_cards();
190  ASSERT_EQ(2U, results1.size());
191  EXPECT_EQ(0, creditcard0.Compare(*results1.at(0)));
192  EXPECT_EQ(0, creditcard1.Compare(*results1.at(1)));
193
194  // Three operations in one:
195  //  - Update creditcard0
196  //  - Remove creditcard1
197  //  - Add creditcard2
198  creditcard0.SetInfo(AutoFillType(CREDIT_CARD_NAME), ASCIIToUTF16("Joe"));
199  update.clear();
200  update.push_back(creditcard0);
201  update.push_back(creditcard2);
202  personal_data_->SetCreditCards(&update);
203
204  const std::vector<CreditCard*>& results2 = personal_data_->credit_cards();
205  ASSERT_EQ(2U, results2.size());
206  EXPECT_EQ(creditcard0, *results2.at(0));
207  EXPECT_EQ(creditcard2, *results2.at(1));
208
209  // Reset the PersonalDataManager.  This tests that the personal data was saved
210  // to the web database, and that we can load the credit cards from the web
211  // database.
212  ResetPersonalDataManager();
213
214  // This will verify that the web database has been loaded and the notification
215  // sent out.
216  EXPECT_CALL(personal_data_observer_,
217              OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
218
219  // The message loop will exit when the PersonalDataLoadedObserver is notified.
220  MessageLoop::current()->Run();
221
222  // Verify that we've loaded the credit cards from the web database.
223  const std::vector<CreditCard*>& results3 = personal_data_->credit_cards();
224  ASSERT_EQ(2U, results3.size());
225  EXPECT_EQ(creditcard0, *results3.at(0));
226  EXPECT_EQ(creditcard2, *results3.at(1));
227}
228
229TEST_F(PersonalDataManagerTest, SetProfilesAndCreditCards) {
230  AutoFillProfile profile0;
231  autofill_test::SetProfileInfo(&profile0,
232      "Billing", "Marion", "Mitchell", "Morrison",
233      "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
234      "91601", "US", "12345678910", "01987654321");
235
236  AutoFillProfile profile1;
237  autofill_test::SetProfileInfo(&profile1,
238      "Home", "Josephine", "Alicia", "Saenz",
239      "joewayne@me.xyz", "Fox", "903 Apple Ct.", NULL, "Orlando", "FL", "32801",
240      "US", "19482937549", "13502849239");
241
242  CreditCard creditcard0;
243  autofill_test::SetCreditCardInfo(&creditcard0, "Corporate",
244      "John Dillinger", "423456789012" /* Visa */, "01", "2010");
245
246  CreditCard creditcard1;
247  autofill_test::SetCreditCardInfo(&creditcard1, "Personal",
248      "Bonnie Parker", "518765432109" /* Mastercard */, "12", "2012");
249
250  // This will verify that the web database has been loaded and the notification
251  // sent out.
252  EXPECT_CALL(
253      personal_data_observer_,
254      OnPersonalDataLoaded()).Times(2).WillRepeatedly(QuitUIMessageLoop());
255
256  // The message loop will exit when the mock observer is notified.
257  MessageLoop::current()->Run();
258
259  // Add two test profiles to the database.
260  std::vector<AutoFillProfile> update;
261  update.push_back(profile0);
262  update.push_back(profile1);
263  personal_data_->SetProfiles(&update);
264
265  const std::vector<AutoFillProfile*>& results1 = personal_data_->profiles();
266  ASSERT_EQ(2U, results1.size());
267  EXPECT_EQ(0, profile0.Compare(*results1.at(0)));
268  EXPECT_EQ(0, profile1.Compare(*results1.at(1)));
269
270  MessageLoop::current()->Run();
271
272  // Add two test credit cards to the database.
273  std::vector<CreditCard> update_cc;
274  update_cc.push_back(creditcard0);
275  update_cc.push_back(creditcard1);
276  personal_data_->SetCreditCards(&update_cc);
277
278
279  const std::vector<CreditCard*>& results2 = personal_data_->credit_cards();
280  ASSERT_EQ(2U, results2.size());
281  EXPECT_EQ(creditcard0, *results2.at(0));
282  EXPECT_EQ(creditcard1, *results2.at(1));
283
284  // Determine uniqueness by inserting all of the GUIDs into a set and verifying
285  // the size of the set matches the number of GUIDs.
286  std::set<std::string> guids;
287  guids.insert(profile0.guid());
288  guids.insert(profile1.guid());
289  guids.insert(creditcard0.guid());
290  guids.insert(creditcard1.guid());
291  EXPECT_EQ(4U, guids.size());
292}
293
294// Test care for 50047. Makes sure that unique_ids_ is populated correctly on
295// load.
296TEST_F(PersonalDataManagerTest, PopulateUniqueIDsOnLoad) {
297  AutoFillProfile profile0;
298  autofill_test::SetProfileInfo(&profile0,
299      "", "y", "", "", "", "", "", "", "", "", "", "", "", "");
300
301  // This will verify that the web database has been loaded and the notification
302  // sent out.
303  EXPECT_CALL(personal_data_observer_,
304              OnPersonalDataLoaded()).WillRepeatedly(QuitUIMessageLoop());
305
306  // The message loop will exit when the mock observer is notified.
307  MessageLoop::current()->Run();
308
309  // Add the profile0 to the db.
310  std::vector<AutoFillProfile> update;
311  update.push_back(profile0);
312  personal_data_->SetProfiles(&update);
313
314  // Reset the PersonalDataManager. This recreates PersonalDataManager, which
315  // should populate unique_ids_.
316  ResetPersonalDataManager();
317
318  // The message loop will exit when the PersonalDataLoadedObserver is notified.
319  MessageLoop::current()->Run();
320
321  // Verify that we've loaded the profiles from the web database.
322  const std::vector<AutoFillProfile*>& results2 = personal_data_->profiles();
323  ASSERT_EQ(1U, results2.size());
324
325  // Add a new profile.
326  AutoFillProfile profile1;
327  autofill_test::SetProfileInfo(&profile1,
328      "", "y", "", "", "", "", "", "", "", "", "", "", "", "");
329  update.clear();
330  update.push_back(*results2[0]);
331  update.push_back(profile1);
332  personal_data_->SetProfiles(&update);
333
334  // Make sure the two profiles have different ids (and neither equal to 0,
335  // which is an invalid id).
336  const std::vector<AutoFillProfile*>& results3 = personal_data_->profiles();
337  ASSERT_EQ(2U, results3.size());
338  EXPECT_NE(results3[0]->guid(), results3[1]->guid());
339  EXPECT_TRUE(guid::IsValidGUID(results3[0]->guid()));
340  EXPECT_TRUE(guid::IsValidGUID(results3[1]->guid()));
341}
342
343TEST_F(PersonalDataManagerTest, SetEmptyProfile) {
344  AutoFillProfile profile0;
345  autofill_test::SetProfileInfo(&profile0,
346      "", "", "", "", "", "", "", "", "", "", "", "", "", "");
347
348  // This will verify that the web database has been loaded and the notification
349  // sent out.
350  EXPECT_CALL(personal_data_observer_,
351              OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
352
353  // The message loop will exit when the mock observer is notified.
354  MessageLoop::current()->Run();
355
356  // Add the empty profile to the database.
357  std::vector<AutoFillProfile> update;
358  update.push_back(profile0);
359  personal_data_->SetProfiles(&update);
360
361  // Check the local store of profiles, not yet saved to the web database.
362  const std::vector<AutoFillProfile*>& results1 = personal_data_->profiles();
363  ASSERT_EQ(0U, results1.size());
364
365  // Reset the PersonalDataManager.  This tests that the personal data was saved
366  // to the web database, and that we can load the profiles from the web
367  // database.
368  ResetPersonalDataManager();
369
370  // This will verify that the web database has been loaded and the notification
371  // sent out.
372  EXPECT_CALL(personal_data_observer_,
373              OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
374
375  // The message loop will exit when the PersonalDataLoadedObserver is notified.
376  MessageLoop::current()->Run();
377
378  // Verify that we've loaded the profiles from the web database.
379  const std::vector<AutoFillProfile*>& results2 = personal_data_->profiles();
380  ASSERT_EQ(0U, results2.size());
381}
382
383TEST_F(PersonalDataManagerTest, SetEmptyCreditCard) {
384  CreditCard creditcard0;
385  autofill_test::SetCreditCardInfo(&creditcard0, "", "", "", "", "");
386
387  // This will verify that the web database has been loaded and the notification
388  // sent out.
389  EXPECT_CALL(personal_data_observer_,
390              OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
391
392  // The message loop will exit when the mock observer is notified.
393  MessageLoop::current()->Run();
394
395  // Add the empty credit card to the database.
396  std::vector<CreditCard> update;
397  update.push_back(creditcard0);
398  personal_data_->SetCreditCards(&update);
399
400  // Check the local store of credit cards, not yet saved to the web database.
401  const std::vector<CreditCard*>& results1 = personal_data_->credit_cards();
402  ASSERT_EQ(0U, results1.size());
403
404  // Reset the PersonalDataManager.  This tests that the personal data was saved
405  // to the web database, and that we can load the credit cards from the web
406  // database.
407  ResetPersonalDataManager();
408
409  // This will verify that the web database has been loaded and the notification
410  // sent out.
411  EXPECT_CALL(personal_data_observer_,
412              OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
413
414  // The message loop will exit when the PersonalDataLoadedObserver is notified.
415  MessageLoop::current()->Run();
416
417  // Verify that we've loaded the credit cards from the web database.
418  const std::vector<CreditCard*>& results2 = personal_data_->credit_cards();
419  ASSERT_EQ(0U, results2.size());
420}
421
422TEST_F(PersonalDataManagerTest, Refresh) {
423  AutoFillProfile profile0;
424  autofill_test::SetProfileInfo(&profile0,
425      "Billing", "Marion", "Mitchell", "Morrison",
426      "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
427      "91601", "US", "12345678910", "01987654321");
428
429  AutoFillProfile profile1;
430  autofill_test::SetProfileInfo(&profile1,
431      "Home", "Josephine", "Alicia", "Saenz",
432      "joewayne@me.xyz", "Fox", "903 Apple Ct.", NULL, "Orlando", "FL", "32801",
433      "US", "19482937549", "13502849239");
434
435  EXPECT_CALL(personal_data_observer_,
436      OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
437
438  MessageLoop::current()->Run();
439
440  // Add the test profiles to the database.
441  std::vector<AutoFillProfile> update;
442  update.push_back(profile0);
443  update.push_back(profile1);
444  personal_data_->SetProfiles(&update);
445
446  // Labels depend on other profiles in the list - update labels manually.
447  std::vector<AutoFillProfile *> profile_pointers;
448  profile_pointers.push_back(&profile0);
449  profile_pointers.push_back(&profile1);
450  AutoFillProfile::AdjustInferredLabels(&profile_pointers);
451
452  // Wait for the refresh.
453  EXPECT_CALL(personal_data_observer_,
454      OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
455
456  MessageLoop::current()->Run();
457
458  const std::vector<AutoFillProfile*>& results1 = personal_data_->profiles();
459  ASSERT_EQ(2U, results1.size());
460  EXPECT_EQ(profile0, *results1.at(0));
461  EXPECT_EQ(profile1, *results1.at(1));
462
463  AutoFillProfile profile2;
464  autofill_test::SetProfileInfo(&profile2,
465      "Work", "Josephine", "Alicia", "Saenz",
466      "joewayne@me.xyz", "Fox", "1212 Center.", "Bld. 5", "Orlando", "FL",
467      "32801", "US", "19482937549", "13502849239");
468
469  // Adjust all labels.
470  profile_pointers.push_back(&profile2);
471  AutoFillProfile::AdjustInferredLabels(&profile_pointers);
472
473  WebDataService* wds = profile_->GetWebDataService(Profile::EXPLICIT_ACCESS);
474  ASSERT_TRUE(wds);
475  wds->AddAutoFillProfileGUID(profile2);
476
477  personal_data_->Refresh();
478
479  // Wait for the refresh.
480  EXPECT_CALL(personal_data_observer_,
481    OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
482
483  MessageLoop::current()->Run();
484
485  const std::vector<AutoFillProfile*>& results2 = personal_data_->profiles();
486  ASSERT_EQ(3U, results2.size());
487  EXPECT_EQ(profile0, *results2.at(0));
488  EXPECT_EQ(profile1, *results2.at(1));
489  EXPECT_EQ(profile2, *results2.at(2));
490
491  wds->RemoveAutoFillProfileGUID(profile1.guid());
492  wds->RemoveAutoFillProfileGUID(profile2.guid());
493
494  // Before telling the PDM to refresh, simulate an edit to one of the profiles
495  // via a SetProfile update (this would happen if the AutoFill window was
496  // open with a previous snapshot of the profiles, and something [e.g. sync]
497  // removed a profile from the browser.  In this edge case, we will end up
498  // in a consistent state by dropping the write).
499  profile2.SetInfo(AutoFillType(NAME_FIRST), ASCIIToUTF16("Jo"));
500  update.clear();
501  update.push_back(profile0);
502  update.push_back(profile1);
503  update.push_back(profile2);
504  personal_data_->SetProfiles(&update);
505
506  // Wait for the refresh.
507  EXPECT_CALL(personal_data_observer_,
508      OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
509
510  MessageLoop::current()->Run();
511
512  const std::vector<AutoFillProfile*>& results3 = personal_data_->profiles();
513  ASSERT_EQ(1U, results3.size());
514  EXPECT_EQ(profile0, *results2.at(0));
515}
516
517TEST_F(PersonalDataManagerTest, ImportFormData) {
518  FormData form;
519  webkit_glue::FormField field;
520  autofill_test::CreateTestFormField(
521      "First name:", "first_name", "George", "text", &field);
522  form.fields.push_back(field);
523  autofill_test::CreateTestFormField(
524      "Last name:", "last_name", "Washington", "text", &field);
525  form.fields.push_back(field);
526  autofill_test::CreateTestFormField(
527      "Email:", "email", "theprez@gmail.com", "text", &field);
528  form.fields.push_back(field);
529  autofill_test::CreateTestFormField(
530      "Address:", "address1", "21 Laussat St", "text", &field);
531  form.fields.push_back(field);
532  autofill_test::CreateTestFormField(
533      "City:", "city", "San Francisco", "text", &field);
534  form.fields.push_back(field);
535  autofill_test::CreateTestFormField(
536      "State:", "state", "California", "text", &field);
537  form.fields.push_back(field);
538  autofill_test::CreateTestFormField(
539      "Zip:", "zip", "94102", "text", &field);
540  form.fields.push_back(field);
541  FormStructure form_structure(form);
542  std::vector<const FormStructure*> forms;
543  forms.push_back(&form_structure);
544  const CreditCard* imported_credit_card;
545  EXPECT_TRUE(personal_data_->ImportFormData(forms, &imported_credit_card));
546  ASSERT_FALSE(imported_credit_card);
547
548  // Wait for the refresh.
549  EXPECT_CALL(personal_data_observer_,
550      OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
551
552  MessageLoop::current()->Run();
553
554  AutoFillProfile expected;
555  autofill_test::SetProfileInfo(&expected, NULL, "George", NULL,
556      "Washington", "theprez@gmail.com", NULL, "21 Laussat St", NULL,
557      "San Francisco", "California", "94102", NULL, NULL, NULL);
558  const std::vector<AutoFillProfile*>& results = personal_data_->profiles();
559  ASSERT_EQ(1U, results.size());
560  EXPECT_EQ(0, expected.Compare(*results[0]));
561}
562
563TEST_F(PersonalDataManagerTest, ImportFormDataBadEmail) {
564  FormData form;
565  webkit_glue::FormField field;
566  autofill_test::CreateTestFormField(
567      "First name:", "first_name", "George", "text", &field);
568  form.fields.push_back(field);
569  autofill_test::CreateTestFormField(
570      "Last name:", "last_name", "Washington", "text", &field);
571  form.fields.push_back(field);
572  autofill_test::CreateTestFormField(
573      "Email:", "email", "bogus", "text", &field);
574  form.fields.push_back(field);
575  autofill_test::CreateTestFormField(
576      "Address:", "address1", "21 Laussat St", "text", &field);
577  form.fields.push_back(field);
578  autofill_test::CreateTestFormField(
579      "City:", "city", "San Francisco", "text", &field);
580  form.fields.push_back(field);
581  autofill_test::CreateTestFormField(
582      "State:", "state", "California", "text", &field);
583  form.fields.push_back(field);
584  autofill_test::CreateTestFormField(
585      "Zip:", "zip", "94102", "text", &field);
586  form.fields.push_back(field);
587  FormStructure form_structure(form);
588  std::vector<const FormStructure*> forms;
589  forms.push_back(&form_structure);
590  const CreditCard* imported_credit_card;
591  EXPECT_TRUE(personal_data_->ImportFormData(forms, &imported_credit_card));
592  ASSERT_FALSE(imported_credit_card);
593
594  // Wait for the refresh.
595  EXPECT_CALL(personal_data_observer_,
596      OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
597
598  MessageLoop::current()->Run();
599
600  AutoFillProfile expected;
601  autofill_test::SetProfileInfo(&expected, NULL, "George", NULL,
602      "Washington", NULL, NULL, "21 Laussat St", NULL,
603      "San Francisco", "California", "94102", NULL, NULL, NULL);
604  const std::vector<AutoFillProfile*>& results = personal_data_->profiles();
605  ASSERT_EQ(1U, results.size());
606  EXPECT_EQ(0, expected.Compare(*results[0]));
607}
608
609TEST_F(PersonalDataManagerTest, ImportFormDataNotEnoughFilledFields) {
610  FormData form;
611  webkit_glue::FormField field;
612  autofill_test::CreateTestFormField(
613      "First name:", "first_name", "George", "text", &field);
614  form.fields.push_back(field);
615  autofill_test::CreateTestFormField(
616      "Last name:", "last_name", "Washington", "text", &field);
617  form.fields.push_back(field);
618  autofill_test::CreateTestFormField(
619      "Card number:", "card_number", "4111 1111 1111 1111", "text", &field);
620  form.fields.push_back(field);
621  FormStructure form_structure(form);
622  std::vector<const FormStructure*> forms;
623  forms.push_back(&form_structure);
624  const CreditCard* imported_credit_card;
625  EXPECT_FALSE(personal_data_->ImportFormData(forms, &imported_credit_card));
626  ASSERT_FALSE(imported_credit_card);
627
628  // Wait for the refresh.
629  EXPECT_CALL(personal_data_observer_,
630      OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
631
632  MessageLoop::current()->Run();
633
634  const std::vector<AutoFillProfile*>& profiles = personal_data_->profiles();
635  ASSERT_EQ(0U, profiles.size());
636  const std::vector<CreditCard*>& credit_cards = personal_data_->credit_cards();
637  ASSERT_EQ(0U, credit_cards.size());
638}
639
640TEST_F(PersonalDataManagerTest, ImportPhoneNumberSplitAcrossMultipleFields) {
641  FormData form;
642  webkit_glue::FormField field;
643  autofill_test::CreateTestFormField(
644      "First name:", "first_name", "George", "text", &field);
645  form.fields.push_back(field);
646  autofill_test::CreateTestFormField(
647      "Last name:", "last_name", "Washington", "text", &field);
648  form.fields.push_back(field);
649  autofill_test::CreateTestFormField(
650      "Phone #:", "home_phone_area_code", "650", "text", &field);
651  field.set_max_length(3);
652  form.fields.push_back(field);
653  autofill_test::CreateTestFormField(
654      "Phone #:", "home_phone_prefix", "555", "text", &field);
655  field.set_max_length(3);
656  form.fields.push_back(field);
657  autofill_test::CreateTestFormField(
658      "Phone #:", "home_phone_suffix", "0000", "text", &field);
659  field.set_max_length(4);
660  form.fields.push_back(field);
661  autofill_test::CreateTestFormField(
662      "Address:", "address1", "21 Laussat St", "text", &field);
663  form.fields.push_back(field);
664  autofill_test::CreateTestFormField(
665      "City:", "city", "San Francisco", "text", &field);
666  form.fields.push_back(field);
667  autofill_test::CreateTestFormField(
668      "State:", "state", "California", "text", &field);
669  form.fields.push_back(field);
670  autofill_test::CreateTestFormField(
671      "Zip:", "zip", "94102", "text", &field);
672  form.fields.push_back(field);
673  FormStructure form_structure(form);
674  std::vector<const FormStructure*> forms;
675  forms.push_back(&form_structure);
676  const CreditCard* imported_credit_card;
677  EXPECT_TRUE(personal_data_->ImportFormData(forms, &imported_credit_card));
678  ASSERT_FALSE(imported_credit_card);
679
680  // Wait for the refresh.
681  EXPECT_CALL(personal_data_observer_,
682      OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
683
684  MessageLoop::current()->Run();
685
686  AutoFillProfile expected;
687  autofill_test::SetProfileInfo(&expected, NULL, "George", NULL,
688      "Washington", NULL, NULL, "21 Laussat St", NULL,
689      "San Francisco", "California", "94102", NULL, "6505550000", NULL);
690  const std::vector<AutoFillProfile*>& results = personal_data_->profiles();
691  ASSERT_EQ(1U, results.size());
692  EXPECT_EQ(0, expected.Compare(*results[0]));
693}
694
695TEST_F(PersonalDataManagerTest, SetUniqueCreditCardLabels) {
696  CreditCard credit_card0;
697  credit_card0.set_label(ASCIIToUTF16("Home"));
698  credit_card0.SetInfo(AutoFillType(CREDIT_CARD_NAME), ASCIIToUTF16("John"));
699  CreditCard credit_card1;
700  credit_card1.set_label(ASCIIToUTF16("Home"));
701  credit_card1.SetInfo(AutoFillType(CREDIT_CARD_NAME), ASCIIToUTF16("Paul"));
702  CreditCard credit_card2;
703  credit_card2.set_label(ASCIIToUTF16("Home"));
704  credit_card2.SetInfo(AutoFillType(CREDIT_CARD_NAME), ASCIIToUTF16("Ringo"));
705  CreditCard credit_card3;
706  credit_card3.set_label(ASCIIToUTF16("NotHome"));
707  credit_card3.SetInfo(AutoFillType(CREDIT_CARD_NAME), ASCIIToUTF16("Other"));
708  CreditCard credit_card4;
709  credit_card4.set_label(ASCIIToUTF16("Work"));
710  credit_card4.SetInfo(AutoFillType(CREDIT_CARD_NAME), ASCIIToUTF16("Ozzy"));
711  CreditCard credit_card5;
712  credit_card5.set_label(ASCIIToUTF16("Work"));
713  credit_card5.SetInfo(AutoFillType(CREDIT_CARD_NAME), ASCIIToUTF16("Dio"));
714
715  // This will verify that the web database has been loaded and the notification
716  // sent out.
717  EXPECT_CALL(personal_data_observer_,
718              OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
719
720  // The message loop will exit when the mock observer is notified.
721  MessageLoop::current()->Run();
722
723  // Add the test credit cards to the database.
724  std::vector<CreditCard> update;
725  update.push_back(credit_card0);
726  update.push_back(credit_card1);
727  update.push_back(credit_card2);
728  update.push_back(credit_card3);
729  update.push_back(credit_card4);
730  update.push_back(credit_card5);
731  personal_data_->SetCreditCards(&update);
732
733  // Reset the PersonalDataManager.  This tests that the personal data was saved
734  // to the web database, and that we can load the credit cards from the web
735  // database.
736  ResetPersonalDataManager();
737
738  // This will verify that the web database has been loaded and the notification
739  // sent out.
740  EXPECT_CALL(personal_data_observer_,
741              OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
742
743  // The message loop will exit when the mock observer is notified.
744  MessageLoop::current()->Run();
745
746  const std::vector<CreditCard*>& results = personal_data_->credit_cards();
747  ASSERT_EQ(6U, results.size());
748  EXPECT_EQ(ASCIIToUTF16("Home"), results[0]->Label());
749  EXPECT_EQ(ASCIIToUTF16("Home2"), results[1]->Label());
750  EXPECT_EQ(ASCIIToUTF16("Home3"), results[2]->Label());
751  EXPECT_EQ(ASCIIToUTF16("NotHome"), results[3]->Label());
752  EXPECT_EQ(ASCIIToUTF16("Work"), results[4]->Label());
753  EXPECT_EQ(ASCIIToUTF16("Work2"), results[5]->Label());
754}
755
756TEST_F(PersonalDataManagerTest, AggregateTwoDifferentProfiles) {
757  FormData form1;
758  webkit_glue::FormField field;
759  autofill_test::CreateTestFormField(
760      "First name:", "first_name", "George", "text", &field);
761  form1.fields.push_back(field);
762  autofill_test::CreateTestFormField(
763      "Last name:", "last_name", "Washington", "text", &field);
764  form1.fields.push_back(field);
765  autofill_test::CreateTestFormField(
766      "Email:", "email", "theprez@gmail.com", "text", &field);
767  form1.fields.push_back(field);
768  autofill_test::CreateTestFormField(
769      "Address:", "address1", "21 Laussat St", "text", &field);
770  form1.fields.push_back(field);
771  autofill_test::CreateTestFormField(
772      "City:", "city", "San Francisco", "text", &field);
773  form1.fields.push_back(field);
774  autofill_test::CreateTestFormField(
775      "State:", "state", "California", "text", &field);
776  form1.fields.push_back(field);
777  autofill_test::CreateTestFormField(
778      "Zip:", "zip", "94102", "text", &field);
779  form1.fields.push_back(field);
780
781  FormStructure form_structure1(form1);
782  std::vector<const FormStructure*> forms;
783  forms.push_back(&form_structure1);
784  const CreditCard* imported_credit_card;
785  EXPECT_TRUE(personal_data_->ImportFormData(forms, &imported_credit_card));
786  ASSERT_FALSE(imported_credit_card);
787
788  // Wait for the refresh.
789  EXPECT_CALL(personal_data_observer_,
790      OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
791
792  MessageLoop::current()->Run();
793
794  AutoFillProfile expected;
795  autofill_test::SetProfileInfo(&expected, NULL, "George", NULL,
796      "Washington", "theprez@gmail.com", NULL, "21 Laussat St", NULL,
797      "San Francisco", "California", "94102", NULL, NULL, NULL);
798  const std::vector<AutoFillProfile*>& results1 = personal_data_->profiles();
799  ASSERT_EQ(1U, results1.size());
800  EXPECT_EQ(0, expected.Compare(*results1[0]));
801
802  // Now create a completely different profile.
803  FormData form2;
804  autofill_test::CreateTestFormField(
805      "First name:", "first_name", "John", "text", &field);
806  form2.fields.push_back(field);
807  autofill_test::CreateTestFormField(
808      "Last name:", "last_name", "Adams", "text", &field);
809  form2.fields.push_back(field);
810  autofill_test::CreateTestFormField(
811      "Email:", "email", "second@gmail.com", "text", &field);
812  form2.fields.push_back(field);
813  autofill_test::CreateTestFormField(
814      "Address:", "address1", "21 Laussat St", "text", &field);
815  form2.fields.push_back(field);
816  autofill_test::CreateTestFormField(
817      "City:", "city", "San Francisco", "text", &field);
818  form2.fields.push_back(field);
819  autofill_test::CreateTestFormField(
820      "State:", "state", "California", "text", &field);
821  form2.fields.push_back(field);
822  autofill_test::CreateTestFormField(
823      "Zip:", "zip", "94102", "text", &field);
824  form2.fields.push_back(field);
825
826  FormStructure form_structure2(form2);
827  forms.clear();
828  forms.push_back(&form_structure2);
829  EXPECT_TRUE(personal_data_->ImportFormData(forms, &imported_credit_card));
830  ASSERT_FALSE(imported_credit_card);
831
832  // Wait for the refresh.
833  EXPECT_CALL(personal_data_observer_,
834      OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
835
836  MessageLoop::current()->Run();
837
838  const std::vector<AutoFillProfile*>& results2 = personal_data_->profiles();
839
840  AutoFillProfile expected2;
841  autofill_test::SetProfileInfo(&expected2, NULL, "John", NULL,
842      "Adams", "second@gmail.com", NULL, "21 Laussat St", NULL,
843      "San Francisco", "California", "94102", NULL, NULL, NULL);
844  ASSERT_EQ(2U, results2.size());
845  EXPECT_EQ(0, expected.Compare(*results2[0]));
846  EXPECT_EQ(0, expected2.Compare(*results2[1]));
847}
848
849TEST_F(PersonalDataManagerTest, AggregateSameProfileWithConflict) {
850  FormData form1;
851  webkit_glue::FormField field;
852  autofill_test::CreateTestFormField(
853      "First name:", "first_name", "George", "text", &field);
854  form1.fields.push_back(field);
855  autofill_test::CreateTestFormField(
856      "Last name:", "last_name", "Washington", "text", &field);
857  form1.fields.push_back(field);
858  autofill_test::CreateTestFormField(
859      "Address:", "address", "1600 Pennsylvania Avenue", "text", &field);
860  form1.fields.push_back(field);
861  autofill_test::CreateTestFormField(
862      "Address Line 2:", "address2", "Suite A", "text", &field);
863  form1.fields.push_back(field);
864  autofill_test::CreateTestFormField(
865      "City:", "city", "San Francisco", "text", &field);
866  form1.fields.push_back(field);
867  autofill_test::CreateTestFormField(
868      "State:", "state", "California", "text", &field);
869  form1.fields.push_back(field);
870  autofill_test::CreateTestFormField(
871      "Zip:", "zip", "94102", "text", &field);
872  form1.fields.push_back(field);
873  autofill_test::CreateTestFormField(
874      "Email:", "email", "theprez@gmail.com", "text", &field);
875  form1.fields.push_back(field);
876  // Phone gets updated.
877  autofill_test::CreateTestFormField(
878      "Phone:", "phone", "4445556666", "text", &field);
879  form1.fields.push_back(field);
880
881  FormStructure form_structure1(form1);
882  std::vector<const FormStructure*> forms;
883  forms.push_back(&form_structure1);
884  const CreditCard* imported_credit_card;
885  EXPECT_TRUE(personal_data_->ImportFormData(forms, &imported_credit_card));
886  ASSERT_FALSE(imported_credit_card);
887
888  // Wait for the refresh.
889  EXPECT_CALL(personal_data_observer_,
890      OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
891
892  MessageLoop::current()->Run();
893
894  AutoFillProfile expected;
895  autofill_test::SetProfileInfo(&expected, NULL, "George", NULL,
896      "Washington", "theprez@gmail.com", NULL, "1600 Pennsylvania Avenue",
897      "Suite A", "San Francisco", "California", "94102", NULL, "4445556666",
898      NULL);
899  const std::vector<AutoFillProfile*>& results1 = personal_data_->profiles();
900  ASSERT_EQ(1U, results1.size());
901  EXPECT_EQ(0, expected.Compare(*results1[0]));
902
903  // Now create an updated profile.
904  FormData form2;
905  autofill_test::CreateTestFormField(
906      "First name:", "first_name", "George", "text", &field);
907  form2.fields.push_back(field);
908  autofill_test::CreateTestFormField(
909      "Last name:", "last_name", "Washington", "text", &field);
910  form2.fields.push_back(field);
911  autofill_test::CreateTestFormField(
912      "Address:", "address", "1600 Pennsylvania Avenue", "text", &field);
913  form2.fields.push_back(field);
914  autofill_test::CreateTestFormField(
915      "Address Line 2:", "address2", "Suite A", "text", &field);
916  form2.fields.push_back(field);
917  autofill_test::CreateTestFormField(
918      "City:", "city", "San Francisco", "text", &field);
919  form2.fields.push_back(field);
920  autofill_test::CreateTestFormField(
921      "State:", "state", "California", "text", &field);
922  form2.fields.push_back(field);
923  autofill_test::CreateTestFormField(
924      "Zip:", "zip", "94102", "text", &field);
925  form2.fields.push_back(field);
926  autofill_test::CreateTestFormField(
927      "Email:", "email", "theprez@gmail.com", "text", &field);
928  form2.fields.push_back(field);
929  // Country gets added.
930  autofill_test::CreateTestFormField(
931      "Country:", "country", "USA", "text", &field);
932  form2.fields.push_back(field);
933  // Phone gets updated.
934  autofill_test::CreateTestFormField(
935      "Phone:", "phone", "1231231234", "text", &field);
936  form2.fields.push_back(field);
937
938  FormStructure form_structure2(form2);
939  forms.clear();
940  forms.push_back(&form_structure2);
941  EXPECT_TRUE(personal_data_->ImportFormData(forms, &imported_credit_card));
942  ASSERT_FALSE(imported_credit_card);
943
944  // Wait for the refresh.
945  EXPECT_CALL(personal_data_observer_,
946      OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
947
948  MessageLoop::current()->Run();
949
950  const std::vector<AutoFillProfile*>& results2 = personal_data_->profiles();
951
952  AutoFillProfile expected2;
953  autofill_test::SetProfileInfo(&expected2, NULL, "George", NULL,
954      "Washington", "theprez@gmail.com", NULL, "1600 Pennsylvania Avenue",
955      "Suite A", "San Francisco", "California", "94102", "USA", "1231231234",
956      NULL);
957  ASSERT_EQ(1U, results2.size());
958  EXPECT_EQ(0, expected2.Compare(*results2[0]));
959}
960
961TEST_F(PersonalDataManagerTest, AggregateProfileWithMissingInfoInOld) {
962  FormData form1;
963  webkit_glue::FormField field;
964  autofill_test::CreateTestFormField(
965      "First name:", "first_name", "George", "text", &field);
966  form1.fields.push_back(field);
967  autofill_test::CreateTestFormField(
968      "Last name:", "last_name", "Washington", "text", &field);
969  form1.fields.push_back(field);
970  autofill_test::CreateTestFormField(
971      "Address Line 1:", "address", "190 High Street", "text", &field);
972  form1.fields.push_back(field);
973  autofill_test::CreateTestFormField(
974      "City:", "city", "Philadelphia", "text", &field);
975  form1.fields.push_back(field);
976  autofill_test::CreateTestFormField(
977      "State:", "state", "Pennsylvania", "text", &field);
978  form1.fields.push_back(field);
979  autofill_test::CreateTestFormField(
980      "Zip:", "zipcode", "19106", "text", &field);
981  form1.fields.push_back(field);
982
983  FormStructure form_structure1(form1);
984  std::vector<const FormStructure*> forms;
985  forms.push_back(&form_structure1);
986  const CreditCard* imported_credit_card;
987  EXPECT_TRUE(personal_data_->ImportFormData(forms, &imported_credit_card));
988  ASSERT_FALSE(imported_credit_card);
989
990  // Wait for the refresh.
991  EXPECT_CALL(personal_data_observer_,
992      OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
993
994  MessageLoop::current()->Run();
995
996  AutoFillProfile expected;
997  autofill_test::SetProfileInfo(&expected, NULL, "George", NULL,
998      "Washington", NULL, NULL, "190 High Street", NULL,
999      "Philadelphia", "Pennsylvania", "19106", NULL, NULL, NULL);
1000  const std::vector<AutoFillProfile*>& results1 = personal_data_->profiles();
1001  ASSERT_EQ(1U, results1.size());
1002  EXPECT_EQ(0, expected.Compare(*results1[0]));
1003
1004  // Submit a form with new data for the first profile.
1005  FormData form2;
1006  autofill_test::CreateTestFormField(
1007      "First name:", "first_name", "George", "text", &field);
1008  form2.fields.push_back(field);
1009  autofill_test::CreateTestFormField(
1010      "Last name:", "last_name", "Washington", "text", &field);
1011  form2.fields.push_back(field);
1012  autofill_test::CreateTestFormField(
1013      "Email:", "email", "theprez@gmail.com", "text", &field);
1014  form2.fields.push_back(field);
1015  autofill_test::CreateTestFormField(
1016      "Address Line 1:", "address", "190 High Street", "text", &field);
1017  form2.fields.push_back(field);
1018  autofill_test::CreateTestFormField(
1019      "City:", "city", "Philadelphia", "text", &field);
1020  form2.fields.push_back(field);
1021  autofill_test::CreateTestFormField(
1022      "State:", "state", "Pennsylvania", "text", &field);
1023  form2.fields.push_back(field);
1024  autofill_test::CreateTestFormField(
1025      "Zip:", "zipcode", "19106", "text", &field);
1026  form2.fields.push_back(field);
1027
1028  FormStructure form_structure2(form2);
1029  forms.clear();
1030  forms.push_back(&form_structure2);
1031  EXPECT_TRUE(personal_data_->ImportFormData(forms, &imported_credit_card));
1032  ASSERT_FALSE(imported_credit_card);
1033
1034  // Wait for the refresh.
1035  EXPECT_CALL(personal_data_observer_,
1036      OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
1037
1038  MessageLoop::current()->Run();
1039
1040  const std::vector<AutoFillProfile*>& results2 = personal_data_->profiles();
1041
1042  AutoFillProfile expected2;
1043  autofill_test::SetProfileInfo(&expected2, NULL, "George", NULL,
1044      "Washington", "theprez@gmail.com", NULL, "190 High Street", NULL,
1045      "Philadelphia", "Pennsylvania", "19106", NULL, NULL, NULL);
1046  ASSERT_EQ(1U, results2.size());
1047  EXPECT_EQ(0, expected2.Compare(*results2[0]));
1048}
1049
1050TEST_F(PersonalDataManagerTest, AggregateProfileWithMissingInfoInNew) {
1051  FormData form1;
1052  webkit_glue::FormField field;
1053  autofill_test::CreateTestFormField(
1054      "First name:", "first_name", "George", "text", &field);
1055  form1.fields.push_back(field);
1056  autofill_test::CreateTestFormField(
1057      "Last name:", "last_name", "Washington", "text", &field);
1058  form1.fields.push_back(field);
1059  autofill_test::CreateTestFormField(
1060      "Company:", "company", "Government", "text", &field);
1061  form1.fields.push_back(field);
1062  autofill_test::CreateTestFormField(
1063      "Email:", "email", "theprez@gmail.com", "text", &field);
1064  form1.fields.push_back(field);
1065  autofill_test::CreateTestFormField(
1066      "Address Line 1:", "address", "190 High Street", "text", &field);
1067  form1.fields.push_back(field);
1068  autofill_test::CreateTestFormField(
1069      "City:", "city", "Philadelphia", "text", &field);
1070  form1.fields.push_back(field);
1071  autofill_test::CreateTestFormField(
1072      "State:", "state", "Pennsylvania", "text", &field);
1073  form1.fields.push_back(field);
1074  autofill_test::CreateTestFormField(
1075      "Zip:", "zipcode", "19106", "text", &field);
1076  form1.fields.push_back(field);
1077
1078  FormStructure form_structure1(form1);
1079  std::vector<const FormStructure*> forms;
1080  forms.push_back(&form_structure1);
1081  const CreditCard* imported_credit_card;
1082  EXPECT_TRUE(personal_data_->ImportFormData(forms, &imported_credit_card));
1083  ASSERT_FALSE(imported_credit_card);
1084
1085  // Wait for the refresh.
1086  EXPECT_CALL(personal_data_observer_,
1087      OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
1088
1089  MessageLoop::current()->Run();
1090
1091  AutoFillProfile expected;
1092  autofill_test::SetProfileInfo(&expected, NULL, "George", NULL,
1093      "Washington", "theprez@gmail.com", "Government", "190 High Street", NULL,
1094      "Philadelphia", "Pennsylvania", "19106", NULL, NULL, NULL);
1095  const std::vector<AutoFillProfile*>& results1 = personal_data_->profiles();
1096  ASSERT_EQ(1U, results1.size());
1097  EXPECT_EQ(0, expected.Compare(*results1[0]));
1098
1099  // Submit a form with new data for the first profile.
1100  FormData form2;
1101  autofill_test::CreateTestFormField(
1102      "First name:", "first_name", "George", "text", &field);
1103  form2.fields.push_back(field);
1104  autofill_test::CreateTestFormField(
1105      "Last name:", "last_name", "Washington", "text", &field);
1106  form2.fields.push_back(field);
1107  // Note missing Company field.
1108  autofill_test::CreateTestFormField(
1109      "Email:", "email", "theprez@gmail.com", "text", &field);
1110  form2.fields.push_back(field);
1111  autofill_test::CreateTestFormField(
1112      "Address Line 1:", "address", "190 High Street", "text", &field);
1113  form2.fields.push_back(field);
1114  autofill_test::CreateTestFormField(
1115      "City:", "city", "Philadelphia", "text", &field);
1116  form2.fields.push_back(field);
1117  autofill_test::CreateTestFormField(
1118      "State:", "state", "Pennsylvania", "text", &field);
1119  form2.fields.push_back(field);
1120  autofill_test::CreateTestFormField(
1121      "Zip:", "zipcode", "19106", "text", &field);
1122  form2.fields.push_back(field);
1123
1124  FormStructure form_structure2(form2);
1125  forms.clear();
1126  forms.push_back(&form_structure2);
1127  EXPECT_TRUE(personal_data_->ImportFormData(forms, &imported_credit_card));
1128  ASSERT_FALSE(imported_credit_card);
1129
1130  // Wait for the refresh.
1131  EXPECT_CALL(personal_data_observer_,
1132      OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
1133
1134  MessageLoop::current()->Run();
1135
1136  const std::vector<AutoFillProfile*>& results2 = personal_data_->profiles();
1137
1138  // Expect no change.
1139  ASSERT_EQ(1U, results2.size());
1140  EXPECT_EQ(0, expected.Compare(*results2[0]));
1141}
1142
1143TEST_F(PersonalDataManagerTest, AggregateProfileWithInsufficientAddress) {
1144  FormData form1;
1145  webkit_glue::FormField field;
1146  autofill_test::CreateTestFormField(
1147      "First name:", "first_name", "George", "text", &field);
1148  form1.fields.push_back(field);
1149  autofill_test::CreateTestFormField(
1150      "Last name:", "last_name", "Washington", "text", &field);
1151  form1.fields.push_back(field);
1152  autofill_test::CreateTestFormField(
1153      "Company:", "company", "Government", "text", &field);
1154  form1.fields.push_back(field);
1155  autofill_test::CreateTestFormField(
1156      "Email:", "email", "theprez@gmail.com", "text", &field);
1157  form1.fields.push_back(field);
1158  autofill_test::CreateTestFormField(
1159      "Address Line 1:", "address", "190 High Street", "text", &field);
1160  form1.fields.push_back(field);
1161  autofill_test::CreateTestFormField(
1162      "City:", "city", "Philadelphia", "text", &field);
1163  form1.fields.push_back(field);
1164
1165  FormStructure form_structure1(form1);
1166  std::vector<const FormStructure*> forms;
1167  forms.push_back(&form_structure1);
1168  const CreditCard* imported_credit_card;
1169  EXPECT_FALSE(personal_data_->ImportFormData(forms, &imported_credit_card));
1170  ASSERT_FALSE(imported_credit_card);
1171
1172  // Wait for the refresh.
1173  EXPECT_CALL(personal_data_observer_,
1174      OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
1175
1176  MessageLoop::current()->Run();
1177
1178  const std::vector<AutoFillProfile*>& profiles = personal_data_->profiles();
1179  ASSERT_EQ(0U, profiles.size());
1180  const std::vector<CreditCard*>& credit_cards = personal_data_->credit_cards();
1181  ASSERT_EQ(0U, credit_cards.size());
1182}
1183
1184
1185TEST_F(PersonalDataManagerTest, AggregateTwoDifferentCreditCards) {
1186  FormData form1;
1187
1188  // Start with a single valid credit card form.
1189  webkit_glue::FormField field;
1190  autofill_test::CreateTestFormField(
1191      "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1192  form1.fields.push_back(field);
1193  autofill_test::CreateTestFormField(
1194      "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
1195  form1.fields.push_back(field);
1196  autofill_test::CreateTestFormField(
1197      "Exp Month:", "exp_month", "01", "text", &field);
1198  form1.fields.push_back(field);
1199  autofill_test::CreateTestFormField(
1200      "Exp Year:", "exp_year", "2011", "text", &field);
1201  form1.fields.push_back(field);
1202
1203  FormStructure form_structure1(form1);
1204  std::vector<const FormStructure*> forms;
1205  forms.push_back(&form_structure1);
1206  const CreditCard* imported_credit_card;
1207  EXPECT_TRUE(personal_data_->ImportFormData(forms, &imported_credit_card));
1208  ASSERT_TRUE(imported_credit_card);
1209  personal_data_->SaveImportedCreditCard(*imported_credit_card);
1210  delete imported_credit_card;
1211
1212  // Wait for the refresh.
1213  EXPECT_CALL(personal_data_observer_,
1214      OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
1215
1216  MessageLoop::current()->Run();
1217
1218  CreditCard expected;
1219  autofill_test::SetCreditCardInfo(&expected,
1220      "L1", "Biggie Smalls", "4111111111111111", "01", "2011");
1221  const std::vector<CreditCard*>& results = personal_data_->credit_cards();
1222  ASSERT_EQ(1U, results.size());
1223  EXPECT_EQ(0, expected.Compare(*results[0]));
1224
1225  // Add a second different valid credit card.
1226  FormData form2;
1227  autofill_test::CreateTestFormField(
1228      "Name on card:", "name_on_card", "Jim Johansen", "text", &field);
1229  form2.fields.push_back(field);
1230  autofill_test::CreateTestFormField(
1231      "Card Number:", "card_number", "5500 0000 0000 0004", "text", &field);
1232  form2.fields.push_back(field);
1233  autofill_test::CreateTestFormField(
1234      "Exp Month:", "exp_month", "02", "text", &field);
1235  form2.fields.push_back(field);
1236  autofill_test::CreateTestFormField(
1237      "Exp Year:", "exp_year", "2012", "text", &field);
1238  form2.fields.push_back(field);
1239
1240  FormStructure form_structure2(form2);
1241  forms.clear();
1242  forms.push_back(&form_structure2);
1243  EXPECT_TRUE(personal_data_->ImportFormData(forms, &imported_credit_card));
1244  ASSERT_TRUE(imported_credit_card);
1245  personal_data_->SaveImportedCreditCard(*imported_credit_card);
1246  delete imported_credit_card;
1247
1248  // Wait for the refresh.
1249  EXPECT_CALL(personal_data_observer_,
1250      OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
1251
1252  MessageLoop::current()->Run();
1253
1254  CreditCard expected2;
1255  autofill_test::SetCreditCardInfo(&expected2,
1256      "L2", "Jim Johansen", "5500000000000004", "02", "2012");
1257  const std::vector<CreditCard*>& results2 = personal_data_->credit_cards();
1258  ASSERT_EQ(2U, results2.size());
1259  EXPECT_EQ(0, expected.Compare(*results2[0]));
1260  EXPECT_EQ(0, expected2.Compare(*results2[1]));
1261}
1262
1263TEST_F(PersonalDataManagerTest, AggregateInvalidCreditCard) {
1264  FormData form1;
1265
1266  // Start with a single valid credit card form.
1267  webkit_glue::FormField field;
1268  autofill_test::CreateTestFormField(
1269      "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1270  form1.fields.push_back(field);
1271  autofill_test::CreateTestFormField(
1272      "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
1273  form1.fields.push_back(field);
1274  autofill_test::CreateTestFormField(
1275      "Exp Month:", "exp_month", "01", "text", &field);
1276  form1.fields.push_back(field);
1277  autofill_test::CreateTestFormField(
1278      "Exp Year:", "exp_year", "2011", "text", &field);
1279  form1.fields.push_back(field);
1280
1281  FormStructure form_structure1(form1);
1282  std::vector<const FormStructure*> forms;
1283  forms.push_back(&form_structure1);
1284  const CreditCard* imported_credit_card;
1285  EXPECT_TRUE(personal_data_->ImportFormData(forms, &imported_credit_card));
1286  ASSERT_TRUE(imported_credit_card);
1287  personal_data_->SaveImportedCreditCard(*imported_credit_card);
1288  delete imported_credit_card;
1289
1290  // Wait for the refresh.
1291  EXPECT_CALL(personal_data_observer_,
1292      OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
1293
1294  MessageLoop::current()->Run();
1295
1296  CreditCard expected;
1297  autofill_test::SetCreditCardInfo(&expected,
1298      "L1", "Biggie Smalls", "4111111111111111", "01", "2011");
1299  const std::vector<CreditCard*>& results = personal_data_->credit_cards();
1300  ASSERT_EQ(1U, results.size());
1301  EXPECT_EQ(0, expected.Compare(*results[0]));
1302
1303  // Add a second different invalid credit card.
1304  FormData form2;
1305  autofill_test::CreateTestFormField(
1306      "Name on card:", "name_on_card", "Jim Johansen", "text", &field);
1307  form2.fields.push_back(field);
1308  autofill_test::CreateTestFormField(
1309      "Card Number:", "card_number", "1000000000000000", "text", &field);
1310  form2.fields.push_back(field);
1311  autofill_test::CreateTestFormField(
1312      "Exp Month:", "exp_month", "02", "text", &field);
1313  form2.fields.push_back(field);
1314  autofill_test::CreateTestFormField(
1315      "Exp Year:", "exp_year", "2012", "text", &field);
1316  form2.fields.push_back(field);
1317
1318  FormStructure form_structure2(form2);
1319  forms.clear();
1320  forms.push_back(&form_structure2);
1321  EXPECT_FALSE(personal_data_->ImportFormData(forms, &imported_credit_card));
1322  ASSERT_FALSE(imported_credit_card);
1323
1324  // Note: no refresh here.
1325
1326  const std::vector<CreditCard*>& results2 = personal_data_->credit_cards();
1327  ASSERT_EQ(1U, results2.size());
1328  EXPECT_EQ(0, expected.Compare(*results2[0]));
1329}
1330
1331TEST_F(PersonalDataManagerTest, AggregateSameCreditCardWithConflict) {
1332  FormData form1;
1333
1334  // Start with a single valid credit card form.
1335  webkit_glue::FormField field;
1336  autofill_test::CreateTestFormField(
1337      "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1338  form1.fields.push_back(field);
1339  autofill_test::CreateTestFormField(
1340      "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
1341  form1.fields.push_back(field);
1342  autofill_test::CreateTestFormField(
1343      "Exp Month:", "exp_month", "01", "text", &field);
1344  form1.fields.push_back(field);
1345  autofill_test::CreateTestFormField(
1346      "Exp Year:", "exp_year", "2011", "text", &field);
1347  form1.fields.push_back(field);
1348
1349  FormStructure form_structure1(form1);
1350  std::vector<const FormStructure*> forms;
1351  forms.push_back(&form_structure1);
1352  const CreditCard* imported_credit_card;
1353  EXPECT_TRUE(personal_data_->ImportFormData(forms, &imported_credit_card));
1354  ASSERT_TRUE(imported_credit_card);
1355  personal_data_->SaveImportedCreditCard(*imported_credit_card);
1356  delete imported_credit_card;
1357
1358  // Wait for the refresh.
1359  EXPECT_CALL(personal_data_observer_,
1360      OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
1361
1362  MessageLoop::current()->Run();
1363
1364  CreditCard expected;
1365  autofill_test::SetCreditCardInfo(&expected,
1366      "L1", "Biggie Smalls", "4111111111111111", "01", "2011");
1367  const std::vector<CreditCard*>& results = personal_data_->credit_cards();
1368  ASSERT_EQ(1U, results.size());
1369  EXPECT_EQ(0, expected.Compare(*results[0]));
1370
1371  // Add a second different valid credit card where the year is different but
1372  // the credit card number matches.
1373  FormData form2;
1374  autofill_test::CreateTestFormField(
1375      "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1376  form2.fields.push_back(field);
1377  autofill_test::CreateTestFormField(
1378      "Card Number:", "card_number", "4111 1111 1111 1111", "text", &field);
1379  form2.fields.push_back(field);
1380  autofill_test::CreateTestFormField(
1381      "Exp Month:", "exp_month", "01", "text", &field);
1382  form2.fields.push_back(field);
1383  autofill_test::CreateTestFormField(
1384      "Exp Year:", "exp_year", "2012", "text", &field);
1385  form2.fields.push_back(field);
1386
1387  FormStructure form_structure2(form2);
1388  forms.clear();
1389  forms.push_back(&form_structure2);
1390  EXPECT_TRUE(personal_data_->ImportFormData(forms, &imported_credit_card));
1391  ASSERT_TRUE(imported_credit_card);
1392  personal_data_->SaveImportedCreditCard(*imported_credit_card);
1393  delete imported_credit_card;
1394
1395  // Wait for the refresh.
1396  EXPECT_CALL(personal_data_observer_,
1397      OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
1398
1399  MessageLoop::current()->Run();
1400
1401  // Expect that the newer information is saved.  In this case the year is
1402  // updated to "2012".
1403  CreditCard expected2;
1404  autofill_test::SetCreditCardInfo(&expected2,
1405      "L1", "Biggie Smalls", "4111111111111111", "01", "2012");
1406  const std::vector<CreditCard*>& results2 = personal_data_->credit_cards();
1407  ASSERT_EQ(1U, results2.size());
1408  EXPECT_EQ(0, expected2.Compare(*results2[0]));
1409}
1410
1411TEST_F(PersonalDataManagerTest, AggregateEmptyCreditCardWithConflict) {
1412  FormData form1;
1413
1414  // Start with a single valid credit card form.
1415  webkit_glue::FormField field;
1416  autofill_test::CreateTestFormField(
1417      "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1418  form1.fields.push_back(field);
1419  autofill_test::CreateTestFormField(
1420      "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
1421  form1.fields.push_back(field);
1422  autofill_test::CreateTestFormField(
1423      "Exp Month:", "exp_month", "01", "text", &field);
1424  form1.fields.push_back(field);
1425  autofill_test::CreateTestFormField(
1426      "Exp Year:", "exp_year", "2011", "text", &field);
1427  form1.fields.push_back(field);
1428
1429  FormStructure form_structure1(form1);
1430  std::vector<const FormStructure*> forms;
1431  forms.push_back(&form_structure1);
1432  const CreditCard* imported_credit_card;
1433  EXPECT_TRUE(personal_data_->ImportFormData(forms, &imported_credit_card));
1434  ASSERT_TRUE(imported_credit_card);
1435  personal_data_->SaveImportedCreditCard(*imported_credit_card);
1436  delete imported_credit_card;
1437
1438  // Wait for the refresh.
1439  EXPECT_CALL(personal_data_observer_,
1440      OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
1441
1442  MessageLoop::current()->Run();
1443
1444  CreditCard expected;
1445  autofill_test::SetCreditCardInfo(&expected,
1446      "L1", "Biggie Smalls", "4111111111111111", "01", "2011");
1447  const std::vector<CreditCard*>& results = personal_data_->credit_cards();
1448  ASSERT_EQ(1U, results.size());
1449  EXPECT_EQ(0, expected.Compare(*results[0]));
1450
1451  // Add a second credit card with no number.
1452  FormData form2;
1453  autofill_test::CreateTestFormField(
1454      "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1455  form2.fields.push_back(field);
1456  autofill_test::CreateTestFormField(
1457      "Exp Month:", "exp_month", "01", "text", &field);
1458  form2.fields.push_back(field);
1459  autofill_test::CreateTestFormField(
1460      "Exp Year:", "exp_year", "2012", "text", &field);
1461  form2.fields.push_back(field);
1462
1463  FormStructure form_structure2(form2);
1464  forms.clear();
1465  forms.push_back(&form_structure2);
1466  EXPECT_FALSE(personal_data_->ImportFormData(forms, &imported_credit_card));
1467  ASSERT_FALSE(imported_credit_card);
1468
1469  // Note: no refresh here.
1470
1471  // No change is expected.
1472  CreditCard expected2;
1473  autofill_test::SetCreditCardInfo(&expected2,
1474      "L1", "Biggie Smalls", "4111111111111111", "01", "2011");
1475  const std::vector<CreditCard*>& results2 = personal_data_->credit_cards();
1476  ASSERT_EQ(1U, results2.size());
1477  EXPECT_EQ(0, expected2.Compare(*results2[0]));
1478}
1479
1480TEST_F(PersonalDataManagerTest, AggregateCreditCardWithMissingInfoInNew) {
1481  FormData form1;
1482
1483  // Start with a single valid credit card form.
1484  webkit_glue::FormField field;
1485  autofill_test::CreateTestFormField(
1486      "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1487  form1.fields.push_back(field);
1488  autofill_test::CreateTestFormField(
1489      "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
1490  form1.fields.push_back(field);
1491  autofill_test::CreateTestFormField(
1492      "Exp Month:", "exp_month", "01", "text", &field);
1493  form1.fields.push_back(field);
1494  autofill_test::CreateTestFormField(
1495      "Exp Year:", "exp_year", "2011", "text", &field);
1496  form1.fields.push_back(field);
1497
1498  FormStructure form_structure1(form1);
1499  std::vector<const FormStructure*> forms;
1500  forms.push_back(&form_structure1);
1501  const CreditCard* imported_credit_card;
1502  EXPECT_TRUE(personal_data_->ImportFormData(forms, &imported_credit_card));
1503  ASSERT_TRUE(imported_credit_card);
1504  personal_data_->SaveImportedCreditCard(*imported_credit_card);
1505  delete imported_credit_card;
1506
1507  // Wait for the refresh.
1508  EXPECT_CALL(personal_data_observer_,
1509      OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
1510
1511  MessageLoop::current()->Run();
1512
1513  CreditCard expected;
1514  autofill_test::SetCreditCardInfo(&expected,
1515      "L1", "Biggie Smalls", "4111111111111111", "01", "2011");
1516  const std::vector<CreditCard*>& results = personal_data_->credit_cards();
1517  ASSERT_EQ(1U, results.size());
1518  EXPECT_EQ(0, expected.Compare(*results[0]));
1519
1520  // Add a second different valid credit card where the name is missing but
1521  // the credit card number matches.
1522  FormData form2;
1523  // Note missing name.
1524  autofill_test::CreateTestFormField(
1525      "Card Number:", "card_number", "4111111111111111", "text", &field);
1526  form2.fields.push_back(field);
1527  autofill_test::CreateTestFormField(
1528      "Exp Month:", "exp_month", "01", "text", &field);
1529  form2.fields.push_back(field);
1530  autofill_test::CreateTestFormField(
1531      "Exp Year:", "exp_year", "2011", "text", &field);
1532  form2.fields.push_back(field);
1533
1534  FormStructure form_structure2(form2);
1535  forms.clear();
1536  forms.push_back(&form_structure2);
1537  EXPECT_FALSE(personal_data_->ImportFormData(forms, &imported_credit_card));
1538  ASSERT_FALSE(imported_credit_card);
1539
1540  // Note: no refresh here.
1541
1542  // No change is expected.
1543  CreditCard expected2;
1544  autofill_test::SetCreditCardInfo(&expected2,
1545      "L1", "Biggie Smalls", "4111111111111111", "01", "2011");
1546  const std::vector<CreditCard*>& results2 = personal_data_->credit_cards();
1547  ASSERT_EQ(1U, results2.size());
1548  EXPECT_EQ(0, expected2.Compare(*results2[0]));
1549}
1550
1551TEST_F(PersonalDataManagerTest, AggregateCreditCardWithMissingInfoInOld) {
1552  FormData form1;
1553
1554  // Start with a single valid credit card form.
1555  webkit_glue::FormField field;
1556  // Note missing name.
1557  autofill_test::CreateTestFormField(
1558      "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
1559  form1.fields.push_back(field);
1560  autofill_test::CreateTestFormField(
1561      "Exp Month:", "exp_month", "01", "text", &field);
1562  form1.fields.push_back(field);
1563  autofill_test::CreateTestFormField(
1564      "Exp Year:", "exp_year", "2011", "text", &field);
1565  form1.fields.push_back(field);
1566
1567  FormStructure form_structure1(form1);
1568  std::vector<const FormStructure*> forms;
1569  forms.push_back(&form_structure1);
1570  const CreditCard* imported_credit_card;
1571  EXPECT_TRUE(personal_data_->ImportFormData(forms, &imported_credit_card));
1572  ASSERT_TRUE(imported_credit_card);
1573  personal_data_->SaveImportedCreditCard(*imported_credit_card);
1574  delete imported_credit_card;
1575
1576  // Wait for the refresh.
1577  EXPECT_CALL(personal_data_observer_,
1578      OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
1579
1580  MessageLoop::current()->Run();
1581
1582  CreditCard expected;
1583  autofill_test::SetCreditCardInfo(&expected,
1584      "L1", NULL, "4111111111111111", "01", "2011");
1585  const std::vector<CreditCard*>& results = personal_data_->credit_cards();
1586  ASSERT_EQ(1U, results.size());
1587  EXPECT_EQ(0, expected.Compare(*results[0]));
1588
1589  // Add a second different valid credit card where the year is different but
1590  // the credit card number matches.
1591  FormData form2;
1592  autofill_test::CreateTestFormField(
1593      "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1594  form2.fields.push_back(field);
1595  autofill_test::CreateTestFormField(
1596      "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
1597  form2.fields.push_back(field);
1598  autofill_test::CreateTestFormField(
1599      "Exp Month:", "exp_month", "01", "text", &field);
1600  form2.fields.push_back(field);
1601  autofill_test::CreateTestFormField(
1602      "Exp Year:", "exp_year", "2011", "text", &field);
1603  form2.fields.push_back(field);
1604
1605  FormStructure form_structure2(form2);
1606  forms.clear();
1607  forms.push_back(&form_structure2);
1608  EXPECT_TRUE(personal_data_->ImportFormData(forms, &imported_credit_card));
1609  ASSERT_TRUE(imported_credit_card);
1610  personal_data_->SaveImportedCreditCard(*imported_credit_card);
1611  delete imported_credit_card;
1612
1613  // Wait for the refresh.
1614  EXPECT_CALL(personal_data_observer_,
1615      OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
1616
1617  MessageLoop::current()->Run();
1618
1619  // Expect that the newer information is saved.  In this case the year is
1620  // added to the existing credit card.
1621  CreditCard expected2;
1622  autofill_test::SetCreditCardInfo(&expected2,
1623      "L1", "Biggie Smalls", "4111111111111111", "01", "2011");
1624  const std::vector<CreditCard*>& results2 = personal_data_->credit_cards();
1625  ASSERT_EQ(1U, results2.size());
1626  EXPECT_EQ(0, expected2.Compare(*results2[0]));
1627}
1628