From 76d2dbb4f32add28f5a04766d1b171e1670fe618 Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Sun, 4 May 2014 19:01:41 +0200 Subject: [PATCH] added tests for all sorts of executeAction --- .../deviceplugins/mock/devicepluginmock.cpp | 50 ++++++++++++-- plugins/deviceplugins/mock/devicepluginmock.h | 2 + tests/auto/auto.pro | 2 +- tests/auto/devices/devices.pro | 2 +- tests/auto/events/events.pro | 3 +- tests/auto/guhtestbase.h | 6 +- tests/auto/jsonrpc/jsonrpc.pro | 2 +- tests/auto/jsonrpc/testjsonrpc.cpp | 65 +------------------ tests/auto/states/states.pro | 3 +- tests/auto/versioning/versioning.pro | 2 +- 10 files changed, 57 insertions(+), 80 deletions(-) diff --git a/plugins/deviceplugins/mock/devicepluginmock.cpp b/plugins/deviceplugins/mock/devicepluginmock.cpp index a52abaee..4e1c98d7 100644 --- a/plugins/deviceplugins/mock/devicepluginmock.cpp +++ b/plugins/deviceplugins/mock/devicepluginmock.cpp @@ -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 DevicePluginMock::supportedDevices() const QList 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 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 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 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)); + 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 = 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()); + } +} diff --git a/plugins/deviceplugins/mock/devicepluginmock.h b/plugins/deviceplugins/mock/devicepluginmock.h index cdaeebfe..0d7f3685 100644 --- a/plugins/deviceplugins/mock/devicepluginmock.h +++ b/plugins/deviceplugins/mock/devicepluginmock.h @@ -57,10 +57,12 @@ private slots: void triggerEvent(const EventTypeId &id); void emitDevicesDiscovered(); void emitDeviceSetupFinished(); + void emitActionExecuted(); private: QHash m_daemons; QList m_asyncSetupDevices; + QList > m_asyncActions; }; #endif // DEVICEPLUGINMOCK_H diff --git a/tests/auto/auto.pro b/tests/auto/auto.pro index dd85f1da..fc3c1f2e 100644 --- a/tests/auto/auto.pro +++ b/tests/auto/auto.pro @@ -1,2 +1,2 @@ TEMPLATE=subdirs -SUBDIRS=versioning devices jsonrpc events states +SUBDIRS=versioning devices jsonrpc events states actions diff --git a/tests/auto/devices/devices.pro b/tests/auto/devices/devices.pro index b8d607bc..dc20bd80 100644 --- a/tests/auto/devices/devices.pro +++ b/tests/auto/devices/devices.pro @@ -1,5 +1,5 @@ include(../../../guh.pri) include(../autotests.pri) -TARGET = devices +TARGET = testdevices SOURCES += testdevices.cpp diff --git a/tests/auto/events/events.pro b/tests/auto/events/events.pro index 9d95f0ec..cc1f4c4f 100644 --- a/tests/auto/events/events.pro +++ b/tests/auto/events/events.pro @@ -1,6 +1,5 @@ -TARGET = events - include(../../../guh.pri) include(../autotests.pri) +TARGET = testevents SOURCES += testevents.cpp diff --git a/tests/auto/guhtestbase.h b/tests/auto/guhtestbase.h index 75764680..ac519c64 100644 --- a/tests/auto/guhtestbase.h +++ b/tests/auto/guhtestbase.h @@ -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; diff --git a/tests/auto/jsonrpc/jsonrpc.pro b/tests/auto/jsonrpc/jsonrpc.pro index ca1f2088..a93e7d16 100644 --- a/tests/auto/jsonrpc/jsonrpc.pro +++ b/tests/auto/jsonrpc/jsonrpc.pro @@ -1,5 +1,5 @@ include(../../../guh.pri) include(../autotests.pri) -TARGET = jsonrpc +TARGET = testjsonrpc SOURCES += testjsonrpc.cpp diff --git a/tests/auto/jsonrpc/testjsonrpc.cpp b/tests/auto/jsonrpc/testjsonrpc.cpp index e787e6fc..60e7ed6a 100644 --- a/tests/auto/jsonrpc/testjsonrpc.cpp +++ b/tests/auto/jsonrpc/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"); - QTest::addColumn("actionTypeId"); - QTest::addColumn("actionParams"); - QTest::addColumn("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"); @@ -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()); } } diff --git a/tests/auto/states/states.pro b/tests/auto/states/states.pro index f5fcf33c..e24dbb94 100644 --- a/tests/auto/states/states.pro +++ b/tests/auto/states/states.pro @@ -1,6 +1,5 @@ -TARGET = states - include(../../../guh.pri) include(../autotests.pri) +TARGET = states SOURCES += teststates.cpp diff --git a/tests/auto/versioning/versioning.pro b/tests/auto/versioning/versioning.pro index e2c8f3c4..46af3e26 100644 --- a/tests/auto/versioning/versioning.pro +++ b/tests/auto/versioning/versioning.pro @@ -1,4 +1,4 @@ -TARGET = versioning +TARGET = testversioning include(../../../guh.pri) include(../autotests.pri)