blob: 2a980ad06efccae52c38f2783a1359c51395b3e3 [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();
Akronbb891982020-10-05 16:07:18 +020014 var id = manager.addWidget(panel, {"name": 'Example 1', "src": '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
Akronbb891982020-10-05 16:07:18 +020042 var id = manager.addService({"name":'Example 1', "src":'about:blank'});
Akron22598cd2019-12-09 14:59:03 +010043 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();
Akronbb891982020-10-05 16:07:18 +020056 var id = manager.addWidget(panel, {"name":'Example 2', "src":'about:blank'});
Akron4a703872018-07-26 10:59:41 +020057 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
Akronbb891982020-10-05 16:07:18 +0200276 widget = widgetClass.create({"name" : "Test", "src":"https://example", "id":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 () {
Akronbb891982020-10-05 16:07:18 +0200284 var widget = widgetClass.create({
285 "name":"Test",
286 "src":"https://example",
287 "id":56,
288 "permissions":["allow-scripts","allow-forms"]
289 });
Akron56a11af2018-07-27 18:28:45 +0200290 var we = widget.element();
291
292 expect(we.tagName).toEqual("DIV");
293 expect(we.classList.contains('view')).toBeTruthy();
294 expect(we.classList.contains('widget')).toBeTruthy();
295
296 var iframe = we.firstChild;
297 expect(iframe.tagName).toEqual("IFRAME");
Akronfb11a962020-10-05 12:12:55 +0200298 expect(iframe.getAttribute("sandbox")).toEqual("allow-forms allow-scripts");
Akron56a11af2018-07-27 18:28:45 +0200299 expect(iframe.getAttribute("src")).toEqual("https://example");
300 expect(iframe.getAttribute("name")).toEqual("56");
Akronfb11a962020-10-05 12:12:55 +0200301
Akron56a11af2018-07-27 18:28:45 +0200302 var btn = we.lastChild;
303 expect(btn.classList.contains("button-group")).toBeTruthy();
304 expect(btn.classList.contains("button-view")).toBeTruthy();
305 expect(btn.classList.contains("widget")).toBeTruthy();
306
307 expect(btn.firstChild.tagName).toEqual("SPAN");
308 expect(btn.firstChild.classList.contains("button-icon")).toBeTruthy();
309 expect(btn.firstChild.classList.contains("close")).toBeTruthy();
310 expect(btn.firstChild.firstChild.tagName).toEqual("SPAN");
311
312 expect(btn.lastChild.tagName).toEqual("SPAN");
313 expect(btn.lastChild.classList.contains("button-icon")).toBeTruthy();
314 expect(btn.lastChild.classList.contains("plugin")).toBeTruthy();
315 expect(btn.lastChild.firstChild.tagName).toEqual("SPAN");
316 expect(btn.lastChild.textContent).toEqual("Test");
317 })
Akronfb11a962020-10-05 12:12:55 +0200318
Akron56a11af2018-07-27 18:28:45 +0200319 it('should be resizable', function () {
Akronbb891982020-10-05 16:07:18 +0200320 var widget = widgetClass.create({"name":"Test", "src":"https://example", "id":56});
Akron56a11af2018-07-27 18:28:45 +0200321 var iframe = widget.show();
322 expect(iframe.style.height).toEqual('0px');
323 widget.resize({ height : 9 });
324 expect(iframe.style.height).toEqual('9px');
Akronb43c8c62018-07-04 18:27:28 +0200325 });
Akron3d9ce5e2020-10-01 15:18:36 +0200326
327 it('should be minimizable', function () {
Akronbb891982020-10-05 16:07:18 +0200328 var widget = widgetClass.create({"name":"Test", "src":"https://example", "id":56});
Akron3d9ce5e2020-10-01 15:18:36 +0200329 var we = widget.element();
330 expect(we.classList.contains('show')).toBeTruthy();
331 widget.minimize();
332 expect(we.classList.contains('show')).toBeFalsy();
333 });
Akronb43c8c62018-07-04 18:27:28 +0200334 });
Akron2d0d96d2019-11-18 19:49:50 +0100335
Akron22598cd2019-12-09 14:59:03 +0100336 describe('KorAP.Plugin.Service', function () {
337 it('should be initializable', function () {
338 expect(function () { serviceClass.create() }).toThrow(new Error("Service not well defined"));
339
Akronbb891982020-10-05 16:07:18 +0200340 let service = serviceClass.create({"name":"Test", "src":"https://example", "id":56});
Akron22598cd2019-12-09 14:59:03 +0100341 expect(service).toBeTruthy();
342 expect(service.id).toEqual(56);
343 expect(service.name).toEqual("Test");
344 expect(service.src).toEqual("https://example");
345 });
346
347 it('should be loadable', function () {
Akronbb891982020-10-05 16:07:18 +0200348 let service = serviceClass.create({"name":"Test", "src":"https://example", "id":56});
Akron22598cd2019-12-09 14:59:03 +0100349 expect(service).toBeTruthy();
350
351 let i = service.load();
352 expect(i.tagName).toEqual("IFRAME");
353 expect(i.getAttribute("allowTransparency")).toEqual("true");
354 expect(i.getAttribute("frameborder")).toEqual(''+0);
355 expect(i.getAttribute("name")).toEqual(''+service.id);
356 expect(i.getAttribute("src")).toEqual(service.src);
357 });
358 });
359
Akron2d0d96d2019-11-18 19:49:50 +0100360 describe('KorAP.Plugin.QueryPanel', function () {
361 it('should establish a query plugin', function () {
362 var queryPanel = queryPanelClass.create();
363
364 var div = document.createElement('div');
365
366 div.appendChild(queryPanel.element());
367 KorAP.Panel = KorAP.Panel || {};
368 KorAP.Panel['query'] = queryPanel;
369
370 // Register plugin afterwards
371 var manager = pluginServerClass.create();
372
373 manager.register({
374 name : 'Check',
375 embed : [{
376 panel : 'query',
377 title : 'Translate',
378 onClick : {
379 template : 'test'
380 }
381 }]
382 });
383
384 expect(manager.buttonGroup('query').length).toEqual(0);
385
386 // Clean up
387 KorAP.Panel['query'] = undefined;
388 manager.destroy();
389 });
hebastaf6adf8d2019-11-26 14:04:10 +0100390
391 it('Plugin buttons should be cleared after adding to panel', function () {
392
393 // Register plugin first
394 KorAP.Plugin = pluginServerClass.create();
395
396 KorAP.Plugin.register({
397 name : 'Check',
398 embed : [{
399 panel : 'query',
400 title : 'Translate',
401 onClick : {
402 template : 'test'
403 }
404 }]
405 });
406
407
408 var queryPanel = queryPanelClass.create();
409 var div = document.createElement('div');
410
411 div.appendChild(queryPanel.element());
412 KorAP.Panel = KorAP.Panel || {};
413 KorAP.Panel['query'] = queryPanel;
414 expect(KorAP.Plugin.buttonGroup('query').length).toEqual(0);
415
416 // Clean up
417 KorAP.Panel['query'] = undefined;
418 KorAP.Plugin.destroy();
419 KorAP.Plugin = undefined;
420 });
Akron2d0d96d2019-11-18 19:49:50 +0100421 });
hebasta043e96f2019-11-28 12:33:00 +0100422
Akron45308ce2020-08-28 14:10:23 +0200423 describe('KorAP.Plugin.ResultPanel', function () {
hebasta043e96f2019-11-28 12:33:00 +0100424 it('Plugin is registered second: buttons should be added to panel', function () {
425
426 var resultPanel = resultPanelClass.create();
427 resultPanel.addAlignAction();
428 var div = document.createElement('div');
429
430 div.appendChild(resultPanel.element());
431 KorAP.Panel = KorAP.Panel || {};
432 KorAP.Panel['result'] = resultPanel;
433
434 // Register plugin afterwards
435 var manager = pluginServerClass.create();
436
437 manager.register({
438 name : 'ResultPlugin',
439 embed : [{
440 panel : 'result',
441 title : 'Dosomething',
442 onClick : {
443 template : 'test'
444 }
445 }]
446 });
447
448 expect(manager.buttonGroup('result').length).toEqual(0);
449 expect(KorAP.Panel['result'].actions.element().innerHTML).toContain('Dosomething');
450
451 // Clean up
452 KorAP.Panel['result'] = undefined;
453 manager.destroy();
Akron45308ce2020-08-28 14:10:23 +0200454 });
hebasta043e96f2019-11-28 12:33:00 +0100455
456 it('Plugin is registered first: Buttons should be added to panel and cleared', function () {
457
458 // Register plugin first
459 KorAP.Plugin = pluginServerClass.create();
460
461 KorAP.Plugin.register({
462 name : 'ResultPlugin',
463 embed : [{
464 panel : 'result',
465 title : 'Dosomething',
466 onClick : {
467 template : 'test'
468 }
469 }]
470 });
471
472 expect(KorAP.Plugin.buttonGroup('result').length).toEqual(1);
473
474 var resultPanel = resultPanelClass.create();
475 var div = document.createElement('div');
476 div.appendChild(resultPanel.element());
477 KorAP.Panel = KorAP.Panel || {};
478 KorAP.Panel['result'] = resultPanel;
479 expect(KorAP.Plugin.buttonGroup('result').length).toEqual(0);
480 expect(KorAP.Panel['result'].actions.element().innerHTML).toContain('Dosomething');
481
482 // Clean up
483 KorAP.Panel['result'] = undefined;
484 KorAP.Plugin.destroy();
485 KorAP.Plugin = undefined;
486 });
487 });
Akronfec66a32020-08-28 13:01:14 +0200488
489 describe('KorAP.Plugin communications', function () {
490 it('should receive messages', function () {
491 var manager = pluginServerClass.create();
Akronbb891982020-10-05 16:07:18 +0200492 var id = manager.addService({"name":'Example 1', "src":'about:blank'});
Akronfec66a32020-08-28 13:01:14 +0200493 expect(id).toMatch(/^id-/);
Akronfec66a32020-08-28 13:01:14 +0200494 var temp = KorAP.koralQuery;
495 KorAP.koralQuery = { "@type" : "koral:test" };
Akronfec66a32020-08-28 13:01:14 +0200496 let data = {
497 "originID" : id,
498 "action" : "get",
499 "key" : "KQ"
500 };
Akronfec66a32020-08-28 13:01:14 +0200501 manager._receiveMsg({
502 "data" : data
503 });
Akronfec66a32020-08-28 13:01:14 +0200504 manager.destroy();
Akronfec66a32020-08-28 13:01:14 +0200505 expect(data.value["@type"]).toEqual("koral:test");
Akronfec66a32020-08-28 13:01:14 +0200506 // Recreate initial state
507 KorAP.koralQuery = temp;
508 });
509
Akronfec66a32020-08-28 13:01:14 +0200510 it('should add to pipe', function () {
511 var manager = pluginServerClass.create();
Akronfec66a32020-08-28 13:01:14 +0200512 var temp = KorAP.Pipe;
513 KorAP.Pipe = pipeClass.create();
Akronfec66a32020-08-28 13:01:14 +0200514 expect(KorAP.Pipe.toString()).toEqual("");
515
Akronbb891982020-10-05 16:07:18 +0200516 var id = manager.addService({"name":'Example 2', "src":'about:blank'});
Akronfec66a32020-08-28 13:01:14 +0200517 expect(id).toMatch(/^id-/);
Akronfec66a32020-08-28 13:01:14 +0200518 manager._receiveMsg({
519 "data" : {
520 "originID" : id,
521 "action" : "pipe",
522 "job" : "add",
523 "service" : "https://pipe-service.de"
524 }
525 });
Akronfec66a32020-08-28 13:01:14 +0200526 expect(KorAP.Pipe.toString()).toEqual("https://pipe-service.de");
Akronfec66a32020-08-28 13:01:14 +0200527 manager._receiveMsg({
528 "data" : {
529 "originID" : id,
530 "action" : "pipe",
531 "job" : "add",
532 "service" : "https://pipe-service-2.de"
533 }
534 });
Akronfec66a32020-08-28 13:01:14 +0200535 expect(KorAP.Pipe.toString()).toEqual("https://pipe-service.de,https://pipe-service-2.de");
Akronfec66a32020-08-28 13:01:14 +0200536 manager._receiveMsg({
537 "data" : {
538 "originID" : id,
539 "action" : "pipe",
540 "job" : "del",
541 "service" : "https://pipe-service.de"
542 }
543 });
Akronfec66a32020-08-28 13:01:14 +0200544 expect(KorAP.Pipe.toString()).toEqual("https://pipe-service-2.de");
Akronfec66a32020-08-28 13:01:14 +0200545 manager.destroy();
Akronfec66a32020-08-28 13:01:14 +0200546 // Recreate initial state
547 KorAP.Pipe = temp;
548 });
Akron45308ce2020-08-28 14:10:23 +0200549
550 it('should reply to query information requests', function () {
551 var manager = pluginServerClass.create();
Akronbb891982020-10-05 16:07:18 +0200552 var id = manager.addService({"name":'Service', "src":'about:blank'});
Akron45308ce2020-08-28 14:10:23 +0200553 expect(id).toMatch(/^id-/);
554 var temp = KorAP.vc;
555 // Create form for query form information
556 let f = document.createElement('form');
557 var qfield = f.addE('input');
558 qfield.setAttribute("id", "q-field");
559 qfield.value = "[orth=Baum]";
560 var qlfield = f.addE('select');
561 qlfield.setAttribute("id", "ql-field");
562 qlfield.addE('option').setAttribute('value', 'cosmas-2');
563 qlfield.addE('option').setAttribute('value', 'poliqarp');
564 qlfield.selectedIndex = 1;
565
566 KorAP.vc = vcClass.create().fromJson({
567 "key" : "title",
568 "type" : "type:regex",
569 "value" : "[^b]ee.+?",
570 "@type" : "koral:doc"
571 });
572 // console.log(KorAP.vc.toQuery());
573
574 document.body.appendChild(f);
575 let data = {
576 "originID" : id,
577 "action" : "get",
578 "key" : "QueryForm"
579 };
580 manager._receiveMsg({
581 "data" : data
582 });
583 manager.destroy();
584 expect(data.value["q"]).toEqual("[orth=Baum]");
585 expect(data.value["ql"]).toEqual("poliqarp");
586 expect(data.value["cq"]).toEqual("title = /[^b]ee.+?/");
587 // Recreate initial state
588 KorAP.vc = temp;
589 document.body.removeChild(f);
590 });
Akronfec66a32020-08-28 13:01:14 +0200591 });
Akronb43c8c62018-07-04 18:27:28 +0200592});