add a test for triggering events

This commit is contained in:
Michael Zanetti 2014-05-04 14:21:58 +02:00
parent 8916626ded
commit 70f6f783b9
9 changed files with 107 additions and 8 deletions

View File

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

View File

@ -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<DeviceDescriptor> &devices);
void deviceSetupFinished(Device *device, DeviceError status, const QString &errorMessage);

View File

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

View File

@ -29,12 +29,16 @@
class Event
{
public:
Event();
Event(const EventTypeId &eventTypeId, const DeviceId &deviceId, const QList<Param> &params = QList<Param>());
EventId eventId() const;
EventTypeId eventTypeId() const;
void setEventTypeId(const EventTypeId &eventTypeId);
DeviceId deviceId() const;
void setDeviceId(const DeviceId &deviceId);
QList<Param> params() const;
void setParams(const QList<Param> &params);
@ -48,6 +52,7 @@ private:
DeviceId m_deviceId;
QList<Param> m_params;
};
Q_DECLARE_METATYPE(Event)
QDebug operator<<(QDebug dbg, const Event &event);
QDebug operator<<(QDebug dbg, const QList<Event> &events);

View File

@ -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(
"<tr>"
"<form action=\"/generateevent\" method=\"get\">"
"<td>%1<input type='hidden' name='eventid' value='%2'/></td>"
"<td>%1<input type='hidden' name='eventtypeid' value='%2'/></td>"
"<td><input type='submit' value='Generate'/></td>"
"</form>"
"</tr>"

View File

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

View File

@ -1,2 +1,2 @@
TEMPLATE=subdirs
SUBDIRS=versioning devices jsonrpc
SUBDIRS=versioning devices jsonrpc events

View File

@ -0,0 +1,8 @@
TARGET = events
include(../../../guh.pri)
include(../autotests.pri)
DEFINES += TESTS_SOURCE_DIR=\\\"$$top_srcdir/tests/auto/\\\"
SOURCES += testevents.cpp

View File

@ -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 <http://www.gnu.org/licenses/>. *
* *
***************************************************************************/
#include "guhtestbase.h"
#include "guhcore.h"
#include "devicemanager.h"
#include "mocktcpserver.h"
#include <QtTest/QtTest>
#include <QCoreApplication>
#include <QTcpSocket>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QCoreApplication>
class TestEvents: public GuhTestBase
{
Q_OBJECT
private slots:
void triggerEvent();
};
void TestEvents::triggerEvent()
{
QList<Device*> 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<Event>();
QCOMPARE(event.eventTypeId(), mockEvent1Id);
QCOMPARE(event.deviceId(), device->id());
}
#include "testevents.moc"
QTEST_MAIN(TestEvents)