summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWolf Bergenheim <ext-wolf.2.bergenheim@nokia.com>2010-02-09 12:18:44 +0200
committerWolf Bergenheim <ext-wolf.2.bergenheim@nokia.com>2010-02-09 12:18:44 +0200
commit910493fac3e328cef329ade5e8c1a71e136ca246 (patch)
tree1515ef7af8544692f70da623e15cc19b36faf588
parent381719b9a0adb8553a041633aed943f5ef48a1ff (diff)
parentae63c568711c128c64b1a94ef50e0af2ca91086b (diff)
downloadlibresourceqt-910493fac3e328cef329ade5e8c1a71e136ca246.tar.gz
Merge commit 'origin/master'
-rw-r--r--libdbus-qeventloop/dbusconnectioneventloop.cpp2
-rw-r--r--libdbus-qeventloop/dbusconnectioneventloop.h27
-rw-r--r--tests/test-dbus-pong/test-dbus-pong.cpp45
-rwxr-xr-xtests/test-dbus-qeventloop-runner.sh18
-rw-r--r--tests/test-dbus-qeventloop/test-dbus-qeventloop.cpp197
-rw-r--r--tests/tests.pro8
6 files changed, 258 insertions, 39 deletions
diff --git a/libdbus-qeventloop/dbusconnectioneventloop.cpp b/libdbus-qeventloop/dbusconnectioneventloop.cpp
index 9d7a255..2c040b2 100644
--- a/libdbus-qeventloop/dbusconnectioneventloop.cpp
+++ b/libdbus-qeventloop/dbusconnectioneventloop.cpp
@@ -241,7 +241,7 @@ void DBUSConnectionEventLoop::wakeupMain(void *data)
}
// The initialization point
-bool DBUSConnectionEventLoop::initConnection(DBusConnection* conn)
+bool DBUSConnectionEventLoop::addConnection(DBusConnection* conn)
{
bool rc;
diff --git a/libdbus-qeventloop/dbusconnectioneventloop.h b/libdbus-qeventloop/dbusconnectioneventloop.h
index 6f8d8e7..993c5c1 100644
--- a/libdbus-qeventloop/dbusconnectioneventloop.h
+++ b/libdbus-qeventloop/dbusconnectioneventloop.h
@@ -41,6 +41,11 @@
class QSocketNotifier;
class QTimerEvent;
+/**
+* This class is handling dbus notifications with QT events. QEventLoop must
+* be handled in order to handle dbus events.
+* Usage: DBUSConnectionEventLoop myLoop; myLoop.addConnection(bus);
+*/
class DBUSConnectionEventLoop : public QObject
{
Q_OBJECT
@@ -52,8 +57,15 @@ public:
virtual ~DBUSConnectionEventLoop();
public:
- bool initConnection(DBusConnection* conn);
-
+ /**
+ * Add new dbus connection into handler.
+ * \return true if everything went well.
+ */
+ bool addConnection(DBusConnection* conn);
+
+ /**
+ * Helper class for dbus watcher
+ */
class Watcher
{
public:
@@ -68,8 +80,19 @@ public:
typedef QHash<int, DBusTimeout*> Timeouts;
typedef QList<DBusConnection*> Connections;
+ /**
+ * DBusWatcher objects
+ */
Watchers watchers;
+
+ /**
+ * DBusTimeout objects
+ */
Timeouts timeouts;
+
+ /**
+ * DBusConnection objects
+ */
Connections connections;
public slots:
diff --git a/tests/test-dbus-pong/test-dbus-pong.cpp b/tests/test-dbus-pong/test-dbus-pong.cpp
index 701abb1..c936cc4 100644
--- a/tests/test-dbus-pong/test-dbus-pong.cpp
+++ b/tests/test-dbus-pong/test-dbus-pong.cpp
@@ -31,17 +31,50 @@ unsigned int Pong::timeout()
int main(int argc, char **argv)
{
+ bool useSessionBus = false;
+
QCoreApplication app(argc, argv);
+ if( app.arguments().count() > 1 )
+ {
+ if( app.arguments().at(1) == "--session" )
+ {
+ useSessionBus = true;
+ }
+ }
+
+ QDBusConnection* myBus;
+ if( useSessionBus )
+ {
+ myBus = new QDBusConnection(QDBusConnection::systemBus().sessionBus());
+ qDebug("Using session bus ...");
+ }
+ else
+ {
+ myBus = new QDBusConnection(QDBusConnection::systemBus().systemBus());
+ qDebug("Using system bus ...");
+ }
// Check system bus connection
- if( !QDBusConnection::systemBus().isConnected() )
+ if( !myBus->isConnected() )
{
- qDebug("Cannot connect to the D-Bus system bus.");
+ // Cleanup!!
+ delete myBus;
+ myBus = NULL;
+
+ if( useSessionBus )
+ {
+ qDebug("Cannot connect to the D-Bus session bus.");
+ }
+ else
+ {
+ qDebug("Cannot connect to the D-Bus system bus.");
+ }
+
return 1;
}
// Create listener service
- if( !QDBusConnection::systemBus().registerService("com.nokia.dbusqeventloop.test") )
+ if( !myBus->registerService("com.nokia.dbusqeventloop.test") )
{
qDebug("%s", qPrintable(QDBusConnection::systemBus().lastError().message()));
exit(2);
@@ -49,10 +82,14 @@ int main(int argc, char **argv)
Pong pong;
// Register all slots as dbus methods
- QDBusConnection::systemBus().registerObject("/", &pong, QDBusConnection::ExportAllSlots);
+ myBus->registerObject("/", &pong, QDBusConnection::ExportAllSlots);
// Let's go!
app.exec();
+ // Cleanup!!
+ delete myBus;
+ myBus = NULL;
+
return 0;
}
diff --git a/tests/test-dbus-qeventloop-runner.sh b/tests/test-dbus-qeventloop-runner.sh
new file mode 100755
index 0000000..75cab5e
--- /dev/null
+++ b/tests/test-dbus-qeventloop-runner.sh
@@ -0,0 +1,18 @@
+#!/bin/sh
+
+# Source and export D-Bus session info
+. /tmp/dbus-info
+export DBUS_SESSION_BUS_ADDRESS
+export DBUS_SESSION_BUS_PID
+export DBUS_SESSION_BUS_WINDOWID
+
+# Run pong server on system bus
+/usr/lib/libresourceqt-tests/test-dbus-pong &
+
+# Run pong server on system bus
+/usr/lib/libresourceqt-tests/test-dbus-pong --session &
+
+sleep 2
+
+# do the testing!
+/usr/lib/libresourceqt-tests/test-dbus-qeventloop
diff --git a/tests/test-dbus-qeventloop/test-dbus-qeventloop.cpp b/tests/test-dbus-qeventloop/test-dbus-qeventloop.cpp
index f50f95a..606d2a9 100644
--- a/tests/test-dbus-qeventloop/test-dbus-qeventloop.cpp
+++ b/tests/test-dbus-qeventloop/test-dbus-qeventloop.cpp
@@ -6,8 +6,9 @@ class TestDbusQEventLoop: public QObject
Q_OBJECT
private:
- DBusConnection* systemBus;
- DBUSConnectionEventLoop* dbusEventLoop;
+ DBusConnection* systemBus;
+ DBusConnection* sessionBus;
+ DBUSConnectionEventLoop* dbusEventLoop;
void resetValues()
{
@@ -16,9 +17,10 @@ private:
responseInt = 0;
typeError = false;
noResponse = false;
+ timerTimeout = false;
}
- void processQTEventLoop(DBusPendingCall* pending)
+ void processQTEventLoop(DBusPendingCall* pending, int timeout)
{
// Reset response values to zeros and reset errors
resetValues();
@@ -26,25 +28,40 @@ private:
// Do we have something pending?
if( pending == NULL )
{
+ qDebug("QTEventLoop: pending call is NULL!");
return;
}
+ MYDEBUGC("QTEventLoop: processing loop for %d ms", timeout);
// If we have some pending operation, let's get notification about result
dbus_pending_call_set_notify(pending, TestDbusQEventLoop::pendingNotify, this, NULL);
+ // Setup timeout timer
+ int activeTimer = startTimer(timeout);
+
// Pump QT event loop, but only until pending operation is not finished
while( !wasInNotifyFnc )
{
QCoreApplication::processEvents(QEventLoop::AllEvents);
+ if( timerTimeout )
+ {
+ MYDEBUGC("QTEventLoop: timeout elapsed!!");
+ return;
+ }
}
+
+ killTimer(activeTimer);
}
public:
+ int pongServerRunningSession;
+ int pongServerRunningSystem;
int wasInNotifyFnc;
const char* responseString;
uint32_t responseInt;
bool typeError;
bool noResponse;
+ bool timerTimeout;
TestDbusQEventLoop()
{
@@ -52,117 +69,239 @@ public:
}
protected:
+ void timerEvent(QTimerEvent *e)
+ {
+ // Set timeout flag and kill timer
+ timerTimeout = true;
+ killTimer(e->timerId());
+ }
+
static void pendingNotify(DBusPendingCall *pending, void *user_data)
{
MYDEBUG();
+ // Get pointer to main class
TestDbusQEventLoop* pThis = reinterpret_cast<TestDbusQEventLoop*>(user_data);
+ // Get message from pending call
DBusMessage* message = dbus_pending_call_steal_reply(pending);
DBusMessageIter args;
+ // Parse message
if( !dbus_message_iter_init(message, &args) )
MYDEBUGC("Reply message has no arguments!");
else
{
+ // Extract error message if is present
const char* error = dbus_message_get_error_name(message);
if( error != NULL )
{
+ // Check if it is NoReply message
pThis->noResponse = (strcmp(error, "org.freedesktop.DBus.Error.NoReply") == 0);
}
+ // Get first argument
int argType = dbus_message_iter_get_arg_type(&args);
switch( argType )
{
case DBUS_TYPE_STRING:
+ // Get string value
dbus_message_iter_get_basic(&args, &pThis->responseString);
+
+ // Display some debug info
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:
+ // Handle integer
dbus_message_iter_get_basic(&args, &pThis->responseInt);
+ // Display some debug info
MYDEBUGC("Got Reply: %d", pThis->responseInt);
break;
default:
- MYDEBUGC("Reply message argument has unsupported type (%d)!", argType);
+ // Set unknown type flag
pThis->typeError = true;
+ // Display some debug info
+ MYDEBUGC("Reply message argument has unsupported type (%d)!", argType);
}
}
+ // Free message if needed
if( message )
dbus_message_unref(message);
+ // Free pending call
dbus_pending_call_unref(pending);
-
+ // Set flag to end QEventLoop handler
pThis->wasInNotifyFnc = 1;
}
private slots:
void initTestCase()
{
+ // First allocate and obtain
+ dbusEventLoop = new DBUSConnectionEventLoop();
systemBus = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
- QVERIFY(systemBus != NULL);
+ sessionBus = dbus_bus_get(DBUS_BUS_SESSION, NULL);;
+ // Last check, if server is running
+ pongServerRunningSystem = dbus_bus_name_has_owner(systemBus, "com.nokia.dbusqeventloop.test", NULL);
+ pongServerRunningSession = dbus_bus_name_has_owner(sessionBus, "com.nokia.dbusqeventloop.test", NULL);
- dbusEventLoop = new DBUSConnectionEventLoop();
- QVERIFY(dbusEventLoop != NULL);
- QVERIFY(dbus_bus_name_has_owner(systemBus, "com.nokia.dbusqeventloop.test", NULL) == true);
+ // Create event loop ...
+ QVERIFY(dbusEventLoop != NULL);
+ QVERIFY(dbusEventLoop->addConnection(systemBus) == true);
+ QVERIFY(dbusEventLoop->addConnection(sessionBus) == true);
+ // Then test ... otherwise something don't have to be allocated (i.e. event loop)
+ QVERIFY(systemBus != NULL);
+ QVERIFY(sessionBus != NULL);
+ QVERIFY(pongServerRunningSystem != 0);
+ QVERIFY(pongServerRunningSession != 0);
}
void cleanupTestCase()
{
+ // Create "quit" method call message
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);
+ QVERIFY(message != NULL);
+
+ // Set correct values to suppress fake errors if connection fails in initTestCase()
+ bool systemTimeout = false, sessionTimeout = false;
+ int systemResponse = 12345, sessionResponse = 12345;
+
+ // If pong server is running on system bus then send quit
+ if( pongServerRunningSystem )
+ {
+ // Send the message
+ dbus_connection_send_with_reply(systemBus, message, &pending, 1000);
+ // Wait for response
+ processQTEventLoop(pending, 4000);
+ systemTimeout = timerTimeout;
+ systemResponse = responseInt;
+ }
- processQTEventLoop(pending);
- QVERIFY(responseInt == 12345);
+ // If pong server is running on session bus then send quit
+ if( pongServerRunningSession )
+ {
+ // Send the message
+ dbus_connection_send_with_reply(sessionBus, message, &pending, 1000);
+ // Wait for response
+ processQTEventLoop(pending, 4000);
+ sessionTimeout = timerTimeout;
+ sessionResponse = responseInt;
+ }
+ // Free message, it is not necessary to test, QVERIFY upper will end function
+ dbus_message_unref(message);
+
+ // Cleanup allocated pointers
QVERIFY(dbusEventLoop != NULL);
- delete dbusEventLoop;
- dbusEventLoop = NULL;
- }
+ if( dbusEventLoop )
+ {
+ delete dbusEventLoop;
+ dbusEventLoop = NULL;
+ }
- void initConnectionTest()
- {
- QVERIFY(dbusEventLoop->initConnection(systemBus) == true);
+ // Check if everything went well
+ QVERIFY(systemTimeout == false);
+ QVERIFY(systemResponse == 12345);
+ QVERIFY(sessionTimeout == false);
+ QVERIFY(sessionResponse == 12345);
}
- void pingTest()
+ void pingSystemBusTest()
{
+ // Create message
DBusMessage* message = dbus_message_new_method_call("com.nokia.dbusqeventloop.test", "/", NULL, "ping");
QVERIFY(message != NULL);
-
+ // Add argument to message
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;
+ // Send the message
dbus_connection_send_with_reply(systemBus, message, &pending, 3000);
- // Free the signal now we have finished with it
+ // Free message
dbus_message_unref(message);
-
- processQTEventLoop(pending);
+ // Process event loop
+ processQTEventLoop(pending, 4000);
+ // Check results
+ QVERIFY(timerTimeout == false);
+ QVERIFY(typeError == false);
QVERIFY(strcmp(temp, responseString) == 0);
}
- void timeoutTest()
+ void timeoutSystemBusTest()
{
+ // Create message
DBusMessage* message = dbus_message_new_method_call("com.nokia.dbusqeventloop.test", "/", NULL, "timeout");
QVERIFY(message != NULL);
DBusPendingCall* pending;
+ // Send the message
dbus_connection_send_with_reply(systemBus, message, &pending, 1000);
- // Free the signal now we have finished with it
+ // Free message
dbus_message_unref(message);
+ // Process event loop
+ processQTEventLoop(pending, 3000);
+ // Check results
+ QVERIFY(timerTimeout == false);
+ QVERIFY(noResponse == true);
+ QVERIFY(typeError == false);
+ // Small pause to process reply
+ sleep(1);
+ }
+
+ void pingSessionBusTest()
+ {
+ // Create message
+ DBusMessage* message = dbus_message_new_method_call("com.nokia.dbusqeventloop.test", "/", NULL, "ping");
+ QVERIFY(message != NULL);
+ // Add argument to message
+ 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);
- processQTEventLoop(pending);
+ DBusPendingCall* pending;
+ // Send the message
+ dbus_connection_send_with_reply(sessionBus, message, &pending, 3000);
+
+ // Free message
+ dbus_message_unref(message);
+ // Process event loop
+ processQTEventLoop(pending, 4000);
+ // Check results
+ QVERIFY(timerTimeout == false);
+ QVERIFY(typeError == false);
+ QVERIFY(strcmp(temp, responseString) == 0);
+ }
+
+ void timeoutSessionBusTest()
+ {
+ // Create message
+ DBusMessage* message = dbus_message_new_method_call("com.nokia.dbusqeventloop.test", "/", NULL, "timeout");
+ QVERIFY(message != NULL);
+
+ DBusPendingCall* pending;
+ // Send the message
+ dbus_connection_send_with_reply(sessionBus, message, &pending, 1000);
+
+ // Free message
+ dbus_message_unref(message);
+ // Process event loop
+ processQTEventLoop(pending, 3000);
+ // Check results
+ QVERIFY(timerTimeout == false);
QVERIFY(noResponse == true);
+ QVERIFY(typeError == false);
+ // Small pause to process reply
sleep(1);
}
};
diff --git a/tests/tests.pro b/tests/tests.pro
index 1d2853f..0c969df 100644
--- a/tests/tests.pro
+++ b/tests/tests.pro
@@ -12,6 +12,8 @@ SUBDIRS = test-dbus-qeventloop \
test-resource-engine
# Install options
-testsxml.path = /usr/share/libresourceqt-tests/
-testsxml.files = tests.xml
-INSTALLS = testsxml
+testsxml.path = /usr/share/libresourceqt-tests/
+testsxml.files = tests.xml
+testrunner.path = /usr/lib/libresourceqt-tests/
+testrunner.files = test-dbus-qeventloop-runner.sh
+INSTALLS = testsxml testrunner