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