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.
5 #include "base/memory/scoped_ptr.h"
6 #include "base/memory/scoped_vector.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "components/autofill/core/browser/autofill_field.h"
9 #include "components/autofill/core/browser/autofill_scanner.h"
10 #include "components/autofill/core/browser/name_field.h"
11 #include "components/autofill/core/common/form_field_data.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 using base::ASCIIToUTF16
;
18 class NameFieldTest
: public testing::Test
{
23 ScopedVector
<AutofillField
> list_
;
24 scoped_ptr
<NameField
> field_
;
25 ServerFieldTypeMap field_type_map_
;
27 // Downcast for tests.
28 static scoped_ptr
<NameField
> Parse(AutofillScanner
* scanner
) {
29 scoped_ptr
<FormField
> field
= NameField::Parse(scanner
);
30 return make_scoped_ptr(static_cast<NameField
*>(field
.release()));
34 DISALLOW_COPY_AND_ASSIGN(NameFieldTest
);
37 TEST_F(NameFieldTest
, FirstMiddleLast
) {
39 field
.form_control_type
= "text";
41 field
.label
= ASCIIToUTF16("First Name");
42 field
.name
= ASCIIToUTF16("First");
43 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name1")));
45 field
.label
= ASCIIToUTF16("Middle Name");
46 field
.name
= ASCIIToUTF16("Middle");
47 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name2")));
49 field
.label
= ASCIIToUTF16("Last Name");
50 field
.name
= ASCIIToUTF16("Last");
51 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name3")));
53 AutofillScanner
scanner(list_
.get());
54 field_
= Parse(&scanner
);
55 ASSERT_NE(nullptr, field_
.get());
56 ASSERT_TRUE(field_
->ClassifyField(&field_type_map_
));
58 field_type_map_
.find(ASCIIToUTF16("name1")) != field_type_map_
.end());
59 EXPECT_EQ(NAME_FIRST
, field_type_map_
[ASCIIToUTF16("name1")]);
61 field_type_map_
.find(ASCIIToUTF16("name2")) != field_type_map_
.end());
62 EXPECT_EQ(NAME_MIDDLE
, field_type_map_
[ASCIIToUTF16("name2")]);
64 field_type_map_
.find(ASCIIToUTF16("name3")) != field_type_map_
.end());
65 EXPECT_EQ(NAME_LAST
, field_type_map_
[ASCIIToUTF16("name3")]);
68 TEST_F(NameFieldTest
, FirstMiddleLast2
) {
70 field
.form_control_type
= "text";
72 field
.label
= base::string16();
73 field
.name
= ASCIIToUTF16("firstName");
74 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name1")));
76 field
.label
= base::string16();
77 field
.name
= ASCIIToUTF16("middleName");
78 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name2")));
80 field
.label
= base::string16();
81 field
.name
= ASCIIToUTF16("lastName");
82 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name3")));
84 AutofillScanner
scanner(list_
.get());
85 field_
= Parse(&scanner
);
86 ASSERT_NE(nullptr, field_
.get());
87 ASSERT_TRUE(field_
->ClassifyField(&field_type_map_
));
89 field_type_map_
.find(ASCIIToUTF16("name1")) != field_type_map_
.end());
90 EXPECT_EQ(NAME_FIRST
, field_type_map_
[ASCIIToUTF16("name1")]);
92 field_type_map_
.find(ASCIIToUTF16("name2")) != field_type_map_
.end());
93 EXPECT_EQ(NAME_MIDDLE
, field_type_map_
[ASCIIToUTF16("name2")]);
95 field_type_map_
.find(ASCIIToUTF16("name3")) != field_type_map_
.end());
96 EXPECT_EQ(NAME_LAST
, field_type_map_
[ASCIIToUTF16("name3")]);
99 TEST_F(NameFieldTest
, FirstLast
) {
101 field
.form_control_type
= "text";
103 field
.label
= base::string16();
104 field
.name
= ASCIIToUTF16("first_name");
105 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name1")));
107 field
.label
= base::string16();
108 field
.name
= ASCIIToUTF16("last_name");
109 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name2")));
111 AutofillScanner
scanner(list_
.get());
112 field_
= Parse(&scanner
);
113 ASSERT_NE(nullptr, field_
.get());
114 ASSERT_TRUE(field_
->ClassifyField(&field_type_map_
));
116 field_type_map_
.find(ASCIIToUTF16("name1")) != field_type_map_
.end());
117 EXPECT_EQ(NAME_FIRST
, field_type_map_
[ASCIIToUTF16("name1")]);
119 field_type_map_
.find(ASCIIToUTF16("name2")) != field_type_map_
.end());
120 EXPECT_EQ(NAME_LAST
, field_type_map_
[ASCIIToUTF16("name2")]);
123 TEST_F(NameFieldTest
, FirstLast2
) {
125 field
.form_control_type
= "text";
127 field
.label
= ASCIIToUTF16("Name");
128 field
.name
= ASCIIToUTF16("first_name");
129 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name1")));
131 field
.label
= ASCIIToUTF16("Name");
132 field
.name
= ASCIIToUTF16("last_name");
133 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name2")));
135 AutofillScanner
scanner(list_
.get());
136 field_
= Parse(&scanner
);
137 ASSERT_NE(nullptr, field_
.get());
138 ASSERT_TRUE(field_
->ClassifyField(&field_type_map_
));
140 field_type_map_
.find(ASCIIToUTF16("name1")) != field_type_map_
.end());
141 EXPECT_EQ(NAME_FIRST
, field_type_map_
[ASCIIToUTF16("name1")]);
143 field_type_map_
.find(ASCIIToUTF16("name2")) != field_type_map_
.end());
144 EXPECT_EQ(NAME_LAST
, field_type_map_
[ASCIIToUTF16("name2")]);
147 TEST_F(NameFieldTest
, FirstLastMiddleWithSpaces
) {
149 field
.form_control_type
= "text";
151 field
.label
= ASCIIToUTF16("First Name");
152 field
.name
= ASCIIToUTF16("first_name");
153 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name1")));
155 field
.label
= ASCIIToUTF16("Middle Name");
156 field
.name
= ASCIIToUTF16("middle_name");
157 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name2")));
159 field
.label
= ASCIIToUTF16("Last Name");
160 field
.name
= ASCIIToUTF16("last_name");
161 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name3")));
163 AutofillScanner
scanner(list_
.get());
164 field_
= Parse(&scanner
);
165 ASSERT_NE(nullptr, field_
.get());
166 ASSERT_TRUE(field_
->ClassifyField(&field_type_map_
));
168 field_type_map_
.find(ASCIIToUTF16("name1")) != field_type_map_
.end());
169 EXPECT_EQ(NAME_FIRST
, field_type_map_
[ASCIIToUTF16("name1")]);
171 field_type_map_
.find(ASCIIToUTF16("name2")) != field_type_map_
.end());
172 EXPECT_EQ(NAME_MIDDLE
, field_type_map_
[ASCIIToUTF16("name2")]);
174 field_type_map_
.find(ASCIIToUTF16("name3")) != field_type_map_
.end());
175 EXPECT_EQ(NAME_LAST
, field_type_map_
[ASCIIToUTF16("name3")]);
178 TEST_F(NameFieldTest
, FirstLastEmpty
) {
180 field
.form_control_type
= "text";
182 field
.label
= ASCIIToUTF16("Name");
183 field
.name
= ASCIIToUTF16("first_name");
184 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name1")));
186 field
.label
= base::string16();
187 field
.name
= ASCIIToUTF16("last_name");
188 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name2")));
190 AutofillScanner
scanner(list_
.get());
191 field_
= Parse(&scanner
);
192 ASSERT_NE(nullptr, field_
.get());
193 ASSERT_TRUE(field_
->ClassifyField(&field_type_map_
));
195 field_type_map_
.find(ASCIIToUTF16("name1")) != field_type_map_
.end());
196 EXPECT_EQ(NAME_FIRST
, field_type_map_
[ASCIIToUTF16("name1")]);
198 field_type_map_
.find(ASCIIToUTF16("name2")) != field_type_map_
.end());
199 EXPECT_EQ(NAME_LAST
, field_type_map_
[ASCIIToUTF16("name2")]);
202 TEST_F(NameFieldTest
, FirstMiddleLastEmpty
) {
204 field
.form_control_type
= "text";
206 field
.label
= ASCIIToUTF16("Name");
207 field
.name
= ASCIIToUTF16("first_name");
208 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name1")));
210 field
.label
= base::string16();
211 field
.name
= ASCIIToUTF16("middle_name");
212 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name2")));
214 field
.label
= base::string16();
215 field
.name
= ASCIIToUTF16("last_name");
216 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name3")));
218 AutofillScanner
scanner(list_
.get());
219 field_
= Parse(&scanner
);
220 ASSERT_NE(nullptr, field_
.get());
221 ASSERT_TRUE(field_
->ClassifyField(&field_type_map_
));
223 field_type_map_
.find(ASCIIToUTF16("name1")) != field_type_map_
.end());
224 EXPECT_EQ(NAME_FIRST
, field_type_map_
[ASCIIToUTF16("name1")]);
226 field_type_map_
.find(ASCIIToUTF16("name2")) != field_type_map_
.end());
227 EXPECT_EQ(NAME_MIDDLE_INITIAL
, field_type_map_
[ASCIIToUTF16("name2")]);
229 field_type_map_
.find(ASCIIToUTF16("name3")) != field_type_map_
.end());
230 EXPECT_EQ(NAME_LAST
, field_type_map_
[ASCIIToUTF16("name3")]);
233 TEST_F(NameFieldTest
, MiddleInitial
) {
235 field
.form_control_type
= "text";
237 field
.label
= ASCIIToUTF16("First Name");
238 field
.name
= ASCIIToUTF16("first_name");
239 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name1")));
241 field
.label
= ASCIIToUTF16("MI");
242 field
.name
= ASCIIToUTF16("middle_name");
243 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name2")));
245 field
.label
= ASCIIToUTF16("Last Name");
246 field
.name
= ASCIIToUTF16("last_name");
247 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name3")));
249 AutofillScanner
scanner(list_
.get());
250 field_
= Parse(&scanner
);
251 ASSERT_NE(nullptr, field_
.get());
252 ASSERT_TRUE(field_
->ClassifyField(&field_type_map_
));
254 field_type_map_
.find(ASCIIToUTF16("name1")) != field_type_map_
.end());
255 EXPECT_EQ(NAME_FIRST
, field_type_map_
[ASCIIToUTF16("name1")]);
257 field_type_map_
.find(ASCIIToUTF16("name2")) != field_type_map_
.end());
258 EXPECT_EQ(NAME_MIDDLE_INITIAL
, field_type_map_
[ASCIIToUTF16("name2")]);
260 field_type_map_
.find(ASCIIToUTF16("name3")) != field_type_map_
.end());
261 EXPECT_EQ(NAME_LAST
, field_type_map_
[ASCIIToUTF16("name3")]);
264 TEST_F(NameFieldTest
, MiddleInitialNoLastName
) {
266 field
.form_control_type
= "text";
268 field
.label
= ASCIIToUTF16("First Name");
269 field
.name
= ASCIIToUTF16("first_name");
270 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name1")));
272 field
.label
= ASCIIToUTF16("MI");
273 field
.name
= ASCIIToUTF16("middle_name");
274 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name2")));
276 AutofillScanner
scanner(list_
.get());
277 field_
= Parse(&scanner
);
278 ASSERT_EQ(nullptr, field_
.get());
281 // This case is from the dell.com checkout page. The middle initial "mi" string
282 // came at the end following other descriptive text. http://crbug.com/45123.
283 TEST_F(NameFieldTest
, MiddleInitialAtEnd
) {
285 field
.form_control_type
= "text";
287 field
.label
= base::string16();
288 field
.name
= ASCIIToUTF16("XXXnameXXXfirst");
289 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name1")));
291 field
.label
= base::string16();
292 field
.name
= ASCIIToUTF16("XXXnameXXXmi");
293 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name2")));
295 field
.label
= base::string16();
296 field
.name
= ASCIIToUTF16("XXXnameXXXlast");
297 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name3")));
299 AutofillScanner
scanner(list_
.get());
300 field_
= Parse(&scanner
);
301 ASSERT_NE(nullptr, field_
.get());
302 ASSERT_TRUE(field_
->ClassifyField(&field_type_map_
));
304 field_type_map_
.find(ASCIIToUTF16("name1")) != field_type_map_
.end());
305 EXPECT_EQ(NAME_FIRST
, field_type_map_
[ASCIIToUTF16("name1")]);
307 field_type_map_
.find(ASCIIToUTF16("name2")) != field_type_map_
.end());
308 EXPECT_EQ(NAME_MIDDLE_INITIAL
, field_type_map_
[ASCIIToUTF16("name2")]);
310 field_type_map_
.find(ASCIIToUTF16("name3")) != field_type_map_
.end());
311 EXPECT_EQ(NAME_LAST
, field_type_map_
[ASCIIToUTF16("name3")]);
314 } // namespace autofill