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/json/json_reader.h"
6 #include "base/logging.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "base/values.h"
10 #include "components/autofill/content/browser/wallet/wallet_address.h"
11 #include "components/autofill/content/browser/wallet/wallet_test_util.h"
12 #include "components/autofill/core/browser/autofill_profile.h"
13 #include "components/autofill/core/browser/autofill_test_utils.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 using base::ASCIIToUTF16
;
20 const char kAddressMissingObjectId
[] =
22 " \"phone_number\":\"phone_number\","
23 " \"postal_address\":"
25 " \"recipient_name\":\"recipient_name\","
28 " \"address_line_1\","
31 " \"locality_name\":\"locality_name\","
32 " \"dependent_locality_name\":\"dependent_locality_name\","
33 " \"administrative_area_name\":\"administrative_area_name\","
34 " \"postal_code_number\":\"postal_code_number\","
35 " \"sorting_code\":\"sorting_code\","
36 " \"country_name_code\":\"US\","
37 " \"language_code\":\"language_code\""
41 const char kAddressMissingCountryNameCode
[] =
44 " \"phone_number\":\"phone_number\","
45 " \"postal_address\":"
47 " \"recipient_name\":\"recipient_name\","
50 " \"address_line_1\","
53 " \"locality_name\":\"locality_name\","
54 " \"dependent_locality_name\":\"dependent_locality_name\","
55 " \"administrative_area_name\":\"administrative_area_name\","
56 " \"postal_code_number\":\"postal_code_number\","
57 " \"sorting_code\":\"sorting_code\""
61 const char kAddressMissingRecipientName
[] =
64 " \"phone_number\":\"phone_number\","
65 " \"postal_address\":"
69 " \"address_line_1\","
72 " \"locality_name\":\"locality_name\","
73 " \"dependent_locality_name\":\"dependent_locality_name\","
74 " \"administrative_area_name\":\"administrative_area_name\","
75 " \"postal_code_number\":\"postal_code_number\","
76 " \"sorting_code\":\"sorting_code\","
77 " \"country_name_code\":\"US\""
81 const char kAddressMissingPostalCodeNumber
[] =
84 " \"phone_number\":\"phone_number\","
85 " \"postal_address\":"
87 " \"recipient_name\":\"recipient_name\","
90 " \"address_line_1\","
93 " \"locality_name\":\"locality_name\","
94 " \"dependent_locality_name\":\"dependent_locality_name\","
95 " \"administrative_area_name\":\"administrative_area_name\","
96 " \"sorting_code\":\"sorting_code\","
97 " \"country_name_code\":\"US\""
101 const char kAddressMissingLanguageCode
[] =
104 " \"phone_number\":\"phone_number\","
105 " \"is_minimal_address\":true,"
106 " \"postal_address\":"
108 " \"recipient_name\":\"recipient_name\","
111 " \"address_line_1\","
112 " \"address_line_2\""
114 " \"locality_name\":\"locality_name\","
115 " \"dependent_locality_name\":\"dependent_locality_name\","
116 " \"administrative_area_name\":\"administrative_area_name\","
117 " \"country_name_code\":\"US\","
118 " \"postal_code_number\":\"postal_code_number\","
119 " \"sorting_code\":\"sorting_code\""
123 const char kValidAddress
[] =
126 " \"phone_number\":\"phone_number\","
127 " \"is_minimal_address\":true,"
128 " \"postal_address\":"
130 " \"recipient_name\":\"recipient_name\","
133 " \"address_line_1\","
134 " \"address_line_2\""
136 " \"locality_name\":\"locality_name\","
137 " \"dependent_locality_name\":\"dependent_locality_name\","
138 " \"administrative_area_name\":\"administrative_area_name\","
139 " \"country_name_code\":\"US\","
140 " \"postal_code_number\":\"postal_code_number\","
141 " \"sorting_code\":\"sorting_code\","
142 " \"language_code\":\"language_code\""
146 const char kClientAddressMissingCountryCode
[] =
148 " \"name\":\"name\","
149 " \"address1\":\"address1\","
150 " \"address2\":\"address2\","
151 " \"city\":\"city\","
152 " \"state\":\"state\","
153 " \"postal_code\":\"postal_code\","
154 " \"sorting_code\":\"sorting_code\","
155 " \"phone_number\":\"phone_number\""
158 const char kClientAddressMissingPostalCode
[] =
160 " \"name\":\"name\","
161 " \"address1\":\"address1\","
162 " \"address2\":\"address2\","
163 " \"city\":\"city\","
164 " \"state\":\"state\","
165 " \"phone_number\":\"phone_number\","
166 " \"country_code\":\"US\""
169 const char kClientAddressMissingName
[] =
171 " \"address1\":\"address1\","
172 " \"address2\":\"address2\","
173 " \"city\":\"city\","
174 " \"state\":\"state\","
175 " \"postal_code\":\"postal_code\","
176 " \"sorting_code\":\"sorting_code\","
177 " \"phone_number\":\"phone_number\","
178 " \"country_code\":\"US\""
181 const char kClientAddressMissingLanguageCode
[] =
183 " \"name\":\"name\","
184 " \"address1\":\"address1\","
185 " \"address2\":\"address2\","
186 " \"city\":\"city\","
187 " \"dependent_locality_name\":\"district\","
188 " \"state\":\"state\","
189 " \"postal_code\":\"postal_code\","
190 " \"sorting_code\":\"sorting_code\","
191 " \"phone_number\":\"phone_number\","
192 " \"country_code\":\"US\","
196 const char kClientValidAddress
[] =
198 " \"name\":\"name\","
199 " \"address1\":\"address1\","
200 " \"address2\":\"address2\","
201 " \"city\":\"city\","
202 " \"dependent_locality_name\":\"district\","
203 " \"state\":\"state\","
204 " \"postal_code\":\"postal_code\","
205 " \"sorting_code\":\"sorting_code\","
206 " \"phone_number\":\"phone_number\","
207 " \"country_code\":\"US\","
208 " \"type\":\"FULL\","
209 " \"language_code\":\"language_code\""
212 } // anonymous namespace
217 class WalletAddressTest
: public testing::Test
{
219 WalletAddressTest() {}
221 void SetUpDictionary(const std::string
& json
) {
222 scoped_ptr
<base::Value
> value
= base::JSONReader::Read(json
);
224 DCHECK(value
->IsType(base::Value::TYPE_DICTIONARY
));
225 dict_
.reset(static_cast<base::DictionaryValue
*>(value
.release()));
228 scoped_ptr
<const base::DictionaryValue
> dict_
;
231 TEST_F(WalletAddressTest
, AddressEqualsIgnoreID
) {
232 Address
address1("US",
233 ASCIIToUTF16("recipient_name"),
234 StreetAddress("address_line_1", "address_line_2"),
235 ASCIIToUTF16("locality_name"),
236 ASCIIToUTF16("dependent_locality_name"),
237 ASCIIToUTF16("administrative_area_name"),
238 ASCIIToUTF16("postal_code_number"),
239 ASCIIToUTF16("sorting_code"),
240 ASCIIToUTF16("phone_number"),
243 // Same as address1, only id is different.
244 Address
address2("US",
245 ASCIIToUTF16("recipient_name"),
246 StreetAddress("address_line_1", "address_line_2"),
247 ASCIIToUTF16("locality_name"),
248 ASCIIToUTF16("dependent_locality_name"),
249 ASCIIToUTF16("administrative_area_name"),
250 ASCIIToUTF16("postal_code_number"),
251 ASCIIToUTF16("sorting_code"),
252 ASCIIToUTF16("phone_number"),
255 // Has same id as address1, but name is different.
256 Address
address3("US",
257 ASCIIToUTF16("a_different_name"),
258 StreetAddress("address_line_1", "address_line_2"),
259 ASCIIToUTF16("locality_name"),
260 ASCIIToUTF16("dependent_locality_name"),
261 ASCIIToUTF16("administrative_area_name"),
262 ASCIIToUTF16("phone_number"),
263 ASCIIToUTF16("postal_code_number"),
264 ASCIIToUTF16("sorting_code"),
267 // Same as address1, but no id.
268 Address
address4("US",
269 ASCIIToUTF16("recipient_name"),
270 StreetAddress("address_line_1", "address_line_2"),
271 ASCIIToUTF16("locality_name"),
272 ASCIIToUTF16("dependent_locality_name"),
273 ASCIIToUTF16("administrative_area_name"),
274 ASCIIToUTF16("postal_code_number"),
275 ASCIIToUTF16("sorting_code"),
276 ASCIIToUTF16("phone_number"),
279 // Same as address1, only language code is different.
280 Address
address5("US",
281 ASCIIToUTF16("recipient_name"),
282 StreetAddress("address_line_1", "address_line_2"),
283 ASCIIToUTF16("locality_name"),
284 ASCIIToUTF16("dependent_locality_name"),
285 ASCIIToUTF16("administrative_area_name"),
286 ASCIIToUTF16("postal_code_number"),
287 ASCIIToUTF16("sorting_code"),
288 ASCIIToUTF16("phone_number"),
290 "other_language_code");
292 // Compare the address has id field to itself.
293 EXPECT_EQ(address1
, address1
);
294 EXPECT_TRUE(address1
.EqualsIgnoreID(address1
));
296 // Compare the address has no id field to itself
297 EXPECT_EQ(address4
, address4
);
298 EXPECT_TRUE(address4
.EqualsIgnoreID(address4
));
300 // Compare two addresses with different id.
301 EXPECT_NE(address1
, address2
);
302 EXPECT_TRUE(address1
.EqualsIgnoreID(address2
));
303 EXPECT_TRUE(address2
.EqualsIgnoreID(address1
));
305 // Compare two different addresses.
306 EXPECT_NE(address1
, address3
);
307 EXPECT_FALSE(address1
.EqualsIgnoreID(address3
));
308 EXPECT_FALSE(address3
.EqualsIgnoreID(address1
));
310 // Compare two same addresses, one has id, the other doesn't.
311 EXPECT_NE(address1
, address4
);
312 EXPECT_TRUE(address1
.EqualsIgnoreID(address4
));
313 EXPECT_TRUE(address4
.EqualsIgnoreID(address1
));
315 // Compare two addresses with different language code.
316 EXPECT_NE(address1
, address5
);
317 EXPECT_TRUE(address1
.EqualsIgnoreID(address5
));
318 EXPECT_TRUE(address5
.EqualsIgnoreID(address1
));
321 TEST_F(WalletAddressTest
, CreateAddressMissingObjectId
) {
322 SetUpDictionary(kAddressMissingObjectId
);
323 Address
address("US",
324 ASCIIToUTF16("recipient_name"),
325 StreetAddress("address_line_1", "address_line_2"),
326 ASCIIToUTF16("locality_name"),
327 ASCIIToUTF16("dependent_locality_name"),
328 ASCIIToUTF16("administrative_area_name"),
329 ASCIIToUTF16("postal_code_number"),
330 ASCIIToUTF16("sorting_code"),
331 ASCIIToUTF16("phone_number"),
334 EXPECT_EQ(address
, *Address::CreateAddress(*dict_
));
337 TEST_F(WalletAddressTest
, CreateAddressWithIDMissingObjectId
) {
338 SetUpDictionary(kAddressMissingObjectId
);
339 EXPECT_EQ(NULL
, Address::CreateAddressWithID(*dict_
).get());
342 TEST_F(WalletAddressTest
, CreateAddressMissingCountryNameCode
) {
343 SetUpDictionary(kAddressMissingCountryNameCode
);
344 EXPECT_EQ(NULL
, Address::CreateAddress(*dict_
).get());
345 EXPECT_EQ(NULL
, Address::CreateAddressWithID(*dict_
).get());
348 TEST_F(WalletAddressTest
, CreateAddressMissingRecipientName
) {
349 SetUpDictionary(kAddressMissingRecipientName
);
350 EXPECT_EQ(NULL
, Address::CreateAddress(*dict_
).get());
351 EXPECT_EQ(NULL
, Address::CreateAddressWithID(*dict_
).get());
354 TEST_F(WalletAddressTest
, CreateAddressMissingPostalCodeNumber
) {
355 SetUpDictionary(kAddressMissingPostalCodeNumber
);
356 EXPECT_EQ(NULL
, Address::CreateAddress(*dict_
).get());
357 EXPECT_EQ(NULL
, Address::CreateAddressWithID(*dict_
).get());
360 TEST_F(WalletAddressTest
, CreateAddressMissingLanguageCode
) {
361 SetUpDictionary(kAddressMissingLanguageCode
);
362 Address
address("US",
363 ASCIIToUTF16("recipient_name"),
364 StreetAddress("address_line_1", "address_line_2"),
365 ASCIIToUTF16("locality_name"),
366 ASCIIToUTF16("dependent_locality_name"),
367 ASCIIToUTF16("administrative_area_name"),
368 ASCIIToUTF16("postal_code_number"),
369 ASCIIToUTF16("sorting_code"),
370 ASCIIToUTF16("phone_number"),
373 address
.set_is_complete_address(false);
374 EXPECT_EQ(address
, *Address::CreateAddress(*dict_
));
375 EXPECT_EQ(address
, *Address::CreateAddressWithID(*dict_
));
378 TEST_F(WalletAddressTest
, CreateAddressWithID
) {
379 SetUpDictionary(kValidAddress
);
380 Address
address("US",
381 ASCIIToUTF16("recipient_name"),
382 StreetAddress("address_line_1", "address_line_2"),
383 ASCIIToUTF16("locality_name"),
384 ASCIIToUTF16("dependent_locality_name"),
385 ASCIIToUTF16("administrative_area_name"),
386 ASCIIToUTF16("postal_code_number"),
387 ASCIIToUTF16("sorting_code"),
388 ASCIIToUTF16("phone_number"),
391 address
.set_is_complete_address(false);
392 EXPECT_EQ(address
, *Address::CreateAddress(*dict_
));
393 EXPECT_EQ(address
, *Address::CreateAddressWithID(*dict_
));
396 TEST_F(WalletAddressTest
, CreateDisplayAddressMissingCountryNameCode
) {
397 SetUpDictionary(kClientAddressMissingCountryCode
);
398 EXPECT_EQ(NULL
, Address::CreateDisplayAddress(*dict_
).get());
401 TEST_F(WalletAddressTest
, CreateDisplayAddressMissingName
) {
402 SetUpDictionary(kClientAddressMissingName
);
403 EXPECT_EQ(NULL
, Address::CreateDisplayAddress(*dict_
).get());
406 TEST_F(WalletAddressTest
, CreateDisplayAddressMissingPostalCode
) {
407 SetUpDictionary(kClientAddressMissingPostalCode
);
408 EXPECT_EQ(NULL
, Address::CreateDisplayAddress(*dict_
).get());
411 TEST_F(WalletAddressTest
, CreateDisplayAddressMissingLanguageCode
) {
412 SetUpDictionary(kClientAddressMissingLanguageCode
);
413 Address
address("US",
414 ASCIIToUTF16("name"),
415 StreetAddress("address1", "address2"),
416 ASCIIToUTF16("city"),
417 ASCIIToUTF16("district"),
418 ASCIIToUTF16("state"),
419 ASCIIToUTF16("postal_code"),
420 ASCIIToUTF16("sorting_code"),
421 ASCIIToUTF16("phone_number"),
424 EXPECT_EQ(address
, *Address::CreateDisplayAddress(*dict_
));
427 TEST_F(WalletAddressTest
, CreateDisplayAddress
) {
428 SetUpDictionary(kClientValidAddress
);
429 Address
address("US",
430 ASCIIToUTF16("name"),
431 StreetAddress("address1", "address2"),
432 ASCIIToUTF16("city"),
433 ASCIIToUTF16("district"),
434 ASCIIToUTF16("state"),
435 ASCIIToUTF16("postal_code"),
436 ASCIIToUTF16("sorting_code"),
437 ASCIIToUTF16("phone_number"),
440 EXPECT_EQ(address
, *Address::CreateDisplayAddress(*dict_
));
443 TEST_F(WalletAddressTest
, ToDictionaryWithoutID
) {
444 base::DictionaryValue expected
;
445 expected
.SetString("country_name_code",
447 expected
.SetString("recipient_name",
449 expected
.SetString("locality_name",
451 expected
.SetString("dependent_locality_name",
452 "dependent_locality_name");
453 expected
.SetString("administrative_area_name",
454 "administrative_area_name");
455 expected
.SetString("postal_code_number",
456 "postal_code_number");
457 expected
.SetString("sorting_code",
459 expected
.SetString("language_code",
461 base::ListValue
* address_lines
= new base::ListValue();
462 address_lines
->AppendString("address_line_1");
463 address_lines
->AppendString("address_line_2");
464 expected
.Set("address_line", address_lines
);
466 Address
address("US",
467 ASCIIToUTF16("recipient_name"),
468 StreetAddress("address_line_1", "address_line_2"),
469 ASCIIToUTF16("locality_name"),
470 ASCIIToUTF16("dependent_locality_name"),
471 ASCIIToUTF16("administrative_area_name"),
472 ASCIIToUTF16("postal_code_number"),
473 ASCIIToUTF16("sorting_code"),
474 ASCIIToUTF16("phone_number"),
478 EXPECT_TRUE(expected
.Equals(address
.ToDictionaryWithoutID().get()));
481 TEST_F(WalletAddressTest
, ToDictionaryWithID
) {
482 base::DictionaryValue expected
;
483 expected
.SetString("id", "id");
484 expected
.SetString("phone_number", "phone_number");
485 expected
.SetString("postal_address.country_name_code",
487 expected
.SetString("postal_address.recipient_name",
489 expected
.SetString("postal_address.locality_name",
491 expected
.SetString("postal_address.dependent_locality_name",
492 "dependent_locality_name");
493 expected
.SetString("postal_address.administrative_area_name",
494 "administrative_area_name");
495 expected
.SetString("postal_address.postal_code_number",
496 "postal_code_number");
497 expected
.SetString("postal_address.sorting_code",
499 expected
.SetString("postal_address.language_code",
501 base::ListValue
* address_lines
= new base::ListValue();
502 address_lines
->AppendString("address_line_1");
503 address_lines
->AppendString("address_line_2");
504 expected
.Set("postal_address.address_line", address_lines
);
506 Address
address("US",
507 ASCIIToUTF16("recipient_name"),
508 StreetAddress("address_line_1", "address_line_2"),
509 ASCIIToUTF16("locality_name"),
510 ASCIIToUTF16("dependent_locality_name"),
511 ASCIIToUTF16("administrative_area_name"),
512 ASCIIToUTF16("postal_code_number"),
513 ASCIIToUTF16("sorting_code"),
514 ASCIIToUTF16("phone_number"),
518 EXPECT_TRUE(expected
.Equals(address
.ToDictionaryWithID().get()));
521 // Verifies that WalletAddress::GetInfo() can correctly return both country
522 // codes and localized country names.
523 TEST_F(WalletAddressTest
, GetCountryInfo
) {
524 Address
address("FR",
525 ASCIIToUTF16("recipient_name"),
526 StreetAddress("address_line_1", "address_line_2"),
527 ASCIIToUTF16("locality_name"),
528 ASCIIToUTF16("dependent_locality_name"),
529 ASCIIToUTF16("administrative_area_name"),
530 ASCIIToUTF16("postal_code_number"),
531 ASCIIToUTF16("sorting_code"),
532 ASCIIToUTF16("phone_number"),
536 AutofillType type
= AutofillType(HTML_TYPE_COUNTRY_CODE
, HTML_MODE_NONE
);
537 EXPECT_EQ(ASCIIToUTF16("FR"), address
.GetInfo(type
, "en-US"));
539 type
= AutofillType(HTML_TYPE_COUNTRY_NAME
, HTML_MODE_NONE
);
540 EXPECT_EQ(ASCIIToUTF16("France"), address
.GetInfo(type
, "en-US"));
542 type
= AutofillType(ADDRESS_HOME_COUNTRY
);
543 EXPECT_EQ(ASCIIToUTF16("France"), address
.GetInfo(type
, "en-US"));
546 // Verifies that WalletAddress::GetInfo() can correctly return a concatenated
547 // full street address.
548 TEST_F(WalletAddressTest
, GetStreetAddress
) {
549 std::vector
<base::string16
> street_address
= StreetAddress(
550 "address_line_1", "address_line_2");
551 // Address has both lines 1 and 2.
552 Address
address1("FR",
553 ASCIIToUTF16("recipient_name"),
555 ASCIIToUTF16("locality_name"),
556 ASCIIToUTF16("dependent_locality_name"),
557 ASCIIToUTF16("administrative_area_name"),
558 ASCIIToUTF16("postal_code_number"),
559 ASCIIToUTF16("sorting_code"),
560 ASCIIToUTF16("phone_number"),
563 AutofillType type
= AutofillType(HTML_TYPE_STREET_ADDRESS
, HTML_MODE_NONE
);
564 EXPECT_EQ(ASCIIToUTF16("address_line_1\naddress_line_2"),
565 address1
.GetInfo(type
, "en-US"));
567 // Address has only line 1.
568 street_address
.resize(1);
569 Address
address2("FR",
570 ASCIIToUTF16("recipient_name"),
572 ASCIIToUTF16("locality_name"),
573 ASCIIToUTF16("dependent_locality_name"),
574 ASCIIToUTF16("administrative_area_name"),
575 ASCIIToUTF16("postal_code_number"),
576 ASCIIToUTF16("sorting_code"),
577 ASCIIToUTF16("phone_number"),
580 EXPECT_EQ(ASCIIToUTF16("address_line_1"), address2
.GetInfo(type
, "en-US"));
582 // Address has no address lines.
583 street_address
.clear();
584 Address
address3("FR",
585 ASCIIToUTF16("recipient_name"),
587 ASCIIToUTF16("locality_name"),
588 ASCIIToUTF16("dependent_locality_name"),
589 ASCIIToUTF16("administrative_area_name"),
590 ASCIIToUTF16("postal_code_number"),
591 ASCIIToUTF16("sorting_code"),
592 ASCIIToUTF16("phone_number"),
595 EXPECT_EQ(base::string16(), address3
.GetInfo(type
, "en-US"));
598 } // namespace wallet
599 } // namespace autofill