added tests for all sorts of executeAction
This commit is contained in:
parent
22bc250391
commit
76d2dbb4f3
@ -37,8 +37,11 @@ EventTypeId mockEvent1Id = EventTypeId("45bf3752-0fc6-46b9-89fd-ffd878b5b22b");
|
||||
EventTypeId mockEvent2Id = EventTypeId("863d5920-b1cf-4eb9-88bd-8f7b8583b1cf");
|
||||
StateTypeId mockIntStateId = StateTypeId("80baec19-54de-4948-ac46-31eabfaceb83");
|
||||
StateTypeId mockBoolStateId = StateTypeId("9dd6a97c-dfd1-43dc-acbd-367932742310");
|
||||
ActionTypeId mockAction1Id = ActionTypeId("dea0f4e1-65e3-4981-8eaa-2701c53a9185");
|
||||
ActionTypeId mockAction2Id = ActionTypeId("defd3ed6-1a0d-400b-8879-a0202cf39935");
|
||||
ActionTypeId mockActionIdWithParams = ActionTypeId("dea0f4e1-65e3-4981-8eaa-2701c53a9185");
|
||||
ActionTypeId mockActionIdNoParams = ActionTypeId("defd3ed6-1a0d-400b-8879-a0202cf39935");
|
||||
ActionTypeId mockActionIdAsync = ActionTypeId("fbae06d3-7666-483e-a39e-ec50fe89054e");
|
||||
ActionTypeId mockActionIdFailing = ActionTypeId("df3cf33d-26d5-4577-9132-9823bd33fad0");
|
||||
ActionTypeId mockActionIdAsyncFailing = ActionTypeId("bfe89a1d-3497-4121-8318-e77c37537219");
|
||||
|
||||
DevicePluginMock::DevicePluginMock()
|
||||
{
|
||||
@ -100,8 +103,8 @@ QList<DeviceClass> DevicePluginMock::supportedDevices() const
|
||||
QList<ActionType> mockActions;
|
||||
|
||||
mockParams.clear();
|
||||
ActionType action1(mockAction1Id);
|
||||
action1.setName("Mock Action 1");
|
||||
ActionType action1(mockActionIdWithParams);
|
||||
action1.setName("Mock Action 1 (with params)");
|
||||
ParamType mockActionParam1("mockActionParam1", QVariant::Int);
|
||||
mockParams.append(mockActionParam1);
|
||||
ParamType mockActionParam2("mockActionParam2", QVariant::Bool);
|
||||
@ -109,10 +112,22 @@ QList<DeviceClass> DevicePluginMock::supportedDevices() const
|
||||
action1.setParameters(mockParams);
|
||||
mockActions.append(action1);
|
||||
|
||||
ActionType action2(mockAction2Id);
|
||||
action2.setName("Mock Action 2");
|
||||
ActionType action2(mockActionIdNoParams);
|
||||
action2.setName("Mock Action 3 (without params)");
|
||||
mockActions.append(action2);
|
||||
|
||||
ActionType action3(mockActionIdAsync);
|
||||
action3.setName("Mock Action 3 (async)");
|
||||
mockActions.append(action3);
|
||||
|
||||
ActionType action4(mockActionIdFailing);
|
||||
action4.setName("Mock Action 4 (broken)");
|
||||
mockActions.append(action4);
|
||||
|
||||
ActionType action5(mockActionIdAsyncFailing);
|
||||
action5.setName("Mock Action 5 (async, broken)");
|
||||
mockActions.append(action4);
|
||||
|
||||
deviceClassMock.setActions(mockActions);
|
||||
|
||||
ret.append(deviceClassMock);
|
||||
@ -146,7 +161,7 @@ QList<DeviceClass> DevicePluginMock::supportedDevices() const
|
||||
|
||||
// Async setup device
|
||||
DeviceClass deviceClassMockAsync(pluginId(), guhVendorId, mockDeviceAsyncSetupClassId);
|
||||
deviceClassMockAsync.setName("Mock Device (Async setup)");
|
||||
deviceClassMockAsync.setName("Mock Device (Async)");
|
||||
deviceClassMockAsync.setCreateMethod(DeviceClass::CreateMethodUser);
|
||||
|
||||
deviceClassMockAsync.setParams(mockParams);
|
||||
@ -262,6 +277,16 @@ QPair<DeviceManager::DeviceError, QString> DevicePluginMock::executeAction(Devic
|
||||
return report(DeviceManager::DeviceErrorDeviceNotFound, "Should execute an action for a device which doesn't seem to be mine.");
|
||||
}
|
||||
|
||||
if (action.actionTypeId() == mockActionIdAsync || action.actionTypeId() == mockActionIdAsyncFailing) {
|
||||
m_asyncActions.append(qMakePair<Action, Device*>(action, device));
|
||||
QTimer::singleShot(1000, this, SLOT(emitActionExecuted()));
|
||||
return report(DeviceManager::DeviceErrorAsync);
|
||||
}
|
||||
|
||||
if (action.actionTypeId() == mockActionIdFailing) {
|
||||
return report(DeviceManager::DeviceErrorSetupFailed);
|
||||
}
|
||||
|
||||
qDebug() << "Should execute action" << action.actionTypeId();
|
||||
m_daemons.value(device)->actionExecuted(action.actionTypeId());
|
||||
return report();
|
||||
@ -324,3 +349,14 @@ void DevicePluginMock::emitDeviceSetupFinished()
|
||||
emit deviceSetupFinished(device, DeviceManager::DeviceSetupStatusFailure, QString("This device is intentionally broken"));
|
||||
}
|
||||
}
|
||||
|
||||
void DevicePluginMock::emitActionExecuted()
|
||||
{
|
||||
QPair<Action, Device*> action = m_asyncActions.takeFirst();
|
||||
if (action.first.actionTypeId() == mockActionIdAsync) {
|
||||
m_daemons.value(action.second)->actionExecuted(action.first.actionTypeId());
|
||||
emit actionExecutionFinished(action.first.id(), DeviceManager::DeviceErrorNoError, QString());
|
||||
} else if (action.first.actionTypeId() == mockActionIdAsyncFailing) {
|
||||
emit actionExecutionFinished(action.first.id(), DeviceManager::DeviceErrorSetupFailed, QString());
|
||||
}
|
||||
}
|
||||
|
||||
@ -57,10 +57,12 @@ private slots:
|
||||
void triggerEvent(const EventTypeId &id);
|
||||
void emitDevicesDiscovered();
|
||||
void emitDeviceSetupFinished();
|
||||
void emitActionExecuted();
|
||||
|
||||
private:
|
||||
QHash<Device*, HttpDaemon*> m_daemons;
|
||||
QList<Device*> m_asyncSetupDevices;
|
||||
QList<QPair<Action, Device*> > m_asyncActions;
|
||||
};
|
||||
|
||||
#endif // DEVICEPLUGINMOCK_H
|
||||
|
||||
@ -1,2 +1,2 @@
|
||||
TEMPLATE=subdirs
|
||||
SUBDIRS=versioning devices jsonrpc events states
|
||||
SUBDIRS=versioning devices jsonrpc events states actions
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
include(../../../guh.pri)
|
||||
include(../autotests.pri)
|
||||
|
||||
TARGET = devices
|
||||
TARGET = testdevices
|
||||
SOURCES += testdevices.cpp
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
TARGET = events
|
||||
|
||||
include(../../../guh.pri)
|
||||
include(../autotests.pri)
|
||||
|
||||
TARGET = testevents
|
||||
SOURCES += testevents.cpp
|
||||
|
||||
@ -35,7 +35,11 @@ extern DeviceClassId mockDeviceDiscoveryClassId;
|
||||
extern DeviceClassId mockDeviceAsyncSetupClassId;
|
||||
extern DeviceClassId mockDeviceBrokenClassId;
|
||||
extern DeviceClassId mockDeviceBrokenAsyncSetupClassId;
|
||||
extern ActionTypeId mockAction1Id;
|
||||
extern ActionTypeId mockActionIdWithParams;
|
||||
extern ActionTypeId mockActionIdNoParams;
|
||||
extern ActionTypeId mockActionIdAsync;
|
||||
extern ActionTypeId mockActionIdFailing;
|
||||
extern ActionTypeId mockActionIdAsyncFailing;
|
||||
extern EventTypeId mockEvent1Id;
|
||||
extern StateTypeId mockIntStateId;
|
||||
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
include(../../../guh.pri)
|
||||
include(../autotests.pri)
|
||||
|
||||
TARGET = jsonrpc
|
||||
TARGET = testjsonrpc
|
||||
SOURCES += testjsonrpc.cpp
|
||||
|
||||
@ -38,9 +38,6 @@ private slots:
|
||||
void testBasicCall();
|
||||
void introspect();
|
||||
|
||||
void executeAction_data();
|
||||
void executeAction();
|
||||
|
||||
void getActionTypes_data();
|
||||
void getActionTypes();
|
||||
|
||||
@ -134,66 +131,6 @@ void TestJSONRPC::introspect()
|
||||
}
|
||||
}
|
||||
|
||||
void TestJSONRPC::executeAction_data()
|
||||
{
|
||||
QTest::addColumn<DeviceId>("deviceId");
|
||||
QTest::addColumn<ActionTypeId>("actionTypeId");
|
||||
QTest::addColumn<QVariantList>("actionParams");
|
||||
QTest::addColumn<bool>("success");
|
||||
|
||||
QVariantList params;
|
||||
QVariantMap param1;
|
||||
param1.insert("mockActionParam1", 5);
|
||||
params.append(param1);
|
||||
QVariantMap param2;
|
||||
param2.insert("mockActionParam2", true);
|
||||
params.append(param2);
|
||||
|
||||
QTest::newRow("valid action") << m_mockDeviceId << mockAction1Id << params << true;
|
||||
QTest::newRow("invalid device TypeId") << DeviceId("f2965936-0dd0-4014-8f31-4c2ef7fc5952") << mockAction1Id << params << false;
|
||||
QTest::newRow("invalid action TypeId") << m_mockDeviceId << ActionTypeId("f2965936-0dd0-4014-8f31-4c2ef7fc5952") << params << false;
|
||||
}
|
||||
|
||||
void TestJSONRPC::executeAction()
|
||||
{
|
||||
QFETCH(DeviceId, deviceId);
|
||||
QFETCH(ActionTypeId, actionTypeId);
|
||||
QFETCH(QVariantList, actionParams);
|
||||
QFETCH(bool, success);
|
||||
|
||||
QVariantMap params;
|
||||
params.insert("actionTypeId", actionTypeId);
|
||||
params.insert("deviceId", deviceId);
|
||||
params.insert("params", actionParams);
|
||||
QVariant response = injectAndWait("Actions.ExecuteAction", params);
|
||||
qDebug() << "executeActionresponse" << response;
|
||||
verifySuccess(response, success);
|
||||
|
||||
// Fetch action execution history from mock device
|
||||
QNetworkAccessManager nam;
|
||||
QSignalSpy spy(&nam, SIGNAL(finished(QNetworkReply*)));
|
||||
|
||||
QNetworkRequest request(QUrl(QString("http://localhost:%1/actionhistory").arg(m_mockDevice1Port)));
|
||||
QNetworkReply *reply = nam.get(request);
|
||||
spy.wait();
|
||||
QCOMPARE(spy.count(), 1);
|
||||
reply->deleteLater();
|
||||
|
||||
if (success) {
|
||||
QCOMPARE(actionTypeId, ActionTypeId(reply->readAll()));
|
||||
} else {
|
||||
QCOMPARE(reply->readAll().length(), 0);
|
||||
}
|
||||
|
||||
// cleanup for the next run
|
||||
spy.clear();
|
||||
request.setUrl(QUrl(QString("http://localhost:%1/clearactionhistory").arg(m_mockDevice1Port)));
|
||||
reply = nam.get(request);
|
||||
spy.wait();
|
||||
QCOMPARE(spy.count(), 1);
|
||||
reply->deleteLater();
|
||||
}
|
||||
|
||||
void TestJSONRPC::getActionTypes_data()
|
||||
{
|
||||
QTest::addColumn<DeviceClassId>("deviceClassId");
|
||||
@ -215,7 +152,7 @@ void TestJSONRPC::getActionTypes()
|
||||
QVariantList actionTypes = response.toMap().value("params").toMap().value("actionTypes").toList();
|
||||
QCOMPARE(actionTypes.count(), resultCount);
|
||||
if (resultCount > 0) {
|
||||
QCOMPARE(actionTypes.first().toMap().value("id").toString(), mockAction1Id.toString());
|
||||
QCOMPARE(actionTypes.first().toMap().value("id").toString(), mockActionIdWithParams.toString());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
TARGET = states
|
||||
|
||||
include(../../../guh.pri)
|
||||
include(../autotests.pri)
|
||||
|
||||
TARGET = states
|
||||
SOURCES += teststates.cpp
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
TARGET = versioning
|
||||
TARGET = testversioning
|
||||
|
||||
include(../../../guh.pri)
|
||||
include(../autotests.pri)
|
||||
|
||||
Reference in New Issue
Block a user