summaryrefslogtreecommitdiff
path: root/tests/auto/webchannel
diff options
context:
space:
mode:
authorKai Dohmen <psykai1993@googlemail.com>2016-04-11 19:38:50 +0200
committerMilian Wolff <milian.wolff@kdab.com>2016-06-21 09:50:33 +0000
commitf48e8c9711fbeb350ccf70f852ce3732844d4287 (patch)
tree43756259e8794b89801cdc2aa936840af92c5d98 /tests/auto/webchannel
parent3be67a7799aa1960d459548a43ddf54044f80ec1 (diff)
downloadqtwebchannel-f48e8c9711fbeb350ccf70f852ce3732844d4287.tar.gz
Make passing objects from website to server possible
If you get an object from the server and want to pass it back to the server via a function the id of the object is passed instead of the whole json object. On the server side QMetaObjectPublisher::invokeMethod now looks up the object in QMetaObjectPublisher::wrappedObjects by the passed object-id. Task-number: QTBUG-50075 Change-Id: Id0df2dfaa79bcba12ca48391ae7537ac1a086898 Reviewed-by: Milian Wolff <milian.wolff@kdab.com>
Diffstat (limited to 'tests/auto/webchannel')
-rw-r--r--tests/auto/webchannel/tst_webchannel.cpp50
-rw-r--r--tests/auto/webchannel/tst_webchannel.h16
2 files changed, 66 insertions, 0 deletions
diff --git a/tests/auto/webchannel/tst_webchannel.cpp b/tests/auto/webchannel/tst_webchannel.cpp
index 93c7aa8..7ae6f78 100644
--- a/tests/auto/webchannel/tst_webchannel.cpp
+++ b/tests/auto/webchannel/tst_webchannel.cpp
@@ -345,6 +345,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);
@@ -446,6 +452,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);
}
}
@@ -587,6 +606,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;
@@ -739,6 +788,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 13294c2..5a9fa11 100644
--- a/tests/auto/webchannel/tst_webchannel.h
+++ b/tests/auto/webchannel/tst_webchannel.h
@@ -73,11 +73,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 {
@@ -94,6 +96,11 @@ public:
return mObjectProperty;
}
+ TestObject *returnedObject() const
+ {
+ return mReturnedObject;
+ }
+
Q_INVOKABLE void method1() {}
protected:
@@ -108,11 +115,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;
@@ -127,6 +141,7 @@ private slots:
public:
QObject *mObjectProperty;
+ TestObject *mReturnedObject;
};
class BenchObject : public QObject
@@ -264,6 +279,7 @@ private slots:
void testSetPropertyConversion();
void testDisconnect();
void testWrapRegisteredObject();
+ void testPassWrappedObjectBack();
void testInfiniteRecursion();
void benchClassInfo();