Include all dupe types (event when value is zero) in scan stats.
[chromium-blink-merge.git] / components / autofill / core / browser / autofill_manager_unittest.cc
blob092f624531afeb95f9dd78e862be78caae6ab8e7
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::SaveArg;
50 namespace autofill {
52 namespace {
54 const int kDefaultPageID = 137;
56 class MockAutofillClient : public TestAutofillClient {
57 public:
58 MockAutofillClient() {}
60 ~MockAutofillClient() override {}
62 MOCK_METHOD1(ConfirmSaveCreditCard,
63 void(const base::Closure& save_card_callback));
65 private:
66 DISALLOW_COPY_AND_ASSIGN(MockAutofillClient);
69 class TestPersonalDataManager : public PersonalDataManager {
70 public:
71 TestPersonalDataManager()
72 : PersonalDataManager("en-US"),
73 num_times_save_imported_profile_called_(0) {
74 CreateTestAutofillProfiles(&web_profiles_);
75 CreateTestCreditCards(&local_credit_cards_);
78 using PersonalDataManager::set_database;
79 using PersonalDataManager::SetPrefService;
81 int num_times_save_imported_profile_called() {
82 return num_times_save_imported_profile_called_;
85 std::string SaveImportedProfile(const AutofillProfile& profile) override {
86 num_times_save_imported_profile_called_++;
87 AutofillProfile* imported_profile = new AutofillProfile(profile);
88 AddProfile(imported_profile);
89 return profile.guid();
92 AutofillProfile* GetProfileWithGUID(const char* guid) {
93 for (AutofillProfile* profile : GetProfiles()) {
94 if (!profile->guid().compare(guid))
95 return profile;
97 return NULL;
100 CreditCard* GetCreditCardWithGUID(const char* guid) {
101 for (CreditCard* card : GetCreditCards()) {
102 if (!card->guid().compare(guid))
103 return card;
105 return NULL;
108 void AddProfile(AutofillProfile* profile) {
109 web_profiles_.push_back(profile);
112 void AddCreditCard(CreditCard* credit_card) {
113 local_credit_cards_.push_back(credit_card);
116 void RecordUseOf(const AutofillDataModel& data_model) override {
117 CreditCard* credit_card = GetCreditCardWithGUID(data_model.guid().c_str());
118 if (credit_card)
119 credit_card->RecordUse();
121 AutofillProfile* profile = GetProfileWithGUID(data_model.guid().c_str());
122 if (profile)
123 profile->RecordUse();
126 void RemoveByGUID(const std::string& guid) override {
127 CreditCard* credit_card = GetCreditCardWithGUID(guid.c_str());
128 if (credit_card) {
129 local_credit_cards_.erase(
130 std::find(local_credit_cards_.begin(), local_credit_cards_.end(),
131 credit_card));
134 AutofillProfile* profile = GetProfileWithGUID(guid.c_str());
135 if (profile) {
136 web_profiles_.erase(
137 std::find(web_profiles_.begin(), web_profiles_.end(), profile));
141 // Do nothing (auxiliary profiles will be created in
142 // CreateTestAuxiliaryProfile).
143 void LoadAuxiliaryProfiles(bool record_metrics) const override {}
145 void ClearAutofillProfiles() {
146 web_profiles_.clear();
149 void ClearCreditCards() {
150 local_credit_cards_.clear();
153 void CreateTestAuxiliaryProfiles() {
154 CreateTestAutofillProfiles(&auxiliary_profiles_);
157 void CreateTestCreditCardsYearAndMonth(const char* year, const char* month) {
158 ClearCreditCards();
159 CreditCard* credit_card = new CreditCard;
160 test::SetCreditCardInfo(credit_card, "Miku Hatsune",
161 "4234567890654321", // Visa
162 month, year);
163 credit_card->set_guid("00000000-0000-0000-0000-000000000007");
164 local_credit_cards_.push_back(credit_card);
167 private:
168 void CreateTestAutofillProfiles(ScopedVector<AutofillProfile>* profiles) {
169 AutofillProfile* profile = new AutofillProfile;
170 test::SetProfileInfo(profile, "Elvis", "Aaron",
171 "Presley", "theking@gmail.com", "RCA",
172 "3734 Elvis Presley Blvd.", "Apt. 10",
173 "Memphis", "Tennessee", "38116", "US",
174 "12345678901");
175 profile->set_guid("00000000-0000-0000-0000-000000000001");
176 profiles->push_back(profile);
177 profile = new AutofillProfile;
178 test::SetProfileInfo(profile, "Charles", "Hardin",
179 "Holley", "buddy@gmail.com", "Decca",
180 "123 Apple St.", "unit 6", "Lubbock",
181 "Texas", "79401", "US", "23456789012");
182 profile->set_guid("00000000-0000-0000-0000-000000000002");
183 profiles->push_back(profile);
184 profile = new AutofillProfile;
185 test::SetProfileInfo(
186 profile, "", "", "", "", "", "", "", "", "", "", "", "");
187 profile->set_guid("00000000-0000-0000-0000-000000000003");
188 profiles->push_back(profile);
191 void CreateTestCreditCards(ScopedVector<CreditCard>* credit_cards) {
192 CreditCard* credit_card = new CreditCard;
193 test::SetCreditCardInfo(credit_card, "Elvis Presley",
194 "4234 5678 9012 3456", // Visa
195 "04", "2012");
196 credit_card->set_guid("00000000-0000-0000-0000-000000000004");
197 credit_cards->push_back(credit_card);
199 credit_card = new CreditCard;
200 test::SetCreditCardInfo(credit_card, "Buddy Holly",
201 "5187654321098765", // Mastercard
202 "10", "2014");
203 credit_card->set_guid("00000000-0000-0000-0000-000000000005");
204 credit_cards->push_back(credit_card);
206 credit_card = new CreditCard;
207 test::SetCreditCardInfo(credit_card, "", "", "", "");
208 credit_card->set_guid("00000000-0000-0000-0000-000000000006");
209 credit_cards->push_back(credit_card);
212 size_t num_times_save_imported_profile_called_;
214 DISALLOW_COPY_AND_ASSIGN(TestPersonalDataManager);
217 // Populates |form| with data corresponding to a simple credit card form.
218 // Note that this actually appends fields to the form data, which can be useful
219 // for building up more complex test forms.
220 void CreateTestCreditCardFormData(FormData* form,
221 bool is_https,
222 bool use_month_type) {
223 form->name = ASCIIToUTF16("MyForm");
224 if (is_https) {
225 form->origin = GURL("https://myform.com/form.html");
226 form->action = GURL("https://myform.com/submit.html");
227 } else {
228 form->origin = GURL("http://myform.com/form.html");
229 form->action = GURL("http://myform.com/submit.html");
231 form->user_submitted = true;
233 FormFieldData field;
234 test::CreateTestFormField("Name on Card", "nameoncard", "", "text", &field);
235 form->fields.push_back(field);
236 test::CreateTestFormField("Card Number", "cardnumber", "", "text", &field);
237 form->fields.push_back(field);
238 if (use_month_type) {
239 test::CreateTestFormField(
240 "Expiration Date", "ccmonth", "", "month", &field);
241 form->fields.push_back(field);
242 } else {
243 test::CreateTestFormField("Expiration Date", "ccmonth", "", "text", &field);
244 form->fields.push_back(field);
245 test::CreateTestFormField("", "ccyear", "", "text", &field);
246 form->fields.push_back(field);
250 void ExpectFilledField(const char* expected_label,
251 const char* expected_name,
252 const char* expected_value,
253 const char* expected_form_control_type,
254 const FormFieldData& field) {
255 SCOPED_TRACE(expected_label);
256 EXPECT_EQ(UTF8ToUTF16(expected_label), field.label);
257 EXPECT_EQ(UTF8ToUTF16(expected_name), field.name);
258 EXPECT_EQ(UTF8ToUTF16(expected_value), field.value);
259 EXPECT_EQ(expected_form_control_type, field.form_control_type);
262 // Verifies that the |filled_form| has been filled with the given data.
263 // Verifies address fields if |has_address_fields| is true, and verifies
264 // credit card fields if |has_credit_card_fields| is true. Verifies both if both
265 // are true. |use_month_type| is used for credit card input month type.
266 void ExpectFilledForm(int page_id,
267 const FormData& filled_form,
268 int expected_page_id,
269 const char* first,
270 const char* middle,
271 const char* last,
272 const char* address1,
273 const char* address2,
274 const char* city,
275 const char* state,
276 const char* postal_code,
277 const char* country,
278 const char* phone,
279 const char* email,
280 const char* name_on_card,
281 const char* card_number,
282 const char* expiration_month,
283 const char* expiration_year,
284 bool has_address_fields,
285 bool has_credit_card_fields,
286 bool use_month_type,
287 bool is_user_submitted) {
288 // The number of fields in the address and credit card forms created above.
289 const size_t kAddressFormSize = 11;
290 const size_t kCreditCardFormSize = use_month_type ? 3 : 4;
292 EXPECT_EQ(expected_page_id, page_id);
293 EXPECT_EQ(ASCIIToUTF16("MyForm"), filled_form.name);
294 if (has_credit_card_fields) {
295 EXPECT_EQ(GURL("https://myform.com/form.html"), filled_form.origin);
296 EXPECT_EQ(GURL("https://myform.com/submit.html"), filled_form.action);
297 } else {
298 EXPECT_EQ(GURL("http://myform.com/form.html"), filled_form.origin);
299 EXPECT_EQ(GURL("http://myform.com/submit.html"), filled_form.action);
301 EXPECT_EQ(is_user_submitted, filled_form.user_submitted);
303 size_t form_size = 0;
304 if (has_address_fields)
305 form_size += kAddressFormSize;
306 if (has_credit_card_fields)
307 form_size += kCreditCardFormSize;
308 ASSERT_EQ(form_size, filled_form.fields.size());
310 if (has_address_fields) {
311 ExpectFilledField("First Name", "firstname", first, "text",
312 filled_form.fields[0]);
313 ExpectFilledField("Middle Name", "middlename", middle, "text",
314 filled_form.fields[1]);
315 ExpectFilledField("Last Name", "lastname", last, "text",
316 filled_form.fields[2]);
317 ExpectFilledField("Address Line 1", "addr1", address1, "text",
318 filled_form.fields[3]);
319 ExpectFilledField("Address Line 2", "addr2", address2, "text",
320 filled_form.fields[4]);
321 ExpectFilledField("City", "city", city, "text",
322 filled_form.fields[5]);
323 ExpectFilledField("State", "state", state, "text",
324 filled_form.fields[6]);
325 ExpectFilledField("Postal Code", "zipcode", postal_code, "text",
326 filled_form.fields[7]);
327 ExpectFilledField("Country", "country", country, "text",
328 filled_form.fields[8]);
329 ExpectFilledField("Phone Number", "phonenumber", phone, "tel",
330 filled_form.fields[9]);
331 ExpectFilledField("Email", "email", email, "email",
332 filled_form.fields[10]);
335 if (has_credit_card_fields) {
336 size_t offset = has_address_fields? kAddressFormSize : 0;
337 ExpectFilledField("Name on Card", "nameoncard", name_on_card, "text",
338 filled_form.fields[offset + 0]);
339 ExpectFilledField("Card Number", "cardnumber", card_number, "text",
340 filled_form.fields[offset + 1]);
341 if (use_month_type) {
342 std::string exp_year = expiration_year;
343 std::string exp_month = expiration_month;
344 std::string date;
345 if (!exp_year.empty() && !exp_month.empty())
346 date = exp_year + "-" + exp_month;
348 ExpectFilledField("Expiration Date", "ccmonth", date.c_str(), "month",
349 filled_form.fields[offset + 2]);
350 } else {
351 ExpectFilledField("Expiration Date", "ccmonth", expiration_month, "text",
352 filled_form.fields[offset + 2]);
353 ExpectFilledField("", "ccyear", expiration_year, "text",
354 filled_form.fields[offset + 3]);
359 void ExpectFilledAddressFormElvis(int page_id,
360 const FormData& filled_form,
361 int expected_page_id,
362 bool has_credit_card_fields,
363 bool is_user_submitted) {
364 ExpectFilledForm(page_id, filled_form, expected_page_id, "Elvis", "Aaron",
365 "Presley", "3734 Elvis Presley Blvd.", "Apt. 10", "Memphis",
366 "Tennessee", "38116", "United States", "12345678901",
367 "theking@gmail.com", "", "", "", "", true,
368 has_credit_card_fields, false, is_user_submitted);
371 void ExpectFilledCreditCardFormElvis(int page_id,
372 const FormData& filled_form,
373 int expected_page_id,
374 bool has_address_fields) {
375 ExpectFilledForm(page_id, filled_form, expected_page_id, "", "", "", "", "",
376 "", "", "", "", "", "", "Elvis Presley", "4234567890123456",
377 "04", "2012", has_address_fields, true, false, true);
380 void ExpectFilledCreditCardYearMonthWithYearMonth(int page_id,
381 const FormData& filled_form,
382 int expected_page_id,
383 bool has_address_fields,
384 const char* year,
385 const char* month) {
386 ExpectFilledForm(page_id, filled_form, expected_page_id, "", "", "", "", "",
387 "", "", "", "", "", "", "Miku Hatsune", "4234567890654321",
388 month, year, has_address_fields, true, true, true);
391 class MockAutocompleteHistoryManager : public AutocompleteHistoryManager {
392 public:
393 MockAutocompleteHistoryManager(AutofillDriver* driver, AutofillClient* client)
394 : AutocompleteHistoryManager(driver, client) {}
396 MOCK_METHOD5(OnGetAutocompleteSuggestions, void(
397 int query_id,
398 const base::string16& name,
399 const base::string16& prefix,
400 const std::string& form_control_type,
401 const std::vector<Suggestion>& suggestions));
402 MOCK_METHOD1(OnFormSubmitted, void(const FormData& form));
404 private:
405 DISALLOW_COPY_AND_ASSIGN(MockAutocompleteHistoryManager);
408 class MockAutofillDriver : public TestAutofillDriver {
409 public:
410 MockAutofillDriver() {}
412 // Mock methods to enable testability.
413 MOCK_METHOD3(SendFormDataToRenderer, void(int query_id,
414 RendererFormDataAction action,
415 const FormData& data));
417 private:
418 DISALLOW_COPY_AND_ASSIGN(MockAutofillDriver);
421 class TestAutofillManager : public AutofillManager {
422 public:
423 TestAutofillManager(AutofillDriver* driver,
424 autofill::AutofillClient* client,
425 TestPersonalDataManager* personal_data)
426 : AutofillManager(driver, client, personal_data),
427 personal_data_(personal_data),
428 autofill_enabled_(true),
429 expect_all_unknown_possible_types_(false) {}
430 ~TestAutofillManager() override {}
432 bool IsAutofillEnabled() const override { return autofill_enabled_; }
434 void set_autofill_enabled(bool autofill_enabled) {
435 autofill_enabled_ = autofill_enabled;
438 void set_expected_submitted_field_types(
439 const std::vector<ServerFieldTypeSet>& expected_types) {
440 expected_submitted_field_types_ = expected_types;
443 void UploadFormDataAsyncCallback(
444 const FormStructure* submitted_form,
445 const base::TimeTicks& load_time,
446 const base::TimeTicks& interaction_time,
447 const base::TimeTicks& submission_time) override {
448 run_loop_->Quit();
450 // If we have expected field types set, make sure they match.
451 if (!expected_submitted_field_types_.empty()) {
452 ASSERT_EQ(expected_submitted_field_types_.size(),
453 submitted_form->field_count());
454 for (size_t i = 0; i < expected_submitted_field_types_.size(); ++i) {
455 SCOPED_TRACE(
456 base::StringPrintf(
457 "Field %d with value %s", static_cast<int>(i),
458 base::UTF16ToUTF8(submitted_form->field(i)->value).c_str()));
459 const ServerFieldTypeSet& possible_types =
460 submitted_form->field(i)->possible_types();
461 EXPECT_EQ(expected_submitted_field_types_[i].size(),
462 possible_types.size());
463 for (ServerFieldTypeSet::const_iterator it =
464 expected_submitted_field_types_[i].begin();
465 it != expected_submitted_field_types_[i].end(); ++it) {
466 EXPECT_TRUE(possible_types.count(*it))
467 << "Expected type: " << AutofillType(*it).ToString();
472 AutofillManager::UploadFormDataAsyncCallback(submitted_form,
473 load_time,
474 interaction_time,
475 submission_time);
478 // Resets the run loop so that it can wait for an asynchronous form
479 // submission to complete.
480 void ResetRunLoop() { run_loop_.reset(new base::RunLoop()); }
482 // Wait for the asynchronous OnWillSubmitForm() call to complete.
483 void WaitForAsyncOnWillSubmitForm() { run_loop_->Run(); }
485 void UploadFormData(const FormStructure& submitted_form) override {
486 submitted_form_signature_ = submitted_form.FormSignature();
489 const std::string GetSubmittedFormSignature() {
490 return submitted_form_signature_;
493 AutofillProfile* GetProfileWithGUID(const char* guid) {
494 return personal_data_->GetProfileWithGUID(guid);
497 CreditCard* GetCreditCardWithGUID(const char* guid) {
498 return personal_data_->GetCreditCardWithGUID(guid);
501 void AddProfile(AutofillProfile* profile) {
502 personal_data_->AddProfile(profile);
505 void AddCreditCard(CreditCard* credit_card) {
506 personal_data_->AddCreditCard(credit_card);
509 int GetPackedCreditCardID(int credit_card_id) {
510 std::string credit_card_guid =
511 base::StringPrintf("00000000-0000-0000-0000-%012d", credit_card_id);
513 return MakeFrontendID(credit_card_guid, std::string());
516 void AddSeenForm(FormStructure* form) {
517 form_structures()->push_back(form);
520 void ClearFormStructures() {
521 form_structures()->clear();
524 private:
525 // Weak reference.
526 TestPersonalDataManager* personal_data_;
528 bool autofill_enabled_;
529 bool expect_all_unknown_possible_types_;
531 scoped_ptr<base::RunLoop> run_loop_;
533 std::string submitted_form_signature_;
534 std::vector<ServerFieldTypeSet> expected_submitted_field_types_;
536 DISALLOW_COPY_AND_ASSIGN(TestAutofillManager);
539 class TestAutofillExternalDelegate : public AutofillExternalDelegate {
540 public:
541 explicit TestAutofillExternalDelegate(AutofillManager* autofill_manager,
542 AutofillDriver* autofill_driver)
543 : AutofillExternalDelegate(autofill_manager, autofill_driver),
544 on_query_seen_(false),
545 on_suggestions_returned_seen_(false) {}
546 ~TestAutofillExternalDelegate() override {}
548 void OnQuery(int query_id,
549 const FormData& form,
550 const FormFieldData& field,
551 const gfx::RectF& bounds) override {
552 on_query_seen_ = true;
553 on_suggestions_returned_seen_ = false;
556 void OnSuggestionsReturned(
557 int query_id,
558 const std::vector<Suggestion>& suggestions) override {
559 on_suggestions_returned_seen_ = true;
560 query_id_ = query_id;
561 suggestions_ = suggestions;
564 void CheckSuggestions(int expected_page_id,
565 size_t expected_num_suggestions,
566 const Suggestion expected_suggestions[]) {
567 // Ensure that these results are from the most recent query.
568 EXPECT_TRUE(on_suggestions_returned_seen_);
570 EXPECT_EQ(expected_page_id, query_id_);
571 ASSERT_EQ(expected_num_suggestions, suggestions_.size());
572 for (size_t i = 0; i < expected_num_suggestions; ++i) {
573 SCOPED_TRACE(base::StringPrintf("i: %" PRIuS, i));
574 EXPECT_EQ(expected_suggestions[i].value, suggestions_[i].value);
575 EXPECT_EQ(expected_suggestions[i].label, suggestions_[i].label);
576 EXPECT_EQ(expected_suggestions[i].icon, suggestions_[i].icon);
577 EXPECT_EQ(expected_suggestions[i].frontend_id,
578 suggestions_[i].frontend_id);
582 // Wrappers around the above GetSuggestions call that take a hardcoded number
583 // of expected results so callsites are cleaner.
584 void CheckSuggestions(int expected_page_id,
585 const Suggestion& suggestion0) {
586 std::vector<Suggestion> suggestion_vector;
587 suggestion_vector.push_back(suggestion0);
588 CheckSuggestions(expected_page_id, 1, &suggestion_vector[0]);
590 void CheckSuggestions(int expected_page_id,
591 const Suggestion& suggestion0,
592 const Suggestion& suggestion1) {
593 std::vector<Suggestion> suggestion_vector;
594 suggestion_vector.push_back(suggestion0);
595 suggestion_vector.push_back(suggestion1);
596 CheckSuggestions(expected_page_id, 2, &suggestion_vector[0]);
598 void CheckSuggestions(int expected_page_id,
599 const Suggestion& suggestion0,
600 const Suggestion& suggestion1,
601 const Suggestion& suggestion2) {
602 std::vector<Suggestion> suggestion_vector;
603 suggestion_vector.push_back(suggestion0);
604 suggestion_vector.push_back(suggestion1);
605 suggestion_vector.push_back(suggestion2);
606 CheckSuggestions(expected_page_id, 3, &suggestion_vector[0]);
610 bool on_query_seen() const {
611 return on_query_seen_;
614 bool on_suggestions_returned_seen() const {
615 return on_suggestions_returned_seen_;
618 private:
619 // Records if OnQuery has been called yet.
620 bool on_query_seen_;
622 // Records if OnSuggestionsReturned has been called after the most recent
623 // call to OnQuery.
624 bool on_suggestions_returned_seen_;
626 // The query id of the most recent Autofill query.
627 int query_id_;
629 // The results returned by the most recent Autofill query.
630 std::vector<Suggestion> suggestions_;
632 DISALLOW_COPY_AND_ASSIGN(TestAutofillExternalDelegate);
635 } // namespace
637 class AutofillManagerTest : public testing::Test {
638 public:
639 void SetUp() override {
640 autofill_client_.SetPrefs(test::PrefServiceForTesting());
641 personal_data_.set_database(autofill_client_.GetDatabase());
642 personal_data_.SetPrefService(autofill_client_.GetPrefs());
643 autofill_driver_.reset(new MockAutofillDriver());
644 request_context_ = new net::TestURLRequestContextGetter(
645 base::ThreadTaskRunnerHandle::Get());
646 autofill_driver_->SetURLRequestContext(request_context_.get());
647 autofill_manager_.reset(new TestAutofillManager(
648 autofill_driver_.get(), &autofill_client_, &personal_data_));
650 external_delegate_.reset(new TestAutofillExternalDelegate(
651 autofill_manager_.get(),
652 autofill_driver_.get()));
653 autofill_manager_->SetExternalDelegate(external_delegate_.get());
656 void TearDown() override {
657 // Order of destruction is important as AutofillManager relies on
658 // PersonalDataManager to be around when it gets destroyed.
659 autofill_manager_.reset();
660 autofill_driver_.reset();
662 // Remove the AutofillWebDataService so TestPersonalDataManager does not
663 // need to care about removing self as an observer in destruction.
664 personal_data_.set_database(scoped_refptr<AutofillWebDataService>(NULL));
665 personal_data_.SetPrefService(NULL);
667 request_context_ = nullptr;
670 void GetAutofillSuggestions(int query_id,
671 const FormData& form,
672 const FormFieldData& field) {
673 autofill_manager_->OnQueryFormFieldAutofill(query_id, form, field,
674 gfx::Rect());
677 void GetAutofillSuggestions(const FormData& form,
678 const FormFieldData& field) {
679 GetAutofillSuggestions(kDefaultPageID, form, field);
682 void AutocompleteSuggestionsReturned(
683 const std::vector<base::string16>& result) {
684 autofill_manager_->autocomplete_history_manager_->SendSuggestions(&result);
687 void FormsSeen(const std::vector<FormData>& forms) {
688 autofill_manager_->OnFormsSeen(forms, base::TimeTicks());
691 void FormSubmitted(const FormData& form) {
692 autofill_manager_->ResetRunLoop();
693 if (autofill_manager_->OnWillSubmitForm(form, base::TimeTicks::Now()))
694 autofill_manager_->WaitForAsyncOnWillSubmitForm();
695 autofill_manager_->OnFormSubmitted(form);
698 void FillAutofillFormData(int query_id,
699 const FormData& form,
700 const FormFieldData& field,
701 int unique_id) {
702 autofill_manager_->FillOrPreviewForm(
703 AutofillDriver::FORM_DATA_ACTION_FILL, query_id, form, field,
704 unique_id);
707 // Calls |autofill_manager_->OnFillAutofillFormData()| with the specified
708 // input parameters after setting up the expectation that the mock driver's
709 // |SendFormDataToRenderer()| method will be called and saving the parameters
710 // of that call into the |response_query_id| and |response_data| output
711 // parameters.
712 void FillAutofillFormDataAndSaveResults(int input_query_id,
713 const FormData& input_form,
714 const FormFieldData& input_field,
715 int unique_id,
716 int* response_query_id,
717 FormData* response_data) {
718 EXPECT_CALL(*autofill_driver_, SendFormDataToRenderer(_, _, _)).
719 WillOnce((DoAll(testing::SaveArg<0>(response_query_id),
720 testing::SaveArg<2>(response_data))));
721 FillAutofillFormData(input_query_id, input_form, input_field, unique_id);
724 int MakeFrontendID(const std::string& cc_sid,
725 const std::string& profile_sid) const {
726 return autofill_manager_->MakeFrontendID(cc_sid, profile_sid);
729 bool WillFillCreditCardNumber(const FormData& form,
730 const FormFieldData& field) {
731 return autofill_manager_->WillFillCreditCardNumber(form, field);
734 protected:
735 base::MessageLoop message_loop_;
736 MockAutofillClient autofill_client_;
737 scoped_ptr<MockAutofillDriver> autofill_driver_;
738 scoped_ptr<TestAutofillManager> autofill_manager_;
739 scoped_ptr<TestAutofillExternalDelegate> external_delegate_;
740 scoped_refptr<net::TestURLRequestContextGetter> request_context_;
741 TestPersonalDataManager personal_data_;
744 class TestFormStructure : public FormStructure {
745 public:
746 explicit TestFormStructure(const FormData& form)
747 : FormStructure(form) {}
748 ~TestFormStructure() override {}
750 void SetFieldTypes(const std::vector<ServerFieldType>& heuristic_types,
751 const std::vector<ServerFieldType>& server_types) {
752 ASSERT_EQ(field_count(), heuristic_types.size());
753 ASSERT_EQ(field_count(), server_types.size());
755 for (size_t i = 0; i < field_count(); ++i) {
756 AutofillField* form_field = field(i);
757 ASSERT_TRUE(form_field);
758 form_field->set_heuristic_type(heuristic_types[i]);
759 form_field->set_server_type(server_types[i]);
762 UpdateAutofillCount();
765 private:
766 DISALLOW_COPY_AND_ASSIGN(TestFormStructure);
769 // Test that we return all address profile suggestions when all form fields are
770 // empty.
771 TEST_F(AutofillManagerTest, GetProfileSuggestionsEmptyValue) {
772 // Set up our form data.
773 FormData form;
774 test::CreateTestAddressFormData(&form);
775 std::vector<FormData> forms(1, form);
776 FormsSeen(forms);
778 const FormFieldData& field = form.fields[0];
779 GetAutofillSuggestions(form, field);
781 // No suggestions provided, so send an empty vector as the results.
782 // This triggers the combined message send.
783 AutocompleteSuggestionsReturned(std::vector<base::string16>());
785 // Test that we sent the right values to the external delegate. Inferred
786 // labels include full first relevant field, which in this case is the
787 // address line 1.
788 external_delegate_->CheckSuggestions(
789 kDefaultPageID,
790 Suggestion("Elvis", "3734 Elvis Presley Blvd.", "", 1),
791 Suggestion("Charles", "123 Apple St.", "", 2));
794 // Test that we return only matching address profile suggestions when the
795 // selected form field has been partially filled out.
796 TEST_F(AutofillManagerTest, GetProfileSuggestionsMatchCharacter) {
797 // Set up our form data.
798 FormData form;
799 test::CreateTestAddressFormData(&form);
800 std::vector<FormData> forms(1, form);
801 FormsSeen(forms);
803 FormFieldData field;
804 test::CreateTestFormField("First Name", "firstname", "E", "text",&field);
805 GetAutofillSuggestions(form, field);
807 // No suggestions provided, so send an empty vector as the results.
808 // This triggers the combined message send.
809 AutocompleteSuggestionsReturned(std::vector<base::string16>());
811 // Test that we sent the right values to the external delegate.
812 external_delegate_->CheckSuggestions(
813 kDefaultPageID,
814 Suggestion("Elvis", "3734 Elvis Presley Blvd.", "", 1));
817 // Test that we return no suggestions when the form has no relevant fields.
818 TEST_F(AutofillManagerTest, GetProfileSuggestionsUnknownFields) {
819 // Set up our form data.
820 FormData form;
821 form.name = ASCIIToUTF16("MyForm");
822 form.origin = GURL("http://myform.com/form.html");
823 form.action = GURL("http://myform.com/submit.html");
824 form.user_submitted = true;
826 FormFieldData field;
827 test::CreateTestFormField("Username", "username", "", "text",&field);
828 form.fields.push_back(field);
829 test::CreateTestFormField("Password", "password", "", "password",&field);
830 form.fields.push_back(field);
831 test::CreateTestFormField("Quest", "quest", "", "quest", &field);
832 form.fields.push_back(field);
833 test::CreateTestFormField("Color", "color", "", "text", &field);
834 form.fields.push_back(field);
836 std::vector<FormData> forms(1, form);
837 FormsSeen(forms);
839 GetAutofillSuggestions(form, field);
840 EXPECT_FALSE(external_delegate_->on_suggestions_returned_seen());
843 // Test that we cull duplicate profile suggestions.
844 TEST_F(AutofillManagerTest, GetProfileSuggestionsWithDuplicates) {
845 // Set up our form data.
846 FormData form;
847 test::CreateTestAddressFormData(&form);
848 std::vector<FormData> forms(1, form);
849 FormsSeen(forms);
851 // Add a duplicate profile.
852 AutofillProfile* duplicate_profile =
853 new AutofillProfile(
854 *(autofill_manager_->GetProfileWithGUID(
855 "00000000-0000-0000-0000-000000000001")));
856 autofill_manager_->AddProfile(duplicate_profile);
858 const FormFieldData& field = form.fields[0];
859 GetAutofillSuggestions(form, field);
861 // No suggestions provided, so send an empty vector as the results.
862 // This triggers the combined message send.
863 AutocompleteSuggestionsReturned(std::vector<base::string16>());
865 // Test that we sent the right values to the external delegate.
866 external_delegate_->CheckSuggestions(
867 kDefaultPageID,
868 Suggestion("Elvis", "3734 Elvis Presley Blvd.", "", 1),
869 Suggestion("Charles", "123 Apple St.", "", 2));
872 // Test that we return no suggestions when autofill is disabled.
873 TEST_F(AutofillManagerTest, GetProfileSuggestionsAutofillDisabledByUser) {
874 // Set up our form data.
875 FormData form;
876 test::CreateTestAddressFormData(&form);
877 std::vector<FormData> forms(1, form);
878 FormsSeen(forms);
880 // Disable Autofill.
881 autofill_manager_->set_autofill_enabled(false);
883 const FormFieldData& field = form.fields[0];
884 GetAutofillSuggestions(form, field);
885 EXPECT_FALSE(external_delegate_->on_suggestions_returned_seen());
888 // Test that we return all credit card profile suggestions when all form fields
889 // are empty.
890 TEST_F(AutofillManagerTest, GetCreditCardSuggestionsEmptyValue) {
891 // Set up our form data.
892 FormData form;
893 CreateTestCreditCardFormData(&form, true, false);
894 std::vector<FormData> forms(1, form);
895 FormsSeen(forms);
897 FormFieldData field = form.fields[1];
898 GetAutofillSuggestions(form, field);
900 // No suggestions provided, so send an empty vector as the results.
901 // This triggers the combined message send.
902 AutocompleteSuggestionsReturned(std::vector<base::string16>());
904 // Test that we sent the right values to the external delegate.
905 external_delegate_->CheckSuggestions(
906 kDefaultPageID,
907 Suggestion(
908 "Visa\xC2\xA0\xE2\x8B\xAF"
909 "3456",
910 "04/12", kVisaCard, autofill_manager_->GetPackedCreditCardID(4)),
911 Suggestion(
912 "MasterCard\xC2\xA0\xE2\x8B\xAF"
913 "8765",
914 "10/14", kMasterCard, autofill_manager_->GetPackedCreditCardID(5)));
917 // Test that we return only matching credit card profile suggestions when the
918 // selected form field has been partially filled out.
919 TEST_F(AutofillManagerTest, GetCreditCardSuggestionsMatchCharacter) {
920 // Set up our form data.
921 FormData form;
922 CreateTestCreditCardFormData(&form, true, false);
923 std::vector<FormData> forms(1, form);
924 FormsSeen(forms);
926 FormFieldData field;
927 test::CreateTestFormField("Card Number", "cardnumber", "78", "text", &field);
928 GetAutofillSuggestions(form, field);
930 // No suggestions provided, so send an empty vector as the results.
931 // This triggers the combined message send.
932 AutocompleteSuggestionsReturned(std::vector<base::string16>());
934 // Test that we sent the right values to the external delegate.
935 external_delegate_->CheckSuggestions(
936 kDefaultPageID,
937 Suggestion(
938 "Visa\xC2\xA0\xE2\x8B\xAF"
939 "3456",
940 "04/12", kVisaCard, autofill_manager_->GetPackedCreditCardID(4)));
943 // Test that we return credit card profile suggestions when the selected form
944 // field is not the credit card number field.
945 TEST_F(AutofillManagerTest, GetCreditCardSuggestionsNonCCNumber) {
946 // Set up our form data.
947 FormData form;
948 CreateTestCreditCardFormData(&form, true, false);
949 std::vector<FormData> forms(1, form);
950 FormsSeen(forms);
952 const FormFieldData& field = form.fields[0];
953 GetAutofillSuggestions(form, field);
955 // No suggestions provided, so send an empty vector as the results.
956 // This triggers the combined message send.
957 AutocompleteSuggestionsReturned(std::vector<base::string16>());
959 #if defined(OS_ANDROID)
960 static const char* kVisaSuggestion =
961 "Visa\xC2\xA0\xE2\x8B\xAF"
962 "3456";
963 static const char* kMcSuggestion =
964 "MasterCard\xC2\xA0\xE2\x8B\xAF"
965 "8765";
966 #else
967 static const char* kVisaSuggestion = "*3456";
968 static const char* kMcSuggestion = "*8765";
969 #endif
971 // Test that we sent the right values to the external delegate.
972 external_delegate_->CheckSuggestions(
973 kDefaultPageID,
974 Suggestion("Elvis Presley", kVisaSuggestion, kVisaCard,
975 autofill_manager_->GetPackedCreditCardID(4)),
976 Suggestion("Buddy Holly", kMcSuggestion, kMasterCard,
977 autofill_manager_->GetPackedCreditCardID(5)));
980 // Test that we return a warning explaining that credit card profile suggestions
981 // are unavailable when the form is not https.
982 TEST_F(AutofillManagerTest, GetCreditCardSuggestionsNonHTTPS) {
983 // Set up our form data.
984 FormData form;
985 CreateTestCreditCardFormData(&form, false, false);
986 std::vector<FormData> forms(1, form);
987 FormsSeen(forms);
989 const FormFieldData& field = form.fields[0];
990 GetAutofillSuggestions(form, field);
992 // No suggestions provided, so send an empty vector as the results.
993 // This triggers the combined message send.
994 AutocompleteSuggestionsReturned(std::vector<base::string16>());
996 // Test that we sent the right values to the external delegate.
997 external_delegate_->CheckSuggestions(
998 kDefaultPageID,
999 Suggestion(
1000 l10n_util::GetStringUTF8(IDS_AUTOFILL_WARNING_INSECURE_CONNECTION),
1001 "", "", -1));
1003 // Now add some Autocomplete suggestions. We should show the autocomplete
1004 // suggestions and the warning.
1005 const int kPageID2 = 2;
1006 GetAutofillSuggestions(kPageID2, form, field);
1008 std::vector<base::string16> suggestions;
1009 suggestions.push_back(ASCIIToUTF16("Jay"));
1010 suggestions.push_back(ASCIIToUTF16("Jason"));
1011 AutocompleteSuggestionsReturned(suggestions);
1013 external_delegate_->CheckSuggestions(
1014 kPageID2,
1015 Suggestion(
1016 l10n_util::GetStringUTF8(IDS_AUTOFILL_WARNING_INSECURE_CONNECTION),
1017 "", "", -1),
1018 Suggestion("Jay", "", "", 0),
1019 Suggestion("Jason", "", "", 0));
1021 // Clear the test credit cards and try again -- we shouldn't return a warning.
1022 personal_data_.ClearCreditCards();
1023 GetAutofillSuggestions(form, field);
1024 EXPECT_FALSE(external_delegate_->on_suggestions_returned_seen());
1027 // Test that we return all credit card suggestions in the case that two cards
1028 // have the same obfuscated number.
1029 TEST_F(AutofillManagerTest, GetCreditCardSuggestionsRepeatedObfuscatedNumber) {
1030 // Add a credit card with the same obfuscated number as Elvis's.
1031 // |credit_card| will be owned by the mock PersonalDataManager.
1032 CreditCard* credit_card = new CreditCard;
1033 test::SetCreditCardInfo(credit_card, "Elvis Presley",
1034 "5231567890123456", // Mastercard
1035 "05", "2012");
1036 credit_card->set_guid("00000000-0000-0000-0000-000000000007");
1037 autofill_manager_->AddCreditCard(credit_card);
1039 // Set up our form data.
1040 FormData form;
1041 CreateTestCreditCardFormData(&form, true, false);
1042 std::vector<FormData> forms(1, form);
1043 FormsSeen(forms);
1045 FormFieldData field = form.fields[1];
1046 GetAutofillSuggestions(form, field);
1048 // No suggestions provided, so send an empty vector as the results.
1049 // This triggers the combined message send.
1050 AutocompleteSuggestionsReturned(std::vector<base::string16>());
1052 // Test that we sent the right values to the external delegate.
1053 external_delegate_->CheckSuggestions(
1054 kDefaultPageID,
1055 Suggestion(
1056 "Visa\xC2\xA0\xE2\x8B\xAF"
1057 "3456",
1058 "04/12", kVisaCard, autofill_manager_->GetPackedCreditCardID(4)),
1059 Suggestion(
1060 "MasterCard\xC2\xA0\xE2\x8B\xAF"
1061 "8765",
1062 "10/14", kMasterCard, autofill_manager_->GetPackedCreditCardID(5)),
1063 Suggestion(
1064 "MasterCard\xC2\xA0\xE2\x8B\xAF"
1065 "3456",
1066 "05/12", kMasterCard, autofill_manager_->GetPackedCreditCardID(7)));
1069 // Test that we return profile and credit card suggestions for combined forms.
1070 TEST_F(AutofillManagerTest, GetAddressAndCreditCardSuggestions) {
1071 // Set up our form data.
1072 FormData form;
1073 test::CreateTestAddressFormData(&form);
1074 CreateTestCreditCardFormData(&form, true, false);
1075 std::vector<FormData> forms(1, form);
1076 FormsSeen(forms);
1078 FormFieldData field = form.fields[0];
1079 GetAutofillSuggestions(form, field);
1081 // No suggestions provided, so send an empty vector as the results.
1082 // This triggers the combined message send.
1083 AutocompleteSuggestionsReturned(std::vector<base::string16>());
1085 // Test that we sent the right address suggestions to the external delegate.
1086 external_delegate_->CheckSuggestions(
1087 kDefaultPageID,
1088 Suggestion("Elvis", "3734 Elvis Presley Blvd.", "", 1),
1089 Suggestion("Charles", "123 Apple St.", "", 2));
1091 const int kPageID2 = 2;
1092 test::CreateTestFormField("Card Number", "cardnumber", "", "text", &field);
1093 GetAutofillSuggestions(kPageID2, form, field);
1095 // No suggestions provided, so send an empty vector as the results.
1096 // This triggers the combined message send.
1097 AutocompleteSuggestionsReturned(std::vector<base::string16>());
1099 // Test that we sent the credit card suggestions to the external delegate.
1100 external_delegate_->CheckSuggestions(
1101 kPageID2,
1102 Suggestion(
1103 "Visa\xC2\xA0\xE2\x8B\xAF"
1104 "3456",
1105 "04/12", kVisaCard, autofill_manager_->GetPackedCreditCardID(4)),
1106 Suggestion(
1107 "MasterCard\xC2\xA0\xE2\x8B\xAF"
1108 "8765",
1109 "10/14", kMasterCard, autofill_manager_->GetPackedCreditCardID(5)));
1112 // Test that for non-https forms with both address and credit card fields, we
1113 // only return address suggestions. Instead of credit card suggestions, we
1114 // should return a warning explaining that credit card profile suggestions are
1115 // unavailable when the form is not https.
1116 TEST_F(AutofillManagerTest, GetAddressAndCreditCardSuggestionsNonHttps) {
1117 // Set up our form data.
1118 FormData form;
1119 test::CreateTestAddressFormData(&form);
1120 CreateTestCreditCardFormData(&form, false, false);
1121 std::vector<FormData> forms(1, form);
1122 FormsSeen(forms);
1124 FormFieldData field = form.fields[0];
1125 GetAutofillSuggestions(form, field);
1127 // No suggestions provided, so send an empty vector as the results.
1128 // This triggers the combined message send.
1129 AutocompleteSuggestionsReturned(std::vector<base::string16>());
1131 // Test that we sent the right suggestions to the external delegate.
1132 external_delegate_->CheckSuggestions(
1133 kDefaultPageID,
1134 Suggestion("Elvis", "3734 Elvis Presley Blvd.", "", 1),
1135 Suggestion("Charles", "123 Apple St.", "", 2));
1137 test::CreateTestFormField("Card Number", "cardnumber", "", "text", &field);
1138 const int kPageID2 = 2;
1139 GetAutofillSuggestions(kPageID2, form, field);
1141 // No suggestions provided, so send an empty vector as the results.
1142 // This triggers the combined message send.
1143 AutocompleteSuggestionsReturned(std::vector<base::string16>());
1145 // Test that we sent the right values to the external delegate.
1146 external_delegate_->CheckSuggestions(
1147 kPageID2,
1148 Suggestion(
1149 l10n_util::GetStringUTF8(IDS_AUTOFILL_WARNING_INSECURE_CONNECTION),
1150 "", "", -1));
1152 // Clear the test credit cards and try again -- we shouldn't return a warning.
1153 personal_data_.ClearCreditCards();
1154 GetAutofillSuggestions(form, field);
1155 EXPECT_FALSE(external_delegate_->on_suggestions_returned_seen());
1158 // Test that we correctly combine autofill and autocomplete suggestions.
1159 TEST_F(AutofillManagerTest, GetCombinedAutofillAndAutocompleteSuggestions) {
1160 // Set up our form data.
1161 FormData form;
1162 test::CreateTestAddressFormData(&form);
1163 std::vector<FormData> forms(1, form);
1164 FormsSeen(forms);
1166 const FormFieldData& field = form.fields[0];
1167 GetAutofillSuggestions(form, field);
1169 // Add some Autocomplete suggestions.
1170 // This triggers the combined message send.
1171 std::vector<base::string16> suggestions;
1172 suggestions.push_back(ASCIIToUTF16("Jay"));
1173 // This suggestion is a duplicate, and should be trimmed.
1174 suggestions.push_back(ASCIIToUTF16("Elvis"));
1175 suggestions.push_back(ASCIIToUTF16("Jason"));
1176 AutocompleteSuggestionsReturned(suggestions);
1178 // Test that we sent the right values to the external delegate.
1179 Suggestion expected[] = {
1180 Suggestion("Elvis", "3734 Elvis Presley Blvd.", "", 1),
1181 Suggestion("Charles", "123 Apple St.", "", 2),
1182 Suggestion("Jay", "", "", 0),
1183 Suggestion("Jason", "", "", 0),
1185 external_delegate_->CheckSuggestions(
1186 kDefaultPageID, arraysize(expected), expected);
1189 // Test that we return autocomplete-like suggestions when trying to autofill
1190 // already filled forms.
1191 TEST_F(AutofillManagerTest, GetFieldSuggestionsWhenFormIsAutofilled) {
1192 // Set up our form data.
1193 FormData form;
1194 test::CreateTestAddressFormData(&form);
1195 std::vector<FormData> forms(1, form);
1196 FormsSeen(forms);
1198 // Mark one of the fields as filled.
1199 form.fields[2].is_autofilled = true;
1200 const FormFieldData& field = form.fields[0];
1201 GetAutofillSuggestions(form, field);
1203 // No suggestions provided, so send an empty vector as the results.
1204 // This triggers the combined message send.
1205 AutocompleteSuggestionsReturned(std::vector<base::string16>());
1207 // Test that we sent the right values to the external delegate.
1208 external_delegate_->CheckSuggestions(
1209 kDefaultPageID,
1210 Suggestion("Elvis", "", "", 1),
1211 Suggestion("Charles", "", "", 2));
1214 // Test that nothing breaks when there are autocomplete suggestions but no
1215 // autofill suggestions.
1216 TEST_F(AutofillManagerTest, GetFieldSuggestionsForAutocompleteOnly) {
1217 // Set up our form data.
1218 FormData form;
1219 test::CreateTestAddressFormData(&form);
1220 FormFieldData field;
1221 test::CreateTestFormField("Some Field", "somefield", "", "text", &field);
1222 form.fields.push_back(field);
1223 std::vector<FormData> forms(1, form);
1224 FormsSeen(forms);
1226 GetAutofillSuggestions(form, field);
1228 // Add some Autocomplete suggestions.
1229 // This triggers the combined message send.
1230 std::vector<base::string16> suggestions;
1231 suggestions.push_back(ASCIIToUTF16("one"));
1232 suggestions.push_back(ASCIIToUTF16("two"));
1233 AutocompleteSuggestionsReturned(suggestions);
1235 // Test that we sent the right values to the external delegate.
1236 external_delegate_->CheckSuggestions(
1237 kDefaultPageID,
1238 Suggestion("one", "", "", 0),
1239 Suggestion("two", "", "", 0));
1242 // Test that we do not return duplicate values drawn from multiple profiles when
1243 // filling an already filled field.
1244 TEST_F(AutofillManagerTest, GetFieldSuggestionsWithDuplicateValues) {
1245 // Set up our form data.
1246 FormData form;
1247 test::CreateTestAddressFormData(&form);
1248 std::vector<FormData> forms(1, form);
1249 FormsSeen(forms);
1251 // |profile| will be owned by the mock PersonalDataManager.
1252 AutofillProfile* profile = new AutofillProfile;
1253 test::SetProfileInfo(
1254 profile, "Elvis", "", "", "", "", "", "", "", "", "", "", "");
1255 profile->set_guid("00000000-0000-0000-0000-000000000101");
1256 autofill_manager_->AddProfile(profile);
1258 FormFieldData& field = form.fields[0];
1259 field.is_autofilled = true;
1260 field.value = ASCIIToUTF16("Elvis");
1261 GetAutofillSuggestions(form, field);
1263 // No suggestions provided, so send an empty vector as the results.
1264 // This triggers the combined message send.
1265 AutocompleteSuggestionsReturned(std::vector<base::string16>());
1267 // Test that we sent the right values to the external delegate.
1268 external_delegate_->CheckSuggestions(
1269 kDefaultPageID,
1270 Suggestion("Elvis", "", "", 1));
1273 TEST_F(AutofillManagerTest, GetProfileSuggestionsFancyPhone) {
1274 // Set up our form data.
1275 FormData form;
1276 test::CreateTestAddressFormData(&form);
1277 std::vector<FormData> forms(1, form);
1278 FormsSeen(forms);
1280 AutofillProfile* profile = new AutofillProfile;
1281 profile->set_guid("00000000-0000-0000-0000-000000000103");
1282 profile->SetInfo(AutofillType(NAME_FULL), ASCIIToUTF16("Natty Bumppo"),
1283 "en-US");
1284 profile->SetRawInfo(PHONE_HOME_WHOLE_NUMBER,
1285 ASCIIToUTF16("1800PRAIRIE"));
1286 autofill_manager_->AddProfile(profile);
1288 const FormFieldData& field = form.fields[9];
1289 GetAutofillSuggestions(form, field);
1291 // No suggestions provided, so send an empty vector as the results.
1292 // This triggers the combined message send.
1293 AutocompleteSuggestionsReturned(std::vector<base::string16>());
1295 // Test that we sent the right values to the external delegate. Inferred
1296 // labels include the most private field of those that would be filled.
1297 external_delegate_->CheckSuggestions(
1298 kDefaultPageID,
1299 Suggestion("12345678901", "3734 Elvis Presley Blvd.", "", 1),
1300 Suggestion("23456789012", "123 Apple St.", "", 2),
1301 Suggestion("18007724743", "Natty Bumppo", "", 3)); // 1800PRAIRIE
1304 TEST_F(AutofillManagerTest, GetProfileSuggestionsForPhonePrefixOrSuffix) {
1305 // Set up our form data.
1306 FormData form;
1307 form.name = ASCIIToUTF16("MyForm");
1308 form.origin = GURL("http://myform.com/form.html");
1309 form.action = GURL("http://myform.com/submit.html");
1310 form.user_submitted = true;
1312 struct {
1313 const char* const label;
1314 const char* const name;
1315 size_t max_length;
1316 const char* const autocomplete_attribute;
1317 } test_fields[] = {{"country code", "country_code", 1, "tel-country-code"},
1318 {"area code", "area_code", 3, "tel-area-code"},
1319 {"phone", "phone_prefix", 3, "tel-local-prefix"},
1320 {"-", "phone_suffix", 4, "tel-local-suffix"},
1321 {"Phone Extension", "ext", 5, "tel-extension"}};
1323 FormFieldData field;
1324 for (size_t i = 0; i < arraysize(test_fields); ++i) {
1325 test::CreateTestFormField(
1326 test_fields[i].label, test_fields[i].name, "", "text", &field);
1327 field.max_length = test_fields[i].max_length;
1328 field.autocomplete_attribute = std::string();
1329 form.fields.push_back(field);
1332 std::vector<FormData> forms(1, form);
1333 FormsSeen(forms);
1335 AutofillProfile* profile = new AutofillProfile;
1336 profile->set_guid("00000000-0000-0000-0000-000000000104");
1337 profile->SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("1800FLOWERS"));
1338 personal_data_.ClearAutofillProfiles();
1339 autofill_manager_->AddProfile(profile);
1341 // The sublabels here are somewhat braindead until crbug.com/493247 is fixed.
1342 // TODO(estade): fix the bug and fix this test.
1343 const FormFieldData& phone_prefix = form.fields[2];
1344 GetAutofillSuggestions(form, phone_prefix);
1345 AutocompleteSuggestionsReturned(std::vector<base::string16>());
1346 // Test that we sent the right prefix values to the external delegate.
1347 external_delegate_->CheckSuggestions(kDefaultPageID,
1348 Suggestion("356", "18003569377", "", 1));
1350 const FormFieldData& phone_suffix = form.fields[3];
1351 GetAutofillSuggestions(form, phone_suffix);
1352 AutocompleteSuggestionsReturned(std::vector<base::string16>());
1353 // Test that we sent the right suffix values to the external delegate.
1354 external_delegate_->CheckSuggestions(
1355 kDefaultPageID, Suggestion("9377", "18003569377", "", 1));
1358 // Test that we correctly fill an address form.
1359 TEST_F(AutofillManagerTest, FillAddressForm) {
1360 // Set up our form data.
1361 FormData form;
1362 test::CreateTestAddressFormData(&form);
1363 std::vector<FormData> forms(1, form);
1364 FormsSeen(forms);
1366 std::string guid("00000000-0000-0000-0000-000000000001");
1367 AutofillProfile* profile =
1368 autofill_manager_->GetProfileWithGUID(guid.c_str());
1369 ASSERT_TRUE(profile);
1370 EXPECT_EQ(0U, profile->use_count());
1371 EXPECT_EQ(base::Time(), profile->use_date());
1373 int response_page_id = 0;
1374 FormData response_data;
1375 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0],
1376 MakeFrontendID(std::string(), guid),
1377 &response_page_id, &response_data);
1378 ExpectFilledAddressFormElvis(response_page_id, response_data, kDefaultPageID,
1379 false, true);
1381 EXPECT_EQ(1U, profile->use_count());
1382 EXPECT_NE(base::Time(), profile->use_date());
1385 TEST_F(AutofillManagerTest, WillFillCreditCardNumber) {
1386 // Set up our form data.
1387 FormData form;
1388 CreateTestCreditCardFormData(&form, true, false);
1389 std::vector<FormData> forms(1, form);
1390 FormsSeen(forms);
1392 FormFieldData* number_field = nullptr;
1393 FormFieldData* name_field = nullptr;
1394 FormFieldData* month_field = nullptr;
1395 for (size_t i = 0; i < form.fields.size(); ++i) {
1396 if (form.fields[i].name == ASCIIToUTF16("cardnumber"))
1397 number_field = &form.fields[i];
1398 else if (form.fields[i].name == ASCIIToUTF16("nameoncard"))
1399 name_field = &form.fields[i];
1400 else if (form.fields[i].name == ASCIIToUTF16("ccmonth"))
1401 month_field = &form.fields[i];
1404 // Empty form - whole form is Autofilled (except on iOS).
1405 EXPECT_TRUE(WillFillCreditCardNumber(form, *number_field));
1406 #if defined(OS_IOS)
1407 EXPECT_FALSE(WillFillCreditCardNumber(form, *name_field));
1408 #else
1409 EXPECT_TRUE(WillFillCreditCardNumber(form, *name_field));
1410 #endif // defined(OS_IOS)
1412 // If the user has entered a value, it won't be overridden.
1413 number_field->value = ASCIIToUTF16("gibberish");
1414 EXPECT_TRUE(WillFillCreditCardNumber(form, *number_field));
1415 EXPECT_FALSE(WillFillCreditCardNumber(form, *name_field));
1417 // But if that value is removed, it will be Autofilled (except on iOS).
1418 number_field->value.clear();
1419 #if defined(OS_IOS)
1420 EXPECT_FALSE(WillFillCreditCardNumber(form, *name_field));
1421 #else
1422 EXPECT_TRUE(WillFillCreditCardNumber(form, *name_field));
1423 #endif // defined(OS_IOS)
1425 // When part of the section is Autofilled, only fill the initiating field.
1426 month_field->is_autofilled = true;
1427 EXPECT_FALSE(WillFillCreditCardNumber(form, *name_field));
1428 EXPECT_TRUE(WillFillCreditCardNumber(form, *number_field));
1431 // Test that we correctly fill an address form from an auxiliary profile.
1432 TEST_F(AutofillManagerTest, FillAddressFormFromAuxiliaryProfile) {
1433 personal_data_.ClearAutofillProfiles();
1434 #if defined(OS_MACOSX) && !defined(OS_IOS)
1435 autofill_client_.GetPrefs()->SetBoolean(
1436 ::autofill::prefs::kAutofillUseMacAddressBook, true);
1437 #else
1438 autofill_client_.GetPrefs()->SetBoolean(
1439 ::autofill::prefs::kAutofillAuxiliaryProfilesEnabled, true);
1440 #endif // defined(OS_MACOSX) && !defined(OS_IOS)
1442 personal_data_.CreateTestAuxiliaryProfiles();
1444 // Set up our form data.
1445 FormData form;
1446 test::CreateTestAddressFormData(&form);
1447 std::vector<FormData> forms(1, form);
1448 FormsSeen(forms);
1450 std::string guid("00000000-0000-0000-0000-000000000001");
1451 int response_page_id = 0;
1452 FormData response_data;
1453 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0],
1454 MakeFrontendID(std::string(), guid),
1455 &response_page_id, &response_data);
1456 ExpectFilledAddressFormElvis(response_page_id, response_data, kDefaultPageID,
1457 false, true);
1460 // Test that we correctly fill a credit card form.
1461 TEST_F(AutofillManagerTest, FillCreditCardForm) {
1462 // Set up our form data.
1463 FormData form;
1464 CreateTestCreditCardFormData(&form, true, false);
1465 std::vector<FormData> forms(1, form);
1466 FormsSeen(forms);
1468 std::string guid("00000000-0000-0000-0000-000000000004");
1469 int response_page_id = 0;
1470 FormData response_data;
1471 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, *form.fields.begin(),
1472 MakeFrontendID(guid, std::string()),
1473 &response_page_id, &response_data);
1474 ExpectFilledCreditCardFormElvis(
1475 response_page_id, response_data, kDefaultPageID, false);
1478 // Test that we correctly fill a credit card form with month input type.
1479 // 1. year empty, month empty
1480 TEST_F(AutofillManagerTest, FillCreditCardFormNoYearNoMonth) {
1481 // Same as the SetUp(), but generate 4 credit cards with year month
1482 // combination.
1483 personal_data_.CreateTestCreditCardsYearAndMonth("", "");
1484 // Set up our form data.
1485 FormData form;
1486 CreateTestCreditCardFormData(&form, true, true);
1487 std::vector<FormData> forms(1, form);
1488 FormsSeen(forms);
1490 std::string guid("00000000-0000-0000-0000-000000000007");
1491 int response_page_id = 0;
1492 FormData response_data;
1493 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, *form.fields.begin(),
1494 MakeFrontendID(guid, std::string()),
1495 &response_page_id, &response_data);
1496 ExpectFilledCreditCardYearMonthWithYearMonth(response_page_id, response_data,
1497 kDefaultPageID, false, "", "");
1501 // Test that we correctly fill a credit card form with month input type.
1502 // 2. year empty, month non-empty
1503 TEST_F(AutofillManagerTest, FillCreditCardFormNoYearMonth) {
1504 // Same as the SetUp(), but generate 4 credit cards with year month
1505 // combination.
1506 personal_data_.CreateTestCreditCardsYearAndMonth("", "04");
1507 // Set up our form data.
1508 FormData form;
1509 CreateTestCreditCardFormData(&form, true, true);
1510 std::vector<FormData> forms(1, form);
1511 FormsSeen(forms);
1513 std::string guid("00000000-0000-0000-0000-000000000007");
1514 int response_page_id = 0;
1515 FormData response_data;
1516 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, *form.fields.begin(),
1517 MakeFrontendID(guid, std::string()),
1518 &response_page_id, &response_data);
1519 ExpectFilledCreditCardYearMonthWithYearMonth(response_page_id, response_data,
1520 kDefaultPageID, false, "", "04");
1523 // Test that we correctly fill a credit card form with month input type.
1524 // 3. year non-empty, month empty
1525 TEST_F(AutofillManagerTest, FillCreditCardFormYearNoMonth) {
1526 // Same as the SetUp(), but generate 4 credit cards with year month
1527 // combination.
1528 personal_data_.CreateTestCreditCardsYearAndMonth("2012", "");
1529 // Set up our form data.
1530 FormData form;
1531 CreateTestCreditCardFormData(&form, true, true);
1532 std::vector<FormData> forms(1, form);
1533 FormsSeen(forms);
1535 std::string guid("00000000-0000-0000-0000-000000000007");
1536 int response_page_id = 0;
1537 FormData response_data;
1538 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, *form.fields.begin(),
1539 MakeFrontendID(guid, std::string()),
1540 &response_page_id, &response_data);
1541 ExpectFilledCreditCardYearMonthWithYearMonth(response_page_id, response_data,
1542 kDefaultPageID, false, "2012", "");
1545 // Test that we correctly fill a credit card form with month input type.
1546 // 4. year non-empty, month empty
1547 TEST_F(AutofillManagerTest, FillCreditCardFormYearMonth) {
1548 // Same as the SetUp(), but generate 4 credit cards with year month
1549 // combination.
1550 personal_data_.ClearCreditCards();
1551 personal_data_.CreateTestCreditCardsYearAndMonth("2012", "04");
1552 // Set up our form data.
1553 FormData form;
1554 CreateTestCreditCardFormData(&form, true, true);
1555 std::vector<FormData> forms(1, form);
1556 FormsSeen(forms);
1558 std::string guid("00000000-0000-0000-0000-000000000007");
1559 int response_page_id = 0;
1560 FormData response_data;
1561 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, *form.fields.begin(),
1562 MakeFrontendID(guid, std::string()),
1563 &response_page_id, &response_data);
1564 ExpectFilledCreditCardYearMonthWithYearMonth(response_page_id, response_data,
1565 kDefaultPageID, false, "2012", "04");
1568 // Test that we correctly fill a combined address and credit card form.
1569 TEST_F(AutofillManagerTest, FillAddressAndCreditCardForm) {
1570 // Set up our form data.
1571 FormData form;
1572 test::CreateTestAddressFormData(&form);
1573 CreateTestCreditCardFormData(&form, true, false);
1574 std::vector<FormData> forms(1, form);
1575 FormsSeen(forms);
1577 // First fill the address data.
1578 std::string guid("00000000-0000-0000-0000-000000000001");
1579 int response_page_id = 0;
1580 FormData response_data;
1582 SCOPED_TRACE("Address");
1583 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0],
1584 MakeFrontendID(std::string(), guid),
1585 &response_page_id, &response_data);
1586 ExpectFilledAddressFormElvis(response_page_id, response_data,
1587 kDefaultPageID, true, true);
1590 // Now fill the credit card data.
1591 const int kPageID2 = 2;
1592 std::string guid2("00000000-0000-0000-0000-000000000004");
1593 response_page_id = 0;
1595 FillAutofillFormDataAndSaveResults(kPageID2, form, form.fields.back(),
1596 MakeFrontendID(guid2, std::string()),
1597 &response_page_id, &response_data);
1598 SCOPED_TRACE("Credit card");
1599 ExpectFilledCreditCardFormElvis(
1600 response_page_id, response_data, kPageID2, true);
1604 // Test that non-focusable field is ignored while inferring boundaries between
1605 // sections: http://crbug.com/231160
1606 TEST_F(AutofillManagerTest, FillFormWithNonFocusableFields) {
1607 // Create a form with both focusable and non-focusable fields.
1608 FormData form;
1609 form.name = ASCIIToUTF16("MyForm");
1610 form.origin = GURL("https://myform.com/form.html");
1611 form.action = GURL("https://myform.com/submit.html");
1612 form.user_submitted = true;
1614 FormFieldData field;
1616 test::CreateTestFormField("First Name", "firstname", "", "text", &field);
1617 form.fields.push_back(field);
1619 test::CreateTestFormField("", "lastname", "", "text", &field);
1620 form.fields.push_back(field);
1622 test::CreateTestFormField("", "email", "", "text", &field);
1623 form.fields.push_back(field);
1625 test::CreateTestFormField("Phone Number", "phonenumber", "", "tel", &field);
1626 form.fields.push_back(field);
1628 test::CreateTestFormField("", "email_", "", "text", &field);
1629 field.is_focusable = false;
1630 form.fields.push_back(field);
1632 test::CreateTestFormField("Country", "country", "", "text", &field);
1633 form.fields.push_back(field);
1635 std::vector<FormData> forms(1, form);
1636 FormsSeen(forms);
1638 // Fill the form
1639 std::string guid("00000000-0000-0000-0000-000000000001");
1640 int response_page_id = 0;
1641 FormData response_data;
1642 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0],
1643 MakeFrontendID(std::string(), guid),
1644 &response_page_id, &response_data);
1646 // The whole form should be filled as all the fields belong to the same
1647 // logical section.
1648 ASSERT_EQ(6U, response_data.fields.size());
1649 ExpectFilledField("First Name", "firstname", "Elvis", "text",
1650 response_data.fields[0]);
1651 ExpectFilledField("", "lastname", "Presley", "text",
1652 response_data.fields[1]);
1653 ExpectFilledField("", "email", "theking@gmail.com", "text",
1654 response_data.fields[2]);
1655 ExpectFilledField("Phone Number", "phonenumber", "12345678901", "tel",
1656 response_data.fields[3]);
1657 ExpectFilledField("", "email_", "theking@gmail.com", "text",
1658 response_data.fields[4]);
1659 ExpectFilledField("Country", "country", "United States", "text",
1660 response_data.fields[5]);
1663 // Test that we correctly fill a form that has multiple logical sections, e.g.
1664 // both a billing and a shipping address.
1665 TEST_F(AutofillManagerTest, FillFormWithMultipleSections) {
1666 // Set up our form data.
1667 FormData form;
1668 test::CreateTestAddressFormData(&form);
1669 const size_t kAddressFormSize = form.fields.size();
1670 test::CreateTestAddressFormData(&form);
1671 for (size_t i = kAddressFormSize; i < form.fields.size(); ++i) {
1672 // Make sure the fields have distinct names.
1673 form.fields[i].name = form.fields[i].name + ASCIIToUTF16("_");
1675 std::vector<FormData> forms(1, form);
1676 FormsSeen(forms);
1678 // Fill the first section.
1679 std::string guid("00000000-0000-0000-0000-000000000001");
1680 int response_page_id = 0;
1681 FormData response_data;
1682 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0],
1683 MakeFrontendID(std::string(), guid),
1684 &response_page_id, &response_data);
1686 SCOPED_TRACE("Address 1");
1687 // The second address section should be empty.
1688 ASSERT_EQ(response_data.fields.size(), 2*kAddressFormSize);
1689 for (size_t i = kAddressFormSize; i < form.fields.size(); ++i) {
1690 EXPECT_EQ(base::string16(), response_data.fields[i].value);
1693 // The first address section should be filled with Elvis's data.
1694 response_data.fields.resize(kAddressFormSize);
1695 ExpectFilledAddressFormElvis(response_page_id, response_data,
1696 kDefaultPageID, false, true);
1699 // Fill the second section, with the initiating field somewhere in the middle
1700 // of the section.
1701 const int kPageID2 = 2;
1702 std::string guid2("00000000-0000-0000-0000-000000000001");
1703 ASSERT_LT(9U, kAddressFormSize);
1704 response_page_id = 0;
1705 FillAutofillFormDataAndSaveResults(
1706 kPageID2, form, form.fields[kAddressFormSize + 9],
1707 MakeFrontendID(std::string(), guid2), &response_page_id, &response_data);
1709 SCOPED_TRACE("Address 2");
1710 ASSERT_EQ(response_data.fields.size(), form.fields.size());
1712 // The first address section should be empty.
1713 ASSERT_EQ(response_data.fields.size(), 2*kAddressFormSize);
1714 for (size_t i = 0; i < kAddressFormSize; ++i) {
1715 EXPECT_EQ(base::string16(), response_data.fields[i].value);
1718 // The second address section should be filled with Elvis's data.
1719 FormData secondSection = response_data;
1720 secondSection.fields.erase(secondSection.fields.begin(),
1721 secondSection.fields.begin() + kAddressFormSize);
1722 for (size_t i = 0; i < kAddressFormSize; ++i) {
1723 // Restore the expected field names.
1724 base::string16 name = secondSection.fields[i].name;
1725 base::string16 original_name = name.substr(0, name.size() - 1);
1726 secondSection.fields[i].name = original_name;
1728 ExpectFilledAddressFormElvis(response_page_id, secondSection, kPageID2,
1729 false, true);
1733 // Test that we correctly fill a form that has author-specified sections, which
1734 // might not match our expected section breakdown.
1735 TEST_F(AutofillManagerTest, FillFormWithAuthorSpecifiedSections) {
1736 // Create a form with a billing section and an unnamed section, interleaved.
1737 // The billing section includes both address and credit card fields.
1738 FormData form;
1739 form.name = ASCIIToUTF16("MyForm");
1740 form.origin = GURL("https://myform.com/form.html");
1741 form.action = GURL("https://myform.com/submit.html");
1742 form.user_submitted = true;
1744 FormFieldData field;
1746 test::CreateTestFormField("", "country", "", "text", &field);
1747 field.autocomplete_attribute = "section-billing country";
1748 form.fields.push_back(field);
1750 test::CreateTestFormField("", "firstname", "", "text", &field);
1751 field.autocomplete_attribute = "given-name";
1752 form.fields.push_back(field);
1754 test::CreateTestFormField("", "lastname", "", "text", &field);
1755 field.autocomplete_attribute = "family-name";
1756 form.fields.push_back(field);
1758 test::CreateTestFormField("", "address", "", "text", &field);
1759 field.autocomplete_attribute = "section-billing address-line1";
1760 form.fields.push_back(field);
1762 test::CreateTestFormField("", "city", "", "text", &field);
1763 field.autocomplete_attribute = "section-billing locality";
1764 form.fields.push_back(field);
1766 test::CreateTestFormField("", "state", "", "text", &field);
1767 field.autocomplete_attribute = "section-billing region";
1768 form.fields.push_back(field);
1770 test::CreateTestFormField("", "zip", "", "text", &field);
1771 field.autocomplete_attribute = "section-billing postal-code";
1772 form.fields.push_back(field);
1774 test::CreateTestFormField("", "ccname", "", "text", &field);
1775 field.autocomplete_attribute = "section-billing cc-name";
1776 form.fields.push_back(field);
1778 test::CreateTestFormField("", "ccnumber", "", "text", &field);
1779 field.autocomplete_attribute = "section-billing cc-number";
1780 form.fields.push_back(field);
1782 test::CreateTestFormField("", "ccexp", "", "text", &field);
1783 field.autocomplete_attribute = "section-billing cc-exp";
1784 form.fields.push_back(field);
1786 test::CreateTestFormField("", "email", "", "text", &field);
1787 field.autocomplete_attribute = "email";
1788 form.fields.push_back(field);
1790 std::vector<FormData> forms(1, form);
1791 FormsSeen(forms);
1793 // Fill the unnamed section.
1794 std::string guid("00000000-0000-0000-0000-000000000001");
1795 int response_page_id = 0;
1796 FormData response_data;
1797 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[1],
1798 MakeFrontendID(std::string(), guid),
1799 &response_page_id, &response_data);
1801 SCOPED_TRACE("Unnamed section");
1802 EXPECT_EQ(kDefaultPageID, response_page_id);
1803 EXPECT_EQ(ASCIIToUTF16("MyForm"), response_data.name);
1804 EXPECT_EQ(GURL("https://myform.com/form.html"), response_data.origin);
1805 EXPECT_EQ(GURL("https://myform.com/submit.html"), response_data.action);
1806 EXPECT_TRUE(response_data.user_submitted);
1807 ASSERT_EQ(11U, response_data.fields.size());
1809 ExpectFilledField("", "country", "", "text", response_data.fields[0]);
1810 ExpectFilledField("", "firstname", "Elvis", "text",
1811 response_data.fields[1]);
1812 ExpectFilledField("", "lastname", "Presley", "text",
1813 response_data.fields[2]);
1814 ExpectFilledField("", "address", "", "text", response_data.fields[3]);
1815 ExpectFilledField("", "city", "", "text", response_data.fields[4]);
1816 ExpectFilledField("", "state", "", "text", response_data.fields[5]);
1817 ExpectFilledField("", "zip", "", "text", response_data.fields[6]);
1818 ExpectFilledField("", "ccname", "", "text", response_data.fields[7]);
1819 ExpectFilledField("", "ccnumber", "", "text", response_data.fields[8]);
1820 ExpectFilledField("", "ccexp", "", "text", response_data.fields[9]);
1821 ExpectFilledField("", "email", "theking@gmail.com", "text",
1822 response_data.fields[10]);
1825 // Fill the address portion of the billing section.
1826 const int kPageID2 = 2;
1827 std::string guid2("00000000-0000-0000-0000-000000000001");
1828 response_page_id = 0;
1829 FillAutofillFormDataAndSaveResults(kPageID2, form, form.fields[0],
1830 MakeFrontendID(std::string(), guid2),
1831 &response_page_id, &response_data);
1833 SCOPED_TRACE("Billing address");
1834 EXPECT_EQ(kPageID2, response_page_id);
1835 EXPECT_EQ(ASCIIToUTF16("MyForm"), response_data.name);
1836 EXPECT_EQ(GURL("https://myform.com/form.html"), response_data.origin);
1837 EXPECT_EQ(GURL("https://myform.com/submit.html"), response_data.action);
1838 EXPECT_TRUE(response_data.user_submitted);
1839 ASSERT_EQ(11U, response_data.fields.size());
1841 ExpectFilledField("", "country", "US", "text",
1842 response_data.fields[0]);
1843 ExpectFilledField("", "firstname", "", "text", response_data.fields[1]);
1844 ExpectFilledField("", "lastname", "", "text", response_data.fields[2]);
1845 ExpectFilledField("", "address", "3734 Elvis Presley Blvd.", "text",
1846 response_data.fields[3]);
1847 ExpectFilledField("", "city", "Memphis", "text", response_data.fields[4]);
1848 ExpectFilledField("", "state", "Tennessee", "text",
1849 response_data.fields[5]);
1850 ExpectFilledField("", "zip", "38116", "text", response_data.fields[6]);
1851 ExpectFilledField("", "ccname", "", "text", response_data.fields[7]);
1852 ExpectFilledField("", "ccnumber", "", "text", response_data.fields[8]);
1853 ExpectFilledField("", "ccexp", "", "text", response_data.fields[9]);
1854 ExpectFilledField("", "email", "", "text", response_data.fields[10]);
1857 // Fill the credit card portion of the billing section.
1858 const int kPageID3 = 3;
1859 std::string guid3("00000000-0000-0000-0000-000000000004");
1860 response_page_id = 0;
1861 FillAutofillFormDataAndSaveResults(
1862 kPageID3, form, form.fields[form.fields.size() - 2],
1863 MakeFrontendID(guid3, std::string()), &response_page_id, &response_data);
1865 SCOPED_TRACE("Credit card");
1866 EXPECT_EQ(kPageID3, response_page_id);
1867 EXPECT_EQ(ASCIIToUTF16("MyForm"), response_data.name);
1868 EXPECT_EQ(GURL("https://myform.com/form.html"), response_data.origin);
1869 EXPECT_EQ(GURL("https://myform.com/submit.html"), response_data.action);
1870 EXPECT_TRUE(response_data.user_submitted);
1871 ASSERT_EQ(11U, response_data.fields.size());
1873 ExpectFilledField("", "country", "", "text", response_data.fields[0]);
1874 ExpectFilledField("", "firstname", "", "text", response_data.fields[1]);
1875 ExpectFilledField("", "lastname", "", "text", response_data.fields[2]);
1876 ExpectFilledField("", "address", "", "text", response_data.fields[3]);
1877 ExpectFilledField("", "city", "", "text", response_data.fields[4]);
1878 ExpectFilledField("", "state", "", "text", response_data.fields[5]);
1879 ExpectFilledField("", "zip", "", "text", response_data.fields[6]);
1880 ExpectFilledField("", "ccname", "Elvis Presley", "text",
1881 response_data.fields[7]);
1882 ExpectFilledField("", "ccnumber", "4234567890123456", "text",
1883 response_data.fields[8]);
1884 ExpectFilledField("", "ccexp", "04/2012", "text", response_data.fields[9]);
1885 ExpectFilledField("", "email", "", "text", response_data.fields[10]);
1889 // Test that we correctly fill a form that has a single logical section with
1890 // multiple email address fields.
1891 TEST_F(AutofillManagerTest, FillFormWithMultipleEmails) {
1892 // Set up our form data.
1893 FormData form;
1894 test::CreateTestAddressFormData(&form);
1895 FormFieldData field;
1896 test::CreateTestFormField("Confirm email", "email2", "", "text", &field);
1897 form.fields.push_back(field);
1899 std::vector<FormData> forms(1, form);
1900 FormsSeen(forms);
1902 // Fill the form.
1903 std::string guid("00000000-0000-0000-0000-000000000001");
1904 int response_page_id = 0;
1905 FormData response_data;
1906 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0],
1907 MakeFrontendID(std::string(), guid),
1908 &response_page_id, &response_data);
1910 // The second email address should be filled.
1911 EXPECT_EQ(ASCIIToUTF16("theking@gmail.com"),
1912 response_data.fields.back().value);
1914 // The remainder of the form should be filled as usual.
1915 response_data.fields.pop_back();
1916 ExpectFilledAddressFormElvis(response_page_id, response_data, kDefaultPageID,
1917 false, true);
1920 // Test that we correctly fill a previously auto-filled form.
1921 TEST_F(AutofillManagerTest, FillAutofilledForm) {
1922 // Set up our form data.
1923 FormData form;
1924 test::CreateTestAddressFormData(&form);
1925 // Mark one of the address fields as autofilled.
1926 form.fields[4].is_autofilled = true;
1927 CreateTestCreditCardFormData(&form, true, false);
1928 std::vector<FormData> forms(1, form);
1929 FormsSeen(forms);
1931 // First fill the address data.
1932 std::string guid("00000000-0000-0000-0000-000000000001");
1933 int response_page_id = 0;
1934 FormData response_data;
1935 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, *form.fields.begin(),
1936 MakeFrontendID(std::string(), guid),
1937 &response_page_id, &response_data);
1939 SCOPED_TRACE("Address");
1940 ExpectFilledForm(response_page_id, response_data, kDefaultPageID, "Elvis",
1941 "", "", "", "", "", "", "", "", "", "", "", "", "", "",
1942 true, true, false, true);
1945 // Now fill the credit card data.
1946 const int kPageID2 = 2;
1947 std::string guid2("00000000-0000-0000-0000-000000000004");
1948 response_page_id = 0;
1949 FillAutofillFormDataAndSaveResults(kPageID2, form, form.fields.back(),
1950 MakeFrontendID(guid2, std::string()),
1951 &response_page_id, &response_data);
1953 SCOPED_TRACE("Credit card 1");
1954 ExpectFilledCreditCardFormElvis(
1955 response_page_id, response_data, kPageID2, true);
1958 // Now set the credit card fields to also be auto-filled, and try again to
1959 // fill the credit card data
1960 for (std::vector<FormFieldData>::iterator iter = form.fields.begin();
1961 iter != form.fields.end();
1962 ++iter) {
1963 iter->is_autofilled = true;
1966 const int kPageID3 = 3;
1967 response_page_id = 0;
1968 FillAutofillFormDataAndSaveResults(kPageID3, form, *form.fields.rbegin(),
1969 MakeFrontendID(guid2, std::string()),
1970 &response_page_id, &response_data);
1972 SCOPED_TRACE("Credit card 2");
1973 ExpectFilledForm(response_page_id, response_data, kPageID3, "", "", "", "",
1974 "", "", "", "", "", "", "", "", "", "", "2012", true, true,
1975 false, true);
1979 // Test that we correctly fill a phone number split across multiple fields.
1980 TEST_F(AutofillManagerTest, FillPhoneNumber) {
1981 // In one form, rely on the maxlength attribute to imply phone number parts.
1982 // In the other form, rely on the autocompletetype attribute.
1983 FormData form_with_maxlength;
1984 form_with_maxlength.name = ASCIIToUTF16("MyMaxlengthPhoneForm");
1985 form_with_maxlength.origin = GURL("http://myform.com/phone_form.html");
1986 form_with_maxlength.action = GURL("http://myform.com/phone_submit.html");
1987 form_with_maxlength.user_submitted = true;
1988 FormData form_with_autocompletetype = form_with_maxlength;
1989 form_with_autocompletetype.name = ASCIIToUTF16("MyAutocompletetypePhoneForm");
1991 struct {
1992 const char* label;
1993 const char* name;
1994 size_t max_length;
1995 const char* autocomplete_attribute;
1996 } test_fields[] = {
1997 { "country code", "country_code", 1, "tel-country-code" },
1998 { "area code", "area_code", 3, "tel-area-code" },
1999 { "phone", "phone_prefix", 3, "tel-local-prefix" },
2000 { "-", "phone_suffix", 4, "tel-local-suffix" },
2001 { "Phone Extension", "ext", 3, "tel-extension" }
2004 FormFieldData field;
2005 const size_t default_max_length = field.max_length;
2006 for (size_t i = 0; i < arraysize(test_fields); ++i) {
2007 test::CreateTestFormField(
2008 test_fields[i].label, test_fields[i].name, "", "text", &field);
2009 field.max_length = test_fields[i].max_length;
2010 field.autocomplete_attribute = std::string();
2011 form_with_maxlength.fields.push_back(field);
2013 field.max_length = default_max_length;
2014 field.autocomplete_attribute = test_fields[i].autocomplete_attribute;
2015 form_with_autocompletetype.fields.push_back(field);
2018 std::vector<FormData> forms;
2019 forms.push_back(form_with_maxlength);
2020 forms.push_back(form_with_autocompletetype);
2021 FormsSeen(forms);
2023 // We should be able to fill prefix and suffix fields for US numbers.
2024 AutofillProfile* work_profile = autofill_manager_->GetProfileWithGUID(
2025 "00000000-0000-0000-0000-000000000002");
2026 ASSERT_TRUE(work_profile != NULL);
2027 work_profile->SetRawInfo(PHONE_HOME_WHOLE_NUMBER,
2028 ASCIIToUTF16("16505554567"));
2030 std::string guid(work_profile->guid());
2031 int page_id = 1;
2032 int response_page_id = 0;
2033 FormData response_data1;
2034 FillAutofillFormDataAndSaveResults(
2035 page_id, form_with_maxlength, *form_with_maxlength.fields.begin(),
2036 MakeFrontendID(std::string(), guid), &response_page_id, &response_data1);
2037 EXPECT_EQ(1, response_page_id);
2039 ASSERT_EQ(5U, response_data1.fields.size());
2040 EXPECT_EQ(ASCIIToUTF16("1"), response_data1.fields[0].value);
2041 EXPECT_EQ(ASCIIToUTF16("650"), response_data1.fields[1].value);
2042 EXPECT_EQ(ASCIIToUTF16("555"), response_data1.fields[2].value);
2043 EXPECT_EQ(ASCIIToUTF16("4567"), response_data1.fields[3].value);
2044 EXPECT_EQ(base::string16(), response_data1.fields[4].value);
2046 page_id = 2;
2047 response_page_id = 0;
2048 FormData response_data2;
2049 FillAutofillFormDataAndSaveResults(page_id, form_with_autocompletetype,
2050 *form_with_autocompletetype.fields.begin(),
2051 MakeFrontendID(std::string(), guid),
2052 &response_page_id, &response_data2);
2053 EXPECT_EQ(2, response_page_id);
2055 ASSERT_EQ(5U, response_data2.fields.size());
2056 EXPECT_EQ(ASCIIToUTF16("1"), response_data2.fields[0].value);
2057 EXPECT_EQ(ASCIIToUTF16("650"), response_data2.fields[1].value);
2058 EXPECT_EQ(ASCIIToUTF16("555"), response_data2.fields[2].value);
2059 EXPECT_EQ(ASCIIToUTF16("4567"), response_data2.fields[3].value);
2060 EXPECT_EQ(base::string16(), response_data2.fields[4].value);
2062 // We should not be able to fill prefix and suffix fields for international
2063 // numbers.
2064 work_profile->SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("GB"));
2065 work_profile->SetRawInfo(PHONE_HOME_WHOLE_NUMBER,
2066 ASCIIToUTF16("447700954321"));
2067 page_id = 3;
2068 response_page_id = 0;
2069 FormData response_data3;
2070 FillAutofillFormDataAndSaveResults(
2071 page_id, form_with_maxlength, *form_with_maxlength.fields.begin(),
2072 MakeFrontendID(std::string(), guid), &response_page_id, &response_data3);
2073 EXPECT_EQ(3, response_page_id);
2075 ASSERT_EQ(5U, response_data3.fields.size());
2076 EXPECT_EQ(ASCIIToUTF16("44"), response_data3.fields[0].value);
2077 EXPECT_EQ(ASCIIToUTF16("7700"), response_data3.fields[1].value);
2078 EXPECT_EQ(ASCIIToUTF16("954321"), response_data3.fields[2].value);
2079 EXPECT_EQ(ASCIIToUTF16("954321"), response_data3.fields[3].value);
2080 EXPECT_EQ(base::string16(), response_data3.fields[4].value);
2082 page_id = 4;
2083 response_page_id = 0;
2084 FormData response_data4;
2085 FillAutofillFormDataAndSaveResults(page_id, form_with_autocompletetype,
2086 *form_with_autocompletetype.fields.begin(),
2087 MakeFrontendID(std::string(), guid),
2088 &response_page_id, &response_data4);
2089 EXPECT_EQ(4, response_page_id);
2091 ASSERT_EQ(5U, response_data4.fields.size());
2092 EXPECT_EQ(ASCIIToUTF16("44"), response_data4.fields[0].value);
2093 EXPECT_EQ(ASCIIToUTF16("7700"), response_data4.fields[1].value);
2094 EXPECT_EQ(ASCIIToUTF16("954321"), response_data4.fields[2].value);
2095 EXPECT_EQ(ASCIIToUTF16("954321"), response_data4.fields[3].value);
2096 EXPECT_EQ(base::string16(), response_data4.fields[4].value);
2099 // Test that we can still fill a form when a field has been removed from it.
2100 TEST_F(AutofillManagerTest, FormChangesRemoveField) {
2101 // Set up our form data.
2102 FormData form;
2103 test::CreateTestAddressFormData(&form);
2105 // Add a field -- we'll remove it again later.
2106 FormFieldData field;
2107 test::CreateTestFormField("Some", "field", "", "text", &field);
2108 form.fields.insert(form.fields.begin() + 3, field);
2110 std::vector<FormData> forms(1, form);
2111 FormsSeen(forms);
2113 // Now, after the call to |FormsSeen|, we remove the field before filling.
2114 form.fields.erase(form.fields.begin() + 3);
2116 std::string guid("00000000-0000-0000-0000-000000000001");
2117 int response_page_id = 0;
2118 FormData response_data;
2119 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0],
2120 MakeFrontendID(std::string(), guid),
2121 &response_page_id, &response_data);
2122 ExpectFilledAddressFormElvis(response_page_id, response_data, kDefaultPageID,
2123 false, true);
2126 // Test that we can still fill a form when a field has been added to it.
2127 TEST_F(AutofillManagerTest, FormChangesAddField) {
2128 // The offset of the phone field in the address form.
2129 const int kPhoneFieldOffset = 9;
2131 // Set up our form data.
2132 FormData form;
2133 test::CreateTestAddressFormData(&form);
2135 // Remove the phone field -- we'll add it back later.
2136 std::vector<FormFieldData>::iterator pos =
2137 form.fields.begin() + kPhoneFieldOffset;
2138 FormFieldData field = *pos;
2139 pos = form.fields.erase(pos);
2141 std::vector<FormData> forms(1, form);
2142 FormsSeen(forms);
2144 // Now, after the call to |FormsSeen|, we restore the field before filling.
2145 form.fields.insert(pos, field);
2147 std::string guid("00000000-0000-0000-0000-000000000001");
2148 int response_page_id = 0;
2149 FormData response_data;
2150 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0],
2151 MakeFrontendID(std::string(), guid),
2152 &response_page_id, &response_data);
2153 ExpectFilledAddressFormElvis(response_page_id, response_data, kDefaultPageID,
2154 false, true);
2157 // Test that we are able to save form data when forms are submitted.
2158 TEST_F(AutofillManagerTest, FormSubmitted) {
2159 // Set up our form data.
2160 FormData form;
2161 test::CreateTestAddressFormData(&form);
2162 std::vector<FormData> forms(1, form);
2163 FormsSeen(forms);
2165 // Fill the form.
2166 std::string guid("00000000-0000-0000-0000-000000000001");
2167 int response_page_id = 0;
2168 FormData response_data;
2169 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0],
2170 MakeFrontendID(std::string(), guid),
2171 &response_page_id, &response_data);
2172 ExpectFilledAddressFormElvis(response_page_id, response_data, kDefaultPageID,
2173 false, true);
2175 // Simulate form submission. We should call into the PDM to try to save the
2176 // filled data.
2177 FormSubmitted(response_data);
2178 EXPECT_EQ(1, personal_data_.num_times_save_imported_profile_called());
2181 // Test that we are able to save form data when forms are not user submitted.
2182 TEST_F(AutofillManagerTest, FormSubmittedNotUserSubmitted) {
2183 // Set up our form data.
2184 FormData form;
2185 test::CreateTestAddressFormData(&form);
2186 // Mark the form as not user submitted.
2187 form.user_submitted = false;
2188 std::vector<FormData> forms(1, form);
2189 FormsSeen(forms);
2191 // Fill the form.
2192 std::string guid("00000000-0000-0000-0000-000000000001");
2193 int response_page_id = 0;
2194 FormData response_data;
2195 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0],
2196 MakeFrontendID(std::string(), guid),
2197 &response_page_id, &response_data);
2198 ExpectFilledAddressFormElvis(response_page_id, response_data, kDefaultPageID,
2199 false, false);
2201 // Simulate form submission. We should call into the PDM to try to save the
2202 // filled data.
2203 FormSubmitted(response_data);
2204 EXPECT_EQ(1, personal_data_.num_times_save_imported_profile_called());
2207 // Test that we are not saving form data when only the WillSubmitForm event is
2208 // sent.
2209 TEST_F(AutofillManagerTest, FormWillSubmitDoesNotSaveData) {
2210 // Set up our form data.
2211 FormData form;
2212 test::CreateTestAddressFormData(&form);
2213 std::vector<FormData> forms(1, form);
2214 FormsSeen(forms);
2216 // Fill the form.
2217 std::string guid("00000000-0000-0000-0000-000000000001");
2218 int response_page_id = 0;
2219 FormData response_data;
2220 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0],
2221 MakeFrontendID(std::string(), guid),
2222 &response_page_id, &response_data);
2223 ExpectFilledAddressFormElvis(response_page_id, response_data, kDefaultPageID,
2224 false, true);
2226 // Simulate OnWillSubmitForm(). We should *not* be calling into the PDM at
2227 // this point (since the form was not submitted). Does not call
2228 // OnFormSubmitted.
2229 autofill_manager_->ResetRunLoop();
2230 autofill_manager_->OnWillSubmitForm(response_data, base::TimeTicks::Now());
2231 autofill_manager_->WaitForAsyncOnWillSubmitForm();
2232 EXPECT_EQ(0, personal_data_.num_times_save_imported_profile_called());
2235 // Test that when Autocomplete is enabled and Autofill is disabled,
2236 // form submissions are still received by AutocompleteHistoryManager.
2237 TEST_F(AutofillManagerTest, FormSubmittedAutocompleteEnabled) {
2238 TestAutofillClient client;
2239 autofill_manager_.reset(
2240 new TestAutofillManager(autofill_driver_.get(), &client, NULL));
2241 autofill_manager_->set_autofill_enabled(false);
2242 autofill_manager_->autocomplete_history_manager_.reset(
2243 new MockAutocompleteHistoryManager(autofill_driver_.get(), &client));
2245 // Set up our form data.
2246 FormData form;
2247 test::CreateTestAddressFormData(&form);
2248 MockAutocompleteHistoryManager* m = static_cast<
2249 MockAutocompleteHistoryManager*>(
2250 autofill_manager_->autocomplete_history_manager_.get());
2251 EXPECT_CALL(*m,
2252 OnFormSubmitted(_)).Times(1);
2253 FormSubmitted(form);
2256 // Test that when Autocomplete is enabled and Autofill is disabled,
2257 // Autocomplete suggestions are still received.
2258 TEST_F(AutofillManagerTest, AutocompleteSuggestionsWhenAutofillDisabled) {
2259 TestAutofillClient client;
2260 autofill_manager_.reset(
2261 new TestAutofillManager(autofill_driver_.get(), &client, NULL));
2262 autofill_manager_->set_autofill_enabled(false);
2263 autofill_manager_->SetExternalDelegate(external_delegate_.get());
2265 // Set up our form data.
2266 FormData form;
2267 test::CreateTestAddressFormData(&form);
2268 std::vector<FormData> forms(1, form);
2269 FormsSeen(forms);
2270 const FormFieldData& field = form.fields[0];
2271 GetAutofillSuggestions(form, field);
2273 // Add some Autocomplete suggestions. We should return the autocomplete
2274 // suggestions, these will be culled by the renderer.
2275 std::vector<base::string16> suggestions;
2276 suggestions.push_back(ASCIIToUTF16("Jay"));
2277 suggestions.push_back(ASCIIToUTF16("Jason"));
2278 AutocompleteSuggestionsReturned(suggestions);
2280 external_delegate_->CheckSuggestions(
2281 kDefaultPageID,
2282 Suggestion("Jay", "", "", 0),
2283 Suggestion("Jason", "", "", 0));
2286 TEST_F(AutofillManagerTest, AutocompleteOffRespectedForAutocomplete) {
2287 TestAutofillClient client;
2288 autofill_manager_.reset(
2289 new TestAutofillManager(autofill_driver_.get(), &client, NULL));
2290 autofill_manager_->set_autofill_enabled(false);
2291 autofill_manager_->SetExternalDelegate(external_delegate_.get());
2293 autofill_manager_->autocomplete_history_manager_.reset(
2294 new MockAutocompleteHistoryManager(autofill_driver_.get(), &client));
2295 MockAutocompleteHistoryManager* m = static_cast<
2296 MockAutocompleteHistoryManager*>(
2297 autofill_manager_->autocomplete_history_manager_.get());
2298 EXPECT_CALL(*m,
2299 OnGetAutocompleteSuggestions(_, _, _, _, _)).Times(0);
2301 // Set up our form data.
2302 FormData form;
2303 test::CreateTestAddressFormData(&form);
2304 std::vector<FormData> forms(1, form);
2305 FormsSeen(forms);
2306 FormFieldData* field = &form.fields[0];
2307 field->should_autocomplete = false;
2308 GetAutofillSuggestions(form, *field);
2311 // Test that we are able to save form data when forms are submitted and we only
2312 // have server data for the field types.
2313 TEST_F(AutofillManagerTest, FormSubmittedServerTypes) {
2314 // Set up our form data.
2315 FormData form;
2316 test::CreateTestAddressFormData(&form);
2318 // Simulate having seen this form on page load.
2319 // |form_structure| will be owned by |autofill_manager_|.
2320 TestFormStructure* form_structure = new TestFormStructure(form);
2321 form_structure->DetermineHeuristicTypes();
2323 // Clear the heuristic types, and instead set the appropriate server types.
2324 std::vector<ServerFieldType> heuristic_types, server_types;
2325 for (size_t i = 0; i < form.fields.size(); ++i) {
2326 heuristic_types.push_back(UNKNOWN_TYPE);
2327 server_types.push_back(form_structure->field(i)->heuristic_type());
2329 form_structure->SetFieldTypes(heuristic_types, server_types);
2330 autofill_manager_->AddSeenForm(form_structure);
2332 // Fill the form.
2333 std::string guid("00000000-0000-0000-0000-000000000001");
2334 int response_page_id = 0;
2335 FormData response_data;
2336 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0],
2337 MakeFrontendID(std::string(), guid),
2338 &response_page_id, &response_data);
2339 ExpectFilledAddressFormElvis(response_page_id, response_data, kDefaultPageID,
2340 false, true);
2342 // Simulate form submission. We should call into the PDM to try to save the
2343 // filled data.
2344 FormSubmitted(response_data);
2345 EXPECT_EQ(1, personal_data_.num_times_save_imported_profile_called());
2348 // Test that we are able to save form data after the possible types have been
2349 // determined. We do two submissions and verify that only at the second
2350 // submission are the possible types able to be inferred.
2351 TEST_F(AutofillManagerTest, FormSubmittedPossibleTypesTwoSubmissions) {
2352 // Set up our form data.
2353 FormData form;
2354 std::vector<ServerFieldTypeSet> expected_types;
2355 test::CreateTestAddressFormData(&form, &expected_types);
2357 // Fill the form.
2358 std::string guid("00000000-0000-0000-0000-000000000001");
2359 int response_page_id = 0;
2360 FormData response_data;
2361 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[0],
2362 MakeFrontendID(std::string(), guid),
2363 &response_page_id, &response_data);
2364 ExpectFilledAddressFormElvis(response_page_id, response_data, kDefaultPageID,
2365 false, true);
2367 personal_data_.ClearAutofillProfiles();
2368 ASSERT_EQ(0u, personal_data_.GetProfiles().size());
2370 // Simulate form submission. The first submission should not count the data
2371 // towards possible types. Therefore we expect all UNKNOWN_TYPE entries.
2372 ServerFieldTypeSet type_set;
2373 type_set.insert(UNKNOWN_TYPE);
2374 std::vector<ServerFieldTypeSet> unknown_types(expected_types.size(),
2375 type_set);
2376 autofill_manager_->set_expected_submitted_field_types(unknown_types);
2377 FormSubmitted(response_data);
2378 ASSERT_EQ(1u, personal_data_.GetProfiles().size());
2380 // The second submission should now have data by which to infer types.
2381 autofill_manager_->set_expected_submitted_field_types(expected_types);
2382 FormSubmitted(response_data);
2383 ASSERT_EQ(2u, personal_data_.GetProfiles().size());
2386 // Test that the form signature for an uploaded form always matches the form
2387 // signature from the query.
2388 TEST_F(AutofillManagerTest, FormSubmittedWithDifferentFields) {
2389 // Set up our form data.
2390 FormData form;
2391 test::CreateTestAddressFormData(&form);
2392 std::vector<FormData> forms(1, form);
2393 FormsSeen(forms);
2395 // Cache the expected form signature.
2396 std::string signature = FormStructure(form).FormSignature();
2398 // Change the structure of the form prior to submission.
2399 // Websites would typically invoke JavaScript either on page load or on form
2400 // submit to achieve this.
2401 form.fields.pop_back();
2402 FormFieldData field = form.fields[3];
2403 form.fields[3] = form.fields[7];
2404 form.fields[7] = field;
2406 // Simulate form submission.
2407 FormSubmitted(form);
2408 EXPECT_EQ(signature, autofill_manager_->GetSubmittedFormSignature());
2411 // Test that we do not save form data when submitted fields contain default
2412 // values.
2413 TEST_F(AutofillManagerTest, FormSubmittedWithDefaultValues) {
2414 // Set up our form data.
2415 FormData form;
2416 test::CreateTestAddressFormData(&form);
2417 form.fields[3].value = ASCIIToUTF16("Enter your address");
2419 // Convert the state field to a <select> popup, to make sure that we only
2420 // reject default values for text fields.
2421 ASSERT_TRUE(form.fields[6].name == ASCIIToUTF16("state"));
2422 form.fields[6].form_control_type = "select-one";
2423 form.fields[6].value = ASCIIToUTF16("Tennessee");
2425 std::vector<FormData> forms(1, form);
2426 FormsSeen(forms);
2428 // Fill the form.
2429 std::string guid("00000000-0000-0000-0000-000000000001");
2430 int response_page_id = 0;
2431 FormData response_data;
2432 FillAutofillFormDataAndSaveResults(kDefaultPageID, form, form.fields[3],
2433 MakeFrontendID(std::string(), guid),
2434 &response_page_id, &response_data);
2436 // Simulate form submission. We should call into the PDM to try to save the
2437 // filled data.
2438 FormSubmitted(response_data);
2439 EXPECT_EQ(1, personal_data_.num_times_save_imported_profile_called());
2441 // Set the address field's value back to the default value.
2442 response_data.fields[3].value = ASCIIToUTF16("Enter your address");
2444 // Simulate form submission. We should not call into the PDM to try to save
2445 // the filled data, since the filled form is effectively missing an address.
2446 FormSubmitted(response_data);
2447 EXPECT_EQ(1, personal_data_.num_times_save_imported_profile_called());
2450 // Checks that resetting the auxiliary profile enabled preference does the right
2451 // thing on all platforms.
2452 TEST_F(AutofillManagerTest, AuxiliaryProfilesReset) {
2453 PrefService* prefs = autofill_client_.GetPrefs();
2454 #if defined(OS_MACOSX)
2455 // Auxiliary profiles is implemented on Mac only.
2456 // OSX: This preference exists for legacy reasons. It is no longer used.
2457 ASSERT_TRUE(
2458 prefs->GetBoolean(::autofill::prefs::kAutofillAuxiliaryProfilesEnabled));
2459 prefs->SetBoolean(::autofill::prefs::kAutofillAuxiliaryProfilesEnabled,
2460 false);
2461 prefs->ClearPref(::autofill::prefs::kAutofillAuxiliaryProfilesEnabled);
2462 ASSERT_TRUE(
2463 prefs->GetBoolean(::autofill::prefs::kAutofillAuxiliaryProfilesEnabled));
2464 #else
2465 ASSERT_FALSE(
2466 prefs->GetBoolean(::autofill::prefs::kAutofillAuxiliaryProfilesEnabled));
2467 prefs->SetBoolean(::autofill::prefs::kAutofillAuxiliaryProfilesEnabled, true);
2468 prefs->ClearPref(::autofill::prefs::kAutofillAuxiliaryProfilesEnabled);
2469 ASSERT_FALSE(
2470 prefs->GetBoolean(::autofill::prefs::kAutofillAuxiliaryProfilesEnabled));
2471 #endif // defined(OS_MACOSX)
2474 TEST_F(AutofillManagerTest, DeterminePossibleFieldTypesForUpload) {
2475 FormData form;
2476 form.name = ASCIIToUTF16("MyForm");
2477 form.origin = GURL("http://myform.com/form.html");
2478 form.action = GURL("http://myform.com/submit.html");
2479 form.user_submitted = true;
2481 std::vector<ServerFieldTypeSet> expected_types;
2482 std::vector<base::string16> expected_values;
2484 // These fields should all match.
2485 FormFieldData field;
2486 ServerFieldTypeSet types;
2487 test::CreateTestFormField("", "1", "", "text", &field);
2488 expected_values.push_back(ASCIIToUTF16("Elvis"));
2489 types.clear();
2490 types.insert(NAME_FIRST);
2491 form.fields.push_back(field);
2492 expected_types.push_back(types);
2494 test::CreateTestFormField("", "2", "", "text", &field);
2495 expected_values.push_back(ASCIIToUTF16("Aaron"));
2496 types.clear();
2497 types.insert(NAME_MIDDLE);
2498 form.fields.push_back(field);
2499 expected_types.push_back(types);
2501 test::CreateTestFormField("", "3", "", "text", &field);
2502 expected_values.push_back(ASCIIToUTF16("A"));
2503 types.clear();
2504 types.insert(NAME_MIDDLE_INITIAL);
2505 form.fields.push_back(field);
2506 expected_types.push_back(types);
2508 test::CreateTestFormField("", "4", "", "text", &field);
2509 expected_values.push_back(ASCIIToUTF16("Presley"));
2510 types.clear();
2511 types.insert(NAME_LAST);
2512 form.fields.push_back(field);
2513 expected_types.push_back(types);
2515 test::CreateTestFormField("", "5", "", "text", &field);
2516 expected_values.push_back(ASCIIToUTF16("Elvis Presley"));
2517 types.clear();
2518 types.insert(CREDIT_CARD_NAME);
2519 form.fields.push_back(field);
2520 expected_types.push_back(types);
2522 test::CreateTestFormField("", "6", "", "text", &field);
2523 expected_values.push_back(ASCIIToUTF16("Elvis Aaron Presley"));
2524 types.clear();
2525 types.insert(NAME_FULL);
2526 form.fields.push_back(field);
2527 expected_types.push_back(types);
2529 test::CreateTestFormField("", "7", "", "email", &field);
2530 expected_values.push_back(ASCIIToUTF16("theking@gmail.com"));
2531 types.clear();
2532 types.insert(EMAIL_ADDRESS);
2533 form.fields.push_back(field);
2534 expected_types.push_back(types);
2536 test::CreateTestFormField("", "8", "", "text", &field);
2537 expected_values.push_back(ASCIIToUTF16("RCA"));
2538 types.clear();
2539 types.insert(COMPANY_NAME);
2540 form.fields.push_back(field);
2541 expected_types.push_back(types);
2543 test::CreateTestFormField("", "9", "", "text", &field);
2544 expected_values.push_back(ASCIIToUTF16("3734 Elvis Presley Blvd."));
2545 types.clear();
2546 types.insert(ADDRESS_HOME_LINE1);
2547 form.fields.push_back(field);
2548 expected_types.push_back(types);
2550 test::CreateTestFormField("", "10", "", "text", &field);
2551 expected_values.push_back(ASCIIToUTF16("Apt. 10"));
2552 types.clear();
2553 types.insert(ADDRESS_HOME_LINE2);
2554 form.fields.push_back(field);
2555 expected_types.push_back(types);
2557 test::CreateTestFormField("", "11", "", "text", &field);
2558 expected_values.push_back(ASCIIToUTF16("Memphis"));
2559 types.clear();
2560 types.insert(ADDRESS_HOME_CITY);
2561 form.fields.push_back(field);
2562 expected_types.push_back(types);
2564 test::CreateTestFormField("", "12", "", "text", &field);
2565 expected_values.push_back(ASCIIToUTF16("Tennessee"));
2566 types.clear();
2567 types.insert(ADDRESS_HOME_STATE);
2568 form.fields.push_back(field);
2569 expected_types.push_back(types);
2571 test::CreateTestFormField("", "13", "", "text", &field);
2572 expected_values.push_back(ASCIIToUTF16("38116"));
2573 types.clear();
2574 types.insert(ADDRESS_HOME_ZIP);
2575 form.fields.push_back(field);
2576 expected_types.push_back(types);
2578 test::CreateTestFormField("", "14", "", "text", &field);
2579 expected_values.push_back(ASCIIToUTF16("USA"));
2580 types.clear();
2581 types.insert(ADDRESS_HOME_COUNTRY);
2582 form.fields.push_back(field);
2583 expected_types.push_back(types);
2585 test::CreateTestFormField("", "15", "", "text", &field);
2586 expected_values.push_back(ASCIIToUTF16("United States"));
2587 types.clear();
2588 types.insert(ADDRESS_HOME_COUNTRY);
2589 form.fields.push_back(field);
2590 expected_types.push_back(types);
2592 test::CreateTestFormField("", "16", "", "text", &field);
2593 expected_values.push_back(ASCIIToUTF16("+1 (234) 567-8901"));
2594 types.clear();
2595 types.insert(PHONE_HOME_WHOLE_NUMBER);
2596 form.fields.push_back(field);
2597 expected_types.push_back(types);
2599 test::CreateTestFormField("", "17", "", "text", &field);
2600 expected_values.push_back(ASCIIToUTF16("2345678901"));
2601 types.clear();
2602 types.insert(PHONE_HOME_CITY_AND_NUMBER);
2603 form.fields.push_back(field);
2604 expected_types.push_back(types);
2606 test::CreateTestFormField("", "18", "", "text", &field);
2607 expected_values.push_back(ASCIIToUTF16("1"));
2608 types.clear();
2609 types.insert(PHONE_HOME_COUNTRY_CODE);
2610 form.fields.push_back(field);
2611 expected_types.push_back(types);
2613 test::CreateTestFormField("", "19", "", "text", &field);
2614 expected_values.push_back(ASCIIToUTF16("234"));
2615 types.clear();
2616 types.insert(PHONE_HOME_CITY_CODE);
2617 form.fields.push_back(field);
2618 expected_types.push_back(types);
2620 test::CreateTestFormField("", "20", "", "text", &field);
2621 expected_values.push_back(ASCIIToUTF16("5678901"));
2622 types.clear();
2623 types.insert(PHONE_HOME_NUMBER);
2624 form.fields.push_back(field);
2625 expected_types.push_back(types);
2627 test::CreateTestFormField("", "21", "", "text", &field);
2628 expected_values.push_back(ASCIIToUTF16("567"));
2629 types.clear();
2630 types.insert(PHONE_HOME_NUMBER);
2631 form.fields.push_back(field);
2632 expected_types.push_back(types);
2634 test::CreateTestFormField("", "22", "", "text", &field);
2635 expected_values.push_back(ASCIIToUTF16("8901"));
2636 types.clear();
2637 types.insert(PHONE_HOME_NUMBER);
2638 form.fields.push_back(field);
2639 expected_types.push_back(types);
2641 test::CreateTestFormField("", "23", "", "text", &field);
2642 expected_values.push_back(ASCIIToUTF16("4234-5678-9012-3456"));
2643 types.clear();
2644 types.insert(CREDIT_CARD_NUMBER);
2645 form.fields.push_back(field);
2646 expected_types.push_back(types);
2648 test::CreateTestFormField("", "24", "", "text", &field);
2649 expected_values.push_back(ASCIIToUTF16("04"));
2650 types.clear();
2651 types.insert(CREDIT_CARD_EXP_MONTH);
2652 form.fields.push_back(field);
2653 expected_types.push_back(types);
2655 test::CreateTestFormField("", "25", "", "text", &field);
2656 expected_values.push_back(ASCIIToUTF16("April"));
2657 types.clear();
2658 types.insert(CREDIT_CARD_EXP_MONTH);
2659 form.fields.push_back(field);
2660 expected_types.push_back(types);
2662 test::CreateTestFormField("", "26", "", "text", &field);
2663 expected_values.push_back(ASCIIToUTF16("2012"));
2664 types.clear();
2665 types.insert(CREDIT_CARD_EXP_4_DIGIT_YEAR);
2666 form.fields.push_back(field);
2667 expected_types.push_back(types);
2669 test::CreateTestFormField("", "27", "", "text", &field);
2670 expected_values.push_back(ASCIIToUTF16("12"));
2671 types.clear();
2672 types.insert(CREDIT_CARD_EXP_2_DIGIT_YEAR);
2673 form.fields.push_back(field);
2674 expected_types.push_back(types);
2676 test::CreateTestFormField("", "28", "", "text", &field);
2677 expected_values.push_back(ASCIIToUTF16("04/2012"));
2678 types.clear();
2679 types.insert(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR);
2680 form.fields.push_back(field);
2681 expected_types.push_back(types);
2683 // Make sure that we trim whitespace properly.
2684 test::CreateTestFormField("", "29", "", "text", &field);
2685 expected_values.push_back(ASCIIToUTF16(""));
2686 types.clear();
2687 types.insert(EMPTY_TYPE);
2688 form.fields.push_back(field);
2689 expected_types.push_back(types);
2691 test::CreateTestFormField("", "30", " ", "text", &field);
2692 expected_values.push_back(ASCIIToUTF16(" "));
2693 types.clear();
2694 types.insert(EMPTY_TYPE);
2695 form.fields.push_back(field);
2696 expected_types.push_back(types);
2698 test::CreateTestFormField("", "31", " ", "text", &field);
2699 expected_values.push_back(ASCIIToUTF16(" Elvis"));
2700 types.clear();
2701 types.insert(NAME_FIRST);
2702 form.fields.push_back(field);
2703 expected_types.push_back(types);
2705 test::CreateTestFormField("", "32", " ", "text", &field);
2706 expected_values.push_back(ASCIIToUTF16("Elvis "));
2707 types.clear();
2708 types.insert(NAME_FIRST);
2709 form.fields.push_back(field);
2710 expected_types.push_back(types);
2712 // These fields should not match, as they differ by case.
2713 test::CreateTestFormField("", "33", "", "text", &field);
2714 expected_values.push_back(ASCIIToUTF16("elvis"));
2715 types.clear();
2716 types.insert(UNKNOWN_TYPE);
2717 form.fields.push_back(field);
2718 expected_types.push_back(types);
2720 test::CreateTestFormField("", "34", "", "text", &field);
2721 expected_values.push_back(ASCIIToUTF16("3734 Elvis Presley BLVD"));
2722 types.clear();
2723 types.insert(UNKNOWN_TYPE);
2724 form.fields.push_back(field);
2725 expected_types.push_back(types);
2727 // These fields should not match, as they are unsupported variants.
2728 test::CreateTestFormField("", "35", "", "text", &field);
2729 expected_values.push_back(ASCIIToUTF16("Elvis Aaron"));
2730 types.clear();
2731 types.insert(UNKNOWN_TYPE);
2732 form.fields.push_back(field);
2733 expected_types.push_back(types);
2735 test::CreateTestFormField("", "36", "", "text", &field);
2736 expected_values.push_back(ASCIIToUTF16("Mr. Presley"));
2737 types.clear();
2738 types.insert(UNKNOWN_TYPE);
2739 form.fields.push_back(field);
2740 expected_types.push_back(types);
2742 test::CreateTestFormField("", "37", "", "text", &field);
2743 expected_values.push_back(ASCIIToUTF16("3734 Elvis Presley"));
2744 types.clear();
2745 types.insert(UNKNOWN_TYPE);
2746 form.fields.push_back(field);
2747 expected_types.push_back(types);
2749 test::CreateTestFormField("", "38", "", "text", &field);
2750 expected_values.push_back(ASCIIToUTF16("TN"));
2751 types.clear();
2752 types.insert(UNKNOWN_TYPE);
2753 form.fields.push_back(field);
2754 expected_types.push_back(types);
2756 test::CreateTestFormField("", "39", "", "text", &field);
2757 expected_values.push_back(ASCIIToUTF16("38116-1023"));
2758 types.clear();
2759 types.insert(UNKNOWN_TYPE);
2760 form.fields.push_back(field);
2761 expected_types.push_back(types);
2763 test::CreateTestFormField("", "20", "", "text", &field);
2764 expected_values.push_back(ASCIIToUTF16("5"));
2765 types.clear();
2766 types.insert(UNKNOWN_TYPE);
2767 form.fields.push_back(field);
2768 expected_types.push_back(types);
2770 test::CreateTestFormField("", "20", "", "text", &field);
2771 expected_values.push_back(ASCIIToUTF16("56"));
2772 types.clear();
2773 types.insert(UNKNOWN_TYPE);
2774 form.fields.push_back(field);
2775 expected_types.push_back(types);
2777 test::CreateTestFormField("", "20", "", "text", &field);
2778 expected_values.push_back(ASCIIToUTF16("901"));
2779 types.clear();
2780 types.insert(UNKNOWN_TYPE);
2781 form.fields.push_back(field);
2782 expected_types.push_back(types);
2784 // Make sure the form is in the cache so that it is processed for Autofill
2785 // upload.
2786 std::vector<FormData> forms(1, form);
2787 FormsSeen(forms);
2789 // Once the form is cached, fill the values.
2790 EXPECT_EQ(form.fields.size(), expected_values.size());
2791 for (size_t i = 0; i < expected_values.size(); i++) {
2792 form.fields[i].value = expected_values[i];
2795 autofill_manager_->set_expected_submitted_field_types(expected_types);
2796 FormSubmitted(form);
2799 TEST_F(AutofillManagerTest, RemoveProfile) {
2800 // Add and remove an Autofill profile.
2801 AutofillProfile* profile = new AutofillProfile;
2802 std::string guid = "00000000-0000-0000-0000-000000000102";
2803 profile->set_guid(guid.c_str());
2804 autofill_manager_->AddProfile(profile);
2806 int id = MakeFrontendID(std::string(), guid);
2808 autofill_manager_->RemoveAutofillProfileOrCreditCard(id);
2810 EXPECT_FALSE(autofill_manager_->GetProfileWithGUID(guid.c_str()));
2813 TEST_F(AutofillManagerTest, RemoveCreditCard){
2814 // Add and remove an Autofill credit card.
2815 CreditCard* credit_card = new CreditCard;
2816 std::string guid = "00000000-0000-0000-0000-000000100007";
2817 credit_card->set_guid(guid.c_str());
2818 autofill_manager_->AddCreditCard(credit_card);
2820 int id = MakeFrontendID(guid, std::string());
2822 autofill_manager_->RemoveAutofillProfileOrCreditCard(id);
2824 EXPECT_FALSE(autofill_manager_->GetCreditCardWithGUID(guid.c_str()));
2827 #if defined(OS_MACOSX) && !defined(OS_IOS)
2828 TEST_F(AutofillManagerTest, AccessAddressBookPrompt) {
2829 // TODO(erikchen): After Address Book integration has been disabled for 6
2830 // weeks, and there are no major problems, rip out all the code. Expected
2831 // removal date: 07/15/2015. http://crbug.com/488146.
2832 return;
2834 FormData form;
2835 test::CreateTestAddressFormData(&form);
2836 std::vector<FormData> forms(1, form);
2837 FormsSeen(forms);
2838 FormFieldData& field = form.fields[0];
2839 field.should_autocomplete = true;
2841 // A profile already exists.
2842 EXPECT_FALSE(
2843 autofill_manager_->ShouldShowAccessAddressBookSuggestion(form, field));
2845 // Remove all profiles.
2846 personal_data_.ClearAutofillProfiles();
2847 EXPECT_TRUE(
2848 autofill_manager_->ShouldShowAccessAddressBookSuggestion(form, field));
2850 field.should_autocomplete = false;
2851 EXPECT_TRUE(
2852 autofill_manager_->ShouldShowAccessAddressBookSuggestion(form, field));
2854 #endif // defined(OS_MACOSX) && !defined(OS_IOS)
2856 // Test our external delegate is called at the right time.
2857 TEST_F(AutofillManagerTest, TestExternalDelegate) {
2858 FormData form;
2859 test::CreateTestAddressFormData(&form);
2860 std::vector<FormData> forms(1, form);
2861 FormsSeen(forms);
2862 const FormFieldData& field = form.fields[0];
2863 GetAutofillSuggestions(form, field); // should call the delegate's OnQuery()
2865 EXPECT_TRUE(external_delegate_->on_query_seen());
2868 // Test to verify suggestions appears for forms having credit card number split
2869 // across fields.
2870 TEST_F(AutofillManagerTest, GetCreditCardSuggestionsForNumberSpitAcrossFields) {
2871 // Set up our form data with credit card number split across fields.
2872 FormData form;
2873 form.name = ASCIIToUTF16("MyForm");
2874 form.origin = GURL("https://myform.com/form.html");
2875 form.action = GURL("https://myform.com/submit.html");
2876 form.user_submitted = true;
2878 FormFieldData name_field;
2879 test::CreateTestFormField("Name on Card", "nameoncard", "", "text",
2880 &name_field);
2881 form.fields.push_back(name_field);
2883 // Add new 4 |card_number_field|s to the |form|.
2884 FormFieldData card_number_field;
2885 card_number_field.max_length = 4;
2886 test::CreateTestFormField("Card Number", "cardnumber_1", "", "text",
2887 &card_number_field);
2888 form.fields.push_back(card_number_field);
2890 test::CreateTestFormField("", "cardnumber_2", "", "text", &card_number_field);
2891 form.fields.push_back(card_number_field);
2893 test::CreateTestFormField("", "cardnumber_3", "", "text", &card_number_field);
2894 form.fields.push_back(card_number_field);
2896 test::CreateTestFormField("", "cardnumber_4", "", "text", &card_number_field);
2897 form.fields.push_back(card_number_field);
2899 FormFieldData exp_field;
2900 test::CreateTestFormField("Expiration Date", "ccmonth", "", "text",
2901 &exp_field);
2902 form.fields.push_back(exp_field);
2904 test::CreateTestFormField("", "ccyear", "", "text", &exp_field);
2905 form.fields.push_back(exp_field);
2907 std::vector<FormData> forms(1, form);
2908 FormsSeen(forms);
2910 // Verify whether suggestions are populated correctly for one of the middle
2911 // credit card number fields when filled partially.
2912 FormFieldData number_field = form.fields[3];
2913 number_field.value = ASCIIToUTF16("901");
2915 // Get the suggestions for already filled credit card |number_field|.
2916 GetAutofillSuggestions(form, number_field);
2918 // No autocomplete suggestions provided, so send an empty vector as the
2919 // results. This triggers the combined message send.
2920 AutocompleteSuggestionsReturned(std::vector<base::string16>());
2922 external_delegate_->CheckSuggestions(
2923 kDefaultPageID,
2924 Suggestion(
2925 "Visa\xC2\xA0\xE2\x8B\xAF"
2926 "3456",
2927 "04/12", kVisaCard, autofill_manager_->GetPackedCreditCardID(4)));
2930 // Test that inputs detected to be CVC inputs are forced to
2931 // !should_autocomplete for AutocompleteHistoryManager::OnFormSubmitted.
2932 TEST_F(AutofillManagerTest, DontSaveCvcInAutocompleteHistory) {
2933 autofill_manager_->autocomplete_history_manager_.reset(
2934 new MockAutocompleteHistoryManager(autofill_driver_.get(),
2935 &autofill_client_));
2936 FormData form_seen_by_ahm;
2937 MockAutocompleteHistoryManager* mock_ahm =
2938 static_cast<MockAutocompleteHistoryManager*>(
2939 autofill_manager_->autocomplete_history_manager_.get());
2940 EXPECT_CALL(*mock_ahm, OnFormSubmitted(_))
2941 .WillOnce(SaveArg<0>(&form_seen_by_ahm));
2943 FormData form;
2944 form.name = ASCIIToUTF16("MyForm");
2945 form.origin = GURL("http://myform.com/form.html");
2946 form.action = GURL("http://myform.com/submit.html");
2947 form.user_submitted = true;
2949 struct {
2950 const char* label;
2951 const char* name;
2952 const char* value;
2953 ServerFieldType expected_field_type;
2954 } fields[] = {
2955 {"Card number", "1", "4234-5678-9012-3456", CREDIT_CARD_NUMBER},
2956 {"Card verification code", "2", "123", CREDIT_CARD_VERIFICATION_CODE},
2957 {"expiration date", "3", "04/2020", CREDIT_CARD_EXP_4_DIGIT_YEAR},
2960 for (size_t i = 0; i < arraysize(fields); ++i) {
2961 FormFieldData field;
2962 test::CreateTestFormField(fields[i].label, fields[i].name, fields[i].value,
2963 "text", &field);
2964 form.fields.push_back(field);
2967 std::vector<FormData> forms(1, form);
2968 FormsSeen(forms);
2969 FormSubmitted(form);
2971 EXPECT_EQ(form.fields.size(), form_seen_by_ahm.fields.size());
2972 ASSERT_EQ(arraysize(fields), form_seen_by_ahm.fields.size());
2973 for (size_t i = 0; i < arraysize(fields); ++i) {
2974 EXPECT_EQ(form_seen_by_ahm.fields[i].should_autocomplete,
2975 fields[i].expected_field_type != CREDIT_CARD_VERIFICATION_CODE);
2979 TEST_F(AutofillManagerTest, DontOfferToSaveWalletCard) {
2980 // This line silences the warning from RealPanWalletClient about matching
2981 // sync and wallet server types.
2982 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
2983 "sync-url", "https://google.com");
2985 // Set up our form data.
2986 FormData form;
2987 CreateTestCreditCardFormData(&form, true, false);
2988 std::vector<FormData> forms(1, form);
2989 FormsSeen(forms);
2991 CreditCard card(CreditCard::MASKED_SERVER_CARD, "a123");
2992 test::SetCreditCardInfo(&card, "John Dillinger", "1881" /* Visa */, "01",
2993 "2017");
2994 card.SetTypeForMaskedCard(kVisaCard);
2996 EXPECT_CALL(autofill_client_, ConfirmSaveCreditCard(_)).Times(0);
2997 #if defined(OS_IOS)
2998 EXPECT_CALL(*autofill_driver_, SendFormDataToRenderer(_, _, _)).Times(4);
2999 #else
3000 EXPECT_CALL(*autofill_driver_, SendFormDataToRenderer(_, _, _));
3001 #endif // defined(OS_IOS)
3002 autofill_manager_->FillOrPreviewCreditCardForm(
3003 AutofillDriver::FORM_DATA_ACTION_FILL, kDefaultPageID, form,
3004 form.fields[0], card);
3006 #if defined(OS_IOS)
3007 // Filling out the entire form on iOS requires requesting autofill on each of
3008 // the form fields.
3009 autofill_manager_->FillOrPreviewCreditCardForm(
3010 AutofillDriver::FORM_DATA_ACTION_FILL, kDefaultPageID, form,
3011 form.fields[1], card);
3012 autofill_manager_->FillOrPreviewCreditCardForm(
3013 AutofillDriver::FORM_DATA_ACTION_FILL, kDefaultPageID, form,
3014 form.fields[2], card);
3015 autofill_manager_->FillOrPreviewCreditCardForm(
3016 AutofillDriver::FORM_DATA_ACTION_FILL, kDefaultPageID, form,
3017 form.fields[3], card);
3018 #endif // defined(OS_IOS)
3020 // Manually fill out |form| so we can use it in OnFormSubmitted.
3021 for (size_t i = 0; i < form.fields.size(); ++i) {
3022 if (form.fields[i].name == ASCIIToUTF16("cardnumber"))
3023 form.fields[i].value = ASCIIToUTF16("4012888888881881");
3024 else if (form.fields[i].name == ASCIIToUTF16("nameoncard"))
3025 form.fields[i].value = ASCIIToUTF16("John H Dillinger");
3026 else if (form.fields[i].name == ASCIIToUTF16("ccmonth"))
3027 form.fields[i].value = ASCIIToUTF16("01");
3028 else if (form.fields[i].name == ASCIIToUTF16("ccyear"))
3029 form.fields[i].value = ASCIIToUTF16("2017");
3032 AutofillManager::UnmaskResponse response;
3033 response.should_store_pan = false;
3034 response.cvc = ASCIIToUTF16("123");
3035 autofill_manager_->OnUnmaskResponse(response);
3036 autofill_manager_->OnDidGetRealPan(AutofillClient::SUCCESS,
3037 "4012888888881881");
3038 autofill_manager_->OnFormSubmitted(form);
3040 // The rest of this test is a regression test for http://crbug.com/483602.
3041 // The goal is not to crash.
3042 EXPECT_CALL(*autofill_driver_, SendFormDataToRenderer(_, _, _));
3043 for (size_t i = 0; i < form.fields.size(); ++i) {
3044 form.fields[i].value.clear();
3046 autofill_manager_->FillOrPreviewCreditCardForm(
3047 AutofillDriver::FORM_DATA_ACTION_FILL, kDefaultPageID, form,
3048 form.fields[1], card);
3049 autofill_manager_->OnUnmaskResponse(response);
3050 autofill_manager_->OnDidGetRealPan(AutofillClient::SUCCESS,
3051 "4012888888881881");
3053 form = FormData();
3054 test::CreateTestAddressFormData(&form);
3055 forms[0] = form;
3056 FormsSeen(forms);
3057 for (size_t i = 0; i < form.fields.size(); ++i) {
3058 if (form.fields[i].name == ASCIIToUTF16("firstname"))
3059 form.fields[i].value = ASCIIToUTF16("Flo");
3060 else if (form.fields[i].name == ASCIIToUTF16("lastname"))
3061 form.fields[i].value = ASCIIToUTF16("Master");
3062 else if (form.fields[i].name == ASCIIToUTF16("addr1"))
3063 form.fields[i].value = ASCIIToUTF16("123 Maple");
3064 else if (form.fields[i].name == ASCIIToUTF16("city"))
3065 form.fields[i].value = ASCIIToUTF16("Dallas");
3066 else if (form.fields[i].name == ASCIIToUTF16("state"))
3067 form.fields[i].value = ASCIIToUTF16("Texas");
3068 else if (form.fields[i].name == ASCIIToUTF16("zipcode"))
3069 form.fields[i].value = ASCIIToUTF16("77401");
3070 else if (form.fields[i].name == ASCIIToUTF16("country"))
3071 form.fields[i].value = ASCIIToUTF16("US");
3073 autofill_manager_->OnFormSubmitted(form);
3076 } // namespace autofill