summaryrefslogtreecommitdiff
path: root/tests/auto/qml
diff options
context:
space:
mode:
authorMilian Wolff <milian.wolff@kdab.com>2014-03-21 16:15:47 +0100
committerPierre Rossi <pierre.rossi@gmail.com>2014-07-04 12:37:29 +0200
commit856dc072acb649be03e02ac64c81015d3f2675c2 (patch)
tree649985d553d6ea92360a66ca291af91c1114596e /tests/auto/qml
parent9f78e0985d2f4fdc2588be57c5f25afdd59c6365 (diff)
downloadqtwebchannel-856dc072acb649be03e02ac64c81015d3f2675c2.tar.gz
Refactor code to use QWebChannelAbstractTransport and QtWebSockets.
This is a quite big changeset, but necessary to get the roadmap implemented that was discussed at QtCS. With this patchset landed, the QWebChannel does not depend on QtWebKit anymore, not even for the tests. Rather, we will introduce the dependency in the other way (i.e. QtWebKit will optionally use QtWebChannel if available). For the pure Qt/C++ use-case, we ship a utility implementation of a QWebChannelAbstractTransport that uses a QWebSocket for the server-client communication. This way, we can get rid of the custom WebSocket implementation. The tests are refactored to run the qwebchannel.js code directly inside QML. Integration tests for QtWebKit/QtWebEngine as well as examples will be added to these repositories. Change-Id: Icc1c1c5918ec46e31d5070937c14c4ca25a3e2d6 Reviewed-by: Pierre Rossi <pierre.rossi@gmail.com>
Diffstat (limited to 'tests/auto/qml')
-rw-r--r--tests/auto/qml/Client.qml (renamed from tests/auto/qml/WebChannelTest.qml)133
-rw-r--r--tests/auto/qml/data/bench_init.html13
-rw-r--r--tests/auto/qml/data/disconnect.html21
-rw-r--r--tests/auto/qml/data/grouping.html17
-rw-r--r--tests/auto/qml/data/method.html17
-rw-r--r--tests/auto/qml/data/multiclient.html19
-rw-r--r--tests/auto/qml/data/property.html21
-rw-r--r--tests/auto/qml/data/receiveRaw.html15
-rw-r--r--tests/auto/qml/data/send.html17
-rw-r--r--tests/auto/qml/data/signal.html17
-rw-r--r--tests/auto/qml/data/wrapper.html28
-rw-r--r--tests/auto/qml/qml.cpp14
-rw-r--r--tests/auto/qml/qml.pro6
-rw-r--r--tests/auto/qml/testtransport.cpp (renamed from tests/auto/qml/data/testsetup.js)18
-rw-r--r--tests/auto/qml/testtransport.h (renamed from tests/auto/qml/TestWebView.qml)48
-rw-r--r--tests/auto/qml/tst_bench.qml38
-rw-r--r--tests/auto/qml/tst_metaobjectpublisher.qml177
-rw-r--r--tests/auto/qml/tst_multiclient.qml56
-rw-r--r--tests/auto/qml/tst_webchannel.qml37
19 files changed, 318 insertions, 394 deletions
diff --git a/tests/auto/qml/WebChannelTest.qml b/tests/auto/qml/Client.qml
index 9a0baa9..609fbac 100644
--- a/tests/auto/qml/WebChannelTest.qml
+++ b/tests/auto/qml/Client.qml
@@ -43,77 +43,86 @@ import QtQuick 2.0
import QtTest 1.0
import QtWebChannel 1.0
+import QtWebChannel.Tests 1.0
+import "qrc:///qwebchannel/qwebchannel.js" as Client
-TestCase {
- property var lastLoadStatus
- property bool useWebViewTransport: false
+Item {
+ TestTransport {
+ id: serverTransport
+ }
+ readonly property var serverTransport: serverTransport
- // only run after the webchannel has finished initialization
- when: webSocketTransport.baseUrl != ""
+ property var clientMessages: []
- WebViewTransport {
- id: webViewTransport
- webViewExperimental: defaultView.experimental
- }
- WebSocketTransport {
- id: webSocketTransport
- }
+ property bool debug: false
- TestWebView {
- id: defaultView
- }
+ QtObject {
+ id: clientTransport
+
+ property var send;
+ property var onmessage;
- WebChannel {
- id: webChannel
+ function postMessage(message)
+ {
+ if (debug) {
+ console.log("client posts message: ", message);
+ }
+ clientMessages.push(message);
+ serverTransport.textMessageReceived(message);
+ }
+
+ Component.onCompleted: {
+ serverTransport.sendTextMessageRequested.connect(function(message) {
+ if (debug) {
+ console.log("client received message: ", message);
+ }
+ onmessage({data:message});
+ });
+ }
}
- property var webChannel: webChannel
+ readonly property var clientTransport: clientTransport
+
+ Timer {
+ id: timer
+ running: false
+ repeat: false
- SignalSpy {
- id: rawMessageSpy
- target: useWebViewTransport ? webViewTransport : webSocketTransport;
- signalName: "onMessageReceived"
+ property var callback
+
+ onTriggered: {
+ callback();
+ }
}
- property var rawMessageSpy: rawMessageSpy
- property var rawMessageIdx: 0;
- function urlForFile(file)
+ function setTimeout(callback, delay)
{
- verify(useWebViewTransport || webSocketTransport.baseUrl != "", "webSocketTransport.baseUrl is empty");
- return "data/" + file + (!useWebViewTransport ? "?webChannelBaseUrl=" + webSocketTransport.baseUrl : "");
+ if (timer.running) {
+ console.error("nested calls to setTimeout are not supported!", JSON.stringify(callback), JSON.stringify(timer.callback));
+ return;
+ }
+ timer.callback = callback;
+ // note: an interval of 0 is directly triggered, so add a little padding
+ timer.interval = delay + 1;
+ timer.running = true;
}
- // load file in the given view or use the global one by default
- function loadUrl(file, view)
+ function createChannel(callback, raw)
{
- if (useWebViewTransport) {
- webChannel.disconnectFrom(webSocketTransport);
- webChannel.connectTo(webViewTransport);
- } else {
- webChannel.disconnectFrom(webViewTransport);
- webChannel.connectTo(webSocketTransport);
- }
- if (!view) {
- view = defaultView;
- }
- view.url = urlForFile(file);
- view.waitForLoaded();
+ return new Client.QWebChannel(clientTransport, callback, raw);
}
function cleanup()
{
- defaultView.clear();
- rawMessageSpy.clear();
- rawMessageIdx = 0;
+ clientMessages = [];
+ timer.running = false;
}
function awaitRawMessage()
{
- rawMessageSpy.wait(500);
- if (rawMessageSpy.signalArguments.length <= rawMessageIdx) {
- // still no message received, fail
- return null;
+ for (var i = 0; i < 10 && !clientMessages.length; ++i) {
+ wait(10);
}
- return rawMessageSpy.signalArguments[rawMessageIdx++][0];
+ return clientMessages.shift();
}
function awaitMessage()
@@ -131,7 +140,7 @@ TestCase {
verify(msg);
verify(msg.data);
verify(msg.data.type);
- compare(msg.data.type, qWebChannelMessageTypes.init);
+ compare(msg.data.type, Client.QWebChannelMessageTypes.init);
}
function awaitIdle()
@@ -139,19 +148,19 @@ TestCase {
var msg = awaitMessage();
verify(msg);
verify(msg.data);
- compare(msg.data.type, qWebChannelMessageTypes.idle);
+ compare(msg.data.type, Client.QWebChannelMessageTypes.idle);
verify(webChannel.test_clientIsIdle())
}
- property var qWebChannelMessageTypes: ({
- signal: 1,
- propertyUpdate: 2,
- init: 3,
- idle: 4,
- debug: 5,
- invokeMethod: 6,
- connectToSignal: 7,
- disconnectFromSignal: 8,
- setProperty: 9,
- });
+ function awaitMessageSkipIdle()
+ {
+ var msg;
+ do {
+ msg = awaitMessage();
+ verify(msg);
+ verify(msg.data);
+ } while (msg.data.type === Client.QWebChannelMessageTypes.idle);
+ return msg;
+ }
+
}
diff --git a/tests/auto/qml/data/bench_init.html b/tests/auto/qml/data/bench_init.html
deleted file mode 100644
index 7d3af5b..0000000
--- a/tests/auto/qml/data/bench_init.html
+++ /dev/null
@@ -1,13 +0,0 @@
-<html>
- <head>
- <script type="text/javascript" src="qrc:///qwebchannel/qwebchannel.js"></script>
- <script type="text/javascript" src="testsetup.js"></script>
- <script type="text/javascript">
- //BEGIN SETUP
- createWebChannel(function(channel) {});
- //END SETUP
- </script>
- </head>
- <body>
- </body>
-</html>
diff --git a/tests/auto/qml/data/disconnect.html b/tests/auto/qml/data/disconnect.html
deleted file mode 100644
index a9a479c..0000000
--- a/tests/auto/qml/data/disconnect.html
+++ /dev/null
@@ -1,21 +0,0 @@
-<html>
- <head>
- <script type="text/javascript" src="qrc:///qwebchannel/qwebchannel.js"></script>
- <script type="text/javascript" src="testsetup.js"></script>
- <script type="text/javascript">
- //BEGIN SETUP
- createWebChannel(function(channel) {
- myObj.mySignal.connect(function(arg) {
- channel.exec({label: "mySignalReceived", args: [arg]});
- myObj.mySignal.disconnect(this);
- });
- channel.subscribe("report", function() {
- channel.exec({label: "report"});
- });
- });
- //END SETUP
- </script>
- </head>
- <body>
- </body>
-</html>
diff --git a/tests/auto/qml/data/grouping.html b/tests/auto/qml/data/grouping.html
deleted file mode 100644
index f6bc33c..0000000
--- a/tests/auto/qml/data/grouping.html
+++ /dev/null
@@ -1,17 +0,0 @@
-<html>
- <head>
- <script type="text/javascript" src="qrc:///qwebchannel/qwebchannel.js"></script>
- <script type="text/javascript" src="testsetup.js"></script>
- <script type="text/javascript">
- //BEGIN SETUP
- window.channel = createWebChannel(function(channel) {
- channel.subscribe(QWebChannelMessageTypes.propertyUpdate, function() {
- channel.exec({label: "gotPropertyUpdate", values: [myObj.myProperty, myOtherObj.foo, myOtherObj.bar]});
- });
- });
- //END SETUP
- </script>
- </head>
- <body>
- </body>
-</html>
diff --git a/tests/auto/qml/data/method.html b/tests/auto/qml/data/method.html
deleted file mode 100644
index 6dbaa90..0000000
--- a/tests/auto/qml/data/method.html
+++ /dev/null
@@ -1,17 +0,0 @@
-<html>
- <head>
- <script type="text/javascript" src="qrc:///qwebchannel/qwebchannel.js"></script>
- <script type="text/javascript" src="testsetup.js"></script>
- <script type="text/javascript">
- //BEGIN SETUP
- createWebChannel(function(channel) {
- channel.subscribe("invokeMethod", function(arg) {
- myObj.myMethod(arg);
- });
- });
- //END SETUP
- </script>
- </head>
- <body>
- </body>
-</html>
diff --git a/tests/auto/qml/data/multiclient.html b/tests/auto/qml/data/multiclient.html
deleted file mode 100644
index 1573a1a..0000000
--- a/tests/auto/qml/data/multiclient.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html>
- <head>
- <script type="text/javascript" src="qrc:///qwebchannel/qwebchannel.js"></script>
- <script type="text/javascript" src="testsetup.js"></script>
- <script type="text/javascript">
- //BEGIN SETUP
- createWebChannel(function(channel) {
- foo.ping.connect(function() {
- foo.pong(function(value) {
- channel.exec({pongAnswer: value});
- });
- });
- });
- //END SETUP
- </script>
- </head>
- <body>
- </body>
-</html>
diff --git a/tests/auto/qml/data/property.html b/tests/auto/qml/data/property.html
deleted file mode 100644
index 49b3811..0000000
--- a/tests/auto/qml/data/property.html
+++ /dev/null
@@ -1,21 +0,0 @@
-<html>
- <head>
- <script type="text/javascript" src="qrc:///qwebchannel/qwebchannel.js"></script>
- <script type="text/javascript" src="testsetup.js"></script>
- <script type="text/javascript">
- //BEGIN SETUP
- createWebChannel(function(channel) {
- channel.exec({label: "init", value: myObj.myProperty});
- myObj.myPropertyChanged.connect(function() {
- channel.exec({label: "changed", value: myObj.myProperty});
- });
- channel.subscribe("setProperty", function(newValue) {
- myObj.myProperty = newValue;
- });
- });
- //END SETUP
- </script>
- </head>
- <body>
- </body>
-</html>
diff --git a/tests/auto/qml/data/receiveRaw.html b/tests/auto/qml/data/receiveRaw.html
deleted file mode 100644
index 139b2b1..0000000
--- a/tests/auto/qml/data/receiveRaw.html
+++ /dev/null
@@ -1,15 +0,0 @@
-<html>
- <head>
- <script type="text/javascript" src="qrc:///qwebchannel/qwebchannel.js"></script>
- <script type="text/javascript" src="testsetup.js"></script>
- <script type="text/javascript">
- //BEGIN SETUP
- createWebChannel(function(channel) {
- channel.send("foobar");
- }, true /* raw */);
- //END SETUP
- </script>
- </head>
- <body>
- </body>
-</html>
diff --git a/tests/auto/qml/data/send.html b/tests/auto/qml/data/send.html
deleted file mode 100644
index c60fbf4..0000000
--- a/tests/auto/qml/data/send.html
+++ /dev/null
@@ -1,17 +0,0 @@
-<html>
- <head>
- <script type="text/javascript" src="qrc:///qwebchannel/qwebchannel.js"></script>
- <script type="text/javascript" src="testsetup.js"></script>
- <script type="text/javascript">
- //BEGIN SETUP
- createWebChannel(function(channel) {
- channel.subscribe("myMessage", function(payload) {
- channel.send("myMessagePong:" + payload);
- });
- }, true /* raw */);
- //END SETUP
- </script>
- </head>
- <body>
- </body>
-</html>
diff --git a/tests/auto/qml/data/signal.html b/tests/auto/qml/data/signal.html
deleted file mode 100644
index bdce0c7..0000000
--- a/tests/auto/qml/data/signal.html
+++ /dev/null
@@ -1,17 +0,0 @@
-<html>
- <head>
- <script type="text/javascript" src="qrc:///qwebchannel/qwebchannel.js"></script>
- <script type="text/javascript" src="testsetup.js"></script>
- <script type="text/javascript">
- //BEGIN SETUP
- createWebChannel(function(channel) {
- myObj.mySignal.connect(function(arg) {
- channel.exec({label: "signalReceived", value: arg});
- });
- });
- //END SETUP
- </script>
- </head>
- <body>
- </body>
-</html>
diff --git a/tests/auto/qml/data/wrapper.html b/tests/auto/qml/data/wrapper.html
deleted file mode 100644
index df368ee..0000000
--- a/tests/auto/qml/data/wrapper.html
+++ /dev/null
@@ -1,28 +0,0 @@
-<html>
- <head>
- <script type="text/javascript" src="qrc:///qwebchannel/qwebchannel.js"></script>
- <script type="text/javascript" src="testsetup.js"></script>
- <script type="text/javascript">
- //BEGIN SETUP
- createWebChannel(function(channel) {
- myFactory.create("testObj", function(obj) {
- window[obj.objectName] = obj;
- obj.mySignal.connect(function(arg1, arg2) {
- channel.exec({label: "signalReceived", args: [arg1, arg2]});
- });
- obj.myProperty = 42;
- obj.myMethod("foobar");
- });
- channel.subscribe("triggerDelete", function() {
- testObj.deleteLater();
- });
- channel.subscribe("report", function() {
- channel.exec({label:"report", obj: testObj})
- });
- });
- //END SETUP
- </script>
- </head>
- <body>
- </body>
-</html>
diff --git a/tests/auto/qml/qml.cpp b/tests/auto/qml/qml.cpp
index 7267842..0612bdb 100644
--- a/tests/auto/qml/qml.cpp
+++ b/tests/auto/qml/qml.cpp
@@ -40,5 +40,17 @@
****************************************************************************/
#include <QtQuickTest/quicktest.h>
+#include <QtQml/qqml.h>
-QUICK_TEST_MAIN(qml)
+#ifndef QUICK_TEST_SOURCE_DIR
+#define QUICK_TEST_SOURCE_DIR Q_NULLPTR
+#endif
+
+#include "testtransport.h"
+
+int main(int argc, char **argv)
+{
+ qmlRegisterType<TestTransport>("QtWebChannel.Tests", 1, 0, "TestTransport");
+
+ return quick_test_main(argc, argv, "qml", QUICK_TEST_SOURCE_DIR);
+}
diff --git a/tests/auto/qml/qml.pro b/tests/auto/qml/qml.pro
index f86dc90..ddef2cb 100644
--- a/tests/auto/qml/qml.pro
+++ b/tests/auto/qml/qml.pro
@@ -8,7 +8,11 @@ CONFIG += warn_on qmltestcase
IMPORTPATH += $$OUT_PWD/../../../qml $$PWD
SOURCES += \
- qml.cpp
+ qml.cpp \
+ testtransport.cpp
+
+HEADERS += \
+ testtransport.h
OTHER_FILES += \
WebChannelTest.qml \
diff --git a/tests/auto/qml/data/testsetup.js b/tests/auto/qml/testtransport.cpp
index c0db83a..a332955 100644
--- a/tests/auto/qml/data/testsetup.js
+++ b/tests/auto/qml/testtransport.cpp
@@ -39,9 +39,19 @@
**
****************************************************************************/
-window.createWebChannel = function(callback, raw)
+#include "testtransport.h"
+
+QT_BEGIN_NAMESPACE
+
+TestTransport::TestTransport(QObject *parent)
+: QWebChannelAbstractTransport(parent)
{
- var baseUrlMatch = /[?&]webChannelBaseUrl=([A-Za-z0-9\-:/\.]+)/.exec(location.search);
- var transport = baseUrlMatch ? baseUrlMatch[1] : navigator.qt;
- return new QWebChannel(transport, callback, raw);
+
}
+
+void TestTransport::sendTextMessage(const QString &message)
+{
+ emit sendTextMessageRequested(message);
+}
+
+QT_END_NAMESPACE
diff --git a/tests/auto/qml/TestWebView.qml b/tests/auto/qml/testtransport.h
index 3b12f38..dd07832 100644
--- a/tests/auto/qml/TestWebView.qml
+++ b/tests/auto/qml/testtransport.h
@@ -39,43 +39,25 @@
**
****************************************************************************/
-import QtQuick 2.0
-import QtTest 1.0
+#ifndef TESTTRANSPORT_H
+#define TESTTRANSPORT_H
-import QtWebKit 3.0
-import QtWebKit.experimental 1.0
+#include <QtWebChannel/QWebChannelAbstractTransport>
-WebView {
- id: view
- property var lastLoadStatus
+QT_BEGIN_NAMESPACE
- experimental.preferences.developerExtrasEnabled: true
- experimental.preferences.navigatorQtObjectEnabled: true
+class TestTransport : public QWebChannelAbstractTransport
+{
+ Q_OBJECT
+public:
+ explicit TestTransport(QObject *parent = 0);
- onLoadingChanged: {
- // NOTE: we cannot use spy.signalArguments nor save the loadRequest anywhere, as it gets
- // deleted after the slots connected to the signal have finished... i.e. it's a weak pointer,
- // not a shared pointer. As such, we have to copy out the interesting data we need later on here...
- lastLoadStatus = loadRequest.status
- }
+ virtual void sendTextMessage(const QString &message);
- SignalSpy {
- id: loadingSpy
- target: view
- signalName: "onLoadingChanged"
- }
+Q_SIGNALS:
+ void sendTextMessageRequested(const QString &message);
+};
- function waitForLoaded()
- {
- do {
- loadingSpy.wait(500);
- } while (loading);
- return lastLoadStatus == WebView.LoadSucceededStatus;
- }
+QT_END_NAMESPACE
- function clear()
- {
- url = "";
- loadingSpy.clear()
- }
-}
+#endif // TESTTRANSPORT_H
diff --git a/tests/auto/qml/tst_bench.qml b/tests/auto/qml/tst_bench.qml
index 056dda9..833720e 100644
--- a/tests/auto/qml/tst_bench.qml
+++ b/tests/auto/qml/tst_bench.qml
@@ -40,13 +40,24 @@
****************************************************************************/
import QtQuick 2.0
+import QtTest 1.0
import QtWebChannel 1.0
+import "qrc:///qwebchannel/qwebchannel.js" as Client
-WebChannelTest {
+TestCase {
name: "Bench"
id: test
+ Client {
+ id: client
+ }
+
+ WebChannel {
+ id: webChannel
+ transports: [client.serverTransport]
+ }
+
Component {
id: component
QtObject {
@@ -96,28 +107,15 @@ WebChannelTest {
webChannel.registerObjects(objects);
}
- function benchmark_init_baseline()
- {
- loadUrl("bench_init.html");
- }
-
- function benchmark_init_webview()
+ function cleanup()
{
- useWebViewTransport = true;
- loadUrl("bench_init.html");
- // init
- awaitMessage();
- // idle
- awaitMessage();
+ client.cleanup();
}
- function benchmark_init_websocket()
+ function benchmark_init()
{
- useWebViewTransport = false;
- loadUrl("bench_init.html");
- // init
- awaitMessage();
- // idle
- awaitMessage();
+ var channel = client.createChannel(function() {});
+ client.awaitInit();
+ client.awaitIdle();
}
}
diff --git a/tests/auto/qml/tst_metaobjectpublisher.qml b/tests/auto/qml/tst_metaobjectpublisher.qml
index e07f105..880b30d 100644
--- a/tests/auto/qml/tst_metaobjectpublisher.qml
+++ b/tests/auto/qml/tst_metaobjectpublisher.qml
@@ -40,12 +40,17 @@
****************************************************************************/
import QtQuick 2.0
+import QtTest 1.0
import QtWebChannel 1.0
+import "qrc:///qwebchannel/qwebchannel.js" as Client
-WebChannelTest {
+TestCase {
name: "MetaObjectPublisher"
- id: test
+
+ Client {
+ id: client
+ }
property var lastMethodArg
@@ -90,62 +95,71 @@ WebChannelTest {
}
}
- function initTestCase()
- {
- webChannel.registeredObjects = [myObj, myOtherObj, myFactory];
+ WebChannel {
+ id: webChannel
+ transports: [client.serverTransport]
+ registeredObjects: [myObj, myOtherObj, myFactory]
}
- function awaitMessageSkipIdle()
+ function init()
{
- var msg;
- do {
- msg = awaitMessage();
- verify(msg);
- verify(msg.data);
- } while (msg.data.type === qWebChannelMessageTypes.idle);
- return msg;
+ myObj.myProperty = 1
+ client.cleanup();
}
function test_property()
{
- myObj.myProperty = 1
- loadUrl("property.html");
- awaitInit();
- var msg = awaitMessageSkipIdle();
+ var channel = client.createChannel(function(channel) {
+ channel.exec({label: "init", value: channel.objects.myObj.myProperty});
+ channel.objects.myObj.myPropertyChanged.connect(function() {
+ channel.exec({label: "changed", value: channel.objects.myObj.myProperty});
+ });
+ channel.subscribe("setProperty", function(newValue) {
+ channel.objects.myObj.myProperty = newValue;
+ });
+ });
+
+ client.awaitInit();
+ var msg = client.awaitMessageSkipIdle();
compare(msg.data.label, "init");
compare(msg.data.value, 1);
compare(myObj.myProperty, 1);
// change property, should be propagated to HTML client and a message be send there
myObj.myProperty = 2;
- msg = awaitMessageSkipIdle();
+ msg = client.awaitMessageSkipIdle();
compare(msg.data.label, "changed");
compare(msg.data.value, 2);
compare(myObj.myProperty, 2);
// now trigger a write from the client side
webChannel.sendMessage("setProperty", 3);
- msg = awaitMessageSkipIdle();
+ msg = client.awaitMessageSkipIdle();
compare(myObj.myProperty, 3);
// the above write is also propagated to the HTML client
- msg = awaitMessageSkipIdle();
+ msg = client.awaitMessageSkipIdle();
compare(msg.data.label, "changed");
compare(msg.data.value, 3);
- awaitIdle();
+ client.awaitIdle();
}
function test_method()
{
- loadUrl("method.html");
- awaitInit();
- awaitIdle();
+ var channel = client.createChannel(function (channel) {
+ channel.subscribe("invokeMethod", function (arg) {
+ channel.objects.myObj.myMethod(arg);
+ });
+ });
+
+ client.awaitInit();
+ client.awaitIdle();
webChannel.sendMessage("invokeMethod", "test");
- var msg = awaitMessage();
- compare(msg.data.type, qWebChannelMessageTypes.invokeMethod);
+ var msg = client.awaitMessage();
+ compare(msg.data.type, Client.QWebChannelMessageTypes.invokeMethod);
compare(msg.data.object, "myObj");
compare(msg.data.args, ["test"]);
@@ -154,27 +168,35 @@ WebChannelTest {
function test_signal()
{
- loadUrl("signal.html");
- awaitInit();
-
- var msg = awaitMessage();
- compare(msg.data.type, qWebChannelMessageTypes.connectToSignal);
+ var channel = client.createChannel(function(channel) {
+ channel.objects.myObj.mySignal.connect(function(arg) {
+ channel.exec({label: "signalReceived", value: arg});
+ });
+ });
+ client.awaitInit();
+
+ var msg = client.awaitMessage();
+ compare(msg.data.type, Client.QWebChannelMessageTypes.connectToSignal);
compare(msg.data.object, "myObj");
- awaitIdle();
+ client.awaitIdle();
myObj.mySignal("test");
- msg = awaitMessage();
+ msg = client.awaitMessageSkipIdle();
compare(msg.data.label, "signalReceived");
compare(msg.data.value, "test");
}
function test_grouping()
{
- loadUrl("grouping.html");
- awaitInit();
- awaitIdle();
+ var channel = client.createChannel(function(channel) {
+ channel.subscribe(Client.QWebChannelMessageTypes.propertyUpdate, function() {
+ channel.exec({label: "gotPropertyUpdate", values: [channel.objects.myObj.myProperty, channel.objects.myOtherObj.foo, channel.objects.myOtherObj.bar]});
+ });
+ });
+ client.awaitInit();
+ client.awaitIdle();
// change properties a lot, we expect this to be grouped into a single update notification
for (var i = 0; i < 10; ++i) {
@@ -183,81 +205,106 @@ WebChannelTest {
myOtherObj.bar = i;
}
- var msg = awaitMessage();
+ var msg = client.awaitMessage();
verify(msg);
compare(msg.data.label, "gotPropertyUpdate");
compare(msg.data.values, [myObj.myProperty, myOtherObj.foo, myOtherObj.bar]);
- awaitIdle();
+ client.awaitIdle();
}
function test_wrapper()
{
- loadUrl("wrapper.html");
- awaitInit();
-
- var msg = awaitMessageSkipIdle();
- compare(msg.data.type, qWebChannelMessageTypes.invokeMethod);
+ var channel = client.createChannel(function(channel) {
+ channel.objects.myFactory.create("testObj", function(obj) {
+ channel.objects["testObj"] = obj;
+ obj.mySignal.connect(function(arg1, arg2) {
+ channel.exec({label: "signalReceived", args: [arg1, arg2]});
+ });
+ obj.myProperty = 42;
+ obj.myMethod("foobar");
+ });
+ channel.subscribe("triggerDelete", function() {
+ channel.objects.testObj.deleteLater();
+ });
+ channel.subscribe("report", function() {
+ channel.exec({label:"report", obj: channel.objects.testObj})
+ });
+ });
+ client.awaitInit();
+
+ var msg = client.awaitMessageSkipIdle();
+ compare(msg.data.type, Client.QWebChannelMessageTypes.invokeMethod);
compare(msg.data.object, "myFactory");
verify(myFactory.lastObj);
compare(myFactory.lastObj.objectName, "testObj");
- msg = awaitMessageSkipIdle();
- compare(msg.data.type, qWebChannelMessageTypes.connectToSignal);
+ msg = client.awaitMessageSkipIdle();
+ compare(msg.data.type, Client.QWebChannelMessageTypes.connectToSignal);
verify(msg.data.object);
var objId = msg.data.object;
- msg = awaitMessageSkipIdle();
- compare(msg.data.type, qWebChannelMessageTypes.connectToSignal);
+ msg = client.awaitMessageSkipIdle();
+ compare(msg.data.type, Client.QWebChannelMessageTypes.connectToSignal);
compare(msg.data.object, objId);
- msg = awaitMessageSkipIdle();
- compare(msg.data.type, qWebChannelMessageTypes.setProperty);
+ msg = client.awaitMessageSkipIdle();
+ compare(msg.data.type, Client.QWebChannelMessageTypes.setProperty);
compare(msg.data.object, objId);
compare(myFactory.lastObj.myProperty, 42);
- msg = awaitMessageSkipIdle();
- compare(msg.data.type, qWebChannelMessageTypes.invokeMethod);
+ msg = client.awaitMessageSkipIdle();
+ compare(msg.data.type, Client.QWebChannelMessageTypes.invokeMethod);
compare(msg.data.object, objId);
compare(msg.data.args, ["foobar"]);
- msg = awaitMessageSkipIdle();
+ msg = client.awaitMessageSkipIdle();
compare(msg.data.label, "signalReceived");
compare(msg.data.args, ["foobar", 42]);
// pass QObject* on the fly and trigger deleteLater from client side
webChannel.sendMessage("triggerDelete");
- msg = awaitMessageSkipIdle();
- compare(msg.data.type, qWebChannelMessageTypes.invokeMethod);
+ msg = client.awaitMessageSkipIdle();
+ compare(msg.data.type, Client.QWebChannelMessageTypes.invokeMethod);
compare(msg.data.object, objId);
+ client.awaitIdle();
+
webChannel.sendMessage("report");
- msg = awaitMessageSkipIdle();
+ msg = client.awaitMessageSkipIdle();
compare(msg.data.label, "report");
compare(msg.data.obj, {});
}
function test_disconnect()
{
- loadUrl("disconnect.html");
- awaitInit();
-
- var msg = awaitMessage();
- compare(msg.data.type, qWebChannelMessageTypes.connectToSignal);
+ var channel = client.createChannel(function(channel) {
+ channel.objects.myObj.mySignal.connect(function(arg) {
+ channel.exec({label: "mySignalReceived", args: [arg]});
+ channel.objects.myObj.mySignal.disconnect(this);
+ });
+ channel.subscribe("report", function() {
+ channel.exec({label: "report"});
+ });
+ });
+ client.awaitInit();
+
+ var msg = client.awaitMessage();
+ compare(msg.data.type, Client.QWebChannelMessageTypes.connectToSignal);
compare(msg.data.object, "myObj");
- awaitIdle();
+ client.awaitIdle();
myObj.mySignal(42);
- msg = awaitMessage();
+ msg = client.awaitMessage();
compare(msg.data.label, "mySignalReceived");
compare(msg.data.args, [42]);
- msg = awaitMessage();
- compare(msg.data.type, qWebChannelMessageTypes.disconnectFromSignal);
+ msg = client.awaitMessage();
+ compare(msg.data.type, Client.QWebChannelMessageTypes.disconnectFromSignal);
compare(msg.data.object, "myObj");
myObj.mySignal(0);
@@ -266,7 +313,7 @@ WebChannelTest {
// and verify no mySignalReceived was triggered by the above emission
webChannel.sendMessage("report");
- msg = awaitMessage();
+ msg = client.awaitMessage();
compare(msg.data.label, "report");
}
}
diff --git a/tests/auto/qml/tst_multiclient.qml b/tests/auto/qml/tst_multiclient.qml
index 519a63c..6c4b9b7 100644
--- a/tests/auto/qml/tst_multiclient.qml
+++ b/tests/auto/qml/tst_multiclient.qml
@@ -40,12 +40,21 @@
****************************************************************************/
import QtQuick 2.0
+import QtTest 1.0
import QtWebChannel 1.0
+import "qrc:///qwebchannel/qwebchannel.js" as Client
-WebChannelTest {
+TestCase {
name: "MultiClient"
- id: test
+
+ Client {
+ id: client1
+ }
+
+ Client {
+ id: client2
+ }
QtObject {
id: foo
@@ -61,39 +70,50 @@ WebChannelTest {
WebChannel.id: "foo"
}
- TestWebView {
- id: client1
+ WebChannel {
+ id: webChannel
+ transports: [client1.serverTransport, client2.serverTransport]
+ registeredObjects: [foo]
}
- TestWebView {
- id: client2
+
+ function init()
+ {
+ client1.cleanup();
+ client2.cleanup();
}
- function initTestCase()
+ function clientInitCallback(channel)
{
- webChannel.registeredObjects = [foo];
+ channel.objects.foo.ping.connect(function() {
+ channel.objects.foo.pong(function(value) {
+ channel.exec({pongAnswer: value});
+ });
+ });
}
function test_multiclient()
{
- loadUrl("multiclient.html", client1);
- loadUrl("multiclient.html", client2);
+ var c1 = client1.createChannel(clientInitCallback);
+ var c2 = client2.createChannel(clientInitCallback);
+
// init, connect & idle messages for two clients
- for (var i = 0; i < 3 * 2; ++i) {
- awaitMessage();
+ for (var i = 0; i < 3; ++i) {
+ client1.awaitMessage();
+ client2.awaitMessage();
}
foo.ping();
// invoke of pong method
- awaitMessage();
- awaitMessage();
+ client1.awaitMessage();
+ client2.awaitMessage();
- var msg = awaitMessage();
+ var msg = client1.awaitMessage();
compare(msg.data.pongAnswer, 1);
- msg = awaitMessage();
+ msg = client2.awaitMessage();
compare(msg.data.pongAnswer, 2);
- awaitIdle();
- awaitIdle();
+ client1.awaitIdle();
+ client2.awaitIdle();
}
}
diff --git a/tests/auto/qml/tst_webchannel.qml b/tests/auto/qml/tst_webchannel.qml
index dddba71..3c404d3 100644
--- a/tests/auto/qml/tst_webchannel.qml
+++ b/tests/auto/qml/tst_webchannel.qml
@@ -40,20 +40,47 @@
****************************************************************************/
import QtQuick 2.0
+import QtTest 1.0
-WebChannelTest {
+import QtWebChannel 1.0
+import "qrc:///qwebchannel/qwebchannel.js" as Client
+
+TestCase {
name: "WebChannel"
+ Client {
+ id: client
+ }
+
+ WebChannel {
+ id: webChannel
+ transports: [client.serverTransport]
+ }
+
+ function cleanup()
+ {
+ client.cleanup();
+ }
+
function test_receiveRawMessage()
{
- loadUrl("receiveRaw.html");
- compare(awaitRawMessage(), "foobar");
+ var channel = client.createChannel(function (channel) {
+ channel.send("foobar");
+ }, true /* raw */);
+ compare(client.awaitRawMessage(), "foobar");
}
function test_sendMessage()
{
- loadUrl("send.html");
+ var channel = client.createChannel(function (channel) {
+ channel.subscribe("myMessage", function(payload) {
+ channel.send("myMessagePong:" + payload);
+ });
+ channel.send("initialized");
+ }, true /* raw */);
+
+ compare(client.awaitRawMessage(), "initialized");
webChannel.sendMessage("myMessage", "foobar");
- compare(awaitRawMessage(), "myMessagePong:foobar");
+ compare(client.awaitRawMessage(), "myMessagePong:foobar");
}
}