summaryrefslogtreecommitdiff
path: root/tests/dbus/simple-observer.cpp
blob: c656b4210cf62f3143c07b17de2c09ba77c06f89 (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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
#include <QtCore/QDebug>
#include <QtCore/QTimer>
#include <QtDBus/QtDBus>
#include <QtTest/QtTest>

#include <cstring>

#include <QDateTime>
#include <QString>
#include <QVariantMap>

#define TP_QT_ENABLE_LOWLEVEL_API

#include <TelepathyQt/Account>
#include <TelepathyQt/ChannelClassSpec>
#include <TelepathyQt/Client>
#include <TelepathyQt/ConnectionLowlevel>
#include <TelepathyQt/Debug>
#include <TelepathyQt/PendingReady>
#include <TelepathyQt/SimpleCallObserver>
#include <TelepathyQt/SimpleObserver>
#include <TelepathyQt/SimpleTextObserver>
#include <TelepathyQt/StreamedMediaChannel>
#include <TelepathyQt/TextChannel>
#include <TelepathyQt/Types>

#include <telepathy-glib/cm-message.h>
#include <telepathy-glib/debug.h>

#include <glib-object.h>
#include <dbus/dbus-glib.h>

#include <tests/lib/glib/contacts-conn.h>
#include <tests/lib/glib/echo2/chan.h>
#include <tests/lib/glib/callable/media-channel.h>
#include <tests/lib/test.h>

using namespace Tp;
using namespace Tp::Client;

class TestSimpleObserver;

namespace
{

QList<ChannelPtr> channels(const QList<TextChannelPtr> &channels)
{
    QList<ChannelPtr> ret;
    Q_FOREACH (const TextChannelPtr &channel, channels) {
        ret << channel;
    }
    return ret;
}

QList<ChannelPtr> channels(const QList<StreamedMediaChannelPtr> &channels)
{
    QList<ChannelPtr> ret;
    Q_FOREACH (const StreamedMediaChannelPtr &channel, channels) {
        ret << channel;
    }
    return ret;
}

}

class AccountAdaptor : public QDBusAbstractAdaptor
{
    Q_OBJECT
    Q_CLASSINFO("D-Bus Interface", "org.freedesktop.Telepathy.Account")
    Q_CLASSINFO("D-Bus Introspection", ""
"  <interface name=\"org.freedesktop.Telepathy.Account\" >\n"
"    <property name=\"Interfaces\" type=\"as\" access=\"read\" />\n"
"    <property name=\"Connection\" type=\"o\" access=\"read\" />\n"
"    <signal name=\"AccountPropertyChanged\" >\n"
"      <arg name=\"Properties\" type=\"a{sv}\" />\n"
"    </signal>\n"
"  </interface>\n"
        "")

    Q_PROPERTY(QDBusObjectPath Connection READ Connection)
    Q_PROPERTY(QStringList Interfaces READ Interfaces)

public:
    AccountAdaptor(QObject *parent)
        : QDBusAbstractAdaptor(parent), mConnection(QLatin1String("/"))
    {
    }

    ~AccountAdaptor() override
    {
    }

    void setConnection(QString conn)
    {
        if (conn.isEmpty()) {
            conn = QLatin1String("/");
        }

        mConnection = QDBusObjectPath(conn);
        QVariantMap props;
        props.insert(QLatin1String("Connection"), QVariant::fromValue(mConnection));
        Q_EMIT AccountPropertyChanged(props);
    }

public: // Properties
    inline QDBusObjectPath Connection() const
    {
        return mConnection;
    }

    inline QStringList Interfaces() const
    {
        return QStringList();
    }

Q_SIGNALS: // Signals
    void AccountPropertyChanged(const QVariantMap &properties);

private:
    QDBusObjectPath mConnection;
};

class Dispatcher : public QObject, public QDBusContext
{
    Q_OBJECT;

public:
    Dispatcher(QObject *parent)
        : QObject(parent)
    {
    }

    ~Dispatcher() override
    {
    }
};

class TestSimpleObserver : public Test
{
    Q_OBJECT

public:
    TestSimpleObserver(QObject *parent = 0)
        : Test(parent),
          mChannelsCount(0), mSMChannelsCount(0)
    {
        std::memset(mMessagesChanServices, 0, sizeof(mMessagesChanServices));
        std::memset(mCallableChanServices, 0, sizeof(mCallableChanServices));
    }

protected Q_SLOTS:
    void onObserverNewChannels(const QList<Tp::ChannelPtr> &channels);
    void onObserverChannelInvalidated(const Tp::ChannelPtr &channel,
            const QString &errorName, const QString &errorMessage);
    void onObserverStreamedMediaCallStarted(
            const Tp::StreamedMediaChannelPtr &channel);
    void onObserverStreamedMediaCallEnded(
            const Tp::StreamedMediaChannelPtr &channel,
            const QString &errorMessage, const QString &errorName);

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

    void testObserverRegistration();
    void testCrossTalk();

    void cleanup();
    void cleanupTestCase();

private:
    QMap<QString, QString> ourObservers();

    AccountPtr mAccounts[2];

    struct ConnInfo {
        ConnInfo()
            : connService(0), baseConnService(0), contactRepo(0)
        {
        }

        TpTestsContactsConnection *connService;
        TpBaseConnection *baseConnService;
        ConnectionPtr conn;
        TpHandleRepoIface *contactRepo;
    };
    ConnInfo mConns[2];

    QStringList mContacts;

    ExampleEcho2Channel *mMessagesChanServices[2];
    TextChannelPtr mTextChans[2];

    ExampleCallableMediaChannel *mCallableChanServices[2];
    StreamedMediaChannelPtr mSMChans[2];

    int mChannelsCount;
    int mSMChannelsCount;
};

void TestSimpleObserver::onObserverNewChannels(const QList<Tp::ChannelPtr> &channels)
{
    QVERIFY(channels.size() == 1);
    mChannelsCount += channels.size();
}

void TestSimpleObserver::onObserverChannelInvalidated(const Tp::ChannelPtr &channel,
        const QString &errorName, const QString &errorMessage)
{
    QVERIFY(!channel.isNull());
    mChannelsCount -= 1;
}

void TestSimpleObserver::onObserverStreamedMediaCallStarted(
        const Tp::StreamedMediaChannelPtr &channel)
{
    QVERIFY(!channel.isNull());
    mSMChannelsCount++;
}

void TestSimpleObserver::onObserverStreamedMediaCallEnded(const Tp::StreamedMediaChannelPtr &channel,
        const QString &errorMessage, const QString &errorName)
{
    QVERIFY(!channel.isNull());
    mSMChannelsCount--;
}

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

    g_type_init();
    g_set_prgname("simple-observer");
    tp_debug_set_flags("all");
    dbus_g_bus_get(DBUS_BUS_STARTER, 0);

    QDBusConnection bus = QDBusConnection::sessionBus();
    QString channelDispatcherBusName = TP_QT_IFACE_CHANNEL_DISPATCHER;
    QString channelDispatcherPath = QLatin1String("/org/freedesktop/Telepathy/ChannelDispatcher");
    Dispatcher *dispatcher = new Dispatcher(this);
    QVERIFY(bus.registerService(channelDispatcherBusName));
    QVERIFY(bus.registerObject(channelDispatcherPath, dispatcher));

    mContacts << QLatin1String("alice") << QLatin1String("bob");

    // Create 2 accounts to be used by the tests:
    // - each account contains a connection, a text channel and a SM channel setup:
    // - the channels for the first account have alice as target and;
    // - the channels for the second account have bob as target
    for (int i = 0; i < 2; ++i) {
        // setup account
        QString accountBusName = TP_QT_IFACE_ACCOUNT_MANAGER;
        QString accountPath = QLatin1String("/org/freedesktop/Telepathy/Account/simple/account/") +
            QString::number(i);

        QObject *adaptorObject = new QObject(this);
        AccountAdaptor *accountAdaptor = new AccountAdaptor(adaptorObject);
        QVERIFY(bus.registerService(accountBusName));
        QVERIFY(bus.registerObject(accountPath, adaptorObject));

        AccountPtr acc = Account::create(accountBusName, accountPath);
        QVERIFY(connect(acc->becomeReady(),
                        SIGNAL(finished(Tp::PendingOperation *)),
                        SLOT(expectSuccessfulCall(Tp::PendingOperation *))));
        QCOMPARE(mLoop->exec(), 0);
        QCOMPARE(acc->isReady(), true);
        QCOMPARE(acc->supportsRequestHints(), false);
        QCOMPARE(acc->requestsSucceedWithChannel(), false);
        mAccounts[i] = acc;

        // setup conn
        TpTestsContactsConnection *connService = TP_TESTS_CONTACTS_CONNECTION(
                g_object_new(TP_TESTS_TYPE_CONTACTS_CONNECTION,
                    "account", "me@example.com",
                    "protocol", "example",
                    NULL));
        QVERIFY(connService != 0);
        TpBaseConnection *baseConnService = TP_BASE_CONNECTION(connService);
        QVERIFY(baseConnService != 0);

        gchar *connName, *connPath;
        GError *error = NULL;

        QString name(QLatin1String("example") + QString::number(i));
        QVERIFY(tp_base_connection_register(baseConnService,
                    name.toLatin1().constData(), &connName, &connPath, &error));
        QVERIFY(error == 0);

        QVERIFY(connName != 0);
        QVERIFY(connPath != 0);

        ConnectionPtr conn = Connection::create(QLatin1String(connName), QLatin1String(connPath),
                ChannelFactory::create(QDBusConnection::sessionBus()), ContactFactory::create());
        QCOMPARE(conn->isReady(), false);

        accountAdaptor->setConnection(QLatin1String(connPath));

        QVERIFY(connect(conn->lowlevel()->requestConnect(),
                        SIGNAL(finished(Tp::PendingOperation*)),
                        SLOT(expectSuccessfulCall(Tp::PendingOperation*))));
        QCOMPARE(mLoop->exec(), 0);
        QCOMPARE(conn->isReady(), true);
        QCOMPARE(conn->status(), ConnectionStatusConnected);

        TpHandleRepoIface *contactRepo = tp_base_connection_get_handles(baseConnService,
                TP_HANDLE_TYPE_CONTACT);

        mConns[i].connService = connService;
        mConns[i].baseConnService = baseConnService;
        mConns[i].conn = conn;
        mConns[i].contactRepo = contactRepo;

        // setup channels
        guint handle = tp_handle_ensure(contactRepo, mContacts[i].toLatin1().constData(), 0, 0);

        QString messagesChanPath = QLatin1String(connPath) +
            QLatin1String("/MessagesChannel/") + QString::number(i);
        mMessagesChanServices[i] = EXAMPLE_ECHO_2_CHANNEL(g_object_new(
                    EXAMPLE_TYPE_ECHO_2_CHANNEL,
                    "connection", connService,
                    "object-path", messagesChanPath.toLatin1().constData(),
                    "handle", handle,
                    NULL));

        QVariantMap immutableProperties;
        immutableProperties.insert(TP_QT_IFACE_CHANNEL + QLatin1String(".TargetID"), mContacts[i]);
        mTextChans[i] = TextChannel::create(conn, messagesChanPath, immutableProperties);
        QVERIFY(connect(mTextChans[i]->becomeReady(),
                    SIGNAL(finished(Tp::PendingOperation *)),
                    SLOT(expectSuccessfulCall(Tp::PendingOperation *))));
        QCOMPARE(mLoop->exec(), 0);

        QString callableChanPath = QLatin1String(connPath) +
            QLatin1String("/CallableChannel/") + QString::number(i);
        mCallableChanServices[i] = EXAMPLE_CALLABLE_MEDIA_CHANNEL(g_object_new(
                    EXAMPLE_TYPE_CALLABLE_MEDIA_CHANNEL,
                    "connection", connService,
                    "object-path", callableChanPath.toLatin1().constData(),
                    "handle", handle,
                    NULL));

        immutableProperties.clear();
        immutableProperties.insert(TP_QT_IFACE_CHANNEL + QLatin1String(".TargetID"), mContacts[i]);
        mSMChans[i] = StreamedMediaChannel::create(conn, callableChanPath, immutableProperties);
        QVERIFY(connect(mSMChans[i]->becomeReady(),
                    SIGNAL(finished(Tp::PendingOperation *)),
                    SLOT(expectSuccessfulCall(Tp::PendingOperation *))));
        QCOMPARE(mLoop->exec(), 0);

        g_free(connName);
        g_free(connPath);
    }
}

