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
;
18 class PhoneFieldTest
: public testing::Test
{
23 ScopedVector
<AutofillField
> list_
;
24 scoped_ptr
<PhoneField
> field_
;
25 ServerFieldTypeMap field_type_map_
;
27 // Downcast for tests.
28 static scoped_ptr
<PhoneField
> Parse(AutofillScanner
* scanner
) {
29 scoped_ptr
<FormField
> field
= PhoneField::Parse(scanner
);
30 return make_scoped_ptr(static_cast<PhoneField
*>(field
.release()));
34 DISALLOW_COPY_AND_ASSIGN(PhoneFieldTest
);
37 TEST_F(PhoneFieldTest
, Empty
) {
38 AutofillScanner
scanner(list_
.get());
39 field_
= Parse(&scanner
);
40 ASSERT_EQ(nullptr, field_
.get());
43 TEST_F(PhoneFieldTest
, NonParse
) {
44 list_
.push_back(new AutofillField
);
45 AutofillScanner
scanner(list_
.get());
46 field_
= Parse(&scanner
);
47 ASSERT_EQ(nullptr, field_
.get());
50 TEST_F(PhoneFieldTest
, ParseOneLinePhone
) {
52 field
.form_control_type
= "text";
54 field
.label
= ASCIIToUTF16("Phone");
55 field
.name
= ASCIIToUTF16("phone");
56 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("phone1")));
58 AutofillScanner
scanner(list_
.get());
59 field_
= Parse(&scanner
);
60 ASSERT_NE(nullptr, field_
.get());
61 ASSERT_TRUE(field_
->ClassifyField(&field_type_map_
));
63 field_type_map_
.find(ASCIIToUTF16("phone1")) != field_type_map_
.end());
64 EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER
, field_type_map_
[ASCIIToUTF16("phone1")]);
67 TEST_F(PhoneFieldTest
, ParseTwoLinePhone
) {
69 field
.form_control_type
= "text";
71 field
.label
= ASCIIToUTF16("Area Code");
72 field
.name
= ASCIIToUTF16("area code");
73 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("areacode1")));
75 field
.label
= ASCIIToUTF16("Phone");
76 field
.name
= ASCIIToUTF16("phone");
77 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("phone2")));
79 AutofillScanner
scanner(list_
.get());
80 field_
= Parse(&scanner
);
81 ASSERT_NE(nullptr, field_
.get());
82 ASSERT_TRUE(field_
->ClassifyField(&field_type_map_
));
84 field_type_map_
.find(ASCIIToUTF16("areacode1")) != field_type_map_
.end());
85 EXPECT_EQ(PHONE_HOME_CITY_CODE
, field_type_map_
[ASCIIToUTF16("areacode1")]);
87 field_type_map_
.find(ASCIIToUTF16("phone2")) != field_type_map_
.end());
88 EXPECT_EQ(PHONE_HOME_NUMBER
, field_type_map_
[ASCIIToUTF16("phone2")]);
91 TEST_F(PhoneFieldTest
, ThreePartPhoneNumber
) {
92 // Phone in format <field> - <field> - <field> could be either
93 // <area code> - <prefix> - <suffix>, or
94 // <country code> - <area code> - <phone>. The only distinguishing feature is
95 // size: <prefix> is no bigger than 3 characters, and <suffix> is no bigger
98 field
.form_control_type
= "text";
100 field
.label
= ASCIIToUTF16("Phone:");
101 field
.name
= ASCIIToUTF16("dayphone1");
102 field
.max_length
= 0;
103 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("areacode1")));
105 field
.label
= ASCIIToUTF16("-");
106 field
.name
= ASCIIToUTF16("dayphone2");
107 field
.max_length
= 3;
108 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("prefix2")));
110 field
.label
= ASCIIToUTF16("-");
111 field
.name
= ASCIIToUTF16("dayphone3");
112 field
.max_length
= 4;
113 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("suffix3")));
115 field
.label
= ASCIIToUTF16("ext.:");
116 field
.name
= ASCIIToUTF16("dayphone4");
117 field
.max_length
= 0;
118 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("ext4")));
120 AutofillScanner
scanner(list_
.get());
121 field_
= Parse(&scanner
);
122 ASSERT_NE(nullptr, field_
.get());
123 ASSERT_TRUE(field_
->ClassifyField(&field_type_map_
));
125 field_type_map_
.find(ASCIIToUTF16("areacode1")) != field_type_map_
.end());
126 EXPECT_EQ(PHONE_HOME_CITY_CODE
, field_type_map_
[ASCIIToUTF16("areacode1")]);
128 field_type_map_
.find(ASCIIToUTF16("prefix2")) != field_type_map_
.end());
129 EXPECT_EQ(PHONE_HOME_NUMBER
, field_type_map_
[ASCIIToUTF16("prefix2")]);
131 field_type_map_
.find(ASCIIToUTF16("suffix3")) != field_type_map_
.end());
132 EXPECT_EQ(PHONE_HOME_NUMBER
, field_type_map_
[ASCIIToUTF16("suffix3")]);
134 field_type_map_
.find(ASCIIToUTF16("ext4")) == field_type_map_
.end());
137 // This scenario of explicitly labeled "prefix" and "suffix" phone numbers
138 // encountered in http://crbug.com/40694 with page
139 // https://www.wrapables.com/jsp/Signup.jsp.
140 TEST_F(PhoneFieldTest
, ThreePartPhoneNumberPrefixSuffix
) {
142 field
.form_control_type
= "text";
144 field
.label
= ASCIIToUTF16("Phone:");
145 field
.name
= ASCIIToUTF16("area");
146 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("areacode1")));
148 field
.label
= base::string16();
149 field
.name
= ASCIIToUTF16("prefix");
150 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("prefix2")));
152 field
.label
= base::string16();
153 field
.name
= ASCIIToUTF16("suffix");
154 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("suffix3")));
156 AutofillScanner
scanner(list_
.get());
157 field_
= Parse(&scanner
);
158 ASSERT_NE(nullptr, field_
.get());
159 ASSERT_TRUE(field_
->ClassifyField(&field_type_map_
));
161 field_type_map_
.find(ASCIIToUTF16("areacode1")) != field_type_map_
.end());
162 EXPECT_EQ(PHONE_HOME_CITY_CODE
, field_type_map_
[ASCIIToUTF16("areacode1")]);
164 field_type_map_
.find(ASCIIToUTF16("prefix2")) != field_type_map_
.end());
165 EXPECT_EQ(PHONE_HOME_NUMBER
, field_type_map_
[ASCIIToUTF16("prefix2")]);
167 field_type_map_
.find(ASCIIToUTF16("suffix3")) != field_type_map_
.end());
168 EXPECT_EQ(PHONE_HOME_NUMBER
, field_type_map_
[ASCIIToUTF16("suffix3")]);
171 TEST_F(PhoneFieldTest
, ThreePartPhoneNumberPrefixSuffix2
) {
173 field
.form_control_type
= "text";
175 field
.label
= ASCIIToUTF16("(");
176 field
.name
= ASCIIToUTF16("phone1");
177 field
.max_length
= 3;
178 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("phone1")));
180 field
.label
= ASCIIToUTF16(")");
181 field
.name
= ASCIIToUTF16("phone2");
182 field
.max_length
= 3;
183 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("phone2")));
185 field
.label
= base::string16();
186 field
.name
= ASCIIToUTF16("phone3");
187 field
.max_length
= 4;
188 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("phone3")));
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("phone1")) != field_type_map_
.end());
196 EXPECT_EQ(PHONE_HOME_CITY_CODE
, field_type_map_
[ASCIIToUTF16("phone1")]);
198 field_type_map_
.find(ASCIIToUTF16("phone2")) != field_type_map_
.end());
199 EXPECT_EQ(PHONE_HOME_NUMBER
, field_type_map_
[ASCIIToUTF16("phone2")]);
201 field_type_map_
.find(ASCIIToUTF16("phone3")) != field_type_map_
.end());
202 EXPECT_EQ(PHONE_HOME_NUMBER
, field_type_map_
[ASCIIToUTF16("phone3")]);
205 TEST_F(PhoneFieldTest
, CountryAndCityAndPhoneNumber
) {
206 // Phone in format <country code>:3 - <city and number>:10
207 // The |maxlength| is considered, otherwise it's too broad.
209 field
.form_control_type
= "text";
211 field
.label
= ASCIIToUTF16("Phone Number");
212 field
.name
= ASCIIToUTF16("CountryCode");
213 field
.max_length
= 3;
214 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("country")));
216 field
.label
= ASCIIToUTF16("Phone Number");
217 field
.name
= ASCIIToUTF16("PhoneNumber");
218 field
.max_length
= 10;
219 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("phone")));
221 AutofillScanner
scanner(list_
.get());
222 field_
= Parse(&scanner
);
223 ASSERT_NE(nullptr, field_
.get());
224 ASSERT_TRUE(field_
->ClassifyField(&field_type_map_
));
226 field_type_map_
.find(ASCIIToUTF16("country")) != field_type_map_
.end());
227 EXPECT_EQ(PHONE_HOME_COUNTRY_CODE
, field_type_map_
[ASCIIToUTF16("country")]);
229 field_type_map_
.find(ASCIIToUTF16("phone")) != field_type_map_
.end());
230 EXPECT_EQ(PHONE_HOME_CITY_AND_NUMBER
, field_type_map_
[ASCIIToUTF16("phone")]);
233 } // namespace autofill