summaryrefslogtreecommitdiff
path: root/tests/dbus/contacts-location.cpp
blob: d1c1f406176ebc182231c017ac31544c528b3124 (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
#include <tests/lib/test.h>

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

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

#include <TelepathyQt/Connection>
#include <TelepathyQt/Contact>
#include <TelepathyQt/LocationInfo>
#include <TelepathyQt/ContactManager>
#include <TelepathyQt/PendingContacts>

#include <telepathy-glib/debug.h>

using namespace Tp;

class TestContactsLocation : public Test
{
    Q_OBJECT

public:
    TestContactsLocation(QObject *parent = nullptr)
        : Test(parent), mConn(nullptr), mContactsLocationUpdated(0)
    { }

protected Q_SLOTS:
    void onLocationInfoUpdated(const Tp::LocationInfo &location);

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

    void testLocation();

    void cleanup();
    void cleanupTestCase();

private:
    TestConnHelper *mConn;
    int mContactsLocationUpdated;
};

void TestContactsLocation::onLocationInfoUpdated(const Tp::LocationInfo &location)
{
    Q_UNUSED(location);
    mContactsLocationUpdated++;
    mLoop->exit(0);
}

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

    g_type_init();
    g_set_prgname("contacts-location");
    tp_debug_set_flags("all");
    dbus_g_bus_get(DBUS_BUS_STARTER, nullptr);

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

void TestContactsLocation::init()
{
    initImpl();
    mContactsLocationUpdated = 0;
}

void TestContactsLocation::testLocation()
{
    ContactManagerPtr contactManager = mConn->client()->contactManager();

    QVERIFY(contactManager->supportedFeatures().contains(Contact::FeatureLocation));

    QStringList validIDs = QStringList() << QLatin1String("foo")
        << QLatin1String("bar");
    QList<ContactPtr> contacts = mConn->contacts(validIDs, Contact::FeatureLocation);
    QCOMPARE(contacts.size(), validIDs.size());
    for (int i = 0; i < contacts.size(); i++) {
        ContactPtr contact = contacts[i];

        QCOMPARE(contact->requestedFeatures().contains(Contact::FeatureLocation), true);
        QCOMPARE(contact->actualFeatures().contains(Contact::FeatureLocation), true);

        QVERIFY(connect(contact.data(),
                        SIGNAL(locationUpdated(const Tp::LocationInfo &)),
                        SLOT(onLocationInfoUpdated(const Tp::LocationInfo &))));
    }

    GHashTable *location_1 = tp_asv_new(
        "country",  G_TYPE_STRING, "United-kingdoms",
        "lat",  G_TYPE_DOUBLE, 20.0,
        NULL);
    GHashTable *location_2 = tp_asv_new(
        "country",  G_TYPE_STRING, "Atlantis",
        "lat",  G_TYPE_DOUBLE, 10.0,
        NULL);
    GHashTable *locations[] = { location_1, location_2 };

    TpHandle handles[] = { 0, 0 };
    TpHandleRepoIface *serviceRepo = tp_base_connection_get_handles(
            TP_BASE_CONNECTION(mConn->service()), TP_HANDLE_TYPE_CONTACT);

    for (unsigned i = 0; i < 2; i++) {
        handles[i] = tp_handle_ensure(serviceRepo, qPrintable(validIDs[i]),
                nullptr, nullptr);
    }

    tp_tests_contacts_connection_change_locations(TP_TESTS_CONTACTS_CONNECTION(mConn->service()), 2,
            handles, locations);

    while (mContactsLocationUpdated != 2)  {
        QCOMPARE(mLoop->exec(), 0);
    }

    for (int i = 0; i < contacts.size(); i++) {
        ContactPtr contact = contacts[i];

        QCOMPARE(contact->location().country(),
                 QLatin1String(tp_asv_get_string(locations[i], "country")));
        QCOMPARE(contact->location().latitude(),
                 tp_asv_get_double(locations[i], "lat", nullptr));
    }

    g_hash_table_unref(location_1);
    g_hash_table_unref(location_2);
}

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

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

    cleanupTestCaseImpl();
}

QTEST_MAIN(TestContactsLocation)
#include "_gen/contacts-location.cpp.moc.hpp"