| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 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 <stddef.h> | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <memory> | |
| 9 #include <utility> | |
| 10 | |
| 11 #include "base/macros.h" | |
| 12 #include "base/path_service.h" | |
| 13 #include "base/strings/string_split.h" | |
| 14 #include "base/strings/string_util.h" | |
| 15 #include "base/strings/utf_string_conversions.h" | |
| 16 #include "chrome/renderer/spellchecker/spellcheck.h" | |
| 17 #include "chrome/renderer/spellchecker/spellcheck_provider_test.h" | |
| 18 #include "components/spellcheck/common/spellcheck_common.h" | |
| 19 #include "components/spellcheck/common/spellcheck_result.h" | |
| 20 #include "testing/gtest/include/gtest/gtest.h" | |
| 21 #include "third_party/WebKit/public/platform/WebString.h" | |
| 22 #include "third_party/WebKit/public/platform/WebVector.h" | |
| 23 #include "third_party/WebKit/public/web/WebTextCheckingResult.h" | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 struct SpellcheckTestCase { | |
| 28 // A string of text for checking. | |
| 29 const wchar_t* input; | |
| 30 // The position and the length of the first misspelled word, if any. | |
| 31 int expected_misspelling_start; | |
| 32 int expected_misspelling_length; | |
| 33 }; | |
| 34 | |
| 35 base::FilePath GetHunspellDirectory() { | |
| 36 base::FilePath hunspell_directory; | |
| 37 if (!PathService::Get(base::DIR_SOURCE_ROOT, &hunspell_directory)) | |
| 38 return base::FilePath(); | |
| 39 | |
| 40 hunspell_directory = hunspell_directory.AppendASCII("third_party"); | |
| 41 hunspell_directory = hunspell_directory.AppendASCII("hunspell_dictionaries"); | |
| 42 return hunspell_directory; | |
| 43 } | |
| 44 | |
| 45 } // namespace | |
| 46 | |
| 47 class MultilingualSpellCheckTest : public testing::Test { | |
| 48 public: | |
| 49 MultilingualSpellCheckTest() {} | |
| 50 | |
| 51 void ReinitializeSpellCheck(const std::string& unsplit_languages) { | |
| 52 spellcheck_ = new SpellCheck(); | |
| 53 provider_.reset(new TestingSpellCheckProvider(spellcheck_)); | |
| 54 InitializeSpellCheck(unsplit_languages); | |
| 55 } | |
| 56 | |
| 57 void InitializeSpellCheck(const std::string& unsplit_languages) { | |
| 58 base::FilePath hunspell_directory = GetHunspellDirectory(); | |
| 59 EXPECT_FALSE(hunspell_directory.empty()); | |
| 60 std::vector<std::string> languages = base::SplitString( | |
| 61 unsplit_languages, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | |
| 62 | |
| 63 for (const auto& language : languages) { | |
| 64 base::File file( | |
| 65 spellcheck::GetVersionedFileName(language, hunspell_directory), | |
| 66 base::File::FLAG_OPEN | base::File::FLAG_READ); | |
| 67 spellcheck_->AddSpellcheckLanguage(std::move(file), language); | |
| 68 } | |
| 69 } | |
| 70 | |
| 71 ~MultilingualSpellCheckTest() override {} | |
| 72 TestingSpellCheckProvider* provider() { return provider_.get(); } | |
| 73 | |
| 74 protected: | |
| 75 void ExpectSpellCheckWordResults(const std::string& languages, | |
| 76 const SpellcheckTestCase* test_cases, | |
| 77 size_t num_test_cases) { | |
| 78 ReinitializeSpellCheck(languages); | |
| 79 | |
| 80 for (size_t i = 0; i < num_test_cases; ++i) { | |
| 81 int misspelling_start = 0; | |
| 82 int misspelling_length = 0; | |
| 83 static_cast<blink::WebSpellCheckClient*>(provider()) | |
| 84 ->spellCheck(blink::WebString(base::WideToUTF16(test_cases[i].input)), | |
| 85 misspelling_start, misspelling_length, nullptr); | |
| 86 | |
| 87 EXPECT_EQ(test_cases[i].expected_misspelling_start, misspelling_start) | |
| 88 << "Improper misspelling location found with the languages " | |
| 89 << languages << " when checking \"" << test_cases[i].input << "\"."; | |
| 90 EXPECT_EQ(test_cases[i].expected_misspelling_length, misspelling_length) | |
| 91 << "Improper misspelling length found with the languages " | |
| 92 << languages << " when checking \"" << test_cases[i].input << "\"."; | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 void ExpectSpellCheckParagraphResults( | |
| 97 const base::string16& input, | |
| 98 const std::vector<SpellCheckResult>& expected) { | |
| 99 blink::WebVector<blink::WebTextCheckingResult> results; | |
| 100 spellcheck_->SpellCheckParagraph(blink::WebString(input), &results); | |
| 101 | |
| 102 EXPECT_EQ(expected.size(), results.size()); | |
| 103 size_t size = std::min(results.size(), expected.size()); | |
| 104 for (size_t i = 0; i < size; ++i) { | |
| 105 EXPECT_EQ(blink::WebTextDecorationTypeSpelling, results[i].decoration); | |
| 106 EXPECT_EQ(expected[i].location, results[i].location); | |
| 107 EXPECT_EQ(expected[i].length, results[i].length); | |
| 108 } | |
| 109 } | |
| 110 | |
| 111 private: | |
| 112 // Owned by |provider_|. | |
| 113 SpellCheck* spellcheck_; | |
| 114 std::unique_ptr<TestingSpellCheckProvider> provider_; | |
| 115 }; | |
| 116 | |
| 117 // Check that a string of different words is properly spellchecked for different | |
| 118 // combinations of different languages. | |
| 119 TEST_F(MultilingualSpellCheckTest, MultilingualSpellCheckWord) { | |
| 120 static const SpellcheckTestCase kTestCases[] = { | |
| 121 // An English, Spanish, Russian, and Greek word, all spelled correctly. | |
| 122 {L"rocket destruyan \x0432\x0441\x0435\x0445 \x03C4\x03B9\x03C2", 0, 0}, | |
| 123 // A misspelled English word. | |
| 124 {L"rocktt destruyan \x0432\x0441\x0435\x0445 \x03C4\x03B9\x03C2", 0, 6}, | |
| 125 // A misspelled Spanish word. | |
| 126 {L"rocket destruynn \x0432\x0441\x0435\x0445 \x03C4\x03B9\x03C2", 7, 9}, | |
| 127 // A misspelled Russian word. | |
| 128 {L"rocket destruyan \x0430\x0430\x0430\x0430 \x03C4\x03B9\x03C2", 17, 4}, | |
| 129 // A misspelled Greek word. | |
| 130 {L"rocket destruyan \x0432\x0441\x0435\x0445 \x03B1\x03B1\x03B1\x03B1", | |
| 131 22, 4}, | |
| 132 // An English word, then Russian, and then a misspelled English word. | |
| 133 {L"rocket \x0432\x0441\x0435\x0445 rocktt", 12, 6}, | |
| 134 }; | |
| 135 | |
| 136 // A sorted list of languages. This must start sorted to get all possible | |
| 137 // permutations. | |
| 138 std::string languages = "el-GR,en-US,es-ES,ru-RU"; | |
| 139 std::vector<std::string> permuted_languages = base::SplitString( | |
| 140 languages, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | |
| 141 | |
| 142 do { | |
| 143 languages = base::JoinString(permuted_languages, ","); | |
| 144 ExpectSpellCheckWordResults(languages, kTestCases, arraysize(kTestCases)); | |
| 145 } while (std::next_permutation(permuted_languages.begin(), | |
| 146 permuted_languages.end())); | |
| 147 } | |
| 148 | |
| 149 TEST_F(MultilingualSpellCheckTest, MultilingualSpellCheckWordEnglishSpanish) { | |
| 150 static const SpellcheckTestCase kTestCases[] = { | |
| 151 {L"", 0, 0}, | |
| 152 {L"head hand foot legs arms", 0, 0}, | |
| 153 {L"head hand foot legs arms zzzz", 25, 4}, | |
| 154 {L"head hand zzzz foot legs arms", 10, 4}, | |
| 155 {L"zzzz head hand foot legs arms", 0, 4}, | |
| 156 {L"zzzz head zzzz foot zzzz arms", 0, 4}, | |
| 157 {L"head hand foot arms zzzz zzzz", 20, 4}, | |
| 158 {L"I do not want a monstrous snake near me.", 0, 0}, | |
| 159 {L"zz do not want a monstrous snake near me.", 0, 2}, | |
| 160 {L"I do not want zz monstrous snake near me.", 14, 2}, | |
| 161 {L"I do not want a monstrous zz near me.", 26, 2}, | |
| 162 {L"I do not want a monstrou snake near me.", 16, 8}, | |
| 163 {L"I do not want a monstrous snake near zz.", 37, 2}, | |
| 164 {L"Partially Spanish is very bueno.", 0, 0}, | |
| 165 {L"Sleeping in the biblioteca is good.", 0, 0}, | |
| 166 {L"Hermano is my favorite name.", 0, 0}, | |
| 167 {L"hola hola hola hola hola hola", 0, 0}, | |
| 168 {L"sand hola hola hola hola hola", 0, 0}, | |
| 169 {L"hola sand sand sand sand sand", 0, 0}, | |
| 170 {L"sand sand sand sand sand hola", 0, 0}, | |
| 171 {L"sand hola sand hola sand hola", 0, 0}, | |
| 172 {L"hola sand hola sand hola sand", 0, 0}, | |
| 173 {L"hola:legs", 0, 9}, | |
| 174 {L"legs:hola", 0, 9}}; | |
| 175 ExpectSpellCheckWordResults("en-US,es-ES", kTestCases, arraysize(kTestCases)); | |
| 176 } | |
| 177 | |
| 178 // If there are no spellcheck languages, no text should be marked as misspelled. | |
| 179 TEST_F(MultilingualSpellCheckTest, MultilingualSpellCheckParagraphBlank) { | |
| 180 ReinitializeSpellCheck(std::string()); | |
| 181 | |
| 182 ExpectSpellCheckParagraphResults( | |
| 183 // English, German, Spanish, and a misspelled word. | |
| 184 base::UTF8ToUTF16("rocket Schwarzkommando destruyan pcnyhon"), | |
| 185 std::vector<SpellCheckResult>()); | |
| 186 } | |
| 187 | |
| 188 // Make sure nothing is considered misspelled when at least one of the selected | |
| 189 // languages determines that a word is correctly spelled. | |
| 190 TEST_F(MultilingualSpellCheckTest, MultilingualSpellCheckParagraphCorrect) { | |
| 191 ReinitializeSpellCheck("en-US,es-ES,de-DE"); | |
| 192 | |
| 193 ExpectSpellCheckParagraphResults( | |
| 194 // English, German, and Spanish words, all spelled correctly. | |
| 195 base::UTF8ToUTF16("rocket Schwarzkommando destruyan"), | |
| 196 std::vector<SpellCheckResult>()); | |
| 197 } | |
| 198 | |
| 199 // Make sure that all the misspellings in the text are found. | |
| 200 TEST_F(MultilingualSpellCheckTest, MultilingualSpellCheckParagraph) { | |
| 201 ReinitializeSpellCheck("en-US,es-ES"); | |
| 202 std::vector<SpellCheckResult> expected; | |
| 203 expected.push_back(SpellCheckResult(SpellCheckResult::SPELLING, 7, 15)); | |
| 204 expected.push_back(SpellCheckResult(SpellCheckResult::SPELLING, 33, 7)); | |
| 205 | |
| 206 ExpectSpellCheckParagraphResults( | |
| 207 // English, German, Spanish, and a misspelled word. | |
| 208 base::UTF8ToUTF16("rocket Schwarzkommando destruyan pcnyhon"), expected); | |
| 209 } | |
| 210 | |
| 211 // Ensure that suggestions are handled properly for multiple languages. | |
| 212 TEST_F(MultilingualSpellCheckTest, MultilingualSpellCheckSuggestions) { | |
| 213 ReinitializeSpellCheck("en-US,es-ES"); | |
| 214 static const struct { | |
| 215 // A string of text for checking. | |
| 216 const wchar_t* input; | |
| 217 // The position and the length of the first invalid word. | |
| 218 int expected_misspelling_start; | |
| 219 int expected_misspelling_length; | |
| 220 // A comma separated string of suggested words that should occur, in their | |
| 221 // expected order. | |
| 222 const wchar_t* expected_suggestions; | |
| 223 } kTestCases[] = { | |
| 224 {L"rocket", 0, 0}, | |
| 225 {L"destruyan", 0, 0}, | |
| 226 {L"rocet", 0, 5, L"rocket,roce,crochet,troce,rocen"}, | |
| 227 {L"jum", 0, 3, L"hum,jun,ju,um,juma"}, | |
| 228 {L"asdne", 0, 5, L"sadness,desasne"}, | |
| 229 }; | |
| 230 | |
| 231 for (size_t i = 0; i < arraysize(kTestCases); ++i) { | |
| 232 blink::WebVector<blink::WebString> suggestions; | |
| 233 int misspelling_start; | |
| 234 int misspelling_length; | |
| 235 static_cast<blink::WebSpellCheckClient*>(provider()) | |
| 236 ->spellCheck(blink::WebString(base::WideToUTF16(kTestCases[i].input)), | |
| 237 misspelling_start, misspelling_length, &suggestions); | |
| 238 | |
| 239 EXPECT_EQ(kTestCases[i].expected_misspelling_start, misspelling_start); | |
| 240 EXPECT_EQ(kTestCases[i].expected_misspelling_length, misspelling_length); | |
| 241 if (!kTestCases[i].expected_suggestions) { | |
| 242 EXPECT_EQ(0UL, suggestions.size()); | |
| 243 continue; | |
| 244 } | |
| 245 | |
| 246 std::vector<base::string16> expected_suggestions = base::SplitString( | |
| 247 base::WideToUTF16(kTestCases[i].expected_suggestions), | |
| 248 base::string16(1, ','), base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | |
| 249 | |
| 250 EXPECT_EQ(expected_suggestions.size(), suggestions.size()); | |
| 251 for (size_t j = 0; | |
| 252 j < std::min(expected_suggestions.size(), suggestions.size()); j++) { | |
| 253 EXPECT_EQ(expected_suggestions[j], base::string16(suggestions[j])); | |
| 254 } | |
| 255 } | |
| 256 } | |
| OLD | NEW |