Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / components / autofill / core / browser / address_unittest.cc
blob9a845e813f81f662177f3dce9daf473b15881b38
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 <string>
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;
15 namespace autofill {
17 // Test that country data can be properly returned as either a country code or a
18 // localized country name.
19 TEST(AddressTest, GetCountry) {
20 Address address;
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);
28 address.SetInfo(
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) {
52 Address address;
53 EXPECT_EQ(base::string16(), address.GetRawInfo(ADDRESS_HOME_COUNTRY));
55 // Test basic conversion.
56 address.SetInfo(
57 AutofillType(ADDRESS_HOME_COUNTRY), ASCIIToUTF16("United States"),
58 "en-US");
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.
65 address.SetInfo(
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.
72 address.SetInfo(
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.
79 address.SetInfo(
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.
86 address.SetInfo(
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
101 // type.
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) {
116 Address address;
117 address.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"));
119 const char* const kValidMatches[] = {
120 "United States",
121 "USA",
122 "US",
123 "United states",
124 "us"
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",
130 &matching_types);
131 ASSERT_EQ(1U, matching_types.size());
132 EXPECT_EQ(ADDRESS_HOME_COUNTRY, *matching_types.begin());
135 const char* const kInvalidMatches[] = {
136 "United",
137 "Garbage"
139 for (size_t i = 0; i < arraysize(kInvalidMatches); ++i) {
140 ServerFieldTypeSet matching_types;
141 address.GetMatchingTypes(ASCIIToUTF16(kInvalidMatches[i]), "US",
142 &matching_types);
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.
159 Address address;
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"
187 "Apt. 42"),
188 address.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS));
189 EXPECT_EQ(ASCIIToUTF16("123 Example Ave.\n"
190 "Apt. 42"),
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"
198 "Apt. 42\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"
208 "\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.
217 Address address;
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"
237 "Apt. 42\n"
238 "(The one with the blue door)");
240 Address address;
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"
269 "Unit 3.14\n"
270 "Box 9");
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.
276 Address 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.
313 Address 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"
324 "\n"
325 "Address line 3"),
326 "en-US"));
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.
336 Address 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),
346 ASCIIToUTF16("\n"
347 "Address line 2"
348 "Address line 3"),
349 "en-US"));
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.
359 Address 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"
370 "Address line 2"
371 "\n"),
372 "en-US"));
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