| OLD | NEW |
| (Empty) |
| 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 "chrome/common/translate/language_detection_util.h" | |
| 6 | |
| 7 #include "base/strings/string16.h" | |
| 8 #include "base/strings/utf_string_conversions.h" | |
| 9 #include "chrome/common/chrome_constants.h" | |
| 10 #include "testing/gtest/include/gtest/gtest.h" | |
| 11 | |
| 12 typedef testing::Test LanguageDetectionUtilTest; | |
| 13 | |
| 14 // Tests that well-known language code typos are fixed. | |
| 15 TEST_F(LanguageDetectionUtilTest, LanguageCodeTypoCorrection) { | |
| 16 std::string language; | |
| 17 | |
| 18 // Strip the second and later codes. | |
| 19 language = std::string("ja,en"); | |
| 20 LanguageDetectionUtil::CorrectLanguageCodeTypo(&language); | |
| 21 EXPECT_EQ("ja", language); | |
| 22 | |
| 23 // Replace dash with hyphen. | |
| 24 language = std::string("ja_JP"); | |
| 25 LanguageDetectionUtil::CorrectLanguageCodeTypo(&language); | |
| 26 EXPECT_EQ("ja-JP", language); | |
| 27 | |
| 28 // Correct wrong cases. | |
| 29 language = std::string("JA-jp"); | |
| 30 LanguageDetectionUtil::CorrectLanguageCodeTypo(&language); | |
| 31 EXPECT_EQ("ja-JP", language); | |
| 32 } | |
| 33 | |
| 34 // Tests if the language codes' format is invalid. | |
| 35 TEST_F(LanguageDetectionUtilTest, IsValidLanguageCode) { | |
| 36 std::string language; | |
| 37 | |
| 38 language = std::string("ja"); | |
| 39 EXPECT_TRUE(LanguageDetectionUtil::IsValidLanguageCode(language)); | |
| 40 | |
| 41 language = std::string("ja-JP"); | |
| 42 EXPECT_TRUE(LanguageDetectionUtil::IsValidLanguageCode(language)); | |
| 43 | |
| 44 language = std::string("ceb"); | |
| 45 EXPECT_TRUE(LanguageDetectionUtil::IsValidLanguageCode(language)); | |
| 46 | |
| 47 language = std::string("ceb-XX"); | |
| 48 EXPECT_TRUE(LanguageDetectionUtil::IsValidLanguageCode(language)); | |
| 49 | |
| 50 // Invalid because the sub code consists of a number. | |
| 51 language = std::string("utf-8"); | |
| 52 EXPECT_FALSE(LanguageDetectionUtil::IsValidLanguageCode(language)); | |
| 53 | |
| 54 // Invalid because of six characters after hyphen. | |
| 55 language = std::string("ja-YUKARI"); | |
| 56 EXPECT_FALSE(LanguageDetectionUtil::IsValidLanguageCode(language)); | |
| 57 | |
| 58 // Invalid because of four characters. | |
| 59 language = std::string("DHMO"); | |
| 60 EXPECT_FALSE(LanguageDetectionUtil::IsValidLanguageCode(language)); | |
| 61 } | |
| 62 | |
| 63 // Tests that similar language table works. | |
| 64 TEST_F(LanguageDetectionUtilTest, SimilarLanguageCode) { | |
| 65 EXPECT_TRUE(LanguageDetectionUtil::IsSameOrSimilarLanguages("en", "en")); | |
| 66 EXPECT_FALSE(LanguageDetectionUtil::IsSameOrSimilarLanguages("en", "ja")); | |
| 67 | |
| 68 // Language codes are same if the main parts are same. The synonyms should be | |
| 69 // took into account (ex: 'iw' and 'he'). | |
| 70 EXPECT_TRUE(LanguageDetectionUtil::IsSameOrSimilarLanguages("sr-ME", "sr")); | |
| 71 EXPECT_TRUE(LanguageDetectionUtil::IsSameOrSimilarLanguages("sr", "sr-ME")); | |
| 72 EXPECT_TRUE(LanguageDetectionUtil::IsSameOrSimilarLanguages("he", "he-IL")); | |
| 73 EXPECT_TRUE(LanguageDetectionUtil::IsSameOrSimilarLanguages("eng", "eng-US")); | |
| 74 EXPECT_TRUE(LanguageDetectionUtil::IsSameOrSimilarLanguages("eng-US", "eng")); | |
| 75 EXPECT_FALSE(LanguageDetectionUtil::IsSameOrSimilarLanguages("eng", "enm")); | |
| 76 | |
| 77 // Even though the main parts are different, some special language pairs are | |
| 78 // recognized as same languages. | |
| 79 EXPECT_TRUE(LanguageDetectionUtil::IsSameOrSimilarLanguages("bs", "hr")); | |
| 80 EXPECT_TRUE(LanguageDetectionUtil::IsSameOrSimilarLanguages("ne", "hi")); | |
| 81 EXPECT_FALSE(LanguageDetectionUtil::IsSameOrSimilarLanguages("bs", "hi")); | |
| 82 } | |
| 83 | |
| 84 // Tests that well-known languages which often have wrong server configuration | |
| 85 // are handles. | |
| 86 TEST_F(LanguageDetectionUtilTest, WellKnownWrongConfiguration) { | |
| 87 EXPECT_TRUE(LanguageDetectionUtil::MaybeServerWrongConfiguration("en", "ja")); | |
| 88 EXPECT_TRUE(LanguageDetectionUtil::MaybeServerWrongConfiguration("en-US", | |
| 89 "ja")); | |
| 90 EXPECT_TRUE(LanguageDetectionUtil::MaybeServerWrongConfiguration("en", | |
| 91 "zh-CN")); | |
| 92 EXPECT_FALSE(LanguageDetectionUtil::MaybeServerWrongConfiguration("ja", | |
| 93 "en")); | |
| 94 EXPECT_FALSE(LanguageDetectionUtil::MaybeServerWrongConfiguration("en", | |
| 95 "he")); | |
| 96 } | |
| 97 | |
| 98 // Tests that the language meta tag providing wrong information is ignored by | |
| 99 // LanguageDetectionUtil due to disagreement between meta tag and CLD. | |
| 100 TEST_F(LanguageDetectionUtilTest, CLDDisagreeWithWrongLanguageCode) { | |
| 101 base::string16 contents = ASCIIToUTF16( | |
| 102 "<html><head><meta http-equiv='Content-Language' content='ja'></head>" | |
| 103 "<body>This is a page apparently written in English. Even though " | |
| 104 "content-language is provided, the value will be ignored if the value " | |
| 105 "is suspicious.</body></html>"); | |
| 106 std::string cld_language; | |
| 107 bool is_cld_reliable; | |
| 108 std::string language = LanguageDetectionUtil::DeterminePageLanguage( | |
| 109 std::string("ja"), std::string(), contents, &cld_language, | |
| 110 &is_cld_reliable); | |
| 111 EXPECT_EQ(chrome::kUnknownLanguageCode, language); | |
| 112 EXPECT_EQ("en", cld_language); | |
| 113 EXPECT_TRUE(is_cld_reliable); | |
| 114 } | |
| 115 | |
| 116 // Tests that the language meta tag providing "en-US" style information is | |
| 117 // agreed by CLD. | |
| 118 TEST_F(LanguageDetectionUtilTest, CLDAgreeWithLanguageCodeHavingCountryCode) { | |
| 119 base::string16 contents = ASCIIToUTF16( | |
| 120 "<html><head><meta http-equiv='Content-Language' content='en-US'></head>" | |
| 121 "<body>This is a page apparently written in English. Even though " | |
| 122 "content-language is provided, the value will be ignored if the value " | |
| 123 "is suspicious.</body></html>"); | |
| 124 std::string cld_language; | |
| 125 bool is_cld_reliable; | |
| 126 std::string language = LanguageDetectionUtil::DeterminePageLanguage( | |
| 127 std::string("en-US"), std::string(), contents, &cld_language, | |
| 128 &is_cld_reliable); | |
| 129 EXPECT_EQ("en-US", language); | |
| 130 EXPECT_EQ("en", cld_language); | |
| 131 EXPECT_TRUE(is_cld_reliable); | |
| 132 } | |
| 133 | |
| 134 // Tests that the language meta tag providing wrong information is ignored and | |
| 135 // CLD's language will be adopted by LanguageDetectionUtil due to an invalid | |
| 136 // meta tag. | |
| 137 TEST_F(LanguageDetectionUtilTest, InvalidLanguageMetaTagProviding) { | |
| 138 base::string16 contents = ASCIIToUTF16( | |
| 139 "<html><head><meta http-equiv='Content-Language' content='utf-8'></head>" | |
| 140 "<body>This is a page apparently written in English. Even though " | |
| 141 "content-language is provided, the value will be ignored and CLD's" | |
| 142 " language will be adopted if the value is invalid.</body></html>"); | |
| 143 std::string cld_language; | |
| 144 bool is_cld_reliable; | |
| 145 std::string language = LanguageDetectionUtil::DeterminePageLanguage( | |
| 146 std::string("utf-8"), std::string(), contents, &cld_language, | |
| 147 &is_cld_reliable); | |
| 148 EXPECT_EQ("en", language); | |
| 149 EXPECT_EQ("en", cld_language); | |
| 150 EXPECT_TRUE(is_cld_reliable); | |
| 151 } | |
| 152 | |
| 153 // Tests that the language meta tag providing wrong information is ignored | |
| 154 // because of valid html lang attribute. | |
| 155 TEST_F(LanguageDetectionUtilTest, AdoptHtmlLang) { | |
| 156 base::string16 contents = ASCIIToUTF16( | |
| 157 "<html lang='en'><head><meta http-equiv='Content-Language' content='ja'>" | |
| 158 "</head><body>This is a page apparently written in English. Even though " | |
| 159 "content-language is provided, the value will be ignored if the value " | |
| 160 "is suspicious.</body></html>"); | |
| 161 std::string cld_language; | |
| 162 bool is_cld_reliable; | |
| 163 std::string language = LanguageDetectionUtil::DeterminePageLanguage( | |
| 164 std::string("ja"), std::string("en"), contents, &cld_language, | |
| 165 &is_cld_reliable); | |
| 166 EXPECT_EQ("en", language); | |
| 167 EXPECT_EQ("en", cld_language); | |
| 168 EXPECT_TRUE(is_cld_reliable); | |
| 169 } | |
| OLD | NEW |