blob: 5cd7ce5a17e3809dbe3850a25f49e68ea2b138ea [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);
Akronba09ed22020-10-01 16:01:45 +0200240
241 manager.destroy();
242
243 KorAP.Panel["result"] = undefined;
244 });
Akronb43c8c62018-07-04 18:27:28 +0200245 });
Akron2d0d96d2019-11-18 19:49:50 +0100246
Akronb43c8c62018-07-04 18:27:28 +0200247 describe('KorAP.Plugin.Widget', function () {
248 it('should be initializable', function () {
Akron22598cd2019-12-09 14:59:03 +0100249 expect(function () { widgetClass.create() }).toThrow(new Error("Service not well defined"));
Akron56a11af2018-07-27 18:28:45 +0200250
251 widget = widgetClass.create("Test", "https://example", 56);
Akronb43c8c62018-07-04 18:27:28 +0200252 expect(widget).toBeTruthy();
Akron56a11af2018-07-27 18:28:45 +0200253 expect(widget.id).toEqual(56);
254 expect(widget.name).toEqual("Test");
255 expect(widget.src).toEqual("https://example");
256 });
257
258 it('should create a view element', function () {
259 var widget = widgetClass.create("Test", "https://example", 56);
260 var we = widget.element();
261
262 expect(we.tagName).toEqual("DIV");
263 expect(we.classList.contains('view')).toBeTruthy();
264 expect(we.classList.contains('widget')).toBeTruthy();
265
266 var iframe = we.firstChild;
267 expect(iframe.tagName).toEqual("IFRAME");
hebasta78913242020-03-30 13:39:20 +0200268 expect(iframe.getAttribute("sandbox")).toEqual("allow-scripts allow-forms");
Akron56a11af2018-07-27 18:28:45 +0200269 expect(iframe.getAttribute("src")).toEqual("https://example");
270 expect(iframe.getAttribute("name")).toEqual("56");
271
272 var btn = we.lastChild;
273 expect(btn.classList.contains("button-group")).toBeTruthy();
274 expect(btn.classList.contains("button-view")).toBeTruthy();
275 expect(btn.classList.contains("widget")).toBeTruthy();
276
277 expect(btn.firstChild.tagName).toEqual("SPAN");
278 expect(btn.firstChild.classList.contains("button-icon")).toBeTruthy();
279 expect(btn.firstChild.classList.contains("close")).toBeTruthy();
280 expect(btn.firstChild.firstChild.tagName).toEqual("SPAN");
281
282 expect(btn.lastChild.tagName).toEqual("SPAN");
283 expect(btn.lastChild.classList.contains("button-icon")).toBeTruthy();
284 expect(btn.lastChild.classList.contains("plugin")).toBeTruthy();
285 expect(btn.lastChild.firstChild.tagName).toEqual("SPAN");
286 expect(btn.lastChild.textContent).toEqual("Test");
287 })
288
289 it('should be resizable', function () {
290 var widget = widgetClass.create("Test", "https://example", 56);
291 var iframe = widget.show();
292 expect(iframe.style.height).toEqual('0px');
293 widget.resize({ height : 9 });
294 expect(iframe.style.height).toEqual('9px');
Akronb43c8c62018-07-04 18:27:28 +0200295 });
Akron3d9ce5e2020-10-01 15:18:36 +0200296
297 it('should be minimizable', function () {
298 var widget = widgetClass.create("Test", "https://example", 56);
299 var we = widget.element();
300 expect(we.classList.contains('show')).toBeTruthy();
301 widget.minimize();
302 expect(we.classList.contains('show')).toBeFalsy();
303 });
Akronb43c8c62018-07-04 18:27:28 +0200304 });
Akron2d0d96d2019-11-18 19:49:50 +0100305
Akron22598cd2019-12-09 14:59:03 +0100306 describe('KorAP.Plugin.Service', function () {
307 it('should be initializable', function () {
308 expect(function () { serviceClass.create() }).toThrow(new Error("Service not well defined"));
309
310 let service = serviceClass.create("Test", "https://example", 56);
311 expect(service).toBeTruthy();
312 expect(service.id).toEqual(56);
313 expect(service.name).toEqual("Test");
314 expect(service.src).toEqual("https://example");
315 });
316
317 it('should be loadable', function () {
318 let service = serviceClass.create("Test", "https://example", 56);
319 expect(service).toBeTruthy();
320
321 let i = service.load();
322 expect(i.tagName).toEqual("IFRAME");
323 expect(i.getAttribute("allowTransparency")).toEqual("true");
324 expect(i.getAttribute("frameborder")).toEqual(''+0);
325 expect(i.getAttribute("name")).toEqual(''+service.id);
326 expect(i.getAttribute("src")).toEqual(service.src);
327 });
328 });
329
Akron2d0d96d2019-11-18 19:49:50 +0100330 describe('KorAP.Plugin.QueryPanel', function () {
331 it('should establish a query plugin', function () {
332 var queryPanel = queryPanelClass.create();
333
334 var div = document.createElement('div');
335
336 div.appendChild(queryPanel.element());
337 KorAP.Panel = KorAP.Panel || {};
338 KorAP.Panel['query'] = queryPanel;
339
340 // Register plugin afterwards
341 var manager = pluginServerClass.create();
342
343 manager.register({
344 name : 'Check',
345 embed : [{
346 panel : 'query',
347 title : 'Translate',
348 onClick : {
349 template : 'test'
350 }
351 }]
352 });
353
354 expect(manager.buttonGroup('query').length).toEqual(0);
355
356 // Clean up
357 KorAP.Panel['query'] = undefined;
358 manager.destroy();
359 });
hebastaf6adf8d2019-11-26 14:04:10 +0100360
361 it('Plugin buttons should be cleared after adding to panel', function () {
362
363 // Register plugin first
364 KorAP.Plugin = pluginServerClass.create();
365
366 KorAP.Plugin.register({
367 name : 'Check',
368 embed : [{
369 panel : 'query',
370 title : 'Translate',
371 onClick : {
372 template : 'test'
373 }
374 }]
375 });
376
377
378 var queryPanel = queryPanelClass.create();
379 var div = document.createElement('div');
380
381 div.appendChild(queryPanel.element());
382 KorAP.Panel = KorAP.Panel || {};
383 KorAP.Panel['query'] = queryPanel;
384 expect(KorAP.Plugin.buttonGroup('query').length).toEqual(0);
385
386 // Clean up
387 KorAP.Panel['query'] = undefined;
388 KorAP.Plugin.destroy();
389 KorAP.Plugin = undefined;
390 });
Akron2d0d96d2019-11-18 19:49:50 +0100391 });
hebasta043e96f2019-11-28 12:33:00 +0100392
Akron45308ce2020-08-28 14:10:23 +0200393 describe('KorAP.Plugin.ResultPanel', function () {
hebasta043e96f2019-11-28 12:33:00 +0100394 it('Plugin is registered second: buttons should be added to panel', function () {
395
396 var resultPanel = resultPanelClass.create();
397 resultPanel.addAlignAction();
398 var div = document.createElement('div');
399
400 div.appendChild(resultPanel.element());
401 KorAP.Panel = KorAP.Panel || {};
402 KorAP.Panel['result'] = resultPanel;
403
404 // Register plugin afterwards
405 var manager = pluginServerClass.create();
406
407 manager.register({
408 name : 'ResultPlugin',
409 embed : [{
410 panel : 'result',
411 title : 'Dosomething',
412 onClick : {
413 template : 'test'
414 }
415 }]
416 });
417
418 expect(manager.buttonGroup('result').length).toEqual(0);
419 expect(KorAP.Panel['result'].actions.element().innerHTML).toContain('Dosomething');
420
421 // Clean up
422 KorAP.Panel['result'] = undefined;
423 manager.destroy();
Akron45308ce2020-08-28 14:10:23 +0200424 });
hebasta043e96f2019-11-28 12:33:00 +0100425
426 it('Plugin is registered first: Buttons should be added to panel and cleared', function () {
427
428 // Register plugin first
429 KorAP.Plugin = pluginServerClass.create();
430
431 KorAP.Plugin.register({
432 name : 'ResultPlugin',
433 embed : [{
434 panel : 'result',
435 title : 'Dosomething',
436 onClick : {
437 template : 'test'
438 }
439 }]
440 });
441
442 expect(KorAP.Plugin.buttonGroup('result').length).toEqual(1);
443
444 var resultPanel = resultPanelClass.create();
445 var div = document.createElement('div');
446 div.appendChild(resultPanel.element());
447 KorAP.Panel = KorAP.Panel || {};
448 KorAP.Panel['result'] = resultPanel;
449 expect(KorAP.Plugin.buttonGroup('result').length).toEqual(0);
450 expect(KorAP.Panel['result'].actions.element().innerHTML).toContain('Dosomething');
451
452 // Clean up
453 KorAP.Panel['result'] = undefined;
454 KorAP.Plugin.destroy();
455 KorAP.Plugin = undefined;
456 });
457 });
Akronfec66a32020-08-28 13:01:14 +0200458
459 describe('KorAP.Plugin communications', function () {
460 it('should receive messages', function () {
461 var manager = pluginServerClass.create();
Akronfec66a32020-08-28 13:01:14 +0200462 var id = manager.addService('Example 1', 'about:blank');
463 expect(id).toMatch(/^id-/);
Akronfec66a32020-08-28 13:01:14 +0200464 var temp = KorAP.koralQuery;
465 KorAP.koralQuery = { "@type" : "koral:test" };
Akronfec66a32020-08-28 13:01:14 +0200466 let data = {
467 "originID" : id,
468 "action" : "get",
469 "key" : "KQ"
470 };
Akronfec66a32020-08-28 13:01:14 +0200471 manager._receiveMsg({
472 "data" : data
473 });
Akronfec66a32020-08-28 13:01:14 +0200474 manager.destroy();
Akronfec66a32020-08-28 13:01:14 +0200475 expect(data.value["@type"]).toEqual("koral:test");
Akronfec66a32020-08-28 13:01:14 +0200476 // Recreate initial state
477 KorAP.koralQuery = temp;
478 });
479
Akronfec66a32020-08-28 13:01:14 +0200480 it('should add to pipe', function () {
481 var manager = pluginServerClass.create();
Akronfec66a32020-08-28 13:01:14 +0200482 var temp = KorAP.Pipe;
483 KorAP.Pipe = pipeClass.create();
Akronfec66a32020-08-28 13:01:14 +0200484 expect(KorAP.Pipe.toString()).toEqual("");
485
486 var id = manager.addService('Example 2', 'about:blank');
487 expect(id).toMatch(/^id-/);
Akronfec66a32020-08-28 13:01:14 +0200488 manager._receiveMsg({
489 "data" : {
490 "originID" : id,
491 "action" : "pipe",
492 "job" : "add",
493 "service" : "https://pipe-service.de"
494 }
495 });
Akronfec66a32020-08-28 13:01:14 +0200496 expect(KorAP.Pipe.toString()).toEqual("https://pipe-service.de");
Akronfec66a32020-08-28 13:01:14 +0200497 manager._receiveMsg({
498 "data" : {
499 "originID" : id,
500 "action" : "pipe",
501 "job" : "add",
502 "service" : "https://pipe-service-2.de"
503 }
504 });
Akronfec66a32020-08-28 13:01:14 +0200505 expect(KorAP.Pipe.toString()).toEqual("https://pipe-service.de,https://pipe-service-2.de");
Akronfec66a32020-08-28 13:01:14 +0200506 manager._receiveMsg({
507 "data" : {
508 "originID" : id,
509 "action" : "pipe",
510 "job" : "del",
511 "service" : "https://pipe-service.de"
512 }
513 });
Akronfec66a32020-08-28 13:01:14 +0200514 expect(KorAP.Pipe.toString()).toEqual("https://pipe-service-2.de");
Akronfec66a32020-08-28 13:01:14 +0200515 manager.destroy();
Akronfec66a32020-08-28 13:01:14 +0200516 // Recreate initial state
517 KorAP.Pipe = temp;
518 });
Akron45308ce2020-08-28 14:10:23 +0200519
520 it('should reply to query information requests', function () {
521 var manager = pluginServerClass.create();
522 var id = manager.addService('Service', 'about:blank');
523 expect(id).toMatch(/^id-/);
524 var temp = KorAP.vc;
525 // Create form for query form information
526 let f = document.createElement('form');
527 var qfield = f.addE('input');
528 qfield.setAttribute("id", "q-field");
529 qfield.value = "[orth=Baum]";
530 var qlfield = f.addE('select');
531 qlfield.setAttribute("id", "ql-field");
532 qlfield.addE('option').setAttribute('value', 'cosmas-2');
533 qlfield.addE('option').setAttribute('value', 'poliqarp');
534 qlfield.selectedIndex = 1;
535
536 KorAP.vc = vcClass.create().fromJson({
537 "key" : "title",
538 "type" : "type:regex",
539 "value" : "[^b]ee.+?",
540 "@type" : "koral:doc"
541 });
542 // console.log(KorAP.vc.toQuery());
543
544 document.body.appendChild(f);
545 let data = {
546 "originID" : id,
547 "action" : "get",
548 "key" : "QueryForm"
549 };
550 manager._receiveMsg({
551 "data" : data
552 });
553 manager.destroy();
554 expect(data.value["q"]).toEqual("[orth=Baum]");
555 expect(data.value["ql"]).toEqual("poliqarp");
556 expect(data.value["cq"]).toEqual("title = /[^b]ee.+?/");
557 // Recreate initial state
558 KorAP.vc = temp;
559 document.body.removeChild(f);
560 });
Akronfec66a32020-08-28 13:01:14 +0200561 });
Akronb43c8c62018-07-04 18:27:28 +0200562});