summaryrefslogtreecommitdiff
path: root/chromium/components/autofill/core/browser/address_i18n_unittest.cc
blob: 59ddc2137a3585f4bf6f6ad1a8d35c31fa7d4b28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/autofill/core/browser/address_i18n.h"

#include <stddef.h>

#include <memory>
#include <string>
#include <vector>

#include "base/guid.h"
#include "base/macros.h"
#include "components/autofill/core/browser/autofill_profile.h"
#include "components/autofill/core/browser/autofill_test_utils.h"
#include "components/autofill/core/browser/field_types.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_data.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_field.h"

namespace autofill {
namespace i18n {

using ::i18n::addressinput::AddressData;
using ::i18n::addressinput::AddressField;

using ::i18n::addressinput::ADMIN_AREA;
using ::i18n::addressinput::COUNTRY;
using ::i18n::addressinput::DEPENDENT_LOCALITY;
using ::i18n::addressinput::LOCALITY;
using ::i18n::addressinput::ORGANIZATION;
using ::i18n::addressinput::POSTAL_CODE;
using ::i18n::addressinput::RECIPIENT;
using ::i18n::addressinput::SORTING_CODE;
using ::i18n::addressinput::STREET_ADDRESS;

TEST(AddressI18nTest, FieldTypeMirrorConversions) {
  static const struct {
    bool billing;
    ServerFieldType server_field;
    AddressField address_field;
  } kTestData[] = {
    {true, ADDRESS_BILLING_COUNTRY, COUNTRY},
    {true, ADDRESS_BILLING_STATE, ADMIN_AREA},
    {true, ADDRESS_BILLING_CITY, LOCALITY},
    {true, ADDRESS_BILLING_DEPENDENT_LOCALITY, DEPENDENT_LOCALITY},
    {true, ADDRESS_BILLING_SORTING_CODE, SORTING_CODE},
    {true, ADDRESS_BILLING_ZIP, POSTAL_CODE},
    {true, ADDRESS_BILLING_STREET_ADDRESS, STREET_ADDRESS},
    {true, COMPANY_NAME, ORGANIZATION},
    {true, NAME_BILLING_FULL, RECIPIENT},
    {false, ADDRESS_HOME_COUNTRY, COUNTRY},
    {false, ADDRESS_HOME_STATE, ADMIN_AREA},
    {false, ADDRESS_HOME_CITY, LOCALITY},
    {false, ADDRESS_HOME_DEPENDENT_LOCALITY, DEPENDENT_LOCALITY},
    {false, ADDRESS_HOME_SORTING_CODE, SORTING_CODE},
    {false, ADDRESS_HOME_ZIP, POSTAL_CODE},
    {false, ADDRESS_HOME_STREET_ADDRESS, STREET_ADDRESS},
    {false, COMPANY_NAME, ORGANIZATION},
    {false, NAME_FULL, RECIPIENT},
  };

  for (const auto& test_data : kTestData) {
    AddressField address_field;
    EXPECT_TRUE(FieldForType(test_data.server_field, &address_field));
    EXPECT_EQ(test_data.address_field, address_field);

    ServerFieldType server_field =
        TypeForField(test_data.address_field, test_data.billing);
    EXPECT_EQ(test_data.server_field, server_field);
  }
}

TEST(AddressI18nTest, FieldTypeUnidirectionalConversions) {
  static const struct {
    ServerFieldType server_field;
    AddressField expected_address_field;
  } kTestData[] = {
    {ADDRESS_BILLING_LINE1, STREET_ADDRESS},
    {ADDRESS_BILLING_LINE2, STREET_ADDRESS},
    {ADDRESS_HOME_LINE1, STREET_ADDRESS},
    {ADDRESS_HOME_LINE2, STREET_ADDRESS},
  };

  for (const auto& test_data : kTestData) {
    AddressField actual_address_field;
    FieldForType(test_data.server_field, &actual_address_field);
    EXPECT_EQ(test_data.expected_address_field, actual_address_field);
  }
}

TEST(AddressI18nTest, UnconvertableServerFields) {
  EXPECT_FALSE(FieldForType(PHONE_HOME_NUMBER, NULL));
  EXPECT_FALSE(FieldForType(EMAIL_ADDRESS, NULL));
}

TEST(AddressI18nTest, CreateAddressDataFromAutofillProfile) {
  AutofillProfile profile(base::GenerateGUID(), "http://www.example.com/");
  test::SetProfileInfo(&profile,
                       "John",
                       "H.",
                       "Doe",
                       "johndoe@hades.com",
                       "Underworld",
                       "666 Erebus St.",
                       "Apt 8",
                       "Elysium", "CA",
                       "91111",
                       "US",
                       "16502111111");
  profile.set_language_code("en");
  std::unique_ptr<AddressData> actual =
      CreateAddressDataFromAutofillProfile(profile, "en_US");

  AddressData expected;
  expected.region_code = "US";
  expected.address_line.push_back("666 Erebus St.");
  expected.address_line.push_back("Apt 8");
  expected.administrative_area = "CA";
  expected.locality = "Elysium";
  expected.postal_code = "91111";
  expected.language_code = "en";
  expected.organization = "Underworld";
  expected.recipient = "John H. Doe";

  EXPECT_EQ(expected, *actual);
}

}  // namespace i18n
}  // namespace autofill