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