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_common_test.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "components/autofill/core/browser/autofill_profile.h"
11 #include "components/autofill/core/browser/credit_card.h"
12 #include "components/autofill/core/browser/field_types.h"
13 #include "components/autofill/core/common/autofill_pref_names.h"
14 #include "components/autofill/core/common/form_data.h"
15 #include "components/autofill/core/common/form_field_data.h"
16 #include "components/user_prefs/user_prefs.h"
17 #include "components/webdata/encryptor/encryptor.h"
18 #include "content/public/browser/browser_context.h"
25 const char kSettingsOrigin[] = "Chrome settings";
29 void CreateTestFormField(const char* label,
33 FormFieldData* field) {
34 field->label = ASCIIToUTF16(label);
35 field->name = ASCIIToUTF16(name);
36 field->value = ASCIIToUTF16(value);
37 field->form_control_type = type;
40 void CreateTestAddressFormData(FormData* form) {
41 form->name = ASCIIToUTF16("MyForm");
42 form->method = ASCIIToUTF16("POST");
43 form->origin = GURL("http://myform.com/form.html");
44 form->action = GURL("http://myform.com/submit.html");
45 form->user_submitted = true;
48 test::CreateTestFormField("First Name", "firstname", "", "text", &field);
49 form->fields.push_back(field);
50 test::CreateTestFormField("Middle Name", "middlename", "", "text", &field);
51 form->fields.push_back(field);
52 test::CreateTestFormField("Last Name", "lastname", "", "text", &field);
53 form->fields.push_back(field);
54 test::CreateTestFormField("Address Line 1", "addr1", "", "text", &field);
55 form->fields.push_back(field);
56 test::CreateTestFormField("Address Line 2", "addr2", "", "text", &field);
57 form->fields.push_back(field);
58 test::CreateTestFormField("City", "city", "", "text", &field);
59 form->fields.push_back(field);
60 test::CreateTestFormField("State", "state", "", "text", &field);
61 form->fields.push_back(field);
62 test::CreateTestFormField("Postal Code", "zipcode", "", "text", &field);
63 form->fields.push_back(field);
64 test::CreateTestFormField("Country", "country", "", "text", &field);
65 form->fields.push_back(field);
66 test::CreateTestFormField("Phone Number", "phonenumber", "", "tel", &field);
67 form->fields.push_back(field);
68 test::CreateTestFormField("Email", "email", "", "email", &field);
69 form->fields.push_back(field);
72 inline void check_and_set(
73 FormGroup* profile, ServerFieldType type, const char* value) {
75 profile->SetRawInfo(type, UTF8ToUTF16(value));
78 AutofillProfile GetFullProfile() {
79 AutofillProfile profile(base::GenerateGUID(), "http://www.example.com/");
80 SetProfileInfo(&profile,
95 AutofillProfile GetFullProfile2() {
96 AutofillProfile profile(base::GenerateGUID(), "https://www.example.com/");
97 SetProfileInfo(&profile,
101 "jsmith@example.com",
112 AutofillProfile GetVerifiedProfile() {
113 AutofillProfile profile(GetFullProfile());
114 profile.set_origin(kSettingsOrigin);
118 AutofillProfile GetVerifiedProfile2() {
119 AutofillProfile profile(GetFullProfile2());
120 profile.set_origin(kSettingsOrigin);
124 CreditCard GetCreditCard() {
125 CreditCard credit_card(base::GenerateGUID(), "http://www.example.com");
127 &credit_card, "Test User", "4111111111111111" /* Visa */, "11", "2017");
131 CreditCard GetCreditCard2() {
132 CreditCard credit_card(base::GenerateGUID(), "https://www.example.com");
134 &credit_card, "Someone Else", "378282246310005" /* AmEx */, "07", "2019");
138 CreditCard GetVerifiedCreditCard() {
139 CreditCard credit_card(GetCreditCard());
140 credit_card.set_origin(kSettingsOrigin);
144 CreditCard GetVerifiedCreditCard2() {
145 CreditCard credit_card(GetCreditCard2());
146 credit_card.set_origin(kSettingsOrigin);
150 void SetProfileInfo(AutofillProfile* profile,
151 const char* first_name, const char* middle_name,
152 const char* last_name, const char* email, const char* company,
153 const char* address1, const char* address2, const char* city,
154 const char* state, const char* zipcode, const char* country,
156 check_and_set(profile, NAME_FIRST, first_name);
157 check_and_set(profile, NAME_MIDDLE, middle_name);
158 check_and_set(profile, NAME_LAST, last_name);
159 check_and_set(profile, EMAIL_ADDRESS, email);
160 check_and_set(profile, COMPANY_NAME, company);
161 check_and_set(profile, ADDRESS_HOME_LINE1, address1);
162 check_and_set(profile, ADDRESS_HOME_LINE2, address2);
163 check_and_set(profile, ADDRESS_HOME_CITY, city);
164 check_and_set(profile, ADDRESS_HOME_STATE, state);
165 check_and_set(profile, ADDRESS_HOME_ZIP, zipcode);
166 check_and_set(profile, ADDRESS_HOME_COUNTRY, country);
167 check_and_set(profile, PHONE_HOME_WHOLE_NUMBER, phone);
170 void SetProfileInfoWithGuid(AutofillProfile* profile,
171 const char* guid, const char* first_name, const char* middle_name,
172 const char* last_name, const char* email, const char* company,
173 const char* address1, const char* address2, const char* city,
174 const char* state, const char* zipcode, const char* country,
177 profile->set_guid(guid);
178 SetProfileInfo(profile, first_name, middle_name, last_name, email,
179 company, address1, address2, city, state, zipcode, country,
183 void SetCreditCardInfo(CreditCard* credit_card,
184 const char* name_on_card, const char* card_number,
185 const char* expiration_month, const char* expiration_year) {
186 check_and_set(credit_card, CREDIT_CARD_NAME, name_on_card);
187 check_and_set(credit_card, CREDIT_CARD_NUMBER, card_number);
188 check_and_set(credit_card, CREDIT_CARD_EXP_MONTH, expiration_month);
189 check_and_set(credit_card, CREDIT_CARD_EXP_4_DIGIT_YEAR, expiration_year);
192 void DisableSystemServices(content::BrowserContext* browser_context) {
193 // Use a mock Keychain rather than the OS one to store credit card data.
194 #if defined(OS_MACOSX)
195 Encryptor::UseMockKeychain(true);
198 // Disable auxiliary profiles for unit testing. These reach out to system
199 // services on the Mac.
200 if (browser_context) {
201 user_prefs::UserPrefs::Get(browser_context)->SetBoolean(
202 prefs::kAutofillAuxiliaryProfilesEnabled, false);
207 } // namespace autofill