Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / components / autofill / core / browser / autofill_test_utils.cc
blob2035481fbbe2ba401ea33c09f7f4590c1c16db30
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"
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/browser/webdata/autofill_table.h"
17 #include "components/autofill/core/common/autofill_pref_names.h"
18 #include "components/autofill/core/common/form_data.h"
19 #include "components/autofill/core/common/form_field_data.h"
20 #include "components/os_crypt/os_crypt.h"
21 #include "components/pref_registry/pref_registry_syncable.h"
22 #include "components/signin/core/browser/account_fetcher_service.h"
23 #include "components/signin/core/browser/account_tracker_service.h"
24 #include "components/signin/core/common/signin_pref_names.h"
26 using base::ASCIIToUTF16;
28 namespace autofill {
29 namespace test {
31 namespace {
33 const char kSettingsOrigin[] = "Chrome settings";
35 } // namespace
37 scoped_ptr<PrefService> PrefServiceForTesting() {
38 scoped_refptr<user_prefs::PrefRegistrySyncable> registry(
39 new user_prefs::PrefRegistrySyncable());
40 AutofillManager::RegisterProfilePrefs(registry.get());
42 // PDM depends on this pref, which is normally registered in
43 // SigninManagerFactory.
44 registry->RegisterStringPref(::prefs::kGoogleServicesAccountId,
45 std::string());
47 // PDM depends on these prefs, which are normally registered in
48 // AccountTrackerServiceFactory.
49 registry->RegisterListPref(AccountTrackerService::kAccountInfoPref);
50 registry->RegisterIntegerPref(::prefs::kAccountIdMigrationState,
51 AccountTrackerService::MIGRATION_NOT_STARTED);
52 registry->RegisterInt64Pref(AccountFetcherService::kLastUpdatePref, 0);
54 base::PrefServiceFactory factory;
55 factory.set_user_prefs(make_scoped_refptr(new TestingPrefStore()));
56 return factory.Create(registry.get());
59 void CreateTestFormField(const char* label,
60 const char* name,
61 const char* value,
62 const char* type,
63 FormFieldData* field) {
64 field->label = ASCIIToUTF16(label);
65 field->name = ASCIIToUTF16(name);
66 field->value = ASCIIToUTF16(value);
67 field->form_control_type = type;
68 field->is_focusable = true;
71 void CreateTestAddressFormData(FormData* form) {
72 std::vector<ServerFieldTypeSet> types;
73 CreateTestAddressFormData(form, &types);
76 void CreateTestAddressFormData(FormData* form,
77 std::vector<ServerFieldTypeSet>* types) {
78 form->name = ASCIIToUTF16("MyForm");
79 form->origin = GURL("http://myform.com/form.html");
80 form->action = GURL("http://myform.com/submit.html");
81 types->clear();
83 FormFieldData field;
84 ServerFieldTypeSet type_set;
85 test::CreateTestFormField("First Name", "firstname", "", "text", &field);
86 form->fields.push_back(field);
87 type_set.clear();
88 type_set.insert(NAME_FIRST);
89 types->push_back(type_set);
90 test::CreateTestFormField("Middle Name", "middlename", "", "text", &field);
91 form->fields.push_back(field);
92 type_set.clear();
93 type_set.insert(NAME_MIDDLE);
94 types->push_back(type_set);
95 test::CreateTestFormField("Last Name", "lastname", "", "text", &field);
96 form->fields.push_back(field);
97 type_set.clear();
98 type_set.insert(NAME_LAST);
99 types->push_back(type_set);
100 test::CreateTestFormField("Address Line 1", "addr1", "", "text", &field);
101 form->fields.push_back(field);
102 type_set.clear();
103 type_set.insert(ADDRESS_HOME_LINE1);
104 types->push_back(type_set);
105 test::CreateTestFormField("Address Line 2", "addr2", "", "text", &field);
106 form->fields.push_back(field);
107 type_set.clear();
108 type_set.insert(ADDRESS_HOME_LINE2);
109 types->push_back(type_set);
110 test::CreateTestFormField("City", "city", "", "text", &field);
111 form->fields.push_back(field);
112 type_set.clear();
113 type_set.insert(ADDRESS_HOME_CITY);
114 types->push_back(type_set);
115 test::CreateTestFormField("State", "state", "", "text", &field);
116 form->fields.push_back(field);
117 type_set.clear();
118 type_set.insert(ADDRESS_HOME_STATE);
119 types->push_back(type_set);
120 test::CreateTestFormField("Postal Code", "zipcode", "", "text", &field);
121 form->fields.push_back(field);
122 type_set.clear();
123 type_set.insert(ADDRESS_HOME_ZIP);
124 types->push_back(type_set);
125 test::CreateTestFormField("Country", "country", "", "text", &field);
126 form->fields.push_back(field);
127 type_set.clear();
128 type_set.insert(ADDRESS_HOME_COUNTRY);
129 types->push_back(type_set);
130 test::CreateTestFormField("Phone Number", "phonenumber", "", "tel", &field);
131 form->fields.push_back(field);
132 type_set.clear();
133 type_set.insert(PHONE_HOME_WHOLE_NUMBER);
134 types->push_back(type_set);
135 test::CreateTestFormField("Email", "email", "", "email", &field);
136 form->fields.push_back(field);
137 type_set.clear();
138 type_set.insert(EMAIL_ADDRESS);
139 types->push_back(type_set);
142 inline void check_and_set(
143 FormGroup* profile, ServerFieldType type, const char* value) {
144 if (value)
145 profile->SetRawInfo(type, base::UTF8ToUTF16(value));
148 AutofillProfile GetFullProfile() {
149 AutofillProfile profile(base::GenerateGUID(), "http://www.example.com/");
150 SetProfileInfo(&profile,
151 "John",
152 "H.",
153 "Doe",
154 "johndoe@hades.com",
155 "Underworld",
156 "666 Erebus St.",
157 "Apt 8",
158 "Elysium", "CA",
159 "91111",
160 "US",
161 "16502111111");
162 return profile;
165 AutofillProfile GetFullProfile2() {
166 AutofillProfile profile(base::GenerateGUID(), "https://www.example.com/");
167 SetProfileInfo(&profile,
168 "Jane",
169 "A.",
170 "Smith",
171 "jsmith@example.com",
172 "ACME",
173 "123 Main Street",
174 "Unit 1",
175 "Greensdale", "MI",
176 "48838",
177 "US",
178 "13105557889");
179 return profile;
182 AutofillProfile GetVerifiedProfile() {
183 AutofillProfile profile(GetFullProfile());
184 profile.set_origin(kSettingsOrigin);
185 return profile;
188 AutofillProfile GetVerifiedProfile2() {
189 AutofillProfile profile(GetFullProfile2());
190 profile.set_origin(kSettingsOrigin);
191 return profile;
194 CreditCard GetCreditCard() {
195 CreditCard credit_card(base::GenerateGUID(), "http://www.example.com");
196 SetCreditCardInfo(
197 &credit_card, "Test User", "4111111111111111" /* Visa */, "11", "2017");
198 return credit_card;
201 CreditCard GetCreditCard2() {
202 CreditCard credit_card(base::GenerateGUID(), "https://www.example.com");
203 SetCreditCardInfo(
204 &credit_card, "Someone Else", "378282246310005" /* AmEx */, "07", "2019");
205 return credit_card;
208 CreditCard GetVerifiedCreditCard() {
209 CreditCard credit_card(GetCreditCard());
210 credit_card.set_origin(kSettingsOrigin);
211 return credit_card;
214 CreditCard GetVerifiedCreditCard2() {
215 CreditCard credit_card(GetCreditCard2());
216 credit_card.set_origin(kSettingsOrigin);
217 return credit_card;
220 CreditCard GetMaskedServerCard() {
221 CreditCard credit_card(CreditCard::MASKED_SERVER_CARD, "a123");
222 test::SetCreditCardInfo(&credit_card, "Bonnie Parker",
223 "2109" /* Mastercard */, "12", "2012");
224 credit_card.SetTypeForMaskedCard(kMasterCard);
225 return credit_card;
228 CreditCard GetMaskedServerCardAmex() {
229 CreditCard credit_card(CreditCard::MASKED_SERVER_CARD, "b456");
230 test::SetCreditCardInfo(&credit_card, "Justin Thyme",
231 "8431" /* Amex */, "9", "2020");
232 credit_card.SetTypeForMaskedCard(kAmericanExpressCard);
233 return credit_card;
236 void SetProfileInfo(AutofillProfile* profile,
237 const char* first_name, const char* middle_name,
238 const char* last_name, const char* email, const char* company,
239 const char* address1, const char* address2, const char* city,
240 const char* state, const char* zipcode, const char* country,
241 const char* phone) {
242 check_and_set(profile, NAME_FIRST, first_name);
243 check_and_set(profile, NAME_MIDDLE, middle_name);
244 check_and_set(profile, NAME_LAST, last_name);
245 check_and_set(profile, EMAIL_ADDRESS, email);
246 check_and_set(profile, COMPANY_NAME, company);
247 check_and_set(profile, ADDRESS_HOME_LINE1, address1);
248 check_and_set(profile, ADDRESS_HOME_LINE2, address2);
249 check_and_set(profile, ADDRESS_HOME_CITY, city);
250 check_and_set(profile, ADDRESS_HOME_STATE, state);
251 check_and_set(profile, ADDRESS_HOME_ZIP, zipcode);
252 check_and_set(profile, ADDRESS_HOME_COUNTRY, country);
253 check_and_set(profile, PHONE_HOME_WHOLE_NUMBER, phone);
256 void SetProfileInfoWithGuid(AutofillProfile* profile,
257 const char* guid, const char* first_name, const char* middle_name,
258 const char* last_name, const char* email, const char* company,
259 const char* address1, const char* address2, const char* city,
260 const char* state, const char* zipcode, const char* country,
261 const char* phone) {
262 if (guid)
263 profile->set_guid(guid);
264 SetProfileInfo(profile, first_name, middle_name, last_name, email,
265 company, address1, address2, city, state, zipcode, country,
266 phone);
269 void SetCreditCardInfo(CreditCard* credit_card,
270 const char* name_on_card, const char* card_number,
271 const char* expiration_month, const char* expiration_year) {
272 check_and_set(credit_card, CREDIT_CARD_NAME, name_on_card);
273 check_and_set(credit_card, CREDIT_CARD_NUMBER, card_number);
274 check_and_set(credit_card, CREDIT_CARD_EXP_MONTH, expiration_month);
275 check_and_set(credit_card, CREDIT_CARD_EXP_4_DIGIT_YEAR, expiration_year);
278 void DisableSystemServices(PrefService* prefs) {
279 // Use a mock Keychain rather than the OS one to store credit card data.
280 #if defined(OS_MACOSX)
281 OSCrypt::UseMockKeychain(true);
282 #endif // defined(OS_MACOSX)
285 void SetServerCreditCards(AutofillTable* table,
286 const std::vector<CreditCard>& cards) {
287 std::vector<CreditCard> as_masked_cards = cards;
288 for (CreditCard& card : as_masked_cards) {
289 card.set_record_type(CreditCard::MASKED_SERVER_CARD);
290 std::string type = card.type();
291 card.SetNumber(card.LastFourDigits());
292 card.SetTypeForMaskedCard(type.c_str());
294 table->SetServerCreditCards(as_masked_cards);
296 for (const CreditCard& card : cards) {
297 if (card.record_type() != CreditCard::FULL_SERVER_CARD)
298 continue;
300 table->UnmaskServerCreditCard(card, card.number());
304 } // namespace test
305 } // namespace autofill