File: address_i18n_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (159 lines) | stat: -rw-r--r-- 5,946 bytes parent folder | download | duplicates (6)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright 2014 The Chromium Authors
// 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/geo/address_i18n.h"

#include <stddef.h>

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

#include "base/containers/contains.h"
#include "base/uuid.h"
#include "components/autofill/core/browser/data_model/addresses/autofill_profile.h"
#include "components/autofill/core/browser/field_types.h"
#include "components/autofill/core/browser/test_utils/autofill_test_utils.h"
#include "testing/gmock/include/gmock/gmock-matchers.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 i18n::addressinput {

using ::operator<<;

}  // namespace i18n::addressinput

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;

struct FieldTypeMirrorConversionsTestCase {
  FieldType server_field;
  AddressField address_field;
};

class FieldTypeMirrorConversionsTest
    : public testing::TestWithParam<FieldTypeMirrorConversionsTestCase> {};

TEST_P(FieldTypeMirrorConversionsTest, FieldTypeMirrorConversions) {
  auto test_data = GetParam();
  AddressField address_field;
  EXPECT_TRUE(FieldForType(test_data.server_field, &address_field));
  EXPECT_EQ(test_data.address_field, address_field);

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

INSTANTIATE_TEST_SUITE_P(
    AddressI18nTest,
    FieldTypeMirrorConversionsTest,
    testing::Values(
        FieldTypeMirrorConversionsTestCase{ADDRESS_HOME_COUNTRY, COUNTRY},
        FieldTypeMirrorConversionsTestCase{ADDRESS_HOME_STATE, ADMIN_AREA},
        FieldTypeMirrorConversionsTestCase{ADDRESS_HOME_CITY, LOCALITY},
        FieldTypeMirrorConversionsTestCase{ADDRESS_HOME_DEPENDENT_LOCALITY,
                                           DEPENDENT_LOCALITY},
        FieldTypeMirrorConversionsTestCase{ADDRESS_HOME_SORTING_CODE,
                                           SORTING_CODE},
        FieldTypeMirrorConversionsTestCase{ADDRESS_HOME_ZIP, POSTAL_CODE},
        FieldTypeMirrorConversionsTestCase{ADDRESS_HOME_STREET_ADDRESS,
                                           STREET_ADDRESS},
        FieldTypeMirrorConversionsTestCase{COMPANY_NAME, ORGANIZATION},
        FieldTypeMirrorConversionsTestCase{NAME_FULL, RECIPIENT}));

struct FieldTypeUnidirectionalConversionsTestCase {
  FieldType server_field;
  AddressField expected_address_field;
};

class FieldTypeUnidirectionalConversionsTest
    : public testing::TestWithParam<
          FieldTypeUnidirectionalConversionsTestCase> {};

TEST_P(FieldTypeUnidirectionalConversionsTest,
       FieldTypeUnidirectionalConversions) {
  auto test_data = GetParam();
  AddressField actual_address_field;
  FieldForType(test_data.server_field, &actual_address_field);
  EXPECT_EQ(test_data.expected_address_field, actual_address_field);
}

INSTANTIATE_TEST_SUITE_P(AddressI18nTest,
                         FieldTypeUnidirectionalConversionsTest,
                         testing::Values(
                             FieldTypeUnidirectionalConversionsTestCase{
                                 ADDRESS_HOME_LINE1, STREET_ADDRESS},
                             FieldTypeUnidirectionalConversionsTestCase{
                                 ADDRESS_HOME_LINE2, STREET_ADDRESS}));

TEST(AddressI18nTest, UnconvertableFields) {
  EXPECT_FALSE(FieldForType(PHONE_HOME_NUMBER, nullptr));
  EXPECT_FALSE(FieldForType(EMAIL_ADDRESS, nullptr));
}

TEST(AddressI18nTest, CreateAddressDataFromAutofillProfile) {
  AutofillProfile profile(i18n_model_definition::kLegacyHierarchyCountryCode);
  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);
}

TEST(AddressI18nTest, ProfileOnlyWithAddressLine2ReturnsOneAddressLine) {
  AutofillProfile profile(i18n_model_definition::kLegacyHierarchyCountryCode);
  test::SetProfileInfo(&profile, "", "", "", "", "", "", "Apt 8", "", "", "",
                       "", "");
  profile.set_language_code("en");
  std::unique_ptr<AddressData> actual =
      CreateAddressDataFromAutofillProfile(profile, "en_US");

  AddressData expected;
  expected.address_line.push_back("Apt 8");
  expected.language_code = "en";

  EXPECT_EQ(expected, *actual);
}

TEST(AddressI18nTest, IsFieldRequired) {
  EXPECT_TRUE(IsFieldRequired(ADDRESS_HOME_LINE1, "CA"));

  EXPECT_FALSE(IsFieldRequired(ADDRESS_HOME_SORTING_CODE, "CA"));
  EXPECT_FALSE(IsFieldRequired(ADDRESS_HOME_SORTING_CODE, "FR"));

  EXPECT_TRUE(IsFieldRequired(ADDRESS_HOME_STATE, "AE"));
  EXPECT_FALSE(IsFieldRequired(ADDRESS_HOME_STATE, "BS"));
}

}  // namespace i18n
}  // namespace autofill