• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "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"
13 
14 using base::ASCIIToUTF16;
15 
16 namespace autofill {
17 
18 class NameFieldTest : public testing::Test {
19  public:
NameFieldTest()20   NameFieldTest() {}
21 
22  protected:
23   ScopedVector<const AutofillField> list_;
24   scoped_ptr<NameField> field_;
25   ServerFieldTypeMap field_type_map_;
26 
27   // Downcast for tests.
Parse(AutofillScanner * scanner)28   static NameField* Parse(AutofillScanner* scanner) {
29     return static_cast<NameField*>(NameField::Parse(scanner));
30   }
31 
32  private:
33   DISALLOW_COPY_AND_ASSIGN(NameFieldTest);
34 };
35 
TEST_F(NameFieldTest,FirstMiddleLast)36 TEST_F(NameFieldTest, FirstMiddleLast) {
37   FormFieldData field;
38   field.form_control_type = "text";
39 
40   field.label = ASCIIToUTF16("First Name");
41   field.name = ASCIIToUTF16("First");
42   list_.push_back(new AutofillField(field, ASCIIToUTF16("name1")));
43 
44   field.label = ASCIIToUTF16("Middle Name");
45   field.name = ASCIIToUTF16("Middle");
46   list_.push_back(new AutofillField(field, ASCIIToUTF16("name2")));
47 
48   field.label = ASCIIToUTF16("Last Name");
49   field.name = ASCIIToUTF16("Last");
50   list_.push_back(new AutofillField(field, ASCIIToUTF16("name3")));
51 
52   AutofillScanner scanner(list_.get());
53   field_.reset(Parse(&scanner));
54   ASSERT_NE(static_cast<NameField*>(NULL), field_.get());
55   ASSERT_TRUE(field_->ClassifyField(&field_type_map_));
56   ASSERT_TRUE(
57       field_type_map_.find(ASCIIToUTF16("name1")) != field_type_map_.end());
58   EXPECT_EQ(NAME_FIRST, field_type_map_[ASCIIToUTF16("name1")]);
59   ASSERT_TRUE(
60       field_type_map_.find(ASCIIToUTF16("name2")) != field_type_map_.end());
61   EXPECT_EQ(NAME_MIDDLE, field_type_map_[ASCIIToUTF16("name2")]);
62   ASSERT_TRUE(
63       field_type_map_.find(ASCIIToUTF16("name3")) != field_type_map_.end());
64   EXPECT_EQ(NAME_LAST, field_type_map_[ASCIIToUTF16("name3")]);
65 }
66 
TEST_F(NameFieldTest,FirstMiddleLast2)67 TEST_F(NameFieldTest, FirstMiddleLast2) {
68   FormFieldData field;
69   field.form_control_type = "text";
70 
71   field.label = base::string16();
72   field.name = ASCIIToUTF16("firstName");
73   list_.push_back(new AutofillField(field, ASCIIToUTF16("name1")));
74 
75   field.label = base::string16();
76   field.name = ASCIIToUTF16("middleName");
77   list_.push_back(new AutofillField(field, ASCIIToUTF16("name2")));
78 
79   field.label = base::string16();
80   field.name = ASCIIToUTF16("lastName");
81   list_.push_back(new AutofillField(field, ASCIIToUTF16("name3")));
82 
83   AutofillScanner scanner(list_.get());
84   field_.reset(Parse(&scanner));
85   ASSERT_NE(static_cast<NameField*>(NULL), field_.get());
86   ASSERT_TRUE(field_->ClassifyField(&field_type_map_));
87   ASSERT_TRUE(
88       field_type_map_.find(ASCIIToUTF16("name1")) != field_type_map_.end());
89   EXPECT_EQ(NAME_FIRST, field_type_map_[ASCIIToUTF16("name1")]);
90   ASSERT_TRUE(
91       field_type_map_.find(ASCIIToUTF16("name2")) != field_type_map_.end());
92   EXPECT_EQ(NAME_MIDDLE, field_type_map_[ASCIIToUTF16("name2")]);
93   ASSERT_TRUE(
94       field_type_map_.find(ASCIIToUTF16("name3")) != field_type_map_.end());
95   EXPECT_EQ(NAME_LAST, field_type_map_[ASCIIToUTF16("name3")]);
96 }
97 
TEST_F(NameFieldTest,FirstLast)98 TEST_F(NameFieldTest, FirstLast) {
99   FormFieldData field;
100   field.form_control_type = "text";
101 
102   field.label = base::string16();
103   field.name = ASCIIToUTF16("first_name");
104   list_.push_back(new AutofillField(field, ASCIIToUTF16("name1")));
105 
106   field.label = base::string16();
107   field.name = ASCIIToUTF16("last_name");
108   list_.push_back(new AutofillField(field, ASCIIToUTF16("name2")));
109 
110   AutofillScanner scanner(list_.get());
111   field_.reset(Parse(&scanner));
112   ASSERT_NE(static_cast<NameField*>(NULL), field_.get());
113   ASSERT_TRUE(field_->ClassifyField(&field_type_map_));
114   ASSERT_TRUE(
115       field_type_map_.find(ASCIIToUTF16("name1")) != field_type_map_.end());
116   EXPECT_EQ(NAME_FIRST, field_type_map_[ASCIIToUTF16("name1")]);
117   ASSERT_TRUE(
118       field_type_map_.find(ASCIIToUTF16("name2")) != field_type_map_.end());
119   EXPECT_EQ(NAME_LAST, field_type_map_[ASCIIToUTF16("name2")]);
120 }
121 
TEST_F(NameFieldTest,FirstLast2)122 TEST_F(NameFieldTest, FirstLast2) {
123   FormFieldData field;
124   field.form_control_type = "text";
125 
126   field.label = ASCIIToUTF16("Name");
127   field.name = ASCIIToUTF16("first_name");
128   list_.push_back(new AutofillField(field, ASCIIToUTF16("name1")));
129 
130   field.label = ASCIIToUTF16("Name");
131   field.name = ASCIIToUTF16("last_name");
132   list_.push_back(new AutofillField(field, ASCIIToUTF16("name2")));
133 
134   AutofillScanner scanner(list_.get());
135   field_.reset(Parse(&scanner));
136   ASSERT_NE(static_cast<NameField*>(NULL), field_.get());
137   ASSERT_TRUE(field_->ClassifyField(&field_type_map_));
138   ASSERT_TRUE(
139       field_type_map_.find(ASCIIToUTF16("name1")) != field_type_map_.end());
140   EXPECT_EQ(NAME_FIRST, field_type_map_[ASCIIToUTF16("name1")]);
141   ASSERT_TRUE(
142       field_type_map_.find(ASCIIToUTF16("name2")) != field_type_map_.end());
143   EXPECT_EQ(NAME_LAST, field_type_map_[ASCIIToUTF16("name2")]);
144 }
145 
TEST_F(NameFieldTest,FirstLastMiddleWithSpaces)146 TEST_F(NameFieldTest, FirstLastMiddleWithSpaces) {
147   FormFieldData field;
148   field.form_control_type = "text";
149 
150   field.label = ASCIIToUTF16("First  Name");
151   field.name = ASCIIToUTF16("first_name");
152   list_.push_back(new AutofillField(field, ASCIIToUTF16("name1")));
153 
154   field.label = ASCIIToUTF16("Middle  Name");
155   field.name = ASCIIToUTF16("middle_name");
156   list_.push_back(new AutofillField(field, ASCIIToUTF16("name2")));
157 
158   field.label = ASCIIToUTF16("Last  Name");
159   field.name = ASCIIToUTF16("last_name");
160   list_.push_back(new AutofillField(field, ASCIIToUTF16("name3")));
161 
162   AutofillScanner scanner(list_.get());
163   field_.reset(Parse(&scanner));
164   ASSERT_NE(static_cast<NameField*>(NULL), field_.get());
165   ASSERT_TRUE(field_->ClassifyField(&field_type_map_));
166   ASSERT_TRUE(
167       field_type_map_.find(ASCIIToUTF16("name1")) != field_type_map_.end());
168   EXPECT_EQ(NAME_FIRST, field_type_map_[ASCIIToUTF16("name1")]);
169   ASSERT_TRUE(
170       field_type_map_.find(ASCIIToUTF16("name2")) != field_type_map_.end());
171   EXPECT_EQ(NAME_MIDDLE, field_type_map_[ASCIIToUTF16("name2")]);
172   ASSERT_TRUE(
173       field_type_map_.find(ASCIIToUTF16("name3")) != field_type_map_.end());
174   EXPECT_EQ(NAME_LAST, field_type_map_[ASCIIToUTF16("name3")]);
175 }
176 
TEST_F(NameFieldTest,FirstLastEmpty)177 TEST_F(NameFieldTest, FirstLastEmpty) {
178   FormFieldData field;
179   field.form_control_type = "text";
180 
181   field.label = ASCIIToUTF16("Name");
182   field.name = ASCIIToUTF16("first_name");
183   list_.push_back(new AutofillField(field, ASCIIToUTF16("name1")));
184 
185     field.label = base::string16();
186   field.name = ASCIIToUTF16("last_name");
187   list_.push_back(new AutofillField(field, ASCIIToUTF16("name2")));
188 
189   AutofillScanner scanner(list_.get());
190   field_.reset(Parse(&scanner));
191   ASSERT_NE(static_cast<NameField*>(NULL), field_.get());
192   ASSERT_TRUE(field_->ClassifyField(&field_type_map_));
193   ASSERT_TRUE(
194       field_type_map_.find(ASCIIToUTF16("name1")) != field_type_map_.end());
195   EXPECT_EQ(NAME_FIRST, field_type_map_[ASCIIToUTF16("name1")]);
196   ASSERT_TRUE(
197       field_type_map_.find(ASCIIToUTF16("name2")) != field_type_map_.end());
198   EXPECT_EQ(NAME_LAST, field_type_map_[ASCIIToUTF16("name2")]);
199 }
200 
TEST_F(NameFieldTest,FirstMiddleLastEmpty)201 TEST_F(NameFieldTest, FirstMiddleLastEmpty) {
202   FormFieldData field;
203   field.form_control_type = "text";
204 
205   field.label = ASCIIToUTF16("Name");
206   field.name = ASCIIToUTF16("first_name");
207   list_.push_back(new AutofillField(field, ASCIIToUTF16("name1")));
208 
209   field.label = base::string16();
210   field.name = ASCIIToUTF16("middle_name");
211   list_.push_back(new AutofillField(field, ASCIIToUTF16("name2")));
212 
213   field.label = base::string16();
214   field.name = ASCIIToUTF16("last_name");
215   list_.push_back(new AutofillField(field, ASCIIToUTF16("name3")));
216 
217   AutofillScanner scanner(list_.get());
218   field_.reset(Parse(&scanner));
219   ASSERT_NE(static_cast<NameField*>(NULL), field_.get());
220   ASSERT_TRUE(field_->ClassifyField(&field_type_map_));
221   ASSERT_TRUE(
222       field_type_map_.find(ASCIIToUTF16("name1")) != field_type_map_.end());
223   EXPECT_EQ(NAME_FIRST, field_type_map_[ASCIIToUTF16("name1")]);
224   ASSERT_TRUE(
225       field_type_map_.find(ASCIIToUTF16("name2")) != field_type_map_.end());
226   EXPECT_EQ(NAME_MIDDLE_INITIAL, field_type_map_[ASCIIToUTF16("name2")]);
227   ASSERT_TRUE(
228       field_type_map_.find(ASCIIToUTF16("name3")) != field_type_map_.end());
229   EXPECT_EQ(NAME_LAST, field_type_map_[ASCIIToUTF16("name3")]);
230 }
231 
TEST_F(NameFieldTest,MiddleInitial)232 TEST_F(NameFieldTest, MiddleInitial) {
233   FormFieldData field;
234   field.form_control_type = "text";
235 
236   field.label = ASCIIToUTF16("First Name");
237   field.name = ASCIIToUTF16("first_name");
238   list_.push_back(new AutofillField(field, ASCIIToUTF16("name1")));
239 
240   field.label = ASCIIToUTF16("MI");
241   field.name = ASCIIToUTF16("middle_name");
242   list_.push_back(new AutofillField(field, ASCIIToUTF16("name2")));
243 
244   field.label = ASCIIToUTF16("Last Name");
245   field.name = ASCIIToUTF16("last_name");
246   list_.push_back(new AutofillField(field, ASCIIToUTF16("name3")));
247 
248   AutofillScanner scanner(list_.get());
249   field_.reset(Parse(&scanner));
250   ASSERT_NE(static_cast<NameField*>(NULL), field_.get());
251   ASSERT_TRUE(field_->ClassifyField(&field_type_map_));
252   ASSERT_TRUE(
253       field_type_map_.find(ASCIIToUTF16("name1")) != field_type_map_.end());
254   EXPECT_EQ(NAME_FIRST, field_type_map_[ASCIIToUTF16("name1")]);
255   ASSERT_TRUE(
256       field_type_map_.find(ASCIIToUTF16("name2")) != field_type_map_.end());
257   EXPECT_EQ(NAME_MIDDLE_INITIAL, field_type_map_[ASCIIToUTF16("name2")]);
258   ASSERT_TRUE(
259       field_type_map_.find(ASCIIToUTF16("name3")) != field_type_map_.end());
260   EXPECT_EQ(NAME_LAST, field_type_map_[ASCIIToUTF16("name3")]);
261 }
262 
TEST_F(NameFieldTest,MiddleInitialNoLastName)263 TEST_F(NameFieldTest, MiddleInitialNoLastName) {
264   FormFieldData field;
265   field.form_control_type = "text";
266 
267   field.label = ASCIIToUTF16("First Name");
268   field.name = ASCIIToUTF16("first_name");
269   list_.push_back(new AutofillField(field, ASCIIToUTF16("name1")));
270 
271   field.label = ASCIIToUTF16("MI");
272   field.name = ASCIIToUTF16("middle_name");
273   list_.push_back(new AutofillField(field, ASCIIToUTF16("name2")));
274 
275   AutofillScanner scanner(list_.get());
276   field_.reset(Parse(&scanner));
277   ASSERT_EQ(static_cast<NameField*>(NULL), field_.get());
278 }
279 
280 // This case is from the dell.com checkout page.  The middle initial "mi" string
281 // came at the end following other descriptive text.  http://crbug.com/45123.
TEST_F(NameFieldTest,MiddleInitialAtEnd)282 TEST_F(NameFieldTest, MiddleInitialAtEnd) {
283   FormFieldData field;
284   field.form_control_type = "text";
285 
286   field.label = base::string16();
287   field.name = ASCIIToUTF16("XXXnameXXXfirst");
288   list_.push_back(new AutofillField(field, ASCIIToUTF16("name1")));
289 
290   field.label = base::string16();
291   field.name = ASCIIToUTF16("XXXnameXXXmi");
292   list_.push_back(new AutofillField(field, ASCIIToUTF16("name2")));
293 
294   field.label = base::string16();
295   field.name = ASCIIToUTF16("XXXnameXXXlast");
296   list_.push_back(new AutofillField(field, ASCIIToUTF16("name3")));
297 
298   AutofillScanner scanner(list_.get());
299   field_.reset(Parse(&scanner));
300   ASSERT_NE(static_cast<NameField*>(NULL), field_.get());
301   ASSERT_TRUE(field_->ClassifyField(&field_type_map_));
302   ASSERT_TRUE(
303       field_type_map_.find(ASCIIToUTF16("name1")) != field_type_map_.end());
304   EXPECT_EQ(NAME_FIRST, field_type_map_[ASCIIToUTF16("name1")]);
305   ASSERT_TRUE(
306       field_type_map_.find(ASCIIToUTF16("name2")) != field_type_map_.end());
307   EXPECT_EQ(NAME_MIDDLE_INITIAL, field_type_map_[ASCIIToUTF16("name2")]);
308   ASSERT_TRUE(
309       field_type_map_.find(ASCIIToUTF16("name3")) != field_type_map_.end());
310   EXPECT_EQ(NAME_LAST, field_type_map_[ASCIIToUTF16("name3")]);
311 }
312 
313 }  // namespace autofill
314