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