blob: ac50f0fe99020e96958066da75a5501d44d4da97 [file] [log] [blame]
Nils Diewald19ccee92014-12-08 11:30:08 +00001function emitKeyboardEvent (element, type, keyCode) {
2 // event type : keydown, keyup, keypress
3 // http://stackoverflow.com/questions/596481/simulate-javascript-key-events
4 // http://stackoverflow.com/questions/961532/firing-a-keyboard-event-in-javascript
5 var keyboardEvent = document.createEvent("KeyboardEvent");
6 var initMethod = typeof keyboardEvent.initKeyboardEvent !== 'undefined' ?
7 "initKeyboardEvent" : "initKeyEvent";
Nils Diewald19ccee92014-12-08 11:30:08 +00008 keyboardEvent[initMethod](
9 type,
10 true, // bubbles
11 true, // cancelable
12 window, // viewArg: should be window
13 false, // ctrlKeyArg
14 false, // altKeyArg
15 false, // shiftKeyArg
16 false, // metaKeyArg
17 keyCode, // keyCodeArg : unsigned long the virtual key code, else 0
18 0 // charCodeArgs : unsigned long the Unicode character
19 // associated with the depressed key, else 0
20 );
Nils Diewald19ccee92014-12-08 11:30:08 +000021 element.dispatchEvent(keyboardEvent);
22};
23
24
Nils Diewald7c8ced22015-04-15 19:21:00 +000025define(['hint'], function () {
Nils Diewald5c5a7472015-04-02 22:13:38 +000026
Nils Diewald7c8ced22015-04-15 19:21:00 +000027 var hintClass = require("hint");
28 var inputClass = require("hint/input");
29 var contextClass = require("hint/contextanalyzer");
30 var menuClass = require("hint/menu");
31 var menuItemClass = require("hint/item");
Nils Diewald5c5a7472015-04-02 22:13:38 +000032
Nils Diewald7c8ced22015-04-15 19:21:00 +000033 describe('KorAP.InputField', function () {
34 var input;
Nils Diewald1c546922015-04-13 01:56:19 +000035
Nils Diewald7c8ced22015-04-15 19:21:00 +000036 beforeEach(function () {
37 input = document.createElement("input");
38 input.setAttribute('type', "text");
39 input.setAttribute("value", "abcdefghijklmno");
40 input.style.position = 'absolute';
41 document.getElementsByTagName('body')[0].appendChild(input);
42 input.style.top = "20px";
43 input.style.left = "30px";
44 input.focus();
45 input.selectionStart = 5;
Nils Diewald5c5a7472015-04-02 22:13:38 +000046 });
47
Nils Diewald7c8ced22015-04-15 19:21:00 +000048 afterEach(function () {
49 document.getElementsByTagName("body")[0].removeChild(
50 input
51 );
52 });
Nils Diewald5c5a7472015-04-02 22:13:38 +000053
Nils Diewald7c8ced22015-04-15 19:21:00 +000054 afterAll(function () {
55 try {
56 document.getElementsByTagName("body")[0].removeChild(
57 document.getElementById("searchMirror")
58 );
59 }
60 catch (e) {};
61 });
Nils Diewald19ccee92014-12-08 11:30:08 +000062
Nils Diewald7c8ced22015-04-15 19:21:00 +000063 it('should be initializable', function () {
64 // Supports: context, searchField
65 var inputField = inputClass.create(input);
66 expect(inputField._element).not.toBe(undefined);
67 });
Nils Diewald19ccee92014-12-08 11:30:08 +000068
Nils Diewald7c8ced22015-04-15 19:21:00 +000069 it('should have text', function () {
70 expect(input.value).toEqual('abcdefghijklmno');
71 var inputField = inputClass.create(input);
Nils Diewald19ccee92014-12-08 11:30:08 +000072
Nils Diewald7c8ced22015-04-15 19:21:00 +000073 expect(inputField.value()).toEqual("abcdefghijklmno");
Nils Diewald19ccee92014-12-08 11:30:08 +000074
Nils Diewald7c8ced22015-04-15 19:21:00 +000075 expect(input.selectionStart).toEqual(5);
76 expect(inputField.element().selectionStart).toEqual(5);
Nils Diewald7148c6f2015-05-04 15:07:53 +000077 expect(inputField._split()[0]).toEqual("abcde");
78 expect(inputField._split()[1]).toEqual("fghijklmno");
Nils Diewald19ccee92014-12-08 11:30:08 +000079
Nils Diewald7c8ced22015-04-15 19:21:00 +000080 inputField.insert("xyz");
81 expect(inputField.value()).toEqual('abcdexyzfghijklmno');
Nils Diewald7148c6f2015-05-04 15:07:53 +000082 expect(inputField._split()[0]).toEqual("abcdexyz");
83 expect(inputField._split()[1]).toEqual("fghijklmno");
Nils Diewald7c8ced22015-04-15 19:21:00 +000084 });
Nils Diewald19ccee92014-12-08 11:30:08 +000085
Nils Diewald7c8ced22015-04-15 19:21:00 +000086 it('should be correctly positioned', function () {
87 expect(input.value).toEqual('abcdefghijklmno');
88 var inputField = inputClass.create(input);
89 document.getElementsByTagName("body")[0].appendChild(input);
90 inputField.reposition();
91 expect(input.style.left).toEqual("30px");
92 expect(inputField.mirror().style.left.match(/^(\d+)px$/)[1]).toBeGreaterThan(29);
93 expect(inputField.mirror().style.top.match(/^(\d+)px$/)[1]).toBeGreaterThan(20);
94 });
Nils Diewald19ccee92014-12-08 11:30:08 +000095
Nils Diewald7c8ced22015-04-15 19:21:00 +000096 it('should have a correct context', function () {
97 expect(input.value).toEqual('abcdefghijklmno');
98 var inputField = inputClass.create(input);
99 expect(inputField.value()).toEqual("abcdefghijklmno");
100 expect(inputField.element().selectionStart).toEqual(5);
Nils Diewald7148c6f2015-05-04 15:07:53 +0000101 expect(inputField._split()[0]).toEqual("abcde");
Nils Diewald7c8ced22015-04-15 19:21:00 +0000102 expect(inputField.context()).toEqual("abcde");
103 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000104
Nils Diewald7c8ced22015-04-15 19:21:00 +0000105 /*
106 it('should be correctly triggerable', function () {
107 // https://developer.mozilla.org/samples/domref/dispatchEvent.html
108 var hint = KorAP.Hint.create({ "inputField" : input });
109 emitKeyboardEvent(hint.inputField.element, "keypress", 20);
110 });
111 */
Nils Diewald19ccee92014-12-08 11:30:08 +0000112 });
113
Nils Diewald1c546922015-04-13 01:56:19 +0000114
Nils Diewald7c8ced22015-04-15 19:21:00 +0000115 describe('KorAP.ContextAnalyzer', function () {
116 it('should be initializable', function () {
117 var analyzer = contextClass.create(")");
118 expect(analyzer).toBe(undefined);
119 analyzer = contextClass.create(".+?");
120 expect(analyzer).not.toBe(undefined);
121 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000122
Nils Diewald7c8ced22015-04-15 19:21:00 +0000123 it('should check correctly', function () {
124 analyzer = contextClass.create(KorAP.context);
125 expect(analyzer.test("cnx/]cnx/c=")).toEqual("cnx/c=");
126 expect(analyzer.test("cnx/c=")).toEqual("cnx/c=");
127 expect(analyzer.test("cnx/c=np mate/m=mood:")).toEqual("mate/m=mood:");
128 expect(analyzer.test("impcnx/")).toEqual("impcnx/");
129 expect(analyzer.test("cnx/c=npcnx/")).toEqual("npcnx/");
130 expect(analyzer.test("mate/m=degree:pos corenlp/ne_dewac_175m_600="))
131 .toEqual("corenlp/ne_dewac_175m_600=");
132 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000133 });
134
Nils Diewald19ccee92014-12-08 11:30:08 +0000135
Nils Diewald7c8ced22015-04-15 19:21:00 +0000136 describe('KorAP.Hint', function () {
137 KorAP.hintArray = {
138 "corenlp/" : [
139 ["Named Entity", "ne=" , "Combined"],
140 ["Named Entity", "ne_dewac_175m_600=" , "ne_dewac_175m_600"],
141 ["Named Entity", "ne_hgc_175m_600=", "ne_hgc_175m_600"]
142 ]
143 };
Nils Diewald19ccee92014-12-08 11:30:08 +0000144
Nils Diewald7c8ced22015-04-15 19:21:00 +0000145 beforeEach(function () {
146 input = document.createElement("input");
147 input.setAttribute("type", "text");
148 input.setAttribute("value", "abcdefghijklmno");
149 input.style.position = 'absolute';
150 input.style.top = "20px";
151 input.style.left = "30px";
152 input.focus();
153 input.selectionStart = 5;
154 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000155
Nils Diewald7c8ced22015-04-15 19:21:00 +0000156 it('should be initializable', function () {
157 // Supports: context, searchField
158 var hint = hintClass.create({
159 inputField : input
160 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000161
Nils Diewald7c8ced22015-04-15 19:21:00 +0000162 expect(hint).toBeTruthy();
163 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000164 });
165
Nils Diewald7c8ced22015-04-15 19:21:00 +0000166 describe('KorAP.HintMenuItem', function () {
167 it('should be initializable', function () {
168 expect(
169 function() { menuItemClass.create([]) }
170 ).toThrow(new Error("Missing parameters"));
Nils Diewald19ccee92014-12-08 11:30:08 +0000171
Nils Diewald7c8ced22015-04-15 19:21:00 +0000172 expect(
173 function() { menuItemClass.create(['CoreNLP']) }
174 ).toThrow(new Error("Missing parameters"));
Nils Diewald19ccee92014-12-08 11:30:08 +0000175
Nils Diewald7c8ced22015-04-15 19:21:00 +0000176 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
177 expect(menuItem.name()).toEqual('CoreNLP');
178 expect(menuItem.action()).toEqual('corenlp/');
179 expect(menuItem.desc()).toBeUndefined();
Nils Diewald19ccee92014-12-08 11:30:08 +0000180
Nils Diewald7c8ced22015-04-15 19:21:00 +0000181 menuItem = menuItemClass.create(
182 ['CoreNLP', 'corenlp/', 'It\'s funny']
183 );
184 expect(menuItem.name()).toEqual('CoreNLP');
185 expect(menuItem.action()).toEqual('corenlp/');
186 expect(menuItem.desc()).not.toBeUndefined();
187 expect(menuItem.desc()).toEqual('It\'s funny');
188 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000189
Nils Diewald7c8ced22015-04-15 19:21:00 +0000190 it('should have an element', function () {
191 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
192 expect(menuItem.element()).not.toBe(undefined);
193 expect(menuItem.element().nodeName).toEqual("LI");
Nils Diewald19ccee92014-12-08 11:30:08 +0000194
Nils Diewald7c8ced22015-04-15 19:21:00 +0000195 var title = menuItem.element().firstChild;
196 expect(title.nodeName).toEqual("SPAN");
197 expect(title.firstChild.nodeType).toEqual(3);
198 expect(title.firstChild.nodeValue).toEqual("CoreNLP");
199 expect(menuItem.element().childNodes[0]).not.toBe(undefined);
200 expect(menuItem.element().childNodes[1]).toBe(undefined);
Nils Diewald19ccee92014-12-08 11:30:08 +0000201
Nils Diewald7c8ced22015-04-15 19:21:00 +0000202 menuItem = menuItemClass.create(
203 ['CoreNLP', 'corenlp/', 'my DescRiption']
204 );
205 expect(menuItem.element()).not.toBe(undefined);
206 expect(menuItem.element().nodeName).toEqual("LI");
Nils Diewald19ccee92014-12-08 11:30:08 +0000207
Nils Diewald7c8ced22015-04-15 19:21:00 +0000208 title = menuItem.element().firstChild;
209 expect(title.nodeName).toEqual("SPAN");
210 expect(title.firstChild.nodeType).toEqual(3); // TextNode
211 expect(title.firstChild.nodeValue).toEqual("CoreNLP");
Nils Diewald19ccee92014-12-08 11:30:08 +0000212
Nils Diewald7c8ced22015-04-15 19:21:00 +0000213 expect(menuItem.element().childNodes[0]).not.toBe(undefined);
214 expect(menuItem.element().childNodes[1]).not.toBe(undefined);
Nils Diewald19ccee92014-12-08 11:30:08 +0000215
Nils Diewald7c8ced22015-04-15 19:21:00 +0000216 var desc = menuItem.element().lastChild;
217 expect(desc.nodeName).toEqual("SPAN");
218 expect(desc.firstChild.nodeType).toEqual(3); // TextNode
219 expect(desc.firstChild.nodeValue).toEqual("my DescRiption");
220 });
Nils Diewald1c546922015-04-13 01:56:19 +0000221
Nils Diewald19ccee92014-12-08 11:30:08 +0000222
Nils Diewald7c8ced22015-04-15 19:21:00 +0000223 it('should be activatable and deactivateable by class', function () {
224 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
225 expect(menuItem.active()).toBe(false);
226 expect(menuItem.element().getAttribute("class")).toBe(null);
227 menuItem.active(true);
228 expect(menuItem.active()).toBe(true);
229 expect(menuItem.element().getAttribute("class")).toEqual("active");
230 menuItem.active(false); // Is active
231 expect(menuItem.active()).toBe(false);
232 expect(menuItem.element().getAttribute("class")).toEqual("");
233 menuItem.active(true);
234 expect(menuItem.active()).toBe(true);
235 expect(menuItem.element().getAttribute("class")).toEqual("active");
Nils Diewald19ccee92014-12-08 11:30:08 +0000236
Nils Diewald7c8ced22015-04-15 19:21:00 +0000237 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
238 expect(menuItem.active()).toBe(false);
239 expect(menuItem.element().getAttribute("class")).toBe(null);
240 menuItem.active(false); // Is not active
241 expect(menuItem.active()).toBe(false);
242 expect(menuItem.element().getAttribute("class")).toBe(null);
243 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000244
Nils Diewald7c8ced22015-04-15 19:21:00 +0000245 it('should be set to boundary', function () {
246 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
247 expect(menuItem.active()).toBe(false);
248 expect(menuItem.element().getAttribute("class")).toBe(null);
Nils Diewald19ccee92014-12-08 11:30:08 +0000249
Nils Diewald7c8ced22015-04-15 19:21:00 +0000250 // Set active
251 menuItem.active(true);
252 expect(menuItem.active()).toBe(true);
253 expect(menuItem.noMore()).toBe(false);
254 expect(menuItem.element().getAttribute("class")).toEqual("active");
Nils Diewald19ccee92014-12-08 11:30:08 +0000255
Nils Diewald7c8ced22015-04-15 19:21:00 +0000256 // Set no more
257 menuItem.noMore(true);
258 expect(menuItem.active()).toBe(true);
259 expect(menuItem.noMore()).toBe(true);
260 expect(menuItem.element().getAttribute("class")).toEqual("active no-more");
Nils Diewald1c546922015-04-13 01:56:19 +0000261
Nils Diewald7c8ced22015-04-15 19:21:00 +0000262 // No no more
263 menuItem.noMore(false);
264 expect(menuItem.active()).toBe(true);
265 expect(menuItem.noMore()).toBe(false);
266 expect(menuItem.element().getAttribute("class")).toEqual("active");
Nils Diewald1c546922015-04-13 01:56:19 +0000267
Nils Diewald7c8ced22015-04-15 19:21:00 +0000268 // Set no more, deactivate
269 menuItem.noMore(true);
270 menuItem.active(false);
271 expect(menuItem.active()).toBe(false);
272 expect(menuItem.noMore()).toBe(true);
273 expect(menuItem.element().getAttribute("class")).toEqual("no-more");
Nils Diewald19ccee92014-12-08 11:30:08 +0000274
Nils Diewald7c8ced22015-04-15 19:21:00 +0000275 // Set active
276 menuItem.active(true);
277 expect(menuItem.active()).toBe(true);
278 expect(menuItem.noMore()).toBe(true);
279 expect(menuItem.element().getAttribute("class")).toEqual("no-more active");
280 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000281
Nils Diewald7c8ced22015-04-15 19:21:00 +0000282 it('should be highlightable', function () {
283 // Highlight in the middle
284 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
285 menuItem.highlight("ren");
286 expect(menuItem.element().innerHTML).toEqual("<span>Co<mark>reN</mark>LP</span>");
Nils Diewald19ccee92014-12-08 11:30:08 +0000287
Nils Diewald7c8ced22015-04-15 19:21:00 +0000288 menuItem.lowlight();
289 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
Nils Diewald19ccee92014-12-08 11:30:08 +0000290
Nils Diewald7c8ced22015-04-15 19:21:00 +0000291 // Starting highlight
292 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
293 menuItem.highlight("cor");
294 expect(menuItem.element().innerHTML).toEqual("<span><mark>Cor</mark>eNLP</span>");
Nils Diewald19ccee92014-12-08 11:30:08 +0000295
Nils Diewald7c8ced22015-04-15 19:21:00 +0000296 menuItem.lowlight();
297 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
298
299 // Starting highlight - short
300 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
301 menuItem.highlight("c");
302 expect(menuItem.element().innerHTML).toEqual("<span><mark>C</mark>oreNLP</span>");
303
304 menuItem.lowlight();
305 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
306
307 // Highlight at the end
308 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
309 menuItem.highlight("nlp");
310 expect(menuItem.element().innerHTML).toEqual("<span>Core<mark>NLP</mark></span>");
311
312 menuItem.lowlight();
313 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
314
315 // Highlight at the end - short
316 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
317 menuItem.highlight("p");
318 expect(menuItem.element().innerHTML).toEqual("<span>CoreNL<mark>P</mark></span>");
319
320 menuItem.lowlight();
321 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
322
323 // No highlight
324 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
325 menuItem.highlight("xp");
326 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
327
328 menuItem.lowlight();
329 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
330
331 // Highlight in the middle - first
332 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
333
334 menuItem.highlight("ren");
335 expect(menuItem.element().innerHTML).toEqual("<span>Co<mark>reN</mark>LP</span><span class=\"desc\">This is my Example</span>");
336
337 menuItem.lowlight();
338 expect(menuItem.element().innerHTML).toEqual('<span>CoreNLP</span><span class="desc">This is my Example</span>');
339
340 // Highlight in the middle - second
341 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
342 menuItem.highlight("ampl");
343 expect(menuItem.element().innerHTML).toEqual('<span>CoreNLP</span><span class="desc">This is my Ex<mark>ampl</mark>e</span>');
344
345 menuItem.lowlight();
346 expect(menuItem.element().innerHTML).toEqual('<span>CoreNLP</span><span class="desc">This is my Example</span>');
347
348 // Highlight in the middle - both
349 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
350
351 menuItem.highlight("e");
352 expect(menuItem.element().innerHTML).toEqual('<span>Cor<mark>e</mark>NLP</span><span class="desc">This is my <mark>E</mark>xampl<mark>e</mark></span>');
353
354 menuItem.lowlight();
355 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\">This is my Example</span>");
356
357 // Highlight in the end - second
358 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
359 menuItem.highlight("le");
360 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\">This is my Examp<mark>le</mark></span>");
361
362 menuItem.lowlight();
363 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\">This is my Example</span>");
364
365 // Highlight at the beginning - second
366 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
367 menuItem.highlight("this");
368 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\"><mark>This</mark> is my Example</span>");
369
370 menuItem.lowlight();
371 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\">This is my Example</span>");
372 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000373 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000374
Nils Diewald7c8ced22015-04-15 19:21:00 +0000375 describe('KorAP.HintMenu', function () {
Nils Diewald19ccee92014-12-08 11:30:08 +0000376
Nils Diewald7c8ced22015-04-15 19:21:00 +0000377 var list = [
378 ["Constituency", "c=", "Example 1"],
379 ["Lemma", "l="],
380 ["Morphology", "m=", "Example 2"],
381 ["Part-of-Speech", "p="],
382 ["Syntax", "syn="]
383 ];
Nils Diewald19ccee92014-12-08 11:30:08 +0000384
Nils Diewald7c8ced22015-04-15 19:21:00 +0000385 it('should be initializable', function () {
Nils Diewald19ccee92014-12-08 11:30:08 +0000386
Nils Diewald7c8ced22015-04-15 19:21:00 +0000387 var menu = menuClass.create(null, "cnx/", list);
Nils Diewald7c8ced22015-04-15 19:21:00 +0000388 expect(menu.element().nodeName).toEqual('UL');
389 expect(menu.element().style.opacity).toEqual("0");
Nils Diewald19ccee92014-12-08 11:30:08 +0000390
Nils Diewald7c8ced22015-04-15 19:21:00 +0000391 menu.limit(8);
Nils Diewald19ccee92014-12-08 11:30:08 +0000392
Nils Diewald7c8ced22015-04-15 19:21:00 +0000393 // view
394 menu.show();
Nils Diewald19ccee92014-12-08 11:30:08 +0000395
Nils Diewald7c8ced22015-04-15 19:21:00 +0000396 // First element in list
397 expect(menu.item(0).active()).toBe(true);
398 expect(menu.item(0).noMore()).toBe(true);
399
400 // Middle element in list
401 expect(menu.item(2).active()).toBe(false);
402 expect(menu.item(2).noMore()).toBe(false);
Nils Diewald19ccee92014-12-08 11:30:08 +0000403
Nils Diewald7c8ced22015-04-15 19:21:00 +0000404 // Last element in list
405 expect(menu.item(menu.length() - 1).active()).toBe(false);
406 expect(menu.item(menu.length() - 1).noMore()).toBe(true);
407 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000408 });
409});