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 "components/autofill/core/browser/autofill_test_utils.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/prefs/pref_service_factory.h"
10 #include "base/prefs/testing_pref_store.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "components/autofill/core/browser/autofill_manager.h"
13 #include "components/autofill/core/browser/autofill_profile.h"
14 #include "components/autofill/core/browser/credit_card.h"
15 #include "components/autofill/core/browser/field_types.h"
16 #include "components/autofill/core/common/autofill_pref_names.h"
17 #include "components/autofill/core/common/form_data.h"
18 #include "components/autofill/core/common/form_field_data.h"
19 #include "components/os_crypt/os_crypt.h"
20 #include "components/pref_registry/pref_registry_syncable.h"
22 using base::ASCIIToUTF16;
29 const char kSettingsOrigin[] = "Chrome settings";
33 scoped_ptr<PrefService> PrefServiceForTesting() {
34 scoped_refptr<user_prefs::PrefRegistrySyncable> registry(
35 new user_prefs::PrefRegistrySyncable());
36 AutofillManager::RegisterProfilePrefs(registry.get());
37 base::PrefServiceFactory factory;
38 factory.set_user_prefs(make_scoped_refptr(new TestingPrefStore()));
39 return factory.Create(registry.get());
42 void CreateTestFormField(const char* label,
46 FormFieldData* field) {
47 field->label = ASCIIToUTF16(label);
48 field->name = ASCIIToUTF16(name);
49 field->value = ASCIIToUTF16(value);
50 field->form_control_type = type;
53 void CreateTestAddressFormData(FormData* form) {
54 form->name = ASCIIToUTF16("MyForm");
55 form->origin = GURL("http://myform.com/form.html");
56 form->action = GURL("http://myform.com/submit.html");
57 form->user_submitted = true;
60 test::CreateTestFormField("First Name", "firstname", "", "text", &field);
61 form->fields.push_back(field);
62 test::CreateTestFormField("Middle Name", "middlename", "", "text", &field);
63 form->fields.push_back(field);
64 test::CreateTestFormField("Last Name", "lastname", "", "text", &field);
65 form->fields.push_back(field);
66 test::CreateTestFormField("Address Line 1", "addr1", "", "text", &field);
67 form->fields.push_back(field);
68 test::CreateTestFormField("Address Line 2", "addr2", "", "text", &field);
69 form->fields.push_back(field);
70 test::CreateTestFormField("City", "city", "", "text", &field);
71 form->fields.push_back(field);
72 test::CreateTestFormField("State", "state", "", "text", &field);
73 form->fields.push_back(field);
74 test::CreateTestFormField("Postal Code", "zipcode", "", "text", &field);
75 form->fields.push_back(field);
76 test::CreateTestFormField("Country", "country", "", "text", &field);
77 form->fields.push_back(field);
78 test::CreateTestFormField("Phone Number", "phonenumber", "", "tel", &field);
79 form->fields.push_back(field);
80 test::CreateTestFormField("Email", "email", "", "email", &field);
81 form->fields.push_back(field);
84 inline void check_and_set(
85 FormGroup* profile, ServerFieldType type, const char* value) {
87 profile->SetRawInfo(type, base::UTF8ToUTF16(value));
90 AutofillProfile GetFullProfile() {
91 AutofillProfile profile(base::GenerateGUID(), "http://www.example.com/");
92 SetProfileInfo(&profile,
107 AutofillProfile GetFullProfile2() {
108 AutofillProfile profile(base::GenerateGUID(), "https://www.example.com/");
109 SetProfileInfo(&profile,
113 "jsmith@example.com",
124 AutofillProfile GetVerifiedProfile() {
125 AutofillProfile profile(GetFullProfile());
126 profile.set_origin(kSettingsOrigin);
130 AutofillProfile GetVerifiedProfile2() {
131 AutofillProfile profile(GetFullProfile2());
132 profile.set_origin(kSettingsOrigin);
136 CreditCard GetCreditCard() {
137 CreditCard credit_card(base::GenerateGUID(), "http://www.example.com");
139 &credit_card, "Test User", "4111111111111111" /* Visa */, "11", "2017");
143 CreditCard GetCreditCard2() {
144 CreditCard credit_card(base::GenerateGUID(), "https://www.example.com");
146 &credit_card, "Someone Else", "378282246310005" /* AmEx */, "07", "2019");
150 CreditCard GetVerifiedCreditCard() {
151 CreditCard credit_card(GetCreditCard());
152 credit_card.set_origin(kSettingsOrigin);
156 CreditCard GetVerifiedCreditCard2() {
157 CreditCard credit_card(GetCreditCard2());
158 credit_card.set_origin(kSettingsOrigin);
162 void SetProfileInfo(AutofillProfile* profile,
163 const char* first_name, const char* middle_name,
164 const char* last_name, const char* email, const char* company,
165 const char* address1, const char* address2, const char* city,
166 const char* state, const char* zipcode, const char* country,
168 check_and_set(profile, NAME_FIRST, first_name);
169 check_and_set(profile, NAME_MIDDLE, middle_name);
170 check_and_set(profile, NAME_LAST, last_name);
171 check_and_set(profile, EMAIL_ADDRESS, email);
172 check_and_set(profile, COMPANY_NAME, company);
173 check_and_set(profile, ADDRESS_HOME_LINE1, address1);
174 check_and_set(profile, ADDRESS_HOME_LINE2, address2);
175 check_and_set(profile, ADDRESS_HOME_CITY, city);
176 check_and_set(profile, ADDRESS_HOME_STATE, state);
177 check_and_set(profile, ADDRESS_HOME_ZIP, zipcode);
178 check_and_set(profile, ADDRESS_HOME_COUNTRY, country);
179 check_and_set(profile, PHONE_HOME_WHOLE_NUMBER, phone);
182 void SetProfileInfoWithGuid(AutofillProfile* profile,
183 const char* guid, const char* first_name, const char* middle_name,
184 const char* last_name, const char* email, const char* company,
185 const char* address1, const char* address2, const char* city,
186 const char* state, const char* zipcode, const char* country,
189 profile->set_guid(guid);
190 SetProfileInfo(profile, first_name, middle_name, last_name, email,
191 company, address1, address2, city, state, zipcode, country,
195 void SetCreditCardInfo(CreditCard* credit_card,
196 const char* name_on_card, const char* card_number,
197 const char* expiration_month, const char* expiration_year) {
198 check_and_set(credit_card, CREDIT_CARD_NAME, name_on_card);
199 check_and_set(credit_card, CREDIT_CARD_NUMBER, card_number);
200 check_and_set(credit_card, CREDIT_CARD_EXP_MONTH, expiration_month);
201 check_and_set(credit_card, CREDIT_CARD_EXP_4_DIGIT_YEAR, expiration_year);
204 void DisableSystemServices(PrefService* prefs) {
205 // Use a mock Keychain rather than the OS one to store credit card data.
206 #if defined(OS_MACOSX)
207 OSCrypt::UseMockKeychain(true);
208 #endif // defined(OS_MACOSX)
210 #if defined(OS_MACOSX) && !defined(OS_IOS)
211 // Don't use the Address Book on Mac, as it reaches out to system services.
213 prefs->SetBoolean(prefs::kAutofillUseMacAddressBook, false);
215 // Disable auxiliary profiles for unit testing by default.
217 prefs->SetBoolean(prefs::kAutofillAuxiliaryProfilesEnabled, false);
218 #endif // defined(OS_MACOSX) && !defined(OS_IOS)
222 } // namespace autofill