summaryrefslogtreecommitdiff
path: root/qt4/tests/dbus/chan-basics.cpp
blob: 7b27e27584d969216763832d46e9690c91816887 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include <tests/lib/test.h>

#include <tests/lib/glib-helpers/test-conn-helper.h>

#include <tests/lib/glib/echo2/conn.h>
#include <tests/lib/glib/textchan-null.h>

#define TP_QT4_ENABLE_LOWLEVEL_API

#include <TelepathyQt4/Channel>
#include <TelepathyQt4/ChannelFactory>
#include <TelepathyQt4/Connection>
#include <TelepathyQt4/ConnectionLowlevel>
#include <TelepathyQt4/ContactFactory>
#include <TelepathyQt4/PendingChannel>
#include <TelepathyQt4/PendingHandles>
#include <TelepathyQt4/PendingReady>
#include <TelepathyQt4/ReferencedHandles>
#include <TelepathyQt4/TextChannel>

#include <telepathy-glib/debug.h>

using namespace Tp;

class TestChanBasics : public Test
{
    Q_OBJECT

public:
    TestChanBasics(QObject *parent = 0)
        : Test(parent), mConn(0), mHandle(0)
    { }

protected Q_SLOTS:
    void expectInvalidated();
    void expectPendingHandleFinished(Tp::PendingOperation *);

private Q_SLOTS:
    void initTestCase();
    void init();

    void testRequestHandle();
    void testCreateChannel();
    void testEnsureChannel();
    void testFallback();

    void cleanup();
    void cleanupTestCase();

private:
    TestConnHelper *mConn;
    ChannelPtr mChan;
    QString mChanObjectPath;
    uint mHandle;
};

void TestChanBasics::expectInvalidated()
{
    mLoop->exit(0);
}

void TestChanBasics::expectPendingHandleFinished(PendingOperation *op)
{
    TEST_VERIFY_OP(op);

    PendingHandles *pending = qobject_cast<PendingHandles*>(op);
    mHandle = pending->handles().at(0);
    mLoop->exit(0);
}

void TestChanBasics::initTestCase()
{
    initTestCaseImpl();

    g_type_init();
    g_set_prgname("chan-basics");
    tp_debug_set_flags("all");
    dbus_g_bus_get(DBUS_BUS_STARTER, 0);

    mConn = new TestConnHelper(this,
            EXAMPLE_TYPE_ECHO_2_CONNECTION,
            "account", "me@example.com",
            "protocol", "contacts",
            NULL);
    QCOMPARE(mConn->connect(), true);

    QCOMPARE(mConn->enableFeatures(Connection::FeatureSelfContact), true);
}

void TestChanBasics::init()
{
    initImpl();

    mChan.reset();
}

void TestChanBasics::testRequestHandle()
{
    // Test identifiers
    QStringList ids = QStringList() << QLatin1String("alice");

    // Request handles for the identifiers and wait for the request to process
    PendingHandles *pending = mConn->client()->lowlevel()->requestHandles(Tp::HandleTypeContact, ids);
    QVERIFY(connect(pending,
                    SIGNAL(finished(Tp::PendingOperation*)),
                    SLOT(expectPendingHandleFinished(Tp::PendingOperation*))));
    QCOMPARE(mLoop->exec(), 0);
    QVERIFY(disconnect(pending,
                       SIGNAL(finished(Tp::PendingOperation*)),
                       this,
                       SLOT(expectPendingHandleFinished(Tp::PendingOperation*))));
    QVERIFY(mHandle != 0);
}

