add a test for async device creation

This commit is contained in:
Michael Zanetti 2014-05-04 04:49:19 +02:00
parent 3a17985d50
commit 384690bb23
7 changed files with 57 additions and 18 deletions

View File

@ -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<DeviceClass> 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<DeviceManager::DeviceSetupStatus, QString> 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<Device *> 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());
}

View File

@ -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<Device*, HttpDaemon*> m_daemons;
QList<Device*> m_asyncSetupDevices;
};
#endif // DEVICEPLUGINMOCK_H

View File

@ -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);

View File

@ -64,8 +64,8 @@ void TestDevices::getSupportedDevices_data()
QTest::addColumn<VendorId>("vendorId");
QTest::addColumn<int>("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"

View File

@ -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());
}

View File

@ -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 &params = QVariantMap());
void verifySuccess(const QVariant &response, bool success = true);
protected:
MockTcpServer *m_mockTcpServer;

View File

@ -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();