Some unit test cleanup and fixes
[qtcontacts-tracker:sirajs-contactsd-merge.git] / tests / telepathyplugin / test-telepathy-plugin.cpp
1 /***************************************************************************
2 **
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (people-users@projects.maemo.org)
6 **
7 ** This file is part of contactsd.
8 **
9 ** If you have questions regarding the use of this file, please contact
10 ** Nokia at people-users@projects.maemo.org.
11 **
12 ** This library is free software; you can redistribute it and/or
13 ** modify it under the terms of the GNU Lesser General Public
14 ** License version 2.1 as published by the Free Software Foundation
15 ** and appearing in the file LICENSE.LGPL included in the packaging
16 ** of this file.
17 **
18 ****************************************************************************/
19
20 #include "test-telepathy-plugin.h"
21
22 #include <QMap>
23 #include <QPair>
24
25 #include <QtTracker/Tracker>
26 #include <QtTracker/ontologies/nco.h>
27 #include <QtTracker/ontologies/nie.h>
28 #include <qcontactfilters.h>
29 #include <qtcontacts.h>
30
31 #include <TelepathyQt4/PendingReady>
32
33 #include <telepathy-glib/svc-account-manager.h>
34 #include <telepathy-glib/svc-account.h>
35
36 #define ACCOUNT_PATH TP_ACCOUNT_OBJECT_PATH_BASE "fakecm/fakeproto/UnitTest"
37 #define BUS_NAME "org.maemo.Contactsd.UnitTest"
38
39 TestExpectation::TestExpectation():presence(TP_TESTS_CONTACTS_CONNECTION_STATUS_UNKNOWN)
40 {
41 }
42
43 void TestExpectation::verify(QContact &contact) const
44 {
45     QString acutalAccountUri = contact.detail<QContactOnlineAccount>().accountUri();
46     QCOMPARE(acutalAccountUri, accountUri);
47
48     QString actualAlias = contact.detail<QContactDisplayLabel>().label();
49     QCOMPARE(actualAlias, alias);
50
51     QContactPresence::PresenceState actualPresence = contact.detail<QContactPresence>().presenceState();
52     switch (presence) {
53     case TP_TESTS_CONTACTS_CONNECTION_STATUS_AVAILABLE:
54         QCOMPARE(actualPresence, QContactPresence::PresenceAvailable);
55         break;
56     case TP_TESTS_CONTACTS_CONNECTION_STATUS_BUSY:
57         QCOMPARE(actualPresence, QContactPresence::PresenceBusy);
58         break;
59     case TP_TESTS_CONTACTS_CONNECTION_STATUS_AWAY:
60         QCOMPARE(actualPresence, QContactPresence::PresenceAway);
61         break;
62     case TP_TESTS_CONTACTS_CONNECTION_STATUS_OFFLINE:
63         QCOMPARE(actualPresence, QContactPresence::PresenceOffline);
64         break;
65     case TP_TESTS_CONTACTS_CONNECTION_STATUS_UNKNOWN:
66         QCOMPARE(actualPresence, QContactPresence::PresenceUnknown);
67         break;
68     case TP_TESTS_CONTACTS_CONNECTION_STATUS_ERROR:
69         break;
70     }
71
72     QString avatarFileName = contact.detail<QContactAvatar>().imageUrl().path();
73     if (avatarData.isEmpty()) {
74         QVERIFY(avatarFileName.isEmpty());
75     } else {
76         QFile file(avatarFileName);
77         QCOMPARE(file.readAll(), avatarData);
78     }
79 }
80
81 TestTelepathyPlugin::TestTelepathyPlugin(QObject *parent) : Test(parent), mContactManager(0)
82 {
83 }
84
85 void TestTelepathyPlugin::initTestCase()
86 {
87     initTestCaseImpl();
88
89     g_type_init();
90     g_set_prgname("test-telepathy-plugin");
91     tp_debug_set_flags("all");
92     dbus_g_bus_get(DBUS_BUS_STARTER, 0);
93
94     /* Create a QContactManager and track added/changed contacts */
95     mContactManager = new QContactManager(QLatin1String("tracker"));
96     connect(mContactManager,
97             SIGNAL(contactsAdded(const QList<QContactLocalId>&)),
98             SLOT(contactsAdded(const QList<QContactLocalId>&)));
99     connect(mContactManager,
100             SIGNAL(contactsChanged(const QList<QContactLocalId>&)),
101             SLOT(contactsChanged(const QList<QContactLocalId>&)));
102
103     /* Create a fake Connection */
104     mConnService = TP_BASE_CONNECTION(g_object_new(
105             TP_TESTS_TYPE_CONTACTS_CONNECTION,
106             "account", "fakeaccount",
107             "protocol", "fakeproto",
108             NULL));
109     QVERIFY(mConnService != 0);
110     QVERIFY(tp_base_connection_register(mConnService, "fakecm", NULL, NULL, NULL));
111     TpHandleRepoIface *serviceRepo = tp_base_connection_get_handles(
112         mConnService, TP_HANDLE_TYPE_CONTACT);
113     mConnService->self_handle = tp_handle_ensure(serviceRepo,
114         "fakeselfcontact", NULL, NULL);
115     tp_base_connection_change_status(mConnService,
116         TP_CONNECTION_STATUS_CONNECTED,
117         TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED);
118     mListManager = tp_tests_contacts_connection_get_contact_list_manager(
119         TP_TESTS_CONTACTS_CONNECTION(mConnService));
120     g_object_set(mListManager, "simulation-delay", 0, NULL);
121
122     /* Request the UnitTest bus name, so the AM knows we are ready to go */
123     TpDBusDaemon *dbus = tp_dbus_daemon_dup(NULL);
124     QVERIFY(tp_dbus_daemon_request_name(dbus, BUS_NAME, FALSE, NULL));
125     g_object_unref (dbus);
126 }
127
128 void TestTelepathyPlugin::testTrackerImport()
129 {
130     TestContactListManager *listManager =
131         tp_tests_contacts_connection_get_contact_list_manager(
132             TP_TESTS_CONTACTS_CONNECTION(mConnService));
133
134     /* Create a new contact "alice" */
135     TpHandleRepoIface *serviceRepo =
136         tp_base_connection_get_handles(mConnService, TP_HANDLE_TYPE_CONTACT);
137     TpHandle handle = tp_handle_ensure(serviceRepo, "alice", NULL, NULL);
138     QVERIFY(handle != 0);
139
140     /* Set alias to "Alice" */
141     const char *alias = "Alice";
142     tp_tests_contacts_connection_change_aliases(
143         TP_TESTS_CONTACTS_CONNECTION (mConnService),
144         1, &handle, &alias);
145
146     /* Add Alice in the ContactList */
147     test_contact_list_manager_add_to_list(listManager, NULL,
148         TEST_CONTACT_LIST_SUBSCRIBE, handle, "please", NULL);
149
150     TestExpectation e;
151     e.event = TestExpectation::Changed; // FIXME: Should be Added
152     e.accountUri = QString("alice");
153     e.alias = QString("Alice");
154     e.presence = TP_TESTS_CONTACTS_CONNECTION_STATUS_AVAILABLE;
155     mExpectations.append(e);
156
157     /* Wait for the scenario to happen */
158     QCOMPARE(mLoop->exec(), 0);
159
160     /* Change the presence of Alice to busy */
161     TpTestsContactsConnectionPresenceStatusIndex presence =
162         TP_TESTS_CONTACTS_CONNECTION_STATUS_BUSY;
163     const gchar *message = "Making coffee";
164     tp_tests_contacts_connection_change_presences(
165         TP_TESTS_CONTACTS_CONNECTION (mConnService),
166         1, &handle, &presence, &message);
167
168     e.event = TestExpectation::Changed;
169     e.presence = presence;
170     mExpectations.append(e);
171
172     /* Wait for the scenario to happen */
173     QCOMPARE(mLoop->exec(), 0);
174
175 #if 0
176     /* Change the avatar of Alice */
177     const gchar avatarData[] = "fake-avatar-data";
178     const gchar avatarToken[] = "fake-avatar-token";
179     const gchar avatarMimeType[] = "fake-avatar-mime-type";
180     GArray *array = g_array_new(FALSE, FALSE, sizeof(gchar));
181     g_array_append_vals (array, avatarData, strlen(avatarData));
182     tp_tests_contacts_connection_change_avatar_data(
183         TP_TESTS_CONTACTS_CONNECTION (mConnService),
184         handle, array, avatarMimeType, avatarToken);
185
186     e.avatarData = QByteArray(avatarData);
187     mExpectations.append(e);
188
189     /* Wait for the scenario to happen */
190     QCOMPARE(mLoop->exec(), 0);
191 #endif
192
193     qDebug() << "All expectations passed";
194 }
195
196 void TestTelepathyPlugin::verify(TestExpectation::Event event,
197     const QList<QContactLocalId> &contactIds)
198 {
199     foreach (QContactLocalId localId, contactIds) {
200         if (mExpectations.isEmpty()) {
201             mLoop->exit(0);
202         }
203         QVERIFY(!mExpectations.isEmpty());
204
205         const TestExpectation &e = mExpectations.takeFirst();
206         QCOMPARE(e.event, event);
207
208         QContact contact = mContactManager->contact(localId);
209         e.verify(contact);
210     }
211
212     if (mExpectations.isEmpty()) {
213         mLoop->exit(0);
214     }
215 }
216
217 void TestTelepathyPlugin::contactsAdded(const QList<QContactLocalId>& contactIds)
218 {
219     qDebug() << "Got contactsAdded";
220     verify(TestExpectation::Added, contactIds);
221 }
222
223 void TestTelepathyPlugin::contactsChanged(const QList<QContactLocalId>& contactIds)
224 {
225     qDebug() << "Got contactsChanged";
226     verify(TestExpectation::Changed, contactIds);
227 }
228
229 void TestTelepathyPlugin::cleanupTestCase()
230 {
231     tp_base_connection_change_status(mConnService,
232         TP_CONNECTION_STATUS_DISCONNECTED,
233         TP_CONNECTION_STATUS_REASON_REQUESTED);
234
235     g_object_unref(mAccountManagerService);
236     g_object_unref(mAccountService);
237     g_object_unref(mConnService);
238
239     delete mContactManager;
240
241     cleanupTestCaseImpl();
242 }
243
244 void TestTelepathyPlugin::init()
245 {
246     initImpl();
247 }
248
249 void TestTelepathyPlugin::cleanup()
250 {
251     cleanupImpl();
252 }
253
254 QTEST_MAIN(TestTelepathyPlugin)