diff options
Diffstat (limited to 'tests/auto/qml/Client.qml')
-rw-r--r-- | tests/auto/qml/Client.qml | 132 |
1 files changed, 88 insertions, 44 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; + } } |