Debian Codegraph @

tests/auto/qml/data/tst_multiclient.qml

raw
1 // Copyright (C) 2016 Klarälvdalens Datakonsult AB, a KDAB Group company, [email protected], author Milian Wolff <[email protected]>
2 // Copyright (C) 2016 basysKom GmbH, [email protected], author Lutz Schönemann <[email protected]>
3 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
4
5 import QtQuick
6 import QtTest
7
8 import QtWebChannel
9 import QtWebChannel.Tests
10
11 import "qrc:///qtwebchannel/qwebchannel.js" as JSClient
12
13 TestCase {
14 name: "MultiClient"
15
16 Client {
17 id: client1
18 }
19
20 Client {
21 id: client2
22 }
23
24 property int bar: 0
25 QtObject {
26 id: foo
27
28 signal ping()
29
30 function pong()
31 {
32 return ++bar;
33 }
34
35 WebChannel.id: "foo"
36 }
37
38 property var lastMethodArg
39 QtObject {
40 id: myObj
41 property int myProperty: 1
42
43 signal mySignal(var arg)
44
45 function myMethod(arg)
46 {
47 lastMethodArg = arg;
48 }
49
50 WebChannel.id: "myObj"
51 }
52
53 QtObject {
54 id: myOtherObj
55 property var foo: 1
56 property var bar: 1
57 WebChannel.id: "myOtherObj"
58 }
59
60 property var lastFactoryObj
61 property var createdFactoryObjects: []
62 QtObject {
63 id: myFactory
64
65 function cleanup() {
66 while (createdFactoryObjects.length) {
67 var obj = createdFactoryObjects.shift();
68 if (obj) {
69 obj.destroy();
70 }
71 }
72 }
73
74 function create(id)
75 {
76 lastFactoryObj = component.createObject(myFactory, {objectName: id});
77 createdFactoryObjects.push(lastFactoryObj);
78 return lastFactoryObj;
79 }
80 WebChannel.id: "myFactory"
81 }
82
83 Component {
84 id: component
85 QtObject {
86 property var myProperty : 0
87 function myMethod(arg) {
88 lastMethodArg = arg;
89 }
90 signal mySignal(var arg1, var arg2)
91 }
92 }
93
94 TestWebChannel {
95 id: webChannel
96 transports: [client1.serverTransport, client2.serverTransport]
97 registeredObjects: [foo, myObj, myOtherObj, myFactory]
98 }
99
100 function init()
101 {
102 myObj.myProperty = 1
103 client1.cleanup();
104 client2.cleanup();
105 }
106
107 function cleanup() {
108 client1.debug = false;
109 client2.debug = false;
110 // delete all created objects
111 myFactory.cleanup();
112 lastFactoryObj = undefined;
113 createdFactoryObjects = [];
114 // reschedule current task to end of event loop
115 wait(1);
116 }
117
118 function clientInitCallback(channel)
119 {
120 channel.objects.foo.ping.connect(function() {
121 channel.objects.foo.pong(function(value) {
122 channel.pongAnswer = value;
123 });
124 });
125 }
126
127 function test_multiclient()
128 {
129 var c1 = client1.createChannel(clientInitCallback);
130 var c2 = client2.createChannel(clientInitCallback);
131
132 // init, connect & idle messages for two clients
133 for (var i = 0; i < 3; ++i) {
134 client1.awaitMessage();
135 client2.awaitMessage();
136 }
137
138 foo.ping();
139
140 // invoke of pong method
141 client1.awaitMessage();
142 client2.awaitMessage();
143
144 compare(c1.pongAnswer, 1);
145 compare(c2.pongAnswer, 2);
146 }
147
148 function test_autowrappedObjectsNotInInit() {
149 var testObj1;
150 var testObj1Id;
151
152 var channel1 = client1.createChannel(function (channel1) {
153 channel1.objects.myFactory.create("testObj1", function (obj1) {
154 testObj1 = lastFactoryObj;
155 testObj1Id = obj1.__id__;
156
157 // create second channel after factory has created first
158 // object to make sure that a dynamically created object
159 // exists but does not get exposed to new channels
160 createSecondChannel();
161 });
162 });
163 var channel2;
164 function createSecondChannel() {
165 // dismiss all messges received before channel creation
166 client2.cleanup();
167
168 channel2 = client2.createChannel(function (channel2) {
169 });
170 }
171
172 client1.awaitInit();
173 var msg1 = client1.awaitMessage();
174 compare(msg1.type, JSClient.QWebChannelMessageTypes.invokeMethod); // create
175
176 client1.awaitIdle();
177
178 client2.awaitInit();
179 client2.awaitIdle();
180
181 compare(typeof channel2.objects[testObj1Id], "undefined")
182 }
183
184 function test_autowrappedObjectsNotBroadcasted() {
185 var testObj2;
186 var testObj2Id;
187
188 var channel1 = client1.createChannel(function (channel1) {
189 // create second channel after first channel to make sure
190 // that a dynamically created object do not get exposed to
191 // existing channels
192 createSecondChannel();
193 });
194 var channel2;
195 function createSecondChannel() {
196 // dismiss all messges received before channel creation
197 client2.cleanup();
198
199 channel2 = client2.createChannel(function (channel2) {
200 channel2.objects.myFactory.create("testObj2", function (obj2) {
201 testObj2 = lastFactoryObj;
202 testObj2Id = obj2.__id__;
203 });
204 });
205 }
206
207 client1.awaitInit();
208 client1.awaitIdle();
209
210 client2.awaitInit();
211 var msg2 = client2.awaitMessage();
212 compare(msg2.type, JSClient.QWebChannelMessageTypes.invokeMethod); // create
213
214 client2.awaitIdle();
215
216 compare(typeof channel1.objects[testObj2Id], "undefined")
217 }
218 }