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"
14 // Test the constructor and accessors
15 TEST(AutofillCountryTest, AutofillCountry) {
16 AutofillCountry united_states_en("US", "en_US");
17 EXPECT_EQ("US", united_states_en.country_code());
18 EXPECT_EQ(ASCIIToUTF16("United States"), united_states_en.name());
19 EXPECT_EQ(ASCIIToUTF16("ZIP code"), united_states_en.postal_code_label());
20 EXPECT_EQ(ASCIIToUTF16("State"), united_states_en.state_label());
22 AutofillCountry united_states_es("US", "es");
23 EXPECT_EQ("US", united_states_es.country_code());
24 EXPECT_EQ(ASCIIToUTF16("Estados Unidos"), united_states_es.name());
26 AutofillCountry canada_en("CA", "en_US");
27 EXPECT_EQ("CA", canada_en.country_code());
28 EXPECT_EQ(ASCIIToUTF16("Canada"), canada_en.name());
29 EXPECT_EQ(ASCIIToUTF16("Postal code"), canada_en.postal_code_label());
30 EXPECT_EQ(ASCIIToUTF16("Province"), canada_en.state_label());
32 AutofillCountry canada_hu("CA", "hu");
33 EXPECT_EQ("CA", canada_hu.country_code());
34 EXPECT_EQ(ASCIIToUTF16("Kanada"), canada_hu.name());
37 // Test locale to country code mapping.
38 TEST(AutofillCountryTest, CountryCodeForLocale) {
39 EXPECT_EQ("US", AutofillCountry::CountryCodeForLocale("en_US"));
40 EXPECT_EQ("CA", AutofillCountry::CountryCodeForLocale("fr_CA"));
41 EXPECT_EQ("FR", AutofillCountry::CountryCodeForLocale("fr"));
42 EXPECT_EQ("US", AutofillCountry::CountryCodeForLocale("Unknown"));
43 // "es-419" isn't associated with a country. See base/l10n/l10n_util.cc
44 // for details about this locale. Default to US.
45 EXPECT_EQ("US", AutofillCountry::CountryCodeForLocale("es-419"));
48 // Test mapping of localized country names to country codes.
49 TEST(AutofillCountryTest, GetCountryCode) {
51 EXPECT_EQ("US", AutofillCountry::GetCountryCode(ASCIIToUTF16("United States"),
53 EXPECT_EQ("CA", AutofillCountry::GetCountryCode(ASCIIToUTF16("Canada"),
56 // Case-insensitive mapping
57 EXPECT_EQ("US", AutofillCountry::GetCountryCode(ASCIIToUTF16("united states"),
60 // Country codes should map to themselves, independent of locale.
61 EXPECT_EQ("US", AutofillCountry::GetCountryCode(ASCIIToUTF16("US"), "en_US"));
62 EXPECT_EQ("HU", AutofillCountry::GetCountryCode(ASCIIToUTF16("hu"), "en_US"));
63 EXPECT_EQ("CA", AutofillCountry::GetCountryCode(ASCIIToUTF16("CA"), "fr_CA"));
64 EXPECT_EQ("MX", AutofillCountry::GetCountryCode(ASCIIToUTF16("mx"), "fr_CA"));
68 AutofillCountry::GetCountryCode(
69 ASCIIToUTF16("United States of America"), "en_US"));
70 EXPECT_EQ("US", AutofillCountry::GetCountryCode(ASCIIToUTF16("USA"),
75 AutofillCountry::GetCountryCode(ASCIIToUTF16("Estados Unidos"),
77 EXPECT_EQ("IT", AutofillCountry::GetCountryCode(ASCIIToUTF16("Italia"),
79 EXPECT_EQ("DE", AutofillCountry::GetCountryCode(ASCIIToUTF16("duitsland"),
82 // Should fall back to "en_US" locale if all else fails.
83 EXPECT_EQ("US", AutofillCountry::GetCountryCode(ASCIIToUTF16("United States"),
85 EXPECT_EQ("US", AutofillCountry::GetCountryCode(ASCIIToUTF16("united states"),
87 EXPECT_EQ("US", AutofillCountry::GetCountryCode(ASCIIToUTF16("USA"), "es"));
90 } // namespace autofill