blob: e2dab8fa9c509552e6cc9399b161631cf5e0c6fe [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);
Akronfcf89db2020-10-01 17:40:20 +0200230
231 p.element().querySelector("span.close").click();
232
233 expect(p.element().querySelectorAll("iframe").length).toEqual(0);
234
235 b.click();
236
237 expect(p.element().querySelectorAll("iframe").length).toEqual(1);
238 expect(p.element().querySelectorAll("div.view.widget").length).toEqual(1);
239 expect(p.element().querySelectorAll("div.view.show.widget").length).toEqual(1);
Akronfb11a962020-10-05 12:12:55 +0200240 expect(p.element().querySelector("iframe").getAttribute('sandbox')).toEqual('');
Akronba09ed22020-10-01 16:01:45 +0200241
242 manager.destroy();
243
244 KorAP.Panel["result"] = undefined;
245 });
Akronfb11a962020-10-05 12:12:55 +0200246
247 it('should accept widget permissions', function () {
248 let p = KorAP.Panel["result"] = panelClass.create();
249
250 let manager = pluginServerClass.create();
251
252 manager.register({
253 name : 'Check',
254 embed : [{
255 panel : 'result',
256 title : 'Add',
257 onClick : {
258 template : 'about:blank',
259 action : 'addWidget',
260 permissions: ['allow-scripts', 'allow-forms']
261 }
262 }]
263 });
264
265 let b = p.actions.element().firstChild;
266 b.click();
267 expect(p.element().querySelectorAll("iframe").length).toEqual(1);
268 expect(p.element().querySelector("iframe").getAttribute('sandbox')).toEqual('allow-forms allow-scripts');
269 });
Akronb43c8c62018-07-04 18:27:28 +0200270 });
Akron2d0d96d2019-11-18 19:49:50 +0100271
Akronb43c8c62018-07-04 18:27:28 +0200272 describe('KorAP.Plugin.Widget', function () {
273 it('should be initializable', function () {
Akron22598cd2019-12-09 14:59:03 +0100274 expect(function () { widgetClass.create() }).toThrow(new Error("Service not well defined"));
Akron56a11af2018-07-27 18:28:45 +0200275
276 widget = widgetClass.create("Test", "https://example", 56);
Akronb43c8c62018-07-04 18:27:28 +0200277 expect(widget).toBeTruthy();
Akron56a11af2018-07-27 18:28:45 +0200278 expect(widget.id).toEqual(56);
279 expect(widget.name).toEqual("Test");
280 expect(widget.src).toEqual("https://example");
281 });
282
283 it('should create a view element', function () {
284 var widget = widgetClass.create("Test", "https://example", 56);
Akronfb11a962020-10-05 12:12:55 +0200285 widget.allow("allow-scripts");
286 widget.allow("allow-forms");
Akron56a11af2018-07-27 18:28:45 +0200287 var we = widget.element();
288
289 expect(we.tagName).toEqual("DIV");
290 expect(we.classList.contains('view')).toBeTruthy();
291 expect(we.classList.contains('widget')).toBeTruthy();
292
293 var iframe = we.firstChild;
294 expect(iframe.tagName).toEqual("IFRAME");
Akronfb11a962020-10-05 12:12:55 +0200295 expect(iframe.getAttribute("sandbox")).toEqual("allow-forms allow-scripts");
Akron56a11af2018-07-27 18:28:45 +0200296 expect(iframe.getAttribute("src")).toEqual("https://example");
297 expect(iframe.getAttribute("name")).toEqual("56");
298
Akronfb11a962020-10-05 12:12:55 +0200299
300 widget.allow(["allow-downloads","allow-everything"]);
301 expect(iframe.getAttribute("sandbox")).toEqual("allow-downloads allow-everything allow-forms allow-scripts");
302
Akron56a11af2018-07-27 18:28:45 +0200303 var btn = we.lastChild;
304 expect(btn.classList.contains("button-group")).toBeTruthy();
305 expect(btn.classList.contains("button-view")).toBeTruthy();
306 expect(btn.classList.contains("widget")).toBeTruthy();
307
308 expect(btn.firstChild.tagName).toEqual("SPAN");
309 expect(btn.firstChild.classList.contains("button-icon")).toBeTruthy();
310 expect(btn.firstChild.classList.contains("close")).toBeTruthy();
311 expect(btn.firstChild.firstChild.tagName).toEqual("SPAN");
312
313 expect(btn.lastChild.tagName).toEqual("SPAN");
314 expect(btn.lastChild.classList.contains("button-icon")).toBeTruthy();
315 expect(btn.lastChild.classList.contains("plugin")).toBeTruthy();
316 expect(btn.lastChild.firstChild.tagName).toEqual("SPAN");
317 expect(btn.lastChild.textContent).toEqual("Test");
318 })
319
Akronfb11a962020-10-05 12:12:55 +0200320 it('should have mutable permissions', function () {
321 var widget = widgetClass.create("Test", "https://example", 56);
322 var we = widget.element();
323 var iframe = we.firstChild;
324 expect(iframe.tagName).toEqual("IFRAME");
325 expect(iframe.getAttribute("sandbox")).toEqual("");
326 widget.allow("allow-scripts");
327 widget.allow("allow-forms");
328 expect(iframe.tagName).toEqual("IFRAME");
329 expect(iframe.getAttribute("sandbox")).toEqual("allow-forms allow-scripts");
330 });
331
Akron56a11af2018-07-27 18:28:45 +0200332 it('should be resizable', function () {
333 var widget = widgetClass.create("Test", "https://example", 56);
334 var iframe = widget.show();
335 expect(iframe.style.height).toEqual('0px');
336 widget.resize({ height : 9 });
337 expect(iframe.style.height).toEqual('9px');
Akronb43c8c62018-07-04 18:27:28 +0200338 });
Akron3d9ce5e2020-10-01 15:18:36 +0200339
340 it('should be minimizable', function () {
341 var widget = widgetClass.create("Test", "https://example", 56);
342 var we = widget.element();
343 expect(we.classList.contains('show')).toBeTruthy();
344 widget.minimize();
345 expect(we.classList.contains('show')).toBeFalsy();
346 });
Akronb43c8c62018-07-04 18:27:28 +0200347 });
Akron2d0d96d2019-11-18 19:49:50 +0100348
Akron22598cd2019-12-09 14:59:03 +0100349 describe('KorAP.Plugin.Service', function () {
350 it('should be initializable', function () {
351 expect(function () { serviceClass.create() }).toThrow(new Error("Service not well defined"));
352
353 let service = serviceClass.create("Test", "https://example", 56);
354 expect(service).toBeTruthy();
355 expect(service.id).toEqual(56);
356 expect(service.name).toEqual("Test");
357 expect(service.src).toEqual("https://example");
358 });
359
360 it('should be loadable', function () {
361 let service = serviceClass.create("Test", "https://example", 56);
362 expect(service).toBeTruthy();
363
364 let i = service.load();
365 expect(i.tagName).toEqual("IFRAME");
366 expect(i.getAttribute("allowTransparency")).toEqual("true");
367 expect(i.getAttribute("frameborder")).toEqual(''+0);
368 expect(i.getAttribute("name")).toEqual(''+service.id);
369 expect(i.getAttribute("src")).toEqual(service.src);
370 });
371 });
372
Akron2d0d96d2019-11-18 19:49:50 +0100373 describe('KorAP.Plugin.QueryPanel', function () {
374 it('should establish a query plugin', function () {
375 var queryPanel = queryPanelClass.create();
376
377 var div = document.createElement('div');
378
379 div.appendChild(queryPanel.element());
380 KorAP.Panel = KorAP.Panel || {};
381 KorAP.Panel['query'] = queryPanel;
382
383 // Register plugin afterwards
384 var manager = pluginServerClass.create();
385
386 manager.register({
387 name : 'Check',
388 embed : [{
389 panel : 'query',
390 title : 'Translate',
391 onClick : {
392 template : 'test'
393 }
394 }]
395 });
396
397 expect(manager.buttonGroup('query').length).toEqual(0);
398
399 // Clean up
400 KorAP.Panel['query'] = undefined;
401 manager.destroy();
402 });
hebastaf6adf8d2019-11-26 14:04:10 +0100403
404 it('Plugin buttons should be cleared after adding to panel', function () {
405
406 // Register plugin first
407 KorAP.Plugin = pluginServerClass.create();
408
409 KorAP.Plugin.register({
410 name : 'Check',
411 embed : [{
412 panel : 'query',
413 title : 'Translate',
414 onClick : {
415 template : 'test'
416 }
417 }]
418 });
419
420
421 var queryPanel = queryPanelClass.create();
422 var div = document.createElement('div');
423
424 div.appendChild(queryPanel.element());
425 KorAP.Panel = KorAP.Panel || {};
426 KorAP.Panel['query'] = queryPanel;
427 expect(KorAP.Plugin.buttonGroup('query').length).toEqual(0);
428
429 // Clean up
430 KorAP.Panel['query'] = undefined;
431 KorAP.Plugin.destroy();
432 KorAP.Plugin = undefined;
433 });
Akron2d0d96d2019-11-18 19:49:50 +0100434 });
hebasta043e96f2019-11-28 12:33:00 +0100435
Akron45308ce2020-08-28 14:10:23 +0200436 describe('KorAP.Plugin.ResultPanel', function () {
hebasta043e96f2019-11-28 12:33:00 +0100437 it('Plugin is registered second: buttons should be added to panel', function () {
438
439 var resultPanel = resultPanelClass.create();
440 resultPanel.addAlignAction();
441 var div = document.createElement('div');
442
443 div.appendChild(resultPanel.element());
444 KorAP.Panel = KorAP.Panel || {};
445 KorAP.Panel['result'] = resultPanel;
446
447 // Register plugin afterwards
448 var manager = pluginServerClass.create();
449
450 manager.register({
451 name : 'ResultPlugin',
452 embed : [{
453 panel : 'result',
454 title : 'Dosomething',
455 onClick : {
456 template : 'test'
457 }
458 }]
459 });
460
461 expect(manager.buttonGroup('result').length).toEqual(0);
462 expect(KorAP.Panel['result'].actions.element().innerHTML).toContain('Dosomething');
463
464 // Clean up
465 KorAP.Panel['result'] = undefined;
466 manager.destroy();
Akron45308ce2020-08-28 14:10:23 +0200467 });
hebasta043e96f2019-11-28 12:33:00 +0100468
469 it('Plugin is registered first: Buttons should be added to panel and cleared', function () {
470
471 // Register plugin first
472 KorAP.Plugin = pluginServerClass.create();
473
474 KorAP.Plugin.register({
475 name : 'ResultPlugin',
476 embed : [{
477 panel : 'result',
478 title : 'Dosomething',
479 onClick : {
480 template : 'test'
481 }
482 }]
483 });
484
485 expect(KorAP.Plugin.buttonGroup('result').length).toEqual(1);
486
487 var resultPanel = resultPanelClass.create();
488 var div = document.createElement('div');
489 div.appendChild(resultPanel.element());
490 KorAP.Panel = KorAP.Panel || {};
491 KorAP.Panel['result'] = resultPanel;
492 expect(KorAP.Plugin.buttonGroup('result').length).toEqual(0);
493 expect(KorAP.Panel['result'].actions.element().innerHTML).toContain('Dosomething');
494
495 // Clean up
496 KorAP.Panel['result'] = undefined;
497 KorAP.Plugin.destroy();
498 KorAP.Plugin = undefined;
499 });
500 });
Akronfec66a32020-08-28 13:01:14 +0200501
502 describe('KorAP.Plugin communications', function () {
503 it('should receive messages', function () {
504 var manager = pluginServerClass.create();
Akronfec66a32020-08-28 13:01:14 +0200505 var id = manager.addService('Example 1', 'about:blank');
506 expect(id).toMatch(/^id-/);
Akronfec66a32020-08-28 13:01:14 +0200507 var temp = KorAP.koralQuery;
508 KorAP.koralQuery = { "@type" : "koral:test" };
Akronfec66a32020-08-28 13:01:14 +0200509 let data = {
510 "originID" : id,
511 "action" : "get",
512 "key" : "KQ"
513 };
Akronfec66a32020-08-28 13:01:14 +0200514 manager._receiveMsg({
515 "data" : data
516 });
Akronfec66a32020-08-28 13:01:14 +0200517 manager.destroy();
Akronfec66a32020-08-28 13:01:14 +0200518 expect(data.value["@type"]).toEqual("koral:test");
Akronfec66a32020-08-28 13:01:14 +0200519 // Recreate initial state
520 KorAP.koralQuery = temp;
521 });
522
Akronfec66a32020-08-28 13:01:14 +0200523 it('should add to pipe', function () {
524 var manager = pluginServerClass.create();
Akronfec66a32020-08-28 13:01:14 +0200525 var temp = KorAP.Pipe;
526 KorAP.Pipe = pipeClass.create();
Akronfec66a32020-08-28 13:01:14 +0200527 expect(KorAP.Pipe.toString()).toEqual("");
528
529 var id = manager.addService('Example 2', 'about:blank');
530 expect(id).toMatch(/^id-/);
Akronfec66a32020-08-28 13:01:14 +0200531 manager._receiveMsg({
532 "data" : {
533 "originID" : id,
534 "action" : "pipe",
535 "job" : "add",
536 "service" : "https://pipe-service.de"
537 }
538 });
Akronfec66a32020-08-28 13:01:14 +0200539 expect(KorAP.Pipe.toString()).toEqual("https://pipe-service.de");
Akronfec66a32020-08-28 13:01:14 +0200540 manager._receiveMsg({
541 "data" : {
542 "originID" : id,
543 "action" : "pipe",
544 "job" : "add",
545 "service" : "https://pipe-service-2.de"
546 }
547 });
Akronfec66a32020-08-28 13:01:14 +0200548 expect(KorAP.Pipe.toString()).toEqual("https://pipe-service.de,https://pipe-service-2.de");
Akronfec66a32020-08-28 13:01:14 +0200549 manager._receiveMsg({
550 "data" : {
551 "originID" : id,
552 "action" : "pipe",
553 "job" : "del",
554 "service" : "https://pipe-service.de"
555 }
556 });
Akronfec66a32020-08-28 13:01:14 +0200557 expect(KorAP.Pipe.toString()).toEqual("https://pipe-service-2.de");
Akronfec66a32020-08-28 13:01:14 +0200558 manager.destroy();
Akronfec66a32020-08-28 13:01:14 +0200559 // Recreate initial state
560 KorAP.Pipe = temp;
561 });
Akron45308ce2020-08-28 14:10:23 +0200562
563 it('should reply to query information requests', function () {
564 var manager = pluginServerClass.create();
565 var id = manager.addService('Service', 'about:blank');
566 expect(id).toMatch(/^id-/);
567 var temp = KorAP.vc;
568 // Create form for query form information
569 let f = document.createElement('form');
570 var qfield = f.addE('input');
571 qfield.setAttribute("id", "q-field");
572 qfield.value = "[orth=Baum]";
573 var qlfield = f.addE('select');
574 qlfield.setAttribute("id", "ql-field");
575 qlfield.addE('option').setAttribute('value', 'cosmas-2');
576 qlfield.addE('option').setAttribute('value', 'poliqarp');
577 qlfield.selectedIndex = 1;
578
579 KorAP.vc = vcClass.create().fromJson({
580 "key" : "title",
581 "type" : "type:regex",
582 "value" : "[^b]ee.+?",
583 "@type" : "koral:doc"
584 });
585 // console.log(KorAP.vc.toQuery());
586
587 document.body.appendChild(f);
588 let data = {
589 "originID" : id,
590 "action" : "get",
591 "key" : "QueryForm"
592 };
593 manager._receiveMsg({
594 "data" : data
595 });
596 manager.destroy();
597 expect(data.value["q"]).toEqual("[orth=Baum]");
598 expect(data.value["ql"]).toEqual("poliqarp");
599 expect(data.value["cq"]).toEqual("title = /[^b]ee.+?/");
600 // Recreate initial state
601 KorAP.vc = temp;
602 document.body.removeChild(f);
603 });
Akronfec66a32020-08-28 13:01:14 +0200604 });
Akronb43c8c62018-07-04 18:27:28 +0200605});