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/phone_field.h"
11 #include "components/autofill/core/common/form_field_data.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 using base::ASCIIToUTF16
;
20 const char* const kFieldTypes
[] = {
28 class PhoneFieldTest
: public testing::Test
{
33 // Downcast for tests.
34 static scoped_ptr
<PhoneField
> Parse(AutofillScanner
* scanner
) {
35 scoped_ptr
<FormField
> field
= PhoneField::Parse(scanner
);
36 return make_scoped_ptr(static_cast<PhoneField
*>(field
.release()));
42 field_type_map_
.clear();
45 void CheckField(const std::string
& name
,
46 ServerFieldType expected_type
) const {
47 auto it
= field_type_map_
.find(ASCIIToUTF16(name
));
48 ASSERT_TRUE(it
!= field_type_map_
.end()) << name
;
49 EXPECT_EQ(expected_type
, it
->second
) << name
;
52 ScopedVector
<AutofillField
> list_
;
53 scoped_ptr
<PhoneField
> field_
;
54 ServerFieldTypeMap field_type_map_
;
57 DISALLOW_COPY_AND_ASSIGN(PhoneFieldTest
);
60 TEST_F(PhoneFieldTest
, Empty
) {
61 AutofillScanner
scanner(list_
.get());
62 field_
= Parse(&scanner
);
63 ASSERT_EQ(nullptr, field_
.get());
66 TEST_F(PhoneFieldTest
, NonParse
) {
67 list_
.push_back(new AutofillField
);
68 AutofillScanner
scanner(list_
.get());
69 field_
= Parse(&scanner
);
70 ASSERT_EQ(nullptr, field_
.get());
73 TEST_F(PhoneFieldTest
, ParseOneLinePhone
) {
76 for (size_t i
= 0; i
< arraysize(kFieldTypes
); ++i
) {
79 field
.form_control_type
= kFieldTypes
[i
];
80 field
.label
= ASCIIToUTF16("Phone");
81 field
.name
= ASCIIToUTF16("phone");
82 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("phone1")));
84 AutofillScanner
scanner(list_
.get());
85 field_
= Parse(&scanner
);
86 ASSERT_NE(nullptr, field_
.get());
87 ASSERT_TRUE(field_
->ClassifyField(&field_type_map_
));
88 CheckField("phone1", PHONE_HOME_WHOLE_NUMBER
);
92 TEST_F(PhoneFieldTest
, ParseTwoLinePhone
) {
95 for (size_t i
= 0; i
< arraysize(kFieldTypes
); ++i
) {
98 field
.form_control_type
= kFieldTypes
[i
];
99 field
.label
= ASCIIToUTF16("Area Code");
100 field
.name
= ASCIIToUTF16("area code");
101 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("areacode1")));
103 field
.label
= ASCIIToUTF16("Phone");
104 field
.name
= ASCIIToUTF16("phone");
105 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("phone2")));
107 AutofillScanner
scanner(list_
.get());
108 field_
= Parse(&scanner
);
109 ASSERT_NE(nullptr, field_
.get());
110 ASSERT_TRUE(field_
->ClassifyField(&field_type_map_
));
111 CheckField("areacode1", PHONE_HOME_CITY_CODE
);
112 CheckField("phone2", PHONE_HOME_NUMBER
);
116 TEST_F(PhoneFieldTest
, ThreePartPhoneNumber
) {
117 // Phone in format <field> - <field> - <field> could be either
118 // <area code> - <prefix> - <suffix>, or
119 // <country code> - <area code> - <phone>. The only distinguishing feature is
120 // size: <prefix> is no bigger than 3 characters, and <suffix> is no bigger
124 for (size_t i
= 0; i
< arraysize(kFieldTypes
); ++i
) {
127 field
.form_control_type
= kFieldTypes
[i
];
128 field
.label
= ASCIIToUTF16("Phone:");
129 field
.name
= ASCIIToUTF16("dayphone1");
130 field
.max_length
= 0;
131 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("areacode1")));
133 field
.label
= ASCIIToUTF16("-");
134 field
.name
= ASCIIToUTF16("dayphone2");
135 field
.max_length
= 3;
136 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("prefix2")));
138 field
.label
= ASCIIToUTF16("-");
139 field
.name
= ASCIIToUTF16("dayphone3");
140 field
.max_length
= 4;
141 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("suffix3")));
143 field
.label
= ASCIIToUTF16("ext.:");
144 field
.name
= ASCIIToUTF16("dayphone4");
145 field
.max_length
= 0;
146 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("ext4")));
148 AutofillScanner
scanner(list_
.get());
149 field_
= Parse(&scanner
);
150 ASSERT_NE(nullptr, field_
.get());
151 ASSERT_TRUE(field_
->ClassifyField(&field_type_map_
));
152 CheckField("areacode1", PHONE_HOME_CITY_CODE
);
153 CheckField("prefix2", PHONE_HOME_NUMBER
);
154 CheckField("suffix3", PHONE_HOME_NUMBER
);
155 EXPECT_FALSE(ContainsKey(field_type_map_
, ASCIIToUTF16("ext4")));
159 // This scenario of explicitly labeled "prefix" and "suffix" phone numbers
160 // encountered in http://crbug.com/40694 with page
161 // https://www.wrapables.com/jsp/Signup.jsp.
162 TEST_F(PhoneFieldTest
, ThreePartPhoneNumberPrefixSuffix
) {
165 for (size_t i
= 0; i
< arraysize(kFieldTypes
); ++i
) {
168 field
.form_control_type
= kFieldTypes
[i
];
169 field
.label
= ASCIIToUTF16("Phone:");
170 field
.name
= ASCIIToUTF16("area");
171 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("areacode1")));
173 field
.label
= base::string16();
174 field
.name
= ASCIIToUTF16("prefix");
175 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("prefix2")));
177 field
.label
= base::string16();
178 field
.name
= ASCIIToUTF16("suffix");
179 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("suffix3")));
181 AutofillScanner
scanner(list_
.get());
182 field_
= Parse(&scanner
);
183 ASSERT_NE(nullptr, field_
.get());
184 ASSERT_TRUE(field_
->ClassifyField(&field_type_map_
));
185 CheckField("areacode1", PHONE_HOME_CITY_CODE
);
186 CheckField("prefix2", PHONE_HOME_NUMBER
);
187 CheckField("suffix3", PHONE_HOME_NUMBER
);
191 TEST_F(PhoneFieldTest
, ThreePartPhoneNumberPrefixSuffix2
) {
194 for (size_t i
= 0; i
< arraysize(kFieldTypes
); ++i
) {
197 field
.form_control_type
= kFieldTypes
[i
];
198 field
.label
= ASCIIToUTF16("(");
199 field
.name
= ASCIIToUTF16("phone1");
200 field
.max_length
= 3;
201 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("phone1")));
203 field
.label
= ASCIIToUTF16(")");
204 field
.name
= ASCIIToUTF16("phone2");
205 field
.max_length
= 3;
206 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("phone2")));
208 field
.label
= base::string16();
209 field
.name
= ASCIIToUTF16("phone3");
210 field
.max_length
= 4;
211 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("phone3")));
213 AutofillScanner
scanner(list_
.get());
214 field_
= Parse(&scanner
);
215 ASSERT_NE(nullptr, field_
.get());
216 ASSERT_TRUE(field_
->ClassifyField(&field_type_map_
));
217 CheckField("phone1", PHONE_HOME_CITY_CODE
);
218 CheckField("phone2", PHONE_HOME_NUMBER
);
219 CheckField("phone3", PHONE_HOME_NUMBER
);
223 TEST_F(PhoneFieldTest
, CountryAndCityAndPhoneNumber
) {
224 // Phone in format <country code>:3 - <city and number>:10
225 // The |maxlength| is considered, otherwise it's too broad.
228 for (size_t i
= 0; i
< arraysize(kFieldTypes
); ++i
) {
231 field
.form_control_type
= kFieldTypes
[i
];
232 field
.label
= ASCIIToUTF16("Phone Number");
233 field
.name
= ASCIIToUTF16("CountryCode");
234 field
.max_length
= 3;
235 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("country")));
237 field
.label
= ASCIIToUTF16("Phone Number");
238 field
.name
= ASCIIToUTF16("PhoneNumber");
239 field
.max_length
= 10;
240 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("phone")));
242 AutofillScanner
scanner(list_
.get());
243 field_
= Parse(&scanner
);
244 ASSERT_NE(nullptr, field_
.get());
245 ASSERT_TRUE(field_
->ClassifyField(&field_type_map_
));
246 CheckField("country", PHONE_HOME_COUNTRY_CODE
);
247 CheckField("phone", PHONE_HOME_CITY_AND_NUMBER
);
251 } // namespace autofill