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