ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / components / autofill / core / browser / autofill_manager_unittest.cc
blob144925ae9bd9e6dad64bb559c1ee8aee259e8cb8
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.
5 #include <algorithm>
6 #include <vector>
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/string_util.h"
19 #include "base/strings/stringprintf.h"
20 #include "base/strings/utf_string_conversions.h"
21 #include "base/time/time.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_profile.h"
25 #include "components/autofill/core/browser/autofill_test_utils.h"
26 #include "components/autofill/core/browser/credit_card.h"
27 #include "components/autofill/core/browser/personal_data_manager.h"
28 #include "components/autofill/core/browser/popup_item_ids.h"
29 #include "components/autofill/core/browser/test_autofill_client.h"
30 #include "components/autofill/core/browser/test_autofill_driver.h"
31 #include "components/autofill/core/browser/test_autofill_external_delegate.h"
32 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
33 #include "components/autofill/core/common/autofill_pref_names.h"
34 #include "components/autofill/core/common/autofill_switches.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/geometry/rect.h"
42 #include "url/gurl.h"
44 using base::ASCIIToUTF16;
45 using base::UTF8ToUTF16;
46 using testing::_;
48 namespace autofill {
50 namespace {
52 const int kDefaultPageID = 137;
54 class TestPersonalDataManager : public PersonalDataManager {
55 public:
56 TestPersonalDataManager() : PersonalDataManager("en-US") {
57 CreateTestAutofillProfiles(&web_profiles_);
58 CreateTestCreditCards(&local_credit_cards_);
61 using PersonalDataManager::set_database;
62 using PersonalDataManager::SetPrefService;
64 MOCK_METHOD1(SaveImportedProfile, std::string(const AutofillProfile&));
66 AutofillProfile* GetProfileWithGUID(const char* guid) {
67 for (AutofillProfile* profile : GetProfiles()) {
68 if (!profile->guid().compare(guid))
69 return profile;
71 return NULL;
74 CreditCard* GetCreditCardWithGUID(const char* guid) {
75 for (CreditCard* card : GetCreditCards()) {
76 if (!card->guid().compare(guid))
77 return card;
79 return NULL;
82 void AddProfile(AutofillProfile* profile) {
83 web_profiles_.push_back(profile);
86 void AddCreditCard(CreditCard* credit_card) {
87 local_credit_cards_.push_back(credit_card);
90 void RecordUseOf(const AutofillDataModel& data_model) override {
91 CreditCard* credit_card = GetCreditCardWithGUID(data_model.guid().c_str());
92 if (credit_card)
93 credit_card->RecordUse();
95 AutofillProfile* profile = GetProfileWithGUID(data_model.guid().c_str());
96 if (profile)
97 profile->RecordUse();
100 void RemoveByGUID(const std::string& guid) override {
101 CreditCard* credit_card = GetCreditCardWithGUID(guid.c_str());
102 if (credit_card) {
103 local_credit_cards_.erase(
104 std::find(local_credit_cards_.begin(), local_credit_cards_.end(),
105 credit_card));
108 AutofillProfile* profile = GetProfileWithGUID(guid.c_str());
109 if (profile) {
110 web_profiles_.erase(
111 std::find(web_profiles_.begin(), web_profiles_.end(), profile));
115 // Do nothing (auxiliary profiles will be created in
116 // CreateTestAuxiliaryProfile).
117 virtual void LoadAuxiliaryProfiles(bool record_metrics) const override {}
119 void ClearAutofillProfiles() {
120 web_profiles_.clear();
123 void ClearCreditCards() {
124 local_credit_cards_.clear();
127 void CreateTestAuxiliaryProfiles() {
128 CreateTestAutofillProfiles(&auxiliary_profiles_);
131 void CreateTestCreditCardsYearAndMonth(const char* year, const char* month) {
132 ClearCreditCards();
133 CreditCard* credit_card = new CreditCard;
134 test::SetCreditCardInfo(credit_card, "Miku Hatsune",
135 "4234567890654321", // Visa
136 month, year);
137 credit_card->set_guid("00000000-0000-0000-0000-000000000007");
138 local_credit_cards_.push_back(credit_card);
141 private:
142 void CreateTestAutofillProfiles(ScopedVector<AutofillProfile>* profiles) {
143 AutofillProfile* profile = new AutofillProfile;
144 test::SetProfileInfo(profile, "Elvis", "Aaron",
145 "Presley", "theking@gmail.com", "RCA",
146 "3734 Elvis Presley Blvd.", "Apt. 10",
147 "Memphis", "Tennessee", "38116", "US",
148 "12345678901");
149 profile->set_guid("00000000-0000-0000-0000-000000000001");
150 profiles->push_back(profile);
151 profile = new AutofillProfile;
152 test::SetProfileInfo(profile, "Charles", "Hardin",
153 "Holley", "buddy@gmail.com", "Decca",
154 "123 Apple St.", "unit 6", "Lubbock",
155 "Texas", "79401", "US", "23456789012");
156 profile->set_guid("00000000-0000-0000-0000-000000000002");
157 profiles->push_back(profile);
158 profile = new AutofillProfile;
159 test::SetProfileInfo(
160 profile, "", "", "", "", "", "", "", "", "", "", "", "");
161 profile->set_guid("00000000-0000-0000-0000-000000000003");
162 profiles->push_back(profile);
165 void CreateTestCreditCards(ScopedVector<CreditCard>* credit_cards) {
166 CreditCard* credit_card = new CreditCard;
167 test::SetCreditCardInfo(credit_card, "Elvis Presley",
168 "4234 5678 9012 3456", // Visa
169 "04", "2012");
170 credit_card->set_guid("00000000-0000-0000-0000-000000000004");
171 credit_cards->push_back(credit_card);
173 credit_card = new CreditCard;
174 test::SetCreditCardInfo(credit_card, "Buddy Holly",
175 "5187654321098765", // Mastercard
176 "10", "2014");
177 credit_card->set_guid("00000000-0000-0000-0000-000000000005");
178 credit_cards->push_back(credit_card);
180 credit_card = new CreditCard;
181 test::SetCreditCardInfo(credit_card, "", "", "", "");
182 credit_card->set_guid("00000000-0000-0000-0000-000000000006");
183 credit_cards->push_back(credit_card);
186 DISALLOW_COPY_AND_ASSIGN(TestPersonalDataManager);
189 // Populates |form| with data corresponding to a simple credit card form.
190 // Note that this actually appends fields to the form data, which can be useful
191 // for building up more complex test forms.
192 void CreateTestCreditCardFormData(FormData* form,
193 bool is_https,
194 bool use_month_type) {
195 form->name = ASCIIToUTF16("MyForm");
196 if (is_https) {
197 form->origin = GURL("https://myform.com/form.html");
198 form->action = GURL("https://myform.com/submit.html");
199 } else {
200 form->origin = GURL("http://myform.com/form.html");
201 form->action = GURL("http://myform.com/submit.html");
203 form->user_submitted = true;
205 FormFieldData field;
206 test::CreateTestFormField("Name on Card", "nameoncard", "", "text", &field);
207 form->fields.push_back(field);
208 test::CreateTestFormField("Card Number", "cardnumber", "", "text", &field);
209 form->fields.push_back(field);
210 if (use_month_type) {
211 test::CreateTestFormField(
212 "Expiration Date", "ccmonth", "", "month", &field);
213 form->fields.push_back(field);
214 } else {
215 test::CreateTestFormField("Expiration Date", "ccmonth", "", "text", &field);
216 form->fields.push_back(field);
217 test::CreateTestFormField("", "ccyear", "", "text", &field);
218 form->fields.push_back(field);
222 void ExpectFilledField(const char* expected_label,
223 const char* expected_name,
224 const char* expected_value,
225 const char* expected_form_control_type,
226 const FormFieldData& field) {
227 SCOPED_TRACE(expected_label);
228 EXPECT_EQ(UTF8ToUTF16(expected_label), field.label);
229 EXPECT_EQ(UTF8ToUTF16(expected_name), field.name);
230 EXPECT_EQ(UTF8ToUTF16(expected_value), field.value);
231 EXPECT_EQ(expected_form_control_type, field.form_control_type);
234 // Verifies that the |filled_form| has been filled with the given data.
235 // Verifies address fields if |has_address_fields| is true, and verifies
236 // credit card fields if |has_credit_card_fields| is true. Verifies both if both
237 // are true. |use_month_type| is used for credit card input month type.
238 void ExpectFilledForm(int page_id,
239 const FormData& filled_form,
240 int expected_page_id,
241 const char* first,
242 const char* middle,
243 const char* last,
244 const char* address1,
245 const char* address2,
246 const char* city,
247 const char* state,
248 const char* postal_code,
249 const char* country,
250 const char* phone,
251 const char* email,
252 const char* name_on_card,
253 const char* card_number,
254 const char* expiration_month,
255 const char* expiration_year,
256 bool has_address_fields,
257 bool has_credit_card_fields,
258 bool use_month_type) {
259 // The number of fields in the address and credit card forms created above.
260 const size_t kAddressFormSize = 11;
261 const size_t kCreditCardFormSize = use_month_type ? 3 : 4;
263 EXPECT_EQ(expected_page_id, page_id);
264 EXPECT_EQ(ASCIIToUTF16("MyForm"), filled_form.name);
265 if (has_credit_card_fields) {
266 EXPECT_EQ(GURL("https://myform.com/form.html"), filled_form.origin);
267 EXPECT_EQ(GURL("https://myform.com/submit.html"), filled_form.action);
268 } else {
269 EXPECT_EQ(GURL("http://myform.com/form.html"), filled_form.origin);
270 EXPECT_EQ(GURL("http://myform.com/submit.html"), filled_form.action);
272 EXPECT_TRUE(filled_form.user_submitted);
274 size_t form_size = 0;
275 if (has_address_fields)
276 form_size += kAddressFormSize;
277 if (has_credit_card_fields)
278 form_size += kCreditCardFormSize;
279 ASSERT_EQ(form_size, filled_form.fields.size());
281 if (has_address_fields) {
282 ExpectFilledField("First Name", "firstname", first, "text",
283 filled_form.fields[0]);
284 ExpectFilledField("Middle Name", "middlename", middle, "text",
285 filled_form.fields[1]);
286 ExpectFilledField("Last Name", "lastname", last, "text",
287 filled_form.fields[2]);
288 ExpectFilledField("Address Line 1", "addr1", address1, "text",
289 filled_form.fields[3]);
290 ExpectFilledField("Address Line 2", "addr2", address2, "text",
291 filled_form.fields[4]);
292 ExpectFilledField("City", "city", city, "text",
293 filled_form.fields[5]);
294 ExpectFilledField("State", "state", state, "text",
295 filled_form.fields[6]);
296 ExpectFilledField("Postal Code", "zipcode", postal_code, "text",
297 filled_form.fields[7]);
298 ExpectFilledField("Country", "country", country, "text",
299 filled_form.fields[8]);
300 ExpectFilledField("Phone Number", "phonenumber", phone, "tel",
301 filled_form.fields[9]);
302 ExpectFilledField("Email", "email", email, "email",
303 filled_form.fields[10]);
306 if (has_credit_card_fields) {
307 size_t offset = has_address_fields? kAddressFormSize : 0;
308 ExpectFilledField("Name on Card", "nameoncard", name_on_card, "text",
309 filled_form.fields[offset + 0]);
310 ExpectFilledField("Card Number", "cardnumber", card_number, "text",
311 filled_form.fields[offset + 1]);
312 if (use_month_type) {
313 std::string exp_year = expiration_year;
314 std::string exp_month = expiration_month;
315 std::string date;
316 if (!exp_year.empty() && !exp_month.empty())
317 date = exp_year + "-" + exp_month;
319 ExpectFilledField("Expiration Date", "ccmonth", date.c_str(), "month",
320 filled_form.fields[offset + 2]);
321 } else {
322 ExpectFilledField("Expiration Date", "ccmonth", expiration_month, "text",
323 filled_form.fields[offset + 2]);
324 ExpectFilledField("", "ccyear", expiration_year, "text",
325 filled_form.fields[offset + 3]);
330 void ExpectFilledAddressFormElvis(int page_id,
331 const FormData& filled_form,
332 int expected_page_id,
333 bool has_credit_card_fields) {
334 ExpectFilledForm(page_id, filled_form, expected_page_id, "Elvis", "Aaron",
335 "Presley", "3734 Elvis Presley Blvd.", "Apt. 10", "Memphis",
336 "Tennessee", "38116", "United States", "12345678901",
337 "theking@gmail.com", "", "", "", "", true,
338 has_credit_card_fields, false);
341 void ExpectFilledCreditCardFormElvis(int page_id,
342 const FormData& filled_form,
343 int expected_page_id,
344 bool has_address_fields) {
345 ExpectFilledForm(page_id, filled_form, expected_page_id,
346 "", "", "", "", "", "", "", "", "", "", "",
347 "Elvis Presley", "4234567890123456", "04", "2012",
348 has_address_fields, true, false);
351 void ExpectFilledCreditCardYearMonthWithYearMonth(int page_id,
352 const FormData& filled_form,
353 int expected_page_id,
354 bool has_address_fields,
355 const char* year,
356 const char* month) {
357 ExpectFilledForm(page_id, filled_form, expected_page_id,
358 "", "", "", "", "", "", "", "", "", "", "",
359 "Miku Hatsune", "4234567890654321", month, year,
360 has_address_fields, true, true);
363 class MockAutocompleteHistoryManager : public AutocompleteHistoryManager {
364 public:
365 MockAutocompleteHistoryManager(AutofillDriver* driver, AutofillClient* client)
366 : AutocompleteHistoryManager(driver, client) {}
368 MOCK_METHOD5(OnGetAutocompleteSuggestions, void(
369 int query_id,
370 const base::string16& name,
371 const base::string16& prefix,
372 const std::string& form_control_type,
373 const std::vector<Suggestion>& suggestions));
374 MOCK_METHOD1(OnFormSubmitted, void(const FormData& form));
376 private:
377 DISALLOW_COPY_AND_ASSIGN(MockAutocompleteHistoryManager);
380 class MockAutofillDriver : public TestAutofillDriver {
381 public:
382 MockAutofillDriver() {}
384 // Mock methods to enable testability.
385 MOCK_METHOD3(SendFormDataToRenderer, void(int query_id,
386 RendererFormDataAction action,
387 const FormData& data));
389 private:
390 DISALLOW_COPY_AND_ASSIGN(MockAutofillDriver);
393 class TestAutofillManager : public AutofillManager {
394 public:
395 TestAutofillManager(AutofillDriver* driver,
396 autofill::AutofillClient* client,
397 TestPersonalDataManager* personal_data)
398 : AutofillManager(driver, client, personal_data),
399 personal_data_(personal_data),
400 autofill_enabled_(true) {}
401 ~TestAutofillManager() override {}
403 bool IsAutofillEnabled() const override { return autofill_enabled_; }
405 void set_autofill_enabled(bool autofill_enabled) {
406 autofill_enabled_ = autofill_enabled;
409 void set_expected_submitted_field_types(
410 const std::vector<ServerFieldTypeSet>& expected_types) {
411 expected_submitted_field_types_ = expected_types;
414 void UploadFormDataAsyncCallback(
415 const FormStructure* submitted_form,
416 const base::TimeTicks& load_time,
417 const base::TimeTicks& interaction_time,
418 const base::TimeTicks& submission_time) override {
419 run_loop_->Quit();
421 // If we have expected field types set, make sure they match.
422 if (!expected_submitted_field_types_.empty()) {
423 ASSERT_EQ(expected_submitted_field_types_.size(),
424 submitted_form->field_count());
425 for (size_t i = 0; i < expected_submitted_field_types_.size(); ++i) {
426 SCOPED_TRACE(
427 base::StringPrintf(
428 "Field %d with value %s", static_cast<int>(i),
429 base::UTF16ToUTF8(submitted_form->field(i)->value).c_str()));
430 const ServerFieldTypeSet& possible_types =
431 submitted_form->field(i)->possible_types();
432 EXPECT_EQ(expected_submitted_field_types_[i].size(),
433 possible_types.size());
434 for (ServerFieldTypeSet::const_iterator it =
435 expected_submitted_field_types_[i].begin();
436 it != expected_submitted_field_types_[i].end(); ++it) {
437 EXPECT_TRUE(possible_types.count(*it))
438 << "Expected type: " << AutofillType(*it).ToString();
443 AutofillManager::UploadFormDataAsyncCallback(submitted_form,
444 load_time,
445 interaction_time,
446 submission_time);
449 // Resets the run loop so that it can wait for an asynchronous form
450 // submission to complete.
451 void ResetRunLoop() { run_loop_.reset(new base::RunLoop()); }
453 // Wait for the asynchronous OnFormSubmitted() call to complete.
454 void WaitForAsyncFormSubmit() { run_loop_->Run(); }
456 void UploadFormData(const FormStructure& submitted_form) override {
457 submitted_form_signature_ = submitted_form.FormSignature();
460 const std::string GetSubmittedFormSignature() {
461 return submitted_form_signature_;
464 AutofillProfile* GetProfileWithGUID(const char* guid) {
465 return personal_data_->GetProfileWithGUID(guid);
468 CreditCard* GetCreditCardWithGUID(const char* guid) {
469 return personal_data_->GetCreditCardWithGUID(guid);
472 void AddProfile(AutofillProfile* profile) {
473 personal_data_->AddProfile(profile);
476 void AddCreditCard(CreditCard* credit_card) {
477 personal_data_->AddCreditCard(credit_card);
480 int GetPackedCreditCardID(int credit_card_id) {
481 std::string credit_card_guid =
482 base::StringPrintf("00000000-0000-0000-0000-%012d", credit_card_id);
484 return MakeFrontendID(SuggestionBackendID(credit_card_guid, 0),
485 SuggestionBackendID());
488 void AddSeenForm(FormStructure* form) {
489 form_structures()->push_back(form);
492 void ClearFormStructures() {
493 form_structures()->clear();
496 private:
497 // Weak reference.
498 TestPersonalDataManager* personal_data_;
500 bool autofill_enabled_;
502 scoped_ptr<base::RunLoop> run_loop_;
504 std::string submitted_form_signature_;
505 std::vector<ServerFieldTypeSet> expected_submitted_field_types_;
507 DISALLOW_COPY_AND_ASSIGN(TestAutofillManager);
510 class TestAutofillExternalDelegate : public AutofillExternalDelegate {
511 public:
512 explicit TestAutofillExternalDelegate(AutofillManager* autofill_manager,
513 AutofillDriver* autofill_driver)
514 : AutofillExternalDelegate(autofill_manager, autofill_driver),
515 on_query_seen_(false),
516 on_suggestions_returned_seen_(false) {}
517 ~TestAutofillExternalDelegate() override {}
519 void OnQuery(int query_id,
520 const FormData& form,
521 const FormFieldData& field,
522 const gfx::RectF& bounds,
523 bool display_warning) override {
524 on_query_seen_ = true;
525 on_suggestions_returned_seen_ = false;
528 void OnSuggestionsReturned(
529 int query_id,
530 const std::vector<Suggestion>& suggestions) override {
531 on_suggestions_returned_seen_ = true;
532 query_id_ = query_id;
533 suggestions_ = suggestions;
536 void CheckSuggestions(int expected_page_id,
537 size_t expected_num_suggestions,
538 const Suggestion expected_suggestions[]) {
539 // Ensure that these results are from the most recent query.
540 EXPECT_TRUE(on_suggestions_returned_seen_);
542 EXPECT_EQ(expected_page_id, query_id_);
543 ASSERT_EQ(expected_num_suggestions, suggestions_.size());
544 for (size_t i = 0; i < expected_num_suggestions; ++i) {
545 SCOPED_TRACE(base::StringPrintf("i: %" PRIuS, i));
546 EXPECT_EQ(expected_suggestions[i].value, suggestions_[i].value);
547 EXPECT_EQ(expected_suggestions[i].label, suggestions_[i].label);
548 EXPECT_EQ(expected_suggestions[i].icon, suggestions_[i].icon);
549 EXPECT_EQ(expected_suggestions[i].frontend_id,
550 suggestions_[i].frontend_id);
554 // Wrappers around the above GetSuggestions call that take a hardcoded number
555 // of expected results so callsites are cleaner.
556 void CheckSuggestions(int expected_page_id,
557 const Suggestion& suggestion0) {
558 std::vector<Suggestion> suggestion_vector;
559 suggestion_vector.push_back(suggestion0);
560 CheckSuggestions(expected_page_id, 1, &suggestion_vector[0]);
562 void CheckSuggestions(int expected_page_id,
563 const Suggestion& suggestion0,
564 const Suggestion& suggestion1) {
565 std::vector<Suggestion> suggestion_vector;
566 suggestion_vector.push_back(suggestion0);
567 suggestion_vector.push_back(suggestion1);
568 CheckSuggestions(expected_page_id, 2, &suggestion_vector[0]);
570 void CheckSuggestions(int expected_page_id,
571 const Suggestion& suggestion0,
572 const Suggestion& suggestion1,
573 const Suggestion& suggestion2) {
574 std::vector<Suggestion> suggestion_vector;
575 suggestion_vector.push_back(suggestion0);
576 suggestion_vector.push_back(suggestion1);
577 suggestion_vector.push_back(suggestion2);
578 CheckSuggestions(expected_page_id, 3, &suggestion_vector[0]);
582 bool on_query_seen() const {
583 return on_query_seen_;
586 bool on_suggestions_returned_seen() const {
587 return on_suggestions_returned_seen_;
590 private:
591 // Records if OnQuery has been called yet.
592 bool on_query_seen_;
594 // Records if OnSuggestionsReturned has been called after the most recent
595 // call to OnQuery.
596 bool on_suggestions_returned_seen_;
598 // The query id of the most recent Autofill query.
599 int query_id_;
601 // The results returned by the most recent Autofill query.
602 std::vector<Suggestion> suggestions_;
604 DISALLOW_COPY_AND_ASSIGN(TestAutofillExternalDelegate);
607 } // namespace
609 class AutofillManagerTest : public testing::Test {
610 public:
611 void SetUp() override {
612 autofill_client_.SetPrefs(test::PrefServiceForTesting());
613 personal_data_.set_database(autofill_client_.GetDatabase());
614 personal_data_.SetPrefService(autofill_client_.GetPrefs());
615 autofill_driver_.reset(new MockAutofillDriver());
616 autofill_manager_.reset(new TestAutofillManager(
617 autofill_driver_.get(), &autofill_client_, &personal_data_));
619 external_delegate_.reset(new TestAutofillExternalDelegate(
620 autofill_manager_.get(),
621 autofill_driver_.get()));
622 autofill_manager_->SetExternalDelegate(external_delegate_.get());
625 void TearDown() override {
626 // Order of destruction is important as AutofillManager relies on
627 // PersonalDataManager to be around when it gets destroyed.
628 autofill_manager_.reset();
629 autofill_driver_.reset();
631 // Remove the AutofillWebDataService so TestPersonalDataManager does not
632 // need to care about removing self as an observer in destruction.
633 personal_data_.set_database(scoped_refptr<AutofillWebDataService>(NULL));
634 personal_data_.SetPrefService(NULL);
637 void GetAutofillSuggestions(int query_id,
638 const FormData& form,
639 const FormFieldData& field) {
640 autofill_manager_->OnQueryFormFieldAutofill(query_id,
641 form,
642 field,
643 gfx::Rect(),
644 false);
647 void GetAutofillSuggestions(const FormData& form,
648 const FormFieldData& field) {
649 GetAutofillSuggestions(kDefaultPageID, form, field);
652 void AutocompleteSuggestionsReturned(
653 const std::vector<base::string16>& result) {
654 autofill_manager_->autocomplete_history_manager_->SendSuggestions(&result);
657 void FormsSeen(const std::vector<FormData>& forms) {
658 autofill_manager_->OnFormsSeen(forms, base::TimeTicks());
661 void FormSubmitted(const FormData& form) {
662 autofill_manager_->ResetRunLoop();
663 if (autofill_manager_->OnFormSubmitted(form, base::TimeTicks::Now()))
664 autofill_manager_->WaitForAsyncFormSubmit();
667 void FillAutofillFormData(int query_id,
668 const FormData& form,
669 const FormFieldData& field,
670 int unique_id) {
671 autofill_manager_->FillOrPreviewForm(
672 AutofillDriver::FORM_DATA_ACTION_FILL, query_id, form, field,
673 unique_id);
676 // Calls |autofill_manager_->OnFillAutofillFormData()| with the specified
677 // input parameters after setting up the expectation that the mock driver's
678 // |SendFormDataToRenderer()| method will be called and saving the parameters
679 // of that call into the |response_query_id| and |response_data| output
680 // parameters.
681 void FillAutofillFormDataAndSaveResults(int input_query_id,
682 const FormData& input_form,
683 const FormFieldData& input_field,
684 int unique_id,
685 int* response_query_id,
686 FormData* response_data) {
687 EXPECT_CALL(*autofill_driver_, SendFormDataToRenderer(_, _, _)).
688 WillOnce((DoAll(testing::SaveArg<0>(response_query_id),
689 testing::SaveArg<2>(response_data))));
690 FillAutofillFormData(input_query_id, input_form, input_field, unique_id);
693 int MakeFrontendID(const SuggestionBackendID& cc_sid,
694 const SuggestionBackendID& profile_sid) const {
695 return autofill_manager_->MakeFrontendID(cc_sid, profile_sid);
698 protected:
699 base::MessageLoop message_loop_;
700 TestAutofillClient autofill_client_;
701 scoped_ptr<MockAutofillDriver> autofill_driver_;
702 scoped_ptr<TestAutofillManager> autofill_manager_;
703 scoped_ptr<TestAutofillExternalDelegate> external_delegate_;
704 TestPersonalDataManager personal_data_;
707 class TestFormStructure : public FormStructure {
708 public:
709 explicit TestFormStructure(const FormData& form)
710 : FormStructure(form) {}
711 ~TestFormStructure() override {}
713 void SetFieldTypes(const std::vector<ServerFieldType>& heuristic_types,
714 const std::vector<ServerFieldType>& server_types) {
715 ASSERT_EQ(field_count(), heuristic_types.size());
716 ASSERT_EQ(field_count(), server_types.size());
718 for (size_t i = 0; i < field_count(); ++i) {
719 AutofillField* form_field = field(i);
720 ASSERT_TRUE(form_field);
721 form_field->set_heuristic_type(heuristic_types[i]);
722 form_field->set_server_type(server_types[i]);
725 UpdateAutofillCount();
728 private:
729 DISALLOW_COPY_AND_ASSIGN(TestFormStructure);
732 // Test that we return all address profile suggestions when all form fields are
733 // empty.
734 TEST_F(AutofillManagerTest, GetProfileSuggestionsEmptyValue) {
735 // Set up our form data.
736 FormData form;
737 test::CreateTestAddressFormData(&form);
738 std::vector<FormData> forms(1, form);
739 FormsSeen(forms);
741 const FormFieldData& field = form.fields[0];
742 GetAutofillSuggestions(form, field);
744 // No suggestions provided, so send an empty vector as the results.
745 // This triggers the combined message send.
746 AutocompleteSuggestionsReturned(std::vector<base::string16>());
748 // Test that we sent the right values to the external delegate. Inferred
749 // labels include full first relevant field, which in this case is the
750 // address line 1.
751 external_delegate_->CheckSuggestions(
752 kDefaultPageID,
753 Suggestion("Elvis", "3734 Elvis Presley Blvd.", "", 1),
754 Suggestion("Charles", "123 Apple St.", "", 2));
757 // Test that we return only matching address profile suggestions when the
758 // selected form field has been partially filled out.
759 TEST_F(AutofillManagerTest, GetProfileSuggestionsMatchCharacter) {
760 // Set up our form data.
761 FormData form;
762 test::CreateTestAddressFormData(&form);
763 std::vector<FormData> forms(1, form);
764 FormsSeen(forms);
766 FormFieldData field;
767 test::CreateTestFormField("First Name", "firstname", "E", "text",&field);
768 GetAutofillSuggestions(form, field);
770 // No suggestions provided, so send an empty vector as the results.
771 // This triggers the combined message send.
772 AutocompleteSuggestionsReturned(std::vector<base::string16>());
774 // Test that we sent the right values to the external delegate.
775 external_delegate_->CheckSuggestions(
776 kDefaultPageID,
777 Suggestion("Elvis", "3734 Elvis Presley Blvd.", "", 1));
780 // Test that we return no suggestions when the form has no relevant fields.
781 TEST_F(AutofillManagerTest, GetProfileSuggestionsUnknownFields) {
782 // Set up our form data.
783 FormData form;
784 form.name = ASCIIToUTF16("MyForm");
785 form.origin = GURL("http://myform.com/form.html");
786 form.action = GURL("http://myform.com/submit.html");
787 form.user_submitted = true;
789 FormFieldData field;
790 test::CreateTestFormField("Username", "username", "", "text",&field);
791 form.fields.push_back(field);
792 test::CreateTestFormField("Password", "password", "", "password",&field);
793 form.fields.push_back(field);
794 test::CreateTestFormField("Quest", "quest", "", "quest", &field);
795 form.fields.push_back(field);
796 test::CreateTestFormField("Color", "color", "", "text", &field);
797 form.fields.push_back(field);
799 std::vector<FormData> forms(1, form);
800 FormsSeen(forms);
802 GetAutofillSuggestions(form, field);
803 EXPECT_FALSE(external_delegate_->on_suggestions_returned_seen());
806 // Test that we cull duplicate profile suggestions.
807 TEST_F(AutofillManagerTest, GetProfileSuggestionsWithDuplicates) {
808 // Set up our form data.
809 FormData form;
810 test::CreateTestAddressFormData(&form);
811 std::vector<FormData> forms(1, form);
812 FormsSeen(forms);
814 // Add a duplicate profile.
815 AutofillProfile* duplicate_profile =
816 new AutofillProfile(
817 *(autofill_manager_->GetProfileWithGUID(
818 "00000000-0000-0000-0000-000000000001")));
819 autofill_manager_->AddProfile(duplicate_profile);
821 const FormFieldData& field = form.fields[0];
822 GetAutofillSuggestions(form, field);
824 // No suggestions provided, so send an empty vector as the results.
825 // This triggers the combined message send.
826 AutocompleteSuggestionsReturned(std::vector<base::string16>());
828 // Test that we sent the right values to the external delegate.
829 external_delegate_->CheckSuggestions(
830 kDefaultPageID,
831 Suggestion("Elvis", "3734 Elvis Presley Blvd.", "", 1),
832 Suggestion("Charles", "123 Apple St.", "", 2));
835 // Test that we return no suggestions when autofill is disabled.
836 TEST_F(AutofillManagerTest, GetProfileSuggestionsAutofillDisabledByUser) {
837 // Set up our form data.
838 FormData form;
839 test::CreateTestAddressFormData(&form);
840 std::vector<FormData> forms(1, form);
841 FormsSeen(forms);
843 // Disable Autofill.
844 autofill_manager_->set_autofill_enabled(false);
846 const FormFieldData& field = form.fields[0];
847 GetAutofillSuggestions(form, field);
848 EXPECT_FALSE(external_delegate_->on_suggestions_returned_seen());
851 // Test that we return all credit card profile suggestions when all form fields
852 // are empty.
853 TEST_F(AutofillManagerTest, GetCreditCardSuggestionsEmptyValue) {
854 // Set up our form data.
855 FormData form;
856 CreateTestCreditCardFormData(&form, true, false);
857 std::vector<FormData> forms(1, form);
858 FormsSeen(forms);
860 FormFieldData field = form.fields[1];
861 GetAutofillSuggestions(form, field);
863 // No suggestions provided, so send an empty vector as the results.
864 // This triggers the combined message send.
865 AutocompleteSuggestionsReturned(std::vector<base::string16>());
867 // Test that we sent the right values to the external delegate.
868 external_delegate_->CheckSuggestions(
869 kDefaultPageID,
870 Suggestion("Visa - 3456", "04/12", kVisaCard,
871 autofill_manager_->GetPackedCreditCardID(4)),
872 Suggestion("MasterCard - 8765", "10/14", kMasterCard,
873 autofill_manager_->GetPackedCreditCardID(5)));
876 // Test that we return only matching credit card profile suggestions when the
877 // selected form field has been partially filled out.
878 TEST_F(AutofillManagerTest, GetCreditCardSuggestionsMatchCharacter) {
879 // Set up our form data.
880 FormData form;
881 CreateTestCreditCardFormData(&form, true, false);
882 std::vector<FormData> forms(1, form);
883 FormsSeen(forms);
885 FormFieldData field;
886 test::CreateTestFormField("Card Number", "cardnumber", "78", "text", &field);
887 GetAutofillSuggestions(form, field);
889 // No suggestions provided, so send an empty vector as the results.
890 // This triggers the combined message send.
891 AutocompleteSuggestionsReturned(std::vector<base::string16>());
893 // Test that we sent the right values to the external delegate.
894 external_delegate_->CheckSuggestions(
895 kDefaultPageID,
896 Suggestion("Visa - 3456", "04/12", kVisaCard,
897 autofill_manager_->GetPackedCreditCardID(4)));
900 // Test that we return credit card profile suggestions when the selected form
901 // field is not the credit card number field.
902 TEST_F(AutofillManagerTest, GetCreditCardSuggestionsNonCCNumber) {
903 // Set up our form data.
904 FormData form;
905 CreateTestCreditCardFormData(&form, true, false);
906 std::vector<FormData> forms(1, form);
907 FormsSeen(forms);
909 const FormFieldData& field = form.fields[0];
910 GetAutofillSuggestions(form, field);
912 // No suggestions provided, so send an empty vector as the results.
913 // This triggers the combined message send.
914 AutocompleteSuggestionsReturned(std::vector<base::string16>());
916 #if defined(OS_ANDROID)
917 static const char* kVisaSuggestion = "Visa - 3456";
918 static const char* kMcSuggestion = "MasterCard - 8765";
919 #else
920 static const char* kVisaSuggestion = "*3456";
921 static const char* kMcSuggestion = "*8765";
922 #endif
924 // Test that we sent the right values to the external delegate.
925 external_delegate_->CheckSuggestions(
926 kDefaultPageID,
927 Suggestion("Elvis Presley", kVisaSuggestion, kVisaCard,
928 autofill_manager_->GetPackedCreditCardID(4)),
929 Suggestion("Buddy Holly", kMcSuggestion, kMasterCard,
930 autofill_manager_->GetPackedCreditCardID(5)));
933 // Test that we return a warning explaining that credit card profile suggestions
934 // are unavailable when the form is not https.
935 TEST_F(AutofillManagerTest, GetCreditCardSuggestionsNonHTTPS) {
936 // Set up our form data.
937 FormData form;
938 CreateTestCreditCardFormData(&form, false, false);
939 std::vector<FormData> forms(1, form);
940 FormsSeen(forms);
942 const FormFieldData& field = form.fields[0];
943 GetAutofillSuggestions(form, field);
945 // No suggestions provided, so send an empty vector as the results.
946 // This triggers the combined message send.
947 AutocompleteSuggestionsReturned(std::vector<base::string16>());
949 // Test that we sent the right values to the external delegate.
950 external_delegate_->CheckSuggestions(
951 kDefaultPageID,
952 Suggestion(
953 l10n_util::GetStringUTF8(IDS_AUTOFILL_WARNING_INSECURE_CONNECTION),
954 "", "", -1));
956 // Now add some Autocomplete suggestions. We should show the autocomplete
957 // suggestions and the warning.
958 const int kPageID2 = 2;
959 GetAutofillSuggestions(kPageID2, form, field);
961 std::vector<base::string16> suggestions;
962 suggestions.push_back(ASCIIToUTF16("Jay"));
963 suggestions.push_back(ASCIIToUTF16("Jason"));
964 AutocompleteSuggestionsReturned(suggestions);
966 external_delegate_->CheckSuggestions(
967 kPageID2,
968 Suggestion(
969 l10n_util::GetStringUTF8(IDS_AUTOFILL_WARNING_INSECURE_CONNECTION),
970 "", "", -1),
971 Suggestion("Jay", "", "", 0),
972 Suggestion("Jason", "", "", 0));
974 // Clear the test credit cards and try again -- we shouldn't return a warning.
975 personal_data_.ClearCreditCards();
976 GetAutofillSuggestions(form, field);
977 EXPECT_FALSE(external_delegate_->on_suggestions_returned_seen());
980 // Test that we return all credit card suggestions in the case that two cards
981 // have the same obfuscated number.
982 TEST_F(AutofillManagerTest, GetCreditCardSuggestionsRepeatedObfuscatedNumber) {
983 // Add a credit card with the same obfuscated number as Elvis's.
984 // |credit_card| will be owned by the mock PersonalDataManager.
985 CreditCard* credit_card = new CreditCard;
986 test::SetCreditCardInfo(credit_card, "Elvis Presley",
987 "5231567890123456", // Mastercard
988 "05", "2012");
989 credit_card->set_guid("00000000-0000-0000-0000-000000000007");
990 autofill_manager_->AddCreditCard(credit_card);
992 // Set up our form data.
993 FormData form;
994 CreateTestCreditCardFormData(&form, true, false);
995 std::vector<FormData> forms(1, form);
996 FormsSeen(forms);
998 FormFieldData field = form.fields[1];
999 GetAutofillSuggestions(form, field);
1001 // No suggestions provided, so send an empty vector as the results.
1002 // This triggers the combined message send.
1003 AutocompleteSuggestionsReturned(std::vector<base::string16>());
1005 // Test that we sent the right values to the external delegate.
1006 external_delegate_->CheckSuggestions(
1007 kDefaultPageID,
1008 Suggestion("Visa - 3456", "04/12", kVisaCard,
1009 autofill_manager_->GetPackedCreditCardID(4)),
1010 Suggestion("MasterCard - 8765", "10/14", kMasterCard,
1011 autofill_manager_->GetPackedCreditCardID(5)),
1012 Suggestion("MasterCard - 3456", "05/12", kMasterCard,
1013 autofill_manager_->GetPackedCreditCardID(7)));
1016 // Test that we return profile and credit card suggestions for combined forms.
1017 TEST_F(AutofillManagerTest, GetAddressAndCreditCardSuggestions) {
1018 // Set up our form data.
1019 FormData form;
1020 test::CreateTestAddressFormData(&form);
1021 CreateTestCreditCardFormData(&form, true, false);
1022 std::vector<FormData> forms(1, form);
1023 FormsSeen(forms);
1025 FormFieldData field = form.fields[0];
1026 GetAutofillSuggestions(form, field);
1028 // No suggestions provided, so send an empty vector as the results.
1029 // This triggers the combined message send.
1030 AutocompleteSuggestionsReturned(std::vector<base::string16>());
1032 // Test that we sent the right address suggestions to the external delegate.
1033 external_delegate_->CheckSuggestions(
1034 kDefaultPageID,
1035 Suggestion("Elvis", "3734 Elvis Presley Blvd.", "", 1),
1036 Suggestion("Charles", "123 Apple St.", "", 2));
1038 const int kPageID2 = 2;
1039 test::CreateTestFormField("Card Number", "cardnumber", "", "text", &field);
1040 GetAutofillSuggestions(kPageID2, form, field);
1042 // No suggestions provided, so send an empty vector as the results.
1043 // This triggers the combined message send.
1044 AutocompleteSuggestionsReturned(std::vector<base::string16>());
1046 // Test that we sent the credit card suggestions to the external delegate.
1047 external_delegate_->CheckSuggestions(
1048 kPageID2,
1049 Suggestion("Visa - 3456", "04/12", kVisaCard,
1050 autofill_manager_->GetPackedCreditCardID(4)),
1051 Suggestion("MasterCard - 8765", "10/14", kMasterCard,
1052 autofill_manager_->GetPackedCreditCardID(5)));
1055 // Test that for non-https forms with both address and credit card fields, we
1056 // only return address suggestions. Instead of credit card suggestions, we
1057 // should return a warning explaining that credit card profile suggestions are
1058 // unavailable when the form is not https.
1059 TEST_F(AutofillManagerTest, GetAddressAndCreditCardSuggestionsNonHttps) {
1060 // Set up our form data.
1061 FormData form;
1062 test::CreateTestAddressFormData(&form);
1063 CreateTestCreditCardFormData(&form, false, false);
1064 std::vector<FormData> forms(1, form);
1065 FormsSeen(forms);
1067 FormFieldData field = form.fields[0];
1068 GetAutofillSuggestions(form, field);
1070 // No suggestions provided, so send an empty vector as the results.
1071 // This triggers the combined message send.
1072 AutocompleteSuggestionsReturned(std::vector<base::string16>());
1074 // Test that we sent the right suggestions to the external delegate.
1075 external_delegate_->CheckSuggestions(
1076 kDefaultPageID,
1077 Suggestion("Elvis", "3734 Elvis Presley Blvd.", "", 1),
1078 Suggestion("Charles", "123 Apple St.", "", 2));
1080 test::CreateTestFormField("Card Number", "cardnumber", "", "text", &field);
1081 const int kPageID2 = 2;
1082 GetAutofillSuggestions(kPageID2, form, field);
1084 // No suggestions provided, so send an empty vector as the results.
1085 // This triggers the combined message send.
1086 AutocompleteSuggestionsReturned(std::vector<base::string16>());
1088 // Test that we sent the right values to the external delegate.
1089 external_delegate_->CheckSuggestions(
1090 kPageID2,
1091 Suggestion(
1092 l10n_util::GetStringUTF8(IDS_AUTOFILL_WARNING_INSECURE_CONNECTION),
1093 "", "", -1));
1095 // Clear the test credit cards and try again -- we shouldn't return a warning.
1096 personal_data_.ClearCreditCards();
1097 GetAutofillSuggestions(form, field);
1098 EXPECT_FALSE(external_delegate_->on_suggestions_returned_seen());
1101 // Test that we correctly combine autofill and autocomplete suggestions.
1102 TEST_F(AutofillManagerTest, GetCombinedAutofillAndAutocompleteSuggestions) {
1103 // Set up our form data.
1104 FormData form;
1105 test::CreateTestAddressFormData(&form);
1106 std::vector<FormData> forms(1, form);
1107 FormsSeen(forms);
1109 const FormFieldData& field = form.fields[0];
1110 GetAutofillSuggestions(form, field);
1112 // Add some Autocomplete suggestions.
1113 // This triggers the combined message send.
1114 std::vector<base::string16> suggestions;
1115 suggestions.push_back(ASCIIToUTF16("Jay"));
1116 // This suggestion is a duplicate, and should be trimmed.
1117 suggestions.push_back(ASCIIToUTF16("Elvis"));
1118 suggestions.push_back(ASCIIToUTF16("Jason"));
1119 AutocompleteSuggestionsReturned(suggestions);
1121 // Test that we sent the right values to the external delegate.
1122 Suggestion expected[] = {
1123 Suggestion("Elvis", "3734 Elvis Presley Blvd.", "", 1),
1124 Suggestion("Charles", "123 Apple St.", "", 2),
1125 Suggestion("Jay", "", "", 0),
1126 Suggestion("Jason", "", "", 0),
1128 external_delegate_->CheckSuggestions(
1129 kDefaultPageID, arraysize(expected), expected);
1132 // Test that we return autocomplete-like suggestions when trying to autofill
1133 // already filled forms.
1134 TEST_F(AutofillManagerTest, GetFieldSuggestionsWhenFormIsAutofilled) {
1135 // Set up our form data.
1136 FormData form;
1137 test::CreateTestAddressFormData(&form);
1138 std::vector<FormData> forms(1, form);
1139 FormsSeen(forms);
1141 // Mark one of the fields as filled.
1142 form.fields[2].is_autofilled = true;
1143 const FormFieldData& field = form.fields[0];
1144 GetAutofillSuggestions(form, field);
1146 // No suggestions provided, so send an empty vector as the results.
1147 // This triggers the combined message send.
1148 AutocompleteSuggestionsReturned(std::vector<base::string16>());
1150 // Test that we sent the right values to the external delegate.
1151 external_delegate_->CheckSuggestions(
1152 kDefaultPageID,
1153 Suggestion("Elvis", "", "", 1),
1154 Suggestion("Charles", "", "", 2));
1157 // Test that nothing breaks when there are autocomplete suggestions but no
1158 // autofill suggestions.
1159 TEST_F(AutofillManagerTest, GetFieldSuggestionsForAutocompleteOnly) {
1160 // Set up our form data.
1161 FormData form;
1162 test::CreateTestAddressFormData(&form);
1163 FormFieldData field;
1164 test::CreateTestFormField("Some Field", "somefield", "", "text", &field);
1165 form.fields.push_back(field);
1166 std::vector<FormData> forms(1, form);
1167 FormsSeen(forms);
1169 GetAutofillSuggestions(form, field);
1171 // Add some Autocomplete suggestions.
1172 // This triggers the combined message send.
1173 std::vector<base::string16> suggestions;
1174 suggestions.push_back(ASCIIToUTF16("one"));
1175 suggestions.push_back(ASCIIToUTF16("two"));
1176 AutocompleteSuggestionsReturned(suggestions);
1178 // Test that we sent the right values to the external delegate.
1179 external_delegate_->CheckSuggestions(
1180 kDefaultPageID,
1181 Suggestion("one", "", "", 0),
1182 Suggestion("two", "", "", 0));
1185 // Test that we do not return duplicate values drawn from multiple profiles when
1186 // filling an already filled field.
1187 TEST_F(AutofillManagerTest, GetFieldSuggestionsWithDuplicateValues) {
1188 // Set up our form data.
1189 FormData form;
1190 test::CreateTestAddressFormData(&form);
1191 std::vector<FormData> forms(1, form);
1192 FormsSeen(forms);
1194 // |profile| will be owned by the mock PersonalDataManager.
1195 AutofillProfile* profile = new AutofillProfile;
1196 test::SetProfileInfo(
1197 profile, "Elvis", "", "", "", "", "", "", "", "", "", "", "");
1198 profile->set_guid("00000000-0000-0000-0000-000000000101");
1199 autofill_manager_->AddProfile(profile);
1201 FormFieldData& field = form.fields[0];
1202 field.is_autofilled = true;
1203 field.value = ASCIIToUTF16("Elvis");
1204 GetAutofillSuggestions(form, field);
1206 // No suggestions provided, so send an empty vector as the results.
1207 // This triggers the combined message send.
1208 AutocompleteSuggestionsReturned(std::vector<base::string16>());
1210 // Test that we sent the right values to the external delegate.
1211 external_delegate_->CheckSuggestions(
1212 kDefaultPageID,
1213 Suggestion("Elvis", "", "", 1));
1216 // Test that a non-default value is suggested for multi-valued profile, on an
1217 // unfilled form.
1218 TEST_F(AutofillManagerTest, GetFieldSuggestionsForMultiValuedProfileUnfilled) {
1219 // Set up our form data.
1220 FormData form;
1221 test::CreateTestAddressFormData(&form);
1222 std::vector<FormData> forms(1, form);
1223 FormsSeen(forms);
1225 // |profile| will be owned by the mock PersonalDataManager.
1226 AutofillProfile* profile = new AutofillProfile;
1227 test::SetProfileInfo(profile, "Elvis", "", "Presley", "me@x.com", "",
1228 "", "", "", "", "", "", "");
1229 profile->set_guid("00000000-0000-0000-0000-000000000101");
1230 std::vector<base::string16> multi_values(2);
1231 multi_values[0] = ASCIIToUTF16("Elvis");
1232 multi_values[1] = ASCIIToUTF16("Elena");
1233 profile->SetRawMultiInfo(NAME_FIRST, multi_values);
1234 multi_values[0] = ASCIIToUTF16("Presley");
1235 multi_values[1] = ASCIIToUTF16("Love");
1236 profile->SetRawMultiInfo(NAME_LAST, multi_values);
1237 personal_data_.ClearAutofillProfiles();
1238 autofill_manager_->AddProfile(profile);
1241 // Get the first name field.
1242 // Start out with "E", hoping for either "Elvis" or "Elena.
1243 FormFieldData& field = form.fields[0];
1244 field.value = ASCIIToUTF16("E");
1245 field.is_autofilled = false;
1246 GetAutofillSuggestions(form, field);
1248 // Trigger the |Send|.
1249 AutocompleteSuggestionsReturned(std::vector<base::string16>());
1251 // Test that we sent the right values to the external delegate.
1252 external_delegate_->CheckSuggestions(
1253 kDefaultPageID,
1254 Suggestion("Elvis", "me@x.com", "", 1),
1255 Suggestion("Elena", "me@x.com", "", 2));
1259 // Get the first name field.
1260 // This time, start out with "Ele", hoping for "Elena".
1261 FormFieldData& field = form.fields[0];
1262 field.value = ASCIIToUTF16("Ele");
1263 field.is_autofilled = false;
1264 GetAutofillSuggestions(form, field);
1266 // Trigger the |Send|.
1267 AutocompleteSuggestionsReturned(std::vector<base::string16>());
1269 // Test that we sent the right values to the external delegate.
1270 external_delegate_->CheckSuggestions(
1271 kDefaultPageID,
1272 Suggestion("Elena", "me@x.com", "", 2));
1276 // Test that all values are suggested for multi-valued profile, on a filled
1277 // form. This is the per-field "override" case.
1278 TEST_F(AutofillManagerTest, GetFieldSuggestionsForMultiValuedProfileFilled) {
1279 // Set up our form data.
1280 FormData form;
1281 test::CreateTestAddressFormData(&form);
1282 std::vector<FormData> forms(1, form);
1283 FormsSeen(forms);
1285 // |profile| will be owned by the mock PersonalDataManager.
1286 AutofillProfile* profile = new AutofillProfile;
1287 profile->set_guid("00000000-0000-0000-0000-000000000102");
1288 std::vector<base::string16> multi_values(3);
1289 multi_values[0] = ASCIIToUTF16("Travis");
1290 multi_values[1] = ASCIIToUTF16("Cynthia");
1291 multi_values[2] = ASCIIToUTF16("Zac");
1292 profile->SetRawMultiInfo(NAME_FIRST, multi_values);
1293 multi_values[0] = ASCIIToUTF16("Smith");
1294 multi_values[1] = ASCIIToUTF16("Love");
1295 multi_values[2] = ASCIIToUTF16("Mango");
1296 profile->SetRawMultiInfo(NAME_LAST, multi_values);
1297 autofill_manager_->AddProfile(profile);
1299 // Get the first name field. And start out with "Travis", hoping for all the
1300 // multi-valued variants as suggestions.
1301 FormFieldData& field = form.fields[0];
1302 field.value = ASCIIToUTF16("Travis");
1303 field.is_autofilled = true;
1304 GetAutofillSuggestions(form, field);
1306 // Trigger the |Send|.
1307 AutocompleteSuggestionsReturned(std::vector<base::string16>());
1309 // Test that we sent the right values to the external delegate.
1310 external_delegate_->CheckSuggestions(
1311 kDefaultPageID,
1312 Suggestion("Travis", "", "", 1),
1313 Suggestion("Cynthia", "", "", 2),
1314 Suggestion("Zac", "", "", 3));
1317 TEST_F(AutofillManagerTest, GetProfileSuggestionsFancyPhone) {
1318 // Set up our form data.
1319 FormData form;
1320 test::CreateTestAddressFormData(&form);
1321 std::vector<FormData> forms(1, form);
1322 FormsSeen(forms);
1324 AutofillProfile* profile = new AutofillProfile;
1325 profile->set_guid("00000000-0000-0000-0000-000000000103");
1326 profile->SetInfo(AutofillType(NAME_FULL), ASCIIToUTF16("Natty Bumppo"),
1327 "en-US");
1328 profile->SetRawInfo(PHONE_HOME_WHOLE_NUMBER,
1329 ASCIIToUTF16("1800PRAIRIE"));
1330 autofill_manager_->AddProfile(profile);
1332 const FormFieldData& field = form.fields[9];
1333 GetAutofillSuggestions(form, field);
1335 // No suggestions provided, so send an empty vector as the results.
1336 // This triggers the combined message send.
1337 AutocompleteSuggestionsReturned(std::vector<base::string16>());
1339 // Test that we sent the right values to the external delegate. Inferred
1340 // labels include full first relevant field, which in this case is the
1341 // address line 1.
1342 external_delegate_->CheckSuggestions(
1343 kDefaultPageID,
1344 Suggestion("12345678901", "Elvis Aaron Presley", "", 1),
1345 Suggestion("23456789012", "Charles Hardin Holley", "", 2),
1346 Suggestion("18007724743", "Natty Bumppo", "", 3)); // 1800PRAIRIE
1350 TEST_F(AutofillManagerTest, GetProfileSuggestionsForPhonePrefixOrSuffix) {
1351 // Set up our form data.
1352 FormData form;
1353 form.name = ASCIIToUTF16("MyForm");
1354 form.origin = GURL("http://myform.com/form.html");
1355 form.action = GURL("http://myform.com/submit.html");
1356 form.user_submitted = true;
1358 struct {
1359 const char* const label;
1360 const char* const name;
1361 size_t max_length;
1362 const char* const autocomplete_attribute;
1363 } test_fields[] = {{"country code", "country_code", 1, "tel-country-code"},
1364 {"area code", "area_code", 3, "tel-area-code"},
1365 {"phone", "phone_prefix", 3, "tel-local-prefix"},
1366 {"-", "phone_suffix", 4, "tel-local-suffix"},
1367 {"Phone Extension", "ext", 5, "tel-extension"}};
1369 FormFieldData field;
1370 for (size_t i = 0; i < arraysize(test_fields); ++i) {
1371 test::CreateTestFormField(
1372 test_fields[i].label, test_fields[i].name, "", "text", &field);
1373 field.max_length = test_fields[i].max_length;
1374 field.autocomplete_attribute = std::string();
1375 form.fields.push_back(field);
1378 std::vector<FormData> forms(1, form);
1379 FormsSeen(forms);
1381 AutofillProfile* profile = new AutofillProfile;
1382 profile->set_guid("00000000-0000-0000-0000-000000000104");
1383 std::vector<base::string16> multi_values(2);
1384 multi_values[0] = ASCIIToUTF16("1800FLOWERS");
1385 multi_values[1] = ASCIIToUTF16("14158889999");
1387 profile->SetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, multi_values);
1388 personal_data_.ClearAutofillProfiles();
1389 autofill_manager_->AddProfile(profile);
1391 const FormFieldData& phone_prefix = form.fields[2];
1392 GetAutofillSuggestions(form, phone_prefix);
1393 AutocompleteSuggestionsReturned(std::vector<base::string16>());
1394 // Test that we sent the right prefix values to the external delegate.
1395 external_delegate_->CheckSuggestions(
1396 kDefaultPageID,
1397 Suggestion("356", "1", "", 1),
1398 Suggestion("888", "1", "", 2));
1400 const FormFieldData& phone_suffix = form.fields[3];
1401 GetAutofillSuggestions(form, phone_suffix);
1402 AutocompleteSuggestionsReturned(std::vector<base::string16>());
1403 // Test that we sent the right suffix values to the external delegate.
1404 external_delegate_->CheckSuggestions(
1405 kDefaultPageID,
1406 Suggestion("9377", "1", "", 1),
1407 Suggestion("9999", "1", "", 2));
1410 // Test that we correctly fill an address form.
1411 TEST_F(AutofillManagerTest, FillAddressForm) {
1412 // Set up our form data.
1413 FormData form;
1414 test::CreateTestAddressFormData(&form);
1415 std::vector<FormData> forms(1, form);
1416 FormsSeen(forms);
1418 std::string guid("00000000-0000-0000-0000-000000000001");
1419 AutofillProfile* profile =
1420 autofill_manager_->GetProfileWithGUID(guid.c_str());
1421 ASSERT_TRUE(profile);
1422 EXPECT_EQ(0U, profile->use_count());
1423 EXPECT_EQ(base::Time(), profile->use_date());
1425 SuggestionBackendID profile_id(guid, 0);
1426 SuggestionBackendID card_id(std::string(), 0);
1427 int response_page_id = 0;
1428 FormData response_data;
1429 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0],
1430 MakeFrontendID(card_id, profile_id),
1431 &response_page_id, &response_data);
1432 ExpectFilledAddressFormElvis(
1433 response_page_id, response_data, kDefaultPageID, false);
1435 EXPECT_EQ(1U, profile->use_count());
1436 EXPECT_NE(base::Time(), profile->use_date());
1439 // Test that we correctly fill an address form from an auxiliary profile.
1440 TEST_F(AutofillManagerTest, FillAddressFormFromAuxiliaryProfile) {
1441 personal_data_.ClearAutofillProfiles();
1442 #if defined(OS_MACOSX) && !defined(OS_IOS)
1443 autofill_client_.GetPrefs()->SetBoolean(
1444 ::autofill::prefs::kAutofillUseMacAddressBook, true);
1445 #else
1446 autofill_client_.GetPrefs()->SetBoolean(
1447 ::autofill::prefs::kAutofillAuxiliaryProfilesEnabled, true);
1448 #endif // defined(OS_MACOSX) && !defined(OS_IOS)
1450 personal_data_.CreateTestAuxiliaryProfiles();
1452 // Set up our form data.
1453 FormData form;
1454 test::CreateTestAddressFormData(&form);
1455 std::vector<FormData> forms(1, form);
1456 FormsSeen(forms);
1458 SuggestionBackendID guid("00000000-0000-0000-0000-000000000001", 0);
1459 SuggestionBackendID empty(std::string(), 0);
1460 int response_page_id = 0;
1461 FormData response_data;
1462 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0],
1463 MakeFrontendID(empty, guid), &response_page_id, &response_data);
1464 ExpectFilledAddressFormElvis(
1465 response_page_id, response_data, kDefaultPageID, false);
1468 // Test that we correctly fill a credit card form.
1469 TEST_F(AutofillManagerTest, FillCreditCardForm) {
1470 // Set up our form data.
1471 FormData form;
1472 CreateTestCreditCardFormData(&form, true, false);
1473 std::vector<FormData> forms(1, form);
1474 FormsSeen(forms);
1476 SuggestionBackendID guid("00000000-0000-0000-0000-000000000004", 0);
1477 SuggestionBackendID empty(std::string(), 0);
1478 int response_page_id = 0;
1479 FormData response_data;
1480 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, *form.fields.begin(),
1481 MakeFrontendID(guid, empty), &response_page_id, &response_data);
1482 ExpectFilledCreditCardFormElvis(
1483 response_page_id, response_data, kDefaultPageID, false);
1486 // Test that we correctly fill a credit card form with month input type.
1487 // 1. year empty, month empty
1488 TEST_F(AutofillManagerTest, FillCreditCardFormNoYearNoMonth) {
1489 // Same as the SetUp(), but generate 4 credit cards with year month
1490 // combination.
1491 personal_data_.CreateTestCreditCardsYearAndMonth("", "");
1492 // Set up our form data.
1493 FormData form;
1494 CreateTestCreditCardFormData(&form, true, true);
1495 std::vector<FormData> forms(1, form);
1496 FormsSeen(forms);
1498 SuggestionBackendID guid("00000000-0000-0000-0000-000000000007", 0);
1499 SuggestionBackendID empty(std::string(), 0);
1500 int response_page_id = 0;
1501 FormData response_data;
1502 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, *form.fields.begin(),
1503 MakeFrontendID(guid, empty), &response_page_id, &response_data);
1504 ExpectFilledCreditCardYearMonthWithYearMonth(response_page_id, response_data,
1505 kDefaultPageID, false, "", "");
1509 // Test that we correctly fill a credit card form with month input type.
1510 // 2. year empty, month non-empty
1511 TEST_F(AutofillManagerTest, FillCreditCardFormNoYearMonth) {
1512 // Same as the SetUp(), but generate 4 credit cards with year month
1513 // combination.
1514 personal_data_.CreateTestCreditCardsYearAndMonth("", "04");
1515 // Set up our form data.
1516 FormData form;
1517 CreateTestCreditCardFormData(&form, true, true);
1518 std::vector<FormData> forms(1, form);
1519 FormsSeen(forms);
1521 SuggestionBackendID guid("00000000-0000-0000-0000-000000000007", 0);
1522 SuggestionBackendID empty(std::string(), 0);
1523 int response_page_id = 0;
1524 FormData response_data;
1525 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, *form.fields.begin(),
1526 MakeFrontendID(guid, empty), &response_page_id, &response_data);
1527 ExpectFilledCreditCardYearMonthWithYearMonth(response_page_id, response_data,
1528 kDefaultPageID, false, "", "04");
1531 // Test that we correctly fill a credit card form with month input type.
1532 // 3. year non-empty, month empty
1533 TEST_F(AutofillManagerTest, FillCreditCardFormYearNoMonth) {
1534 // Same as the SetUp(), but generate 4 credit cards with year month
1535 // combination.
1536 personal_data_.CreateTestCreditCardsYearAndMonth("2012", "");
1537 // Set up our form data.
1538 FormData form;
1539 CreateTestCreditCardFormData(&form, true, true);
1540 std::vector<FormData> forms(1, form);
1541 FormsSeen(forms);
1543 SuggestionBackendID guid("00000000-0000-0000-0000-000000000007", 0);
1544 SuggestionBackendID empty(std::string(), 0);
1545 int response_page_id = 0;
1546 FormData response_data;
1547 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, *form.fields.begin(),
1548 MakeFrontendID(guid, empty), &response_page_id, &response_data);
1549 ExpectFilledCreditCardYearMonthWithYearMonth(response_page_id, response_data,
1550 kDefaultPageID, false, "2012", "");
1553 // Test that we correctly fill a credit card form with month input type.
1554 // 4. year non-empty, month empty
1555 TEST_F(AutofillManagerTest, FillCreditCardFormYearMonth) {
1556 // Same as the SetUp(), but generate 4 credit cards with year month
1557 // combination.
1558 personal_data_.ClearCreditCards();
1559 personal_data_.CreateTestCreditCardsYearAndMonth("2012", "04");
1560 // Set up our form data.
1561 FormData form;
1562 CreateTestCreditCardFormData(&form, true, true);
1563 std::vector<FormData> forms(1, form);
1564 FormsSeen(forms);
1566 SuggestionBackendID guid("00000000-0000-0000-0000-000000000007", 0);
1567 SuggestionBackendID empty(std::string(), 0);
1568 int response_page_id = 0;
1569 FormData response_data;
1570 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, *form.fields.begin(),
1571 MakeFrontendID(guid, empty), &response_page_id, &response_data);
1572 ExpectFilledCreditCardYearMonthWithYearMonth(response_page_id, response_data,
1573 kDefaultPageID, false, "2012", "04");
1576 // Test that we correctly fill a combined address and credit card form.
1577 TEST_F(AutofillManagerTest, FillAddressAndCreditCardForm) {
1578 // Set up our form data.
1579 FormData form;
1580 test::CreateTestAddressFormData(&form);
1581 CreateTestCreditCardFormData(&form, true, false);
1582 std::vector<FormData> forms(1, form);
1583 FormsSeen(forms);
1585 // First fill the address data.
1586 SuggestionBackendID guid("00000000-0000-0000-0000-000000000001", 0);
1587 SuggestionBackendID empty(std::string(), 0);
1588 int response_page_id = 0;
1589 FormData response_data;
1591 SCOPED_TRACE("Address");
1592 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0],
1593 MakeFrontendID(empty, guid), &response_page_id, &response_data);
1594 ExpectFilledAddressFormElvis(
1595 response_page_id, response_data, kDefaultPageID, true);
1598 // Now fill the credit card data.
1599 const int kPageID2 = 2;
1600 SuggestionBackendID guid2("00000000-0000-0000-0000-000000000004", 0);
1601 response_page_id = 0;
1603 FillAutofillFormDataAndSaveResults(kPageID2, form, form.fields.back(),
1604 MakeFrontendID(guid2, empty), &response_page_id, &response_data);
1605 SCOPED_TRACE("Credit card");
1606 ExpectFilledCreditCardFormElvis(
1607 response_page_id, response_data, kPageID2, true);
1611 // Test that non-focusable field is ignored while inferring boundaries between
1612 // sections: http://crbug.com/231160
1613 TEST_F(AutofillManagerTest, FillFormWithNonFocusableFields) {
1614 // Create a form with both focusable and non-focusable fields.
1615 FormData form;
1616 form.name = ASCIIToUTF16("MyForm");
1617 form.origin = GURL("https://myform.com/form.html");
1618 form.action = GURL("https://myform.com/submit.html");
1619 form.user_submitted = true;
1621 FormFieldData field;
1623 test::CreateTestFormField("First Name", "firstname", "", "text", &field);
1624 form.fields.push_back(field);
1626 test::CreateTestFormField("", "lastname", "", "text", &field);
1627 form.fields.push_back(field);
1629 test::CreateTestFormField("", "email", "", "text", &field);
1630 form.fields.push_back(field);
1632 test::CreateTestFormField("Phone Number", "phonenumber", "", "tel", &field);
1633 form.fields.push_back(field);
1635 test::CreateTestFormField("", "email_", "", "text", &field);
1636 field.is_focusable = false;
1637 form.fields.push_back(field);
1639 test::CreateTestFormField("Country", "country", "", "text", &field);
1640 form.fields.push_back(field);
1642 std::vector<FormData> forms(1, form);
1643 FormsSeen(forms);
1645 // Fill the form
1646 SuggestionBackendID guid("00000000-0000-0000-0000-000000000001", 0);
1647 SuggestionBackendID empty(std::string(), 0);
1648 int response_page_id = 0;
1649 FormData response_data;
1650 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0],
1651 MakeFrontendID(empty, guid), &response_page_id, &response_data);
1653 // The whole form should be filled as all the fields belong to the same
1654 // logical section.
1655 ASSERT_EQ(6U, response_data.fields.size());
1656 ExpectFilledField("First Name", "firstname", "Elvis", "text",
1657 response_data.fields[0]);
1658 ExpectFilledField("", "lastname", "Presley", "text",
1659 response_data.fields[1]);
1660 ExpectFilledField("", "email", "theking@gmail.com", "text",
1661 response_data.fields[2]);
1662 ExpectFilledField("Phone Number", "phonenumber", "12345678901", "tel",
1663 response_data.fields[3]);
1664 ExpectFilledField("", "email_", "theking@gmail.com", "text",
1665 response_data.fields[4]);
1666 ExpectFilledField("Country", "country", "United States", "text",
1667 response_data.fields[5]);
1670 // Test that we correctly fill a form that has multiple logical sections, e.g.
1671 // both a billing and a shipping address.
1672 TEST_F(AutofillManagerTest, FillFormWithMultipleSections) {
1673 // Set up our form data.
1674 FormData form;
1675 test::CreateTestAddressFormData(&form);
1676 const size_t kAddressFormSize = form.fields.size();
1677 test::CreateTestAddressFormData(&form);
1678 for (size_t i = kAddressFormSize; i < form.fields.size(); ++i) {
1679 // Make sure the fields have distinct names.
1680 form.fields[i].name = form.fields[i].name + ASCIIToUTF16("_");
1682 std::vector<FormData> forms(1, form);
1683 FormsSeen(forms);
1685 // Fill the first section.
1686 SuggestionBackendID guid("00000000-0000-0000-0000-000000000001", 0);
1687 SuggestionBackendID empty(std::string(), 0);
1688 int response_page_id = 0;
1689 FormData response_data;
1690 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0],
1691 MakeFrontendID(empty, guid), &response_page_id, &response_data);
1693 SCOPED_TRACE("Address 1");
1694 // The second address section should be empty.
1695 ASSERT_EQ(response_data.fields.size(), 2*kAddressFormSize);
1696 for (size_t i = kAddressFormSize; i < form.fields.size(); ++i) {
1697 EXPECT_EQ(base::string16(), response_data.fields[i].value);
1700 // The first address section should be filled with Elvis's data.
1701 response_data.fields.resize(kAddressFormSize);
1702 ExpectFilledAddressFormElvis(
1703 response_page_id, response_data, kDefaultPageID, false);
1706 // Fill the second section, with the initiating field somewhere in the middle
1707 // of the section.
1708 const int kPageID2 = 2;
1709 SuggestionBackendID guid2("00000000-0000-0000-0000-000000000001", 0);
1710 ASSERT_LT(9U, kAddressFormSize);
1711 response_page_id = 0;
1712 FillAutofillFormDataAndSaveResults(
1713 kPageID2, form, form.fields[kAddressFormSize + 9],
1714 MakeFrontendID(empty, guid2), &response_page_id, &response_data);
1716 SCOPED_TRACE("Address 2");
1717 ASSERT_EQ(response_data.fields.size(), form.fields.size());
1719 // The first address section should be empty.
1720 ASSERT_EQ(response_data.fields.size(), 2*kAddressFormSize);
1721 for (size_t i = 0; i < kAddressFormSize; ++i) {
1722 EXPECT_EQ(base::string16(), response_data.fields[i].value);
1725 // The second address section should be filled with Elvis's data.
1726 FormData secondSection = response_data;
1727 secondSection.fields.erase(secondSection.fields.begin(),
1728 secondSection.fields.begin() + kAddressFormSize);
1729 for (size_t i = 0; i < kAddressFormSize; ++i) {
1730 // Restore the expected field names.
1731 base::string16 name = secondSection.fields[i].name;
1732 base::string16 original_name = name.substr(0, name.size() - 1);
1733 secondSection.fields[i].name = original_name;
1735 ExpectFilledAddressFormElvis(
1736 response_page_id, secondSection, kPageID2, false);
1740 // Test that we correctly fill a form that has author-specified sections, which
1741 // might not match our expected section breakdown.
1742 TEST_F(AutofillManagerTest, FillFormWithAuthorSpecifiedSections) {
1743 // Create a form with a billing section and an unnamed section, interleaved.
1744 // The billing section includes both address and credit card fields.
1745 FormData form;
1746 form.name = ASCIIToUTF16("MyForm");
1747 form.origin = GURL("https://myform.com/form.html");
1748 form.action = GURL("https://myform.com/submit.html");
1749 form.user_submitted = true;
1751 FormFieldData field;
1753 test::CreateTestFormField("", "country", "", "text", &field);
1754 field.autocomplete_attribute = "section-billing country";
1755 form.fields.push_back(field);
1757 test::CreateTestFormField("", "firstname", "", "text", &field);
1758 field.autocomplete_attribute = "given-name";
1759 form.fields.push_back(field);
1761 test::CreateTestFormField("", "lastname", "", "text", &field);
1762 field.autocomplete_attribute = "family-name";
1763 form.fields.push_back(field);
1765 test::CreateTestFormField("", "address", "", "text", &field);
1766 field.autocomplete_attribute = "section-billing address-line1";
1767 form.fields.push_back(field);
1769 test::CreateTestFormField("", "city", "", "text", &field);
1770 field.autocomplete_attribute = "section-billing locality";
1771 form.fields.push_back(field);
1773 test::CreateTestFormField("", "state", "", "text", &field);
1774 field.autocomplete_attribute = "section-billing region";
1775 form.fields.push_back(field);
1777 test::CreateTestFormField("", "zip", "", "text", &field);
1778 field.autocomplete_attribute = "section-billing postal-code";
1779 form.fields.push_back(field);
1781 test::CreateTestFormField("", "ccname", "", "text", &field);
1782 field.autocomplete_attribute = "section-billing cc-name";
1783 form.fields.push_back(field);
1785 test::CreateTestFormField("", "ccnumber", "", "text", &field);
1786 field.autocomplete_attribute = "section-billing cc-number";
1787 form.fields.push_back(field);
1789 test::CreateTestFormField("", "ccexp", "", "text", &field);
1790 field.autocomplete_attribute = "section-billing cc-exp";
1791 form.fields.push_back(field);
1793 test::CreateTestFormField("", "email", "", "text", &field);
1794 field.autocomplete_attribute = "email";
1795 form.fields.push_back(field);
1797 std::vector<FormData> forms(1, form);
1798 FormsSeen(forms);
1800 // Fill the unnamed section.
1801 SuggestionBackendID guid("00000000-0000-0000-0000-000000000001", 0);
1802 SuggestionBackendID empty(std::string(), 0);
1803 int response_page_id = 0;
1804 FormData response_data;
1805 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[1],
1806 MakeFrontendID(empty, guid), &response_page_id, &response_data);
1808 SCOPED_TRACE("Unnamed section");
1809 EXPECT_EQ(kDefaultPageID, response_page_id);
1810 EXPECT_EQ(ASCIIToUTF16("MyForm"), response_data.name);
1811 EXPECT_EQ(GURL("https://myform.com/form.html"), response_data.origin);
1812 EXPECT_EQ(GURL("https://myform.com/submit.html"), response_data.action);
1813 EXPECT_TRUE(response_data.user_submitted);
1814 ASSERT_EQ(11U, response_data.fields.size());
1816 ExpectFilledField("", "country", "", "text", response_data.fields[0]);
1817 ExpectFilledField("", "firstname", "Elvis", "text",
1818 response_data.fields[1]);
1819 ExpectFilledField("", "lastname", "Presley", "text",
1820 response_data.fields[2]);
1821 ExpectFilledField("", "address", "", "text", response_data.fields[3]);
1822 ExpectFilledField("", "city", "", "text", response_data.fields[4]);
1823 ExpectFilledField("", "state", "", "text", response_data.fields[5]);
1824 ExpectFilledField("", "zip", "", "text", response_data.fields[6]);
1825 ExpectFilledField("", "ccname", "", "text", response_data.fields[7]);
1826 ExpectFilledField("", "ccnumber", "", "text", response_data.fields[8]);
1827 ExpectFilledField("", "ccexp", "", "text", response_data.fields[9]);
1828 ExpectFilledField("", "email", "theking@gmail.com", "text",
1829 response_data.fields[10]);
1832 // Fill the address portion of the billing section.
1833 const int kPageID2 = 2;
1834 SuggestionBackendID guid2("00000000-0000-0000-0000-000000000001", 0);
1835 response_page_id = 0;
1836 FillAutofillFormDataAndSaveResults(kPageID2, form, form.fields[0],
1837 MakeFrontendID(empty, guid2), &response_page_id, &response_data);
1839 SCOPED_TRACE("Billing address");
1840 EXPECT_EQ(kPageID2, response_page_id);
1841 EXPECT_EQ(ASCIIToUTF16("MyForm"), response_data.name);
1842 EXPECT_EQ(GURL("https://myform.com/form.html"), response_data.origin);
1843 EXPECT_EQ(GURL("https://myform.com/submit.html"), response_data.action);
1844 EXPECT_TRUE(response_data.user_submitted);
1845 ASSERT_EQ(11U, response_data.fields.size());
1847 ExpectFilledField("", "country", "US", "text",
1848 response_data.fields[0]);
1849 ExpectFilledField("", "firstname", "", "text", response_data.fields[1]);
1850 ExpectFilledField("", "lastname", "", "text", response_data.fields[2]);
1851 ExpectFilledField("", "address", "3734 Elvis Presley Blvd.", "text",
1852 response_data.fields[3]);
1853 ExpectFilledField("", "city", "Memphis", "text", response_data.fields[4]);
1854 ExpectFilledField("", "state", "Tennessee", "text",
1855 response_data.fields[5]);
1856 ExpectFilledField("", "zip", "38116", "text", response_data.fields[6]);
1857 ExpectFilledField("", "ccname", "", "text", response_data.fields[7]);
1858 ExpectFilledField("", "ccnumber", "", "text", response_data.fields[8]);
1859 ExpectFilledField("", "ccexp", "", "text", response_data.fields[9]);
1860 ExpectFilledField("", "email", "", "text", response_data.fields[10]);
1863 // Fill the credit card portion of the billing section.
1864 const int kPageID3 = 3;
1865 SuggestionBackendID guid3("00000000-0000-0000-0000-000000000004", 0);
1866 response_page_id = 0;
1867 FillAutofillFormDataAndSaveResults(
1868 kPageID3, form, form.fields[form.fields.size() - 2],
1869 MakeFrontendID(guid3, empty), &response_page_id, &response_data);
1871 SCOPED_TRACE("Credit card");
1872 EXPECT_EQ(kPageID3, response_page_id);
1873 EXPECT_EQ(ASCIIToUTF16("MyForm"), response_data.name);
1874 EXPECT_EQ(GURL("https://myform.com/form.html"), response_data.origin);
1875 EXPECT_EQ(GURL("https://myform.com/submit.html"), response_data.action);
1876 EXPECT_TRUE(response_data.user_submitted);
1877 ASSERT_EQ(11U, response_data.fields.size());
1879 ExpectFilledField("", "country", "", "text", response_data.fields[0]);
1880 ExpectFilledField("", "firstname", "", "text", response_data.fields[1]);
1881 ExpectFilledField("", "lastname", "", "text", response_data.fields[2]);
1882 ExpectFilledField("", "address", "", "text", response_data.fields[3]);
1883 ExpectFilledField("", "city", "", "text", response_data.fields[4]);
1884 ExpectFilledField("", "state", "", "text", response_data.fields[5]);
1885 ExpectFilledField("", "zip", "", "text", response_data.fields[6]);
1886 ExpectFilledField("", "ccname", "Elvis Presley", "text",
1887 response_data.fields[7]);
1888 ExpectFilledField("", "ccnumber", "4234567890123456", "text",
1889 response_data.fields[8]);
1890 ExpectFilledField("", "ccexp", "04/2012", "text", response_data.fields[9]);
1891 ExpectFilledField("", "email", "", "text", response_data.fields[10]);
1895 // Test that we correctly fill a form that has a single logical section with
1896 // multiple email address fields.
1897 TEST_F(AutofillManagerTest, FillFormWithMultipleEmails) {
1898 // Set up our form data.
1899 FormData form;
1900 test::CreateTestAddressFormData(&form);
1901 FormFieldData field;
1902 test::CreateTestFormField("Confirm email", "email2", "", "text", &field);
1903 form.fields.push_back(field);
1905 std::vector<FormData> forms(1, form);
1906 FormsSeen(forms);
1908 // Fill the form.
1909 SuggestionBackendID guid("00000000-0000-0000-0000-000000000001", 0);
1910 SuggestionBackendID empty(std::string(), 0);
1911 int response_page_id = 0;
1912 FormData response_data;
1913 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0],
1914 MakeFrontendID(empty, guid), &response_page_id, &response_data);
1916 // The second email address should be filled.
1917 EXPECT_EQ(ASCIIToUTF16("theking@gmail.com"),
1918 response_data.fields.back().value);
1920 // The remainder of the form should be filled as usual.
1921 response_data.fields.pop_back();
1922 ExpectFilledAddressFormElvis(
1923 response_page_id, response_data, kDefaultPageID, false);
1926 // Test that we correctly fill a previously auto-filled form.
1927 TEST_F(AutofillManagerTest, FillAutofilledForm) {
1928 // Set up our form data.
1929 FormData form;
1930 test::CreateTestAddressFormData(&form);
1931 // Mark one of the address fields as autofilled.
1932 form.fields[4].is_autofilled = true;
1933 CreateTestCreditCardFormData(&form, true, false);
1934 std::vector<FormData> forms(1, form);
1935 FormsSeen(forms);
1937 // First fill the address data.
1938 SuggestionBackendID guid("00000000-0000-0000-0000-000000000001", 0);
1939 SuggestionBackendID empty(std::string(), 0);
1940 int response_page_id = 0;
1941 FormData response_data;
1942 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, *form.fields.begin(),
1943 MakeFrontendID(empty, guid), &response_page_id, &response_data);
1945 SCOPED_TRACE("Address");
1946 ExpectFilledForm(response_page_id, response_data, kDefaultPageID,
1947 "Elvis", "", "", "", "", "", "", "", "", "", "", "", "",
1948 "", "", true, true, false);
1951 // Now fill the credit card data.
1952 const int kPageID2 = 2;
1953 SuggestionBackendID guid2("00000000-0000-0000-0000-000000000004", 0);
1954 response_page_id = 0;
1955 FillAutofillFormDataAndSaveResults(kPageID2, form, form.fields.back(),
1956 MakeFrontendID(guid2, empty), &response_page_id, &response_data);
1958 SCOPED_TRACE("Credit card 1");
1959 ExpectFilledCreditCardFormElvis(
1960 response_page_id, response_data, kPageID2, true);
1963 // Now set the credit card fields to also be auto-filled, and try again to
1964 // fill the credit card data
1965 for (std::vector<FormFieldData>::iterator iter = form.fields.begin();
1966 iter != form.fields.end();
1967 ++iter) {
1968 iter->is_autofilled = true;
1971 const int kPageID3 = 3;
1972 response_page_id = 0;
1973 FillAutofillFormDataAndSaveResults(kPageID3, form, *form.fields.rbegin(),
1974 MakeFrontendID(guid2, empty), &response_page_id, &response_data);
1976 SCOPED_TRACE("Credit card 2");
1977 ExpectFilledForm(response_page_id, response_data, kPageID3,
1978 "", "", "", "", "", "", "", "", "", "", "", "", "", "",
1979 "2012", true, true, false);
1983 // Test that we correctly fill an address form with a non-default variant for a
1984 // multi-valued field.
1985 TEST_F(AutofillManagerTest, FillAddressFormWithVariantType) {
1986 // Set up our form data.
1987 FormData form;
1988 test::CreateTestAddressFormData(&form);
1989 std::vector<FormData> forms(1, form);
1990 FormsSeen(forms);
1992 // Add a name variant to the Elvis profile.
1993 AutofillProfile* profile = autofill_manager_->GetProfileWithGUID(
1994 "00000000-0000-0000-0000-000000000001");
1996 std::vector<base::string16> name_variants;
1997 name_variants.push_back(ASCIIToUTF16("Some"));
1998 name_variants.push_back(profile->GetRawInfo(NAME_FIRST));
1999 profile->SetRawMultiInfo(NAME_FIRST, name_variants);
2001 name_variants.clear();
2002 name_variants.push_back(ASCIIToUTF16("Other"));
2003 name_variants.push_back(profile->GetRawInfo(NAME_MIDDLE));
2004 profile->SetRawMultiInfo(NAME_MIDDLE, name_variants);
2006 name_variants.clear();
2007 name_variants.push_back(ASCIIToUTF16("Guy"));
2008 name_variants.push_back(profile->GetRawInfo(NAME_LAST));
2009 profile->SetRawMultiInfo(NAME_LAST, name_variants);
2011 SuggestionBackendID guid(profile->guid(), 1);
2012 SuggestionBackendID empty(std::string(), 0);
2013 int response_page_id = 0;
2014 FormData response_data1;
2015 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0],
2016 MakeFrontendID(empty, guid), &response_page_id, &response_data1);
2018 SCOPED_TRACE("Valid variant");
2019 ExpectFilledAddressFormElvis(
2020 response_page_id, response_data1, kDefaultPageID, false);
2023 // Try filling with a variant that doesn't exist. The fields to which this
2024 // variant would normally apply should not be filled.
2025 const int kPageID2 = 2;
2026 SuggestionBackendID guid2(profile->guid(), 2);
2027 response_page_id = 0;
2028 FormData response_data2;
2029 FillAutofillFormDataAndSaveResults(kPageID2, form, form.fields[0],
2030 MakeFrontendID(empty, guid2), &response_page_id, &response_data2);
2032 SCOPED_TRACE("Invalid variant");
2033 ExpectFilledForm(response_page_id, response_data2, kPageID2, "", "", "",
2034 "3734 Elvis Presley Blvd.", "Apt. 10", "Memphis",
2035 "Tennessee", "38116", "United States", "12345678901",
2036 "theking@gmail.com", "", "", "", "", true, false, false);
2040 // Test that we correctly fill a phone number split across multiple fields.
2041 TEST_F(AutofillManagerTest, FillPhoneNumber) {
2042 // In one form, rely on the maxlength attribute to imply phone number parts.
2043 // In the other form, rely on the autocompletetype attribute.
2044 FormData form_with_maxlength;
2045 form_with_maxlength.name = ASCIIToUTF16("MyMaxlengthPhoneForm");
2046 form_with_maxlength.origin = GURL("http://myform.com/phone_form.html");
2047 form_with_maxlength.action = GURL("http://myform.com/phone_submit.html");
2048 form_with_maxlength.user_submitted = true;
2049 FormData form_with_autocompletetype = form_with_maxlength;
2050 form_with_autocompletetype.name = ASCIIToUTF16("MyAutocompletetypePhoneForm");
2052 struct {
2053 const char* label;
2054 const char* name;
2055 size_t max_length;
2056 const char* autocomplete_attribute;
2057 } test_fields[] = {
2058 { "country code", "country_code", 1, "tel-country-code" },
2059 { "area code", "area_code", 3, "tel-area-code" },
2060 { "phone", "phone_prefix", 3, "tel-local-prefix" },
2061 { "-", "phone_suffix", 4, "tel-local-suffix" },
2062 { "Phone Extension", "ext", 3, "tel-extension" }
2065 FormFieldData field;
2066 const size_t default_max_length = field.max_length;
2067 for (size_t i = 0; i < arraysize(test_fields); ++i) {
2068 test::CreateTestFormField(
2069 test_fields[i].label, test_fields[i].name, "", "text", &field);
2070 field.max_length = test_fields[i].max_length;
2071 field.autocomplete_attribute = std::string();
2072 form_with_maxlength.fields.push_back(field);
2074 field.max_length = default_max_length;
2075 field.autocomplete_attribute = test_fields[i].autocomplete_attribute;
2076 form_with_autocompletetype.fields.push_back(field);
2079 std::vector<FormData> forms;
2080 forms.push_back(form_with_maxlength);
2081 forms.push_back(form_with_autocompletetype);
2082 FormsSeen(forms);
2084 // We should be able to fill prefix and suffix fields for US numbers.
2085 AutofillProfile* work_profile = autofill_manager_->GetProfileWithGUID(
2086 "00000000-0000-0000-0000-000000000002");
2087 ASSERT_TRUE(work_profile != NULL);
2088 work_profile->SetRawInfo(PHONE_HOME_WHOLE_NUMBER,
2089 ASCIIToUTF16("16505554567"));
2091 SuggestionBackendID guid(work_profile->guid(), 0);
2092 SuggestionBackendID empty(std::string(), 0);
2093 int page_id = 1;
2094 int response_page_id = 0;
2095 FormData response_data1;
2096 FillAutofillFormDataAndSaveResults(page_id, form_with_maxlength,
2097 *form_with_maxlength.fields.begin(),
2098 MakeFrontendID(empty, guid), &response_page_id, &response_data1);
2099 EXPECT_EQ(1, response_page_id);
2101 ASSERT_EQ(5U, response_data1.fields.size());
2102 EXPECT_EQ(ASCIIToUTF16("1"), response_data1.fields[0].value);
2103 EXPECT_EQ(ASCIIToUTF16("650"), response_data1.fields[1].value);
2104 EXPECT_EQ(ASCIIToUTF16("555"), response_data1.fields[2].value);
2105 EXPECT_EQ(ASCIIToUTF16("4567"), response_data1.fields[3].value);
2106 EXPECT_EQ(base::string16(), response_data1.fields[4].value);
2108 page_id = 2;
2109 response_page_id = 0;
2110 FormData response_data2;
2111 FillAutofillFormDataAndSaveResults(page_id, form_with_autocompletetype,
2112 *form_with_autocompletetype.fields.begin(),
2113 MakeFrontendID(empty, guid), &response_page_id, &response_data2);
2114 EXPECT_EQ(2, response_page_id);
2116 ASSERT_EQ(5U, response_data2.fields.size());
2117 EXPECT_EQ(ASCIIToUTF16("1"), response_data2.fields[0].value);
2118 EXPECT_EQ(ASCIIToUTF16("650"), response_data2.fields[1].value);
2119 EXPECT_EQ(ASCIIToUTF16("555"), response_data2.fields[2].value);
2120 EXPECT_EQ(ASCIIToUTF16("4567"), response_data2.fields[3].value);
2121 EXPECT_EQ(base::string16(), response_data2.fields[4].value);
2123 // We should not be able to fill prefix and suffix fields for international
2124 // numbers.
2125 work_profile->SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("GB"));
2126 work_profile->SetRawInfo(PHONE_HOME_WHOLE_NUMBER,
2127 ASCIIToUTF16("447700954321"));
2128 page_id = 3;
2129 response_page_id = 0;
2130 FormData response_data3;
2131 FillAutofillFormDataAndSaveResults(page_id, form_with_maxlength,
2132 *form_with_maxlength.fields.begin(),
2133 MakeFrontendID(empty, guid), &response_page_id, &response_data3);
2134 EXPECT_EQ(3, response_page_id);
2136 ASSERT_EQ(5U, response_data3.fields.size());
2137 EXPECT_EQ(ASCIIToUTF16("44"), response_data3.fields[0].value);
2138 EXPECT_EQ(ASCIIToUTF16("7700"), response_data3.fields[1].value);
2139 EXPECT_EQ(ASCIIToUTF16("954321"), response_data3.fields[2].value);
2140 EXPECT_EQ(ASCIIToUTF16("954321"), response_data3.fields[3].value);
2141 EXPECT_EQ(base::string16(), response_data3.fields[4].value);
2143 page_id = 4;
2144 response_page_id = 0;
2145 FormData response_data4;
2146 FillAutofillFormDataAndSaveResults(page_id, form_with_autocompletetype,
2147 *form_with_autocompletetype.fields.begin(),
2148 MakeFrontendID(empty, guid), &response_page_id, &response_data4);
2149 EXPECT_EQ(4, response_page_id);
2151 ASSERT_EQ(5U, response_data4.fields.size());
2152 EXPECT_EQ(ASCIIToUTF16("44"), response_data4.fields[0].value);
2153 EXPECT_EQ(ASCIIToUTF16("7700"), response_data4.fields[1].value);
2154 EXPECT_EQ(ASCIIToUTF16("954321"), response_data4.fields[2].value);
2155 EXPECT_EQ(ASCIIToUTF16("954321"), response_data4.fields[3].value);
2156 EXPECT_EQ(base::string16(), response_data4.fields[4].value);
2158 // We should fill all phone fields with the same phone number variant.
2159 std::vector<base::string16> phone_variants;
2160 phone_variants.push_back(ASCIIToUTF16("16505554567"));
2161 phone_variants.push_back(ASCIIToUTF16("18887771234"));
2162 work_profile->SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"));
2163 work_profile->SetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, phone_variants);
2165 page_id = 5;
2166 response_page_id = 0;
2167 FormData response_data5;
2168 SuggestionBackendID variant_guid(work_profile->guid(), 1);
2169 FillAutofillFormDataAndSaveResults(page_id, form_with_maxlength,
2170 *form_with_maxlength.fields.begin(),
2171 MakeFrontendID(empty, variant_guid), &response_page_id, &response_data5);
2172 EXPECT_EQ(5, response_page_id);
2174 ASSERT_EQ(5U, response_data5.fields.size());
2175 EXPECT_EQ(ASCIIToUTF16("1"), response_data5.fields[0].value);
2176 EXPECT_EQ(ASCIIToUTF16("888"), response_data5.fields[1].value);
2177 EXPECT_EQ(ASCIIToUTF16("777"), response_data5.fields[2].value);
2178 EXPECT_EQ(ASCIIToUTF16("1234"), response_data5.fields[3].value);
2179 EXPECT_EQ(base::string16(), response_data5.fields[4].value);
2182 // Test that we can still fill a form when a field has been removed from it.
2183 TEST_F(AutofillManagerTest, FormChangesRemoveField) {
2184 // Set up our form data.
2185 FormData form;
2186 test::CreateTestAddressFormData(&form);
2188 // Add a field -- we'll remove it again later.
2189 FormFieldData field;
2190 test::CreateTestFormField("Some", "field", "", "text", &field);
2191 form.fields.insert(form.fields.begin() + 3, field);
2193 std::vector<FormData> forms(1, form);
2194 FormsSeen(forms);
2196 // Now, after the call to |FormsSeen|, we remove the field before filling.
2197 form.fields.erase(form.fields.begin() + 3);
2199 SuggestionBackendID guid("00000000-0000-0000-0000-000000000001", 0);
2200 SuggestionBackendID empty(std::string(), 0);
2201 int response_page_id = 0;
2202 FormData response_data;
2203 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0],
2204 MakeFrontendID(empty, guid), &response_page_id, &response_data);
2205 ExpectFilledAddressFormElvis(
2206 response_page_id, response_data, kDefaultPageID, false);
2209 // Test that we can still fill a form when a field has been added to it.
2210 TEST_F(AutofillManagerTest, FormChangesAddField) {
2211 // The offset of the phone field in the address form.
2212 const int kPhoneFieldOffset = 9;
2214 // Set up our form data.
2215 FormData form;
2216 test::CreateTestAddressFormData(&form);
2218 // Remove the phone field -- we'll add it back later.
2219 std::vector<FormFieldData>::iterator pos =
2220 form.fields.begin() + kPhoneFieldOffset;
2221 FormFieldData field = *pos;
2222 pos = form.fields.erase(pos);
2224 std::vector<FormData> forms(1, form);
2225 FormsSeen(forms);
2227 // Now, after the call to |FormsSeen|, we restore the field before filling.
2228 form.fields.insert(pos, field);
2230 SuggestionBackendID guid("00000000-0000-0000-0000-000000000001", 0);
2231 SuggestionBackendID empty(std::string(), 0);
2232 int response_page_id = 0;
2233 FormData response_data;
2234 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0],
2235 MakeFrontendID(empty, guid), &response_page_id, &response_data);
2236 ExpectFilledAddressFormElvis(
2237 response_page_id, response_data, kDefaultPageID, false);
2240 // Test that we are able to save form data when forms are submitted.
2241 TEST_F(AutofillManagerTest, FormSubmitted) {
2242 // Set up our form data.
2243 FormData form;
2244 test::CreateTestAddressFormData(&form);
2245 std::vector<FormData> forms(1, form);
2246 FormsSeen(forms);
2248 // Fill the form.
2249 SuggestionBackendID guid("00000000-0000-0000-0000-000000000001", 0);
2250 SuggestionBackendID empty(std::string(), 0);
2251 int response_page_id = 0;
2252 FormData response_data;
2253 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0],
2254 MakeFrontendID(empty, guid), &response_page_id, &response_data);
2255 ExpectFilledAddressFormElvis(
2256 response_page_id, response_data, kDefaultPageID, false);
2258 // Simulate form submission. We should call into the PDM to try to save the
2259 // filled data.
2260 EXPECT_CALL(personal_data_, SaveImportedProfile(::testing::_)).Times(1);
2261 FormSubmitted(response_data);
2264 // Test that when Autocomplete is enabled and Autofill is disabled,
2265 // form submissions are still received by AutocompleteHistoryManager.
2266 TEST_F(AutofillManagerTest, FormSubmittedAutocompleteEnabled) {
2267 TestAutofillClient client;
2268 autofill_manager_.reset(
2269 new TestAutofillManager(autofill_driver_.get(), &client, NULL));
2270 autofill_manager_->set_autofill_enabled(false);
2271 scoped_ptr<MockAutocompleteHistoryManager> autocomplete_history_manager;
2272 autocomplete_history_manager.reset(
2273 new MockAutocompleteHistoryManager(autofill_driver_.get(), &client));
2274 autofill_manager_->autocomplete_history_manager_ =
2275 autocomplete_history_manager.Pass();
2277 // Set up our form data.
2278 FormData form;
2279 test::CreateTestAddressFormData(&form);
2280 MockAutocompleteHistoryManager* m = static_cast<
2281 MockAutocompleteHistoryManager*>(
2282 autofill_manager_->autocomplete_history_manager_.get());
2283 EXPECT_CALL(*m,
2284 OnFormSubmitted(_)).Times(1);
2285 FormSubmitted(form);
2288 // Test that when Autocomplete is enabled and Autofill is disabled,
2289 // Autocomplete suggestions are still received.
2290 TEST_F(AutofillManagerTest, AutocompleteSuggestionsWhenAutofillDisabled) {
2291 TestAutofillClient client;
2292 autofill_manager_.reset(
2293 new TestAutofillManager(autofill_driver_.get(), &client, NULL));
2294 autofill_manager_->set_autofill_enabled(false);
2295 autofill_manager_->SetExternalDelegate(external_delegate_.get());
2297 // Set up our form data.
2298 FormData form;
2299 test::CreateTestAddressFormData(&form);
2300 std::vector<FormData> forms(1, form);
2301 FormsSeen(forms);
2302 const FormFieldData& field = form.fields[0];
2303 GetAutofillSuggestions(form, field);
2305 // Add some Autocomplete suggestions. We should return the autocomplete
2306 // suggestions, these will be culled by the renderer.
2307 std::vector<base::string16> suggestions;
2308 suggestions.push_back(ASCIIToUTF16("Jay"));
2309 suggestions.push_back(ASCIIToUTF16("Jason"));
2310 AutocompleteSuggestionsReturned(suggestions);
2312 external_delegate_->CheckSuggestions(
2313 kDefaultPageID,
2314 Suggestion("Jay", "", "", 0),
2315 Suggestion("Jason", "", "", 0));
2318 // Duplicate of the below test with the respect-autocomplete-off-autofill
2319 // switch.
2320 TEST_F(AutofillManagerTest, AutocompleteOffRespected) {
2321 base::CommandLine::ForCurrentProcess()->AppendSwitch(
2322 switches::kRespectAutocompleteOffForAutofill);
2324 TestAutofillClient client;
2325 autofill_manager_.reset(
2326 new TestAutofillManager(autofill_driver_.get(), &client, NULL));
2327 autofill_manager_->set_autofill_enabled(false);
2328 autofill_manager_->SetExternalDelegate(external_delegate_.get());
2330 scoped_ptr<MockAutocompleteHistoryManager> autocomplete_history_manager;
2331 autocomplete_history_manager.reset(
2332 new MockAutocompleteHistoryManager(autofill_driver_.get(), &client));
2333 autofill_manager_->autocomplete_history_manager_ =
2334 autocomplete_history_manager.Pass();
2335 MockAutocompleteHistoryManager* m = static_cast<
2336 MockAutocompleteHistoryManager*>(
2337 autofill_manager_->autocomplete_history_manager_.get());
2338 EXPECT_CALL(*m,
2339 OnGetAutocompleteSuggestions(_, _, _, _, _)).Times(0);
2341 // Set up our form data.
2342 FormData form;
2343 test::CreateTestAddressFormData(&form);
2344 std::vector<FormData> forms(1, form);
2345 FormsSeen(forms);
2346 FormFieldData* field = &form.fields[0];
2347 field->should_autocomplete = false;
2348 GetAutofillSuggestions(form, *field);
2351 TEST_F(AutofillManagerTest, AutocompleteOffRespectedWithFlag) {
2352 TestAutofillClient client;
2353 autofill_manager_.reset(
2354 new TestAutofillManager(autofill_driver_.get(), &client, NULL));
2355 autofill_manager_->set_autofill_enabled(false);
2356 autofill_manager_->SetExternalDelegate(external_delegate_.get());
2358 scoped_ptr<MockAutocompleteHistoryManager> autocomplete_history_manager;
2359 autocomplete_history_manager.reset(
2360 new MockAutocompleteHistoryManager(autofill_driver_.get(), &client));
2361 autofill_manager_->autocomplete_history_manager_ =
2362 autocomplete_history_manager.Pass();
2363 MockAutocompleteHistoryManager* m = static_cast<
2364 MockAutocompleteHistoryManager*>(
2365 autofill_manager_->autocomplete_history_manager_.get());
2366 EXPECT_CALL(*m,
2367 OnGetAutocompleteSuggestions(_, _, _, _, _)).Times(0);
2369 // Set up our form data.
2370 FormData form;
2371 test::CreateTestAddressFormData(&form);
2372 std::vector<FormData> forms(1, form);
2373 FormsSeen(forms);
2374 FormFieldData* field = &form.fields[0];
2375 field->should_autocomplete = false;
2376 GetAutofillSuggestions(form, *field);
2379 // Test that we are able to save form data when forms are submitted and we only
2380 // have server data for the field types.
2381 TEST_F(AutofillManagerTest, FormSubmittedServerTypes) {
2382 // Set up our form data.
2383 FormData form;
2384 test::CreateTestAddressFormData(&form);
2386 // Simulate having seen this form on page load.
2387 // |form_structure| will be owned by |autofill_manager_|.
2388 TestFormStructure* form_structure = new TestFormStructure(form);
2389 form_structure->DetermineHeuristicTypes();
2391 // Clear the heuristic types, and instead set the appropriate server types.
2392 std::vector<ServerFieldType> heuristic_types, server_types;
2393 for (size_t i = 0; i < form.fields.size(); ++i) {
2394 heuristic_types.push_back(UNKNOWN_TYPE);
2395 server_types.push_back(form_structure->field(i)->heuristic_type());
2397 form_structure->SetFieldTypes(heuristic_types, server_types);
2398 autofill_manager_->AddSeenForm(form_structure);
2400 // Fill the form.
2401 SuggestionBackendID guid("00000000-0000-0000-0000-000000000001", 0);
2402 SuggestionBackendID empty(std::string(), 0);
2403 int response_page_id = 0;
2404 FormData response_data;
2405 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0],
2406 MakeFrontendID(empty, guid), &response_page_id, &response_data);
2407 ExpectFilledAddressFormElvis(
2408 response_page_id, response_data, kDefaultPageID, false);
2410 // Simulate form submission. We should call into the PDM to try to save the
2411 // filled data.
2412 EXPECT_CALL(personal_data_, SaveImportedProfile(::testing::_)).Times(1);
2413 FormSubmitted(response_data);
2416 // Test that the form signature for an uploaded form always matches the form
2417 // signature from the query.
2418 TEST_F(AutofillManagerTest, FormSubmittedWithDifferentFields) {
2419 // Set up our form data.
2420 FormData form;
2421 test::CreateTestAddressFormData(&form);
2422 std::vector<FormData> forms(1, form);
2423 FormsSeen(forms);
2425 // Cache the expected form signature.
2426 std::string signature = FormStructure(form).FormSignature();
2428 // Change the structure of the form prior to submission.
2429 // Websites would typically invoke JavaScript either on page load or on form
2430 // submit to achieve this.
2431 form.fields.pop_back();
2432 FormFieldData field = form.fields[3];
2433 form.fields[3] = form.fields[7];
2434 form.fields[7] = field;
2436 // Simulate form submission.
2437 FormSubmitted(form);
2438 EXPECT_EQ(signature, autofill_manager_->GetSubmittedFormSignature());
2441 // Test that we do not save form data when submitted fields contain default
2442 // values.
2443 TEST_F(AutofillManagerTest, FormSubmittedWithDefaultValues) {
2444 // Set up our form data.
2445 FormData form;
2446 test::CreateTestAddressFormData(&form);
2447 form.fields[3].value = ASCIIToUTF16("Enter your address");
2449 // Convert the state field to a <select> popup, to make sure that we only
2450 // reject default values for text fields.
2451 ASSERT_TRUE(form.fields[6].name == ASCIIToUTF16("state"));
2452 form.fields[6].form_control_type = "select-one";
2453 form.fields[6].value = ASCIIToUTF16("Tennessee");
2455 std::vector<FormData> forms(1, form);
2456 FormsSeen(forms);
2458 // Fill the form.
2459 SuggestionBackendID guid("00000000-0000-0000-0000-000000000001", 0);
2460 SuggestionBackendID empty(std::string(), 0);
2461 int response_page_id = 0;
2462 FormData response_data;
2463 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[3],
2464 MakeFrontendID(empty, guid), &response_page_id, &response_data);
2466 // Simulate form submission. We should call into the PDM to try to save the
2467 // filled data.
2468 EXPECT_CALL(personal_data_, SaveImportedProfile(::testing::_)).Times(1);
2469 FormSubmitted(response_data);
2471 // Set the address field's value back to the default value.
2472 response_data.fields[3].value = ASCIIToUTF16("Enter your address");
2474 // Simulate form submission. We should not call into the PDM to try to save
2475 // the filled data, since the filled form is effectively missing an address.
2476 EXPECT_CALL(personal_data_, SaveImportedProfile(::testing::_)).Times(0);
2477 FormSubmitted(response_data);
2480 // Checks that resetting the auxiliary profile enabled preference does the right
2481 // thing on all platforms.
2482 TEST_F(AutofillManagerTest, AuxiliaryProfilesReset) {
2483 PrefService* prefs = autofill_client_.GetPrefs();
2484 #if defined(OS_MACOSX)
2485 // Auxiliary profiles is implemented on Mac only.
2486 // OSX: This preference exists for legacy reasons. It is no longer used.
2487 ASSERT_TRUE(
2488 prefs->GetBoolean(::autofill::prefs::kAutofillAuxiliaryProfilesEnabled));
2489 prefs->SetBoolean(::autofill::prefs::kAutofillAuxiliaryProfilesEnabled,
2490 false);
2491 prefs->ClearPref(::autofill::prefs::kAutofillAuxiliaryProfilesEnabled);
2492 ASSERT_TRUE(
2493 prefs->GetBoolean(::autofill::prefs::kAutofillAuxiliaryProfilesEnabled));
2494 #else
2495 ASSERT_FALSE(
2496 prefs->GetBoolean(::autofill::prefs::kAutofillAuxiliaryProfilesEnabled));
2497 prefs->SetBoolean(::autofill::prefs::kAutofillAuxiliaryProfilesEnabled, true);
2498 prefs->ClearPref(::autofill::prefs::kAutofillAuxiliaryProfilesEnabled);
2499 ASSERT_FALSE(
2500 prefs->GetBoolean(::autofill::prefs::kAutofillAuxiliaryProfilesEnabled));
2501 #endif // defined(OS_MACOSX)
2504 TEST_F(AutofillManagerTest, DeterminePossibleFieldTypesForUpload) {
2505 FormData form;
2506 form.name = ASCIIToUTF16("MyForm");
2507 form.origin = GURL("http://myform.com/form.html");
2508 form.action = GURL("http://myform.com/submit.html");
2509 form.user_submitted = true;
2511 std::vector<ServerFieldTypeSet> expected_types;
2513 // These fields should all match.
2514 FormFieldData field;
2515 ServerFieldTypeSet types;
2516 test::CreateTestFormField("", "1", "Elvis", "text", &field);
2517 types.clear();
2518 types.insert(NAME_FIRST);
2519 form.fields.push_back(field);
2520 expected_types.push_back(types);
2522 test::CreateTestFormField("", "2", "Aaron", "text", &field);
2523 types.clear();
2524 types.insert(NAME_MIDDLE);
2525 form.fields.push_back(field);
2526 expected_types.push_back(types);
2528 test::CreateTestFormField("", "3", "A", "text", &field);
2529 types.clear();
2530 types.insert(NAME_MIDDLE_INITIAL);
2531 form.fields.push_back(field);
2532 expected_types.push_back(types);
2534 test::CreateTestFormField("", "4", "Presley", "text", &field);
2535 types.clear();
2536 types.insert(NAME_LAST);
2537 form.fields.push_back(field);
2538 expected_types.push_back(types);
2540 test::CreateTestFormField("", "5", "Elvis Presley", "text", &field);
2541 types.clear();
2542 types.insert(CREDIT_CARD_NAME);
2543 form.fields.push_back(field);
2544 expected_types.push_back(types);
2546 test::CreateTestFormField("", "6", "Elvis Aaron Presley", "text",
2547 &field);
2548 types.clear();
2549 types.insert(NAME_FULL);
2550 form.fields.push_back(field);
2551 expected_types.push_back(types);
2553 test::CreateTestFormField("", "7", "theking@gmail.com", "email",
2554 &field);
2555 types.clear();
2556 types.insert(EMAIL_ADDRESS);
2557 form.fields.push_back(field);
2558 expected_types.push_back(types);
2560 test::CreateTestFormField("", "8", "RCA", "text", &field);
2561 types.clear();
2562 types.insert(COMPANY_NAME);
2563 form.fields.push_back(field);
2564 expected_types.push_back(types);
2566 test::CreateTestFormField("", "9", "3734 Elvis Presley Blvd.",
2567 "text", &field);
2568 types.clear();
2569 types.insert(ADDRESS_HOME_LINE1);
2570 form.fields.push_back(field);
2571 expected_types.push_back(types);
2573 test::CreateTestFormField("", "10", "Apt. 10", "text", &field);
2574 types.clear();
2575 types.insert(ADDRESS_HOME_LINE2);
2576 form.fields.push_back(field);
2577 expected_types.push_back(types);
2579 test::CreateTestFormField("", "11", "Memphis", "text", &field);
2580 types.clear();
2581 types.insert(ADDRESS_HOME_CITY);
2582 form.fields.push_back(field);
2583 expected_types.push_back(types);
2585 test::CreateTestFormField("", "12", "Tennessee", "text", &field);
2586 types.clear();
2587 types.insert(ADDRESS_HOME_STATE);
2588 form.fields.push_back(field);
2589 expected_types.push_back(types);
2591 test::CreateTestFormField("", "13", "38116", "text", &field);
2592 types.clear();
2593 types.insert(ADDRESS_HOME_ZIP);
2594 form.fields.push_back(field);
2595 expected_types.push_back(types);
2597 test::CreateTestFormField("", "14", "USA", "text", &field);
2598 types.clear();
2599 types.insert(ADDRESS_HOME_COUNTRY);
2600 form.fields.push_back(field);
2601 expected_types.push_back(types);
2603 test::CreateTestFormField("", "15", "United States", "text", &field);
2604 types.clear();
2605 types.insert(ADDRESS_HOME_COUNTRY);
2606 form.fields.push_back(field);
2607 expected_types.push_back(types);
2609 test::CreateTestFormField("", "16", "+1 (234) 567-8901", "text",
2610 &field);
2611 types.clear();
2612 types.insert(PHONE_HOME_WHOLE_NUMBER);
2613 form.fields.push_back(field);
2614 expected_types.push_back(types);
2616 test::CreateTestFormField("", "17", "2345678901", "text", &field);
2617 types.clear();
2618 types.insert(PHONE_HOME_CITY_AND_NUMBER);
2619 form.fields.push_back(field);
2620 expected_types.push_back(types);
2622 test::CreateTestFormField("", "18", "1", "text", &field);
2623 types.clear();
2624 types.insert(PHONE_HOME_COUNTRY_CODE);
2625 form.fields.push_back(field);
2626 expected_types.push_back(types);
2628 test::CreateTestFormField("", "19", "234", "text", &field);
2629 types.clear();
2630 types.insert(PHONE_HOME_CITY_CODE);
2631 form.fields.push_back(field);
2632 expected_types.push_back(types);
2634 test::CreateTestFormField("", "20", "5678901", "text", &field);
2635 types.clear();
2636 types.insert(PHONE_HOME_NUMBER);
2637 form.fields.push_back(field);
2638 expected_types.push_back(types);
2640 test::CreateTestFormField("", "21", "567", "text", &field);
2641 types.clear();
2642 types.insert(PHONE_HOME_NUMBER);
2643 form.fields.push_back(field);
2644 expected_types.push_back(types);
2646 test::CreateTestFormField("", "22", "8901", "text", &field);
2647 types.clear();
2648 types.insert(PHONE_HOME_NUMBER);
2649 form.fields.push_back(field);
2650 expected_types.push_back(types);
2652 test::CreateTestFormField("", "23", "4234-5678-9012-3456", "text",
2653 &field);
2654 types.clear();
2655 types.insert(CREDIT_CARD_NUMBER);
2656 form.fields.push_back(field);
2657 expected_types.push_back(types);
2659 test::CreateTestFormField("", "24", "04", "text", &field);
2660 types.clear();
2661 types.insert(CREDIT_CARD_EXP_MONTH);
2662 form.fields.push_back(field);
2663 expected_types.push_back(types);
2665 test::CreateTestFormField("", "25", "April", "text", &field);
2666 types.clear();
2667 types.insert(CREDIT_CARD_EXP_MONTH);
2668 form.fields.push_back(field);
2669 expected_types.push_back(types);
2671 test::CreateTestFormField("", "26", "2012", "text", &field);
2672 types.clear();
2673 types.insert(CREDIT_CARD_EXP_4_DIGIT_YEAR);
2674 form.fields.push_back(field);
2675 expected_types.push_back(types);
2677 test::CreateTestFormField("", "27", "12", "text", &field);
2678 types.clear();
2679 types.insert(CREDIT_CARD_EXP_2_DIGIT_YEAR);
2680 form.fields.push_back(field);
2681 expected_types.push_back(types);
2683 test::CreateTestFormField("", "28", "04/2012", "text", &field);
2684 types.clear();
2685 types.insert(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR);
2686 form.fields.push_back(field);
2687 expected_types.push_back(types);
2689 // Make sure that we trim whitespace properly.
2690 test::CreateTestFormField("", "29", "", "text", &field);
2691 types.clear();
2692 types.insert(EMPTY_TYPE);
2693 form.fields.push_back(field);
2694 expected_types.push_back(types);
2696 test::CreateTestFormField("", "30", " ", "text", &field);
2697 types.clear();
2698 types.insert(EMPTY_TYPE);
2699 form.fields.push_back(field);
2700 expected_types.push_back(types);
2702 test::CreateTestFormField("", "31", " Elvis", "text", &field);
2703 types.clear();
2704 types.insert(NAME_FIRST);
2705 form.fields.push_back(field);
2706 expected_types.push_back(types);
2708 test::CreateTestFormField("", "32", "Elvis ", "text", &field);
2709 types.clear();
2710 types.insert(NAME_FIRST);
2711 form.fields.push_back(field);
2712 expected_types.push_back(types);
2714 // These fields should not match, as they differ by case.
2715 test::CreateTestFormField("", "33", "elvis", "text", &field);
2716 types.clear();
2717 types.insert(UNKNOWN_TYPE);
2718 form.fields.push_back(field);
2719 expected_types.push_back(types);
2721 test::CreateTestFormField("", "34", "3734 Elvis Presley BLVD",
2722 "text", &field);
2723 types.clear();
2724 types.insert(UNKNOWN_TYPE);
2725 form.fields.push_back(field);
2726 expected_types.push_back(types);
2728 // These fields should not match, as they are unsupported variants.
2729 test::CreateTestFormField("", "35", "Elvis Aaron", "text", &field);
2730 types.clear();
2731 types.insert(UNKNOWN_TYPE);
2732 form.fields.push_back(field);
2733 expected_types.push_back(types);
2735 test::CreateTestFormField("", "36", "Mr. Presley", "text", &field);
2736 types.clear();
2737 types.insert(UNKNOWN_TYPE);
2738 form.fields.push_back(field);
2739 expected_types.push_back(types);
2741 test::CreateTestFormField("", "37", "3734 Elvis Presley", "text",
2742 &field);
2743 types.clear();
2744 types.insert(UNKNOWN_TYPE);
2745 form.fields.push_back(field);
2746 expected_types.push_back(types);
2748 test::CreateTestFormField("", "38", "TN", "text", &field);
2749 types.clear();
2750 types.insert(UNKNOWN_TYPE);
2751 form.fields.push_back(field);
2752 expected_types.push_back(types);
2754 test::CreateTestFormField("", "39", "38116-1023", "text", &field);
2755 types.clear();
2756 types.insert(UNKNOWN_TYPE);
2757 form.fields.push_back(field);
2758 expected_types.push_back(types);
2760 test::CreateTestFormField("", "20", "5", "text", &field);
2761 types.clear();
2762 types.insert(UNKNOWN_TYPE);
2763 form.fields.push_back(field);
2764 expected_types.push_back(types);
2766 test::CreateTestFormField("", "20", "56", "text", &field);
2767 types.clear();
2768 types.insert(UNKNOWN_TYPE);
2769 form.fields.push_back(field);
2770 expected_types.push_back(types);
2772 test::CreateTestFormField("", "20", "901", "text", &field);
2773 types.clear();
2774 types.insert(UNKNOWN_TYPE);
2775 form.fields.push_back(field);
2776 expected_types.push_back(types);
2778 test::CreateTestFormField("", "40", "mypassword", "password", &field);
2779 types.clear();
2780 types.insert(PASSWORD);
2781 form.fields.push_back(field);
2782 expected_types.push_back(types);
2784 autofill_manager_->set_expected_submitted_field_types(expected_types);
2785 FormSubmitted(form);
2788 TEST_F(AutofillManagerTest, RemoveProfile) {
2789 // Add and remove an Autofill profile.
2790 AutofillProfile* profile = new AutofillProfile;
2791 std::string guid = "00000000-0000-0000-0000-000000000102";
2792 profile->set_guid(guid.c_str());
2793 autofill_manager_->AddProfile(profile);
2795 SuggestionBackendID guid_pair(guid, 0);
2796 SuggestionBackendID empty(std::string(), 0);
2797 int id = MakeFrontendID(empty, guid_pair);
2799 autofill_manager_->RemoveAutofillProfileOrCreditCard(id);
2801 EXPECT_FALSE(autofill_manager_->GetProfileWithGUID(guid.c_str()));
2804 TEST_F(AutofillManagerTest, RemoveCreditCard){
2805 // Add and remove an Autofill credit card.
2806 CreditCard* credit_card = new CreditCard;
2807 std::string guid = "00000000-0000-0000-0000-000000100007";
2808 credit_card->set_guid(guid.c_str());
2809 autofill_manager_->AddCreditCard(credit_card);
2811 SuggestionBackendID guid_pair(guid, 0);
2812 SuggestionBackendID empty(std::string(), 0);
2813 int id = MakeFrontendID(guid_pair, empty);
2815 autofill_manager_->RemoveAutofillProfileOrCreditCard(id);
2817 EXPECT_FALSE(autofill_manager_->GetCreditCardWithGUID(guid.c_str()));
2820 TEST_F(AutofillManagerTest, RemoveProfileVariant) {
2821 // Add and remove an Autofill profile.
2822 AutofillProfile* profile = new AutofillProfile;
2823 std::string guid = "00000000-0000-0000-0000-000000000102";
2824 profile->set_guid(guid.c_str());
2825 autofill_manager_->AddProfile(profile);
2827 SuggestionBackendID guid_pair(guid, 1);
2828 SuggestionBackendID empty(std::string(), 0);
2829 int id = MakeFrontendID(empty, guid_pair);
2831 autofill_manager_->RemoveAutofillProfileOrCreditCard(id);
2833 // TODO(csharp): Currently variants should not be deleted, but once they are
2834 // update these expectations.
2835 // http://crbug.com/124211
2836 EXPECT_TRUE(autofill_manager_->GetProfileWithGUID(guid.c_str()));
2839 #if defined(OS_MACOSX) && !defined(OS_IOS)
2840 TEST_F(AutofillManagerTest, AccessAddressBookPrompt) {
2841 FormData form;
2842 test::CreateTestAddressFormData(&form);
2843 std::vector<FormData> forms(1, form);
2844 FormsSeen(forms);
2845 FormFieldData& field = form.fields[0];
2846 field.should_autocomplete = true;
2848 // A profile already exists.
2849 EXPECT_FALSE(
2850 autofill_manager_->ShouldShowAccessAddressBookSuggestion(form, field));
2852 // Remove all profiles.
2853 personal_data_.ClearAutofillProfiles();
2854 EXPECT_TRUE(
2855 autofill_manager_->ShouldShowAccessAddressBookSuggestion(form, field));
2857 field.should_autocomplete = false;
2858 EXPECT_TRUE(
2859 autofill_manager_->ShouldShowAccessAddressBookSuggestion(form, field));
2861 #endif // defined(OS_MACOSX) && !defined(OS_IOS)
2863 namespace {
2865 class MockAutofillClient : public TestAutofillClient {
2866 public:
2867 MockAutofillClient() {}
2869 ~MockAutofillClient() override {}
2871 void ShowRequestAutocompleteDialog(const FormData& form,
2872 content::RenderFrameHost* rfh,
2873 const ResultCallback& callback) override {
2874 callback.Run(user_supplied_data_ ? AutocompleteResultSuccess :
2875 AutocompleteResultErrorDisabled,
2876 base::string16(),
2877 user_supplied_data_.get());
2880 void SetUserSuppliedData(scoped_ptr<FormStructure> user_supplied_data) {
2881 user_supplied_data_.reset(user_supplied_data.release());
2884 private:
2885 scoped_ptr<FormStructure> user_supplied_data_;
2887 DISALLOW_COPY_AND_ASSIGN(MockAutofillClient);
2890 } // namespace
2892 // Test our external delegate is called at the right time.
2893 TEST_F(AutofillManagerTest, TestExternalDelegate) {
2894 FormData form;
2895 test::CreateTestAddressFormData(&form);
2896 std::vector<FormData> forms(1, form);
2897 FormsSeen(forms);
2898 const FormFieldData& field = form.fields[0];
2899 GetAutofillSuggestions(form, field); // should call the delegate's OnQuery()
2901 EXPECT_TRUE(external_delegate_->on_query_seen());
2904 // Test to verify suggestions appears for forms having credit card number split
2905 // across fields.
2906 TEST_F(AutofillManagerTest, GetCreditCardSuggestionsForNumberSpitAcrossFields) {
2907 // Set up our form data with credit card number split across fields.
2908 FormData form;
2909 form.name = ASCIIToUTF16("MyForm");
2910 form.origin = GURL("https://myform.com/form.html");
2911 form.action = GURL("https://myform.com/submit.html");
2912 form.user_submitted = true;
2914 FormFieldData name_field;
2915 test::CreateTestFormField("Name on Card", "nameoncard", "", "text",
2916 &name_field);
2917 form.fields.push_back(name_field);
2919 // Add new 4 |card_number_field|s to the |form|.
2920 FormFieldData card_number_field;
2921 card_number_field.max_length = 4;
2922 test::CreateTestFormField("Card Number", "cardnumber_1", "", "text",
2923 &card_number_field);
2924 form.fields.push_back(card_number_field);
2926 test::CreateTestFormField("", "cardnumber_2", "", "text", &card_number_field);
2927 form.fields.push_back(card_number_field);
2929 test::CreateTestFormField("", "cardnumber_3", "", "text", &card_number_field);
2930 form.fields.push_back(card_number_field);
2932 test::CreateTestFormField("", "cardnumber_4", "", "text", &card_number_field);
2933 form.fields.push_back(card_number_field);
2935 FormFieldData exp_field;
2936 test::CreateTestFormField("Expiration Date", "ccmonth", "", "text",
2937 &exp_field);
2938 form.fields.push_back(exp_field);
2940 test::CreateTestFormField("", "ccyear", "", "text", &exp_field);
2941 form.fields.push_back(exp_field);
2943 std::vector<FormData> forms(1, form);
2944 FormsSeen(forms);
2946 // Verify whether suggestions are populated correctly for one of the middle
2947 // credit card number fields when filled partially.
2948 FormFieldData number_field = form.fields[3];
2949 number_field.value = ASCIIToUTF16("901");
2951 // Get the suggestions for already filled credit card |number_field|.
2952 GetAutofillSuggestions(form, number_field);
2954 // No autocomplete suggestions provided, so send an empty vector as the
2955 // results. This triggers the combined message send.
2956 AutocompleteSuggestionsReturned(std::vector<base::string16>());
2958 external_delegate_->CheckSuggestions(
2959 kDefaultPageID,
2960 Suggestion("Visa - 3456", "04/12", kVisaCard,
2961 autofill_manager_->GetPackedCreditCardID(4)));
2964 } // namespace autofill