diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/qml/Client.qml | 132 | ||||
-rw-r--r-- | tests/auto/qml/qml.cpp | 12 | ||||
-rw-r--r-- | tests/auto/qml/qml.pro | 7 | ||||
-rw-r--r-- | tests/auto/qml/testtransport.cpp | 12 | ||||
-rw-r--r-- | tests/auto/qml/testtransport.h | 12 | ||||
-rw-r--r-- | tests/auto/qml/testwebchannel.cpp | 12 | ||||
-rw-r--r-- | tests/auto/qml/testwebchannel.h | 12 | ||||
-rw-r--r-- | tests/auto/qml/tst_bench.qml | 12 | ||||
-rw-r--r-- | tests/auto/qml/tst_multiclient.qml | 156 | ||||
-rw-r--r-- | tests/auto/qml/tst_webchannel.qml | 75 | ||||
-rw-r--r-- | tests/auto/qml/tst_webchannelseparation.qml | 358 | ||||
-rw-r--r-- | tests/auto/webchannel/tst_webchannel.cpp | 22 | ||||
-rw-r--r-- | tests/auto/webchannel/tst_webchannel.h | 12 |
13 files changed, 704 insertions, 130 deletions
diff --git a/tests/auto/qml/Client.qml b/tests/auto/qml/Client.qml index 20da8f6..332b67c 100644 --- a/tests/auto/qml/Client.qml +++ b/tests/auto/qml/Client.qml @@ -1,7 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff <milian.wolff@kdab.com> -** Contact: http://www.qt-project.org/legal +** Contact: http://www.qt.io/licensing/ ** ** This file is part of the QtWebChannel module of the Qt Toolkit. ** @@ -10,9 +10,9 @@ ** 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 Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. +** 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 @@ -23,8 +23,8 @@ ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception +** 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$ @@ -39,12 +39,15 @@ import QtWebChannel.Tests 1.0 import "qrc:///qtwebchannel/qwebchannel.js" as JSClient Item { + id: root + TestTransport { id: serverTransport } readonly property var serverTransport: serverTransport property var clientMessages: [] + property var serverMessages: [] property bool debug: false @@ -55,24 +58,21 @@ Item { function send(message) { - if (debug) { - console.log("client posts message: ", message, "is idle:", webChannel.clientIsIdle()); - } - clientMessages.push(message); + if (debug) + console.log("client", (root.objectName ? "(" + root.objectName + ")" : ""), "posts message: ", message, "is idle:", webChannel.clientIsIdle()); + clientMessages.push(JSON.parse(message)); serverTransport.receiveMessage(message); - if (message && message.type && message.type === JSClient.QWebChannelMessageTypes.idle) { + if (message && message.type && message.type === JSClient.QWebChannelMessageTypes.idle) verify(webChannel.clientIsIdle()); - } } Component.onCompleted: { - serverTransport.sendMessageRequested.connect(function(message) { - if (debug) { - console.log("client received message: ", JSON.stringify(message)); - } - if (onmessage) { + serverTransport.sendMessageRequested.connect(function receive(message) { + if (debug) + console.log("client", (root.objectName ? "(" + root.objectName + ")" : ""), "received message:", JSON.stringify(message)); + serverMessages.push(message); + if (onmessage) onmessage({data:message}); - } }); } } @@ -86,51 +86,95 @@ Item { function cleanup() { clientMessages = []; + serverMessages = []; } - function awaitRawMessage() + function awaitRawMessage(from) { - for (var i = 0; i < 10 && !clientMessages.length; ++i) { + var messages; + if (!from || typeof from !== "string" || from == "client") { + from = "client"; + messages = clientMessages; + } else { + from = "server"; + messages = serverMessages; + } + + for (var i = 0; i < 10 && !messages.length; ++i) wait(10); + + var msg = messages.shift(); + if (debug) { + console.log((root.objectName ? "(" + root.objectName + ")" : ""), "shifting message " + from + "[" + messages.length + "]" + ":" + JSON.stringify(msg)); } - return clientMessages.shift(); + return msg; } - function awaitMessage() + function awaitMessage(from) { - var msg = awaitRawMessage() - if (debug) { - console.log("handling message: ", msg); - } - if (!msg) { + var msg = awaitRawMessage(from) + if (debug) + console.log((root.objectName ? "(" + root.objectName + ")" : ""), "handling message: ", JSON.stringify(msg)); + if (!msg) return false; + return msg; + } + + function await(type, from, skip) { + var msg; + do { + msg = awaitMessage(from); + if (!msg) { + console.trace(); + verify(msg); + } + } while (skip && (msg.type === JSClient.QWebChannelMessageTypes.idle)); + if (type !== null) { + if (!msg || msg.type != type) + console.trace(); + verify(msg); + compare(msg.type, type); } - return JSON.parse(msg); + return msg; + } + + function awaitInit() { + return await(JSClient.QWebChannelMessageTypes.init); + } + + function awaitIdle() { + return await(JSClient.QWebChannelMessageTypes.idle); + } + + function awaitMessageSkipIdle() { + return awaitFunc(null, null, true); } - function awaitInit() + function awaitServerInit() { + return await(JSClient.QWebChannelMessageTypes.init, "server"); + } + + function awaitSignal() { - var msg = awaitMessage(); - verify(msg); - verify(msg.type); - compare(msg.type, JSClient.QWebChannelMessageTypes.init); + return await(JSClient.QWebChannelMessageTypes.signal, "server"); } - function awaitIdle() + function awaitPropertyUpdate() { - var msg = awaitMessage(); - verify(msg); - compare(msg.type, JSClient.QWebChannelMessageTypes.idle); + return await(JSClient.QWebChannelMessageTypes.propertyUpdate, "server"); } - function awaitMessageSkipIdle() + function awaitResponse() { - var msg; - do { - msg = awaitMessage(); - verify(msg); - } while (msg.type === JSClient.QWebChannelMessageTypes.idle); - return msg; + return await(JSClient.QWebChannelMessageTypes.response, "server"); } + function skipToMessage(type, from, max) { + do { + var msg = awaitMessage(from); + if (msg && msg.type === type) + return msg + } while (--max > 0); + return false; + } } diff --git a/tests/auto/qml/qml.cpp b/tests/auto/qml/qml.cpp index e77179a..0c6919e 100644 --- a/tests/auto/qml/qml.cpp +++ b/tests/auto/qml/qml.cpp @@ -1,7 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff <milian.wolff@kdab.com> -** Contact: http://www.qt-project.org/legal +** Contact: http://www.qt.io/licensing/ ** ** This file is part of the QtWebChannel module of the Qt Toolkit. ** @@ -10,9 +10,9 @@ ** 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 Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. +** 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 @@ -23,8 +23,8 @@ ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception +** 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$ diff --git a/tests/auto/qml/qml.pro b/tests/auto/qml/qml.pro index b0c52b5..5dbac40 100644 --- a/tests/auto/qml/qml.pro +++ b/tests/auto/qml/qml.pro @@ -17,9 +17,14 @@ HEADERS += \ testwebchannel.h OTHER_FILES += \ + Client.qml \ WebChannelTest.qml \ tst_webchannel.qml \ tst_metaobjectpublisher.qml \ - tst_bench.qml + tst_bench.qml \ + tst_multiclient.qml TESTDATA = data/* + +DISTFILES += \ + tst_webchannelseparation.qml diff --git a/tests/auto/qml/testtransport.cpp b/tests/auto/qml/testtransport.cpp index 0e2742b..ebf6ce1 100644 --- a/tests/auto/qml/testtransport.cpp +++ b/tests/auto/qml/testtransport.cpp @@ -1,7 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff <milian.wolff@kdab.com> -** Contact: http://www.qt-project.org/legal +** Contact: http://www.qt.io/licensing/ ** ** This file is part of the QtWebChannel module of the Qt Toolkit. ** @@ -10,9 +10,9 @@ ** 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 Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. +** 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 @@ -23,8 +23,8 @@ ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception +** 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$ diff --git a/tests/auto/qml/testtransport.h b/tests/auto/qml/testtransport.h index cf3b6d3..56dc198 100644 --- a/tests/auto/qml/testtransport.h +++ b/tests/auto/qml/testtransport.h @@ -1,7 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff <milian.wolff@kdab.com> -** Contact: http://www.qt-project.org/legal +** Contact: http://www.qt.io/licensing/ ** ** This file is part of the QtWebChannel module of the Qt Toolkit. ** @@ -10,9 +10,9 @@ ** 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 Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. +** 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 @@ -23,8 +23,8 @@ ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception +** 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$ diff --git a/tests/auto/qml/testwebchannel.cpp b/tests/auto/qml/testwebchannel.cpp index 9b48ef3..40ac536 100644 --- a/tests/auto/qml/testwebchannel.cpp +++ b/tests/auto/qml/testwebchannel.cpp @@ -1,7 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff <milian.wolff@kdab.com> -** Contact: http://www.qt-project.org/legal +** Contact: http://www.qt.io/licensing/ ** ** This file is part of the QtWebChannel module of the Qt Toolkit. ** @@ -10,9 +10,9 @@ ** 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 Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. +** 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 @@ -23,8 +23,8 @@ ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception +** 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$ diff --git a/tests/auto/qml/testwebchannel.h b/tests/auto/qml/testwebchannel.h index cf6e6bc..0a22ff6 100644 --- a/tests/auto/qml/testwebchannel.h +++ b/tests/auto/qml/testwebchannel.h @@ -1,7 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff <milian.wolff@kdab.com> -** Contact: http://www.qt-project.org/legal +** Contact: http://www.qt.io/licensing/ ** ** This file is part of the QtWebChannel module of the Qt Toolkit. ** @@ -10,9 +10,9 @@ ** 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 Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. +** 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 @@ -23,8 +23,8 @@ ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception +** 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$ diff --git a/tests/auto/qml/tst_bench.qml b/tests/auto/qml/tst_bench.qml index ca4117e..e7769a7 100644 --- a/tests/auto/qml/tst_bench.qml +++ b/tests/auto/qml/tst_bench.qml @@ -1,7 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff <milian.wolff@kdab.com> -** Contact: http://www.qt-project.org/legal +** Contact: http://www.qt.io/licensing/ ** ** This file is part of the QtWebChannel module of the Qt Toolkit. ** @@ -10,9 +10,9 @@ ** 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 Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. +** 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 @@ -23,8 +23,8 @@ ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception +** 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$ diff --git a/tests/auto/qml/tst_multiclient.qml b/tests/auto/qml/tst_multiclient.qml index b696391..d646349 100644 --- a/tests/auto/qml/tst_multiclient.qml +++ b/tests/auto/qml/tst_multiclient.qml @@ -1,7 +1,8 @@ /**************************************************************************** ** ** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff <milian.wolff@kdab.com> -** Contact: http://www.qt-project.org/legal +** 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. ** @@ -10,9 +11,9 @@ ** 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 Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. +** 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 @@ -23,8 +24,8 @@ ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception +** 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$ @@ -37,6 +38,8 @@ import QtTest 1.0 import QtWebChannel 1.0 import QtWebChannel.Tests 1.0 +import "qrc:///qtwebchannel/qwebchannel.js" as JSClient + TestCase { name: "MultiClient" @@ -62,18 +65,86 @@ TestCase { WebChannel.id: "foo" } + 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" + } + + 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: [foo] + registeredObjects: [foo, myObj, myOtherObj, 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 clientInitCallback(channel) { channel.objects.foo.ping.connect(function() { @@ -103,4 +174,75 @@ TestCase { compare(c1.pongAnswer, 1); compare(c2.pongAnswer, 2); } + + function test_autowrappedObjectsNotInInit() { + var testObj1; + var testObj1Id; + + var channel1 = client1.createChannel(function (channel1) { + channel1.objects.myFactory.create("testObj1", function (obj1) { + testObj1 = lastFactoryObj; + testObj1Id = obj1.__id__; + + // 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) { + }); + } + + client1.awaitInit(); + var msg1 = client1.awaitMessage(); + compare(msg1.type, JSClient.QWebChannelMessageTypes.invokeMethod); // create + + client1.awaitIdle(); + + client2.awaitInit(); + client2.awaitIdle(); + + compare(typeof channel2.objects[testObj1Id], "undefined") + } + + function test_autowrappedObjectsNotBroadcasted() { + var testObj2; + var testObj2Id; + + var channel1 = client1.createChannel(function (channel1) { + // create second channel after first channel to make sure + // that a dynamically created object do not get exposed to + // existing 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__; + }); + }); + } + + client1.awaitInit(); + client1.awaitIdle(); + + client2.awaitInit(); + var msg2 = client2.awaitMessage(); + compare(msg2.type, JSClient.QWebChannelMessageTypes.invokeMethod); // create + + client2.awaitIdle(); + + compare(typeof channel1.objects[testObj2Id], "undefined") + } } diff --git a/tests/auto/qml/tst_webchannel.qml b/tests/auto/qml/tst_webchannel.qml index e7fcedc..f1042ca 100644 --- a/tests/auto/qml/tst_webchannel.qml +++ b/tests/auto/qml/tst_webchannel.qml @@ -1,7 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff <milian.wolff@kdab.com> -** Contact: http://www.qt-project.org/legal +** Contact: http://www.qt.io/licensing/ ** ** This file is part of the QtWebChannel module of the Qt Toolkit. ** @@ -10,9 +10,9 @@ ** 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 Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. +** 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 @@ -23,8 +23,8 @@ ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception +** 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$ @@ -66,15 +66,15 @@ TestCase { property var bar: 1 WebChannel.id: "myOtherObj" } + property var lastFactoryObj QtObject{ id: bar; objectName: "bar" } QtObject{ id: baz; objectName: "baz" } QtObject { id: myFactory - property var lastObj function create(id) { - lastObj = component.createObject(myFactory, {objectName: id}); - return lastObj; + lastFactoryObj = component.createObject(myFactory, {objectName: id}); + return lastFactoryObj; } property var objectInProperty: QtObject { objectName: "foo" @@ -145,7 +145,6 @@ TestCase { compare(myObj.myProperty, 3); client.awaitIdle(); // init - client.awaitIdle(); // property update // change property, should be propagated to HTML client and a message be send there myObj.myProperty = 2; @@ -242,12 +241,15 @@ TestCase { }); }); client.awaitInit(); + client.awaitResponse(); + // create testObj var msg = client.awaitMessage(); compare(msg.type, JSClient.QWebChannelMessageTypes.invokeMethod); compare(msg.object, "myFactory"); - verify(myFactory.lastObj); - compare(myFactory.lastObj.objectName, "testObj"); + client.awaitResponse(); + verify(lastFactoryObj); + compare(lastFactoryObj.objectName, "testObj"); compare(channel.objects[testObjId].objectName, "testObj"); // mySignal connection @@ -255,20 +257,30 @@ TestCase { compare(msg.type, JSClient.QWebChannelMessageTypes.connectToSignal); compare(msg.object, testObjId); + // set myProperty msg = client.awaitMessage(); compare(msg.type, JSClient.QWebChannelMessageTypes.setProperty); compare(msg.object, testObjId); - compare(myFactory.lastObj.myProperty, 42); + compare(lastFactoryObj.myProperty, 42); + // call myMethod msg = client.awaitMessage(); compare(msg.type, JSClient.QWebChannelMessageTypes.invokeMethod); compare(msg.object, testObjId); compare(msg.args, ["foobar"]); + client.awaitResponse(); compare(lastMethodArg, "foobar"); client.awaitIdle(); - myFactory.lastObj.mySignal("foobar", 42); + // the server should eventually notify the client about the property update + client.awaitPropertyUpdate(); + + client.awaitIdle(); + + // trigger a signal and ensure it gets transmitted + lastFactoryObj.mySignal("foobar", 42); + client.awaitSignal(); // property should be wrapped compare(channel.objects.myFactory.objectInProperty.objectName, "foo"); @@ -283,10 +295,13 @@ TestCase { msg = client.awaitMessage(); compare(msg.type, JSClient.QWebChannelMessageTypes.invokeMethod); compare(msg.object, testObjId); + client.awaitResponse(); + // now the signalArgs should also be set compare(signalArgs, {"0": "foobar", "1": 42}); - client.awaitIdle(); // destroyed signal + // and also a destroyed signal + client.awaitSignal(); compare(JSON.stringify(testObjBeforeDeletion), JSON.stringify({})); compare(JSON.stringify(testObjAfterDeletion), JSON.stringify({})); @@ -304,51 +319,61 @@ TestCase { }); }); client.awaitInit(); + client.awaitResponse(); + + // call to myFactory.create() + var msg = client.awaitMessage(); + compare(msg.type, JSClient.QWebChannelMessageTypes.invokeMethod); + client.awaitResponse(); - // ignore first message (call to myFactory.create()) - client.awaitMessage(); client.awaitIdle(); verify(testObj); var testObjId = testObj.__id__; testObj.deleteLater(); - var msg = client.awaitMessage(); + msg = client.awaitMessage(); compare(msg.type, JSClient.QWebChannelMessageTypes.invokeMethod); compare(msg.object, testObjId); + client.awaitResponse(); + // destroyed signal + client.awaitSignal(); - // after receiving the destroyed signal the client deletes - // local objects and sends back a idle message - client.awaitIdle(); - - compare(myFactory.lastObj, null); + compare(lastFactoryObj, null); compare(typeof channel.objects[testObjId], "undefined"); } - function test_wrapper_propertyUpdateOfWrappedObjects() { + function test_wrapper_propertyUpdateOfWrappedObjects() + { var testObj; var testObjId; var channel = client.createChannel(function(channel) { channel.objects.myFactory.create("testObj", function(obj) { - testObj = myFactory.lastObj; + testObj = lastFactoryObj; testObjId = obj.__id__; }); }); client.awaitInit(); + client.awaitResponse(); // call to myFactory.create() var msg = client.awaitMessage(); compare(msg.type, JSClient.QWebChannelMessageTypes.invokeMethod); + client.awaitResponse(); client.awaitIdle(); testObj.myProperty = 42; + client.awaitPropertyUpdate(); client.awaitIdle(); compare(channel.objects[testObjId].myProperty, 42); channel.objects[testObjId].deleteLater(); msg = client.awaitMessage(); compare(msg.type, JSClient.QWebChannelMessageTypes.invokeMethod); + client.awaitResponse(); + // destroyed signal + client.awaitSignal(); } function test_disconnect() diff --git a/tests/auto/qml/tst_webchannelseparation.qml b/tests/auto/qml/tst_webchannelseparation.qml new file mode 100644 index 0000000..647f162 --- /dev/null +++ b/tests/auto/qml/tst_webchannelseparation.qml @@ -0,0 +1,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"]); + } +} + diff --git a/tests/auto/webchannel/tst_webchannel.cpp b/tests/auto/webchannel/tst_webchannel.cpp index 3304293..55fd2d9 100644 --- a/tests/auto/webchannel/tst_webchannel.cpp +++ b/tests/auto/webchannel/tst_webchannel.cpp @@ -1,7 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff <milian.wolff@kdab.com> -** Contact: http://www.qt-project.org/legal +** Contact: http://www.qt.io/licensing/ ** ** This file is part of the QtWebChannel module of the Qt Toolkit. ** @@ -10,9 +10,9 @@ ** 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 Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. +** 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 @@ -23,8 +23,8 @@ ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception +** 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$ @@ -93,7 +93,7 @@ void TestWebChannel::testDeregisterObjects() channel.registerObject(testObject.objectName(), &testObject); channel.connectTo(m_dummyTransport); - channel.d_func()->publisher->initializeClients(); + channel.d_func()->publisher->initializeClient(m_dummyTransport); QJsonObject connectMessage = QJsonDocument::fromJson(("{\"type\": 7," @@ -113,7 +113,7 @@ void TestWebChannel::testInfoForObject() obj.setObjectName("myTestObject"); QWebChannel channel; - const QJsonObject info = channel.d_func()->publisher->classInfoForObject(&obj); + const QJsonObject info = channel.d_func()->publisher->classInfoForObject(&obj, m_dummyTransport); QCOMPARE(info.keys(), QStringList() << "enums" << "methods" << "properties" << "signals"); @@ -291,7 +291,7 @@ void TestWebChannel::benchClassInfo() QBENCHMARK { foreach (const QObject *object, objects) { - channel.d_func()->publisher->classInfoForObject(object); + channel.d_func()->publisher->classInfoForObject(object, m_dummyTransport); } } } @@ -306,7 +306,7 @@ void TestWebChannel::benchInitializeClients() QMetaObjectPublisher *publisher = channel.d_func()->publisher; QBENCHMARK { - publisher->initializeClients(); + publisher->initializeClient(m_dummyTransport); publisher->propertyUpdatesInitialized = false; publisher->signalToPropertyMap.clear(); @@ -328,7 +328,7 @@ void TestWebChannel::benchPropertyUpdates() } channel.registerObjects(objects); - channel.d_func()->publisher->initializeClients(); + channel.d_func()->publisher->initializeClient(m_dummyTransport); QBENCHMARK { foreach (BenchObject *obj, objectList) { diff --git a/tests/auto/webchannel/tst_webchannel.h b/tests/auto/webchannel/tst_webchannel.h index 6564944..7b9a1e3 100644 --- a/tests/auto/webchannel/tst_webchannel.h +++ b/tests/auto/webchannel/tst_webchannel.h @@ -1,7 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff <milian.wolff@kdab.com> -** Contact: http://www.qt-project.org/legal +** Contact: http://www.qt.io/licensing/ ** ** This file is part of the QtWebChannel module of the Qt Toolkit. ** @@ -10,9 +10,9 @@ ** 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 Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. +** 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 @@ -23,8 +23,8 @@ ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception +** 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$ |