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