Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(181)

Side by Side Diff: chrome/browser/autofill/personal_data_manager_unittest.cc

Issue 12434004: Move remaining Autofill code to //components/autofill. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix long lines Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <string>
6
7 #include "base/basictypes.h"
8 #include "base/guid.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop.h"
11 #include "base/utf_string_conversions.h"
12 #include "chrome/browser/autofill/autofill_common_test.h"
13 #include "chrome/browser/autofill/autofill_metrics.h"
14 #include "chrome/browser/autofill/autofill_profile.h"
15 #include "chrome/browser/autofill/form_structure.h"
16 #include "chrome/browser/autofill/personal_data_manager.h"
17 #include "chrome/browser/autofill/personal_data_manager_observer.h"
18 #include "chrome/browser/password_manager/encryptor.h"
19 #include "chrome/browser/webdata/web_data_service.h"
20 #include "chrome/browser/webdata/web_data_service_factory.h"
21 #include "chrome/test/base/testing_browser_process.h"
22 #include "chrome/test/base/testing_profile.h"
23 #include "components/autofill/common/form_data.h"
24 #include "content/public/browser/notification_details.h"
25 #include "content/public/browser/notification_registrar.h"
26 #include "content/public/browser/notification_source.h"
27 #include "content/public/browser/notification_types.h"
28 #include "content/public/test/mock_notification_observer.h"
29 #include "content/public/test/test_browser_thread.h"
30 #include "testing/gmock/include/gmock/gmock.h"
31 #include "testing/gtest/include/gtest/gtest.h"
32
33 using content::BrowserThread;
34
35 namespace {
36
37 ACTION(QuitUIMessageLoop) {
38 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
39 MessageLoop::current()->Quit();
40 }
41
42 class PersonalDataLoadedObserverMock : public PersonalDataManagerObserver {
43 public:
44 PersonalDataLoadedObserverMock() {}
45 virtual ~PersonalDataLoadedObserverMock() {}
46
47 MOCK_METHOD0(OnPersonalDataChanged, void());
48 };
49
50 // Unlike the base AutofillMetrics, exposes copy and assignment constructors,
51 // which are handy for briefer test code. The AutofillMetrics class is
52 // stateless, so this is safe.
53 class TestAutofillMetrics : public AutofillMetrics {
54 public:
55 TestAutofillMetrics() {}
56 virtual ~TestAutofillMetrics() {}
57 };
58
59 } // anonymous namespace
60
61 class PersonalDataManagerTest : public testing::Test {
62 protected:
63 PersonalDataManagerTest()
64 : ui_thread_(BrowserThread::UI, &message_loop_),
65 db_thread_(BrowserThread::DB) {
66 }
67
68 virtual void SetUp() {
69 db_thread_.Start();
70
71 profile_.reset(new TestingProfile);
72 profile_->CreateWebDatabaseService();
73 profile_->CreateWebDataService();
74
75 autofill_test::DisableSystemServices(profile_.get());
76 ResetPersonalDataManager();
77 }
78
79 virtual void TearDown() {
80 // Destruction order is imposed explicitly here.
81 personal_data_.reset(NULL);
82 profile_.reset(NULL);
83
84 db_thread_.Stop();
85 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
86 MessageLoop::current()->Run();
87 }
88
89 void ResetPersonalDataManager() {
90 personal_data_.reset(new PersonalDataManager);
91 personal_data_->Init(profile_.get());
92 personal_data_->AddObserver(&personal_data_observer_);
93
94 // Verify that the web database has been updated and the notification sent.
95 EXPECT_CALL(personal_data_observer_,
96 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
97 MessageLoop::current()->Run();
98 }
99
100 MessageLoopForUI message_loop_;
101 content::TestBrowserThread ui_thread_;
102 content::TestBrowserThread db_thread_;
103 scoped_ptr<TestingProfile> profile_;
104 scoped_ptr<PersonalDataManager> personal_data_;
105 content::NotificationRegistrar registrar_;
106 content::MockNotificationObserver observer_;
107 PersonalDataLoadedObserverMock personal_data_observer_;
108 };
109
110 TEST_F(PersonalDataManagerTest, AddProfile) {
111 AutofillProfile profile0;
112 autofill_test::SetProfileInfo(&profile0,
113 "John", "Mitchell", "Smith",
114 "j@s.com", "Acme Inc.", "1 Main", "Apt A", "San Francisco", "CA",
115 "94102", "US", "4158889999");
116
117 // Add profile0 to the database.
118 personal_data_->AddProfile(profile0);
119
120 // Reload the database.
121 ResetPersonalDataManager();
122
123 // Verify the addition.
124 const std::vector<AutofillProfile*>& results1 = personal_data_->GetProfiles();
125 ASSERT_EQ(1U, results1.size());
126 EXPECT_EQ(0, profile0.Compare(*results1[0]));
127
128 // Add profile with identical values. Duplicates should not get saved.
129 AutofillProfile profile0a = profile0;
130 profile0a.set_guid(base::GenerateGUID());
131 personal_data_->AddProfile(profile0a);
132
133 // Reload the database.
134 ResetPersonalDataManager();
135
136 // Verify the non-addition.
137 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
138 ASSERT_EQ(1U, results2.size());
139 EXPECT_EQ(0, profile0.Compare(*results2[0]));
140
141 // New profile with different email.
142 AutofillProfile profile1 = profile0;
143 profile1.set_guid(base::GenerateGUID());
144 profile1.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("john@smith.com"));
145
146 // Add the different profile. This should save as a separate profile.
147 // Note that if this same profile was "merged" it would collapse to one
148 // profile with a multi-valued entry for email.
149 personal_data_->AddProfile(profile1);
150
151 // Reload the database.
152 ResetPersonalDataManager();
153
154 // Verify the addition.
155 const std::vector<AutofillProfile*>& results3 = personal_data_->GetProfiles();
156 ASSERT_EQ(2U, results3.size());
157 EXPECT_EQ(0, profile0.Compare(*results3[0]));
158 EXPECT_EQ(0, profile1.Compare(*results3[1]));
159 }
160
161 TEST_F(PersonalDataManagerTest, AddUpdateRemoveProfiles) {
162 AutofillProfile profile0;
163 autofill_test::SetProfileInfo(&profile0,
164 "Marion", "Mitchell", "Morrison",
165 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
166 "91601", "US", "12345678910");
167
168 AutofillProfile profile1;
169 autofill_test::SetProfileInfo(&profile1,
170 "Josephine", "Alicia", "Saenz",
171 "joewayne@me.xyz", "Fox", "903 Apple Ct.", NULL, "Orlando", "FL", "32801",
172 "US", "19482937549");
173
174 AutofillProfile profile2;
175 autofill_test::SetProfileInfo(&profile2,
176 "Josephine", "Alicia", "Saenz",
177 "joewayne@me.xyz", "Fox", "1212 Center.", "Bld. 5", "Orlando", "FL",
178 "32801", "US", "19482937549");
179
180 // Add two test profiles to the database.
181 personal_data_->AddProfile(profile0);
182 personal_data_->AddProfile(profile1);
183
184 // Verify that the web database has been updated and the notification sent.
185 EXPECT_CALL(personal_data_observer_,
186 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
187 MessageLoop::current()->Run();
188
189 const std::vector<AutofillProfile*>& results1 = personal_data_->GetProfiles();
190 ASSERT_EQ(2U, results1.size());
191 EXPECT_EQ(0, profile0.Compare(*results1[0]));
192 EXPECT_EQ(0, profile1.Compare(*results1[1]));
193
194 // Update, remove, and add.
195 profile0.SetRawInfo(NAME_FIRST, ASCIIToUTF16("John"));
196 personal_data_->UpdateProfile(profile0);
197 personal_data_->RemoveByGUID(profile1.guid());
198 personal_data_->AddProfile(profile2);
199
200 // Verify that the web database has been updated and the notification sent.
201 EXPECT_CALL(personal_data_observer_,
202 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
203 MessageLoop::current()->Run();
204
205 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
206 ASSERT_EQ(2U, results2.size());
207 EXPECT_EQ(0, profile0.Compare(*results2[0]));
208 EXPECT_EQ(0, profile2.Compare(*results2[1]));
209
210 // Reset the PersonalDataManager. This tests that the personal data was saved
211 // to the web database, and that we can load the profiles from the web
212 // database.
213 ResetPersonalDataManager();
214
215 // Verify that we've loaded the profiles from the web database.
216 const std::vector<AutofillProfile*>& results3 = personal_data_->GetProfiles();
217 ASSERT_EQ(2U, results3.size());
218 EXPECT_EQ(0, profile0.Compare(*results3[0]));
219 EXPECT_EQ(0, profile2.Compare(*results3[1]));
220 }
221
222 TEST_F(PersonalDataManagerTest, AddUpdateRemoveCreditCards) {
223 CreditCard credit_card0;
224 autofill_test::SetCreditCardInfo(&credit_card0,
225 "John Dillinger", "423456789012" /* Visa */, "01", "2010");
226
227 CreditCard credit_card1;
228 autofill_test::SetCreditCardInfo(&credit_card1,
229 "Bonnie Parker", "518765432109" /* Mastercard */, "12", "2012");
230
231 CreditCard credit_card2;
232 autofill_test::SetCreditCardInfo(&credit_card2,
233 "Clyde Barrow", "347666888555" /* American Express */, "04", "2015");
234
235 // Add two test credit cards to the database.
236 personal_data_->AddCreditCard(credit_card0);
237 personal_data_->AddCreditCard(credit_card1);
238
239 // Verify that the web database has been updated and the notification sent.
240 EXPECT_CALL(personal_data_observer_,
241 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
242 MessageLoop::current()->Run();
243
244 const std::vector<CreditCard*>& results1 = personal_data_->credit_cards();
245 ASSERT_EQ(2U, results1.size());
246 EXPECT_EQ(0, credit_card0.Compare(*results1[0]));
247 EXPECT_EQ(0, credit_card1.Compare(*results1[1]));
248
249 // Update, remove, and add.
250 credit_card0.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Joe"));
251 personal_data_->UpdateCreditCard(credit_card0);
252 personal_data_->RemoveByGUID(credit_card1.guid());
253 personal_data_->AddCreditCard(credit_card2);
254
255 // Verify that the web database has been updated and the notification sent.
256 EXPECT_CALL(personal_data_observer_,
257 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
258 MessageLoop::current()->Run();
259
260 const std::vector<CreditCard*>& results2 = personal_data_->credit_cards();
261 ASSERT_EQ(2U, results2.size());
262 EXPECT_EQ(credit_card0, *results2[0]);
263 EXPECT_EQ(credit_card2, *results2[1]);
264
265 // Reset the PersonalDataManager. This tests that the personal data was saved
266 // to the web database, and that we can load the credit cards from the web
267 // database.
268 ResetPersonalDataManager();
269
270 // Verify that we've loaded the credit cards from the web database.
271 const std::vector<CreditCard*>& results3 = personal_data_->credit_cards();
272 ASSERT_EQ(2U, results3.size());
273 EXPECT_EQ(credit_card0, *results3[0]);
274 EXPECT_EQ(credit_card2, *results3[1]);
275 }
276
277 TEST_F(PersonalDataManagerTest, AddProfilesAndCreditCards) {
278 AutofillProfile profile0;
279 autofill_test::SetProfileInfo(&profile0,
280 "Marion", "Mitchell", "Morrison",
281 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
282 "91601", "US", "12345678910");
283
284 AutofillProfile profile1;
285 autofill_test::SetProfileInfo(&profile1,
286 "Josephine", "Alicia", "Saenz",
287 "joewayne@me.xyz", "Fox", "903 Apple Ct.", NULL, "Orlando", "FL", "32801",
288 "US", "19482937549");
289
290 CreditCard credit_card0;
291 autofill_test::SetCreditCardInfo(&credit_card0,
292 "John Dillinger", "423456789012" /* Visa */, "01", "2010");
293
294 CreditCard credit_card1;
295 autofill_test::SetCreditCardInfo(&credit_card1,
296 "Bonnie Parker", "518765432109" /* Mastercard */, "12", "2012");
297
298 // Add two test profiles to the database.
299 personal_data_->AddProfile(profile0);
300 personal_data_->AddProfile(profile1);
301
302 // Verify that the web database has been updated and the notification sent.
303 EXPECT_CALL(personal_data_observer_,
304 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
305 MessageLoop::current()->Run();
306
307 const std::vector<AutofillProfile*>& results1 = personal_data_->GetProfiles();
308 ASSERT_EQ(2U, results1.size());
309 EXPECT_EQ(0, profile0.Compare(*results1[0]));
310 EXPECT_EQ(0, profile1.Compare(*results1[1]));
311
312 // Add two test credit cards to the database.
313 personal_data_->AddCreditCard(credit_card0);
314 personal_data_->AddCreditCard(credit_card1);
315
316 // Verify that the web database has been updated and the notification sent.
317 EXPECT_CALL(personal_data_observer_,
318 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
319 MessageLoop::current()->Run();
320
321 const std::vector<CreditCard*>& results2 = personal_data_->credit_cards();
322 ASSERT_EQ(2U, results2.size());
323 EXPECT_EQ(credit_card0, *results2[0]);
324 EXPECT_EQ(credit_card1, *results2[1]);
325
326 // Determine uniqueness by inserting all of the GUIDs into a set and verifying
327 // the size of the set matches the number of GUIDs.
328 std::set<std::string> guids;
329 guids.insert(profile0.guid());
330 guids.insert(profile1.guid());
331 guids.insert(credit_card0.guid());
332 guids.insert(credit_card1.guid());
333 EXPECT_EQ(4U, guids.size());
334 }
335
336 // Test for http://crbug.com/50047. Makes sure that guids are populated
337 // correctly on load.
338 TEST_F(PersonalDataManagerTest, PopulateUniqueIDsOnLoad) {
339 AutofillProfile profile0;
340 autofill_test::SetProfileInfo(&profile0,
341 "y", "", "", "", "", "", "", "", "", "", "", "");
342
343 // Add the profile0 to the db.
344 personal_data_->AddProfile(profile0);
345
346 // Verify that the web database has been updated and the notification sent.
347 EXPECT_CALL(personal_data_observer_,
348 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
349 MessageLoop::current()->Run();
350
351 // Verify that we've loaded the profiles from the web database.
352 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
353 ASSERT_EQ(1U, results2.size());
354 EXPECT_EQ(0, profile0.Compare(*results2[0]));
355
356 // Add a new profile.
357 AutofillProfile profile1;
358 autofill_test::SetProfileInfo(&profile1,
359 "z", "", "", "", "", "", "", "", "", "", "", "");
360 personal_data_->AddProfile(profile1);
361
362 // Verify that the web database has been updated and the notification sent.
363 EXPECT_CALL(personal_data_observer_,
364 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
365 MessageLoop::current()->Run();
366
367 // Make sure the two profiles have different GUIDs, both valid.
368 const std::vector<AutofillProfile*>& results3 = personal_data_->GetProfiles();
369 ASSERT_EQ(2U, results3.size());
370 EXPECT_NE(results3[0]->guid(), results3[1]->guid());
371 EXPECT_TRUE(base::IsValidGUID(results3[0]->guid()));
372 EXPECT_TRUE(base::IsValidGUID(results3[1]->guid()));
373 }
374
375 TEST_F(PersonalDataManagerTest, SetEmptyProfile) {
376 AutofillProfile profile0;
377 autofill_test::SetProfileInfo(&profile0,
378 "", "", "", "", "", "", "", "", "", "", "", "");
379
380 // Add the empty profile to the database.
381 personal_data_->AddProfile(profile0);
382
383 // Note: no refresh here.
384
385 // Reset the PersonalDataManager. This tests that the personal data was saved
386 // to the web database, and that we can load the profiles from the web
387 // database.
388 ResetPersonalDataManager();
389
390 // Verify that we've loaded the profiles from the web database.
391 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
392 ASSERT_EQ(0U, results2.size());
393 }
394
395 TEST_F(PersonalDataManagerTest, SetEmptyCreditCard) {
396 CreditCard credit_card0;
397 autofill_test::SetCreditCardInfo(&credit_card0, "", "", "", "");
398
399 // Add the empty credit card to the database.
400 personal_data_->AddCreditCard(credit_card0);
401
402 // Note: no refresh here.
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 // Verify that we've loaded the credit cards from the web database.
410 const std::vector<CreditCard*>& results2 = personal_data_->credit_cards();
411 ASSERT_EQ(0U, results2.size());
412 }
413
414 TEST_F(PersonalDataManagerTest, Refresh) {
415 AutofillProfile profile0;
416 autofill_test::SetProfileInfo(&profile0,
417 "Marion", "Mitchell", "Morrison",
418 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
419 "91601", "US", "12345678910");
420
421 AutofillProfile profile1;
422 autofill_test::SetProfileInfo(&profile1,
423 "Josephine", "Alicia", "Saenz",
424 "joewayne@me.xyz", "Fox", "903 Apple Ct.", NULL, "Orlando", "FL", "32801",
425 "US", "19482937549");
426
427 // Add the test profiles to the database.
428 personal_data_->AddProfile(profile0);
429 personal_data_->AddProfile(profile1);
430
431 // Labels depend on other profiles in the list - update labels manually.
432 std::vector<AutofillProfile *> profile_pointers;
433 profile_pointers.push_back(&profile0);
434 profile_pointers.push_back(&profile1);
435 AutofillProfile::AdjustInferredLabels(&profile_pointers);
436
437 // Verify that the web database has been updated and the notification sent.
438 EXPECT_CALL(personal_data_observer_,
439 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
440 MessageLoop::current()->Run();
441
442 const std::vector<AutofillProfile*>& results1 = personal_data_->GetProfiles();
443 ASSERT_EQ(2U, results1.size());
444 EXPECT_EQ(profile0, *results1[0]);
445 EXPECT_EQ(profile1, *results1[1]);
446
447 AutofillProfile profile2;
448 autofill_test::SetProfileInfo(&profile2,
449 "Josephine", "Alicia", "Saenz",
450 "joewayne@me.xyz", "Fox", "1212 Center.", "Bld. 5", "Orlando", "FL",
451 "32801", "US", "19482937549");
452
453 // Adjust all labels.
454 profile_pointers.push_back(&profile2);
455 AutofillProfile::AdjustInferredLabels(&profile_pointers);
456
457 scoped_refptr<WebDataService> wds = WebDataServiceFactory::GetForProfile(
458 profile_.get(), Profile::EXPLICIT_ACCESS);
459 ASSERT_TRUE(wds.get());
460 wds->AddAutofillProfile(profile2);
461
462 personal_data_->Refresh();
463
464 // Verify that the web database has been updated and the notification sent.
465 EXPECT_CALL(personal_data_observer_,
466 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
467 MessageLoop::current()->Run();
468
469 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
470 ASSERT_EQ(3U, results2.size());
471 EXPECT_EQ(profile0, *results2[0]);
472 EXPECT_EQ(profile1, *results2[1]);
473 EXPECT_EQ(profile2, *results2[2]);
474
475 wds->RemoveAutofillProfile(profile1.guid());
476 wds->RemoveAutofillProfile(profile2.guid());
477
478 // Before telling the PDM to refresh, simulate an edit to one of the profiles
479 // via a SetProfile update (this would happen if the Autofill window was
480 // open with a previous snapshot of the profiles, and something [e.g. sync]
481 // removed a profile from the browser. In this edge case, we will end up
482 // in a consistent state by dropping the write).
483 profile2.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Jo"));
484 personal_data_->UpdateProfile(profile0);
485 personal_data_->AddProfile(profile1);
486 personal_data_->AddProfile(profile2);
487
488 // Verify that the web database has been updated and the notification sent.
489 EXPECT_CALL(personal_data_observer_,
490 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
491 MessageLoop::current()->Run();
492
493 const std::vector<AutofillProfile*>& results3 = personal_data_->GetProfiles();
494 ASSERT_EQ(1U, results3.size());
495 EXPECT_EQ(profile0, *results2[0]);
496 }
497
498 TEST_F(PersonalDataManagerTest, ImportFormData) {
499 FormData form;
500 FormFieldData field;
501 autofill_test::CreateTestFormField(
502 "First name:", "first_name", "George", "text", &field);
503 form.fields.push_back(field);
504 autofill_test::CreateTestFormField(
505 "Last name:", "last_name", "Washington", "text", &field);
506 form.fields.push_back(field);
507 autofill_test::CreateTestFormField(
508 "Email:", "email", "theprez@gmail.com", "text", &field);
509 form.fields.push_back(field);
510 autofill_test::CreateTestFormField(
511 "Address:", "address1", "21 Laussat St", "text", &field);
512 form.fields.push_back(field);
513 autofill_test::CreateTestFormField(
514 "City:", "city", "San Francisco", "text", &field);
515 form.fields.push_back(field);
516 autofill_test::CreateTestFormField(
517 "State:", "state", "California", "text", &field);
518 form.fields.push_back(field);
519 autofill_test::CreateTestFormField(
520 "Zip:", "zip", "94102", "text", &field);
521 form.fields.push_back(field);
522 FormStructure form_structure(form, std::string());
523 form_structure.DetermineHeuristicTypes(TestAutofillMetrics());
524 const CreditCard* imported_credit_card;
525 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
526 &imported_credit_card));
527 ASSERT_FALSE(imported_credit_card);
528
529 // Verify that the web database has been updated and the notification sent.
530 EXPECT_CALL(personal_data_observer_,
531 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
532 MessageLoop::current()->Run();
533
534 AutofillProfile expected;
535 autofill_test::SetProfileInfo(&expected, "George", NULL,
536 "Washington", "theprez@gmail.com", NULL, "21 Laussat St", NULL,
537 "San Francisco", "California", "94102", NULL, NULL);
538 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
539 ASSERT_EQ(1U, results.size());
540 EXPECT_EQ(0, expected.Compare(*results[0]));
541 }
542
543 TEST_F(PersonalDataManagerTest, ImportFormDataBadEmail) {
544 FormData form;
545 FormFieldData field;
546 autofill_test::CreateTestFormField(
547 "First name:", "first_name", "George", "text", &field);
548 form.fields.push_back(field);
549 autofill_test::CreateTestFormField(
550 "Last name:", "last_name", "Washington", "text", &field);
551 form.fields.push_back(field);
552 autofill_test::CreateTestFormField(
553 "Email:", "email", "bogus", "text", &field);
554 form.fields.push_back(field);
555 autofill_test::CreateTestFormField(
556 "Address:", "address1", "21 Laussat St", "text", &field);
557 form.fields.push_back(field);
558 autofill_test::CreateTestFormField(
559 "City:", "city", "San Francisco", "text", &field);
560 form.fields.push_back(field);
561 autofill_test::CreateTestFormField(
562 "State:", "state", "California", "text", &field);
563 form.fields.push_back(field);
564 autofill_test::CreateTestFormField(
565 "Zip:", "zip", "94102", "text", &field);
566 form.fields.push_back(field);
567 FormStructure form_structure(form, std::string());
568 form_structure.DetermineHeuristicTypes(TestAutofillMetrics());
569 const CreditCard* imported_credit_card;
570 EXPECT_FALSE(personal_data_->ImportFormData(form_structure,
571 &imported_credit_card));
572 ASSERT_EQ(static_cast<CreditCard*>(NULL), imported_credit_card);
573
574 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
575 ASSERT_EQ(0U, results.size());
576 }
577
578 TEST_F(PersonalDataManagerTest, ImportFormDataNotEnoughFilledFields) {
579 FormData form;
580 FormFieldData field;
581 autofill_test::CreateTestFormField(
582 "First name:", "first_name", "George", "text", &field);
583 form.fields.push_back(field);
584 autofill_test::CreateTestFormField(
585 "Last name:", "last_name", "Washington", "text", &field);
586 form.fields.push_back(field);
587 autofill_test::CreateTestFormField(
588 "Card number:", "card_number", "4111 1111 1111 1111", "text", &field);
589 form.fields.push_back(field);
590 FormStructure form_structure(form, std::string());
591 form_structure.DetermineHeuristicTypes(TestAutofillMetrics());
592 const CreditCard* imported_credit_card;
593 EXPECT_FALSE(personal_data_->ImportFormData(form_structure,
594 &imported_credit_card));
595 ASSERT_FALSE(imported_credit_card);
596
597 const std::vector<AutofillProfile*>& profiles = personal_data_->GetProfiles();
598 ASSERT_EQ(0U, profiles.size());
599 const std::vector<CreditCard*>& credit_cards = personal_data_->credit_cards();
600 ASSERT_EQ(0U, credit_cards.size());
601 }
602
603 TEST_F(PersonalDataManagerTest, ImportPhoneNumberSplitAcrossMultipleFields) {
604 FormData form;
605 FormFieldData field;
606 autofill_test::CreateTestFormField(
607 "First name:", "first_name", "George", "text", &field);
608 form.fields.push_back(field);
609 autofill_test::CreateTestFormField(
610 "Last name:", "last_name", "Washington", "text", &field);
611 form.fields.push_back(field);
612 autofill_test::CreateTestFormField(
613 "Phone #:", "home_phone_area_code", "650", "text", &field);
614 field.max_length = 3;
615 form.fields.push_back(field);
616 autofill_test::CreateTestFormField(
617 "Phone #:", "home_phone_prefix", "555", "text", &field);
618 field.max_length = 3;
619 form.fields.push_back(field);
620 autofill_test::CreateTestFormField(
621 "Phone #:", "home_phone_suffix", "0000", "text", &field);
622 field.max_length = 4;
623 form.fields.push_back(field);
624 autofill_test::CreateTestFormField(
625 "Address:", "address1", "21 Laussat St", "text", &field);
626 form.fields.push_back(field);
627 autofill_test::CreateTestFormField(
628 "City:", "city", "San Francisco", "text", &field);
629 form.fields.push_back(field);
630 autofill_test::CreateTestFormField(
631 "State:", "state", "California", "text", &field);
632 form.fields.push_back(field);
633 autofill_test::CreateTestFormField(
634 "Zip:", "zip", "94102", "text", &field);
635 form.fields.push_back(field);
636 FormStructure form_structure(form, std::string());
637 form_structure.DetermineHeuristicTypes(TestAutofillMetrics());
638 const CreditCard* imported_credit_card;
639 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
640 &imported_credit_card));
641 ASSERT_FALSE(imported_credit_card);
642
643 // Verify that the web database has been updated and the notification sent.
644 EXPECT_CALL(personal_data_observer_,
645 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
646 MessageLoop::current()->Run();
647
648 AutofillProfile expected;
649 autofill_test::SetProfileInfo(&expected, "George", NULL,
650 "Washington", NULL, NULL, "21 Laussat St", NULL,
651 "San Francisco", "California", "94102", NULL, "(650) 555-0000");
652 const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
653 ASSERT_EQ(1U, results.size());
654 EXPECT_EQ(0, expected.Compare(*results[0]));
655 }
656
657 TEST_F(PersonalDataManagerTest, SetUniqueCreditCardLabels) {
658 CreditCard credit_card0;
659 credit_card0.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("John"));
660 CreditCard credit_card1;
661 credit_card1.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Paul"));
662 CreditCard credit_card2;
663 credit_card2.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Ringo"));
664 CreditCard credit_card3;
665 credit_card3.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Other"));
666 CreditCard credit_card4;
667 credit_card4.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Ozzy"));
668 CreditCard credit_card5;
669 credit_card5.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Dio"));
670
671 // Add the test credit cards to the database.
672 personal_data_->AddCreditCard(credit_card0);
673 personal_data_->AddCreditCard(credit_card1);
674 personal_data_->AddCreditCard(credit_card2);
675 personal_data_->AddCreditCard(credit_card3);
676 personal_data_->AddCreditCard(credit_card4);
677 personal_data_->AddCreditCard(credit_card5);
678
679 // Reset the PersonalDataManager. This tests that the personal data was saved
680 // to the web database, and that we can load the credit cards from the web
681 // database.
682 ResetPersonalDataManager();
683
684 const std::vector<CreditCard*>& results = personal_data_->credit_cards();
685 ASSERT_EQ(6U, results.size());
686 EXPECT_EQ(credit_card0.guid(), results[0]->guid());
687 EXPECT_EQ(credit_card1.guid(), results[1]->guid());
688 EXPECT_EQ(credit_card2.guid(), results[2]->guid());
689 EXPECT_EQ(credit_card3.guid(), results[3]->guid());
690 EXPECT_EQ(credit_card4.guid(), results[4]->guid());
691 EXPECT_EQ(credit_card5.guid(), results[5]->guid());
692 }
693
694 TEST_F(PersonalDataManagerTest, AggregateTwoDifferentProfiles) {
695 FormData form1;
696 FormFieldData field;
697 autofill_test::CreateTestFormField(
698 "First name:", "first_name", "George", "text", &field);
699 form1.fields.push_back(field);
700 autofill_test::CreateTestFormField(
701 "Last name:", "last_name", "Washington", "text", &field);
702 form1.fields.push_back(field);
703 autofill_test::CreateTestFormField(
704 "Email:", "email", "theprez@gmail.com", "text", &field);
705 form1.fields.push_back(field);
706 autofill_test::CreateTestFormField(
707 "Address:", "address1", "21 Laussat St", "text", &field);
708 form1.fields.push_back(field);
709 autofill_test::CreateTestFormField(
710 "City:", "city", "San Francisco", "text", &field);
711 form1.fields.push_back(field);
712 autofill_test::CreateTestFormField(
713 "State:", "state", "California", "text", &field);
714 form1.fields.push_back(field);
715 autofill_test::CreateTestFormField(
716 "Zip:", "zip", "94102", "text", &field);
717 form1.fields.push_back(field);
718
719 FormStructure form_structure1(form1, std::string());
720 form_structure1.DetermineHeuristicTypes(TestAutofillMetrics());
721 const CreditCard* imported_credit_card;
722 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
723 &imported_credit_card));
724 ASSERT_FALSE(imported_credit_card);
725
726 // Verify that the web database has been updated and the notification sent.
727 EXPECT_CALL(personal_data_observer_,
728 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
729 MessageLoop::current()->Run();
730
731 AutofillProfile expected;
732 autofill_test::SetProfileInfo(&expected, "George", NULL,
733 "Washington", "theprez@gmail.com", NULL, "21 Laussat St", NULL,
734 "San Francisco", "California", "94102", NULL, NULL);
735 const std::vector<AutofillProfile*>& results1 = personal_data_->GetProfiles();
736 ASSERT_EQ(1U, results1.size());
737 EXPECT_EQ(0, expected.Compare(*results1[0]));
738
739 // Now create a completely different profile.
740 FormData form2;
741 autofill_test::CreateTestFormField(
742 "First name:", "first_name", "John", "text", &field);
743 form2.fields.push_back(field);
744 autofill_test::CreateTestFormField(
745 "Last name:", "last_name", "Adams", "text", &field);
746 form2.fields.push_back(field);
747 autofill_test::CreateTestFormField(
748 "Email:", "email", "second@gmail.com", "text", &field);
749 form2.fields.push_back(field);
750 autofill_test::CreateTestFormField(
751 "Address:", "address1", "22 Laussat St", "text", &field);
752 form2.fields.push_back(field);
753 autofill_test::CreateTestFormField(
754 "City:", "city", "San Francisco", "text", &field);
755 form2.fields.push_back(field);
756 autofill_test::CreateTestFormField(
757 "State:", "state", "California", "text", &field);
758 form2.fields.push_back(field);
759 autofill_test::CreateTestFormField(
760 "Zip:", "zip", "94102", "text", &field);
761 form2.fields.push_back(field);
762
763 FormStructure form_structure2(form2, std::string());
764 form_structure2.DetermineHeuristicTypes(TestAutofillMetrics());
765 EXPECT_TRUE(personal_data_->ImportFormData(form_structure2,
766 &imported_credit_card));
767 ASSERT_FALSE(imported_credit_card);
768
769 // Verify that the web database has been updated and the notification sent.
770 EXPECT_CALL(personal_data_observer_,
771 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
772 MessageLoop::current()->Run();
773
774 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
775
776 AutofillProfile expected2;
777 autofill_test::SetProfileInfo(&expected2, "John", NULL,
778 "Adams", "second@gmail.com", NULL, "22 Laussat St", NULL,
779 "San Francisco", "California", "94102", NULL, NULL);
780 ASSERT_EQ(2U, results2.size());
781 EXPECT_EQ(0, expected.Compare(*results2[0]));
782 EXPECT_EQ(0, expected2.Compare(*results2[1]));
783 }
784
785 TEST_F(PersonalDataManagerTest, AggregateTwoProfilesWithMultiValue) {
786 FormData form1;
787 FormFieldData field;
788 autofill_test::CreateTestFormField(
789 "First name:", "first_name", "George", "text", &field);
790 form1.fields.push_back(field);
791 autofill_test::CreateTestFormField(
792 "Last name:", "last_name", "Washington", "text", &field);
793 form1.fields.push_back(field);
794 autofill_test::CreateTestFormField(
795 "Email:", "email", "theprez@gmail.com", "text", &field);
796 form1.fields.push_back(field);
797 autofill_test::CreateTestFormField(
798 "Address:", "address1", "21 Laussat St", "text", &field);
799 form1.fields.push_back(field);
800 autofill_test::CreateTestFormField(
801 "City:", "city", "San Francisco", "text", &field);
802 form1.fields.push_back(field);
803 autofill_test::CreateTestFormField(
804 "State:", "state", "California", "text", &field);
805 form1.fields.push_back(field);
806 autofill_test::CreateTestFormField(
807 "Zip:", "zip", "94102", "text", &field);
808 form1.fields.push_back(field);
809
810 FormStructure form_structure1(form1, std::string());
811 form_structure1.DetermineHeuristicTypes(TestAutofillMetrics());
812 const CreditCard* imported_credit_card;
813 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
814 &imported_credit_card));
815 ASSERT_FALSE(imported_credit_card);
816
817 // Verify that the web database has been updated and the notification sent.
818 EXPECT_CALL(personal_data_observer_,
819 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
820 MessageLoop::current()->Run();
821
822 AutofillProfile expected;
823 autofill_test::SetProfileInfo(&expected, "George", NULL,
824 "Washington", "theprez@gmail.com", NULL, "21 Laussat St", NULL,
825 "San Francisco", "California", "94102", NULL, NULL);
826 const std::vector<AutofillProfile*>& results1 = personal_data_->GetProfiles();
827 ASSERT_EQ(1U, results1.size());
828 EXPECT_EQ(0, expected.Compare(*results1[0]));
829
830 // Now create a completely different profile.
831 FormData form2;
832 autofill_test::CreateTestFormField(
833 "First name:", "first_name", "John", "text", &field);
834 form2.fields.push_back(field);
835 autofill_test::CreateTestFormField(
836 "Last name:", "last_name", "Adams", "text", &field);
837 form2.fields.push_back(field);
838 autofill_test::CreateTestFormField(
839 "Email:", "email", "second@gmail.com", "text", &field);
840 form2.fields.push_back(field);
841 autofill_test::CreateTestFormField(
842 "Address:", "address1", "21 Laussat St", "text", &field);
843 form2.fields.push_back(field);
844 autofill_test::CreateTestFormField(
845 "City:", "city", "San Francisco", "text", &field);
846 form2.fields.push_back(field);
847 autofill_test::CreateTestFormField(
848 "State:", "state", "California", "text", &field);
849 form2.fields.push_back(field);
850 autofill_test::CreateTestFormField(
851 "Zip:", "zip", "94102", "text", &field);
852 form2.fields.push_back(field);
853
854 FormStructure form_structure2(form2, std::string());
855 form_structure2.DetermineHeuristicTypes(TestAutofillMetrics());
856 EXPECT_TRUE(personal_data_->ImportFormData(form_structure2,
857 &imported_credit_card));
858 ASSERT_FALSE(imported_credit_card);
859
860 // Verify that the web database has been updated and the notification sent.
861 EXPECT_CALL(personal_data_observer_,
862 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
863 MessageLoop::current()->Run();
864
865 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
866
867 // Modify expected to include multi-valued fields.
868 std::vector<string16> values;
869 expected.GetRawMultiInfo(NAME_FULL, &values);
870 values.push_back(ASCIIToUTF16("John Adams"));
871 expected.SetRawMultiInfo(NAME_FULL, values);
872 expected.GetRawMultiInfo(EMAIL_ADDRESS, &values);
873 values.push_back(ASCIIToUTF16("second@gmail.com"));
874 expected.SetRawMultiInfo(EMAIL_ADDRESS, values);
875
876 ASSERT_EQ(1U, results2.size());
877 EXPECT_EQ(0, expected.Compare(*results2[0]));
878 }
879
880 TEST_F(PersonalDataManagerTest, AggregateSameProfileWithConflict) {
881 FormData form1;
882 FormFieldData field;
883 autofill_test::CreateTestFormField(
884 "First name:", "first_name", "George", "text", &field);
885 form1.fields.push_back(field);
886 autofill_test::CreateTestFormField(
887 "Last name:", "last_name", "Washington", "text", &field);
888 form1.fields.push_back(field);
889 autofill_test::CreateTestFormField(
890 "Address:", "address", "1600 Pennsylvania Avenue", "text", &field);
891 form1.fields.push_back(field);
892 autofill_test::CreateTestFormField(
893 "Address Line 2:", "address2", "Suite A", "text", &field);
894 form1.fields.push_back(field);
895 autofill_test::CreateTestFormField(
896 "City:", "city", "San Francisco", "text", &field);
897 form1.fields.push_back(field);
898 autofill_test::CreateTestFormField(
899 "State:", "state", "California", "text", &field);
900 form1.fields.push_back(field);
901 autofill_test::CreateTestFormField(
902 "Zip:", "zip", "94102", "text", &field);
903 form1.fields.push_back(field);
904 autofill_test::CreateTestFormField(
905 "Email:", "email", "theprez@gmail.com", "text", &field);
906 form1.fields.push_back(field);
907 autofill_test::CreateTestFormField(
908 "Phone:", "phone", "6505556666", "text", &field);
909 form1.fields.push_back(field);
910
911 FormStructure form_structure1(form1, std::string());
912 form_structure1.DetermineHeuristicTypes(TestAutofillMetrics());
913 const CreditCard* imported_credit_card;
914 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
915 &imported_credit_card));
916 ASSERT_FALSE(imported_credit_card);
917
918 // Verify that the web database has been updated and the notification sent.
919 EXPECT_CALL(personal_data_observer_,
920 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
921 MessageLoop::current()->Run();
922
923 AutofillProfile expected;
924 autofill_test::SetProfileInfo(
925 &expected, "George", NULL, "Washington", "theprez@gmail.com", NULL,
926 "1600 Pennsylvania Avenue", "Suite A", "San Francisco", "California",
927 "94102", NULL, "(650) 555-6666");
928 const std::vector<AutofillProfile*>& results1 = personal_data_->GetProfiles();
929 ASSERT_EQ(1U, results1.size());
930 EXPECT_EQ(0, expected.Compare(*results1[0]));
931
932 // Now create an updated profile.
933 FormData form2;
934 autofill_test::CreateTestFormField(
935 "First name:", "first_name", "George", "text", &field);
936 form2.fields.push_back(field);
937 autofill_test::CreateTestFormField(
938 "Last name:", "last_name", "Washington", "text", &field);
939 form2.fields.push_back(field);
940 autofill_test::CreateTestFormField(
941 "Address:", "address", "1600 Pennsylvania Avenue", "text", &field);
942 form2.fields.push_back(field);
943 autofill_test::CreateTestFormField(
944 "Address Line 2:", "address2", "Suite A", "text", &field);
945 form2.fields.push_back(field);
946 autofill_test::CreateTestFormField(
947 "City:", "city", "San Francisco", "text", &field);
948 form2.fields.push_back(field);
949 autofill_test::CreateTestFormField(
950 "State:", "state", "California", "text", &field);
951 form2.fields.push_back(field);
952 autofill_test::CreateTestFormField(
953 "Zip:", "zip", "94102", "text", &field);
954 form2.fields.push_back(field);
955 autofill_test::CreateTestFormField(
956 "Email:", "email", "theprez@gmail.com", "text", &field);
957 form2.fields.push_back(field);
958 // Country gets added.
959 autofill_test::CreateTestFormField(
960 "Country:", "country", "USA", "text", &field);
961 form2.fields.push_back(field);
962 // Phone gets updated.
963 autofill_test::CreateTestFormField(
964 "Phone:", "phone", "6502231234", "text", &field);
965 form2.fields.push_back(field);
966
967 FormStructure form_structure2(form2, std::string());
968 form_structure2.DetermineHeuristicTypes(TestAutofillMetrics());
969 EXPECT_TRUE(personal_data_->ImportFormData(form_structure2,
970 &imported_credit_card));
971 ASSERT_FALSE(imported_credit_card);
972
973 // Verify that the web database has been updated and the notification sent.
974 EXPECT_CALL(personal_data_observer_,
975 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
976 MessageLoop::current()->Run();
977
978 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
979
980 // Add multi-valued phone number to expectation. Also, country gets added.
981 std::vector<string16> values;
982 expected.GetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, &values);
983 values.push_back(ASCIIToUTF16("(650) 223-1234"));
984 expected.SetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, values);
985 expected.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"));
986 ASSERT_EQ(1U, results2.size());
987 EXPECT_EQ(0, expected.Compare(*results2[0]));
988 }
989
990 TEST_F(PersonalDataManagerTest, AggregateProfileWithMissingInfoInOld) {
991 FormData form1;
992 FormFieldData field;
993 autofill_test::CreateTestFormField(
994 "First name:", "first_name", "George", "text", &field);
995 form1.fields.push_back(field);
996 autofill_test::CreateTestFormField(
997 "Last name:", "last_name", "Washington", "text", &field);
998 form1.fields.push_back(field);
999 autofill_test::CreateTestFormField(
1000 "Address Line 1:", "address", "190 High Street", "text", &field);
1001 form1.fields.push_back(field);
1002 autofill_test::CreateTestFormField(
1003 "City:", "city", "Philadelphia", "text", &field);
1004 form1.fields.push_back(field);
1005 autofill_test::CreateTestFormField(
1006 "State:", "state", "Pennsylvania", "text", &field);
1007 form1.fields.push_back(field);
1008 autofill_test::CreateTestFormField(
1009 "Zip:", "zipcode", "19106", "text", &field);
1010 form1.fields.push_back(field);
1011
1012 FormStructure form_structure1(form1, std::string());
1013 form_structure1.DetermineHeuristicTypes(TestAutofillMetrics());
1014 const CreditCard* imported_credit_card;
1015 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1016 &imported_credit_card));
1017 EXPECT_FALSE(imported_credit_card);
1018
1019 // Verify that the web database has been updated and the notification sent.
1020 EXPECT_CALL(personal_data_observer_,
1021 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
1022 MessageLoop::current()->Run();
1023
1024 AutofillProfile expected;
1025 autofill_test::SetProfileInfo(&expected, "George", NULL,
1026 "Washington", NULL, NULL, "190 High Street", NULL,
1027 "Philadelphia", "Pennsylvania", "19106", NULL, NULL);
1028 const std::vector<AutofillProfile*>& results1 = personal_data_->GetProfiles();
1029 ASSERT_EQ(1U, results1.size());
1030 EXPECT_EQ(0, expected.Compare(*results1[0]));
1031
1032 // Submit a form with new data for the first profile.
1033 FormData form2;
1034 autofill_test::CreateTestFormField(
1035 "First name:", "first_name", "George", "text", &field);
1036 form2.fields.push_back(field);
1037 autofill_test::CreateTestFormField(
1038 "Last name:", "last_name", "Washington", "text", &field);
1039 form2.fields.push_back(field);
1040 autofill_test::CreateTestFormField(
1041 "Email:", "email", "theprez@gmail.com", "text", &field);
1042 form2.fields.push_back(field);
1043 autofill_test::CreateTestFormField(
1044 "Address Line 1:", "address", "190 High Street", "text", &field);
1045 form2.fields.push_back(field);
1046 autofill_test::CreateTestFormField(
1047 "City:", "city", "Philadelphia", "text", &field);
1048 form2.fields.push_back(field);
1049 autofill_test::CreateTestFormField(
1050 "State:", "state", "Pennsylvania", "text", &field);
1051 form2.fields.push_back(field);
1052 autofill_test::CreateTestFormField(
1053 "Zip:", "zipcode", "19106", "text", &field);
1054 form2.fields.push_back(field);
1055
1056 FormStructure form_structure2(form2, std::string());
1057 form_structure2.DetermineHeuristicTypes(TestAutofillMetrics());
1058 EXPECT_TRUE(personal_data_->ImportFormData(form_structure2,
1059 &imported_credit_card));
1060 ASSERT_FALSE(imported_credit_card);
1061
1062 // Verify that the web database has been updated and the notification sent.
1063 EXPECT_CALL(personal_data_observer_,
1064 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
1065 MessageLoop::current()->Run();
1066
1067 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
1068
1069 AutofillProfile expected2;
1070 autofill_test::SetProfileInfo(&expected2, "George", NULL,
1071 "Washington", "theprez@gmail.com", NULL, "190 High Street", NULL,
1072 "Philadelphia", "Pennsylvania", "19106", NULL, NULL);
1073 ASSERT_EQ(1U, results2.size());
1074 EXPECT_EQ(0, expected2.Compare(*results2[0]));
1075 }
1076
1077 TEST_F(PersonalDataManagerTest, AggregateProfileWithMissingInfoInNew) {
1078 FormData form1;
1079 FormFieldData field;
1080 autofill_test::CreateTestFormField(
1081 "First name:", "first_name", "George", "text", &field);
1082 form1.fields.push_back(field);
1083 autofill_test::CreateTestFormField(
1084 "Last name:", "last_name", "Washington", "text", &field);
1085 form1.fields.push_back(field);
1086 autofill_test::CreateTestFormField(
1087 "Company:", "company", "Government", "text", &field);
1088 form1.fields.push_back(field);
1089 autofill_test::CreateTestFormField(
1090 "Email:", "email", "theprez@gmail.com", "text", &field);
1091 form1.fields.push_back(field);
1092 autofill_test::CreateTestFormField(
1093 "Address Line 1:", "address", "190 High Street", "text", &field);
1094 form1.fields.push_back(field);
1095 autofill_test::CreateTestFormField(
1096 "City:", "city", "Philadelphia", "text", &field);
1097 form1.fields.push_back(field);
1098 autofill_test::CreateTestFormField(
1099 "State:", "state", "Pennsylvania", "text", &field);
1100 form1.fields.push_back(field);
1101 autofill_test::CreateTestFormField(
1102 "Zip:", "zipcode", "19106", "text", &field);
1103 form1.fields.push_back(field);
1104
1105 FormStructure form_structure1(form1, std::string());
1106 form_structure1.DetermineHeuristicTypes(TestAutofillMetrics());
1107 const CreditCard* imported_credit_card;
1108 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1109 &imported_credit_card));
1110 ASSERT_FALSE(imported_credit_card);
1111
1112 // Verify that the web database has been updated and the notification sent.
1113 EXPECT_CALL(personal_data_observer_,
1114 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
1115 MessageLoop::current()->Run();
1116
1117 AutofillProfile expected;
1118 autofill_test::SetProfileInfo(&expected, "George", NULL,
1119 "Washington", "theprez@gmail.com", "Government", "190 High Street", NULL,
1120 "Philadelphia", "Pennsylvania", "19106", NULL, NULL);
1121 const std::vector<AutofillProfile*>& results1 = personal_data_->GetProfiles();
1122 ASSERT_EQ(1U, results1.size());
1123 EXPECT_EQ(0, expected.Compare(*results1[0]));
1124
1125 // Submit a form with new data for the first profile.
1126 FormData form2;
1127 autofill_test::CreateTestFormField(
1128 "First name:", "first_name", "George", "text", &field);
1129 form2.fields.push_back(field);
1130 autofill_test::CreateTestFormField(
1131 "Last name:", "last_name", "Washington", "text", &field);
1132 form2.fields.push_back(field);
1133 // Note missing Company field.
1134 autofill_test::CreateTestFormField(
1135 "Email:", "email", "theprez@gmail.com", "text", &field);
1136 form2.fields.push_back(field);
1137 autofill_test::CreateTestFormField(
1138 "Address Line 1:", "address", "190 High Street", "text", &field);
1139 form2.fields.push_back(field);
1140 autofill_test::CreateTestFormField(
1141 "City:", "city", "Philadelphia", "text", &field);
1142 form2.fields.push_back(field);
1143 autofill_test::CreateTestFormField(
1144 "State:", "state", "Pennsylvania", "text", &field);
1145 form2.fields.push_back(field);
1146 autofill_test::CreateTestFormField(
1147 "Zip:", "zipcode", "19106", "text", &field);
1148 form2.fields.push_back(field);
1149
1150 FormStructure form_structure2(form2, std::string());
1151 form_structure2.DetermineHeuristicTypes(TestAutofillMetrics());
1152 EXPECT_TRUE(personal_data_->ImportFormData(form_structure2,
1153 &imported_credit_card));
1154 ASSERT_FALSE(imported_credit_card);
1155
1156 // Verify that the web database has been updated and the notification sent.
1157 EXPECT_CALL(personal_data_observer_,
1158 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
1159 MessageLoop::current()->Run();
1160
1161 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
1162
1163 // Expect no change.
1164 ASSERT_EQ(1U, results2.size());
1165 EXPECT_EQ(0, expected.Compare(*results2[0]));
1166 }
1167
1168 TEST_F(PersonalDataManagerTest, AggregateProfileWithInsufficientAddress) {
1169 FormData form1;
1170 FormFieldData field;
1171 autofill_test::CreateTestFormField(
1172 "First name:", "first_name", "George", "text", &field);
1173 form1.fields.push_back(field);
1174 autofill_test::CreateTestFormField(
1175 "Last name:", "last_name", "Washington", "text", &field);
1176 form1.fields.push_back(field);
1177 autofill_test::CreateTestFormField(
1178 "Company:", "company", "Government", "text", &field);
1179 form1.fields.push_back(field);
1180 autofill_test::CreateTestFormField(
1181 "Email:", "email", "theprez@gmail.com", "text", &field);
1182 form1.fields.push_back(field);
1183 autofill_test::CreateTestFormField(
1184 "Address Line 1:", "address", "190 High Street", "text", &field);
1185 form1.fields.push_back(field);
1186 autofill_test::CreateTestFormField(
1187 "City:", "city", "Philadelphia", "text", &field);
1188 form1.fields.push_back(field);
1189
1190 FormStructure form_structure1(form1, std::string());
1191 form_structure1.DetermineHeuristicTypes(TestAutofillMetrics());
1192 const CreditCard* imported_credit_card;
1193 EXPECT_FALSE(personal_data_->ImportFormData(form_structure1,
1194 &imported_credit_card));
1195 ASSERT_FALSE(imported_credit_card);
1196
1197 // Note: no refresh here.
1198
1199 const std::vector<AutofillProfile*>& profiles = personal_data_->GetProfiles();
1200 ASSERT_EQ(0U, profiles.size());
1201 const std::vector<CreditCard*>& credit_cards = personal_data_->credit_cards();
1202 ASSERT_EQ(0U, credit_cards.size());
1203 }
1204
1205 TEST_F(PersonalDataManagerTest, AggregateExistingAuxiliaryProfile) {
1206 // Simulate having access to an auxiliary profile.
1207 // |auxiliary_profile| will be owned by |personal_data_|.
1208 AutofillProfile* auxiliary_profile = new AutofillProfile;
1209 autofill_test::SetProfileInfo(auxiliary_profile,
1210 "Tester", "Frederick", "McAddressBookTesterson",
1211 "tester@example.com", "Acme Inc.", "1 Main", "Apt A", "San Francisco",
1212 "CA", "94102", "US", "1.415.888.9999");
1213 ScopedVector<AutofillProfile>& auxiliary_profiles =
1214 personal_data_->auxiliary_profiles_;
1215 auxiliary_profiles.push_back(auxiliary_profile);
1216
1217 // Simulate a form submission with a subset of the info.
1218 // Note that the phone number format is different from the saved format.
1219 FormData form;
1220 FormFieldData field;
1221 autofill_test::CreateTestFormField(
1222 "First name:", "first_name", "Tester", "text", &field);
1223 form.fields.push_back(field);
1224 autofill_test::CreateTestFormField(
1225 "Last name:", "last_name", "McAddressBookTesterson", "text", &field);
1226 form.fields.push_back(field);
1227 autofill_test::CreateTestFormField(
1228 "Email:", "email", "tester@example.com", "text", &field);
1229 form.fields.push_back(field);
1230 autofill_test::CreateTestFormField(
1231 "Address:", "address1", "1 Main", "text", &field);
1232 form.fields.push_back(field);
1233 autofill_test::CreateTestFormField(
1234 "City:", "city", "San Francisco", "text", &field);
1235 form.fields.push_back(field);
1236 autofill_test::CreateTestFormField(
1237 "State:", "state", "CA", "text", &field);
1238 form.fields.push_back(field);
1239 autofill_test::CreateTestFormField(
1240 "Zip:", "zip", "94102", "text", &field);
1241 form.fields.push_back(field);
1242 autofill_test::CreateTestFormField(
1243 "Phone:", "phone", "4158889999", "text", &field);
1244 form.fields.push_back(field);
1245
1246 FormStructure form_structure(form, std::string());
1247 form_structure.DetermineHeuristicTypes(TestAutofillMetrics());
1248 const CreditCard* imported_credit_card;
1249 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
1250 &imported_credit_card));
1251 EXPECT_FALSE(imported_credit_card);
1252
1253 // Note: No refresh.
1254
1255 // Expect no change.
1256 const std::vector<AutofillProfile*>& web_profiles =
1257 personal_data_->web_profiles();
1258 EXPECT_EQ(0U, web_profiles.size());
1259 ASSERT_EQ(1U, auxiliary_profiles.size());
1260 EXPECT_EQ(0, auxiliary_profile->Compare(*auxiliary_profiles[0]));
1261 }
1262
1263 TEST_F(PersonalDataManagerTest, AggregateTwoDifferentCreditCards) {
1264 FormData form1;
1265
1266 // Start with a single valid credit card form.
1267 FormFieldData 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, std::string());
1282 form_structure1.DetermineHeuristicTypes(TestAutofillMetrics());
1283 const CreditCard* imported_credit_card;
1284 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1285 &imported_credit_card));
1286 ASSERT_TRUE(imported_credit_card);
1287 personal_data_->SaveImportedCreditCard(*imported_credit_card);
1288 delete imported_credit_card;
1289
1290 // Verify that the web database has been updated and the notification sent.
1291 EXPECT_CALL(personal_data_observer_,
1292 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
1293 MessageLoop::current()->Run();
1294
1295 CreditCard expected;
1296 autofill_test::SetCreditCardInfo(&expected,
1297 "Biggie Smalls", "4111111111111111", "01", "2011");
1298 const std::vector<CreditCard*>& results = personal_data_->credit_cards();
1299 ASSERT_EQ(1U, results.size());
1300 EXPECT_EQ(0, expected.Compare(*results[0]));
1301
1302 // Add a second different valid credit card.
1303 FormData form2;
1304 autofill_test::CreateTestFormField(
1305 "Name on card:", "name_on_card", "", "text", &field);
1306 form2.fields.push_back(field);
1307 autofill_test::CreateTestFormField(
1308 "Card Number:", "card_number", "5500 0000 0000 0004", "text", &field);
1309 form2.fields.push_back(field);
1310 autofill_test::CreateTestFormField(
1311 "Exp Month:", "exp_month", "02", "text", &field);
1312 form2.fields.push_back(field);
1313 autofill_test::CreateTestFormField(
1314 "Exp Year:", "exp_year", "2012", "text", &field);
1315 form2.fields.push_back(field);
1316
1317 FormStructure form_structure2(form2, std::string());
1318 form_structure2.DetermineHeuristicTypes(TestAutofillMetrics());
1319 EXPECT_TRUE(personal_data_->ImportFormData(form_structure2,
1320 &imported_credit_card));
1321 ASSERT_TRUE(imported_credit_card);
1322 personal_data_->SaveImportedCreditCard(*imported_credit_card);
1323 delete imported_credit_card;
1324
1325 // Verify that the web database has been updated and the notification sent.
1326 EXPECT_CALL(personal_data_observer_,
1327 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
1328 MessageLoop::current()->Run();
1329
1330 CreditCard expected2;
1331 autofill_test::SetCreditCardInfo(&expected2,
1332 "", "5500000000000004", "02", "2012");
1333 const std::vector<CreditCard*>& results2 = personal_data_->credit_cards();
1334 ASSERT_EQ(2U, results2.size());
1335 EXPECT_EQ(0, expected.Compare(*results2[0]));
1336 EXPECT_EQ(0, expected2.Compare(*results2[1]));
1337 }
1338
1339 TEST_F(PersonalDataManagerTest, AggregateInvalidCreditCard) {
1340 FormData form1;
1341
1342 // Start with a single valid credit card form.
1343 FormFieldData field;
1344 autofill_test::CreateTestFormField(
1345 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1346 form1.fields.push_back(field);
1347 autofill_test::CreateTestFormField(
1348 "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
1349 form1.fields.push_back(field);
1350 autofill_test::CreateTestFormField(
1351 "Exp Month:", "exp_month", "01", "text", &field);
1352 form1.fields.push_back(field);
1353 autofill_test::CreateTestFormField(
1354 "Exp Year:", "exp_year", "2011", "text", &field);
1355 form1.fields.push_back(field);
1356
1357 FormStructure form_structure1(form1, std::string());
1358 form_structure1.DetermineHeuristicTypes(TestAutofillMetrics());
1359 const CreditCard* imported_credit_card;
1360 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1361 &imported_credit_card));
1362 ASSERT_TRUE(imported_credit_card);
1363 personal_data_->SaveImportedCreditCard(*imported_credit_card);
1364 delete imported_credit_card;
1365
1366 // Verify that the web database has been updated and the notification sent.
1367 EXPECT_CALL(personal_data_observer_,
1368 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
1369 MessageLoop::current()->Run();
1370
1371 CreditCard expected;
1372 autofill_test::SetCreditCardInfo(&expected,
1373 "Biggie Smalls", "4111111111111111", "01", "2011");
1374 const std::vector<CreditCard*>& results = personal_data_->credit_cards();
1375 ASSERT_EQ(1U, results.size());
1376 EXPECT_EQ(0, expected.Compare(*results[0]));
1377
1378 // Add a second different invalid credit card.
1379 FormData form2;
1380 autofill_test::CreateTestFormField(
1381 "Name on card:", "name_on_card", "Jim Johansen", "text", &field);
1382 form2.fields.push_back(field);
1383 autofill_test::CreateTestFormField(
1384 "Card Number:", "card_number", "1000000000000000", "text", &field);
1385 form2.fields.push_back(field);
1386 autofill_test::CreateTestFormField(
1387 "Exp Month:", "exp_month", "02", "text", &field);
1388 form2.fields.push_back(field);
1389 autofill_test::CreateTestFormField(
1390 "Exp Year:", "exp_year", "2012", "text", &field);
1391 form2.fields.push_back(field);
1392
1393 FormStructure form_structure2(form2, std::string());
1394 form_structure2.DetermineHeuristicTypes(TestAutofillMetrics());
1395 EXPECT_FALSE(personal_data_->ImportFormData(form_structure2,
1396 &imported_credit_card));
1397 ASSERT_FALSE(imported_credit_card);
1398
1399 // Note: no refresh here.
1400
1401 const std::vector<CreditCard*>& results2 = personal_data_->credit_cards();
1402 ASSERT_EQ(1U, results2.size());
1403 EXPECT_EQ(0, expected.Compare(*results2[0]));
1404 }
1405
1406 TEST_F(PersonalDataManagerTest, AggregateSameCreditCardWithConflict) {
1407 FormData form1;
1408
1409 // Start with a single valid credit card form.
1410 FormFieldData field;
1411 autofill_test::CreateTestFormField(
1412 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1413 form1.fields.push_back(field);
1414 autofill_test::CreateTestFormField(
1415 "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
1416 form1.fields.push_back(field);
1417 autofill_test::CreateTestFormField(
1418 "Exp Month:", "exp_month", "01", "text", &field);
1419 form1.fields.push_back(field);
1420 autofill_test::CreateTestFormField(
1421 "Exp Year:", "exp_year", "2011", "text", &field);
1422 form1.fields.push_back(field);
1423
1424 FormStructure form_structure1(form1, std::string());
1425 form_structure1.DetermineHeuristicTypes(TestAutofillMetrics());
1426 const CreditCard* imported_credit_card;
1427 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1428 &imported_credit_card));
1429 ASSERT_TRUE(imported_credit_card);
1430 personal_data_->SaveImportedCreditCard(*imported_credit_card);
1431 delete imported_credit_card;
1432
1433 // Verify that the web database has been updated and the notification sent.
1434 EXPECT_CALL(personal_data_observer_,
1435 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
1436 MessageLoop::current()->Run();
1437
1438 CreditCard expected;
1439 autofill_test::SetCreditCardInfo(&expected,
1440 "Biggie Smalls", "4111111111111111", "01", "2011");
1441 const std::vector<CreditCard*>& results = personal_data_->credit_cards();
1442 ASSERT_EQ(1U, results.size());
1443 EXPECT_EQ(0, expected.Compare(*results[0]));
1444
1445 // Add a second different valid credit card where the year is different but
1446 // the credit card number matches.
1447 FormData form2;
1448 autofill_test::CreateTestFormField(
1449 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1450 form2.fields.push_back(field);
1451 autofill_test::CreateTestFormField(
1452 "Card Number:", "card_number", "4111 1111 1111 1111", "text", &field);
1453 form2.fields.push_back(field);
1454 autofill_test::CreateTestFormField(
1455 "Exp Month:", "exp_month", "01", "text", &field);
1456 form2.fields.push_back(field);
1457 autofill_test::CreateTestFormField(
1458 "Exp Year:", "exp_year", "2012", "text", &field);
1459 form2.fields.push_back(field);
1460
1461 FormStructure form_structure2(form2, std::string());
1462 form_structure2.DetermineHeuristicTypes(TestAutofillMetrics());
1463 EXPECT_TRUE(personal_data_->ImportFormData(form_structure2,
1464 &imported_credit_card));
1465 EXPECT_FALSE(imported_credit_card);
1466
1467 // Verify that the web database has been updated and the notification sent.
1468 EXPECT_CALL(personal_data_observer_,
1469 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
1470 MessageLoop::current()->Run();
1471
1472 // Expect that the newer information is saved. In this case the year is
1473 // updated to "2012".
1474 CreditCard expected2;
1475 autofill_test::SetCreditCardInfo(&expected2,
1476 "Biggie Smalls", "4111111111111111", "01", "2012");
1477 const std::vector<CreditCard*>& results2 = personal_data_->credit_cards();
1478 ASSERT_EQ(1U, results2.size());
1479 EXPECT_EQ(0, expected2.Compare(*results2[0]));
1480 }
1481
1482 TEST_F(PersonalDataManagerTest, AggregateEmptyCreditCardWithConflict) {
1483 FormData form1;
1484
1485 // Start with a single valid credit card form.
1486 FormFieldData field;
1487 autofill_test::CreateTestFormField(
1488 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1489 form1.fields.push_back(field);
1490 autofill_test::CreateTestFormField(
1491 "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
1492 form1.fields.push_back(field);
1493 autofill_test::CreateTestFormField(
1494 "Exp Month:", "exp_month", "01", "text", &field);
1495 form1.fields.push_back(field);
1496 autofill_test::CreateTestFormField(
1497 "Exp Year:", "exp_year", "2011", "text", &field);
1498 form1.fields.push_back(field);
1499
1500 FormStructure form_structure1(form1, std::string());
1501 form_structure1.DetermineHeuristicTypes(TestAutofillMetrics());
1502 const CreditCard* imported_credit_card;
1503 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1504 &imported_credit_card));
1505 ASSERT_TRUE(imported_credit_card);
1506 personal_data_->SaveImportedCreditCard(*imported_credit_card);
1507 delete imported_credit_card;
1508
1509 // Verify that the web database has been updated and the notification sent.
1510 EXPECT_CALL(personal_data_observer_,
1511 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
1512 MessageLoop::current()->Run();
1513
1514 CreditCard expected;
1515 autofill_test::SetCreditCardInfo(&expected,
1516 "Biggie Smalls", "4111111111111111", "01", "2011");
1517 const std::vector<CreditCard*>& results = personal_data_->credit_cards();
1518 ASSERT_EQ(1U, results.size());
1519 EXPECT_EQ(0, expected.Compare(*results[0]));
1520
1521 // Add a second credit card with no number.
1522 FormData form2;
1523 autofill_test::CreateTestFormField(
1524 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1525 form2.fields.push_back(field);
1526 autofill_test::CreateTestFormField(
1527 "Exp Month:", "exp_month", "01", "text", &field);
1528 form2.fields.push_back(field);
1529 autofill_test::CreateTestFormField(
1530 "Exp Year:", "exp_year", "2012", "text", &field);
1531 form2.fields.push_back(field);
1532
1533 FormStructure form_structure2(form2, std::string());
1534 form_structure2.DetermineHeuristicTypes(TestAutofillMetrics());
1535 EXPECT_FALSE(personal_data_->ImportFormData(form_structure2,
1536 &imported_credit_card));
1537 EXPECT_FALSE(imported_credit_card);
1538
1539 // Note: no refresh here.
1540
1541 // No change is expected.
1542 CreditCard expected2;
1543 autofill_test::SetCreditCardInfo(&expected2,
1544 "Biggie Smalls", "4111111111111111", "01", "2011");
1545 const std::vector<CreditCard*>& results2 = personal_data_->credit_cards();
1546 ASSERT_EQ(1U, results2.size());
1547 EXPECT_EQ(0, expected2.Compare(*results2[0]));
1548 }
1549
1550 TEST_F(PersonalDataManagerTest, AggregateCreditCardWithMissingInfoInNew) {
1551 FormData form1;
1552
1553 // Start with a single valid credit card form.
1554 FormFieldData field;
1555 autofill_test::CreateTestFormField(
1556 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1557 form1.fields.push_back(field);
1558 autofill_test::CreateTestFormField(
1559 "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
1560 form1.fields.push_back(field);
1561 autofill_test::CreateTestFormField(
1562 "Exp Month:", "exp_month", "01", "text", &field);
1563 form1.fields.push_back(field);
1564 autofill_test::CreateTestFormField(
1565 "Exp Year:", "exp_year", "2011", "text", &field);
1566 form1.fields.push_back(field);
1567
1568 FormStructure form_structure1(form1, std::string());
1569 form_structure1.DetermineHeuristicTypes(TestAutofillMetrics());
1570 const CreditCard* imported_credit_card;
1571 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1572 &imported_credit_card));
1573 ASSERT_TRUE(imported_credit_card);
1574 personal_data_->SaveImportedCreditCard(*imported_credit_card);
1575 delete imported_credit_card;
1576
1577 // Verify that the web database has been updated and the notification sent.
1578 EXPECT_CALL(personal_data_observer_,
1579 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
1580 MessageLoop::current()->Run();
1581
1582 CreditCard expected;
1583 autofill_test::SetCreditCardInfo(&expected,
1584 "Biggie Smalls", "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 name is missing but
1590 // the credit card number matches.
1591 FormData form2;
1592 // Note missing name.
1593 autofill_test::CreateTestFormField(
1594 "Card Number:", "card_number", "4111111111111111", "text", &field);
1595 form2.fields.push_back(field);
1596 autofill_test::CreateTestFormField(
1597 "Exp Month:", "exp_month", "01", "text", &field);
1598 form2.fields.push_back(field);
1599 autofill_test::CreateTestFormField(
1600 "Exp Year:", "exp_year", "2011", "text", &field);
1601 form2.fields.push_back(field);
1602
1603 FormStructure form_structure2(form2, std::string());
1604 form_structure2.DetermineHeuristicTypes(TestAutofillMetrics());
1605 EXPECT_TRUE(personal_data_->ImportFormData(form_structure2,
1606 &imported_credit_card));
1607 EXPECT_FALSE(imported_credit_card);
1608
1609 // Wait for the refresh, which in this case is a no-op.
1610 EXPECT_CALL(personal_data_observer_,
1611 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
1612 MessageLoop::current()->Run();
1613
1614 // No change is expected.
1615 CreditCard expected2;
1616 autofill_test::SetCreditCardInfo(&expected2,
1617 "Biggie Smalls", "4111111111111111", "01", "2011");
1618 const std::vector<CreditCard*>& results2 = personal_data_->credit_cards();
1619 ASSERT_EQ(1U, results2.size());
1620 EXPECT_EQ(0, expected2.Compare(*results2[0]));
1621
1622 // Add a third credit card where the expiration date is missing.
1623 FormData form3;
1624 autofill_test::CreateTestFormField(
1625 "Name on card:", "name_on_card", "Johnny McEnroe", "text", &field);
1626 form3.fields.push_back(field);
1627 autofill_test::CreateTestFormField(
1628 "Card Number:", "card_number", "5555555555554444", "text", &field);
1629 form3.fields.push_back(field);
1630 // Note missing expiration month and year..
1631
1632 FormStructure form_structure3(form3, std::string());
1633 form_structure3.DetermineHeuristicTypes(TestAutofillMetrics());
1634 EXPECT_FALSE(personal_data_->ImportFormData(form_structure3,
1635 &imported_credit_card));
1636 ASSERT_FALSE(imported_credit_card);
1637
1638 // Note: no refresh here.
1639
1640 // No change is expected.
1641 CreditCard expected3;
1642 autofill_test::SetCreditCardInfo(&expected3,
1643 "Biggie Smalls", "4111111111111111", "01", "2011");
1644 const std::vector<CreditCard*>& results3 = personal_data_->credit_cards();
1645 ASSERT_EQ(1U, results3.size());
1646 EXPECT_EQ(0, expected3.Compare(*results2[0]));
1647 }
1648
1649 TEST_F(PersonalDataManagerTest, AggregateCreditCardWithMissingInfoInOld) {
1650 // Start with a single valid credit card stored via the preferences.
1651 // Note the empty name.
1652 CreditCard saved_credit_card;
1653 autofill_test::SetCreditCardInfo(&saved_credit_card,
1654 "", "4111111111111111" /* Visa */, "01", "2011");
1655 personal_data_->AddCreditCard(saved_credit_card);
1656
1657 // Verify that the web database has been updated and the notification sent.
1658 EXPECT_CALL(personal_data_observer_,
1659 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
1660 MessageLoop::current()->Run();
1661
1662 const std::vector<CreditCard*>& results1 = personal_data_->credit_cards();
1663 ASSERT_EQ(1U, results1.size());
1664 EXPECT_EQ(saved_credit_card, *results1[0]);
1665
1666
1667 // Add a second different valid credit card where the year is different but
1668 // the credit card number matches.
1669 FormData form;
1670 FormFieldData field;
1671 autofill_test::CreateTestFormField(
1672 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1673 form.fields.push_back(field);
1674 autofill_test::CreateTestFormField(
1675 "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
1676 form.fields.push_back(field);
1677 autofill_test::CreateTestFormField(
1678 "Exp Month:", "exp_month", "01", "text", &field);
1679 form.fields.push_back(field);
1680 autofill_test::CreateTestFormField(
1681 "Exp Year:", "exp_year", "2012", "text", &field);
1682 form.fields.push_back(field);
1683
1684 FormStructure form_structure(form, std::string());
1685 form_structure.DetermineHeuristicTypes(TestAutofillMetrics());
1686 const CreditCard* imported_credit_card;
1687 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
1688 &imported_credit_card));
1689 EXPECT_FALSE(imported_credit_card);
1690
1691 // Verify that the web database has been updated and the notification sent.
1692 EXPECT_CALL(personal_data_observer_,
1693 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
1694 MessageLoop::current()->Run();
1695
1696 // Expect that the newer information is saved. In this case the year is
1697 // added to the existing credit card.
1698 CreditCard expected2;
1699 autofill_test::SetCreditCardInfo(&expected2,
1700 "Biggie Smalls", "4111111111111111", "01", "2012");
1701 const std::vector<CreditCard*>& results2 = personal_data_->credit_cards();
1702 ASSERT_EQ(1U, results2.size());
1703 EXPECT_EQ(0, expected2.Compare(*results2[0]));
1704 }
1705
1706 // We allow the user to store a credit card number with separators via the UI.
1707 // We should not try to re-aggregate the same card with the separators stripped.
1708 TEST_F(PersonalDataManagerTest, AggregateSameCreditCardWithSeparators) {
1709 // Start with a single valid credit card stored via the preferences.
1710 // Note the separators in the credit card number.
1711 CreditCard saved_credit_card;
1712 autofill_test::SetCreditCardInfo(&saved_credit_card,
1713 "Biggie Smalls", "4111 1111 1111 1111" /* Visa */, "01", "2011");
1714 personal_data_->AddCreditCard(saved_credit_card);
1715
1716 // Verify that the web database has been updated and the notification sent.
1717 EXPECT_CALL(personal_data_observer_,
1718 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
1719 MessageLoop::current()->Run();
1720
1721 const std::vector<CreditCard*>& results1 = personal_data_->credit_cards();
1722 ASSERT_EQ(1U, results1.size());
1723 EXPECT_EQ(0, saved_credit_card.Compare(*results1[0]));
1724
1725 // Import the same card info, but with different separators in the number.
1726 FormData form;
1727 FormFieldData field;
1728 autofill_test::CreateTestFormField(
1729 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1730 form.fields.push_back(field);
1731 autofill_test::CreateTestFormField(
1732 "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
1733 form.fields.push_back(field);
1734 autofill_test::CreateTestFormField(
1735 "Exp Month:", "exp_month", "01", "text", &field);
1736 form.fields.push_back(field);
1737 autofill_test::CreateTestFormField(
1738 "Exp Year:", "exp_year", "2011", "text", &field);
1739 form.fields.push_back(field);
1740
1741 FormStructure form_structure(form, std::string());
1742 form_structure.DetermineHeuristicTypes(TestAutofillMetrics());
1743 const CreditCard* imported_credit_card;
1744 EXPECT_TRUE(personal_data_->ImportFormData(form_structure,
1745 &imported_credit_card));
1746 EXPECT_FALSE(imported_credit_card);
1747
1748 // Wait for the refresh, which in this case is a no-op.
1749 EXPECT_CALL(personal_data_observer_,
1750 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
1751 MessageLoop::current()->Run();
1752
1753 // Expect that no new card is saved.
1754 const std::vector<CreditCard*>& results2 = personal_data_->credit_cards();
1755 ASSERT_EQ(1U, results2.size());
1756 EXPECT_EQ(0, saved_credit_card.Compare(*results2[0]));
1757 }
1758
1759 TEST_F(PersonalDataManagerTest, GetNonEmptyTypes) {
1760 // Check that there are no available types with no profiles stored.
1761 FieldTypeSet non_empty_types;
1762 personal_data_->GetNonEmptyTypes(&non_empty_types);
1763 EXPECT_EQ(0U, non_empty_types.size());
1764
1765 // Test with one profile stored.
1766 AutofillProfile profile0;
1767 autofill_test::SetProfileInfo(&profile0,
1768 "Marion", NULL, "Morrison",
1769 "johnwayne@me.xyz", NULL, "123 Zoo St.", NULL, "Hollywood", "CA",
1770 "91601", "US", "14155678910");
1771
1772 personal_data_->AddProfile(profile0);
1773
1774 // Verify that the web database has been updated and the notification sent.
1775 EXPECT_CALL(personal_data_observer_,
1776 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
1777 MessageLoop::current()->Run();
1778
1779 personal_data_->GetNonEmptyTypes(&non_empty_types);
1780 EXPECT_EQ(14U, non_empty_types.size());
1781 EXPECT_TRUE(non_empty_types.count(NAME_FIRST));
1782 EXPECT_TRUE(non_empty_types.count(NAME_LAST));
1783 EXPECT_TRUE(non_empty_types.count(NAME_FULL));
1784 EXPECT_TRUE(non_empty_types.count(EMAIL_ADDRESS));
1785 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_LINE1));
1786 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_CITY));
1787 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_STATE));
1788 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_ZIP));
1789 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_COUNTRY));
1790 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_NUMBER));
1791 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_COUNTRY_CODE));
1792 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_CITY_CODE));
1793 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_CITY_AND_NUMBER));
1794 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_WHOLE_NUMBER));
1795
1796 // Test with multiple profiles stored.
1797 AutofillProfile profile1;
1798 autofill_test::SetProfileInfo(&profile1,
1799 "Josephine", "Alicia", "Saenz",
1800 "joewayne@me.xyz", "Fox", "903 Apple Ct.", NULL, "Orlando", "FL", "32801",
1801 "US", "16502937549");
1802
1803 AutofillProfile profile2;
1804 autofill_test::SetProfileInfo(&profile2,
1805 "Josephine", "Alicia", "Saenz",
1806 "joewayne@me.xyz", "Fox", "1212 Center.", "Bld. 5", "Orlando", "FL",
1807 "32801", "US", "16502937549");
1808
1809 personal_data_->AddProfile(profile1);
1810 personal_data_->AddProfile(profile2);
1811
1812 // Verify that the web database has been updated and the notification sent.
1813 EXPECT_CALL(personal_data_observer_,
1814 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
1815 MessageLoop::current()->Run();
1816
1817 personal_data_->GetNonEmptyTypes(&non_empty_types);
1818 EXPECT_EQ(18U, non_empty_types.size());
1819 EXPECT_TRUE(non_empty_types.count(NAME_FIRST));
1820 EXPECT_TRUE(non_empty_types.count(NAME_MIDDLE));
1821 EXPECT_TRUE(non_empty_types.count(NAME_MIDDLE_INITIAL));
1822 EXPECT_TRUE(non_empty_types.count(NAME_LAST));
1823 EXPECT_TRUE(non_empty_types.count(NAME_FULL));
1824 EXPECT_TRUE(non_empty_types.count(EMAIL_ADDRESS));
1825 EXPECT_TRUE(non_empty_types.count(COMPANY_NAME));
1826 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_LINE1));
1827 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_LINE2));
1828 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_CITY));
1829 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_STATE));
1830 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_ZIP));
1831 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_COUNTRY));
1832 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_NUMBER));
1833 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_CITY_CODE));
1834 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_COUNTRY_CODE));
1835 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_CITY_AND_NUMBER));
1836 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_WHOLE_NUMBER));
1837
1838 // Test with credit card information also stored.
1839 CreditCard credit_card;
1840 autofill_test::SetCreditCardInfo(&credit_card,
1841 "John Dillinger", "423456789012" /* Visa */,
1842 "01", "2010");
1843 personal_data_->AddCreditCard(credit_card);
1844
1845 // Verify that the web database has been updated and the notification sent.
1846 EXPECT_CALL(personal_data_observer_,
1847 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
1848 MessageLoop::current()->Run();
1849
1850 personal_data_->GetNonEmptyTypes(&non_empty_types);
1851 EXPECT_EQ(25U, non_empty_types.size());
1852 EXPECT_TRUE(non_empty_types.count(NAME_FIRST));
1853 EXPECT_TRUE(non_empty_types.count(NAME_MIDDLE));
1854 EXPECT_TRUE(non_empty_types.count(NAME_MIDDLE_INITIAL));
1855 EXPECT_TRUE(non_empty_types.count(NAME_LAST));
1856 EXPECT_TRUE(non_empty_types.count(NAME_FULL));
1857 EXPECT_TRUE(non_empty_types.count(EMAIL_ADDRESS));
1858 EXPECT_TRUE(non_empty_types.count(COMPANY_NAME));
1859 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_LINE1));
1860 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_LINE2));
1861 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_CITY));
1862 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_STATE));
1863 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_ZIP));
1864 EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_COUNTRY));
1865 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_NUMBER));
1866 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_CITY_CODE));
1867 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_COUNTRY_CODE));
1868 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_CITY_AND_NUMBER));
1869 EXPECT_TRUE(non_empty_types.count(PHONE_HOME_WHOLE_NUMBER));
1870 EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_NAME));
1871 EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_NUMBER));
1872 EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_EXP_MONTH));
1873 EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_EXP_2_DIGIT_YEAR));
1874 EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_EXP_4_DIGIT_YEAR));
1875 EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR));
1876 EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR));
1877 }
1878
1879 TEST_F(PersonalDataManagerTest, CaseInsensitiveMultiValueAggregation) {
1880 FormData form1;
1881 FormFieldData field;
1882 autofill_test::CreateTestFormField(
1883 "First name:", "first_name", "George", "text", &field);
1884 form1.fields.push_back(field);
1885 autofill_test::CreateTestFormField(
1886 "Last name:", "last_name", "Washington", "text", &field);
1887 form1.fields.push_back(field);
1888 autofill_test::CreateTestFormField(
1889 "Email:", "email", "theprez@gmail.com", "text", &field);
1890 form1.fields.push_back(field);
1891 autofill_test::CreateTestFormField(
1892 "Address:", "address1", "21 Laussat St", "text", &field);
1893 form1.fields.push_back(field);
1894 autofill_test::CreateTestFormField(
1895 "City:", "city", "San Francisco", "text", &field);
1896 form1.fields.push_back(field);
1897 autofill_test::CreateTestFormField(
1898 "State:", "state", "California", "text", &field);
1899 form1.fields.push_back(field);
1900 autofill_test::CreateTestFormField(
1901 "Zip:", "zip", "94102", "text", &field);
1902 form1.fields.push_back(field);
1903 autofill_test::CreateTestFormField(
1904 "Phone number:", "phone_number", "817-555-6789", "text", &field);
1905 form1.fields.push_back(field);
1906
1907 FormStructure form_structure1(form1, std::string());
1908 form_structure1.DetermineHeuristicTypes(TestAutofillMetrics());
1909 const CreditCard* imported_credit_card;
1910 EXPECT_TRUE(personal_data_->ImportFormData(form_structure1,
1911 &imported_credit_card));
1912 ASSERT_FALSE(imported_credit_card);
1913
1914 // Verify that the web database has been updated and the notification sent.
1915 EXPECT_CALL(personal_data_observer_,
1916 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
1917 MessageLoop::current()->Run();
1918
1919 AutofillProfile expected;
1920 autofill_test::SetProfileInfo(&expected, "George", NULL,
1921 "Washington", "theprez@gmail.com", NULL, "21 Laussat St", NULL,
1922 "San Francisco", "California", "94102", NULL, "(817) 555-6789");
1923 const std::vector<AutofillProfile*>& results1 = personal_data_->GetProfiles();
1924 ASSERT_EQ(1U, results1.size());
1925 EXPECT_EQ(0, expected.Compare(*results1[0]));
1926
1927 // Upper-case the first name and change the phone number.
1928 FormData form2;
1929 autofill_test::CreateTestFormField(
1930 "First name:", "first_name", "GEORGE", "text", &field);
1931 form2.fields.push_back(field);
1932 autofill_test::CreateTestFormField(
1933 "Last name:", "last_name", "Washington", "text", &field);
1934 form2.fields.push_back(field);
1935 autofill_test::CreateTestFormField(
1936 "Email:", "email", "theprez@gmail.com", "text", &field);
1937 form2.fields.push_back(field);
1938 autofill_test::CreateTestFormField(
1939 "Address:", "address1", "21 Laussat St", "text", &field);
1940 form2.fields.push_back(field);
1941 autofill_test::CreateTestFormField(
1942 "City:", "city", "San Francisco", "text", &field);
1943 form2.fields.push_back(field);
1944 autofill_test::CreateTestFormField(
1945 "State:", "state", "California", "text", &field);
1946 form2.fields.push_back(field);
1947 autofill_test::CreateTestFormField(
1948 "Zip:", "zip", "94102", "text", &field);
1949 form2.fields.push_back(field);
1950 autofill_test::CreateTestFormField(
1951 "Phone number:", "phone_number", "214-555-1234", "text", &field);
1952 form2.fields.push_back(field);
1953
1954 FormStructure form_structure2(form2, std::string());
1955 form_structure2.DetermineHeuristicTypes(TestAutofillMetrics());
1956 EXPECT_TRUE(personal_data_->ImportFormData(form_structure2,
1957 &imported_credit_card));
1958 ASSERT_FALSE(imported_credit_card);
1959
1960 // Verify that the web database has been updated and the notification sent.
1961 EXPECT_CALL(personal_data_observer_,
1962 OnPersonalDataChanged()).WillOnce(QuitUIMessageLoop());
1963 MessageLoop::current()->Run();
1964
1965 const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
1966
1967 // Modify expected to include multi-valued fields.
1968 std::vector<string16> values;
1969 expected.GetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, &values);
1970 values.push_back(ASCIIToUTF16("(214) 555-1234"));
1971 expected.SetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, values);
1972
1973 ASSERT_EQ(1U, results2.size());
1974 EXPECT_EQ(0, expected.Compare(*results2[0]));
1975 }
OLDNEW
« no previous file with comments | « chrome/browser/autofill/personal_data_manager_observer.h ('k') | chrome/browser/autofill/phone_field.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698