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