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