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.
5 #include "base/memory/scoped_ptr.h"
6 #include "base/memory/scoped_vector.h"
7 #include "base/utf_string_conversions.h"
8 #include "chrome/browser/autofill/autofill_field.h"
9 #include "chrome/browser/autofill/autofill_scanner.h"
10 #include "chrome/browser/autofill/name_field.h"
11 #include "chrome/common/form_field_data.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 class NameFieldTest
: public testing::Test
{
19 ScopedVector
<const AutofillField
> list_
;
20 scoped_ptr
<NameField
> field_
;
21 FieldTypeMap field_type_map_
;
23 // Downcast for tests.
24 static NameField
* Parse(AutofillScanner
* scanner
) {
25 return static_cast<NameField
*>(NameField::Parse(scanner
, false));
29 DISALLOW_COPY_AND_ASSIGN(NameFieldTest
);
32 TEST_F(NameFieldTest
, FirstMiddleLast
) {
34 field
.form_control_type
= "text";
36 field
.label
= ASCIIToUTF16("First Name");
37 field
.name
= ASCIIToUTF16("First");
38 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name1")));
40 field
.label
= ASCIIToUTF16("Middle Name");
41 field
.name
= ASCIIToUTF16("Middle");
42 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name2")));
44 field
.label
= ASCIIToUTF16("Last Name");
45 field
.name
= ASCIIToUTF16("Last");
46 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name3")));
48 AutofillScanner
scanner(list_
.get());
49 field_
.reset(Parse(&scanner
));
50 ASSERT_NE(static_cast<NameField
*>(NULL
), field_
.get());
51 ASSERT_TRUE(field_
->ClassifyField(&field_type_map_
));
53 field_type_map_
.find(ASCIIToUTF16("name1")) != field_type_map_
.end());
54 EXPECT_EQ(NAME_FIRST
, field_type_map_
[ASCIIToUTF16("name1")]);
56 field_type_map_
.find(ASCIIToUTF16("name2")) != field_type_map_
.end());
57 EXPECT_EQ(NAME_MIDDLE
, field_type_map_
[ASCIIToUTF16("name2")]);
59 field_type_map_
.find(ASCIIToUTF16("name3")) != field_type_map_
.end());
60 EXPECT_EQ(NAME_LAST
, field_type_map_
[ASCIIToUTF16("name3")]);
63 TEST_F(NameFieldTest
, FirstMiddleLast2
) {
65 field
.form_control_type
= "text";
67 field
.label
= string16();
68 field
.name
= ASCIIToUTF16("firstName");
69 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name1")));
71 field
.label
= string16();
72 field
.name
= ASCIIToUTF16("middleName");
73 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name2")));
75 field
.label
= string16();
76 field
.name
= ASCIIToUTF16("lastName");
77 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name3")));
79 AutofillScanner
scanner(list_
.get());
80 field_
.reset(Parse(&scanner
));
81 ASSERT_NE(static_cast<NameField
*>(NULL
), field_
.get());
82 ASSERT_TRUE(field_
->ClassifyField(&field_type_map_
));
84 field_type_map_
.find(ASCIIToUTF16("name1")) != field_type_map_
.end());
85 EXPECT_EQ(NAME_FIRST
, field_type_map_
[ASCIIToUTF16("name1")]);
87 field_type_map_
.find(ASCIIToUTF16("name2")) != field_type_map_
.end());
88 EXPECT_EQ(NAME_MIDDLE
, field_type_map_
[ASCIIToUTF16("name2")]);
90 field_type_map_
.find(ASCIIToUTF16("name3")) != field_type_map_
.end());
91 EXPECT_EQ(NAME_LAST
, field_type_map_
[ASCIIToUTF16("name3")]);
94 TEST_F(NameFieldTest
, FirstLast
) {
96 field
.form_control_type
= "text";
98 field
.label
= string16();
99 field
.name
= ASCIIToUTF16("first_name");
100 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name1")));
102 field
.label
= string16();
103 field
.name
= ASCIIToUTF16("last_name");
104 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name2")));
106 AutofillScanner
scanner(list_
.get());
107 field_
.reset(Parse(&scanner
));
108 ASSERT_NE(static_cast<NameField
*>(NULL
), field_
.get());
109 ASSERT_TRUE(field_
->ClassifyField(&field_type_map_
));
111 field_type_map_
.find(ASCIIToUTF16("name1")) != field_type_map_
.end());
112 EXPECT_EQ(NAME_FIRST
, field_type_map_
[ASCIIToUTF16("name1")]);
114 field_type_map_
.find(ASCIIToUTF16("name2")) != field_type_map_
.end());
115 EXPECT_EQ(NAME_LAST
, field_type_map_
[ASCIIToUTF16("name2")]);
118 TEST_F(NameFieldTest
, FirstLast2
) {
120 field
.form_control_type
= "text";
122 field
.label
= ASCIIToUTF16("Name");
123 field
.name
= ASCIIToUTF16("first_name");
124 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name1")));
126 field
.label
= ASCIIToUTF16("Name");
127 field
.name
= ASCIIToUTF16("last_name");
128 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name2")));
130 AutofillScanner
scanner(list_
.get());
131 field_
.reset(Parse(&scanner
));
132 ASSERT_NE(static_cast<NameField
*>(NULL
), field_
.get());
133 ASSERT_TRUE(field_
->ClassifyField(&field_type_map_
));
135 field_type_map_
.find(ASCIIToUTF16("name1")) != field_type_map_
.end());
136 EXPECT_EQ(NAME_FIRST
, field_type_map_
[ASCIIToUTF16("name1")]);
138 field_type_map_
.find(ASCIIToUTF16("name2")) != field_type_map_
.end());
139 EXPECT_EQ(NAME_LAST
, field_type_map_
[ASCIIToUTF16("name2")]);
142 TEST_F(NameFieldTest
, FirstLastMiddleWithSpaces
) {
144 field
.form_control_type
= "text";
146 field
.label
= ASCIIToUTF16("First Name");
147 field
.name
= ASCIIToUTF16("first_name");
148 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name1")));
150 field
.label
= ASCIIToUTF16("Middle Name");
151 field
.name
= ASCIIToUTF16("middle_name");
152 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name2")));
154 field
.label
= ASCIIToUTF16("Last Name");
155 field
.name
= ASCIIToUTF16("last_name");
156 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name3")));
158 AutofillScanner
scanner(list_
.get());
159 field_
.reset(Parse(&scanner
));
160 ASSERT_NE(static_cast<NameField
*>(NULL
), field_
.get());
161 ASSERT_TRUE(field_
->ClassifyField(&field_type_map_
));
163 field_type_map_
.find(ASCIIToUTF16("name1")) != field_type_map_
.end());
164 EXPECT_EQ(NAME_FIRST
, field_type_map_
[ASCIIToUTF16("name1")]);
166 field_type_map_
.find(ASCIIToUTF16("name2")) != field_type_map_
.end());
167 EXPECT_EQ(NAME_MIDDLE
, field_type_map_
[ASCIIToUTF16("name2")]);
169 field_type_map_
.find(ASCIIToUTF16("name3")) != field_type_map_
.end());
170 EXPECT_EQ(NAME_LAST
, field_type_map_
[ASCIIToUTF16("name3")]);
173 TEST_F(NameFieldTest
, FirstLastEmpty
) {
175 field
.form_control_type
= "text";
177 field
.label
= ASCIIToUTF16("Name");
178 field
.name
= ASCIIToUTF16("first_name");
179 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name1")));
181 field
.label
= string16();
182 field
.name
= ASCIIToUTF16("last_name");
183 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name2")));
185 AutofillScanner
scanner(list_
.get());
186 field_
.reset(Parse(&scanner
));
187 ASSERT_NE(static_cast<NameField
*>(NULL
), field_
.get());
188 ASSERT_TRUE(field_
->ClassifyField(&field_type_map_
));
190 field_type_map_
.find(ASCIIToUTF16("name1")) != field_type_map_
.end());
191 EXPECT_EQ(NAME_FIRST
, field_type_map_
[ASCIIToUTF16("name1")]);
193 field_type_map_
.find(ASCIIToUTF16("name2")) != field_type_map_
.end());
194 EXPECT_EQ(NAME_LAST
, field_type_map_
[ASCIIToUTF16("name2")]);
197 TEST_F(NameFieldTest
, FirstMiddleLastEmpty
) {
199 field
.form_control_type
= "text";
201 field
.label
= ASCIIToUTF16("Name");
202 field
.name
= ASCIIToUTF16("first_name");
203 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name1")));
205 field
.label
= string16();
206 field
.name
= ASCIIToUTF16("middle_name");
207 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name2")));
209 field
.label
= string16();
210 field
.name
= ASCIIToUTF16("last_name");
211 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name3")));
213 AutofillScanner
scanner(list_
.get());
214 field_
.reset(Parse(&scanner
));
215 ASSERT_NE(static_cast<NameField
*>(NULL
), field_
.get());
216 ASSERT_TRUE(field_
->ClassifyField(&field_type_map_
));
218 field_type_map_
.find(ASCIIToUTF16("name1")) != field_type_map_
.end());
219 EXPECT_EQ(NAME_FIRST
, field_type_map_
[ASCIIToUTF16("name1")]);
221 field_type_map_
.find(ASCIIToUTF16("name2")) != field_type_map_
.end());
222 EXPECT_EQ(NAME_MIDDLE_INITIAL
, field_type_map_
[ASCIIToUTF16("name2")]);
224 field_type_map_
.find(ASCIIToUTF16("name3")) != field_type_map_
.end());
225 EXPECT_EQ(NAME_LAST
, field_type_map_
[ASCIIToUTF16("name3")]);
228 TEST_F(NameFieldTest
, MiddleInitial
) {
230 field
.form_control_type
= "text";
232 field
.label
= ASCIIToUTF16("First Name");
233 field
.name
= ASCIIToUTF16("first_name");
234 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name1")));
236 field
.label
= ASCIIToUTF16("MI");
237 field
.name
= ASCIIToUTF16("middle_name");
238 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name2")));
240 field
.label
= ASCIIToUTF16("Last Name");
241 field
.name
= ASCIIToUTF16("last_name");
242 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name3")));
244 AutofillScanner
scanner(list_
.get());
245 field_
.reset(Parse(&scanner
));
246 ASSERT_NE(static_cast<NameField
*>(NULL
), field_
.get());
247 ASSERT_TRUE(field_
->ClassifyField(&field_type_map_
));
249 field_type_map_
.find(ASCIIToUTF16("name1")) != field_type_map_
.end());
250 EXPECT_EQ(NAME_FIRST
, field_type_map_
[ASCIIToUTF16("name1")]);
252 field_type_map_
.find(ASCIIToUTF16("name2")) != field_type_map_
.end());
253 EXPECT_EQ(NAME_MIDDLE_INITIAL
, field_type_map_
[ASCIIToUTF16("name2")]);
255 field_type_map_
.find(ASCIIToUTF16("name3")) != field_type_map_
.end());
256 EXPECT_EQ(NAME_LAST
, field_type_map_
[ASCIIToUTF16("name3")]);
259 TEST_F(NameFieldTest
, MiddleInitialNoLastName
) {
261 field
.form_control_type
= "text";
263 field
.label
= ASCIIToUTF16("First Name");
264 field
.name
= ASCIIToUTF16("first_name");
265 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name1")));
267 field
.label
= ASCIIToUTF16("MI");
268 field
.name
= ASCIIToUTF16("middle_name");
269 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name2")));
271 AutofillScanner
scanner(list_
.get());
272 field_
.reset(Parse(&scanner
));
273 ASSERT_EQ(static_cast<NameField
*>(NULL
), field_
.get());
276 // This case is from the dell.com checkout page. The middle initial "mi" string
277 // came at the end following other descriptive text. http://crbug.com/45123.
278 TEST_F(NameFieldTest
, MiddleInitialAtEnd
) {
280 field
.form_control_type
= "text";
282 field
.label
= string16();
283 field
.name
= ASCIIToUTF16("XXXnameXXXfirst");
284 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name1")));
286 field
.label
= string16();
287 field
.name
= ASCIIToUTF16("XXXnameXXXmi");
288 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name2")));
290 field
.label
= string16();
291 field
.name
= ASCIIToUTF16("XXXnameXXXlast");
292 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("name3")));
294 AutofillScanner
scanner(list_
.get());
295 field_
.reset(Parse(&scanner
));
296 ASSERT_NE(static_cast<NameField
*>(NULL
), field_
.get());
297 ASSERT_TRUE(field_
->ClassifyField(&field_type_map_
));
299 field_type_map_
.find(ASCIIToUTF16("name1")) != field_type_map_
.end());
300 EXPECT_EQ(NAME_FIRST
, field_type_map_
[ASCIIToUTF16("name1")]);
302 field_type_map_
.find(ASCIIToUTF16("name2")) != field_type_map_
.end());
303 EXPECT_EQ(NAME_MIDDLE_INITIAL
, field_type_map_
[ASCIIToUTF16("name2")]);
305 field_type_map_
.find(ASCIIToUTF16("name3")) != field_type_map_
.end());
306 EXPECT_EQ(NAME_LAST
, field_type_map_
[ASCIIToUTF16("name3")]);