Debian Codegraph @

tests/auto/qml/data/tst_webchannel.qml

raw
1 // Copyright (C) 2016 Klarälvdalens Datakonsult AB, a KDAB Group company, [email protected], author Milian Wolff <[email protected]>
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4 import QtQuick
5 import QtTest
6
7 import QtWebChannel
8 import QtWebChannel.Tests
9 import "qrc:///qtwebchannel/qwebchannel.js" as JSClient
10
11 TestCase {
12 name: "WebChannel"
13
14 Client {
15 id: client
16 }
17
18 property var lastMethodArg
19
20 QtObject {
21 id: myObj
22 property int myProperty: 1
23
24 signal mySignal(var arg, QtObject object)
25
26 function myMethod(arg)
27 {
28 lastMethodArg = arg;
29 }
30
31 WebChannel.id: "myObj"
32 }
33 QtObject {
34 id: myOtherObj
35 property var foo: 1
36 property var bar: 1
37 WebChannel.id: "myOtherObj"
38 }
39 QtObject {
40 id: myValueObj
41 property var value: undefined
42 WebChannel.id: "myValueObj"
43 }
44 property var lastFactoryObj
45 QtObject{ id: bar; objectName: "bar" }
46 QtObject{ id: baz; objectName: "baz" }
47 QtObject {
48 id: myFactory
49 function create(id)
50 {
51 lastFactoryObj = component.createObject(myFactory, {objectName: id});
52 return lastFactoryObj;
53 }
54 function switchObject() {
55 otherObject = myOtherObj;
56 }
57 property var objectInProperty: QtObject {
58 objectName: "foo"
59 }
60 property var otherObject: myObj
61 property var objects: [ bar, baz ];
62 WebChannel.id: "myFactory"
63 }
64 Component {
65 id: component
66 QtObject {
67 property var myProperty : 0
68 function myMethod(arg) {
69 lastMethodArg = arg;
70 return myProperty;
71 }
72 signal mySignal(var arg1, var arg2)
73 }
74 }
75
76 TestObject {
77 id: testObject
78 WebChannel.id: "testObject"
79 }
80
81 TestWebChannel {
82 id: webChannel
83 transports: [client.serverTransport]
84 registeredObjects: [myObj, myOtherObj, myValueObj, myFactory, testObject]
85 }
86 WebChannel {
87 id: nullTest
88 }
89
90 function initChannel() {
91 client.serverTransport.receiveMessage(JSON.stringify({type: JSClient.QWebChannelMessageTypes.idle}));
92 webChannel.blockUpdates = true;
93 webChannel.blockUpdates = false;
94 }
95
96 function init()
97 {
98 myObj.myProperty = 1
99 // immediately send pending updates
100 // to avoid property changed signals
101 // during run of test case
102 initChannel();
103 client.cleanup();
104 }
105 function cleanup() {
106 // make tests a bit more strict and predictable
107 // by assuming that a test consumes all messages
108 initChannel();
109 compare(client.clientMessages.length, 0);
110 }
111
112 function test_notifyProperty()
113 {
114 compare(myObj.myProperty, 1);
115
116 var initialValue;
117 var changedValue;
118
119 var channel = client.createChannel(function(channel) {
120 initialValue = channel.objects.myObj.myProperty;
121 channel.objects.myObj.myPropertyChanged.connect(function() {
122 changedValue = channel.objects.myObj.myProperty;
123 });
124 // now trigger a write from the client side
125 channel.objects.myObj.myProperty = 3;
126 });
127
128 client.awaitInit();
129 var msg = client.awaitMessage();
130
131 compare(initialValue, 1);
132 compare(myObj.myProperty, 3);
133
134 client.awaitIdle(); // init
135
136 // change property, should be propagated to HTML client and a message be send there
137 myObj.myProperty = 2;
138 compare(myObj.myProperty, 2);
139 client.awaitIdle(); // property update
140 compare(changedValue, 2);
141 compare(channel.objects.myObj.myProperty, 2)
142 }
143
144 function test_bindableProperty()
145 {
146 compare(testObject.stringProperty, "foo");
147
148 var initialValue;
149
150 var channel = client.createChannel(function(channel) {
151 initialValue = channel.objects.testObject.stringProperty;
152 // now trigger a write from the client side
153 channel.objects.testObject.stringProperty = "bar";
154 });
155
156 client.awaitInit();
157 var msg = client.awaitMessage();
158
159 compare(initialValue, "foo");
160 compare(testObject.stringProperty, "bar");
161
162 client.awaitIdle(); // init
163
164 // Change property, should be propagated to HTML client.
165 // This is a bindable property only. Not change signal will be emitted
166 // because there is none.
167 testObject.stringProperty = "baz";
168 compare(testObject.stringProperty, "baz");
169 client.awaitIdle(); // property update
170 compare(channel.objects.testObject.stringProperty, "baz");
171 }
172
173 function test_method()
174 {
175 var channel = client.createChannel(function (channel) {
176 channel.objects.myObj.myMethod("test");
177 });
178
179 client.awaitInit();
180
181 var msg = client.awaitMessage();
182 compare(msg.type, JSClient.QWebChannelMessageTypes.invokeMethod);
183 compare(msg.object, "myObj");
184 compare(msg.args, ["test"]);
185
186 compare(lastMethodArg, "test")
187
188 client.awaitIdle();
189 }
190
191 function test_signal()
192 {
193 var signalReceivedArg;
194 var signalReceivedObject;
195 var channel = client.createChannel(function(channel) {
196 channel.objects.myObj.mySignal.connect(function(arg, object) {
197 signalReceivedArg = arg;
198 signalReceivedObject = object;
199 });
200 });
201 client.awaitInit();
202
203 var msg = client.awaitMessage();
204 compare(msg.type, JSClient.QWebChannelMessageTypes.connectToSignal);
205 compare(msg.object, "myObj");
206
207 client.awaitIdle(); // initialization
208
209 myObj.mySignal("test", myObj);
210
211 compare(signalReceivedArg, "test");
212 compare(signalReceivedObject.__id__, "myObj");
213
214 var newObj = myFactory.create("newObj");
215 myObj.mySignal(newObj, newObj);
216
217 compare(signalReceivedArg.objectName, newObj.objectName);
218 compare(signalReceivedObject.objectName, newObj.objectName);
219 }
220
221 function test_grouping()
222 {
223 var receivedPropertyUpdates = 0;
224 var properties = 0;
225 var channel = client.createChannel(function(channel) {
226 var originalHandler = channel.handlePropertyUpdate;
227 channel.handlePropertyUpdate = function(message) {
228 originalHandler(message);
229 receivedPropertyUpdates++;
230 properties = [channel.objects.myObj.myProperty, channel.objects.myOtherObj.foo, channel.objects.myOtherObj.bar];
231 };
232 });
233 client.awaitInit();
234 client.awaitIdle();
235
236 // change properties a lot, we expect this to be grouped into a single update notification
237 for (var i = 0; i < 10; ++i) {
238 myObj.myProperty = i;
239 myOtherObj.foo = i;
240 myOtherObj.bar = i;
241 }
242
243 client.awaitIdle();
244 compare(receivedPropertyUpdates, 1);
245 compare(properties, [myObj.myProperty, myOtherObj.foo, myOtherObj.bar]);
246 verify(!client.awaitMessage());
247 }
248
249 function test_wrapper()
250 {
251 var signalArgs;
252 var testObjBeforeDeletion;
253 var testObjAfterDeletion;
254 var testObjId;
255 var testReturn;
256 var channel = client.createChannel(function(channel) {
257 channel.objects.myFactory.create("testObj", function(obj) {
258 testObjId = obj.__id__;
259 compare(channel.objects[testObjId], obj);
260 obj.mySignal.connect(function() {
261 signalArgs = arguments;
262 testObjBeforeDeletion = obj;
263 obj.deleteLater();
264 testObjAfterDeletion = obj;
265 });
266 obj.myProperty = 42;
267 obj.myMethod("foobar").then(function(result) {
268 testReturn = result;
269 });
270 });
271 });
272 client.awaitInit();
273 client.awaitResponse();
274
275 // create testObj
276 var msg = client.awaitMessage();
277 compare(msg.type, JSClient.QWebChannelMessageTypes.invokeMethod);
278 compare(msg.object, "myFactory");
279 client.awaitResponse();
280 verify(lastFactoryObj);
281 compare(lastFactoryObj.objectName, "testObj");
282 compare(channel.objects[testObjId].objectName, "testObj");
283
284 // mySignal connection
285 msg = client.awaitMessage();
286 compare(msg.type, JSClient.QWebChannelMessageTypes.connectToSignal);
287 compare(msg.object, testObjId);
288
289 // set myProperty
290 msg = client.awaitMessage();
291 compare(msg.type, JSClient.QWebChannelMessageTypes.setProperty);
292 compare(msg.object, testObjId);
293 compare(lastFactoryObj.myProperty, 42);
294
295 // call myMethod
296 msg = client.awaitMessage();
297 compare(msg.type, JSClient.QWebChannelMessageTypes.invokeMethod);
298 compare(msg.object, testObjId);
299 compare(msg.args, ["foobar"]);
300 client.awaitResponse();
301 compare(lastMethodArg, "foobar");
302
303 client.awaitIdle();
304
305 // the server should eventually notify the client about the property update
306 client.awaitPropertyUpdate();
307
308 // check that the Promise from myMethod was resolved
309 // must happen after waiting for something so the Promise callback
310 // can execute
311 compare(testReturn, 42);
312
313 client.awaitIdle();
314
315 // property should be wrapped
316 compare(channel.objects.myFactory.objectInProperty.objectName, "foo");
317 // list property as well
318 compare(channel.objects.myFactory.objects.length, 2);
319 compare(channel.objects.myFactory.objects[0].objectName, "bar");
320 compare(channel.objects.myFactory.objects[1].objectName, "baz");
321 // map property as well
322 compare(channel.objects.testObject.objectMap.subObject.objectName,
323 "embedded");
324 // also works with properties that reference other registered objects
325 compare(channel.objects.myFactory.otherObject, channel.objects.myObj);
326
327 // change object property
328 channel.objects.myFactory.switchObject();
329 client.awaitMessage();
330 client.awaitResponse();
331 client.awaitIdle();
332 client.awaitPropertyUpdate();
333 compare(channel.objects.myFactory.otherObject, channel.objects.myOtherObj);
334
335 // trigger a signal and ensure it gets transmitted
336 lastFactoryObj.mySignal("foobar", 42);
337 client.awaitSignal();
338
339 // deleteLater call
340 msg = client.awaitMessage();
341 compare(msg.type, JSClient.QWebChannelMessageTypes.invokeMethod);
342 compare(msg.object, testObjId);
343 client.awaitResponse();
344
345 // now the signalArgs should also be set
346 compare(signalArgs, {"0": "foobar", "1": 42});
347
348 // and also a destroyed signal
349 client.awaitSignal();
350
351 compare(JSON.stringify(testObjBeforeDeletion), JSON.stringify({}));
352 compare(JSON.stringify(testObjAfterDeletion), JSON.stringify({}));
353 compare(typeof channel.objects[testObjId], "undefined");
354 }
355
356 // test if returned QObjects get inserted into list of
357 // objects even if no callback function is set
358 function test_wrapper_wrapEveryQObject()
359 {
360 var testObj;
361 var channel = client.createChannel(function(channel) {
362 channel.objects.myFactory.create("testObj", function(obj) {
363 testObj = obj;
364 });
365 });
366 client.awaitInit();
367 client.awaitResponse();
368
369 // call to myFactory.create()
370 var msg = client.awaitMessage();
371 compare(msg.type, JSClient.QWebChannelMessageTypes.invokeMethod);
372 client.awaitResponse();
373
374 client.awaitIdle();
375
376 verify(testObj);
377 var testObjId = testObj.__id__;
378
379 testObj.deleteLater();
380 msg = client.awaitMessage();
381 compare(msg.type, JSClient.QWebChannelMessageTypes.invokeMethod);
382 compare(msg.object, testObjId);
383 client.awaitResponse();
384 // destroyed signal
385 client.awaitSignal();
386
387 compare(lastFactoryObj, null);
388 compare(typeof channel.objects[testObjId], "undefined");
389 }
390
391 function test_wrapper_propertyUpdateOfWrappedObjects()
392 {
393 var testObj;
394 var testObjId;
395 var channel = client.createChannel(function(channel) {
396 channel.objects.myFactory.create("testObj", function(obj) {
397 testObj = lastFactoryObj;
398 testObjId = obj.__id__;
399 });
400 });
401 client.awaitInit();
402 client.awaitResponse();
403
404 // call to myFactory.create()
405 var msg = client.awaitMessage();
406 compare(msg.type, JSClient.QWebChannelMessageTypes.invokeMethod);
407 client.awaitResponse();
408
409 client.awaitIdle();
410
411 testObj.myProperty = 42;
412 client.awaitPropertyUpdate();
413 client.awaitIdle();
414 compare(channel.objects[testObjId].myProperty, 42);
415
416 channel.objects[testObjId].deleteLater();
417 msg = client.awaitMessage();
418 compare(msg.type, JSClient.QWebChannelMessageTypes.invokeMethod);
419 client.awaitResponse();
420 // destroyed signal
421 client.awaitSignal();
422 }
423
424 function test_disconnect()
425 {
426 var signalArg;
427 var channel = client.createChannel(function(channel) {
428 channel.objects.myObj.mySignal.connect(function(arg) {
429 signalArg = arg;
430 channel.objects.myObj.mySignal.disconnect(this);
431 });
432 });
433 client.awaitInit();
434
435 var msg = client.awaitMessage();
436 compare(msg.type, JSClient.QWebChannelMessageTypes.connectToSignal);
437 compare(msg.object, "myObj");
438
439 client.awaitIdle();
440
441 myObj.mySignal(42, myObj);
442 compare(signalArg, 42);
443
444 msg = client.awaitMessage();
445 compare(msg.type, JSClient.QWebChannelMessageTypes.disconnectFromSignal);
446 compare(msg.object, "myObj");
447
448 myObj.mySignal(0, myObj);
449 compare(signalArg, 42);
450 }
451
452 // see also: https://bugreports.qt.io/browse/QTBUG-54074
453 function test_signalArgumentTypeConversion()
454 {
455 var signalArgs = [];
456 function logSignalArgs(arg) {
457 signalArgs.push(arg);
458 }
459 var channel = client.createChannel(function(channel) {
460 var testObject = channel.objects.testObject;
461 testObject.testSignalBool.connect(logSignalArgs);
462 testObject.testSignalInt.connect(logSignalArgs);
463 testObject.triggerSignals();
464 });
465 client.awaitInit();
466
467 var msg = client.awaitMessage();
468 compare(msg.type, JSClient.QWebChannelMessageTypes.connectToSignal);
469 compare(msg.object, "testObject");
470
471 msg = client.awaitMessage();
472 compare(msg.type, JSClient.QWebChannelMessageTypes.connectToSignal);
473 compare(msg.object, "testObject");
474
475 msg = client.awaitMessage();
476 compare(msg.type, JSClient.QWebChannelMessageTypes.invokeMethod);
477 client.awaitIdle();
478
479 compare(signalArgs, [
480 true,
481 false,
482 42,
483 1,
484 0
485 ]);
486 }
487
488 function test_multiConnect()
489 {
490 var signalArgs = [];
491 function logSignalArgs(arg) {
492 signalArgs.push(arg);
493 }
494 var channel = client.createChannel(function(channel) {
495 var testObject = channel.objects.testObject;
496 testObject.testSignalInt.connect(logSignalArgs);
497 testObject.testSignalInt.connect(logSignalArgs);
498 testObject.triggerSignals();
499 });
500 client.awaitInit();
501
502 var msg = client.awaitMessage();
503 compare(msg.type, JSClient.QWebChannelMessageTypes.connectToSignal);
504 compare(msg.object, "testObject");
505
506 msg = client.awaitMessage();
507 compare(msg.type, JSClient.QWebChannelMessageTypes.invokeMethod);
508 client.awaitIdle();
509
510 compare(signalArgs, [42, 42, 1, 1, 0, 0]);
511 }
512
513 function test_connectDuringEmit()
514 {
515 var cb1 = 0;
516 var cb2 = 0;
517 var channel = client.createChannel(function(channel) {
518 var myObj = channel.objects.myObj;
519 myObj.mySignal.connect(function() {
520 cb1++;
521 myObj.mySignal.connect(function() {
522 cb2++;
523 });
524 });
525 });
526 client.awaitInit();
527
528 var msg = client.awaitMessage();
529 compare(msg.type, JSClient.QWebChannelMessageTypes.connectToSignal);
530 compare(msg.object, "myObj");
531
532 client.awaitIdle();
533
534 myObj.mySignal(42, myObj);
535
536 compare(cb1, 1);
537 compare(cb2, 0);
538 }
539
540 function test_disconnectDuringEmit()
541 {
542 var cb1 = 0;
543 var cb2 = 0;
544 var cb3 = 0;
545 var channel = client.createChannel(function(channel) {
546 var myObj = channel.objects.myObj;
547 var cb1impl = function() {
548 cb1++;
549 };
550 myObj.mySignal.connect(cb1impl);
551 myObj.mySignal.connect(function() {
552 cb2++;
553 myObj.mySignal.disconnect(cb1impl);
554 });
555 myObj.mySignal.connect(function() {
556 cb3++;
557 });
558 });
559 client.awaitInit();
560
561 var msg = client.awaitMessage();
562 compare(msg.type, JSClient.QWebChannelMessageTypes.connectToSignal);
563 compare(msg.object, "myObj");
564
565 client.awaitIdle();
566
567 myObj.mySignal(42, myObj);
568
569 compare(cb1, 1);
570 compare(cb2, 1);
571 compare(cb3, 1);
572 }
573
574 function test_overloading()
575 {
576 var signalArgs_implicit = [];
577 var signalArgs_explicit1 = [];
578 var signalArgs_explicit2 = [];
579 var signalArgs_explicit3 = [];
580 function logSignalArgs(container) {
581 return function(...args) {
582 container.push(args);
583 };
584 }
585 var returnValues = [];
586 function logReturnValue(value) {
587 returnValues.push(value);
588 }
589 var channel = client.createChannel(function(channel) {
590 var testObject = channel.objects.testObject;
591 testObject.testOverloadSignal.connect(logSignalArgs(signalArgs_implicit));
592 testObject["testOverloadSignal(int)"].connect(logSignalArgs(signalArgs_explicit1));
593 testObject["testOverloadSignal(QString)"].connect(logSignalArgs(signalArgs_explicit2));
594 testObject["testOverloadSignal(QString,int)"].connect(logSignalArgs(signalArgs_explicit3));
595 testObject.testOverload(99, logReturnValue);
596 testObject["testOverload(int)"](41, logReturnValue);
597 testObject["testOverload(QString)"]("hello world", logReturnValue);
598 testObject["testOverload(QString,int)"]("the answer is ", 41, logReturnValue);
599 });
600 client.awaitInit();
601
602 function awaitMessage(type)
603 {
604 var msg = client.awaitMessage();
605 compare(msg.type, type);
606 compare(msg.object, "testObject");
607 }
608
609 console.log("sig1");
610 awaitMessage(JSClient.QWebChannelMessageTypes.connectToSignal);
611 console.log("sig2");
612 awaitMessage(JSClient.QWebChannelMessageTypes.connectToSignal);
613 console.log("sig3");
614 awaitMessage(JSClient.QWebChannelMessageTypes.connectToSignal);
615
616 console.log("method1");
617 awaitMessage(JSClient.QWebChannelMessageTypes.invokeMethod);
618 console.log("method2");
619 awaitMessage(JSClient.QWebChannelMessageTypes.invokeMethod);
620 console.log("method3");
621 awaitMessage(JSClient.QWebChannelMessageTypes.invokeMethod);
622 console.log("method4");
623 awaitMessage(JSClient.QWebChannelMessageTypes.invokeMethod);
624
625 client.awaitIdle();
626
627 compare(signalArgs_implicit, [[99], [41]]);
628 compare(signalArgs_explicit1, signalArgs_implicit);
629 compare(signalArgs_explicit2, [["hello world"]]);
630 compare(signalArgs_explicit3, [["the answer is ", 41]]);
631 compare(returnValues, [100, 42, "HELLO WORLD", "THE ANSWER IS 42"]);
632 }
633
634 function test_variantType()
635 {
636 var returnValues = [];
637 function logReturnValue(value) {
638 returnValues.push(value);
639 }
640 var channel = client.createChannel(function(channel) {
641 var testObject = channel.objects.testObject;
642 testObject.testVariantType(0.25, logReturnValue);
643 testObject.testVariantType("0", logReturnValue);
644 testObject.testVariantType(null, logReturnValue);
645 testObject.testVariantType(testObject, logReturnValue);
646 });
647 client.awaitInit();
648
649 function awaitMessage(type)
650 {
651 var msg = client.awaitMessage();
652 compare(msg.type, type);
653 compare(msg.object, "testObject");
654 }
655
656 console.log("double arg");
657 awaitMessage(JSClient.QWebChannelMessageTypes.invokeMethod);
658 console.log("string arg");
659 awaitMessage(JSClient.QWebChannelMessageTypes.invokeMethod);
660 console.log("null arg");
661 awaitMessage(JSClient.QWebChannelMessageTypes.invokeMethod);
662 console.log("QObject arg");
663 awaitMessage(JSClient.QWebChannelMessageTypes.invokeMethod);
664
665 client.awaitIdle();
666
667 // QMetaType::Double: 6, QMetaType::QString: 10, QMetaType::Nullptr: 51,
668 // QMetaType::QObjectStar: 39
669 compare(returnValues, [6, 10, 51, 39]);
670 }
671
672 function test_embeddedQObject()
673 {
674 var success = false;
675 function logReturnValue(value) {
676 success = value;
677 }
678 var channel = client.createChannel(function(channel) {
679 var testObject = channel.objects.testObject;
680 testObject.testEmbeddedObjects([testObject, { obj: testObject }], logReturnValue);
681 });
682 client.awaitInit();
683
684 function awaitMessage(type)
685 {
686 var msg = client.awaitMessage();
687 compare(msg.type, type);
688 compare(msg.object, "testObject");
689 }
690
691 awaitMessage(JSClient.QWebChannelMessageTypes.invokeMethod);
692
693 client.awaitIdle();
694
695 compare(success, true);
696 }
697
698 function test_customUpcaseWrapper()
699 {
700 var channel = client.createChannel(function(channel) {
701 channel.objects.testObject.stringProperty = "foo";
702 }, function(arg) { return (typeof arg === "string") ? arg.toUpperCase() : undefined });
703
704 client.awaitInit();
705 function awaitMessage(type)
706 {
707 var msg = client.awaitMessage();
708 compare(msg.type, type);
709 compare(msg.object, "testObject");
710 }
711 awaitMessage(JSClient.QWebChannelMessageTypes.setProperty);
712 compare(testObject.stringProperty, "foo"); // Don't convert in this direction
713 client.awaitIdle(); // init
714
715 testObject.stringProperty = "bar";
716 compare(testObject.stringProperty, "bar");
717 client.awaitIdle(); // property update
718 compare(channel.objects.testObject.stringProperty, "BAR"); // Case converted
719 }
720
721 function test_dateWrapper()
722 {
723 var channel = client.createChannel(undefined, "Date");
724 client.awaitInit();
725 client.awaitIdle();
726
727 var dateString = "2022-01-01T10:00:00Z";
728 myValueObj.value = dateString;
729 compare(myValueObj.value, dateString);
730 client.awaitIdle(); // property update
731 var value = channel.objects.myValueObj.value;
732 verify(value instanceof Date);
733 verify(!isNaN(value));
734 compare(value.getUTCFullYear(), 2022);
735 compare(value.getUTCMonth(), 0); // 0 = January
736 compare(value.getUTCDate(), 1);
737 compare(value.getUTCHours(), 10);
738 compare(value.getUTCMinutes(), 0);
739 compare(value.getUTCSeconds(), 0);
740
741 var invalidDate = "2022-13-31T10:00:00Z"; // Month after december
742 myValueObj.value = invalidDate;
743 compare(myValueObj.value, invalidDate);
744 client.awaitIdle(); // property update
745 value = channel.objects.myValueObj.value;
746 verify(typeof value === "string"); // Not converted to Date
747 compare(value, invalidDate);
748 }
749
750 function test_registerNull()
751 {
752 nullTest.registeredObjects = [null]
753 compare(nullTest.registeredObjects, [])
754 }
755
756 function test_noContext()
757 {
758 nullTest.registeredObjects = [testObject.createQObject()]
759 compare(nullTest.registeredObjects, [])
760 }
761 }