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