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
;
51 field
->is_focusable
= true;
54 void CreateTestAddressFormData(FormData
* form
) {
55 form
->name
= ASCIIToUTF16("MyForm");
56 form
->origin
= GURL("http://myform.com/form.html");
57 form
->action
= GURL("http://myform.com/submit.html");
58 form
->user_submitted
= true;
61 test::CreateTestFormField("First Name", "firstname", "", "text", &field
);
62 form
->fields
.push_back(field
);
63 test::CreateTestFormField("Middle Name", "middlename", "", "text", &field
);
64 form
->fields
.push_back(field
);
65 test::CreateTestFormField("Last Name", "lastname", "", "text", &field
);
66 form
->fields
.push_back(field
);
67 test::CreateTestFormField("Address Line 1", "addr1", "", "text", &field
);
68 form
->fields
.push_back(field
);
69 test::CreateTestFormField("Address Line 2", "addr2", "", "text", &field
);
70 form
->fields
.push_back(field
);
71 test::CreateTestFormField("City", "city", "", "text", &field
);
72 form
->fields
.push_back(field
);
73 test::CreateTestFormField("State", "state", "", "text", &field
);
74 form
->fields
.push_back(field
);
75 test::CreateTestFormField("Postal Code", "zipcode", "", "text", &field
);
76 form
->fields
.push_back(field
);
77 test::CreateTestFormField("Country", "country", "", "text", &field
);
78 form
->fields
.push_back(field
);
79 test::CreateTestFormField("Phone Number", "phonenumber", "", "tel", &field
);
80 form
->fields
.push_back(field
);
81 test::CreateTestFormField("Email", "email", "", "email", &field
);
82 form
->fields
.push_back(field
);
85 inline void check_and_set(
86 FormGroup
* profile
, ServerFieldType type
, const char* value
) {
88 profile
->SetRawInfo(type
, base::UTF8ToUTF16(value
));
91 AutofillProfile
GetFullProfile() {
92 AutofillProfile
profile(base::GenerateGUID(), "http://www.example.com/");
93 SetProfileInfo(&profile
,
108 AutofillProfile
GetFullProfile2() {
109 AutofillProfile
profile(base::GenerateGUID(), "https://www.example.com/");
110 SetProfileInfo(&profile
,
114 "jsmith@example.com",
125 AutofillProfile
GetVerifiedProfile() {
126 AutofillProfile
profile(GetFullProfile());
127 profile
.set_origin(kSettingsOrigin
);
131 AutofillProfile
GetVerifiedProfile2() {
132 AutofillProfile
profile(GetFullProfile2());
133 profile
.set_origin(kSettingsOrigin
);
137 CreditCard
GetCreditCard() {
138 CreditCard
credit_card(base::GenerateGUID(), "http://www.example.com");
140 &credit_card
, "Test User", "4111111111111111" /* Visa */, "11", "2017");
144 CreditCard
GetCreditCard2() {
145 CreditCard
credit_card(base::GenerateGUID(), "https://www.example.com");
147 &credit_card
, "Someone Else", "378282246310005" /* AmEx */, "07", "2019");
151 CreditCard
GetVerifiedCreditCard() {
152 CreditCard
credit_card(GetCreditCard());
153 credit_card
.set_origin(kSettingsOrigin
);
157 CreditCard
GetVerifiedCreditCard2() {
158 CreditCard
credit_card(GetCreditCard2());
159 credit_card
.set_origin(kSettingsOrigin
);
163 void SetProfileInfo(AutofillProfile
* profile
,
164 const char* first_name
, const char* middle_name
,
165 const char* last_name
, const char* email
, const char* company
,
166 const char* address1
, const char* address2
, const char* city
,
167 const char* state
, const char* zipcode
, const char* country
,
169 check_and_set(profile
, NAME_FIRST
, first_name
);
170 check_and_set(profile
, NAME_MIDDLE
, middle_name
);
171 check_and_set(profile
, NAME_LAST
, last_name
);
172 check_and_set(profile
, EMAIL_ADDRESS
, email
);
173 check_and_set(profile
, COMPANY_NAME
, company
);
174 check_and_set(profile
, ADDRESS_HOME_LINE1
, address1
);
175 check_and_set(profile
, ADDRESS_HOME_LINE2
, address2
);
176 check_and_set(profile
, ADDRESS_HOME_CITY
, city
);
177 check_and_set(profile
, ADDRESS_HOME_STATE
, state
);
178 check_and_set(profile
, ADDRESS_HOME_ZIP
, zipcode
);
179 check_and_set(profile
, ADDRESS_HOME_COUNTRY
, country
);
180 check_and_set(profile
, PHONE_HOME_WHOLE_NUMBER
, phone
);
183 void SetProfileInfoWithGuid(AutofillProfile
* profile
,
184 const char* guid
, const char* first_name
, const char* middle_name
,
185 const char* last_name
, const char* email
, const char* company
,
186 const char* address1
, const char* address2
, const char* city
,
187 const char* state
, const char* zipcode
, const char* country
,
190 profile
->set_guid(guid
);
191 SetProfileInfo(profile
, first_name
, middle_name
, last_name
, email
,
192 company
, address1
, address2
, city
, state
, zipcode
, country
,
196 void SetCreditCardInfo(CreditCard
* credit_card
,
197 const char* name_on_card
, const char* card_number
,
198 const char* expiration_month
, const char* expiration_year
) {
199 check_and_set(credit_card
, CREDIT_CARD_NAME
, name_on_card
);
200 check_and_set(credit_card
, CREDIT_CARD_NUMBER
, card_number
);
201 check_and_set(credit_card
, CREDIT_CARD_EXP_MONTH
, expiration_month
);
202 check_and_set(credit_card
, CREDIT_CARD_EXP_4_DIGIT_YEAR
, expiration_year
);
205 void DisableSystemServices(PrefService
* prefs
) {
206 // Use a mock Keychain rather than the OS one to store credit card data.
207 #if defined(OS_MACOSX)
208 OSCrypt::UseMockKeychain(true);
209 #endif // defined(OS_MACOSX)
211 #if defined(OS_MACOSX) && !defined(OS_IOS)
212 // Don't use the Address Book on Mac, as it reaches out to system services.
214 prefs
->SetBoolean(prefs::kAutofillUseMacAddressBook
, false);
216 // Disable auxiliary profiles for unit testing by default.
218 prefs
->SetBoolean(prefs::kAutofillAuxiliaryProfilesEnabled
, false);
219 #endif // defined(OS_MACOSX) && !defined(OS_IOS)
223 } // namespace autofill