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