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->method = ASCIIToUTF16("POST"); 56 form->origin = GURL("http://myform.com/form.html"); 57 form->action = GURL("http://myform.com/submit.html"); 58 form->user_submitted = true; 59 60 FormFieldData field; 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); 83} 84 85inline void check_and_set( 86 FormGroup* profile, ServerFieldType type, const char* value) { 87 if (value) 88 profile->SetRawInfo(type, base::UTF8ToUTF16(value)); 89} 90 91AutofillProfile GetFullProfile() { 92 AutofillProfile profile(base::GenerateGUID(), "http://www.example.com/"); 93 SetProfileInfo(&profile, 94 "John", 95 "H.", 96 "Doe", 97 "johndoe@hades.com", 98 "Underworld", 99 "666 Erebus St.", 100 "Apt 8", 101 "Elysium", "CA", 102 "91111", 103 "US", 104 "16502111111"); 105 return profile; 106} 107 108AutofillProfile GetFullProfile2() { 109 AutofillProfile profile(base::GenerateGUID(), "https://www.example.com/"); 110 SetProfileInfo(&profile, 111 "Jane", 112 "A.", 113 "Smith", 114 "jsmith@example.com", 115 "ACME", 116 "123 Main Street", 117 "Unit 1", 118 "Greensdale", "MI", 119 "48838", 120 "US", 121 "13105557889"); 122 return profile; 123} 124 125AutofillProfile GetVerifiedProfile() { 126 AutofillProfile profile(GetFullProfile()); 127 profile.set_origin(kSettingsOrigin); 128 return profile; 129} 130 131AutofillProfile GetVerifiedProfile2() { 132 AutofillProfile profile(GetFullProfile2()); 133 profile.set_origin(kSettingsOrigin); 134 return profile; 135} 136 137CreditCard GetCreditCard() { 138 CreditCard credit_card(base::GenerateGUID(), "http://www.example.com"); 139 SetCreditCardInfo( 140 &credit_card, "Test User", "4111111111111111" /* Visa */, "11", "2017"); 141 return credit_card; 142} 143 144CreditCard GetCreditCard2() { 145 CreditCard credit_card(base::GenerateGUID(), "https://www.example.com"); 146 SetCreditCardInfo( 147 &credit_card, "Someone Else", "378282246310005" /* AmEx */, "07", "2019"); 148 return credit_card; 149} 150 151CreditCard GetVerifiedCreditCard() { 152 CreditCard credit_card(GetCreditCard()); 153 credit_card.set_origin(kSettingsOrigin); 154 return credit_card; 155} 156 157CreditCard GetVerifiedCreditCard2() { 158 CreditCard credit_card(GetCreditCard2()); 159 credit_card.set_origin(kSettingsOrigin); 160 return credit_card; 161} 162 163void 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, 168 const char* phone) { 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); 181} 182 183void 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, 188 const char* phone) { 189 if (guid) 190 profile->set_guid(guid); 191 SetProfileInfo(profile, first_name, middle_name, last_name, email, 192 company, address1, address2, city, state, zipcode, country, 193 phone); 194} 195 196void 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); 203} 204 205void 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) 210 211#if defined(OS_MACOSX) && !defined(OS_IOS) 212 // Don't use the Address Book on Mac, as it reaches out to system services. 213 if (prefs) 214 prefs->SetBoolean(prefs::kAutofillUseMacAddressBook, false); 215#else 216 // Disable auxiliary profiles for unit testing by default. 217 if (prefs) 218 prefs->SetBoolean(prefs::kAutofillAuxiliaryProfilesEnabled, false); 219#endif // defined(OS_MACOSX) && !defined(OS_IOS) 220} 221 222} // namespace test 223} // namespace autofill 224