autofill_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 <algorithm> 6#include <vector> 7 8#include "base/command_line.h" 9#include "base/format_macros.h" 10#include "base/memory/ref_counted.h" 11#include "base/memory/scoped_ptr.h" 12#include "base/memory/scoped_vector.h" 13#include "base/prefs/pref_service.h" 14#include "base/strings/string16.h" 15#include "base/strings/string_number_conversions.h" 16#include "base/strings/stringprintf.h" 17#include "base/strings/utf_string_conversions.h" 18#include "base/time/time.h" 19#include "base/tuple.h" 20#include "chrome/browser/autofill/personal_data_manager_factory.h" 21#include "chrome/browser/password_manager/chrome_password_manager_client.h" 22#include "chrome/browser/password_manager/password_manager.h" 23#include "chrome/browser/profiles/profile.h" 24#include "chrome/browser/sync/profile_sync_service.h" 25#include "chrome/browser/sync/profile_sync_service_factory.h" 26#include "chrome/browser/ui/autofill/tab_autofill_manager_delegate.h" 27#include "chrome/test/base/chrome_render_view_host_test_harness.h" 28#include "chrome/test/base/testing_profile.h" 29#include "components/autofill/core/browser/autocomplete_history_manager.h" 30#include "components/autofill/core/browser/autofill_manager.h" 31#include "components/autofill/core/browser/autofill_metrics.h" 32#include "components/autofill/core/browser/autofill_profile.h" 33#include "components/autofill/core/browser/autofill_test_utils.h" 34#include "components/autofill/core/browser/credit_card.h" 35#include "components/autofill/core/browser/personal_data_manager.h" 36#include "components/autofill/core/browser/test_autofill_driver.h" 37#include "components/autofill/core/browser/test_autofill_external_delegate.h" 38#include "components/autofill/core/browser/test_autofill_manager_delegate.h" 39#include "components/autofill/core/browser/webdata/autofill_webdata_service.h" 40#include "components/autofill/core/common/autofill_pref_names.h" 41#include "components/autofill/core/common/form_data.h" 42#include "components/autofill/core/common/form_field_data.h" 43#include "components/autofill/core/common/forms_seen_state.h" 44#include "components/user_prefs/user_prefs.h" 45#include "content/public/test/mock_render_process_host.h" 46#include "content/public/test/test_utils.h" 47#include "grit/component_strings.h" 48#include "testing/gmock/include/gmock/gmock.h" 49#include "testing/gtest/include/gtest/gtest.h" 50#include "third_party/WebKit/public/web/WebAutofillClient.h" 51#include "ui/base/l10n/l10n_util.h" 52#include "ui/gfx/rect.h" 53#include "url/gurl.h" 54 55using base::ASCIIToUTF16; 56using base::UTF8ToUTF16; 57using content::WebContents; 58using testing::_; 59 60namespace autofill { 61 62typedef PersonalDataManager::GUIDPair GUIDPair; 63 64namespace { 65 66// The page ID sent to the AutofillManager from the RenderView, used to send 67// an IPC message back to the renderer. 68const int kDefaultPageID = 137; 69 70class TestPersonalDataManager : public PersonalDataManager { 71 public: 72 TestPersonalDataManager() : PersonalDataManager("en-US") { 73 CreateTestAutofillProfiles(&web_profiles_); 74 CreateTestCreditCards(&credit_cards_); 75 } 76 77 using PersonalDataManager::set_database; 78 using PersonalDataManager::set_pref_service; 79 80 // Factory method for keyed service. PersonalDataManager is NULL for testing. 81 static BrowserContextKeyedService* Build(content::BrowserContext* profile) { 82 return NULL; 83 } 84 85 MOCK_METHOD1(SaveImportedProfile, std::string(const AutofillProfile&)); 86 87 AutofillProfile* GetProfileWithGUID(const char* guid) { 88 for (std::vector<AutofillProfile *>::iterator it = web_profiles_.begin(); 89 it != web_profiles_.end(); ++it) { 90 if (!(*it)->guid().compare(guid)) 91 return *it; 92 } 93 return NULL; 94 } 95 96 CreditCard* GetCreditCardWithGUID(const char* guid) { 97 for (std::vector<CreditCard *>::iterator it = credit_cards_.begin(); 98 it != credit_cards_.end(); ++it){ 99 if (!(*it)->guid().compare(guid)) 100 return *it; 101 } 102 return NULL; 103 } 104 105 void AddProfile(AutofillProfile* profile) { 106 web_profiles_.push_back(profile); 107 } 108 109 void AddCreditCard(CreditCard* credit_card) { 110 credit_cards_.push_back(credit_card); 111 } 112 113 virtual void RemoveByGUID(const std::string& guid) OVERRIDE { 114 CreditCard* credit_card = GetCreditCardWithGUID(guid.c_str()); 115 if (credit_card) { 116 credit_cards_.erase( 117 std::remove(credit_cards_.begin(), credit_cards_.end(), credit_card), 118 credit_cards_.end()); 119 } 120 121 AutofillProfile* profile = GetProfileWithGUID(guid.c_str()); 122 if (profile) { 123 web_profiles_.erase( 124 std::remove(web_profiles_.begin(), web_profiles_.end(), profile), 125 web_profiles_.end()); 126 } 127 } 128 129 // Do nothing (auxiliary profiles will be created in 130 // CreateTestAuxiliaryProfile). 131 virtual void LoadAuxiliaryProfiles() const OVERRIDE {} 132 133 void ClearAutofillProfiles() { 134 web_profiles_.clear(); 135 } 136 137 void ClearCreditCards() { 138 credit_cards_.clear(); 139 } 140 141 void CreateTestAuxiliaryProfiles() { 142 CreateTestAutofillProfiles(&auxiliary_profiles_); 143 } 144 145 void CreateTestCreditCardsYearAndMonth(const char* year, const char* month) { 146 ClearCreditCards(); 147 CreditCard* credit_card = new CreditCard; 148 test::SetCreditCardInfo(credit_card, "Miku Hatsune", 149 "4234567890654321", // Visa 150 month, year); 151 credit_card->set_guid("00000000-0000-0000-0000-000000000007"); 152 credit_cards_.push_back(credit_card); 153 } 154 155 private: 156 void CreateTestAutofillProfiles(ScopedVector<AutofillProfile>* profiles) { 157 AutofillProfile* profile = new AutofillProfile; 158 test::SetProfileInfo(profile, "Elvis", "Aaron", 159 "Presley", "theking@gmail.com", "RCA", 160 "3734 Elvis Presley Blvd.", "Apt. 10", 161 "Memphis", "Tennessee", "38116", "US", 162 "12345678901"); 163 profile->set_guid("00000000-0000-0000-0000-000000000001"); 164 profiles->push_back(profile); 165 profile = new AutofillProfile; 166 test::SetProfileInfo(profile, "Charles", "Hardin", 167 "Holley", "buddy@gmail.com", "Decca", 168 "123 Apple St.", "unit 6", "Lubbock", 169 "Texas", "79401", "US", "23456789012"); 170 profile->set_guid("00000000-0000-0000-0000-000000000002"); 171 profiles->push_back(profile); 172 profile = new AutofillProfile; 173 test::SetProfileInfo( 174 profile, "", "", "", "", "", "", "", "", "", "", "", ""); 175 profile->set_guid("00000000-0000-0000-0000-000000000003"); 176 profiles->push_back(profile); 177 } 178 179 void CreateTestCreditCards(ScopedVector<CreditCard>* credit_cards) { 180 CreditCard* credit_card = new CreditCard; 181 test::SetCreditCardInfo(credit_card, "Elvis Presley", 182 "4234 5678 9012 3456", // Visa 183 "04", "2012"); 184 credit_card->set_guid("00000000-0000-0000-0000-000000000004"); 185 credit_cards->push_back(credit_card); 186 187 credit_card = new CreditCard; 188 test::SetCreditCardInfo(credit_card, "Buddy Holly", 189 "5187654321098765", // Mastercard 190 "10", "2014"); 191 credit_card->set_guid("00000000-0000-0000-0000-000000000005"); 192 credit_cards->push_back(credit_card); 193 194 credit_card = new CreditCard; 195 test::SetCreditCardInfo(credit_card, "", "", "", ""); 196 credit_card->set_guid("00000000-0000-0000-0000-000000000006"); 197 credit_cards->push_back(credit_card); 198 } 199 200 DISALLOW_COPY_AND_ASSIGN(TestPersonalDataManager); 201}; 202 203// Populates |form| with data corresponding to a simple credit card form. 204// Note that this actually appends fields to the form data, which can be useful 205// for building up more complex test forms. 206void CreateTestCreditCardFormData(FormData* form, 207 bool is_https, 208 bool use_month_type) { 209 form->name = ASCIIToUTF16("MyForm"); 210 form->method = ASCIIToUTF16("POST"); 211 if (is_https) { 212 form->origin = GURL("https://myform.com/form.html"); 213 form->action = GURL("https://myform.com/submit.html"); 214 } else { 215 form->origin = GURL("http://myform.com/form.html"); 216 form->action = GURL("http://myform.com/submit.html"); 217 } 218 form->user_submitted = true; 219 220 FormFieldData field; 221 test::CreateTestFormField("Name on Card", "nameoncard", "", "text", &field); 222 form->fields.push_back(field); 223 test::CreateTestFormField("Card Number", "cardnumber", "", "text", &field); 224 form->fields.push_back(field); 225 if (use_month_type) { 226 test::CreateTestFormField( 227 "Expiration Date", "ccmonth", "", "month", &field); 228 form->fields.push_back(field); 229 } else { 230 test::CreateTestFormField("Expiration Date", "ccmonth", "", "text", &field); 231 form->fields.push_back(field); 232 test::CreateTestFormField("", "ccyear", "", "text", &field); 233 form->fields.push_back(field); 234 } 235} 236 237void ExpectFilledField(const char* expected_label, 238 const char* expected_name, 239 const char* expected_value, 240 const char* expected_form_control_type, 241 const FormFieldData& field) { 242 SCOPED_TRACE(expected_label); 243 EXPECT_EQ(UTF8ToUTF16(expected_label), field.label); 244 EXPECT_EQ(UTF8ToUTF16(expected_name), field.name); 245 EXPECT_EQ(UTF8ToUTF16(expected_value), field.value); 246 EXPECT_EQ(expected_form_control_type, field.form_control_type); 247} 248 249// Verifies that the |filled_form| has been filled with the given data. 250// Verifies address fields if |has_address_fields| is true, and verifies 251// credit card fields if |has_credit_card_fields| is true. Verifies both if both 252// are true. |use_month_type| is used for credit card input month type. 253void ExpectFilledForm(int page_id, 254 const FormData& filled_form, 255 int expected_page_id, 256 const char* first, 257 const char* middle, 258 const char* last, 259 const char* address1, 260 const char* address2, 261 const char* city, 262 const char* state, 263 const char* postal_code, 264 const char* country, 265 const char* phone, 266 const char* email, 267 const char* name_on_card, 268 const char* card_number, 269 const char* expiration_month, 270 const char* expiration_year, 271 bool has_address_fields, 272 bool has_credit_card_fields, 273 bool use_month_type) { 274 // The number of fields in the address and credit card forms created above. 275 const size_t kAddressFormSize = 11; 276 const size_t kCreditCardFormSize = use_month_type ? 3 : 4; 277 278 EXPECT_EQ(expected_page_id, page_id); 279 EXPECT_EQ(ASCIIToUTF16("MyForm"), filled_form.name); 280 EXPECT_EQ(ASCIIToUTF16("POST"), filled_form.method); 281 if (has_credit_card_fields) { 282 EXPECT_EQ(GURL("https://myform.com/form.html"), filled_form.origin); 283 EXPECT_EQ(GURL("https://myform.com/submit.html"), filled_form.action); 284 } else { 285 EXPECT_EQ(GURL("http://myform.com/form.html"), filled_form.origin); 286 EXPECT_EQ(GURL("http://myform.com/submit.html"), filled_form.action); 287 } 288 EXPECT_TRUE(filled_form.user_submitted); 289 290 size_t form_size = 0; 291 if (has_address_fields) 292 form_size += kAddressFormSize; 293 if (has_credit_card_fields) 294 form_size += kCreditCardFormSize; 295 ASSERT_EQ(form_size, filled_form.fields.size()); 296 297 if (has_address_fields) { 298 ExpectFilledField("First Name", "firstname", first, "text", 299 filled_form.fields[0]); 300 ExpectFilledField("Middle Name", "middlename", middle, "text", 301 filled_form.fields[1]); 302 ExpectFilledField("Last Name", "lastname", last, "text", 303 filled_form.fields[2]); 304 ExpectFilledField("Address Line 1", "addr1", address1, "text", 305 filled_form.fields[3]); 306 ExpectFilledField("Address Line 2", "addr2", address2, "text", 307 filled_form.fields[4]); 308 ExpectFilledField("City", "city", city, "text", 309 filled_form.fields[5]); 310 ExpectFilledField("State", "state", state, "text", 311 filled_form.fields[6]); 312 ExpectFilledField("Postal Code", "zipcode", postal_code, "text", 313 filled_form.fields[7]); 314 ExpectFilledField("Country", "country", country, "text", 315 filled_form.fields[8]); 316 ExpectFilledField("Phone Number", "phonenumber", phone, "tel", 317 filled_form.fields[9]); 318 ExpectFilledField("Email", "email", email, "email", 319 filled_form.fields[10]); 320 } 321 322 if (has_credit_card_fields) { 323 size_t offset = has_address_fields? kAddressFormSize : 0; 324 ExpectFilledField("Name on Card", "nameoncard", name_on_card, "text", 325 filled_form.fields[offset + 0]); 326 ExpectFilledField("Card Number", "cardnumber", card_number, "text", 327 filled_form.fields[offset + 1]); 328 if (use_month_type) { 329 std::string exp_year = expiration_year; 330 std::string exp_month = expiration_month; 331 std::string date; 332 if (!exp_year.empty() && !exp_month.empty()) 333 date = exp_year + "-" + exp_month; 334 335 ExpectFilledField("Expiration Date", "ccmonth", date.c_str(), "month", 336 filled_form.fields[offset + 2]); 337 } else { 338 ExpectFilledField("Expiration Date", "ccmonth", expiration_month, "text", 339 filled_form.fields[offset + 2]); 340 ExpectFilledField("", "ccyear", expiration_year, "text", 341 filled_form.fields[offset + 3]); 342 } 343 } 344} 345 346void ExpectFilledAddressFormElvis(int page_id, 347 const FormData& filled_form, 348 int expected_page_id, 349 bool has_credit_card_fields) { 350 ExpectFilledForm(page_id, filled_form, expected_page_id, "Elvis", "Aaron", 351 "Presley", "3734 Elvis Presley Blvd.", "Apt. 10", "Memphis", 352 "Tennessee", "38116", "United States", "12345678901", 353 "theking@gmail.com", "", "", "", "", true, 354 has_credit_card_fields, false); 355} 356 357void ExpectFilledCreditCardFormElvis(int page_id, 358 const FormData& filled_form, 359 int expected_page_id, 360 bool has_address_fields) { 361 ExpectFilledForm(page_id, filled_form, expected_page_id, 362 "", "", "", "", "", "", "", "", "", "", "", 363 "Elvis Presley", "4234567890123456", "04", "2012", 364 has_address_fields, true, false); 365} 366 367void ExpectFilledCreditCardYearMonthWithYearMonth(int page_id, 368 const FormData& filled_form, 369 int expected_page_id, 370 bool has_address_fields, 371 const char* year, 372 const char* month) { 373 ExpectFilledForm(page_id, filled_form, expected_page_id, 374 "", "", "", "", "", "", "", "", "", "", "", 375 "Miku Hatsune", "4234567890654321", month, year, 376 has_address_fields, true, true); 377} 378 379class MockAutocompleteHistoryManager : public AutocompleteHistoryManager { 380 public: 381 MockAutocompleteHistoryManager(AutofillDriver* driver, 382 AutofillManagerDelegate* delegate) 383 : AutocompleteHistoryManager(driver, delegate) {} 384 385 MOCK_METHOD1(OnFormSubmitted, void(const FormData& form)); 386 387 private: 388 DISALLOW_COPY_AND_ASSIGN(MockAutocompleteHistoryManager); 389}; 390 391class MockAutofillDriver : public TestAutofillDriver { 392 public: 393 MockAutofillDriver() {} 394 395 // Mock methods to enable testability. 396 MOCK_METHOD2(SendFormDataToRenderer, void(int query_id, 397 const FormData& data)); 398 399 private: 400 DISALLOW_COPY_AND_ASSIGN(MockAutofillDriver); 401}; 402 403class TestAutofillManager : public AutofillManager { 404 public: 405 TestAutofillManager(AutofillDriver* driver, 406 autofill::AutofillManagerDelegate* delegate, 407 TestPersonalDataManager* personal_data) 408 : AutofillManager(driver, delegate, personal_data), 409 personal_data_(personal_data), 410 autofill_enabled_(true) {} 411 virtual ~TestAutofillManager() {} 412 413 virtual bool IsAutofillEnabled() const OVERRIDE { return autofill_enabled_; } 414 415 void set_autofill_enabled(bool autofill_enabled) { 416 autofill_enabled_ = autofill_enabled; 417 } 418 419 void set_expected_submitted_field_types( 420 const std::vector<ServerFieldTypeSet>& expected_types) { 421 expected_submitted_field_types_ = expected_types; 422 } 423 424 virtual void UploadFormDataAsyncCallback( 425 const FormStructure* submitted_form, 426 const base::TimeTicks& load_time, 427 const base::TimeTicks& interaction_time, 428 const base::TimeTicks& submission_time) OVERRIDE { 429 message_loop_runner_->Quit(); 430 431 // If we have expected field types set, make sure they match. 432 if (!expected_submitted_field_types_.empty()) { 433 ASSERT_EQ(expected_submitted_field_types_.size(), 434 submitted_form->field_count()); 435 for (size_t i = 0; i < expected_submitted_field_types_.size(); ++i) { 436 SCOPED_TRACE( 437 base::StringPrintf( 438 "Field %d with value %s", static_cast<int>(i), 439 base::UTF16ToUTF8(submitted_form->field(i)->value).c_str())); 440 const ServerFieldTypeSet& possible_types = 441 submitted_form->field(i)->possible_types(); 442 EXPECT_EQ(expected_submitted_field_types_[i].size(), 443 possible_types.size()); 444 for (ServerFieldTypeSet::const_iterator it = 445 expected_submitted_field_types_[i].begin(); 446 it != expected_submitted_field_types_[i].end(); ++it) { 447 EXPECT_TRUE(possible_types.count(*it)) 448 << "Expected type: " << AutofillType(*it).ToString(); 449 } 450 } 451 } 452 453 AutofillManager::UploadFormDataAsyncCallback(submitted_form, 454 load_time, 455 interaction_time, 456 submission_time); 457 } 458 459 // Resets the MessageLoopRunner so that it can wait for an asynchronous form 460 // submission to complete. 461 void ResetMessageLoopRunner() { 462 message_loop_runner_ = new content::MessageLoopRunner(); 463 } 464 465 // Wait for the asynchronous OnFormSubmitted() call to complete. 466 void WaitForAsyncFormSubmit() { 467 message_loop_runner_->Run(); 468 } 469 470 virtual void UploadFormData(const FormStructure& submitted_form) OVERRIDE { 471 submitted_form_signature_ = submitted_form.FormSignature(); 472 } 473 474 const std::string GetSubmittedFormSignature() { 475 return submitted_form_signature_; 476 } 477 478 AutofillProfile* GetProfileWithGUID(const char* guid) { 479 return personal_data_->GetProfileWithGUID(guid); 480 } 481 482 CreditCard* GetCreditCardWithGUID(const char* guid) { 483 return personal_data_->GetCreditCardWithGUID(guid); 484 } 485 486 void AddProfile(AutofillProfile* profile) { 487 personal_data_->AddProfile(profile); 488 } 489 490 void AddCreditCard(CreditCard* credit_card) { 491 personal_data_->AddCreditCard(credit_card); 492 } 493 494 int GetPackedCreditCardID(int credit_card_id) { 495 std::string credit_card_guid = 496 base::StringPrintf("00000000-0000-0000-0000-%012d", credit_card_id); 497 498 return PackGUIDs(GUIDPair(credit_card_guid, 0), GUIDPair(std::string(), 0)); 499 } 500 501 void AddSeenForm(FormStructure* form) { 502 form_structures()->push_back(form); 503 } 504 505 void ClearFormStructures() { 506 form_structures()->clear(); 507 } 508 509 private: 510 // Weak reference. 511 TestPersonalDataManager* personal_data_; 512 513 bool autofill_enabled_; 514 515 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; 516 517 std::string submitted_form_signature_; 518 std::vector<ServerFieldTypeSet> expected_submitted_field_types_; 519 520 DISALLOW_COPY_AND_ASSIGN(TestAutofillManager); 521}; 522 523class TestAutofillExternalDelegate : public AutofillExternalDelegate { 524 public: 525 explicit TestAutofillExternalDelegate(content::WebContents* web_contents, 526 AutofillManager* autofill_manager, 527 AutofillDriver* autofill_driver) 528 : AutofillExternalDelegate(autofill_manager, autofill_driver), 529 on_query_seen_(false), 530 on_suggestions_returned_seen_(false) {} 531 virtual ~TestAutofillExternalDelegate() {} 532 533 virtual void OnQuery(int query_id, 534 const FormData& form, 535 const FormFieldData& field, 536 const gfx::RectF& bounds, 537 bool display_warning) OVERRIDE { 538 on_query_seen_ = true; 539 on_suggestions_returned_seen_ = false; 540 } 541 542 virtual void OnSuggestionsReturned( 543 int query_id, 544 const std::vector<base::string16>& autofill_values, 545 const std::vector<base::string16>& autofill_labels, 546 const std::vector<base::string16>& autofill_icons, 547 const std::vector<int>& autofill_unique_ids) OVERRIDE { 548 on_suggestions_returned_seen_ = true; 549 550 query_id_ = query_id; 551 autofill_values_ = autofill_values; 552 autofill_labels_ = autofill_labels; 553 autofill_icons_ = autofill_icons; 554 autofill_unique_ids_ = autofill_unique_ids; 555 } 556 557 void CheckSuggestions(int expected_page_id, 558 size_t expected_num_suggestions, 559 const base::string16 expected_values[], 560 const base::string16 expected_labels[], 561 const base::string16 expected_icons[], 562 const int expected_unique_ids[]) { 563 // Ensure that these results are from the most recent query. 564 EXPECT_TRUE(on_suggestions_returned_seen_); 565 566 EXPECT_EQ(expected_page_id, query_id_); 567 ASSERT_EQ(expected_num_suggestions, autofill_values_.size()); 568 ASSERT_EQ(expected_num_suggestions, autofill_labels_.size()); 569 ASSERT_EQ(expected_num_suggestions, autofill_icons_.size()); 570 ASSERT_EQ(expected_num_suggestions, autofill_unique_ids_.size()); 571 for (size_t i = 0; i < expected_num_suggestions; ++i) { 572 SCOPED_TRACE(base::StringPrintf("i: %" PRIuS, i)); 573 EXPECT_EQ(expected_values[i], autofill_values_[i]); 574 EXPECT_EQ(expected_labels[i], autofill_labels_[i]); 575 EXPECT_EQ(expected_icons[i], autofill_icons_[i]); 576 EXPECT_EQ(expected_unique_ids[i], autofill_unique_ids_[i]); 577 } 578 } 579 580 bool on_query_seen() const { 581 return on_query_seen_; 582 } 583 584 bool on_suggestions_returned_seen() const { 585 return on_suggestions_returned_seen_; 586 } 587 588 private: 589 // Records if OnQuery has been called yet. 590 bool on_query_seen_; 591 592 // Records if OnSuggestionsReturned has been called after the most recent 593 // call to OnQuery. 594 bool on_suggestions_returned_seen_; 595 596 // The query id of the most recent Autofill query. 597 int query_id_; 598 599 // The results returned by the most recent Autofill query. 600 std::vector<base::string16> autofill_values_; 601 std::vector<base::string16> autofill_labels_; 602 std::vector<base::string16> autofill_icons_; 603 std::vector<int> autofill_unique_ids_; 604 605 DISALLOW_COPY_AND_ASSIGN(TestAutofillExternalDelegate); 606}; 607 608} // namespace 609 610class AutofillManagerTest : public ChromeRenderViewHostTestHarness { 611 public: 612 virtual void SetUp() OVERRIDE { 613 ChromeRenderViewHostTestHarness::SetUp(); 614 615 autofill::PersonalDataManagerFactory::GetInstance()->SetTestingFactory( 616 profile(), TestPersonalDataManager::Build); 617 618 autofill::TabAutofillManagerDelegate::CreateForWebContents(web_contents()); 619 620 autofill::TabAutofillManagerDelegate* manager_delegate = 621 autofill::TabAutofillManagerDelegate::FromWebContents(web_contents()); 622 personal_data_.set_database(manager_delegate->GetDatabase()); 623 personal_data_.set_pref_service(profile()->GetPrefs()); 624 autofill_driver_.reset(new MockAutofillDriver()); 625 autofill_manager_.reset(new TestAutofillManager( 626 autofill_driver_.get(), manager_delegate, &personal_data_)); 627 628 external_delegate_.reset(new TestAutofillExternalDelegate( 629 web_contents(), 630 autofill_manager_.get(), 631 autofill_driver_.get())); 632 autofill_manager_->SetExternalDelegate(external_delegate_.get()); 633 } 634 635 virtual void TearDown() OVERRIDE { 636 // Order of destruction is important as AutofillManager relies on 637 // PersonalDataManager to be around when it gets destroyed. Also, a real 638 // AutofillManager is tied to the lifetime of the WebContents, so it must 639 // be destroyed at the destruction of the WebContents. 640 autofill_manager_.reset(); 641 autofill_driver_.reset(); 642 ChromeRenderViewHostTestHarness::TearDown(); 643 644 // Remove the AutofillWebDataService so TestPersonalDataManager does not 645 // need to care about removing self as an observer in destruction. 646 personal_data_.set_database(scoped_refptr<AutofillWebDataService>(NULL)); 647 } 648 649 void GetAutofillSuggestions(int query_id, 650 const FormData& form, 651 const FormFieldData& field) { 652 autofill_manager_->OnQueryFormFieldAutofill(query_id, 653 form, 654 field, 655 gfx::Rect(), 656 false); 657 } 658 659 void GetAutofillSuggestions(const FormData& form, 660 const FormFieldData& field) { 661 GetAutofillSuggestions(kDefaultPageID, form, field); 662 } 663 664 void AutocompleteSuggestionsReturned( 665 const std::vector<base::string16>& result) { 666 autofill_manager_->autocomplete_history_manager_->SendSuggestions(&result); 667 } 668 669 void FormsSeen(const std::vector<FormData>& forms) { 670 autofill_manager_->OnFormsSeen(forms, base::TimeTicks(), 671 autofill::NO_SPECIAL_FORMS_SEEN); 672 } 673 674 void PartialFormsSeen(const std::vector<FormData>& forms) { 675 autofill_manager_->OnFormsSeen(forms, base::TimeTicks(), 676 autofill::PARTIAL_FORMS_SEEN); 677 } 678 679 void DynamicFormsSeen(const std::vector<FormData>& forms) { 680 autofill_manager_->OnFormsSeen(forms, base::TimeTicks(), 681 autofill::DYNAMIC_FORMS_SEEN); 682 } 683 684 void FormSubmitted(const FormData& form) { 685 autofill_manager_->ResetMessageLoopRunner(); 686 if (autofill_manager_->OnFormSubmitted(form, base::TimeTicks::Now())) 687 autofill_manager_->WaitForAsyncFormSubmit(); 688 } 689 690 void FillAutofillFormData(int query_id, 691 const FormData& form, 692 const FormFieldData& field, 693 int unique_id) { 694 autofill_manager_->OnFillAutofillFormData(query_id, form, field, unique_id); 695 } 696 697 // Calls |autofill_manager_->OnFillAutofillFormData()| with the specified 698 // input parameters after setting up the expectation that the mock driver's 699 // |SendFormDataToRenderer()| method will be called and saving the parameters 700 // of that call into the |response_query_id| and |response_data| output 701 // parameters. 702 void FillAutofillFormDataAndSaveResults(int input_query_id, 703 const FormData& input_form, 704 const FormFieldData& input_field, 705 int unique_id, 706 int* response_query_id, 707 FormData* response_data) { 708 EXPECT_CALL(*autofill_driver_, SendFormDataToRenderer(_, _)). 709 WillOnce((DoAll(testing::SaveArg<0>(response_query_id), 710 testing::SaveArg<1>(response_data)))); 711 FillAutofillFormData(input_query_id, input_form, input_field, unique_id); 712 } 713 714 int PackGUIDs(const GUIDPair& cc_guid, const GUIDPair& profile_guid) const { 715 return autofill_manager_->PackGUIDs(cc_guid, profile_guid); 716 } 717 718 protected: 719 scoped_ptr<MockAutofillDriver> autofill_driver_; 720 scoped_ptr<TestAutofillManager> autofill_manager_; 721 scoped_ptr<TestAutofillExternalDelegate> external_delegate_; 722 TestPersonalDataManager personal_data_; 723 724 // Used when we want an off the record profile. This will store the original 725 // profile from which the off the record profile is derived. 726 scoped_ptr<Profile> other_browser_context_; 727}; 728 729class TestFormStructure : public FormStructure { 730 public: 731 explicit TestFormStructure(const FormData& form) 732 : FormStructure(form) {} 733 virtual ~TestFormStructure() {} 734 735 void SetFieldTypes(const std::vector<ServerFieldType>& heuristic_types, 736 const std::vector<ServerFieldType>& server_types) { 737 ASSERT_EQ(field_count(), heuristic_types.size()); 738 ASSERT_EQ(field_count(), server_types.size()); 739 740 for (size_t i = 0; i < field_count(); ++i) { 741 AutofillField* form_field = field(i); 742 ASSERT_TRUE(form_field); 743 form_field->set_heuristic_type(heuristic_types[i]); 744 form_field->set_server_type(server_types[i]); 745 } 746 747 UpdateAutofillCount(); 748 } 749 750 private: 751 DISALLOW_COPY_AND_ASSIGN(TestFormStructure); 752}; 753 754// Test that we return all address profile suggestions when all form fields are 755// empty. 756TEST_F(AutofillManagerTest, GetProfileSuggestionsEmptyValue) { 757 // Set up our form data. 758 FormData form; 759 test::CreateTestAddressFormData(&form); 760 std::vector<FormData> forms(1, form); 761 FormsSeen(forms); 762 763 const FormFieldData& field = form.fields[0]; 764 GetAutofillSuggestions(form, field); 765 766 // No suggestions provided, so send an empty vector as the results. 767 // This triggers the combined message send. 768 AutocompleteSuggestionsReturned(std::vector<base::string16>()); 769 770 // Test that we sent the right values to the external delegate. 771 base::string16 expected_values[] = { 772 ASCIIToUTF16("Elvis"), 773 ASCIIToUTF16("Charles") 774 }; 775 // Inferred labels include full first relevant field, which in this case is 776 // the address line 1. 777 base::string16 expected_labels[] = { 778 ASCIIToUTF16("3734 Elvis Presley Blvd."), 779 ASCIIToUTF16("123 Apple St.") 780 }; 781 base::string16 expected_icons[] = {base::string16(), base::string16()}; 782 int expected_unique_ids[] = {1, 2}; 783 external_delegate_->CheckSuggestions( 784 kDefaultPageID, arraysize(expected_values), expected_values, 785 expected_labels, expected_icons, expected_unique_ids); 786} 787 788// Test that we return only matching address profile suggestions when the 789// selected form field has been partially filled out. 790TEST_F(AutofillManagerTest, GetProfileSuggestionsMatchCharacter) { 791 // Set up our form data. 792 FormData form; 793 test::CreateTestAddressFormData(&form); 794 std::vector<FormData> forms(1, form); 795 FormsSeen(forms); 796 797 FormFieldData field; 798 test::CreateTestFormField("First Name", "firstname", "E", "text",&field); 799 GetAutofillSuggestions(form, field); 800 801 // No suggestions provided, so send an empty vector as the results. 802 // This triggers the combined message send. 803 AutocompleteSuggestionsReturned(std::vector<base::string16>()); 804 805 // Test that we sent the right values to the external delegate. 806 base::string16 expected_values[] = {ASCIIToUTF16("Elvis")}; 807 base::string16 expected_labels[] = {ASCIIToUTF16("3734 Elvis Presley Blvd.")}; 808 base::string16 expected_icons[] = {base::string16()}; 809 int expected_unique_ids[] = {1}; 810 external_delegate_->CheckSuggestions( 811 kDefaultPageID, arraysize(expected_values), expected_values, 812 expected_labels, expected_icons, expected_unique_ids); 813} 814 815// Test that we return no suggestions when the form has no relevant fields. 816TEST_F(AutofillManagerTest, GetProfileSuggestionsUnknownFields) { 817 // Set up our form data. 818 FormData form; 819 form.name = ASCIIToUTF16("MyForm"); 820 form.method = ASCIIToUTF16("POST"); 821 form.origin = GURL("http://myform.com/form.html"); 822 form.action = GURL("http://myform.com/submit.html"); 823 form.user_submitted = true; 824 825 FormFieldData field; 826 test::CreateTestFormField("Username", "username", "", "text",&field); 827 form.fields.push_back(field); 828 test::CreateTestFormField("Password", "password", "", "password",&field); 829 form.fields.push_back(field); 830 test::CreateTestFormField("Quest", "quest", "", "quest", &field); 831 form.fields.push_back(field); 832 test::CreateTestFormField("Color", "color", "", "text", &field); 833 form.fields.push_back(field); 834 835 std::vector<FormData> forms(1, form); 836 FormsSeen(forms); 837 838 GetAutofillSuggestions(form, field); 839 EXPECT_FALSE(external_delegate_->on_suggestions_returned_seen()); 840} 841 842// Test that we cull duplicate profile suggestions. 843TEST_F(AutofillManagerTest, GetProfileSuggestionsWithDuplicates) { 844 // Set up our form data. 845 FormData form; 846 test::CreateTestAddressFormData(&form); 847 std::vector<FormData> forms(1, form); 848 FormsSeen(forms); 849 850 // Add a duplicate profile. 851 AutofillProfile* duplicate_profile = 852 new AutofillProfile( 853 *(autofill_manager_->GetProfileWithGUID( 854 "00000000-0000-0000-0000-000000000001"))); 855 autofill_manager_->AddProfile(duplicate_profile); 856 857 const FormFieldData& field = form.fields[0]; 858 GetAutofillSuggestions(form, field); 859 860 // No suggestions provided, so send an empty vector as the results. 861 // This triggers the combined message send. 862 AutocompleteSuggestionsReturned(std::vector<base::string16>()); 863 864 // Test that we sent the right values to the external delegate. 865 base::string16 expected_values[] = { 866 ASCIIToUTF16("Elvis"), 867 ASCIIToUTF16("Charles") 868 }; 869 base::string16 expected_labels[] = { 870 ASCIIToUTF16("3734 Elvis Presley Blvd."), 871 ASCIIToUTF16("123 Apple St.") 872 }; 873 base::string16 expected_icons[] = {base::string16(), base::string16()}; 874 int expected_unique_ids[] = {1, 2}; 875 external_delegate_->CheckSuggestions( 876 kDefaultPageID, arraysize(expected_values), expected_values, 877 expected_labels, expected_icons, expected_unique_ids); 878} 879 880// Test that we return no suggestions when autofill is disabled. 881TEST_F(AutofillManagerTest, GetProfileSuggestionsAutofillDisabledByUser) { 882 // Set up our form data. 883 FormData form; 884 test::CreateTestAddressFormData(&form); 885 std::vector<FormData> forms(1, form); 886 FormsSeen(forms); 887 888 // Disable Autofill. 889 autofill_manager_->set_autofill_enabled(false); 890 891 const FormFieldData& field = form.fields[0]; 892 GetAutofillSuggestions(form, field); 893 EXPECT_FALSE(external_delegate_->on_suggestions_returned_seen()); 894} 895 896// Test that we return a warning explaining that autofill suggestions are 897// unavailable when the form method is GET rather than POST. 898TEST_F(AutofillManagerTest, GetProfileSuggestionsMethodGet) { 899 // Set up our form data. 900 FormData form; 901 test::CreateTestAddressFormData(&form); 902 form.method = ASCIIToUTF16("GET"); 903 std::vector<FormData> forms(1, form); 904 FormsSeen(forms); 905 906 const FormFieldData& field = form.fields[0]; 907 GetAutofillSuggestions(form, field); 908 909 // No suggestions provided, so send an empty vector as the results. 910 // This triggers the combined message send. 911 AutocompleteSuggestionsReturned(std::vector<base::string16>()); 912 913 // Test that we sent the right values to the external delegate. 914 base::string16 expected_values[] = { 915 l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_FORM_DISABLED) 916 }; 917 base::string16 expected_labels[] = {base::string16()}; 918 base::string16 expected_icons[] = {base::string16()}; 919 int expected_unique_ids[] = 920 {blink::WebAutofillClient::MenuItemIDWarningMessage}; 921 external_delegate_->CheckSuggestions( 922 kDefaultPageID, arraysize(expected_values), expected_values, 923 expected_labels, expected_icons, expected_unique_ids); 924 925 // Now add some Autocomplete suggestions. We should return the autocomplete 926 // suggestions and the warning; these will be culled by the renderer. 927 const int kPageID2 = 2; 928 GetAutofillSuggestions(kPageID2, form, field); 929 930 std::vector<base::string16> suggestions; 931 suggestions.push_back(ASCIIToUTF16("Jay")); 932 suggestions.push_back(ASCIIToUTF16("Jason")); 933 AutocompleteSuggestionsReturned(suggestions); 934 935 base::string16 expected_values2[] = { 936 l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_FORM_DISABLED), 937 ASCIIToUTF16("Jay"), 938 ASCIIToUTF16("Jason") 939 }; 940 base::string16 expected_labels2[] = { base::string16(), base::string16(), 941 base::string16()}; 942 base::string16 expected_icons2[] = { base::string16(), base::string16(), 943 base::string16()}; 944 int expected_unique_ids2[] = {-1, 0, 0}; 945 external_delegate_->CheckSuggestions( 946 kPageID2, arraysize(expected_values2), expected_values2, 947 expected_labels2, expected_icons2, expected_unique_ids2); 948 949 // Now clear the test profiles and try again -- we shouldn't return a warning. 950 personal_data_.ClearAutofillProfiles(); 951 GetAutofillSuggestions(form, field); 952 EXPECT_FALSE(external_delegate_->on_suggestions_returned_seen()); 953} 954 955// Test that we return all credit card profile suggestions when all form fields 956// are empty. 957TEST_F(AutofillManagerTest, GetCreditCardSuggestionsEmptyValue) { 958 // Set up our form data. 959 FormData form; 960 CreateTestCreditCardFormData(&form, true, false); 961 std::vector<FormData> forms(1, form); 962 FormsSeen(forms); 963 964 FormFieldData field = form.fields[1]; 965 GetAutofillSuggestions(form, field); 966 967 // No suggestions provided, so send an empty vector as the results. 968 // This triggers the combined message send. 969 AutocompleteSuggestionsReturned(std::vector<base::string16>()); 970 971 // Test that we sent the right values to the external delegate. 972 base::string16 expected_values[] = { 973 ASCIIToUTF16("************3456"), 974 ASCIIToUTF16("************8765") 975 }; 976 base::string16 expected_labels[] = { ASCIIToUTF16("*3456"), 977 ASCIIToUTF16("*8765")}; 978 base::string16 expected_icons[] = { 979 ASCIIToUTF16(kVisaCard), 980 ASCIIToUTF16(kMasterCard) 981 }; 982 int expected_unique_ids[] = { 983 autofill_manager_->GetPackedCreditCardID(4), 984 autofill_manager_->GetPackedCreditCardID(5) 985 }; 986 external_delegate_->CheckSuggestions( 987 kDefaultPageID, arraysize(expected_values), expected_values, 988 expected_labels, expected_icons, expected_unique_ids); 989} 990 991// Test that we return only matching credit card profile suggestions when the 992// selected form field has been partially filled out. 993TEST_F(AutofillManagerTest, GetCreditCardSuggestionsMatchCharacter) { 994 // Set up our form data. 995 FormData form; 996 CreateTestCreditCardFormData(&form, true, false); 997 std::vector<FormData> forms(1, form); 998 FormsSeen(forms); 999 1000 FormFieldData field; 1001 test::CreateTestFormField("Card Number", "cardnumber", "4", "text", &field); 1002 GetAutofillSuggestions(form, field); 1003 1004 // No suggestions provided, so send an empty vector as the results. 1005 // This triggers the combined message send. 1006 AutocompleteSuggestionsReturned(std::vector<base::string16>()); 1007 1008 // Test that we sent the right values to the external delegate. 1009 base::string16 expected_values[] = {ASCIIToUTF16("************3456")}; 1010 base::string16 expected_labels[] = {ASCIIToUTF16("*3456")}; 1011 base::string16 expected_icons[] = {ASCIIToUTF16(kVisaCard)}; 1012 int expected_unique_ids[] = {autofill_manager_->GetPackedCreditCardID(4)}; 1013 external_delegate_->CheckSuggestions( 1014 kDefaultPageID, arraysize(expected_values), expected_values, 1015 expected_labels, expected_icons, expected_unique_ids); 1016} 1017 1018// Test that we return credit card profile suggestions when the selected form 1019// field is not the credit card number field. 1020TEST_F(AutofillManagerTest, GetCreditCardSuggestionsNonCCNumber) { 1021 // Set up our form data. 1022 FormData form; 1023 CreateTestCreditCardFormData(&form, true, false); 1024 std::vector<FormData> forms(1, form); 1025 FormsSeen(forms); 1026 1027 const FormFieldData& field = form.fields[0]; 1028 GetAutofillSuggestions(form, field); 1029 1030 // No suggestions provided, so send an empty vector as the results. 1031 // This triggers the combined message send. 1032 AutocompleteSuggestionsReturned(std::vector<base::string16>()); 1033 1034 // Test that we sent the right values to the external delegate. 1035 base::string16 expected_values[] = { 1036 ASCIIToUTF16("Elvis Presley"), 1037 ASCIIToUTF16("Buddy Holly") 1038 }; 1039 base::string16 expected_labels[] = { ASCIIToUTF16("*3456"), 1040 ASCIIToUTF16("*8765") }; 1041 base::string16 expected_icons[] = { 1042 ASCIIToUTF16(kVisaCard), 1043 ASCIIToUTF16(kMasterCard) 1044 }; 1045 int expected_unique_ids[] = { 1046 autofill_manager_->GetPackedCreditCardID(4), 1047 autofill_manager_->GetPackedCreditCardID(5) 1048 }; 1049 external_delegate_->CheckSuggestions( 1050 kDefaultPageID, arraysize(expected_values), expected_values, 1051 expected_labels, expected_icons, expected_unique_ids); 1052} 1053 1054// Test that we return a warning explaining that credit card profile suggestions 1055// are unavailable when the form is not https. 1056TEST_F(AutofillManagerTest, GetCreditCardSuggestionsNonHTTPS) { 1057 // Set up our form data. 1058 FormData form; 1059 CreateTestCreditCardFormData(&form, false, false); 1060 std::vector<FormData> forms(1, form); 1061 FormsSeen(forms); 1062 1063 const FormFieldData& field = form.fields[0]; 1064 GetAutofillSuggestions(form, field); 1065 1066 // No suggestions provided, so send an empty vector as the results. 1067 // This triggers the combined message send. 1068 AutocompleteSuggestionsReturned(std::vector<base::string16>()); 1069 1070 // Test that we sent the right values to the external delegate. 1071 base::string16 expected_values[] = { 1072 l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_INSECURE_CONNECTION) 1073 }; 1074 base::string16 expected_labels[] = {base::string16()}; 1075 base::string16 expected_icons[] = {base::string16()}; 1076 int expected_unique_ids[] = {-1}; 1077 external_delegate_->CheckSuggestions( 1078 kDefaultPageID, arraysize(expected_values), expected_values, 1079 expected_labels, expected_icons, expected_unique_ids); 1080 1081 // Now add some Autocomplete suggestions. We should show the autocomplete 1082 // suggestions and the warning. 1083 const int kPageID2 = 2; 1084 GetAutofillSuggestions(kPageID2, form, field); 1085 1086 std::vector<base::string16> suggestions; 1087 suggestions.push_back(ASCIIToUTF16("Jay")); 1088 suggestions.push_back(ASCIIToUTF16("Jason")); 1089 AutocompleteSuggestionsReturned(suggestions); 1090 1091 base::string16 expected_values2[] = { 1092 l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_INSECURE_CONNECTION), 1093 ASCIIToUTF16("Jay"), 1094 ASCIIToUTF16("Jason") 1095 }; 1096 base::string16 expected_labels2[] = { base::string16(), base::string16(), 1097 base::string16() }; 1098 base::string16 expected_icons2[] = { base::string16(), base::string16(), 1099 base::string16() }; 1100 int expected_unique_ids2[] = {-1, 0, 0}; 1101 external_delegate_->CheckSuggestions( 1102 kPageID2, arraysize(expected_values2), expected_values2, 1103 expected_labels2, expected_icons2, expected_unique_ids2); 1104 1105 // Clear the test credit cards and try again -- we shouldn't return a warning. 1106 personal_data_.ClearCreditCards(); 1107 GetAutofillSuggestions(form, field); 1108 EXPECT_FALSE(external_delegate_->on_suggestions_returned_seen()); 1109} 1110 1111// Test that we return all credit card suggestions in the case that two cards 1112// have the same obfuscated number. 1113TEST_F(AutofillManagerTest, GetCreditCardSuggestionsRepeatedObfuscatedNumber) { 1114 // Add a credit card with the same obfuscated number as Elvis's. 1115 // |credit_card| will be owned by the mock PersonalDataManager. 1116 CreditCard* credit_card = new CreditCard; 1117 test::SetCreditCardInfo(credit_card, "Elvis Presley", 1118 "5231567890123456", // Mastercard 1119 "04", "2012"); 1120 credit_card->set_guid("00000000-0000-0000-0000-000000000007"); 1121 autofill_manager_->AddCreditCard(credit_card); 1122 1123 // Set up our form data. 1124 FormData form; 1125 CreateTestCreditCardFormData(&form, true, false); 1126 std::vector<FormData> forms(1, form); 1127 FormsSeen(forms); 1128 1129 FormFieldData field = form.fields[1]; 1130 GetAutofillSuggestions(form, field); 1131 1132 // No suggestions provided, so send an empty vector as the results. 1133 // This triggers the combined message send. 1134 AutocompleteSuggestionsReturned(std::vector<base::string16>()); 1135 1136 // Test that we sent the right values to the external delegate. 1137 base::string16 expected_values[] = { 1138 ASCIIToUTF16("************3456"), 1139 ASCIIToUTF16("************8765"), 1140 ASCIIToUTF16("************3456") 1141 }; 1142 base::string16 expected_labels[] = { 1143 ASCIIToUTF16("*3456"), 1144 ASCIIToUTF16("*8765"), 1145 ASCIIToUTF16("*3456"), 1146 }; 1147 base::string16 expected_icons[] = { 1148 ASCIIToUTF16(kVisaCard), 1149 ASCIIToUTF16(kMasterCard), 1150 ASCIIToUTF16(kMasterCard) 1151 }; 1152 int expected_unique_ids[] = { 1153 autofill_manager_->GetPackedCreditCardID(4), 1154 autofill_manager_->GetPackedCreditCardID(5), 1155 autofill_manager_->GetPackedCreditCardID(7) 1156 }; 1157 external_delegate_->CheckSuggestions( 1158 kDefaultPageID, arraysize(expected_values), expected_values, 1159 expected_labels, expected_icons, expected_unique_ids); 1160} 1161 1162// Test that we return profile and credit card suggestions for combined forms. 1163TEST_F(AutofillManagerTest, GetAddressAndCreditCardSuggestions) { 1164 // Set up our form data. 1165 FormData form; 1166 test::CreateTestAddressFormData(&form); 1167 CreateTestCreditCardFormData(&form, true, false); 1168 std::vector<FormData> forms(1, form); 1169 FormsSeen(forms); 1170 1171 FormFieldData field = form.fields[0]; 1172 GetAutofillSuggestions(form, field); 1173 1174 // No suggestions provided, so send an empty vector as the results. 1175 // This triggers the combined message send. 1176 AutocompleteSuggestionsReturned(std::vector<base::string16>()); 1177 1178 // Test that we sent the right address suggestions to the external delegate. 1179 base::string16 expected_values[] = { 1180 ASCIIToUTF16("Elvis"), 1181 ASCIIToUTF16("Charles") 1182 }; 1183 base::string16 expected_labels[] = { 1184 ASCIIToUTF16("3734 Elvis Presley Blvd."), 1185 ASCIIToUTF16("123 Apple St.") 1186 }; 1187 base::string16 expected_icons[] = {base::string16(), base::string16()}; 1188 int expected_unique_ids[] = {1, 2}; 1189 external_delegate_->CheckSuggestions( 1190 kDefaultPageID, arraysize(expected_values), expected_values, 1191 expected_labels, expected_icons, expected_unique_ids); 1192 1193 const int kPageID2 = 2; 1194 test::CreateTestFormField("Card Number", "cardnumber", "", "text", &field); 1195 GetAutofillSuggestions(kPageID2, form, field); 1196 1197 // No suggestions provided, so send an empty vector as the results. 1198 // This triggers the combined message send. 1199 AutocompleteSuggestionsReturned(std::vector<base::string16>()); 1200 1201 // Test that we sent the credit card suggestions to the external delegate. 1202 base::string16 expected_values2[] = { 1203 ASCIIToUTF16("************3456"), 1204 ASCIIToUTF16("************8765") 1205 }; 1206 base::string16 expected_labels2[] = { ASCIIToUTF16("*3456"), 1207 ASCIIToUTF16("*8765")}; 1208 base::string16 expected_icons2[] = { 1209 ASCIIToUTF16(kVisaCard), 1210 ASCIIToUTF16(kMasterCard) 1211 }; 1212 int expected_unique_ids2[] = { 1213 autofill_manager_->GetPackedCreditCardID(4), 1214 autofill_manager_->GetPackedCreditCardID(5) 1215 }; 1216 external_delegate_->CheckSuggestions( 1217 kPageID2, arraysize(expected_values2), expected_values2, 1218 expected_labels2, expected_icons2, expected_unique_ids2); 1219} 1220 1221// Test that for non-https forms with both address and credit card fields, we 1222// only return address suggestions. Instead of credit card suggestions, we 1223// should return a warning explaining that credit card profile suggestions are 1224// unavailable when the form is not https. 1225TEST_F(AutofillManagerTest, GetAddressAndCreditCardSuggestionsNonHttps) { 1226 // Set up our form data. 1227 FormData form; 1228 test::CreateTestAddressFormData(&form); 1229 CreateTestCreditCardFormData(&form, false, false); 1230 std::vector<FormData> forms(1, form); 1231 FormsSeen(forms); 1232 1233 FormFieldData field = form.fields[0]; 1234 GetAutofillSuggestions(form, field); 1235 1236 // No suggestions provided, so send an empty vector as the results. 1237 // This triggers the combined message send. 1238 AutocompleteSuggestionsReturned(std::vector<base::string16>()); 1239 1240 // Test that we sent the right suggestions to the external delegate. 1241 base::string16 expected_values[] = { 1242 ASCIIToUTF16("Elvis"), 1243 ASCIIToUTF16("Charles") 1244 }; 1245 base::string16 expected_labels[] = { 1246 ASCIIToUTF16("3734 Elvis Presley Blvd."), 1247 ASCIIToUTF16("123 Apple St.") 1248 }; 1249 base::string16 expected_icons[] = {base::string16(), base::string16()}; 1250 int expected_unique_ids[] = {1, 2}; 1251 external_delegate_->CheckSuggestions( 1252 kDefaultPageID, arraysize(expected_values), expected_values, 1253 expected_labels, expected_icons, expected_unique_ids); 1254 1255 test::CreateTestFormField("Card Number", "cardnumber", "", "text", &field); 1256 const int kPageID2 = 2; 1257 GetAutofillSuggestions(kPageID2, form, field); 1258 1259 // No suggestions provided, so send an empty vector as the results. 1260 // This triggers the combined message send. 1261 AutocompleteSuggestionsReturned(std::vector<base::string16>()); 1262 1263 // Test that we sent the right values to the external delegate. 1264 base::string16 expected_values2[] = { 1265 l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_INSECURE_CONNECTION) 1266 }; 1267 base::string16 expected_labels2[] = {base::string16()}; 1268 base::string16 expected_icons2[] = {base::string16()}; 1269 int expected_unique_ids2[] = {-1}; 1270 external_delegate_->CheckSuggestions( 1271 kPageID2, arraysize(expected_values2), expected_values2, 1272 expected_labels2, expected_icons2, expected_unique_ids2); 1273 1274 // Clear the test credit cards and try again -- we shouldn't return a warning. 1275 personal_data_.ClearCreditCards(); 1276 GetAutofillSuggestions(form, field); 1277 EXPECT_FALSE(external_delegate_->on_suggestions_returned_seen()); 1278} 1279 1280// Test that we correctly combine autofill and autocomplete suggestions. 1281TEST_F(AutofillManagerTest, GetCombinedAutofillAndAutocompleteSuggestions) { 1282 // Set up our form data. 1283 FormData form; 1284 test::CreateTestAddressFormData(&form); 1285 std::vector<FormData> forms(1, form); 1286 FormsSeen(forms); 1287 1288 const FormFieldData& field = form.fields[0]; 1289 GetAutofillSuggestions(form, field); 1290 1291 // Add some Autocomplete suggestions. 1292 // This triggers the combined message send. 1293 std::vector<base::string16> suggestions; 1294 suggestions.push_back(ASCIIToUTF16("Jay")); 1295 // This suggestion is a duplicate, and should be trimmed. 1296 suggestions.push_back(ASCIIToUTF16("Elvis")); 1297 suggestions.push_back(ASCIIToUTF16("Jason")); 1298 AutocompleteSuggestionsReturned(suggestions); 1299 1300 // Test that we sent the right values to the external delegate. 1301 base::string16 expected_values[] = { 1302 ASCIIToUTF16("Elvis"), 1303 ASCIIToUTF16("Charles"), 1304 ASCIIToUTF16("Jay"), 1305 ASCIIToUTF16("Jason") 1306 }; 1307 base::string16 expected_labels[] = { 1308 ASCIIToUTF16("3734 Elvis Presley Blvd."), 1309 ASCIIToUTF16("123 Apple St."), 1310 base::string16(), 1311 base::string16() 1312 }; 1313 base::string16 expected_icons[] = { base::string16(), base::string16(), 1314 base::string16(), base::string16()}; 1315 int expected_unique_ids[] = {1, 2, 0, 0}; 1316 external_delegate_->CheckSuggestions( 1317 kDefaultPageID, arraysize(expected_values), expected_values, 1318 expected_labels, expected_icons, expected_unique_ids); 1319} 1320 1321// Test that we return autocomplete-like suggestions when trying to autofill 1322// already filled forms. 1323TEST_F(AutofillManagerTest, GetFieldSuggestionsWhenFormIsAutofilled) { 1324 // Set up our form data. 1325 FormData form; 1326 test::CreateTestAddressFormData(&form); 1327 std::vector<FormData> forms(1, form); 1328 FormsSeen(forms); 1329 1330 // Mark one of the fields as filled. 1331 form.fields[2].is_autofilled = true; 1332 const FormFieldData& field = form.fields[0]; 1333 GetAutofillSuggestions(form, field); 1334 1335 // No suggestions provided, so send an empty vector as the results. 1336 // This triggers the combined message send. 1337 AutocompleteSuggestionsReturned(std::vector<base::string16>()); 1338 1339 // Test that we sent the right values to the external delegate. 1340 base::string16 expected_values[] = { 1341 ASCIIToUTF16("Elvis"), 1342 ASCIIToUTF16("Charles") 1343 }; 1344 base::string16 expected_labels[] = {base::string16(), base::string16()}; 1345 base::string16 expected_icons[] = {base::string16(), base::string16()}; 1346 int expected_unique_ids[] = {1, 2}; 1347 external_delegate_->CheckSuggestions( 1348 kDefaultPageID, arraysize(expected_values), expected_values, 1349 expected_labels, expected_icons, expected_unique_ids); 1350} 1351 1352// Test that nothing breaks when there are autocomplete suggestions but no 1353// autofill suggestions. 1354TEST_F(AutofillManagerTest, GetFieldSuggestionsForAutocompleteOnly) { 1355 // Set up our form data. 1356 FormData form; 1357 test::CreateTestAddressFormData(&form); 1358 FormFieldData field; 1359 test::CreateTestFormField("Some Field", "somefield", "", "text", &field); 1360 form.fields.push_back(field); 1361 std::vector<FormData> forms(1, form); 1362 FormsSeen(forms); 1363 1364 GetAutofillSuggestions(form, field); 1365 1366 // Add some Autocomplete suggestions. 1367 // This triggers the combined message send. 1368 std::vector<base::string16> suggestions; 1369 suggestions.push_back(ASCIIToUTF16("one")); 1370 suggestions.push_back(ASCIIToUTF16("two")); 1371 AutocompleteSuggestionsReturned(suggestions); 1372 1373 // Test that we sent the right values to the external delegate. 1374 base::string16 expected_values[] = { 1375 ASCIIToUTF16("one"), 1376 ASCIIToUTF16("two") 1377 }; 1378 base::string16 expected_labels[] = {base::string16(), base::string16()}; 1379 base::string16 expected_icons[] = {base::string16(), base::string16()}; 1380 int expected_unique_ids[] = {0, 0}; 1381 external_delegate_->CheckSuggestions( 1382 kDefaultPageID, arraysize(expected_values), expected_values, 1383 expected_labels, expected_icons, expected_unique_ids); 1384} 1385 1386// Test that we do not return duplicate values drawn from multiple profiles when 1387// filling an already filled field. 1388TEST_F(AutofillManagerTest, GetFieldSuggestionsWithDuplicateValues) { 1389 // Set up our form data. 1390 FormData form; 1391 test::CreateTestAddressFormData(&form); 1392 std::vector<FormData> forms(1, form); 1393 FormsSeen(forms); 1394 1395 // |profile| will be owned by the mock PersonalDataManager. 1396 AutofillProfile* profile = new AutofillProfile; 1397 test::SetProfileInfo( 1398 profile, "Elvis", "", "", "", "", "", "", "", "", "", "", ""); 1399 profile->set_guid("00000000-0000-0000-0000-000000000101"); 1400 autofill_manager_->AddProfile(profile); 1401 1402 FormFieldData& field = form.fields[0]; 1403 field.is_autofilled = true; 1404 field.value = ASCIIToUTF16("Elvis"); 1405 GetAutofillSuggestions(form, field); 1406 1407 // No suggestions provided, so send an empty vector as the results. 1408 // This triggers the combined message send. 1409 AutocompleteSuggestionsReturned(std::vector<base::string16>()); 1410 1411 // Test that we sent the right values to the external delegate. 1412 base::string16 expected_values[] = { ASCIIToUTF16("Elvis") }; 1413 base::string16 expected_labels[] = { base::string16() }; 1414 base::string16 expected_icons[] = { base::string16() }; 1415 int expected_unique_ids[] = { 1 }; 1416 external_delegate_->CheckSuggestions( 1417 kDefaultPageID, arraysize(expected_values), expected_values, 1418 expected_labels, expected_icons, expected_unique_ids); 1419} 1420 1421// Test that a non-default value is suggested for multi-valued profile, on an 1422// unfilled form. 1423TEST_F(AutofillManagerTest, GetFieldSuggestionsForMultiValuedProfileUnfilled) { 1424 // Set up our form data. 1425 FormData form; 1426 test::CreateTestAddressFormData(&form); 1427 std::vector<FormData> forms(1, form); 1428 FormsSeen(forms); 1429 1430 // |profile| will be owned by the mock PersonalDataManager. 1431 AutofillProfile* profile = new AutofillProfile; 1432 test::SetProfileInfo(profile, "Elvis", "", "Presley", "me@x.com", "", 1433 "", "", "", "", "", "", ""); 1434 profile->set_guid("00000000-0000-0000-0000-000000000101"); 1435 std::vector<base::string16> multi_values(2); 1436 multi_values[0] = ASCIIToUTF16("Elvis Presley"); 1437 multi_values[1] = ASCIIToUTF16("Elena Love"); 1438 profile->SetRawMultiInfo(NAME_FULL, multi_values); 1439 personal_data_.ClearAutofillProfiles(); 1440 autofill_manager_->AddProfile(profile); 1441 1442 { 1443 // Get the first name field. 1444 // Start out with "E", hoping for either "Elvis" or "Elena. 1445 FormFieldData& field = form.fields[0]; 1446 field.value = ASCIIToUTF16("E"); 1447 field.is_autofilled = false; 1448 GetAutofillSuggestions(form, field); 1449 1450 // Trigger the |Send|. 1451 AutocompleteSuggestionsReturned(std::vector<base::string16>()); 1452 1453 // Test that we sent the right values to the external delegate. 1454 base::string16 expected_values[] = { 1455 ASCIIToUTF16("Elvis"), 1456 ASCIIToUTF16("Elena") 1457 }; 1458 base::string16 expected_labels[] = { 1459 ASCIIToUTF16("me@x.com"), 1460 ASCIIToUTF16("me@x.com") 1461 }; 1462 base::string16 expected_icons[] = { base::string16(), base::string16() }; 1463 int expected_unique_ids[] = { 1, 2 }; 1464 external_delegate_->CheckSuggestions( 1465 kDefaultPageID, arraysize(expected_values), expected_values, 1466 expected_labels, expected_icons, expected_unique_ids); 1467 } 1468 1469 { 1470 // Get the first name field. 1471 // This time, start out with "Ele", hoping for "Elena". 1472 FormFieldData& field = form.fields[0]; 1473 field.value = ASCIIToUTF16("Ele"); 1474 field.is_autofilled = false; 1475 GetAutofillSuggestions(form, field); 1476 1477 // Trigger the |Send|. 1478 AutocompleteSuggestionsReturned(std::vector<base::string16>()); 1479 1480 // Test that we sent the right values to the external delegate. 1481 base::string16 expected_values[] = { ASCIIToUTF16("Elena") }; 1482 base::string16 expected_labels[] = { ASCIIToUTF16("me@x.com") }; 1483 base::string16 expected_icons[] = { base::string16() }; 1484 int expected_unique_ids[] = { 2 }; 1485 external_delegate_->CheckSuggestions( 1486 kDefaultPageID, arraysize(expected_values), expected_values, 1487 expected_labels, expected_icons, expected_unique_ids); 1488 } 1489} 1490 1491// Test that all values are suggested for multi-valued profile, on a filled 1492// form. This is the per-field "override" case. 1493TEST_F(AutofillManagerTest, GetFieldSuggestionsForMultiValuedProfileFilled) { 1494 // Set up our form data. 1495 FormData form; 1496 test::CreateTestAddressFormData(&form); 1497 std::vector<FormData> forms(1, form); 1498 FormsSeen(forms); 1499 1500 // |profile| will be owned by the mock PersonalDataManager. 1501 AutofillProfile* profile = new AutofillProfile; 1502 profile->set_guid("00000000-0000-0000-0000-000000000102"); 1503 std::vector<base::string16> multi_values(3); 1504 multi_values[0] = ASCIIToUTF16("Travis Smith"); 1505 multi_values[1] = ASCIIToUTF16("Cynthia Love"); 1506 multi_values[2] = ASCIIToUTF16("Zac Mango"); 1507 profile->SetRawMultiInfo(NAME_FULL, multi_values); 1508 autofill_manager_->AddProfile(profile); 1509 1510 // Get the first name field. And start out with "Travis", hoping for all the 1511 // multi-valued variants as suggestions. 1512 FormFieldData& field = form.fields[0]; 1513 field.value = ASCIIToUTF16("Travis"); 1514 field.is_autofilled = true; 1515 GetAutofillSuggestions(form, field); 1516 1517 // Trigger the |Send|. 1518 AutocompleteSuggestionsReturned(std::vector<base::string16>()); 1519 1520 // Test that we sent the right values to the external delegate. 1521 base::string16 expected_values[] = { 1522 ASCIIToUTF16("Travis"), 1523 ASCIIToUTF16("Cynthia"), 1524 ASCIIToUTF16("Zac") 1525 }; 1526 base::string16 expected_labels[] = { base::string16(), base::string16(), 1527 base::string16() }; 1528 base::string16 expected_icons[] = { base::string16(), base::string16(), 1529 base::string16() }; 1530 int expected_unique_ids[] = { 1, 2, 3 }; 1531 external_delegate_->CheckSuggestions( 1532 kDefaultPageID, arraysize(expected_values), expected_values, 1533 expected_labels, expected_icons, expected_unique_ids); 1534} 1535 1536TEST_F(AutofillManagerTest, GetProfileSuggestionsFancyPhone) { 1537 // Set up our form data. 1538 FormData form; 1539 test::CreateTestAddressFormData(&form); 1540 std::vector<FormData> forms(1, form); 1541 FormsSeen(forms); 1542 1543 AutofillProfile* profile = new AutofillProfile; 1544 profile->set_guid("00000000-0000-0000-0000-000000000103"); 1545 std::vector<base::string16> multi_values(1); 1546 multi_values[0] = ASCIIToUTF16("Natty Bumppo"); 1547 profile->SetRawMultiInfo(NAME_FULL, multi_values); 1548 multi_values[0] = ASCIIToUTF16("1800PRAIRIE"); 1549 profile->SetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, multi_values); 1550 autofill_manager_->AddProfile(profile); 1551 1552 const FormFieldData& field = form.fields[9]; 1553 GetAutofillSuggestions(form, field); 1554 1555 // No suggestions provided, so send an empty vector as the results. 1556 // This triggers the combined message send. 1557 AutocompleteSuggestionsReturned(std::vector<base::string16>()); 1558 1559 // Test that we sent the right values to the external delegate. 1560 base::string16 expected_values[] = { 1561 ASCIIToUTF16("12345678901"), 1562 ASCIIToUTF16("23456789012"), 1563 ASCIIToUTF16("18007724743"), // 1800PRAIRIE 1564 }; 1565 // Inferred labels include full first relevant field, which in this case is 1566 // the address line 1. 1567 base::string16 expected_labels[] = { 1568 ASCIIToUTF16("Elvis Aaron Presley"), 1569 ASCIIToUTF16("Charles Hardin Holley"), 1570 ASCIIToUTF16("Natty Bumppo"), 1571 }; 1572 base::string16 expected_icons[] = { base::string16(), base::string16(), 1573 base::string16()}; 1574 int expected_unique_ids[] = {1, 2, 3}; 1575 external_delegate_->CheckSuggestions( 1576 kDefaultPageID, arraysize(expected_values), expected_values, 1577 expected_labels, expected_icons, expected_unique_ids); 1578} 1579 1580// Test that we correctly fill an address form. 1581TEST_F(AutofillManagerTest, FillAddressForm) { 1582 // Set up our form data. 1583 FormData form; 1584 test::CreateTestAddressFormData(&form); 1585 std::vector<FormData> forms(1, form); 1586 FormsSeen(forms); 1587 1588 GUIDPair guid("00000000-0000-0000-0000-000000000001", 0); 1589 GUIDPair empty(std::string(), 0); 1590 int response_page_id = 0; 1591 FormData response_data; 1592 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0], 1593 PackGUIDs(empty, guid), &response_page_id, &response_data); 1594 ExpectFilledAddressFormElvis( 1595 response_page_id, response_data, kDefaultPageID, false); 1596} 1597 1598// Test that we correctly fill an address form from an auxiliary profile. 1599TEST_F(AutofillManagerTest, FillAddressFormFromAuxiliaryProfile) { 1600 personal_data_.ClearAutofillProfiles(); 1601 PrefService* prefs = user_prefs::UserPrefs::Get(profile()); 1602 prefs->SetBoolean(::autofill::prefs::kAutofillAuxiliaryProfilesEnabled, true); 1603 personal_data_.CreateTestAuxiliaryProfiles(); 1604 1605 // Set up our form data. 1606 FormData form; 1607 test::CreateTestAddressFormData(&form); 1608 std::vector<FormData> forms(1, form); 1609 FormsSeen(forms); 1610 1611 GUIDPair guid("00000000-0000-0000-0000-000000000001", 0); 1612 GUIDPair empty(std::string(), 0); 1613 int response_page_id = 0; 1614 FormData response_data; 1615 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0], 1616 PackGUIDs(empty, guid), &response_page_id, &response_data); 1617 ExpectFilledAddressFormElvis( 1618 response_page_id, response_data, kDefaultPageID, false); 1619} 1620 1621// Test that we correctly fill a credit card form. 1622TEST_F(AutofillManagerTest, FillCreditCardForm) { 1623 // Set up our form data. 1624 FormData form; 1625 CreateTestCreditCardFormData(&form, true, false); 1626 std::vector<FormData> forms(1, form); 1627 FormsSeen(forms); 1628 1629 GUIDPair guid("00000000-0000-0000-0000-000000000004", 0); 1630 GUIDPair empty(std::string(), 0); 1631 int response_page_id = 0; 1632 FormData response_data; 1633 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, *form.fields.begin(), 1634 PackGUIDs(guid, empty), &response_page_id, &response_data); 1635 ExpectFilledCreditCardFormElvis( 1636 response_page_id, response_data, kDefaultPageID, false); 1637} 1638 1639// Test that we correctly fill a credit card form with month input type. 1640// 1. year empty, month empty 1641TEST_F(AutofillManagerTest, FillCreditCardFormNoYearNoMonth) { 1642 // Same as the SetUp(), but generate 4 credit cards with year month 1643 // combination. 1644 personal_data_.CreateTestCreditCardsYearAndMonth("", ""); 1645 // Set up our form data. 1646 FormData form; 1647 CreateTestCreditCardFormData(&form, true, true); 1648 std::vector<FormData> forms(1, form); 1649 FormsSeen(forms); 1650 1651 GUIDPair guid("00000000-0000-0000-0000-000000000007", 0); 1652 GUIDPair empty(std::string(), 0); 1653 int response_page_id = 0; 1654 FormData response_data; 1655 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, *form.fields.begin(), 1656 PackGUIDs(guid, empty), &response_page_id, &response_data); 1657 ExpectFilledCreditCardYearMonthWithYearMonth(response_page_id, response_data, 1658 kDefaultPageID, false, "", ""); 1659} 1660 1661 1662// Test that we correctly fill a credit card form with month input type. 1663// 2. year empty, month non-empty 1664TEST_F(AutofillManagerTest, FillCreditCardFormNoYearMonth) { 1665 // Same as the SetUp(), but generate 4 credit cards with year month 1666 // combination. 1667 personal_data_.CreateTestCreditCardsYearAndMonth("", "04"); 1668 // Set up our form data. 1669 FormData form; 1670 CreateTestCreditCardFormData(&form, true, true); 1671 std::vector<FormData> forms(1, form); 1672 FormsSeen(forms); 1673 1674 GUIDPair guid("00000000-0000-0000-0000-000000000007", 0); 1675 GUIDPair empty(std::string(), 0); 1676 int response_page_id = 0; 1677 FormData response_data; 1678 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, *form.fields.begin(), 1679 PackGUIDs(guid, empty), &response_page_id, &response_data); 1680 ExpectFilledCreditCardYearMonthWithYearMonth(response_page_id, response_data, 1681 kDefaultPageID, false, "", "04"); 1682} 1683 1684// Test that we correctly fill a credit card form with month input type. 1685// 3. year non-empty, month empty 1686TEST_F(AutofillManagerTest, FillCreditCardFormYearNoMonth) { 1687 // Same as the SetUp(), but generate 4 credit cards with year month 1688 // combination. 1689 personal_data_.CreateTestCreditCardsYearAndMonth("2012", ""); 1690 // Set up our form data. 1691 FormData form; 1692 CreateTestCreditCardFormData(&form, true, true); 1693 std::vector<FormData> forms(1, form); 1694 FormsSeen(forms); 1695 1696 GUIDPair guid("00000000-0000-0000-0000-000000000007", 0); 1697 GUIDPair empty(std::string(), 0); 1698 int response_page_id = 0; 1699 FormData response_data; 1700 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, *form.fields.begin(), 1701 PackGUIDs(guid, empty), &response_page_id, &response_data); 1702 ExpectFilledCreditCardYearMonthWithYearMonth(response_page_id, response_data, 1703 kDefaultPageID, false, "2012", ""); 1704} 1705 1706// Test that we correctly fill a credit card form with month input type. 1707// 4. year non-empty, month empty 1708TEST_F(AutofillManagerTest, FillCreditCardFormYearMonth) { 1709 // Same as the SetUp(), but generate 4 credit cards with year month 1710 // combination. 1711 personal_data_.ClearCreditCards(); 1712 personal_data_.CreateTestCreditCardsYearAndMonth("2012", "04"); 1713 // Set up our form data. 1714 FormData form; 1715 CreateTestCreditCardFormData(&form, true, true); 1716 std::vector<FormData> forms(1, form); 1717 FormsSeen(forms); 1718 1719 GUIDPair guid("00000000-0000-0000-0000-000000000007", 0); 1720 GUIDPair empty(std::string(), 0); 1721 int response_page_id = 0; 1722 FormData response_data; 1723 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, *form.fields.begin(), 1724 PackGUIDs(guid, empty), &response_page_id, &response_data); 1725 ExpectFilledCreditCardYearMonthWithYearMonth(response_page_id, response_data, 1726 kDefaultPageID, false, "2012", "04"); 1727} 1728 1729// Test that we correctly fill a combined address and credit card form. 1730TEST_F(AutofillManagerTest, FillAddressAndCreditCardForm) { 1731 // Set up our form data. 1732 FormData form; 1733 test::CreateTestAddressFormData(&form); 1734 CreateTestCreditCardFormData(&form, true, false); 1735 std::vector<FormData> forms(1, form); 1736 FormsSeen(forms); 1737 1738 // First fill the address data. 1739 GUIDPair guid("00000000-0000-0000-0000-000000000001", 0); 1740 GUIDPair empty(std::string(), 0); 1741 int response_page_id = 0; 1742 FormData response_data; 1743 { 1744 SCOPED_TRACE("Address"); 1745 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0], 1746 PackGUIDs(empty, guid), &response_page_id, &response_data); 1747 ExpectFilledAddressFormElvis( 1748 response_page_id, response_data, kDefaultPageID, true); 1749 } 1750 1751 // Now fill the credit card data. 1752 const int kPageID2 = 2; 1753 GUIDPair guid2("00000000-0000-0000-0000-000000000004", 0); 1754 response_page_id = 0; 1755 { 1756 FillAutofillFormDataAndSaveResults(kPageID2, form, form.fields.back(), 1757 PackGUIDs(guid2, empty), &response_page_id, &response_data); 1758 SCOPED_TRACE("Credit card"); 1759 ExpectFilledCreditCardFormElvis( 1760 response_page_id, response_data, kPageID2, true); 1761 } 1762} 1763 1764// Test that we correctly fill a form that has multiple logical sections, e.g. 1765// both a billing and a shipping address. 1766TEST_F(AutofillManagerTest, FillFormWithMultipleSections) { 1767 // Set up our form data. 1768 FormData form; 1769 test::CreateTestAddressFormData(&form); 1770 const size_t kAddressFormSize = form.fields.size(); 1771 test::CreateTestAddressFormData(&form); 1772 for (size_t i = kAddressFormSize; i < form.fields.size(); ++i) { 1773 // Make sure the fields have distinct names. 1774 form.fields[i].name = form.fields[i].name + ASCIIToUTF16("_"); 1775 } 1776 std::vector<FormData> forms(1, form); 1777 FormsSeen(forms); 1778 1779 // Fill the first section. 1780 GUIDPair guid("00000000-0000-0000-0000-000000000001", 0); 1781 GUIDPair empty(std::string(), 0); 1782 int response_page_id = 0; 1783 FormData response_data; 1784 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0], 1785 PackGUIDs(empty, guid), &response_page_id, &response_data); 1786 { 1787 SCOPED_TRACE("Address 1"); 1788 // The second address section should be empty. 1789 ASSERT_EQ(response_data.fields.size(), 2*kAddressFormSize); 1790 for (size_t i = kAddressFormSize; i < form.fields.size(); ++i) { 1791 EXPECT_EQ(base::string16(), response_data.fields[i].value); 1792 } 1793 1794 // The first address section should be filled with Elvis's data. 1795 response_data.fields.resize(kAddressFormSize); 1796 ExpectFilledAddressFormElvis( 1797 response_page_id, response_data, kDefaultPageID, false); 1798 } 1799 1800 // Fill the second section, with the initiating field somewhere in the middle 1801 // of the section. 1802 const int kPageID2 = 2; 1803 GUIDPair guid2("00000000-0000-0000-0000-000000000001", 0); 1804 ASSERT_LT(9U, kAddressFormSize); 1805 response_page_id = 0; 1806 FillAutofillFormDataAndSaveResults( 1807 kPageID2, form, form.fields[kAddressFormSize + 9], 1808 PackGUIDs(empty, guid2), &response_page_id, &response_data); 1809 { 1810 SCOPED_TRACE("Address 2"); 1811 ASSERT_EQ(response_data.fields.size(), form.fields.size()); 1812 1813 // The first address section should be empty. 1814 ASSERT_EQ(response_data.fields.size(), 2*kAddressFormSize); 1815 for (size_t i = 0; i < kAddressFormSize; ++i) { 1816 EXPECT_EQ(base::string16(), response_data.fields[i].value); 1817 } 1818 1819 // The second address section should be filled with Elvis's data. 1820 FormData secondSection = response_data; 1821 secondSection.fields.erase(secondSection.fields.begin(), 1822 secondSection.fields.begin() + kAddressFormSize); 1823 for (size_t i = 0; i < kAddressFormSize; ++i) { 1824 // Restore the expected field names. 1825 base::string16 name = secondSection.fields[i].name; 1826 base::string16 original_name = name.substr(0, name.size() - 1); 1827 secondSection.fields[i].name = original_name; 1828 } 1829 ExpectFilledAddressFormElvis( 1830 response_page_id, secondSection, kPageID2, false); 1831 } 1832} 1833 1834// Test that we correctly fill a form that has author-specified sections, which 1835// might not match our expected section breakdown. 1836TEST_F(AutofillManagerTest, FillFormWithAuthorSpecifiedSections) { 1837 // Create a form with a billing section and an unnamed section, interleaved. 1838 // The billing section includes both address and credit card fields. 1839 FormData form; 1840 form.name = ASCIIToUTF16("MyForm"); 1841 form.method = ASCIIToUTF16("POST"); 1842 form.origin = GURL("https://myform.com/form.html"); 1843 form.action = GURL("https://myform.com/submit.html"); 1844 form.user_submitted = true; 1845 1846 FormFieldData field; 1847 1848 test::CreateTestFormField("", "country", "", "text", &field); 1849 field.autocomplete_attribute = "section-billing country"; 1850 form.fields.push_back(field); 1851 1852 test::CreateTestFormField("", "firstname", "", "text", &field); 1853 field.autocomplete_attribute = "given-name"; 1854 form.fields.push_back(field); 1855 1856 test::CreateTestFormField("", "lastname", "", "text", &field); 1857 field.autocomplete_attribute = "family-name"; 1858 form.fields.push_back(field); 1859 1860 test::CreateTestFormField("", "address", "", "text", &field); 1861 field.autocomplete_attribute = "section-billing address-line1"; 1862 form.fields.push_back(field); 1863 1864 test::CreateTestFormField("", "city", "", "text", &field); 1865 field.autocomplete_attribute = "section-billing locality"; 1866 form.fields.push_back(field); 1867 1868 test::CreateTestFormField("", "state", "", "text", &field); 1869 field.autocomplete_attribute = "section-billing region"; 1870 form.fields.push_back(field); 1871 1872 test::CreateTestFormField("", "zip", "", "text", &field); 1873 field.autocomplete_attribute = "section-billing postal-code"; 1874 form.fields.push_back(field); 1875 1876 test::CreateTestFormField("", "ccname", "", "text", &field); 1877 field.autocomplete_attribute = "section-billing cc-name"; 1878 form.fields.push_back(field); 1879 1880 test::CreateTestFormField("", "ccnumber", "", "text", &field); 1881 field.autocomplete_attribute = "section-billing cc-number"; 1882 form.fields.push_back(field); 1883 1884 test::CreateTestFormField("", "ccexp", "", "text", &field); 1885 field.autocomplete_attribute = "section-billing cc-exp"; 1886 form.fields.push_back(field); 1887 1888 test::CreateTestFormField("", "email", "", "text", &field); 1889 field.autocomplete_attribute = "email"; 1890 form.fields.push_back(field); 1891 1892 std::vector<FormData> forms(1, form); 1893 FormsSeen(forms); 1894 1895 // Fill the unnamed section. 1896 GUIDPair guid("00000000-0000-0000-0000-000000000001", 0); 1897 GUIDPair empty(std::string(), 0); 1898 int response_page_id = 0; 1899 FormData response_data; 1900 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[1], 1901 PackGUIDs(empty, guid), &response_page_id, &response_data); 1902 { 1903 SCOPED_TRACE("Unnamed section"); 1904 EXPECT_EQ(kDefaultPageID, response_page_id); 1905 EXPECT_EQ(ASCIIToUTF16("MyForm"), response_data.name); 1906 EXPECT_EQ(ASCIIToUTF16("POST"), response_data.method); 1907 EXPECT_EQ(GURL("https://myform.com/form.html"), response_data.origin); 1908 EXPECT_EQ(GURL("https://myform.com/submit.html"), response_data.action); 1909 EXPECT_TRUE(response_data.user_submitted); 1910 ASSERT_EQ(11U, response_data.fields.size()); 1911 1912 ExpectFilledField("", "country", "", "text", response_data.fields[0]); 1913 ExpectFilledField("", "firstname", "Elvis", "text", 1914 response_data.fields[1]); 1915 ExpectFilledField("", "lastname", "Presley", "text", 1916 response_data.fields[2]); 1917 ExpectFilledField("", "address", "", "text", response_data.fields[3]); 1918 ExpectFilledField("", "city", "", "text", response_data.fields[4]); 1919 ExpectFilledField("", "state", "", "text", response_data.fields[5]); 1920 ExpectFilledField("", "zip", "", "text", response_data.fields[6]); 1921 ExpectFilledField("", "ccname", "", "text", response_data.fields[7]); 1922 ExpectFilledField("", "ccnumber", "", "text", response_data.fields[8]); 1923 ExpectFilledField("", "ccexp", "", "text", response_data.fields[9]); 1924 ExpectFilledField("", "email", "theking@gmail.com", "text", 1925 response_data.fields[10]); 1926 } 1927 1928 // Fill the address portion of the billing section. 1929 const int kPageID2 = 2; 1930 GUIDPair guid2("00000000-0000-0000-0000-000000000001", 0); 1931 response_page_id = 0; 1932 FillAutofillFormDataAndSaveResults(kPageID2, form, form.fields[0], 1933 PackGUIDs(empty, guid2), &response_page_id, &response_data); 1934 { 1935 SCOPED_TRACE("Billing address"); 1936 EXPECT_EQ(kPageID2, response_page_id); 1937 EXPECT_EQ(ASCIIToUTF16("MyForm"), response_data.name); 1938 EXPECT_EQ(ASCIIToUTF16("POST"), response_data.method); 1939 EXPECT_EQ(GURL("https://myform.com/form.html"), response_data.origin); 1940 EXPECT_EQ(GURL("https://myform.com/submit.html"), response_data.action); 1941 EXPECT_TRUE(response_data.user_submitted); 1942 ASSERT_EQ(11U, response_data.fields.size()); 1943 1944 ExpectFilledField("", "country", "US", "text", 1945 response_data.fields[0]); 1946 ExpectFilledField("", "firstname", "", "text", response_data.fields[1]); 1947 ExpectFilledField("", "lastname", "", "text", response_data.fields[2]); 1948 ExpectFilledField("", "address", "3734 Elvis Presley Blvd.", "text", 1949 response_data.fields[3]); 1950 ExpectFilledField("", "city", "Memphis", "text", response_data.fields[4]); 1951 ExpectFilledField("", "state", "Tennessee", "text", 1952 response_data.fields[5]); 1953 ExpectFilledField("", "zip", "38116", "text", response_data.fields[6]); 1954 ExpectFilledField("", "ccname", "", "text", response_data.fields[7]); 1955 ExpectFilledField("", "ccnumber", "", "text", response_data.fields[8]); 1956 ExpectFilledField("", "ccexp", "", "text", response_data.fields[9]); 1957 ExpectFilledField("", "email", "", "text", response_data.fields[10]); 1958 } 1959 1960 // Fill the credit card portion of the billing section. 1961 const int kPageID3 = 3; 1962 GUIDPair guid3("00000000-0000-0000-0000-000000000004", 0); 1963 response_page_id = 0; 1964 FillAutofillFormDataAndSaveResults( 1965 kPageID3, form, form.fields[form.fields.size() - 2], 1966 PackGUIDs(guid3, empty), &response_page_id, &response_data); 1967 { 1968 SCOPED_TRACE("Credit card"); 1969 EXPECT_EQ(kPageID3, response_page_id); 1970 EXPECT_EQ(ASCIIToUTF16("MyForm"), response_data.name); 1971 EXPECT_EQ(ASCIIToUTF16("POST"), response_data.method); 1972 EXPECT_EQ(GURL("https://myform.com/form.html"), response_data.origin); 1973 EXPECT_EQ(GURL("https://myform.com/submit.html"), response_data.action); 1974 EXPECT_TRUE(response_data.user_submitted); 1975 ASSERT_EQ(11U, response_data.fields.size()); 1976 1977 ExpectFilledField("", "country", "", "text", response_data.fields[0]); 1978 ExpectFilledField("", "firstname", "", "text", response_data.fields[1]); 1979 ExpectFilledField("", "lastname", "", "text", response_data.fields[2]); 1980 ExpectFilledField("", "address", "", "text", response_data.fields[3]); 1981 ExpectFilledField("", "city", "", "text", response_data.fields[4]); 1982 ExpectFilledField("", "state", "", "text", response_data.fields[5]); 1983 ExpectFilledField("", "zip", "", "text", response_data.fields[6]); 1984 ExpectFilledField("", "ccname", "Elvis Presley", "text", 1985 response_data.fields[7]); 1986 ExpectFilledField("", "ccnumber", "4234567890123456", "text", 1987 response_data.fields[8]); 1988 ExpectFilledField("", "ccexp", "04/2012", "text", response_data.fields[9]); 1989 ExpectFilledField("", "email", "", "text", response_data.fields[10]); 1990 } 1991} 1992 1993// Test that we correctly fill a form that has a single logical section with 1994// multiple email address fields. 1995TEST_F(AutofillManagerTest, FillFormWithMultipleEmails) { 1996 // Set up our form data. 1997 FormData form; 1998 test::CreateTestAddressFormData(&form); 1999 FormFieldData field; 2000 test::CreateTestFormField("Confirm email", "email2", "", "text", &field); 2001 form.fields.push_back(field); 2002 2003 std::vector<FormData> forms(1, form); 2004 FormsSeen(forms); 2005 2006 // Fill the form. 2007 GUIDPair guid("00000000-0000-0000-0000-000000000001", 0); 2008 GUIDPair empty(std::string(), 0); 2009 int response_page_id = 0; 2010 FormData response_data; 2011 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0], 2012 PackGUIDs(empty, guid), &response_page_id, &response_data); 2013 2014 // The second email address should be filled. 2015 EXPECT_EQ(ASCIIToUTF16("theking@gmail.com"), 2016 response_data.fields.back().value); 2017 2018 // The remainder of the form should be filled as usual. 2019 response_data.fields.pop_back(); 2020 ExpectFilledAddressFormElvis( 2021 response_page_id, response_data, kDefaultPageID, false); 2022} 2023 2024// Test that we correctly fill a previously auto-filled form. 2025TEST_F(AutofillManagerTest, FillAutofilledForm) { 2026 // Set up our form data. 2027 FormData form; 2028 test::CreateTestAddressFormData(&form); 2029 // Mark one of the address fields as autofilled. 2030 form.fields[4].is_autofilled = true; 2031 CreateTestCreditCardFormData(&form, true, false); 2032 std::vector<FormData> forms(1, form); 2033 FormsSeen(forms); 2034 2035 // First fill the address data. 2036 GUIDPair guid("00000000-0000-0000-0000-000000000001", 0); 2037 GUIDPair empty(std::string(), 0); 2038 int response_page_id = 0; 2039 FormData response_data; 2040 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, *form.fields.begin(), 2041 PackGUIDs(empty, guid), &response_page_id, &response_data); 2042 { 2043 SCOPED_TRACE("Address"); 2044 ExpectFilledForm(response_page_id, response_data, kDefaultPageID, 2045 "Elvis", "", "", "", "", "", "", "", "", "", "", "", "", 2046 "", "", true, true, false); 2047 } 2048 2049 // Now fill the credit card data. 2050 const int kPageID2 = 2; 2051 GUIDPair guid2("00000000-0000-0000-0000-000000000004", 0); 2052 response_page_id = 0; 2053 FillAutofillFormDataAndSaveResults(kPageID2, form, form.fields.back(), 2054 PackGUIDs(guid2, empty), &response_page_id, &response_data); 2055 { 2056 SCOPED_TRACE("Credit card 1"); 2057 ExpectFilledCreditCardFormElvis( 2058 response_page_id, response_data, kPageID2, true); 2059 } 2060 2061 // Now set the credit card fields to also be auto-filled, and try again to 2062 // fill the credit card data 2063 for (std::vector<FormFieldData>::iterator iter = form.fields.begin(); 2064 iter != form.fields.end(); 2065 ++iter) { 2066 iter->is_autofilled = true; 2067 } 2068 2069 const int kPageID3 = 3; 2070 response_page_id = 0; 2071 FillAutofillFormDataAndSaveResults(kPageID3, form, *form.fields.rbegin(), 2072 PackGUIDs(guid2, empty), &response_page_id, &response_data); 2073 { 2074 SCOPED_TRACE("Credit card 2"); 2075 ExpectFilledForm(response_page_id, response_data, kPageID3, 2076 "", "", "", "", "", "", "", "", "", "", "", "", "", "", 2077 "2012", true, true, false); 2078 } 2079} 2080 2081// Test that we correctly fill an address form with a non-default variant for a 2082// multi-valued field. 2083TEST_F(AutofillManagerTest, FillAddressFormWithVariantType) { 2084 // Set up our form data. 2085 FormData form; 2086 test::CreateTestAddressFormData(&form); 2087 std::vector<FormData> forms(1, form); 2088 FormsSeen(forms); 2089 2090 // Add a name variant to the Elvis profile. 2091 AutofillProfile* profile = autofill_manager_->GetProfileWithGUID( 2092 "00000000-0000-0000-0000-000000000001"); 2093 const base::string16 elvis_name = profile->GetRawInfo(NAME_FULL); 2094 2095 std::vector<base::string16> name_variants; 2096 name_variants.push_back(ASCIIToUTF16("Some Other Guy")); 2097 name_variants.push_back(elvis_name); 2098 profile->SetRawMultiInfo(NAME_FULL, name_variants); 2099 2100 GUIDPair guid(profile->guid(), 1); 2101 GUIDPair empty(std::string(), 0); 2102 int response_page_id = 0; 2103 FormData response_data1; 2104 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0], 2105 PackGUIDs(empty, guid), &response_page_id, &response_data1); 2106 { 2107 SCOPED_TRACE("Valid variant"); 2108 ExpectFilledAddressFormElvis( 2109 response_page_id, response_data1, kDefaultPageID, false); 2110 } 2111 2112 // Try filling with a variant that doesn't exist. The fields to which this 2113 // variant would normally apply should not be filled. 2114 const int kPageID2 = 2; 2115 GUIDPair guid2(profile->guid(), 2); 2116 response_page_id = 0; 2117 FormData response_data2; 2118 FillAutofillFormDataAndSaveResults(kPageID2, form, form.fields[0], 2119 PackGUIDs(empty, guid2), &response_page_id, &response_data2); 2120 { 2121 SCOPED_TRACE("Invalid variant"); 2122 ExpectFilledForm(response_page_id, response_data2, kPageID2, "", "", "", 2123 "3734 Elvis Presley Blvd.", "Apt. 10", "Memphis", 2124 "Tennessee", "38116", "United States", "12345678901", 2125 "theking@gmail.com", "", "", "", "", true, false, false); 2126 } 2127} 2128 2129// Test that we correctly fill a phone number split across multiple fields. 2130TEST_F(AutofillManagerTest, FillPhoneNumber) { 2131 // In one form, rely on the maxlength attribute to imply phone number parts. 2132 // In the other form, rely on the autocompletetype attribute. 2133 FormData form_with_maxlength; 2134 form_with_maxlength.name = ASCIIToUTF16("MyMaxlengthPhoneForm"); 2135 form_with_maxlength.method = ASCIIToUTF16("POST"); 2136 form_with_maxlength.origin = GURL("http://myform.com/phone_form.html"); 2137 form_with_maxlength.action = GURL("http://myform.com/phone_submit.html"); 2138 form_with_maxlength.user_submitted = true; 2139 FormData form_with_autocompletetype = form_with_maxlength; 2140 form_with_autocompletetype.name = ASCIIToUTF16("MyAutocompletetypePhoneForm"); 2141 2142 struct { 2143 const char* label; 2144 const char* name; 2145 size_t max_length; 2146 const char* autocomplete_attribute; 2147 } test_fields[] = { 2148 { "country code", "country_code", 1, "tel-country-code" }, 2149 { "area code", "area_code", 3, "tel-area-code" }, 2150 { "phone", "phone_prefix", 3, "tel-local-prefix" }, 2151 { "-", "phone_suffix", 4, "tel-local-suffix" }, 2152 { "Phone Extension", "ext", 3, "tel-extension" } 2153 }; 2154 2155 FormFieldData field; 2156 const size_t default_max_length = field.max_length; 2157 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_fields); ++i) { 2158 test::CreateTestFormField( 2159 test_fields[i].label, test_fields[i].name, "", "text", &field); 2160 field.max_length = test_fields[i].max_length; 2161 field.autocomplete_attribute = std::string(); 2162 form_with_maxlength.fields.push_back(field); 2163 2164 field.max_length = default_max_length; 2165 field.autocomplete_attribute = test_fields[i].autocomplete_attribute; 2166 form_with_autocompletetype.fields.push_back(field); 2167 } 2168 2169 std::vector<FormData> forms; 2170 forms.push_back(form_with_maxlength); 2171 forms.push_back(form_with_autocompletetype); 2172 FormsSeen(forms); 2173 2174 // We should be able to fill prefix and suffix fields for US numbers. 2175 AutofillProfile* work_profile = autofill_manager_->GetProfileWithGUID( 2176 "00000000-0000-0000-0000-000000000002"); 2177 ASSERT_TRUE(work_profile != NULL); 2178 work_profile->SetRawInfo(PHONE_HOME_WHOLE_NUMBER, 2179 ASCIIToUTF16("16505554567")); 2180 2181 GUIDPair guid(work_profile->guid(), 0); 2182 GUIDPair empty(std::string(), 0); 2183 int page_id = 1; 2184 int response_page_id = 0; 2185 FormData response_data1; 2186 FillAutofillFormDataAndSaveResults(page_id, form_with_maxlength, 2187 *form_with_maxlength.fields.begin(), 2188 PackGUIDs(empty, guid), &response_page_id, &response_data1); 2189 EXPECT_EQ(1, response_page_id); 2190 2191 ASSERT_EQ(5U, response_data1.fields.size()); 2192 EXPECT_EQ(ASCIIToUTF16("1"), response_data1.fields[0].value); 2193 EXPECT_EQ(ASCIIToUTF16("650"), response_data1.fields[1].value); 2194 EXPECT_EQ(ASCIIToUTF16("555"), response_data1.fields[2].value); 2195 EXPECT_EQ(ASCIIToUTF16("4567"), response_data1.fields[3].value); 2196 EXPECT_EQ(base::string16(), response_data1.fields[4].value); 2197 2198 page_id = 2; 2199 response_page_id = 0; 2200 FormData response_data2; 2201 FillAutofillFormDataAndSaveResults(page_id, form_with_autocompletetype, 2202 *form_with_autocompletetype.fields.begin(), 2203 PackGUIDs(empty, guid), &response_page_id, &response_data2); 2204 EXPECT_EQ(2, response_page_id); 2205 2206 ASSERT_EQ(5U, response_data2.fields.size()); 2207 EXPECT_EQ(ASCIIToUTF16("1"), response_data2.fields[0].value); 2208 EXPECT_EQ(ASCIIToUTF16("650"), response_data2.fields[1].value); 2209 EXPECT_EQ(ASCIIToUTF16("555"), response_data2.fields[2].value); 2210 EXPECT_EQ(ASCIIToUTF16("4567"), response_data2.fields[3].value); 2211 EXPECT_EQ(base::string16(), response_data2.fields[4].value); 2212 2213 // We should not be able to fill prefix and suffix fields for international 2214 // numbers. 2215 work_profile->SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("GB")); 2216 work_profile->SetRawInfo(PHONE_HOME_WHOLE_NUMBER, 2217 ASCIIToUTF16("447700954321")); 2218 page_id = 3; 2219 response_page_id = 0; 2220 FormData response_data3; 2221 FillAutofillFormDataAndSaveResults(page_id, form_with_maxlength, 2222 *form_with_maxlength.fields.begin(), 2223 PackGUIDs(empty, guid), &response_page_id, &response_data3); 2224 EXPECT_EQ(3, response_page_id); 2225 2226 ASSERT_EQ(5U, response_data3.fields.size()); 2227 EXPECT_EQ(ASCIIToUTF16("44"), response_data3.fields[0].value); 2228 EXPECT_EQ(ASCIIToUTF16("7700"), response_data3.fields[1].value); 2229 EXPECT_EQ(ASCIIToUTF16("954321"), response_data3.fields[2].value); 2230 EXPECT_EQ(ASCIIToUTF16("954321"), response_data3.fields[3].value); 2231 EXPECT_EQ(base::string16(), response_data3.fields[4].value); 2232 2233 page_id = 4; 2234 response_page_id = 0; 2235 FormData response_data4; 2236 FillAutofillFormDataAndSaveResults(page_id, form_with_autocompletetype, 2237 *form_with_autocompletetype.fields.begin(), 2238 PackGUIDs(empty, guid), &response_page_id, &response_data4); 2239 EXPECT_EQ(4, response_page_id); 2240 2241 ASSERT_EQ(5U, response_data4.fields.size()); 2242 EXPECT_EQ(ASCIIToUTF16("44"), response_data4.fields[0].value); 2243 EXPECT_EQ(ASCIIToUTF16("7700"), response_data4.fields[1].value); 2244 EXPECT_EQ(ASCIIToUTF16("954321"), response_data4.fields[2].value); 2245 EXPECT_EQ(ASCIIToUTF16("954321"), response_data4.fields[3].value); 2246 EXPECT_EQ(base::string16(), response_data4.fields[4].value); 2247 2248 // We should fill all phone fields with the same phone number variant. 2249 std::vector<base::string16> phone_variants; 2250 phone_variants.push_back(ASCIIToUTF16("16505554567")); 2251 phone_variants.push_back(ASCIIToUTF16("18887771234")); 2252 work_profile->SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US")); 2253 work_profile->SetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, phone_variants); 2254 2255 page_id = 5; 2256 response_page_id = 0; 2257 FormData response_data5; 2258 GUIDPair variant_guid(work_profile->guid(), 1); 2259 FillAutofillFormDataAndSaveResults(page_id, form_with_maxlength, 2260 *form_with_maxlength.fields.begin(), 2261 PackGUIDs(empty, variant_guid), &response_page_id, &response_data5); 2262 EXPECT_EQ(5, response_page_id); 2263 2264 ASSERT_EQ(5U, response_data5.fields.size()); 2265 EXPECT_EQ(ASCIIToUTF16("1"), response_data5.fields[0].value); 2266 EXPECT_EQ(ASCIIToUTF16("888"), response_data5.fields[1].value); 2267 EXPECT_EQ(ASCIIToUTF16("777"), response_data5.fields[2].value); 2268 EXPECT_EQ(ASCIIToUTF16("1234"), response_data5.fields[3].value); 2269 EXPECT_EQ(base::string16(), response_data5.fields[4].value); 2270} 2271 2272// Test that we can still fill a form when a field has been removed from it. 2273TEST_F(AutofillManagerTest, FormChangesRemoveField) { 2274 // Set up our form data. 2275 FormData form; 2276 test::CreateTestAddressFormData(&form); 2277 2278 // Add a field -- we'll remove it again later. 2279 FormFieldData field; 2280 test::CreateTestFormField("Some", "field", "", "text", &field); 2281 form.fields.insert(form.fields.begin() + 3, field); 2282 2283 std::vector<FormData> forms(1, form); 2284 FormsSeen(forms); 2285 2286 // Now, after the call to |FormsSeen|, we remove the field before filling. 2287 form.fields.erase(form.fields.begin() + 3); 2288 2289 GUIDPair guid("00000000-0000-0000-0000-000000000001", 0); 2290 GUIDPair empty(std::string(), 0); 2291 int response_page_id = 0; 2292 FormData response_data; 2293 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0], 2294 PackGUIDs(empty, guid), &response_page_id, &response_data); 2295 ExpectFilledAddressFormElvis( 2296 response_page_id, response_data, kDefaultPageID, false); 2297} 2298 2299// Test that we can still fill a form when a field has been added to it. 2300TEST_F(AutofillManagerTest, FormChangesAddField) { 2301 // The offset of the phone field in the address form. 2302 const int kPhoneFieldOffset = 9; 2303 2304 // Set up our form data. 2305 FormData form; 2306 test::CreateTestAddressFormData(&form); 2307 2308 // Remove the phone field -- we'll add it back later. 2309 std::vector<FormFieldData>::iterator pos = 2310 form.fields.begin() + kPhoneFieldOffset; 2311 FormFieldData field = *pos; 2312 pos = form.fields.erase(pos); 2313 2314 std::vector<FormData> forms(1, form); 2315 FormsSeen(forms); 2316 2317 // Now, after the call to |FormsSeen|, we restore the field before filling. 2318 form.fields.insert(pos, field); 2319 2320 GUIDPair guid("00000000-0000-0000-0000-000000000001", 0); 2321 GUIDPair empty(std::string(), 0); 2322 int response_page_id = 0; 2323 FormData response_data; 2324 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0], 2325 PackGUIDs(empty, guid), &response_page_id, &response_data); 2326 ExpectFilledAddressFormElvis( 2327 response_page_id, response_data, kDefaultPageID, false); 2328} 2329 2330// Test that we are able to save form data when forms are submitted. 2331TEST_F(AutofillManagerTest, FormSubmitted) { 2332 // Set up our form data. 2333 FormData form; 2334 test::CreateTestAddressFormData(&form); 2335 std::vector<FormData> forms(1, form); 2336 FormsSeen(forms); 2337 2338 // Fill the form. 2339 GUIDPair guid("00000000-0000-0000-0000-000000000001", 0); 2340 GUIDPair empty(std::string(), 0); 2341 int response_page_id = 0; 2342 FormData response_data; 2343 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0], 2344 PackGUIDs(empty, guid), &response_page_id, &response_data); 2345 ExpectFilledAddressFormElvis( 2346 response_page_id, response_data, kDefaultPageID, false); 2347 2348 // Simulate form submission. We should call into the PDM to try to save the 2349 // filled data. 2350 EXPECT_CALL(personal_data_, SaveImportedProfile(::testing::_)).Times(1); 2351 FormSubmitted(response_data); 2352} 2353 2354// Test that when Autocomplete is enabled and Autofill is disabled, 2355// form submissions are still received by AutocompleteHistoryManager. 2356TEST_F(AutofillManagerTest, FormSubmittedAutocompleteEnabled) { 2357 TestAutofillManagerDelegate delegate; 2358 autofill_manager_.reset(new TestAutofillManager( 2359 autofill_driver_.get(), 2360 &delegate, 2361 NULL)); 2362 autofill_manager_->set_autofill_enabled(false); 2363 scoped_ptr<MockAutocompleteHistoryManager> autocomplete_history_manager; 2364 autocomplete_history_manager.reset( 2365 new MockAutocompleteHistoryManager(autofill_driver_.get(), &delegate)); 2366 autofill_manager_->autocomplete_history_manager_ = 2367 autocomplete_history_manager.Pass(); 2368 2369 // Set up our form data. 2370 FormData form; 2371 test::CreateTestAddressFormData(&form); 2372 form.method = ASCIIToUTF16("GET"); 2373 MockAutocompleteHistoryManager* m = static_cast< 2374 MockAutocompleteHistoryManager*>( 2375 autofill_manager_->autocomplete_history_manager_.get()); 2376 EXPECT_CALL(*m, 2377 OnFormSubmitted(_)).Times(1); 2378 FormSubmitted(form); 2379} 2380 2381// Test that when Autocomplete is enabled and Autofill is disabled, 2382// Autocomplete suggestions are still received. 2383TEST_F(AutofillManagerTest, AutocompleteSuggestionsWhenAutofillDisabled) { 2384 TestAutofillManagerDelegate delegate; 2385 autofill_manager_.reset(new TestAutofillManager( 2386 autofill_driver_.get(), 2387 &delegate, 2388 NULL)); 2389 autofill_manager_->set_autofill_enabled(false); 2390 autofill_manager_->SetExternalDelegate(external_delegate_.get()); 2391 2392 // Set up our form data. 2393 FormData form; 2394 test::CreateTestAddressFormData(&form); 2395 form.method = ASCIIToUTF16("GET"); 2396 std::vector<FormData> forms(1, form); 2397 FormsSeen(forms); 2398 const FormFieldData& field = form.fields[0]; 2399 GetAutofillSuggestions(form, field); 2400 2401 // Add some Autocomplete suggestions. We should return the autocomplete 2402 // suggestions, these will be culled by the renderer. 2403 std::vector<base::string16> suggestions; 2404 suggestions.push_back(ASCIIToUTF16("Jay")); 2405 suggestions.push_back(ASCIIToUTF16("Jason")); 2406 AutocompleteSuggestionsReturned(suggestions); 2407 2408 base::string16 expected_values[] = { 2409 ASCIIToUTF16("Jay"), 2410 ASCIIToUTF16("Jason") 2411 }; 2412 base::string16 expected_labels[] = { base::string16(), base::string16()}; 2413 base::string16 expected_icons[] = { base::string16(), base::string16()}; 2414 int expected_unique_ids[] = {0, 0}; 2415 external_delegate_->CheckSuggestions( 2416 kDefaultPageID, arraysize(expected_values), expected_values, 2417 expected_labels, expected_icons, expected_unique_ids); 2418} 2419 2420// Test that we are able to save form data when forms are submitted and we only 2421// have server data for the field types. 2422TEST_F(AutofillManagerTest, FormSubmittedServerTypes) { 2423 // Set up our form data. 2424 FormData form; 2425 test::CreateTestAddressFormData(&form); 2426 2427 // Simulate having seen this form on page load. 2428 // |form_structure| will be owned by |autofill_manager_|. 2429 TestFormStructure* form_structure = new TestFormStructure(form); 2430 AutofillMetrics metrics_logger; // ignored 2431 form_structure->DetermineHeuristicTypes(metrics_logger); 2432 2433 // Clear the heuristic types, and instead set the appropriate server types. 2434 std::vector<ServerFieldType> heuristic_types, server_types; 2435 for (size_t i = 0; i < form.fields.size(); ++i) { 2436 heuristic_types.push_back(UNKNOWN_TYPE); 2437 server_types.push_back(form_structure->field(i)->heuristic_type()); 2438 } 2439 form_structure->SetFieldTypes(heuristic_types, server_types); 2440 autofill_manager_->AddSeenForm(form_structure); 2441 2442 // Fill the form. 2443 GUIDPair guid("00000000-0000-0000-0000-000000000001", 0); 2444 GUIDPair empty(std::string(), 0); 2445 int response_page_id = 0; 2446 FormData response_data; 2447 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0], 2448 PackGUIDs(empty, guid), &response_page_id, &response_data); 2449 ExpectFilledAddressFormElvis( 2450 response_page_id, response_data, kDefaultPageID, false); 2451 2452 // Simulate form submission. We should call into the PDM to try to save the 2453 // filled data. 2454 EXPECT_CALL(personal_data_, SaveImportedProfile(::testing::_)).Times(1); 2455 FormSubmitted(response_data); 2456} 2457 2458// Test that the form signature for an uploaded form always matches the form 2459// signature from the query. 2460TEST_F(AutofillManagerTest, FormSubmittedWithDifferentFields) { 2461 // Set up our form data. 2462 FormData form; 2463 test::CreateTestAddressFormData(&form); 2464 std::vector<FormData> forms(1, form); 2465 FormsSeen(forms); 2466 2467 // Cache the expected form signature. 2468 std::string signature = FormStructure(form).FormSignature(); 2469 2470 // Change the structure of the form prior to submission. 2471 // Websites would typically invoke JavaScript either on page load or on form 2472 // submit to achieve this. 2473 form.fields.pop_back(); 2474 FormFieldData field = form.fields[3]; 2475 form.fields[3] = form.fields[7]; 2476 form.fields[7] = field; 2477 2478 // Simulate form submission. 2479 FormSubmitted(form); 2480 EXPECT_EQ(signature, autofill_manager_->GetSubmittedFormSignature()); 2481} 2482 2483// Test that we do not save form data when submitted fields contain default 2484// values. 2485TEST_F(AutofillManagerTest, FormSubmittedWithDefaultValues) { 2486 // Set up our form data. 2487 FormData form; 2488 test::CreateTestAddressFormData(&form); 2489 form.fields[3].value = ASCIIToUTF16("Enter your address"); 2490 2491 // Convert the state field to a <select> popup, to make sure that we only 2492 // reject default values for text fields. 2493 ASSERT_TRUE(form.fields[6].name == ASCIIToUTF16("state")); 2494 form.fields[6].form_control_type = "select-one"; 2495 form.fields[6].value = ASCIIToUTF16("Tennessee"); 2496 2497 std::vector<FormData> forms(1, form); 2498 FormsSeen(forms); 2499 2500 // Fill the form. 2501 GUIDPair guid("00000000-0000-0000-0000-000000000001", 0); 2502 GUIDPair empty(std::string(), 0); 2503 int response_page_id = 0; 2504 FormData response_data; 2505 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[3], 2506 PackGUIDs(empty, guid), &response_page_id, &response_data); 2507 2508 // Simulate form submission. We should call into the PDM to try to save the 2509 // filled data. 2510 EXPECT_CALL(personal_data_, SaveImportedProfile(::testing::_)).Times(1); 2511 FormSubmitted(response_data); 2512 2513 // Set the address field's value back to the default value. 2514 response_data.fields[3].value = ASCIIToUTF16("Enter your address"); 2515 2516 // Simulate form submission. We should not call into the PDM to try to save 2517 // the filled data, since the filled form is effectively missing an address. 2518 EXPECT_CALL(personal_data_, SaveImportedProfile(::testing::_)).Times(0); 2519 FormSubmitted(response_data); 2520} 2521 2522// Checks that resetting the auxiliary profile enabled preference does the right 2523// thing on all platforms. 2524TEST_F(AutofillManagerTest, AuxiliaryProfilesReset) { 2525 PrefService* prefs = user_prefs::UserPrefs::Get(profile()); 2526#if defined(OS_MACOSX) || defined(OS_ANDROID) 2527 // Auxiliary profiles is implemented on Mac and Android only. 2528 // OSX: enables Mac Address Book integration. 2529 // Android: enables integration with user's contact profile. 2530 ASSERT_TRUE( 2531 prefs->GetBoolean(::autofill::prefs::kAutofillAuxiliaryProfilesEnabled)); 2532 prefs->SetBoolean( 2533 ::autofill::prefs::kAutofillAuxiliaryProfilesEnabled, false); 2534 prefs->ClearPref(::autofill::prefs::kAutofillAuxiliaryProfilesEnabled); 2535 ASSERT_TRUE( 2536 prefs->GetBoolean(::autofill::prefs::kAutofillAuxiliaryProfilesEnabled)); 2537#else 2538 ASSERT_FALSE( 2539 prefs->GetBoolean(::autofill::prefs::kAutofillAuxiliaryProfilesEnabled)); 2540 prefs->SetBoolean(::autofill::prefs::kAutofillAuxiliaryProfilesEnabled, true); 2541 prefs->ClearPref(::autofill::prefs::kAutofillAuxiliaryProfilesEnabled); 2542 ASSERT_FALSE( 2543 prefs->GetBoolean(::autofill::prefs::kAutofillAuxiliaryProfilesEnabled)); 2544#endif 2545} 2546 2547TEST_F(AutofillManagerTest, DeterminePossibleFieldTypesForUpload) { 2548 FormData form; 2549 form.name = ASCIIToUTF16("MyForm"); 2550 form.method = ASCIIToUTF16("POST"); 2551 form.origin = GURL("http://myform.com/form.html"); 2552 form.action = GURL("http://myform.com/submit.html"); 2553 form.user_submitted = true; 2554 2555 std::vector<ServerFieldTypeSet> expected_types; 2556 2557 // These fields should all match. 2558 FormFieldData field; 2559 ServerFieldTypeSet types; 2560 test::CreateTestFormField("", "1", "Elvis", "text", &field); 2561 types.clear(); 2562 types.insert(NAME_FIRST); 2563 form.fields.push_back(field); 2564 expected_types.push_back(types); 2565 2566 test::CreateTestFormField("", "2", "Aaron", "text", &field); 2567 types.clear(); 2568 types.insert(NAME_MIDDLE); 2569 form.fields.push_back(field); 2570 expected_types.push_back(types); 2571 2572 test::CreateTestFormField("", "3", "A", "text", &field); 2573 types.clear(); 2574 types.insert(NAME_MIDDLE_INITIAL); 2575 form.fields.push_back(field); 2576 expected_types.push_back(types); 2577 2578 test::CreateTestFormField("", "4", "Presley", "text", &field); 2579 types.clear(); 2580 types.insert(NAME_LAST); 2581 form.fields.push_back(field); 2582 expected_types.push_back(types); 2583 2584 test::CreateTestFormField("", "5", "Elvis Presley", "text", &field); 2585 types.clear(); 2586 types.insert(CREDIT_CARD_NAME); 2587 form.fields.push_back(field); 2588 expected_types.push_back(types); 2589 2590 test::CreateTestFormField("", "6", "Elvis Aaron Presley", "text", 2591 &field); 2592 types.clear(); 2593 types.insert(NAME_FULL); 2594 form.fields.push_back(field); 2595 expected_types.push_back(types); 2596 2597 test::CreateTestFormField("", "7", "theking@gmail.com", "email", 2598 &field); 2599 types.clear(); 2600 types.insert(EMAIL_ADDRESS); 2601 form.fields.push_back(field); 2602 expected_types.push_back(types); 2603 2604 test::CreateTestFormField("", "8", "RCA", "text", &field); 2605 types.clear(); 2606 types.insert(COMPANY_NAME); 2607 form.fields.push_back(field); 2608 expected_types.push_back(types); 2609 2610 test::CreateTestFormField("", "9", "3734 Elvis Presley Blvd.", 2611 "text", &field); 2612 types.clear(); 2613 types.insert(ADDRESS_HOME_LINE1); 2614 form.fields.push_back(field); 2615 expected_types.push_back(types); 2616 2617 test::CreateTestFormField("", "10", "Apt. 10", "text", &field); 2618 types.clear(); 2619 types.insert(ADDRESS_HOME_LINE2); 2620 form.fields.push_back(field); 2621 expected_types.push_back(types); 2622 2623 test::CreateTestFormField("", "11", "Memphis", "text", &field); 2624 types.clear(); 2625 types.insert(ADDRESS_HOME_CITY); 2626 form.fields.push_back(field); 2627 expected_types.push_back(types); 2628 2629 test::CreateTestFormField("", "12", "Tennessee", "text", &field); 2630 types.clear(); 2631 types.insert(ADDRESS_HOME_STATE); 2632 form.fields.push_back(field); 2633 expected_types.push_back(types); 2634 2635 test::CreateTestFormField("", "13", "38116", "text", &field); 2636 types.clear(); 2637 types.insert(ADDRESS_HOME_ZIP); 2638 form.fields.push_back(field); 2639 expected_types.push_back(types); 2640 2641 test::CreateTestFormField("", "14", "USA", "text", &field); 2642 types.clear(); 2643 types.insert(ADDRESS_HOME_COUNTRY); 2644 form.fields.push_back(field); 2645 expected_types.push_back(types); 2646 2647 test::CreateTestFormField("", "15", "United States", "text", &field); 2648 types.clear(); 2649 types.insert(ADDRESS_HOME_COUNTRY); 2650 form.fields.push_back(field); 2651 expected_types.push_back(types); 2652 2653 test::CreateTestFormField("", "16", "+1 (234) 567-8901", "text", 2654 &field); 2655 types.clear(); 2656 types.insert(PHONE_HOME_WHOLE_NUMBER); 2657 form.fields.push_back(field); 2658 expected_types.push_back(types); 2659 2660 test::CreateTestFormField("", "17", "2345678901", "text", &field); 2661 types.clear(); 2662 types.insert(PHONE_HOME_CITY_AND_NUMBER); 2663 form.fields.push_back(field); 2664 expected_types.push_back(types); 2665 2666 test::CreateTestFormField("", "18", "1", "text", &field); 2667 types.clear(); 2668 types.insert(PHONE_HOME_COUNTRY_CODE); 2669 form.fields.push_back(field); 2670 expected_types.push_back(types); 2671 2672 test::CreateTestFormField("", "19", "234", "text", &field); 2673 types.clear(); 2674 types.insert(PHONE_HOME_CITY_CODE); 2675 form.fields.push_back(field); 2676 expected_types.push_back(types); 2677 2678 test::CreateTestFormField("", "20", "5678901", "text", &field); 2679 types.clear(); 2680 types.insert(PHONE_HOME_NUMBER); 2681 form.fields.push_back(field); 2682 expected_types.push_back(types); 2683 2684 test::CreateTestFormField("", "21", "567", "text", &field); 2685 types.clear(); 2686 types.insert(PHONE_HOME_NUMBER); 2687 form.fields.push_back(field); 2688 expected_types.push_back(types); 2689 2690 test::CreateTestFormField("", "22", "8901", "text", &field); 2691 types.clear(); 2692 types.insert(PHONE_HOME_NUMBER); 2693 form.fields.push_back(field); 2694 expected_types.push_back(types); 2695 2696 test::CreateTestFormField("", "23", "4234-5678-9012-3456", "text", 2697 &field); 2698 types.clear(); 2699 types.insert(CREDIT_CARD_NUMBER); 2700 form.fields.push_back(field); 2701 expected_types.push_back(types); 2702 2703 test::CreateTestFormField("", "24", "04", "text", &field); 2704 types.clear(); 2705 types.insert(CREDIT_CARD_EXP_MONTH); 2706 form.fields.push_back(field); 2707 expected_types.push_back(types); 2708 2709 test::CreateTestFormField("", "25", "April", "text", &field); 2710 types.clear(); 2711 types.insert(CREDIT_CARD_EXP_MONTH); 2712 form.fields.push_back(field); 2713 expected_types.push_back(types); 2714 2715 test::CreateTestFormField("", "26", "2012", "text", &field); 2716 types.clear(); 2717 types.insert(CREDIT_CARD_EXP_4_DIGIT_YEAR); 2718 form.fields.push_back(field); 2719 expected_types.push_back(types); 2720 2721 test::CreateTestFormField("", "27", "12", "text", &field); 2722 types.clear(); 2723 types.insert(CREDIT_CARD_EXP_2_DIGIT_YEAR); 2724 form.fields.push_back(field); 2725 expected_types.push_back(types); 2726 2727 test::CreateTestFormField("", "28", "04/2012", "text", &field); 2728 types.clear(); 2729 types.insert(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR); 2730 form.fields.push_back(field); 2731 expected_types.push_back(types); 2732 2733 // Make sure that we trim whitespace properly. 2734 test::CreateTestFormField("", "29", "", "text", &field); 2735 types.clear(); 2736 types.insert(EMPTY_TYPE); 2737 form.fields.push_back(field); 2738 expected_types.push_back(types); 2739 2740 test::CreateTestFormField("", "30", " ", "text", &field); 2741 types.clear(); 2742 types.insert(EMPTY_TYPE); 2743 form.fields.push_back(field); 2744 expected_types.push_back(types); 2745 2746 test::CreateTestFormField("", "31", " Elvis", "text", &field); 2747 types.clear(); 2748 types.insert(NAME_FIRST); 2749 form.fields.push_back(field); 2750 expected_types.push_back(types); 2751 2752 test::CreateTestFormField("", "32", "Elvis ", "text", &field); 2753 types.clear(); 2754 types.insert(NAME_FIRST); 2755 form.fields.push_back(field); 2756 expected_types.push_back(types); 2757 2758 // These fields should not match, as they differ by case. 2759 test::CreateTestFormField("", "33", "elvis", "text", &field); 2760 types.clear(); 2761 types.insert(UNKNOWN_TYPE); 2762 form.fields.push_back(field); 2763 expected_types.push_back(types); 2764 2765 test::CreateTestFormField("", "34", "3734 Elvis Presley BLVD", 2766 "text", &field); 2767 types.clear(); 2768 types.insert(UNKNOWN_TYPE); 2769 form.fields.push_back(field); 2770 expected_types.push_back(types); 2771 2772 // These fields should not match, as they are unsupported variants. 2773 test::CreateTestFormField("", "35", "Elvis Aaron", "text", &field); 2774 types.clear(); 2775 types.insert(UNKNOWN_TYPE); 2776 form.fields.push_back(field); 2777 expected_types.push_back(types); 2778 2779 test::CreateTestFormField("", "36", "Mr. Presley", "text", &field); 2780 types.clear(); 2781 types.insert(UNKNOWN_TYPE); 2782 form.fields.push_back(field); 2783 expected_types.push_back(types); 2784 2785 test::CreateTestFormField("", "37", "3734 Elvis Presley", "text", 2786 &field); 2787 types.clear(); 2788 types.insert(UNKNOWN_TYPE); 2789 form.fields.push_back(field); 2790 expected_types.push_back(types); 2791 2792 test::CreateTestFormField("", "38", "TN", "text", &field); 2793 types.clear(); 2794 types.insert(UNKNOWN_TYPE); 2795 form.fields.push_back(field); 2796 expected_types.push_back(types); 2797 2798 test::CreateTestFormField("", "39", "38116-1023", "text", &field); 2799 types.clear(); 2800 types.insert(UNKNOWN_TYPE); 2801 form.fields.push_back(field); 2802 expected_types.push_back(types); 2803 2804 test::CreateTestFormField("", "20", "5", "text", &field); 2805 types.clear(); 2806 types.insert(UNKNOWN_TYPE); 2807 form.fields.push_back(field); 2808 expected_types.push_back(types); 2809 2810 test::CreateTestFormField("", "20", "56", "text", &field); 2811 types.clear(); 2812 types.insert(UNKNOWN_TYPE); 2813 form.fields.push_back(field); 2814 expected_types.push_back(types); 2815 2816 test::CreateTestFormField("", "20", "901", "text", &field); 2817 types.clear(); 2818 types.insert(UNKNOWN_TYPE); 2819 form.fields.push_back(field); 2820 expected_types.push_back(types); 2821 2822 test::CreateTestFormField("", "40", "mypassword", "password", &field); 2823 types.clear(); 2824 types.insert(PASSWORD); 2825 form.fields.push_back(field); 2826 expected_types.push_back(types); 2827 2828 autofill_manager_->set_expected_submitted_field_types(expected_types); 2829 FormSubmitted(form); 2830} 2831 2832TEST_F(AutofillManagerTest, RemoveProfile) { 2833 // Add and remove an Autofill profile. 2834 AutofillProfile* profile = new AutofillProfile; 2835 std::string guid = "00000000-0000-0000-0000-000000000102"; 2836 profile->set_guid(guid.c_str()); 2837 autofill_manager_->AddProfile(profile); 2838 2839 GUIDPair guid_pair(guid, 0); 2840 GUIDPair empty(std::string(), 0); 2841 int id = PackGUIDs(empty, guid_pair); 2842 2843 autofill_manager_->RemoveAutofillProfileOrCreditCard(id); 2844 2845 EXPECT_FALSE(autofill_manager_->GetProfileWithGUID(guid.c_str())); 2846} 2847 2848TEST_F(AutofillManagerTest, RemoveCreditCard){ 2849 // Add and remove an Autofill credit card. 2850 CreditCard* credit_card = new CreditCard; 2851 std::string guid = "00000000-0000-0000-0000-000000100007"; 2852 credit_card->set_guid(guid.c_str()); 2853 autofill_manager_->AddCreditCard(credit_card); 2854 2855 GUIDPair guid_pair(guid, 0); 2856 GUIDPair empty(std::string(), 0); 2857 int id = PackGUIDs(guid_pair, empty); 2858 2859 autofill_manager_->RemoveAutofillProfileOrCreditCard(id); 2860 2861 EXPECT_FALSE(autofill_manager_->GetCreditCardWithGUID(guid.c_str())); 2862} 2863 2864TEST_F(AutofillManagerTest, RemoveProfileVariant) { 2865 // Add and remove an Autofill profile. 2866 AutofillProfile* profile = new AutofillProfile; 2867 std::string guid = "00000000-0000-0000-0000-000000000102"; 2868 profile->set_guid(guid.c_str()); 2869 autofill_manager_->AddProfile(profile); 2870 2871 GUIDPair guid_pair(guid, 1); 2872 GUIDPair empty(std::string(), 0); 2873 int id = PackGUIDs(empty, guid_pair); 2874 2875 autofill_manager_->RemoveAutofillProfileOrCreditCard(id); 2876 2877 // TODO(csharp): Currently variants should not be deleted, but once they are 2878 // update these expectations. 2879 // http://crbug.com/124211 2880 EXPECT_TRUE(autofill_manager_->GetProfileWithGUID(guid.c_str())); 2881} 2882 2883namespace { 2884 2885class MockAutofillManagerDelegate : public TestAutofillManagerDelegate { 2886 public: 2887 MockAutofillManagerDelegate() {} 2888 2889 virtual ~MockAutofillManagerDelegate() {} 2890 2891 virtual void ShowRequestAutocompleteDialog( 2892 const FormData& form, 2893 const GURL& source_url, 2894 const base::Callback<void(const FormStructure*)>& callback) OVERRIDE { 2895 callback.Run(user_supplied_data_.get()); 2896 } 2897 2898 void SetUserSuppliedData(scoped_ptr<FormStructure> user_supplied_data) { 2899 user_supplied_data_.reset(user_supplied_data.release()); 2900 } 2901 2902 private: 2903 scoped_ptr<FormStructure> user_supplied_data_; 2904 2905 DISALLOW_COPY_AND_ASSIGN(MockAutofillManagerDelegate); 2906}; 2907 2908} // namespace 2909 2910// Test our external delegate is called at the right time. 2911TEST_F(AutofillManagerTest, TestExternalDelegate) { 2912 FormData form; 2913 test::CreateTestAddressFormData(&form); 2914 std::vector<FormData> forms(1, form); 2915 FormsSeen(forms); 2916 const FormFieldData& field = form.fields[0]; 2917 GetAutofillSuggestions(form, field); // should call the delegate's OnQuery() 2918 2919 EXPECT_TRUE(external_delegate_->on_query_seen()); 2920} 2921 2922} // namespace autofill 2923