autofill_common_test.cc revision 513209b27ff55e2841eac0e4120199c23acce758
1// Copyright (c) 2010 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/profile.h"
13#include "chrome/common/pref_names.h"
14#include "webkit/glue/form_field.h"
15
16namespace autofill_test {
17
18void 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}
26
27inline void check_and_set(
28    FormGroup* profile, AutoFillFieldType type, const char* value) {
29  if (value)
30    profile->SetInfo(AutoFillType(type), ASCIIToUTF16(value));
31}
32
33void SetProfileInfo(AutoFillProfile* profile,
34    const char* label, const char* first_name, const char* middle_name,
35    const char* last_name, const char* email, const char* company,
36    const char* address1, const char* address2, const char* city,
37    const char* state, const char* zipcode, const char* country,
38    const char* phone, const char* fax) {
39  // TODO(jhawkins): Remove |label|.
40  if (label)
41    profile->set_label(ASCIIToUTF16(label));
42  check_and_set(profile, NAME_FIRST, first_name);
43  check_and_set(profile, NAME_MIDDLE, middle_name);
44  check_and_set(profile, NAME_LAST, last_name);
45  check_and_set(profile, EMAIL_ADDRESS, email);
46  check_and_set(profile, COMPANY_NAME, company);
47  check_and_set(profile, ADDRESS_HOME_LINE1, address1);
48  check_and_set(profile, ADDRESS_HOME_LINE2, address2);
49  check_and_set(profile, ADDRESS_HOME_CITY, city);
50  check_and_set(profile, ADDRESS_HOME_STATE, state);
51  check_and_set(profile, ADDRESS_HOME_ZIP, zipcode);
52  check_and_set(profile, ADDRESS_HOME_COUNTRY, country);
53  check_and_set(profile, PHONE_HOME_WHOLE_NUMBER, phone);
54  check_and_set(profile, PHONE_FAX_WHOLE_NUMBER, fax);
55}
56
57void SetCreditCardInfo(CreditCard* credit_card,
58    const char* label, const char* name_on_card, const char* card_number,
59    const char* expiration_month, const char* expiration_year) {
60  credit_card->set_label(ASCIIToUTF16(label));
61  check_and_set(credit_card, CREDIT_CARD_NAME, name_on_card);
62  check_and_set(credit_card, CREDIT_CARD_NUMBER, card_number);
63  check_and_set(credit_card, CREDIT_CARD_EXP_MONTH, expiration_month);
64  check_and_set(credit_card, CREDIT_CARD_EXP_4_DIGIT_YEAR, expiration_year);
65}
66
67void DisableSystemServices(Profile* profile) {
68  // Use a mock Keychain rather than the OS one to store credit card data.
69#if defined(OS_MACOSX)
70  Encryptor::UseMockKeychain(true);
71#endif
72
73  // Disable auxiliary profiles for unit testing.  These reach out to system
74  // services on the Mac.
75  profile->GetPrefs()->SetBoolean(prefs::kAutoFillAuxiliaryProfilesEnabled,
76                                  false);
77}
78
79}  // namespace autofill_test
80