From e060d27a5783b8f2a8024ca0cce8a85f65e6d630 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Mon, 29 Feb 2016 09:34:07 +0100 Subject: [PATCH] add checkNotifications --- tests/auto/guhtestbase.cpp | 23 +++++++++++++++++++++++ tests/auto/guhtestbase.h | 1 + tests/auto/jsonrpc/testjsonrpc.cpp | 20 +++++++++++++++----- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/tests/auto/guhtestbase.cpp b/tests/auto/guhtestbase.cpp index d5401559..733aed7f 100644 --- a/tests/auto/guhtestbase.cpp +++ b/tests/auto/guhtestbase.cpp @@ -177,6 +177,27 @@ QVariant GuhTestBase::checkNotification(const QSignalSpy &spy, const QString &no return QVariant(); } +QVariantList GuhTestBase::checkNotifications(const QSignalSpy &spy, const QString ¬ification) +{ + qDebug() << "Got" << spy.count() << "notifications while waiting for" << notification; + QVariantList notificationList; + for (int i = 0; i < spy.count(); i++) { + // Make sure the response it a valid JSON string + QJsonParseError error; + QJsonDocument jsonDoc = QJsonDocument::fromJson(spy.at(i).last().toByteArray(), &error); + if (error.error != QJsonParseError::NoError) { + qWarning() << "JSON parser error" << error.errorString(); + return notificationList; + } + + QVariantMap response = jsonDoc.toVariant().toMap(); + if (response.value("notification").toString() == notification) { + notificationList.append(jsonDoc.toVariant()); + } + } + return notificationList; +} + QVariant GuhTestBase::getAndWait(const QNetworkRequest &request, const int &expectedStatus) { QNetworkAccessManager nam; @@ -323,6 +344,7 @@ bool GuhTestBase::enableNotifications() if (response.toMap().value("params").toMap().value("enabled").toBool() != true) { return false; } + qDebug() << "Notifications enabled."; return true; } @@ -334,6 +356,7 @@ bool GuhTestBase::disableNotifications() if (response.toMap().value("params").toMap().value("enabled").toBool() != false) { return false; } + qDebug() << "Notifications disabled."; return true; } diff --git a/tests/auto/guhtestbase.h b/tests/auto/guhtestbase.h index 84181a58..cf540740 100644 --- a/tests/auto/guhtestbase.h +++ b/tests/auto/guhtestbase.h @@ -72,6 +72,7 @@ protected slots: protected: QVariant injectAndWait(const QString &method, const QVariantMap ¶ms = QVariantMap()); QVariant checkNotification(const QSignalSpy &spy, const QString ¬ification); + QVariantList checkNotifications(const QSignalSpy &spy, const QString ¬ification); QVariant getAndWait(const QNetworkRequest &request, const int &expectedStatus = 200); QVariant deleteAndWait(const QNetworkRequest &request, const int &expectedStatus = 200); diff --git a/tests/auto/jsonrpc/testjsonrpc.cpp b/tests/auto/jsonrpc/testjsonrpc.cpp index 231923da..01dd548a 100644 --- a/tests/auto/jsonrpc/testjsonrpc.cpp +++ b/tests/auto/jsonrpc/testjsonrpc.cpp @@ -215,15 +215,25 @@ void TestJSONRPC::stateChangeEmitsNotifications() clientSpy.wait(); // Make sure the notification contains all the stuff we expect - QVariant stateChangedVariant = checkNotification(clientSpy, "Devices.StateChanged"); - QVERIFY2(!stateChangedVariant.isNull(), "Did not get Devices.StateChanged notification."); - QCOMPARE(stateChangedVariant.toMap().value("params").toMap().value("stateTypeId").toUuid(), stateTypeId); - QCOMPARE(stateChangedVariant.toMap().value("params").toMap().value("value").toInt(), newVal); + QVariantList stateChangedVariants = checkNotifications(clientSpy, "Devices.StateChanged"); + QVERIFY2(!stateChangedVariants.isEmpty(), "Did not get Devices.StateChanged notification."); + + qDebug() << "got" << stateChangedVariants.count() << "Devices.StateChanged notifications"; + + bool found = false; + foreach (const QVariant &stateChangedVariant, stateChangedVariants) { + if (stateChangedVariant.toMap().value("params").toMap().value("stateTypeId").toUuid() == stateTypeId) + { + QCOMPARE(stateChangedVariant.toMap().value("params").toMap().value("value").toInt(), newVal); + found = true; + break; + } + } + QCOMPARE(found, true); // Make sure the notification contains all the stuff we expect QVariant loggEntryAddedVariant = checkNotification(clientSpy, "Logging.LogEntryAdded"); QVERIFY2(!loggEntryAddedVariant.isNull(), "Did not get Logging.LogEntryAdded notification."); - QCOMPARE(loggEntryAddedVariant.toMap().value("params").toMap().value("logEntry").toMap().value("typeId").toUuid(), stateTypeId); // Make sure the notification contains all the stuff we expect QVariant eventTriggeredVariant = checkNotification(clientSpy, "Events.EventTriggered");