OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <string> | 5 #include <string> |
6 | 6 |
7 #include "base/strings/string16.h" | 7 #include "base/strings/string16.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "components/autofill/core/browser/country_names.h" | 9 #include "components/autofill/core/browser/country_names.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 | 11 |
12 using base::ASCIIToUTF16; | 12 using base::ASCIIToUTF16; |
13 | 13 |
14 namespace autofill { | 14 namespace autofill { |
| 15 namespace { |
| 16 |
| 17 class TestCountryNames : public CountryNames { |
| 18 public: |
| 19 explicit TestCountryNames(const std::string& locale_name) |
| 20 : CountryNames(locale_name) {} |
| 21 |
| 22 ~TestCountryNames() = default; |
| 23 }; |
| 24 |
| 25 } // namespace |
15 | 26 |
16 // Test mapping of localized country names to country codes. | 27 // Test mapping of localized country names to country codes. |
17 TEST(CountryNamesTest, GetCountryCode) { | 28 TEST(CountryNamesTest, GetCountryCode_BasicMapping) { |
18 // Basic mapping | 29 TestCountryNames en_us_names("en_US"); |
19 EXPECT_EQ("US", CountryNames::GetInstance()->GetCountryCode( | 30 EXPECT_EQ("US", en_us_names.GetCountryCode(ASCIIToUTF16("United States"))); |
20 ASCIIToUTF16("United States"), "en_US")); | 31 EXPECT_EQ("CA", en_us_names.GetCountryCode(ASCIIToUTF16("Canada"))); |
21 EXPECT_EQ("CA", CountryNames::GetInstance()->GetCountryCode( | 32 } |
22 ASCIIToUTF16("Canada"), "en_US")); | |
23 | 33 |
24 // Case-insensitive mapping | 34 TEST(CountryNamesTest, GetCountryCode_CaseInsensitiveMapping) { |
25 EXPECT_EQ("US", CountryNames::GetInstance()->GetCountryCode( | 35 EXPECT_EQ("US", TestCountryNames("en_US") |
26 ASCIIToUTF16("united states"), "en_US")); | 36 .GetCountryCode(ASCIIToUTF16("united states"))); |
| 37 } |
27 | 38 |
28 // Country codes should map to themselves, independent of locale. | 39 TEST(CountryNamesTest, GetCountryCode_CodesMapToThemselves) { |
29 EXPECT_EQ("US", CountryNames::GetInstance()->GetCountryCode( | 40 TestCountryNames en_us_names("en_US"); |
30 ASCIIToUTF16("US"), "en_US")); | 41 TestCountryNames fr_ca_names("fr_CA"); |
31 EXPECT_EQ("HU", CountryNames::GetInstance()->GetCountryCode( | 42 EXPECT_EQ("US", en_us_names.GetCountryCode(ASCIIToUTF16("US"))); |
32 ASCIIToUTF16("hu"), "en_US")); | 43 EXPECT_EQ("HU", en_us_names.GetCountryCode(ASCIIToUTF16("hu"))); |
33 EXPECT_EQ("CA", CountryNames::GetInstance()->GetCountryCode( | 44 EXPECT_EQ("CA", fr_ca_names.GetCountryCode(ASCIIToUTF16("CA"))); |
34 ASCIIToUTF16("CA"), "fr_CA")); | 45 EXPECT_EQ("MX", fr_ca_names.GetCountryCode(ASCIIToUTF16("mx"))); |
35 EXPECT_EQ("MX", CountryNames::GetInstance()->GetCountryCode( | 46 } |
36 ASCIIToUTF16("mx"), "fr_CA")); | |
37 | 47 |
38 // Basic synonyms | 48 TEST(CountryNamesTest, GetCountryCode_BasicSynonyms) { |
39 EXPECT_EQ("US", CountryNames::GetInstance()->GetCountryCode( | 49 TestCountryNames en_us_names("en_US"); |
40 ASCIIToUTF16("United States of America"), "en_US")); | 50 EXPECT_EQ("US", en_us_names.GetCountryCode( |
41 EXPECT_EQ("US", CountryNames::GetInstance()->GetCountryCode( | 51 ASCIIToUTF16("United States of America"))); |
42 ASCIIToUTF16("USA"), "en_US")); | 52 EXPECT_EQ("US", en_us_names.GetCountryCode(ASCIIToUTF16("USA"))); |
| 53 } |
43 | 54 |
44 // Other locales | 55 TEST(CountryNamesTest, GetCountryCode_OtherLocales) { |
45 EXPECT_EQ("US", CountryNames::GetInstance()->GetCountryCode( | 56 EXPECT_EQ("US", TestCountryNames("es") |
46 ASCIIToUTF16("Estados Unidos"), "es")); | 57 .GetCountryCode(ASCIIToUTF16("Estados Unidos"))); |
47 EXPECT_EQ("IT", CountryNames::GetInstance()->GetCountryCode( | 58 EXPECT_EQ("IT", |
48 ASCIIToUTF16("Italia"), "it")); | 59 TestCountryNames("it").GetCountryCode(ASCIIToUTF16("Italia"))); |
49 EXPECT_EQ("DE", CountryNames::GetInstance()->GetCountryCode( | 60 EXPECT_EQ("DE", |
50 ASCIIToUTF16("duitsland"), "nl")); | 61 TestCountryNames("nl").GetCountryCode(ASCIIToUTF16("duitsland"))); |
| 62 } |
51 | 63 |
52 // Should fall back to "en_US" locale if all else fails. | 64 TEST(CountryNamesTest, GetCountryCode_EnUsFallback) { |
53 EXPECT_EQ("US", CountryNames::GetInstance()->GetCountryCode( | 65 TestCountryNames es_names("es"); |
54 ASCIIToUTF16("United States"), "es")); | 66 EXPECT_EQ("US", es_names.GetCountryCode(ASCIIToUTF16("United States"))); |
55 EXPECT_EQ("US", CountryNames::GetInstance()->GetCountryCode( | 67 EXPECT_EQ("US", es_names.GetCountryCode(ASCIIToUTF16("united states"))); |
56 ASCIIToUTF16("united states"), "es")); | 68 EXPECT_EQ("US", es_names.GetCountryCode(ASCIIToUTF16("USA"))); |
57 EXPECT_EQ("US", CountryNames::GetInstance()->GetCountryCode( | |
58 ASCIIToUTF16("USA"), "es")); | |
59 } | 69 } |
60 | 70 |
61 // Test mapping of empty country name to country code. | 71 // Test mapping of empty country name to country code. |
62 TEST(CountryNamesTest, EmptyCountryNameHasEmptyCountryCode) { | 72 TEST(CountryNamesTest, EmptyCountryNameHasEmptyCountryCode) { |
63 std::string country_code = | 73 std::string country_code = |
64 CountryNames::GetInstance()->GetCountryCode(base::string16(), "en"); | 74 TestCountryNames("en").GetCountryCode(base::string16()); |
65 EXPECT_TRUE(country_code.empty()) << country_code; | 75 EXPECT_TRUE(country_code.empty()) << country_code; |
66 } | 76 } |
67 | 77 |
68 } // namespace autofill | 78 } // namespace autofill |
OLD | NEW |