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.
8 #include "base/command_line.h"
9 #include "base/format_macros.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/prefs/pref_service.h"
15 #include "base/run_loop.h"
16 #include "base/strings/string16.h"
17 #include "base/strings/string_number_conversions.h"
18 #include "base/strings/stringprintf.h"
19 #include "base/strings/utf_string_conversions.h"
20 #include "base/time/time.h"
21 #include "base/tuple.h"
22 #include "components/autofill/core/browser/autocomplete_history_manager.h"
23 #include "components/autofill/core/browser/autofill_manager.h"
24 #include "components/autofill/core/browser/autofill_metrics.h"
25 #include "components/autofill/core/browser/autofill_profile.h"
26 #include "components/autofill/core/browser/autofill_test_utils.h"
27 #include "components/autofill/core/browser/credit_card.h"
28 #include "components/autofill/core/browser/personal_data_manager.h"
29 #include "components/autofill/core/browser/popup_item_ids.h"
30 #include "components/autofill/core/browser/test_autofill_client.h"
31 #include "components/autofill/core/browser/test_autofill_driver.h"
32 #include "components/autofill/core/browser/test_autofill_external_delegate.h"
33 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
34 #include "components/autofill/core/common/autofill_pref_names.h"
35 #include "components/autofill/core/common/form_data.h"
36 #include "components/autofill/core/common/form_field_data.h"
37 #include "grit/components_strings.h"
38 #include "testing/gmock/include/gmock/gmock.h"
39 #include "testing/gtest/include/gtest/gtest.h"
40 #include "ui/base/l10n/l10n_util.h"
41 #include "ui/gfx/rect.h"
44 using base::ASCIIToUTF16;
45 using base::UTF8ToUTF16;
50 typedef PersonalDataManager::GUIDPair GUIDPair;
54 const int kDefaultPageID = 137;
56 class TestPersonalDataManager : public PersonalDataManager {
58 TestPersonalDataManager() : PersonalDataManager("en-US") {
59 CreateTestAutofillProfiles(&web_profiles_);
60 CreateTestCreditCards(&credit_cards_);
63 using PersonalDataManager::set_database;
64 using PersonalDataManager::SetPrefService;
66 MOCK_METHOD1(SaveImportedProfile, std::string(const AutofillProfile&));
68 AutofillProfile* GetProfileWithGUID(const char* guid) {
69 for (std::vector<AutofillProfile *>::iterator it = web_profiles_.begin();
70 it != web_profiles_.end(); ++it) {
71 if (!(*it)->guid().compare(guid))
77 CreditCard* GetCreditCardWithGUID(const char* guid) {
78 for (std::vector<CreditCard *>::iterator it = credit_cards_.begin();
79 it != credit_cards_.end(); ++it){
80 if (!(*it)->guid().compare(guid))
86 void AddProfile(AutofillProfile* profile) {
87 web_profiles_.push_back(profile);
90 void AddCreditCard(CreditCard* credit_card) {
91 credit_cards_.push_back(credit_card);
94 virtual void RemoveByGUID(const std::string& guid) OVERRIDE {
95 CreditCard* credit_card = GetCreditCardWithGUID(guid.c_str());
98 std::remove(credit_cards_.begin(), credit_cards_.end(), credit_card),
102 AutofillProfile* profile = GetProfileWithGUID(guid.c_str());
105 std::remove(web_profiles_.begin(), web_profiles_.end(), profile),
106 web_profiles_.end());
110 // Do nothing (auxiliary profiles will be created in
111 // CreateTestAuxiliaryProfile).
112 virtual void LoadAuxiliaryProfiles(bool record_metrics) const OVERRIDE {}
114 void ClearAutofillProfiles() {
115 web_profiles_.clear();
118 void ClearCreditCards() {
119 credit_cards_.clear();
122 void CreateTestAuxiliaryProfiles() {
123 CreateTestAutofillProfiles(&auxiliary_profiles_);
126 void CreateTestCreditCardsYearAndMonth(const char* year, const char* month) {
128 CreditCard* credit_card = new CreditCard;
129 test::SetCreditCardInfo(credit_card, "Miku Hatsune",
130 "4234567890654321", // Visa
132 credit_card->set_guid("00000000-0000-0000-0000-000000000007");
133 credit_cards_.push_back(credit_card);
137 void CreateTestAutofillProfiles(ScopedVector<AutofillProfile>* profiles) {
138 AutofillProfile* profile = new AutofillProfile;
139 test::SetProfileInfo(profile, "Elvis", "Aaron",
140 "Presley", "theking@gmail.com", "RCA",
141 "3734 Elvis Presley Blvd.", "Apt. 10",
142 "Memphis", "Tennessee", "38116", "US",
144 profile->set_guid("00000000-0000-0000-0000-000000000001");
145 profiles->push_back(profile);
146 profile = new AutofillProfile;
147 test::SetProfileInfo(profile, "Charles", "Hardin",
148 "Holley", "buddy@gmail.com", "Decca",
149 "123 Apple St.", "unit 6", "Lubbock",
150 "Texas", "79401", "US", "23456789012");
151 profile->set_guid("00000000-0000-0000-0000-000000000002");
152 profiles->push_back(profile);
153 profile = new AutofillProfile;
154 test::SetProfileInfo(
155 profile, "", "", "", "", "", "", "", "", "", "", "", "");
156 profile->set_guid("00000000-0000-0000-0000-000000000003");
157 profiles->push_back(profile);
160 void CreateTestCreditCards(ScopedVector<CreditCard>* credit_cards) {
161 CreditCard* credit_card = new CreditCard;
162 test::SetCreditCardInfo(credit_card, "Elvis Presley",
163 "4234 5678 9012 3456", // Visa
165 credit_card->set_guid("00000000-0000-0000-0000-000000000004");
166 credit_cards->push_back(credit_card);
168 credit_card = new CreditCard;
169 test::SetCreditCardInfo(credit_card, "Buddy Holly",
170 "5187654321098765", // Mastercard
172 credit_card->set_guid("00000000-0000-0000-0000-000000000005");
173 credit_cards->push_back(credit_card);
175 credit_card = new CreditCard;
176 test::SetCreditCardInfo(credit_card, "", "", "", "");
177 credit_card->set_guid("00000000-0000-0000-0000-000000000006");
178 credit_cards->push_back(credit_card);
181 DISALLOW_COPY_AND_ASSIGN(TestPersonalDataManager);
184 // Populates |form| with data corresponding to a simple credit card form.
185 // Note that this actually appends fields to the form data, which can be useful
186 // for building up more complex test forms.
187 void CreateTestCreditCardFormData(FormData* form,
189 bool use_month_type) {
190 form->name = ASCIIToUTF16("MyForm");
192 form->origin = GURL("https://myform.com/form.html");
193 form->action = GURL("https://myform.com/submit.html");
195 form->origin = GURL("http://myform.com/form.html");
196 form->action = GURL("http://myform.com/submit.html");
198 form->user_submitted = true;
201 test::CreateTestFormField("Name on Card", "nameoncard", "", "text", &field);
202 form->fields.push_back(field);
203 test::CreateTestFormField("Card Number", "cardnumber", "", "text", &field);
204 form->fields.push_back(field);
205 if (use_month_type) {
206 test::CreateTestFormField(
207 "Expiration Date", "ccmonth", "", "month", &field);
208 form->fields.push_back(field);
210 test::CreateTestFormField("Expiration Date", "ccmonth", "", "text", &field);
211 form->fields.push_back(field);
212 test::CreateTestFormField("", "ccyear", "", "text", &field);
213 form->fields.push_back(field);
217 void ExpectFilledField(const char* expected_label,
218 const char* expected_name,
219 const char* expected_value,
220 const char* expected_form_control_type,
221 const FormFieldData& field) {
222 SCOPED_TRACE(expected_label);
223 EXPECT_EQ(UTF8ToUTF16(expected_label), field.label);
224 EXPECT_EQ(UTF8ToUTF16(expected_name), field.name);
225 EXPECT_EQ(UTF8ToUTF16(expected_value), field.value);
226 EXPECT_EQ(expected_form_control_type, field.form_control_type);
229 // Verifies that the |filled_form| has been filled with the given data.
230 // Verifies address fields if |has_address_fields| is true, and verifies
231 // credit card fields if |has_credit_card_fields| is true. Verifies both if both
232 // are true. |use_month_type| is used for credit card input month type.
233 void ExpectFilledForm(int page_id,
234 const FormData& filled_form,
235 int expected_page_id,
239 const char* address1,
240 const char* address2,
243 const char* postal_code,
247 const char* name_on_card,
248 const char* card_number,
249 const char* expiration_month,
250 const char* expiration_year,
251 bool has_address_fields,
252 bool has_credit_card_fields,
253 bool use_month_type) {
254 // The number of fields in the address and credit card forms created above.
255 const size_t kAddressFormSize = 11;
256 const size_t kCreditCardFormSize = use_month_type ? 3 : 4;
258 EXPECT_EQ(expected_page_id, page_id);
259 EXPECT_EQ(ASCIIToUTF16("MyForm"), filled_form.name);
260 if (has_credit_card_fields) {
261 EXPECT_EQ(GURL("https://myform.com/form.html"), filled_form.origin);
262 EXPECT_EQ(GURL("https://myform.com/submit.html"), filled_form.action);
264 EXPECT_EQ(GURL("http://myform.com/form.html"), filled_form.origin);
265 EXPECT_EQ(GURL("http://myform.com/submit.html"), filled_form.action);
267 EXPECT_TRUE(filled_form.user_submitted);
269 size_t form_size = 0;
270 if (has_address_fields)
271 form_size += kAddressFormSize;
272 if (has_credit_card_fields)
273 form_size += kCreditCardFormSize;
274 ASSERT_EQ(form_size, filled_form.fields.size());
276 if (has_address_fields) {
277 ExpectFilledField("First Name", "firstname", first, "text",
278 filled_form.fields[0]);
279 ExpectFilledField("Middle Name", "middlename", middle, "text",
280 filled_form.fields[1]);
281 ExpectFilledField("Last Name", "lastname", last, "text",
282 filled_form.fields[2]);
283 ExpectFilledField("Address Line 1", "addr1", address1, "text",
284 filled_form.fields[3]);
285 ExpectFilledField("Address Line 2", "addr2", address2, "text",
286 filled_form.fields[4]);
287 ExpectFilledField("City", "city", city, "text",
288 filled_form.fields[5]);
289 ExpectFilledField("State", "state", state, "text",
290 filled_form.fields[6]);
291 ExpectFilledField("Postal Code", "zipcode", postal_code, "text",
292 filled_form.fields[7]);
293 ExpectFilledField("Country", "country", country, "text",
294 filled_form.fields[8]);
295 ExpectFilledField("Phone Number", "phonenumber", phone, "tel",
296 filled_form.fields[9]);
297 ExpectFilledField("Email", "email", email, "email",
298 filled_form.fields[10]);
301 if (has_credit_card_fields) {
302 size_t offset = has_address_fields? kAddressFormSize : 0;
303 ExpectFilledField("Name on Card", "nameoncard", name_on_card, "text",
304 filled_form.fields[offset + 0]);
305 ExpectFilledField("Card Number", "cardnumber", card_number, "text",
306 filled_form.fields[offset + 1]);
307 if (use_month_type) {
308 std::string exp_year = expiration_year;
309 std::string exp_month = expiration_month;
311 if (!exp_year.empty() && !exp_month.empty())
312 date = exp_year + "-" + exp_month;
314 ExpectFilledField("Expiration Date", "ccmonth", date.c_str(), "month",
315 filled_form.fields[offset + 2]);
317 ExpectFilledField("Expiration Date", "ccmonth", expiration_month, "text",
318 filled_form.fields[offset + 2]);
319 ExpectFilledField("", "ccyear", expiration_year, "text",
320 filled_form.fields[offset + 3]);
325 void ExpectFilledAddressFormElvis(int page_id,
326 const FormData& filled_form,
327 int expected_page_id,
328 bool has_credit_card_fields) {
329 ExpectFilledForm(page_id, filled_form, expected_page_id, "Elvis", "Aaron",
330 "Presley", "3734 Elvis Presley Blvd.", "Apt. 10", "Memphis",
331 "Tennessee", "38116", "United States", "12345678901",
332 "theking@gmail.com", "", "", "", "", true,
333 has_credit_card_fields, false);
336 void ExpectFilledCreditCardFormElvis(int page_id,
337 const FormData& filled_form,
338 int expected_page_id,
339 bool has_address_fields) {
340 ExpectFilledForm(page_id, filled_form, expected_page_id,
341 "", "", "", "", "", "", "", "", "", "", "",
342 "Elvis Presley", "4234567890123456", "04", "2012",
343 has_address_fields, true, false);
346 void ExpectFilledCreditCardYearMonthWithYearMonth(int page_id,
347 const FormData& filled_form,
348 int expected_page_id,
349 bool has_address_fields,
352 ExpectFilledForm(page_id, filled_form, expected_page_id,
353 "", "", "", "", "", "", "", "", "", "", "",
354 "Miku Hatsune", "4234567890654321", month, year,
355 has_address_fields, true, true);
358 class MockAutocompleteHistoryManager : public AutocompleteHistoryManager {
360 MockAutocompleteHistoryManager(AutofillDriver* driver, AutofillClient* client)
361 : AutocompleteHistoryManager(driver, client) {}
363 MOCK_METHOD1(OnFormSubmitted, void(const FormData& form));
366 DISALLOW_COPY_AND_ASSIGN(MockAutocompleteHistoryManager);
369 class MockAutofillDriver : public TestAutofillDriver {
371 MockAutofillDriver() {}
373 // Mock methods to enable testability.
374 MOCK_METHOD3(SendFormDataToRenderer, void(int query_id,
375 RendererFormDataAction action,
376 const FormData& data));
379 DISALLOW_COPY_AND_ASSIGN(MockAutofillDriver);
382 class TestAutofillManager : public AutofillManager {
384 TestAutofillManager(AutofillDriver* driver,
385 autofill::AutofillClient* client,
386 TestPersonalDataManager* personal_data)
387 : AutofillManager(driver, client, personal_data),
388 personal_data_(personal_data),
389 autofill_enabled_(true) {}
390 virtual ~TestAutofillManager() {}
392 virtual bool IsAutofillEnabled() const OVERRIDE { return autofill_enabled_; }
394 void set_autofill_enabled(bool autofill_enabled) {
395 autofill_enabled_ = autofill_enabled;
398 void set_expected_submitted_field_types(
399 const std::vector<ServerFieldTypeSet>& expected_types) {
400 expected_submitted_field_types_ = expected_types;
403 virtual void UploadFormDataAsyncCallback(
404 const FormStructure* submitted_form,
405 const base::TimeTicks& load_time,
406 const base::TimeTicks& interaction_time,
407 const base::TimeTicks& submission_time) OVERRIDE {
410 // If we have expected field types set, make sure they match.
411 if (!expected_submitted_field_types_.empty()) {
412 ASSERT_EQ(expected_submitted_field_types_.size(),
413 submitted_form->field_count());
414 for (size_t i = 0; i < expected_submitted_field_types_.size(); ++i) {
417 "Field %d with value %s", static_cast<int>(i),
418 base::UTF16ToUTF8(submitted_form->field(i)->value).c_str()));
419 const ServerFieldTypeSet& possible_types =
420 submitted_form->field(i)->possible_types();
421 EXPECT_EQ(expected_submitted_field_types_[i].size(),
422 possible_types.size());
423 for (ServerFieldTypeSet::const_iterator it =
424 expected_submitted_field_types_[i].begin();
425 it != expected_submitted_field_types_[i].end(); ++it) {
426 EXPECT_TRUE(possible_types.count(*it))
427 << "Expected type: " << AutofillType(*it).ToString();
432 AutofillManager::UploadFormDataAsyncCallback(submitted_form,
438 // Resets the run loop so that it can wait for an asynchronous form
439 // submission to complete.
440 void ResetRunLoop() { run_loop_.reset(new base::RunLoop()); }
442 // Wait for the asynchronous OnFormSubmitted() call to complete.
443 void WaitForAsyncFormSubmit() { run_loop_->Run(); }
445 virtual void UploadFormData(const FormStructure& submitted_form) OVERRIDE {
446 submitted_form_signature_ = submitted_form.FormSignature();
449 const std::string GetSubmittedFormSignature() {
450 return submitted_form_signature_;
453 AutofillProfile* GetProfileWithGUID(const char* guid) {
454 return personal_data_->GetProfileWithGUID(guid);
457 CreditCard* GetCreditCardWithGUID(const char* guid) {
458 return personal_data_->GetCreditCardWithGUID(guid);
461 void AddProfile(AutofillProfile* profile) {
462 personal_data_->AddProfile(profile);
465 void AddCreditCard(CreditCard* credit_card) {
466 personal_data_->AddCreditCard(credit_card);
469 int GetPackedCreditCardID(int credit_card_id) {
470 std::string credit_card_guid =
471 base::StringPrintf("00000000-0000-0000-0000-%012d", credit_card_id);
473 return PackGUIDs(GUIDPair(credit_card_guid, 0), GUIDPair(std::string(), 0));
476 void AddSeenForm(FormStructure* form) {
477 form_structures()->push_back(form);
480 void ClearFormStructures() {
481 form_structures()->clear();
486 TestPersonalDataManager* personal_data_;
488 bool autofill_enabled_;
490 scoped_ptr<base::RunLoop> run_loop_;
492 std::string submitted_form_signature_;
493 std::vector<ServerFieldTypeSet> expected_submitted_field_types_;
495 DISALLOW_COPY_AND_ASSIGN(TestAutofillManager);
498 class TestAutofillExternalDelegate : public AutofillExternalDelegate {
500 explicit TestAutofillExternalDelegate(AutofillManager* autofill_manager,
501 AutofillDriver* autofill_driver)
502 : AutofillExternalDelegate(autofill_manager, autofill_driver),
503 on_query_seen_(false),
504 on_suggestions_returned_seen_(false) {}
505 virtual ~TestAutofillExternalDelegate() {}
507 virtual void OnQuery(int query_id,
508 const FormData& form,
509 const FormFieldData& field,
510 const gfx::RectF& bounds,
511 bool display_warning) OVERRIDE {
512 on_query_seen_ = true;
513 on_suggestions_returned_seen_ = false;
516 virtual void OnSuggestionsReturned(
518 const std::vector<base::string16>& autofill_values,
519 const std::vector<base::string16>& autofill_labels,
520 const std::vector<base::string16>& autofill_icons,
521 const std::vector<int>& autofill_unique_ids) OVERRIDE {
522 on_suggestions_returned_seen_ = true;
524 query_id_ = query_id;
525 autofill_values_ = autofill_values;
526 autofill_labels_ = autofill_labels;
527 autofill_icons_ = autofill_icons;
528 autofill_unique_ids_ = autofill_unique_ids;
531 void CheckSuggestions(int expected_page_id,
532 size_t expected_num_suggestions,
533 const base::string16 expected_values[],
534 const base::string16 expected_labels[],
535 const base::string16 expected_icons[],
536 const int expected_unique_ids[]) {
537 // Ensure that these results are from the most recent query.
538 EXPECT_TRUE(on_suggestions_returned_seen_);
540 EXPECT_EQ(expected_page_id, query_id_);
541 ASSERT_EQ(expected_num_suggestions, autofill_values_.size());
542 ASSERT_EQ(expected_num_suggestions, autofill_labels_.size());
543 ASSERT_EQ(expected_num_suggestions, autofill_icons_.size());
544 ASSERT_EQ(expected_num_suggestions, autofill_unique_ids_.size());
545 for (size_t i = 0; i < expected_num_suggestions; ++i) {
546 SCOPED_TRACE(base::StringPrintf("i: %" PRIuS, i));
547 EXPECT_EQ(expected_values[i], autofill_values_[i]);
548 EXPECT_EQ(expected_labels[i], autofill_labels_[i]);
549 EXPECT_EQ(expected_icons[i], autofill_icons_[i]);
550 EXPECT_EQ(expected_unique_ids[i], autofill_unique_ids_[i]);
554 bool on_query_seen() const {
555 return on_query_seen_;
558 bool on_suggestions_returned_seen() const {
559 return on_suggestions_returned_seen_;
563 // Records if OnQuery has been called yet.
566 // Records if OnSuggestionsReturned has been called after the most recent
568 bool on_suggestions_returned_seen_;
570 // The query id of the most recent Autofill query.
573 // The results returned by the most recent Autofill query.
574 std::vector<base::string16> autofill_values_;
575 std::vector<base::string16> autofill_labels_;
576 std::vector<base::string16> autofill_icons_;
577 std::vector<int> autofill_unique_ids_;
579 DISALLOW_COPY_AND_ASSIGN(TestAutofillExternalDelegate);
584 class AutofillManagerTest : public testing::Test {
586 virtual void SetUp() OVERRIDE {
587 autofill_client_.SetPrefs(test::PrefServiceForTesting());
588 personal_data_.set_database(autofill_client_.GetDatabase());
589 personal_data_.SetPrefService(autofill_client_.GetPrefs());
590 autofill_driver_.reset(new MockAutofillDriver());
591 autofill_manager_.reset(new TestAutofillManager(
592 autofill_driver_.get(), &autofill_client_, &personal_data_));
594 external_delegate_.reset(new TestAutofillExternalDelegate(
595 autofill_manager_.get(),
596 autofill_driver_.get()));
597 autofill_manager_->SetExternalDelegate(external_delegate_.get());
600 virtual void TearDown() OVERRIDE {
601 // Order of destruction is important as AutofillManager relies on
602 // PersonalDataManager to be around when it gets destroyed.
603 autofill_manager_.reset();
604 autofill_driver_.reset();
606 // Remove the AutofillWebDataService so TestPersonalDataManager does not
607 // need to care about removing self as an observer in destruction.
608 personal_data_.set_database(scoped_refptr<AutofillWebDataService>(NULL));
609 personal_data_.SetPrefService(NULL);
612 void GetAutofillSuggestions(int query_id,
613 const FormData& form,
614 const FormFieldData& field) {
615 autofill_manager_->OnQueryFormFieldAutofill(query_id,
622 void GetAutofillSuggestions(const FormData& form,
623 const FormFieldData& field) {
624 GetAutofillSuggestions(kDefaultPageID, form, field);
627 void AutocompleteSuggestionsReturned(
628 const std::vector<base::string16>& result) {
629 autofill_manager_->autocomplete_history_manager_->SendSuggestions(&result);
632 void FormsSeen(const std::vector<FormData>& forms) {
633 autofill_manager_->OnFormsSeen(forms, base::TimeTicks());
636 void FormSubmitted(const FormData& form) {
637 autofill_manager_->ResetRunLoop();
638 if (autofill_manager_->OnFormSubmitted(form, base::TimeTicks::Now()))
639 autofill_manager_->WaitForAsyncFormSubmit();
642 void FillAutofillFormData(int query_id,
643 const FormData& form,
644 const FormFieldData& field,
646 autofill_manager_->FillOrPreviewForm(
647 AutofillDriver::FORM_DATA_ACTION_FILL, query_id, form, field,
651 // Calls |autofill_manager_->OnFillAutofillFormData()| with the specified
652 // input parameters after setting up the expectation that the mock driver's
653 // |SendFormDataToRenderer()| method will be called and saving the parameters
654 // of that call into the |response_query_id| and |response_data| output
656 void FillAutofillFormDataAndSaveResults(int input_query_id,
657 const FormData& input_form,
658 const FormFieldData& input_field,
660 int* response_query_id,
661 FormData* response_data) {
662 EXPECT_CALL(*autofill_driver_, SendFormDataToRenderer(_, _, _)).
663 WillOnce((DoAll(testing::SaveArg<0>(response_query_id),
664 testing::SaveArg<2>(response_data))));
665 FillAutofillFormData(input_query_id, input_form, input_field, unique_id);
668 int PackGUIDs(const GUIDPair& cc_guid, const GUIDPair& profile_guid) const {
669 return autofill_manager_->PackGUIDs(cc_guid, profile_guid);
673 base::MessageLoop message_loop_;
674 TestAutofillClient autofill_client_;
675 scoped_ptr<MockAutofillDriver> autofill_driver_;
676 scoped_ptr<TestAutofillManager> autofill_manager_;
677 scoped_ptr<TestAutofillExternalDelegate> external_delegate_;
678 TestPersonalDataManager personal_data_;
681 class TestFormStructure : public FormStructure {
683 explicit TestFormStructure(const FormData& form)
684 : FormStructure(form) {}
685 virtual ~TestFormStructure() {}
687 void SetFieldTypes(const std::vector<ServerFieldType>& heuristic_types,
688 const std::vector<ServerFieldType>& server_types) {
689 ASSERT_EQ(field_count(), heuristic_types.size());
690 ASSERT_EQ(field_count(), server_types.size());
692 for (size_t i = 0; i < field_count(); ++i) {
693 AutofillField* form_field = field(i);
694 ASSERT_TRUE(form_field);
695 form_field->set_heuristic_type(heuristic_types[i]);
696 form_field->set_server_type(server_types[i]);
699 UpdateAutofillCount();
703 DISALLOW_COPY_AND_ASSIGN(TestFormStructure);
706 // Test that we return all address profile suggestions when all form fields are
708 TEST_F(AutofillManagerTest, GetProfileSuggestionsEmptyValue) {
709 // Set up our form data.
711 test::CreateTestAddressFormData(&form);
712 std::vector<FormData> forms(1, form);
715 const FormFieldData& field = form.fields[0];
716 GetAutofillSuggestions(form, field);
718 // No suggestions provided, so send an empty vector as the results.
719 // This triggers the combined message send.
720 AutocompleteSuggestionsReturned(std::vector<base::string16>());
722 // Test that we sent the right values to the external delegate.
723 base::string16 expected_values[] = {
724 ASCIIToUTF16("Elvis"),
725 ASCIIToUTF16("Charles")
727 // Inferred labels include full first relevant field, which in this case is
728 // the address line 1.
729 base::string16 expected_labels[] = {
730 ASCIIToUTF16("3734 Elvis Presley Blvd."),
731 ASCIIToUTF16("123 Apple St.")
733 base::string16 expected_icons[] = {base::string16(), base::string16()};
734 int expected_unique_ids[] = {1, 2};
735 external_delegate_->CheckSuggestions(
736 kDefaultPageID, arraysize(expected_values), expected_values,
737 expected_labels, expected_icons, expected_unique_ids);
740 // Test that we return only matching address profile suggestions when the
741 // selected form field has been partially filled out.
742 TEST_F(AutofillManagerTest, GetProfileSuggestionsMatchCharacter) {
743 // Set up our form data.
745 test::CreateTestAddressFormData(&form);
746 std::vector<FormData> forms(1, form);
750 test::CreateTestFormField("First Name", "firstname", "E", "text",&field);
751 GetAutofillSuggestions(form, field);
753 // No suggestions provided, so send an empty vector as the results.
754 // This triggers the combined message send.
755 AutocompleteSuggestionsReturned(std::vector<base::string16>());
757 // Test that we sent the right values to the external delegate.
758 base::string16 expected_values[] = {ASCIIToUTF16("Elvis")};
759 base::string16 expected_labels[] = {ASCIIToUTF16("3734 Elvis Presley Blvd.")};
760 base::string16 expected_icons[] = {base::string16()};
761 int expected_unique_ids[] = {1};
762 external_delegate_->CheckSuggestions(
763 kDefaultPageID, arraysize(expected_values), expected_values,
764 expected_labels, expected_icons, expected_unique_ids);
767 // Test that we return no suggestions when the form has no relevant fields.
768 TEST_F(AutofillManagerTest, GetProfileSuggestionsUnknownFields) {
769 // Set up our form data.
771 form.name = ASCIIToUTF16("MyForm");
772 form.origin = GURL("http://myform.com/form.html");
773 form.action = GURL("http://myform.com/submit.html");
774 form.user_submitted = true;
777 test::CreateTestFormField("Username", "username", "", "text",&field);
778 form.fields.push_back(field);
779 test::CreateTestFormField("Password", "password", "", "password",&field);
780 form.fields.push_back(field);
781 test::CreateTestFormField("Quest", "quest", "", "quest", &field);
782 form.fields.push_back(field);
783 test::CreateTestFormField("Color", "color", "", "text", &field);
784 form.fields.push_back(field);
786 std::vector<FormData> forms(1, form);
789 GetAutofillSuggestions(form, field);
790 EXPECT_FALSE(external_delegate_->on_suggestions_returned_seen());
793 // Test that we cull duplicate profile suggestions.
794 TEST_F(AutofillManagerTest, GetProfileSuggestionsWithDuplicates) {
795 // Set up our form data.
797 test::CreateTestAddressFormData(&form);
798 std::vector<FormData> forms(1, form);
801 // Add a duplicate profile.
802 AutofillProfile* duplicate_profile =
804 *(autofill_manager_->GetProfileWithGUID(
805 "00000000-0000-0000-0000-000000000001")));
806 autofill_manager_->AddProfile(duplicate_profile);
808 const FormFieldData& field = form.fields[0];
809 GetAutofillSuggestions(form, field);
811 // No suggestions provided, so send an empty vector as the results.
812 // This triggers the combined message send.
813 AutocompleteSuggestionsReturned(std::vector<base::string16>());
815 // Test that we sent the right values to the external delegate.
816 base::string16 expected_values[] = {
817 ASCIIToUTF16("Elvis"),
818 ASCIIToUTF16("Charles")
820 base::string16 expected_labels[] = {
821 ASCIIToUTF16("3734 Elvis Presley Blvd."),
822 ASCIIToUTF16("123 Apple St.")
824 base::string16 expected_icons[] = {base::string16(), base::string16()};
825 int expected_unique_ids[] = {1, 2};
826 external_delegate_->CheckSuggestions(
827 kDefaultPageID, arraysize(expected_values), expected_values,
828 expected_labels, expected_icons, expected_unique_ids);
831 // Test that we return no suggestions when autofill is disabled.
832 TEST_F(AutofillManagerTest, GetProfileSuggestionsAutofillDisabledByUser) {
833 // Set up our form data.
835 test::CreateTestAddressFormData(&form);
836 std::vector<FormData> forms(1, form);
840 autofill_manager_->set_autofill_enabled(false);
842 const FormFieldData& field = form.fields[0];
843 GetAutofillSuggestions(form, field);
844 EXPECT_FALSE(external_delegate_->on_suggestions_returned_seen());
847 // Test that we return all credit card profile suggestions when all form fields
849 TEST_F(AutofillManagerTest, GetCreditCardSuggestionsEmptyValue) {
850 // Set up our form data.
852 CreateTestCreditCardFormData(&form, true, false);
853 std::vector<FormData> forms(1, form);
856 FormFieldData field = form.fields[1];
857 GetAutofillSuggestions(form, field);
859 // No suggestions provided, so send an empty vector as the results.
860 // This triggers the combined message send.
861 AutocompleteSuggestionsReturned(std::vector<base::string16>());
863 // Test that we sent the right values to the external delegate.
864 base::string16 expected_values[] = {
865 ASCIIToUTF16("************3456"),
866 ASCIIToUTF16("************8765")
868 base::string16 expected_labels[] = { ASCIIToUTF16("04/12"),
869 ASCIIToUTF16("10/14")};
870 base::string16 expected_icons[] = {
871 ASCIIToUTF16(kVisaCard),
872 ASCIIToUTF16(kMasterCard)
874 int expected_unique_ids[] = {
875 autofill_manager_->GetPackedCreditCardID(4),
876 autofill_manager_->GetPackedCreditCardID(5)
878 external_delegate_->CheckSuggestions(
879 kDefaultPageID, arraysize(expected_values), expected_values,
880 expected_labels, expected_icons, expected_unique_ids);
883 // Test that we return only matching credit card profile suggestions when the
884 // selected form field has been partially filled out.
885 TEST_F(AutofillManagerTest, GetCreditCardSuggestionsMatchCharacter) {
886 // Set up our form data.
888 CreateTestCreditCardFormData(&form, true, false);
889 std::vector<FormData> forms(1, form);
893 test::CreateTestFormField("Card Number", "cardnumber", "4", "text", &field);
894 GetAutofillSuggestions(form, field);
896 // No suggestions provided, so send an empty vector as the results.
897 // This triggers the combined message send.
898 AutocompleteSuggestionsReturned(std::vector<base::string16>());
900 // Test that we sent the right values to the external delegate.
901 base::string16 expected_values[] = {ASCIIToUTF16("************3456")};
902 base::string16 expected_labels[] = {ASCIIToUTF16("04/12")};
903 base::string16 expected_icons[] = {ASCIIToUTF16(kVisaCard)};
904 int expected_unique_ids[] = {autofill_manager_->GetPackedCreditCardID(4)};
905 external_delegate_->CheckSuggestions(
906 kDefaultPageID, arraysize(expected_values), expected_values,
907 expected_labels, expected_icons, expected_unique_ids);
910 // Test that we return credit card profile suggestions when the selected form
911 // field is not the credit card number field.
912 TEST_F(AutofillManagerTest, GetCreditCardSuggestionsNonCCNumber) {
913 // Set up our form data.
915 CreateTestCreditCardFormData(&form, true, false);
916 std::vector<FormData> forms(1, form);
919 const FormFieldData& field = form.fields[0];
920 GetAutofillSuggestions(form, field);
922 // No suggestions provided, so send an empty vector as the results.
923 // This triggers the combined message send.
924 AutocompleteSuggestionsReturned(std::vector<base::string16>());
926 // Test that we sent the right values to the external delegate.
927 base::string16 expected_values[] = {
928 ASCIIToUTF16("Elvis Presley"),
929 ASCIIToUTF16("Buddy Holly")
931 base::string16 expected_labels[] = { ASCIIToUTF16("*3456"),
932 ASCIIToUTF16("*8765") };
933 base::string16 expected_icons[] = {
934 ASCIIToUTF16(kVisaCard),
935 ASCIIToUTF16(kMasterCard)
937 int expected_unique_ids[] = {
938 autofill_manager_->GetPackedCreditCardID(4),
939 autofill_manager_->GetPackedCreditCardID(5)
941 external_delegate_->CheckSuggestions(
942 kDefaultPageID, arraysize(expected_values), expected_values,
943 expected_labels, expected_icons, expected_unique_ids);
946 // Test that we return a warning explaining that credit card profile suggestions
947 // are unavailable when the form is not https.
948 TEST_F(AutofillManagerTest, GetCreditCardSuggestionsNonHTTPS) {
949 // Set up our form data.
951 CreateTestCreditCardFormData(&form, false, false);
952 std::vector<FormData> forms(1, form);
955 const FormFieldData& field = form.fields[0];
956 GetAutofillSuggestions(form, field);
958 // No suggestions provided, so send an empty vector as the results.
959 // This triggers the combined message send.
960 AutocompleteSuggestionsReturned(std::vector<base::string16>());
962 // Test that we sent the right values to the external delegate.
963 base::string16 expected_values[] = {
964 l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_INSECURE_CONNECTION)
966 base::string16 expected_labels[] = {base::string16()};
967 base::string16 expected_icons[] = {base::string16()};
968 int expected_unique_ids[] = {-1};
969 external_delegate_->CheckSuggestions(
970 kDefaultPageID, arraysize(expected_values), expected_values,
971 expected_labels, expected_icons, expected_unique_ids);
973 // Now add some Autocomplete suggestions. We should show the autocomplete
974 // suggestions and the warning.
975 const int kPageID2 = 2;
976 GetAutofillSuggestions(kPageID2, form, field);
978 std::vector<base::string16> suggestions;
979 suggestions.push_back(ASCIIToUTF16("Jay"));
980 suggestions.push_back(ASCIIToUTF16("Jason"));
981 AutocompleteSuggestionsReturned(suggestions);
983 base::string16 expected_values2[] = {
984 l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_INSECURE_CONNECTION),
986 ASCIIToUTF16("Jason")
988 base::string16 expected_labels2[] = { base::string16(), base::string16(),
990 base::string16 expected_icons2[] = { base::string16(), base::string16(),
992 int expected_unique_ids2[] = {-1, 0, 0};
993 external_delegate_->CheckSuggestions(
994 kPageID2, arraysize(expected_values2), expected_values2,
995 expected_labels2, expected_icons2, expected_unique_ids2);
997 // Clear the test credit cards and try again -- we shouldn't return a warning.
998 personal_data_.ClearCreditCards();
999 GetAutofillSuggestions(form, field);
1000 EXPECT_FALSE(external_delegate_->on_suggestions_returned_seen());
1003 // Test that we return all credit card suggestions in the case that two cards
1004 // have the same obfuscated number.
1005 TEST_F(AutofillManagerTest, GetCreditCardSuggestionsRepeatedObfuscatedNumber) {
1006 // Add a credit card with the same obfuscated number as Elvis's.
1007 // |credit_card| will be owned by the mock PersonalDataManager.
1008 CreditCard* credit_card = new CreditCard;
1009 test::SetCreditCardInfo(credit_card, "Elvis Presley",
1010 "5231567890123456", // Mastercard
1012 credit_card->set_guid("00000000-0000-0000-0000-000000000007");
1013 autofill_manager_->AddCreditCard(credit_card);
1015 // Set up our form data.
1017 CreateTestCreditCardFormData(&form, true, false);
1018 std::vector<FormData> forms(1, form);
1021 FormFieldData field = form.fields[1];
1022 GetAutofillSuggestions(form, field);
1024 // No suggestions provided, so send an empty vector as the results.
1025 // This triggers the combined message send.
1026 AutocompleteSuggestionsReturned(std::vector<base::string16>());
1028 // Test that we sent the right values to the external delegate.
1029 base::string16 expected_values[] = {
1030 ASCIIToUTF16("************3456"),
1031 ASCIIToUTF16("************8765"),
1032 ASCIIToUTF16("************3456")
1034 base::string16 expected_labels[] = {
1035 ASCIIToUTF16("04/12"),
1036 ASCIIToUTF16("10/14"),
1037 ASCIIToUTF16("05/12"),
1039 base::string16 expected_icons[] = {
1040 ASCIIToUTF16(kVisaCard),
1041 ASCIIToUTF16(kMasterCard),
1042 ASCIIToUTF16(kMasterCard)
1044 int expected_unique_ids[] = {
1045 autofill_manager_->GetPackedCreditCardID(4),
1046 autofill_manager_->GetPackedCreditCardID(5),
1047 autofill_manager_->GetPackedCreditCardID(7)
1049 external_delegate_->CheckSuggestions(
1050 kDefaultPageID, arraysize(expected_values), expected_values,
1051 expected_labels, expected_icons, expected_unique_ids);
1054 // Test that we return profile and credit card suggestions for combined forms.
1055 TEST_F(AutofillManagerTest, GetAddressAndCreditCardSuggestions) {
1056 // Set up our form data.
1058 test::CreateTestAddressFormData(&form);
1059 CreateTestCreditCardFormData(&form, true, false);
1060 std::vector<FormData> forms(1, form);
1063 FormFieldData field = form.fields[0];
1064 GetAutofillSuggestions(form, field);
1066 // No suggestions provided, so send an empty vector as the results.
1067 // This triggers the combined message send.
1068 AutocompleteSuggestionsReturned(std::vector<base::string16>());
1070 // Test that we sent the right address suggestions to the external delegate.
1071 base::string16 expected_values[] = {
1072 ASCIIToUTF16("Elvis"),
1073 ASCIIToUTF16("Charles")
1075 base::string16 expected_labels[] = {
1076 ASCIIToUTF16("3734 Elvis Presley Blvd."),
1077 ASCIIToUTF16("123 Apple St.")
1079 base::string16 expected_icons[] = {base::string16(), base::string16()};
1080 int expected_unique_ids[] = {1, 2};
1081 external_delegate_->CheckSuggestions(
1082 kDefaultPageID, arraysize(expected_values), expected_values,
1083 expected_labels, expected_icons, expected_unique_ids);
1085 const int kPageID2 = 2;
1086 test::CreateTestFormField("Card Number", "cardnumber", "", "text", &field);
1087 GetAutofillSuggestions(kPageID2, form, field);
1089 // No suggestions provided, so send an empty vector as the results.
1090 // This triggers the combined message send.
1091 AutocompleteSuggestionsReturned(std::vector<base::string16>());
1093 // Test that we sent the credit card suggestions to the external delegate.
1094 base::string16 expected_values2[] = {
1095 ASCIIToUTF16("************3456"),
1096 ASCIIToUTF16("************8765")
1098 base::string16 expected_labels2[] = { ASCIIToUTF16("04/12"),
1099 ASCIIToUTF16("10/14")};
1100 base::string16 expected_icons2[] = {
1101 ASCIIToUTF16(kVisaCard),
1102 ASCIIToUTF16(kMasterCard)
1104 int expected_unique_ids2[] = {
1105 autofill_manager_->GetPackedCreditCardID(4),
1106 autofill_manager_->GetPackedCreditCardID(5)
1108 external_delegate_->CheckSuggestions(
1109 kPageID2, arraysize(expected_values2), expected_values2,
1110 expected_labels2, expected_icons2, expected_unique_ids2);
1113 // Test that for non-https forms with both address and credit card fields, we
1114 // only return address suggestions. Instead of credit card suggestions, we
1115 // should return a warning explaining that credit card profile suggestions are
1116 // unavailable when the form is not https.
1117 TEST_F(AutofillManagerTest, GetAddressAndCreditCardSuggestionsNonHttps) {
1118 // Set up our form data.
1120 test::CreateTestAddressFormData(&form);
1121 CreateTestCreditCardFormData(&form, false, false);
1122 std::vector<FormData> forms(1, form);
1125 FormFieldData field = form.fields[0];
1126 GetAutofillSuggestions(form, field);
1128 // No suggestions provided, so send an empty vector as the results.
1129 // This triggers the combined message send.
1130 AutocompleteSuggestionsReturned(std::vector<base::string16>());
1132 // Test that we sent the right suggestions to the external delegate.
1133 base::string16 expected_values[] = {
1134 ASCIIToUTF16("Elvis"),
1135 ASCIIToUTF16("Charles")
1137 base::string16 expected_labels[] = {
1138 ASCIIToUTF16("3734 Elvis Presley Blvd."),
1139 ASCIIToUTF16("123 Apple St.")
1141 base::string16 expected_icons[] = {base::string16(), base::string16()};
1142 int expected_unique_ids[] = {1, 2};
1143 external_delegate_->CheckSuggestions(
1144 kDefaultPageID, arraysize(expected_values), expected_values,
1145 expected_labels, expected_icons, expected_unique_ids);
1147 test::CreateTestFormField("Card Number", "cardnumber", "", "text", &field);
1148 const int kPageID2 = 2;
1149 GetAutofillSuggestions(kPageID2, form, field);
1151 // No suggestions provided, so send an empty vector as the results.
1152 // This triggers the combined message send.
1153 AutocompleteSuggestionsReturned(std::vector<base::string16>());
1155 // Test that we sent the right values to the external delegate.
1156 base::string16 expected_values2[] = {
1157 l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_INSECURE_CONNECTION)
1159 base::string16 expected_labels2[] = {base::string16()};
1160 base::string16 expected_icons2[] = {base::string16()};
1161 int expected_unique_ids2[] = {-1};
1162 external_delegate_->CheckSuggestions(
1163 kPageID2, arraysize(expected_values2), expected_values2,
1164 expected_labels2, expected_icons2, expected_unique_ids2);
1166 // Clear the test credit cards and try again -- we shouldn't return a warning.
1167 personal_data_.ClearCreditCards();
1168 GetAutofillSuggestions(form, field);
1169 EXPECT_FALSE(external_delegate_->on_suggestions_returned_seen());
1172 // Test that we correctly combine autofill and autocomplete suggestions.
1173 TEST_F(AutofillManagerTest, GetCombinedAutofillAndAutocompleteSuggestions) {
1174 // Set up our form data.
1176 test::CreateTestAddressFormData(&form);
1177 std::vector<FormData> forms(1, form);
1180 const FormFieldData& field = form.fields[0];
1181 GetAutofillSuggestions(form, field);
1183 // Add some Autocomplete suggestions.
1184 // This triggers the combined message send.
1185 std::vector<base::string16> suggestions;
1186 suggestions.push_back(ASCIIToUTF16("Jay"));
1187 // This suggestion is a duplicate, and should be trimmed.
1188 suggestions.push_back(ASCIIToUTF16("Elvis"));
1189 suggestions.push_back(ASCIIToUTF16("Jason"));
1190 AutocompleteSuggestionsReturned(suggestions);
1192 // Test that we sent the right values to the external delegate.
1193 base::string16 expected_values[] = {
1194 ASCIIToUTF16("Elvis"),
1195 ASCIIToUTF16("Charles"),
1196 ASCIIToUTF16("Jay"),
1197 ASCIIToUTF16("Jason")
1199 base::string16 expected_labels[] = {
1200 ASCIIToUTF16("3734 Elvis Presley Blvd."),
1201 ASCIIToUTF16("123 Apple St."),
1205 base::string16 expected_icons[] = { base::string16(), base::string16(),
1206 base::string16(), base::string16()};
1207 int expected_unique_ids[] = {1, 2, 0, 0};
1208 external_delegate_->CheckSuggestions(
1209 kDefaultPageID, arraysize(expected_values), expected_values,
1210 expected_labels, expected_icons, expected_unique_ids);
1213 // Test that we return autocomplete-like suggestions when trying to autofill
1214 // already filled forms.
1215 TEST_F(AutofillManagerTest, GetFieldSuggestionsWhenFormIsAutofilled) {
1216 // Set up our form data.
1218 test::CreateTestAddressFormData(&form);
1219 std::vector<FormData> forms(1, form);
1222 // Mark one of the fields as filled.
1223 form.fields[2].is_autofilled = true;
1224 const FormFieldData& field = form.fields[0];
1225 GetAutofillSuggestions(form, field);
1227 // No suggestions provided, so send an empty vector as the results.
1228 // This triggers the combined message send.
1229 AutocompleteSuggestionsReturned(std::vector<base::string16>());
1231 // Test that we sent the right values to the external delegate.
1232 base::string16 expected_values[] = {
1233 ASCIIToUTF16("Elvis"),
1234 ASCIIToUTF16("Charles")
1236 base::string16 expected_labels[] = {base::string16(), base::string16()};
1237 base::string16 expected_icons[] = {base::string16(), base::string16()};
1238 int expected_unique_ids[] = {1, 2};
1239 external_delegate_->CheckSuggestions(
1240 kDefaultPageID, arraysize(expected_values), expected_values,
1241 expected_labels, expected_icons, expected_unique_ids);
1244 // Test that nothing breaks when there are autocomplete suggestions but no
1245 // autofill suggestions.
1246 TEST_F(AutofillManagerTest, GetFieldSuggestionsForAutocompleteOnly) {
1247 // Set up our form data.
1249 test::CreateTestAddressFormData(&form);
1250 FormFieldData field;
1251 test::CreateTestFormField("Some Field", "somefield", "", "text", &field);
1252 form.fields.push_back(field);
1253 std::vector<FormData> forms(1, form);
1256 GetAutofillSuggestions(form, field);
1258 // Add some Autocomplete suggestions.
1259 // This triggers the combined message send.
1260 std::vector<base::string16> suggestions;
1261 suggestions.push_back(ASCIIToUTF16("one"));
1262 suggestions.push_back(ASCIIToUTF16("two"));
1263 AutocompleteSuggestionsReturned(suggestions);
1265 // Test that we sent the right values to the external delegate.
1266 base::string16 expected_values[] = {
1267 ASCIIToUTF16("one"),
1270 base::string16 expected_labels[] = {base::string16(), base::string16()};
1271 base::string16 expected_icons[] = {base::string16(), base::string16()};
1272 int expected_unique_ids[] = {0, 0};
1273 external_delegate_->CheckSuggestions(
1274 kDefaultPageID, arraysize(expected_values), expected_values,
1275 expected_labels, expected_icons, expected_unique_ids);
1278 // Test that we do not return duplicate values drawn from multiple profiles when
1279 // filling an already filled field.
1280 TEST_F(AutofillManagerTest, GetFieldSuggestionsWithDuplicateValues) {
1281 // Set up our form data.
1283 test::CreateTestAddressFormData(&form);
1284 std::vector<FormData> forms(1, form);
1287 // |profile| will be owned by the mock PersonalDataManager.
1288 AutofillProfile* profile = new AutofillProfile;
1289 test::SetProfileInfo(
1290 profile, "Elvis", "", "", "", "", "", "", "", "", "", "", "");
1291 profile->set_guid("00000000-0000-0000-0000-000000000101");
1292 autofill_manager_->AddProfile(profile);
1294 FormFieldData& field = form.fields[0];
1295 field.is_autofilled = true;
1296 field.value = ASCIIToUTF16("Elvis");
1297 GetAutofillSuggestions(form, field);
1299 // No suggestions provided, so send an empty vector as the results.
1300 // This triggers the combined message send.
1301 AutocompleteSuggestionsReturned(std::vector<base::string16>());
1303 // Test that we sent the right values to the external delegate.
1304 base::string16 expected_values[] = { ASCIIToUTF16("Elvis") };
1305 base::string16 expected_labels[] = { base::string16() };
1306 base::string16 expected_icons[] = { base::string16() };
1307 int expected_unique_ids[] = { 1 };
1308 external_delegate_->CheckSuggestions(
1309 kDefaultPageID, arraysize(expected_values), expected_values,
1310 expected_labels, expected_icons, expected_unique_ids);
1313 // Test that a non-default value is suggested for multi-valued profile, on an
1315 TEST_F(AutofillManagerTest, GetFieldSuggestionsForMultiValuedProfileUnfilled) {
1316 // Set up our form data.
1318 test::CreateTestAddressFormData(&form);
1319 std::vector<FormData> forms(1, form);
1322 // |profile| will be owned by the mock PersonalDataManager.
1323 AutofillProfile* profile = new AutofillProfile;
1324 test::SetProfileInfo(profile, "Elvis", "", "Presley", "me@x.com", "",
1325 "", "", "", "", "", "", "");
1326 profile->set_guid("00000000-0000-0000-0000-000000000101");
1327 std::vector<base::string16> multi_values(2);
1328 multi_values[0] = ASCIIToUTF16("Elvis");
1329 multi_values[1] = ASCIIToUTF16("Elena");
1330 profile->SetRawMultiInfo(NAME_FIRST, multi_values);
1331 multi_values[0] = ASCIIToUTF16("Presley");
1332 multi_values[1] = ASCIIToUTF16("Love");
1333 profile->SetRawMultiInfo(NAME_LAST, multi_values);
1334 personal_data_.ClearAutofillProfiles();
1335 autofill_manager_->AddProfile(profile);
1338 // Get the first name field.
1339 // Start out with "E", hoping for either "Elvis" or "Elena.
1340 FormFieldData& field = form.fields[0];
1341 field.value = ASCIIToUTF16("E");
1342 field.is_autofilled = false;
1343 GetAutofillSuggestions(form, field);
1345 // Trigger the |Send|.
1346 AutocompleteSuggestionsReturned(std::vector<base::string16>());
1348 // Test that we sent the right values to the external delegate.
1349 base::string16 expected_values[] = {
1350 ASCIIToUTF16("Elvis"),
1351 ASCIIToUTF16("Elena")
1353 base::string16 expected_labels[] = {
1354 ASCIIToUTF16("me@x.com"),
1355 ASCIIToUTF16("me@x.com")
1357 base::string16 expected_icons[] = { base::string16(), base::string16() };
1358 int expected_unique_ids[] = { 1, 2 };
1359 external_delegate_->CheckSuggestions(
1360 kDefaultPageID, arraysize(expected_values), expected_values,
1361 expected_labels, expected_icons, expected_unique_ids);
1365 // Get the first name field.
1366 // This time, start out with "Ele", hoping for "Elena".
1367 FormFieldData& field = form.fields[0];
1368 field.value = ASCIIToUTF16("Ele");
1369 field.is_autofilled = false;
1370 GetAutofillSuggestions(form, field);
1372 // Trigger the |Send|.
1373 AutocompleteSuggestionsReturned(std::vector<base::string16>());
1375 // Test that we sent the right values to the external delegate.
1376 base::string16 expected_values[] = { ASCIIToUTF16("Elena") };
1377 base::string16 expected_labels[] = { ASCIIToUTF16("me@x.com") };
1378 base::string16 expected_icons[] = { base::string16() };
1379 int expected_unique_ids[] = { 2 };
1380 external_delegate_->CheckSuggestions(
1381 kDefaultPageID, arraysize(expected_values), expected_values,
1382 expected_labels, expected_icons, expected_unique_ids);
1386 // Test that all values are suggested for multi-valued profile, on a filled
1387 // form. This is the per-field "override" case.
1388 TEST_F(AutofillManagerTest, GetFieldSuggestionsForMultiValuedProfileFilled) {
1389 // Set up our form data.
1391 test::CreateTestAddressFormData(&form);
1392 std::vector<FormData> forms(1, form);
1395 // |profile| will be owned by the mock PersonalDataManager.
1396 AutofillProfile* profile = new AutofillProfile;
1397 profile->set_guid("00000000-0000-0000-0000-000000000102");
1398 std::vector<base::string16> multi_values(3);
1399 multi_values[0] = ASCIIToUTF16("Travis");
1400 multi_values[1] = ASCIIToUTF16("Cynthia");
1401 multi_values[2] = ASCIIToUTF16("Zac");
1402 profile->SetRawMultiInfo(NAME_FIRST, multi_values);
1403 multi_values[0] = ASCIIToUTF16("Smith");
1404 multi_values[1] = ASCIIToUTF16("Love");
1405 multi_values[2] = ASCIIToUTF16("Mango");
1406 profile->SetRawMultiInfo(NAME_LAST, multi_values);
1407 autofill_manager_->AddProfile(profile);
1409 // Get the first name field. And start out with "Travis", hoping for all the
1410 // multi-valued variants as suggestions.
1411 FormFieldData& field = form.fields[0];
1412 field.value = ASCIIToUTF16("Travis");
1413 field.is_autofilled = true;
1414 GetAutofillSuggestions(form, field);
1416 // Trigger the |Send|.
1417 AutocompleteSuggestionsReturned(std::vector<base::string16>());
1419 // Test that we sent the right values to the external delegate.
1420 base::string16 expected_values[] = {
1421 ASCIIToUTF16("Travis"),
1422 ASCIIToUTF16("Cynthia"),
1425 base::string16 expected_labels[] = { base::string16(), base::string16(),
1427 base::string16 expected_icons[] = { base::string16(), base::string16(),
1429 int expected_unique_ids[] = { 1, 2, 3 };
1430 external_delegate_->CheckSuggestions(
1431 kDefaultPageID, arraysize(expected_values), expected_values,
1432 expected_labels, expected_icons, expected_unique_ids);
1435 TEST_F(AutofillManagerTest, GetProfileSuggestionsFancyPhone) {
1436 // Set up our form data.
1438 test::CreateTestAddressFormData(&form);
1439 std::vector<FormData> forms(1, form);
1442 AutofillProfile* profile = new AutofillProfile;
1443 profile->set_guid("00000000-0000-0000-0000-000000000103");
1444 profile->SetInfo(AutofillType(NAME_FULL), ASCIIToUTF16("Natty Bumppo"),
1446 profile->SetRawInfo(PHONE_HOME_WHOLE_NUMBER,
1447 ASCIIToUTF16("1800PRAIRIE"));
1448 autofill_manager_->AddProfile(profile);
1450 const FormFieldData& field = form.fields[9];
1451 GetAutofillSuggestions(form, field);
1453 // No suggestions provided, so send an empty vector as the results.
1454 // This triggers the combined message send.
1455 AutocompleteSuggestionsReturned(std::vector<base::string16>());
1457 // Test that we sent the right values to the external delegate.
1458 base::string16 expected_values[] = {
1459 ASCIIToUTF16("12345678901"),
1460 ASCIIToUTF16("23456789012"),
1461 ASCIIToUTF16("18007724743"), // 1800PRAIRIE
1463 // Inferred labels include full first relevant field, which in this case is
1464 // the address line 1.
1465 base::string16 expected_labels[] = {
1466 ASCIIToUTF16("Elvis Aaron Presley"),
1467 ASCIIToUTF16("Charles Hardin Holley"),
1468 ASCIIToUTF16("Natty Bumppo"),
1470 base::string16 expected_icons[] = { base::string16(), base::string16(),
1472 int expected_unique_ids[] = {1, 2, 3};
1473 external_delegate_->CheckSuggestions(
1474 kDefaultPageID, arraysize(expected_values), expected_values,
1475 expected_labels, expected_icons, expected_unique_ids);
1478 // Test that we correctly fill an address form.
1479 TEST_F(AutofillManagerTest, FillAddressForm) {
1480 // Set up our form data.
1482 test::CreateTestAddressFormData(&form);
1483 std::vector<FormData> forms(1, form);
1486 GUIDPair guid("00000000-0000-0000-0000-000000000001", 0);
1487 GUIDPair empty(std::string(), 0);
1488 int response_page_id = 0;
1489 FormData response_data;
1490 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0],
1491 PackGUIDs(empty, guid), &response_page_id, &response_data);
1492 ExpectFilledAddressFormElvis(
1493 response_page_id, response_data, kDefaultPageID, false);
1496 // Test that we correctly fill an address form from an auxiliary profile.
1497 TEST_F(AutofillManagerTest, FillAddressFormFromAuxiliaryProfile) {
1498 personal_data_.ClearAutofillProfiles();
1499 #if defined(OS_MACOSX) && !defined(OS_IOS)
1500 autofill_client_.GetPrefs()->SetBoolean(
1501 ::autofill::prefs::kAutofillUseMacAddressBook, true);
1503 autofill_client_.GetPrefs()->SetBoolean(
1504 ::autofill::prefs::kAutofillAuxiliaryProfilesEnabled, true);
1505 #endif // defined(OS_MACOSX) && !defined(OS_IOS)
1507 personal_data_.CreateTestAuxiliaryProfiles();
1509 // Set up our form data.
1511 test::CreateTestAddressFormData(&form);
1512 std::vector<FormData> forms(1, form);
1515 GUIDPair guid("00000000-0000-0000-0000-000000000001", 0);
1516 GUIDPair empty(std::string(), 0);
1517 int response_page_id = 0;
1518 FormData response_data;
1519 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0],
1520 PackGUIDs(empty, guid), &response_page_id, &response_data);
1521 ExpectFilledAddressFormElvis(
1522 response_page_id, response_data, kDefaultPageID, false);
1525 // Test that we correctly fill a credit card form.
1526 TEST_F(AutofillManagerTest, FillCreditCardForm) {
1527 // Set up our form data.
1529 CreateTestCreditCardFormData(&form, true, false);
1530 std::vector<FormData> forms(1, form);
1533 GUIDPair guid("00000000-0000-0000-0000-000000000004", 0);
1534 GUIDPair empty(std::string(), 0);
1535 int response_page_id = 0;
1536 FormData response_data;
1537 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, *form.fields.begin(),
1538 PackGUIDs(guid, empty), &response_page_id, &response_data);
1539 ExpectFilledCreditCardFormElvis(
1540 response_page_id, response_data, kDefaultPageID, false);
1543 // Test that we correctly fill a credit card form with month input type.
1544 // 1. year empty, month empty
1545 TEST_F(AutofillManagerTest, FillCreditCardFormNoYearNoMonth) {
1546 // Same as the SetUp(), but generate 4 credit cards with year month
1548 personal_data_.CreateTestCreditCardsYearAndMonth("", "");
1549 // Set up our form data.
1551 CreateTestCreditCardFormData(&form, true, true);
1552 std::vector<FormData> forms(1, form);
1555 GUIDPair guid("00000000-0000-0000-0000-000000000007", 0);
1556 GUIDPair empty(std::string(), 0);
1557 int response_page_id = 0;
1558 FormData response_data;
1559 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, *form.fields.begin(),
1560 PackGUIDs(guid, empty), &response_page_id, &response_data);
1561 ExpectFilledCreditCardYearMonthWithYearMonth(response_page_id, response_data,
1562 kDefaultPageID, false, "", "");
1566 // Test that we correctly fill a credit card form with month input type.
1567 // 2. year empty, month non-empty
1568 TEST_F(AutofillManagerTest, FillCreditCardFormNoYearMonth) {
1569 // Same as the SetUp(), but generate 4 credit cards with year month
1571 personal_data_.CreateTestCreditCardsYearAndMonth("", "04");
1572 // Set up our form data.
1574 CreateTestCreditCardFormData(&form, true, true);
1575 std::vector<FormData> forms(1, form);
1578 GUIDPair guid("00000000-0000-0000-0000-000000000007", 0);
1579 GUIDPair empty(std::string(), 0);
1580 int response_page_id = 0;
1581 FormData response_data;
1582 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, *form.fields.begin(),
1583 PackGUIDs(guid, empty), &response_page_id, &response_data);
1584 ExpectFilledCreditCardYearMonthWithYearMonth(response_page_id, response_data,
1585 kDefaultPageID, false, "", "04");
1588 // Test that we correctly fill a credit card form with month input type.
1589 // 3. year non-empty, month empty
1590 TEST_F(AutofillManagerTest, FillCreditCardFormYearNoMonth) {
1591 // Same as the SetUp(), but generate 4 credit cards with year month
1593 personal_data_.CreateTestCreditCardsYearAndMonth("2012", "");
1594 // Set up our form data.
1596 CreateTestCreditCardFormData(&form, true, true);
1597 std::vector<FormData> forms(1, form);
1600 GUIDPair guid("00000000-0000-0000-0000-000000000007", 0);
1601 GUIDPair empty(std::string(), 0);
1602 int response_page_id = 0;
1603 FormData response_data;
1604 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, *form.fields.begin(),
1605 PackGUIDs(guid, empty), &response_page_id, &response_data);
1606 ExpectFilledCreditCardYearMonthWithYearMonth(response_page_id, response_data,
1607 kDefaultPageID, false, "2012", "");
1610 // Test that we correctly fill a credit card form with month input type.
1611 // 4. year non-empty, month empty
1612 TEST_F(AutofillManagerTest, FillCreditCardFormYearMonth) {
1613 // Same as the SetUp(), but generate 4 credit cards with year month
1615 personal_data_.ClearCreditCards();
1616 personal_data_.CreateTestCreditCardsYearAndMonth("2012", "04");
1617 // Set up our form data.
1619 CreateTestCreditCardFormData(&form, true, true);
1620 std::vector<FormData> forms(1, form);
1623 GUIDPair guid("00000000-0000-0000-0000-000000000007", 0);
1624 GUIDPair empty(std::string(), 0);
1625 int response_page_id = 0;
1626 FormData response_data;
1627 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, *form.fields.begin(),
1628 PackGUIDs(guid, empty), &response_page_id, &response_data);
1629 ExpectFilledCreditCardYearMonthWithYearMonth(response_page_id, response_data,
1630 kDefaultPageID, false, "2012", "04");
1633 // Test that we correctly fill a combined address and credit card form.
1634 TEST_F(AutofillManagerTest, FillAddressAndCreditCardForm) {
1635 // Set up our form data.
1637 test::CreateTestAddressFormData(&form);
1638 CreateTestCreditCardFormData(&form, true, false);
1639 std::vector<FormData> forms(1, form);
1642 // First fill the address data.
1643 GUIDPair guid("00000000-0000-0000-0000-000000000001", 0);
1644 GUIDPair empty(std::string(), 0);
1645 int response_page_id = 0;
1646 FormData response_data;
1648 SCOPED_TRACE("Address");
1649 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0],
1650 PackGUIDs(empty, guid), &response_page_id, &response_data);
1651 ExpectFilledAddressFormElvis(
1652 response_page_id, response_data, kDefaultPageID, true);
1655 // Now fill the credit card data.
1656 const int kPageID2 = 2;
1657 GUIDPair guid2("00000000-0000-0000-0000-000000000004", 0);
1658 response_page_id = 0;
1660 FillAutofillFormDataAndSaveResults(kPageID2, form, form.fields.back(),
1661 PackGUIDs(guid2, empty), &response_page_id, &response_data);
1662 SCOPED_TRACE("Credit card");
1663 ExpectFilledCreditCardFormElvis(
1664 response_page_id, response_data, kPageID2, true);
1668 // Test that we correctly fill a form that has multiple logical sections, e.g.
1669 // both a billing and a shipping address.
1670 TEST_F(AutofillManagerTest, FillFormWithMultipleSections) {
1671 // Set up our form data.
1673 test::CreateTestAddressFormData(&form);
1674 const size_t kAddressFormSize = form.fields.size();
1675 test::CreateTestAddressFormData(&form);
1676 for (size_t i = kAddressFormSize; i < form.fields.size(); ++i) {
1677 // Make sure the fields have distinct names.
1678 form.fields[i].name = form.fields[i].name + ASCIIToUTF16("_");
1680 std::vector<FormData> forms(1, form);
1683 // Fill the first section.
1684 GUIDPair guid("00000000-0000-0000-0000-000000000001", 0);
1685 GUIDPair empty(std::string(), 0);
1686 int response_page_id = 0;
1687 FormData response_data;
1688 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0],
1689 PackGUIDs(empty, guid), &response_page_id, &response_data);
1691 SCOPED_TRACE("Address 1");
1692 // The second address section should be empty.
1693 ASSERT_EQ(response_data.fields.size(), 2*kAddressFormSize);
1694 for (size_t i = kAddressFormSize; i < form.fields.size(); ++i) {
1695 EXPECT_EQ(base::string16(), response_data.fields[i].value);
1698 // The first address section should be filled with Elvis's data.
1699 response_data.fields.resize(kAddressFormSize);
1700 ExpectFilledAddressFormElvis(
1701 response_page_id, response_data, kDefaultPageID, false);
1704 // Fill the second section, with the initiating field somewhere in the middle
1706 const int kPageID2 = 2;
1707 GUIDPair guid2("00000000-0000-0000-0000-000000000001", 0);
1708 ASSERT_LT(9U, kAddressFormSize);
1709 response_page_id = 0;
1710 FillAutofillFormDataAndSaveResults(
1711 kPageID2, form, form.fields[kAddressFormSize + 9],
1712 PackGUIDs(empty, guid2), &response_page_id, &response_data);
1714 SCOPED_TRACE("Address 2");
1715 ASSERT_EQ(response_data.fields.size(), form.fields.size());
1717 // The first address section should be empty.
1718 ASSERT_EQ(response_data.fields.size(), 2*kAddressFormSize);
1719 for (size_t i = 0; i < kAddressFormSize; ++i) {
1720 EXPECT_EQ(base::string16(), response_data.fields[i].value);
1723 // The second address section should be filled with Elvis's data.
1724 FormData secondSection = response_data;
1725 secondSection.fields.erase(secondSection.fields.begin(),
1726 secondSection.fields.begin() + kAddressFormSize);
1727 for (size_t i = 0; i < kAddressFormSize; ++i) {
1728 // Restore the expected field names.
1729 base::string16 name = secondSection.fields[i].name;
1730 base::string16 original_name = name.substr(0, name.size() - 1);
1731 secondSection.fields[i].name = original_name;
1733 ExpectFilledAddressFormElvis(
1734 response_page_id, secondSection, kPageID2, false);
1738 // Test that we correctly fill a form that has author-specified sections, which
1739 // might not match our expected section breakdown.
1740 TEST_F(AutofillManagerTest, FillFormWithAuthorSpecifiedSections) {
1741 // Create a form with a billing section and an unnamed section, interleaved.
1742 // The billing section includes both address and credit card fields.
1744 form.name = ASCIIToUTF16("MyForm");
1745 form.origin = GURL("https://myform.com/form.html");
1746 form.action = GURL("https://myform.com/submit.html");
1747 form.user_submitted = true;
1749 FormFieldData field;
1751 test::CreateTestFormField("", "country", "", "text", &field);
1752 field.autocomplete_attribute = "section-billing country";
1753 form.fields.push_back(field);
1755 test::CreateTestFormField("", "firstname", "", "text", &field);
1756 field.autocomplete_attribute = "given-name";
1757 form.fields.push_back(field);
1759 test::CreateTestFormField("", "lastname", "", "text", &field);
1760 field.autocomplete_attribute = "family-name";
1761 form.fields.push_back(field);
1763 test::CreateTestFormField("", "address", "", "text", &field);
1764 field.autocomplete_attribute = "section-billing address-line1";
1765 form.fields.push_back(field);
1767 test::CreateTestFormField("", "city", "", "text", &field);
1768 field.autocomplete_attribute = "section-billing locality";
1769 form.fields.push_back(field);
1771 test::CreateTestFormField("", "state", "", "text", &field);
1772 field.autocomplete_attribute = "section-billing region";
1773 form.fields.push_back(field);
1775 test::CreateTestFormField("", "zip", "", "text", &field);
1776 field.autocomplete_attribute = "section-billing postal-code";
1777 form.fields.push_back(field);
1779 test::CreateTestFormField("", "ccname", "", "text", &field);
1780 field.autocomplete_attribute = "section-billing cc-name";
1781 form.fields.push_back(field);
1783 test::CreateTestFormField("", "ccnumber", "", "text", &field);
1784 field.autocomplete_attribute = "section-billing cc-number";
1785 form.fields.push_back(field);
1787 test::CreateTestFormField("", "ccexp", "", "text", &field);
1788 field.autocomplete_attribute = "section-billing cc-exp";
1789 form.fields.push_back(field);
1791 test::CreateTestFormField("", "email", "", "text", &field);
1792 field.autocomplete_attribute = "email";
1793 form.fields.push_back(field);
1795 std::vector<FormData> forms(1, form);
1798 // Fill the unnamed section.
1799 GUIDPair guid("00000000-0000-0000-0000-000000000001", 0);
1800 GUIDPair empty(std::string(), 0);
1801 int response_page_id = 0;
1802 FormData response_data;
1803 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[1],
1804 PackGUIDs(empty, guid), &response_page_id, &response_data);
1806 SCOPED_TRACE("Unnamed section");
1807 EXPECT_EQ(kDefaultPageID, response_page_id);
1808 EXPECT_EQ(ASCIIToUTF16("MyForm"), response_data.name);
1809 EXPECT_EQ(GURL("https://myform.com/form.html"), response_data.origin);
1810 EXPECT_EQ(GURL("https://myform.com/submit.html"), response_data.action);
1811 EXPECT_TRUE(response_data.user_submitted);
1812 ASSERT_EQ(11U, response_data.fields.size());
1814 ExpectFilledField("", "country", "", "text", response_data.fields[0]);
1815 ExpectFilledField("", "firstname", "Elvis", "text",
1816 response_data.fields[1]);
1817 ExpectFilledField("", "lastname", "Presley", "text",
1818 response_data.fields[2]);
1819 ExpectFilledField("", "address", "", "text", response_data.fields[3]);
1820 ExpectFilledField("", "city", "", "text", response_data.fields[4]);
1821 ExpectFilledField("", "state", "", "text", response_data.fields[5]);
1822 ExpectFilledField("", "zip", "", "text", response_data.fields[6]);
1823 ExpectFilledField("", "ccname", "", "text", response_data.fields[7]);
1824 ExpectFilledField("", "ccnumber", "", "text", response_data.fields[8]);
1825 ExpectFilledField("", "ccexp", "", "text", response_data.fields[9]);
1826 ExpectFilledField("", "email", "theking@gmail.com", "text",
1827 response_data.fields[10]);
1830 // Fill the address portion of the billing section.
1831 const int kPageID2 = 2;
1832 GUIDPair guid2("00000000-0000-0000-0000-000000000001", 0);
1833 response_page_id = 0;
1834 FillAutofillFormDataAndSaveResults(kPageID2, form, form.fields[0],
1835 PackGUIDs(empty, guid2), &response_page_id, &response_data);
1837 SCOPED_TRACE("Billing address");
1838 EXPECT_EQ(kPageID2, response_page_id);
1839 EXPECT_EQ(ASCIIToUTF16("MyForm"), response_data.name);
1840 EXPECT_EQ(GURL("https://myform.com/form.html"), response_data.origin);
1841 EXPECT_EQ(GURL("https://myform.com/submit.html"), response_data.action);
1842 EXPECT_TRUE(response_data.user_submitted);
1843 ASSERT_EQ(11U, response_data.fields.size());
1845 ExpectFilledField("", "country", "US", "text",
1846 response_data.fields[0]);
1847 ExpectFilledField("", "firstname", "", "text", response_data.fields[1]);
1848 ExpectFilledField("", "lastname", "", "text", response_data.fields[2]);
1849 ExpectFilledField("", "address", "3734 Elvis Presley Blvd.", "text",
1850 response_data.fields[3]);
1851 ExpectFilledField("", "city", "Memphis", "text", response_data.fields[4]);
1852 ExpectFilledField("", "state", "Tennessee", "text",
1853 response_data.fields[5]);
1854 ExpectFilledField("", "zip", "38116", "text", response_data.fields[6]);
1855 ExpectFilledField("", "ccname", "", "text", response_data.fields[7]);
1856 ExpectFilledField("", "ccnumber", "", "text", response_data.fields[8]);
1857 ExpectFilledField("", "ccexp", "", "text", response_data.fields[9]);
1858 ExpectFilledField("", "email", "", "text", response_data.fields[10]);
1861 // Fill the credit card portion of the billing section.
1862 const int kPageID3 = 3;
1863 GUIDPair guid3("00000000-0000-0000-0000-000000000004", 0);
1864 response_page_id = 0;
1865 FillAutofillFormDataAndSaveResults(
1866 kPageID3, form, form.fields[form.fields.size() - 2],
1867 PackGUIDs(guid3, empty), &response_page_id, &response_data);
1869 SCOPED_TRACE("Credit card");
1870 EXPECT_EQ(kPageID3, response_page_id);
1871 EXPECT_EQ(ASCIIToUTF16("MyForm"), response_data.name);
1872 EXPECT_EQ(GURL("https://myform.com/form.html"), response_data.origin);
1873 EXPECT_EQ(GURL("https://myform.com/submit.html"), response_data.action);
1874 EXPECT_TRUE(response_data.user_submitted);
1875 ASSERT_EQ(11U, response_data.fields.size());
1877 ExpectFilledField("", "country", "", "text", response_data.fields[0]);
1878 ExpectFilledField("", "firstname", "", "text", response_data.fields[1]);
1879 ExpectFilledField("", "lastname", "", "text", response_data.fields[2]);
1880 ExpectFilledField("", "address", "", "text", response_data.fields[3]);
1881 ExpectFilledField("", "city", "", "text", response_data.fields[4]);
1882 ExpectFilledField("", "state", "", "text", response_data.fields[5]);
1883 ExpectFilledField("", "zip", "", "text", response_data.fields[6]);
1884 ExpectFilledField("", "ccname", "Elvis Presley", "text",
1885 response_data.fields[7]);
1886 ExpectFilledField("", "ccnumber", "4234567890123456", "text",
1887 response_data.fields[8]);
1888 ExpectFilledField("", "ccexp", "04/2012", "text", response_data.fields[9]);
1889 ExpectFilledField("", "email", "", "text", response_data.fields[10]);
1893 // Test that we correctly fill a form that has a single logical section with
1894 // multiple email address fields.
1895 TEST_F(AutofillManagerTest, FillFormWithMultipleEmails) {
1896 // Set up our form data.
1898 test::CreateTestAddressFormData(&form);
1899 FormFieldData field;
1900 test::CreateTestFormField("Confirm email", "email2", "", "text", &field);
1901 form.fields.push_back(field);
1903 std::vector<FormData> forms(1, form);
1907 GUIDPair guid("00000000-0000-0000-0000-000000000001", 0);
1908 GUIDPair empty(std::string(), 0);
1909 int response_page_id = 0;
1910 FormData response_data;
1911 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0],
1912 PackGUIDs(empty, guid), &response_page_id, &response_data);
1914 // The second email address should be filled.
1915 EXPECT_EQ(ASCIIToUTF16("theking@gmail.com"),
1916 response_data.fields.back().value);
1918 // The remainder of the form should be filled as usual.
1919 response_data.fields.pop_back();
1920 ExpectFilledAddressFormElvis(
1921 response_page_id, response_data, kDefaultPageID, false);
1924 // Test that we correctly fill a previously auto-filled form.
1925 TEST_F(AutofillManagerTest, FillAutofilledForm) {
1926 // Set up our form data.
1928 test::CreateTestAddressFormData(&form);
1929 // Mark one of the address fields as autofilled.
1930 form.fields[4].is_autofilled = true;
1931 CreateTestCreditCardFormData(&form, true, false);
1932 std::vector<FormData> forms(1, form);
1935 // First fill the address data.
1936 GUIDPair guid("00000000-0000-0000-0000-000000000001", 0);
1937 GUIDPair empty(std::string(), 0);
1938 int response_page_id = 0;
1939 FormData response_data;
1940 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, *form.fields.begin(),
1941 PackGUIDs(empty, guid), &response_page_id, &response_data);
1943 SCOPED_TRACE("Address");
1944 ExpectFilledForm(response_page_id, response_data, kDefaultPageID,
1945 "Elvis", "", "", "", "", "", "", "", "", "", "", "", "",
1946 "", "", true, true, false);
1949 // Now fill the credit card data.
1950 const int kPageID2 = 2;
1951 GUIDPair guid2("00000000-0000-0000-0000-000000000004", 0);
1952 response_page_id = 0;
1953 FillAutofillFormDataAndSaveResults(kPageID2, form, form.fields.back(),
1954 PackGUIDs(guid2, empty), &response_page_id, &response_data);
1956 SCOPED_TRACE("Credit card 1");
1957 ExpectFilledCreditCardFormElvis(
1958 response_page_id, response_data, kPageID2, true);
1961 // Now set the credit card fields to also be auto-filled, and try again to
1962 // fill the credit card data
1963 for (std::vector<FormFieldData>::iterator iter = form.fields.begin();
1964 iter != form.fields.end();
1966 iter->is_autofilled = true;
1969 const int kPageID3 = 3;
1970 response_page_id = 0;
1971 FillAutofillFormDataAndSaveResults(kPageID3, form, *form.fields.rbegin(),
1972 PackGUIDs(guid2, empty), &response_page_id, &response_data);
1974 SCOPED_TRACE("Credit card 2");
1975 ExpectFilledForm(response_page_id, response_data, kPageID3,
1976 "", "", "", "", "", "", "", "", "", "", "", "", "", "",
1977 "2012", true, true, false);
1981 // Test that we correctly fill an address form with a non-default variant for a
1982 // multi-valued field.
1983 TEST_F(AutofillManagerTest, FillAddressFormWithVariantType) {
1984 // Set up our form data.
1986 test::CreateTestAddressFormData(&form);
1987 std::vector<FormData> forms(1, form);
1990 // Add a name variant to the Elvis profile.
1991 AutofillProfile* profile = autofill_manager_->GetProfileWithGUID(
1992 "00000000-0000-0000-0000-000000000001");
1994 std::vector<base::string16> name_variants;
1995 name_variants.push_back(ASCIIToUTF16("Some"));
1996 name_variants.push_back(profile->GetRawInfo(NAME_FIRST));
1997 profile->SetRawMultiInfo(NAME_FIRST, name_variants);
1999 name_variants.clear();
2000 name_variants.push_back(ASCIIToUTF16("Other"));
2001 name_variants.push_back(profile->GetRawInfo(NAME_MIDDLE));
2002 profile->SetRawMultiInfo(NAME_MIDDLE, name_variants);
2004 name_variants.clear();
2005 name_variants.push_back(ASCIIToUTF16("Guy"));
2006 name_variants.push_back(profile->GetRawInfo(NAME_LAST));
2007 profile->SetRawMultiInfo(NAME_LAST, name_variants);
2009 GUIDPair guid(profile->guid(), 1);
2010 GUIDPair empty(std::string(), 0);
2011 int response_page_id = 0;
2012 FormData response_data1;
2013 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0],
2014 PackGUIDs(empty, guid), &response_page_id, &response_data1);
2016 SCOPED_TRACE("Valid variant");
2017 ExpectFilledAddressFormElvis(
2018 response_page_id, response_data1, kDefaultPageID, false);
2021 // Try filling with a variant that doesn't exist. The fields to which this
2022 // variant would normally apply should not be filled.
2023 const int kPageID2 = 2;
2024 GUIDPair guid2(profile->guid(), 2);
2025 response_page_id = 0;
2026 FormData response_data2;
2027 FillAutofillFormDataAndSaveResults(kPageID2, form, form.fields[0],
2028 PackGUIDs(empty, guid2), &response_page_id, &response_data2);
2030 SCOPED_TRACE("Invalid variant");
2031 ExpectFilledForm(response_page_id, response_data2, kPageID2, "", "", "",
2032 "3734 Elvis Presley Blvd.", "Apt. 10", "Memphis",
2033 "Tennessee", "38116", "United States", "12345678901",
2034 "theking@gmail.com", "", "", "", "", true, false, false);
2038 // Test that we correctly fill a phone number split across multiple fields.
2039 TEST_F(AutofillManagerTest, FillPhoneNumber) {
2040 // In one form, rely on the maxlength attribute to imply phone number parts.
2041 // In the other form, rely on the autocompletetype attribute.
2042 FormData form_with_maxlength;
2043 form_with_maxlength.name = ASCIIToUTF16("MyMaxlengthPhoneForm");
2044 form_with_maxlength.origin = GURL("http://myform.com/phone_form.html");
2045 form_with_maxlength.action = GURL("http://myform.com/phone_submit.html");
2046 form_with_maxlength.user_submitted = true;
2047 FormData form_with_autocompletetype = form_with_maxlength;
2048 form_with_autocompletetype.name = ASCIIToUTF16("MyAutocompletetypePhoneForm");
2054 const char* autocomplete_attribute;
2056 { "country code", "country_code", 1, "tel-country-code" },
2057 { "area code", "area_code", 3, "tel-area-code" },
2058 { "phone", "phone_prefix", 3, "tel-local-prefix" },
2059 { "-", "phone_suffix", 4, "tel-local-suffix" },
2060 { "Phone Extension", "ext", 3, "tel-extension" }
2063 FormFieldData field;
2064 const size_t default_max_length = field.max_length;
2065 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_fields); ++i) {
2066 test::CreateTestFormField(
2067 test_fields[i].label, test_fields[i].name, "", "text", &field);
2068 field.max_length = test_fields[i].max_length;
2069 field.autocomplete_attribute = std::string();
2070 form_with_maxlength.fields.push_back(field);
2072 field.max_length = default_max_length;
2073 field.autocomplete_attribute = test_fields[i].autocomplete_attribute;
2074 form_with_autocompletetype.fields.push_back(field);
2077 std::vector<FormData> forms;
2078 forms.push_back(form_with_maxlength);
2079 forms.push_back(form_with_autocompletetype);
2082 // We should be able to fill prefix and suffix fields for US numbers.
2083 AutofillProfile* work_profile = autofill_manager_->GetProfileWithGUID(
2084 "00000000-0000-0000-0000-000000000002");
2085 ASSERT_TRUE(work_profile != NULL);
2086 work_profile->SetRawInfo(PHONE_HOME_WHOLE_NUMBER,
2087 ASCIIToUTF16("16505554567"));
2089 GUIDPair guid(work_profile->guid(), 0);
2090 GUIDPair empty(std::string(), 0);
2092 int response_page_id = 0;
2093 FormData response_data1;
2094 FillAutofillFormDataAndSaveResults(page_id, form_with_maxlength,
2095 *form_with_maxlength.fields.begin(),
2096 PackGUIDs(empty, guid), &response_page_id, &response_data1);
2097 EXPECT_EQ(1, response_page_id);
2099 ASSERT_EQ(5U, response_data1.fields.size());
2100 EXPECT_EQ(ASCIIToUTF16("1"), response_data1.fields[0].value);
2101 EXPECT_EQ(ASCIIToUTF16("650"), response_data1.fields[1].value);
2102 EXPECT_EQ(ASCIIToUTF16("555"), response_data1.fields[2].value);
2103 EXPECT_EQ(ASCIIToUTF16("4567"), response_data1.fields[3].value);
2104 EXPECT_EQ(base::string16(), response_data1.fields[4].value);
2107 response_page_id = 0;
2108 FormData response_data2;
2109 FillAutofillFormDataAndSaveResults(page_id, form_with_autocompletetype,
2110 *form_with_autocompletetype.fields.begin(),
2111 PackGUIDs(empty, guid), &response_page_id, &response_data2);
2112 EXPECT_EQ(2, response_page_id);
2114 ASSERT_EQ(5U, response_data2.fields.size());
2115 EXPECT_EQ(ASCIIToUTF16("1"), response_data2.fields[0].value);
2116 EXPECT_EQ(ASCIIToUTF16("650"), response_data2.fields[1].value);
2117 EXPECT_EQ(ASCIIToUTF16("555"), response_data2.fields[2].value);
2118 EXPECT_EQ(ASCIIToUTF16("4567"), response_data2.fields[3].value);
2119 EXPECT_EQ(base::string16(), response_data2.fields[4].value);
2121 // We should not be able to fill prefix and suffix fields for international
2123 work_profile->SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("GB"));
2124 work_profile->SetRawInfo(PHONE_HOME_WHOLE_NUMBER,
2125 ASCIIToUTF16("447700954321"));
2127 response_page_id = 0;
2128 FormData response_data3;
2129 FillAutofillFormDataAndSaveResults(page_id, form_with_maxlength,
2130 *form_with_maxlength.fields.begin(),
2131 PackGUIDs(empty, guid), &response_page_id, &response_data3);
2132 EXPECT_EQ(3, response_page_id);
2134 ASSERT_EQ(5U, response_data3.fields.size());
2135 EXPECT_EQ(ASCIIToUTF16("44"), response_data3.fields[0].value);
2136 EXPECT_EQ(ASCIIToUTF16("7700"), response_data3.fields[1].value);
2137 EXPECT_EQ(ASCIIToUTF16("954321"), response_data3.fields[2].value);
2138 EXPECT_EQ(ASCIIToUTF16("954321"), response_data3.fields[3].value);
2139 EXPECT_EQ(base::string16(), response_data3.fields[4].value);
2142 response_page_id = 0;
2143 FormData response_data4;
2144 FillAutofillFormDataAndSaveResults(page_id, form_with_autocompletetype,
2145 *form_with_autocompletetype.fields.begin(),
2146 PackGUIDs(empty, guid), &response_page_id, &response_data4);
2147 EXPECT_EQ(4, response_page_id);
2149 ASSERT_EQ(5U, response_data4.fields.size());
2150 EXPECT_EQ(ASCIIToUTF16("44"), response_data4.fields[0].value);
2151 EXPECT_EQ(ASCIIToUTF16("7700"), response_data4.fields[1].value);
2152 EXPECT_EQ(ASCIIToUTF16("954321"), response_data4.fields[2].value);
2153 EXPECT_EQ(ASCIIToUTF16("954321"), response_data4.fields[3].value);
2154 EXPECT_EQ(base::string16(), response_data4.fields[4].value);
2156 // We should fill all phone fields with the same phone number variant.
2157 std::vector<base::string16> phone_variants;
2158 phone_variants.push_back(ASCIIToUTF16("16505554567"));
2159 phone_variants.push_back(ASCIIToUTF16("18887771234"));
2160 work_profile->SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"));
2161 work_profile->SetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, phone_variants);
2164 response_page_id = 0;
2165 FormData response_data5;
2166 GUIDPair variant_guid(work_profile->guid(), 1);
2167 FillAutofillFormDataAndSaveResults(page_id, form_with_maxlength,
2168 *form_with_maxlength.fields.begin(),
2169 PackGUIDs(empty, variant_guid), &response_page_id, &response_data5);
2170 EXPECT_EQ(5, response_page_id);
2172 ASSERT_EQ(5U, response_data5.fields.size());
2173 EXPECT_EQ(ASCIIToUTF16("1"), response_data5.fields[0].value);
2174 EXPECT_EQ(ASCIIToUTF16("888"), response_data5.fields[1].value);
2175 EXPECT_EQ(ASCIIToUTF16("777"), response_data5.fields[2].value);
2176 EXPECT_EQ(ASCIIToUTF16("1234"), response_data5.fields[3].value);
2177 EXPECT_EQ(base::string16(), response_data5.fields[4].value);
2180 // Test that we can still fill a form when a field has been removed from it.
2181 TEST_F(AutofillManagerTest, FormChangesRemoveField) {
2182 // Set up our form data.
2184 test::CreateTestAddressFormData(&form);
2186 // Add a field -- we'll remove it again later.
2187 FormFieldData field;
2188 test::CreateTestFormField("Some", "field", "", "text", &field);
2189 form.fields.insert(form.fields.begin() + 3, field);
2191 std::vector<FormData> forms(1, form);
2194 // Now, after the call to |FormsSeen|, we remove the field before filling.
2195 form.fields.erase(form.fields.begin() + 3);
2197 GUIDPair guid("00000000-0000-0000-0000-000000000001", 0);
2198 GUIDPair empty(std::string(), 0);
2199 int response_page_id = 0;
2200 FormData response_data;
2201 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0],
2202 PackGUIDs(empty, guid), &response_page_id, &response_data);
2203 ExpectFilledAddressFormElvis(
2204 response_page_id, response_data, kDefaultPageID, false);
2207 // Test that we can still fill a form when a field has been added to it.
2208 TEST_F(AutofillManagerTest, FormChangesAddField) {
2209 // The offset of the phone field in the address form.
2210 const int kPhoneFieldOffset = 9;
2212 // Set up our form data.
2214 test::CreateTestAddressFormData(&form);
2216 // Remove the phone field -- we'll add it back later.
2217 std::vector<FormFieldData>::iterator pos =
2218 form.fields.begin() + kPhoneFieldOffset;
2219 FormFieldData field = *pos;
2220 pos = form.fields.erase(pos);
2222 std::vector<FormData> forms(1, form);
2225 // Now, after the call to |FormsSeen|, we restore the field before filling.
2226 form.fields.insert(pos, field);
2228 GUIDPair guid("00000000-0000-0000-0000-000000000001", 0);
2229 GUIDPair empty(std::string(), 0);
2230 int response_page_id = 0;
2231 FormData response_data;
2232 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0],
2233 PackGUIDs(empty, guid), &response_page_id, &response_data);
2234 ExpectFilledAddressFormElvis(
2235 response_page_id, response_data, kDefaultPageID, false);
2238 // Test that we are able to save form data when forms are submitted.
2239 TEST_F(AutofillManagerTest, FormSubmitted) {
2240 // Set up our form data.
2242 test::CreateTestAddressFormData(&form);
2243 std::vector<FormData> forms(1, form);
2247 GUIDPair guid("00000000-0000-0000-0000-000000000001", 0);
2248 GUIDPair empty(std::string(), 0);
2249 int response_page_id = 0;
2250 FormData response_data;
2251 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0],
2252 PackGUIDs(empty, guid), &response_page_id, &response_data);
2253 ExpectFilledAddressFormElvis(
2254 response_page_id, response_data, kDefaultPageID, false);
2256 // Simulate form submission. We should call into the PDM to try to save the
2258 EXPECT_CALL(personal_data_, SaveImportedProfile(::testing::_)).Times(1);
2259 FormSubmitted(response_data);
2262 // Test that when Autocomplete is enabled and Autofill is disabled,
2263 // form submissions are still received by AutocompleteHistoryManager.
2264 TEST_F(AutofillManagerTest, FormSubmittedAutocompleteEnabled) {
2265 TestAutofillClient client;
2266 autofill_manager_.reset(
2267 new TestAutofillManager(autofill_driver_.get(), &client, NULL));
2268 autofill_manager_->set_autofill_enabled(false);
2269 scoped_ptr<MockAutocompleteHistoryManager> autocomplete_history_manager;
2270 autocomplete_history_manager.reset(
2271 new MockAutocompleteHistoryManager(autofill_driver_.get(), &client));
2272 autofill_manager_->autocomplete_history_manager_ =
2273 autocomplete_history_manager.Pass();
2275 // Set up our form data.
2277 test::CreateTestAddressFormData(&form);
2278 MockAutocompleteHistoryManager* m = static_cast<
2279 MockAutocompleteHistoryManager*>(
2280 autofill_manager_->autocomplete_history_manager_.get());
2282 OnFormSubmitted(_)).Times(1);
2283 FormSubmitted(form);
2286 // Test that when Autocomplete is enabled and Autofill is disabled,
2287 // Autocomplete suggestions are still received.
2288 TEST_F(AutofillManagerTest, AutocompleteSuggestionsWhenAutofillDisabled) {
2289 TestAutofillClient client;
2290 autofill_manager_.reset(
2291 new TestAutofillManager(autofill_driver_.get(), &client, NULL));
2292 autofill_manager_->set_autofill_enabled(false);
2293 autofill_manager_->SetExternalDelegate(external_delegate_.get());
2295 // Set up our form data.
2297 test::CreateTestAddressFormData(&form);
2298 std::vector<FormData> forms(1, form);
2300 const FormFieldData& field = form.fields[0];
2301 GetAutofillSuggestions(form, field);
2303 // Add some Autocomplete suggestions. We should return the autocomplete
2304 // suggestions, these will be culled by the renderer.
2305 std::vector<base::string16> suggestions;
2306 suggestions.push_back(ASCIIToUTF16("Jay"));
2307 suggestions.push_back(ASCIIToUTF16("Jason"));
2308 AutocompleteSuggestionsReturned(suggestions);
2310 base::string16 expected_values[] = {
2311 ASCIIToUTF16("Jay"),
2312 ASCIIToUTF16("Jason")
2314 base::string16 expected_labels[] = { base::string16(), base::string16()};
2315 base::string16 expected_icons[] = { base::string16(), base::string16()};
2316 int expected_unique_ids[] = {0, 0};
2317 external_delegate_->CheckSuggestions(
2318 kDefaultPageID, arraysize(expected_values), expected_values,
2319 expected_labels, expected_icons, expected_unique_ids);
2322 // Test that we are able to save form data when forms are submitted and we only
2323 // have server data for the field types.
2324 TEST_F(AutofillManagerTest, FormSubmittedServerTypes) {
2325 // Set up our form data.
2327 test::CreateTestAddressFormData(&form);
2329 // Simulate having seen this form on page load.
2330 // |form_structure| will be owned by |autofill_manager_|.
2331 TestFormStructure* form_structure = new TestFormStructure(form);
2332 AutofillMetrics metrics_logger; // ignored
2333 form_structure->DetermineHeuristicTypes(metrics_logger);
2335 // Clear the heuristic types, and instead set the appropriate server types.
2336 std::vector<ServerFieldType> heuristic_types, server_types;
2337 for (size_t i = 0; i < form.fields.size(); ++i) {
2338 heuristic_types.push_back(UNKNOWN_TYPE);
2339 server_types.push_back(form_structure->field(i)->heuristic_type());
2341 form_structure->SetFieldTypes(heuristic_types, server_types);
2342 autofill_manager_->AddSeenForm(form_structure);
2345 GUIDPair guid("00000000-0000-0000-0000-000000000001", 0);
2346 GUIDPair empty(std::string(), 0);
2347 int response_page_id = 0;
2348 FormData response_data;
2349 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0],
2350 PackGUIDs(empty, guid), &response_page_id, &response_data);
2351 ExpectFilledAddressFormElvis(
2352 response_page_id, response_data, kDefaultPageID, false);
2354 // Simulate form submission. We should call into the PDM to try to save the
2356 EXPECT_CALL(personal_data_, SaveImportedProfile(::testing::_)).Times(1);
2357 FormSubmitted(response_data);
2360 // Test that the form signature for an uploaded form always matches the form
2361 // signature from the query.
2362 TEST_F(AutofillManagerTest, FormSubmittedWithDifferentFields) {
2363 // Set up our form data.
2365 test::CreateTestAddressFormData(&form);
2366 std::vector<FormData> forms(1, form);
2369 // Cache the expected form signature.
2370 std::string signature = FormStructure(form).FormSignature();
2372 // Change the structure of the form prior to submission.
2373 // Websites would typically invoke JavaScript either on page load or on form
2374 // submit to achieve this.
2375 form.fields.pop_back();
2376 FormFieldData field = form.fields[3];
2377 form.fields[3] = form.fields[7];
2378 form.fields[7] = field;
2380 // Simulate form submission.
2381 FormSubmitted(form);
2382 EXPECT_EQ(signature, autofill_manager_->GetSubmittedFormSignature());
2385 // Test that we do not save form data when submitted fields contain default
2387 TEST_F(AutofillManagerTest, FormSubmittedWithDefaultValues) {
2388 // Set up our form data.
2390 test::CreateTestAddressFormData(&form);
2391 form.fields[3].value = ASCIIToUTF16("Enter your address");
2393 // Convert the state field to a <select> popup, to make sure that we only
2394 // reject default values for text fields.
2395 ASSERT_TRUE(form.fields[6].name == ASCIIToUTF16("state"));
2396 form.fields[6].form_control_type = "select-one";
2397 form.fields[6].value = ASCIIToUTF16("Tennessee");
2399 std::vector<FormData> forms(1, form);
2403 GUIDPair guid("00000000-0000-0000-0000-000000000001", 0);
2404 GUIDPair empty(std::string(), 0);
2405 int response_page_id = 0;
2406 FormData response_data;
2407 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[3],
2408 PackGUIDs(empty, guid), &response_page_id, &response_data);
2410 // Simulate form submission. We should call into the PDM to try to save the
2412 EXPECT_CALL(personal_data_, SaveImportedProfile(::testing::_)).Times(1);
2413 FormSubmitted(response_data);
2415 // Set the address field's value back to the default value.
2416 response_data.fields[3].value = ASCIIToUTF16("Enter your address");
2418 // Simulate form submission. We should not call into the PDM to try to save
2419 // the filled data, since the filled form is effectively missing an address.
2420 EXPECT_CALL(personal_data_, SaveImportedProfile(::testing::_)).Times(0);
2421 FormSubmitted(response_data);
2424 // Checks that resetting the auxiliary profile enabled preference does the right
2425 // thing on all platforms.
2426 TEST_F(AutofillManagerTest, AuxiliaryProfilesReset) {
2427 PrefService* prefs = autofill_client_.GetPrefs();
2428 #if defined(OS_MACOSX)
2429 // Auxiliary profiles is implemented on Mac only.
2430 // OSX: This preference exists for legacy reasons. It is no longer used.
2432 prefs->GetBoolean(::autofill::prefs::kAutofillAuxiliaryProfilesEnabled));
2433 prefs->SetBoolean(::autofill::prefs::kAutofillAuxiliaryProfilesEnabled,
2435 prefs->ClearPref(::autofill::prefs::kAutofillAuxiliaryProfilesEnabled);
2437 prefs->GetBoolean(::autofill::prefs::kAutofillAuxiliaryProfilesEnabled));
2440 prefs->GetBoolean(::autofill::prefs::kAutofillAuxiliaryProfilesEnabled));
2441 prefs->SetBoolean(::autofill::prefs::kAutofillAuxiliaryProfilesEnabled, true);
2442 prefs->ClearPref(::autofill::prefs::kAutofillAuxiliaryProfilesEnabled);
2444 prefs->GetBoolean(::autofill::prefs::kAutofillAuxiliaryProfilesEnabled));
2445 #endif // defined(OS_MACOSX)
2448 TEST_F(AutofillManagerTest, DeterminePossibleFieldTypesForUpload) {
2450 form.name = ASCIIToUTF16("MyForm");
2451 form.origin = GURL("http://myform.com/form.html");
2452 form.action = GURL("http://myform.com/submit.html");
2453 form.user_submitted = true;
2455 std::vector<ServerFieldTypeSet> expected_types;
2457 // These fields should all match.
2458 FormFieldData field;
2459 ServerFieldTypeSet types;
2460 test::CreateTestFormField("", "1", "Elvis", "text", &field);
2462 types.insert(NAME_FIRST);
2463 form.fields.push_back(field);
2464 expected_types.push_back(types);
2466 test::CreateTestFormField("", "2", "Aaron", "text", &field);
2468 types.insert(NAME_MIDDLE);
2469 form.fields.push_back(field);
2470 expected_types.push_back(types);
2472 test::CreateTestFormField("", "3", "A", "text", &field);
2474 types.insert(NAME_MIDDLE_INITIAL);
2475 form.fields.push_back(field);
2476 expected_types.push_back(types);
2478 test::CreateTestFormField("", "4", "Presley", "text", &field);
2480 types.insert(NAME_LAST);
2481 form.fields.push_back(field);
2482 expected_types.push_back(types);
2484 test::CreateTestFormField("", "5", "Elvis Presley", "text", &field);
2486 types.insert(CREDIT_CARD_NAME);
2487 form.fields.push_back(field);
2488 expected_types.push_back(types);
2490 test::CreateTestFormField("", "6", "Elvis Aaron Presley", "text",
2493 types.insert(NAME_FULL);
2494 form.fields.push_back(field);
2495 expected_types.push_back(types);
2497 test::CreateTestFormField("", "7", "theking@gmail.com", "email",
2500 types.insert(EMAIL_ADDRESS);
2501 form.fields.push_back(field);
2502 expected_types.push_back(types);
2504 test::CreateTestFormField("", "8", "RCA", "text", &field);
2506 types.insert(COMPANY_NAME);
2507 form.fields.push_back(field);
2508 expected_types.push_back(types);
2510 test::CreateTestFormField("", "9", "3734 Elvis Presley Blvd.",
2513 types.insert(ADDRESS_HOME_LINE1);
2514 form.fields.push_back(field);
2515 expected_types.push_back(types);
2517 test::CreateTestFormField("", "10", "Apt. 10", "text", &field);
2519 types.insert(ADDRESS_HOME_LINE2);
2520 form.fields.push_back(field);
2521 expected_types.push_back(types);
2523 test::CreateTestFormField("", "11", "Memphis", "text", &field);
2525 types.insert(ADDRESS_HOME_CITY);
2526 form.fields.push_back(field);
2527 expected_types.push_back(types);
2529 test::CreateTestFormField("", "12", "Tennessee", "text", &field);
2531 types.insert(ADDRESS_HOME_STATE);
2532 form.fields.push_back(field);
2533 expected_types.push_back(types);
2535 test::CreateTestFormField("", "13", "38116", "text", &field);
2537 types.insert(ADDRESS_HOME_ZIP);
2538 form.fields.push_back(field);
2539 expected_types.push_back(types);
2541 test::CreateTestFormField("", "14", "USA", "text", &field);
2543 types.insert(ADDRESS_HOME_COUNTRY);
2544 form.fields.push_back(field);
2545 expected_types.push_back(types);
2547 test::CreateTestFormField("", "15", "United States", "text", &field);
2549 types.insert(ADDRESS_HOME_COUNTRY);
2550 form.fields.push_back(field);
2551 expected_types.push_back(types);
2553 test::CreateTestFormField("", "16", "+1 (234) 567-8901", "text",
2556 types.insert(PHONE_HOME_WHOLE_NUMBER);
2557 form.fields.push_back(field);
2558 expected_types.push_back(types);
2560 test::CreateTestFormField("", "17", "2345678901", "text", &field);
2562 types.insert(PHONE_HOME_CITY_AND_NUMBER);
2563 form.fields.push_back(field);
2564 expected_types.push_back(types);
2566 test::CreateTestFormField("", "18", "1", "text", &field);
2568 types.insert(PHONE_HOME_COUNTRY_CODE);
2569 form.fields.push_back(field);
2570 expected_types.push_back(types);
2572 test::CreateTestFormField("", "19", "234", "text", &field);
2574 types.insert(PHONE_HOME_CITY_CODE);
2575 form.fields.push_back(field);
2576 expected_types.push_back(types);
2578 test::CreateTestFormField("", "20", "5678901", "text", &field);
2580 types.insert(PHONE_HOME_NUMBER);
2581 form.fields.push_back(field);
2582 expected_types.push_back(types);
2584 test::CreateTestFormField("", "21", "567", "text", &field);
2586 types.insert(PHONE_HOME_NUMBER);
2587 form.fields.push_back(field);
2588 expected_types.push_back(types);
2590 test::CreateTestFormField("", "22", "8901", "text", &field);
2592 types.insert(PHONE_HOME_NUMBER);
2593 form.fields.push_back(field);
2594 expected_types.push_back(types);
2596 test::CreateTestFormField("", "23", "4234-5678-9012-3456", "text",
2599 types.insert(CREDIT_CARD_NUMBER);
2600 form.fields.push_back(field);
2601 expected_types.push_back(types);
2603 test::CreateTestFormField("", "24", "04", "text", &field);
2605 types.insert(CREDIT_CARD_EXP_MONTH);
2606 form.fields.push_back(field);
2607 expected_types.push_back(types);
2609 test::CreateTestFormField("", "25", "April", "text", &field);
2611 types.insert(CREDIT_CARD_EXP_MONTH);
2612 form.fields.push_back(field);
2613 expected_types.push_back(types);
2615 test::CreateTestFormField("", "26", "2012", "text", &field);
2617 types.insert(CREDIT_CARD_EXP_4_DIGIT_YEAR);
2618 form.fields.push_back(field);
2619 expected_types.push_back(types);
2621 test::CreateTestFormField("", "27", "12", "text", &field);
2623 types.insert(CREDIT_CARD_EXP_2_DIGIT_YEAR);
2624 form.fields.push_back(field);
2625 expected_types.push_back(types);
2627 test::CreateTestFormField("", "28", "04/2012", "text", &field);
2629 types.insert(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR);
2630 form.fields.push_back(field);
2631 expected_types.push_back(types);
2633 // Make sure that we trim whitespace properly.
2634 test::CreateTestFormField("", "29", "", "text", &field);
2636 types.insert(EMPTY_TYPE);
2637 form.fields.push_back(field);
2638 expected_types.push_back(types);
2640 test::CreateTestFormField("", "30", " ", "text", &field);
2642 types.insert(EMPTY_TYPE);
2643 form.fields.push_back(field);
2644 expected_types.push_back(types);
2646 test::CreateTestFormField("", "31", " Elvis", "text", &field);
2648 types.insert(NAME_FIRST);
2649 form.fields.push_back(field);
2650 expected_types.push_back(types);
2652 test::CreateTestFormField("", "32", "Elvis ", "text", &field);
2654 types.insert(NAME_FIRST);
2655 form.fields.push_back(field);
2656 expected_types.push_back(types);
2658 // These fields should not match, as they differ by case.
2659 test::CreateTestFormField("", "33", "elvis", "text", &field);
2661 types.insert(UNKNOWN_TYPE);
2662 form.fields.push_back(field);
2663 expected_types.push_back(types);
2665 test::CreateTestFormField("", "34", "3734 Elvis Presley BLVD",
2668 types.insert(UNKNOWN_TYPE);
2669 form.fields.push_back(field);
2670 expected_types.push_back(types);
2672 // These fields should not match, as they are unsupported variants.
2673 test::CreateTestFormField("", "35", "Elvis Aaron", "text", &field);
2675 types.insert(UNKNOWN_TYPE);
2676 form.fields.push_back(field);
2677 expected_types.push_back(types);
2679 test::CreateTestFormField("", "36", "Mr. Presley", "text", &field);
2681 types.insert(UNKNOWN_TYPE);
2682 form.fields.push_back(field);
2683 expected_types.push_back(types);
2685 test::CreateTestFormField("", "37", "3734 Elvis Presley", "text",
2688 types.insert(UNKNOWN_TYPE);
2689 form.fields.push_back(field);
2690 expected_types.push_back(types);
2692 test::CreateTestFormField("", "38", "TN", "text", &field);
2694 types.insert(UNKNOWN_TYPE);
2695 form.fields.push_back(field);
2696 expected_types.push_back(types);
2698 test::CreateTestFormField("", "39", "38116-1023", "text", &field);
2700 types.insert(UNKNOWN_TYPE);
2701 form.fields.push_back(field);
2702 expected_types.push_back(types);
2704 test::CreateTestFormField("", "20", "5", "text", &field);
2706 types.insert(UNKNOWN_TYPE);
2707 form.fields.push_back(field);
2708 expected_types.push_back(types);
2710 test::CreateTestFormField("", "20", "56", "text", &field);
2712 types.insert(UNKNOWN_TYPE);
2713 form.fields.push_back(field);
2714 expected_types.push_back(types);
2716 test::CreateTestFormField("", "20", "901", "text", &field);
2718 types.insert(UNKNOWN_TYPE);
2719 form.fields.push_back(field);
2720 expected_types.push_back(types);
2722 test::CreateTestFormField("", "40", "mypassword", "password", &field);
2724 types.insert(PASSWORD);
2725 form.fields.push_back(field);
2726 expected_types.push_back(types);
2728 autofill_manager_->set_expected_submitted_field_types(expected_types);
2729 FormSubmitted(form);
2732 TEST_F(AutofillManagerTest, RemoveProfile) {
2733 // Add and remove an Autofill profile.
2734 AutofillProfile* profile = new AutofillProfile;
2735 std::string guid = "00000000-0000-0000-0000-000000000102";
2736 profile->set_guid(guid.c_str());
2737 autofill_manager_->AddProfile(profile);
2739 GUIDPair guid_pair(guid, 0);
2740 GUIDPair empty(std::string(), 0);
2741 int id = PackGUIDs(empty, guid_pair);
2743 autofill_manager_->RemoveAutofillProfileOrCreditCard(id);
2745 EXPECT_FALSE(autofill_manager_->GetProfileWithGUID(guid.c_str()));
2748 TEST_F(AutofillManagerTest, RemoveCreditCard){
2749 // Add and remove an Autofill credit card.
2750 CreditCard* credit_card = new CreditCard;
2751 std::string guid = "00000000-0000-0000-0000-000000100007";
2752 credit_card->set_guid(guid.c_str());
2753 autofill_manager_->AddCreditCard(credit_card);
2755 GUIDPair guid_pair(guid, 0);
2756 GUIDPair empty(std::string(), 0);
2757 int id = PackGUIDs(guid_pair, empty);
2759 autofill_manager_->RemoveAutofillProfileOrCreditCard(id);
2761 EXPECT_FALSE(autofill_manager_->GetCreditCardWithGUID(guid.c_str()));
2764 TEST_F(AutofillManagerTest, RemoveProfileVariant) {
2765 // Add and remove an Autofill profile.
2766 AutofillProfile* profile = new AutofillProfile;
2767 std::string guid = "00000000-0000-0000-0000-000000000102";
2768 profile->set_guid(guid.c_str());
2769 autofill_manager_->AddProfile(profile);
2771 GUIDPair guid_pair(guid, 1);
2772 GUIDPair empty(std::string(), 0);
2773 int id = PackGUIDs(empty, guid_pair);
2775 autofill_manager_->RemoveAutofillProfileOrCreditCard(id);
2777 // TODO(csharp): Currently variants should not be deleted, but once they are
2778 // update these expectations.
2779 // http://crbug.com/124211
2780 EXPECT_TRUE(autofill_manager_->GetProfileWithGUID(guid.c_str()));
2785 class MockAutofillClient : public TestAutofillClient {
2787 MockAutofillClient() {}
2789 virtual ~MockAutofillClient() {}
2791 virtual void ShowRequestAutocompleteDialog(
2792 const FormData& form,
2793 const GURL& source_url,
2794 const ResultCallback& callback) OVERRIDE {
2795 callback.Run(user_supplied_data_ ? AutocompleteResultSuccess :
2796 AutocompleteResultErrorDisabled,
2798 user_supplied_data_.get());
2801 void SetUserSuppliedData(scoped_ptr<FormStructure> user_supplied_data) {
2802 user_supplied_data_.reset(user_supplied_data.release());
2806 scoped_ptr<FormStructure> user_supplied_data_;
2808 DISALLOW_COPY_AND_ASSIGN(MockAutofillClient);
2813 // Test our external delegate is called at the right time.
2814 TEST_F(AutofillManagerTest, TestExternalDelegate) {
2816 test::CreateTestAddressFormData(&form);
2817 std::vector<FormData> forms(1, form);
2819 const FormFieldData& field = form.fields[0];
2820 GetAutofillSuggestions(form, field); // should call the delegate's OnQuery()
2822 EXPECT_TRUE(external_delegate_->on_query_seen());
2825 } // namespace autofill