- add sources.
[platform/framework/web/crosswalk.git] / src / components / autofill / core / browser / autofill_common_test.cc
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@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@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 GetCreditCard2() {
132   CreditCard credit_card(base::GenerateGUID(), "https://www.example.com");
133   SetCreditCardInfo(
134       &credit_card, "Someone Else", "378282246310005" /* AmEx */, "07", "2019");
135   return credit_card;
136 }
137
138 CreditCard GetVerifiedCreditCard() {
139   CreditCard credit_card(GetCreditCard());
140   credit_card.set_origin(kSettingsOrigin);
141   return credit_card;
142 }
143
144 CreditCard GetVerifiedCreditCard2() {
145   CreditCard credit_card(GetCreditCard2());
146   credit_card.set_origin(kSettingsOrigin);
147   return credit_card;
148 }
149
150 void SetProfileInfo(AutofillProfile* profile,
151     const char* first_name, const char* middle_name,
152     const char* last_name, const char* email, const char* company,
153     const char* address1, const char* address2, const char* city,
154     const char* state, const char* zipcode, const char* country,
155     const char* phone) {
156   check_and_set(profile, NAME_FIRST, first_name);
157   check_and_set(profile, NAME_MIDDLE, middle_name);
158   check_and_set(profile, NAME_LAST, last_name);
159   check_and_set(profile, EMAIL_ADDRESS, email);
160   check_and_set(profile, COMPANY_NAME, company);
161   check_and_set(profile, ADDRESS_HOME_LINE1, address1);
162   check_and_set(profile, ADDRESS_HOME_LINE2, address2);
163   check_and_set(profile, ADDRESS_HOME_CITY, city);
164   check_and_set(profile, ADDRESS_HOME_STATE, state);
165   check_and_set(profile, ADDRESS_HOME_ZIP, zipcode);
166   check_and_set(profile, ADDRESS_HOME_COUNTRY, country);
167   check_and_set(profile, PHONE_HOME_WHOLE_NUMBER, phone);
168 }
169
170 void SetProfileInfoWithGuid(AutofillProfile* profile,
171     const char* guid, const char* first_name, const char* middle_name,
172     const char* last_name, const char* email, const char* company,
173     const char* address1, const char* address2, const char* city,
174     const char* state, const char* zipcode, const char* country,
175     const char* phone) {
176   if (guid)
177     profile->set_guid(guid);
178   SetProfileInfo(profile, first_name, middle_name, last_name, email,
179                  company, address1, address2, city, state, zipcode, country,
180                  phone);
181 }
182
183 void SetCreditCardInfo(CreditCard* credit_card,
184     const char* name_on_card, const char* card_number,
185     const char* expiration_month, const char* expiration_year) {
186   check_and_set(credit_card, CREDIT_CARD_NAME, name_on_card);
187   check_and_set(credit_card, CREDIT_CARD_NUMBER, card_number);
188   check_and_set(credit_card, CREDIT_CARD_EXP_MONTH, expiration_month);
189   check_and_set(credit_card, CREDIT_CARD_EXP_4_DIGIT_YEAR, expiration_year);
190 }
191
192 void DisableSystemServices(content::BrowserContext* browser_context) {
193   // Use a mock Keychain rather than the OS one to store credit card data.
194 #if defined(OS_MACOSX)
195   Encryptor::UseMockKeychain(true);
196 #endif
197
198   // Disable auxiliary profiles for unit testing.  These reach out to system
199   // services on the Mac.
200   if (browser_context) {
201     user_prefs::UserPrefs::Get(browser_context)->SetBoolean(
202         prefs::kAutofillAuxiliaryProfilesEnabled, false);
203   }
204 }
205
206 }  // namespace test
207 }  // namespace autofill