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