void TestSimpleObserver::init()
{
    initImpl();
}

void TestSimpleObserver::testObserverRegistration()
{
    QVERIFY(ourObservers().isEmpty());

    QList<SimpleObserverPtr> observers;
    QList<SimpleTextObserverPtr> textObservers;
    QList<SimpleCallObserverPtr> callObservers;
    int numRegisteredObservers = 0;
    // Observers should be shared by bus and channel class, meaning that
    // the following code should register only 4 observers:
    // - one for text chat rooms
    // - one for text chats
    // - one for incoming/outgoing calls
    // - one for incoming calls (incoming)
    //
    // The Simple*Observer instances are added to the lists observers/textObservers/callObservers,
    // so they don't get deleted (refcount == 0) when out of scope
    for (int i = 0; i < 2; ++i) {
        AccountPtr acc = mAccounts[i];

        for (int j = 0; j < 2; ++j) {
            QString contact = mContacts[j];

            if (i == 0 && j == 0) {
                numRegisteredObservers = 1;
            }

            // on first run the following code should register an observer for text chat rooms,
            // on consecutive runs it should use the already registered observer for text chat rooms
            SimpleObserverPtr observer = SimpleObserver::create(acc,
                    ChannelClassSpec::textChatroom(), contact);
            QCOMPARE(observer->account(), acc);
            QVERIFY(observer->channelFilter().size() == 1);
            QVERIFY(observer->channelFilter().contains(ChannelClassSpec::textChatroom()));
            QCOMPARE(observer->contactIdentifier(), contact);
            QVERIFY(observer->extraChannelFeatures().isEmpty());
            QVERIFY(observer->channels().isEmpty());
            observers.append(observer);
            QCOMPARE(ourObservers().size(), numRegisteredObservers);

            // the following code should always reuse the observer for text chat rooms already
            // created.
            QList<ChannelClassFeatures> extraChannelFeatures;
            extraChannelFeatures.append(QPair<ChannelClassSpec, Features>(
                        ChannelClassSpec::textChatroom(), Channel::FeatureCore));
            observer = SimpleObserver::create(acc,
                    ChannelClassSpec::textChatroom(), contact, extraChannelFeatures);
            QCOMPARE(observer->account(), acc);
            QVERIFY(observer->channelFilter().size() == 1);
            QVERIFY(observer->channelFilter().contains(ChannelClassSpec::textChatroom()));
            QCOMPARE(observer->contactIdentifier(), contact);
            QCOMPARE(observer->extraChannelFeatures(), extraChannelFeatures);
            QVERIFY(observer->channels().isEmpty());
            observers.append(observer);
            QCOMPARE(ourObservers().size(), numRegisteredObservers);

            if (i == 0 && j == 0) {
                numRegisteredObservers = 2;
            }

            // on first run the following code should register an observer for text chats,
            // on consecutive runs it should use the already registered observer for text chats
            SimpleTextObserverPtr textObserver = SimpleTextObserver::create(acc, contact);
            QCOMPARE(textObserver->account(), acc);
            QCOMPARE(textObserver->contactIdentifier(), contact);
            QVERIFY(textObserver->textChats().isEmpty());
            textObservers.append(textObserver);
            QCOMPARE(ourObservers().size(), numRegisteredObservers);

            // the following code should always reuse the observer for text chats already
            // created.
            textObserver = SimpleTextObserver::create(acc, contact);
            QCOMPARE(textObserver->account(), acc);
            QCOMPARE(textObserver->contactIdentifier(), contact);
            QVERIFY(textObserver->textChats().isEmpty());
            textObservers.append(textObserver);
            QCOMPARE(ourObservers().size(), numRegisteredObservers);

            if (i == 0 && j == 0) {
                numRegisteredObservers = 3;
            }

            // on first run the following code should register an observer for incoming/outgoing
            // calls, on consecutive runs it should use the already registered observer for
            // incoming/outgoing calls
            SimpleCallObserverPtr callObserver = SimpleCallObserver::create(acc, contact);
            QCOMPARE(callObserver->account(), acc);
            QCOMPARE(callObserver->contactIdentifier(), contact);
            QCOMPARE(callObserver->direction(), SimpleCallObserver::CallDirectionBoth);
            QVERIFY(callObserver->streamedMediaCalls().isEmpty());
            callObservers.append(callObserver);
            QCOMPARE(ourObservers().size(), numRegisteredObservers);

            if (i == 0 && j == 0) {
                numRegisteredObservers = 4;
            }

            // on first run the following code should register an observer for incoming calls,
            // on consecutive runs it should use the already registered observer for incoming calls
            callObserver = SimpleCallObserver::create(acc, contact,
                    SimpleCallObserver::CallDirectionIncoming);
            QCOMPARE(callObserver->account(), acc);
            QCOMPARE(callObserver->contactIdentifier(), contact);
            QCOMPARE(callObserver->direction(), SimpleCallObserver::CallDirectionIncoming);
            QVERIFY(callObserver->streamedMediaCalls().isEmpty());
            callObservers.append(callObserver);
            QCOMPARE(ourObservers().size(), numRegisteredObservers);
        }
    }

    // deleting all SimpleObserver instances (text chat room) should unregister 1 observer
    observers.clear();
    QCOMPARE(ourObservers().size(), 3);
    // deleting all SimpleTextObserver instances should unregister 1 observer
    textObservers.clear();
    QCOMPARE(ourObservers().size(), 2);
    // deleting all SimpleCallObserver instances should unregister 2 observers
    callObservers.clear();
    QVERIFY(ourObservers().isEmpty());
}

