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