1 // Copyright 2013 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.
7 #include "base/strings/string16.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "components/autofill/core/browser/autofill_country.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 using base::ASCIIToUTF16
;
16 // Test the constructor and accessors
17 TEST(AutofillCountryTest
, AutofillCountry
) {
18 AutofillCountry
united_states_en("US", "en_US");
19 EXPECT_EQ("US", united_states_en
.country_code());
20 EXPECT_EQ(ASCIIToUTF16("United States"), united_states_en
.name());
21 EXPECT_EQ(ASCIIToUTF16("ZIP code"), united_states_en
.postal_code_label());
22 EXPECT_EQ(ASCIIToUTF16("State"), united_states_en
.state_label());
24 AutofillCountry
united_states_es("US", "es");
25 EXPECT_EQ("US", united_states_es
.country_code());
26 EXPECT_EQ(ASCIIToUTF16("Estados Unidos"), united_states_es
.name());
28 AutofillCountry
canada_en("CA", "en_US");
29 EXPECT_EQ("CA", canada_en
.country_code());
30 EXPECT_EQ(ASCIIToUTF16("Canada"), canada_en
.name());
31 EXPECT_EQ(ASCIIToUTF16("Postal code"), canada_en
.postal_code_label());
32 EXPECT_EQ(ASCIIToUTF16("Province"), canada_en
.state_label());
34 AutofillCountry
canada_hu("CA", "hu");
35 EXPECT_EQ("CA", canada_hu
.country_code());
36 EXPECT_EQ(ASCIIToUTF16("Kanada"), canada_hu
.name());
39 // Test locale to country code mapping.
40 TEST(AutofillCountryTest
, CountryCodeForLocale
) {
41 EXPECT_EQ("US", AutofillCountry::CountryCodeForLocale("en_US"));
42 EXPECT_EQ("CA", AutofillCountry::CountryCodeForLocale("fr_CA"));
43 EXPECT_EQ("FR", AutofillCountry::CountryCodeForLocale("fr"));
44 EXPECT_EQ("US", AutofillCountry::CountryCodeForLocale("Unknown"));
45 // "es-419" isn't associated with a country. See base/l10n/l10n_util.cc
46 // for details about this locale. Default to US.
47 EXPECT_EQ("US", AutofillCountry::CountryCodeForLocale("es-419"));
50 // Test mapping of localized country names to country codes.
51 TEST(AutofillCountryTest
, GetCountryCode
) {
53 EXPECT_EQ("US", AutofillCountry::GetCountryCode(ASCIIToUTF16("United States"),
55 EXPECT_EQ("CA", AutofillCountry::GetCountryCode(ASCIIToUTF16("Canada"),
58 // Case-insensitive mapping
59 EXPECT_EQ("US", AutofillCountry::GetCountryCode(ASCIIToUTF16("united states"),
62 // Country codes should map to themselves, independent of locale.
63 EXPECT_EQ("US", AutofillCountry::GetCountryCode(ASCIIToUTF16("US"), "en_US"));
64 EXPECT_EQ("HU", AutofillCountry::GetCountryCode(ASCIIToUTF16("hu"), "en_US"));
65 EXPECT_EQ("CA", AutofillCountry::GetCountryCode(ASCIIToUTF16("CA"), "fr_CA"));
66 EXPECT_EQ("MX", AutofillCountry::GetCountryCode(ASCIIToUTF16("mx"), "fr_CA"));
70 AutofillCountry::GetCountryCode(
71 ASCIIToUTF16("United States of America"), "en_US"));
72 EXPECT_EQ("US", AutofillCountry::GetCountryCode(ASCIIToUTF16("USA"),
77 AutofillCountry::GetCountryCode(ASCIIToUTF16("Estados Unidos"),
79 EXPECT_EQ("IT", AutofillCountry::GetCountryCode(ASCIIToUTF16("Italia"),
81 EXPECT_EQ("DE", AutofillCountry::GetCountryCode(ASCIIToUTF16("duitsland"),
84 // Should fall back to "en_US" locale if all else fails.
85 EXPECT_EQ("US", AutofillCountry::GetCountryCode(ASCIIToUTF16("United States"),
87 EXPECT_EQ("US", AutofillCountry::GetCountryCode(ASCIIToUTF16("united states"),
89 EXPECT_EQ("US", AutofillCountry::GetCountryCode(ASCIIToUTF16("USA"), "es"));
92 // Test mapping of empty country name to country code.
93 TEST(AutofillCountryTest
, EmptyCountryNameHasEmptyCountryCode
) {
94 EXPECT_TRUE(AutofillCountry::GetCountryCode(base::string16(), "en").empty());
97 // Test mapping all country codes to country names.
98 TEST(AutofillCountryTest
, AllCountryCodesHaveCountryName
) {
99 std::vector
<std::string
> country_codes
;
100 AutofillCountry::GetAvailableCountries(&country_codes
);
101 for (size_t i
= 0; i
< country_codes
.size(); ++i
) {
102 SCOPED_TRACE("Country code '" + country_codes
[i
] + "' should have a name.");
103 EXPECT_NE(ASCIIToUTF16(country_codes
[i
]),
104 AutofillCountry(country_codes
[i
], "en").name());
108 } // namespace autofill