void TestSimpleObserver::testCrossTalk()
{
    SimpleObserverPtr observers[2];
    SimpleTextObserverPtr textObservers[2];
    SimpleTextObserverPtr textObserversNoContact[2];
    SimpleCallObserverPtr callObservers[2];
    SimpleCallObserverPtr callObserversNoContact[2];

    for (int i = 0; i < 2; ++i) {
        AccountPtr acc = mAccounts[i];
        observers[i] = SimpleObserver::create(acc,
                ChannelClassSpec::textChat(), mContacts[i]);
        QVERIFY(connect(observers[i].data(), SIGNAL(newChannels(QList<Tp::ChannelPtr>)),
                        SLOT(onObserverNewChannels(QList<Tp::ChannelPtr>))));
        QVERIFY(connect(observers[i].data(),
                        SIGNAL(channelInvalidated(Tp::ChannelPtr,QString,QString)),
                        SLOT(onObserverChannelInvalidated(Tp::ChannelPtr,QString,QString))));

        // SimpleTextObserver::messageSent/Received is already tested in contact-messenger test
        textObservers[i] = SimpleTextObserver::create(acc, mContacts[i]);
        textObserversNoContact[i] = SimpleTextObserver::create(acc);

        callObservers[i] = SimpleCallObserver::create(acc, mContacts[i]);
        QVERIFY(connect(callObservers[i].data(), SIGNAL(streamedMediaCallStarted(Tp::StreamedMediaChannelPtr)),
                        SLOT(onObserverStreamedMediaCallStarted(Tp::StreamedMediaChannelPtr))));
        QVERIFY(connect(callObservers[i].data(), SIGNAL(streamedMediaCallEnded(Tp::StreamedMediaChannelPtr,QString,QString)),
                        SLOT(onObserverStreamedMediaCallEnded(Tp::StreamedMediaChannelPtr,QString,QString))));
        callObserversNoContact[i] = SimpleCallObserver::create(acc);
    }

    QMap<QString, QString> ourObserversMap = ourObservers();
    QMap<QString, QString>::const_iterator it = ourObserversMap.constBegin();
    QMap<QString, QString>::const_iterator end = ourObserversMap.constEnd();
    for (; it != end; ++it) {
        ClientObserverInterface *observerIface = new ClientObserverInterface(
                it.key(), it.value(), this);

        ChannelClassList observerFilter;
        if (!waitForProperty(observerIface->requestPropertyObserverChannelFilter(),
                    &observerFilter)) {
            continue;
        }

        for (int i = 0; i < 2; ++i) {
            Q_FOREACH (const ChannelClassSpec &spec, observerFilter) {
                // only call ObserveChannels for text chat channels on observers that support text
                // chat
                if (spec.isSubsetOf(ChannelClassSpec::textChat())) {
                    ChannelDetails textChan = {
                        QDBusObjectPath(mTextChans[i]->objectPath()),
                        mTextChans[i]->immutableProperties()
                    };
                    observerIface->ObserveChannels(
                            QDBusObjectPath(mAccounts[i]->objectPath()),
                            QDBusObjectPath(mTextChans[i]->connection()->objectPath()),
                            ChannelDetailsList() << textChan,
                            QDBusObjectPath(QLatin1String("/")),
                            Tp::ObjectPathList(),
                            QVariantMap());
                    break;
                }
            }

            Q_FOREACH (const ChannelClassSpec &spec, observerFilter) {
                // only call ObserveChannels for SM channels on observers that support SM channels
                if (spec.isSubsetOf(ChannelClassSpec::streamedMediaCall())) {
                    ChannelDetails smChan = {
                        QDBusObjectPath(mSMChans[i]->objectPath()),
                        mSMChans[i]->immutableProperties()
                    };
                    observerIface->ObserveChannels(
                            QDBusObjectPath(mAccounts[i]->objectPath()),
                            QDBusObjectPath(mSMChans[i]->connection()->objectPath()),
                            ChannelDetailsList() << smChan,
                            QDBusObjectPath(QLatin1String("/")),
                            Tp::ObjectPathList(),
                            QVariantMap());
                    break;
                }
            }
        }
    }

    // due to QtDBus limitation, we cannot wait for ObserveChannels to finish properly before
    // proceeding, so we have to wait till all observers receive the channels
    while (observers[0]->channels().isEmpty() ||
           observers[1]->channels().isEmpty() ||
           textObservers[0]->textChats().isEmpty() ||
           textObservers[1]->textChats().isEmpty() ||
           textObserversNoContact[0]->textChats().isEmpty() ||
           textObserversNoContact[1]->textChats().isEmpty() ||
           callObservers[0]->streamedMediaCalls().isEmpty() ||
           callObservers[1]->streamedMediaCalls().isEmpty() ||
           callObserversNoContact[0]->streamedMediaCalls().isEmpty() ||
           callObserversNoContact[1]->streamedMediaCalls().isEmpty()) {
        mLoop->processEvents();
    }

    QCOMPARE(mChannelsCount, 2);
    QCOMPARE(mSMChannelsCount, 2);

    QCOMPARE(observers[0]->channels().size(), 1);
    QCOMPARE(textObservers[0]->textChats().size(), 1);
    QCOMPARE(textObserversNoContact[0]->textChats().size(), 1);
    QCOMPARE(callObservers[0]->streamedMediaCalls().size(), 1);
    QCOMPARE(callObserversNoContact[0]->streamedMediaCalls().size(), 1);
    QCOMPARE(observers[1]->channels().size(), 1);
    QCOMPARE(textObservers[1]->textChats().size(), 1);
    QCOMPARE(textObserversNoContact[1]->textChats().size(), 1);
    QCOMPARE(callObservers[1]->streamedMediaCalls().size(), 1);
    QCOMPARE(callObserversNoContact[1]->streamedMediaCalls().size(), 1);

    QVERIFY(textObservers[0]->textChats() != textObservers[1]->textChats());
    QVERIFY(textObserversNoContact[0]->textChats() != textObserversNoContact[1]->textChats());
    QCOMPARE(textObservers[0]->textChats(), textObserversNoContact[0]->textChats());
    QCOMPARE(textObservers[1]->textChats(), textObserversNoContact[1]->textChats());

    QVERIFY(callObservers[0]->streamedMediaCalls() != callObservers[1]->streamedMediaCalls());
    QVERIFY(callObservers[0]->streamedMediaCalls() != callObserversNoContact[1]->streamedMediaCalls());
    QCOMPARE(callObservers[0]->streamedMediaCalls(), callObserversNoContact[0]->streamedMediaCalls());
    QCOMPARE(callObservers[1]->streamedMediaCalls(), callObserversNoContact[1]->streamedMediaCalls());

    QCOMPARE(observers[0]->channels(), channels(textObservers[0]->textChats()));
    QVERIFY(observers[0]->channels() != channels(textObservers[1]->textChats()));
    QVERIFY(observers[0]->channels() != channels(callObservers[0]->streamedMediaCalls()));
    QVERIFY(observers[0]->channels() != channels(callObservers[1]->streamedMediaCalls()));
    QVERIFY(observers[1]->channels() != channels(textObservers[0]->textChats()));
    QCOMPARE(observers[1]->channels(), channels(textObservers[1]->textChats()));
    QVERIFY(observers[1]->channels() != channels(callObservers[0]->streamedMediaCalls()));
    QVERIFY(observers[1]->channels() != channels(callObservers[1]->streamedMediaCalls()));

    QVERIFY(channels(callObservers[0]->streamedMediaCalls()) != channels(textObservers[0]->textChats()));
    QVERIFY(channels(callObservers[0]->streamedMediaCalls()) != channels(textObservers[1]->textChats()));
    QVERIFY(channels(callObservers[1]->streamedMediaCalls()) != channels(textObservers[0]->textChats()));
    QVERIFY(channels(callObservers[1]->streamedMediaCalls()) != channels(textObservers[1]->textChats()));

    QCOMPARE(observers[0]->channels().first()->objectPath(), mTextChans[0]->objectPath());
    QCOMPARE(observers[1]->channels().first()->objectPath(), mTextChans[1]->objectPath());
    QCOMPARE(textObservers[0]->textChats().first()->objectPath(), mTextChans[0]->objectPath());
    QCOMPARE(textObservers[1]->textChats().first()->objectPath(), mTextChans[1]->objectPath());
    QCOMPARE(textObserversNoContact[0]->textChats().first()->objectPath(), mTextChans[0]->objectPath());
    QCOMPARE(textObserversNoContact[1]->textChats().first()->objectPath(), mTextChans[1]->objectPath());
    QCOMPARE(callObservers[0]->streamedMediaCalls().first()->objectPath(), mSMChans[0]->objectPath());
    QCOMPARE(callObservers[1]->streamedMediaCalls().first()->objectPath(), mSMChans[1]->objectPath());

    // invalidate channels
    for (int i = 0; i < 2; ++i) {
        if (mMessagesChanServices[i] != 0) {
            g_object_unref(mMessagesChanServices[i]);
            mMessagesChanServices[i] = 0;
        }

        if (mCallableChanServices[i] != 0) {
            g_object_unref(mCallableChanServices[i]);
            mCallableChanServices[i] = 0;
        }
    }

    while (!observers[0]->channels().isEmpty() ||
           !observers[1]->channels().isEmpty() ||
           !textObservers[0]->textChats().isEmpty() ||
           !textObservers[1]->textChats().isEmpty() ||
           !textObserversNoContact[0]->textChats().isEmpty() ||
           !textObserversNoContact[1]->textChats().isEmpty() ||
           !callObservers[0]->streamedMediaCalls().isEmpty() ||
           !callObservers[1]->streamedMediaCalls().isEmpty() ||
           !callObserversNoContact[0]->streamedMediaCalls().isEmpty() ||
           !callObserversNoContact[1]->streamedMediaCalls().isEmpty()) {
        mLoop->processEvents();
    }

    QCOMPARE(mChannelsCount, 0);
    QCOMPARE(mSMChannelsCount, 0);
}

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

