address_i18n_unittest.cc revision 03b57e008b61dfcb1fbad3aea950ae0e001748b0
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.
4
5#include "components/autofill/core/browser/address_i18n.h"
6
7#include <string>
8#include <vector>
9
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"
18
19namespace autofill {
20namespace i18n {
21
22using ::i18n::addressinput::AddressData;
23using ::i18n::addressinput::AddressField;
24
25using ::i18n::addressinput::ADMIN_AREA;
26using ::i18n::addressinput::COUNTRY;
27using ::i18n::addressinput::DEPENDENT_LOCALITY;
28using ::i18n::addressinput::LOCALITY;
29using ::i18n::addressinput::POSTAL_CODE;
30using ::i18n::addressinput::RECIPIENT;
31using ::i18n::addressinput::SORTING_CODE;
32using ::i18n::addressinput::STREET_ADDRESS;
33
34TEST(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},
56  };
57
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);
62
63    ServerFieldType server_field =
64        TypeForField(kTestData[i].address_field, kTestData[i].billing);
65    EXPECT_EQ(kTestData[i].server_field, server_field);
66  }
67}
68
69TEST(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},
78  };
79
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);
84  }
85}
86
87TEST(AddressI18nTest, UnconvertableServerFields) {
88  EXPECT_FALSE(FieldForType(PHONE_HOME_NUMBER, NULL));
89  EXPECT_FALSE(FieldForType(EMAIL_ADDRESS, NULL));
90}
91
92TEST(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");
109
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";
119
120  EXPECT_EQ(expected, *actual);
121}
122
123}  // namespace i18n
124}  // namespace autofill
125