summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Wolf <ext-martin.2.wolf@nokia.com>2010-02-04 18:32:35 +0200
committerMartin Wolf <ext-martin.2.wolf@nokia.com>2010-02-04 18:32:35 +0200
commit6751c585385f017d08540bbe121606d642afb220 (patch)
treeaa25c7bf41e4d26dcff1ce5d56832912b4cf473d
parent96c2304b134ae2aa1ec56825af40eae4a4fe21cf (diff)
downloadlibresourceqt-6751c585385f017d08540bbe121606d642afb220.tar.gz
Automated tests for libdbus-qeventloop
-rw-r--r--libdbus-qeventloop/dbusconnectioneventloop.cpp32
-rw-r--r--libdbus-qeventloop/dbusconnectioneventloop.h30
-rw-r--r--tests/test-dbus-ping/test-dbus-ping.cpp36
-rw-r--r--tests/test-dbus-ping/test-dbus-ping.pro19
-rw-r--r--tests/test-dbus-pong/pong.h2
-rw-r--r--tests/test-dbus-pong/test-dbus-pong.cpp33
-rw-r--r--tests/test-dbus-qeventloop/test-dbus-qeventloop.cpp159
-rw-r--r--tests/test-dbus-qeventloop/test-dbus-qeventloop.pro1
-rw-r--r--tests/tests.pro1
9 files changed, 241 insertions, 72 deletions
diff --git a/libdbus-qeventloop/dbusconnectioneventloop.cpp b/libdbus-qeventloop/dbusconnectioneventloop.cpp
index 16f4a24..9d7a255 100644
--- a/libdbus-qeventloop/dbusconnectioneventloop.cpp
+++ b/libdbus-qeventloop/dbusconnectioneventloop.cpp
@@ -4,7 +4,6 @@
#include <QTimerEvent>
#include "dbusconnectioneventloop.h"
-#include <stdio.h>
DBUSConnectionEventLoop::DBUSConnectionEventLoop() : QObject()
{
@@ -17,6 +16,8 @@ DBUSConnectionEventLoop::~DBUSConnectionEventLoop()
// Handle a socket being ready to read.
void DBUSConnectionEventLoop::readSocket(int fd)
{
+ MYDEBUG();
+
Watchers::const_iterator it = watchers.find(fd);
while( it != watchers.end() && it.key() == fd )
@@ -40,6 +41,8 @@ void DBUSConnectionEventLoop::readSocket(int fd)
// Handle a socket being ready to write.
void DBUSConnectionEventLoop::writeSocket(int fd)
{
+ MYDEBUG();
+
Watchers::const_iterator it = watchers.find(fd);
while( it != watchers.end() && it.key() == fd )
@@ -60,6 +63,8 @@ void DBUSConnectionEventLoop::writeSocket(int fd)
void DBUSConnectionEventLoop::dispatch()
{
+ MYDEBUG();
+
for( Connections::const_iterator it = connections.constBegin(); it != connections.constEnd(); ++it )
while( dbus_connection_dispatch(*it) == DBUS_DISPATCH_DATA_REMAINS )
;
@@ -68,6 +73,9 @@ void DBUSConnectionEventLoop::dispatch()
// Handle timer events.
void DBUSConnectionEventLoop::timerEvent(QTimerEvent *e)
{
+ MYDEBUG();
+ MYDEBUGC("TimerID: %d", e->timerId());
+
DBusTimeout *timeout = timeouts.value(e->timerId());
if( timeout )
@@ -76,6 +84,8 @@ void DBUSConnectionEventLoop::timerEvent(QTimerEvent *e)
dbus_bool_t DBUSConnectionEventLoop::addWatch(DBusWatch *watch, void *data)
{
+ MYDEBUG();
+
DBUSConnectionEventLoop *loop = reinterpret_cast<DBUSConnectionEventLoop *>(data);
int fd = dbus_watch_get_unix_fd(watch);
@@ -106,6 +116,8 @@ dbus_bool_t DBUSConnectionEventLoop::addWatch(DBusWatch *watch, void *data)
void DBUSConnectionEventLoop::removeWatch(DBusWatch *watch, void *data)
{
+ MYDEBUG();
+
DBUSConnectionEventLoop *loop = reinterpret_cast<DBUSConnectionEventLoop *>(data);
int fd = dbus_watch_get_unix_fd(watch);
@@ -135,7 +147,9 @@ void DBUSConnectionEventLoop::removeWatch(DBusWatch *watch, void *data)
void DBUSConnectionEventLoop::toggleWatch(DBusWatch *watch, void *data)
{
- DBUSConnectionEventLoop *loop = reinterpret_cast<DBUSConnectionEventLoop *>(data);
+ MYDEBUG();
+
+ DBUSConnectionEventLoop *loop = reinterpret_cast<DBUSConnectionEventLoop*>(data);
int fd = dbus_watch_get_unix_fd(watch);
unsigned int flags = dbus_watch_get_flags(watch);
@@ -164,6 +178,8 @@ void DBUSConnectionEventLoop::toggleWatch(DBusWatch *watch, void *data)
dbus_bool_t DBUSConnectionEventLoop::addTimeout(DBusTimeout *timeout, void *data)
{
+ MYDEBUG();
+
// Nothing to do if the timeout is disabled.
if( !dbus_timeout_get_enabled(timeout) )
return true;
@@ -174,7 +190,10 @@ dbus_bool_t DBUSConnectionEventLoop::addTimeout(DBusTimeout *timeout, void *data
DBUSConnectionEventLoop *loop = reinterpret_cast<DBUSConnectionEventLoop *>(data);
- int id = loop->startTimer(dbus_timeout_get_interval(timeout));
+ int timerInterval = dbus_timeout_get_interval(timeout);
+ int id = loop->startTimer(timerInterval);
+
+ MYDEBUGC("Adding timeout %d with interval %d!", id, timerInterval);
if( !id )
return false;
@@ -186,6 +205,8 @@ dbus_bool_t DBUSConnectionEventLoop::addTimeout(DBusTimeout *timeout, void *data
void DBUSConnectionEventLoop::removeTimeout(DBusTimeout *timeout, void *data)
{
+ MYDEBUG();
+
DBUSConnectionEventLoop *loop = reinterpret_cast<DBUSConnectionEventLoop *>(data);
DBUSConnectionEventLoop::Timeouts::iterator it = loop->timeouts.begin();
@@ -204,12 +225,16 @@ void DBUSConnectionEventLoop::removeTimeout(DBusTimeout *timeout, void *data)
void DBUSConnectionEventLoop::toggleTimeout(DBusTimeout *timeout, void *data)
{
+ MYDEBUG();
+
DBUSConnectionEventLoop::removeTimeout(timeout, data);
DBUSConnectionEventLoop::addTimeout(timeout, data);
}
void DBUSConnectionEventLoop::wakeupMain(void *data)
{
+ MYDEBUG();
+
DBUSConnectionEventLoop *loop = reinterpret_cast<DBUSConnectionEventLoop *>(data);
QTimer::singleShot(0, loop, SLOT(dispatch()));
@@ -256,4 +281,3 @@ bool DBUSConnectionEventLoop::initConnection(DBusConnection* conn)
return rc;
}
-
diff --git a/libdbus-qeventloop/dbusconnectioneventloop.h b/libdbus-qeventloop/dbusconnectioneventloop.h
index ef46ea2..6f8d8e7 100644
--- a/libdbus-qeventloop/dbusconnectioneventloop.h
+++ b/libdbus-qeventloop/dbusconnectioneventloop.h
@@ -8,6 +8,36 @@
#include <dbus/dbus.h>
+//#define _DISPLAY_DEBUG_
+
+#ifdef _DISPLAY_DEBUG_
+ #include <stdlib.h>
+
+ #define MYDEBUG() MyDebug deb(__FUNCTION__)
+ #define MYDEBUGC(...) qDebug(__VA_ARGS__)
+
+ class MyDebug
+ {
+ public:
+ MyDebug(const char* func)
+ {
+ funcName = strdup(func);
+ qDebug("--> Entering: %s() ...", funcName);
+ }
+ ~MyDebug()
+ {
+ qDebug("<-- Leaving: %s() ...", funcName);
+ free(funcName);
+ }
+
+ private:
+ char* funcName;
+ };
+#else
+ #define MYDEBUG()
+ #define MYDEBUGC(...)
+#endif
+
class QSocketNotifier;
class QTimerEvent;
diff --git a/tests/test-dbus-ping/test-dbus-ping.cpp b/tests/test-dbus-ping/test-dbus-ping.cpp
deleted file mode 100644
index ff496e6..0000000
--- a/tests/test-dbus-ping/test-dbus-ping.cpp
+++ /dev/null
@@ -1,36 +0,0 @@
-#include <stdio.h>
-
-#include <QtCore/QCoreApplication>
-#include <QtDBus/QtDBus>
-
-#define SERVICE_NAME "com.nokia.dbusqeventloop.test"
-
-int main(int argc, char **argv)
-{
- QCoreApplication app(argc, argv);
-
- if( !QDBusConnection::systemBus().isConnected() )
- {
- fprintf(stderr, "Cannot connect to the D-Bus system bus.\n"
- "To start it, run:\n"
- "\teval `dbus-launch --auto-syntax`\n");
- return 1;
- }
-
- QDBusInterface iface(SERVICE_NAME, "/", "", QDBusConnection::systemBus());
- if( iface.isValid() )
- {
- QDBusReply<QString> reply = iface.call("ping", argc > 1 ? argv[1] : "");
- if( reply.isValid() )
- {
- printf("Reply was: %s\n", qPrintable(reply.value()));
- return 0;
- }
-
- fprintf(stderr, "Call failed: %s\n", qPrintable(reply.error().message()));
- return 2;
- }
-
- fprintf(stderr, "%s\n", qPrintable(QDBusConnection::systemBus().lastError().message()));
- return 3;
-}
diff --git a/tests/test-dbus-ping/test-dbus-ping.pro b/tests/test-dbus-ping/test-dbus-ping.pro
deleted file mode 100644
index b81bd3d..0000000
--- a/tests/test-dbus-ping/test-dbus-ping.pro
+++ /dev/null
@@ -1,19 +0,0 @@
-TEMPLATE = app
-TARGET = test-dbus-ping
-MOC_DIR = .moc
-OBJECTS_DIR = .obj
-DEPENDPATH += .
-QT = core dbus
-CONFIG += console
-CONFIG -= app_bundle
-
-QMAKE_CXXFLAGS += -Wall
-
-# Input
-SOURCES += test-dbus-ping.cpp
-
-QMAKE_DISTCLEAN += -r .moc .obj
-
-# Install options
-target.path = /usr/lib/libresourceqt-tests/
-INSTALLS = target
diff --git a/tests/test-dbus-pong/pong.h b/tests/test-dbus-pong/pong.h
index 8656186..21f0082 100644
--- a/tests/test-dbus-pong/pong.h
+++ b/tests/test-dbus-pong/pong.h
@@ -8,6 +8,8 @@ class Pong: public QObject
Q_OBJECT
public slots:
Q_SCRIPTABLE QString ping(const QString &arg);
+ Q_SCRIPTABLE unsigned int quit();
+ Q_SCRIPTABLE unsigned int timeout();
};
#endif // _PONG_H_
diff --git a/tests/test-dbus-pong/test-dbus-pong.cpp b/tests/test-dbus-pong/test-dbus-pong.cpp
index f781004..701abb1 100644
--- a/tests/test-dbus-pong/test-dbus-pong.cpp
+++ b/tests/test-dbus-pong/test-dbus-pong.cpp
@@ -7,35 +7,52 @@
#include "pong.h"
-#define SERVICE_NAME "com.nokia.dbusqeventloop.test"
-
QString Pong::ping(const QString &arg)
{
+ // Just return back
+ return QString("%1").arg(arg);
+}
+
+unsigned int Pong::quit()
+{
+ // Quit application
QMetaObject::invokeMethod(QCoreApplication::instance(), "quit");
- return QString("ping(\"%1\") got called").arg(arg);
+ // Magic number
+ return 12345;
+}
+
+unsigned int Pong::timeout()
+{
+ // Timeout in testing application should be set to less than 2 seconds!
+ sleep(2);
+ // Just to suppress warning
+ return 54321;
}
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
+ // Check system bus connection
if( !QDBusConnection::systemBus().isConnected() )
{
- fprintf(stderr, "Cannot connect to the D-Bus system bus.\n"
- "To start it, run:\n"
- "\teval `dbus-launch --auto-syntax`\n");
+ qDebug("Cannot connect to the D-Bus system bus.");
return 1;
}
- if( !QDBusConnection::systemBus().registerService(SERVICE_NAME) )
+ // Create listener service
+ if( !QDBusConnection::systemBus().registerService("com.nokia.dbusqeventloop.test") )
{
- fprintf(stderr, "%s\n", qPrintable(QDBusConnection::systemBus().lastError().message()));
+ qDebug("%s", qPrintable(QDBusConnection::systemBus().lastError().message()));
exit(2);
}
Pong pong;
+ // Register all slots as dbus methods
QDBusConnection::systemBus().registerObject("/", &pong, QDBusConnection::ExportAllSlots);
+ // Let's go!
app.exec();
+
return 0;
}
diff --git a/tests/test-dbus-qeventloop/test-dbus-qeventloop.cpp b/tests/test-dbus-qeventloop/test-dbus-qeventloop.cpp
index f0385af..f50f95a 100644
--- a/tests/test-dbus-qeventloop/test-dbus-qeventloop.cpp
+++ b/tests/test-dbus-qeventloop/test-dbus-qeventloop.cpp
@@ -5,14 +5,165 @@ class TestDbusQEventLoop: public QObject
{
Q_OBJECT
+private:
+ DBusConnection* systemBus;
+ DBUSConnectionEventLoop* dbusEventLoop;
+
+ void resetValues()
+ {
+ wasInNotifyFnc = 0;
+ responseString = 0;
+ responseInt = 0;
+ typeError = false;
+ noResponse = false;
+ }
+
+ void processQTEventLoop(DBusPendingCall* pending)
+ {
+ // Reset response values to zeros and reset errors
+ resetValues();
+
+ // Do we have something pending?
+ if( pending == NULL )
+ {
+ return;
+ }
+
+ // If we have some pending operation, let's get notification about result
+ dbus_pending_call_set_notify(pending, TestDbusQEventLoop::pendingNotify, this, NULL);
+
+ // Pump QT event loop, but only until pending operation is not finished
+ while( !wasInNotifyFnc )
+ {
+ QCoreApplication::processEvents(QEventLoop::AllEvents);
+ }
+ }
+
+public:
+ int wasInNotifyFnc;
+ const char* responseString;
+ uint32_t responseInt;
+ bool typeError;
+ bool noResponse;
+
+ TestDbusQEventLoop()
+ {
+ resetValues();
+ }
+
+protected:
+ static void pendingNotify(DBusPendingCall *pending, void *user_data)
+ {
+ MYDEBUG();
+ TestDbusQEventLoop* pThis = reinterpret_cast<TestDbusQEventLoop*>(user_data);
+ DBusMessage* message = dbus_pending_call_steal_reply(pending);
+
+ DBusMessageIter args;
+ if( !dbus_message_iter_init(message, &args) )
+ MYDEBUGC("Reply message has no arguments!");
+ else
+ {
+ const char* error = dbus_message_get_error_name(message);
+
+ if( error != NULL )
+ {
+ pThis->noResponse = (strcmp(error, "org.freedesktop.DBus.Error.NoReply") == 0);
+ }
+
+ int argType = dbus_message_iter_get_arg_type(&args);
+ switch( argType )
+ {
+ case DBUS_TYPE_STRING:
+ dbus_message_iter_get_basic(&args, &pThis->responseString);
+ if( error != NULL )
+ MYDEBUGC("Got error [%s]: %s", error, pThis->responseString);
+ else
+ MYDEBUGC("Got Reply: %s", pThis->responseString);
+ break;
+ case DBUS_TYPE_BOOLEAN:
+ case DBUS_TYPE_UINT32:
+ dbus_message_iter_get_basic(&args, &pThis->responseInt);
+ MYDEBUGC("Got Reply: %d", pThis->responseInt);
+ break;
+ default:
+ MYDEBUGC("Reply message argument has unsupported type (%d)!", argType);
+ pThis->typeError = true;
+ }
+ }
+
+ if( message )
+ dbus_message_unref(message);
+
+ dbus_pending_call_unref(pending);
+
+ pThis->wasInNotifyFnc = 1;
+ }
+
private slots:
- void basicTest()
+ void initTestCase()
{
- DBusConnection* systemBus = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
+ systemBus = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
QVERIFY(systemBus != NULL);
- DBUSConnectionEventLoop d;
- QVERIFY(d.initConnection(systemBus) == true);
+ dbusEventLoop = new DBUSConnectionEventLoop();
+ QVERIFY(dbusEventLoop != NULL);
+
+ QVERIFY(dbus_bus_name_has_owner(systemBus, "com.nokia.dbusqeventloop.test", NULL) == true);
+ }
+
+ void cleanupTestCase()
+ {
+ DBusMessage* message = dbus_message_new_method_call("com.nokia.dbusqeventloop.test", "/", NULL, "quit");
+ QVERIFY(message != NULL);
+ DBusPendingCall* pending;
+ dbus_connection_send_with_reply(systemBus, message, &pending, 3000);
+ dbus_message_unref(message);
+
+ processQTEventLoop(pending);
+ QVERIFY(responseInt == 12345);
+
+ QVERIFY(dbusEventLoop != NULL);
+ delete dbusEventLoop;
+ dbusEventLoop = NULL;
+ }
+
+ void initConnectionTest()
+ {
+ QVERIFY(dbusEventLoop->initConnection(systemBus) == true);
+ }
+
+ void pingTest()
+ {
+ DBusMessage* message = dbus_message_new_method_call("com.nokia.dbusqeventloop.test", "/", NULL, "ping");
+ QVERIFY(message != NULL);
+
+ const char* temp = "pekny kohutik co sa prechadza po svojom novom dvore a obzera si sliepocky";
+ dbus_message_append_args(message, DBUS_TYPE_STRING, &temp, DBUS_TYPE_INVALID);
+
+ DBusPendingCall* pending;
+ dbus_connection_send_with_reply(systemBus, message, &pending, 3000);
+
+ // Free the signal now we have finished with it
+ dbus_message_unref(message);
+
+ processQTEventLoop(pending);
+ QVERIFY(strcmp(temp, responseString) == 0);
+ }
+
+ void timeoutTest()
+ {
+ DBusMessage* message = dbus_message_new_method_call("com.nokia.dbusqeventloop.test", "/", NULL, "timeout");
+ QVERIFY(message != NULL);
+
+ DBusPendingCall* pending;
+ dbus_connection_send_with_reply(systemBus, message, &pending, 1000);
+
+ // Free the signal now we have finished with it
+ dbus_message_unref(message);
+
+ processQTEventLoop(pending);
+ QVERIFY(noResponse == true);
+ sleep(1);
}
};
diff --git a/tests/test-dbus-qeventloop/test-dbus-qeventloop.pro b/tests/test-dbus-qeventloop/test-dbus-qeventloop.pro
index 80066bf..afbd2b5 100644
--- a/tests/test-dbus-qeventloop/test-dbus-qeventloop.pro
+++ b/tests/test-dbus-qeventloop/test-dbus-qeventloop.pro
@@ -20,6 +20,7 @@ LIBS += -L../../libdbus-qeventloop/build -ldbus-qeventloop
# Input
SOURCES += test-dbus-qeventloop.cpp
+HEADERS = ../../libdbus-qeventloop/dbusconnectioneventloop.h
QMAKE_DISTCLEAN += -r .moc .obj
diff --git a/tests/tests.pro b/tests/tests.pro
index e5751bb..c24c97c 100644
--- a/tests/tests.pro
+++ b/tests/tests.pro
@@ -6,7 +6,6 @@ CONFIG += ordered
TEMPLATE = subdirs
SUBDIRS = test-dbus-qeventloop \
- test-dbus-ping \
test-dbus-pong \
test-resource \
# test-resource-engine \