summaryrefslogtreecommitdiff
path: root/tests/auto/qml/tst_webchannelseparation.qml
blob: 647f162fb2d8addcc1c11f5380b0f69142cf0839 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/****************************************************************************
**
** Copyright (C) 2014 basysKom GmbH, info@basyskom.com, author Lutz Schönemann <lutz.schoenemann@basyskom.com>
** Contact: http://www.qt.io/licensing/
**
** This file is part of the QtWebChannel module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL21$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
**
****************************************************************************/
import QtQuick 2.0
import QtTest 1.0

import QtWebChannel 1.0
import QtWebChannel.Tests 1.0
import "qrc:///qtwebchannel/qwebchannel.js" as JSClient

TestCase {
    name: "WebChannelSeparation"

    Client {
        id: client1
        objectName: "client1"
    }
    Client {
        id: client2
        objectName: "client2"
    }

    property var lastMethodArg

    QtObject {
        id: myObj
        property int myProperty: 1

        signal mySignal(var arg)

        function myMethod(arg)
        {
            lastMethodArg = arg;
        }

        WebChannel.id: "myObj"
    }
    QtObject {
        id: myOtherObj
        property var foo: 1
        property var bar: 1
        WebChannel.id: "myOtherObj"
    }
    QtObject {
        id: myObj2
        function myMethod()
        {
            // return a javascript object which is handled as an Object, but not a QObject
            return { "obj1": { "name": "n1", "value": 1 }, "obj2" : {"name": "n2", "value": 2} };
        }

        WebChannel.id: "myObj2"
    }

    QtObject {
        id: myObj3

        // always returns the same object
        function getObject()
        {
            return myObj;
        }
        WebChannel.id: "myObj3"
    }

    property var lastFactoryObj
    property var createdFactoryObjects: []
    QtObject {
        id: myFactory

        function cleanup()
        {
            while (createdFactoryObjects.length) {
                var obj = createdFactoryObjects.shift();
                if (obj) {
                    obj.destroy();
                }
            }
        }

        function create(id)
        {
            lastFactoryObj = component.createObject(myFactory, {objectName: id});
            createdFactoryObjects.push(lastFactoryObj);
            return lastFactoryObj;
        }
        WebChannel.id: "myFactory"
    }

    Component {
        id: component
        QtObject {
            property var myProperty : 0
            function myMethod(arg)
            {
                lastMethodArg = arg;
            }
            signal mySignal(var arg1, var arg2)
        }
    }

    TestWebChannel {
        id: webChannel
        transports: [client1.serverTransport, client2.serverTransport]
        registeredObjects: [myObj, myOtherObj, myObj2, myObj3, myFactory]
    }

    function init()
    {
        myObj.myProperty = 1
        client1.cleanup();
        client2.cleanup();
    }

    function cleanup()
    {
        client1.debug = false;
        client2.debug = false;
        // delete all created objects
        myFactory.cleanup();
        lastFactoryObj = undefined;
        createdFactoryObjects = [];
        // reschedule current task to end of event loop
        wait(1);
    }

    function test_signalSeparation()
    {
        var testObj1;
        var testObj1Id;
        var testObj2;
        var testObj2Id;

        var channel1 = client1.createChannel(function (channel1) {
            channel1.objects.myFactory.create("testObj1", function (obj1) {
                testObj1 = lastFactoryObj;
                testObj1Id = obj1.__id__;

                obj1.mySignal.connect(function (arg1_1, arg1_2) {
                     console.debug("client 1 received signal 'mySignal' " + arg1_1);
                });

                // create second channel after factory has created first
                // object to make sure that a dynamically created object
                // exists but does not get exposed to new channels
                createSecondChannel();
            });
        });

        var channel2;
        function createSecondChannel()
        {
            // dismiss all messges received before channel creation
            client2.cleanup();

            channel2 = client2.createChannel(function (channel2) {
                channel2.objects.myFactory.create("testObj2", function (obj2) {
                    testObj2 = lastFactoryObj;
                    testObj2Id = obj2.__id__;
                    obj2.mySignal.connect(function (arg2_1, arg2_2) {
                         console.debug("client 2 received signal 'mySignal'");
                    });
                });
            });
        }

        client1.awaitInit();
        client1.skipToMessage(JSClient.QWebChannelMessageTypes.idle);

        client2.awaitInit();
        client2.skipToMessage(JSClient.QWebChannelMessageTypes.idle);

        // dismiss server messages
        client1.serverMessages = [];
        client2.serverMessages = [];

        // now everything is set-up
        // and we can kick off a signal
        testObj1.mySignal("foo", "bar");

        var msg1 = client1.awaitSignal();
        compare(msg1.signal, 6);

        // look if there is a signal send to client2, which should not happen
        var msg2 = client2.skipToMessage(1, "server", 10);
        console.log("client2 received a signal. let's check that it does not come from testObj1");
        if (msg2 !== false) {
            verify(msg2.object !== testObj1Id);
        }
    }

    function test_separationForSameObject()
    {
        var testObj1;
        var testObj2;
        var receivedSignal1 = false;
        var receivedSignal2 = false;

        var channel2;
        var channel1 = client1.createChannel(function (channel1) {
            channel1.objects.myObj3.getObject(function (obj) {
                testObj1 = obj;

                testObj1.mySignal.connect(function() {
                    receivedSignal1 = true;
                });

                // create second channel after factory has created first
                // object to make sure that a dynamically created object
                // exists but does not get exposed to new channels
                createSecondChannel();
            });
        });

        function createSecondChannel()
        {
            // dismiss all messges received before channel creation
            client2.cleanup();

            channel2 = client2.createChannel(function (channel2) {
                verify(channel2.objects.myObj2)
                channel2.objects.myObj3.getObject(function (obj) {
                    testObj2 = obj;
                    testObj2.mySignal.connect(function() {
                        receivedSignal2 = true;
                    });

                });
            });
        }

        client1.awaitInit();
        client1.skipToMessage(JSClient.QWebChannelMessageTypes.idle);

        client2.awaitInit();
        client2.skipToMessage(JSClient.QWebChannelMessageTypes.idle);

        // trigger signal, signal should be received by both channels
        myObj.mySignal("foo", "bar");

        verify(receivedSignal1, "Channel 1 missed signal")
        verify(receivedSignal2, "Channel 2 missed signal")
    }

    function test_propertyUpdateSeparation()
    {
        var testObj1;
        var testObj1Id;
        var testObj2;
        var testObj2Id;

        var channel1 = client1.createChannel(function (channel1) {
            channel1.objects.myFactory.create("testObj1", function (obj1) {
                testObj1 = lastFactoryObj;
                testObj1Id = obj1.__id__;

                obj1.myPropertyChanged.connect(function (arg1_1) {
                     console.debug("client 1 received property update 'myProperty' " + obj1.myProperty);
                });

                // create second channel after factory has created first
                // object to make sure that a dynamically created object
                // exists but does not get exposed to new channels
                createSecondChannel();
            });
        });

        var channel2;
        function createSecondChannel()
        {
            // dismiss all messges received before channel creation
            client2.cleanup();

            channel2 = client2.createChannel(function (channel2) {
                channel2.objects.myFactory.create("testObj2", function (obj2) {
                    testObj2 = lastFactoryObj;
                    testObj2Id = obj2.__id__;
                    obj2.myPropertyChanged.connect(function (arg1_1) {
                         console.debug("client 2 received property update 'myProperty' " + obj2.myProperty);
                    });
                });
            });
        }

        client1.awaitInit();
        client1.skipToMessage(JSClient.QWebChannelMessageTypes.idle);

        client2.awaitInit();
        client2.skipToMessage(JSClient.QWebChannelMessageTypes.idle);

        // dismiss server messages
        client1.serverMessages = [];
        client2.serverMessages = [];

        // now everything is set-up
        // and we can kick off a property change
        testObj1.myProperty = 5;

        var msg1 = client1.awaitPropertyUpdate();
        compare(msg1.type, JSClient.QWebChannelMessageTypes.propertyUpdate);

        //look if there is a propertyUpdate sent to client2, which should not happen
        var msg2 = client2.skipToMessage(2, "server", 10);
        console.log("client2 received a propertyUpdate. let's check that it does not come from testObj1");
        if (msg2 !== false) {
            verify(msg2.object !== testObj1Id);
        }
    }

    function test_returnNonQObject()
    {
        var retObj;

        var channel = client1.createChannel(function (channel) {
            channel.objects.myObj2.myMethod(function(result) {
                retObj = result;
            });
        });

        client1.awaitInit();

        var msg1 = client1.awaitMessage();
        compare(msg1.type, JSClient.QWebChannelMessageTypes.invokeMethod); // create

        msg1 = client1.awaitIdle();
        verify(retObj["obj1"]["name"]);
    }
}