From 70f6f783b90695ba28062fead71c757225c18e09 Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Sun, 4 May 2014 14:21:58 +0200 Subject: [PATCH] add a test for triggering events --- libguh/devicemanager.cpp | 2 +- libguh/devicemanager.h | 2 +- libguh/types/event.cpp | 20 ++++++- libguh/types/event.h | 5 ++ plugins/deviceplugins/mock/httpdaemon.cpp | 6 +- server/guhcore.cpp | 2 +- tests/auto/auto.pro | 2 +- tests/auto/events/events.pro | 8 +++ tests/auto/events/testevents.cpp | 68 +++++++++++++++++++++++ 9 files changed, 107 insertions(+), 8 deletions(-) create mode 100644 tests/auto/events/events.pro create mode 100644 tests/auto/events/testevents.cpp diff --git a/libguh/devicemanager.cpp b/libguh/devicemanager.cpp index 6b44bf77..1ca9ac55 100644 --- a/libguh/devicemanager.cpp +++ b/libguh/devicemanager.cpp @@ -423,7 +423,7 @@ void DeviceManager::loadPlugins() } m_devicePlugins.insert(pluginIface->pluginId(), pluginIface); - connect(pluginIface, &DevicePlugin::emitEvent, this, &DeviceManager::emitEvent); + connect(pluginIface, &DevicePlugin::emitEvent, this, &DeviceManager::eventTriggered); connect(pluginIface, &DevicePlugin::devicesDiscovered, this, &DeviceManager::slotDevicesDiscovered); connect(pluginIface, &DevicePlugin::deviceSetupFinished, this, &DeviceManager::slotDeviceSetupFinished); connect(pluginIface, &DevicePlugin::actionExecutionFinished, this, &DeviceManager::actionExecutionFinished); diff --git a/libguh/devicemanager.h b/libguh/devicemanager.h index b897c941..b7286e82 100644 --- a/libguh/devicemanager.h +++ b/libguh/devicemanager.h @@ -90,7 +90,7 @@ public: signals: void loaded(); - void emitEvent(const Event &event); + void eventTriggered(const Event &event); void deviceStateChanged(Device *device, const QUuid &stateTypeId, const QVariant &value); void devicesDiscovered(const DeviceClassId &deviceClassId, const QList &devices); void deviceSetupFinished(Device *device, DeviceError status, const QString &errorMessage); diff --git a/libguh/types/event.cpp b/libguh/types/event.cpp index 0f84f997..c8e55441 100644 --- a/libguh/types/event.cpp +++ b/libguh/types/event.cpp @@ -33,6 +33,12 @@ #include "event.h" +Event::Event(): + m_id(EventId::createEventId()) +{ + +} + /*! Constructs a Event reflecting the \l{Event} given by \a EventTypeId, associated with the \l{Device} given by \a deviceId and the parameters given by \a params. The parameters must match the description in the reflecting \l{Event}.*/ @@ -57,12 +63,24 @@ EventTypeId Event::eventTypeId() const return m_eventTypeId; } +/*! Set the EventTypeId for this Event. */ +void Event::setEventTypeId(const EventTypeId &eventTypeId) +{ + m_eventTypeId = eventTypeId; +} + /*! Returns the id of the \l{Device} associated with this Event.*/ DeviceId Event::deviceId() const { return m_deviceId; } +/*! Set the DeviceId for this Event.*/ +void Event::setDeviceId(const DeviceId &deviceId) +{ + m_deviceId = deviceId; +} + /*! Returns the parameters of this Event.*/ QList Event::params() const { @@ -105,7 +123,7 @@ bool Event::operator ==(const Event &other) const QDebug operator<<(QDebug dbg, const Event &event) { - dbg.nospace() << "Event(EventTypeId: " << event.eventTypeId().toString() << ", DeviceId" << event.deviceId() << ")"; + dbg.nospace() << "Event(EventTypeId: " << event.eventTypeId().toString() << ", DeviceId" << event.deviceId().toString() << ")"; return dbg.space(); } diff --git a/libguh/types/event.h b/libguh/types/event.h index cf9f9efa..2d0a2dd1 100644 --- a/libguh/types/event.h +++ b/libguh/types/event.h @@ -29,12 +29,16 @@ class Event { public: + Event(); Event(const EventTypeId &eventTypeId, const DeviceId &deviceId, const QList ¶ms = QList()); EventId eventId() const; EventTypeId eventTypeId() const; + void setEventTypeId(const EventTypeId &eventTypeId); + DeviceId deviceId() const; + void setDeviceId(const DeviceId &deviceId); QList params() const; void setParams(const QList ¶ms); @@ -48,6 +52,7 @@ private: DeviceId m_deviceId; QList m_params; }; +Q_DECLARE_METATYPE(Event) QDebug operator<<(QDebug dbg, const Event &event); QDebug operator<<(QDebug dbg, const QList &events); diff --git a/plugins/deviceplugins/mock/httpdaemon.cpp b/plugins/deviceplugins/mock/httpdaemon.cpp index ff0c32f9..d9c4bfeb 100644 --- a/plugins/deviceplugins/mock/httpdaemon.cpp +++ b/plugins/deviceplugins/mock/httpdaemon.cpp @@ -77,8 +77,8 @@ void HttpDaemon::readClient() if (url.path() == "/setstate") { emit setState(StateTypeId(query.queryItems().first().first), QVariant(query.queryItems().first().second)); } else if (url.path() == "/generateevent") { - qDebug() << "got generateevent" << query.queryItemValue("eventid"); - emit triggerEvent(EventTypeId(query.queryItemValue("eventid"))); + qDebug() << "got generateevent" << query.queryItemValue("eventtypeid"); + emit triggerEvent(EventTypeId(query.queryItemValue("eventtypeid"))); } else if (url.path() == "/actionhistory") { QTextStream os(socket); os.setAutoDetectUnicode(true); @@ -164,7 +164,7 @@ QString HttpDaemon::generateWebPage() body.append(QString( "" "
" - "%1" + "%1" "" "
" "" diff --git a/server/guhcore.cpp b/server/guhcore.cpp index 6098906d..21aeb279 100644 --- a/server/guhcore.cpp +++ b/server/guhcore.cpp @@ -94,7 +94,7 @@ GuhCore::GuhCore(QObject *parent) : qDebug() << "*****************************************"; m_jsonServer = new JsonRPCServer(this); - connect(m_deviceManager, &DeviceManager::emitEvent, this, &GuhCore::gotEvent); + connect(m_deviceManager, &DeviceManager::eventTriggered, this, &GuhCore::gotEvent); } /*! Connected to the DeviceManager's emitEvent signal. Events received in diff --git a/tests/auto/auto.pro b/tests/auto/auto.pro index a8b34056..5c36ce24 100644 --- a/tests/auto/auto.pro +++ b/tests/auto/auto.pro @@ -1,2 +1,2 @@ TEMPLATE=subdirs -SUBDIRS=versioning devices jsonrpc +SUBDIRS=versioning devices jsonrpc events diff --git a/tests/auto/events/events.pro b/tests/auto/events/events.pro new file mode 100644 index 00000000..6f260434 --- /dev/null +++ b/tests/auto/events/events.pro @@ -0,0 +1,8 @@ +TARGET = events + +include(../../../guh.pri) +include(../autotests.pri) + +DEFINES += TESTS_SOURCE_DIR=\\\"$$top_srcdir/tests/auto/\\\" + +SOURCES += testevents.cpp diff --git a/tests/auto/events/testevents.cpp b/tests/auto/events/testevents.cpp new file mode 100644 index 00000000..05f87075 --- /dev/null +++ b/tests/auto/events/testevents.cpp @@ -0,0 +1,68 @@ +/**************************************************************************** + * * + * This file is part of guh. * + * * + * Guh is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, version 2 of the License. * + * * + * Guh is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with guh. If not, see . * + * * + ***************************************************************************/ + +#include "guhtestbase.h" +#include "guhcore.h" +#include "devicemanager.h" +#include "mocktcpserver.h" + +#include +#include +#include +#include +#include +#include +#include + +class TestEvents: public GuhTestBase +{ + Q_OBJECT + +private slots: + void triggerEvent(); +}; + +void TestEvents::triggerEvent() +{ + QList devices = GuhCore::instance()->deviceManager()->findConfiguredDevices(mockDeviceClassId); + QVERIFY2(devices.count() > 0, "There needs to be at least one configured Mock Device for this test"); + Device *device = devices.first(); + + QSignalSpy spy(GuhCore::instance()->deviceManager(), SIGNAL(eventTriggered(const Event&))); + + // Setup connection to mock client + QNetworkAccessManager nam; + + // trigger event in mock device + int port = device->paramValue("httpport").toInt(); + QNetworkRequest request(QUrl(QString("http://localhost:%1/generateevent?eventtypeid=%2").arg(port).arg(mockEvent1Id.toString()))); + QNetworkReply *reply = nam.get(request); + reply->deleteLater(); + + // Lets wait for the notification + spy.wait(); + QCOMPARE(spy.count(), 1); + + // Make sure the event contains all the stuff we expect + Event event = spy.at(0).at(0).value(); + QCOMPARE(event.eventTypeId(), mockEvent1Id); + QCOMPARE(event.deviceId(), device->id()); +} + +#include "testevents.moc" +QTEST_MAIN(TestEvents)