[Telepathy-commits] [telepathy-qt4/master] Channel: Added test for isReady/becomeReady.

Andre Moreira Magalhaes (andrunko) andre.magalhaes at collabora.co.uk
Wed Jan 28 13:31:14 PST 2009


---
 tests/dbus/Makefile.am     |    5 +
 tests/dbus/chan-basics.cpp |  335 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 340 insertions(+), 0 deletions(-)
 create mode 100644 tests/dbus/chan-basics.cpp

diff --git a/tests/dbus/Makefile.am b/tests/dbus/Makefile.am
index 35eacca..cb25b94 100644
--- a/tests/dbus/Makefile.am
+++ b/tests/dbus/Makefile.am
@@ -38,6 +38,7 @@ if ENABLE_TP_GLIB_TESTS
 # Tests which can only be run if we have telepathy-glib, and Qt was compiled
 # to use the GLib main loop
 TESTS += \
+    test-chan-basics \
     test-cm-basics \
     test-conn-basics \
     test-conn-requests \
@@ -46,6 +47,7 @@ TESTS += \
     test-stateful-proxy
 
 BUILT_SOURCES += \
+    _gen/chan-basics.cpp.moc.hpp \
     _gen/cm-basics.cpp.moc.hpp \
     _gen/conn-basics.cpp.moc.hpp \
     _gen/conn-requests.cpp.moc.hpp \
@@ -55,6 +57,9 @@ BUILT_SOURCES += \
 
 MOC_INCLUDES += $(TP_GLIB_CFLAGS)
 
+test_chan_basics_SOURCES = chan-basics.cpp
+test_chan_basics_LDADD = $(LDADD) $(top_builddir)/tests/lib/echo2/libtp-glib-echo2-tests.la
+
 test_cm_basics_SOURCES = cm-basics.cpp
 test_cm_basics_LDADD = $(LDADD) $(top_builddir)/tests/lib/libtp-glib-tests.la
 
