From ae63c568711c128c64b1a94ef50e0af2ca91086b Mon Sep 17 00:00:00 2001 From: Martin Wolf Date: Mon, 8 Feb 2010 18:39:09 +0200 Subject: Updated comments, fixed unit tests --- .../test-dbus-qeventloop/test-dbus-qeventloop.cpp | 97 ++++++++++++++++------ 1 file changed, 70 insertions(+), 27 deletions(-) (limited to 'tests') diff --git a/tests/test-dbus-qeventloop/test-dbus-qeventloop.cpp b/tests/test-dbus-qeventloop/test-dbus-qeventloop.cpp index f39d3ea..606d2a9 100644 --- a/tests/test-dbus-qeventloop/test-dbus-qeventloop.cpp +++ b/tests/test-dbus-qeventloop/test-dbus-qeventloop.cpp @@ -6,9 +6,9 @@ class TestDbusQEventLoop: public QObject Q_OBJECT private: - DBusConnection* systemBus; - DBusConnection* sessionBus; - DBUSConnectionEventLoop* dbusEventLoop; + DBusConnection* systemBus; + DBusConnection* sessionBus; + DBUSConnectionEventLoop* dbusEventLoop; void resetValues() { @@ -28,14 +28,16 @@ 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 - startTimer(timeout); + int activeTimer = startTimer(timeout); // Pump QT event loop, but only until pending operation is not finished while( !wasInNotifyFnc ) @@ -43,9 +45,12 @@ private: QCoreApplication::processEvents(QEventLoop::AllEvents); if( timerTimeout ) { + MYDEBUGC("QTEventLoop: timeout elapsed!!"); return; } } + + killTimer(activeTimer); } public: @@ -66,6 +71,7 @@ public: protected: void timerEvent(QTimerEvent *e) { + // Set timeout flag and kill timer timerTimeout = true; killTimer(e->timerId()); } @@ -73,26 +79,35 @@ protected: static void pendingNotify(DBusPendingCall *pending, void *user_data) { MYDEBUG(); + // Get pointer to main class TestDbusQEventLoop* pThis = reinterpret_cast(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); @@ -102,22 +117,27 @@ protected: 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; } @@ -146,6 +166,7 @@ private slots: void cleanupTestCase() { + // Create "quit" method call message DBusMessage* message = dbus_message_new_method_call("com.nokia.dbusqeventloop.test", "/", NULL, "quit"); DBusPendingCall* pending; QVERIFY(message != NULL); @@ -154,29 +175,32 @@ private slots: 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; } + // 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; } - if( message ) - { - dbus_message_unref(message); - } + // Free message, it is not necessary to test, QVERIFY upper will end function + dbus_message_unref(message); + // Cleanup allocated pointers QVERIFY(dbusEventLoop != NULL); if( dbusEventLoop ) { @@ -184,6 +208,7 @@ private slots: dbusEventLoop = NULL; } + // Check if everything went well QVERIFY(systemTimeout == false); QVERIFY(systemResponse == 12345); QVERIFY(sessionTimeout == false); @@ -192,73 +217,91 @@ private slots: 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); - + // Process event loop processQTEventLoop(pending, 4000); + // Check results QVERIFY(timerTimeout == false); + QVERIFY(typeError == false); QVERIFY(strcmp(temp, responseString) == 0); } -/* + 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); - - processQTEventLoop(pending, 4000); + // 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); DBusPendingCall* pending; + // Send the message dbus_connection_send_with_reply(sessionBus, message, &pending, 3000); - // Free the signal now we have finished with it + // 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 the signal now we have finished with it + // Free message dbus_message_unref(message); - - processQTEventLoop(pending, 4000); + // Process event loop + processQTEventLoop(pending, 3000); + // Check results QVERIFY(timerTimeout == false); QVERIFY(noResponse == true); + QVERIFY(typeError == false); + // Small pause to process reply sleep(1); } }; -- cgit v1.2.3