From 97c876a1353f29ed0129360f013f2529bed69d98 Mon Sep 17 00:00:00 2001 From: Milian Wolff Date: Wed, 25 May 2016 16:42:01 +0200 Subject: Enable calling C++ functions taking QJson arguments via webchannel. We used to convert the QJsonValue arguments to QVariants, which then failed to call a C++ function which expected on of the three QJson data types, i.e. QJsonValue, QJsonObject or QJsonArray. Instead, we now detect these three cases and manually convert the QJsonValue as needed. [ChangeLog] C++ functions taking arguments of type QJsonValue, QJsonArray or QJsonObject can now be called via the Qt WebChannel. Change-Id: I94e0c8937ca35e2ecd3554f7ddf2d4e5a3328570 Task-number: QTBUG-48198 Reviewed-by: Simon Hausmann --- tests/auto/webchannel/tst_webchannel.cpp | 41 ++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'tests/auto/webchannel/tst_webchannel.cpp') diff --git a/tests/auto/webchannel/tst_webchannel.cpp b/tests/auto/webchannel/tst_webchannel.cpp index f1911e5..4db8bae 100644 --- a/tests/auto/webchannel/tst_webchannel.cpp +++ b/tests/auto/webchannel/tst_webchannel.cpp @@ -216,6 +216,21 @@ void TestWebChannel::setVariant(const QVariant &v) m_lastVariant = v; } +void TestWebChannel::setJsonValue(const QJsonValue& v) +{ + m_lastJsonValue = v; +} + +void TestWebChannel::setJsonObject(const QJsonObject& v) +{ + m_lastJsonValue = v; +} + +void TestWebChannel::setJsonArray(const QJsonArray& v) +{ + m_lastJsonValue = v; +} + void TestWebChannel::testRegisterObjects() { QWebChannel channel; @@ -426,6 +441,32 @@ 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_lastJsonValue.toObject(), 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_lastJsonValue.toArray(), array); + } } void TestWebChannel::testDisconnect() -- cgit v1.2.1