Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / components / autofill / core / browser / autofill_manager_unittest.cc
blob89672171ac7bbaaf9e2aea72dd9b8fdbaac863dc
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/prefs/pref_service.h"
14 #include "base/run_loop.h"
15 #include "base/strings/string16.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/string_util.h"
18 #include "base/strings/stringprintf.h"
19 #include "base/strings/utf_string_conversions.h"
20 #include "base/thread_task_runner_handle.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 "net/url_request/url_request_test_util.h"
39 #include "testing/gmock/include/gmock/gmock.h"
40 #include "testing/gtest/include/gtest/gtest.h"
41 #include "ui/base/l10n/l10n_util.h"
42 #include "ui/gfx/geometry/rect.h"
43 #include "url/gurl.h"
45 using base::ASCIIToUTF16;
46 using base::UTF8ToUTF16;
47 using testing::_;
48 using testing::AtLeast;
49 using testing::SaveArg;
51 namespace autofill {
53 namespace {
55 const int kDefaultPageID = 137;
57 class MockAutofillClient : public TestAutofillClient {
58 public:
59 MockAutofillClient() {}
61 ~MockAutofillClient() override {}
63 MOCK_METHOD1(ConfirmSaveCreditCard,
64 void(const base::Closure& save_card_callback));
66 private:
67 DISALLOW_COPY_AND_ASSIGN(MockAutofillClient);
70 class TestPersonalDataManager : public PersonalDataManager {
71 public:
72 TestPersonalDataManager()
73 : PersonalDataManager("en-US"),
74 num_times_save_imported_profile_called_(0) {
75 CreateTestAutofillProfiles(&web_profiles_);
76 CreateTestCreditCards(&local_credit_cards_);
79 using PersonalDataManager::set_database;
80 using PersonalDataManager::SetPrefService;
82 int num_times_save_imported_profile_called() {
83 return num_times_save_imported_profile_called_;
86 std::string SaveImportedProfile(const AutofillProfile& profile) override {
87 num_times_save_imported_profile_called_++;
88 AutofillProfile* imported_profile = new AutofillProfile(profile);
89 AddProfile(imported_profile);
90 return profile.guid();
93 AutofillProfile* GetProfileWithGUID(const char* guid) {
94 for (AutofillProfile* profile : GetProfiles()) {
95 if (!profile->guid().compare(guid))
96 return profile;
98 return NULL;
101 CreditCard* GetCreditCardWithGUID(const char* guid) {
102 for (CreditCard* card : GetCreditCards()) {
103 if (!card->guid().compare(guid))
104 return card;
106 return NULL;
109 void AddProfile(AutofillProfile* profile) {
110 web_profiles_.push_back(profile);
113 void AddCreditCard(CreditCard* credit_card) {
114 local_credit_cards_.push_back(credit_card);
117 void RecordUseOf(const AutofillDataModel& data_model) override {
118 CreditCard* credit_card = GetCreditCardWithGUID(data_model.guid().c_str());
119 if (credit_card)
120 credit_card->RecordUse();
122 AutofillProfile* profile = GetProfileWithGUID(data_model.guid().c_str());
123 if (profile)
124 profile->RecordUse();
127 void RemoveByGUID(const std::string& guid) override {
128 CreditCard* credit_card = GetCreditCardWithGUID(guid.c_str());
129 if (credit_card) {
130 local_credit_cards_.erase(
131 std::find(local_credit_cards_.begin(), local_credit_cards_.end(),
132 credit_card));
135 AutofillProfile* profile = GetProfileWithGUID(guid.c_str());
136 if (profile) {
137 web_profiles_.erase(
138 std::find(web_profiles_.begin(), web_profiles_.end(), profile));
142 void ClearAutofillProfiles() {
143 web_profiles_.clear();
146 void ClearCreditCards() {
147 local_credit_cards_.clear();
150 void CreateTestCreditCardsYearAndMonth(const char* year, const char* month) {
151 ClearCreditCards();
152 CreditCard* credit_card = new CreditCard;
153 test::SetCreditCardInfo(credit_card, "Miku Hatsune",
154 "4234567890654321", // Visa
155 month, year);
156 credit_card->set_guid("00000000-0000-0000-0000-000000000007");
157 local_credit_cards_.push_back(credit_card);
160 private:
161 void CreateTestAutofillProfiles(ScopedVector<AutofillProfile>* profiles) {
162 AutofillProfile* profile = new AutofillProfile;
163 test::SetProfileInfo(profile, "Elvis", "Aaron",
164 "Presley", "theking@gmail.com", "RCA",
165 "3734 Elvis Presley Blvd.", "Apt. 10",
166 "Memphis", "Tennessee", "38116", "US",
167 "12345678901");
168 profile->set_guid("00000000-0000-0000-0000-000000000001");
169 profiles->push_back(profile);
170 profile = new AutofillProfile;
171 test::SetProfileInfo(profile, "Charles", "Hardin",
172 "Holley", "buddy@gmail.com", "Decca",
173 "123 Apple St.", "unit 6", "Lubbock",
174 "Texas", "79401", "US", "23456789012");
175 profile->set_guid("00000000-0000-0000-0000-000000000002");
176 profiles->push_back(profile);
177 profile = new AutofillProfile;
178 test::SetProfileInfo(
179 profile, "", "", "", "", "", "", "", "", "", "", "", "");
180 profile->set_guid("00000000-0000-0000-0000-000000000003");
181 profiles->push_back(profile);
184 void CreateTestCreditCards(ScopedVector<CreditCard>* credit_cards) {
185 CreditCard* credit_card = new CreditCard;
186 test::SetCreditCardInfo(credit_card, "Elvis Presley",
187 "4234 5678 9012 3456", // Visa
188 "04", "2012");
189 credit_card->set_guid("00000000-0000-0000-0000-000000000004");
190 credit_cards->push_back(credit_card);
192 credit_card = new CreditCard;
193 test::SetCreditCardInfo(credit_card, "Buddy Holly",
194 "5187654321098765", // Mastercard
195 "10", "2014");
196 credit_card->set_guid("00000000-0000-0000-0000-000000000005");
197 credit_cards->push_back(credit_card);
199 credit_card = new CreditCard;
200 test::SetCreditCardInfo(credit_card, "", "", "", "");
201 credit_card->set_guid("00000000-0000-0000-0000-000000000006");
202 credit_cards->push_back(credit_card);
205 size_t num_times_save_imported_profile_called_;
207 DISALLOW_COPY_AND_ASSIGN(TestPersonalDataManager);
210 void ExpectFilledField(const char* expected_label,
211 const char* expected_name,
212 const char* expected_value,
213 const char* expected_form_control_type,
214 const FormFieldData& field) {
215 SCOPED_TRACE(expected_label);
216 EXPECT_EQ(UTF8ToUTF16(expected_label), field.label);
217 EXPECT_EQ(UTF8ToUTF16(expected_name), field.name);
218 EXPECT_EQ(UTF8ToUTF16(expected_value), field.value);
219 EXPECT_EQ(expected_form_control_type, field.form_control_type);
222 // Verifies that the |filled_form| has been filled with the given data.
223 // Verifies address fields if |has_address_fields| is true, and verifies
224 // credit card fields if |has_credit_card_fields| is true. Verifies both if both
225 // are true. |use_month_type| is used for credit card input month type.
226 void ExpectFilledForm(int page_id,
227 const FormData& filled_form,
228 int expected_page_id,
229 const char* first,
230 const char* middle,
231 const char* last,
232 const char* address1,
233 const char* address2,
234 const char* city,
235 const char* state,
236 const char* postal_code,
237 const char* country,
238 const char* phone,
239 const char* email,
240 const char* name_on_card,
241 const char* card_number,
242 const char* expiration_month,
243 const char* expiration_year,
244 bool has_address_fields,
245 bool has_credit_card_fields,
246 bool use_month_type) {
247 // The number of fields in the address and credit card forms created above.
248 const size_t kAddressFormSize = 11;
249 const size_t kCreditCardFormSize = use_month_type ? 3 : 4;
251 EXPECT_EQ(expected_page_id, page_id);
252 EXPECT_EQ(ASCIIToUTF16("MyForm"), filled_form.name);
253 if (has_credit_card_fields) {
254 EXPECT_EQ(GURL("https://myform.com/form.html"), filled_form.origin);
255 EXPECT_EQ(GURL("https://myform.com/submit.html"), filled_form.action);
256 } else {
257 EXPECT_EQ(GURL("http://myform.com/form.html"), filled_form.origin);
258 EXPECT_EQ(GURL("http://myform.com/submit.html"), filled_form.action);
261 size_t form_size = 0;
262 if (has_address_fields)
263 form_size += kAddressFormSize;
264 if (has_credit_card_fields)
265 form_size += kCreditCardFormSize;
266 ASSERT_EQ(form_size, filled_form.fields.size());
268 if (has_address_fields) {
269 ExpectFilledField("First Name", "firstname", first, "text",
270 filled_form.fields[0]);
271 ExpectFilledField("Middle Name", "middlename", middle, "text",
272 filled_form.fields[1]);
273 ExpectFilledField("Last Name", "lastname", last, "text",
274 filled_form.fields[2]);
275 ExpectFilledField("Address Line 1", "addr1", address1, "text",
276 filled_form.fields[3]);
277 ExpectFilledField("Address Line 2", "addr2", address2, "text",
278 filled_form.fields[4]);
279 ExpectFilledField("City", "city", city, "text",
280 filled_form.fields[5]);
281 ExpectFilledField("State", "state", state, "text",
282 filled_form.fields[6]);
283 ExpectFilledField("Postal Code", "zipcode", postal_code, "text",
284 filled_form.fields[7]);
285 ExpectFilledField("Country", "country", country, "text",
286 filled_form.fields[8]);
287 ExpectFilledField("Phone Number", "phonenumber", phone, "tel",
288 filled_form.fields[9]);
289 ExpectFilledField("Email", "email", email, "email",
290 filled_form.fields[10]);
293 if (has_credit_card_fields) {
294 size_t offset = has_address_fields? kAddressFormSize : 0;
295 ExpectFilledField("Name on Card", "nameoncard", name_on_card, "text",
296 filled_form.fields[offset + 0]);
297 ExpectFilledField("Card Number", "cardnumber", card_number, "text",
298 filled_form.fields[offset + 1]);
299 if (use_month_type) {
300 std::string exp_year = expiration_year;
301 std::string exp_month = expiration_month;
302 std::string date;
303 if (!exp_year.empty() && !exp_month.empty())
304 date = exp_year + "-" + exp_month;
306 ExpectFilledField("Expiration Date", "ccmonth", date.c_str(), "month",
307 filled_form.fields[offset + 2]);
308 } else {
309 ExpectFilledField("Expiration Date", "ccmonth", expiration_month, "text",
310 filled_form.fields[offset + 2]);
311 ExpectFilledField("", "ccyear", expiration_year, "text",
312 filled_form.fields[offset + 3]);
317 void ExpectFilledAddressFormElvis(int page_id,
318 const FormData& filled_form,
319 int expected_page_id,
320 bool has_credit_card_fields) {
321 ExpectFilledForm(page_id, filled_form, expected_page_id, "Elvis", "Aaron",
322 "Presley", "3734 Elvis Presley Blvd.", "Apt. 10", "Memphis",
323 "Tennessee", "38116", "United States", "12345678901",
324 "theking@gmail.com", "", "", "", "", true,
325 has_credit_card_fields, false);
328 void ExpectFilledCreditCardFormElvis(int page_id,
329 const FormData& filled_form,
330 int expected_page_id,
331 bool has_address_fields) {
332 ExpectFilledForm(page_id, filled_form, expected_page_id, "", "", "", "", "",
333 "", "", "", "", "", "", "Elvis Presley", "4234567890123456",
334 "04", "2012", has_address_fields, true, false);
337 void ExpectFilledCreditCardYearMonthWithYearMonth(int page_id,
338 const FormData& filled_form,
339 int expected_page_id,
340 bool has_address_fields,
341 const char* year,
342 const char* month) {
343 ExpectFilledForm(page_id, filled_form, expected_page_id, "", "", "", "", "",
344 "", "", "", "", "", "", "Miku Hatsune", "4234567890654321",
345 month, year, has_address_fields, true, true);
348 class MockAutocompleteHistoryManager : public AutocompleteHistoryManager {
349 public:
350 MockAutocompleteHistoryManager(AutofillDriver* driver, AutofillClient* client)
351 : AutocompleteHistoryManager(driver, client) {}
353 MOCK_METHOD5(OnGetAutocompleteSuggestions, void(
354 int query_id,
355 const base::string16& name,
356 const base::string16& prefix,
357 const std::string& form_control_type,
358 const std::vector<Suggestion>& suggestions));
359 MOCK_METHOD1(OnWillSubmitForm, void(const FormData& form));
361 private:
362 DISALLOW_COPY_AND_ASSIGN(MockAutocompleteHistoryManager);
365 class MockAutofillDriver : public TestAutofillDriver {
366 public:
367 MockAutofillDriver() {}
369 // Mock methods to enable testability.
370 MOCK_METHOD3(SendFormDataToRenderer, void(int query_id,
371 RendererFormDataAction action,
372 const FormData& data));
374 private:
375 DISALLOW_COPY_AND_ASSIGN(MockAutofillDriver);
378 class TestAutofillManager : public AutofillManager {
379 public:
380 TestAutofillManager(AutofillDriver* driver,
381 autofill::AutofillClient* client,
382 TestPersonalDataManager* personal_data)
383 : AutofillManager(driver, client, personal_data),
384 personal_data_(personal_data),
385 autofill_enabled_(true),
386 expect_all_unknown_possible_types_(false) {}
387 ~TestAutofillManager() override {}
389 bool IsAutofillEnabled() const override { return autofill_enabled_; }
391 void set_autofill_enabled(bool autofill_enabled) {
392 autofill_enabled_ = autofill_enabled;
395 void set_expected_submitted_field_types(
396 const std::vector<ServerFieldTypeSet>& expected_types) {
397 expected_submitted_field_types_ = expected_types;
400 void UploadFormDataAsyncCallback(
401 const FormStructure* submitted_form,
402 const base::TimeTicks& load_time,
403 const base::TimeTicks& interaction_time,
404 const base::TimeTicks& submission_time) override {
405 run_loop_->Quit();
407 // If we have expected field types set, make sure they match.
408 if (!expected_submitted_field_types_.empty()) {
409 ASSERT_EQ(expected_submitted_field_types_.size(),
410 submitted_form->field_count());
411 for (size_t i = 0; i < expected_submitted_field_types_.size(); ++i) {
412 SCOPED_TRACE(
413 base::StringPrintf(
414 "Field %d with value %s", static_cast<int>(i),
415 base::UTF16ToUTF8(submitted_form->field(i)->value).c_str()));
416 const ServerFieldTypeSet& possible_types =
417 submitted_form->field(i)->possible_types();
418 EXPECT_EQ(expected_submitted_field_types_[i].size(),
419 possible_types.size());
420 for (ServerFieldTypeSet::const_iterator it =
421 expected_submitted_field_types_[i].begin();
422 it != expected_submitted_field_types_[i].end(); ++it) {
423 EXPECT_TRUE(possible_types.count(*it))
424 << "Expected type: " << AutofillType(*it).ToString();
429 AutofillManager::UploadFormDataAsyncCallback(submitted_form,
430 load_time,
431 interaction_time,
432 submission_time);
435 // Resets the run loop so that it can wait for an asynchronous form
436 // submission to complete.
437 void ResetRunLoop() { run_loop_.reset(new base::RunLoop()); }
439 // Wait for the asynchronous OnWillSubmitForm() call to complete.
440 void WaitForAsyncOnWillSubmitForm() { run_loop_->Run(); }
442 void UploadFormData(const FormStructure& submitted_form) override {
443 submitted_form_signature_ = submitted_form.FormSignature();
446 const std::string GetSubmittedFormSignature() {
447 return submitted_form_signature_;
450 AutofillProfile* GetProfileWithGUID(const char* guid) {
451 return personal_data_->GetProfileWithGUID(guid);
454 CreditCard* GetCreditCardWithGUID(const char* guid) {
455 return personal_data_->GetCreditCardWithGUID(guid);
458 void AddProfile(AutofillProfile* profile) {
459 personal_data_->AddProfile(profile);
462 void AddCreditCard(CreditCard* credit_card) {
463 personal_data_->AddCreditCard(credit_card);
466 int GetPackedCreditCardID(int credit_card_id) {
467 std::string credit_card_guid =
468 base::StringPrintf("00000000-0000-0000-0000-%012d", credit_card_id);
470 return MakeFrontendID(credit_card_guid, std::string());
473 void AddSeenForm(FormStructure* form) {
474 form_structures()->push_back(form);
477 void ClearFormStructures() {
478 form_structures()->clear();
481 private:
482 // Weak reference.
483 TestPersonalDataManager* personal_data_;
485 bool autofill_enabled_;
486 bool expect_all_unknown_possible_types_;
488 scoped_ptr<base::RunLoop> run_loop_;
490 std::string submitted_form_signature_;
491 std::vector<ServerFieldTypeSet> expected_submitted_field_types_;
493 DISALLOW_COPY_AND_ASSIGN(TestAutofillManager);
496 class TestAutofillExternalDelegate : public AutofillExternalDelegate {
497 public:
498 explicit TestAutofillExternalDelegate(AutofillManager* autofill_manager,
499 AutofillDriver* autofill_driver)
500 : AutofillExternalDelegate(autofill_manager, autofill_driver),
501 on_query_seen_(false),
502 on_suggestions_returned_seen_(false) {}
503 ~TestAutofillExternalDelegate() override {}
505 void OnQuery(int query_id,
506 const FormData& form,
507 const FormFieldData& field,
508 const gfx::RectF& bounds) override {
509 on_query_seen_ = true;
510 on_suggestions_returned_seen_ = false;
513 void OnSuggestionsReturned(
514 int query_id,
515 const std::vector<Suggestion>& suggestions) override {
516 on_suggestions_returned_seen_ = true;
517 query_id_ = query_id;
518 suggestions_ = suggestions;
521 void CheckSuggestions(int expected_page_id,
522 size_t expected_num_suggestions,
523 const Suggestion expected_suggestions[]) {
524 // Ensure that these results are from the most recent query.
525 EXPECT_TRUE(on_suggestions_returned_seen_);
527 EXPECT_EQ(expected_page_id, query_id_);
528 ASSERT_EQ(expected_num_suggestions, suggestions_.size());
529 for (size_t i = 0; i < expected_num_suggestions; ++i) {
530 SCOPED_TRACE(base::StringPrintf("i: %" PRIuS, i));
531 EXPECT_EQ(expected_suggestions[i].value, suggestions_[i].value);
532 EXPECT_EQ(expected_suggestions[i].label, suggestions_[i].label);
533 EXPECT_EQ(expected_suggestions[i].icon, suggestions_[i].icon);
534 EXPECT_EQ(expected_suggestions[i].frontend_id,
535 suggestions_[i].frontend_id);
539 // Wrappers around the above GetSuggestions call that take a hardcoded number
540 // of expected results so callsites are cleaner.
541 void CheckSuggestions(int expected_page_id,
542 const Suggestion& suggestion0) {
543 std::vector<Suggestion> suggestion_vector;
544 suggestion_vector.push_back(suggestion0);
545 CheckSuggestions(expected_page_id, 1, &suggestion_vector[0]);
547 void CheckSuggestions(int expected_page_id,
548 const Suggestion& suggestion0,
549 const Suggestion& suggestion1) {
550 std::vector<Suggestion> suggestion_vector;
551 suggestion_vector.push_back(suggestion0);
552 suggestion_vector.push_back(suggestion1);
553 CheckSuggestions(expected_page_id, 2, &suggestion_vector[0]);
555 void CheckSuggestions(int expected_page_id,
556 const Suggestion& suggestion0,
557 const Suggestion& suggestion1,
558 const Suggestion& suggestion2) {
559 std::vector<Suggestion> suggestion_vector;
560 suggestion_vector.push_back(suggestion0);
561 suggestion_vector.push_back(suggestion1);
562 suggestion_vector.push_back(suggestion2);
563 CheckSuggestions(expected_page_id, 3, &suggestion_vector[0]);
567 bool on_query_seen() const {
568 return on_query_seen_;
571 bool on_suggestions_returned_seen() const {
572 return on_suggestions_returned_seen_;
575 private:
576 // Records if OnQuery has been called yet.
577 bool on_query_seen_;
579 // Records if OnSuggestionsReturned has been called after the most recent
580 // call to OnQuery.
581 bool on_suggestions_returned_seen_;
583 // The query id of the most recent Autofill query.
584 int query_id_;
586 // The results returned by the most recent Autofill query.
587 std::vector<Suggestion> suggestions_;
589 DISALLOW_COPY_AND_ASSIGN(TestAutofillExternalDelegate);
592 } // namespace
594 class AutofillManagerTest : public testing::Test {
595 public:
596 void SetUp() override {
597 autofill_client_.SetPrefs(test::PrefServiceForTesting());
598 personal_data_.set_database(autofill_client_.GetDatabase());
599 personal_data_.SetPrefService(autofill_client_.GetPrefs());
600 autofill_driver_.reset(new MockAutofillDriver());
601 request_context_ = new net::TestURLRequestContextGetter(
602 base::ThreadTaskRunnerHandle::Get());
603 autofill_driver_->SetURLRequestContext(request_context_.get());
604 autofill_manager_.reset(new TestAutofillManager(
605 autofill_driver_.get(), &autofill_client_, &personal_data_));
607 external_delegate_.reset(new TestAutofillExternalDelegate(
608 autofill_manager_.get(),
609 autofill_driver_.get()));
610 autofill_manager_->SetExternalDelegate(external_delegate_.get());
613 void TearDown() override {
614 // Order of destruction is important as AutofillManager relies on
615 // PersonalDataManager to be around when it gets destroyed.
616 autofill_manager_.reset();
617 autofill_driver_.reset();
619 // Remove the AutofillWebDataService so TestPersonalDataManager does not
620 // need to care about removing self as an observer in destruction.
621 personal_data_.set_database(scoped_refptr<AutofillWebDataService>(NULL));
622 personal_data_.SetPrefService(NULL);
624 request_context_ = nullptr;
627 void GetAutofillSuggestions(int query_id,
628 const FormData& form,
629 const FormFieldData& field) {
630 autofill_manager_->OnQueryFormFieldAutofill(query_id, form, field,
631 gfx::Rect());
634 void GetAutofillSuggestions(const FormData& form,
635 const FormFieldData& field) {
636 GetAutofillSuggestions(kDefaultPageID, form, field);
639 void AutocompleteSuggestionsReturned(
640 const std::vector<base::string16>& result) {
641 autofill_manager_->autocomplete_history_manager_->SendSuggestions(&result);
644 void FormsSeen(const std::vector<FormData>& forms) {
645 autofill_manager_->OnFormsSeen(forms, base::TimeTicks());
648 void FormSubmitted(const FormData& form) {
649 autofill_manager_->ResetRunLoop();
650 if (autofill_manager_->OnWillSubmitForm(form, base::TimeTicks::Now()))
651 autofill_manager_->WaitForAsyncOnWillSubmitForm();
652 autofill_manager_->OnFormSubmitted(form);
655 void FillAutofillFormData(int query_id,
656 const FormData& form,
657 const FormFieldData& field,
658 int unique_id) {
659 autofill_manager_->FillOrPreviewForm(
660 AutofillDriver::FORM_DATA_ACTION_FILL, query_id, form, field,
661 unique_id);
664 // Calls |autofill_manager_->OnFillAutofillFormData()| with the specified
665 // input parameters after setting up the expectation that the mock driver's
666 // |SendFormDataToRenderer()| method will be called and saving the parameters
667 // of that call into the |response_query_id| and |response_data| output
668 // parameters.
669 void FillAutofillFormDataAndSaveResults(int input_query_id,
670 const FormData& input_form,
671 const FormFieldData& input_field,
672 int unique_id,
673 int* response_query_id,
674 FormData* response_data) {
675 EXPECT_CALL(*autofill_driver_, SendFormDataToRenderer(_, _, _)).
676 WillOnce((DoAll(testing::SaveArg<0>(response_query_id),
677 testing::SaveArg<2>(response_data))));
678 FillAutofillFormData(input_query_id, input_form, input_field, unique_id);
681 int MakeFrontendID(const std::string& cc_sid,
682 const std::string& profile_sid) const {
683 return autofill_manager_->MakeFrontendID(cc_sid, profile_sid);
686 bool WillFillCreditCardNumber(const FormData& form,
687 const FormFieldData& field) {
688 return autofill_manager_->WillFillCreditCardNumber(form, field);
691 // Populates |form| with data corresponding to a simple credit card form.
692 // Note that this actually appends fields to the form data, which can be
693 // useful for building up more complex test forms.
694 void CreateTestCreditCardFormData(FormData* form,
695 bool is_https,
696 bool use_month_type) {
697 form->name = ASCIIToUTF16("MyForm");
698 if (is_https) {
699 form->origin = GURL("https://myform.com/form.html");
700 form->action = GURL("https://myform.com/submit.html");
701 } else {
702 form->origin = GURL("http://myform.com/form.html");
703 form->action = GURL("http://myform.com/submit.html");
704 autofill_client_.set_is_context_secure(false);
707 FormFieldData field;
708 test::CreateTestFormField("Name on Card", "nameoncard", "", "text", &field);
709 form->fields.push_back(field);
710 test::CreateTestFormField("Card Number", "cardnumber", "", "text", &field);
711 form->fields.push_back(field);
712 if (use_month_type) {
713 test::CreateTestFormField(
714 "Expiration Date", "ccmonth", "", "month", &field);
715 form->fields.push_back(field);
716 } else {
717 test::CreateTestFormField("Expiration Date", "ccmonth", "", "text",
718 &field);
719 form->fields.push_back(field);
720 test::CreateTestFormField("", "ccyear", "", "text", &field);
721 form->fields.push_back(field);
725 // Tests if credit card data gets saved
726 void TestSaveCreditCards(bool is_https) {
727 // Set up our form data.
728 FormData form;
729 CreateTestCreditCardFormData(&form, is_https, false);
730 std::vector<FormData> forms(1, form);
731 FormsSeen(forms);
733 // Edit the data, and submit
734 form.fields[1].value = ASCIIToUTF16("4111111111111111");
735 form.fields[2].value = ASCIIToUTF16("11");
736 form.fields[3].value = ASCIIToUTF16("2017");
737 EXPECT_CALL(autofill_client_, ConfirmSaveCreditCard(_)).Times(1);
738 FormSubmitted(form);
741 void PrepareForRealPanResponse(FormData* form, CreditCard* card) {
742 // This line silences the warning from RealPanWalletClient about matching
743 // sync and wallet server types.
744 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
745 "sync-url", "https://google.com");
747 CreateTestCreditCardFormData(form, true, false);
748 FormsSeen(std::vector<FormData>(1, *form));
749 *card = CreditCard(CreditCard::MASKED_SERVER_CARD, "a123");
750 test::SetCreditCardInfo(card, "John Dillinger", "1881" /* Visa */, "01",
751 "2017");
752 card->SetTypeForMaskedCard(kVisaCard);
754 EXPECT_CALL(autofill_client_, ConfirmSaveCreditCard(_)).Times(0);
755 EXPECT_CALL(*autofill_driver_, SendFormDataToRenderer(_, _, _))
756 .Times(AtLeast(1));
757 autofill_manager_->FillOrPreviewCreditCardForm(
758 AutofillDriver::FORM_DATA_ACTION_FILL, kDefaultPageID, *form,
759 form->fields[0], *card);
761 #if defined(OS_IOS)
762 // Filling out the entire form on iOS requires requesting autofill on each
763 // of the form fields.
764 autofill_manager_->FillOrPreviewCreditCardForm(
765 AutofillDriver::FORM_DATA_ACTION_FILL, kDefaultPageID, *form,
766 form->fields[1], *card);
767 autofill_manager_->FillOrPreviewCreditCardForm(
768 AutofillDriver::FORM_DATA_ACTION_FILL, kDefaultPageID, *form,
769 form->fields[2], *card);
770 autofill_manager_->FillOrPreviewCreditCardForm(
771 AutofillDriver::FORM_DATA_ACTION_FILL, kDefaultPageID, *form,
772 form->fields[3], *card);
773 #endif // defined(OS_IOS)
776 protected:
777 base::MessageLoop message_loop_;
778 MockAutofillClient autofill_client_;
779 scoped_ptr<MockAutofillDriver> autofill_driver_;
780 scoped_ptr<TestAutofillManager> autofill_manager_;
781 scoped_ptr<TestAutofillExternalDelegate> external_delegate_;
782 scoped_refptr<net::TestURLRequestContextGetter> request_context_;
783 TestPersonalDataManager personal_data_;
786 class TestFormStructure : public FormStructure {
787 public:
788 explicit TestFormStructure(const FormData& form)
789 : FormStructure(form) {}
790 ~TestFormStructure() override {}
792 void SetFieldTypes(const std::vector<ServerFieldType>& heuristic_types,
793 const std::vector<ServerFieldType>& server_types) {
794 ASSERT_EQ(field_count(), heuristic_types.size());
795 ASSERT_EQ(field_count(), server_types.size());
797 for (size_t i = 0; i < field_count(); ++i) {
798 AutofillField* form_field = field(i);
799 ASSERT_TRUE(form_field);
800 form_field->set_heuristic_type(heuristic_types[i]);
801 form_field->set_server_type(server_types[i]);
804 UpdateAutofillCount();
807 private:
808 DISALLOW_COPY_AND_ASSIGN(TestFormStructure);
811 // Test that we return all address profile suggestions when all form fields are
812 // empty.
813 TEST_F(AutofillManagerTest, GetProfileSuggestionsEmptyValue) {
814 // Set up our form data.
815 FormData form;
816 test::CreateTestAddressFormData(&form);
817 std::vector<FormData> forms(1, form);
818 FormsSeen(forms);
820 const FormFieldData& field = form.fields[0];
821 GetAutofillSuggestions(form, field);
823 // No suggestions provided, so send an empty vector as the results.
824 // This triggers the combined message send.
825 AutocompleteSuggestionsReturned(std::vector<base::string16>());
827 // Test that we sent the right values to the external delegate. Inferred
828 // labels include full first relevant field, which in this case is the
829 // address line 1.
830 external_delegate_->CheckSuggestions(
831 kDefaultPageID,
832 Suggestion("Elvis", "3734 Elvis Presley Blvd.", "", 1),
833 Suggestion("Charles", "123 Apple St.", "", 2));
836 // Test that we return only matching address profile suggestions when the
837 // selected form field has been partially filled out.
838 TEST_F(AutofillManagerTest, GetProfileSuggestionsMatchCharacter) {
839 // Set up our form data.
840 FormData form;
841 test::CreateTestAddressFormData(&form);
842 std::vector<FormData> forms(1, form);
843 FormsSeen(forms);
845 FormFieldData field;
846 test::CreateTestFormField("First Name", "firstname", "E", "text",&field);
847 GetAutofillSuggestions(form, field);
849 // No suggestions provided, so send an empty vector as the results.
850 // This triggers the combined message send.
851 AutocompleteSuggestionsReturned(std::vector<base::string16>());
853 // Test that we sent the right values to the external delegate.
854 external_delegate_->CheckSuggestions(
855 kDefaultPageID,
856 Suggestion("Elvis", "3734 Elvis Presley Blvd.", "", 1));
859 // Test that we return no suggestions when the form has no relevant fields.
860 TEST_F(AutofillManagerTest, GetProfileSuggestionsUnknownFields) {
861 // Set up our form data.
862 FormData form;
863 form.name = ASCIIToUTF16("MyForm");
864 form.origin = GURL("http://myform.com/form.html");
865 form.action = GURL("http://myform.com/submit.html");
867 FormFieldData field;
868 test::CreateTestFormField("Username", "username", "", "text", &field);
869 form.fields.push_back(field);
870 test::CreateTestFormField("Password", "password", "", "password", &field);
871 form.fields.push_back(field);
872 test::CreateTestFormField("Quest", "quest", "", "quest", &field);
873 form.fields.push_back(field);
874 test::CreateTestFormField("Color", "color", "", "text", &field);
875 form.fields.push_back(field);
877 std::vector<FormData> forms(1, form);
878 FormsSeen(forms);
880 GetAutofillSuggestions(form, field);
881 EXPECT_FALSE(external_delegate_->on_suggestions_returned_seen());
884 // Test that we cull duplicate profile suggestions.
885 TEST_F(AutofillManagerTest, GetProfileSuggestionsWithDuplicates) {
886 // Set up our form data.
887 FormData form;
888 test::CreateTestAddressFormData(&form);
889 std::vector<FormData> forms(1, form);
890 FormsSeen(forms);
892 // Add a duplicate profile.
893 AutofillProfile* duplicate_profile =
894 new AutofillProfile(
895 *(autofill_manager_->GetProfileWithGUID(
896 "00000000-0000-0000-0000-000000000001")));
897 autofill_manager_->AddProfile(duplicate_profile);
899 const FormFieldData& field = form.fields[0];
900 GetAutofillSuggestions(form, field);
902 // No suggestions provided, so send an empty vector as the results.
903 // This triggers the combined message send.
904 AutocompleteSuggestionsReturned(std::vector<base::string16>());
906 // Test that we sent the right values to the external delegate.
907 external_delegate_->CheckSuggestions(
908 kDefaultPageID,
909 Suggestion("Elvis", "3734 Elvis Presley Blvd.", "", 1),
910 Suggestion("Charles", "123 Apple St.", "", 2));
913 // Test that we return no suggestions when autofill is disabled.
914 TEST_F(AutofillManagerTest, GetProfileSuggestionsAutofillDisabledByUser) {
915 // Set up our form data.
916 FormData form;
917 test::CreateTestAddressFormData(&form);
918 std::vector<FormData> forms(1, form);
919 FormsSeen(forms);
921 // Disable Autofill.
922 autofill_manager_->set_autofill_enabled(false);
924 const FormFieldData& field = form.fields[0];
925 GetAutofillSuggestions(form, field);
926 EXPECT_FALSE(external_delegate_->on_suggestions_returned_seen());
929 // Test that we return all credit card profile suggestions when all form fields
930 // are empty.
931 TEST_F(AutofillManagerTest, GetCreditCardSuggestionsEmptyValue) {
932 // Set up our form data.
933 FormData form;
934 CreateTestCreditCardFormData(&form, true, false);
935 std::vector<FormData> forms(1, form);
936 FormsSeen(forms);
938 FormFieldData field = form.fields[1];
939 GetAutofillSuggestions(form, field);
941 // No suggestions provided, so send an empty vector as the results.
942 // This triggers the combined message send.
943 AutocompleteSuggestionsReturned(std::vector<base::string16>());
945 // Test that we sent the right values to the external delegate.
946 external_delegate_->CheckSuggestions(
947 kDefaultPageID,
948 Suggestion(
949 "Visa\xC2\xA0\xE2\x8B\xAF"
950 "3456",
951 "04/12", kVisaCard, autofill_manager_->GetPackedCreditCardID(4)),
952 Suggestion(
953 "MasterCard\xC2\xA0\xE2\x8B\xAF"
954 "8765",
955 "10/14", kMasterCard, autofill_manager_->GetPackedCreditCardID(5)));
958 // Test that we return only matching credit card profile suggestions when the
959 // selected form field has been partially filled out.
960 TEST_F(AutofillManagerTest, GetCreditCardSuggestionsMatchCharacter) {
961 // Set up our form data.
962 FormData form;
963 CreateTestCreditCardFormData(&form, true, false);
964 std::vector<FormData> forms(1, form);
965 FormsSeen(forms);
967 FormFieldData field;
968 test::CreateTestFormField("Card Number", "cardnumber", "78", "text", &field);
969 GetAutofillSuggestions(form, field);
971 // No suggestions provided, so send an empty vector as the results.
972 // This triggers the combined message send.
973 AutocompleteSuggestionsReturned(std::vector<base::string16>());
975 // Test that we sent the right values to the external delegate.
976 external_delegate_->CheckSuggestions(
977 kDefaultPageID,
978 Suggestion(
979 "Visa\xC2\xA0\xE2\x8B\xAF"
980 "3456",
981 "04/12", kVisaCard, autofill_manager_->GetPackedCreditCardID(4)));
984 // Test that we return credit card profile suggestions when the selected form
985 // field is not the credit card number field.
986 TEST_F(AutofillManagerTest, GetCreditCardSuggestionsNonCCNumber) {
987 // Set up our form data.
988 FormData form;
989 CreateTestCreditCardFormData(&form, true, false);
990 std::vector<FormData> forms(1, form);
991 FormsSeen(forms);
993 const FormFieldData& field = form.fields[0];
994 GetAutofillSuggestions(form, field);
996 // No suggestions provided, so send an empty vector as the results.
997 // This triggers the combined message send.
998 AutocompleteSuggestionsReturned(std::vector<base::string16>());
1000 #if defined(OS_ANDROID)
1001 static const char* kVisaSuggestion =
1002 "Visa\xC2\xA0\xE2\x8B\xAF"
1003 "3456";
1004 static const char* kMcSuggestion =
1005 "MasterCard\xC2\xA0\xE2\x8B\xAF"
1006 "8765";
1007 #else
1008 static const char* kVisaSuggestion = "*3456";
1009 static const char* kMcSuggestion = "*8765";
1010 #endif
1012 // Test that we sent the right values to the external delegate.
1013 external_delegate_->CheckSuggestions(
1014 kDefaultPageID,
1015 Suggestion("Elvis Presley", kVisaSuggestion, kVisaCard,
1016 autofill_manager_->GetPackedCreditCardID(4)),
1017 Suggestion("Buddy Holly", kMcSuggestion, kMasterCard,
1018 autofill_manager_->GetPackedCreditCardID(5)));
1021 // Test that we return a warning explaining that credit card profile suggestions
1022 // are unavailable when the form is not secure.
1023 TEST_F(AutofillManagerTest, GetCreditCardSuggestionsNonHTTPS) {
1024 // Set up our form data.
1025 FormData form;
1026 CreateTestCreditCardFormData(&form, false, false);
1027 std::vector<FormData> forms(1, form);
1028 FormsSeen(forms);
1030 const FormFieldData& field = form.fields[0];
1031 GetAutofillSuggestions(form, field);
1033 // No suggestions provided, so send an empty vector as the results.
1034 // This triggers the combined message send.
1035 AutocompleteSuggestionsReturned(std::vector<base::string16>());
1037 // Test that we sent the right values to the external delegate.
1038 external_delegate_->CheckSuggestions(
1039 kDefaultPageID,
1040 Suggestion(
1041 l10n_util::GetStringUTF8(IDS_AUTOFILL_WARNING_INSECURE_CONNECTION),
1042 "", "", -1));
1044 // Now add some Autocomplete suggestions. We should show the autocomplete
1045 // suggestions and the warning.
1046 const int kPageID2 = 2;
1047 GetAutofillSuggestions(kPageID2, form, field);
1049 std::vector<base::string16> suggestions;
1050 suggestions.push_back(ASCIIToUTF16("Jay"));
1051 suggestions.push_back(ASCIIToUTF16("Jason"));
1052 AutocompleteSuggestionsReturned(suggestions);
1054 external_delegate_->CheckSuggestions(
1055 kPageID2,
1056 Suggestion(
1057 l10n_util::GetStringUTF8(IDS_AUTOFILL_WARNING_INSECURE_CONNECTION),
1058 "", "", -1),
1059 Suggestion("Jay", "", "", 0),
1060 Suggestion("Jason", "", "", 0));
1062 // Clear the test credit cards and try again -- we shouldn't return a warning.
1063 personal_data_.ClearCreditCards();
1064 GetAutofillSuggestions(form, field);
1065 EXPECT_FALSE(external_delegate_->on_suggestions_returned_seen());
1068 // Test that we return all credit card suggestions in the case that two cards
1069 // have the same obfuscated number.
1070 TEST_F(AutofillManagerTest, GetCreditCardSuggestionsRepeatedObfuscatedNumber) {
1071 // Add a credit card with the same obfuscated number as Elvis's.
1072 // |credit_card| will be owned by the mock PersonalDataManager.
1073 CreditCard* credit_card = new CreditCard;
1074 test::SetCreditCardInfo(credit_card, "Elvis Presley",
1075 "5231567890123456", // Mastercard
1076 "05", "2012");
1077 credit_card->set_guid("00000000-0000-0000-0000-000000000007");
1078 autofill_manager_->AddCreditCard(credit_card);
1080 // Set up our form data.
1081 FormData form;
1082 CreateTestCreditCardFormData(&form, true, false);
1083 std::vector<FormData> forms(1, form);
1084 FormsSeen(forms);
1086 FormFieldData field = form.fields[1];
1087 GetAutofillSuggestions(form, field);
1089 // No suggestions provided, so send an empty vector as the results.
1090 // This triggers the combined message send.
1091 AutocompleteSuggestionsReturned(std::vector<base::string16>());
1093 // Test that we sent the right values to the external delegate.
1094 external_delegate_->CheckSuggestions(
1095 kDefaultPageID,
1096 Suggestion(
1097 "Visa\xC2\xA0\xE2\x8B\xAF"
1098 "3456",
1099 "04/12", kVisaCard, autofill_manager_->GetPackedCreditCardID(4)),
1100 Suggestion(
1101 "MasterCard\xC2\xA0\xE2\x8B\xAF"
1102 "8765",
1103 "10/14", kMasterCard, autofill_manager_->GetPackedCreditCardID(5)),
1104 Suggestion(
1105 "MasterCard\xC2\xA0\xE2\x8B\xAF"
1106 "3456",
1107 "05/12", kMasterCard, autofill_manager_->GetPackedCreditCardID(7)));
1110 // Test that we return profile and credit card suggestions for combined forms.
1111 TEST_F(AutofillManagerTest, GetAddressAndCreditCardSuggestions) {
1112 // Set up our form data.
1113 FormData form;
1114 test::CreateTestAddressFormData(&form);
1115 CreateTestCreditCardFormData(&form, true, false);
1116 std::vector<FormData> forms(1, form);
1117 FormsSeen(forms);
1119 FormFieldData field = form.fields[0];
1120 GetAutofillSuggestions(form, field);
1122 // No suggestions provided, so send an empty vector as the results.
1123 // This triggers the combined message send.
1124 AutocompleteSuggestionsReturned(std::vector<base::string16>());
1126 // Test that we sent the right address suggestions to the external delegate.
1127 external_delegate_->CheckSuggestions(
1128 kDefaultPageID,
1129 Suggestion("Elvis", "3734 Elvis Presley Blvd.", "", 1),
1130 Suggestion("Charles", "123 Apple St.", "", 2));
1132 const int kPageID2 = 2;
1133 test::CreateTestFormField("Card Number", "cardnumber", "", "text", &field);
1134 GetAutofillSuggestions(kPageID2, form, field);
1136 // No suggestions provided, so send an empty vector as the results.
1137 // This triggers the combined message send.
1138 AutocompleteSuggestionsReturned(std::vector<base::string16>());
1140 // Test that we sent the credit card suggestions to the external delegate.
1141 external_delegate_->CheckSuggestions(
1142 kPageID2,
1143 Suggestion(
1144 "Visa\xC2\xA0\xE2\x8B\xAF"
1145 "3456",
1146 "04/12", kVisaCard, autofill_manager_->GetPackedCreditCardID(4)),
1147 Suggestion(
1148 "MasterCard\xC2\xA0\xE2\x8B\xAF"
1149 "8765",
1150 "10/14", kMasterCard, autofill_manager_->GetPackedCreditCardID(5)));
1153 // Test that for non-https forms with both address and credit card fields, we
1154 // only return address suggestions. Instead of credit card suggestions, we
1155 // should return a warning explaining that credit card profile suggestions are
1156 // unavailable when the form is not https.
1157 TEST_F(AutofillManagerTest, GetAddressAndCreditCardSuggestionsNonHttps) {
1158 // Set up our form data.
1159 FormData form;
1160 test::CreateTestAddressFormData(&form);
1161 CreateTestCreditCardFormData(&form, false, false);
1162 std::vector<FormData> forms(1, form);
1163 FormsSeen(forms);
1165 FormFieldData field = form.fields[0];
1166 GetAutofillSuggestions(form, field);
1168 // No suggestions provided, so send an empty vector as the results.
1169 // This triggers the combined message send.
1170 AutocompleteSuggestionsReturned(std::vector<base::string16>());
1172 // Test that we sent the right suggestions to the external delegate.
1173 external_delegate_->CheckSuggestions(
1174 kDefaultPageID,
1175 Suggestion("Elvis", "3734 Elvis Presley Blvd.", "", 1),
1176 Suggestion("Charles", "123 Apple St.", "", 2));
1178 test::CreateTestFormField("Card Number", "cardnumber", "", "text", &field);
1179 const int kPageID2 = 2;
1180 GetAutofillSuggestions(kPageID2, form, field);
1182 // No suggestions provided, so send an empty vector as the results.
1183 // This triggers the combined message send.
1184 AutocompleteSuggestionsReturned(std::vector<base::string16>());
1186 // Test that we sent the right values to the external delegate.
1187 external_delegate_->CheckSuggestions(
1188 kPageID2,
1189 Suggestion(
1190 l10n_util::GetStringUTF8(IDS_AUTOFILL_WARNING_INSECURE_CONNECTION),
1191 "", "", -1));
1193 // Clear the test credit cards and try again -- we shouldn't return a warning.
1194 personal_data_.ClearCreditCards();
1195 GetAutofillSuggestions(form, field);
1196 EXPECT_FALSE(external_delegate_->on_suggestions_returned_seen());
1199 // Test that we correctly combine autofill and autocomplete suggestions.
1200 TEST_F(AutofillManagerTest, GetCombinedAutofillAndAutocompleteSuggestions) {
1201 // Set up our form data.
1202 FormData form;
1203 test::CreateTestAddressFormData(&form);
1204 std::vector<FormData> forms(1, form);
1205 FormsSeen(forms);
1207 const FormFieldData& field = form.fields[0];
1208 GetAutofillSuggestions(form, field);
1210 // Add some Autocomplete suggestions.
1211 // This triggers the combined message send.
1212 std::vector<base::string16> suggestions;
1213 suggestions.push_back(ASCIIToUTF16("Jay"));
1214 // This suggestion is a duplicate, and should be trimmed.
1215 suggestions.push_back(ASCIIToUTF16("Elvis"));
1216 suggestions.push_back(ASCIIToUTF16("Jason"));
1217 AutocompleteSuggestionsReturned(suggestions);
1219 // Test that we sent the right values to the external delegate.
1220 Suggestion expected[] = {
1221 Suggestion("Elvis", "3734 Elvis Presley Blvd.", "", 1),
1222 Suggestion("Charles", "123 Apple St.", "", 2),
1223 Suggestion("Jay", "", "", 0),
1224 Suggestion("Jason", "", "", 0),
1226 external_delegate_->CheckSuggestions(
1227 kDefaultPageID, arraysize(expected), expected);
1230 // Test that we return autocomplete-like suggestions when trying to autofill
1231 // already filled forms.
1232 TEST_F(AutofillManagerTest, GetFieldSuggestionsWhenFormIsAutofilled) {
1233 // Set up our form data.
1234 FormData form;
1235 test::CreateTestAddressFormData(&form);
1236 std::vector<FormData> forms(1, form);
1237 FormsSeen(forms);
1239 // Mark one of the fields as filled.
1240 form.fields[2].is_autofilled = true;
1241 const FormFieldData& field = form.fields[0];
1242 GetAutofillSuggestions(form, field);
1244 // No suggestions provided, so send an empty vector as the results.
1245 // This triggers the combined message send.
1246 AutocompleteSuggestionsReturned(std::vector<base::string16>());
1248 // Test that we sent the right values to the external delegate.
1249 external_delegate_->CheckSuggestions(
1250 kDefaultPageID,
1251 Suggestion("Elvis", "", "", 1),
1252 Suggestion("Charles", "", "", 2));
1255 // Test that nothing breaks when there are autocomplete suggestions but no
1256 // autofill suggestions.
1257 TEST_F(AutofillManagerTest, GetFieldSuggestionsForAutocompleteOnly) {
1258 // Set up our form data.
1259 FormData form;
1260 test::CreateTestAddressFormData(&form);
1261 FormFieldData field;
1262 test::CreateTestFormField("Some Field", "somefield", "", "text", &field);
1263 form.fields.push_back(field);
1264 std::vector<FormData> forms(1, form);
1265 FormsSeen(forms);
1267 GetAutofillSuggestions(form, field);
1269 // Add some Autocomplete suggestions.
1270 // This triggers the combined message send.
1271 std::vector<base::string16> suggestions;
1272 suggestions.push_back(ASCIIToUTF16("one"));
1273 suggestions.push_back(ASCIIToUTF16("two"));
1274 AutocompleteSuggestionsReturned(suggestions);
1276 // Test that we sent the right values to the external delegate.
1277 external_delegate_->CheckSuggestions(
1278 kDefaultPageID,
1279 Suggestion("one", "", "", 0),
1280 Suggestion("two", "", "", 0));
1283 // Test that we do not return duplicate values drawn from multiple profiles when
1284 // filling an already filled field.
1285 TEST_F(AutofillManagerTest, GetFieldSuggestionsWithDuplicateValues) {
1286 // Set up our form data.
1287 FormData form;
1288 test::CreateTestAddressFormData(&form);
1289 std::vector<FormData> forms(1, form);
1290 FormsSeen(forms);
1292 // |profile| will be owned by the mock PersonalDataManager.
1293 AutofillProfile* profile = new AutofillProfile;
1294 test::SetProfileInfo(
1295 profile, "Elvis", "", "", "", "", "", "", "", "", "", "", "");
1296 profile->set_guid("00000000-0000-0000-0000-000000000101");
1297 autofill_manager_->AddProfile(profile);
1299 FormFieldData& field = form.fields[0];
1300 field.is_autofilled = true;
1301 field.value = ASCIIToUTF16("Elvis");
1302 GetAutofillSuggestions(form, field);
1304 // No suggestions provided, so send an empty vector as the results.
1305 // This triggers the combined message send.
1306 AutocompleteSuggestionsReturned(std::vector<base::string16>());
1308 // Test that we sent the right values to the external delegate.
1309 external_delegate_->CheckSuggestions(
1310 kDefaultPageID,
1311 Suggestion("Elvis", "", "", 1));
1314 TEST_F(AutofillManagerTest, GetProfileSuggestionsFancyPhone) {
1315 // Set up our form data.
1316 FormData form;
1317 test::CreateTestAddressFormData(&form);
1318 std::vector<FormData> forms(1, form);
1319 FormsSeen(forms);
1321 AutofillProfile* profile = new AutofillProfile;
1322 profile->set_guid("00000000-0000-0000-0000-000000000103");
1323 profile->SetInfo(AutofillType(NAME_FULL), ASCIIToUTF16("Natty Bumppo"),
1324 "en-US");
1325 profile->SetRawInfo(PHONE_HOME_WHOLE_NUMBER,
1326 ASCIIToUTF16("1800PRAIRIE"));
1327 autofill_manager_->AddProfile(profile);
1329 const FormFieldData& field = form.fields[9];
1330 GetAutofillSuggestions(form, field);
1332 // No suggestions provided, so send an empty vector as the results.
1333 // This triggers the combined message send.
1334 AutocompleteSuggestionsReturned(std::vector<base::string16>());
1336 // Test that we sent the right values to the external delegate. Inferred
1337 // labels include the most private field of those that would be filled.
1338 external_delegate_->CheckSuggestions(
1339 kDefaultPageID,
1340 Suggestion("12345678901", "3734 Elvis Presley Blvd.", "", 1),
1341 Suggestion("23456789012", "123 Apple St.", "", 2),
1342 Suggestion("18007724743", "Natty Bumppo", "", 3)); // 1800PRAIRIE
1345 TEST_F(AutofillManagerTest, GetProfileSuggestionsForPhonePrefixOrSuffix) {
1346 // Set up our form data.
1347 FormData form;
1348 form.name = ASCIIToUTF16("MyForm");
1349 form.origin = GURL("http://myform.com/form.html");
1350 form.action = GURL("http://myform.com/submit.html");
1352 struct {
1353 const char* const label;
1354 const char* const name;
1355 size_t max_length;
1356 const char* const autocomplete_attribute;
1357 } test_fields[] = {{"country code", "country_code", 1, "tel-country-code"},
1358 {"area code", "area_code", 3, "tel-area-code"},
1359 {"phone", "phone_prefix", 3, "tel-local-prefix"},
1360 {"-", "phone_suffix", 4, "tel-local-suffix"},
1361 {"Phone Extension", "ext", 5, "tel-extension"}};
1363 FormFieldData field;
1364 for (size_t i = 0; i < arraysize(test_fields); ++i) {
1365 test::CreateTestFormField(
1366 test_fields[i].label, test_fields[i].name, "", "text", &field);
1367 field.max_length = test_fields[i].max_length;
1368 field.autocomplete_attribute = std::string();
1369 form.fields.push_back(field);
1372 std::vector<FormData> forms(1, form);
1373 FormsSeen(forms);
1375 AutofillProfile* profile = new AutofillProfile;
1376 profile->set_guid("00000000-0000-0000-0000-000000000104");
1377 profile->SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("1800FLOWERS"));
1378 personal_data_.ClearAutofillProfiles();
1379 autofill_manager_->AddProfile(profile);
1381 // The sublabels here are somewhat braindead until crbug.com/493247 is fixed.
1382 // TODO(estade): fix the bug and fix this test.
1383 const FormFieldData& phone_prefix = form.fields[2];
1384 GetAutofillSuggestions(form, phone_prefix);
1385 AutocompleteSuggestionsReturned(std::vector<base::string16>());
1386 // Test that we sent the right prefix values to the external delegate.
1387 external_delegate_->CheckSuggestions(kDefaultPageID,
1388 Suggestion("356", "18003569377", "", 1));
1390 const FormFieldData& phone_suffix = form.fields[3];
1391 GetAutofillSuggestions(form, phone_suffix);
1392 AutocompleteSuggestionsReturned(std::vector<base::string16>());
1393 // Test that we sent the right suffix values to the external delegate.
1394 external_delegate_->CheckSuggestions(
1395 kDefaultPageID, Suggestion("9377", "18003569377", "", 1));
1398 // Test that we correctly fill an address form.
1399 TEST_F(AutofillManagerTest, FillAddressForm) {
1400 // Set up our form data.
1401 FormData form;
1402 test::CreateTestAddressFormData(&form);
1403 std::vector<FormData> forms(1, form);
1404 FormsSeen(forms);
1406 std::string guid("00000000-0000-0000-0000-000000000001");
1407 AutofillProfile* profile =
1408 autofill_manager_->GetProfileWithGUID(guid.c_str());
1409 ASSERT_TRUE(profile);
1410 EXPECT_EQ(0U, profile->use_count());
1411 EXPECT_EQ(base::Time(), profile->use_date());
1413 int response_page_id = 0;
1414 FormData response_data;
1415 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0],
1416 MakeFrontendID(std::string(), guid),
1417 &response_page_id, &response_data);
1418 ExpectFilledAddressFormElvis(response_page_id, response_data, kDefaultPageID,
1419 false);
1421 EXPECT_EQ(1U, profile->use_count());
1422 EXPECT_NE(base::Time(), profile->use_date());
1425 TEST_F(AutofillManagerTest, WillFillCreditCardNumber) {
1426 // Set up our form data.
1427 FormData form;
1428 CreateTestCreditCardFormData(&form, true, false);
1429 std::vector<FormData> forms(1, form);
1430 FormsSeen(forms);
1432 FormFieldData* number_field = nullptr;
1433 FormFieldData* name_field = nullptr;
1434 FormFieldData* month_field = nullptr;
1435 for (size_t i = 0; i < form.fields.size(); ++i) {
1436 if (form.fields[i].name == ASCIIToUTF16("cardnumber"))
1437 number_field = &form.fields[i];
1438 else if (form.fields[i].name == ASCIIToUTF16("nameoncard"))
1439 name_field = &form.fields[i];
1440 else if (form.fields[i].name == ASCIIToUTF16("ccmonth"))
1441 month_field = &form.fields[i];
1444 // Empty form - whole form is Autofilled (except on iOS).
1445 EXPECT_TRUE(WillFillCreditCardNumber(form, *number_field));
1446 #if defined(OS_IOS)
1447 EXPECT_FALSE(WillFillCreditCardNumber(form, *name_field));
1448 #else
1449 EXPECT_TRUE(WillFillCreditCardNumber(form, *name_field));
1450 #endif // defined(OS_IOS)
1452 // If the user has entered a value, it won't be overridden.
1453 number_field->value = ASCIIToUTF16("gibberish");
1454 EXPECT_TRUE(WillFillCreditCardNumber(form, *number_field));
1455 EXPECT_FALSE(WillFillCreditCardNumber(form, *name_field));
1457 // But if that value is removed, it will be Autofilled (except on iOS).
1458 number_field->value.clear();
1459 #if defined(OS_IOS)
1460 EXPECT_FALSE(WillFillCreditCardNumber(form, *name_field));
1461 #else
1462 EXPECT_TRUE(WillFillCreditCardNumber(form, *name_field));
1463 #endif // defined(OS_IOS)
1465 // When part of the section is Autofilled, only fill the initiating field.
1466 month_field->is_autofilled = true;
1467 EXPECT_FALSE(WillFillCreditCardNumber(form, *name_field));
1468 EXPECT_TRUE(WillFillCreditCardNumber(form, *number_field));
1471 // Test that we correctly fill a credit card form.
1472 TEST_F(AutofillManagerTest, FillCreditCardForm) {
1473 // Set up our form data.
1474 FormData form;
1475 CreateTestCreditCardFormData(&form, true, false);
1476 std::vector<FormData> forms(1, form);
1477 FormsSeen(forms);
1479 std::string guid("00000000-0000-0000-0000-000000000004");
1480 int response_page_id = 0;
1481 FormData response_data;
1482 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, *form.fields.begin(),
1483 MakeFrontendID(guid, std::string()),
1484 &response_page_id, &response_data);
1485 ExpectFilledCreditCardFormElvis(
1486 response_page_id, response_data, kDefaultPageID, false);
1489 // Test that we correctly fill a credit card form with month input type.
1490 // 1. year empty, month empty
1491 TEST_F(AutofillManagerTest, FillCreditCardFormNoYearNoMonth) {
1492 // Same as the SetUp(), but generate 4 credit cards with year month
1493 // combination.
1494 personal_data_.CreateTestCreditCardsYearAndMonth("", "");
1495 // Set up our form data.
1496 FormData form;
1497 CreateTestCreditCardFormData(&form, true, true);
1498 std::vector<FormData> forms(1, form);
1499 FormsSeen(forms);
1501 std::string guid("00000000-0000-0000-0000-000000000007");
1502 int response_page_id = 0;
1503 FormData response_data;
1504 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, *form.fields.begin(),
1505 MakeFrontendID(guid, std::string()),
1506 &response_page_id, &response_data);
1507 ExpectFilledCreditCardYearMonthWithYearMonth(response_page_id, response_data,
1508 kDefaultPageID, false, "", "");
1512 // Test that we correctly fill a credit card form with month input type.
1513 // 2. year empty, month non-empty
1514 TEST_F(AutofillManagerTest, FillCreditCardFormNoYearMonth) {
1515 // Same as the SetUp(), but generate 4 credit cards with year month
1516 // combination.
1517 personal_data_.CreateTestCreditCardsYearAndMonth("", "04");
1518 // Set up our form data.
1519 FormData form;
1520 CreateTestCreditCardFormData(&form, true, true);
1521 std::vector<FormData> forms(1, form);
1522 FormsSeen(forms);
1524 std::string guid("00000000-0000-0000-0000-000000000007");
1525 int response_page_id = 0;
1526 FormData response_data;
1527 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, *form.fields.begin(),
1528 MakeFrontendID(guid, std::string()),
1529 &response_page_id, &response_data);
1530 ExpectFilledCreditCardYearMonthWithYearMonth(response_page_id, response_data,
1531 kDefaultPageID, false, "", "04");
1534 // Test that we correctly fill a credit card form with month input type.
1535 // 3. year non-empty, month empty
1536 TEST_F(AutofillManagerTest, FillCreditCardFormYearNoMonth) {
1537 // Same as the SetUp(), but generate 4 credit cards with year month
1538 // combination.
1539 personal_data_.CreateTestCreditCardsYearAndMonth("2012", "");
1540 // Set up our form data.
1541 FormData form;
1542 CreateTestCreditCardFormData(&form, true, true);
1543 std::vector<FormData> forms(1, form);
1544 FormsSeen(forms);
1546 std::string guid("00000000-0000-0000-0000-000000000007");
1547 int response_page_id = 0;
1548 FormData response_data;
1549 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, *form.fields.begin(),
1550 MakeFrontendID(guid, std::string()),
1551 &response_page_id, &response_data);
1552 ExpectFilledCreditCardYearMonthWithYearMonth(response_page_id, response_data,
1553 kDefaultPageID, false, "2012", "");
1556 // Test that we correctly fill a credit card form with month input type.
1557 // 4. year non-empty, month empty
1558 TEST_F(AutofillManagerTest, FillCreditCardFormYearMonth) {
1559 // Same as the SetUp(), but generate 4 credit cards with year month
1560 // combination.
1561 personal_data_.ClearCreditCards();
1562 personal_data_.CreateTestCreditCardsYearAndMonth("2012", "04");
1563 // Set up our form data.
1564 FormData form;
1565 CreateTestCreditCardFormData(&form, true, true);
1566 std::vector<FormData> forms(1, form);
1567 FormsSeen(forms);
1569 std::string guid("00000000-0000-0000-0000-000000000007");
1570 int response_page_id = 0;
1571 FormData response_data;
1572 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, *form.fields.begin(),
1573 MakeFrontendID(guid, std::string()),
1574 &response_page_id, &response_data);
1575 ExpectFilledCreditCardYearMonthWithYearMonth(response_page_id, response_data,
1576 kDefaultPageID, false, "2012", "04");
1579 // Test that we correctly fill a combined address and credit card form.
1580 TEST_F(AutofillManagerTest, FillAddressAndCreditCardForm) {
1581 // Set up our form data.
1582 FormData form;
1583 test::CreateTestAddressFormData(&form);
1584 CreateTestCreditCardFormData(&form, true, false);
1585 std::vector<FormData> forms(1, form);
1586 FormsSeen(forms);
1588 // First fill the address data.
1589 std::string guid("00000000-0000-0000-0000-000000000001");
1590 int response_page_id = 0;
1591 FormData response_data;
1593 SCOPED_TRACE("Address");
1594 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0],
1595 MakeFrontendID(std::string(), guid),
1596 &response_page_id, &response_data);
1597 ExpectFilledAddressFormElvis(response_page_id, response_data,
1598 kDefaultPageID, true);
1601 // Now fill the credit card data.
1602 const int kPageID2 = 2;
1603 std::string guid2("00000000-0000-0000-0000-000000000004");
1604 response_page_id = 0;
1606 FillAutofillFormDataAndSaveResults(kPageID2, form, form.fields.back(),
1607 MakeFrontendID(guid2, std::string()),
1608 &response_page_id, &response_data);
1609 SCOPED_TRACE("Credit card");
1610 ExpectFilledCreditCardFormElvis(
1611 response_page_id, response_data, kPageID2, true);
1615 // Test that non-focusable field is ignored while inferring boundaries between
1616 // sections: http://crbug.com/231160
1617 TEST_F(AutofillManagerTest, FillFormWithNonFocusableFields) {
1618 // Create a form with both focusable and non-focusable fields.
1619 FormData form;
1620 form.name = ASCIIToUTF16("MyForm");
1621 form.origin = GURL("https://myform.com/form.html");
1622 form.action = GURL("https://myform.com/submit.html");
1624 FormFieldData field;
1626 test::CreateTestFormField("First Name", "firstname", "", "text", &field);
1627 form.fields.push_back(field);
1629 test::CreateTestFormField("", "lastname", "", "text", &field);
1630 form.fields.push_back(field);
1632 test::CreateTestFormField("", "email", "", "text", &field);
1633 form.fields.push_back(field);
1635 test::CreateTestFormField("Phone Number", "phonenumber", "", "tel", &field);
1636 form.fields.push_back(field);
1638 test::CreateTestFormField("", "email_", "", "text", &field);
1639 field.is_focusable = false;
1640 form.fields.push_back(field);
1642 test::CreateTestFormField("Country", "country", "", "text", &field);
1643 form.fields.push_back(field);
1645 std::vector<FormData> forms(1, form);
1646 FormsSeen(forms);
1648 // Fill the form
1649 std::string guid("00000000-0000-0000-0000-000000000001");
1650 int response_page_id = 0;
1651 FormData response_data;
1652 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0],
1653 MakeFrontendID(std::string(), guid),
1654 &response_page_id, &response_data);
1656 // The whole form should be filled as all the fields belong to the same
1657 // logical section.
1658 ASSERT_EQ(6U, response_data.fields.size());
1659 ExpectFilledField("First Name", "firstname", "Elvis", "text",
1660 response_data.fields[0]);
1661 ExpectFilledField("", "lastname", "Presley", "text",
1662 response_data.fields[1]);
1663 ExpectFilledField("", "email", "theking@gmail.com", "text",
1664 response_data.fields[2]);
1665 ExpectFilledField("Phone Number", "phonenumber", "12345678901", "tel",
1666 response_data.fields[3]);
1667 ExpectFilledField("", "email_", "theking@gmail.com", "text",
1668 response_data.fields[4]);
1669 ExpectFilledField("Country", "country", "United States", "text",
1670 response_data.fields[5]);
1673 // Test that we correctly fill a form that has multiple logical sections, e.g.
1674 // both a billing and a shipping address.
1675 TEST_F(AutofillManagerTest, FillFormWithMultipleSections) {
1676 // Set up our form data.
1677 FormData form;
1678 test::CreateTestAddressFormData(&form);
1679 const size_t kAddressFormSize = form.fields.size();
1680 test::CreateTestAddressFormData(&form);
1681 for (size_t i = kAddressFormSize; i < form.fields.size(); ++i) {
1682 // Make sure the fields have distinct names.
1683 form.fields[i].name = form.fields[i].name + ASCIIToUTF16("_");
1685 std::vector<FormData> forms(1, form);
1686 FormsSeen(forms);
1688 // Fill the first section.
1689 std::string guid("00000000-0000-0000-0000-000000000001");
1690 int response_page_id = 0;
1691 FormData response_data;
1692 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0],
1693 MakeFrontendID(std::string(), guid),
1694 &response_page_id, &response_data);
1696 SCOPED_TRACE("Address 1");
1697 // The second address section should be empty.
1698 ASSERT_EQ(response_data.fields.size(), 2*kAddressFormSize);
1699 for (size_t i = kAddressFormSize; i < form.fields.size(); ++i) {
1700 EXPECT_EQ(base::string16(), response_data.fields[i].value);
1703 // The first address section should be filled with Elvis's data.
1704 response_data.fields.resize(kAddressFormSize);
1705 ExpectFilledAddressFormElvis(response_page_id, response_data,
1706 kDefaultPageID, false);
1709 // Fill the second section, with the initiating field somewhere in the middle
1710 // of the section.
1711 const int kPageID2 = 2;
1712 std::string guid2("00000000-0000-0000-0000-000000000001");
1713 ASSERT_LT(9U, kAddressFormSize);
1714 response_page_id = 0;
1715 FillAutofillFormDataAndSaveResults(
1716 kPageID2, form, form.fields[kAddressFormSize + 9],
1717 MakeFrontendID(std::string(), guid2), &response_page_id, &response_data);
1719 SCOPED_TRACE("Address 2");
1720 ASSERT_EQ(response_data.fields.size(), form.fields.size());
1722 // The first address section should be empty.
1723 ASSERT_EQ(response_data.fields.size(), 2*kAddressFormSize);
1724 for (size_t i = 0; i < kAddressFormSize; ++i) {
1725 EXPECT_EQ(base::string16(), response_data.fields[i].value);
1728 // The second address section should be filled with Elvis's data.
1729 FormData secondSection = response_data;
1730 secondSection.fields.erase(secondSection.fields.begin(),
1731 secondSection.fields.begin() + kAddressFormSize);
1732 for (size_t i = 0; i < kAddressFormSize; ++i) {
1733 // Restore the expected field names.
1734 base::string16 name = secondSection.fields[i].name;
1735 base::string16 original_name = name.substr(0, name.size() - 1);
1736 secondSection.fields[i].name = original_name;
1738 ExpectFilledAddressFormElvis(response_page_id, secondSection, kPageID2,
1739 false);
1743 // Test that we correctly fill a form that has author-specified sections, which
1744 // might not match our expected section breakdown.
1745 TEST_F(AutofillManagerTest, FillFormWithAuthorSpecifiedSections) {
1746 // Create a form with a billing section and an unnamed section, interleaved.
1747 // The billing section includes both address and credit card fields.
1748 FormData form;
1749 form.name = ASCIIToUTF16("MyForm");
1750 form.origin = GURL("https://myform.com/form.html");
1751 form.action = GURL("https://myform.com/submit.html");
1753 FormFieldData field;
1755 test::CreateTestFormField("", "country", "", "text", &field);
1756 field.autocomplete_attribute = "section-billing country";
1757 form.fields.push_back(field);
1759 test::CreateTestFormField("", "firstname", "", "text", &field);
1760 field.autocomplete_attribute = "given-name";
1761 form.fields.push_back(field);
1763 test::CreateTestFormField("", "lastname", "", "text", &field);
1764 field.autocomplete_attribute = "family-name";
1765 form.fields.push_back(field);
1767 test::CreateTestFormField("", "address", "", "text", &field);
1768 field.autocomplete_attribute = "section-billing address-line1";
1769 form.fields.push_back(field);
1771 test::CreateTestFormField("", "city", "", "text", &field);
1772 field.autocomplete_attribute = "section-billing locality";
1773 form.fields.push_back(field);
1775 test::CreateTestFormField("", "state", "", "text", &field);
1776 field.autocomplete_attribute = "section-billing region";
1777 form.fields.push_back(field);
1779 test::CreateTestFormField("", "zip", "", "text", &field);
1780 field.autocomplete_attribute = "section-billing postal-code";
1781 form.fields.push_back(field);
1783 test::CreateTestFormField("", "ccname", "", "text", &field);
1784 field.autocomplete_attribute = "section-billing cc-name";
1785 form.fields.push_back(field);
1787 test::CreateTestFormField("", "ccnumber", "", "text", &field);
1788 field.autocomplete_attribute = "section-billing cc-number";
1789 form.fields.push_back(field);
1791 test::CreateTestFormField("", "ccexp", "", "text", &field);
1792 field.autocomplete_attribute = "section-billing cc-exp";
1793 form.fields.push_back(field);
1795 test::CreateTestFormField("", "email", "", "text", &field);
1796 field.autocomplete_attribute = "email";
1797 form.fields.push_back(field);
1799 std::vector<FormData> forms(1, form);
1800 FormsSeen(forms);
1802 // Fill the unnamed section.
1803 std::string guid("00000000-0000-0000-0000-000000000001");
1804 int response_page_id = 0;
1805 FormData response_data;
1806 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[1],
1807 MakeFrontendID(std::string(), guid),
1808 &response_page_id, &response_data);
1810 SCOPED_TRACE("Unnamed section");
1811 EXPECT_EQ(kDefaultPageID, response_page_id);
1812 EXPECT_EQ(ASCIIToUTF16("MyForm"), response_data.name);
1813 EXPECT_EQ(GURL("https://myform.com/form.html"), response_data.origin);
1814 EXPECT_EQ(GURL("https://myform.com/submit.html"), response_data.action);
1815 ASSERT_EQ(11U, response_data.fields.size());
1817 ExpectFilledField("", "country", "", "text", response_data.fields[0]);
1818 ExpectFilledField("", "firstname", "Elvis", "text",
1819 response_data.fields[1]);
1820 ExpectFilledField("", "lastname", "Presley", "text",
1821 response_data.fields[2]);
1822 ExpectFilledField("", "address", "", "text", response_data.fields[3]);
1823 ExpectFilledField("", "city", "", "text", response_data.fields[4]);
1824 ExpectFilledField("", "state", "", "text", response_data.fields[5]);
1825 ExpectFilledField("", "zip", "", "text", response_data.fields[6]);
1826 ExpectFilledField("", "ccname", "", "text", response_data.fields[7]);
1827 ExpectFilledField("", "ccnumber", "", "text", response_data.fields[8]);
1828 ExpectFilledField("", "ccexp", "", "text", response_data.fields[9]);
1829 ExpectFilledField("", "email", "theking@gmail.com", "text",
1830 response_data.fields[10]);
1833 // Fill the address portion of the billing section.
1834 const int kPageID2 = 2;
1835 std::string guid2("00000000-0000-0000-0000-000000000001");
1836 response_page_id = 0;
1837 FillAutofillFormDataAndSaveResults(kPageID2, form, form.fields[0],
1838 MakeFrontendID(std::string(), guid2),
1839 &response_page_id, &response_data);
1841 SCOPED_TRACE("Billing address");
1842 EXPECT_EQ(kPageID2, response_page_id);
1843 EXPECT_EQ(ASCIIToUTF16("MyForm"), response_data.name);
1844 EXPECT_EQ(GURL("https://myform.com/form.html"), response_data.origin);
1845 EXPECT_EQ(GURL("https://myform.com/submit.html"), response_data.action);
1846 ASSERT_EQ(11U, response_data.fields.size());
1848 ExpectFilledField("", "country", "US", "text",
1849 response_data.fields[0]);
1850 ExpectFilledField("", "firstname", "", "text", response_data.fields[1]);
1851 ExpectFilledField("", "lastname", "", "text", response_data.fields[2]);
1852 ExpectFilledField("", "address", "3734 Elvis Presley Blvd.", "text",
1853 response_data.fields[3]);
1854 ExpectFilledField("", "city", "Memphis", "text", response_data.fields[4]);
1855 ExpectFilledField("", "state", "Tennessee", "text",
1856 response_data.fields[5]);
1857 ExpectFilledField("", "zip", "38116", "text", response_data.fields[6]);
1858 ExpectFilledField("", "ccname", "", "text", response_data.fields[7]);
1859 ExpectFilledField("", "ccnumber", "", "text", response_data.fields[8]);
1860 ExpectFilledField("", "ccexp", "", "text", response_data.fields[9]);
1861 ExpectFilledField("", "email", "", "text", response_data.fields[10]);
1864 // Fill the credit card portion of the billing section.
1865 const int kPageID3 = 3;
1866 std::string guid3("00000000-0000-0000-0000-000000000004");
1867 response_page_id = 0;
1868 FillAutofillFormDataAndSaveResults(
1869 kPageID3, form, form.fields[form.fields.size() - 2],
1870 MakeFrontendID(guid3, std::string()), &response_page_id, &response_data);
1872 SCOPED_TRACE("Credit card");
1873 EXPECT_EQ(kPageID3, response_page_id);
1874 EXPECT_EQ(ASCIIToUTF16("MyForm"), response_data.name);
1875 EXPECT_EQ(GURL("https://myform.com/form.html"), response_data.origin);
1876 EXPECT_EQ(GURL("https://myform.com/submit.html"), response_data.action);
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 std::string guid("00000000-0000-0000-0000-000000000001");
1910 int response_page_id = 0;
1911 FormData response_data;
1912 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0],
1913 MakeFrontendID(std::string(), guid),
1914 &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(response_page_id, response_data, kDefaultPageID,
1923 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 std::string guid("00000000-0000-0000-0000-000000000001");
1939 int response_page_id = 0;
1940 FormData response_data;
1941 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, *form.fields.begin(),
1942 MakeFrontendID(std::string(), guid),
1943 &response_page_id, &response_data);
1945 SCOPED_TRACE("Address");
1946 ExpectFilledForm(response_page_id, response_data, kDefaultPageID, "Elvis",
1947 "", "", "", "", "", "", "", "", "", "", "", "", "", "",
1948 true, true, false);
1951 // Now fill the credit card data.
1952 const int kPageID2 = 2;
1953 std::string guid2("00000000-0000-0000-0000-000000000004");
1954 response_page_id = 0;
1955 FillAutofillFormDataAndSaveResults(kPageID2, form, form.fields.back(),
1956 MakeFrontendID(guid2, std::string()),
1957 &response_page_id, &response_data);
1959 SCOPED_TRACE("Credit card 1");
1960 ExpectFilledCreditCardFormElvis(
1961 response_page_id, response_data, kPageID2, true);
1964 // Now set the credit card fields to also be auto-filled, and try again to
1965 // fill the credit card data
1966 for (std::vector<FormFieldData>::iterator iter = form.fields.begin();
1967 iter != form.fields.end();
1968 ++iter) {
1969 iter->is_autofilled = true;
1972 const int kPageID3 = 3;
1973 response_page_id = 0;
1974 FillAutofillFormDataAndSaveResults(kPageID3, form, *form.fields.rbegin(),
1975 MakeFrontendID(guid2, std::string()),
1976 &response_page_id, &response_data);
1978 SCOPED_TRACE("Credit card 2");
1979 ExpectFilledForm(response_page_id, response_data, kPageID3, "", "", "", "",
1980 "", "", "", "", "", "", "", "", "", "", "2012", true, true,
1981 false);
1985 // Test that we correctly fill a phone number split across multiple fields.
1986 TEST_F(AutofillManagerTest, FillPhoneNumber) {
1987 // In one form, rely on the maxlength attribute to imply phone number parts.
1988 // In the other form, rely on the autocompletetype attribute.
1989 FormData form_with_maxlength;
1990 form_with_maxlength.name = ASCIIToUTF16("MyMaxlengthPhoneForm");
1991 form_with_maxlength.origin = GURL("http://myform.com/phone_form.html");
1992 form_with_maxlength.action = GURL("http://myform.com/phone_submit.html");
1993 FormData form_with_autocompletetype = form_with_maxlength;
1994 form_with_autocompletetype.name = ASCIIToUTF16("MyAutocompletetypePhoneForm");
1996 struct {
1997 const char* label;
1998 const char* name;
1999 size_t max_length;
2000 const char* autocomplete_attribute;
2001 } test_fields[] = {
2002 { "country code", "country_code", 1, "tel-country-code" },
2003 { "area code", "area_code", 3, "tel-area-code" },
2004 { "phone", "phone_prefix", 3, "tel-local-prefix" },
2005 { "-", "phone_suffix", 4, "tel-local-suffix" },
2006 { "Phone Extension", "ext", 3, "tel-extension" }
2009 FormFieldData field;
2010 const size_t default_max_length = field.max_length;
2011 for (size_t i = 0; i < arraysize(test_fields); ++i) {
2012 test::CreateTestFormField(
2013 test_fields[i].label, test_fields[i].name, "", "text", &field);
2014 field.max_length = test_fields[i].max_length;
2015 field.autocomplete_attribute = std::string();
2016 form_with_maxlength.fields.push_back(field);
2018 field.max_length = default_max_length;
2019 field.autocomplete_attribute = test_fields[i].autocomplete_attribute;
2020 form_with_autocompletetype.fields.push_back(field);
2023 std::vector<FormData> forms;
2024 forms.push_back(form_with_maxlength);
2025 forms.push_back(form_with_autocompletetype);
2026 FormsSeen(forms);
2028 // We should be able to fill prefix and suffix fields for US numbers.
2029 AutofillProfile* work_profile = autofill_manager_->GetProfileWithGUID(
2030 "00000000-0000-0000-0000-000000000002");
2031 ASSERT_TRUE(work_profile != NULL);
2032 work_profile->SetRawInfo(PHONE_HOME_WHOLE_NUMBER,
2033 ASCIIToUTF16("16505554567"));
2035 std::string guid(work_profile->guid());
2036 int page_id = 1;
2037 int response_page_id = 0;
2038 FormData response_data1;
2039 FillAutofillFormDataAndSaveResults(
2040 page_id, form_with_maxlength, *form_with_maxlength.fields.begin(),
2041 MakeFrontendID(std::string(), guid), &response_page_id, &response_data1);
2042 EXPECT_EQ(1, response_page_id);
2044 ASSERT_EQ(5U, response_data1.fields.size());
2045 EXPECT_EQ(ASCIIToUTF16("1"), response_data1.fields[0].value);
2046 EXPECT_EQ(ASCIIToUTF16("650"), response_data1.fields[1].value);
2047 EXPECT_EQ(ASCIIToUTF16("555"), response_data1.fields[2].value);
2048 EXPECT_EQ(ASCIIToUTF16("4567"), response_data1.fields[3].value);
2049 EXPECT_EQ(base::string16(), response_data1.fields[4].value);
2051 page_id = 2;
2052 response_page_id = 0;
2053 FormData response_data2;
2054 FillAutofillFormDataAndSaveResults(page_id, form_with_autocompletetype,
2055 *form_with_autocompletetype.fields.begin(),
2056 MakeFrontendID(std::string(), guid),
2057 &response_page_id, &response_data2);
2058 EXPECT_EQ(2, response_page_id);
2060 ASSERT_EQ(5U, response_data2.fields.size());
2061 EXPECT_EQ(ASCIIToUTF16("1"), response_data2.fields[0].value);
2062 EXPECT_EQ(ASCIIToUTF16("650"), response_data2.fields[1].value);
2063 EXPECT_EQ(ASCIIToUTF16("555"), response_data2.fields[2].value);
2064 EXPECT_EQ(ASCIIToUTF16("4567"), response_data2.fields[3].value);
2065 EXPECT_EQ(base::string16(), response_data2.fields[4].value);
2067 // We should not be able to fill prefix and suffix fields for international
2068 // numbers.
2069 work_profile->SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("GB"));
2070 work_profile->SetRawInfo(PHONE_HOME_WHOLE_NUMBER,
2071 ASCIIToUTF16("447700954321"));
2072 page_id = 3;
2073 response_page_id = 0;
2074 FormData response_data3;
2075 FillAutofillFormDataAndSaveResults(
2076 page_id, form_with_maxlength, *form_with_maxlength.fields.begin(),
2077 MakeFrontendID(std::string(), guid), &response_page_id, &response_data3);
2078 EXPECT_EQ(3, response_page_id);
2080 ASSERT_EQ(5U, response_data3.fields.size());
2081 EXPECT_EQ(ASCIIToUTF16("44"), response_data3.fields[0].value);
2082 EXPECT_EQ(ASCIIToUTF16("7700"), response_data3.fields[1].value);
2083 EXPECT_EQ(ASCIIToUTF16("954321"), response_data3.fields[2].value);
2084 EXPECT_EQ(ASCIIToUTF16("954321"), response_data3.fields[3].value);
2085 EXPECT_EQ(base::string16(), response_data3.fields[4].value);
2087 page_id = 4;
2088 response_page_id = 0;
2089 FormData response_data4;
2090 FillAutofillFormDataAndSaveResults(page_id, form_with_autocompletetype,
2091 *form_with_autocompletetype.fields.begin(),
2092 MakeFrontendID(std::string(), guid),
2093 &response_page_id, &response_data4);
2094 EXPECT_EQ(4, response_page_id);
2096 ASSERT_EQ(5U, response_data4.fields.size());
2097 EXPECT_EQ(ASCIIToUTF16("44"), response_data4.fields[0].value);
2098 EXPECT_EQ(ASCIIToUTF16("7700"), response_data4.fields[1].value);
2099 EXPECT_EQ(ASCIIToUTF16("954321"), response_data4.fields[2].value);
2100 EXPECT_EQ(ASCIIToUTF16("954321"), response_data4.fields[3].value);
2101 EXPECT_EQ(base::string16(), response_data4.fields[4].value);
2104 // Test that we can still fill a form when a field has been removed from it.
2105 TEST_F(AutofillManagerTest, FormChangesRemoveField) {
2106 // Set up our form data.
2107 FormData form;
2108 test::CreateTestAddressFormData(&form);
2110 // Add a field -- we'll remove it again later.
2111 FormFieldData field;
2112 test::CreateTestFormField("Some", "field", "", "text", &field);
2113 form.fields.insert(form.fields.begin() + 3, field);
2115 std::vector<FormData> forms(1, form);
2116 FormsSeen(forms);
2118 // Now, after the call to |FormsSeen|, we remove the field before filling.
2119 form.fields.erase(form.fields.begin() + 3);
2121 std::string guid("00000000-0000-0000-0000-000000000001");
2122 int response_page_id = 0;
2123 FormData response_data;
2124 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0],
2125 MakeFrontendID(std::string(), guid),
2126 &response_page_id, &response_data);
2127 ExpectFilledAddressFormElvis(response_page_id, response_data, kDefaultPageID,
2128 false);
2131 // Test that we can still fill a form when a field has been added to it.
2132 TEST_F(AutofillManagerTest, FormChangesAddField) {
2133 // The offset of the phone field in the address form.
2134 const int kPhoneFieldOffset = 9;
2136 // Set up our form data.
2137 FormData form;
2138 test::CreateTestAddressFormData(&form);
2140 // Remove the phone field -- we'll add it back later.
2141 std::vector<FormFieldData>::iterator pos =
2142 form.fields.begin() + kPhoneFieldOffset;
2143 FormFieldData field = *pos;
2144 pos = form.fields.erase(pos);
2146 std::vector<FormData> forms(1, form);
2147 FormsSeen(forms);
2149 // Now, after the call to |FormsSeen|, we restore the field before filling.
2150 form.fields.insert(pos, field);
2152 std::string guid("00000000-0000-0000-0000-000000000001");
2153 int response_page_id = 0;
2154 FormData response_data;
2155 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0],
2156 MakeFrontendID(std::string(), guid),
2157 &response_page_id, &response_data);
2158 ExpectFilledAddressFormElvis(response_page_id, response_data, kDefaultPageID,
2159 false);
2162 // Test that we are able to save form data when forms are submitted.
2163 TEST_F(AutofillManagerTest, FormSubmitted) {
2164 // Set up our form data.
2165 FormData form;
2166 test::CreateTestAddressFormData(&form);
2167 std::vector<FormData> forms(1, form);
2168 FormsSeen(forms);
2170 // Fill the form.
2171 std::string guid("00000000-0000-0000-0000-000000000001");
2172 int response_page_id = 0;
2173 FormData response_data;
2174 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0],
2175 MakeFrontendID(std::string(), guid),
2176 &response_page_id, &response_data);
2177 ExpectFilledAddressFormElvis(response_page_id, response_data, kDefaultPageID,
2178 false);
2180 // Simulate form submission. We should call into the PDM to try to save the
2181 // filled data.
2182 FormSubmitted(response_data);
2183 EXPECT_EQ(1, personal_data_.num_times_save_imported_profile_called());
2186 // Test that we are not saving form data when only the WillSubmitForm event is
2187 // sent.
2188 TEST_F(AutofillManagerTest, FormWillSubmitDoesNotSaveData) {
2189 // Set up our form data.
2190 FormData form;
2191 test::CreateTestAddressFormData(&form);
2192 std::vector<FormData> forms(1, form);
2193 FormsSeen(forms);
2195 // Fill the form.
2196 std::string guid("00000000-0000-0000-0000-000000000001");
2197 int response_page_id = 0;
2198 FormData response_data;
2199 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0],
2200 MakeFrontendID(std::string(), guid),
2201 &response_page_id, &response_data);
2202 ExpectFilledAddressFormElvis(response_page_id, response_data, kDefaultPageID,
2203 false);
2205 // Simulate OnWillSubmitForm(). We should *not* be calling into the PDM at
2206 // this point (since the form was not submitted). Does not call
2207 // OnFormSubmitted.
2208 autofill_manager_->ResetRunLoop();
2209 autofill_manager_->OnWillSubmitForm(response_data, base::TimeTicks::Now());
2210 autofill_manager_->WaitForAsyncOnWillSubmitForm();
2211 EXPECT_EQ(0, personal_data_.num_times_save_imported_profile_called());
2214 // Test that when Autocomplete is enabled and Autofill is disabled,
2215 // form submissions are still received by AutocompleteHistoryManager.
2216 TEST_F(AutofillManagerTest, FormSubmittedAutocompleteEnabled) {
2217 TestAutofillClient client;
2218 autofill_manager_.reset(
2219 new TestAutofillManager(autofill_driver_.get(), &client, NULL));
2220 autofill_manager_->set_autofill_enabled(false);
2221 autofill_manager_->autocomplete_history_manager_.reset(
2222 new MockAutocompleteHistoryManager(autofill_driver_.get(), &client));
2224 // Set up our form data.
2225 FormData form;
2226 test::CreateTestAddressFormData(&form);
2227 MockAutocompleteHistoryManager* m = static_cast<
2228 MockAutocompleteHistoryManager*>(
2229 autofill_manager_->autocomplete_history_manager_.get());
2230 EXPECT_CALL(*m, OnWillSubmitForm(_)).Times(1);
2231 FormSubmitted(form);
2234 // Test that when Autocomplete is enabled and Autofill is disabled,
2235 // Autocomplete suggestions are still received.
2236 TEST_F(AutofillManagerTest, AutocompleteSuggestionsWhenAutofillDisabled) {
2237 TestAutofillClient client;
2238 autofill_manager_.reset(
2239 new TestAutofillManager(autofill_driver_.get(), &client, NULL));
2240 autofill_manager_->set_autofill_enabled(false);
2241 autofill_manager_->SetExternalDelegate(external_delegate_.get());
2243 // Set up our form data.
2244 FormData form;
2245 test::CreateTestAddressFormData(&form);
2246 std::vector<FormData> forms(1, form);
2247 FormsSeen(forms);
2248 const FormFieldData& field = form.fields[0];
2249 GetAutofillSuggestions(form, field);
2251 // Add some Autocomplete suggestions. We should return the autocomplete
2252 // suggestions, these will be culled by the renderer.
2253 std::vector<base::string16> suggestions;
2254 suggestions.push_back(ASCIIToUTF16("Jay"));
2255 suggestions.push_back(ASCIIToUTF16("Jason"));
2256 AutocompleteSuggestionsReturned(suggestions);
2258 external_delegate_->CheckSuggestions(
2259 kDefaultPageID,
2260 Suggestion("Jay", "", "", 0),
2261 Suggestion("Jason", "", "", 0));
2264 TEST_F(AutofillManagerTest, AutocompleteOffRespectedForAutocomplete) {
2265 TestAutofillClient client;
2266 autofill_manager_.reset(
2267 new TestAutofillManager(autofill_driver_.get(), &client, NULL));
2268 autofill_manager_->set_autofill_enabled(false);
2269 autofill_manager_->SetExternalDelegate(external_delegate_.get());
2271 autofill_manager_->autocomplete_history_manager_.reset(
2272 new MockAutocompleteHistoryManager(autofill_driver_.get(), &client));
2273 MockAutocompleteHistoryManager* m = static_cast<
2274 MockAutocompleteHistoryManager*>(
2275 autofill_manager_->autocomplete_history_manager_.get());
2276 EXPECT_CALL(*m,
2277 OnGetAutocompleteSuggestions(_, _, _, _, _)).Times(0);
2279 // Set up our form data.
2280 FormData form;
2281 test::CreateTestAddressFormData(&form);
2282 std::vector<FormData> forms(1, form);
2283 FormsSeen(forms);
2284 FormFieldData* field = &form.fields[0];
2285 field->should_autocomplete = false;
2286 GetAutofillSuggestions(form, *field);
2289 // Test that we are able to save form data when forms are submitted and we only
2290 // have server data for the field types.
2291 TEST_F(AutofillManagerTest, FormSubmittedServerTypes) {
2292 // Set up our form data.
2293 FormData form;
2294 test::CreateTestAddressFormData(&form);
2296 // Simulate having seen this form on page load.
2297 // |form_structure| will be owned by |autofill_manager_|.
2298 TestFormStructure* form_structure = new TestFormStructure(form);
2299 form_structure->DetermineHeuristicTypes();
2301 // Clear the heuristic types, and instead set the appropriate server types.
2302 std::vector<ServerFieldType> heuristic_types, server_types;
2303 for (size_t i = 0; i < form.fields.size(); ++i) {
2304 heuristic_types.push_back(UNKNOWN_TYPE);
2305 server_types.push_back(form_structure->field(i)->heuristic_type());
2307 form_structure->SetFieldTypes(heuristic_types, server_types);
2308 autofill_manager_->AddSeenForm(form_structure);
2310 // Fill the form.
2311 std::string guid("00000000-0000-0000-0000-000000000001");
2312 int response_page_id = 0;
2313 FormData response_data;
2314 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0],
2315 MakeFrontendID(std::string(), guid),
2316 &response_page_id, &response_data);
2317 ExpectFilledAddressFormElvis(response_page_id, response_data, kDefaultPageID,
2318 false);
2320 // Simulate form submission. We should call into the PDM to try to save the
2321 // filled data.
2322 FormSubmitted(response_data);
2323 EXPECT_EQ(1, personal_data_.num_times_save_imported_profile_called());
2326 // Test that we are able to save form data after the possible types have been
2327 // determined. We do two submissions and verify that only at the second
2328 // submission are the possible types able to be inferred.
2329 TEST_F(AutofillManagerTest, FormSubmittedPossibleTypesTwoSubmissions) {
2330 // Set up our form data.
2331 FormData form;
2332 std::vector<ServerFieldTypeSet> expected_types;
2333 test::CreateTestAddressFormData(&form, &expected_types);
2335 // Fill the form.
2336 std::string guid("00000000-0000-0000-0000-000000000001");
2337 int response_page_id = 0;
2338 FormData response_data;
2339 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0],
2340 MakeFrontendID(std::string(), guid),
2341 &response_page_id, &response_data);
2342 ExpectFilledAddressFormElvis(response_page_id, response_data, kDefaultPageID,
2343 false);
2345 personal_data_.ClearAutofillProfiles();
2346 ASSERT_EQ(0u, personal_data_.GetProfiles().size());
2348 // Simulate form submission. The first submission should not count the data
2349 // towards possible types. Therefore we expect all UNKNOWN_TYPE entries.
2350 ServerFieldTypeSet type_set;
2351 type_set.insert(UNKNOWN_TYPE);
2352 std::vector<ServerFieldTypeSet> unknown_types(expected_types.size(),
2353 type_set);
2354 autofill_manager_->set_expected_submitted_field_types(unknown_types);
2355 FormSubmitted(response_data);
2356 ASSERT_EQ(1u, personal_data_.GetProfiles().size());
2358 // The second submission should now have data by which to infer types.
2359 autofill_manager_->set_expected_submitted_field_types(expected_types);
2360 FormSubmitted(response_data);
2361 ASSERT_EQ(2u, personal_data_.GetProfiles().size());
2364 // Test that the form signature for an uploaded form always matches the form
2365 // signature from the query.
2366 TEST_F(AutofillManagerTest, FormSubmittedWithDifferentFields) {
2367 // Set up our form data.
2368 FormData form;
2369 test::CreateTestAddressFormData(&form);
2370 std::vector<FormData> forms(1, form);
2371 FormsSeen(forms);
2373 // Cache the expected form signature.
2374 std::string signature = FormStructure(form).FormSignature();
2376 // Change the structure of the form prior to submission.
2377 // Websites would typically invoke JavaScript either on page load or on form
2378 // submit to achieve this.
2379 form.fields.pop_back();
2380 FormFieldData field = form.fields[3];
2381 form.fields[3] = form.fields[7];
2382 form.fields[7] = field;
2384 // Simulate form submission.
2385 FormSubmitted(form);
2386 EXPECT_EQ(signature, autofill_manager_->GetSubmittedFormSignature());
2389 // Test that we do not save form data when submitted fields contain default
2390 // values.
2391 TEST_F(AutofillManagerTest, FormSubmittedWithDefaultValues) {
2392 // Set up our form data.
2393 FormData form;
2394 test::CreateTestAddressFormData(&form);
2395 form.fields[3].value = ASCIIToUTF16("Enter your address");
2397 // Convert the state field to a <select> popup, to make sure that we only
2398 // reject default values for text fields.
2399 ASSERT_TRUE(form.fields[6].name == ASCIIToUTF16("state"));
2400 form.fields[6].form_control_type = "select-one";
2401 form.fields[6].value = ASCIIToUTF16("Tennessee");
2403 std::vector<FormData> forms(1, form);
2404 FormsSeen(forms);
2406 // Fill the form.
2407 std::string guid("00000000-0000-0000-0000-000000000001");
2408 int response_page_id = 0;
2409 FormData response_data;
2410 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[3],
2411 MakeFrontendID(std::string(), guid),
2412 &response_page_id, &response_data);
2414 // Simulate form submission. We should call into the PDM to try to save the
2415 // filled data.
2416 FormSubmitted(response_data);
2417 EXPECT_EQ(1, personal_data_.num_times_save_imported_profile_called());
2419 // Set the address field's value back to the default value.
2420 response_data.fields[3].value = ASCIIToUTF16("Enter your address");
2422 // Simulate form submission. We should not call into the PDM to try to save
2423 // the filled data, since the filled form is effectively missing an address.
2424 FormSubmitted(response_data);
2425 EXPECT_EQ(1, personal_data_.num_times_save_imported_profile_called());
2428 // Tests that credit card data are saved for forms on https
2429 TEST_F(AutofillManagerTest, ImportFormDataCreditCardHTTPS) {
2430 TestSaveCreditCards(true);
2433 // Tests that credit card data are saved for forms on http
2434 TEST_F(AutofillManagerTest, ImportFormDataCreditCardHTTP) {
2435 TestSaveCreditCards(false);
2438 TEST_F(AutofillManagerTest, DeterminePossibleFieldTypesForUpload) {
2439 FormData form;
2440 form.name = ASCIIToUTF16("MyForm");
2441 form.origin = GURL("http://myform.com/form.html");
2442 form.action = GURL("http://myform.com/submit.html");
2444 std::vector<ServerFieldTypeSet> expected_types;
2445 std::vector<base::string16> expected_values;
2447 // These fields should all match.
2448 FormFieldData field;
2449 ServerFieldTypeSet types;
2450 test::CreateTestFormField("", "1", "", "text", &field);
2451 expected_values.push_back(ASCIIToUTF16("Elvis"));
2452 types.clear();
2453 types.insert(NAME_FIRST);
2454 form.fields.push_back(field);
2455 expected_types.push_back(types);
2457 test::CreateTestFormField("", "2", "", "text", &field);
2458 expected_values.push_back(ASCIIToUTF16("Aaron"));
2459 types.clear();
2460 types.insert(NAME_MIDDLE);
2461 form.fields.push_back(field);
2462 expected_types.push_back(types);
2464 test::CreateTestFormField("", "3", "", "text", &field);
2465 expected_values.push_back(ASCIIToUTF16("A"));
2466 types.clear();
2467 types.insert(NAME_MIDDLE_INITIAL);
2468 form.fields.push_back(field);
2469 expected_types.push_back(types);
2471 test::CreateTestFormField("", "4", "", "text", &field);
2472 expected_values.push_back(ASCIIToUTF16("Presley"));
2473 types.clear();
2474 types.insert(NAME_LAST);
2475 form.fields.push_back(field);
2476 expected_types.push_back(types);
2478 test::CreateTestFormField("", "5", "", "text", &field);
2479 expected_values.push_back(ASCIIToUTF16("Elvis Presley"));
2480 types.clear();
2481 types.insert(CREDIT_CARD_NAME);
2482 form.fields.push_back(field);
2483 expected_types.push_back(types);
2485 test::CreateTestFormField("", "6", "", "text", &field);
2486 expected_values.push_back(ASCIIToUTF16("Elvis Aaron Presley"));
2487 types.clear();
2488 types.insert(NAME_FULL);
2489 form.fields.push_back(field);
2490 expected_types.push_back(types);
2492 test::CreateTestFormField("", "7", "", "email", &field);
2493 expected_values.push_back(ASCIIToUTF16("theking@gmail.com"));
2494 types.clear();
2495 types.insert(EMAIL_ADDRESS);
2496 form.fields.push_back(field);
2497 expected_types.push_back(types);
2499 test::CreateTestFormField("", "8", "", "text", &field);
2500 expected_values.push_back(ASCIIToUTF16("RCA"));
2501 types.clear();
2502 types.insert(COMPANY_NAME);
2503 form.fields.push_back(field);
2504 expected_types.push_back(types);
2506 test::CreateTestFormField("", "9", "", "text", &field);
2507 expected_values.push_back(ASCIIToUTF16("3734 Elvis Presley Blvd."));
2508 types.clear();
2509 types.insert(ADDRESS_HOME_LINE1);
2510 form.fields.push_back(field);
2511 expected_types.push_back(types);
2513 test::CreateTestFormField("", "10", "", "text", &field);
2514 expected_values.push_back(ASCIIToUTF16("Apt. 10"));
2515 types.clear();
2516 types.insert(ADDRESS_HOME_LINE2);
2517 form.fields.push_back(field);
2518 expected_types.push_back(types);
2520 test::CreateTestFormField("", "11", "", "text", &field);
2521 expected_values.push_back(ASCIIToUTF16("Memphis"));
2522 types.clear();
2523 types.insert(ADDRESS_HOME_CITY);
2524 form.fields.push_back(field);
2525 expected_types.push_back(types);
2527 test::CreateTestFormField("", "12", "", "text", &field);
2528 expected_values.push_back(ASCIIToUTF16("Tennessee"));
2529 types.clear();
2530 types.insert(ADDRESS_HOME_STATE);
2531 form.fields.push_back(field);
2532 expected_types.push_back(types);
2534 test::CreateTestFormField("", "13", "", "text", &field);
2535 expected_values.push_back(ASCIIToUTF16("38116"));
2536 types.clear();
2537 types.insert(ADDRESS_HOME_ZIP);
2538 form.fields.push_back(field);
2539 expected_types.push_back(types);
2541 test::CreateTestFormField("", "14", "", "text", &field);
2542 expected_values.push_back(ASCIIToUTF16("USA"));
2543 types.clear();
2544 types.insert(ADDRESS_HOME_COUNTRY);
2545 form.fields.push_back(field);
2546 expected_types.push_back(types);
2548 test::CreateTestFormField("", "15", "", "text", &field);
2549 expected_values.push_back(ASCIIToUTF16("United States"));
2550 types.clear();
2551 types.insert(ADDRESS_HOME_COUNTRY);
2552 form.fields.push_back(field);
2553 expected_types.push_back(types);
2555 test::CreateTestFormField("", "16", "", "text", &field);
2556 expected_values.push_back(ASCIIToUTF16("+1 (234) 567-8901"));
2557 types.clear();
2558 types.insert(PHONE_HOME_WHOLE_NUMBER);
2559 form.fields.push_back(field);
2560 expected_types.push_back(types);
2562 test::CreateTestFormField("", "17", "", "text", &field);
2563 expected_values.push_back(ASCIIToUTF16("2345678901"));
2564 types.clear();
2565 types.insert(PHONE_HOME_CITY_AND_NUMBER);
2566 form.fields.push_back(field);
2567 expected_types.push_back(types);
2569 test::CreateTestFormField("", "18", "", "text", &field);
2570 expected_values.push_back(ASCIIToUTF16("1"));
2571 types.clear();
2572 types.insert(PHONE_HOME_COUNTRY_CODE);
2573 form.fields.push_back(field);
2574 expected_types.push_back(types);
2576 test::CreateTestFormField("", "19", "", "text", &field);
2577 expected_values.push_back(ASCIIToUTF16("234"));
2578 types.clear();
2579 types.insert(PHONE_HOME_CITY_CODE);
2580 form.fields.push_back(field);
2581 expected_types.push_back(types);
2583 test::CreateTestFormField("", "20", "", "text", &field);
2584 expected_values.push_back(ASCIIToUTF16("5678901"));
2585 types.clear();
2586 types.insert(PHONE_HOME_NUMBER);
2587 form.fields.push_back(field);
2588 expected_types.push_back(types);
2590 test::CreateTestFormField("", "21", "", "text", &field);
2591 expected_values.push_back(ASCIIToUTF16("567"));
2592 types.clear();
2593 types.insert(PHONE_HOME_NUMBER);
2594 form.fields.push_back(field);
2595 expected_types.push_back(types);
2597 test::CreateTestFormField("", "22", "", "text", &field);
2598 expected_values.push_back(ASCIIToUTF16("8901"));
2599 types.clear();
2600 types.insert(PHONE_HOME_NUMBER);
2601 form.fields.push_back(field);
2602 expected_types.push_back(types);
2604 test::CreateTestFormField("", "23", "", "text", &field);
2605 expected_values.push_back(ASCIIToUTF16("4234-5678-9012-3456"));
2606 types.clear();
2607 types.insert(CREDIT_CARD_NUMBER);
2608 form.fields.push_back(field);
2609 expected_types.push_back(types);
2611 test::CreateTestFormField("", "24", "", "text", &field);
2612 expected_values.push_back(ASCIIToUTF16("04"));
2613 types.clear();
2614 types.insert(CREDIT_CARD_EXP_MONTH);
2615 form.fields.push_back(field);
2616 expected_types.push_back(types);
2618 test::CreateTestFormField("", "25", "", "text", &field);
2619 expected_values.push_back(ASCIIToUTF16("April"));
2620 types.clear();
2621 types.insert(CREDIT_CARD_EXP_MONTH);
2622 form.fields.push_back(field);
2623 expected_types.push_back(types);
2625 test::CreateTestFormField("", "26", "", "text", &field);
2626 expected_values.push_back(ASCIIToUTF16("2012"));
2627 types.clear();
2628 types.insert(CREDIT_CARD_EXP_4_DIGIT_YEAR);
2629 form.fields.push_back(field);
2630 expected_types.push_back(types);
2632 test::CreateTestFormField("", "27", "", "text", &field);
2633 expected_values.push_back(ASCIIToUTF16("12"));
2634 types.clear();
2635 types.insert(CREDIT_CARD_EXP_2_DIGIT_YEAR);
2636 form.fields.push_back(field);
2637 expected_types.push_back(types);
2639 test::CreateTestFormField("", "28", "", "text", &field);
2640 expected_values.push_back(ASCIIToUTF16("04/2012"));
2641 types.clear();
2642 types.insert(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR);
2643 form.fields.push_back(field);
2644 expected_types.push_back(types);
2646 // Make sure that we trim whitespace properly.
2647 test::CreateTestFormField("", "29", "", "text", &field);
2648 expected_values.push_back(ASCIIToUTF16(""));
2649 types.clear();
2650 types.insert(EMPTY_TYPE);
2651 form.fields.push_back(field);
2652 expected_types.push_back(types);
2654 test::CreateTestFormField("", "30", " ", "text", &field);
2655 expected_values.push_back(ASCIIToUTF16(" "));
2656 types.clear();
2657 types.insert(EMPTY_TYPE);
2658 form.fields.push_back(field);
2659 expected_types.push_back(types);
2661 test::CreateTestFormField("", "31", " ", "text", &field);
2662 expected_values.push_back(ASCIIToUTF16(" Elvis"));
2663 types.clear();
2664 types.insert(NAME_FIRST);
2665 form.fields.push_back(field);
2666 expected_types.push_back(types);
2668 test::CreateTestFormField("", "32", " ", "text", &field);
2669 expected_values.push_back(ASCIIToUTF16("Elvis "));
2670 types.clear();
2671 types.insert(NAME_FIRST);
2672 form.fields.push_back(field);
2673 expected_types.push_back(types);
2675 // These fields should not match, as they differ by case.
2676 test::CreateTestFormField("", "33", "", "text", &field);
2677 expected_values.push_back(ASCIIToUTF16("elvis"));
2678 types.clear();
2679 types.insert(UNKNOWN_TYPE);
2680 form.fields.push_back(field);
2681 expected_types.push_back(types);
2683 test::CreateTestFormField("", "34", "", "text", &field);
2684 expected_values.push_back(ASCIIToUTF16("3734 Elvis Presley BLVD"));
2685 types.clear();
2686 types.insert(UNKNOWN_TYPE);
2687 form.fields.push_back(field);
2688 expected_types.push_back(types);
2690 // These fields should not match, as they are unsupported variants.
2691 test::CreateTestFormField("", "35", "", "text", &field);
2692 expected_values.push_back(ASCIIToUTF16("Elvis Aaron"));
2693 types.clear();
2694 types.insert(UNKNOWN_TYPE);
2695 form.fields.push_back(field);
2696 expected_types.push_back(types);
2698 test::CreateTestFormField("", "36", "", "text", &field);
2699 expected_values.push_back(ASCIIToUTF16("Mr. Presley"));
2700 types.clear();
2701 types.insert(UNKNOWN_TYPE);
2702 form.fields.push_back(field);
2703 expected_types.push_back(types);
2705 test::CreateTestFormField("", "37", "", "text", &field);
2706 expected_values.push_back(ASCIIToUTF16("3734 Elvis Presley"));
2707 types.clear();
2708 types.insert(UNKNOWN_TYPE);
2709 form.fields.push_back(field);
2710 expected_types.push_back(types);
2712 test::CreateTestFormField("", "38", "", "text", &field);
2713 expected_values.push_back(ASCIIToUTF16("TN"));
2714 types.clear();
2715 types.insert(UNKNOWN_TYPE);
2716 form.fields.push_back(field);
2717 expected_types.push_back(types);
2719 test::CreateTestFormField("", "39", "", "text", &field);
2720 expected_values.push_back(ASCIIToUTF16("38116-1023"));
2721 types.clear();
2722 types.insert(UNKNOWN_TYPE);
2723 form.fields.push_back(field);
2724 expected_types.push_back(types);
2726 test::CreateTestFormField("", "20", "", "text", &field);
2727 expected_values.push_back(ASCIIToUTF16("5"));
2728 types.clear();
2729 types.insert(UNKNOWN_TYPE);
2730 form.fields.push_back(field);
2731 expected_types.push_back(types);
2733 test::CreateTestFormField("", "20", "", "text", &field);
2734 expected_values.push_back(ASCIIToUTF16("56"));
2735 types.clear();
2736 types.insert(UNKNOWN_TYPE);
2737 form.fields.push_back(field);
2738 expected_types.push_back(types);
2740 test::CreateTestFormField("", "20", "", "text", &field);
2741 expected_values.push_back(ASCIIToUTF16("901"));
2742 types.clear();
2743 types.insert(UNKNOWN_TYPE);
2744 form.fields.push_back(field);
2745 expected_types.push_back(types);
2747 // Make sure the form is in the cache so that it is processed for Autofill
2748 // upload.
2749 std::vector<FormData> forms(1, form);
2750 FormsSeen(forms);
2752 // Once the form is cached, fill the values.
2753 EXPECT_EQ(form.fields.size(), expected_values.size());
2754 for (size_t i = 0; i < expected_values.size(); i++) {
2755 form.fields[i].value = expected_values[i];
2758 autofill_manager_->set_expected_submitted_field_types(expected_types);
2759 FormSubmitted(form);
2762 TEST_F(AutofillManagerTest, RemoveProfile) {
2763 // Add and remove an Autofill profile.
2764 AutofillProfile* profile = new AutofillProfile;
2765 std::string guid = "00000000-0000-0000-0000-000000000102";
2766 profile->set_guid(guid.c_str());
2767 autofill_manager_->AddProfile(profile);
2769 int id = MakeFrontendID(std::string(), guid);
2771 autofill_manager_->RemoveAutofillProfileOrCreditCard(id);
2773 EXPECT_FALSE(autofill_manager_->GetProfileWithGUID(guid.c_str()));
2776 TEST_F(AutofillManagerTest, RemoveCreditCard){
2777 // Add and remove an Autofill credit card.
2778 CreditCard* credit_card = new CreditCard;
2779 std::string guid = "00000000-0000-0000-0000-000000100007";
2780 credit_card->set_guid(guid.c_str());
2781 autofill_manager_->AddCreditCard(credit_card);
2783 int id = MakeFrontendID(guid, std::string());
2785 autofill_manager_->RemoveAutofillProfileOrCreditCard(id);
2787 EXPECT_FALSE(autofill_manager_->GetCreditCardWithGUID(guid.c_str()));
2790 // Test our external delegate is called at the right time.
2791 TEST_F(AutofillManagerTest, TestExternalDelegate) {
2792 FormData form;
2793 test::CreateTestAddressFormData(&form);
2794 std::vector<FormData> forms(1, form);
2795 FormsSeen(forms);
2796 const FormFieldData& field = form.fields[0];
2797 GetAutofillSuggestions(form, field); // should call the delegate's OnQuery()
2799 EXPECT_TRUE(external_delegate_->on_query_seen());
2802 // Test to verify suggestions appears for forms having credit card number split
2803 // across fields.
2804 TEST_F(AutofillManagerTest, GetCreditCardSuggestionsForNumberSpitAcrossFields) {
2805 // Set up our form data with credit card number split across fields.
2806 FormData form;
2807 form.name = ASCIIToUTF16("MyForm");
2808 form.origin = GURL("https://myform.com/form.html");
2809 form.action = GURL("https://myform.com/submit.html");
2811 FormFieldData name_field;
2812 test::CreateTestFormField("Name on Card", "nameoncard", "", "text",
2813 &name_field);
2814 form.fields.push_back(name_field);
2816 // Add new 4 |card_number_field|s to the |form|.
2817 FormFieldData card_number_field;
2818 card_number_field.max_length = 4;
2819 test::CreateTestFormField("Card Number", "cardnumber_1", "", "text",
2820 &card_number_field);
2821 form.fields.push_back(card_number_field);
2823 test::CreateTestFormField("", "cardnumber_2", "", "text", &card_number_field);
2824 form.fields.push_back(card_number_field);
2826 test::CreateTestFormField("", "cardnumber_3", "", "text", &card_number_field);
2827 form.fields.push_back(card_number_field);
2829 test::CreateTestFormField("", "cardnumber_4", "", "text", &card_number_field);
2830 form.fields.push_back(card_number_field);
2832 FormFieldData exp_field;
2833 test::CreateTestFormField("Expiration Date", "ccmonth", "", "text",
2834 &exp_field);
2835 form.fields.push_back(exp_field);
2837 test::CreateTestFormField("", "ccyear", "", "text", &exp_field);
2838 form.fields.push_back(exp_field);
2840 std::vector<FormData> forms(1, form);
2841 FormsSeen(forms);
2843 // Verify whether suggestions are populated correctly for one of the middle
2844 // credit card number fields when filled partially.
2845 FormFieldData number_field = form.fields[3];
2846 number_field.value = ASCIIToUTF16("901");
2848 // Get the suggestions for already filled credit card |number_field|.
2849 GetAutofillSuggestions(form, number_field);
2851 // No autocomplete suggestions provided, so send an empty vector as the
2852 // results. This triggers the combined message send.
2853 AutocompleteSuggestionsReturned(std::vector<base::string16>());
2855 external_delegate_->CheckSuggestions(
2856 kDefaultPageID,
2857 Suggestion(
2858 "Visa\xC2\xA0\xE2\x8B\xAF"
2859 "3456",
2860 "04/12", kVisaCard, autofill_manager_->GetPackedCreditCardID(4)));
2863 // Test that inputs detected to be CVC inputs are forced to
2864 // !should_autocomplete for AutocompleteHistoryManager::OnWillSubmitForm.
2865 TEST_F(AutofillManagerTest, DontSaveCvcInAutocompleteHistory) {
2866 autofill_manager_->autocomplete_history_manager_.reset(
2867 new MockAutocompleteHistoryManager(autofill_driver_.get(),
2868 &autofill_client_));
2869 FormData form_seen_by_ahm;
2870 MockAutocompleteHistoryManager* mock_ahm =
2871 static_cast<MockAutocompleteHistoryManager*>(
2872 autofill_manager_->autocomplete_history_manager_.get());
2873 EXPECT_CALL(*mock_ahm, OnWillSubmitForm(_))
2874 .WillOnce(SaveArg<0>(&form_seen_by_ahm));
2876 FormData form;
2877 form.name = ASCIIToUTF16("MyForm");
2878 form.origin = GURL("http://myform.com/form.html");
2879 form.action = GURL("http://myform.com/submit.html");
2881 struct {
2882 const char* label;
2883 const char* name;
2884 const char* value;
2885 ServerFieldType expected_field_type;
2886 } fields[] = {
2887 {"Card number", "1", "4234-5678-9012-3456", CREDIT_CARD_NUMBER},
2888 {"Card verification code", "2", "123", CREDIT_CARD_VERIFICATION_CODE},
2889 {"expiration date", "3", "04/2020", CREDIT_CARD_EXP_4_DIGIT_YEAR},
2892 for (size_t i = 0; i < arraysize(fields); ++i) {
2893 FormFieldData field;
2894 test::CreateTestFormField(fields[i].label, fields[i].name, fields[i].value,
2895 "text", &field);
2896 form.fields.push_back(field);
2899 std::vector<FormData> forms(1, form);
2900 FormsSeen(forms);
2901 FormSubmitted(form);
2903 EXPECT_EQ(form.fields.size(), form_seen_by_ahm.fields.size());
2904 ASSERT_EQ(arraysize(fields), form_seen_by_ahm.fields.size());
2905 for (size_t i = 0; i < arraysize(fields); ++i) {
2906 EXPECT_EQ(form_seen_by_ahm.fields[i].should_autocomplete,
2907 fields[i].expected_field_type != CREDIT_CARD_VERIFICATION_CODE);
2911 TEST_F(AutofillManagerTest, DontOfferToSaveWalletCard) {
2912 FormData form;
2913 CreditCard card;
2914 PrepareForRealPanResponse(&form, &card);
2916 // Manually fill out |form| so we can use it in OnFormSubmitted.
2917 for (size_t i = 0; i < form.fields.size(); ++i) {
2918 if (form.fields[i].name == ASCIIToUTF16("cardnumber"))
2919 form.fields[i].value = ASCIIToUTF16("4012888888881881");
2920 else if (form.fields[i].name == ASCIIToUTF16("nameoncard"))
2921 form.fields[i].value = ASCIIToUTF16("John H Dillinger");
2922 else if (form.fields[i].name == ASCIIToUTF16("ccmonth"))
2923 form.fields[i].value = ASCIIToUTF16("01");
2924 else if (form.fields[i].name == ASCIIToUTF16("ccyear"))
2925 form.fields[i].value = ASCIIToUTF16("2017");
2928 AutofillManager::UnmaskResponse response;
2929 response.should_store_pan = false;
2930 response.cvc = ASCIIToUTF16("123");
2931 autofill_manager_->OnUnmaskResponse(response);
2932 autofill_manager_->OnDidGetRealPan(AutofillClient::SUCCESS,
2933 "4012888888881881");
2934 autofill_manager_->OnFormSubmitted(form);
2936 // The rest of this test is a regression test for http://crbug.com/483602.
2937 // The goal is not to crash.
2938 EXPECT_CALL(*autofill_driver_, SendFormDataToRenderer(_, _, _));
2939 for (size_t i = 0; i < form.fields.size(); ++i) {
2940 form.fields[i].value.clear();
2942 autofill_manager_->FillOrPreviewCreditCardForm(
2943 AutofillDriver::FORM_DATA_ACTION_FILL, kDefaultPageID, form,
2944 form.fields[1], card);
2945 autofill_manager_->OnUnmaskResponse(response);
2946 autofill_manager_->OnDidGetRealPan(AutofillClient::SUCCESS,
2947 "4012888888881881");
2949 form = FormData();
2950 test::CreateTestAddressFormData(&form);
2951 FormsSeen(std::vector<FormData>(1, form));
2952 for (size_t i = 0; i < form.fields.size(); ++i) {
2953 if (form.fields[i].name == ASCIIToUTF16("firstname"))
2954 form.fields[i].value = ASCIIToUTF16("Flo");
2955 else if (form.fields[i].name == ASCIIToUTF16("lastname"))
2956 form.fields[i].value = ASCIIToUTF16("Master");
2957 else if (form.fields[i].name == ASCIIToUTF16("addr1"))
2958 form.fields[i].value = ASCIIToUTF16("123 Maple");
2959 else if (form.fields[i].name == ASCIIToUTF16("city"))
2960 form.fields[i].value = ASCIIToUTF16("Dallas");
2961 else if (form.fields[i].name == ASCIIToUTF16("state"))
2962 form.fields[i].value = ASCIIToUTF16("Texas");
2963 else if (form.fields[i].name == ASCIIToUTF16("zipcode"))
2964 form.fields[i].value = ASCIIToUTF16("77401");
2965 else if (form.fields[i].name == ASCIIToUTF16("country"))
2966 form.fields[i].value = ASCIIToUTF16("US");
2968 autofill_manager_->OnFormSubmitted(form);
2971 TEST_F(AutofillManagerTest, FillInUpdatedExpirationDate) {
2972 FormData form;
2973 CreditCard card;
2974 PrepareForRealPanResponse(&form, &card);
2976 AutofillManager::UnmaskResponse response;
2977 response.should_store_pan = false;
2978 response.cvc = ASCIIToUTF16("123");
2979 response.exp_month = ASCIIToUTF16("02");
2980 response.exp_year = ASCIIToUTF16("2018");
2981 autofill_manager_->OnUnmaskResponse(response);
2982 autofill_manager_->OnDidGetRealPan(AutofillClient::SUCCESS,
2983 "4012888888881881");
2985 EXPECT_EQ(ASCIIToUTF16("02"), autofill_manager_->unmasking_card_.GetRawInfo(
2986 CREDIT_CARD_EXP_MONTH));
2987 EXPECT_EQ(ASCIIToUTF16("2018"), autofill_manager_->unmasking_card_.GetRawInfo(
2988 CREDIT_CARD_EXP_4_DIGIT_YEAR));
2991 // Verify that typing "gmail" will match "theking@gmail.com" and
2992 // "buddy@gmail.com" when substring matching is enabled.
2993 TEST_F(AutofillManagerTest, DisplaySuggestionsWithMatchingTokens) {
2994 // Token matching is currently behind a flag.
2995 base::CommandLine::ForCurrentProcess()->AppendSwitch(
2996 autofill::switches::kEnableSuggestionsWithSubstringMatch);
2998 // Set up our form data.
2999 FormData form;
3000 test::CreateTestAddressFormData(&form);
3001 std::vector<FormData> forms(1, form);
3002 FormsSeen(forms);
3004 FormFieldData field;
3005 test::CreateTestFormField("Email", "email", "gmail", "email", &field);
3006 GetAutofillSuggestions(form, field);
3007 AutocompleteSuggestionsReturned(std::vector<base::string16>());
3009 external_delegate_->CheckSuggestions(
3010 kDefaultPageID,
3011 Suggestion("theking@gmail.com", "3734 Elvis Presley Blvd.", "", 1),
3012 Suggestion("buddy@gmail.com", "123 Apple St.", "", 2));
3015 // Verify that typing "apple" will match "123 Apple St." when substring matching
3016 // is enabled.
3017 TEST_F(AutofillManagerTest, DisplaySuggestionsWithMatchingTokens_CaseIgnored) {
3018 // Token matching is currently behind a flag.
3019 base::CommandLine::ForCurrentProcess()->AppendSwitch(
3020 autofill::switches::kEnableSuggestionsWithSubstringMatch);
3022 // Set up our form data.
3023 FormData form;
3024 test::CreateTestAddressFormData(&form);
3025 std::vector<FormData> forms(1, form);
3026 FormsSeen(forms);
3028 FormFieldData field;
3029 test::CreateTestFormField("Address Line 2", "addr2", "apple", "text", &field);
3030 GetAutofillSuggestions(form, field);
3031 AutocompleteSuggestionsReturned(std::vector<base::string16>());
3033 external_delegate_->CheckSuggestions(
3034 kDefaultPageID,
3035 Suggestion("123 Apple St., unit 6", "123 Apple St.", "", 1));
3038 // Verify that typing "mail" will not match any of the "@gmail.com" email
3039 // addresses when substring matching is enabled.
3040 TEST_F(AutofillManagerTest, NoSuggestionForNonPrefixTokenMatch) {
3041 // Token matching is currently behind a flag.
3042 base::CommandLine::ForCurrentProcess()->AppendSwitch(
3043 autofill::switches::kEnableSuggestionsWithSubstringMatch);
3045 // Set up our form data.
3046 FormData form;
3047 test::CreateTestAddressFormData(&form);
3048 std::vector<FormData> forms(1, form);
3049 FormsSeen(forms);
3051 FormFieldData field;
3052 test::CreateTestFormField("Email", "email", "mail", "email", &field);
3053 GetAutofillSuggestions(form, field);
3054 EXPECT_FALSE(external_delegate_->on_suggestions_returned_seen());
3057 // Verify that typing "pres" will match "Elvis Presley" when substring matching
3058 // is enabled.
3059 TEST_F(AutofillManagerTest, DisplayCreditCardSuggestionsWithMatchingTokens) {
3060 // Token matching is currently behind a flag.
3061 base::CommandLine::ForCurrentProcess()->AppendSwitch(
3062 autofill::switches::kEnableSuggestionsWithSubstringMatch);
3064 // Set up our form data.
3065 FormData form;
3066 CreateTestCreditCardFormData(&form, true, false);
3067 std::vector<FormData> forms(1, form);
3068 FormsSeen(forms);
3070 FormFieldData field;
3071 test::CreateTestFormField("Name on Card", "nameoncard", "pres", "text",
3072 &field);
3073 GetAutofillSuggestions(form, field);
3075 // No suggestions provided, so send an empty vector as the results.
3076 // This triggers the combined message send.
3077 AutocompleteSuggestionsReturned(std::vector<base::string16>());
3079 #if defined(OS_ANDROID)
3080 static const char* kVisaSuggestion =
3081 "Visa\xC2\xA0\xE2\x8B\xAF"
3082 "3456";
3083 #else
3084 static const char* kVisaSuggestion = "*3456";
3085 #endif
3087 external_delegate_->CheckSuggestions(
3088 kDefaultPageID, Suggestion("Elvis Presley", kVisaSuggestion, kVisaCard,
3089 autofill_manager_->GetPackedCreditCardID(4)));
3092 // Verify that typing "lvis" will not match any of the credit card name when
3093 // substring matching is enabled.
3094 TEST_F(AutofillManagerTest, NoCreditCardSuggestionsForNonPrefixTokenMatch) {
3095 // Token matching is currently behind a flag.
3096 base::CommandLine::ForCurrentProcess()->AppendSwitch(
3097 autofill::switches::kEnableSuggestionsWithSubstringMatch);
3099 // Set up our form data.
3100 FormData form;
3101 CreateTestCreditCardFormData(&form, true, false);
3102 std::vector<FormData> forms(1, form);
3103 FormsSeen(forms);
3105 FormFieldData field;
3106 test::CreateTestFormField("Name on Card", "nameoncard", "lvis", "text",
3107 &field);
3108 GetAutofillSuggestions(form, field);
3109 EXPECT_FALSE(external_delegate_->on_suggestions_returned_seen());
3112 // Verify that typing "S" into the middle name field will match and order middle
3113 // names "Shawn Smith" followed by "Adam Smith" i.e. prefix matched followed by
3114 // substring matched.
3115 TEST_F(AutofillManagerTest,
3116 DisplaySuggestionsWithPrefixesPrecedeSubstringMatched) {
3117 // Token matching is currently behind a flag.
3118 base::CommandLine::ForCurrentProcess()->AppendSwitch(
3119 autofill::switches::kEnableSuggestionsWithSubstringMatch);
3121 // Set up our form data.
3122 FormData form;
3123 test::CreateTestAddressFormData(&form);
3124 std::vector<FormData> forms(1, form);
3125 FormsSeen(forms);
3127 AutofillProfile* profile1 = new AutofillProfile;
3128 profile1->set_guid("00000000-0000-0000-0000-000000000103");
3129 profile1->SetInfo(AutofillType(NAME_FIRST), ASCIIToUTF16("Robin"), "en-US");
3130 profile1->SetInfo(AutofillType(NAME_MIDDLE), ASCIIToUTF16("Adam Smith"),
3131 "en-US");
3132 profile1->SetInfo(AutofillType(NAME_LAST), ASCIIToUTF16("Grimes"), "en-US");
3133 profile1->SetInfo(AutofillType(ADDRESS_HOME_LINE1),
3134 ASCIIToUTF16("1234 Smith Blvd."), "en-US");
3135 autofill_manager_->AddProfile(profile1);
3137 AutofillProfile* profile2 = new AutofillProfile;
3138 profile2->set_guid("00000000-0000-0000-0000-000000000124");
3139 profile2->SetInfo(AutofillType(NAME_FIRST), ASCIIToUTF16("Carl"), "en-US");
3140 profile2->SetInfo(AutofillType(NAME_MIDDLE), ASCIIToUTF16("Shawn Smith"),
3141 "en-US");
3142 profile2->SetInfo(AutofillType(NAME_LAST), ASCIIToUTF16("Grimes"), "en-US");
3143 profile2->SetInfo(AutofillType(ADDRESS_HOME_LINE1),
3144 ASCIIToUTF16("1234 Smith Blvd."), "en-US");
3145 autofill_manager_->AddProfile(profile2);
3147 FormFieldData field;
3148 test::CreateTestFormField("Middle Name", "middlename", "S", "text", &field);
3149 GetAutofillSuggestions(form, field);
3151 // No suggestions provided, so send an empty vector as the results.
3152 // This triggers the combined message send.
3153 AutocompleteSuggestionsReturned(std::vector<base::string16>());
3155 external_delegate_->CheckSuggestions(
3156 kDefaultPageID,
3157 Suggestion("Shawn Smith", "1234 Smith Blvd., Robin Adam Smith Grimes", "",
3159 Suggestion("Adam Smith", "1234 Smith Blvd., Carl Shawn Smith Grimes", "",
3160 2));
3163 } // namespace autofill