autofill_country_unittest.cc revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
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.
4
5#include <string>
6
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"
11
12namespace autofill {
13
14// Test the constructor and accessors
15TEST(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());
21
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());
25
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());
31
32  AutofillCountry canada_hu("CA", "hu");
33  EXPECT_EQ("CA", canada_hu.country_code());
34  EXPECT_EQ(ASCIIToUTF16("Kanada"), canada_hu.name());
35}
36
37// Test locale to country code mapping.
38TEST(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"));
46}
47
48// Test mapping of localized country names to country codes.
49TEST(AutofillCountryTest, GetCountryCode) {
50  // Basic mapping
51  EXPECT_EQ("US", AutofillCountry::GetCountryCode(ASCIIToUTF16("United States"),
52                                                  "en_US"));
53  EXPECT_EQ("CA", AutofillCountry::GetCountryCode(ASCIIToUTF16("Canada"),
54                                                  "en_US"));
55
56  // Case-insensitive mapping
57  EXPECT_EQ("US", AutofillCountry::GetCountryCode(ASCIIToUTF16("united states"),
58                                                  "en_US"));
59
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"));
65
66  // Basic synonyms
67  EXPECT_EQ("US",
68            AutofillCountry::GetCountryCode(
69                ASCIIToUTF16("United States of America"), "en_US"));
70  EXPECT_EQ("US", AutofillCountry::GetCountryCode(ASCIIToUTF16("USA"),
71                                                  "en_US"));
72
73  // Other locales
74  EXPECT_EQ("US",
75            AutofillCountry::GetCountryCode(ASCIIToUTF16("Estados Unidos"),
76                                            "es"));
77  EXPECT_EQ("IT", AutofillCountry::GetCountryCode(ASCIIToUTF16("Italia"),
78                                                  "it"));
79  EXPECT_EQ("DE", AutofillCountry::GetCountryCode(ASCIIToUTF16("duitsland"),
80                                                  "nl"));
81
82  // Should fall back to "en_US" locale if all else fails.
83  EXPECT_EQ("US", AutofillCountry::GetCountryCode(ASCIIToUTF16("United States"),
84                                                  "es"));
85  EXPECT_EQ("US", AutofillCountry::GetCountryCode(ASCIIToUTF16("united states"),
86                                                  "es"));
87  EXPECT_EQ("US", AutofillCountry::GetCountryCode(ASCIIToUTF16("USA"), "es"));
88}
89
90}  // namespace autofill
91