Revert 168224 - Update V8 to version 3.15.4.
[chromium-blink-merge.git] / chrome / browser / autofill / name_field_unittest.cc
blob08b4be0b9ec9ae38882aba50062a1cba7e76a447
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 {
15 public:
16 NameFieldTest() {}
18 protected:
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));
28 private:
29 DISALLOW_COPY_AND_ASSIGN(NameFieldTest);
32 TEST_F(NameFieldTest, FirstMiddleLast) {
33 FormFieldData field;
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_));
52 ASSERT_TRUE(
53 field_type_map_.find(ASCIIToUTF16("name1")) != field_type_map_.end());
54 EXPECT_EQ(NAME_FIRST, field_type_map_[ASCIIToUTF16("name1")]);
55 ASSERT_TRUE(
56 field_type_map_.find(ASCIIToUTF16("name2")) != field_type_map_.end());
57 EXPECT_EQ(NAME_MIDDLE, field_type_map_[ASCIIToUTF16("name2")]);
58 ASSERT_TRUE(
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) {
64 FormFieldData field;
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_));
83 ASSERT_TRUE(
84 field_type_map_.find(ASCIIToUTF16("name1")) != field_type_map_.end());
85 EXPECT_EQ(NAME_FIRST, field_type_map_[ASCIIToUTF16("name1")]);
86 ASSERT_TRUE(
87 field_type_map_.find(ASCIIToUTF16("name2")) != field_type_map_.end());
88 EXPECT_EQ(NAME_MIDDLE, field_type_map_[ASCIIToUTF16("name2")]);
89 ASSERT_TRUE(
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) {
95 FormFieldData field;
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_));
110 ASSERT_TRUE(
111 field_type_map_.find(ASCIIToUTF16("name1")) != field_type_map_.end());
112 EXPECT_EQ(NAME_FIRST, field_type_map_[ASCIIToUTF16("name1")]);
113 ASSERT_TRUE(
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) {
119 FormFieldData field;
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_));
134 ASSERT_TRUE(
135 field_type_map_.find(ASCIIToUTF16("name1")) != field_type_map_.end());
136 EXPECT_EQ(NAME_FIRST, field_type_map_[ASCIIToUTF16("name1")]);
137 ASSERT_TRUE(
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) {
143 FormFieldData field;
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_));
162 ASSERT_TRUE(
163 field_type_map_.find(ASCIIToUTF16("name1")) != field_type_map_.end());
164 EXPECT_EQ(NAME_FIRST, field_type_map_[ASCIIToUTF16("name1")]);
165 ASSERT_TRUE(
166 field_type_map_.find(ASCIIToUTF16("name2")) != field_type_map_.end());
167 EXPECT_EQ(NAME_MIDDLE, field_type_map_[ASCIIToUTF16("name2")]);
168 ASSERT_TRUE(
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) {
174 FormFieldData field;
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_));
189 ASSERT_TRUE(
190 field_type_map_.find(ASCIIToUTF16("name1")) != field_type_map_.end());
191 EXPECT_EQ(NAME_FIRST, field_type_map_[ASCIIToUTF16("name1")]);
192 ASSERT_TRUE(
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) {
198 FormFieldData field;
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_));
217 ASSERT_TRUE(
218 field_type_map_.find(ASCIIToUTF16("name1")) != field_type_map_.end());
219 EXPECT_EQ(NAME_FIRST, field_type_map_[ASCIIToUTF16("name1")]);
220 ASSERT_TRUE(
221 field_type_map_.find(ASCIIToUTF16("name2")) != field_type_map_.end());
222 EXPECT_EQ(NAME_MIDDLE_INITIAL, field_type_map_[ASCIIToUTF16("name2")]);
223 ASSERT_TRUE(
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) {
229 FormFieldData field;
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_));
248 ASSERT_TRUE(
249 field_type_map_.find(ASCIIToUTF16("name1")) != field_type_map_.end());
250 EXPECT_EQ(NAME_FIRST, field_type_map_[ASCIIToUTF16("name1")]);
251 ASSERT_TRUE(
252 field_type_map_.find(ASCIIToUTF16("name2")) != field_type_map_.end());
253 EXPECT_EQ(NAME_MIDDLE_INITIAL, field_type_map_[ASCIIToUTF16("name2")]);
254 ASSERT_TRUE(
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) {
260 FormFieldData field;
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) {
279 FormFieldData field;
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_));
298 ASSERT_TRUE(
299 field_type_map_.find(ASCIIToUTF16("name1")) != field_type_map_.end());
300 EXPECT_EQ(NAME_FIRST, field_type_map_[ASCIIToUTF16("name1")]);
301 ASSERT_TRUE(
302 field_type_map_.find(ASCIIToUTF16("name2")) != field_type_map_.end());
303 EXPECT_EQ(NAME_MIDDLE_INITIAL, field_type_map_[ASCIIToUTF16("name2")]);
304 ASSERT_TRUE(
305 field_type_map_.find(ASCIIToUTF16("name3")) != field_type_map_.end());
306 EXPECT_EQ(NAME_LAST, field_type_map_[ASCIIToUTF16("name3")]);