1 // Copyright (c) 2011 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 "chrome/browser/autofill/autofill_common_test.h"
6
7 #include "base/utf_string_conversions.h"
8 #include "chrome/browser/autofill/autofill_profile.h"
9 #include "chrome/browser/autofill/credit_card.h"
10 #include "chrome/browser/password_manager/encryptor.h"
11 #include "chrome/browser/prefs/pref_service.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/common/pref_names.h"
14 #include "webkit/glue/form_field.h"
15
16 namespace autofill_test {
17
CreateTestFormField(const char * label,const char * name,const char * value,const char * type,webkit_glue::FormField * field)18 void CreateTestFormField(const char* label,
19 const char* name,
20 const char* value,
21 const char* type,
22 webkit_glue::FormField* field) {
23 *field = webkit_glue::FormField(ASCIIToUTF16(label), ASCIIToUTF16(name),
24 ASCIIToUTF16(value), ASCIIToUTF16(type), 0,
25 false);
26 }
27
check_and_set(FormGroup * profile,AutofillFieldType type,const char * value)28 inline void check_and_set(
29 FormGroup* profile, AutofillFieldType type, const char* value) {
30 if (value)
31 profile->SetInfo(type, ASCIIToUTF16(value));
32 }
33
SetProfileInfo(AutofillProfile * profile,const char * first_name,const char * middle_name,const char * last_name,const char * email,const char * company,const char * address1,const char * address2,const char * city,const char * state,const char * zipcode,const char * country,const char * phone,const char * fax)34 void SetProfileInfo(AutofillProfile* profile,
35 const char* first_name, const char* middle_name,
36 const char* last_name, const char* email, const char* company,
37 const char* address1, const char* address2, const char* city,
38 const char* state, const char* zipcode, const char* country,
39 const char* phone, const char* fax) {
40 check_and_set(profile, NAME_FIRST, first_name);
41 check_and_set(profile, NAME_MIDDLE, middle_name);
42 check_and_set(profile, NAME_LAST, last_name);
43 check_and_set(profile, EMAIL_ADDRESS, email);
44 check_and_set(profile, COMPANY_NAME, company);
45 check_and_set(profile, ADDRESS_HOME_LINE1, address1);
46 check_and_set(profile, ADDRESS_HOME_LINE2, address2);
47 check_and_set(profile, ADDRESS_HOME_CITY, city);
48 check_and_set(profile, ADDRESS_HOME_STATE, state);
49 check_and_set(profile, ADDRESS_HOME_ZIP, zipcode);
50 check_and_set(profile, ADDRESS_HOME_COUNTRY, country);
51 check_and_set(profile, PHONE_HOME_WHOLE_NUMBER, phone);
52 check_and_set(profile, PHONE_FAX_WHOLE_NUMBER, fax);
53 }
54
SetProfileInfoWithGuid(AutofillProfile * profile,const char * guid,const char * first_name,const char * middle_name,const char * last_name,const char * email,const char * company,const char * address1,const char * address2,const char * city,const char * state,const char * zipcode,const char * country,const char * phone,const char * fax)55 void SetProfileInfoWithGuid(AutofillProfile* profile,
56 const char* guid, const char* first_name, const char* middle_name,
57 const char* last_name, const char* email, const char* company,
58 const char* address1, const char* address2, const char* city,
59 const char* state, const char* zipcode, const char* country,
60 const char* phone, const char* fax) {
61 if (guid)
62 profile->set_guid(guid);
63 SetProfileInfo(profile, first_name, middle_name, last_name, email,
64 company, address1, address2, city, state, zipcode, country,
65 phone, fax);
66 }
67
SetCreditCardInfo(CreditCard * credit_card,const char * name_on_card,const char * card_number,const char * expiration_month,const char * expiration_year)68 void SetCreditCardInfo(CreditCard* credit_card,
69 const char* name_on_card, const char* card_number,
70 const char* expiration_month, const char* expiration_year) {
71 check_and_set(credit_card, CREDIT_CARD_NAME, name_on_card);
72 check_and_set(credit_card, CREDIT_CARD_NUMBER, card_number);
73 check_and_set(credit_card, CREDIT_CARD_EXP_MONTH, expiration_month);
74 check_and_set(credit_card, CREDIT_CARD_EXP_4_DIGIT_YEAR, expiration_year);
75 }
76
DisableSystemServices(Profile * profile)77 void DisableSystemServices(Profile* profile) {
78 // Use a mock Keychain rather than the OS one to store credit card data.
79 #if defined(OS_MACOSX)
80 Encryptor::UseMockKeychain(true);
81 #endif
82
83 // Disable auxiliary profiles for unit testing. These reach out to system
84 // services on the Mac.
85 if (profile) {
86 profile->GetPrefs()->SetBoolean(prefs::kAutofillAuxiliaryProfilesEnabled,
87 false);
88 }
89 }
90
91 } // namespace autofill_test
92