summaryrefslogtreecommitdiff
path: root/tests/dbus/conn-basics.cpp
blob: 5caada37bd849e92530f312d4ea86ffd3249446c (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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include <QtCore/QDebug>
#include <QtCore/QTimer>

#include <QtDBus/QtDBus>

#include <QtTest/QtTest>

#define TP_QT_ENABLE_LOWLEVEL_API

#include <TelepathyQt/ChannelFactory>
#include <TelepathyQt/Connection>
#include <TelepathyQt/ConnectionLowlevel>
#include <TelepathyQt/ContactFactory>
#include <TelepathyQt/PendingChannel>
#include <TelepathyQt/PendingReady>
#include <TelepathyQt/Debug>

#include <telepathy-glib/dbus.h>
#include <telepathy-glib/debug.h>

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

using namespace Tp;

class TestConnBasics : public Test
{
    Q_OBJECT

public:
    TestConnBasics(QObject *parent = nullptr)
        : Test(parent), mConnService(nullptr)
    { }

protected Q_SLOTS:
    void expectConnReady(Tp::ConnectionStatus);
    void expectConnInvalidated();
    void expectPresenceAvailable(const Tp::SimplePresence &);
    void onRequestConnectFinished(Tp::PendingOperation *);

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

    void testBasics();
    void testSimplePresence();

    void cleanup();
    void cleanupTestCase();

private:
    QString mConnName, mConnPath;
    TpTestsContactsConnection *mConnService;
    ConnectionPtr mConn;
    QList<ConnectionStatus> mStatuses;
};

void TestConnBasics::expectConnReady(Tp::ConnectionStatus newStatus)
{
    qDebug() << "connection changed to status" << newStatus;
    switch (newStatus) {
    case ConnectionStatusDisconnected:
        qWarning() << "Disconnected";
        break;
    case ConnectionStatusConnecting:
        QCOMPARE(mConn->isReady(Connection::FeatureConnected), false);
        mStatuses << newStatus;
        qDebug() << "Connecting";
        break;
    case ConnectionStatusConnected:
        QCOMPARE(mConn->isReady(Connection::FeatureConnected), true);
        mStatuses << newStatus;
        qDebug() << "Connected";
        break;
    default:
        qWarning().nospace() << "What sort of status is "
            << newStatus << "?!";
        break;
    }
}

void TestConnBasics::expectConnInvalidated()
{
    qDebug() << "conn invalidated";

    mLoop->exit(0);
}

void TestConnBasics::expectPresenceAvailable(const Tp::SimplePresence &presence)
{
    if (presence.type == Tp::ConnectionPresenceTypeAvailable) {
        mLoop->exit(0);
        return;
    }
    mLoop->exit(1);
}

void TestConnBasics::onRequestConnectFinished(Tp::PendingOperation *op)
{
    QCOMPARE(mConn->status(), ConnectionStatusConnected);
    QVERIFY(mStatuses.contains(ConnectionStatusConnected));
    mLoop->exit(0);
}

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

    g_type_init();
    g_set_prgname("conn-basics");
    tp_debug_set_flags("all");
    dbus_g_bus_get(DBUS_BUS_STARTER, nullptr);
}

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

    gchar *name;
    gchar *connPath;
    GError *error = nullptr;

    mConnService = TP_TESTS_CONTACTS_CONNECTION(g_object_new(
            TP_TESTS_TYPE_CONTACTS_CONNECTION,
            "account", "me@example.com",
            "protocol", "contacts",
            NULL));
    QVERIFY(mConnService != nullptr);
    QVERIFY(tp_base_connection_register(TP_BASE_CONNECTION(mConnService),
                "contacts", &name, &connPath, &error));
    QVERIFY(error == nullptr);

    QVERIFY(name != nullptr);
    QVERIFY(connPath != nullptr);

    mConnName = QLatin1String(name);
    mConnPath = QLatin1String(connPath);

    g_free(name);
    g_free(connPath);

    mConn = Connection::create(mConnName, mConnPath,
            ChannelFactory::create(QDBusConnection::sessionBus()),
            ContactFactory::create());
    QCOMPARE(mConn->isReady(), false);

    QVERIFY(connect(mConn.data(),
                    SIGNAL(statusChanged(Tp::ConnectionStatus)),
                    SLOT(expectConnReady(Tp::ConnectionStatus))));

    qDebug() << "waiting connection to become connected";
    PendingOperation *pr = mConn->becomeReady(Connection::FeatureConnected);
    QVERIFY(connect(pr,
                    SIGNAL(finished(Tp::PendingOperation*)),
                    SLOT(expectSuccessfulCall(Tp::PendingOperation*))));

    PendingOperation *pc = mConn->lowlevel()->requestConnect();
    QVERIFY(connect(pc,
                    SIGNAL(finished(Tp::PendingOperation*)),
                    SLOT(onRequestConnectFinished(Tp::PendingOperation*))));
    QCOMPARE(mLoop->exec(), 0);
    QCOMPARE(pr->isFinished(), true);
    QCOMPARE(mLoop->exec(), 0);
    QCOMPARE(pc->isFinished(), true);
    QCOMPARE(mConn->isReady(Connection::FeatureConnected), true);
    qDebug() << "connection is now ready";

    QVERIFY(disconnect(mConn.data(),
                       SIGNAL(statusChanged(Tp::ConnectionStatus)),
                       this,
                       SLOT(expectConnReady(Tp::ConnectionStatus))));
}

