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_common_test.h" 6 7 #include "base/guid.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" 19 20 namespace autofill { 21 namespace test { 22 23 namespace { 24 25 const char kSettingsOrigin[] = "Chrome settings"; 26 27 } // namespace 28 29 void CreateTestFormField(const char* label, 30 const char* name, 31 const char* value, 32 const char* type, 33 FormFieldData* field) { 34 field->label = ASCIIToUTF16(label); 35 field->name = ASCIIToUTF16(name); 36 field->value = ASCIIToUTF16(value); 37 field->form_control_type = type; 38 } 39 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; 46 47 FormFieldData field; 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); 70 } 71 72 inline void check_and_set( 73 FormGroup* profile, ServerFieldType type, const char* value) { 74 if (value) 75 profile->SetRawInfo(type, UTF8ToUTF16(value)); 76 } 77 78 AutofillProfile GetFullProfile() { 79 AutofillProfile profile(base::GenerateGUID(), "http://www.example.com/"); 80 SetProfileInfo(&profile, 81 "John", 82 "H.", 83 "Doe", 84 "johndoe (at) hades.com", 85 "Underworld", 86 "666 Erebus St.", 87 "Apt 8", 88 "Elysium", "CA", 89 "91111", 90 "US", 91 "16502111111"); 92 return profile; 93 } 94 95 AutofillProfile GetFullProfile2() { 96 AutofillProfile profile(base::GenerateGUID(), "https://www.example.com/"); 97 SetProfileInfo(&profile, 98 "Jane", 99 "A.", 100 "Smith", 101 "jsmith (at) example.com", 102 "ACME", 103 "123 Main Street", 104 "Unit 1", 105 "Greensdale", "MI", 106 "48838", 107 "US", 108 "13105557889"); 109 return profile; 110 } 111 112 AutofillProfile GetVerifiedProfile() { 113 AutofillProfile profile(GetFullProfile()); 114 profile.set_origin(kSettingsOrigin); 115 return profile; 116 } 117 118 AutofillProfile GetVerifiedProfile2() { 119 AutofillProfile profile(GetFullProfile2()); 120 profile.set_origin(kSettingsOrigin); 121 return profile; 122 } 123 124 CreditCard GetCreditCard() { 125 CreditCard credit_card(base::GenerateGUID(), "http://www.example.com"); 126 SetCreditCardInfo( 127 &credit_card, "Test User", "4111111111111111" /* Visa */, "11", "2017"); 128 return credit_card; 129 } 130 131 CreditCard GetVerifiedCreditCard() { 132 CreditCard credit_card(GetCreditCard()); 133 credit_card.set_origin(kSettingsOrigin); 134 return credit_card; 135 } 136 137 void SetProfileInfo(AutofillProfile* profile, 138 const char* first_name, const char* middle_name, 139 const char* last_name, const char* email, const char* company, 140 const char* address1, const char* address2, const char* city, 141 const char* state, const char* zipcode, const char* country, 142 const char* phone) { 143 check_and_set(profile, NAME_FIRST, first_name); 144 check_and_set(profile, NAME_MIDDLE, middle_name); 145 check_and_set(profile, NAME_LAST, last_name); 146 check_and_set(profile, EMAIL_ADDRESS, email); 147 check_and_set(profile, COMPANY_NAME, company); 148 check_and_set(profile, ADDRESS_HOME_LINE1, address1); 149 check_and_set(profile, ADDRESS_HOME_LINE2, address2); 150 check_and_set(profile, ADDRESS_HOME_CITY, city); 151 check_and_set(profile, ADDRESS_HOME_STATE, state); 152 check_and_set(profile, ADDRESS_HOME_ZIP, zipcode); 153 check_and_set(profile, ADDRESS_HOME_COUNTRY, country); 154 check_and_set(profile, PHONE_HOME_WHOLE_NUMBER, phone); 155 } 156 157 void SetProfileInfoWithGuid(AutofillProfile* profile, 158 const char* guid, const char* first_name, const char* middle_name, 159 const char* last_name, const char* email, const char* company, 160 const char* address1, const char* address2, const char* city, 161 const char* state, const char* zipcode, const char* country, 162 const char* phone) { 163 if (guid) 164 profile->set_guid(guid); 165 SetProfileInfo(profile, first_name, middle_name, last_name, email, 166 company, address1, address2, city, state, zipcode, country, 167 phone); 168 } 169 170 void SetCreditCardInfo(CreditCard* credit_card, 171 const char* name_on_card, const char* card_number, 172 const char* expiration_month, const char* expiration_year) { 173 check_and_set(credit_card, CREDIT_CARD_NAME, name_on_card); 174 check_and_set(credit_card, CREDIT_CARD_NUMBER, card_number); 175 check_and_set(credit_card, CREDIT_CARD_EXP_MONTH, expiration_month); 176 check_and_set(credit_card, CREDIT_CARD_EXP_4_DIGIT_YEAR, expiration_year); 177 } 178 179 void DisableSystemServices(content::BrowserContext* browser_context) { 180 // Use a mock Keychain rather than the OS one to store credit card data. 181 #if defined(OS_MACOSX) 182 Encryptor::UseMockKeychain(true); 183 #endif 184 185 // Disable auxiliary profiles for unit testing. These reach out to system 186 // services on the Mac. 187 if (browser_context) { 188 user_prefs::UserPrefs::Get(browser_context)->SetBoolean( 189 prefs::kAutofillAuxiliaryProfilesEnabled, false); 190 } 191 } 192 193 } // namespace test 194 } // namespace autofill 195