File: autofill_country_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 (253 lines) | stat: -rw-r--r-- 10,409 bytes parent folder | download | duplicates (5)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Copyright 2013 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/autofill_country.h"

#include <set>
#include <string>

#include "base/containers/contains.h"
#include "base/strings/utf_string_conversions.h"
#include "components/autofill/core/browser/country_type.h"
#include "components/autofill/core/browser/field_types.h"
#include "components/autofill/core/browser/geo/address_i18n.h"
#include "components/autofill/core/browser/geo/country_data.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_field.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_metadata.h"
#if defined(ANDROID)
#include "base/android/build_info.h"
#endif

namespace autofill {
namespace {

using ::base::ASCIIToUTF16;
using ::i18n::addressinput::AddressField;

// Test the constructor and accessors
TEST(AutofillCountryTest, AutofillCountry) {
  AutofillCountry united_states_en("US", "en_US");
  EXPECT_EQ("US", united_states_en.country_code());
  EXPECT_EQ(u"United States", united_states_en.name());

  AutofillCountry united_states_es("US", "es");
  EXPECT_EQ("US", united_states_es.country_code());
  EXPECT_EQ(u"Estados Unidos", united_states_es.name());

  AutofillCountry great_britain_uk_alias("UK", "en_GB");
  EXPECT_EQ("GB", great_britain_uk_alias.country_code());
  EXPECT_EQ("GB", great_britain_uk_alias.country_code());
  EXPECT_EQ(u"United Kingdom", great_britain_uk_alias.name());

  AutofillCountry canada_en("CA", "en_US");
  EXPECT_EQ("CA", canada_en.country_code());
  EXPECT_EQ(u"Canada", canada_en.name());

  AutofillCountry canada_hu("CA", "hu");
  EXPECT_EQ("CA", canada_hu.country_code());
  EXPECT_EQ(u"Kanada", canada_hu.name());

  // Unrecognizable country codes remain that way.
  AutofillCountry unknown("Unknown", "en_US");
  EXPECT_EQ("Unknown", unknown.country_code());

  // If no locale is provided, no `name()` is returned.
  AutofillCountry empty_locale("AT");
  EXPECT_EQ("AT", empty_locale.country_code());
  EXPECT_TRUE(empty_locale.name().empty());
}

// Test locale to country code mapping.
TEST(AutofillCountryTest, CountryCodeForLocale) {
  EXPECT_EQ("US", AutofillCountry::CountryCodeForLocale("en_US"));
  EXPECT_EQ("CA", AutofillCountry::CountryCodeForLocale("fr_CA"));
  EXPECT_EQ("FR", AutofillCountry::CountryCodeForLocale("fr"));
  EXPECT_EQ("US", AutofillCountry::CountryCodeForLocale("Unknown"));
  // "es-419" isn't associated with a country. See base/l10n/l10n_util.cc
  // for details about this locale. Default to US.
  EXPECT_EQ("US", AutofillCountry::CountryCodeForLocale("es-419"));
}

// Test that the correct country code is retrieved from the app locale if no
// geo ip country code could be retrieved.
TEST(AutofillCountryTest, GetDefaultCountryCodeForNewAddressFromAppLocale) {
  EXPECT_EQ("US", AutofillCountry::GetDefaultCountryCodeForNewAddress(
                      GeoIpCountryCode(""), "en_US")
                      .value());
}

// Test that the country code is is set as the geo ip country code,
// and that it is not extracted from the app locale.
TEST(AutofillCountryTest, GetDefaultCountryCodeForNewAddressFromGeoIp) {
  EXPECT_EQ("DE", AutofillCountry::GetDefaultCountryCodeForNewAddress(
                      GeoIpCountryCode("DE"), "en_US")
                      .value());
}

// Test the address requirement methods for the US.
TEST(AutofillCountryTest, UsaAddressRequirements) {
  // The US requires a zip, state, city and line1 entry.
  AutofillCountry country("US", "en_US");

  EXPECT_FALSE(country.requires_zip_or_state());
  EXPECT_TRUE(country.requires_zip());
  EXPECT_TRUE(country.requires_state());
  EXPECT_TRUE(country.requires_city());
  EXPECT_TRUE(country.requires_line1());

  // The same expectations via FieldType.
  EXPECT_TRUE(country.IsAddressFieldRequired(FieldType::ADDRESS_HOME_ZIP));
  EXPECT_TRUE(country.IsAddressFieldRequired(FieldType::ADDRESS_HOME_STATE));
  EXPECT_TRUE(country.IsAddressFieldRequired(FieldType::ADDRESS_HOME_CITY));
  EXPECT_TRUE(
      country.IsAddressFieldRequired(FieldType::ADDRESS_HOME_STREET_ADDRESS));
}

// Test that unknown country codes have US requirements.
TEST(AutofillCountryTest, UnknownAddressRequirements) {
  AutofillCountry us_autofill_country("US", "en_US");
  AutofillCountry unknown_autofill_country("Unknown", "en_US");

  EXPECT_EQ(us_autofill_country.requires_zip_or_state(),
            unknown_autofill_country.requires_zip_or_state());
  EXPECT_EQ(us_autofill_country.requires_zip(),
            unknown_autofill_country.requires_zip());
  EXPECT_EQ(us_autofill_country.requires_state(),
            unknown_autofill_country.requires_state());
  EXPECT_EQ(us_autofill_country.requires_city(),
            unknown_autofill_country.requires_city());
  EXPECT_EQ(us_autofill_country.requires_line1(),
            unknown_autofill_country.requires_line1());
}

// Test the address requirement method for Brazil.
TEST(AutofillCountryTest, BrAddressRequirements) {
  // Brazil only requires a zip entry.
  AutofillCountry country("BR", "en_US");

  EXPECT_FALSE(country.requires_zip_or_state());
  EXPECT_TRUE(country.requires_zip());
  EXPECT_TRUE(country.requires_state());
  EXPECT_TRUE(country.requires_city());
  EXPECT_TRUE(country.requires_line1());

  // The same expectations via FieldType.
  EXPECT_TRUE(country.IsAddressFieldRequired(FieldType::ADDRESS_HOME_ZIP));
  EXPECT_TRUE(country.IsAddressFieldRequired(FieldType::ADDRESS_HOME_STATE));
  EXPECT_TRUE(country.IsAddressFieldRequired(FieldType::ADDRESS_HOME_CITY));
  EXPECT_TRUE(
      country.IsAddressFieldRequired(FieldType::ADDRESS_HOME_STREET_ADDRESS));
}

// Test the address requirement method for Turkey.
TEST(AutofillCountryTest, TrAddressRequirements) {
  // Brazil only requires a zip entry.
  AutofillCountry country("TR", "en_US");

  // Although ZIP codes are existing in Turkey, they are commonly used.
  EXPECT_FALSE(country.requires_zip());
  // In Turkey, a district is the largest level of the address hierarchy and
  // mapped to the Autofill state.
  EXPECT_TRUE(country.requires_state());
  // And the province as the second largest level is mapped to city.
  EXPECT_TRUE(country.requires_city());
  EXPECT_TRUE(country.requires_line1());

  // The same expectations via FieldType.
  EXPECT_FALSE(country.IsAddressFieldRequired(FieldType::ADDRESS_HOME_ZIP));
  EXPECT_TRUE(country.IsAddressFieldRequired(FieldType::ADDRESS_HOME_STATE));
  EXPECT_TRUE(country.IsAddressFieldRequired(FieldType::ADDRESS_HOME_CITY));
  EXPECT_TRUE(
      country.IsAddressFieldRequired(FieldType::ADDRESS_HOME_STREET_ADDRESS));
}

// Test mapping all country codes to country names.
TEST(AutofillCountryTest, AllCountryCodesHaveCountryName) {
  std::set<std::string> expected_failures;
#if defined(ANDROID)
  if (base::android::BuildInfo::GetInstance()->sdk_int() <
      base::android::SDK_VERSION_KITKAT) {
    expected_failures.insert("BQ");
    expected_failures.insert("SS");
    expected_failures.insert("XK");
  }
#endif
  const std::vector<std::string>& country_codes =
      CountryDataMap::GetInstance()->country_codes();
  for (const std::string& country_code : country_codes) {
    if (base::Contains(expected_failures, country_code)) {
      continue;
    }
    SCOPED_TRACE("Country code '" + country_code + "' should have a name.");
    EXPECT_NE(ASCIIToUTF16(country_code),
              AutofillCountry(country_code, "en").name());
  }
}

// Test alias mappings for falsely existing country codes.
TEST(AutofillCountryTest, AliasMappingsForCountryData) {
  CountryDataMap* country_data_map = CountryDataMap::GetInstance();

  // There should be country data for the "GB".
  EXPECT_TRUE(country_data_map->HasRequiredFieldsForAddressImport("GB"));

  // Check the correctness of the alias definitions.
  EXPECT_TRUE(country_data_map->HasCountryCodeAlias("UK"));
  EXPECT_FALSE(country_data_map->HasCountryCodeAlias("does_not_exist"));

  // Query not existing mapping.
  auto expected_country_code = std::string();
  auto actual_country_code =
      country_data_map->GetCountryCodeForAlias("does_not_exist");
  EXPECT_EQ(expected_country_code, actual_country_code);

  // UK should map the GB.
  expected_country_code = "GB";
  actual_country_code = country_data_map->GetCountryCodeForAlias("UK");
  EXPECT_EQ(expected_country_code, actual_country_code);
}

// Verifies that all address format extensions correspond to types that are
// not part of libaddressinputs expected types, but that they are placed
// after a field that is present in libaddressinput.
TEST(AutofillCountryTest, VerifyAddressFormatExtensions) {
  CountryDataMap* country_data_map = CountryDataMap::GetInstance();
  for (const std::string& country_code : country_data_map->country_codes()) {
    AutofillCountry country(country_code);
    for (const AutofillCountry::AddressFormatExtension& rule :
         country.address_format_extensions()) {
      // The separator should not be empty.
      EXPECT_FALSE(rule.separator_before_label.empty());
      // `rule.type` is not part of `country_code`'s address format, but
      // `rule.placed_after` is.
      ::i18n::addressinput::AddressField libaddressinput_field;
      bool is_valid_field =
          i18n::FieldForType(rule.type, &libaddressinput_field);
      EXPECT_TRUE(!is_valid_field || !::i18n::addressinput::IsFieldUsed(
                                         libaddressinput_field, country_code));

      ::i18n::addressinput::AddressField libaddressinput_place_after;
      ASSERT_TRUE(
          i18n::FieldForType(rule.placed_after, &libaddressinput_place_after));
      EXPECT_TRUE(::i18n::addressinput::IsFieldUsed(libaddressinput_place_after,
                                                    country_code));
      // `IsAddressFieldSettingAccessible` considers `rule.type`
      // setting-accessible.
      EXPECT_TRUE(country.IsAddressFieldSettingAccessible(rule.type));
    }
  }
}

// Test the address requirement method for Poland.
TEST(AutofillCountryTest, PLAddressRequirements) {
  AutofillCountry country("PL", "pl_PL");

  EXPECT_FALSE(country.requires_state());
  EXPECT_TRUE(
      country.IsAddressFieldSettingAccessible(FieldType::ADDRESS_HOME_STATE));
}

}  // namespace
}  // namespace autofill