Debian Codegraph @

tests/auto/qml/data/tst_webchannelseparation.qml

raw
1 // Copyright (C) 2016 basysKom GmbH, [email protected], author Lutz Schönemann <[email protected]>
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3 import QtQuick
4 import QtTest
5
6 import QtWebChannel
7 import QtWebChannel.Tests
8 import "qrc:///qtwebchannel/qwebchannel.js" as JSClient
9
10 TestCase {
11 name: "WebChannelSeparation"
12
13 Client {
14 id: client1
15 objectName: "client1"
16 }
17 Client {
18 id: client2
19 objectName: "client2"
20 }
21
22 property var lastMethodArg
23
24 QtObject {
25 id: myObj
26 property int myProperty: 1
27
28 signal mySignal(var arg)
29
30 function myMethod(arg)
31 {
32 lastMethodArg = arg;
33 }
34
35 WebChannel.id: "myObj"
36 }
37 QtObject {
38 id: myOtherObj
39 property var foo: 1
40 property var bar: 1
41 WebChannel.id: "myOtherObj"
42 }
43 QtObject {
44 id: myObj2
45 function myMethod()
46 {
47 // return a javascript object which is handled as an Object, but not a QObject
48 return { "obj1": { "name": "n1", "value": 1 }, "obj2" : {"name": "n2", "value": 2} };
49 }
50
51 WebChannel.id: "myObj2"
52 }
53
54 QtObject {
55 id: myObj3
56
57 // always returns the same object
58 function getObject()
59 {
60 return myObj;
61 }
62 WebChannel.id: "myObj3"
63 }
64
65 property var lastFactoryObj
66 property var createdFactoryObjects: []
67 QtObject {
68 id: myFactory
69
70 function cleanup()
71 {
72 while (createdFactoryObjects.length) {
73 var obj = createdFactoryObjects.shift();
74 if (obj) {
75 obj.destroy();
76 }
77 }
78 }
79
80 function create(id)
81 {
82 lastFactoryObj = component.createObject(myFactory, {objectName: id});
83 createdFactoryObjects.push(lastFactoryObj);
84 return lastFactoryObj;
85 }
86 WebChannel.id: "myFactory"
87 }
88
89 Component {
90 id: component
91 QtObject {
92 property var myProperty : 0
93 function myMethod(arg)
94 {
95 lastMethodArg = arg;
96 }
97 signal mySignal(var arg1, var arg2)
98 }
99 }
100
101 TestWebChannel {
102 id: webChannel
103 transports: [client1.serverTransport, client2.serverTransport]
104 registeredObjects: [myObj, myOtherObj, myObj2, myObj3, myFactory]
105 }
106
107 function init()
108 {
109 myObj.myProperty = 1
110 client1.cleanup();
111 client2.cleanup();
112 }
113
114 function cleanup()
115 {
116 client1.debug = false;
117 client2.debug = false;
118 // delete all created objects
119 myFactory.cleanup();
120 lastFactoryObj = undefined;
121 createdFactoryObjects = [];
122 // reschedule current task to end of event loop
123 wait(1);
124 }
125
126 function test_signalSeparation()
127 {
128 var testObj1;
129 var testObj1Id;
130 var testObj2;
131 var testObj2Id;
132
133 var channel1 = client1.createChannel(function (channel1) {
134 channel1.objects.myFactory.create("testObj1", function (obj1) {
135 testObj1 = lastFactoryObj;
136 testObj1Id = obj1.__id__;
137
138 obj1.mySignal.connect(function (arg1_1, arg1_2) {
139 console.debug("client 1 received signal 'mySignal' " + arg1_1);
140 });
141
142 // create second channel after factory has created first
143 // object to make sure that a dynamically created object
144 // exists but does not get exposed to new channels
145 createSecondChannel();
146 });
147 });
148
149 var channel2;
150 function createSecondChannel()
151 {
152 // dismiss all messges received before channel creation
153 client2.cleanup();
154
155 channel2 = client2.createChannel(function (channel2) {
156 channel2.objects.myFactory.create("testObj2", function (obj2) {
157 testObj2 = lastFactoryObj;
158 testObj2Id = obj2.__id__;
159 obj2.mySignal.connect(function (arg2_1, arg2_2) {
160 console.debug("client 2 received signal 'mySignal'");
161 });
162 });
163 });
164 }
165
166 client1.awaitInit();
167 client1.skipToMessage(JSClient.QWebChannelMessageTypes.idle);
168
169 client2.awaitInit();
170 client2.skipToMessage(JSClient.QWebChannelMessageTypes.idle);
171
172 // dismiss server messages
173 client1.serverMessages = [];
174 client2.serverMessages = [];
175
176 // now everything is set-up
177 // and we can kick off a signal
178 testObj1.mySignal("foo", "bar");
179
180 var msg1 = client1.awaitSignal();
181 compare(msg1.signal, 5);
182
183 // look if there is a signal send to client2, which should not happen
184 var msg2 = client2.skipToMessage(1, "server", 10);
185 console.log("client2 received a signal. let's check that it does not come from testObj1");
186 if (msg2 !== false) {
187 verify(msg2.object !== testObj1Id);
188 }
189 }
190
191 function test_separationForSameObject()
192 {
193 var testObj1;
194 var testObj2;
195 var receivedSignal1 = false;
196 var receivedSignal2 = false;
197
198 var channel2;
199 var channel1 = client1.createChannel(function (channel1) {
200 channel1.objects.myObj3.getObject(function (obj) {
201 testObj1 = obj;
202
203 testObj1.mySignal.connect(function() {
204 receivedSignal1 = true;
205 });
206
207 // create second channel after factory has created first
208 // object to make sure that a dynamically created object
209 // exists but does not get exposed to new channels
210 createSecondChannel();
211 });
212 });
213
214 function createSecondChannel()
215 {
216 // dismiss all messges received before channel creation
217 client2.cleanup();
218
219 channel2 = client2.createChannel(function (channel2) {
220 verify(channel2.objects.myObj2)
221 channel2.objects.myObj3.getObject(function (obj) {
222 testObj2 = obj;
223 testObj2.mySignal.connect(function() {
224 receivedSignal2 = true;
225 });
226
227 });
228 });
229 }
230
231 client1.awaitInit();
232 client1.skipToMessage(JSClient.QWebChannelMessageTypes.idle);
233
234 client2.awaitInit();
235 client2.skipToMessage(JSClient.QWebChannelMessageTypes.idle);
236
237 // trigger signal, signal should be received by both channels
238 myObj.mySignal("foo", "bar");
239
240 verify(receivedSignal1, "Channel 1 missed signal")
241 verify(receivedSignal2, "Channel 2 missed signal")
242 }
243
244 function test_propertyUpdateSeparation()
245 {
246 var testObj1;
247 var testObj1Id;
248 var testObj2;
249 var testObj2Id;
250
251 var channel1 = client1.createChannel(function (channel1) {
252 channel1.objects.myFactory.create("testObj1", function (obj1) {
253 testObj1 = lastFactoryObj;
254 testObj1Id = obj1.__id__;
255
256 obj1.myPropertyChanged.connect(function (arg1_1) {
257 console.debug("client 1 received property update 'myProperty' " + obj1.myProperty);
258 });
259
260 // create second channel after factory has created first
261 // object to make sure that a dynamically created object
262 // exists but does not get exposed to new channels
263 createSecondChannel();
264 });
265 });
266
267 var channel2;
268 function createSecondChannel()
269 {
270 // dismiss all messges received before channel creation
271 client2.cleanup();
272
273 channel2 = client2.createChannel(function (channel2) {
274 channel2.objects.myFactory.create("testObj2", function (obj2) {
275 testObj2 = lastFactoryObj;
276 testObj2Id = obj2.__id__;
277 obj2.myPropertyChanged.connect(function (arg1_1) {
278 console.debug("client 2 received property update 'myProperty' " + obj2.myProperty);
279 });
280 });
281 });
282 }
283
284 client1.awaitInit();
285 client1.skipToMessage(JSClient.QWebChannelMessageTypes.idle);
286
287 client2.awaitInit();
288 client2.skipToMessage(JSClient.QWebChannelMessageTypes.idle);
289
290 // dismiss server messages
291 client1.serverMessages = [];
292 client2.serverMessages = [];
293
294 // now everything is set-up
295 // and we can kick off a property change
296 testObj1.myProperty = 5;
297
298 var msg1 = client1.awaitPropertyUpdate();
299 compare(msg1.type, JSClient.QWebChannelMessageTypes.propertyUpdate);
300
301 //look if there is a propertyUpdate sent to client2, which should not happen
302 var msg2 = client2.skipToMessage(2, "server", 10);
303 console.log("client2 received a propertyUpdate. let's check that it does not come from testObj1");
304 if (msg2 !== false) {
305 verify(msg2.object !== testObj1Id);
306 }
307 }
308
309 function test_returnNonQObject()
310 {
311 var retObj;
312
313 var channel = client1.createChannel(function (channel) {
314 channel.objects.myObj2.myMethod(function(result) {
315 retObj = result;
316 });
317 });
318
319 client1.awaitInit();
320
321 var msg1 = client1.awaitMessage();
322 compare(msg1.type, JSClient.QWebChannelMessageTypes.invokeMethod); // create
323
324 msg1 = client1.awaitIdle();
325 verify(retObj["obj1"]["name"]);
326 }
327 }
328