File: richtextspellchecker_test.js

package info (click to toggle)
aseba-plugin-blockly 20180211%2Bgit-2
  • links: PTS
  • area: non-free
  • in suites: buster
  • size: 64,472 kB
  • sloc: xml: 7,976; python: 2,314; sh: 261; lisp: 24; makefile: 10
file content (372 lines) | stat: -rw-r--r-- 12,625 bytes parent folder | download | duplicates (2)
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
361
362
363
364
365
366
367
368
369
370
371
372
// Copyright 2007 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

goog.provide('goog.ui.RichTextSpellCheckerTest');
goog.setTestOnly('goog.ui.RichTextSpellCheckerTest');

goog.require('goog.dom.Range');
goog.require('goog.dom.TagName');
goog.require('goog.dom.classlist');
goog.require('goog.events.KeyCodes');
goog.require('goog.object');
goog.require('goog.spell.SpellCheck');
goog.require('goog.testing.MockClock');
goog.require('goog.testing.events');
goog.require('goog.testing.jsunit');
goog.require('goog.ui.RichTextSpellChecker');

var VOCABULARY = ['test', 'words', 'a', 'few'];
var SUGGESTIONS = ['foo', 'bar'];
var EXCLUDED_DATA = ['DIV.goog-quote', 'goog-comment', 'SPAN.goog-note'];


/**
 * Delay in ms needed for the spell check word lookup to finish. Finishing the
 * lookup also finishes the spell checking.
 * @see goog.spell.SpellCheck.LOOKUP_DELAY_
 */
var SPELL_CHECK_LOOKUP_DELAY = 100;

var TEST_TEXT1 = 'this test is longer than a few words now';
var TEST_TEXT2 = 'test another simple text with misspelled words';
var TEST_TEXT3 = 'test another simple text with misspelled words' +
    '<b class="goog-quote">test another simple text with misspelled words<u> ' +
    'test another simple text with misspelled words<del class="goog-quote"> ' +
    'test another simple text with misspelled words<i>this test is longer ' +
    'than a few words now</i>test another simple text with misspelled words ' +
    '<i>this test is longer than a few words now</i></del>test another ' +
    'simple text with misspelled words<del class="goog-quote">test another ' +
    'simple text with misspelled words<i>this test is longer than a few ' +
    'words now</i>test another simple text with misspelled words<i>this test ' +
    'is longer than a few words now</i></del></u>test another simple text ' +
    'with misspelled words<u>test another simple text with misspelled words' +
    '<del class="goog-quote">test another simple text with misspelled words' +
    '<i> thistest is longer than a few words now</i>test another simple text ' +
    'with misspelled words<i>this test is longer than a few words ' +
    'now</i></del>test another simple text with misspelled words' +
    '<del class="goog-quote">test another simple text with misspelled words' +
    '<i>this test is longer than a few words now</i>test another simple text ' +
    'with misspelled words<i>this test is longer than a few words ' +
    'now</i></del></u></b>';

var spellChecker;
var handler;
var mockClock;

function setUp() {
  mockClock = new goog.testing.MockClock(true /* install */);
  handler = new goog.spell.SpellCheck(localSpellCheckingFunction);
  spellChecker = new goog.ui.RichTextSpellChecker(handler);
}

function tearDown() {
  spellChecker.dispose();
  handler.dispose();
  mockClock.dispose();
}

function waitForSpellCheckToFinish() {
  mockClock.tick(SPELL_CHECK_LOOKUP_DELAY);
}


/**
 * Function to use for word lookup by the spell check handler. This function is
 * supplied as a constructor parameter for the spell check handler.
 * @param {!Array<string>} words Unknown words that need to be looked up.
 * @param {!goog.spell.SpellCheck} spellChecker The spell check handler.
 * @param {function(!Array.)} callback The lookup callback
 *     function.
 */
function localSpellCheckingFunction(words, spellChecker, callback) {
  var len = words.length;
  var results = [];
  for (var i = 0; i < len; i++) {
    var word = words[i];
    var found = false;
    for (var j = 0; j < VOCABULARY.length; ++j) {
      if (VOCABULARY[j] == word) {
        found = true;
        break;
      }
    }
    if (found) {
      results.push([word, goog.spell.SpellCheck.WordStatus.VALID]);
    } else {
      results.push(
          [word, goog.spell.SpellCheck.WordStatus.INVALID, SUGGESTIONS]);
    }
  }
  callback.call(spellChecker, results);
}

