diff options
author | Qt Forward Merge Bot <qt_forward_merge_bot@qt-project.org> | 2020-05-14 21:06:39 +0200 |
---|---|---|
committer | Qt Forward Merge Bot <qt_forward_merge_bot@qt-project.org> | 2020-05-14 21:06:39 +0200 |
commit | 2398037151babcb817c94463fc240c7694f73d4c (patch) | |
tree | 7ba502f85a57a3aedceb893f379e6a5436f680e7 /tests | |
parent | 8554d71f47f85b111f095ea8f25c3a72c5cae32e (diff) | |
parent | 3342da908955187a4543cdc38e0fad2e78e936e7 (diff) |
Merge remote-tracking branch 'origin/5.15' into dev
Change-Id: I6417958acf66455b993c65f51b58f15955aa95f4
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/webchannel/tst_webchannel.cpp | 41 | ||||
-rw-r--r-- | tests/auto/webchannel/tst_webchannel.h | 13 |
2 files changed, 48 insertions, 6 deletions
diff --git a/tests/auto/webchannel/tst_webchannel.cpp b/tests/auto/webchannel/tst_webchannel.cpp index c47cd6b..a1e824b 100644 --- a/tests/auto/webchannel/tst_webchannel.cpp +++ b/tests/auto/webchannel/tst_webchannel.cpp @@ -183,6 +183,20 @@ void TestJSEngine::initWebChannelJS() #endif // WEBCHANNEL_TESTS_CAN_USE_JS_ENGINE +namespace { +QVariantList convert_to_js(const TestStructVector &list) +{ + QVariantList ret; + ret.reserve(list.size()); + std::transform(list.begin(), list.end(), std::back_inserter(ret), [](const TestStruct &value) -> QVariant { + QVariantMap map; + map["foo"] = value.foo; + map["bar"] = value.bar; + return map; + }); + return ret; +} +} TestWebChannel::TestWebChannel(QObject *parent) : QObject(parent) @@ -191,6 +205,9 @@ TestWebChannel::TestWebChannel(QObject *parent) , m_lastBool(false) , m_lastDouble(0) { + qRegisterMetaType<TestStruct>(); + qRegisterMetaType<TestStructVector>(); + QMetaType::registerConverter<TestStructVector, QVariantList>(convert_to_js); } TestWebChannel::~TestWebChannel() @@ -864,6 +881,14 @@ void TestWebChannel::testWrapValues() QVERIFY(value.isArray()); QCOMPARE(value.toArray(), QJsonArray({1, 2, 3})); } + { + TestStructVector vec{{1, 2}, {3, 4}}; + QVariant variant = QVariant::fromValue(vec); + QJsonValue value = channel.d_func()->publisher->wrapResult(variant, m_dummyTransport); + QVERIFY(value.isArray()); + QCOMPARE(value.toArray(), QJsonArray({QJsonObject{{"foo", 1}, {"bar", 2}}, + QJsonObject{{"foo", 3}, {"bar", 4}}})); + } } void TestWebChannel::testWrapObjectWithMultipleTransports() @@ -930,10 +955,13 @@ void TestWebChannel::testAsyncObject() args.append(QJsonValue("message")); { - QSignalSpy spy(&obj, &TestObject::propChanged); + int received = 0; + connect(&obj, &TestObject::propChanged, this, [&](const QString &arg) { + QCOMPARE(arg, args.at(0).toString()); + ++received; + }); channel.d_func()->publisher->invokeMethod(&obj, "setProp", args); - QTRY_COMPARE(spy.count(), 1); - QCOMPARE(spy.at(0).at(0).toString(), args.at(0).toString()); + QTRY_COMPARE(received, 1); } channel.registerObject("myObj", &obj); @@ -946,12 +974,13 @@ void TestWebChannel::testAsyncObject() channel.d_func()->publisher->handleMessage(connectMessage, m_dummyTransport); { - QSignalSpy spy(&obj, &TestObject::replay); + int received = 0; + connect(&obj, &TestObject::replay, this, [&]() { ++received; }); QMetaObject::invokeMethod(&obj, "fire"); - QTRY_COMPARE(spy.count(), 1); + QTRY_COMPARE(received, 1); channel.deregisterObject(&obj); QMetaObject::invokeMethod(&obj, "fire"); - QTRY_COMPARE(spy.count(), 2); + QTRY_COMPARE(received, 2); } thread.quit(); diff --git a/tests/auto/webchannel/tst_webchannel.h b/tests/auto/webchannel/tst_webchannel.h index 7cfce06..eae21f4 100644 --- a/tests/auto/webchannel/tst_webchannel.h +++ b/tests/auto/webchannel/tst_webchannel.h @@ -39,6 +39,19 @@ #include <QtWebChannel/QWebChannelAbstractTransport> +struct TestStruct +{ + TestStruct(int foo = 0, int bar = 0) + : foo(foo) + , bar(bar) + {} + int foo; + int bar; +}; +Q_DECLARE_METATYPE(TestStruct) +using TestStructVector = std::vector<TestStruct>; +Q_DECLARE_METATYPE(TestStructVector) + QT_BEGIN_NAMESPACE class DummyTransport : public QWebChannelAbstractTransport |