void TestChanBasics::testCreateChannel()
{
    mChan = mConn->createChannel(TP_QT4_IFACE_CHANNEL_TYPE_TEXT, Tp::HandleTypeContact, mHandle);
    QVERIFY(mChan);
    mChanObjectPath = mChan->objectPath();

    QVERIFY(connect(mChan->becomeReady(),
                SIGNAL(finished(Tp::PendingOperation*)),
                SLOT(expectSuccessfulCall(Tp::PendingOperation*))));
    QCOMPARE(mLoop->exec(), 0);
    QCOMPARE(mChan->isReady(), true);
    QCOMPARE(mChan->isRequested(), true);
    QCOMPARE(mChan->channelType(), TP_QT4_IFACE_CHANNEL_TYPE_TEXT);
    QCOMPARE(mChan->groupCanAddContacts(), false);
    QCOMPARE(mChan->groupCanRemoveContacts(), false);
    QCOMPARE(mChan->initiatorContact()->id(), QString(QLatin1String("me@example.com")));
    QCOMPARE(mChan->groupSelfContact()->id(), QString(QLatin1String("me@example.com")));
    QCOMPARE(mChan->groupSelfContact(), mConn->client()->selfContact());
    QCOMPARE(mChan->targetId(), QString::fromLatin1("alice"));
    QVERIFY(!mChan->targetContact().isNull());
    QCOMPARE(mChan->targetContact()->id(), QString::fromLatin1("alice"));

    QStringList ids;
    Q_FOREACH (const ContactPtr &contact, mChan->groupContacts()) {
        ids << contact->id();

        QVERIFY(contact == mChan->groupSelfContact() || contact == mChan->targetContact());
    }
    ids.sort();
    QStringList toCheck = QStringList() << QLatin1String("me@example.com")
        << QLatin1String("alice");
    toCheck.sort();
    QCOMPARE(ids, toCheck);

    ChannelPtr chan = Channel::create(mConn->client(), mChan->objectPath(),
            mChan->immutableProperties());
    QVERIFY(chan);
    QVERIFY(chan->isValid());
    QVERIFY(connect(chan->becomeReady(),
                SIGNAL(finished(Tp::PendingOperation*)),
                SLOT(expectSuccessfulCall(Tp::PendingOperation*))));
    QCOMPARE(mLoop->exec(), 0);
    QCOMPARE(chan->isReady(), true);
    QCOMPARE(chan->channelType(), TP_QT4_IFACE_CHANNEL_TYPE_TEXT);

    // create an invalid connection to use as the channel connection
    ConnectionPtr conn = Connection::create(QLatin1String(""), QLatin1String("/"),
                  ChannelFactory::create(QDBusConnection::sessionBus()),
                  ContactFactory::create());
    QVERIFY(conn);
    QVERIFY(!conn->isValid());

    chan = Channel::create(conn, mChan->objectPath(),
            mChan->immutableProperties());
    QVERIFY(chan);
    QVERIFY(!chan->isValid());
    QVERIFY(connect(chan->becomeReady(),
                SIGNAL(finished(Tp::PendingOperation*)),
                SLOT(expectFailure(Tp::PendingOperation*))));
    QCOMPARE(mLoop->exec(), 0);
    QCOMPARE(mLastError, chan->invalidationReason());
    QCOMPARE(mLastErrorMessage, chan->invalidationMessage());
    QCOMPARE(chan->channelType(), QString());
}

void TestChanBasics::testEnsureChannel()
{
    ChannelPtr chan = mConn->ensureChannel(TP_QT4_IFACE_CHANNEL_TYPE_TEXT,
            Tp::HandleTypeContact, mHandle);
    QVERIFY(chan);
    QCOMPARE(chan->objectPath(), mChanObjectPath);
    mChan = chan;

    QVERIFY(connect(mChan->becomeReady(),
                SIGNAL(finished(Tp::PendingOperation*)),
                SLOT(expectSuccessfulCall(Tp::PendingOperation*))));
    QCOMPARE(mLoop->exec(), 0);
    QCOMPARE(mChan->isReady(), true);
    QCOMPARE(mChan->isRequested(), true);
    QCOMPARE(mChan->groupCanAddContacts(), false);
    QCOMPARE(mChan->groupCanRemoveContacts(), false);
    QCOMPARE(mChan->initiatorContact()->id(), QString(QLatin1String("me@example.com")));
    QCOMPARE(mChan->groupSelfContact()->id(), QString(QLatin1String("me@example.com")));
    QCOMPARE(mChan->groupSelfContact(), mConn->client()->selfContact());

    QStringList ids;
    Q_FOREACH (const ContactPtr &contact, mChan->groupContacts()) {
        ids << contact->id();
    }
    ids.sort();
    QStringList toCheck = QStringList() << QLatin1String("me@example.com")
        << QLatin1String("alice");
    toCheck.sort();
    QCOMPARE(ids, toCheck);

    QVERIFY(connect(mChan->requestClose(),
                SIGNAL(finished(Tp::PendingOperation*)),
                SLOT(expectSuccessfulCall(Tp::PendingOperation*))));
    QCOMPARE(mLoop->exec(), 0);
    QCOMPARE(mChan->isValid(), false);

    // calling requestClose again should be no-op
    QVERIFY(connect(mChan->requestClose(),
                SIGNAL(finished(Tp::PendingOperation*)),
                SLOT(expectSuccessfulCall(Tp::PendingOperation*))));
    QCOMPARE(mLoop->exec(), 0);
    QCOMPARE(mChan->isValid(), false);
}