function testDocumentIntegrity() {
  var el = document.getElementById('test1');
  spellChecker.decorate(el);
  el.appendChild(document.createTextNode(TEST_TEXT3));
  var el2 = el.cloneNode(true);

  spellChecker.setExcludeMarker('goog-quote');
  spellChecker.check();
  waitForSpellCheckToFinish();
  spellChecker.ignoreWord('iggnore');
  waitForSpellCheckToFinish();
  spellChecker.check();
  waitForSpellCheckToFinish();
  spellChecker.resume();
  waitForSpellCheckToFinish();

  assertEquals(
      'Spell checker run should not change the underlying element.',
      el2.innerHTML, el.innerHTML);
}

function testExcludeMarkers() {
  var el = document.getElementById('test1');
  spellChecker.decorate(el);
  spellChecker.setExcludeMarker(
      ['DIV.goog-quote', 'goog-comment', 'SPAN.goog-note']);
  assertArrayEquals(
      ['goog-quote', 'goog-comment', 'goog-note'], spellChecker.excludeMarker);
  assertArrayEquals(
      [String(goog.dom.TagName.DIV), undefined, String(goog.dom.TagName.SPAN)],
      spellChecker.excludeTags);
  el.innerHTML = '<div class="goog-quote">misspelling</div>' +
      '<div class="goog-yes">misspelling</div>' +
      '<div class="goog-note">misspelling</div>' +
      '<div class="goog-comment">misspelling</div>' +
      '<span>misspelling<span>';

  spellChecker.check();
  waitForSpellCheckToFinish();
  assertEquals(3, spellChecker.getLastIndex());
}

function testBiggerDocument() {
  var el = document.getElementById('test2');
  spellChecker.decorate(el);
  el.appendChild(document.createTextNode(TEST_TEXT3));
  var el2 = el.cloneNode(true);

  spellChecker.check();
  waitForSpellCheckToFinish();
  spellChecker.resume();
  waitForSpellCheckToFinish();

  assertEquals(
      'Spell checker run should not change the underlying element.',
      el2.innerHTML, el.innerHTML);
}

function testElementOverflow() {
  var el = document.getElementById('test3');
  spellChecker.decorate(el);
  el.appendChild(document.createTextNode(TEST_TEXT3));

  var el2 = el.cloneNode(true);

  spellChecker.check();
  waitForSpellCheckToFinish();
  spellChecker.check();
  waitForSpellCheckToFinish();
  spellChecker.resume();
  waitForSpellCheckToFinish();

  assertEquals(
      'Spell checker run should not change the underlying element.',
      el2.innerHTML, el.innerHTML);
}

function testKeyboardNavigateNext() {
  var el = document.getElementById('test4');
  spellChecker.decorate(el);
  var text = 'a unit test for keyboard test';
  el.appendChild(document.createTextNode(text));
  var keyEventProperties =
      goog.object.create('ctrlKey', true, 'shiftKey', false);

  spellChecker.check();
  waitForSpellCheckToFinish();

  // First call just moves focus to first misspelled word.
  goog.testing.events.fireKeySequence(
      el, goog.events.KeyCodes.RIGHT, keyEventProperties);

  // Test moving from first to second misspelled word.
  var defaultExecuted = goog.testing.events.fireKeySequence(
      el, goog.events.KeyCodes.RIGHT, keyEventProperties);

  assertFalse(
      'The default action should be prevented for the key event',
      defaultExecuted);
  assertCursorAtElement(spellChecker.makeElementId(2));

  spellChecker.resume();
}

function testKeyboardNavigateNextOnLastWord() {
  var el = document.getElementById('test5');
  spellChecker.decorate(el);
  var text = 'a unit test for keyboard test';
  el.appendChild(document.createTextNode(text));
  var keyEventProperties =
      goog.object.create('ctrlKey', true, 'shiftKey', false);

  spellChecker.check();
  waitForSpellCheckToFinish();

  // Move to the last invalid word.
  goog.testing.events.fireKeySequence(
      el, goog.events.KeyCodes.RIGHT, keyEventProperties);
  goog.testing.events.fireKeySequence(
      el, goog.events.KeyCodes.RIGHT, keyEventProperties);
  goog.testing.events.fireKeySequence(
      el, goog.events.KeyCodes.RIGHT, keyEventProperties);

  // Test moving to the next invalid word. Should have no effect.
  var defaultExecuted = goog.testing.events.fireKeySequence(
      el, goog.events.KeyCodes.RIGHT, keyEventProperties);

  assertFalse(
      'The default action should be prevented for the key event',
      defaultExecuted);
  assertCursorAtElement(spellChecker.makeElementId(3));

  spellChecker.resume();
}

