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.
7 #include "base/strings/string16.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "components/autofill/core/browser/address.h"
10 #include "components/autofill/core/browser/autofill_type.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 using base::ASCIIToUTF16
;
17 // Test that country data can be properly returned as either a country code or a
18 // localized country name.
19 TEST(AddressTest
, GetCountry
) {
21 EXPECT_EQ(base::string16(), address
.GetRawInfo(ADDRESS_HOME_COUNTRY
));
23 // Make sure that nothing breaks when the country code is missing.
24 base::string16 country
=
25 address
.GetInfo(AutofillType(ADDRESS_HOME_COUNTRY
), "en-US");
26 EXPECT_EQ(base::string16(), country
);
29 AutofillType(ADDRESS_HOME_COUNTRY
), ASCIIToUTF16("US"), "en-US");
30 country
= address
.GetInfo(AutofillType(ADDRESS_HOME_COUNTRY
), "en-US");
31 EXPECT_EQ(ASCIIToUTF16("United States"), country
);
32 country
= address
.GetInfo(
33 AutofillType(HTML_TYPE_COUNTRY_NAME
, HTML_MODE_NONE
), "en-US");
34 EXPECT_EQ(ASCIIToUTF16("United States"), country
);
35 country
= address
.GetInfo(
36 AutofillType(HTML_TYPE_COUNTRY_CODE
, HTML_MODE_NONE
), "en-US");
37 EXPECT_EQ(ASCIIToUTF16("US"), country
);
39 address
.SetRawInfo(ADDRESS_HOME_COUNTRY
, ASCIIToUTF16("CA"));
40 country
= address
.GetInfo(AutofillType(ADDRESS_HOME_COUNTRY
), "en-US");
41 EXPECT_EQ(ASCIIToUTF16("Canada"), country
);
42 country
= address
.GetInfo(
43 AutofillType(HTML_TYPE_COUNTRY_NAME
, HTML_MODE_NONE
), "en-US");
44 EXPECT_EQ(ASCIIToUTF16("Canada"), country
);
45 country
= address
.GetInfo(
46 AutofillType(HTML_TYPE_COUNTRY_CODE
, HTML_MODE_NONE
), "en-US");
47 EXPECT_EQ(ASCIIToUTF16("CA"), country
);
50 // Test that we properly detect country codes appropriate for each country.
51 TEST(AddressTest
, SetCountry
) {
53 EXPECT_EQ(base::string16(), address
.GetRawInfo(ADDRESS_HOME_COUNTRY
));
55 // Test basic conversion.
57 AutofillType(ADDRESS_HOME_COUNTRY
), ASCIIToUTF16("United States"),
59 base::string16 country
=
60 address
.GetInfo(AutofillType(ADDRESS_HOME_COUNTRY
), "en-US");
61 EXPECT_EQ(ASCIIToUTF16("US"), address
.GetRawInfo(ADDRESS_HOME_COUNTRY
));
62 EXPECT_EQ(ASCIIToUTF16("United States"), country
);
64 // Test basic synonym detection.
66 AutofillType(ADDRESS_HOME_COUNTRY
), ASCIIToUTF16("USA"), "en-US");
67 country
= address
.GetInfo(AutofillType(ADDRESS_HOME_COUNTRY
), "en-US");
68 EXPECT_EQ(ASCIIToUTF16("US"), address
.GetRawInfo(ADDRESS_HOME_COUNTRY
));
69 EXPECT_EQ(ASCIIToUTF16("United States"), country
);
71 // Test case-insensitivity.
73 AutofillType(ADDRESS_HOME_COUNTRY
), ASCIIToUTF16("canADA"), "en-US");
74 country
= address
.GetInfo(AutofillType(ADDRESS_HOME_COUNTRY
), "en-US");
75 EXPECT_EQ(ASCIIToUTF16("CA"), address
.GetRawInfo(ADDRESS_HOME_COUNTRY
));
76 EXPECT_EQ(ASCIIToUTF16("Canada"), country
);
78 // Test country code detection.
80 AutofillType(ADDRESS_HOME_COUNTRY
), ASCIIToUTF16("JP"), "en-US");
81 country
= address
.GetInfo(AutofillType(ADDRESS_HOME_COUNTRY
), "en-US");
82 EXPECT_EQ(ASCIIToUTF16("JP"), address
.GetRawInfo(ADDRESS_HOME_COUNTRY
));
83 EXPECT_EQ(ASCIIToUTF16("Japan"), country
);
85 // Test that we ignore unknown countries.
87 AutofillType(ADDRESS_HOME_COUNTRY
), ASCIIToUTF16("Unknown"), "en-US");
88 country
= address
.GetInfo(AutofillType(ADDRESS_HOME_COUNTRY
), "en-US");
89 EXPECT_EQ(base::string16(), address
.GetRawInfo(ADDRESS_HOME_COUNTRY
));
90 EXPECT_EQ(base::string16(), country
);
92 // Test setting the country based on an HTML field type.
93 AutofillType html_type_country_code
=
94 AutofillType(HTML_TYPE_COUNTRY_CODE
, HTML_MODE_NONE
);
95 address
.SetInfo(html_type_country_code
, ASCIIToUTF16("US"), "en-US");
96 country
= address
.GetInfo(AutofillType(ADDRESS_HOME_COUNTRY
), "en-US");
97 EXPECT_EQ(ASCIIToUTF16("US"), address
.GetRawInfo(ADDRESS_HOME_COUNTRY
));
98 EXPECT_EQ(ASCIIToUTF16("United States"), country
);
100 // Test case-insensitivity when setting the country based on an HTML field
102 address
.SetInfo(html_type_country_code
, ASCIIToUTF16("cA"), "en-US");
103 country
= address
.GetInfo(AutofillType(ADDRESS_HOME_COUNTRY
), "en-US");
104 EXPECT_EQ(ASCIIToUTF16("CA"), address
.GetRawInfo(ADDRESS_HOME_COUNTRY
));
105 EXPECT_EQ(ASCIIToUTF16("Canada"), country
);
107 // Test setting the country based on invalid data with an HTML field type.
108 address
.SetInfo(html_type_country_code
, ASCIIToUTF16("unknown"), "en-US");
109 country
= address
.GetInfo(AutofillType(ADDRESS_HOME_COUNTRY
), "en-US");
110 EXPECT_EQ(base::string16(), address
.GetRawInfo(ADDRESS_HOME_COUNTRY
));
111 EXPECT_EQ(base::string16(), country
);
114 // Test that we properly match typed values to stored country data.
115 TEST(AddressTest
, IsCountry
) {
117 address
.SetRawInfo(ADDRESS_HOME_COUNTRY
, ASCIIToUTF16("US"));
119 const char* const kValidMatches
[] = {
126 for (size_t i
= 0; i
< arraysize(kValidMatches
); ++i
) {
127 SCOPED_TRACE(kValidMatches
[i
]);
128 ServerFieldTypeSet matching_types
;
129 address
.GetMatchingTypes(ASCIIToUTF16(kValidMatches
[i
]), "US",
131 ASSERT_EQ(1U, matching_types
.size());
132 EXPECT_EQ(ADDRESS_HOME_COUNTRY
, *matching_types
.begin());
135 const char* const kInvalidMatches
[] = {
139 for (size_t i
= 0; i
< arraysize(kInvalidMatches
); ++i
) {
140 ServerFieldTypeSet matching_types
;
141 address
.GetMatchingTypes(ASCIIToUTF16(kInvalidMatches
[i
]), "US",
143 EXPECT_EQ(0U, matching_types
.size());
146 // Make sure that garbage values don't match when the country code is empty.
147 address
.SetRawInfo(ADDRESS_HOME_COUNTRY
, base::string16());
148 EXPECT_EQ(base::string16(), address
.GetRawInfo(ADDRESS_HOME_COUNTRY
));
149 ServerFieldTypeSet matching_types
;
150 address
.GetMatchingTypes(ASCIIToUTF16("Garbage"), "US", &matching_types
);
151 EXPECT_EQ(0U, matching_types
.size());
154 // Verifies that Address::GetInfo() correctly combines address lines.
155 TEST(AddressTest
, GetStreetAddress
) {
156 const AutofillType type
= AutofillType(ADDRESS_HOME_STREET_ADDRESS
);
158 // Address has no address lines.
160 EXPECT_TRUE(address
.GetRawInfo(ADDRESS_HOME_LINE1
).empty());
161 EXPECT_TRUE(address
.GetRawInfo(ADDRESS_HOME_LINE2
).empty());
162 EXPECT_TRUE(address
.GetRawInfo(ADDRESS_HOME_LINE3
).empty());
163 EXPECT_EQ(base::string16(), address
.GetInfo(type
, "en-US"));
165 // Address has only line 1.
166 address
.SetRawInfo(ADDRESS_HOME_LINE1
, ASCIIToUTF16("123 Example Ave."));
167 EXPECT_FALSE(address
.GetRawInfo(ADDRESS_HOME_LINE1
).empty());
168 EXPECT_TRUE(address
.GetRawInfo(ADDRESS_HOME_LINE2
).empty());
169 EXPECT_TRUE(address
.GetRawInfo(ADDRESS_HOME_LINE3
).empty());
170 EXPECT_EQ(ASCIIToUTF16("123 Example Ave."), address
.GetInfo(type
, "en-US"));
172 // Address has only line 2.
173 address
.SetRawInfo(ADDRESS_HOME_LINE1
, base::string16());
174 address
.SetRawInfo(ADDRESS_HOME_LINE2
, ASCIIToUTF16("Apt 42."));
175 EXPECT_TRUE(address
.GetRawInfo(ADDRESS_HOME_LINE1
).empty());
176 EXPECT_FALSE(address
.GetRawInfo(ADDRESS_HOME_LINE2
).empty());
177 EXPECT_TRUE(address
.GetRawInfo(ADDRESS_HOME_LINE3
).empty());
178 EXPECT_EQ(ASCIIToUTF16("\nApt 42."), address
.GetInfo(type
, "en-US"));
180 // Address has lines 1 and 2.
181 address
.SetRawInfo(ADDRESS_HOME_LINE1
, ASCIIToUTF16("123 Example Ave."));
182 address
.SetRawInfo(ADDRESS_HOME_LINE2
, ASCIIToUTF16("Apt. 42"));
183 EXPECT_FALSE(address
.GetRawInfo(ADDRESS_HOME_LINE1
).empty());
184 EXPECT_FALSE(address
.GetRawInfo(ADDRESS_HOME_LINE2
).empty());
185 EXPECT_TRUE(address
.GetRawInfo(ADDRESS_HOME_LINE3
).empty());
186 EXPECT_EQ(ASCIIToUTF16("123 Example Ave.\n"
188 address
.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS
));
189 EXPECT_EQ(ASCIIToUTF16("123 Example Ave.\n"
191 address
.GetInfo(type
, "en-US"));
193 // A wild third line appears.
194 address
.SetRawInfo(ADDRESS_HOME_LINE3
, ASCIIToUTF16("Living room couch"));
195 EXPECT_EQ(ASCIIToUTF16("Living room couch"),
196 address
.GetRawInfo(ADDRESS_HOME_LINE3
));
197 EXPECT_EQ(ASCIIToUTF16("123 Example Ave.\n"
199 "Living room couch"),
200 address
.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS
));
202 // The second line vanishes.
203 address
.SetRawInfo(ADDRESS_HOME_LINE2
, base::string16());
204 EXPECT_FALSE(address
.GetRawInfo(ADDRESS_HOME_LINE1
).empty());
205 EXPECT_TRUE(address
.GetRawInfo(ADDRESS_HOME_LINE2
).empty());
206 EXPECT_FALSE(address
.GetRawInfo(ADDRESS_HOME_LINE3
).empty());
207 EXPECT_EQ(ASCIIToUTF16("123 Example Ave.\n"
209 "Living room couch"),
210 address
.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS
));
213 // Verifies that overwriting an address with N lines with one that has fewer
214 // than N lines does not result in an address with blank lines at the end.
215 TEST(AddressTest
, GetStreetAddressAfterOverwritingLongAddressWithShorterOne
) {
216 // Start with an address that has two lines.
218 address
.SetRawInfo(ADDRESS_HOME_LINE1
, ASCIIToUTF16("123 Example Ave."));
219 address
.SetRawInfo(ADDRESS_HOME_LINE2
, ASCIIToUTF16("Apt. 42"));
221 // Now clear out the second address line.
222 address
.SetRawInfo(ADDRESS_HOME_LINE2
, base::string16());
223 EXPECT_EQ(ASCIIToUTF16("123 Example Ave."),
224 address
.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS
));
226 // Now clear out the first address line as well.
227 address
.SetRawInfo(ADDRESS_HOME_LINE1
, base::string16());
228 EXPECT_EQ(base::string16(), address
.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS
));
231 // Verifies that Address::SetRawInfo() is able to split address lines correctly.
232 TEST(AddressTest
, SetRawStreetAddress
) {
233 const base::string16 empty_street_address
;
234 const base::string16 short_street_address
= ASCIIToUTF16("456 Nowhere Ln.");
235 const base::string16 long_street_address
=
236 ASCIIToUTF16("123 Example Ave.\n"
238 "(The one with the blue door)");
241 EXPECT_EQ(base::string16(), address
.GetRawInfo(ADDRESS_HOME_LINE1
));
242 EXPECT_EQ(base::string16(), address
.GetRawInfo(ADDRESS_HOME_LINE2
));
244 address
.SetRawInfo(ADDRESS_HOME_STREET_ADDRESS
, long_street_address
);
245 EXPECT_EQ(ASCIIToUTF16("123 Example Ave."),
246 address
.GetRawInfo(ADDRESS_HOME_LINE1
));
247 EXPECT_EQ(ASCIIToUTF16("Apt. 42"), address
.GetRawInfo(ADDRESS_HOME_LINE2
));
248 EXPECT_EQ(long_street_address
,
249 address
.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS
));
251 // A short address should clear out unused address lines.
252 address
.SetRawInfo(ADDRESS_HOME_STREET_ADDRESS
, short_street_address
);
253 EXPECT_EQ(ASCIIToUTF16("456 Nowhere Ln."),
254 address
.GetRawInfo(ADDRESS_HOME_LINE1
));
255 EXPECT_EQ(base::string16(), address
.GetRawInfo(ADDRESS_HOME_LINE2
));
257 // An empty address should clear out all address lines.
258 address
.SetRawInfo(ADDRESS_HOME_STREET_ADDRESS
, long_street_address
);
259 address
.SetRawInfo(ADDRESS_HOME_STREET_ADDRESS
, empty_street_address
);
260 EXPECT_EQ(base::string16(), address
.GetRawInfo(ADDRESS_HOME_LINE1
));
261 EXPECT_EQ(base::string16(), address
.GetRawInfo(ADDRESS_HOME_LINE2
));
264 // Street addresses should be set properly.
265 TEST(AddressTest
, SetStreetAddress
) {
266 const base::string16 empty_street_address
;
267 const base::string16 multi_line_street_address
=
268 ASCIIToUTF16("789 Fancy Pkwy.\n"
271 const base::string16 single_line_street_address
=
272 ASCIIToUTF16("123 Main, Apt 7");
273 const AutofillType type
= AutofillType(ADDRESS_HOME_STREET_ADDRESS
);
275 // Start with a non-empty address.
277 address
.SetRawInfo(ADDRESS_HOME_LINE1
, ASCIIToUTF16("123 Example Ave."));
278 address
.SetRawInfo(ADDRESS_HOME_LINE2
, ASCIIToUTF16("Apt. 42"));
279 address
.SetRawInfo(ADDRESS_HOME_LINE3
, ASCIIToUTF16("and a half"));
280 EXPECT_FALSE(address
.GetRawInfo(ADDRESS_HOME_LINE1
).empty());
281 EXPECT_FALSE(address
.GetRawInfo(ADDRESS_HOME_LINE2
).empty());
282 EXPECT_FALSE(address
.GetRawInfo(ADDRESS_HOME_LINE3
).empty());
284 // Attempting to set a multi-line address should succeed.
285 EXPECT_TRUE(address
.SetInfo(type
, multi_line_street_address
, "en-US"));
286 EXPECT_EQ(ASCIIToUTF16("789 Fancy Pkwy."),
287 address
.GetRawInfo(ADDRESS_HOME_LINE1
));
288 EXPECT_EQ(ASCIIToUTF16("Unit 3.14"), address
.GetRawInfo(ADDRESS_HOME_LINE2
));
289 EXPECT_EQ(ASCIIToUTF16("Box 9"), address
.GetRawInfo(ADDRESS_HOME_LINE3
));
291 // Setting a single line street address should clear out subsequent lines.
292 EXPECT_TRUE(address
.SetInfo(type
, single_line_street_address
, "en-US"));
293 EXPECT_EQ(single_line_street_address
, address
.GetRawInfo(ADDRESS_HOME_LINE1
));
294 EXPECT_EQ(base::string16(), address
.GetRawInfo(ADDRESS_HOME_LINE2
));
295 EXPECT_EQ(base::string16(), address
.GetRawInfo(ADDRESS_HOME_LINE3
));
297 // Attempting to set an empty address should also succeed, and clear out the
298 // previously stored data.
299 EXPECT_TRUE(address
.SetInfo(type
, multi_line_street_address
, "en-US"));
300 EXPECT_FALSE(address
.GetRawInfo(ADDRESS_HOME_LINE1
).empty());
301 EXPECT_FALSE(address
.GetRawInfo(ADDRESS_HOME_LINE2
).empty());
302 EXPECT_FALSE(address
.GetRawInfo(ADDRESS_HOME_LINE3
).empty());
303 EXPECT_TRUE(address
.SetInfo(type
, empty_street_address
, "en-US"));
304 EXPECT_EQ(base::string16(), address
.GetRawInfo(ADDRESS_HOME_LINE1
));
305 EXPECT_EQ(base::string16(), address
.GetRawInfo(ADDRESS_HOME_LINE2
));
306 EXPECT_EQ(base::string16(), address
.GetRawInfo(ADDRESS_HOME_LINE3
));
309 // Verifies that Address::SetInfio() rejects setting data for
310 // ADDRESS_HOME_STREET_ADDRESS if the data has any interior blank lines.
311 TEST(AddressTest
, SetStreetAddressRejectsAddressesWithInteriorBlankLines
) {
312 // Start with a non-empty address.
314 address
.SetRawInfo(ADDRESS_HOME_LINE1
, ASCIIToUTF16("123 Example Ave."));
315 address
.SetRawInfo(ADDRESS_HOME_LINE2
, ASCIIToUTF16("Apt. 42"));
316 EXPECT_FALSE(address
.GetRawInfo(ADDRESS_HOME_LINE1
).empty());
317 EXPECT_FALSE(address
.GetRawInfo(ADDRESS_HOME_LINE2
).empty());
318 EXPECT_FALSE(address
.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS
).empty());
320 // Attempting to set an address with interior blank lines should fail, and
321 // clear out the previously stored address.
322 EXPECT_FALSE(address
.SetInfo(AutofillType(ADDRESS_HOME_STREET_ADDRESS
),
323 ASCIIToUTF16("Address line 1\n"
327 EXPECT_EQ(base::string16(), address
.GetRawInfo(ADDRESS_HOME_LINE1
));
328 EXPECT_EQ(base::string16(), address
.GetRawInfo(ADDRESS_HOME_LINE2
));
329 EXPECT_EQ(base::string16(), address
.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS
));
332 // Verifies that Address::SetInfio() rejects setting data for
333 // ADDRESS_HOME_STREET_ADDRESS if the data has any leading blank lines.
334 TEST(AddressTest
, SetStreetAddressRejectsAddressesWithLeadingBlankLines
) {
335 // Start with a non-empty address.
337 address
.SetRawInfo(ADDRESS_HOME_LINE1
, ASCIIToUTF16("123 Example Ave."));
338 address
.SetRawInfo(ADDRESS_HOME_LINE2
, ASCIIToUTF16("Apt. 42"));
339 EXPECT_FALSE(address
.GetRawInfo(ADDRESS_HOME_LINE1
).empty());
340 EXPECT_FALSE(address
.GetRawInfo(ADDRESS_HOME_LINE2
).empty());
341 EXPECT_FALSE(address
.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS
).empty());
343 // Attempting to set an address with leading blank lines should fail, and
344 // clear out the previously stored address.
345 EXPECT_FALSE(address
.SetInfo(AutofillType(ADDRESS_HOME_STREET_ADDRESS
),
350 EXPECT_EQ(base::string16(), address
.GetRawInfo(ADDRESS_HOME_LINE1
));
351 EXPECT_EQ(base::string16(), address
.GetRawInfo(ADDRESS_HOME_LINE2
));
352 EXPECT_EQ(base::string16(), address
.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS
));
355 // Verifies that Address::SetInfio() rejects setting data for
356 // ADDRESS_HOME_STREET_ADDRESS if the data has any trailing blank lines.
357 TEST(AddressTest
, SetStreetAddressRejectsAddressesWithTrailingBlankLines
) {
358 // Start with a non-empty address.
360 address
.SetRawInfo(ADDRESS_HOME_LINE1
, ASCIIToUTF16("123 Example Ave."));
361 address
.SetRawInfo(ADDRESS_HOME_LINE2
, ASCIIToUTF16("Apt. 42"));
362 EXPECT_FALSE(address
.GetRawInfo(ADDRESS_HOME_LINE1
).empty());
363 EXPECT_FALSE(address
.GetRawInfo(ADDRESS_HOME_LINE2
).empty());
364 EXPECT_FALSE(address
.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS
).empty());
366 // Attempting to set an address with leading blank lines should fail, and
367 // clear out the previously stored address.
368 EXPECT_FALSE(address
.SetInfo(AutofillType(ADDRESS_HOME_STREET_ADDRESS
),
369 ASCIIToUTF16("Address line 1"
373 EXPECT_EQ(base::string16(), address
.GetRawInfo(ADDRESS_HOME_LINE1
));
374 EXPECT_EQ(base::string16(), address
.GetRawInfo(ADDRESS_HOME_LINE2
));
375 EXPECT_EQ(base::string16(), address
.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS
));
378 } // namespace autofill