void TestChanBasics::testFallback()
{
    TpHandleRepoIface *contactRepo = tp_base_connection_get_handles(
            TP_BASE_CONNECTION(mConn->service()),
            TP_HANDLE_TYPE_CONTACT);
    guint handle = tp_handle_ensure(contactRepo, "someone@localhost", 0, 0);

    QString textChanPath = mConn->objectPath() + QLatin1String("/Channel");
    QByteArray chanPath(textChanPath.toAscii());

    TpTestsTextChannelNull *textChanService = TP_TESTS_TEXT_CHANNEL_NULL (g_object_new (
                TP_TESTS_TYPE_TEXT_CHANNEL_NULL,
                "connection", mConn->service(),
                "object-path", chanPath.data(),
                "handle", handle,
                NULL));

    TextChannelPtr textChan = TextChannel::create(mConn->client(), textChanPath, QVariantMap());
    QVERIFY(connect(textChan->becomeReady(),
                SIGNAL(finished(Tp::PendingOperation*)),
                SLOT(expectSuccessfulCall(Tp::PendingOperation*))));
    QCOMPARE(mLoop->exec(), 0);
    QCOMPARE(textChan->isReady(), true);

    QCOMPARE(textChanService->get_channel_type_called, static_cast<uint>(1));
    QCOMPARE(textChanService->get_interfaces_called, static_cast<uint>(1));
    QCOMPARE(textChanService->get_handle_called, static_cast<uint>(1));

    QCOMPARE(textChan->channelType(), TP_QT4_IFACE_CHANNEL_TYPE_TEXT);
    QVERIFY(textChan->interfaces().isEmpty());
    QCOMPARE(textChan->targetHandleType(), Tp::HandleTypeContact);
    QCOMPARE(textChan->targetHandle(), handle);

    // we have no Group support, groupAddContacts should fail
    QVERIFY(connect(textChan->groupAddContacts(QList<ContactPtr>() << mConn->client()->selfContact()),
                    SIGNAL(finished(Tp::PendingOperation*)),
                    SLOT(expectFailure(Tp::PendingOperation*))));
    QCOMPARE(mLoop->exec(), 0);
    QCOMPARE(mLastError, TP_QT4_ERROR_NOT_IMPLEMENTED);
    QVERIFY(!mLastErrorMessage.isEmpty());
}

void TestChanBasics::cleanup()
{
    cleanupImpl();
}

void TestChanBasics::cleanupTestCase()
{
    QCOMPARE(mConn->disconnect(), true);
    delete mConn;

    if (mChan) {
        QVERIFY(!mChan->isValid());
        QVERIFY(mChan->invalidationReason() == TP_QT4_ERROR_CANCELLED ||
                mChan->invalidationReason() == TP_QT4_ERROR_ORPHANED);
    }

    cleanupTestCaseImpl();
}

QTEST_MAIN(TestChanBasics)
#include "_gen/chan-basics.cpp.moc.hpp"