diff --git a/tests/dbus/chan-basics.cpp b/tests/dbus/chan-basics.cpp
new file mode 100644
index 0000000..db97d31
--- /dev/null
+++ b/tests/dbus/chan-basics.cpp
@@ -0,0 +1,335 @@
+#include <QtCore/QDebug>
+#include <QtCore/QTimer>
+
+#include <QtDBus/QtDBus>
+
+#include <QtTest/QtTest>
+
+#include <TelepathyQt4/Client/Channel>
+#include <TelepathyQt4/Client/Connection>
+#include <TelepathyQt4/Client/PendingChannel>
+#include <TelepathyQt4/Client/PendingHandles>
+#include <TelepathyQt4/Client/ReferencedHandles>
+#include <TelepathyQt4/Debug>
+
+#include <telepathy-glib/debug.h>
+
+#include <tests/lib/echo2/conn.h>
+#include <tests/lib/test.h>
+
+using namespace Telepathy::Client;
+
+class TestChanBasics : public Test
+{
+    Q_OBJECT
+
+public:
+    TestChanBasics(QObject *parent = 0)
+        : Test(parent), mConnService(0), mConn(0), mChan(0), mHandle(0)
+    { }
+
+protected Q_SLOTS:
+    void expectConnReady(uint, uint);
+    void expectConnInvalidated();
+    void expectPendingHandleFinished(Telepathy::Client::PendingOperation*);
+    void expectCreateChannelFinished(Telepathy::Client::PendingOperation *);
+    void expectEnsureChannelFinished(Telepathy::Client::PendingOperation *);
+
+private Q_SLOTS:
+    void initTestCase();
+    void init();
+
+    void testRequestHandle();
+    void testCreateChannel();
+    void testEnsureChannel();
+
+    void cleanup();
+    void cleanupTestCase();
+
+private:
+    QString mConnName, mConnPath;
+    ExampleEcho2Connection *mConnService;
+    Connection *mConn;
+    Channel *mChan;
+    QString mChanObjectPath;
+    uint mHandle;
+};
+
+void TestChanBasics::expectConnReady(uint newStatus, uint newStatusReason)
+{
+    qDebug() << "connection changed to status" << newStatus;
+    switch (newStatus) {
+    case Connection::StatusDisconnected:
+        qWarning() << "Disconnected";
+        mLoop->exit(1);
+        break;
+    case Connection::StatusConnecting:
+        /* do nothing */
+        break;
+    case Connection::StatusConnected:
+        qDebug() << "Ready";
+        mLoop->exit(0);
+        break;
+    default:
+        qWarning().nospace() << "What sort of status is "
+            << newStatus << "?!";
+        mLoop->exit(2);
+        break;
+    }
+}
+
+void TestChanBasics::expectConnInvalidated()
+{
+    mLoop->exit(0);
+}
+
+void TestChanBasics::expectPendingHandleFinished(PendingOperation *op)
+{
+    if (!op->isFinished()) {
+        qWarning() << "unfinished";
+        mLoop->exit(1);
+        return;
+    }
+
+    if (op->isError()) {
+        qWarning().nospace() << op->errorName()
+            << ": " << op->errorMessage();
+        mLoop->exit(2);
+        return;
+    }
+
+    if (!op->isValid()) {
+        qWarning() << "inconsistent results";
+        mLoop->exit(3);
+        return;
+    }
+
+    qDebug() << "finished";
+    PendingHandles *pending = qobject_cast<PendingHandles*>(op);
+    mHandle = pending->handles().at(0);
+    mLoop->exit(0);
+}
+
+void TestChanBasics::expectCreateChannelFinished(PendingOperation* op)
+{
+    if (!op->isFinished()) {
+        qWarning() << "unfinished";
+        mLoop->exit(1);
+        return;
+    }
+
+    if (op->isError()) {
+        qWarning().nospace() << op->errorName()
+            << ": " << op->errorMessage();
+        mLoop->exit(2);
+        return;
+    }
+
+    if (!op->isValid()) {
+        qWarning() << "inconsistent results";
+        mLoop->exit(3);
+        return;
+    }
+
+    PendingChannel *pc = qobject_cast<PendingChannel*>(op);
+    mChan = pc->channel();
+    mChanObjectPath = mChan->objectPath();
+    mLoop->exit(0);
+}
+
+void TestChanBasics::expectEnsureChannelFinished(PendingOperation* op)
+{
+    if (!op->isFinished()) {
+        qWarning() << "unfinished";
+        mLoop->exit(1);
+        return;
+    }
+
+    if (op->isError()) {
+        qWarning().nospace() << op->errorName()
+            << ": " << op->errorMessage();
+        mLoop->exit(2);
+        return;
+    }
+
+    if (!op->isValid()) {
+        qWarning() << "inconsistent results";
+        mLoop->exit(3);
+        return;
+    }
+
+    PendingChannel *pc = qobject_cast<PendingChannel*>(op);
+    mChan = pc->channel();
+    QCOMPARE(pc->yours(), false);
+    QCOMPARE(mChan->objectPath(), mChanObjectPath);
+    mLoop->exit(0);
+}
+
+void TestChanBasics::initTestCase()
+{
+    initTestCaseImpl();
+
+    g_type_init();
+    g_set_prgname("conn-basics");
+    tp_debug_set_flags("all");
+
+    gchar *name;
+    gchar *connPath;
+    GError *error = 0;
+
+    mConnService = EXAMPLE_ECHO_2_CONNECTION(g_object_new(
+            EXAMPLE_TYPE_ECHO_2_CONNECTION,
+            "account", "me at example.com",
+            "protocol", "contacts",
+            0));
+    QVERIFY(mConnService != 0);
+    QVERIFY(tp_base_connection_register(TP_BASE_CONNECTION(mConnService),
+                "contacts", &name, &connPath, &error));
+    QVERIFY(error == 0);
+
+    QVERIFY(name != 0);
+    QVERIFY(connPath != 0);
+
+    mConnName = name;
+    mConnPath = connPath;
+
+    g_free(name);
+    g_free(connPath);
+
+    mConn = new Connection(mConnName, mConnPath);
+    QCOMPARE(mConn->isReady(), false);
+
+    mConn->requestConnect();
+
+    QVERIFY(connect(mConn->becomeReady(),
+                    SIGNAL(finished(Telepathy::Client::PendingOperation*)),
+                    SLOT(expectSuccessfulCall(Telepathy::Client::PendingOperation*))));
+    QCOMPARE(mLoop->exec(), 0);
+    QCOMPARE(mConn->isReady(), true);
+
+    if (mConn->status() != Connection::StatusConnected) {
+        QVERIFY(connect(mConn,
+                        SIGNAL(statusChanged(uint, uint)),
+                        SLOT(expectConnReady(uint, uint))));
+        QCOMPARE(mLoop->exec(), 0);
+        QVERIFY(disconnect(mConn,
+                           SIGNAL(statusChanged(uint, uint)),
+                           this,
+                           SLOT(expectConnReady(uint, uint))));
+        QCOMPARE(mConn->status(), (uint) Connection::StatusConnected);
+    }
+
+    QVERIFY(mConn->requestsInterface() != 0);
+}
+
+void TestChanBasics::init()
+{
+    initImpl();
+}
+
+void TestChanBasics::testRequestHandle()
+{
+    // Test identifiers
+    QStringList ids = QStringList() << "alice";
+
+    // Request handles for the identifiers and wait for the request to process
+    PendingHandles *pending = mConn->requestHandles(Telepathy::HandleTypeContact, ids);
+    QVERIFY(connect(pending,
+                    SIGNAL(finished(Telepathy::Client::PendingOperation*)),
+                    SLOT(expectPendingHandleFinished(Telepathy::Client::PendingOperation*))));
+    QCOMPARE(mLoop->exec(), 0);
+    QVERIFY(disconnect(pending,
+                       SIGNAL(finished(Telepathy::Client::PendingOperation*)),
+                       this,
+                       SLOT(expectPendingHandleFinished(Telepathy::Client::PendingOperation*))));
+    QVERIFY(mHandle != 0);
+}
+
+void TestChanBasics::testCreateChannel()
+{
+    QVariantMap request;
+    request.insert(QLatin1String(TELEPATHY_INTERFACE_CHANNEL ".ChannelType"),
+                   TELEPATHY_INTERFACE_CHANNEL_TYPE_TEXT);
+    request.insert(QLatin1String(TELEPATHY_INTERFACE_CHANNEL ".TargetHandleType"),
+                   Telepathy::HandleTypeContact);
+    request.insert(QLatin1String(TELEPATHY_INTERFACE_CHANNEL ".TargetHandle"),
+                   mHandle);
+    QVERIFY(connect(mConn->createChannel(request),
+                    SIGNAL(finished(Telepathy::Client::PendingOperation*)),
+                    SLOT(expectCreateChannelFinished(Telepathy::Client::PendingOperation*))));
+    QCOMPARE(mLoop->exec(), 0);
+
+    if (mChan) {
+        QVERIFY(connect(mChan->becomeReady(),
+                        SIGNAL(finished(Telepathy::Client::PendingOperation*)),
+                        SLOT(expectSuccessfulCall(Telepathy::Client::PendingOperation*))));
+        QCOMPARE(mLoop->exec(), 0);
+        QCOMPARE(mChan->isReady(), true);
+
+        delete mChan;
+        mChan = 0;
+    }
+}
+
+void TestChanBasics::testEnsureChannel()
+{
+    QVariantMap request;
+    request.insert(QLatin1String(TELEPATHY_INTERFACE_CHANNEL ".ChannelType"),
+                   TELEPATHY_INTERFACE_CHANNEL_TYPE_TEXT);
+    request.insert(QLatin1String(TELEPATHY_INTERFACE_CHANNEL ".TargetHandleType"),
+                   Telepathy::HandleTypeContact);
+    request.insert(QLatin1String(TELEPATHY_INTERFACE_CHANNEL ".TargetHandle"),
+                   mHandle);
+    QVERIFY(connect(mConn->ensureChannel(request),
+                    SIGNAL(finished(Telepathy::Client::PendingOperation*)),
+                    SLOT(expectEnsureChannelFinished(Telepathy::Client::PendingOperation*))));
+    QCOMPARE(mLoop->exec(), 0);
+
+    if (mChan) {
+        QVERIFY(connect(mChan->becomeReady(),
+                        SIGNAL(finished(Telepathy::Client::PendingOperation*)),
+                        SLOT(expectSuccessfulCall(Telepathy::Client::PendingOperation*))));
+        QCOMPARE(mLoop->exec(), 0);
+        QCOMPARE(mChan->isReady(), true);
+
+        delete mChan;
+        mChan = 0;
+    }
+}
+
+void TestChanBasics::cleanup()
+{
+    cleanupImpl();
+}
+
+void TestChanBasics::cleanupTestCase()
+{
+    if (mConn != 0) {
+        // Disconnect and wait for the readiness change
+        QVERIFY(connect(mConn->requestDisconnect(),
+                        SIGNAL(finished(Telepathy::Client::PendingOperation*)),
+                        SLOT(expectSuccessfulCall(Telepathy::Client::PendingOperation*))));
+        QCOMPARE(mLoop->exec(), 0);
+
+        if (mConn->isValid()) {
+            QVERIFY(connect(mConn,
+                            SIGNAL(invalidated(Telepathy::Client::DBusProxy *,
+                                               const QString &, const QString &)),
+                            SLOT(expectConnInvalidated())));
+            QCOMPARE(mLoop->exec(), 0);
+        }
+
+        delete mConn;
+        mConn = 0;
+    }
+
+    if (mConnService != 0) {
+        g_object_unref(mConnService);
+        mConnService = 0;
+    }
+
+    cleanupTestCaseImpl();
+}
+
+QTEST_MAIN(TestChanBasics)
+#include "_gen/chan-basics.cpp.moc.hpp"
-- 
1.5.6.5




More information about the telepathy-commits mailing list