added a test for device discovery
This commit is contained in:
parent
7b0cbe58d3
commit
46e6642b90
@ -28,6 +28,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");
|
||||
EventTypeId mockEvent1Id = EventTypeId("45bf3752-0fc6-46b9-89fd-ffd878b5b22b");
|
||||
EventTypeId mockEvent2Id = EventTypeId("863d5920-b1cf-4eb9-88bd-8f7b8583b1cf");
|
||||
StateTypeId mockIntStateId = StateTypeId("80baec19-54de-4948-ac46-31eabfaceb83");
|
||||
@ -127,6 +128,20 @@ QList<DeviceClass> DevicePluginMock::supportedDevices() const
|
||||
|
||||
ret.append(deviceClassMockAuto);
|
||||
|
||||
// Discovery created device
|
||||
DeviceClass deviceClassMockDiscovery(pluginId(), guhVendorId, mockDeviceDiscoveryClassId);
|
||||
deviceClassMockDiscovery.setName("Mock Device (Discovery created)");
|
||||
deviceClassMockDiscovery.setCreateMethod(DeviceClass::CreateMethodDiscovery);
|
||||
|
||||
mockParams.clear();
|
||||
mockParams.append(portParam);
|
||||
deviceClassMockDiscovery.setParams(mockParams);
|
||||
deviceClassMockDiscovery.setStates(mockStates);
|
||||
deviceClassMockDiscovery.setEvents(mockEvents);
|
||||
deviceClassMockDiscovery.setActions(mockActions);
|
||||
|
||||
ret.append(deviceClassMockDiscovery);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -135,6 +150,14 @@ DeviceManager::HardwareResources DevicePluginMock::requiredHardware() const
|
||||
return DeviceManager::HardwareResourceTimer;
|
||||
}
|
||||
|
||||
DeviceManager::DeviceError DevicePluginMock::discoverDevices(const DeviceClassId &deviceClassId, const QVariantMap ¶ms) const
|
||||
{
|
||||
Q_UNUSED(deviceClassId)
|
||||
Q_UNUSED(params)
|
||||
QTimer::singleShot(1000, this, SLOT(emitDevicesDiscovered()));
|
||||
return DeviceManager::DeviceErrorNoError;
|
||||
}
|
||||
|
||||
QString DevicePluginMock::pluginName() const
|
||||
{
|
||||
return "Mock Devices";
|
||||
@ -218,3 +241,24 @@ void DevicePluginMock::triggerEvent(const EventTypeId &id)
|
||||
qDebug() << "Emitting event " << event.eventTypeId();
|
||||
emit emitEvent(event);
|
||||
}
|
||||
|
||||
void DevicePluginMock::emitDevicesDiscovered()
|
||||
{
|
||||
QList<DeviceDescriptor> deviceDescriptors;
|
||||
|
||||
DeviceDescriptor d1(mockDeviceDiscoveryClassId, "Mock Device (Discovered)");
|
||||
QList<Param> params;
|
||||
Param httpParam("httpport", "7777");
|
||||
params.append(httpParam);
|
||||
d1.setParams(params);
|
||||
deviceDescriptors.append(d1);
|
||||
|
||||
DeviceDescriptor d2(mockDeviceDiscoveryClassId, "Mock Device (Discovered)");
|
||||
params.clear();
|
||||
httpParam.setValue("7778");
|
||||
params.append(httpParam);
|
||||
d2.setParams(params);
|
||||
deviceDescriptors.append(d2);
|
||||
|
||||
emit devicesDiscovered(mockDeviceDiscoveryClassId, deviceDescriptors);
|
||||
}
|
||||
|
||||
@ -39,6 +39,7 @@ public:
|
||||
QList<Vendor> supportedVendors() const override;
|
||||
QList<DeviceClass> supportedDevices() const override;
|
||||
DeviceManager::HardwareResources requiredHardware() const override;
|
||||
DeviceManager::DeviceError discoverDevices(const DeviceClassId &deviceClassId, const QVariantMap ¶ms) const;
|
||||
|
||||
QString pluginName() const override;
|
||||
PluginId pluginId() const override;
|
||||
@ -54,6 +55,7 @@ public slots:
|
||||
private slots:
|
||||
void setState(const StateTypeId &stateTypeId, const QVariant &value);
|
||||
void triggerEvent(const EventTypeId &id);
|
||||
void emitDevicesDiscovered();
|
||||
|
||||
private:
|
||||
QHash<Device*, HttpDaemon*> m_daemons;
|
||||
|
||||
@ -210,6 +210,9 @@ JsonReply *DeviceHandler::GetDiscoveredDevices(const QVariantMap ¶ms) const
|
||||
case DeviceManager::DeviceErrorPluginNotFound:
|
||||
returns.insert("errorMessage", "Cannot discover devices. Plugin for DeviceClass not found.");
|
||||
break;
|
||||
case DeviceManager::DeviceErrorCreationMethodNotSupported:
|
||||
returns.insert("errorMessage", "This device can't be discovered.");
|
||||
break;
|
||||
default:
|
||||
returns.insert("errorMessage", QString("Unknown error %1").arg(status));
|
||||
}
|
||||
|
||||
@ -43,6 +43,8 @@ private slots:
|
||||
|
||||
void storedDevices();
|
||||
|
||||
void discoverDevices();
|
||||
|
||||
};
|
||||
|
||||
void TestDevices::getSupportedVendors()
|
||||
@ -62,8 +64,8 @@ void TestDevices::getSupportedDevices_data()
|
||||
QTest::addColumn<VendorId>("vendorId");
|
||||
QTest::addColumn<int>("resultCount");
|
||||
|
||||
QTest::newRow("vendor guh") << guhVendorId << 2;
|
||||
QTest::newRow("no filter") << VendorId() << 2;
|
||||
QTest::newRow("vendor guh") << guhVendorId << 3;
|
||||
QTest::newRow("no filter") << VendorId() << 3;
|
||||
QTest::newRow("invalid vendor") << VendorId("93e7d361-8025-4354-b17e-b68406c800bc") << 0;
|
||||
}
|
||||
|
||||
@ -78,11 +80,11 @@ void TestDevices::getSupportedDevices()
|
||||
}
|
||||
QVariant supportedDevices = injectAndWait("Devices.GetSupportedDevices", params);
|
||||
|
||||
// Make sure there is exactly 1 supported device class with the name Mock Wifi Device
|
||||
// Make sure there are the right amount of supported device classes with the name Mock Device
|
||||
QCOMPARE(supportedDevices.toMap().value("params").toMap().value("deviceClasses").toList().count(), resultCount);
|
||||
if (resultCount > 0) {
|
||||
QString deviceName = supportedDevices.toMap().value("params").toMap().value("deviceClasses").toList().first().toMap().value("name").toString();
|
||||
QVERIFY(deviceName.startsWith(QString("Mock Device")));
|
||||
QVERIFY2(deviceName.startsWith(QString("Mock Device")), QString("Got: %1 Expected: %2").arg(deviceName).arg("Mock Device").toLatin1().data());
|
||||
}
|
||||
}
|
||||
|
||||
@ -97,6 +99,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;
|
||||
|
||||
QVariantMap invalidDeviceParams;
|
||||
invalidDeviceParams.insert("tropptth", m_mockDevice1Port - 1);
|
||||
@ -195,6 +198,31 @@ void TestDevices::storedDevices()
|
||||
response = injectAndWait("Devices.RemoveConfiguredDevice", params);
|
||||
}
|
||||
|
||||
void TestDevices::discoverDevices()
|
||||
{
|
||||
QVariantMap params;
|
||||
params.insert("deviceClassId", mockDeviceDiscoveryClassId);
|
||||
QVariant response = injectAndWait("Devices.GetDiscoveredDevices", params);
|
||||
|
||||
qDebug() << "response" << response;
|
||||
|
||||
QCOMPARE(response.toMap().value("params").toMap().value("deviceDescriptors").toList().count(), 2);
|
||||
|
||||
DeviceDescriptorId descriptorId = DeviceDescriptorId(response.toMap().value("params").toMap().value("deviceDescriptors").toList().first().toMap().value("id").toString());
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
#include "testdevices.moc"
|
||||
|
||||
QTEST_MAIN(TestDevices)
|
||||
|
||||
@ -30,6 +30,7 @@
|
||||
extern VendorId guhVendorId;
|
||||
extern DeviceClassId mockDeviceClassId;
|
||||
extern DeviceClassId mockDeviceAutoClassId;
|
||||
extern DeviceClassId mockDeviceDiscoveryClassId;
|
||||
extern ActionTypeId mockAction1Id;
|
||||
extern EventTypeId mockEvent1Id;
|
||||
extern StateTypeId mockIntStateId;
|
||||
|
||||
Reference in New Issue
Block a user