void TestConnBasics::testBasics()
{
    QCOMPARE(static_cast<uint>(mConn->statusReason()),
            static_cast<uint>(ConnectionStatusReasonRequested));
}

void TestConnBasics::testSimplePresence()
{
    qDebug() << "Making SimplePresence ready";

    Features features = Features() << Connection::FeatureSimplePresence;
    QCOMPARE(mConn->isReady(features), false);
    QVERIFY(connect(mConn->becomeReady(features),
                    SIGNAL(finished(Tp::PendingOperation*)),
                    SLOT(expectSuccessfulCall(Tp::PendingOperation*))));
    QCOMPARE(mLoop->exec(), 0);
    QCOMPARE(mConn->isReady(features), true);

    qDebug() << "SimplePresence ready";
    qDebug() << "mConn->status:" << mConn->status();

    const QStringList canSetNames = QStringList()
        << QLatin1String("available")
        << QLatin1String("busy")
        << QLatin1String("away");

    const QStringList cantSetNames = QStringList()
        << QLatin1String("offline")
        << QLatin1String("unknown")
        << QLatin1String("error");

    const QStringList expectedNames = canSetNames + cantSetNames;

    const ConnectionPresenceType expectedTypes[] = {
        ConnectionPresenceTypeAvailable,
        ConnectionPresenceTypeBusy,
        ConnectionPresenceTypeAway,
        ConnectionPresenceTypeOffline,
        ConnectionPresenceTypeUnknown,
        ConnectionPresenceTypeError
    };

    SimpleStatusSpecMap statuses = mConn->lowlevel()->allowedPresenceStatuses();
    Q_FOREACH (QString name, statuses.keys()) {
        QVERIFY(expectedNames.contains(name));

        if (canSetNames.contains(name)) {
            QVERIFY(statuses[name].maySetOnSelf);
            QVERIFY(statuses[name].canHaveMessage);
        } else {
            QVERIFY(cantSetNames.contains(name));
            QVERIFY(!statuses[name].maySetOnSelf);
            QVERIFY(!statuses[name].canHaveMessage);
        }

        QCOMPARE(statuses[name].type,
                 static_cast<uint>(expectedTypes[expectedNames.indexOf(name)]));
    }

    QCOMPARE(mConn->lowlevel()->maxPresenceStatusMessageLength(), (uint) 512);
}

void TestConnBasics::cleanup()
{
    if (mConn) {
        QVERIFY(mConn->isValid());

        GHashTable *details = tp_asv_new(
                "debug-message", G_TYPE_STRING, "woo i'm going doooooown",
                "x-tpqt-test-rgba-herring-color", G_TYPE_UINT, 0xff0000ffU,
                NULL
                );

        // Disconnect and wait for invalidation
        tp_base_connection_disconnect_with_dbus_error(TP_BASE_CONNECTION(mConnService),
                TP_QT_ERROR_CANCELLED.latin1(),
                details,
                TP_CONNECTION_STATUS_REASON_REQUESTED);

        g_hash_table_destroy(details);

        QVERIFY(connect(mConn.data(),
                    SIGNAL(invalidated(Tp::DBusProxy *,
                            const QString &, const QString &)),
                    SLOT(expectConnInvalidated())));
        QCOMPARE(mLoop->exec(), 0);
        QVERIFY(!mConn->isValid());

        // Check that we got the connection error details
        QCOMPARE(static_cast<uint>(mConn->statusReason()),
                 static_cast<uint>(ConnectionStatusReasonRequested));

        QVERIFY(mConn->errorDetails().isValid());

        QVERIFY(mConn->errorDetails().hasDebugMessage());
        QCOMPARE(mConn->errorDetails().debugMessage(), QLatin1String("woo i'm going doooooown"));

#if 0
        // Not yet there
        QVERIFY(!mConn->errorDetails().hasExpectedHostname());
        QVERIFY(!mConn->errorDetails().hasCertificateHostname());
#endif

        QVERIFY(mConn->errorDetails().allDetails().contains(
                    QLatin1String("x-tpqt-test-rgba-herring-color")));
        QCOMPARE(qdbus_cast<uint>(mConn->errorDetails().allDetails().value(
                        QLatin1String("x-tpqt-test-rgba-herring-color"))),
                0xff0000ffU);

        processDBusQueue(mConn.data());

        mConn.reset();
    }

    if (mConnService != nullptr) {
        g_object_unref(mConnService);
        mConnService = nullptr;
    }

    cleanupImpl();
}

void TestConnBasics::cleanupTestCase()
{
    cleanupTestCaseImpl();
}

QTEST_MAIN(TestConnBasics)
#include "_gen/conn-basics.cpp.moc.hpp"