autofill_test_utils.cc revision 116680a4aac90f2aa7413d9095a592090648e557
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.
4
5#include "components/autofill/core/browser/autofill_test_utils.h"
6
7#include "base/guid.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"
21
22using base::ASCIIToUTF16;
23
24namespace autofill {
25namespace test {
26
27namespace {
28
29const char kSettingsOrigin[] = "Chrome settings";
30
31}  // namespace
32
33scoped_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());
40}
41
42void CreateTestFormField(const char* label,
43                         const char* name,
44                         const char* value,
45                         const char* type,
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}
52
53void 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;
58
59  FormFieldData field;
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);
82}
83
84inline void check_and_set(
85    FormGroup* profile, ServerFieldType type, const char* value) {
86  if (value)
87    profile->SetRawInfo(type, base::UTF8ToUTF16(value));
88}
89
90AutofillProfile GetFullProfile() {
91  AutofillProfile profile(base::GenerateGUID(), "http://www.example.com/");
92  SetProfileInfo(&profile,
93                 "John",
94                 "H.",
95                 "Doe",
96                 "johndoe@hades.com",
97                 "Underworld",
98                 "666 Erebus St.",
99                 "Apt 8",
100                 "Elysium", "CA",
101                 "91111",
102                 "US",
103                 "16502111111");
104  return profile;
105}
106
107AutofillProfile GetFullProfile2() {
108  AutofillProfile profile(base::GenerateGUID(), "https://www.example.com/");
109  SetProfileInfo(&profile,
110                 "Jane",
111                 "A.",
112                 "Smith",
113                 "jsmith@example.com",
114                 "ACME",
115                 "123 Main Street",
116                 "Unit 1",
117                 "Greensdale", "MI",
118                 "48838",
119                 "US",
120                 "13105557889");
121  return profile;
122}
123
124AutofillProfile GetVerifiedProfile() {
125  AutofillProfile profile(GetFullProfile());
126  profile.set_origin(kSettingsOrigin);
127  return profile;
128}
129
130AutofillProfile GetVerifiedProfile2() {
131  AutofillProfile profile(GetFullProfile2());
132  profile.set_origin(kSettingsOrigin);
133  return profile;
134}
135
136CreditCard GetCreditCard() {
137  CreditCard credit_card(base::GenerateGUID(), "http://www.example.com");
138  SetCreditCardInfo(
139      &credit_card, "Test User", "4111111111111111" /* Visa */, "11", "2017");
140  return credit_card;
141}
142
143CreditCard GetCreditCard2() {
144  CreditCard credit_card(base::GenerateGUID(), "https://www.example.com");
145  SetCreditCardInfo(
146      &credit_card, "Someone Else", "378282246310005" /* AmEx */, "07", "2019");
147  return credit_card;
148}
149
150CreditCard GetVerifiedCreditCard() {
151  CreditCard credit_card(GetCreditCard());
152  credit_card.set_origin(kSettingsOrigin);
153  return credit_card;
154}
155
156CreditCard GetVerifiedCreditCard2() {
157  CreditCard credit_card(GetCreditCard2());
158  credit_card.set_origin(kSettingsOrigin);
159  return credit_card;
160}
161
162void 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,
167    const char* phone) {
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);
180}
181
182void 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,
187    const char* phone) {
188  if (guid)
189    profile->set_guid(guid);
190  SetProfileInfo(profile, first_name, middle_name, last_name, email,
191                 company, address1, address2, city, state, zipcode, country,
192                 phone);
193}
194
195void 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);
202}
203
204void 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)
209
210#if defined(OS_MACOSX) && !defined(OS_IOS)
211  // Don't use the Address Book on Mac, as it reaches out to system services.
212  if (prefs)
213    prefs->SetBoolean(prefs::kAutofillUseMacAddressBook, false);
214#else
215  // Disable auxiliary profiles for unit testing by default.
216  if (prefs)
217    prefs->SetBoolean(prefs::kAutofillAuxiliaryProfilesEnabled, false);
218#endif  // defined(OS_MACOSX) && !defined(OS_IOS)
219}
220
221}  // namespace test
222}  // namespace autofill
223