From 384690bb23d17b017ec86fca14209899d9cf5a3d Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Sun, 4 May 2014 04:49:19 +0200 Subject: [PATCH] add a test for async device creation --- .../deviceplugins/mock/devicepluginmock.cpp | 28 ++++++++++++++++++- plugins/deviceplugins/mock/devicepluginmock.h | 2 ++ server/jsonrpc/devicehandler.cpp | 4 ++- tests/auto/devices/testdevices.cpp | 24 ++++++++-------- tests/auto/guhtestbase.cpp | 7 +++++ tests/auto/guhtestbase.h | 2 ++ tests/auto/jsonrpc/testjsonrpc.cpp | 8 +++--- 7 files changed, 57 insertions(+), 18 deletions(-) diff --git a/plugins/deviceplugins/mock/devicepluginmock.cpp b/plugins/deviceplugins/mock/devicepluginmock.cpp index f052103c..036e4c7e 100644 --- a/plugins/deviceplugins/mock/devicepluginmock.cpp +++ b/plugins/deviceplugins/mock/devicepluginmock.cpp @@ -29,6 +29,7 @@ VendorId guhVendorId = VendorId("2062d64d-3232-433c-88bc-0d33c0ba2ba6"); DeviceClassId mockDeviceClassId = DeviceClassId("753f0d32-0468-4d08-82ed-1964aab03298"); DeviceClassId mockDeviceAutoClassId = DeviceClassId("ab4257b3-7548-47ee-9bd4-7dc3004fd197"); DeviceClassId mockDeviceDiscoveryClassId = DeviceClassId("1bbaf751-36b7-4d3d-b05a-58dab2a3be8c"); +DeviceClassId mockDeviceAsyncSetupClassId = DeviceClassId("c08a8b27-8200-413d-b96b-4cff78b864d9"); EventTypeId mockEvent1Id = EventTypeId("45bf3752-0fc6-46b9-89fd-ffd878b5b22b"); EventTypeId mockEvent2Id = EventTypeId("863d5920-b1cf-4eb9-88bd-8f7b8583b1cf"); StateTypeId mockIntStateId = StateTypeId("80baec19-54de-4948-ac46-31eabfaceb83"); @@ -142,6 +143,20 @@ QList DevicePluginMock::supportedDevices() const ret.append(deviceClassMockDiscovery); + // Async setup device + DeviceClass deviceClassMockAsync(pluginId(), guhVendorId, mockDeviceAsyncSetupClassId); + deviceClassMockAsync.setName("Mock Device (Async setup)"); + deviceClassMockAsync.setCreateMethod(DeviceClass::CreateMethodUser); + + mockParams.clear(); + mockParams.append(portParam); + deviceClassMockAsync.setParams(mockParams); + deviceClassMockAsync.setStates(mockStates); + deviceClassMockAsync.setEvents(mockEvents); + deviceClassMockAsync.setActions(mockActions); + + ret.append(deviceClassMockAsync); + return ret; } @@ -183,12 +198,17 @@ QPair DevicePluginMock::setupDevice(D connect(daemon, &HttpDaemon::triggerEvent, this, &DevicePluginMock::triggerEvent); connect(daemon, &HttpDaemon::setState, this, &DevicePluginMock::setState); + if (device->deviceClassId() == mockDeviceAsyncSetupClassId) { + m_asyncSetupDevices.append(device); + QTimer::singleShot(1000, this, SLOT(emitDeviceSetupFinished())); + return reportDeviceSetup(DeviceManager::DeviceSetupStatusAsync); + } return reportDeviceSetup(); } void DevicePluginMock::deviceRemoved(Device *device) { - m_daemons.take(device)->deleteLater(); + delete m_daemons.take(device); } bool DevicePluginMock::configureAutoDevice(QList loadedDevices, Device *device) const @@ -262,3 +282,9 @@ void DevicePluginMock::emitDevicesDiscovered() emit devicesDiscovered(mockDeviceDiscoveryClassId, deviceDescriptors); } + +void DevicePluginMock::emitDeviceSetupFinished() +{ + qDebug() << "emitting setup finised"; + emit deviceSetupFinished(m_asyncSetupDevices.takeFirst(), DeviceManager::DeviceSetupStatusSuccess, QString()); +} diff --git a/plugins/deviceplugins/mock/devicepluginmock.h b/plugins/deviceplugins/mock/devicepluginmock.h index 0f26acf4..cdaeebfe 100644 --- a/plugins/deviceplugins/mock/devicepluginmock.h +++ b/plugins/deviceplugins/mock/devicepluginmock.h @@ -56,9 +56,11 @@ private slots: void setState(const StateTypeId &stateTypeId, const QVariant &value); void triggerEvent(const EventTypeId &id); void emitDevicesDiscovered(); + void emitDeviceSetupFinished(); private: QHash m_daemons; + QList m_asyncSetupDevices; }; #endif // DEVICEPLUGINMOCK_H diff --git a/server/jsonrpc/devicehandler.cpp b/server/jsonrpc/devicehandler.cpp index 24e05f57..8d97a2e9 100644 --- a/server/jsonrpc/devicehandler.cpp +++ b/server/jsonrpc/devicehandler.cpp @@ -155,6 +155,7 @@ DeviceHandler::DeviceHandler(QObject *parent) : connect(GuhCore::instance()->deviceManager(), &DeviceManager::deviceStateChanged, this, &DeviceHandler::deviceStateChanged); connect(GuhCore::instance()->deviceManager(), &DeviceManager::devicesDiscovered, this, &DeviceHandler::devicesDiscovered); + connect(GuhCore::instance()->deviceManager(), &DeviceManager::deviceSetupFinished, this, &DeviceHandler::deviceSetupFinished); } QString DeviceHandler::name() const @@ -435,6 +436,7 @@ void DeviceHandler::devicesDiscovered(const DeviceClassId &deviceClassId, const void DeviceHandler::deviceSetupFinished(Device *device, DeviceManager::DeviceError status) { + qDebug() << "got a device setup finished"; if (!m_asynDeviceAdditions.contains(device->id())) { return; // Not the device we're waiting for... } @@ -446,7 +448,7 @@ void DeviceHandler::deviceSetupFinished(Device *device, DeviceManager::DeviceErr if(status == DeviceManager::DeviceErrorNoError) { returns.insert("success", true); returns.insert("errorMessage", ""); - returns.insert("deviceId", device->deviceClassId()); + returns.insert("deviceId", device->id()); } else if (status == DeviceManager::DeviceErrorSetupFailed) { returns.insert("errorMessage", QString("Error creating device. Device setup failed.")); returns.insert("success", false); diff --git a/tests/auto/devices/testdevices.cpp b/tests/auto/devices/testdevices.cpp index 3866c237..c4386380 100644 --- a/tests/auto/devices/testdevices.cpp +++ b/tests/auto/devices/testdevices.cpp @@ -64,8 +64,8 @@ void TestDevices::getSupportedDevices_data() QTest::addColumn("vendorId"); QTest::addColumn("resultCount"); - QTest::newRow("vendor guh") << guhVendorId << 3; - QTest::newRow("no filter") << VendorId() << 3; + QTest::newRow("vendor guh") << guhVendorId << 4; + QTest::newRow("no filter") << VendorId() << 4; QTest::newRow("invalid vendor") << VendorId("93e7d361-8025-4354-b17e-b68406c800bc") << 0; } @@ -100,6 +100,7 @@ void TestDevices::addConfiguredDevice_data() QTest::newRow("User, JustAdd") << mockDeviceClassId << deviceParams << true; QTest::newRow("Auto, JustAdd") << mockDeviceAutoClassId << deviceParams << false; QTest::newRow("Discovery, JustAdd") << mockDeviceDiscoveryClassId << deviceParams << false; + QTest::newRow("User, JustAdd, Async") << mockDeviceAsyncSetupClassId << deviceParams << true; QVariantMap invalidDeviceParams; invalidDeviceParams.insert("tropptth", m_mockDevice1Port - 1); @@ -119,15 +120,14 @@ void TestDevices::addConfiguredDevice() QVariant response = injectAndWait("Devices.AddConfiguredDevice", params); qDebug() << "response is" << response; - QCOMPARE(response.toMap().value("status").toString(), QString("success")); - QCOMPARE(response.toMap().value("params").toMap().value("success").toBool(), success); + verifySuccess(response, success); if (success) { QUuid deviceId(response.toMap().value("params").toMap().value("deviceId").toString()); params.clear(); params.insert("deviceId", deviceId.toString()); - injectAndWait("Devices.RemoveConfiguredDevice", params); - QCOMPARE(response.toMap().value("params").toMap().value("success").toBool(), true); + response = injectAndWait("Devices.RemoveConfiguredDevice", params); + verifySuccess(response); } } @@ -153,7 +153,7 @@ void TestDevices::removeDevice() QVariant response = injectAndWait("Devices.RemoveConfiguredDevice", params); - QCOMPARE(response.toMap().value("params").toMap().value("success").toBool(), true); + verifySuccess(response); // Make sure the device is gone from settings too QCOMPARE(settings.allKeys().count(), 0); @@ -167,7 +167,7 @@ void TestDevices::storedDevices() deviceParams.insert("httpport", 8888); params.insert("deviceParams", deviceParams); QVariant response = injectAndWait("Devices.AddConfiguredDevice", params); - QCOMPARE(response.toMap().value("params").toMap().value("success").toBool(), true); + verifySuccess(response); DeviceId addedDeviceId = DeviceId(response.toMap().value("params").toMap().value("deviceId").toString()); QVERIFY(!addedDeviceId.isNull()); @@ -196,6 +196,7 @@ void TestDevices::storedDevices() params.clear(); params.insert("deviceId", addedDeviceId); response = injectAndWait("Devices.RemoveConfiguredDevice", params); + verifySuccess(response); } void TestDevices::discoverDevices() @@ -213,14 +214,13 @@ void TestDevices::discoverDevices() params.insert("deviceDescriptorId", descriptorId.toString()); response = injectAndWait("Devices.AddConfiguredDevice", params); - QCOMPARE(response.toMap().value("status").toString(), QString("success")); - QCOMPARE(response.toMap().value("params").toMap().value("success").toBool(), true); + verifySuccess(response); DeviceId deviceId(response.toMap().value("params").toMap().value("deviceId").toString()); params.clear(); params.insert("deviceId", deviceId.toString()); - injectAndWait("Devices.RemoveConfiguredDevice", params); - QCOMPARE(response.toMap().value("params").toMap().value("success").toBool(), true); + response = injectAndWait("Devices.RemoveConfiguredDevice", params); + verifySuccess(response); } #include "testdevices.moc" diff --git a/tests/auto/guhtestbase.cpp b/tests/auto/guhtestbase.cpp index 9e4b7547..01702a3a 100644 --- a/tests/auto/guhtestbase.cpp +++ b/tests/auto/guhtestbase.cpp @@ -98,3 +98,10 @@ QVariant GuhTestBase::injectAndWait(const QString &method, const QVariantMap &pa return jsonDoc.toVariant(); } + +void GuhTestBase::verifySuccess(const QVariant &response, bool success) +{ + QJsonDocument jsonDoc = QJsonDocument::fromVariant(response); + QVERIFY2(response.toMap().value("status").toString() == QString("success"), jsonDoc.toJson().data()); + QVERIFY2(response.toMap().value("params").toMap().value("success").toBool() == success, jsonDoc.toJson().data()); +} diff --git a/tests/auto/guhtestbase.h b/tests/auto/guhtestbase.h index a73bf6a0..32aeac4f 100644 --- a/tests/auto/guhtestbase.h +++ b/tests/auto/guhtestbase.h @@ -31,6 +31,7 @@ extern VendorId guhVendorId; extern DeviceClassId mockDeviceClassId; extern DeviceClassId mockDeviceAutoClassId; extern DeviceClassId mockDeviceDiscoveryClassId; +extern DeviceClassId mockDeviceAsyncSetupClassId; extern ActionTypeId mockAction1Id; extern EventTypeId mockEvent1Id; extern StateTypeId mockIntStateId; @@ -49,6 +50,7 @@ protected slots: protected: QVariant injectAndWait(const QString &method, const QVariantMap ¶ms = QVariantMap()); + void verifySuccess(const QVariant &response, bool success = true); protected: MockTcpServer *m_mockTcpServer; diff --git a/tests/auto/jsonrpc/testjsonrpc.cpp b/tests/auto/jsonrpc/testjsonrpc.cpp index ff5d73b7..e787e6fc 100644 --- a/tests/auto/jsonrpc/testjsonrpc.cpp +++ b/tests/auto/jsonrpc/testjsonrpc.cpp @@ -167,7 +167,7 @@ void TestJSONRPC::executeAction() params.insert("params", actionParams); QVariant response = injectAndWait("Actions.ExecuteAction", params); qDebug() << "executeActionresponse" << response; - QCOMPARE(response.toMap().value("params").toMap().value("success").toBool(), success); + verifySuccess(response, success); // Fetch action execution history from mock device QNetworkAccessManager nam; @@ -285,7 +285,7 @@ void TestJSONRPC::enableDisableNotifications() params.insert("enabled", enabled); QVariant response = injectAndWait("JSONRPC.SetNotificationStatus", params); - QCOMPARE(response.toMap().value("params").toMap().value("success").toBool(), true); + verifySuccess(response); QCOMPARE(response.toMap().value("params").toMap().value("enabled").toString(), enabled); } @@ -295,7 +295,7 @@ void TestJSONRPC::stateChangeEmitsNotifications() QVariantMap params; params.insert("enabled", true); QVariant response = injectAndWait("JSONRPC.SetNotificationStatus", params); - QCOMPARE(response.toMap().value("params").toMap().value("success").toBool(), true); + verifySuccess(response); // Setup connection to mock client QNetworkAccessManager nam; @@ -324,7 +324,7 @@ void TestJSONRPC::stateChangeEmitsNotifications() params.clear(); params.insert("enabled", false); response = injectAndWait("JSONRPC.SetNotificationStatus", params); - QCOMPARE(response.toMap().value("params").toMap().value("success").toBool(), true); + verifySuccess(response); // Fire the a statechange once again clientSpy.clear();