From 6751c585385f017d08540bbe121606d642afb220 Mon Sep 17 00:00:00 2001 From: Martin Wolf Date: Thu, 4 Feb 2010 18:32:35 +0200 Subject: Automated tests for libdbus-qeventloop --- tests/test-dbus-ping/test-dbus-ping.cpp | 36 ----- tests/test-dbus-ping/test-dbus-ping.pro | 19 --- tests/test-dbus-pong/pong.h | 2 + tests/test-dbus-pong/test-dbus-pong.cpp | 33 +++-- .../test-dbus-qeventloop/test-dbus-qeventloop.cpp | 159 ++++++++++++++++++++- .../test-dbus-qeventloop/test-dbus-qeventloop.pro | 1 + tests/tests.pro | 1 - 7 files changed, 183 insertions(+), 68 deletions(-) delete mode 100644 tests/test-dbus-ping/test-dbus-ping.cpp delete mode 100644 tests/test-dbus-ping/test-dbus-ping.pro (limited to 'tests') 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 - -#include -#include - -#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 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(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 \ -- cgit v1.2.3