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