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