| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "components/translate/language_detection/language_detection_util.h" | 5 #include "components/translate/language_detection/language_detection_util.h" |
| 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/translate/common/translate_constants.h" | 9 #include "components/translate/common/translate_constants.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 EXPECT_TRUE(translate::MaybeServerWrongConfiguration("en", "ja")); | 87 EXPECT_TRUE(translate::MaybeServerWrongConfiguration("en", "ja")); |
| 88 EXPECT_TRUE(translate::MaybeServerWrongConfiguration("en-US", "ja")); | 88 EXPECT_TRUE(translate::MaybeServerWrongConfiguration("en-US", "ja")); |
| 89 EXPECT_TRUE(translate::MaybeServerWrongConfiguration("en", "zh-CN")); | 89 EXPECT_TRUE(translate::MaybeServerWrongConfiguration("en", "zh-CN")); |
| 90 EXPECT_FALSE(translate::MaybeServerWrongConfiguration("ja", "en")); | 90 EXPECT_FALSE(translate::MaybeServerWrongConfiguration("ja", "en")); |
| 91 EXPECT_FALSE(translate::MaybeServerWrongConfiguration("en", "he")); | 91 EXPECT_FALSE(translate::MaybeServerWrongConfiguration("en", "he")); |
| 92 } | 92 } |
| 93 | 93 |
| 94 // Tests that the language meta tag providing wrong information is ignored by | 94 // Tests that the language meta tag providing wrong information is ignored by |
| 95 // LanguageDetectionUtil due to disagreement between meta tag and CLD. | 95 // LanguageDetectionUtil due to disagreement between meta tag and CLD. |
| 96 TEST_F(LanguageDetectionUtilTest, CLDDisagreeWithWrongLanguageCode) { | 96 TEST_F(LanguageDetectionUtilTest, CLDDisagreeWithWrongLanguageCode) { |
| 97 base::string16 contents = ASCIIToUTF16( | 97 base::string16 contents = base::ASCIIToUTF16( |
| 98 "<html><head><meta http-equiv='Content-Language' content='ja'></head>" | 98 "<html><head><meta http-equiv='Content-Language' content='ja'></head>" |
| 99 "<body>This is a page apparently written in English. Even though " | 99 "<body>This is a page apparently written in English. Even though " |
| 100 "content-language is provided, the value will be ignored if the value " | 100 "content-language is provided, the value will be ignored if the value " |
| 101 "is suspicious.</body></html>"); | 101 "is suspicious.</body></html>"); |
| 102 std::string cld_language; | 102 std::string cld_language; |
| 103 bool is_cld_reliable; | 103 bool is_cld_reliable; |
| 104 std::string language = translate::DeterminePageLanguage(std::string("ja"), | 104 std::string language = translate::DeterminePageLanguage(std::string("ja"), |
| 105 std::string(), | 105 std::string(), |
| 106 contents, | 106 contents, |
| 107 &cld_language, | 107 &cld_language, |
| 108 &is_cld_reliable); | 108 &is_cld_reliable); |
| 109 EXPECT_EQ(translate::kUnknownLanguageCode, language); | 109 EXPECT_EQ(translate::kUnknownLanguageCode, language); |
| 110 EXPECT_EQ("en", cld_language); | 110 EXPECT_EQ("en", cld_language); |
| 111 EXPECT_TRUE(is_cld_reliable); | 111 EXPECT_TRUE(is_cld_reliable); |
| 112 } | 112 } |
| 113 | 113 |
| 114 // Tests that the language meta tag providing "en-US" style information is | 114 // Tests that the language meta tag providing "en-US" style information is |
| 115 // agreed by CLD. | 115 // agreed by CLD. |
| 116 TEST_F(LanguageDetectionUtilTest, CLDAgreeWithLanguageCodeHavingCountryCode) { | 116 TEST_F(LanguageDetectionUtilTest, CLDAgreeWithLanguageCodeHavingCountryCode) { |
| 117 base::string16 contents = ASCIIToUTF16( | 117 base::string16 contents = base::ASCIIToUTF16( |
| 118 "<html><head><meta http-equiv='Content-Language' content='en-US'></head>" | 118 "<html><head><meta http-equiv='Content-Language' content='en-US'></head>" |
| 119 "<body>This is a page apparently written in English. Even though " | 119 "<body>This is a page apparently written in English. Even though " |
| 120 "content-language is provided, the value will be ignored if the value " | 120 "content-language is provided, the value will be ignored if the value " |
| 121 "is suspicious.</body></html>"); | 121 "is suspicious.</body></html>"); |
| 122 std::string cld_language; | 122 std::string cld_language; |
| 123 bool is_cld_reliable; | 123 bool is_cld_reliable; |
| 124 std::string language = translate::DeterminePageLanguage(std::string("en-US"), | 124 std::string language = translate::DeterminePageLanguage(std::string("en-US"), |
| 125 std::string(), | 125 std::string(), |
| 126 contents, | 126 contents, |
| 127 &cld_language, | 127 &cld_language, |
| 128 &is_cld_reliable); | 128 &is_cld_reliable); |
| 129 EXPECT_EQ("en-US", language); | 129 EXPECT_EQ("en-US", language); |
| 130 EXPECT_EQ("en", cld_language); | 130 EXPECT_EQ("en", cld_language); |
| 131 EXPECT_TRUE(is_cld_reliable); | 131 EXPECT_TRUE(is_cld_reliable); |
| 132 } | 132 } |
| 133 | 133 |
| 134 // Tests that the language meta tag providing wrong information is ignored and | 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 | 135 // CLD's language will be adopted by LanguageDetectionUtil due to an invalid |
| 136 // meta tag. | 136 // meta tag. |
| 137 TEST_F(LanguageDetectionUtilTest, InvalidLanguageMetaTagProviding) { | 137 TEST_F(LanguageDetectionUtilTest, InvalidLanguageMetaTagProviding) { |
| 138 base::string16 contents = ASCIIToUTF16( | 138 base::string16 contents = base::ASCIIToUTF16( |
| 139 "<html><head><meta http-equiv='Content-Language' content='utf-8'></head>" | 139 "<html><head><meta http-equiv='Content-Language' content='utf-8'></head>" |
| 140 "<body>This is a page apparently written in English. Even though " | 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" | 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>"); | 142 " language will be adopted if the value is invalid.</body></html>"); |
| 143 std::string cld_language; | 143 std::string cld_language; |
| 144 bool is_cld_reliable; | 144 bool is_cld_reliable; |
| 145 std::string language = translate::DeterminePageLanguage(std::string("utf-8"), | 145 std::string language = translate::DeterminePageLanguage(std::string("utf-8"), |
| 146 std::string(), | 146 std::string(), |
| 147 contents, | 147 contents, |
| 148 &cld_language, | 148 &cld_language, |
| 149 &is_cld_reliable); | 149 &is_cld_reliable); |
| 150 EXPECT_EQ("en", language); | 150 EXPECT_EQ("en", language); |
| 151 EXPECT_EQ("en", cld_language); | 151 EXPECT_EQ("en", cld_language); |
| 152 EXPECT_TRUE(is_cld_reliable); | 152 EXPECT_TRUE(is_cld_reliable); |
| 153 } | 153 } |
| 154 | 154 |
| 155 // Tests that the language meta tag providing wrong information is ignored | 155 // Tests that the language meta tag providing wrong information is ignored |
| 156 // because of valid html lang attribute. | 156 // because of valid html lang attribute. |
| 157 TEST_F(LanguageDetectionUtilTest, AdoptHtmlLang) { | 157 TEST_F(LanguageDetectionUtilTest, AdoptHtmlLang) { |
| 158 base::string16 contents = ASCIIToUTF16( | 158 base::string16 contents = base::ASCIIToUTF16( |
| 159 "<html lang='en'><head><meta http-equiv='Content-Language' content='ja'>" | 159 "<html lang='en'><head><meta http-equiv='Content-Language' content='ja'>" |
| 160 "</head><body>This is a page apparently written in English. Even though " | 160 "</head><body>This is a page apparently written in English. Even though " |
| 161 "content-language is provided, the value will be ignored if the value " | 161 "content-language is provided, the value will be ignored if the value " |
| 162 "is suspicious.</body></html>"); | 162 "is suspicious.</body></html>"); |
| 163 std::string cld_language; | 163 std::string cld_language; |
| 164 bool is_cld_reliable; | 164 bool is_cld_reliable; |
| 165 std::string language = translate::DeterminePageLanguage(std::string("ja"), | 165 std::string language = translate::DeterminePageLanguage(std::string("ja"), |
| 166 std::string("en"), | 166 std::string("en"), |
| 167 contents, | 167 contents, |
| 168 &cld_language, | 168 &cld_language, |
| 169 &is_cld_reliable); | 169 &is_cld_reliable); |
| 170 EXPECT_EQ("en", language); | 170 EXPECT_EQ("en", language); |
| 171 EXPECT_EQ("en", cld_language); | 171 EXPECT_EQ("en", cld_language); |
| 172 EXPECT_TRUE(is_cld_reliable); | 172 EXPECT_TRUE(is_cld_reliable); |
| 173 } | 173 } |
| OLD | NEW |