blob: 36d6f649a132b9436d4d4f4754e5575eaf67ebf7 [file] [log] [blame]
Akron45308ce2020-08-28 14:10:23 +02001define(['plugin/server','plugin/widget','panel', 'panel/query', 'panel/result', 'plugin/service', 'pipe', 'vc','util'], function (pluginServerClass, widgetClass, panelClass, queryPanelClass, resultPanelClass, serviceClass, pipeClass, vcClass) {
Akronb43c8c62018-07-04 18:27:28 +02002
3 describe('KorAP.Plugin.Server', function () {
4
5 it('should be initializable', function () {
6 var manager = pluginServerClass.create();
7 expect(manager).toBeTruthy();
8 manager.destroy();
9 });
10
11 it('should add a widget', function () {
12 var manager = pluginServerClass.create();
Akron4a703872018-07-26 10:59:41 +020013 var panel = panelClass.create();
14 var id = manager.addWidget(panel, 'Example 1', 'about:blank');
Akronb43c8c62018-07-04 18:27:28 +020015 expect(id).toMatch(/^id-/);
Akron4a703872018-07-26 10:59:41 +020016
17 var panelE = panel.element();
18 var widgetE = panelE.firstChild.firstChild;
19 expect(widgetE.classList.contains('widget')).toBeTruthy();
20 expect(widgetE.firstChild.tagName).toEqual("IFRAME");
21 var iframe = widgetE.firstChild;
22 expect(iframe.getAttribute("src")).toEqual("about:blank");
23
24 expect(widgetE.lastChild.firstChild.textContent).toEqual("Close");
25 expect(widgetE.lastChild.lastChild.textContent).toEqual("Example 1");
26
Akronb43c8c62018-07-04 18:27:28 +020027 manager.destroy();
28 });
Akron10a47962018-07-12 21:17:10 +020029
Akron22598cd2019-12-09 14:59:03 +010030 it('should add a service', function () {
31 var manager = pluginServerClass.create();
32
33 var e = manager.element();
34
35 document.body.appendChild(e);
36
37 expect(document.getElementById("services")).toBeTruthy();
38
39 expect(e.getAttribute("id")).toBe("services");
40 expect(e.children.length).toBe(0);
41
42 var id = manager.addService('Example 1', 'about:blank');
43 expect(id).toMatch(/^id-/);
44
45 expect(e.children.length).toBe(1);
46
47 manager.destroy();
48
49 expect(document.getElementById("services")).toBeFalsy();
50
51 });
52
Akron4a703872018-07-26 10:59:41 +020053 it('should close a widget', function () {
54 var manager = pluginServerClass.create();
55 var panel = panelClass.create();
56 var id = manager.addWidget(panel, 'Example 2', 'about:blank');
57 expect(id).toMatch(/^id-/);
58
59 var panelE = panel.element();
60 var widgetE = panelE.firstChild.firstChild;
61 expect(widgetE.classList.contains('widget')).toBeTruthy();
62
63 expect(panelE.getElementsByClassName('view').length).toEqual(1);
64
Akron22598cd2019-12-09 14:59:03 +010065 var widget = manager.service(id);
66 expect(widget.isWidget).toBeTruthy();
Akron4a703872018-07-26 10:59:41 +020067 widget.close();
68
69 expect(panelE.getElementsByClassName('view').length).toEqual(0);
Akron4a703872018-07-26 10:59:41 +020070 manager.destroy();
71 });
72
73
74 it('should fail on invalid registrations', function () {
Akron10a47962018-07-12 21:17:10 +020075 var manager = pluginServerClass.create();
76
77 expect(
78 function() { manager.register({}) }
79 ).toThrow(new Error("Missing name of plugin"));
80
81 expect(
82 function() { manager.register({
83 name : 'Example',
84 embed : ''
85 })}
86 ).toThrow(new Error("Embedding of plugin is no list"));
87
88 expect(
89 function() { manager.register({
90 name : 'Example',
91 embed : [{
92 panel : ''
93 }]
94 })}
95 ).toThrow(new Error("Panel for plugin is invalid"));
Akron2d0d96d2019-11-18 19:49:50 +010096 manager.destroy();
97 });
98
99 it('should accept valid registrations for matches', function () {
100 var manager = pluginServerClass.create();
101
102 manager.register({
103 name : 'Check',
104 embed : [{
105 panel : 'match',
106 title : 'Translate',
107 onClick : {
108 template : 'test'
109 }
110 }]
111 });
112
113 expect(manager.buttonGroup('match').length).toEqual(1);
114 manager.destroy();
115 });
116
117 it('should accept valid registrations for query temporary', function () {
118 var manager = pluginServerClass.create();
119
120 manager.register({
121 name : 'Check',
122 embed : [{
123 panel : 'query',
124 title : 'Translate',
125 onClick : {
126 template : 'test'
127 }
128 }]
129 });
130
131 expect(manager.buttonGroup('query').length).toEqual(1);
132 manager.destroy();
Akron10a47962018-07-12 21:17:10 +0200133 });
hebasta043e96f2019-11-28 12:33:00 +0100134
135
136 it('should accept valid registrations for result', function () {
137 var manager = pluginServerClass.create();
138
139 manager.register({
140 name : 'Check',
141 embed : [{
142 panel : 'result',
143 title : 'Translate',
144 onClick : {
145 template : 'test'
146 }
147 }]
148 });
149
150 expect(manager.buttonGroup('result').length).toEqual(1);
151 manager.destroy();
152 });
Akronba09ed22020-10-01 16:01:45 +0200153
154 it('should accept valid registrations for addWidget', function () {
155 let p = KorAP.Panel["result"] = panelClass.create();
156
157 let manager = pluginServerClass.create();
158
159 manager.register({
160 name : 'Check',
161 embed : [{
162 panel : 'result',
163 title : 'Add',
164 onClick : {
165 template : 'about:blank',
166 action : 'addWidget'
167 }
168 }]
169 });
170
171 let b = p.actions.element().firstChild;
172 expect(b.hasAttribute("data-icon")).toBeFalsy();
173 expect(b.hasAttribute("cls")).toBeFalsy();
174 expect(b.getAttribute("title")).toEqual("Add");
175
176 expect(p.element().querySelectorAll("iframe").length).toEqual(0);
177
178 b.click();
179
180 expect(p.element().querySelectorAll("iframe").length).toEqual(1);
181 expect(p.element().querySelectorAll("div.view.widget").length).toEqual(1);
182 expect(p.element().querySelectorAll("div.view.show.widget").length).toEqual(1);
183
184 b.click();
185
186 expect(p.element().querySelectorAll("iframe").length).toEqual(2);
187 expect(p.element().querySelectorAll("div.view.widget").length).toEqual(2);
188 expect(p.element().querySelectorAll("div.view.show.widget").length).toEqual(2);
189
190 manager.destroy();
191
192 KorAP.Panel["result"] = undefined;
193 });
194
195
196 it('should accept valid registrations for setWidget', function () {
197 let p = KorAP.Panel["result"] = panelClass.create();
198
199 let manager = pluginServerClass.create();
200
201 manager.register({
202 name : 'Check',
203 embed : [{
204 panel : 'result',
205 title : 'Add',
206 onClick : {
207 template : 'about:blank',
208 action : 'setWidget'
209 }
210 }]
211 });
212
213 let b = p.actions.element().firstChild;
214 expect(b.hasAttribute("data-icon")).toBeFalsy();
215 expect(b.hasAttribute("cls")).toBeFalsy();
216 expect(b.getAttribute("title")).toEqual("Add");
217
218 expect(p.element().querySelectorAll("iframe").length).toEqual(0);
219
220 b.click();
221
222 expect(p.element().querySelectorAll("iframe").length).toEqual(1);
223 expect(p.element().querySelectorAll("div.view.show.widget").length).toEqual(1);
224
225 b.click();
226
227 expect(p.element().querySelectorAll("iframe").length).toEqual(1);
228 expect(p.element().querySelectorAll("div.view.widget").length).toEqual(1);
229 expect(p.element().querySelectorAll("div.view.show.widget").length).toEqual(0);
230
231 manager.destroy();
232
233 KorAP.Panel["result"] = undefined;
234 });
Akronb43c8c62018-07-04 18:27:28 +0200235 });
Akron2d0d96d2019-11-18 19:49:50 +0100236
Akronb43c8c62018-07-04 18:27:28 +0200237 describe('KorAP.Plugin.Widget', function () {
238 it('should be initializable', function () {
Akron22598cd2019-12-09 14:59:03 +0100239 expect(function () { widgetClass.create() }).toThrow(new Error("Service not well defined"));
Akron56a11af2018-07-27 18:28:45 +0200240
241 widget = widgetClass.create("Test", "https://example", 56);
Akronb43c8c62018-07-04 18:27:28 +0200242 expect(widget).toBeTruthy();
Akron56a11af2018-07-27 18:28:45 +0200243 expect(widget.id).toEqual(56);
244 expect(widget.name).toEqual("Test");
245 expect(widget.src).toEqual("https://example");
246 });
247
248 it('should create a view element', function () {
249 var widget = widgetClass.create("Test", "https://example", 56);
250 var we = widget.element();
251
252 expect(we.tagName).toEqual("DIV");
253 expect(we.classList.contains('view')).toBeTruthy();
254 expect(we.classList.contains('widget')).toBeTruthy();
255
256 var iframe = we.firstChild;
257 expect(iframe.tagName).toEqual("IFRAME");
hebasta78913242020-03-30 13:39:20 +0200258 expect(iframe.getAttribute("sandbox")).toEqual("allow-scripts allow-forms");
Akron56a11af2018-07-27 18:28:45 +0200259 expect(iframe.getAttribute("src")).toEqual("https://example");
260 expect(iframe.getAttribute("name")).toEqual("56");
261
262 var btn = we.lastChild;
263 expect(btn.classList.contains("button-group")).toBeTruthy();
264 expect(btn.classList.contains("button-view")).toBeTruthy();
265 expect(btn.classList.contains("widget")).toBeTruthy();
266
267 expect(btn.firstChild.tagName).toEqual("SPAN");
268 expect(btn.firstChild.classList.contains("button-icon")).toBeTruthy();
269 expect(btn.firstChild.classList.contains("close")).toBeTruthy();
270 expect(btn.firstChild.firstChild.tagName).toEqual("SPAN");
271
272 expect(btn.lastChild.tagName).toEqual("SPAN");
273 expect(btn.lastChild.classList.contains("button-icon")).toBeTruthy();
274 expect(btn.lastChild.classList.contains("plugin")).toBeTruthy();
275 expect(btn.lastChild.firstChild.tagName).toEqual("SPAN");
276 expect(btn.lastChild.textContent).toEqual("Test");
277 })
278
279 it('should be resizable', function () {
280 var widget = widgetClass.create("Test", "https://example", 56);
281 var iframe = widget.show();
282 expect(iframe.style.height).toEqual('0px');
283 widget.resize({ height : 9 });
284 expect(iframe.style.height).toEqual('9px');
Akronb43c8c62018-07-04 18:27:28 +0200285 });
Akron3d9ce5e2020-10-01 15:18:36 +0200286
287 it('should be minimizable', function () {
288 var widget = widgetClass.create("Test", "https://example", 56);
289 var we = widget.element();
290 expect(we.classList.contains('show')).toBeTruthy();
291 widget.minimize();
292 expect(we.classList.contains('show')).toBeFalsy();
293 });
Akronb43c8c62018-07-04 18:27:28 +0200294 });
Akron2d0d96d2019-11-18 19:49:50 +0100295
Akron22598cd2019-12-09 14:59:03 +0100296 describe('KorAP.Plugin.Service', function () {
297 it('should be initializable', function () {
298 expect(function () { serviceClass.create() }).toThrow(new Error("Service not well defined"));
299
300 let service = serviceClass.create("Test", "https://example", 56);
301 expect(service).toBeTruthy();
302 expect(service.id).toEqual(56);
303 expect(service.name).toEqual("Test");
304 expect(service.src).toEqual("https://example");
305 });
306
307 it('should be loadable', function () {
308 let service = serviceClass.create("Test", "https://example", 56);
309 expect(service).toBeTruthy();
310
311 let i = service.load();
312 expect(i.tagName).toEqual("IFRAME");
313 expect(i.getAttribute("allowTransparency")).toEqual("true");
314 expect(i.getAttribute("frameborder")).toEqual(''+0);
315 expect(i.getAttribute("name")).toEqual(''+service.id);
316 expect(i.getAttribute("src")).toEqual(service.src);
317 });
318 });
319
Akron2d0d96d2019-11-18 19:49:50 +0100320 describe('KorAP.Plugin.QueryPanel', function () {
321 it('should establish a query plugin', function () {
322 var queryPanel = queryPanelClass.create();
323
324 var div = document.createElement('div');
325
326 div.appendChild(queryPanel.element());
327 KorAP.Panel = KorAP.Panel || {};
328 KorAP.Panel['query'] = queryPanel;
329
330 // Register plugin afterwards
331 var manager = pluginServerClass.create();
332
333 manager.register({
334 name : 'Check',
335 embed : [{
336 panel : 'query',
337 title : 'Translate',
338 onClick : {
339 template : 'test'
340 }
341 }]
342 });
343
344 expect(manager.buttonGroup('query').length).toEqual(0);
345
346 // Clean up
347 KorAP.Panel['query'] = undefined;
348 manager.destroy();
349 });
hebastaf6adf8d2019-11-26 14:04:10 +0100350
351 it('Plugin buttons should be cleared after adding to panel', function () {
352
353 // Register plugin first
354 KorAP.Plugin = pluginServerClass.create();
355
356 KorAP.Plugin.register({
357 name : 'Check',
358 embed : [{
359 panel : 'query',
360 title : 'Translate',
361 onClick : {
362 template : 'test'
363 }
364 }]
365 });
366
367
368 var queryPanel = queryPanelClass.create();
369 var div = document.createElement('div');
370
371 div.appendChild(queryPanel.element());
372 KorAP.Panel = KorAP.Panel || {};
373 KorAP.Panel['query'] = queryPanel;
374 expect(KorAP.Plugin.buttonGroup('query').length).toEqual(0);
375
376 // Clean up
377 KorAP.Panel['query'] = undefined;
378 KorAP.Plugin.destroy();
379 KorAP.Plugin = undefined;
380 });
Akron2d0d96d2019-11-18 19:49:50 +0100381 });
hebasta043e96f2019-11-28 12:33:00 +0100382
Akron45308ce2020-08-28 14:10:23 +0200383 describe('KorAP.Plugin.ResultPanel', function () {
hebasta043e96f2019-11-28 12:33:00 +0100384 it('Plugin is registered second: buttons should be added to panel', function () {
385
386 var resultPanel = resultPanelClass.create();
387 resultPanel.addAlignAction();
388 var div = document.createElement('div');
389
390 div.appendChild(resultPanel.element());
391 KorAP.Panel = KorAP.Panel || {};
392 KorAP.Panel['result'] = resultPanel;
393
394 // Register plugin afterwards
395 var manager = pluginServerClass.create();
396
397 manager.register({
398 name : 'ResultPlugin',
399 embed : [{
400 panel : 'result',
401 title : 'Dosomething',
402 onClick : {
403 template : 'test'
404 }
405 }]
406 });
407
408 expect(manager.buttonGroup('result').length).toEqual(0);
409 expect(KorAP.Panel['result'].actions.element().innerHTML).toContain('Dosomething');
410
411 // Clean up
412 KorAP.Panel['result'] = undefined;
413 manager.destroy();
Akron45308ce2020-08-28 14:10:23 +0200414 });
hebasta043e96f2019-11-28 12:33:00 +0100415
416 it('Plugin is registered first: Buttons should be added to panel and cleared', function () {
417
418 // Register plugin first
419 KorAP.Plugin = pluginServerClass.create();
420
421 KorAP.Plugin.register({
422 name : 'ResultPlugin',
423 embed : [{
424 panel : 'result',
425 title : 'Dosomething',
426 onClick : {
427 template : 'test'
428 }
429 }]
430 });
431
432 expect(KorAP.Plugin.buttonGroup('result').length).toEqual(1);
433
434 var resultPanel = resultPanelClass.create();
435 var div = document.createElement('div');
436 div.appendChild(resultPanel.element());
437 KorAP.Panel = KorAP.Panel || {};
438 KorAP.Panel['result'] = resultPanel;
439 expect(KorAP.Plugin.buttonGroup('result').length).toEqual(0);
440 expect(KorAP.Panel['result'].actions.element().innerHTML).toContain('Dosomething');
441
442 // Clean up
443 KorAP.Panel['result'] = undefined;
444 KorAP.Plugin.destroy();
445 KorAP.Plugin = undefined;
446 });
447 });
Akronfec66a32020-08-28 13:01:14 +0200448
449 describe('KorAP.Plugin communications', function () {
450 it('should receive messages', function () {
451 var manager = pluginServerClass.create();
Akronfec66a32020-08-28 13:01:14 +0200452 var id = manager.addService('Example 1', 'about:blank');
453 expect(id).toMatch(/^id-/);
Akronfec66a32020-08-28 13:01:14 +0200454 var temp = KorAP.koralQuery;
455 KorAP.koralQuery = { "@type" : "koral:test" };
Akronfec66a32020-08-28 13:01:14 +0200456 let data = {
457 "originID" : id,
458 "action" : "get",
459 "key" : "KQ"
460 };
Akronfec66a32020-08-28 13:01:14 +0200461 manager._receiveMsg({
462 "data" : data
463 });
Akronfec66a32020-08-28 13:01:14 +0200464 manager.destroy();
Akronfec66a32020-08-28 13:01:14 +0200465 expect(data.value["@type"]).toEqual("koral:test");
Akronfec66a32020-08-28 13:01:14 +0200466 // Recreate initial state
467 KorAP.koralQuery = temp;
468 });
469
Akronfec66a32020-08-28 13:01:14 +0200470 it('should add to pipe', function () {
471 var manager = pluginServerClass.create();
Akronfec66a32020-08-28 13:01:14 +0200472 var temp = KorAP.Pipe;
473 KorAP.Pipe = pipeClass.create();
Akronfec66a32020-08-28 13:01:14 +0200474 expect(KorAP.Pipe.toString()).toEqual("");
475
476 var id = manager.addService('Example 2', 'about:blank');
477 expect(id).toMatch(/^id-/);
Akronfec66a32020-08-28 13:01:14 +0200478 manager._receiveMsg({
479 "data" : {
480 "originID" : id,
481 "action" : "pipe",
482 "job" : "add",
483 "service" : "https://pipe-service.de"
484 }
485 });
Akronfec66a32020-08-28 13:01:14 +0200486 expect(KorAP.Pipe.toString()).toEqual("https://pipe-service.de");
Akronfec66a32020-08-28 13:01:14 +0200487 manager._receiveMsg({
488 "data" : {
489 "originID" : id,
490 "action" : "pipe",
491 "job" : "add",
492 "service" : "https://pipe-service-2.de"
493 }
494 });
Akronfec66a32020-08-28 13:01:14 +0200495 expect(KorAP.Pipe.toString()).toEqual("https://pipe-service.de,https://pipe-service-2.de");
Akronfec66a32020-08-28 13:01:14 +0200496 manager._receiveMsg({
497 "data" : {
498 "originID" : id,
499 "action" : "pipe",
500 "job" : "del",
501 "service" : "https://pipe-service.de"
502 }
503 });
Akronfec66a32020-08-28 13:01:14 +0200504 expect(KorAP.Pipe.toString()).toEqual("https://pipe-service-2.de");
Akronfec66a32020-08-28 13:01:14 +0200505 manager.destroy();
Akronfec66a32020-08-28 13:01:14 +0200506 // Recreate initial state
507 KorAP.Pipe = temp;
508 });
Akron45308ce2020-08-28 14:10:23 +0200509
510 it('should reply to query information requests', function () {
511 var manager = pluginServerClass.create();
512 var id = manager.addService('Service', 'about:blank');
513 expect(id).toMatch(/^id-/);
514 var temp = KorAP.vc;
515 // Create form for query form information
516 let f = document.createElement('form');
517 var qfield = f.addE('input');
518 qfield.setAttribute("id", "q-field");
519 qfield.value = "[orth=Baum]";
520 var qlfield = f.addE('select');
521 qlfield.setAttribute("id", "ql-field");
522 qlfield.addE('option').setAttribute('value', 'cosmas-2');
523 qlfield.addE('option').setAttribute('value', 'poliqarp');
524 qlfield.selectedIndex = 1;
525
526 KorAP.vc = vcClass.create().fromJson({
527 "key" : "title",
528 "type" : "type:regex",
529 "value" : "[^b]ee.+?",
530 "@type" : "koral:doc"
531 });
532 // console.log(KorAP.vc.toQuery());
533
534 document.body.appendChild(f);
535 let data = {
536 "originID" : id,
537 "action" : "get",
538 "key" : "QueryForm"
539 };
540 manager._receiveMsg({
541 "data" : data
542 });
543 manager.destroy();
544 expect(data.value["q"]).toEqual("[orth=Baum]");
545 expect(data.value["ql"]).toEqual("poliqarp");
546 expect(data.value["cq"]).toEqual("title = /[^b]ee.+?/");
547 // Recreate initial state
548 KorAP.vc = temp;
549 document.body.removeChild(f);
550 });
Akronfec66a32020-08-28 13:01:14 +0200551 });
Akronb43c8c62018-07-04 18:27:28 +0200552});