summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEdward Welbourne <edward.welbourne@qt.io>2016-07-15 21:24:23 +0200
committerEdward Welbourne <edward.welbourne@qt.io>2016-07-15 21:24:23 +0200
commit3836fd7d12777fc53836a72d6542d2fbf2f20ac9 (patch)
treeffd8a3d1c82fea142d7b50fb42111ceb1fbc2e1a
parentb84f22a3249aa1af46b17e33ac030abb077251bf (diff)
parente35346cf70598c47a456946327927643910aa277 (diff)
downloadqtwebchannel-3836fd7d12777fc53836a72d6542d2fbf2f20ac9.tar.gz
Merge remote-tracking branch 'origin/5.6' into 5.7
Alleged Conflicts: examples/webchannel/chatclient-html/doc/src/chatclient-html.qdoc examples/webchannel/chatclient-qml/doc/src/chatclient-qml.qdoc examples/webchannel/chatserver-cpp/doc/src/chatserver-cpp.qdoc In each case, the two sides agreed byte-for-byte. Not quite sure what git thought the conflict was ! Change-Id: I5da9695b667f4112848c520b630ab1304d61cea3
-rw-r--r--.qmake.conf2
-rw-r--r--LICENSE.GPLv22
-rw-r--r--LICENSE.GPLv32
-rw-r--r--LICENSE.LGPLv212
-rw-r--r--LICENSE.LGPLv32
-rw-r--r--src/webchannel/doc/src/index.qdoc40
-rw-r--r--src/webchannel/doc/src/javascript.qdoc17
-rw-r--r--src/webchannel/qmetaobjectpublisher.cpp66
-rw-r--r--src/webchannel/qmetaobjectpublisher_p.h9
-rw-r--r--src/webchannel/qqmlwebchannel.cpp21
-rw-r--r--src/webchannel/qwebchannel.cpp10
-rw-r--r--src/webchannel/qwebchannel.js18
-rw-r--r--src/webchannel/qwebchannelabstracttransport.cpp7
-rw-r--r--tests/auto/qml/tst_webchannel.qml19
-rw-r--r--tests/auto/webchannel/tst_webchannel.cpp205
-rw-r--r--tests/auto/webchannel/tst_webchannel.h51
16 files changed, 409 insertions, 64 deletions
diff --git a/.qmake.conf b/.qmake.conf
index b4f392a..40e5787 100644
--- a/.qmake.conf
+++ b/.qmake.conf
@@ -1,4 +1,4 @@
load(qt_build_config)
-CONFIG += qt_example_installs warning_clean
+CONFIG += warning_clean
MODULE_VERSION = 5.7.0
diff --git a/LICENSE.GPLv2 b/LICENSE.GPLv2
index 6dbb032..a424477 100644
--- a/LICENSE.GPLv2
+++ b/LICENSE.GPLv2
@@ -3,7 +3,7 @@
The Qt Toolkit is Copyright (C) 2015 The Qt Company Ltd.
Contact: http://www.qt.io/licensing/
- You may use, distribute and copy the Qt GUI Toolkit under the terms of
+ You may use, distribute and copy the Qt Toolkit under the terms of
GNU General Public License version 2, which is displayed below.
-------------------------------------------------------------------------
diff --git a/LICENSE.GPLv3 b/LICENSE.GPLv3
index 4e49b12..71c4ad4 100644
--- a/LICENSE.GPLv3
+++ b/LICENSE.GPLv3
@@ -3,7 +3,7 @@
The Qt Toolkit is Copyright (C) 2015 The Qt Company Ltd.
Contact: http://www.qt.io/licensing/
- You may use, distribute and copy the Qt GUI Toolkit under the terms of
+ You may use, distribute and copy the Qt Toolkit under the terms of
GNU Lesser General Public License version 3. That license references
the General Public License version 3, that is displayed below. Other
portions of the Qt Toolkit may be licensed directly under this license.
diff --git a/LICENSE.LGPLv21 b/LICENSE.LGPLv21
index 6e18461..dfcab5e 100644
--- a/LICENSE.LGPLv21
+++ b/LICENSE.LGPLv21
@@ -3,7 +3,7 @@
The Qt Toolkit is Copyright (C) 2015 The Qt Company Ltd.
Contact: http://www.qt.io/licensing/
- You may use, distribute and copy the Qt GUI Toolkit under the terms of
+ You may use, distribute and copy the Qt Toolkit under the terms of
GNU Lesser General Public License version 2.1, which is displayed below.
-------------------------------------------------------------------------
diff --git a/LICENSE.LGPLv3 b/LICENSE.LGPLv3
index 4d67bac..6bf924c 100644
--- a/LICENSE.LGPLv3
+++ b/LICENSE.LGPLv3
@@ -3,7 +3,7 @@
The Qt Toolkit is Copyright (C) 2015 The Qt Company Ltd.
Contact: http://www.qt.io/licensing/
- You may use, distribute and copy the Qt GUI Toolkit under the terms of
+ You may use, distribute and copy the Qt Toolkit under the terms of
GNU Lesser General Public License version 3, which is displayed below.
This license makes reference to the version 3 of the GNU General
Public License, which you can find in the LICENSE.GPLv3 file.
diff --git a/src/webchannel/doc/src/index.qdoc b/src/webchannel/doc/src/index.qdoc
index 5f354bd..43f4e6f 100644
--- a/src/webchannel/doc/src/index.qdoc
+++ b/src/webchannel/doc/src/index.qdoc
@@ -31,26 +31,50 @@
\title Qt WebChannel
\brief Bridges the gap between Qt applications and HTML/JavaScript.
- Qt WebChannel enables peer-to-peer communication between the host
- (QML/C++ application) and the client (HTML/JavaScript application). The
- transport mechanism is supported out of the box by the two popular web
- engines, Qt WebKit 2 and Qt WebEngine (experimental). It works on all
- browsers that support Qt WebSockets, enabling Qt WebChannel applications
+ Qt WebChannel enables peer-to-peer communication between the server
+ (QML/C++ application) and the client (HTML/JavaScript or QML application). The
+ transport mechanism is supported out of the box by \l{Qt WebEngine}. It works on all
+ browsers that support \l{Qt WebSockets}, enabling Qt WebChannel applications
to run in any JavaScript runtime. Additionally, a custom transport
mechanism can also be implemented using Qt WebSockets to support Qt
WebChannel-based communication.
The module provides a JavaScript library for seamless integration of C++
- and QML applications with HTML/JavaScript clients. The client must use the
+ and QML applications with HTML/JavaScript and QML clients. The clients must use the
JavaScript library to access the serialized QObjects published by the host
applications.
- \section1 Related Information
+ \section1 Getting Started
+
+ To use these classes in your application, use the following include
+ statement:
+
+ \code
+ #include <QtWebChannel/QtWebChannel>
+ \endcode
+
+ To link against the module, add this line to your \l qmake \c .pro file:
+
+ \code
+ QT += webchannel
+ \endcode
+
+ The QML types are accessed by using:
+ \badcode
+ import QtWebChannel 1.0
+ \endcode
+
+ \section1 API Reference
\list
\li \l{Qt WebChannel JavaScript API}{JavaScript API}
\li \l{Qt WebChannel C++ Classes}{C++ API}
\li \l{Qt WebChannel QML Types}{QML API}
- \li \l{Qt WebChannel Examples}{Examples} - show how use the API in practice
+ \endlist
+
+ \section1 Examples
+
+ \list
+ \li \l{Qt WebChannel Examples}{Examples}
\endlist
*/
diff --git a/src/webchannel/doc/src/javascript.qdoc b/src/webchannel/doc/src/javascript.qdoc
index cfb5b21..a508ff9 100644
--- a/src/webchannel/doc/src/javascript.qdoc
+++ b/src/webchannel/doc/src/javascript.qdoc
@@ -31,21 +31,20 @@
\brief This page explains how to use the JavaScript QWebChannel API in HTML clients.
- \section1 Setup
+ \section1 Setting up the JavaScript API
- To communicate with a QWebChannel or WebChannel, any HTML client must use and setup the
- JavaScript API provided by \c qwebchannel.js. For HTML clients run inside Qt WebKit, you
- can load the file via \c qrc:///qtwebchannel/qwebchannel.js. For external clients you will
- need to copy the file to your webserver. Then instantiate a QWebChannel object and pass
+ To communicate with a QWebChannel or \l [QML] WebChannel, a client must use and set up the
+ JavaScript API provided by \c qwebchannel.js. For clients run inside \l{Qt WebEngine}, you
+ can load the file via \c qrc:///qtwebchannel/qwebchannel.js. For external clients, you
+ need to copy the file to your web server. Then instantiate a QWebChannel object and pass
it a transport object and a callback function, which will be invoked once the
- initialization of the channel finished and published objects become available.
+ initialization of the channel finishes and the published objects become available.
The transport object implements a minimal message passing interface. It should be an object
with a \c send() function, which takes a stringified JSON message and transmits it to the
server-side QWebChannelAbstractTransport object. Furthermore, its \c onmessage property should
- be called when a message from the server was received. This interface is implemented internally
- by the Qt WebKit navigator.qtWebChannelTransport object. Alternatively, you can also use a
- WebSocket, which also implements this interface.
+ be called when a message from the server was received. Alternatively, you can use a
+ \l{Qt WebSockets}{WebSocket} to implement the interface.
Note that the JavaScript QWebChannel object should be constructed once the transport object is
fully operational. In case of a WebSocket, that means you should create the QWebChannel in the
diff --git a/src/webchannel/qmetaobjectpublisher.cpp b/src/webchannel/qmetaobjectpublisher.cpp
index a0c3af7..e9d93ea 100644
--- a/src/webchannel/qmetaobjectpublisher.cpp
+++ b/src/webchannel/qmetaobjectpublisher.cpp
@@ -372,11 +372,7 @@ QVariant QMetaObjectPublisher::invokeMethod(QObject *const object, const int met
// construct converter objects of QVariant to QGenericArgument
VariantArgument arguments[10];
for (int i = 0; i < qMin(args.size(), method.parameterCount()); ++i) {
- QVariant arg = args.at(i).toVariant();
- if (method.parameterType(i) != QMetaType::QVariant && !arg.convert(method.parameterType(i))) {
- qWarning() << "Could not convert argument" << args.at(i) << "to target type" << method.parameterTypes().at(i) << '.';
- }
- arguments[i].value = arg;
+ arguments[i].value = toVariant(args.at(i), method.parameterType(i));
}
// construct QGenericReturnArgument
@@ -397,6 +393,16 @@ QVariant QMetaObjectPublisher::invokeMethod(QObject *const object, const int met
return returnValue;
}
+void QMetaObjectPublisher::setProperty(QObject *object, const int propertyIndex, const QJsonValue &value)
+{
+ QMetaProperty property = object->metaObject()->property(propertyIndex);
+ if (!property.isValid()) {
+ qWarning() << "Cannot set unknown property" << propertyIndex << "of object" << object;
+ } else if (!property.write(object, toVariant(value, property.userType()))) {
+ qWarning() << "Could not write value " << value << "to property" << property.name() << "of object" << object;
+ }
+}
+
void QMetaObjectPublisher::signalEmitted(const QObject *object, const int signalIndex, const QVariantList &arguments)
{
if (!webChannel || webChannel->d_func()->transports.isEmpty()) {
@@ -449,6 +455,46 @@ void QMetaObjectPublisher::objectDestroyed(const QObject *object)
pendingPropertyUpdates.remove(object);
}
+QObject *QMetaObjectPublisher::unwrapObject(const QString &objectId) const
+{
+ if (!objectId.isEmpty()) {
+ ObjectInfo objectInfo = wrappedObjects.value(objectId);
+ if (objectInfo.object && !objectInfo.classinfo.isEmpty())
+ return objectInfo.object;
+ }
+
+ qWarning() << "No wrapped object" << objectId;
+ return Q_NULLPTR;
+}
+
+QVariant QMetaObjectPublisher::toVariant(const QJsonValue &value, int targetType) const
+{
+ if (targetType == QMetaType::QJsonValue) {
+ return QVariant::fromValue(value);
+ } else if (targetType == QMetaType::QJsonArray) {
+ if (!value.isArray())
+ qWarning() << "Cannot not convert non-array argument" << value << "to QJsonArray.";
+ return QVariant::fromValue(value.toArray());
+ } else if (targetType == QMetaType::QJsonObject) {
+ if (!value.isObject())
+ qWarning() << "Cannot not convert non-object argument" << value << "to QJsonObject.";
+ return QVariant::fromValue(value.toObject());
+ } else if (QMetaType::typeFlags(targetType) & QMetaType::PointerToQObject) {
+ QObject *unwrappedObject = unwrapObject(value.toObject()[KEY_ID].toString());
+ if (unwrappedObject == Q_NULLPTR)
+ qWarning() << "Cannot not convert non-object argument" << value << "to QObject*.";
+ return QVariant::fromValue(unwrappedObject);
+ }
+
+ // this converts QJsonObjects to QVariantMaps, which is not desired when
+ // we want to get a QJsonObject or QJsonValue (see above)
+ QVariant variant = value.toVariant();
+ if (targetType != QMetaType::QVariant && !variant.convert(targetType)) {
+ qWarning() << "Could not convert argument" << value << "to target type" << QVariant::typeToName(targetType) << '.';
+ }
+ return variant;
+}
+
// NOTE: transport can be a nullptr
// in such a case, we need to ensure that the property is registered to
// the target transports of the parentObjectId
@@ -594,14 +640,8 @@ void QMetaObjectPublisher::handleMessage(const QJsonObject &message, QWebChannel
} else if (type == TypeDisconnectFromSignal) {
signalHandler.disconnectFrom(object, message.value(KEY_SIGNAL).toInt(-1));
} else if (type == TypeSetProperty) {
- const int propertyIdx = message.value(KEY_PROPERTY).toInt(-1);
- QMetaProperty property = object->metaObject()->property(propertyIdx);
- if (!property.isValid()) {
- qWarning() << "Cannot set unknown property" << message.value(KEY_PROPERTY) << "of object" << objectName;
- } else if (!object->metaObject()->property(propertyIdx).write(object, message.value(KEY_VALUE).toVariant())) {
- qWarning() << "Could not write value " << message.value(KEY_VALUE)
- << "to property" << property.name() << "of object" << objectName;
- }
+ setProperty(object, message.value(KEY_PROPERTY).toInt(-1),
+ message.value(KEY_VALUE));
}
}
}
diff --git a/src/webchannel/qmetaobjectpublisher_p.h b/src/webchannel/qmetaobjectpublisher_p.h
index f5109b6..29956e0 100644
--- a/src/webchannel/qmetaobjectpublisher_p.h
+++ b/src/webchannel/qmetaobjectpublisher_p.h
@@ -156,6 +156,11 @@ public:
QVariant invokeMethod(QObject *const object, const int methodIndex, const QJsonArray &args);
/**
+ * Set the value of property @p propertyIndex on @p object to @p value.
+ */
+ void setProperty(QObject *object, const int propertyIndex, const QJsonValue &value);
+
+ /**
* Callback of the signalHandler which forwards the signal invocation to the webchannel clients.
*/
void signalEmitted(const QObject *object, const int signalIndex, const QVariantList &arguments);
@@ -167,6 +172,10 @@ public:
*/
void objectDestroyed(const QObject *object);
+ QObject *unwrapObject(const QString &objectId) const;
+
+ QVariant toVariant(const QJsonValue &value, int targetType) const;
+
/**
* Given a QVariant containing a QObject*, wrap the object and register for property updates
* return the objects class information.
diff --git a/src/webchannel/qqmlwebchannel.cpp b/src/webchannel/qqmlwebchannel.cpp
index 264bc54..d23ebef 100644
--- a/src/webchannel/qqmlwebchannel.cpp
+++ b/src/webchannel/qqmlwebchannel.cpp
@@ -69,7 +69,7 @@ QT_BEGIN_NAMESPACE
A list of transport objects, which implement QWebChannelAbstractTransport. The transports
are used to talk to the remote clients.
- \sa WebChannel::connectTo(), WebChannel::disconnectFrom()
+ \sa connectTo(), disconnectFrom()
*/
/*!
@@ -77,7 +77,7 @@ QT_BEGIN_NAMESPACE
\brief A list of objects which should be accessible to remote clients.
- The objects must have the attached WebChannel::id property set to an identifier, under which the
+ The objects must have the attached \l id property set to an identifier, under which the
object is then known on the HTML side.
Once registered, all signals and property changes are automatically propagated to the clients.
@@ -86,7 +86,7 @@ QT_BEGIN_NAMESPACE
If one needs to register objects which are not available when the component is created, use the
imperative registerObjects method.
- \sa WebChannel::registerObjects(), WebChannel::id
+ \sa registerObjects(), id
*/
class QQmlWebChannelPrivate : public QWebChannelPrivate
@@ -134,8 +134,8 @@ QQmlWebChannel::~QQmlWebChannel()
/*!
\qmlmethod void WebChannel::registerObjects(QVariantMap objects)
- Register objects to make them accessible to HTML clients. The key of the map is used as an identifier
- for the object on the client side.
+ Registers objects to make them accessible to HTML clients. The key of the
+ map is used as an identifier for the object on the client side.
Once registered, all signals and property changes are automatically propagated to the clients.
Public invokable methods, including slots, are also accessible to the clients.
@@ -143,7 +143,7 @@ QQmlWebChannel::~QQmlWebChannel()
This imperative API can be used to register objects on the fly. For static objects, the declarative
registeredObjects property should be preferred.
- \sa WebChannel::registeredObjects
+ \sa registeredObjects
*/
void QQmlWebChannel::registerObjects(const QVariantMap &objects)
{
@@ -167,11 +167,12 @@ QQmlWebChannelAttached *QQmlWebChannel::qmlAttachedProperties(QObject *obj)
/*!
\qmlmethod void WebChannel::connectTo(QWebChannelAbstractTransport transport)
- \brief Connectect to the \a transport, which represents a communication channel to a single client.
+ \brief Connects to the \a transport, which represents a communication
+ channel to a single client.
The transport object must be an implementation of QWebChannelAbstractTransport.
- \sa WebChannel::transports, WebChannel::disconnectFrom()
+ \sa transports, disconnectFrom()
*/
void QQmlWebChannel::connectTo(QObject *transport)
{
@@ -185,12 +186,12 @@ void QQmlWebChannel::connectTo(QObject *transport)
/*!
\qmlmethod void WebChannel::disconnectFrom(QWebChannelAbstractTransport transport)
- \brief Disconnect the \a transport from this WebChannel.
+ \brief Disconnects the \a transport from this WebChannel.
The client will not be able to communicate with the WebChannel anymore, nor will it receive any
signals or property updates.
- \sa WebChannel::connectTo()
+ \sa connectTo()
*/
void QQmlWebChannel::disconnectFrom(QObject *transport)
{
diff --git a/src/webchannel/qwebchannel.cpp b/src/webchannel/qwebchannel.cpp
index b7c62e9..c2e0d19 100644
--- a/src/webchannel/qwebchannel.cpp
+++ b/src/webchannel/qwebchannel.cpp
@@ -52,7 +52,7 @@ QT_BEGIN_NAMESPACE
\class QWebChannel
\inmodule QtWebChannel
- \brief Expose QObjects to remote HTML clients.
+ \brief Exposes QObjects to remote HTML clients.
\since 5.4
The QWebChannel fills the gap between C++ applications and HTML/JavaScript
@@ -69,7 +69,7 @@ QT_BEGIN_NAMESPACE
features used by \c{qwebchannel.js}. As such, one can interact
with basically any modern HTML browser or standalone JavaScript runtime, such as node.js.
- There also exists a declarative WebChannel API.
+ There also exists a declarative \l{Qt WebChannel QML Types}{WebChannel API}.
\sa {Qt WebChannel Standalone Example}, {Qt WebChannel JavaScript API}{JavaScript API}
*/
@@ -136,7 +136,7 @@ QWebChannel::~QWebChannel()
}
/*!
- Register a group of objects to the QWebChannel.
+ Registers a group of objects to the QWebChannel.
The properties, signals and public invokable methods of the objects are published to the remote clients.
There, an object with the identifier used as key in the \a objects map is then constructed.
@@ -166,7 +166,7 @@ QHash<QString, QObject *> QWebChannel::registeredObjects() const
}
/*!
- Register a single object to the QWebChannel.
+ Registers a single object to the QWebChannel.
The properties, signals and public methods of the \a object are published to the remote clients.
There, an object with the identifier \a id is then constructed.
@@ -182,7 +182,7 @@ void QWebChannel::registerObject(const QString &id, QObject *object)
}
/*!
- Deregister the given \a object from the QWebChannel.
+ Deregisters the given \a object from the QWebChannel.
Remote clients will receive a \c destroyed signal for the given object.
diff --git a/src/webchannel/qwebchannel.js b/src/webchannel/qwebchannel.js
index d39e301..5b047c2 100644
--- a/src/webchannel/qwebchannel.js
+++ b/src/webchannel/qwebchannel.js
@@ -319,7 +319,7 @@ function QObject(name, data, webChannel)
this.signalEmitted = function(signalName, signalArgs)
{
- invokeSignalCallbacks(signalName, signalArgs);
+ invokeSignalCallbacks(signalName, this.unwrapQObject(signalArgs));
}
function addMethod(methodData)
@@ -330,10 +330,15 @@ function QObject(name, data, webChannel)
var args = [];
var callback;
for (var i = 0; i < arguments.length; ++i) {
- if (typeof arguments[i] === "function")
- callback = arguments[i];
+ var argument = arguments[i];
+ if (typeof argument === "function")
+ callback = argument;
+ else if (argument instanceof QObject && webChannel.objects[argument.__id__] !== undefined)
+ args.push({
+ "id": argument.__id__
+ });
else
- args.push(arguments[i]);
+ args.push(argument);
}
webChannel.exec({
@@ -387,11 +392,14 @@ function QObject(name, data, webChannel)
return;
}
object.__propertyCache__[propertyIndex] = value;
+ var valueToSend = value;
+ if (valueToSend instanceof QObject && webChannel.objects[valueToSend.__id__] !== undefined)
+ valueToSend = { "id": valueToSend.__id__ };
webChannel.exec({
"type": QWebChannelMessageTypes.setProperty,
"object": object.__id__,
"property": propertyIndex,
- "value": value
+ "value": valueToSend
});
}
});
diff --git a/src/webchannel/qwebchannelabstracttransport.cpp b/src/webchannel/qwebchannelabstracttransport.cpp
index 414c9fe..86a9c18 100644
--- a/src/webchannel/qwebchannelabstracttransport.cpp
+++ b/src/webchannel/qwebchannelabstracttransport.cpp
@@ -50,9 +50,8 @@ QT_BEGIN_NAMESPACE
Users of the QWebChannel must implement this interface and connect instances of it
to the QWebChannel server for every client that should be connected to the QWebChannel.
- The {Qt WebChannel Standalone Example}{Standalone Example} shows how this can be done
- using Qt WebSockets. Qt WebKit implements this interface internally and uses the native
- WebKit IPC mechanism to transmit messages to HTML clients.
+ The \l{Qt WebChannel Standalone Example} shows how this can be done
+ using \l{Qt WebSockets}.
\note The JSON message protocol is considered internal and might change over time.
@@ -69,7 +68,7 @@ QT_BEGIN_NAMESPACE
/*!
\fn QWebChannelAbstractTransport::sendMessage(const QJsonObject &message)
- Send a JSON \a message to the remote client. An implementation would serialize the message and
+ Sends a JSON \a message to the remote client. An implementation would serialize the message and
transmit it to the remote JavaScript client.
*/
diff --git a/tests/auto/qml/tst_webchannel.qml b/tests/auto/qml/tst_webchannel.qml
index 403cc4b..5e28db3 100644
--- a/tests/auto/qml/tst_webchannel.qml
+++ b/tests/auto/qml/tst_webchannel.qml
@@ -46,7 +46,7 @@ TestCase {
id: myObj
property int myProperty: 1
- signal mySignal(var arg)
+ signal mySignal(var arg, QtObject object)
function myMethod(arg)
{
@@ -169,9 +169,11 @@ TestCase {
function test_signal()
{
var signalReceivedArg;
+ var signalReceivedObject;
var channel = client.createChannel(function(channel) {
- channel.objects.myObj.mySignal.connect(function(arg) {
+ channel.objects.myObj.mySignal.connect(function(arg, object) {
signalReceivedArg = arg;
+ signalReceivedObject = object;
});
});
client.awaitInit();
@@ -182,9 +184,16 @@ TestCase {
client.awaitIdle(); // initialization
- myObj.mySignal("test");
+ myObj.mySignal("test", myObj);
compare(signalReceivedArg, "test");
+ compare(signalReceivedObject.__id__, "myObj");
+
+ var newObj = myFactory.create("newObj");
+ myObj.mySignal(newObj, newObj);
+
+ compare(signalReceivedArg.objectName, newObj.objectName);
+ compare(signalReceivedObject.objectName, newObj.objectName);
}
function test_grouping()
@@ -388,14 +397,14 @@ TestCase {
client.awaitIdle();
- myObj.mySignal(42);
+ myObj.mySignal(42, myObj);
compare(signalArg, 42);
msg = client.awaitMessage();
compare(msg.type, JSClient.QWebChannelMessageTypes.disconnectFromSignal);
compare(msg.object, "myObj");
- myObj.mySignal(0);
+ myObj.mySignal(0, myObj);
compare(signalArg, 42);
}
}
diff --git a/tests/auto/webchannel/tst_webchannel.cpp b/tests/auto/webchannel/tst_webchannel.cpp
index 7e8e5f5..a8a658f 100644
--- a/tests/auto/webchannel/tst_webchannel.cpp
+++ b/tests/auto/webchannel/tst_webchannel.cpp
@@ -187,6 +187,7 @@ TestWebChannel::TestWebChannel(QObject *parent)
: QObject(parent)
, m_dummyTransport(new DummyTransport(this))
, m_lastInt(0)
+ , m_lastBool(false)
, m_lastDouble(0)
{
}
@@ -196,19 +197,81 @@ TestWebChannel::~TestWebChannel()
}
+int TestWebChannel::readInt() const
+{
+ return m_lastInt;
+}
+
void TestWebChannel::setInt(int i)
{
m_lastInt = i;
+ emit lastIntChanged();
+}
+
+bool TestWebChannel::readBool() const
+{
+ return m_lastBool;
+}
+
+void TestWebChannel::setBool(bool b)
+{
+ m_lastBool = b;
+ emit lastBoolChanged();
+}
+
+double TestWebChannel::readDouble() const
+{
+ return m_lastDouble;
}
void TestWebChannel::setDouble(double d)
{
m_lastDouble = d;
+ emit lastDoubleChanged();
+}
+
+QVariant TestWebChannel::readVariant() const
+{
+ return m_lastVariant;
}
void TestWebChannel::setVariant(const QVariant &v)
{
m_lastVariant = v;
+ emit lastVariantChanged();
+}
+
+QJsonValue TestWebChannel::readJsonValue() const
+{
+ return m_lastJsonValue;
+}
+
+void TestWebChannel::setJsonValue(const QJsonValue& v)
+{
+ m_lastJsonValue = v;
+ emit lastJsonValueChanged();
+}
+
+QJsonObject TestWebChannel::readJsonObject() const
+{
+ return m_lastJsonObject;
+}
+
+void TestWebChannel::setJsonObject(const QJsonObject& v)
+{
+ m_lastJsonObject = v;
+ emit lastJsonObjectChanged();
+}
+
+QJsonArray TestWebChannel::readJsonArray() const
+{
+ return m_lastJsonArray;
+}
+
+void TestWebChannel::setJsonArray(const QJsonArray& v)
+{
+ m_lastJsonArray = v;
+ emit lastJsonArrayChanged();
}
void TestWebChannel::testRegisterObjects()
@@ -290,6 +353,12 @@ void TestWebChannel::testInfoForObject()
}
{
QJsonArray method;
+ method.append(QStringLiteral("setReturnedObject"));
+ method.append(obj.metaObject()->indexOfMethod("setReturnedObject(TestObject*)"));
+ expected.append(method);
+ }
+ {
+ QJsonArray method;
method.append(QStringLiteral("setObjectProperty"));
method.append(obj.metaObject()->indexOfMethod("setObjectProperty(QObject*)"));
expected.append(method);
@@ -391,6 +460,19 @@ void TestWebChannel::testInfoForObject()
property.append(QJsonValue::fromVariant(QVariant::fromValue(obj.objectProperty())));
expected.append(property);
}
+ {
+ QJsonArray property;
+ property.append(obj.metaObject()->indexOfProperty("returnedObject"));
+ property.append(QStringLiteral("returnedObject"));
+ {
+ QJsonArray signal;
+ signal.append(1);
+ signal.append(obj.metaObject()->indexOfMethod("returnedObjectChanged()"));
+ property.append(signal);
+ }
+ property.append(QJsonValue::fromVariant(QVariant::fromValue(obj.returnedObject())));
+ expected.append(property);
+ }
QCOMPARE(info["properties"].toArray(), expected);
}
}
@@ -410,6 +492,14 @@ void TestWebChannel::testInvokeMethodConversion()
QCOMPARE(m_lastInt, args.at(0).toInt());
}
{
+ int method = metaObject()->indexOfMethod("setBool(bool)");
+ QVERIFY(method != -1);
+ QJsonArray args;
+ args.append(QJsonValue(!m_lastBool));
+ channel.d_func()->publisher->invokeMethod(this, method, args);
+ QCOMPARE(m_lastBool, args.at(0).toBool());
+ }
+ {
int method = metaObject()->indexOfMethod("setDouble(double)");
QVERIFY(method != -1);
channel.d_func()->publisher->invokeMethod(this, method, args);
@@ -421,6 +511,90 @@ void TestWebChannel::testInvokeMethodConversion()
channel.d_func()->publisher->invokeMethod(this, method, args);
QCOMPARE(m_lastVariant, args.at(0).toVariant());
}
+ {
+ int method = metaObject()->indexOfMethod("setJsonValue(QJsonValue)");
+ QVERIFY(method != -1);
+ channel.d_func()->publisher->invokeMethod(this, method, args);
+ QCOMPARE(m_lastJsonValue, args.at(0));
+ }
+ {
+ int method = metaObject()->indexOfMethod("setJsonObject(QJsonObject)");
+ QVERIFY(method != -1);
+ QJsonObject object;
+ object["foo"] = QJsonValue(123);
+ object["bar"] = QJsonValue(4.2);
+ args[0] = object;
+ channel.d_func()->publisher->invokeMethod(this, method, args);
+ QCOMPARE(m_lastJsonObject, object);
+ }
+ {
+ int method = metaObject()->indexOfMethod("setJsonArray(QJsonArray)");
+ QVERIFY(method != -1);
+ QJsonArray array;
+ array << QJsonValue(123);
+ array << QJsonValue(4.2);
+ args[0] = array;
+ channel.d_func()->publisher->invokeMethod(this, method, args);
+ QCOMPARE(m_lastJsonArray, array);
+ }
+}
+
+void TestWebChannel::testSetPropertyConversion()
+{
+ QWebChannel channel;
+ channel.connectTo(m_dummyTransport);
+
+ {
+ int property = metaObject()->indexOfProperty("lastInt");
+ QVERIFY(property != -1);
+ channel.d_func()->publisher->setProperty(this, property, QJsonValue(42));
+ QCOMPARE(m_lastInt, 42);
+ }
+ {
+ int property = metaObject()->indexOfProperty("lastBool");
+ QVERIFY(property != -1);
+ bool newValue = !m_lastBool;
+ channel.d_func()->publisher->setProperty(this, property, QJsonValue(newValue));
+ QCOMPARE(m_lastBool, newValue);
+ }
+ {
+ int property = metaObject()->indexOfProperty("lastDouble");
+ QVERIFY(property != -1);
+ channel.d_func()->publisher->setProperty(this, property, QJsonValue(-4.2));
+ QCOMPARE(m_lastDouble, -4.2);
+ }
+ {
+ int property = metaObject()->indexOfProperty("lastVariant");
+ QVERIFY(property != -1);
+ QVariant variant("foo bar asdf");
+ channel.d_func()->publisher->setProperty(this, property, QJsonValue::fromVariant(variant));
+ QCOMPARE(m_lastVariant, variant);
+ }
+ {
+ int property = metaObject()->indexOfProperty("lastJsonValue");
+ QVERIFY(property != -1);
+ QJsonValue value("asdf asdf");
+ channel.d_func()->publisher->setProperty(this, property, value);
+ QCOMPARE(m_lastJsonValue, value);
+ }
+ {
+ int property = metaObject()->indexOfProperty("lastJsonArray");
+ QVERIFY(property != -1);
+ QJsonArray array;
+ array << QJsonValue(-123);
+ array << QJsonValue(-42);
+ channel.d_func()->publisher->setProperty(this, property, array);
+ QCOMPARE(m_lastJsonArray, array);
+ }
+ {
+ int property = metaObject()->indexOfProperty("lastJsonObject");
+ QVERIFY(property != -1);
+ QJsonObject object;
+ object["foo"] = QJsonValue(-123);
+ object["bar"] = QJsonValue(-4.2);
+ channel.d_func()->publisher->setProperty(this, property, object);
+ QCOMPARE(m_lastJsonObject, object);
+ }
}
void TestWebChannel::testDisconnect()
@@ -455,6 +629,36 @@ void TestWebChannel::testWrapRegisteredObject()
QCOMPARE(obj.objectName(), returnedId);
}
+void TestWebChannel::testPassWrappedObjectBack()
+{
+ QWebChannel channel;
+ TestObject registeredObj;
+ TestObject returnedObjMethod;
+ TestObject returnedObjProperty;
+
+ registeredObj.setObjectName("registeredObject");
+
+ channel.registerObject(registeredObj.objectName(), &registeredObj);
+ channel.connectTo(m_dummyTransport);
+ channel.d_func()->publisher->initializeClient(m_dummyTransport);
+
+ QMetaObjectPublisher *pub = channel.d_func()->publisher;
+ QJsonObject returnedObjMethodInfo = pub->wrapResult(QVariant::fromValue(&returnedObjMethod), m_dummyTransport).toObject();
+ QJsonObject returnedObjPropertyInfo = pub->wrapResult(QVariant::fromValue(&returnedObjProperty), m_dummyTransport).toObject();
+
+ QJsonArray argsMethod;
+ QJsonObject argMethod0;
+ argMethod0["id"] = returnedObjMethodInfo["id"];
+ argsMethod << argMethod0;
+ QJsonObject argProperty;
+ argProperty["id"] = returnedObjPropertyInfo["id"];
+
+ pub->invokeMethod(&registeredObj, registeredObj.metaObject()->indexOfSlot("setReturnedObject(TestObject*)"), argsMethod);
+ QCOMPARE(registeredObj.mReturnedObject, &returnedObjMethod);
+ pub->setProperty(&registeredObj, registeredObj.metaObject()->indexOfProperty("returnedObject"), argProperty);
+ QCOMPARE(registeredObj.mReturnedObject, &returnedObjProperty);
+}
+
void TestWebChannel::testInfiniteRecursion()
{
QWebChannel channel;
@@ -607,6 +811,7 @@ void TestWebChannel::qtbug46548_overriddenProperties()
#endif // WEBCHANNEL_TESTS_CAN_USE_JS_ENGINE
}
+
QTEST_MAIN(TestWebChannel)
#include "tst_webchannel.moc"
diff --git a/tests/auto/webchannel/tst_webchannel.h b/tests/auto/webchannel/tst_webchannel.h
index 98733dc..b46f21b 100644
--- a/tests/auto/webchannel/tst_webchannel.h
+++ b/tests/auto/webchannel/tst_webchannel.h
@@ -31,6 +31,9 @@
#include <QObject>
#include <QVariant>
+#include <QJsonValue>
+#include <QJsonObject>
+#include <QJsonArray>
#include <QtWebChannel/QWebChannelAbstractTransport>
@@ -65,11 +68,13 @@ class TestObject : public QObject
Q_PROPERTY(int asdf READ asdf NOTIFY asdfChanged)
Q_PROPERTY(QString bar READ bar NOTIFY theBarHasChanged)
Q_PROPERTY(QObject * objectProperty READ objectProperty WRITE setObjectProperty NOTIFY objectPropertyChanged)
+ Q_PROPERTY(TestObject * returnedObject READ returnedObject WRITE setReturnedObject NOTIFY returnedObjectChanged)
public:
explicit TestObject(QObject *parent = 0)
: QObject(parent)
, mObjectProperty(0)
+ , mReturnedObject(Q_NULLPTR)
{ }
enum Foo {
@@ -86,6 +91,11 @@ public:
return mObjectProperty;
}
+ TestObject *returnedObject() const
+ {
+ return mReturnedObject;
+ }
+
Q_INVOKABLE void method1() {}
protected:
@@ -100,11 +110,18 @@ signals:
void asdfChanged();
void theBarHasChanged();
void objectPropertyChanged();
+ void returnedObjectChanged();
public slots:
void slot1() {}
void slot2(const QString&) {}
+ void setReturnedObject(TestObject *obj)
+ {
+ mReturnedObject = obj;
+ emit returnedObjectChanged();
+ }
+
void setObjectProperty(QObject *object)
{
mObjectProperty = object;
@@ -119,6 +136,7 @@ private slots:
public:
QObject *mObjectProperty;
+ TestObject *mReturnedObject;
};
class BenchObject : public QObject
@@ -217,21 +235,50 @@ class TestWebChannel : public QObject
{
Q_OBJECT
+ Q_PROPERTY(int lastInt READ readInt WRITE setInt NOTIFY lastIntChanged);
+ Q_PROPERTY(bool lastBool READ readBool WRITE setBool NOTIFY lastBoolChanged);
+ Q_PROPERTY(double lastDouble READ readDouble WRITE setDouble NOTIFY lastDoubleChanged);
+ Q_PROPERTY(QVariant lastVariant READ readVariant WRITE setVariant NOTIFY lastVariantChanged);
+ Q_PROPERTY(QJsonValue lastJsonValue READ readJsonValue WRITE setJsonValue NOTIFY lastJsonValueChanged);
+ Q_PROPERTY(QJsonObject lastJsonObject READ readJsonObject WRITE setJsonObject NOTIFY lastJsonObjectChanged);
+ Q_PROPERTY(QJsonArray lastJsonArray READ readJsonArray WRITE setJsonArray NOTIFY lastJsonArrayChanged);
public:
explicit TestWebChannel(QObject *parent = 0);
virtual ~TestWebChannel();
+ int readInt() const;
Q_INVOKABLE void setInt(int i);
+ bool readBool() const;
+ Q_INVOKABLE void setBool(bool b);
+ double readDouble() const;
Q_INVOKABLE void setDouble(double d);
+ QVariant readVariant() const;
Q_INVOKABLE void setVariant(const QVariant &v);
+ QJsonValue readJsonValue() const;
+ Q_INVOKABLE void setJsonValue(const QJsonValue &v);
+ QJsonObject readJsonObject() const;
+ Q_INVOKABLE void setJsonObject(const QJsonObject &v);
+ QJsonArray readJsonArray() const;
+ Q_INVOKABLE void setJsonArray(const QJsonArray &v);
+
+signals:
+ void lastIntChanged();
+ void lastBoolChanged();
+ void lastDoubleChanged();
+ void lastVariantChanged();
+ void lastJsonValueChanged();
+ void lastJsonObjectChanged();
+ void lastJsonArrayChanged();
private slots:
void testRegisterObjects();
void testDeregisterObjects();
void testInfoForObject();
void testInvokeMethodConversion();
+ void testSetPropertyConversion();
void testDisconnect();
void testWrapRegisteredObject();
+ void testPassWrappedObjectBack();
void testInfiniteRecursion();
void benchClassInfo();
@@ -245,8 +292,12 @@ private:
DummyTransport *m_dummyTransport;
int m_lastInt;
+ bool m_lastBool;
double m_lastDouble;
QVariant m_lastVariant;
+ QJsonValue m_lastJsonValue;
+ QJsonObject m_lastJsonObject;
+ QJsonArray m_lastJsonArray;
};
QT_END_NAMESPACE