Debian Codegraph @

closure-library/closure/goog/ui/plaintextspellchecker_test.js

raw
1 // Copyright 2007 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 goog.provide('goog.ui.PlainTextSpellCheckerTest');
16 goog.setTestOnly('goog.ui.PlainTextSpellCheckerTest');
17
18 goog.require('goog.Timer');
19 goog.require('goog.dom');
20 goog.require('goog.events.KeyCodes');
21 goog.require('goog.spell.SpellCheck');
22 goog.require('goog.testing.events');
23 goog.require('goog.testing.jsunit');
24 goog.require('goog.ui.PlainTextSpellChecker');
25
26 var missspelling = 'missspelling';
27 var iggnore = 'iggnore';
28 var vocabulary = ['test', 'words', 'a', 'few', missspelling, iggnore];
29
30 // We don't use Math.random() to make test predictable. Math.random is not
31 // repeatable, so a success on the dev machine != success in the lab (or on
32 // other dev machines). This is the same pseudorandom logic that CRT rand()
33 // uses.
34 var rseed = 1;
35 function random(range) {
36 rseed = (rseed * 1103515245 + 12345) & 0xffffffff;
37 return ((rseed >> 16) & 0x7fff) % range;
38 }
39
40 function localSpellCheckingFunction(words, spellChecker, callback) {
41 var len = words.length;
42 var results = [];
43 for (var i = 0; i < len; i++) {
44 var word = words[i];
45 var found = false;
46 // Last two words are considered misspellings
47 for (var j = 0; j < vocabulary.length - 2; ++j) {
48 if (vocabulary[j] == word) {
49 found = true;
50 break;
51 }
52 }
53 if (found) {
54 results.push([word, goog.spell.SpellCheck.WordStatus.VALID]);
55 } else {
56 results.push(
57 [word, goog.spell.SpellCheck.WordStatus.INVALID, ['foo', 'bar']]);
58 }
59 }
60 callback.call(spellChecker, results);
61 }
62
63 function generateRandomSpace() {
64 var string = '';
65 var nSpace = 1 + random(4);
66 for (var i = 0; i < nSpace; ++i) {
67 string += ' ';
68 }
69 return string;
70 }
71
72 function generateRandomString(maxWords, doQuotes) {
73 var x = random(10);
74 var string = '';
75 if (doQuotes) {
76 if (x == 0) {
77 string = 'On xxxxx yyyy wrote:\n> ';
78 } else if (x < 3) {
79 string = '> ';
80 }
81 }
82
83 var nWords = 1 + random(maxWords);
84 for (var i = 0; i < nWords; ++i) {
85 string += vocabulary[random(vocabulary.length)];
86 string += generateRandomSpace();
87 }
88 return string;
89 }
90
91 var timerQueue = [];
92 function processTimerQueue() {
93 while (timerQueue.length > 0) {
94 var fn = timerQueue.shift();
95 fn();
96 }
97 }
98
99 function localTimer(fn, delay, obj) {
100 if (obj) {
101 fn = goog.bind(fn, obj);
102 }
103 timerQueue.push(fn);
104 return timerQueue.length;
105 }
106
107 function testPlainTextSpellCheckerNoQuotes() {
108 var handler = new goog.spell.SpellCheck(localSpellCheckingFunction);
109 var s = new goog.ui.PlainTextSpellChecker(handler);
110 s.asyncWordsPerBatch_ = 100;
111 var el = document.getElementById('test1');
112 s.decorate(el);
113 var text = '';
114 for (var i = 0; i < 10; ++i) {
115 text += generateRandomString(10, false) + '\n';
116 }
117 el.value = text;
118 // Yes this looks bizarre. This is for '\n' processing.
119 // They get converted to CRLF as part of the above statement.
120 text = el.value;
121
122 var timerSav = goog.Timer.callOnce;
123 goog.Timer.callOnce = localTimer;
124
125 s.check();
126 processTimerQueue();
127 s.ignoreWord(iggnore);
128 processTimerQueue();
129 s.check();
130 processTimerQueue();
131 s.resume();
132 processTimerQueue();
133
134 goog.Timer.callOnce = timerSav;
135
136 assertEquals(
137 'Spell checker run should not change the underlying element.', text,
138 el.value);
139 s.dispose();
140 }
141
142 function testPlainTextSpellCheckerWithQuotes() {
143 var handler = new goog.spell.SpellCheck(localSpellCheckingFunction);
144 var s = new goog.ui.PlainTextSpellChecker(handler);
145 s.asyncWordsPerBatch_ = 100;
146 var el = document.getElementById('test2');
147 s.decorate(el);
148 var text = '';
149 for (var i = 0; i < 10; ++i) {
150 text += generateRandomString(10, true) + '\n';
151 }
152 el.value = text;
153 // Yes this looks bizarre. This is for '\n' processing.
154 // They get converted to CRLF as part of the above statement.
155 text = el.value;
156
157 var timerSav = goog.Timer.callOnce;
158 goog.Timer.callOnce = localTimer;
159
160 s.setExcludeMarker(new RegExp('\nOn .* wrote:\n(> .*\n)+|\n(> .*\n)', 'g'));
161 s.check();
162 processTimerQueue();
163 s.ignoreWord(iggnore);
164 processTimerQueue();
165 s.check();
166 processTimerQueue();
167 s.resume();
168 processTimerQueue();
169
170 goog.Timer.callOnce = timerSav;
171
172 assertEquals(
173 'Spell checker run should not change the underlying element.', text,
174 el.value);
175 s.dispose();
176 }
177
178 function testPlainTextSpellCheckerWordReplacement() {
179 var handler = new goog.spell.SpellCheck(localSpellCheckingFunction);
180 var s = new goog.ui.PlainTextSpellChecker(handler);
181 s.asyncWordsPerBatch_ = 100;
182 var el = document.getElementById('test3');
183 s.decorate(el);
184 var text = '';
185 for (var i = 0; i < 10; ++i) {
186 text += generateRandomString(10, false) + '\n';
187 }
188 el.value = text;
189
190 var timerSav = goog.Timer.callOnce;
191 goog.Timer.callOnce = localTimer;
192
193 s.check();
194 processTimerQueue();
195
196 var container = s.overlay_;
197 var wordEl = container.firstChild;
198 while (wordEl) {
199 if (goog.dom.getTextContent(wordEl) == missspelling) {
200 break;
201 }
202 wordEl = wordEl.nextSibling;
203 }
204
205 if (!wordEl) {
206 assertTrue(
207 'Cannot find the world that should have been here.' +
208 'Please revise the test',
209 false);
210 return;
211 }
212
213 s.activeWord_ = missspelling;
214 s.activeElement_ = wordEl;
215 var suggestions = s.getSuggestions_();
216 s.replaceWord(wordEl, missspelling, 'foo');
217 assertEquals(
218 'Should have set the original word attribute!',
219 wordEl.getAttribute(goog.ui.AbstractSpellChecker.ORIGINAL_),
220 missspelling);
221
222 s.activeWord_ = goog.dom.getTextContent(wordEl);
223 s.activeElement_ = wordEl;
224 var newSuggestions = s.getSuggestions_();
225 assertEquals(
226 'Suggestion list should still be present even if the word ' +
227 'is now correct!',
228 suggestions, newSuggestions);
229
230 s.resume();
231 processTimerQueue();
232
233 goog.Timer.callOnce = timerSav;
234 s.dispose();
235 }
236
237
238 function testPlainTextSpellCheckerKeyboardNavigateNext() {
239 var handler = new goog.spell.SpellCheck(localSpellCheckingFunction);
240 var s = new goog.ui.PlainTextSpellChecker(handler);
241 var el = document.getElementById('test4');
242 s.decorate(el);
243 var text = 'a unit test for keyboard test';
244 el.value = text;
245 var keyEventProperties = {};
246 keyEventProperties.ctrlKey = true;
247 keyEventProperties.shiftKey = false;
248
249 var timerSav = goog.Timer.callOnce;
250 goog.Timer.callOnce = localTimer;
251
252 s.check();
253 processTimerQueue();
254
255 var container = s.overlay_;
256
257 // First call just moves focus to first misspelled word.
258 goog.testing.events.fireKeySequence(
259 container, goog.events.KeyCodes.RIGHT, keyEventProperties);
260
261 // Test moving from first to second misspelled word.
262 var defaultExecuted = goog.testing.events.fireKeySequence(
263 container, goog.events.KeyCodes.RIGHT, keyEventProperties);
264
265 assertFalse(
266 'The default action should be prevented for the key event',
267 defaultExecuted);
268 assertEquals(
269 'The second misspelled word should have focus.', document.activeElement,
270 container.children[1]);
271
272 s.resume();
273 processTimerQueue();
274
275 goog.Timer.callOnce = timerSav;
276 s.dispose();
277 }
278
279
280 function testPlainTextSpellCheckerKeyboardNavigateNextOnLastWord() {
281 var handler = new goog.spell.SpellCheck(localSpellCheckingFunction);
282 var s = new goog.ui.PlainTextSpellChecker(handler);
283 var el = document.getElementById('test5');
284 s.decorate(el);
285 var text = 'a unit test for keyboard test';
286 el.value = text;
287 var keyEventProperties = {};
288 keyEventProperties.ctrlKey = true;
289 keyEventProperties.shiftKey = false;
290
291 var timerSav = goog.Timer.callOnce;
292 goog.Timer.callOnce = localTimer;
293
294 s.check();
295 processTimerQueue();
296
297 var container = s.overlay_;
298
299 // First call just moves focus to first misspelled word.
300 goog.testing.events.fireKeySequence(
301 container, goog.events.KeyCodes.RIGHT, keyEventProperties);
302 goog.testing.events.fireKeySequence(
303 container, goog.events.KeyCodes.RIGHT, keyEventProperties);
304 goog.testing.events.fireKeySequence(
305 container, goog.events.KeyCodes.RIGHT, keyEventProperties);
306
307 // Test moving to the next invalid word.
308 var defaultExecuted = goog.testing.events.fireKeySequence(
309 container, goog.events.KeyCodes.RIGHT, keyEventProperties);
310
311 assertFalse(
312 'The default action should be prevented for the key event',
313 defaultExecuted);
314 assertEquals(
315 'The third/last misspelled word should have focus.',
316 document.activeElement, container.children[2]);
317
318 s.resume();
319 processTimerQueue();
320
321 goog.Timer.callOnce = timerSav;
322 s.dispose();
323 }
324
325
326 function testPlainTextSpellCheckerKeyboardNavigateOpenSuggestions() {
327 var handler = new goog.spell.SpellCheck(localSpellCheckingFunction);
328 var s = new goog.ui.PlainTextSpellChecker(handler);
329 var el = document.getElementById('test6');
330 s.decorate(el);
331 var text = 'unit';
332 el.value = text;
333 var keyEventProperties = {};
334 keyEventProperties.ctrlKey = true;
335 keyEventProperties.shiftKey = false;
336
337 var timerSav = goog.Timer.callOnce;
338 goog.Timer.callOnce = localTimer;
339
340 s.check();
341 processTimerQueue();
342
343 var container = s.overlay_;
344 var suggestionMenu = s.getMenu();
345
346 goog.testing.events.fireKeySequence(
347 container, goog.events.KeyCodes.RIGHT, keyEventProperties);
348
349 assertFalse(
350 'The suggestion menu should not be visible yet.',
351 suggestionMenu.isVisible());
352
353 keyEventProperties.ctrlKey = false;
354 var defaultExecuted = goog.testing.events.fireKeySequence(
355 container, goog.events.KeyCodes.DOWN, keyEventProperties);
356
357 assertFalse(
358 'The default action should be prevented for the key event',
359 defaultExecuted);
360 assertTrue(
361 'The suggestion menu should be visible after the key event.',
362 suggestionMenu.isVisible());
363
364 s.resume();
365 processTimerQueue();
366
367 goog.Timer.callOnce = timerSav;
368 s.dispose();
369 }
370
371
372 function testPlainTextSpellCheckerKeyboardNavigatePrevious() {
373 var handler = new goog.spell.SpellCheck(localSpellCheckingFunction);
374 var s = new goog.ui.PlainTextSpellChecker(handler);
375 var el = document.getElementById('test7');
376 s.decorate(el);
377 var text = 'a unit test for keyboard test';
378 el.value = text;
379 var keyEventProperties = {};
380 keyEventProperties.ctrlKey = true;
381 keyEventProperties.shiftKey = false;
382
383 var timerSav = goog.Timer.callOnce;
384 goog.Timer.callOnce = localTimer;
385
386 s.check();
387 processTimerQueue();
388
389 var container = s.overlay_;
390
391 // Move to the third element, so we can test the move back to the second.
392 goog.testing.events.fireKeySequence(
393 container, goog.events.KeyCodes.RIGHT, keyEventProperties);
394 goog.testing.events.fireKeySequence(
395 container, goog.events.KeyCodes.RIGHT, keyEventProperties);
396 goog.testing.events.fireKeySequence(
397 container, goog.events.KeyCodes.RIGHT, keyEventProperties);
398
399 // Test moving from third to second misspelled word.
400 var defaultExecuted = goog.testing.events.fireKeySequence(
401 container, goog.events.KeyCodes.LEFT, keyEventProperties);
402
403 assertFalse(
404 'The default action should be prevented for the key event',
405 defaultExecuted);
406 assertEquals(
407 'The second misspelled word should have focus.', document.activeElement,
408 container.children[1]);
409
410 s.resume();
411 processTimerQueue();
412
413 goog.Timer.callOnce = timerSav;
414 s.dispose();
415 }
416
417
418 function testPlainTextSpellCheckerKeyboardNavigatePreviousOnFirstWord() {
419 var handler = new goog.spell.SpellCheck(localSpellCheckingFunction);
420 var s = new goog.ui.PlainTextSpellChecker(handler);
421 var el = document.getElementById('test8');
422 s.decorate(el);
423 var text = 'a unit test for keyboard test';
424 el.value = text;
425 var keyEventProperties = {};
426 keyEventProperties.ctrlKey = true;
427 keyEventProperties.shiftKey = false;
428
429 var timerSav = goog.Timer.callOnce;
430 goog.Timer.callOnce = localTimer;
431
432 s.check();
433 processTimerQueue();
434
435 var container = s.overlay_;
436
437 // Move to the first invalid word.
438 goog.testing.events.fireKeySequence(
439 container, goog.events.KeyCodes.RIGHT, keyEventProperties);
440
441 // Test moving to the previous invalid word.
442 var defaultExecuted = goog.testing.events.fireKeySequence(
443 container, goog.events.KeyCodes.LEFT, keyEventProperties);
444
445 assertFalse(
446 'The default action should be prevented for the key event',
447 defaultExecuted);
448 assertEquals(
449 'The first misspelled word should have focus.', document.activeElement,
450 container.children[0]);
451
452 s.resume();
453 processTimerQueue();
454
455 goog.Timer.callOnce = timerSav;
456 s.dispose();
457 }