Use address formatter from libaddressinput for autofill profile labels.
[chromium-blink-merge.git] / components / autofill / core / browser / address_i18n_unittest.cc
blob72ec64a80bc380f4d085cb8c03cd06b172bb6b42
1 // Copyright 2014 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 "components/autofill/core/browser/address_i18n.h"
7 #include <string>
8 #include <vector>
10 #include "base/guid.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "components/autofill/core/browser/autofill_profile.h"
13 #include "components/autofill/core/browser/autofill_test_utils.h"
14 #include "components/autofill/core/browser/field_types.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_data.h"
17 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_field.h"
19 namespace autofill {
20 namespace i18n {
22 using ::i18n::addressinput::AddressData;
23 using ::i18n::addressinput::AddressField;
25 using ::i18n::addressinput::ADMIN_AREA;
26 using ::i18n::addressinput::COUNTRY;
27 using ::i18n::addressinput::DEPENDENT_LOCALITY;
28 using ::i18n::addressinput::LOCALITY;
29 using ::i18n::addressinput::POSTAL_CODE;
30 using ::i18n::addressinput::RECIPIENT;
31 using ::i18n::addressinput::SORTING_CODE;
32 using ::i18n::addressinput::STREET_ADDRESS;
34 TEST(AddressI18nTest, FieldTypeMirrorConversions) {
35 static const struct {
36 bool billing;
37 ServerFieldType server_field;
38 AddressField address_field;
39 } kTestData[] = {
40 {true, ADDRESS_BILLING_COUNTRY, COUNTRY},
41 {true, ADDRESS_BILLING_STATE, ADMIN_AREA},
42 {true, ADDRESS_BILLING_CITY, LOCALITY},
43 {true, ADDRESS_BILLING_DEPENDENT_LOCALITY, DEPENDENT_LOCALITY},
44 {true, ADDRESS_BILLING_SORTING_CODE, SORTING_CODE},
45 {true, ADDRESS_BILLING_ZIP, POSTAL_CODE},
46 {true, ADDRESS_BILLING_STREET_ADDRESS, STREET_ADDRESS},
47 {true, NAME_BILLING_FULL, RECIPIENT},
48 {false, ADDRESS_HOME_COUNTRY, COUNTRY},
49 {false, ADDRESS_HOME_STATE, ADMIN_AREA},
50 {false, ADDRESS_HOME_CITY, LOCALITY},
51 {false, ADDRESS_HOME_DEPENDENT_LOCALITY, DEPENDENT_LOCALITY},
52 {false, ADDRESS_HOME_SORTING_CODE, SORTING_CODE},
53 {false, ADDRESS_HOME_ZIP, POSTAL_CODE},
54 {false, ADDRESS_HOME_STREET_ADDRESS, STREET_ADDRESS},
55 {false, NAME_FULL, RECIPIENT},
58 for (size_t i = 0; i < arraysize(kTestData); ++i) {
59 AddressField address_field;
60 EXPECT_TRUE(FieldForType(kTestData[i].server_field, &address_field));
61 EXPECT_EQ(kTestData[i].address_field, address_field);
63 ServerFieldType server_field =
64 TypeForField(kTestData[i].address_field, kTestData[i].billing);
65 EXPECT_EQ(kTestData[i].server_field, server_field);
69 TEST(AddressI18nTest, FieldTypeUnidirectionalConversions) {
70 static const struct {
71 ServerFieldType server_field;
72 AddressField expected_address_field;
73 } kTestData[] = {
74 {ADDRESS_BILLING_LINE1, STREET_ADDRESS},
75 {ADDRESS_BILLING_LINE2, STREET_ADDRESS},
76 {ADDRESS_HOME_LINE1, STREET_ADDRESS},
77 {ADDRESS_HOME_LINE2, STREET_ADDRESS},
80 for (size_t i = 0; i < arraysize(kTestData); ++i) {
81 AddressField actual_address_field;
82 FieldForType(kTestData[i].server_field, &actual_address_field);
83 EXPECT_EQ(kTestData[i].expected_address_field, actual_address_field);
87 TEST(AddressI18nTest, UnconvertableServerFields) {
88 EXPECT_FALSE(FieldForType(PHONE_HOME_NUMBER, NULL));
89 EXPECT_FALSE(FieldForType(EMAIL_ADDRESS, NULL));
92 TEST(AddressI18nTest, CreateAddressDataFromAutofillProfile) {
93 AutofillProfile profile(base::GenerateGUID(), "http://www.example.com/");
94 test::SetProfileInfo(&profile,
95 "John",
96 "H.",
97 "Doe",
98 "johndoe@hades.com",
99 "Underworld",
100 "666 Erebus St.",
101 "Apt 8",
102 "Elysium", "CA",
103 "91111",
104 "US",
105 "16502111111");
106 profile.set_language_code("en");
107 scoped_ptr<AddressData> actual =
108 CreateAddressDataFromAutofillProfile(profile, "en_US");
110 AddressData expected;
111 expected.region_code = "US";
112 expected.address_line.push_back("666 Erebus St.");
113 expected.address_line.push_back("Apt 8");
114 expected.administrative_area = "CA";
115 expected.locality = "Elysium";
116 expected.postal_code = "91111";
117 expected.language_code = "en";
118 expected.recipient = "John H. Doe";
120 EXPECT_EQ(expected, *actual);
123 } // namespace i18n
124 } // namespace autofill