From fd22517204cd038ff47b33ccb05cc17384eb8230 Mon Sep 17 00:00:00 2001 From: Martin Wolf Date: Mon, 8 Feb 2010 14:39:14 +0200 Subject: Small refactoring, added unit tests --- tests/test-dbus-pong/test-dbus-pong.cpp | 45 ++++++- .../test-dbus-qeventloop/test-dbus-qeventloop.cpp | 136 ++++++++++++++++++--- tests/tests.pro | 8 +- 3 files changed, 162 insertions(+), 27 deletions(-) (limited to 'tests') 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/test-dbus-qeventloop.cpp b/tests/test-dbus-qeventloop/test-dbus-qeventloop.cpp index f50f95a..f39d3ea 100644 --- a/tests/test-dbus-qeventloop/test-dbus-qeventloop.cpp +++ b/tests/test-dbus-qeventloop/test-dbus-qeventloop.cpp @@ -7,6 +7,7 @@ class TestDbusQEventLoop: public QObject private: 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(); @@ -32,19 +34,29 @@ private: // 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 + startTimer(timeout); + // Pump QT event loop, but only until pending operation is not finished while( !wasInNotifyFnc ) { QCoreApplication::processEvents(QEventLoop::AllEvents); + if( timerTimeout ) + { + return; + } } } public: + int pongServerRunningSession; + int pongServerRunningSystem; int wasInNotifyFnc; const char* responseString; uint32_t responseInt; bool typeError; bool noResponse; + bool timerTimeout; TestDbusQEventLoop() { @@ -52,6 +64,12 @@ public: } protected: + void timerEvent(QTimerEvent *e) + { + timerTimeout = true; + killTimer(e->timerId()); + } + static void pendingNotify(DBusPendingCall *pending, void *user_data) { MYDEBUG(); @@ -76,9 +94,13 @@ protected: 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: @@ -102,37 +124,109 @@ protected: 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() { 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); - processQTEventLoop(pending); - QVERIFY(responseInt == 12345); + // Set correct values to suppress fake errors if connection fails in initTestCase() + bool systemTimeout = false, sessionTimeout = false; + int systemResponse = 12345, sessionResponse = 12345; + + if( pongServerRunningSystem ) + { + dbus_connection_send_with_reply(systemBus, message, &pending, 1000); + + processQTEventLoop(pending, 4000); + systemTimeout = timerTimeout; + systemResponse = responseInt; + } + + if( pongServerRunningSession ) + { + dbus_connection_send_with_reply(sessionBus, message, &pending, 1000); + + processQTEventLoop(pending, 4000); + sessionTimeout = timerTimeout; + sessionResponse = responseInt; + } + + if( message ) + { + dbus_message_unref(message); + } QVERIFY(dbusEventLoop != NULL); - delete dbusEventLoop; - dbusEventLoop = NULL; + if( dbusEventLoop ) + { + delete dbusEventLoop; + dbusEventLoop = NULL; + } + + QVERIFY(systemTimeout == false); + QVERIFY(systemResponse == 12345); + QVERIFY(sessionTimeout == false); + QVERIFY(sessionResponse == 12345); } - void initConnectionTest() + void pingSystemBusTest() { - QVERIFY(dbusEventLoop->initConnection(systemBus) == true); + 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, 4000); + QVERIFY(timerTimeout == false); + QVERIFY(strcmp(temp, responseString) == 0); } +/* + void timeoutSystemBusTest() + { + 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); - void pingTest() + processQTEventLoop(pending, 4000); + QVERIFY(timerTimeout == false); + QVERIFY(noResponse == true); + sleep(1); + } +*/ + void pingSessionBusTest() { DBusMessage* message = dbus_message_new_method_call("com.nokia.dbusqeventloop.test", "/", NULL, "ping"); QVERIFY(message != NULL); @@ -141,27 +235,29 @@ private slots: dbus_message_append_args(message, DBUS_TYPE_STRING, &temp, DBUS_TYPE_INVALID); DBusPendingCall* pending; - dbus_connection_send_with_reply(systemBus, message, &pending, 3000); + dbus_connection_send_with_reply(sessionBus, message, &pending, 3000); // Free the signal now we have finished with it dbus_message_unref(message); - processQTEventLoop(pending); + processQTEventLoop(pending, 4000); + QVERIFY(timerTimeout == false); QVERIFY(strcmp(temp, responseString) == 0); } - void timeoutTest() + void timeoutSessionBusTest() { 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); + dbus_connection_send_with_reply(sessionBus, message, &pending, 1000); // Free the signal now we have finished with it dbus_message_unref(message); - processQTEventLoop(pending); + processQTEventLoop(pending, 4000); + QVERIFY(timerTimeout == false); QVERIFY(noResponse == true); sleep(1); } diff --git a/tests/tests.pro b/tests/tests.pro index c24c97c..2740ab4 100644 --- a/tests/tests.pro +++ b/tests/tests.pro @@ -12,6 +12,8 @@ SUBDIRS = test-dbus-qeventloop \ test-resource-set # 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 -- cgit v1.2.3