Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / chrome / renderer / spellchecker / spellcheck_unittest.cc
blob09b0ba9a2255747a1b2f1f2f5b817087f67b7f78
1 // Copyright (c) 2012 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.h"
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/path_service.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/common/spellcheck_common.h"
13 #include "chrome/common/spellcheck_result.h"
14 #include "chrome/renderer/spellchecker/hunspell_engine.h"
15 #include "chrome/renderer/spellchecker/spellcheck_language.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "third_party/WebKit/public/platform/WebVector.h"
18 #include "third_party/WebKit/public/web/WebTextCheckingCompletion.h"
19 #include "third_party/WebKit/public/web/WebTextCheckingResult.h"
21 #define TYPOGRAPHICAL_APOSTROPHE L"\x2019"
23 namespace {
24 const int kNoOffset = 0;
25 const int kNoTag = 0;
27 base::FilePath GetHunspellDirectory() {
28 base::FilePath hunspell_directory;
29 if (!PathService::Get(base::DIR_SOURCE_ROOT, &hunspell_directory))
30 return base::FilePath();
32 hunspell_directory = hunspell_directory.AppendASCII("third_party");
33 hunspell_directory = hunspell_directory.AppendASCII("hunspell_dictionaries");
34 return hunspell_directory;
37 } // namespace
39 // TODO(groby): This needs to be a BrowserTest for OSX.
40 class SpellCheckTest : public testing::Test {
41 public:
42 SpellCheckTest() {
43 ReinitializeSpellCheck("en-US");
46 void ReinitializeSpellCheck(const std::string& language) {
47 spell_check_.reset(new SpellCheck());
48 InitializeSpellCheck(language);
51 void UninitializeSpellCheck() {
52 spell_check_.reset(new SpellCheck());
55 bool InitializeIfNeeded() {
56 return spell_check()->InitializeIfNeeded();
59 void InitializeSpellCheck(const std::string& language) {
60 base::FilePath hunspell_directory = GetHunspellDirectory();
61 EXPECT_FALSE(hunspell_directory.empty());
62 base::File file(
63 chrome::spellcheck_common::GetVersionedFileName(language,
64 hunspell_directory),
65 base::File::FLAG_OPEN | base::File::FLAG_READ);
66 #if defined(OS_MACOSX)
67 // TODO(groby): Forcing spellcheck to use hunspell, even on OSX.
68 // Instead, tests should exercise individual spelling engines.
69 spell_check_->languages_.push_back(new SpellcheckLanguage());
70 spell_check_->languages_.front()->platform_spelling_engine_.reset(
71 new HunspellEngine);
72 spell_check_->languages_.front()->Init(file.Pass(), language);
73 #else
74 spell_check_->AddSpellcheckLanguage(file.Pass(), language);
75 #endif
78 void EnableAutoCorrect(bool enable_autocorrect) {
79 spell_check_->OnEnableAutoSpellCorrect(enable_autocorrect);
82 ~SpellCheckTest() override {}
84 SpellCheck* spell_check() { return spell_check_.get(); }
86 bool CheckSpelling(const std::string& word, int tag) {
87 return spell_check_->languages_.front()
88 ->platform_spelling_engine_->CheckSpelling(base::ASCIIToUTF16(word),
89 tag);
92 bool IsValidContraction(const base::string16& word, int tag) {
93 return spell_check_->languages_.front()->IsValidContraction(word, tag);
96 #if !defined(OS_MACOSX)
97 protected:
98 void TestSpellCheckParagraph(
99 const base::string16& input,
100 const std::vector<SpellCheckResult>& expected) {
101 blink::WebVector<blink::WebTextCheckingResult> results;
102 spell_check()->SpellCheckParagraph(input,
103 &results);
105 EXPECT_EQ(results.size(), expected.size());
106 size_t size = std::min(results.size(), expected.size());
107 for (size_t j = 0; j < size; ++j) {
108 EXPECT_EQ(results[j].decoration, blink::WebTextDecorationTypeSpelling);
109 EXPECT_EQ(results[j].location, expected[j].location);
110 EXPECT_EQ(results[j].length, expected[j].length);
113 #endif
115 private:
116 scoped_ptr<SpellCheck> spell_check_;
117 base::MessageLoop loop;
120 // A fake completion object for verification.
121 class MockTextCheckingCompletion : public blink::WebTextCheckingCompletion {
122 public:
123 MockTextCheckingCompletion()
124 : completion_count_(0) {
127 void didFinishCheckingText(
128 const blink::WebVector<blink::WebTextCheckingResult>& results) override {
129 completion_count_++;
130 last_results_ = results;
133 void didCancelCheckingText() override {
134 completion_count_++;
137 size_t completion_count_;
138 blink::WebVector<blink::WebTextCheckingResult> last_results_;
141 // Operates unit tests for the content::SpellCheck::SpellCheckWord() function
142 // with the US English dictionary.
143 // The unit tests in this function consist of:
144 // * Tests for the function with empty strings;
145 // * Tests for the function with a valid English word;
146 // * Tests for the function with a valid non-English word;
147 // * Tests for the function with a valid English word with a preceding
148 // space character;
149 // * Tests for the function with a valid English word with a preceding
150 // non-English word;
151 // * Tests for the function with a valid English word with a following
152 // space character;
153 // * Tests for the function with a valid English word with a following
154 // non-English word;
155 // * Tests for the function with two valid English words concatenated
156 // with space characters or non-English words;
157 // * Tests for the function with an invalid English word;
158 // * Tests for the function with an invalid English word with a preceding
159 // space character;
160 // * Tests for the function with an invalid English word with a preceding
161 // non-English word;
162 // * Tests for the function with an invalid English word with a following
163 // space character;
164 // * Tests for the function with an invalid English word with a following
165 // non-English word, and;
166 // * Tests for the function with two invalid English words concatenated
167 // with space characters or non-English words.
168 // A test with a "[ROBUSTNESS]" mark shows it is a robustness test and it uses
169 // grammatically incorrect string.
170 // TODO(groby): Please feel free to add more tests.
171 TEST_F(SpellCheckTest, SpellCheckStrings_EN_US) {
172 static const struct {
173 // A string to be tested.
174 const wchar_t* input;
175 // An expected result for this test case.
176 // * true: the input string does not have any invalid words.
177 // * false: the input string has one or more invalid words.
178 bool expected_result;
179 // The position and the length of the first invalid word.
180 int misspelling_start;
181 int misspelling_length;
182 } kTestCases[] = {
183 // Empty strings.
184 {L"", true},
185 {L" ", true},
186 {L"\xA0", true},
187 {L"\x3000", true},
189 // A valid English word "hello".
190 {L"hello", true},
191 // A valid Chinese word (meaning "hello") consisting of two CJKV
192 // ideographs
193 {L"\x4F60\x597D", true},
194 // A valid Korean word (meaning "hello") consisting of five hangul
195 // syllables
196 {L"\xC548\xB155\xD558\xC138\xC694", true},
197 // A valid Japanese word (meaning "hello") consisting of five Hiragana
198 // letters
199 {L"\x3053\x3093\x306B\x3061\x306F", true},
200 // A valid Hindi word (meaning ?) consisting of six Devanagari letters
201 // (This word is copied from "http://b/issue?id=857583".)
202 {L"\x0930\x093E\x091C\x0927\x093E\x0928", true},
203 // A valid English word "affix" using a Latin ligature 'ffi'
204 {L"a\xFB03x", true},
205 // A valid English word "hello" (fullwidth version)
206 {L"\xFF28\xFF45\xFF4C\xFF4C\xFF4F", true},
207 // Two valid Greek words (meaning "hello") consisting of seven Greek
208 // letters
209 {L"\x03B3\x03B5\x03B9\x03AC" L" " L"\x03C3\x03BF\x03C5", true},
210 // A valid Russian word (meaning "hello") consisting of twelve Cyrillic
211 // letters
212 {L"\x0437\x0434\x0440\x0430\x0432\x0441"
213 L"\x0442\x0432\x0443\x0439\x0442\x0435", true},
214 // A valid English contraction
215 {L"isn't", true},
216 // A valid English contraction with a typographical apostrophe.
217 {L"isn" TYPOGRAPHICAL_APOSTROPHE L"t", true},
218 // A valid English word enclosed with underscores.
219 {L"_hello_", true},
221 // A valid English word with a preceding whitespace
222 {L" " L"hello", true},
223 // A valid English word with a preceding no-break space
224 {L"\xA0" L"hello", true},
225 // A valid English word with a preceding ideographic space
226 {L"\x3000" L"hello", true},
227 // A valid English word with a preceding Chinese word
228 {L"\x4F60\x597D" L"hello", true},
229 // [ROBUSTNESS] A valid English word with a preceding Korean word
230 {L"\xC548\xB155\xD558\xC138\xC694" L"hello", true},
231 // A valid English word with a preceding Japanese word
232 {L"\x3053\x3093\x306B\x3061\x306F" L"hello", true},
233 // [ROBUSTNESS] A valid English word with a preceding Hindi word
234 {L"\x0930\x093E\x091C\x0927\x093E\x0928" L"hello", true},
235 // [ROBUSTNESS] A valid English word with two preceding Greek words
236 {L"\x03B3\x03B5\x03B9\x03AC" L" " L"\x03C3\x03BF\x03C5"
237 L"hello", true},
238 // [ROBUSTNESS] A valid English word with a preceding Russian word
239 {L"\x0437\x0434\x0440\x0430\x0432\x0441"
240 L"\x0442\x0432\x0443\x0439\x0442\x0435" L"hello", true},
242 // A valid English word with a following whitespace
243 {L"hello" L" ", true},
244 // A valid English word with a following no-break space
245 {L"hello" L"\xA0", true},
246 // A valid English word with a following ideographic space
247 {L"hello" L"\x3000", true},
248 // A valid English word with a following Chinese word
249 {L"hello" L"\x4F60\x597D", true},
250 // [ROBUSTNESS] A valid English word with a following Korean word
251 {L"hello" L"\xC548\xB155\xD558\xC138\xC694", true},
252 // A valid English word with a following Japanese word
253 {L"hello" L"\x3053\x3093\x306B\x3061\x306F", true},
254 // [ROBUSTNESS] A valid English word with a following Hindi word
255 {L"hello" L"\x0930\x093E\x091C\x0927\x093E\x0928", true},
256 // [ROBUSTNESS] A valid English word with two following Greek words
257 {L"hello"
258 L"\x03B3\x03B5\x03B9\x03AC" L" " L"\x03C3\x03BF\x03C5", true},
259 // [ROBUSTNESS] A valid English word with a following Russian word
260 {L"hello" L"\x0437\x0434\x0440\x0430\x0432\x0441"
261 L"\x0442\x0432\x0443\x0439\x0442\x0435", true},
263 // Two valid English words concatenated with a whitespace
264 {L"hello" L" " L"hello", true},
265 // Two valid English words concatenated with a no-break space
266 {L"hello" L"\xA0" L"hello", true},
267 // Two valid English words concatenated with an ideographic space
268 {L"hello" L"\x3000" L"hello", true},
269 // Two valid English words concatenated with a Chinese word
270 {L"hello" L"\x4F60\x597D" L"hello", true},
271 // [ROBUSTNESS] Two valid English words concatenated with a Korean word
272 {L"hello" L"\xC548\xB155\xD558\xC138\xC694" L"hello", true},
273 // Two valid English words concatenated with a Japanese word
274 {L"hello" L"\x3053\x3093\x306B\x3061\x306F" L"hello", true},
275 // [ROBUSTNESS] Two valid English words concatenated with a Hindi word
276 {L"hello" L"\x0930\x093E\x091C\x0927\x093E\x0928" L"hello" , true},
277 // [ROBUSTNESS] Two valid English words concatenated with two Greek words
278 {L"hello" L"\x03B3\x03B5\x03B9\x03AC" L" " L"\x03C3\x03BF\x03C5"
279 L"hello", true},
280 // [ROBUSTNESS] Two valid English words concatenated with a Russian word
281 {L"hello" L"\x0437\x0434\x0440\x0430\x0432\x0441"
282 L"\x0442\x0432\x0443\x0439\x0442\x0435" L"hello", true},
283 // [ROBUSTNESS] Two valid English words concatenated with a contraction
284 // character.
285 {L"hello:hello", true},
287 // An invalid English word
288 {L"ifmmp", false, 0, 5},
289 // An invalid English word "bffly" containing a Latin ligature 'ffl'
290 {L"b\xFB04y", false, 0, 3},
291 // An invalid English word "ifmmp" (fullwidth version)
292 {L"\xFF29\xFF46\xFF4D\xFF4D\xFF50", false, 0, 5},
293 // An invalid English contraction
294 {L"jtm'u", false, 0, 5},
295 // An invalid English word enclosed with underscores.
296 {L"_ifmmp_", false, 1, 5},
298 // An invalid English word with a preceding whitespace
299 {L" " L"ifmmp", false, 1, 5},
300 // An invalid English word with a preceding no-break space
301 {L"\xA0" L"ifmmp", false, 1, 5},
302 // An invalid English word with a preceding ideographic space
303 {L"\x3000" L"ifmmp", false, 1, 5},
304 // An invalid English word with a preceding Chinese word
305 {L"\x4F60\x597D" L"ifmmp", false, 2, 5},
306 // [ROBUSTNESS] An invalid English word with a preceding Korean word
307 {L"\xC548\xB155\xD558\xC138\xC694" L"ifmmp", false, 5, 5},
308 // An invalid English word with a preceding Japanese word
309 {L"\x3053\x3093\x306B\x3061\x306F" L"ifmmp", false, 5, 5},
310 // [ROBUSTNESS] An invalid English word with a preceding Hindi word
311 {L"\x0930\x093E\x091C\x0927\x093E\x0928" L"ifmmp", false, 6, 5},
312 // [ROBUSTNESS] An invalid English word with two preceding Greek words
313 {L"\x03B3\x03B5\x03B9\x03AC" L" " L"\x03C3\x03BF\x03C5"
314 L"ifmmp", false, 8, 5},
315 // [ROBUSTNESS] An invalid English word with a preceding Russian word
316 {L"\x0437\x0434\x0440\x0430\x0432\x0441"
317 L"\x0442\x0432\x0443\x0439\x0442\x0435" L"ifmmp", false, 12, 5},
319 // An invalid English word with a following whitespace
320 {L"ifmmp" L" ", false, 0, 5},
321 // An invalid English word with a following no-break space
322 {L"ifmmp" L"\xA0", false, 0, 5},
323 // An invalid English word with a following ideographic space
324 {L"ifmmp" L"\x3000", false, 0, 5},
325 // An invalid English word with a following Chinese word
326 {L"ifmmp" L"\x4F60\x597D", false, 0, 5},
327 // [ROBUSTNESS] An invalid English word with a following Korean word
328 {L"ifmmp" L"\xC548\xB155\xD558\xC138\xC694", false, 0, 5},
329 // An invalid English word with a following Japanese word
330 {L"ifmmp" L"\x3053\x3093\x306B\x3061\x306F", false, 0, 5},
331 // [ROBUSTNESS] An invalid English word with a following Hindi word
332 {L"ifmmp" L"\x0930\x093E\x091C\x0927\x093E\x0928", false, 0, 5},
333 // [ROBUSTNESS] An invalid English word with two following Greek words
334 {L"ifmmp"
335 L"\x03B3\x03B5\x03B9\x03AC" L" " L"\x03C3\x03BF\x03C5", false, 0, 5},
336 // [ROBUSTNESS] An invalid English word with a following Russian word
337 {L"ifmmp" L"\x0437\x0434\x0440\x0430\x0432\x0441"
338 L"\x0442\x0432\x0443\x0439\x0442\x0435", false, 0, 5},
340 // Two invalid English words concatenated with a whitespace
341 {L"ifmmp" L" " L"ifmmp", false, 0, 5},
342 // Two invalid English words concatenated with a no-break space
343 {L"ifmmp" L"\xA0" L"ifmmp", false, 0, 5},
344 // Two invalid English words concatenated with an ideographic space
345 {L"ifmmp" L"\x3000" L"ifmmp", false, 0, 5},
346 // Two invalid English words concatenated with a Chinese word
347 {L"ifmmp" L"\x4F60\x597D" L"ifmmp", false, 0, 5},
348 // [ROBUSTNESS] Two invalid English words concatenated with a Korean word
349 {L"ifmmp" L"\xC548\xB155\xD558\xC138\xC694" L"ifmmp", false, 0, 5},
350 // Two invalid English words concatenated with a Japanese word
351 {L"ifmmp" L"\x3053\x3093\x306B\x3061\x306F" L"ifmmp", false, 0, 5},
352 // [ROBUSTNESS] Two invalid English words concatenated with a Hindi word
353 {L"ifmmp" L"\x0930\x093E\x091C\x0927\x093E\x0928" L"ifmmp" , false, 0, 5},
354 // [ROBUSTNESS] Two invalid English words concatenated with two Greek words
355 {L"ifmmp" L"\x03B3\x03B5\x03B9\x03AC" L" " L"\x03C3\x03BF\x03C5"
356 L"ifmmp", false, 0, 5},
357 // [ROBUSTNESS] Two invalid English words concatenated with a Russian word
358 {L"ifmmp" L"\x0437\x0434\x0440\x0430\x0432\x0441"
359 L"\x0442\x0432\x0443\x0439\x0442\x0435" L"ifmmp", false, 0, 5},
360 // [ROBUSTNESS] Two invalid English words concatenated with a contraction
361 // character.
362 {L"ifmmp:ifmmp", false, 0, 11},
364 // [REGRESSION] Issue 13432: "Any word of 13 or 14 characters is not
365 // spellcheck" <http://crbug.com/13432>.
366 {L"qwertyuiopasd", false, 0, 13},
367 {L"qwertyuiopasdf", false, 0, 14},
369 // [REGRESSION] Issue 128896: "en_US hunspell dictionary includes
370 // acknowledgement but not acknowledgements" <http://crbug.com/128896>
371 {L"acknowledgement", true},
372 {L"acknowledgements", true},
374 // Issue 123290: "Spellchecker should treat numbers as word characters"
375 {L"0th", true},
376 {L"1st", true},
377 {L"2nd", true},
378 {L"3rd", true},
379 {L"4th", true},
380 {L"5th", true},
381 {L"6th", true},
382 {L"7th", true},
383 {L"8th", true},
384 {L"9th", true},
385 {L"10th", true},
386 {L"100th", true},
387 {L"1000th", true},
388 {L"25", true},
389 {L"2012", true},
390 {L"100,000,000", true},
391 {L"3.141592653", true},
394 for (size_t i = 0; i < arraysize(kTestCases); ++i) {
395 size_t input_length = 0;
396 if (kTestCases[i].input != NULL) {
397 input_length = wcslen(kTestCases[i].input);
399 int misspelling_start;
400 int misspelling_length;
401 bool result = spell_check()->SpellCheckWord(
402 base::WideToUTF16(kTestCases[i].input).c_str(),
403 kNoOffset,
404 static_cast<int>(input_length),
405 kNoTag,
406 &misspelling_start,
407 &misspelling_length, NULL);
409 EXPECT_EQ(kTestCases[i].expected_result, result);
410 EXPECT_EQ(kTestCases[i].misspelling_start, misspelling_start);
411 EXPECT_EQ(kTestCases[i].misspelling_length, misspelling_length);
415 TEST_F(SpellCheckTest, SpellCheckSuggestions_EN_US) {
416 static const struct {
417 // A string to be tested.
418 const wchar_t* input;
419 // An expected result for this test case.
420 // * true: the input string does not have any invalid words.
421 // * false: the input string has one or more invalid words.
422 bool expected_result;
423 // The position and the length of the first invalid word.
424 int misspelling_start;
425 int misspelling_length;
427 // A suggested word that should occur.
428 const wchar_t* suggested_word;
429 } kTestCases[] = {
430 {L"ello", false, 0, 0, L"hello"},
431 {L"ello", false, 0, 0, L"cello"},
432 {L"wate", false, 0, 0, L"water"},
433 {L"wate", false, 0, 0, L"waste"},
434 {L"wate", false, 0, 0, L"sate"},
435 {L"wate", false, 0, 0, L"ate"},
436 {L"jum", false, 0, 0, L"jump"},
437 {L"jum", false, 0, 0, L"hum"},
438 {L"jum", false, 0, 0, L"sum"},
439 {L"jum", false, 0, 0, L"um"},
440 {L"alot", false, 0, 0, L"a lot"},
441 // A regression test for Issue 36523.
442 {L"privliged", false, 0, 0, L"privileged"},
443 // TODO (Sidchat): add many more examples.
446 for (size_t i = 0; i < arraysize(kTestCases); ++i) {
447 std::vector<base::string16> suggestions;
448 size_t input_length = 0;
449 if (kTestCases[i].input != NULL) {
450 input_length = wcslen(kTestCases[i].input);
452 int misspelling_start;
453 int misspelling_length;
454 bool result = spell_check()->SpellCheckWord(
455 base::WideToUTF16(kTestCases[i].input).c_str(),
456 kNoOffset,
457 static_cast<int>(input_length),
458 kNoTag,
459 &misspelling_start,
460 &misspelling_length,
461 &suggestions);
463 // Check for spelling.
464 EXPECT_EQ(kTestCases[i].expected_result, result);
466 // Check if the suggested words occur.
467 bool suggested_word_is_present = false;
468 for (int j = 0; j < static_cast<int>(suggestions.size()); j++) {
469 if (suggestions.at(j).compare(
470 base::WideToUTF16(kTestCases[i].suggested_word)) == 0) {
471 suggested_word_is_present = true;
472 break;
476 EXPECT_TRUE(suggested_word_is_present);
480 // This test verifies our spellchecker can split a text into words and check
481 // the spelling of each word in the text.
482 #if defined(THREAD_SANITIZER)
483 // SpellCheckTest.SpellCheckText fails under ThreadSanitizer v2.
484 // See http://crbug.com/217909.
485 #define MAYBE_SpellCheckText DISABLED_SpellCheckText
486 #else
487 #define MAYBE_SpellCheckText SpellCheckText
488 #endif // THREAD_SANITIZER
489 TEST_F(SpellCheckTest, MAYBE_SpellCheckText) {
490 static const struct {
491 const char* language;
492 const wchar_t* input;
493 } kTestCases[] = {
495 // Afrikaans
496 "af-ZA",
497 L"Google se missie is om die w\x00EAreld se inligting te organiseer en "
498 L"dit bruikbaar en toeganklik te maak."
499 }, {
500 // Catalan
501 "ca-ES",
502 L"La missi\x00F3 de Google \x00E9s organitzar la informaci\x00F3 "
503 L"del m\x00F3n i fer que sigui \x00FAtil i accessible universalment."
504 }, {
505 // Czech
506 "cs-CZ",
507 L"Posl\x00E1n\x00EDm spole\x010Dnosti Google je "
508 L"uspo\x0159\x00E1\x0064\x0061t informace z cel\x00E9ho sv\x011Bta "
509 L"tak, aby byly v\x0161\x0065obecn\x011B p\x0159\x00EDstupn\x00E9 "
510 L"a u\x017Eite\x010Dn\x00E9."
511 }, {
512 // Danish
513 "da-DK",
514 L"Googles "
515 L"mission er at organisere verdens information og g\x00F8re den "
516 L"almindeligt tilg\x00E6ngelig og nyttig."
517 }, {
518 // German
519 "de-DE",
520 L"Das Ziel von Google besteht darin, die auf der Welt vorhandenen "
521 L"Informationen zu organisieren und allgemein zug\x00E4nglich und "
522 L"nutzbar zu machen."
523 }, {
524 // Greek
525 "el-GR",
526 L"\x0391\x03C0\x03BF\x03C3\x03C4\x03BF\x03BB\x03AE "
527 L"\x03C4\x03B7\x03C2 Google \x03B5\x03AF\x03BD\x03B1\x03B9 "
528 L"\x03BD\x03B1 \x03BF\x03C1\x03B3\x03B1\x03BD\x03CE\x03BD\x03B5\x03B9 "
529 L"\x03C4\x03B9\x03C2 "
530 L"\x03C0\x03BB\x03B7\x03C1\x03BF\x03C6\x03BF\x03C1\x03AF\x03B5\x03C2 "
531 L"\x03C4\x03BF\x03C5 \x03BA\x03CC\x03C3\x03BC\x03BF\x03C5 "
532 L"\x03BA\x03B1\x03B9 \x03BD\x03B1 \x03C4\x03B9\x03C2 "
533 L"\x03BA\x03B1\x03B8\x03B9\x03C3\x03C4\x03AC "
534 L"\x03C0\x03C1\x03BF\x03C3\x03B2\x03AC\x03C3\x03B9\x03BC\x03B5\x03C2 "
535 L"\x03BA\x03B1\x03B9 \x03C7\x03C1\x03AE\x03C3\x03B9\x03BC\x03B5\x03C2."
536 }, {
537 // English (Australia)
538 "en-AU",
539 L"Google's mission is to organise the world's information and make it "
540 L"universally accessible and useful."
541 }, {
542 // English (Canada)
543 "en-CA",
544 L"Google's mission is to organize the world's information and make it "
545 L"universally accessible and useful."
546 }, {
547 // English (United Kingdom)
548 "en-GB",
549 L"Google's mission is to organise the world's information and make it "
550 L"universally accessible and useful."
551 }, {
552 // English (United States)
553 "en-US",
554 L"Google's mission is to organize the world's information and make it "
555 L"universally accessible and useful."
556 }, {
557 // Bulgarian
558 "bg-BG",
559 L"\x041c\x0438\x0441\x0438\x044f\x0442\x0430 "
560 L"\x043d\x0430 Google \x0435 \x0434\x0430 \x043e"
561 L"\x0440\x0433\x0430\x043d\x0438\x0437\x0438\x0440"
562 L"\x0430 \x0441\x0432\x0435\x0442\x043e\x0432"
563 L"\x043d\x0430\x0442\x0430 \x0438\x043d\x0444"
564 L"\x043e\x0440\x043c\x0430\x0446\x0438\x044f "
565 L"\x0438 \x0434\x0430 \x044f \x043d"
566 L"\x0430\x043f\x0440\x0430\x0432\x0438 \x0443"
567 L"\x043d\x0438\x0432\x0435\x0440\x0441\x0430\x043b"
568 L"\x043d\x043e \x0434\x043e\x0441\x0442\x044a"
569 L"\x043f\x043d\x0430 \x0438 \x043f\x043e"
570 L"\x043b\x0435\x0437\x043d\x0430."
571 }, {
572 // Spanish
573 "es-ES",
574 L"La misi\x00F3n de "
575 // L"Google" - to be added.
576 L" es organizar la informaci\x00F3n mundial "
577 L"para que resulte universalmente accesible y \x00FAtil."
578 }, {
579 // Estonian
580 "et-EE",
581 // L"Google'ile " - to be added.
582 L"\x00FClesanne on korraldada maailma teavet ja teeb selle "
583 L"k\x00F5igile k\x00E4ttesaadavaks ja kasulikuks.",
584 }, {
585 // Faroese
586 "fo-FO",
587 L"Google er at samskipa alla vitan \x00ED heiminum og gera hana alment "
588 L"atkomiliga og n\x00FDtiliga."
589 }, {
590 // French
591 "fr-FR",
592 L"Google a pour mission d'organiser les informations \x00E0 "
593 L"l'\x00E9\x0063helle mondiale dans le but de les rendre accessibles "
594 L"et utiles \x00E0 tous."
595 }, {
596 // Hebrew
597 "he-IL",
598 L"\x05D4\x05DE\x05E9\x05D9\x05DE\x05D4 \x05E9\x05DC Google "
599 L"\x05D4\x05D9\x05D0 \x05DC\x05D0\x05E8\x05D2\x05DF "
600 L"\x05D0\x05EA \x05D4\x05DE\x05D9\x05D3\x05E2 "
601 L"\x05D4\x05E2\x05D5\x05DC\x05DE\x05D9 "
602 L"\x05D5\x05DC\x05D4\x05E4\x05D5\x05DA \x05D0\x05D5\x05EA\x05D5 "
603 L"\x05DC\x05D6\x05DE\x05D9\x05DF "
604 L"\x05D5\x05E9\x05D9\x05DE\x05D5\x05E9\x05D9 \x05D1\x05DB\x05DC "
605 L"\x05D4\x05E2\x05D5\x05DC\x05DD. "
606 // Two words with ASCII double/single quoation marks.
607 L"\x05DE\x05E0\x05DB\x0022\x05DC \x05E6\x0027\x05D9\x05E4\x05E1"
608 }, {
609 // Hindi
610 "hi-IN",
611 L"Google \x0915\x093E \x092E\x093F\x0936\x0928 "
612 L"\x0926\x0941\x0928\x093F\x092F\x093E \x0915\x0940 "
613 L"\x091C\x093E\x0928\x0915\x093E\x0930\x0940 \x0915\x094B "
614 L"\x0935\x094D\x092F\x0935\x0938\x094D\x0925\x093F\x0924 "
615 L"\x0915\x0930\x0928\x093E \x0914\x0930 \x0909\x0938\x0947 "
616 L"\x0938\x093E\x0930\x094D\x0935\x092D\x094C\x092E\x093F\x0915 "
617 L"\x0930\x0942\x092A \x0938\x0947 \x092A\x0939\x0941\x0901\x091A "
618 L"\x092E\x0947\x0902 \x0914\x0930 \x0909\x092A\x092F\x094B\x0917\x0940 "
619 L"\x092C\x0928\x093E\x0928\x093E \x0939\x0948."
620 }, {
621 // Hungarian
622 "hu-HU",
623 L"A Google azt a k\x00FCldet\x00E9st v\x00E1llalta mag\x00E1ra, "
624 L"hogy a vil\x00E1gon fellelhet\x0151 inform\x00E1\x0063i\x00F3kat "
625 L"rendszerezze \x00E9s \x00E1ltal\x00E1nosan el\x00E9rhet\x0151v\x00E9, "
626 L"illetve haszn\x00E1lhat\x00F3v\x00E1 tegye."
627 }, {
628 // Croatian
629 "hr-HR",
630 // L"Googleova " - to be added.
631 L"je misija organizirati svjetske informacije i u\x010Diniti ih "
632 // L"univerzalno " - to be added.
633 L"pristupa\x010Dnima i korisnima."
634 }, {
635 // Indonesian
636 "id-ID",
637 L"Misi Google adalah untuk mengelola informasi dunia dan membuatnya "
638 L"dapat diakses dan bermanfaat secara universal."
639 }, {
640 // Italian
641 "it-IT",
642 L"La missione di Google \x00E8 organizzare le informazioni a livello "
643 L"mondiale e renderle universalmente accessibili e fruibili."
644 }, {
645 // Lithuanian
646 "lt-LT",
647 L"\x201EGoogle\x201C tikslas \x2013 rinkti ir sisteminti pasaulio "
648 L"informacij\x0105 bei padaryti j\x0105 prieinam\x0105 ir "
649 L"nauding\x0105 visiems."
650 }, {
651 // Latvian
652 "lv-LV",
653 L"Google uzdevums ir k\x0101rtot pasaules inform\x0101"
654 L"ciju un padar\x012Bt to univers\x0101li pieejamu un noder\x012Bgu."
655 }, {
656 // Norwegian
657 "nb-NO",
658 // L"Googles " - to be added.
659 L"m\x00E5l er \x00E5 organisere informasjonen i verden og "
660 L"gj\x00F8re den tilgjengelig og nyttig for alle."
661 }, {
662 // Dutch
663 "nl-NL",
664 L"Het doel van Google is om alle informatie wereldwijd toegankelijk "
665 L"en bruikbaar te maken."
666 }, {
667 // Polish
668 "pl-PL",
669 L"Misj\x0105 Google jest uporz\x0105" L"dkowanie \x015Bwiatowych "
670 L"zasob\x00F3w informacji, aby sta\x0142y si\x0119 one powszechnie "
671 L"dost\x0119pne i u\x017Cyteczne."
672 }, {
673 // Portuguese (Brazil)
674 "pt-BR",
675 L"A miss\x00E3o do "
676 #if !defined(OS_MACOSX)
677 L"Google "
678 #endif
679 L"\x00E9 organizar as informa\x00E7\x00F5"
680 L"es do mundo todo e "
681 #if !defined(OS_MACOSX)
682 L"torn\x00E1-las "
683 #endif
684 L"acess\x00EDveis e \x00FAteis em car\x00E1ter universal."
685 }, {
686 // Portuguese (Portugal)
687 "pt-PT",
688 L"O "
689 #if !defined(OS_MACOSX)
690 L"Google "
691 #endif
692 L"tem por miss\x00E3o organizar a informa\x00E7\x00E3o do "
693 L"mundo e "
694 #if !defined(OS_MACOSX)
695 L"torn\x00E1-la "
696 #endif
697 L"universalmente acess\x00EDvel e \x00FAtil"
698 }, {
699 // Romanian
700 "ro-RO",
701 L"Misiunea Google este de a organiza informa\x021B3iile lumii \x0219i de "
702 L"a le face accesibile \x0219i utile la nivel universal."
703 }, {
704 // Russian
705 "ru-RU",
706 L"\x041C\x0438\x0441\x0441\x0438\x044F Google "
707 L"\x0441\x043E\x0441\x0442\x043E\x0438\x0442 \x0432 "
708 L"\x043E\x0440\x0433\x0430\x043D\x0438\x0437\x0430\x0446\x0438\x0438 "
709 L"\x043C\x0438\x0440\x043E\x0432\x043E\x0439 "
710 L"\x0438\x043D\x0444\x043E\x0440\x043C\x0430\x0446\x0438\x0438, "
711 L"\x043E\x0431\x0435\x0441\x043F\x0435\x0447\x0435\x043D\x0438\x0438 "
712 L"\x0435\x0435 "
713 L"\x0434\x043E\x0441\x0442\x0443\x043F\x043D\x043E\x0441\x0442\x0438 "
714 L"\x0438 \x043F\x043E\x043B\x044C\x0437\x044B \x0434\x043B\x044F "
715 L"\x0432\x0441\x0435\x0445."
716 // A Russian word including U+0451. (Bug 15558 <http://crbug.com/15558>)
717 L"\x0451\x043B\x043A\x0430"
718 }, {
719 // Serbo-Croatian (Serbian Latin)
720 "sh",
721 L"Google-ova misija je da organizuje sve informacije na svetu i "
722 L"u\x010dini ih univerzal-no dostupnim i korisnim."
723 }, {
724 // Serbian
725 "sr",
726 L"\x0047\x006f\x006f\x0067\x006c\x0065\x002d\x043e\x0432\x0430 "
727 L"\x043c\x0438\x0441\x0438\x0458\x0430 \x0458\x0435 \x0434\x0430 "
728 L"\x043e\x0440\x0433\x0430\x043d\x0438\x0437\x0443\x0458\x0435 "
729 L"\x0441\x0432\x0435 "
730 L"\x0438\x043d\x0444\x043e\x0440\x043c\x0430\x0446\x0438\x0458\x0435 "
731 L"\x043d\x0430 \x0441\x0432\x0435\x0442\x0443 \x0438 "
732 L"\x0443\x0447\x0438\x043d\x0438 \x0438\x0445 "
733 L"\x0443\x043d\x0438\x0432\x0435\x0440\x0437\x0430\x043b\x043d\x043e "
734 L"\x0434\x043e\x0441\x0442\x0443\x043f\x043d\x0438\x043c \x0438 "
735 L"\x043a\x043e\x0440\x0438\x0441\x043d\x0438\x043c."
736 }, {
737 // Slovak
738 "sk-SK",
739 L"Spolo\x010Dnos\x0165 Google si dala za \x00FAlohu usporiada\x0165 "
740 L"inform\x00E1\x0063ie "
741 L"z cel\x00E9ho sveta a zabezpe\x010Di\x0165, "
742 L"aby boli v\x0161eobecne dostupn\x00E9 a u\x017Eito\x010Dn\x00E9."
743 }, {
744 // Slovenian
745 "sl-SI",
746 // L"Googlovo " - to be added.
747 L"poslanstvo je organizirati svetovne informacije in "
748 L"omogo\x010Diti njihovo dostopnost in s tem uporabnost za vse."
749 }, {
750 // Swedish
751 "sv-SE",
752 L"Googles m\x00E5ls\x00E4ttning \x00E4r att ordna v\x00E4rldens "
753 L"samlade information och g\x00F6ra den tillg\x00E4nglig f\x00F6r alla."
754 }, {
755 // Turkish
756 "tr-TR",
757 // L"Google\x2019\x0131n " - to be added.
758 L"misyonu, d\x00FCnyadaki t\x00FCm bilgileri "
759 L"organize etmek ve evrensel olarak eri\x015Filebilir ve "
760 L"kullan\x0131\x015Fl\x0131 k\x0131lmakt\x0131r."
761 }, {
762 // Ukranian
763 "uk-UA",
764 L"\x041c\x0456\x0441\x0456\x044f "
765 L"\x043a\x043e\x043c\x043f\x0430\x043d\x0456\x0457 Google "
766 L"\x043f\x043e\x043b\x044f\x0433\x0430\x0454 \x0432 "
767 L"\x0442\x043e\x043c\x0443, \x0449\x043e\x0431 "
768 L"\x0443\x043f\x043e\x0440\x044f\x0434\x043a\x0443\x0432\x0430\x0442"
769 L"\x0438 \x0456\x043d\x0444\x043e\x0440\x043c\x0430\x0446\x0456\x044e "
770 L"\x0437 \x0443\x0441\x044c\x043e\x0433\x043e "
771 L"\x0441\x0432\x0456\x0442\x0443 \x0442\x0430 "
772 L"\x0437\x0440\x043e\x0431\x0438\x0442\x0438 \x0457\x0457 "
773 L"\x0443\x043d\x0456\x0432\x0435\x0440\x0441\x0430\x043b\x044c\x043d"
774 L"\x043e \x0434\x043e\x0441\x0442\x0443\x043f\x043d\x043e\x044e "
775 L"\x0442\x0430 \x043a\x043e\x0440\x0438\x0441\x043d\x043e\x044e."
776 }, {
777 // Vietnamese
778 "vi-VN",
779 L"Nhi\x1EC7m v\x1EE5 c\x1EE7\x0061 "
780 L"Google la \x0111\x1EC3 t\x1ED5 ch\x1EE9\x0063 "
781 L"c\x00E1\x0063 th\x00F4ng tin c\x1EE7\x0061 "
782 L"th\x1EBF gi\x1EDBi va l\x00E0m cho n\x00F3 universal c\x00F3 "
783 L"th\x1EC3 truy c\x1EADp va h\x1EEFu d\x1EE5ng h\x01A1n."
784 }, {
785 // Korean
786 "ko",
787 L"Google\xC758 \xBAA9\xD45C\xB294 \xC804\xC138\xACC4\xC758 "
788 L"\xC815\xBCF4\xB97C \xCCB4\xACC4\xD654\xD558\xC5EC \xBAA8\xB450\xAC00 "
789 L"\xD3B8\xB9AC\xD558\xAC8C \xC774\xC6A9\xD560 \xC218 "
790 L"\xC788\xB3C4\xB85D \xD558\xB294 \xAC83\xC785\xB2C8\xB2E4."
791 }, {
792 // Albanian
793 "sq",
794 L"Misioni i Google \x00EBsht\x00EB q\x00EB t\x00EB organizoj\x00EB "
795 L"informacionin e bot\x00EBs dhe t\x00EB b\x00EBjn\x00EB at\x00EB "
796 L"universalisht t\x00EB arritshme dhe t\x00EB dobishme."
797 }, {
798 // Tamil
799 "ta",
800 L"Google \x0B87\x0BA9\x0BCD "
801 L"\x0BA8\x0BC7\x0BBE\x0B95\x0BCD\x0B95\x0BAE\x0BCD "
802 L"\x0B89\x0BB2\x0B95\x0BBF\x0BA9\x0BCD \x0BA4\x0B95\x0BB5\x0BB2\x0BCD "
803 L"\x0B8F\x0BB1\x0BCD\x0BAA\x0BBE\x0B9F\x0BC1 \x0B87\x0BA4\x0BC1 "
804 L"\x0B89\x0BB2\x0B95\x0BB3\x0BBE\x0BB5\x0BBF\x0BAF "
805 L"\x0B85\x0BA3\x0BC1\x0B95\x0B95\x0BCD \x0B95\x0BC2\x0B9F\x0BBF\x0BAF "
806 L"\x0BAE\x0BB1\x0BCD\x0BB1\x0BC1\x0BAE\x0BCD "
807 L"\x0BAA\x0BAF\x0BA9\x0BC1\x0BB3\x0BCD\x0BB3 "
808 L"\x0B9A\x0BC6\x0BAF\x0BCD\x0BAF \x0B89\x0BB3\x0BCD\x0BB3\x0BA4\x0BC1."
809 }, {
810 // Tajik
811 "tg",
812 L"\x041c\x0438\x0441\x0441\x0438\x044f\x0438 Google \x0438\x043d "
813 L"\x043c\x0443\x0440\x0430\x0442\x0442\x0430\x0431 "
814 L"\x0441\x043e\x0445\x0442\x0430\x043d\x0438 "
815 L"\x043c\x0430\x044a\x043b\x0443\x043c\x043e\x0442\x04b3\x043e\x0438 "
816 L"\x043c\x0430\x0432\x04b7\x0443\x0434\x0430, \x043e\x0441\x043e\x043d "
817 L"\x043d\x0430\x043c\x0443\x0434\x0430\x043d\x0438 "
818 L"\x0438\x0441\x0442\x0438\x0444\x043e\x0434\x0430\x0431\x0430\x0440"
819 L"\x04e3 \x0432\x0430 \x0434\x0430\x0441\x0442\x0440\x0430\x0441\x0438 "
820 L"\x0443\x043c\x0443\x043c "
821 L"\x0433\x0430\x0440\x0434\x043e\x043d\x0438\x0434\x0430\x043d\x0438 "
822 L"\x043e\x043d\x04b3\x043e \x0430\x0441\x0442."
826 for (size_t i = 0; i < arraysize(kTestCases); ++i) {
827 ReinitializeSpellCheck(kTestCases[i].language);
828 size_t input_length = 0;
829 if (kTestCases[i].input != NULL)
830 input_length = wcslen(kTestCases[i].input);
832 int misspelling_start = 0;
833 int misspelling_length = 0;
834 bool result = spell_check()->SpellCheckWord(
835 base::WideToUTF16(kTestCases[i].input).c_str(),
836 kNoOffset,
837 static_cast<int>(input_length),
838 kNoTag,
839 &misspelling_start,
840 &misspelling_length, NULL);
842 EXPECT_TRUE(result)
843 << "\""
844 << std::wstring(kTestCases[i].input).substr(
845 misspelling_start, misspelling_length)
846 << "\" is misspelled in "
847 << kTestCases[i].language
848 << ".";
849 EXPECT_EQ(0, misspelling_start);
850 EXPECT_EQ(0, misspelling_length);
854 TEST_F(SpellCheckTest, GetAutoCorrectionWord_EN_US) {
855 static const struct {
856 // A misspelled word.
857 const char* input;
859 // An expected result for this test case.
860 // Should be an empty string if there are no suggestions for auto correct.
861 const char* expected_result;
862 } kTestCases[] = {
863 {"teh", "the"},
864 {"moer", "more"},
865 {"watre", "water"},
866 {"noen", ""},
867 {"what", ""},
870 EnableAutoCorrect(true);
872 for (size_t i = 0; i < arraysize(kTestCases); ++i) {
873 base::string16 misspelled_word(base::UTF8ToUTF16(kTestCases[i].input));
874 base::string16 expected_autocorrect_word(
875 base::UTF8ToUTF16(kTestCases[i].expected_result));
876 base::string16 autocorrect_word = spell_check()->GetAutoCorrectionWord(
877 misspelled_word, 0);
879 // Check for spelling.
880 EXPECT_EQ(expected_autocorrect_word, autocorrect_word);
884 // Verify that our SpellCheck::SpellCheckWord() returns false when it checks
885 // misspelled words.
886 TEST_F(SpellCheckTest, MisspelledWords) {
887 static const struct {
888 const char* language;
889 const wchar_t* input;
890 } kTestCases[] = {
892 // A misspelled word for English
893 "en-US",
894 L"aaaaaaaaaa",
895 }, {
896 // A misspelled word for Greek.
897 "el-GR",
898 L"\x03B1\x03B1\x03B1\x03B1\x03B1\x03B1\x03B1\x03B1\x03B1\x03B1",
899 }, {
900 // A misspelled word for Hebrew
901 "he-IL",
902 L"\x05D0\x05D0\x05D0\x05D0\x05D0\x05D0\x05D0\x05D0\x05D0\x05D0",
903 }, {
904 // Hindi
905 "hi-IN",
906 L"\x0905\x0905\x0905\x0905\x0905\x0905\x0905\x0905\x0905\x0905",
907 }, {
908 // A misspelled word for Russian
909 "ru-RU",
910 L"\x0430\x0430\x0430\x0430\x0430\x0430\x0430\x0430\x0430\x0430",
914 for (size_t i = 0; i < arraysize(kTestCases); ++i) {
915 ReinitializeSpellCheck(kTestCases[i].language);
917 base::string16 word(base::WideToUTF16(kTestCases[i].input));
918 int word_length = static_cast<int>(word.length());
919 int misspelling_start = 0;
920 int misspelling_length = 0;
921 bool result = spell_check()->SpellCheckWord(word.c_str(),
922 kNoOffset,
923 word_length,
924 kNoTag,
925 &misspelling_start,
926 &misspelling_length,
927 NULL);
928 EXPECT_FALSE(result);
929 EXPECT_EQ(0, misspelling_start);
930 EXPECT_EQ(word_length, misspelling_length);
934 // Since SpellCheck::SpellCheckParagraph is not implemented on Mac,
935 // we skip these SpellCheckParagraph tests on Mac.
936 #if !defined(OS_MACOSX)
938 // Make sure SpellCheckParagraph does not crash if the input is empty.
939 TEST_F(SpellCheckTest, SpellCheckParagraphEmptyParagraph) {
940 std::vector<SpellCheckResult> expected;
941 TestSpellCheckParagraph(base::UTF8ToUTF16(""), expected);
944 // A simple test case having no misspellings.
945 TEST_F(SpellCheckTest, SpellCheckParagraphNoMisspellings) {
946 const base::string16 text = base::UTF8ToUTF16("apple");
947 std::vector<SpellCheckResult> expected;
948 TestSpellCheckParagraph(text, expected);
951 // A simple test case having one misspelling.
952 TEST_F(SpellCheckTest, SpellCheckParagraphSingleMisspellings) {
953 const base::string16 text = base::UTF8ToUTF16("zz");
954 std::vector<SpellCheckResult> expected;
955 expected.push_back(SpellCheckResult(
956 SpellCheckResult::SPELLING, 0, 2));
958 TestSpellCheckParagraph(text, expected);
961 // A simple test case having multiple misspellings.
962 TEST_F(SpellCheckTest, SpellCheckParagraphMultipleMisspellings) {
963 const base::string16 text = base::UTF8ToUTF16("zz, zz");
964 std::vector<SpellCheckResult> expected;
965 expected.push_back(SpellCheckResult(
966 SpellCheckResult::SPELLING, 0, 2));
967 expected.push_back(SpellCheckResult(
968 SpellCheckResult::SPELLING, 4, 2));
970 TestSpellCheckParagraph(text, expected);
973 // Make sure a relatively long (correct) sentence can be spellchecked.
974 TEST_F(SpellCheckTest, SpellCheckParagraphLongSentence) {
975 std::vector<SpellCheckResult> expected;
976 // The text is taken from US constitution preamble.
977 const base::string16 text = base::UTF8ToUTF16(
978 "We the people of the United States, in order to form a more perfect "
979 "union, establish justice, insure domestic tranquility, provide for "
980 "the common defense, promote the general welfare, and secure the "
981 "blessings of liberty to ourselves and our posterity, do ordain and "
982 "establish this Constitution for the United States of America.");
984 TestSpellCheckParagraph(text, expected);
987 // Make sure all misspellings can be found in a relatively long sentence.
988 TEST_F(SpellCheckTest, SpellCheckParagraphLongSentenceMultipleMisspellings) {
989 std::vector<SpellCheckResult> expected;
991 // All 'the' are converted to 'hte' in US consitition preamble.
992 const base::string16 text = base::UTF8ToUTF16(
993 "We hte people of hte United States, in order to form a more perfect "
994 "union, establish justice, insure domestic tranquility, provide for "
995 "hte common defense, promote hte general welfare, and secure hte "
996 "blessings of liberty to ourselves and our posterity, do ordain and "
997 "establish this Constitution for hte United States of America.");
999 expected.push_back(SpellCheckResult(
1000 SpellCheckResult::SPELLING, 3, 3));
1001 expected.push_back(SpellCheckResult(
1002 SpellCheckResult::SPELLING, 17, 3));
1003 expected.push_back(SpellCheckResult(
1004 SpellCheckResult::SPELLING, 135, 3));
1005 expected.push_back(SpellCheckResult(
1006 SpellCheckResult::SPELLING, 163, 3));
1007 expected.push_back(SpellCheckResult(
1008 SpellCheckResult::SPELLING, 195, 3));
1009 expected.push_back(SpellCheckResult(
1010 SpellCheckResult::SPELLING, 298, 3));
1012 TestSpellCheckParagraph(text, expected);
1015 // We also skip RequestSpellCheck tests on Mac, because a system spellchecker
1016 // is used on Mac instead of SpellCheck::RequestTextChecking.
1018 // Make sure RequestTextChecking does not crash if input is empty.
1019 TEST_F(SpellCheckTest, RequestSpellCheckWithEmptyString) {
1020 MockTextCheckingCompletion completion;
1022 spell_check()->RequestTextChecking(base::string16(), &completion);
1024 base::MessageLoop::current()->RunUntilIdle();
1026 EXPECT_EQ(completion.completion_count_, 1U);
1029 // A simple test case having no misspellings.
1030 TEST_F(SpellCheckTest, RequestSpellCheckWithoutMisspelling) {
1031 MockTextCheckingCompletion completion;
1033 const base::string16 text = base::ASCIIToUTF16("hello");
1034 spell_check()->RequestTextChecking(text, &completion);
1036 base::MessageLoop::current()->RunUntilIdle();
1038 EXPECT_EQ(completion.completion_count_, 1U);
1041 // A simple test case having one misspelling.
1042 TEST_F(SpellCheckTest, RequestSpellCheckWithSingleMisspelling) {
1043 MockTextCheckingCompletion completion;
1045 const base::string16 text = base::ASCIIToUTF16("apple, zz");
1046 spell_check()->RequestTextChecking(text, &completion);
1048 base::MessageLoop::current()->RunUntilIdle();
1050 EXPECT_EQ(completion.completion_count_, 1U);
1051 EXPECT_EQ(completion.last_results_.size(), 1U);
1052 EXPECT_EQ(completion.last_results_[0].location, 7);
1053 EXPECT_EQ(completion.last_results_[0].length, 2);
1056 // A simple test case having a few misspellings.
1057 TEST_F(SpellCheckTest, RequestSpellCheckWithMisspellings) {
1058 MockTextCheckingCompletion completion;
1060 const base::string16 text = base::ASCIIToUTF16("apple, zz, orange, zz");
1061 spell_check()->RequestTextChecking(text, &completion);
1063 base::MessageLoop::current()->RunUntilIdle();
1065 EXPECT_EQ(completion.completion_count_, 1U);
1066 EXPECT_EQ(completion.last_results_.size(), 2U);
1067 EXPECT_EQ(completion.last_results_[0].location, 7);
1068 EXPECT_EQ(completion.last_results_[0].length, 2);
1069 EXPECT_EQ(completion.last_results_[1].location, 19);
1070 EXPECT_EQ(completion.last_results_[1].length, 2);
1073 // A test case that multiple requests comes at once. Make sure all
1074 // requests are processed.
1075 TEST_F(SpellCheckTest, RequestSpellCheckWithMultipleRequests) {
1076 MockTextCheckingCompletion completion[3];
1078 const base::string16 text[3] = {
1079 base::ASCIIToUTF16("what, zz"),
1080 base::ASCIIToUTF16("apple, zz"),
1081 base::ASCIIToUTF16("orange, zz")
1084 for (int i = 0; i < 3; ++i)
1085 spell_check()->RequestTextChecking(text[i], &completion[i]);
1087 base::MessageLoop::current()->RunUntilIdle();
1089 for (int i = 0; i < 3; ++i) {
1090 EXPECT_EQ(completion[i].completion_count_, 1U);
1091 EXPECT_EQ(completion[i].last_results_.size(), 1U);
1092 EXPECT_EQ(completion[i].last_results_[0].location, 6 + i);
1093 EXPECT_EQ(completion[i].last_results_[0].length, 2);
1097 // A test case that spellchecking is requested before initializing.
1098 // In this case, we postpone to post a request.
1099 TEST_F(SpellCheckTest, RequestSpellCheckWithoutInitialization) {
1100 UninitializeSpellCheck();
1102 MockTextCheckingCompletion completion;
1103 const base::string16 text = base::ASCIIToUTF16("zz");
1105 spell_check()->RequestTextChecking(text, &completion);
1107 // The task will not be posted yet.
1108 base::MessageLoop::current()->RunUntilIdle();
1109 EXPECT_EQ(completion.completion_count_, 0U);
1112 // Requests several spellchecking before initializing. Except the last one,
1113 // posting requests is cancelled and text is rendered as correct one.
1114 TEST_F(SpellCheckTest, RequestSpellCheckMultipleTimesWithoutInitialization) {
1115 UninitializeSpellCheck();
1117 MockTextCheckingCompletion completion[3];
1118 const base::string16 text[3] = {
1119 base::ASCIIToUTF16("what, zz"),
1120 base::ASCIIToUTF16("apple, zz"),
1121 base::ASCIIToUTF16("orange, zz")
1124 // Calls RequestTextchecking a few times.
1125 for (int i = 0; i < 3; ++i)
1126 spell_check()->RequestTextChecking(text[i], &completion[i]);
1128 // The last task will be posted after initialization, however the other
1129 // requests should be pressed without spellchecking.
1130 base::MessageLoop::current()->RunUntilIdle();
1131 for (int i = 0; i < 2; ++i)
1132 EXPECT_EQ(completion[i].completion_count_, 1U);
1133 EXPECT_EQ(completion[2].completion_count_, 0U);
1135 // Checks the last request is processed after initialization.
1136 InitializeSpellCheck("en-US");
1138 // Calls PostDelayedSpellCheckTask instead of OnInit here for simplicity.
1139 spell_check()->PostDelayedSpellCheckTask(
1140 spell_check()->pending_request_param_.release());
1141 base::MessageLoop::current()->RunUntilIdle();
1142 for (int i = 0; i < 3; ++i)
1143 EXPECT_EQ(completion[i].completion_count_, 1U);
1146 #endif
1148 // Verify that the SpellCheck class keeps the spelling marker added to a
1149 // misspelled word "zz".
1150 TEST_F(SpellCheckTest, CreateTextCheckingResultsKeepsMarkers) {
1151 base::string16 text = base::ASCIIToUTF16("zz");
1152 std::vector<SpellCheckResult> spellcheck_results;
1153 spellcheck_results.push_back(
1154 SpellCheckResult(SpellCheckResult::SPELLING, 0, 2, base::string16()));
1155 blink::WebVector<blink::WebTextCheckingResult> textcheck_results;
1156 spell_check()->CreateTextCheckingResults(SpellCheck::USE_NATIVE_CHECKER, 0,
1157 text, spellcheck_results,
1158 &textcheck_results);
1159 ASSERT_EQ(spellcheck_results.size(), textcheck_results.size());
1160 EXPECT_EQ(blink::WebTextDecorationTypeSpelling,
1161 textcheck_results[0].decoration);
1162 EXPECT_EQ(spellcheck_results[0].location, textcheck_results[0].location);
1163 EXPECT_EQ(spellcheck_results[0].length, textcheck_results[0].length);
1166 // Verify that the SpellCheck class replaces the spelling marker added to a
1167 // contextually-misspelled word "bean" with a grammar marker.
1168 TEST_F(SpellCheckTest, CreateTextCheckingResultsAddsGrammarMarkers) {
1169 base::string16 text = base::ASCIIToUTF16("I have bean to USA.");
1170 std::vector<SpellCheckResult> spellcheck_results;
1171 spellcheck_results.push_back(
1172 SpellCheckResult(SpellCheckResult::SPELLING, 7, 4, base::string16()));
1173 blink::WebVector<blink::WebTextCheckingResult> textcheck_results;
1174 spell_check()->CreateTextCheckingResults(SpellCheck::USE_NATIVE_CHECKER, 0,
1175 text, spellcheck_results,
1176 &textcheck_results);
1177 ASSERT_EQ(spellcheck_results.size(), textcheck_results.size());
1178 EXPECT_EQ(blink::WebTextDecorationTypeGrammar,
1179 textcheck_results[0].decoration);
1180 EXPECT_EQ(spellcheck_results[0].location, textcheck_results[0].location);
1181 EXPECT_EQ(spellcheck_results[0].length, textcheck_results[0].length);
1184 // Verify that the SpellCheck preserves the original apostrophe type in the
1185 // checked text, regardless of the type of apostrophe the browser returns.
1186 TEST_F(SpellCheckTest, CreateTextCheckingResultsKeepsTypographicalApostrophe) {
1187 base::string16 text = base::WideToUTF16(
1188 L"Ik've havn" TYPOGRAPHICAL_APOSTROPHE L"t ni'n"
1189 TYPOGRAPHICAL_APOSTROPHE L"out-s I've I" TYPOGRAPHICAL_APOSTROPHE
1190 L"ve");
1191 std::vector<SpellCheckResult> spellcheck_results;
1193 // All typewriter apostrophe results.
1194 spellcheck_results.push_back(SpellCheckResult(SpellCheckResult::SPELLING, 0,
1195 5, base::UTF8ToUTF16("I've")));
1196 spellcheck_results.push_back(SpellCheckResult(
1197 SpellCheckResult::SPELLING, 6, 6, base::UTF8ToUTF16("haven't")));
1198 spellcheck_results.push_back(SpellCheckResult(
1199 SpellCheckResult::SPELLING, 13, 10, base::UTF8ToUTF16("in'n'out's")));
1201 // Replacements that differ only by apostrophe type should be ignored.
1202 spellcheck_results.push_back(SpellCheckResult(SpellCheckResult::SPELLING, 24,
1203 4, base::UTF8ToUTF16("I've")));
1204 spellcheck_results.push_back(SpellCheckResult(SpellCheckResult::SPELLING, 29,
1205 4, base::UTF8ToUTF16("I've")));
1207 // All typographical apostrophe results.
1208 spellcheck_results.push_back(
1209 SpellCheckResult(SpellCheckResult::SPELLING, 0, 5,
1210 base::WideToUTF16(L"I" TYPOGRAPHICAL_APOSTROPHE L"ve")));
1211 spellcheck_results.push_back(SpellCheckResult(
1212 SpellCheckResult::SPELLING, 6, 6,
1213 base::WideToUTF16(L"haven" TYPOGRAPHICAL_APOSTROPHE L"t")));
1214 spellcheck_results.push_back(SpellCheckResult(
1215 SpellCheckResult::SPELLING, 13, 10, base::WideToUTF16(
1216 L"in" TYPOGRAPHICAL_APOSTROPHE L"n" TYPOGRAPHICAL_APOSTROPHE L"out"
1217 TYPOGRAPHICAL_APOSTROPHE L"s")));
1219 // Replacements that differ only by apostrophe type should be ignored.
1220 spellcheck_results.push_back(
1221 SpellCheckResult(SpellCheckResult::SPELLING, 24, 4,
1222 base::WideToUTF16(L"I" TYPOGRAPHICAL_APOSTROPHE L"ve")));
1223 spellcheck_results.push_back(
1224 SpellCheckResult(SpellCheckResult::SPELLING, 29, 4,
1225 base::WideToUTF16(L"I" TYPOGRAPHICAL_APOSTROPHE L"ve")));
1227 blink::WebVector<blink::WebTextCheckingResult> textcheck_results;
1228 spell_check()->CreateTextCheckingResults(SpellCheck::USE_NATIVE_CHECKER, 0,
1229 text, spellcheck_results,
1230 &textcheck_results);
1232 static const wchar_t* kExpectedReplacements[] = {
1233 L"I've",
1234 L"haven" TYPOGRAPHICAL_APOSTROPHE L"t",
1235 L"in'n" TYPOGRAPHICAL_APOSTROPHE L"out's",
1236 L"I've",
1237 L"haven" TYPOGRAPHICAL_APOSTROPHE L"t",
1238 L"in'n" TYPOGRAPHICAL_APOSTROPHE L"out" TYPOGRAPHICAL_APOSTROPHE L"s",
1241 ASSERT_EQ(arraysize(kExpectedReplacements), textcheck_results.size());
1242 for (size_t i = 0; i < arraysize(kExpectedReplacements); ++i) {
1243 EXPECT_EQ(base::WideToUTF16(kExpectedReplacements[i]),
1244 textcheck_results[i].replacement)
1245 << "i=" << i << "\nactual: \""
1246 << base::string16(textcheck_results[i].replacement) << "\"";
1250 // Checks some words that should be present in all English dictionaries.
1251 TEST_F(SpellCheckTest, EnglishWords) {
1252 static const struct {
1253 const char* input;
1254 bool should_pass;
1255 } kTestCases[] = {
1256 // Issue 146093: "Chromebook" and "Chromebox" not included in spell-checking
1257 // dictionary.
1258 {"Chromebook", true},
1259 {"Chromebooks", true},
1260 {"Chromebox", true},
1261 {"Chromeboxes", true},
1262 {"Chromeblade", true},
1263 {"Chromeblades", true},
1264 {"Chromebase", true},
1265 {"Chromebases", true},
1266 // Issue 94708: Spell-checker incorrectly reports whisky as misspelled.
1267 {"whisky", true},
1268 {"whiskey", true},
1269 {"whiskies", true},
1270 // Issue 98678: "Recency" should be included in client-side dictionary.
1271 {"recency", true},
1272 {"recencies", false},
1273 // Issue 140486
1274 {"movie", true},
1275 {"movies", true},
1278 static const char* const kLocales[] = { "en-GB", "en-US", "en-CA", "en-AU" };
1280 for (size_t j = 0; j < arraysize(kLocales); ++j) {
1281 ReinitializeSpellCheck(kLocales[j]);
1282 for (size_t i = 0; i < arraysize(kTestCases); ++i) {
1283 size_t input_length = 0;
1284 if (kTestCases[i].input != NULL)
1285 input_length = strlen(kTestCases[i].input);
1287 int misspelling_start = 0;
1288 int misspelling_length = 0;
1289 bool result = spell_check()->SpellCheckWord(
1290 base::ASCIIToUTF16(kTestCases[i].input).c_str(),
1291 kNoOffset,
1292 static_cast<int>(input_length),
1293 kNoTag,
1294 &misspelling_start,
1295 &misspelling_length, NULL);
1297 EXPECT_EQ(kTestCases[i].should_pass, result) << kTestCases[i].input <<
1298 " in " << kLocales[j];
1303 // Checks that NOSUGGEST works in English dictionaries.
1304 TEST_F(SpellCheckTest, NoSuggest) {
1305 static const struct {
1306 const char* input;
1307 const char* suggestion;
1308 const char* locale;
1309 bool should_pass;
1310 } kTestCases[] = {
1311 {"suckerbert", "cocksucker", "en-GB", true},
1312 {"suckerbert", "cocksucker", "en-US", true},
1313 {"suckerbert", "cocksucker", "en-CA", true},
1314 {"suckerbert", "cocksucker", "en-AU", true},
1315 {"suckerbert", "cocksuckers", "en-GB", true},
1316 {"suckerbert", "cocksuckers", "en-US", true},
1317 {"suckerbert", "cocksuckers", "en-CA", true},
1318 {"suckerbert", "cocksuckers", "en-AU", true},
1319 {"Batasunaa", "Batasuna", "ca-ES", true},
1320 {"pornoo", "porno", "it-IT", true},
1321 {"catass", "catas", "lt-LT", true},
1322 {"kuracc", "kurac", "sl-SI", true},
1323 {"pittt", "pitt", "sv-SE", true},
1326 size_t test_cases_size = arraysize(kTestCases);
1327 for (size_t i = 0; i < test_cases_size; ++i) {
1328 ReinitializeSpellCheck(kTestCases[i].locale);
1329 size_t suggestion_length = 0;
1330 if (kTestCases[i].suggestion != NULL)
1331 suggestion_length = strlen(kTestCases[i].suggestion);
1333 // First check that the NOSUGGEST flag didn't mark this word as not being in
1334 // the dictionary.
1335 int misspelling_start = 0;
1336 int misspelling_length = 0;
1337 bool result = spell_check()->SpellCheckWord(
1338 base::ASCIIToUTF16(kTestCases[i].suggestion).c_str(),
1339 kNoOffset,
1340 static_cast<int>(suggestion_length),
1341 kNoTag,
1342 &misspelling_start,
1343 &misspelling_length, NULL);
1345 EXPECT_EQ(kTestCases[i].should_pass, result) << kTestCases[i].suggestion <<
1346 " in " << kTestCases[i].locale;
1348 // Now verify that this test case does not show up as a suggestion.
1349 std::vector<base::string16> suggestions;
1350 size_t input_length = 0;
1351 if (kTestCases[i].input != NULL)
1352 input_length = strlen(kTestCases[i].input);
1353 result = spell_check()->SpellCheckWord(
1354 base::ASCIIToUTF16(kTestCases[i].input).c_str(),
1355 kNoOffset,
1356 static_cast<int>(input_length),
1357 kNoTag,
1358 &misspelling_start,
1359 &misspelling_length,
1360 &suggestions);
1361 // Input word should be a misspelling.
1362 EXPECT_FALSE(result) << kTestCases[i].input
1363 << " is not a misspelling in "
1364 << kTestCases[i].locale;
1365 // Check if the suggested words occur.
1366 for (int j = 0; j < static_cast<int>(suggestions.size()); j++) {
1367 for (size_t t = 0; t < test_cases_size; t++) {
1368 int compare_result = suggestions.at(j).compare(
1369 base::ASCIIToUTF16(kTestCases[t].suggestion));
1370 EXPECT_FALSE(compare_result == 0) << kTestCases[t].suggestion <<
1371 " in " << kTestCases[i].locale;
1377 // Check that the correct dictionary files are checked in.
1378 TEST_F(SpellCheckTest, DictionaryFiles) {
1379 std::vector<std::string> spellcheck_languages;
1380 chrome::spellcheck_common::SpellCheckLanguages(&spellcheck_languages);
1381 EXPECT_FALSE(spellcheck_languages.empty());
1383 base::FilePath hunspell = GetHunspellDirectory();
1384 for (size_t i = 0; i < spellcheck_languages.size(); ++i) {
1385 base::FilePath dict = chrome::spellcheck_common::GetVersionedFileName(
1386 spellcheck_languages[i], hunspell);
1387 EXPECT_TRUE(base::PathExists(dict)) << dict.value() << " not found";
1391 // TODO(groby): Add a test for hunspell itself, when MAXWORDLEN is exceeded.
1392 TEST_F(SpellCheckTest, SpellingEngine_CheckSpelling) {
1393 static const struct {
1394 const char* word;
1395 bool expected_result;
1396 } kTestCases[] = {
1397 { "", true },
1398 { "automatic", true },
1399 { "hello", true },
1400 { "forglobantic", false },
1401 { "xfdssfsdfaasds", false },
1402 { // 64 chars are the longest word to check - this should fail checking.
1403 "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl",
1404 false
1406 { // Any word longer than 64 chars should be exempt from checking.
1407 "reallylongwordthatabsolutelyexceedsthespecifiedcharacterlimitabit",
1408 true
1412 // Initialization magic - call InitializeIfNeeded twice. The first one simply
1413 // flags internal state that a dictionary was requested. The second one will
1414 // take the passed-in file and initialize hunspell with it. (The file was
1415 // passed to hunspell in the ctor for the test fixture).
1416 // This needs to be done since we need to ensure the SpellingEngine objects
1417 // contained in the SpellcheckLanguages in |languages_| from the test fixture
1418 // does get initialized.
1419 // TODO(groby): Clean up this mess.
1420 InitializeIfNeeded();
1421 ASSERT_FALSE(InitializeIfNeeded());
1423 for (size_t i = 0; i < arraysize(kTestCases); ++i) {
1424 bool result = CheckSpelling(kTestCases[i].word, 0);
1425 EXPECT_EQ(kTestCases[i].expected_result, result) <<
1426 "Failed test for " << kTestCases[i].word;
1430 // Chrome should not suggest "Othello" for "hellllo" or "identically" for
1431 // "accidently".
1432 TEST_F(SpellCheckTest, LogicalSuggestions) {
1433 static const struct {
1434 const char* misspelled;
1435 const char* suggestion;
1436 } kTestCases[] = {
1437 { "hellllo", "hello" },
1438 { "accidently", "accidentally" }
1441 for (size_t i = 0; i < arraysize(kTestCases); ++i) {
1442 int misspelling_start = 0;
1443 int misspelling_length = 0;
1444 std::vector<base::string16> suggestions;
1445 EXPECT_FALSE(spell_check()->SpellCheckWord(
1446 base::ASCIIToUTF16(kTestCases[i].misspelled).c_str(),
1447 kNoOffset,
1448 strlen(kTestCases[i].misspelled),
1449 kNoTag,
1450 &misspelling_start,
1451 &misspelling_length,
1452 &suggestions));
1453 EXPECT_GE(suggestions.size(), static_cast<size_t>(1));
1454 if (suggestions.size() > 0)
1455 EXPECT_EQ(suggestions[0], base::ASCIIToUTF16(kTestCases[i].suggestion));
1459 // Words with apostrophes should be valid contractions.
1460 TEST_F(SpellCheckTest, IsValidContraction) {
1461 static const char* kLanguages[] = {
1462 "en-AU",
1463 "en-CA",
1464 "en-GB",
1465 "en-US",
1468 static const wchar_t* kWords[] = {
1469 L"in'n'out",
1470 L"in" TYPOGRAPHICAL_APOSTROPHE L"n" TYPOGRAPHICAL_APOSTROPHE L"out",
1473 for (size_t i = 0; i < arraysize(kLanguages); ++i) {
1474 ReinitializeSpellCheck(kLanguages[i]);
1475 for (size_t j = 0; j < arraysize(kWords); ++j)
1476 EXPECT_TRUE(IsValidContraction(base::WideToUTF16(kWords[j]), 0));