function testKeyboardNavigateOpenSuggestions() {
  var el = document.getElementById('test6');
  spellChecker.decorate(el);
  var text = 'unit';
  el.appendChild(document.createTextNode(text));
  var keyEventProperties =
      goog.object.create('ctrlKey', true, 'shiftKey', false);

  spellChecker.check();
  waitForSpellCheckToFinish();

  var suggestionMenu = spellChecker.getMenu();

  goog.testing.events.fireKeySequence(
      el, goog.events.KeyCodes.RIGHT, keyEventProperties);

  assertFalse(
      'The suggestion menu should not be visible yet.',
      suggestionMenu.isVisible());

  keyEventProperties.ctrlKey = false;
  var defaultExecuted = goog.testing.events.fireKeySequence(
      el, goog.events.KeyCodes.DOWN, keyEventProperties);

  assertFalse(
      'The default action should be prevented for the key event',
      defaultExecuted);
  assertTrue(
      'The suggestion menu should be visible after the key event.',
      suggestionMenu.isVisible());

  spellChecker.resume();
}

function testKeyboardNavigatePrevious() {
  var el = document.getElementById('test7');
  spellChecker.decorate(el);
  var text = 'a unit test for keyboard test';
  el.appendChild(document.createTextNode(text));
  var keyEventProperties =
      goog.object.create('ctrlKey', true, 'shiftKey', false);

  spellChecker.check();
  waitForSpellCheckToFinish();

  // Move to the third element, so we can test the move back to the second.
  goog.testing.events.fireKeySequence(
      el, goog.events.KeyCodes.RIGHT, keyEventProperties);
  goog.testing.events.fireKeySequence(
      el, goog.events.KeyCodes.RIGHT, keyEventProperties);
  goog.testing.events.fireKeySequence(
      el, goog.events.KeyCodes.RIGHT, keyEventProperties);

  var defaultExecuted = goog.testing.events.fireKeySequence(
      el, goog.events.KeyCodes.LEFT, keyEventProperties);

  assertFalse(
      'The default action should be prevented for the key event',
      defaultExecuted);
  assertCursorAtElement(spellChecker.makeElementId(2));

  spellChecker.resume();
}

function testKeyboardNavigatePreviousOnLastWord() {
  var el = document.getElementById('test8');
  spellChecker.decorate(el);
  var text = 'a unit test for keyboard test';
  el.appendChild(document.createTextNode(text));
  var keyEventProperties =
      goog.object.create('ctrlKey', true, 'shiftKey', false);

  spellChecker.check();
  waitForSpellCheckToFinish();

  // Move to the first invalid word.
  goog.testing.events.fireKeySequence(
      el, goog.events.KeyCodes.RIGHT, keyEventProperties);

  // Test moving to the previous invalid word. Should have no effect.
  var defaultExecuted = goog.testing.events.fireKeySequence(
      el, goog.events.KeyCodes.LEFT, keyEventProperties);

  assertFalse(
      'The default action should be prevented for the key event',
      defaultExecuted);
  assertCursorAtElement(spellChecker.makeElementId(1));

  spellChecker.resume();
}

function assertCursorAtElement(expectedId) {
  var range = goog.dom.Range.createFromWindow();

  if (isCaret(range)) {
    if (isMisspelledWordElement(range.getStartNode())) {
      var focusedElementId = range.getStartNode().id;
    }

    // In Chrome a cursor at the start of a misspelled word will appear to be at
    // the end of the text node preceding it.
    if (isCursorAtEndOfStartNode(range) &&
        range.getStartNode().nextSibling != null &&
        isMisspelledWordElement(range.getStartNode().nextSibling)) {
      var focusedElementId = range.getStartNode().nextSibling.id;
    }
  }

  assertEquals(
      'The cursor is not at the expected misspelled word.', expectedId,
      focusedElementId);
}

function isCaret(range) {
  return range.getStartNode() == range.getEndNode();
}

function isMisspelledWordElement(element) {
  return goog.dom.classlist.contains(element, 'goog-spellcheck-word');
}

function isCursorAtEndOfStartNode(range) {
  return range.getStartNode().length == range.getStartOffset();
}