1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
// Copyright 2008, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "webkit/glue/webkit_glue.h"
#include "base/file_util.h"
#include "base/path_service.h"
#include "chrome/browser/spellchecker.h"
#include "chrome/common/chrome_paths.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
class SpellCheckTest : public testing::Test {
};
} // namespace
// Represents a special initialization function used only for the unit tests
// in this file.
extern void InitHunspellWithFiles(FILE* file_aff_hunspell,
FILE* file_dic_hunspell);
// Operates unit tests for the webkit_glue::SpellCheckWord() function
// with the US English dictionary.
// The unit tests in this function consist of:
// * Tests for the function with empty strings;
// * Tests for the function with a valid English word;
// * Tests for the function with a valid non-English word;
// * Tests for the function with a valid English word with a preceding
// space character;
// * Tests for the function with a valid English word with a preceding
// non-English word;
// * Tests for the function with a valid English word with a following
// space character;
// * Tests for the function with a valid English word with a following
// non-English word;
// * Tests for the function with two valid English words concatenated
// with space characters or non-English words;
// * Tests for the function with an invalid English word;
// * Tests for the function with an invalid English word with a preceding
// space character;
// * Tests for the function with an invalid English word with a preceding
// non-English word;
// * Tests for the function with2 an invalid English word with a following
// space character;
// * Tests for the function with an invalid English word with a following
// non-English word, and;
// * Tests for the function with two invalid English words concatenated
// with space characters or non-English words.
// A test with a "[ROBUSTNESS]" mark shows it is a robustness test and it uses
// grammartically incorrect string.
// TODO(hbono): Please feel free to add more tests.
TEST(SpellCheckTest, SpellCheckStrings_EN_US) {
static const struct {
// A string to be tested.
const wchar_t* input;
// An expected result for this test case.
// * true: the input string does not have any invalid words.
// * false: the input string has one or more invalid words.
bool expected_result;
// The position and the length of the first invalid word.
int misspelling_start;
int misspelling_length;
} kTestCases[] = {
// Empty strings.
{NULL, true, 0, 0},
{L"", true, 0, 0},
{L" ", true, 0, 0},
{L"\xA0", true, 0, 0},
{L"\x3000", true, 0, 0},
// A valid English word "hello".
{L"hello", true, 0, 0},
// A valid Chinese word (meaning "hello") consisiting of two CJKV
// ideographs
{L"\x4F60\x597D", true, 0, 0},
// A valid Korean word (meaning "hello") consisting of five hangul
// syllables
{L"\xC548\xB155\xD558\xC138\xC694", true, 0, 0},
// A valid Japanese word (meaning "hello") consisting of five Hiragana
// letters
{L"\x3053\x3093\x306B\x3061\x306F", true, 0, 0},
// A valid Hindi word (meaning ?) consisting of six Devanagari letters
// (This word is copied from "http://b/issue?id=857583".)
{L"\x0930\x093E\x091C\x0927\x093E\x0928", true, 0, 0},
// A valid English word "affix" using a Latin ligature 'ffi'
{L"a\xFB03x", true, 0, 0},
// A valid English word "hello" (fullwidth version)
{L"\xFF28\xFF45\xFF4C\xFF4C\xFF4F", true, 0, 0},
// Two valid Greek words (meaning "hello") consisting of seven Greek
// letters
{L"\x03B3\x03B5\x03B9\x03AC" L" " L"\x03C3\x03BF\x03C5", true, 0, 0},
// A valid Russian word (meainng "hello") consisting of twelve Cyrillic
// letters
{L"\x0437\x0434\x0440\x0430\x0432\x0441"
L"\x0442\x0432\x0443\x0439\x0442\x0435", true, 0, 0},
// A valid English contraction
{L"isn't", true, 0, 0},
// A valid English word enclosed with underscores.
{L"_hello_", true, 0, 0},
// A valid English word with a preceding whitespace
{L" " L"hello", true, 0, 0},
// A valid English word with a preceding no-break space
{L"\xA0" L"hello", true, 0, 0},
// A valid English word with a preceding ideographic space
{L"\x3000" L"hello", true, 0, 0},
// A valid English word with a preceding Chinese word
{L"\x4F60\x597D" L"hello", true, 0, 0},
// [ROBUSTNESS] A valid English word with a preceding Korean word
{L"\xC548\xB155\xD558\xC138\xC694" L"hello", true, 0, 0},
// A valid English word with a preceding Japanese word
{L"\x3053\x3093\x306B\x3061\x306F" L"hello", true, 0, 0},
// [ROBUSTNESS] A valid English word with a preceding Hindi word
{L"\x0930\x093E\x091C\x0927\x093E\x0928" L"hello", true, 0, 0},
// [ROBUSTNESS] A valid English word with two preceding Greek words
{L"\x03B3\x03B5\x03B9\x03AC" L" " L"\x03C3\x03BF\x03C5"
L"hello", true, 0, 0},
// [ROBUSTNESS] A valid English word with a preceding Russian word
{L"\x0437\x0434\x0440\x0430\x0432\x0441"
L"\x0442\x0432\x0443\x0439\x0442\x0435" L"hello", true, 0, 0},
// A valid English word with a following whitespace
{L"hello" L" ", true, 0, 0},
// A valid English word with a following no-break space
{L"hello" L"\xA0", true, 0, 0},
// A valid English word with a following ideographic space
{L"hello" L"\x3000", true, 0, 0},
// A valid English word with a following Chinese word
{L"hello" L"\x4F60\x597D", true, 0, 0},
// [ROBUSTNESS] A valid English word with a following Korean word
{L"hello" L"\xC548\xB155\xD558\xC138\xC694", true, 0, 0},
// A valid English word with a following Japanese word
{L"hello" L"\x3053\x3093\x306B\x3061\x306F", true, 0, 0},
// [ROBUSTNESS] A valid English word with a following Hindi word
{L"hello" L"\x0930\x093E\x091C\x0927\x093E\x0928", true, 0, 0},
// [ROBUSTNESS] A valid English word with two following Greek words
{L"hello"
L"\x03B3\x03B5\x03B9\x03AC" L" " L"\x03C3\x03BF\x03C5", true, 0, 0},
// [ROBUSTNESS] A valid English word with a following Russian word
{L"hello" L"\x0437\x0434\x0440\x0430\x0432\x0441"
L"\x0442\x0432\x0443\x0439\x0442\x0435", true, 0, 0},
// Two valid English words concatenated with a whitespace
{L"hello" L" " L"hello", true, 0, 0},
// Two valid English words concatenated with a no-break space
{L"hello" L"\xA0" L"hello", true, 0, 0},
// Two valid English words concatenated with an ideographic space
{L"hello" L"\x3000" L"hello", true, 0, 0},
// Two valid English words concatenated with a Chinese word
{L"hello" L"\x4F60\x597D" L"hello", true, 0, 0},
// [ROBUSTNESS] Two valid English words concatenated with a Korean word
{L"hello" L"\xC548\xB155\xD558\xC138\xC694" L"hello", true, 0, 0},
// Two valid English words concatenated with a Japanese word
{L"hello" L"\x3053\x3093\x306B\x3061\x306F" L"hello", true, 0, 0},
// [ROBUSTNESS] Two valid English words concatenated with a Hindi word
{L"hello" L"\x0930\x093E\x091C\x0927\x093E\x0928" L"hello" , true, 0, 0},
// [ROBUSTNESS] Two valid English words concatenated with two Greek words
{L"hello" L"\x03B3\x03B5\x03B9\x03AC" L" " L"\x03C3\x03BF\x03C5"
L"hello", true, 0, 0},
// [ROBUSTNESS] Two valid English words concatenated with a Russian word
{L"hello" L"\x0437\x0434\x0440\x0430\x0432\x0441"
L"\x0442\x0432\x0443\x0439\x0442\x0435" L"hello", true, 0, 0},
// [ROBUSTNESS] Two valid English words concatenated with a contraction
// character.
{L"hello:hello", true, 0, 0},
// An invalid English word
{L"ifmmp", false, 0, 5},
// An invalid English word "bffly" containing a Latin ligature 'ffl'
{L"b\xFB04y", false, 0, 3},
// An invalid English word "ifmmp" (fullwidth version)
{L"\xFF29\xFF46\xFF4D\xFF4D\xFF50", false, 0, 5},
// An invalid English contraction
{L"jtm'u", false, 0, 5},
// An invalid English word enclosed with underscores.
{L"_ifmmp_", false, 1, 5},
// An invalid English word with a preceding whitespace
{L" " L"ifmmp", false, 1, 5},
// An invalid English word with a preceding no-break space
{L"\xA0" L"ifmmp", false, 1, 5},
// An invalid English word with a preceding ideographic space
{L"\x3000" L"ifmmp", false, 1, 5},
// An invalid English word with a preceding Chinese word
{L"\x4F60\x597D" L"ifmmp", false, 2, 5},
// [ROBUSTNESS] An invalid English word with a preceding Korean word
{L"\xC548\xB155\xD558\xC138\xC694" L"ifmmp", false, 5, 5},
// An invalid English word with a preceding Japanese word
{L"\x3053\x3093\x306B\x3061\x306F" L"ifmmp", false, 5, 5},
// [ROBUSTNESS] An invalid English word with a preceding Hindi word
{L"\x0930\x093E\x091C\x0927\x093E\x0928" L"ifmmp", false, 6, 5},
// [ROBUSTNESS] An invalid English word with two preceding Greek words
{L"\x03B3\x03B5\x03B9\x03AC" L" " L"\x03C3\x03BF\x03C5"
L"ifmmp", false, 8, 5},
// [ROBUSTNESS] An invalid English word with a preceding Russian word
{L"\x0437\x0434\x0440\x0430\x0432\x0441"
L"\x0442\x0432\x0443\x0439\x0442\x0435" L"ifmmp", false, 12, 5},
// An invalid English word with a following whitespace
{L"ifmmp" L" ", false, 0, 5},
// An invalid English word with a following no-break space
{L"ifmmp" L"\xA0", false, 0, 5},
// An invalid English word with a following ideographic space
{L"ifmmp" L"\x3000", false, 0, 5},
// An invalid English word with a following Chinese word
{L"ifmmp" L"\x4F60\x597D", false, 0, 5},
// [ROBUSTNESS] An invalid English word with a following Korean word
{L"ifmmp" L"\xC548\xB155\xD558\xC138\xC694", false, 0, 5},
// An invalid English word with a following Japanese word
{L"ifmmp" L"\x3053\x3093\x306B\x3061\x306F", false, 0, 5},
// [ROBUSTNESS] An invalid English word with a following Hindi word
{L"ifmmp" L"\x0930\x093E\x091C\x0927\x093E\x0928", false, 0, 5},
// [ROBUSTNESS] An invalid English word with two following Greek words
{L"ifmmp"
L"\x03B3\x03B5\x03B9\x03AC" L" " L"\x03C3\x03BF\x03C5", false, 0, 5},
// [ROBUSTNESS] An invalid English word with a following Russian word
{L"ifmmp" L"\x0437\x0434\x0440\x0430\x0432\x0441"
L"\x0442\x0432\x0443\x0439\x0442\x0435", false, 0, 5},
// Two invalid English words concatenated with a whitespace
{L"ifmmp" L" " L"ifmmp", false, 0, 5},
// Two invalid English words concatenated with a no-break space
{L"ifmmp" L"\xA0" L"ifmmp", false, 0, 5},
// Two invalid English words concatenated with an ideographic space
{L"ifmmp" L"\x3000" L"ifmmp", false, 0, 5},
// Two invalid English words concatenated with a Chinese word
{L"ifmmp" L"\x4F60\x597D" L"ifmmp", false, 0, 5},
// [ROBUSTNESS] Two invalid English words concatenated with a Korean word
{L"ifmmp" L"\xC548\xB155\xD558\xC138\xC694" L"ifmmp", false, 0, 5},
// Two invalid English words concatenated with a Japanese word
{L"ifmmp" L"\x3053\x3093\x306B\x3061\x306F" L"ifmmp", false, 0, 5},
// [ROBUSTNESS] Two invalid English words concatenated with a Hindi word
{L"ifmmp" L"\x0930\x093E\x091C\x0927\x093E\x0928" L"ifmmp" , false, 0, 5},
// [ROBUSTNESS] Two invalid English words concatenated with two Greek words
{L"ifmmp" L"\x03B3\x03B5\x03B9\x03AC" L" " L"\x03C3\x03BF\x03C5"
L"ifmmp", false, 0, 5},
// [ROBUSTNESS] Two invalid English words concatenated with a Russian word
{L"ifmmp" L"\x0437\x0434\x0440\x0430\x0432\x0441"
L"\x0442\x0432\x0443\x0439\x0442\x0435" L"ifmmp", false, 0, 5},
// [ROBUSTNESS] Two invalid English words concatenated with a contraction
// character.
{L"ifmmp:ifmmp", false, 0, 11},
};
std::wstring hunspell_directory;
ASSERT_TRUE(PathService::Get(chrome::DIR_APP_DICTIONARIES,
&hunspell_directory));
scoped_refptr<SpellChecker> spell_checker(new SpellChecker(
hunspell_directory, L"en-US", NULL));
for (int i = 0; i < arraysize(kTestCases); i++) {
size_t input_length = 0;
if (kTestCases[i].input != NULL) {
input_length = wcslen(kTestCases[i].input);
}
int misspelling_start;
int misspelling_length;
bool result = spell_checker->SpellCheckWord(kTestCases[i].input,
static_cast<int>(input_length),
&misspelling_start,
&misspelling_length, NULL);
EXPECT_EQ(result, kTestCases[i].expected_result);
EXPECT_EQ(misspelling_start, kTestCases[i].misspelling_start);
EXPECT_EQ(misspelling_length, kTestCases[i].misspelling_length);
}
}
TEST(SpellCheckTest, SpellCheckSuggestions_EN_US) {
static const struct {
// A string to be tested.
const wchar_t* input;
// An expected result for this test case.
// * true: the input string does not have any invalid words.
// * false: the input string has one or more invalid words.
bool expected_result;
// The position and the length of the first invalid word.
int misspelling_start;
int misspelling_length;
// A suggested word that should occur.
const wchar_t* suggested_word;
} kTestCases[] = { // A valid English word with a preceding whitespace
{L"ello", false, 0, 0, L"hello"},
{L"ello", false, 0, 0, L"cello"},
{L"wate", false, 0, 0, L"water"},
{L"wate", false, 0, 0, L"waste"},
{L"wate", false, 0, 0, L"sate"},
{L"wate", false, 0, 0, L"rate"},
{L"jum", false, 0, 0, L"jump"},
{L"jum", false, 0, 0, L"rum"},
{L"jum", false, 0, 0, L"sum"},
{L"jum", false, 0, 0, L"tum"},
// TODO (Sidchat): add many more examples.
};
std::wstring hunspell_directory;
ASSERT_TRUE(PathService::Get(chrome::DIR_APP_DICTIONARIES,
&hunspell_directory));
scoped_refptr<SpellChecker> spell_checker(new SpellChecker(
hunspell_directory, L"en-US", NULL));
for (int i = 0; i < arraysize(kTestCases); i++) {
std::vector<std::wstring> suggestions;
size_t input_length = 0;
if (kTestCases[i].input != NULL) {
input_length = wcslen(kTestCases[i].input);
}
int misspelling_start;
int misspelling_length;
bool result = spell_checker->SpellCheckWord(kTestCases[i].input,
static_cast<int>(input_length),
&misspelling_start,
&misspelling_length,
&suggestions);
// Check for spelling.
EXPECT_EQ(result, kTestCases[i].expected_result);
// Check if the suggested words occur.
bool suggested_word_is_present = false;
for (int j=0; j < static_cast<int>(suggestions.size()); j++) {
if (suggestions.at(j).compare(kTestCases[i].suggested_word) == 0) {
suggested_word_is_present = true;
break;
}
}
EXPECT_TRUE(suggested_word_is_present);
}
}
|