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/string16.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "components/autofill/core/browser/address_field.h"
10 #include "components/autofill/core/browser/autofill_field.h"
11 #include "components/autofill/core/browser/autofill_scanner.h"
12 #include "components/autofill/core/common/form_field_data.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 using base::ASCIIToUTF16
;
19 class AddressFieldTest
: public testing::Test
{
24 ScopedVector
<AutofillField
> list_
;
25 scoped_ptr
<AddressField
> field_
;
26 ServerFieldTypeMap field_type_map_
;
28 // Downcast for tests.
29 static scoped_ptr
<AddressField
> Parse(AutofillScanner
* scanner
) {
30 scoped_ptr
<FormField
> field
= AddressField::Parse(scanner
);
31 return make_scoped_ptr(static_cast<AddressField
*>(field
.release()));
35 DISALLOW_COPY_AND_ASSIGN(AddressFieldTest
);
38 TEST_F(AddressFieldTest
, Empty
) {
39 AutofillScanner
scanner(list_
.get());
40 field_
= Parse(&scanner
);
41 ASSERT_EQ(nullptr, field_
.get());
44 TEST_F(AddressFieldTest
, NonParse
) {
45 list_
.push_back(new AutofillField
);
46 AutofillScanner
scanner(list_
.get());
47 field_
= Parse(&scanner
);
48 ASSERT_EQ(nullptr, field_
.get());
51 TEST_F(AddressFieldTest
, ParseOneLineAddress
) {
53 field
.form_control_type
= "text";
55 field
.label
= ASCIIToUTF16("Address");
56 field
.name
= ASCIIToUTF16("address");
57 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("addr1")));
59 AutofillScanner
scanner(list_
.get());
60 field_
= Parse(&scanner
);
61 ASSERT_NE(nullptr, field_
.get());
62 ASSERT_TRUE(field_
->ClassifyField(&field_type_map_
));
64 field_type_map_
.find(ASCIIToUTF16("addr1")) != field_type_map_
.end());
65 EXPECT_EQ(ADDRESS_HOME_LINE1
, field_type_map_
[ASCIIToUTF16("addr1")]);
68 TEST_F(AddressFieldTest
, ParseTwoLineAddress
) {
70 field
.form_control_type
= "text";
72 field
.label
= ASCIIToUTF16("Address");
73 field
.name
= ASCIIToUTF16("address");
74 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("addr1")));
76 field
.label
= base::string16();
77 field
.name
= ASCIIToUTF16("address2");
78 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("addr2")));
80 AutofillScanner
scanner(list_
.get());
81 field_
= Parse(&scanner
);
82 ASSERT_NE(nullptr, field_
.get());
83 ASSERT_TRUE(field_
->ClassifyField(&field_type_map_
));
84 ASSERT_TRUE(field_type_map_
.find(ASCIIToUTF16("addr1")) !=
85 field_type_map_
.end());
86 EXPECT_EQ(ADDRESS_HOME_LINE1
, field_type_map_
[ASCIIToUTF16("addr1")]);
87 ASSERT_TRUE(field_type_map_
.find(ASCIIToUTF16("addr2")) !=
88 field_type_map_
.end());
89 EXPECT_EQ(ADDRESS_HOME_LINE2
, field_type_map_
[ASCIIToUTF16("addr2")]);
92 TEST_F(AddressFieldTest
, ParseThreeLineAddress
) {
94 field
.form_control_type
= "text";
96 field
.label
= ASCIIToUTF16("Address Line1");
97 field
.name
= ASCIIToUTF16("Address1");
98 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("addr1")));
100 field
.label
= ASCIIToUTF16("Address Line2");
101 field
.name
= ASCIIToUTF16("Address2");
102 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("addr2")));
104 field
.label
= ASCIIToUTF16("Address Line3");
105 field
.name
= ASCIIToUTF16("Address3");
106 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("addr3")));
108 AutofillScanner
scanner(list_
.get());
109 field_
= Parse(&scanner
);
110 ASSERT_NE(nullptr, field_
.get());
111 ASSERT_TRUE(field_
->ClassifyField(&field_type_map_
));
113 field_type_map_
.find(ASCIIToUTF16("addr1")) != field_type_map_
.end());
114 EXPECT_EQ(ADDRESS_HOME_LINE1
, field_type_map_
[ASCIIToUTF16("addr1")]);
116 field_type_map_
.find(ASCIIToUTF16("addr2")) != field_type_map_
.end());
117 EXPECT_EQ(ADDRESS_HOME_LINE2
, field_type_map_
[ASCIIToUTF16("addr2")]);
119 field_type_map_
.find(ASCIIToUTF16("addr3")) != field_type_map_
.end());
120 EXPECT_EQ(ADDRESS_HOME_LINE3
, field_type_map_
[ASCIIToUTF16("addr3")]);
123 TEST_F(AddressFieldTest
, ParseStreetAddressFromTextArea
) {
125 field
.form_control_type
= "textarea";
127 field
.label
= ASCIIToUTF16("Address");
128 field
.name
= ASCIIToUTF16("address");
129 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("addr")));
131 AutofillScanner
scanner(list_
.get());
132 field_
= Parse(&scanner
);
133 ASSERT_NE(nullptr, field_
.get());
134 ASSERT_TRUE(field_
->ClassifyField(&field_type_map_
));
136 field_type_map_
.find(ASCIIToUTF16("addr")) != field_type_map_
.end());
137 EXPECT_EQ(ADDRESS_HOME_STREET_ADDRESS
, field_type_map_
[ASCIIToUTF16("addr")]);
140 TEST_F(AddressFieldTest
, ParseCity
) {
142 field
.form_control_type
= "text";
144 field
.label
= ASCIIToUTF16("City");
145 field
.name
= ASCIIToUTF16("city");
146 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("city1")));
148 AutofillScanner
scanner(list_
.get());
149 field_
= Parse(&scanner
);
150 ASSERT_NE(nullptr, field_
.get());
151 ASSERT_TRUE(field_
->ClassifyField(&field_type_map_
));
153 field_type_map_
.find(ASCIIToUTF16("city1")) != field_type_map_
.end());
154 EXPECT_EQ(ADDRESS_HOME_CITY
, field_type_map_
[ASCIIToUTF16("city1")]);
157 TEST_F(AddressFieldTest
, ParseState
) {
159 field
.form_control_type
= "text";
161 field
.label
= ASCIIToUTF16("State");
162 field
.name
= ASCIIToUTF16("state");
163 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("state1")));
165 AutofillScanner
scanner(list_
.get());
166 field_
= Parse(&scanner
);
167 ASSERT_NE(nullptr, field_
.get());
168 ASSERT_TRUE(field_
->ClassifyField(&field_type_map_
));
170 field_type_map_
.find(ASCIIToUTF16("state1")) != field_type_map_
.end());
171 EXPECT_EQ(ADDRESS_HOME_STATE
, field_type_map_
[ASCIIToUTF16("state1")]);
174 TEST_F(AddressFieldTest
, ParseZip
) {
176 field
.form_control_type
= "text";
178 field
.label
= ASCIIToUTF16("Zip");
179 field
.name
= ASCIIToUTF16("zip");
180 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("zip1")));
182 AutofillScanner
scanner(list_
.get());
183 field_
= Parse(&scanner
);
184 ASSERT_NE(nullptr, field_
.get());
185 ASSERT_TRUE(field_
->ClassifyField(&field_type_map_
));
187 field_type_map_
.find(ASCIIToUTF16("zip1")) != field_type_map_
.end());
188 EXPECT_EQ(ADDRESS_HOME_ZIP
, field_type_map_
[ASCIIToUTF16("zip1")]);
191 TEST_F(AddressFieldTest
, ParseStateAndZipOneLabel
) {
193 field
.form_control_type
= "text";
195 field
.label
= ASCIIToUTF16("State/Province, Zip/Postal Code");
196 field
.name
= ASCIIToUTF16("state");
197 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("state")));
199 field
.label
= ASCIIToUTF16("State/Province, Zip/Postal Code");
200 field
.name
= ASCIIToUTF16("zip");
201 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("zip")));
203 AutofillScanner
scanner(list_
.get());
204 field_
= Parse(&scanner
);
205 ASSERT_NE(nullptr, field_
.get());
206 ASSERT_TRUE(field_
->ClassifyField(&field_type_map_
));
208 field_type_map_
.find(ASCIIToUTF16("state")) != field_type_map_
.end());
209 EXPECT_EQ(ADDRESS_HOME_STATE
, field_type_map_
[ASCIIToUTF16("state")]);
211 field_type_map_
.find(ASCIIToUTF16("zip")) != field_type_map_
.end());
212 EXPECT_EQ(ADDRESS_HOME_ZIP
, field_type_map_
[ASCIIToUTF16("zip")]);
215 TEST_F(AddressFieldTest
, ParseCountry
) {
217 field
.form_control_type
= "text";
219 field
.label
= ASCIIToUTF16("Country");
220 field
.name
= ASCIIToUTF16("country");
221 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("country1")));
223 AutofillScanner
scanner(list_
.get());
224 field_
= Parse(&scanner
);
225 ASSERT_NE(nullptr, field_
.get());
226 ASSERT_TRUE(field_
->ClassifyField(&field_type_map_
));
228 field_type_map_
.find(ASCIIToUTF16("country1")) != field_type_map_
.end());
229 EXPECT_EQ(ADDRESS_HOME_COUNTRY
, field_type_map_
[ASCIIToUTF16("country1")]);
232 TEST_F(AddressFieldTest
, ParseCompany
) {
234 field
.form_control_type
= "text";
236 field
.label
= ASCIIToUTF16("Company");
237 field
.name
= ASCIIToUTF16("company");
238 list_
.push_back(new AutofillField(field
, ASCIIToUTF16("company1")));
240 AutofillScanner
scanner(list_
.get());
241 field_
= Parse(&scanner
);
242 ASSERT_NE(nullptr, field_
.get());
243 ASSERT_TRUE(field_
->ClassifyField(&field_type_map_
));
245 field_type_map_
.find(ASCIIToUTF16("company1")) != field_type_map_
.end());
246 EXPECT_EQ(COMPANY_NAME
, field_type_map_
[ASCIIToUTF16("company1")]);
249 } // namespace autofill