1 // Copyright (c) 2011 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.
4
5 #include <string>
6
7 #include "base/string16.h"
8 #include "base/utf_string_conversions.h"
9 #include "chrome/browser/autofill/autofill_country.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 // Test the constructor and accessors
TEST(AutofillCountryTest,AutofillCountry)13 TEST(AutofillCountryTest, AutofillCountry) {
14 AutofillCountry united_states_en("US", "en_US");
15 EXPECT_EQ("US", united_states_en.country_code());
16 EXPECT_EQ(ASCIIToUTF16("United States"), united_states_en.name());
17 EXPECT_EQ(ASCIIToUTF16("ZIP code"), united_states_en.postal_code_label());
18 EXPECT_EQ(ASCIIToUTF16("State"), united_states_en.state_label());
19
20 AutofillCountry united_states_es("US", "es");
21 EXPECT_EQ("US", united_states_es.country_code());
22 EXPECT_EQ(ASCIIToUTF16("Estados Unidos"), united_states_es.name());
23
24 AutofillCountry canada_en("CA", "en_US");
25 EXPECT_EQ("CA", canada_en.country_code());
26 EXPECT_EQ(ASCIIToUTF16("Canada"), canada_en.name());
27 EXPECT_EQ(ASCIIToUTF16("Postal code"), canada_en.postal_code_label());
28 EXPECT_EQ(ASCIIToUTF16("Province"), canada_en.state_label());
29
30 AutofillCountry canada_hu("CA", "hu");
31 EXPECT_EQ("CA", canada_hu.country_code());
32 EXPECT_EQ(ASCIIToUTF16("Kanada"), canada_hu.name());
33 }
34
35 // Test locale to country code mapping.
TEST(AutofillCountryTest,CountryCodeForLocale)36 TEST(AutofillCountryTest, CountryCodeForLocale) {
37 EXPECT_EQ("US", AutofillCountry::CountryCodeForLocale("en_US"));
38 EXPECT_EQ("CA", AutofillCountry::CountryCodeForLocale("fr_CA"));
39 EXPECT_EQ("FR", AutofillCountry::CountryCodeForLocale("fr"));
40 EXPECT_EQ("US", AutofillCountry::CountryCodeForLocale("Unknown"));
41 // "es-419" isn't associated with a country. See base/l10n/l10n_util.cc
42 // for details about this locale. Default to US.
43 EXPECT_EQ("US", AutofillCountry::CountryCodeForLocale("es-419"));
44 }
45
46 // Test mapping of localized country names to country codes.
TEST(AutofillCountryTest,GetCountryCode)47 TEST(AutofillCountryTest, GetCountryCode) {
48 // Basic mapping
49 EXPECT_EQ("US", AutofillCountry::GetCountryCode(ASCIIToUTF16("United States"),
50 "en_US"));
51 EXPECT_EQ("CA", AutofillCountry::GetCountryCode(ASCIIToUTF16("Canada"),
52 "en_US"));
53
54 // Case-insensitive mapping
55 EXPECT_EQ("US", AutofillCountry::GetCountryCode(ASCIIToUTF16("united states"),
56 "en_US"));
57
58 // Country codes should map to themselves, independent of locale.
59 EXPECT_EQ("US", AutofillCountry::GetCountryCode(ASCIIToUTF16("US"), "en_US"));
60 EXPECT_EQ("HU", AutofillCountry::GetCountryCode(ASCIIToUTF16("hu"), "en_US"));
61 EXPECT_EQ("CA", AutofillCountry::GetCountryCode(ASCIIToUTF16("CA"), "fr_CA"));
62 EXPECT_EQ("MX", AutofillCountry::GetCountryCode(ASCIIToUTF16("mx"), "fr_CA"));
63
64 // Basic synonyms
65 EXPECT_EQ("US",
66 AutofillCountry::GetCountryCode(
67 ASCIIToUTF16("United States of America"), "en_US"));
68 EXPECT_EQ("US", AutofillCountry::GetCountryCode(ASCIIToUTF16("USA"),
69 "en_US"));
70
71 // Other locales
72 EXPECT_EQ("US",
73 AutofillCountry::GetCountryCode(ASCIIToUTF16("Estados Unidos"),
74 "es"));
75 EXPECT_EQ("IT", AutofillCountry::GetCountryCode(ASCIIToUTF16("Italia"),
76 "it"));
77 EXPECT_EQ("DE", AutofillCountry::GetCountryCode(ASCIIToUTF16("duitsland"),
78 "nl"));
79
80 // Should fall back to "en_US" locale if all else fails.
81 EXPECT_EQ("US", AutofillCountry::GetCountryCode(ASCIIToUTF16("United States"),
82 "es"));
83 EXPECT_EQ("US", AutofillCountry::GetCountryCode(ASCIIToUTF16("united states"),
84 "es"));
85 EXPECT_EQ("US", AutofillCountry::GetCountryCode(ASCIIToUTF16("USA"), "es"));
86 }
87