void TestSimpleObserver::cleanupTestCase()
{
    for (int i = 0; i < 2; ++i) {
        if (!mConns[i].conn) {
            continue;
        }

        if (mConns[i].conn->requestedFeatures().contains(Connection::FeatureCore)) {
            QVERIFY(mConns[i].connService != NULL);

            if (TP_BASE_CONNECTION(mConns[i].connService)->status != TP_CONNECTION_STATUS_DISCONNECTED) {
                tp_base_connection_change_status(TP_BASE_CONNECTION(mConns[i].connService),
                        TP_CONNECTION_STATUS_DISCONNECTED,
                        TP_CONNECTION_STATUS_REASON_REQUESTED);
            }

            while (mConns[i].conn->isValid()) {
                mLoop->processEvents();
            }

        }
        mConns[i].conn.reset();

        mTextChans[i].reset();
        mSMChans[i].reset();

        if (mMessagesChanServices[i] != 0) {
            g_object_unref(mMessagesChanServices[i]);
            mMessagesChanServices[i] = 0;
        }

        if (mCallableChanServices[i] != 0) {
            g_object_unref(mCallableChanServices[i]);
            mCallableChanServices[i] = 0;
        }

        if (mConns[i].connService != 0) {
            mConns[i].baseConnService = 0;
            g_object_unref(mConns[i].connService);
            mConns[i].connService = 0;
        }
    }

    cleanupTestCaseImpl();
}

QMap<QString, QString> TestSimpleObserver::ourObservers()
{
    QStringList registeredNames =
        QDBusConnection::sessionBus().interface()->registeredServiceNames();
    QMap<QString, QString> observers;

    Q_FOREACH (QString name, registeredNames) {
        if (!name.startsWith(QLatin1String("org.freedesktop.Telepathy.Client.TpQtSO"))) {
            continue;
        }

        if (QDBusConnection::sessionBus().interface()->serviceOwner(name).value() !=
                QDBusConnection::sessionBus().baseService()) {
            continue;
        }

        QString path = QLatin1Char('/') + name;
        path.replace(QLatin1Char('.'), QLatin1Char('/'));

        ClientInterface client(name, path);
        QStringList ifaces;
        if (!waitForProperty(client.requestPropertyInterfaces(), &ifaces)) {
            continue;
        }

        if (!ifaces.contains(TP_QT_IFACE_CLIENT_OBSERVER)) {
            continue;
        }

        observers.insert(name, path);
    }

    return observers;
}

QTEST_MAIN(TestSimpleObserver)
#include "_gen/simple-observer.cpp.moc.hpp"