Debian Codegraph @

closure-library/closure/goog/ui/richtextspellchecker_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.RichTextSpellCheckerTest');
16 goog.setTestOnly('goog.ui.RichTextSpellCheckerTest');
17
18 goog.require('goog.dom.Range');
19 goog.require('goog.dom.TagName');
20 goog.require('goog.dom.classlist');
21 goog.require('goog.events.KeyCodes');
22 goog.require('goog.object');
23 goog.require('goog.spell.SpellCheck');
24 goog.require('goog.testing.MockClock');
25 goog.require('goog.testing.events');
26 goog.require('goog.testing.jsunit');
27 goog.require('goog.ui.RichTextSpellChecker');
28
29 var VOCABULARY = ['test', 'words', 'a', 'few'];
30 var SUGGESTIONS = ['foo', 'bar'];
31 var EXCLUDED_DATA = ['DIV.goog-quote', 'goog-comment', 'SPAN.goog-note'];
32
33
34 /**
35 * Delay in ms needed for the spell check word lookup to finish. Finishing the
36 * lookup also finishes the spell checking.
37 * @see goog.spell.SpellCheck.LOOKUP_DELAY_
38 */
39 var SPELL_CHECK_LOOKUP_DELAY = 100;
40
41 var TEST_TEXT1 = 'this test is longer than a few words now';
42 var TEST_TEXT2 = 'test another simple text with misspelled words';
43 var TEST_TEXT3 = 'test another simple text with misspelled words' +
44 '<b class="goog-quote">test another simple text with misspelled words<u> ' +
45 'test another simple text with misspelled words<del class="goog-quote"> ' +
46 'test another simple text with misspelled words<i>this test is longer ' +
47 'than a few words now</i>test another simple text with misspelled words ' +
48 '<i>this test is longer than a few words now</i></del>test another ' +
49 'simple text with misspelled words<del class="goog-quote">test another ' +
50 'simple text with misspelled words<i>this test is longer than a few ' +
51 'words now</i>test another simple text with misspelled words<i>this test ' +
52 'is longer than a few words now</i></del></u>test another simple text ' +
53 'with misspelled words<u>test another simple text with misspelled words' +
54 '<del class="goog-quote">test another simple text with misspelled words' +
55 '<i> thistest is longer than a few words now</i>test another simple text ' +
56 'with misspelled words<i>this test is longer than a few words ' +
57 'now</i></del>test another simple text with misspelled words' +
58 '<del class="goog-quote">test another simple text with misspelled words' +
59 '<i>this test is longer than a few words now</i>test another simple text ' +
60 'with misspelled words<i>this test is longer than a few words ' +
61 'now</i></del></u></b>';
62
63 var spellChecker;
64 var handler;
65 var mockClock;
66
67 function setUp() {
68 mockClock = new goog.testing.MockClock(true /* install */);
69 handler = new goog.spell.SpellCheck(localSpellCheckingFunction);
70 spellChecker = new goog.ui.RichTextSpellChecker(handler);
71 }
72
73 function tearDown() {
74 spellChecker.dispose();
75 handler.dispose();
76 mockClock.dispose();
77 }
78
79 function waitForSpellCheckToFinish() {
80 mockClock.tick(SPELL_CHECK_LOOKUP_DELAY);
81 }
82
83
84 /**
85 * Function to use for word lookup by the spell check handler. This function is
86 * supplied as a constructor parameter for the spell check handler.
87 * @param {!Array<string>} words Unknown words that need to be looked up.
88 * @param {!goog.spell.SpellCheck} spellChecker The spell check handler.
89 * @param {function(!Array.)} callback The lookup callback
90 * function.
91 */
92 function localSpellCheckingFunction(words, spellChecker, callback) {
93 var len = words.length;
94 var results = [];
95 for (var i = 0; i < len; i++) {
96 var word = words[i];
97 var found = false;
98 for (var j = 0; j < VOCABULARY.length; ++j) {
99 if (VOCABULARY[j] == word) {
100 found = true;
101 break;
102 }
103 }
104 if (found) {
105 results.push([word, goog.spell.SpellCheck.WordStatus.VALID]);
106 } else {
107 results.push(
108 [word, goog.spell.SpellCheck.WordStatus.INVALID, SUGGESTIONS]);
109 }
110 }
111 callback.call(spellChecker, results);
112 }
113
114 function testDocumentIntegrity() {
115 var el = document.getElementById('test1');
116 spellChecker.decorate(el);
117 el.appendChild(document.createTextNode(TEST_TEXT3));
118 var el2 = el.cloneNode(true);
119
120 spellChecker.setExcludeMarker('goog-quote');
121 spellChecker.check();
122 waitForSpellCheckToFinish();
123 spellChecker.ignoreWord('iggnore');
124 waitForSpellCheckToFinish();
125 spellChecker.check();
126 waitForSpellCheckToFinish();
127 spellChecker.resume();
128 waitForSpellCheckToFinish();
129
130 assertEquals(
131 'Spell checker run should not change the underlying element.',
132 el2.innerHTML, el.innerHTML);
133 }
134
135 function testExcludeMarkers() {
136 var el = document.getElementById('test1');
137 spellChecker.decorate(el);
138 spellChecker.setExcludeMarker(
139 ['DIV.goog-quote', 'goog-comment', 'SPAN.goog-note']);
140 assertArrayEquals(
141 ['goog-quote', 'goog-comment', 'goog-note'], spellChecker.excludeMarker);
142 assertArrayEquals(
143 [String(goog.dom.TagName.DIV), undefined, String(goog.dom.TagName.SPAN)],
144 spellChecker.excludeTags);
145 el.innerHTML = '<div class="goog-quote">misspelling</div>' +
146 '<div class="goog-yes">misspelling</div>' +
147 '<div class="goog-note">misspelling</div>' +
148 '<div class="goog-comment">misspelling</div>' +
149 '<span>misspelling<span>';
150
151 spellChecker.check();
152 waitForSpellCheckToFinish();
153 assertEquals(3, spellChecker.getLastIndex());
154 }
155
156 function testBiggerDocument() {
157 var el = document.getElementById('test2');
158 spellChecker.decorate(el);
159 el.appendChild(document.createTextNode(TEST_TEXT3));
160 var el2 = el.cloneNode(true);
161
162 spellChecker.check();
163 waitForSpellCheckToFinish();
164 spellChecker.resume();
165 waitForSpellCheckToFinish();
166
167 assertEquals(
168 'Spell checker run should not change the underlying element.',
169 el2.innerHTML, el.innerHTML);
170 }
171
172 function testElementOverflow() {
173 var el = document.getElementById('test3');
174 spellChecker.decorate(el);
175 el.appendChild(document.createTextNode(TEST_TEXT3));
176
177 var el2 = el.cloneNode(true);
178
179 spellChecker.check();
180 waitForSpellCheckToFinish();
181 spellChecker.check();
182 waitForSpellCheckToFinish();
183 spellChecker.resume();
184 waitForSpellCheckToFinish();
185
186 assertEquals(
187 'Spell checker run should not change the underlying element.',
188 el2.innerHTML, el.innerHTML);
189 }
190
191 function testKeyboardNavigateNext() {
192 var el = document.getElementById('test4');
193 spellChecker.decorate(el);
194 var text = 'a unit test for keyboard test';
195 el.appendChild(document.createTextNode(text));
196 var keyEventProperties =
197 goog.object.create('ctrlKey', true, 'shiftKey', false);
198
199 spellChecker.check();
200 waitForSpellCheckToFinish();
201
202 // First call just moves focus to first misspelled word.
203 goog.testing.events.fireKeySequence(
204 el, goog.events.KeyCodes.RIGHT, keyEventProperties);
205
206 // Test moving from first to second misspelled word.
207 var defaultExecuted = goog.testing.events.fireKeySequence(
208 el, goog.events.KeyCodes.RIGHT, keyEventProperties);
209
210 assertFalse(
211 'The default action should be prevented for the key event',
212 defaultExecuted);
213 assertCursorAtElement(spellChecker.makeElementId(2));
214
215 spellChecker.resume();
216 }
217
218 function testKeyboardNavigateNextOnLastWord() {
219 var el = document.getElementById('test5');
220 spellChecker.decorate(el);
221 var text = 'a unit test for keyboard test';
222 el.appendChild(document.createTextNode(text));
223 var keyEventProperties =
224 goog.object.create('ctrlKey', true, 'shiftKey', false);
225
226 spellChecker.check();
227 waitForSpellCheckToFinish();
228
229 // Move to the last invalid word.
230 goog.testing.events.fireKeySequence(
231 el, goog.events.KeyCodes.RIGHT, keyEventProperties);
232 goog.testing.events.fireKeySequence(
233 el, goog.events.KeyCodes.RIGHT, keyEventProperties);
234 goog.testing.events.fireKeySequence(
235 el, goog.events.KeyCodes.RIGHT, keyEventProperties);
236
237 // Test moving to the next invalid word. Should have no effect.
238 var defaultExecuted = goog.testing.events.fireKeySequence(
239 el, goog.events.KeyCodes.RIGHT, keyEventProperties);
240
241 assertFalse(
242 'The default action should be prevented for the key event',
243 defaultExecuted);
244 assertCursorAtElement(spellChecker.makeElementId(3));
245
246 spellChecker.resume();
247 }
248
249 function testKeyboardNavigateOpenSuggestions() {
250 var el = document.getElementById('test6');
251 spellChecker.decorate(el);
252 var text = 'unit';
253 el.appendChild(document.createTextNode(text));
254 var keyEventProperties =
255 goog.object.create('ctrlKey', true, 'shiftKey', false);
256
257 spellChecker.check();
258 waitForSpellCheckToFinish();
259
260 var suggestionMenu = spellChecker.getMenu();
261
262 goog.testing.events.fireKeySequence(
263 el, goog.events.KeyCodes.RIGHT, keyEventProperties);
264
265 assertFalse(
266 'The suggestion menu should not be visible yet.',
267 suggestionMenu.isVisible());
268
269 keyEventProperties.ctrlKey = false;
270 var defaultExecuted = goog.testing.events.fireKeySequence(
271 el, goog.events.KeyCodes.DOWN, keyEventProperties);
272
273 assertFalse(
274 'The default action should be prevented for the key event',
275 defaultExecuted);
276 assertTrue(
277 'The suggestion menu should be visible after the key event.',
278 suggestionMenu.isVisible());
279
280 spellChecker.resume();
281 }
282
283 function testKeyboardNavigatePrevious() {
284 var el = document.getElementById('test7');
285 spellChecker.decorate(el);
286 var text = 'a unit test for keyboard test';
287 el.appendChild(document.createTextNode(text));
288 var keyEventProperties =
289 goog.object.create('ctrlKey', true, 'shiftKey', false);
290
291 spellChecker.check();
292 waitForSpellCheckToFinish();
293
294 // Move to the third element, so we can test the move back to the second.
295 goog.testing.events.fireKeySequence(
296 el, goog.events.KeyCodes.RIGHT, keyEventProperties);
297 goog.testing.events.fireKeySequence(
298 el, goog.events.KeyCodes.RIGHT, keyEventProperties);
299 goog.testing.events.fireKeySequence(
300 el, goog.events.KeyCodes.RIGHT, keyEventProperties);
301
302 var defaultExecuted = goog.testing.events.fireKeySequence(
303 el, goog.events.KeyCodes.LEFT, keyEventProperties);
304
305 assertFalse(
306 'The default action should be prevented for the key event',
307 defaultExecuted);
308 assertCursorAtElement(spellChecker.makeElementId(2));
309
310 spellChecker.resume();
311 }
312
313 function testKeyboardNavigatePreviousOnLastWord() {
314 var el = document.getElementById('test8');
315 spellChecker.decorate(el);
316 var text = 'a unit test for keyboard test';
317 el.appendChild(document.createTextNode(text));
318 var keyEventProperties =
319 goog.object.create('ctrlKey', true, 'shiftKey', false);
320
321 spellChecker.check();
322 waitForSpellCheckToFinish();
323
324 // Move to the first invalid word.
325 goog.testing.events.fireKeySequence(
326 el, goog.events.KeyCodes.RIGHT, keyEventProperties);
327
328 // Test moving to the previous invalid word. Should have no effect.
329 var defaultExecuted = goog.testing.events.fireKeySequence(
330 el, goog.events.KeyCodes.LEFT, keyEventProperties);
331
332 assertFalse(
333 'The default action should be prevented for the key event',
334 defaultExecuted);
335 assertCursorAtElement(spellChecker.makeElementId(1));
336
337 spellChecker.resume();
338 }
339
340 function assertCursorAtElement(expectedId) {
341 var range = goog.dom.Range.createFromWindow();
342
343 if (isCaret(range)) {
344 if (isMisspelledWordElement(range.getStartNode())) {
345 var focusedElementId = range.getStartNode().id;
346 }
347
348 // In Chrome a cursor at the start of a misspelled word will appear to be at
349 // the end of the text node preceding it.
350 if (isCursorAtEndOfStartNode(range) &&
351 range.getStartNode().nextSibling != null &&
352 isMisspelledWordElement(range.getStartNode().nextSibling)) {
353 var focusedElementId = range.getStartNode().nextSibling.id;
354 }
355 }
356
357 assertEquals(
358 'The cursor is not at the expected misspelled word.', expectedId,
359 focusedElementId);
360 }
361
362 function isCaret(range) {
363 return range.getStartNode() == range.getEndNode();
364 }
365
366 function isMisspelledWordElement(element) {
367 return goog.dom.classlist.contains(element, 'goog-spellcheck-word');
368 }
369
370 function isCursorAtEndOfStartNode(range) {
371 return range.getStartNode().length == range.getStartOffset();
372 }