1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the Qt Mobility Components. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "ut_qtcontacts_trackerplugin.h" |
|
43 |
|
44 #include <QMap> |
|
45 #include <QPair> |
|
46 #include <QUuid> |
|
47 |
|
48 #include <QtTracker/Tracker> |
|
49 #include <QtTracker/ontologies/nco.h> |
|
50 #include <QtTracker/ontologies/nie.h> |
|
51 #include <qcontactfilters.h> |
|
52 #include <QContactChangeLogFilter> |
|
53 #include <qtcontacts.h> |
|
54 #include <trackerchangelistener.h> |
|
55 #include <qcontactrelationshipsaverequest.h> |
|
56 #include <qcontactrelationshipfetchrequest.h> |
|
57 #include <qtrackercontactidfetchrequest.h> |
|
58 #include <qcontacttrackerbackend_p.h> |
|
59 #include <qtrackercontactasyncrequest.h> |
|
60 |
|
61 #include "contactmanager.h" |
|
62 |
|
63 // update this when creating debian package |
|
64 const QString PATH_TO_SPARQL_TESTS("./ut_qtcontacts_trackerplugin_data"); |
|
65 |
|
66 ut_qtcontacts_trackerplugin::ut_qtcontacts_trackerplugin() |
|
67 { |
|
68 |
|
69 } |
|
70 |
|
71 void ut_qtcontacts_trackerplugin::initTestCase() |
|
72 { |
|
73 QMap<QString, QString> trackerEngineParams; |
|
74 trackerEngine = new QContactTrackerEngine(trackerEngineParams); |
|
75 errorMap = new QMap<int, QContactManager::Error>(); |
|
76 } |
|
77 |
|
78 void ut_qtcontacts_trackerplugin::testContacts() |
|
79 { |
|
80 QContact c1; |
|
81 QContact c2; |
|
82 |
|
83 trackerEngine->saveContact(&c1, error); |
|
84 trackerEngine->saveContact(&c2, error); |
|
85 QVERIFY2((error == QContactManager::NoError),"Saving contact"); |
|
86 QList<QContactLocalId> contacts = trackerEngine->contactIds(queryFilter, sortOrders, error); |
|
87 QVERIFY2(contacts.contains(c1.localId()), "Previously added contact is not found"); |
|
88 QVERIFY2(contacts.contains(c2.localId()), "Previously added contact is not found"); |
|
89 } |
|
90 |
|
91 void ut_qtcontacts_trackerplugin::testContact() |
|
92 { |
|
93 // Test invalid contact id |
|
94 QContact invalidContact = trackerEngine->contact_impl( -1, QStringList(), error); |
|
95 QVERIFY(error != QContactManager::NoError); |
|
96 |
|
97 // Add a contact |
|
98 QContact newContact; |
|
99 const QContactLocalId oldid = newContact.localId(); |
|
100 QVERIFY( trackerEngine->saveContact( &newContact, error ) ); |
|
101 |
|
102 QContactLocalId id = newContact.localId(); |
|
103 QVERIFY( id != oldid ); |
|
104 |
|
105 // Find the added contact |
|
106 QContact c = trackerEngine->contact_impl( id, QStringList(), error ); |
|
107 QVERIFY( c.localId() == newContact.localId() ); |
|
108 } |
|
109 |
|
110 void ut_qtcontacts_trackerplugin::testSaveName() |
|
111 { |
|
112 QContact c; |
|
113 QContactLocalId initialId = c.localId(); |
|
114 int detailsAdded = 0; |
|
115 |
|
116 QMap<QString,QString> nameValues; |
|
117 QContactName name; |
|
118 nameValues.insert(QLatin1String(QContactName::FieldPrefix), "Mr"); |
|
119 nameValues.insert(QLatin1String(QContactName::FieldFirst), "John"); |
|
120 nameValues.insert(QLatin1String(QContactName::FieldMiddle), "Rupert"); |
|
121 nameValues.insert(QLatin1String(QContactName::FieldLast), "Doe"); |
|
122 // nameValues.insert(QContactName::FieldSuffix, "III"); |
|
123 |
|
124 foreach (QString field, nameValues.keys()) { |
|
125 name.setValue(field, nameValues.value(field)); |
|
126 } |
|
127 c.saveDetail(&name); |
|
128 |
|
129 QContactNickname nick; |
|
130 nick.setValue(QLatin1String(QContactNickname::FieldNickname), "Johnny"); |
|
131 c.saveDetail(&nick); |
|
132 |
|
133 QVERIFY(c.detail<QContactName>().prefix() == "Mr"); |
|
134 QVERIFY(c.detail<QContactName>().firstName() == "John"); |
|
135 QVERIFY(c.detail<QContactName>().middleName() == "Rupert"); |
|
136 QVERIFY(c.detail<QContactName>().lastName() == "Doe"); |
|
137 QVERIFY(c.detail<QContactNickname>().nickname() == "Johnny"); |
|
138 |
|
139 detailsAdded++; |
|
140 |
|
141 trackerEngine->saveContact(&c, error); |
|
142 QCOMPARE(error, QContactManager::NoError); |
|
143 QVERIFY(c.localId() != initialId); |
|
144 QContact contact = this->contact(c.localId()); |
|
145 QList<QContactName> details = contact.details<QContactName>(); |
|
146 QList<QContactNickname> details2 = contact.details<QContactNickname>(); |
|
147 QCOMPARE(details.count(), detailsAdded); |
|
148 QCOMPARE(details2.count(), detailsAdded); |
|
149 // Name is unique |
|
150 foreach(QString field, nameValues.keys()) { |
|
151 QCOMPARE(details.at(0).value(field), nameValues.value(field)); |
|
152 } |
|
153 QCOMPARE(details2.at(0).value(QLatin1String(QContactNickname::FieldNickname)), QString("Johnny")); |
|
154 |
|
155 // Try changing the name of the saved contact. |
|
156 { |
|
157 QMap<QString,QString> nameValues; |
|
158 QContactName name = c.detail<QContactName>(); |
|
159 nameValues.insert(QLatin1String(QContactName::FieldPrefix), "Mr2"); |
|
160 nameValues.insert(QLatin1String(QContactName::FieldFirst), "John2"); |
|
161 nameValues.insert(QLatin1String(QContactName::FieldMiddle), "Rupert2"); |
|
162 nameValues.insert(QLatin1String(QContactName::FieldLast), ""); |
|
163 // nameValues.insert(QContactName::FieldSuffix, "III"); |
|
164 |
|
165 foreach (QString field, nameValues.keys()) { |
|
166 name.setValue(field, nameValues.value(field)); |
|
167 } |
|
168 c.saveDetail(&name); |
|
169 |
|
170 QContactNickname nick = c.detail<QContactNickname>(); |
|
171 nick.setValue(QLatin1String(QContactNickname::FieldNickname), "Johnny2"); |
|
172 c.saveDetail(&nick); |
|
173 |
|
174 |
|
175 QVERIFY(trackerEngine->saveContact(&c, error)); |
|
176 QCOMPARE(error, QContactManager::NoError); |
|
177 QVERIFY(c.localId() != initialId); |
|
178 |
|
179 QContact contact = trackerEngine->contact_impl(c.localId(), QStringList(), error); |
|
180 QCOMPARE(error, QContactManager::NoError); |
|
181 QList<QContactName> details = contact.details<QContactName>(); |
|
182 QList<QContactNickname> details2 = contact.details<QContactNickname>(); |
|
183 QCOMPARE(details.count(), detailsAdded); |
|
184 QCOMPARE(details2.count(), detailsAdded); |
|
185 // Name is unique |
|
186 foreach(QString field, nameValues.keys()) { |
|
187 QCOMPARE(details.at(0).value(field), nameValues.value(field)); |
|
188 } |
|
189 QCOMPARE(details2.at(0).value(QLatin1String(QContactNickname::FieldNickname)), QString("Johnny2")); |
|
190 |
|
191 // now try to add new name detail fails - this is how currently unique fields are implemented |
|
192 // so cover it in unit tests |
|
193 QContactName name1; |
|
194 name1.setValue(QContactName::FieldFirst, "Something that wont be stored as name is unique"); |
|
195 c.saveDetail(&name1); |
|
196 // validate that unique name is not saved |
|
197 QVERIFY(!trackerEngine->saveContact(&c, error)); |
|
198 details = contact.details<QContactName>(); |
|
199 details2 = contact.details<QContactNickname>(); |
|
200 QCOMPARE(details.count(), detailsAdded); |
|
201 QCOMPARE(details2.count(), detailsAdded); |
|
202 } |
|
203 } |
|
204 |
|
205 void ut_qtcontacts_trackerplugin::testSavePhoneNumber() |
|
206 { |
|
207 // use the same values for 2 contacts |
|
208 for (int i = 0; i <2; i++ ) |
|
209 { |
|
210 QContact c; |
|
211 QContactLocalId initialId = c.localId(); |
|
212 int detailsAdded = 0; |
|
213 QContactName name; |
|
214 name.setFirstName("I have phone numbers"); |
|
215 name.setLastName("Girl"); |
|
216 c.saveDetail(&name); |
|
217 |
|
218 // key: phonenumber; value: context,subtype |
|
219 QMap<QString,QPair<QString,QString> > phoneValues; |
|
220 |
|
221 phoneValues.insert("(704)486-6472", QPair<QString,QString>(QLatin1String(QContactDetail::ContextHome), QString())); |
|
222 phoneValues.insert("(765)957-1663", QPair<QString,QString>(QLatin1String(QContactDetail::ContextHome), QString())); |
|
223 phoneValues.insert("(999)888-1111", QPair<QString,QString>(QLatin1String(QContactDetail::ContextHome), |
|
224 QLatin1String(QContactPhoneNumber::SubTypeMobile))); |
|
225 |
|
226 phoneValues.insert("(792)123-6113", QPair<QString,QString>(QLatin1String(QContactDetail::ContextWork), QString())); |
|
227 phoneValues.insert("(918)491-7361", QPair<QString,QString>(QLatin1String(QContactDetail::ContextWork), |
|
228 QLatin1String(QContactPhoneNumber::SubTypeMobile))); |
|
229 phoneValues.insert("(412)670-1514", QPair<QString,QString>(QLatin1String(QContactDetail::ContextWork), |
|
230 QLatin1String(QContactPhoneNumber::SubTypeCar))); |
|
231 QMap<QString,QPair<QString,QString> > formattedPhoneValues; |
|
232 |
|
233 |
|
234 foreach (QString number, phoneValues.keys()) { |
|
235 QContactPhoneNumber phone; |
|
236 phone.setNumber(number); |
|
237 // Stripped automatically on saving RFC 3966 visual-separators reg exp "[(|-|.|)| ]" |
|
238 formattedPhoneValues.insert(QString(number).replace( QRegExp("[\\(|" \ |
|
239 "\\-|" \ |
|
240 "\\.|" \ |
|
241 "\\)|" \ |
|
242 " ]"), ""),phoneValues.value(number)); |
|
243 if (!phoneValues.value(number).first.isEmpty()) { |
|
244 phone.setContexts(phoneValues.value(number).first); |
|
245 } |
|
246 if (!phoneValues.value(number).second.isEmpty()) { |
|
247 phone.setSubTypes(phoneValues.value(number).second); |
|
248 } |
|
249 c.saveDetail(&phone); |
|
250 detailsAdded++; |
|
251 } |
|
252 |
|
253 trackerEngine->saveContact(&c, error); |
|
254 QCOMPARE(error, QContactManager::NoError); |
|
255 QVERIFY(c.localId() != initialId); |
|
256 // wait for commit transaction to be done, no signals yet |
|
257 for(int i = 0; i < 100; i++) |
|
258 { |
|
259 usleep(10000); |
|
260 QCoreApplication::processEvents(); |
|
261 } |
|
262 |
|
263 |
|
264 // verify with synchronous read too |
|
265 QContact contact = trackerEngine->contact_impl(c.localId(), QStringList(), error); |
|
266 QCOMPARE(error, QContactManager::NoError); |
|
267 QList<QContactPhoneNumber> details = contact.details<QContactPhoneNumber>(); |
|
268 |
|
269 |
|
270 QCOMPARE(details.count(), detailsAdded); |
|
271 |
|
272 |
|
273 foreach (QContactPhoneNumber detail, details) { |
|
274 // Verify that the stored values and attributes are the same as given |
|
275 QVERIFY(formattedPhoneValues.contains(detail.number())); |
|
276 QCOMPARE(detail.contexts()[0], formattedPhoneValues.value(detail.number()).first); |
|
277 if( formattedPhoneValues.value(detail.number()).second.isEmpty()) // default empty is voice |
|
278 QCOMPARE(detail.subTypes()[0], QLatin1String(QContactPhoneNumber::SubTypeVoice)); |
|
279 else |
|
280 QCOMPARE(detail.subTypes()[0], formattedPhoneValues.value(detail.number()).second); |
|
281 } |
|
282 |
|
283 // edit one of numbers . values, context and subtypes and save again |
|
284 QString editedPhoneValue = "+7044866473"; |
|
285 QContactPhoneNumber phone = details[0]; |
|
286 phone.setNumber(editedPhoneValue); |
|
287 phone.setContexts(QContactDetail::ContextWork); |
|
288 phone.setSubTypes(QContactPhoneNumber::SubTypeMobile); |
|
289 c = contact; |
|
290 c.saveDetail(&phone); |
|
291 trackerEngine->saveContact(&c, error); |
|
292 QCOMPARE(error, QContactManager::NoError); |
|
293 c = this->contact(c.localId(), QStringList()<<QContactPhoneNumber::DefinitionName); |
|
294 details = c.details<QContactPhoneNumber>(); |
|
295 QCOMPARE(details.count(), detailsAdded); |
|
296 bool found = false; |
|
297 foreach (QContactPhoneNumber detail, details) { |
|
298 if(detail.number() == phone.number()) |
|
299 { |
|
300 found = true; |
|
301 QVERIFY(detail.subTypes().contains(QContactPhoneNumber::SubTypeMobile)); |
|
302 QVERIFY(detail.contexts().contains(QContactPhoneNumber::ContextWork)); |
|
303 break; |
|
304 } |
|
305 } |
|
306 QVERIFY(found); |
|
307 } |
|
308 } |
|
309 |
|
310 void ut_qtcontacts_trackerplugin::testPhoneNumberContext() |
|
311 { |
|
312 QContact c; |
|
313 QContactPhoneNumber phone; |
|
314 phone.setContexts(QContactDetail::ContextHome); |
|
315 phone.setNumber("555-888"); |
|
316 phone.setSubTypes(QContactPhoneNumber::SubTypeMobile); |
|
317 c.saveDetail(&phone); |
|
318 QContact contactToSave = c; |
|
319 // Let's do this all twice, first time save new detail, and next iteration change the context |
|
320 for (int iterations = 0; iterations < 2; iterations++) { |
|
321 QVERIFY(trackerEngine->saveContact(&contactToSave, error)); |
|
322 // wait for commit transaction to be done, no signals yet |
|
323 for(int i = 0; i < 100; i++) { |
|
324 usleep(10000); |
|
325 QCoreApplication::processEvents(); |
|
326 } |
|
327 |
|
328 QContactFetchRequest request; |
|
329 QContactLocalIdFilter filter; |
|
330 QList<QContactLocalId> ids; |
|
331 ids.append(contactToSave.localId()); |
|
332 filter.setIds(ids); |
|
333 request.setFilter(filter); |
|
334 |
|
335 QStringList details; |
|
336 details << QContactPhoneNumber::DefinitionName; |
|
337 request.setDefinitionRestrictions(details); |
|
338 |
|
339 Slots slot; |
|
340 QObject::connect(&request, SIGNAL(resultsAvailable()), |
|
341 &slot, SLOT(resultsAvailable())); |
|
342 |
|
343 trackerEngine->startRequest(&request); |
|
344 |
|
345 for(int i = 0; i < 100; i++) { |
|
346 usleep(100000); |
|
347 QCoreApplication::processEvents(); |
|
348 if(request.isFinished() ) |
|
349 break; |
|
350 } |
|
351 |
|
352 // if it takes more, then something is wrong |
|
353 QVERIFY(request.isFinished()); |
|
354 QVERIFY(!slot.contacts.isEmpty()); |
|
355 |
|
356 QContact contactToTest; |
|
357 foreach (QContact savedContact, slot.contacts) { |
|
358 if (savedContact.localId() == contactToSave.localId()) { |
|
359 contactToTest = savedContact; |
|
360 } |
|
361 } |
|
362 QVERIFY(contactToTest.localId() == contactToSave.localId()); // Just to be sure we got the saved contact |
|
363 qDebug()<<contactToTest.details<QContactPhoneNumber>().count(); |
|
364 |
|
365 QVERIFY(contactToTest.details<QContactPhoneNumber>().count() == 1); |
|
366 if (0 == iterations) { |
|
367 // perform context change |
|
368 QContactPhoneNumber phoneToEdit = contactToTest.detail<QContactPhoneNumber>(); |
|
369 phoneToEdit.setContexts(QContactDetail::ContextWork); |
|
370 contactToTest.saveDetail(&phoneToEdit); |
|
371 contactToSave = contactToTest; |
|
372 } |
|
373 QVERIFY(contactToTest.details<QContactPhoneNumber>().count() == 1); |
|
374 } |
|
375 } |
|
376 |
|
377 void ut_qtcontacts_trackerplugin::testWritingOnlyWorkMobile() |
|
378 { |
|
379 QContact c; |
|
380 QContactPhoneNumber phone; |
|
381 phone.setContexts(QContactDetail::ContextWork); |
|
382 phone.setNumber("555999"); |
|
383 phone.setSubTypes(QContactPhoneNumber::SubTypeMobile); |
|
384 c.saveDetail(&phone); |
|
385 QContact& contactToSave = c; |
|
386 QVERIFY(trackerEngine->saveContact(&contactToSave, error)); |
|
387 // wait for commit transaction to be done, no signals yet |
|
388 for(int i = 0; i < 100; i++) { |
|
389 usleep(10000); |
|
390 QCoreApplication::processEvents(); |
|
391 } |
|
392 |
|
393 QContactFetchRequest request; |
|
394 QContactLocalIdFilter filter; |
|
395 QList<QContactLocalId> ids; |
|
396 ids.append(contactToSave.localId()); |
|
397 filter.setIds(ids); |
|
398 request.setFilter(filter); |
|
399 QStringList details; |
|
400 details << QContactPhoneNumber::DefinitionName; |
|
401 request.setDefinitionRestrictions(details); |
|
402 |
|
403 Slots slot; |
|
404 QObject::connect(&request, SIGNAL(resultsAvailable()), |
|
405 &slot, SLOT(resultsAvailable())); |
|
406 |
|
407 trackerEngine->startRequest(&request); |
|
408 |
|
409 for(int i = 0; i < 100; i++) { |
|
410 usleep(100000); |
|
411 QCoreApplication::processEvents(); |
|
412 if(request.isFinished() ) |
|
413 break; |
|
414 } |
|
415 |
|
416 // if it takes more, then something is wrong |
|
417 QVERIFY(request.isFinished()); |
|
418 QVERIFY(!slot.contacts.isEmpty()); |
|
419 |
|
420 QContact contactToTest; |
|
421 foreach (QContact savedContact, slot.contacts) { |
|
422 if (savedContact.localId() == c.localId()) { |
|
423 contactToTest = savedContact; |
|
424 } |
|
425 } |
|
426 QVERIFY(contactToTest.localId() == c.localId()); // Just to be sure we got the saved contact |
|
427 QVERIFY(contactToTest.details<QContactPhoneNumber>().count() == 1); |
|
428 QVERIFY(contactToTest.detail<QContactPhoneNumber>().number() == phone.number()); |
|
429 QVERIFY(contactToTest.detail<QContactPhoneNumber>().subTypes() == phone.subTypes()); |
|
430 QVERIFY(contactToTest.detail<QContactPhoneNumber>().contexts() == phone.contexts()); |
|
431 } |
|
432 |
|
433 void ut_qtcontacts_trackerplugin::testSaveAddress() |
|
434 { |
|
435 QContact c; |
|
436 QContactName name; |
|
437 name.setFirstName("Aruba & Barbados"); |
|
438 name.setLastName("Girl"); |
|
439 c.saveDetail(&name); |
|
440 QContactLocalId initialId = c.localId(); |
|
441 int detailsAdded = 0; |
|
442 |
|
443 // List of pairs of field-value map and context |
|
444 typedef QMap<QString,QString> typeAddress; |
|
445 typedef QPair<typeAddress,QString> typeAddressWithContext; |
|
446 QList<typeAddressWithContext> addressValues; |
|
447 |
|
448 // TODO check status of 137174 and other libqttracker1pre6 bugs before refactoring |
|
449 typeAddress values; |
|
450 values.insert(QLatin1String(QContactAddress::FieldCountry), "Barbados"); |
|
451 values.insert(QLatin1String(QContactAddress::FieldPostcode), "55555"); |
|
452 values.insert(QLatin1String(QContactAddress::FieldStreet), "Martindales Rd"); |
|
453 values.insert(QLatin1String(QContactAddress::FieldRegion), "Bridgetown"); |
|
454 addressValues.append(typeAddressWithContext(values, QLatin1String(QContactDetail::ContextHome))); |
|
455 values.clear(); |
|
456 values.insert(QLatin1String(QContactAddress::FieldCountry), "Aruba"); |
|
457 values.insert(QLatin1String(QContactAddress::FieldPostcode), "44444"); |
|
458 values.insert(QLatin1String(QContactAddress::FieldStreet), "Brazilie Straat"); |
|
459 values.insert(QLatin1String(QContactAddress::FieldRegion), "Oranjestad"); |
|
460 addressValues.append(typeAddressWithContext(values, QLatin1String(QContactDetail::ContextHome))); |
|
461 values.clear(); |
|
462 values.insert(QLatin1String(QContactAddress::FieldCountry), "ArubaWork"); |
|
463 values.insert(QLatin1String(QContactAddress::FieldPostcode), "44445"); |
|
464 values.insert(QLatin1String(QContactAddress::FieldStreet), "Sunset Blvd"); |
|
465 values.insert(QLatin1String(QContactAddress::FieldRegion), "Oranjestad"); |
|
466 addressValues.append(typeAddressWithContext(values, QLatin1String(QContactDetail::ContextHome))); |
|
467 foreach (typeAddressWithContext addressWithContext, addressValues) { |
|
468 QContactAddress address; |
|
469 foreach (QString field, addressWithContext.first.keys()) { |
|
470 address.setValue(field, addressWithContext.first.value(field)); |
|
471 } |
|
472 address.setContexts(addressWithContext.second); |
|
473 c.saveDetail(&address); |
|
474 detailsAdded++; |
|
475 } |
|
476 |
|
477 trackerEngine->saveContact(&c, error); |
|
478 QCOMPARE(error, QContactManager::NoError); |
|
479 QVERIFY(c.localId() != initialId); |
|
480 QContact contact = trackerEngine->contact_impl(c.localId(), QStringList(), error); |
|
481 QList<QContactAddress> details = contact.details<QContactAddress>(); |
|
482 QCOMPARE(details.count(), detailsAdded); |
|
483 bool found = false; |
|
484 // Test if inserted values are found in some of the details |
|
485 foreach (typeAddressWithContext addressWithContext, addressValues) { |
|
486 foreach (QContactAddress detail, details) { |
|
487 foreach (QString field, addressWithContext.first.keys()) { |
|
488 found = (detail.value(field) == addressWithContext.first.value(field)); |
|
489 if (!found) |
|
490 break; |
|
491 } |
|
492 if (found) |
|
493 break; |
|
494 } |
|
495 QVERIFY2(found, "Inserted detail was not found in the fetched details"); |
|
496 } |
|
497 } |
|
498 |
|
499 void ut_qtcontacts_trackerplugin::testSaveEmailAddress() |
|
500 { |
|
501 QContact c; |
|
502 QContactLocalId initialId = c.localId(); |
|
503 int detailsAdded = 0; |
|
504 |
|
505 QMap<QString,QString> values; |
|
506 values.insert("john.does@hotmail.com", QContactDetail::ContextHome); |
|
507 values.insert("john.doe@gmail.com", QContactDetail::ContextWork); |
|
508 values.insert("john.doe@nokia.com", QContactDetail::ContextWork); |
|
509 values.insert("john.doe@johndoe.com", QContactDetail::ContextHome); |
|
510 foreach(QString address, values.keys()) { |
|
511 QContactEmailAddress emailAddress; |
|
512 emailAddress.setEmailAddress(address); |
|
513 emailAddress.setContexts(values.value(address)); |
|
514 c.saveDetail(&emailAddress); |
|
515 detailsAdded++; |
|
516 } |
|
517 QContactName name; |
|
518 name.setFirstName("Jo"); |
|
519 name.setLastName("H N Doe"); |
|
520 c.saveDetail(&name); |
|
521 trackerEngine->saveContact(&c, error); |
|
522 QCOMPARE(error, QContactManager::NoError); |
|
523 QVERIFY(c.localId() != initialId); |
|
524 QContact contact = trackerEngine->contact_impl(c.localId(), QStringList(), error); |
|
525 QList<QContactEmailAddress> details = contact.details<QContactEmailAddress>(); |
|
526 QCOMPARE(details.count(), detailsAdded); |
|
527 foreach (QContactEmailAddress detail, details) { |
|
528 QString address = detail.value(QContactEmailAddress::FieldEmailAddress); |
|
529 QVERIFY(values.contains(address)); |
|
530 QCOMPARE(detail.contexts()[0], values.value(address)); |
|
531 } |
|
532 } |
|
533 |
|
534 void ut_qtcontacts_trackerplugin::testRemoveContact() |
|
535 { |
|
536 QContact c; |
|
537 QContactPhoneNumber phone; |
|
538 phone.setNumber("+358501234567"); |
|
539 c.saveDetail(&phone); |
|
540 QContactEmailAddress email; |
|
541 email.setEmailAddress("super.man@hotmail.com"); |
|
542 c.saveDetail(&email); |
|
543 QContactName name; |
|
544 name.setFirstName("Super"); |
|
545 name.setLastName("Man"); |
|
546 c.saveDetail(&name); |
|
547 |
|
548 QVERIFY2(trackerEngine->saveContact(&c, error) && error == QContactManager::NoError, "Saving a contact failed"); |
|
549 QVERIFY2(trackerEngine->removeContact(c.localId(), error), "Removing a contact failed"); |
|
550 QCOMPARE(error, QContactManager::NoError); |
|
551 QVERIFY2(trackerEngine->contact_impl(c.localId(), QStringList(), error) == QContact(), "Found a contact, which should have been removed"); |
|
552 } |
|
553 |
|
554 void ut_qtcontacts_trackerplugin::testSaveContacts() |
|
555 { |
|
556 QList<QContact> contacts; |
|
557 for (int i = 0; i < 3; i++) { |
|
558 QContact c; |
|
559 QContactName name; |
|
560 name.setFirstName("John"); |
|
561 name.setLastName(QString::number(i,10)); |
|
562 c.saveDetail(&name); |
|
563 contacts.append(c); |
|
564 } |
|
565 |
|
566 QMap<int, QContactManager::Error>* errorMap; |
|
567 trackerEngine->saveContacts(&contacts, errorMap, error); |
|
568 QCOMPARE(error, QContactManager::NoError); |
|
569 for (int i = 0; i < contacts.count(); i++) { |
|
570 QVERIFY(contacts[i].localId() != 0); |
|
571 QList<QContactName> details = trackerEngine->contact_impl(contacts[i].localId(), QStringList(), error).details<QContactName>(); |
|
572 QVERIFY(details.count()); |
|
573 QCOMPARE(details.at(0).lastName(), |
|
574 QString("%1").arg(QString::number(i,10))); |
|
575 } |
|
576 } |
|
577 |
|
578 void ut_qtcontacts_trackerplugin::testRemoveContacts() |
|
579 { |
|
580 QList<QContactLocalId> addedIds; |
|
581 for (int i = 0; i < 5; i++) { |
|
582 QContact c; |
|
583 QContactName name; |
|
584 name.setFirstName(QString("John%1").arg(QString::number(i,10))); |
|
585 c.saveDetail(&name); |
|
586 QVERIFY2(trackerEngine->saveContact(&c, error) && error == QContactManager::NoError, "Saving a contact failed"); |
|
587 addedIds.append(c.localId()); |
|
588 } |
|
589 QList<QContactLocalId> toApiRemove; |
|
590 toApiRemove.append(addedIds.takeLast()); |
|
591 toApiRemove.append(addedIds.takeLast()); |
|
592 QList<QContactLocalId> toPluginRemove(addedIds); |
|
593 // Remove all, but last of the added contacts |
|
594 bool success = trackerEngine->removeContacts(&toPluginRemove, errorMap, error); |
|
595 QCOMPARE(success, true); |
|
596 for (int i = 0; i < errorMap->count(); i++) { |
|
597 QVERIFY(toPluginRemove[i] == 0); |
|
598 } |
|
599 QCOMPARE(error, QContactManager::NoError); |
|
600 |
|
601 success = ContactManager::instance()->removeContacts(&toApiRemove, errorMap); |
|
602 QCOMPARE(success, true); |
|
603 for (int i = 0; i < errorMap->count(); i++) { |
|
604 QVERIFY(toApiRemove[i] == 0); |
|
605 } |
|
606 |
|
607 // Try to remove some previously removed contacts, but one valid contact |
|
608 success = trackerEngine->removeContacts(&addedIds, errorMap, error); |
|
609 QCOMPARE(errorMap->count(), addedIds.count()); |
|
610 for (int i = 0; i < errorMap->count() - 1; i++) { |
|
611 QVERIFY2(addedIds[i] != 0, "Manager should not mark id as zero"); |
|
612 } |
|
613 } |
|
614 |
|
615 void ut_qtcontacts_trackerplugin::testAvatar() |
|
616 { |
|
617 QContact contactWithAvatar; |
|
618 QContactAvatar avatar; |
|
619 |
|
620 avatar.setAvatar("file:///home/user/.contacts/avatars/default_avatar.png"); |
|
621 contactWithAvatar.saveDetail(&avatar); |
|
622 QContactName name; |
|
623 name.setFirstName("John");name.setLastName("A Frog"); |
|
624 contactWithAvatar.saveDetail(&name); |
|
625 QVERIFY(trackerEngine->saveContact( &contactWithAvatar, error)); |
|
626 |
|
627 QContact c = trackerEngine->contact_impl( contactWithAvatar.localId(), QStringList(), error); |
|
628 QList<QContactAvatar> avatars = c.details<QContactAvatar>(); |
|
629 QVERIFY( avatars.size() ); |
|
630 QCOMPARE( avatars[0].avatar(), avatar.avatar() ); |
|
631 } |
|
632 |
|
633 void ut_qtcontacts_trackerplugin::testUrl() |
|
634 { |
|
635 |
|
636 //Context home, homepage url |
|
637 QContact contactWithUrl1; |
|
638 QContactUrl url1; |
|
639 url1.setUrl("http://home.homepage"); |
|
640 url1.setContexts(QContactDetail::ContextHome); |
|
641 url1.setSubType(QContactUrl::SubTypeHomePage); |
|
642 QContactName name; |
|
643 name.setFirstName("John");name.setLastName("TestUrl1"); |
|
644 contactWithUrl1.saveDetail(&name); |
|
645 contactWithUrl1.saveDetail(&url1); |
|
646 QVERIFY(trackerEngine->saveContact(&contactWithUrl1, error)); |
|
647 |
|
648 //Context work, homepage url |
|
649 QContact contactWithUrl2; |
|
650 QContactUrl url2; |
|
651 url2.setUrl("http://work.homepage"); |
|
652 url2.setContexts(QContactDetail::ContextWork); |
|
653 url2.setSubType(QContactUrl::SubTypeHomePage); |
|
654 QContactName name2; |
|
655 name2.setLastName("TestUrl2"); |
|
656 contactWithUrl2.saveDetail(&name2); |
|
657 contactWithUrl2.saveDetail(&url2); |
|
658 QVERIFY(trackerEngine->saveContact(&contactWithUrl2, error)); |
|
659 |
|
660 //Context home, favourite url |
|
661 QContact contactWithUrl3; |
|
662 QContactUrl url3; |
|
663 url3.setUrl("http://home.favourite"); |
|
664 url3.setContexts(QContactDetail::ContextHome); |
|
665 url3.setSubType(QContactUrl::SubTypeFavourite); |
|
666 |
|
667 name2.setLastName("TestUrl3"); |
|
668 contactWithUrl3.saveDetail(&name2); |
|
669 contactWithUrl3.saveDetail(&url3); |
|
670 QVERIFY(trackerEngine->saveContact(&contactWithUrl3, error)); |
|
671 |
|
672 |
|
673 QContactLocalId id1 = contactWithUrl1.localId(); |
|
674 QContactLocalId id2 = contactWithUrl2.localId(); |
|
675 QContactLocalId id3 = contactWithUrl3.localId(); |
|
676 QCOMPARE(contact(id1).detail<QContactUrl>().url(), QString("http://home.homepage")); |
|
677 QCOMPARE(contact(id2).detail<QContactUrl>().url(), QString("http://work.homepage")); |
|
678 QCOMPARE(contact(id3).detail<QContactUrl>().url(), QString("http://home.favourite")); |
|
679 |
|
680 QVERIFY(contact(id1).detail<QContactUrl>().contexts()[0] == QContactDetail::ContextHome ); |
|
681 QVERIFY(contact(id2).detail<QContactUrl>().contexts()[0] == QContactDetail::ContextWork ); |
|
682 QVERIFY(contact(id3).detail<QContactUrl>().contexts()[0] == QContactDetail::ContextHome ); |
|
683 |
|
684 QVERIFY(contact(id1).detail<QContactUrl>().subType() == QContactUrl::SubTypeHomePage ); |
|
685 QVERIFY(contact(id2).detail<QContactUrl>().subType() == QContactUrl::SubTypeHomePage ); |
|
686 QVERIFY(contact(id3).detail<QContactUrl>().subType() == QContactUrl::SubTypeFavourite ); |
|
687 |
|
688 } |
|
689 |
|
690 /* |
|
691 void ut_qtcontacts_trackerplugin::testGroups() |
|
692 { |
|
693 qDebug() << "Not implemented"; |
|
694 QVERIFY(false); |
|
695 } |
|
696 |
|
697 void ut_qtcontacts_trackerplugin::testGroup() |
|
698 { |
|
699 qDebug() << "Not implemented"; |
|
700 QVERIFY(false); |
|
701 } |
|
702 |
|
703 void ut_qtcontacts_trackerplugin::testSaveGroup() |
|
704 { |
|
705 qDebug() << "Not implemented"; |
|
706 QVERIFY(false); |
|
707 } |
|
708 |
|
709 void ut_qtcontacts_trackerplugin::testRemoveGroup() |
|
710 { |
|
711 qDebug() << "Not implemented"; |
|
712 QVERIFY(false); |
|
713 } |
|
714 |
|
715 void ut_qtcontacts_trackerplugin::testDetailDefinitions() |
|
716 { |
|
717 qDebug() << "Not implemented"; |
|
718 QVERIFY(false); |
|
719 } |
|
720 |
|
721 void ut_qtcontacts_trackerplugin::testDetailDefinition() |
|
722 { |
|
723 qDebug() << "Not implemented"; |
|
724 QVERIFY(false); |
|
725 } |
|
726 |
|
727 void ut_qtcontacts_trackerplugin::testSaveDetailDefinition() |
|
728 { |
|
729 qDebug() << "Not implemented"; |
|
730 QVERIFY(false); |
|
731 } |
|
732 |
|
733 void ut_qtcontacts_trackerplugin::testRemoveDetailDefinition() |
|
734 { |
|
735 qDebug() << "Not implemented"; |
|
736 QVERIFY(false); |
|
737 } |
|
738 */ |
|
739 |
|
740 void ut_qtcontacts_trackerplugin::testSyncContactManagerContactsAddedSince() |
|
741 { |
|
742 // FIXME move this code out: not supposed to compile in and load the same code as dll plugin |
|
743 QSKIP("Statically and dinamically linking the same code is not working", SkipAll); |
|
744 |
|
745 QDateTime start; |
|
746 QList<QContactLocalId> addedIds; |
|
747 syncContactsAddedSinceHelper(start, addedIds); |
|
748 |
|
749 QContactChangeLogFilter filter(QContactChangeLogFilter::EventAdded); |
|
750 filter.setSince(start); |
|
751 |
|
752 QList<QContactSortOrder> sortOrder; |
|
753 |
|
754 QList<QContact> contactIds = ContactManager::instance()->contacts( filter, sortOrder, QStringList() ); |
|
755 qDebug() << "addedIds" << addedIds.size(); |
|
756 qDebug() << "contactIds" << contactIds.size(); |
|
757 QVERIFY2( contactIds.size() == addedIds.size(), "Incorrect number of filtered contacts"); |
|
758 } |
|
759 |
|
760 void ut_qtcontacts_trackerplugin::testSyncTrackerEngineContactsIdsAddedSince() |
|
761 { |
|
762 QDateTime start; |
|
763 QList<QContactLocalId> addedIds; |
|
764 syncContactsAddedSinceHelper(start, addedIds); |
|
765 |
|
766 QContactChangeLogFilter filter(QContactChangeLogFilter::EventAdded); |
|
767 filter.setSince(start); |
|
768 |
|
769 QList<QContactSortOrder> sortOrder; |
|
770 QContactManager::Error error; |
|
771 |
|
772 QList<QContactLocalId> contactIds = trackerEngine->contactIds( filter, sortOrder, error ); |
|
773 qDebug() << "addedIds" << addedIds; |
|
774 qDebug() << "contactIds" << contactIds; |
|
775 QVERIFY2( contactIds.size() == addedIds.size(), "Incorrect number of filtered contacts"); |
|
776 } |
|
777 |
|
778 void ut_qtcontacts_trackerplugin::testSyncContactManagerContactIdsAddedSince() |
|
779 { |
|
780 // FIXME move this code out: not supposed to compile in and load the same code as dll plugin |
|
781 QSKIP("Statically and dinamically linking the same code is not working", SkipAll); |
|
782 QDateTime start; |
|
783 QList<QContactLocalId> addedIds; |
|
784 syncContactsAddedSinceHelper(start, addedIds); |
|
785 QContactChangeLogFilter filter(QContactChangeLogFilter::EventAdded); |
|
786 filter.setSince(start); |
|
787 QList<QContactSortOrder> sortOrder; |
|
788 |
|
789 |
|
790 QList<QContactLocalId> contactIds = ContactManager::instance()->contactIds(filter, sortOrder); |
|
791 qDebug() << "addedIds" << addedIds; |
|
792 qDebug() << "contactIds" << contactIds; |
|
793 QVERIFY2( contactIds.size() == addedIds.size(), "Incorrect number of filtered contacts"); |
|
794 } |
|
795 |
|
796 |
|
797 void ut_qtcontacts_trackerplugin::syncContactsAddedSinceHelper(QDateTime& start, QList<QContactLocalId>& addedIds) |
|
798 { |
|
799 for (int i = 0; i < 3; i++) { |
|
800 QContact c; |
|
801 QContactName name; |
|
802 name.setFirstName("A"+QString::number(i)); |
|
803 QVERIFY2(c.saveDetail(&name), "Failed to save detail"); |
|
804 QVERIFY2(trackerEngine->saveContact(&c, error), "Failed to save contact"); |
|
805 } |
|
806 |
|
807 QTest::qWait(1000); |
|
808 start = QDateTime::currentDateTime(); |
|
809 |
|
810 for (int i = 0; i < 3; i++) { |
|
811 QContact c; |
|
812 QContactName name; |
|
813 name.setFirstName("B"+QString::number(i)); |
|
814 QVERIFY2(c.saveDetail(&name), "Failed to save detail"); |
|
815 QVERIFY2(trackerEngine->saveContact(&c, error), "Failed to save contact"); |
|
816 addedIds.append(c.localId()); |
|
817 } |
|
818 } |
|
819 |
|
820 void ut_qtcontacts_trackerplugin::testContactsAddedSince() |
|
821 { |
|
822 QList<QContactLocalId> addedIds; |
|
823 QDateTime start; |
|
824 for (int i = 0; i < 3; i++) { |
|
825 QContact c; |
|
826 QContactName name; |
|
827 name.setFirstName("A"+QString::number(i)); |
|
828 QVERIFY2(c.saveDetail(&name), "Failed to save detail"); |
|
829 QVERIFY2(trackerEngine->saveContact(&c, error), "Failed to save contact"); |
|
830 } |
|
831 |
|
832 QTest::qWait(1000); |
|
833 start = QDateTime::currentDateTime(); |
|
834 |
|
835 for (int i = 0; i < 3; i++) { |
|
836 QContact c; |
|
837 QContactName name; |
|
838 name.setFirstName("B"+QString::number(i)); |
|
839 QVERIFY2(c.saveDetail(&name), "Failed to save detail"); |
|
840 QVERIFY2(trackerEngine->saveContact(&c, error), "Failed to save contact"); |
|
841 addedIds.append(c.localId()); |
|
842 } |
|
843 |
|
844 // now one asynchronous request to read all the |
|
845 QContactFetchRequest request; |
|
846 QContactChangeLogFilter filter(QContactChangeLogFilter::EventAdded); |
|
847 filter.setSince(start); |
|
848 request.setFilter(filter); |
|
849 |
|
850 // here You specify which details are of interest |
|
851 QStringList details; |
|
852 details << QContactAvatar::DefinitionName |
|
853 << QContactBirthday::DefinitionName |
|
854 << QContactAddress::DefinitionName |
|
855 << QContactEmailAddress::DefinitionName |
|
856 << QContactDisplayLabel::DefinitionName |
|
857 << QContactGender::DefinitionName |
|
858 << QContactAnniversary::DefinitionName |
|
859 << QContactName::DefinitionName |
|
860 << QContactOnlineAccount::DefinitionName |
|
861 << QContactOrganization::DefinitionName |
|
862 << QContactPhoneNumber::DefinitionName; |
|
863 request.setDefinitionRestrictions(details); |
|
864 |
|
865 Slots slot; |
|
866 QObject::connect(&request, SIGNAL(resultsAvailable()), |
|
867 &slot, SLOT(resultsAvailable())); |
|
868 |
|
869 // start. clients should, instead of following use |
|
870 // request.setManager(trackermanagerinstance); |
|
871 // request.start(); |
|
872 trackerEngine->startRequest(&request); |
|
873 trackerEngine->waitForRequestFinished(&request, 10000); |
|
874 // if it takes more, then something is wrong |
|
875 QVERIFY(request.isFinished()); |
|
876 QCOMPARE(slot.contacts.count(), addedIds.count()); |
|
877 |
|
878 foreach(QContact cont, slot.contacts) { |
|
879 QVERIFY2(addedIds.contains(cont.localId()), "One of the added contacts was not reported as added"); |
|
880 } |
|
881 |
|
882 QContactLocalIdFetchRequest idreq; |
|
883 filter.setSince(start); |
|
884 idreq.setFilter(filter); |
|
885 |
|
886 Slots slot2; |
|
887 QObject::connect(&idreq, SIGNAL(resultsAvailable()), |
|
888 &slot2, SLOT(idResultsAvailable())); |
|
889 trackerEngine->startRequest(&idreq); |
|
890 trackerEngine->waitForRequestFinished(&idreq, 10000); |
|
891 QVERIFY(idreq.isFinished()); |
|
892 QCOMPARE(slot2.ids.count(), addedIds.count()); |
|
893 foreach(QContactLocalId id, slot2.ids) { |
|
894 QVERIFY2(addedIds.contains(id), "One of the added contacts was not reported as added"); |
|
895 } |
|
896 |
|
897 } |
|
898 |
|
899 void ut_qtcontacts_trackerplugin::testContactsModifiedSince() |
|
900 { |
|
901 QDateTime start; |
|
902 QList<QContactLocalId> addedIds; |
|
903 QList<QContactLocalId> modified; |
|
904 |
|
905 const int contactsToAdd = 5; |
|
906 const int contactsToModify = 3; |
|
907 QVERIFY2(contactsToAdd >= contactsToModify, "Cannot modify more contacts than this test has added"); |
|
908 QVERIFY2(contactsToModify+1 <= contactsToAdd, "Cannot modify more contacts than this test has added"); |
|
909 |
|
910 // Add contacts with only first name and store them to list of added |
|
911 for (int i = 0; i < contactsToAdd; i++) { |
|
912 QContact c; |
|
913 QContactName name; |
|
914 name.setFirstName("A"+QString::number(i)); |
|
915 QVERIFY2(c.saveDetail(&name), "Failed to save detail"); |
|
916 QVERIFY2(trackerEngine->saveContact(&c, error), "Failed to save contact"); |
|
917 addedIds.append(c.localId()); |
|
918 } |
|
919 |
|
920 QTest::qWait(2000); |
|
921 start = QDateTime::currentDateTime(); |
|
922 |
|
923 // Modify and save rest of the contacts |
|
924 for (int i = 0; i < contactsToModify; i++) { |
|
925 QContact c = contact(addedIds[i]); |
|
926 QContactName name = c.detail<QContactName>(); |
|
927 // Modify name |
|
928 name.setFirstName("B"+QString::number(i)); |
|
929 QVERIFY2(c.saveDetail(&name), "Failed to save detail"); |
|
930 QVERIFY2(trackerEngine->saveContact(&c, error), "Failed to save contact"); |
|
931 modified.append(c.localId()); |
|
932 } |
|
933 // Set filter |
|
934 QContactChangeLogFilter filter(QContactChangeLogFilter::EventChanged); |
|
935 filter.setSince(start); |
|
936 |
|
937 QContactLocalIdFetchRequest idfetch; |
|
938 QContactFetchRequest fetch; |
|
939 idfetch.setFilter(filter); |
|
940 fetch.setFilter(filter); |
|
941 trackerEngine->startRequest(&idfetch); |
|
942 trackerEngine->waitForRequestFinished(&idfetch, 10000); |
|
943 QVERIFY2(idfetch.isFinished(), "Id fetch request did not finish on time"); |
|
944 QVERIFY2(idfetch.error() == QContactManager::NoError, "Id fetch request finished with errors"); |
|
945 QList<QContactLocalId> actuallyModifiedIds = idfetch.ids(); |
|
946 trackerEngine->startRequest(&fetch); |
|
947 trackerEngine->waitForRequestFinished(&fetch, 10000); |
|
948 QVERIFY2(fetch.isFinished(), "Fetch request did not finish on time"); |
|
949 QVERIFY2(fetch.error() == QContactManager::NoError, "Fetch request finished with errors"); |
|
950 QList<QContact> actuallyModified = fetch.contacts(); |
|
951 |
|
952 // Num of actually modified should be same as supposedly modified |
|
953 QCOMPARE(actuallyModifiedIds.count(), modified.count()); |
|
954 QCOMPARE(actuallyModified.count(), modified.count()); |
|
955 // All the ids of the modified contacts should be found in the result list |
|
956 foreach (QContactLocalId id, modified) { |
|
957 QVERIFY2(actuallyModifiedIds.contains(id), "One the modified contacts was not reported as modified"); |
|
958 } |
|
959 } |
|
960 |
|
961 void ut_qtcontacts_trackerplugin::testContactsRemovedSince() |
|
962 { |
|
963 QDateTime start = QDateTime::currentDateTime(); |
|
964 QContactChangeLogFilter filter(QContactChangeLogFilter::EventRemoved); |
|
965 filter.setSince(start); |
|
966 QList<QContactSortOrder> sorts; |
|
967 QList<QContactLocalId> actuallyRemoved = trackerEngine->contactIds(filter, sorts, error); |
|
968 QVERIFY(actuallyRemoved.isEmpty()); |
|
969 QVERIFY(error == QContactManager::NotSupportedError); |
|
970 } |
|
971 /* |
|
972 void ut_qtcontacts_trackerplugin::testGroupsAddedSince() |
|
973 { |
|
974 qDebug() << "Not implemented"; |
|
975 QVERIFY(false); |
|
976 } |
|
977 |
|
978 void ut_qtcontacts_trackerplugin::testGroupsModifiedSince() |
|
979 { |
|
980 qDebug() << "Not implemented"; |
|
981 QVERIFY(false); |
|
982 } |
|
983 |
|
984 void ut_qtcontacts_trackerplugin::testGroupsRemovedSince() |
|
985 { |
|
986 qDebug() << "Not implemented"; |
|
987 QVERIFY(false); |
|
988 } |
|
989 */ |
|
990 |
|
991 void ut_qtcontacts_trackerplugin::cleanupTestCase() |
|
992 { |
|
993 delete trackerEngine; |
|
994 delete errorMap; |
|
995 } |
|
996 |
|
997 void ut_qtcontacts_trackerplugin::cleanup() |
|
998 { |
|
999 foreach (QContactLocalId id, addedContacts) { |
|
1000 trackerEngine->removeContact(id, error); |
|
1001 } |
|
1002 addedContacts.clear(); |
|
1003 } |
|
1004 |
|
1005 |
|
1006 void ut_qtcontacts_trackerplugin::testNcoTypes() |
|
1007 { |
|
1008 using namespace SopranoLive; |
|
1009 |
|
1010 QList<QContactLocalId> ids; |
|
1011 RDFVariable RDFContact = RDFVariable::fromType<nco::PersonContact>(); |
|
1012 RDFSelect query; |
|
1013 |
|
1014 query.addColumn("contact_uri", RDFContact); |
|
1015 query.addColumn("contactId", RDFContact.property<nco::contactUID>()); |
|
1016 LiveNodes ncoContacts = ::tracker()->modelQuery(query); |
|
1017 foreach( Live<nco::PersonContact> p, ncoContacts ) { |
|
1018 QVERIFY(p.hasType<nco::Contact>()); |
|
1019 QVERIFY(p.hasType<nco::Role>()); |
|
1020 QVERIFY(p.hasType<nco::PersonContact>()); |
|
1021 } |
|
1022 } |
|
1023 |
|
1024 void ut_qtcontacts_trackerplugin::testAsyncReadContacts() |
|
1025 { |
|
1026 addedContacts.clear(); |
|
1027 // Add at least one contact to be sure that this doesn't fail because tracker is clean |
|
1028 |
|
1029 QStringList firstNames, lastNames; |
|
1030 firstNames << "aa" << "ab" << "ac" << "dd" << "fe"; |
|
1031 lastNames << "fe" << "ab" << "dd" << "dd" << "aa"; |
|
1032 for (int i = 0; i < firstNames.count(); i++) { |
|
1033 QContact c; |
|
1034 QContactName name; |
|
1035 name.setFirstName(firstNames.at(i)); |
|
1036 name.setLastName(lastNames.at(i)); |
|
1037 QContactAvatar avatar; |
|
1038 avatar.setAvatar("default_avatar.png"); |
|
1039 avatar.setSubType(QContactAvatar::SubTypeImage); |
|
1040 QVERIFY(c.saveDetail(&name)); |
|
1041 QVERIFY(c.saveDetail(&avatar)); |
|
1042 QVERIFY(trackerEngine->saveContact(&c, error)); |
|
1043 addedContacts.append(c.localId()); |
|
1044 } |
|
1045 |
|
1046 // Prepare the filter for the request - we really should test only the contact we add here. |
|
1047 QContactLocalIdFilter filter; |
|
1048 filter.setIds(addedContacts); |
|
1049 |
|
1050 // this one will get complete contacts |
|
1051 |
|
1052 Slots slot; |
|
1053 QContactFetchRequest request; |
|
1054 QList<QContactSortOrder> sorting; |
|
1055 QContactSortOrder sort, sort1; |
|
1056 sort.setDetailDefinitionName(QContactName::DefinitionName, QContactName::FieldLast); |
|
1057 sort1.setDetailDefinitionName(QContactName::DefinitionName, QContactName::FieldFirst); |
|
1058 sorting << sort << sort1; |
|
1059 QStringList details; details << QContactName::DefinitionName << QContactAvatar::DefinitionName; |
|
1060 request.setDefinitionRestrictions(details); |
|
1061 request.setSorting(sorting); |
|
1062 request.setFilter(filter); |
|
1063 |
|
1064 QObject::connect(&request, SIGNAL(resultsAvailable()), |
|
1065 &slot, SLOT(resultsAvailable())); |
|
1066 |
|
1067 // this one only ids |
|
1068 QContactLocalIdFetchRequest request1; |
|
1069 request1.setFilter(filter); |
|
1070 QObject::connect(&request1, SIGNAL(resultsAvailable()), |
|
1071 &slot, SLOT(idResultsAvailable())); |
|
1072 |
|
1073 // the purpose is to compare if all contacts are loaded, and |
|
1074 // if optional fields are defined properly in request |
|
1075 |
|
1076 // start both at once |
|
1077 trackerEngine->startRequest(&request); |
|
1078 trackerEngine->startRequest(&request1); |
|
1079 trackerEngine->waitForRequestFinished(&request, 10000); |
|
1080 trackerEngine->waitForRequestFinished(&request1, 10000); |
|
1081 |
|
1082 |
|
1083 // if it takes more, then something is wrong |
|
1084 QVERIFY(request.isFinished()); |
|
1085 QVERIFY(request1.isFinished()); |
|
1086 |
|
1087 // there need1 to be something added to be verified |
|
1088 QVERIFY(!request.contacts().isEmpty()); |
|
1089 // now ask for one contact |
|
1090 QVERIFY(!slot.contacts.isEmpty()); |
|
1091 // there need to be something added to be verified |
|
1092 QVERIFY(!request1.ids().isEmpty()); |
|
1093 // now ask for one contact |
|
1094 QVERIFY(!slot.ids.isEmpty()); |
|
1095 |
|
1096 QVERIFY2(slot.contacts.count() == slot.ids.count(), "not all contacts were loaded"); |
|
1097 QVERIFY(slot.contacts.count() >= firstNames.count()); |
|
1098 for( int i = 0; i < slot.contacts.size() -1 ; i++) |
|
1099 { |
|
1100 QContact contact = slot.contacts[i]; |
|
1101 QContact contact1 = slot.contacts[i+1]; |
|
1102 QString last0 = contact.detail<QContactName>().lastName(); |
|
1103 QString first0 = contact.detail<QContactName>().firstName(); |
|
1104 QString last1 = contact1.detail<QContactName>().lastName(); |
|
1105 QString first1 = contact1.detail<QContactName>().firstName(); |
|
1106 // sorting |
|
1107 qDebug() << "contacts:" << contact.localId() << first0 << last0; |
|
1108 bool test = last0 < last1 || (last0 == last1 && first0 <= first1); |
|
1109 if (!test) { |
|
1110 qDebug() << "contacts sort failed. First: " << contact1.localId() << first0 << last1 << "lasts: " << last0 << last1; |
|
1111 } |
|
1112 QVERIFY2(test, "Sorting failed."); |
|
1113 } |
|
1114 |
|
1115 } |
|
1116 |
|
1117 void ut_qtcontacts_trackerplugin::testFilterContacts() |
|
1118 { |
|
1119 // this one will get complete contacts |
|
1120 QContact c; |
|
1121 QContactName name; |
|
1122 name.setFirstName("Zuba"); |
|
1123 name.setLastName("Zub"); |
|
1124 c.saveDetail(&name); |
|
1125 QContactPhoneNumber phone; |
|
1126 |
|
1127 phone.setNumber("4872444"); |
|
1128 c.saveDetail(&phone); |
|
1129 |
|
1130 QContactBirthday birthday; |
|
1131 birthday.setDate(QDate(2010, 2, 14)); |
|
1132 c.saveDetail(&birthday); |
|
1133 |
|
1134 trackerEngine->saveContact(&c, error); |
|
1135 |
|
1136 QStringList details; |
|
1137 details << QContactName::DefinitionName << QContactAvatar::DefinitionName |
|
1138 << QContactPhoneNumber::DefinitionName; |
|
1139 |
|
1140 QContactFetchRequest request; |
|
1141 QContactDetailFilter filter; |
|
1142 filter.setDetailDefinitionName(QContactPhoneNumber::DefinitionName, QContactPhoneNumber::FieldNumber); |
|
1143 |
|
1144 Slots slot; |
|
1145 QObject::connect(&request, SIGNAL(resultsAvailable()), |
|
1146 &slot, SLOT(resultsAvailable())); |
|
1147 filter.setValue(QString("4872444")); |
|
1148 filter.setMatchFlags(QContactFilter::MatchEndsWith); |
|
1149 |
|
1150 request.setDefinitionRestrictions(details); |
|
1151 request.setFilter(filter); |
|
1152 |
|
1153 trackerEngine->startRequest(&request); |
|
1154 |
|
1155 for(int i = 0; i < 100; i++) |
|
1156 { |
|
1157 usleep(100000); |
|
1158 QCoreApplication::processEvents(); |
|
1159 if(request.isFinished() ) |
|
1160 break; |
|
1161 } |
|
1162 |
|
1163 // if it takes more, then something is wrong |
|
1164 QVERIFY(request.isFinished()); |
|
1165 QVERIFY(!request.contacts().isEmpty()); |
|
1166 |
|
1167 QVERIFY(!slot.contacts.isEmpty()); |
|
1168 |
|
1169 bool containsThisId = false; |
|
1170 foreach(const QContact &contact, slot.contacts) |
|
1171 { |
|
1172 if( contact.localId() == c.localId()) |
|
1173 containsThisId = true; |
|
1174 bool containsPhone = false; |
|
1175 foreach(const QContactDetail &detail, contact.details(QContactPhoneNumber::DefinitionName)) |
|
1176 { |
|
1177 if(detail.value(QContactPhoneNumber::FieldNumber).contains("4872444")) |
|
1178 { |
|
1179 containsPhone = true; |
|
1180 break; |
|
1181 } |
|
1182 } |
|
1183 QVERIFY(containsPhone); |
|
1184 } |
|
1185 QVERIFY(containsThisId); |
|
1186 |
|
1187 // filter by birthday range |
|
1188 QContactDetailRangeFilter rangeFilter; |
|
1189 rangeFilter.setDetailDefinitionName(QContactBirthday::DefinitionName, QContactBirthday::FieldBirthday); |
|
1190 // include lower & exclude upper by default |
|
1191 rangeFilter.setRange(QDate(2010, 2, 14), QDate(2010, 2, 15)); |
|
1192 QList<QContact> contacts = trackerEngine->contacts(rangeFilter, QList<QContactSortOrder>(), QStringList()<< QContactBirthday::DefinitionName, error); |
|
1193 QVERIFY(!contacts.isEmpty()); |
|
1194 bool containsOurContact(false); |
|
1195 foreach(const QContact &cont, contacts) |
|
1196 { |
|
1197 QVERIFY(cont.detail<QContactBirthday>().date() == QDate(2010, 2, 14)); |
|
1198 if( c.id() == cont.id() ) |
|
1199 containsOurContact = true; |
|
1200 } |
|
1201 QVERIFY(containsOurContact); |
|
1202 } |
|
1203 |
|
1204 void ut_qtcontacts_trackerplugin::testFilterContactsEndsWith() |
|
1205 { |
|
1206 QSettings settings(QSettings::IniFormat, QSettings::UserScope, "Nokia", "Trackerplugin"); |
|
1207 QString restoreValue = settings.value("phoneNumberMatchDigitCount", "7").toString(); |
|
1208 |
|
1209 QContact matchingContact; |
|
1210 QContactName name; |
|
1211 name.setFirstName("Zuba"); |
|
1212 name.setLastName("Zub"); |
|
1213 matchingContact.saveDetail(&name); |
|
1214 QContactPhoneNumber phone; |
|
1215 // TODO doesnt work yet phone.setContexts(QContactPhoneNumber::ContextWork); |
|
1216 phone.setNumber("3210987654321"); |
|
1217 matchingContact.saveDetail(&phone); |
|
1218 trackerEngine->saveContact(&matchingContact, error); |
|
1219 |
|
1220 QStringList details; |
|
1221 details << QContactName::DefinitionName << QContactAvatar::DefinitionName |
|
1222 << QContactPhoneNumber::DefinitionName; |
|
1223 |
|
1224 QContactFetchRequest request; |
|
1225 QContactDetailFilter filter; |
|
1226 filter.setDetailDefinitionName(QContactPhoneNumber::DefinitionName, QContactPhoneNumber::FieldNumber); |
|
1227 |
|
1228 Slots slot; |
|
1229 QObject::connect(&request, SIGNAL(resultsAvailable()), |
|
1230 &slot, SLOT(resultsAvailable())); |
|
1231 |
|
1232 { |
|
1233 // test matching of 7 last digits |
|
1234 int matchCount = 7; |
|
1235 qDebug() << "Test matching of" << matchCount << "last digits."; |
|
1236 settings.setValue("phoneNumberMatchDigitCount", matchCount); |
|
1237 QString matchValue = "3000007654321"; |
|
1238 QContact nonMatchingContact; |
|
1239 nonMatchingContact.saveDetail(&name); |
|
1240 phone.setNumber("3210980654321"); |
|
1241 nonMatchingContact.saveDetail(&phone); |
|
1242 trackerEngine->saveContact(&nonMatchingContact, error); |
|
1243 |
|
1244 filter.setValue(matchValue); |
|
1245 filter.setMatchFlags(QContactFilter::MatchEndsWith); |
|
1246 |
|
1247 request.setDefinitionRestrictions(details); |
|
1248 request.setFilter(filter); |
|
1249 |
|
1250 trackerEngine->startRequest(&request); |
|
1251 |
|
1252 for(int i = 0; i < 100; i++) { |
|
1253 usleep(100000); |
|
1254 QCoreApplication::processEvents(); |
|
1255 if (request.isFinished()) |
|
1256 break; |
|
1257 } |
|
1258 QVERIFY(request.isFinished()); |
|
1259 QVERIFY(!slot.contacts.isEmpty()); |
|
1260 |
|
1261 bool containsMatchingId = false; |
|
1262 bool containsNonMatchingId = false; |
|
1263 foreach(const QContact &contact, slot.contacts) { |
|
1264 if (contact.localId() == nonMatchingContact.localId()) |
|
1265 containsNonMatchingId = true; |
|
1266 if (contact.localId() == matchingContact.localId()) |
|
1267 containsMatchingId = true; |
|
1268 bool containsPhone = false; |
|
1269 foreach(const QContactDetail &detail, contact.details(QContactPhoneNumber::DefinitionName)) { |
|
1270 if (detail.value(QContactPhoneNumber::FieldNumber).endsWith(matchValue.right(matchCount))) { |
|
1271 containsPhone = true; |
|
1272 break; |
|
1273 } |
|
1274 } |
|
1275 QVERIFY(containsPhone); |
|
1276 } |
|
1277 QVERIFY(containsMatchingId); |
|
1278 QVERIFY(!containsNonMatchingId); |
|
1279 } |
|
1280 |
|
1281 { |
|
1282 // test matching of 11 last digits |
|
1283 int matchCount = 11; |
|
1284 qDebug() << "Test matching of" << matchCount << "last digits."; |
|
1285 settings.setValue("phoneNumberMatchDigitCount", matchCount); |
|
1286 QString matchValue = "3010987654321"; |
|
1287 QContact nonMatchingContact; |
|
1288 nonMatchingContact.saveDetail(&name); |
|
1289 phone.setNumber("3200987654321"); |
|
1290 nonMatchingContact.saveDetail(&phone); |
|
1291 trackerEngine->saveContact(&nonMatchingContact, error); |
|
1292 |
|
1293 QContact matchingContactWithShorterNumber; |
|
1294 QContactName name1; |
|
1295 name1.setFirstName("ShortNumber"); |
|
1296 name1.setLastName("Zub1"); |
|
1297 matchingContactWithShorterNumber.saveDetail(&name1); |
|
1298 QContactPhoneNumber phone1; |
|
1299 phone1.setNumber("54321"); |
|
1300 matchingContactWithShorterNumber.saveDetail(&phone1); |
|
1301 trackerEngine->saveContact(&matchingContactWithShorterNumber, error); |
|
1302 QVERIFY(QContactManager::NoError == error); |
|
1303 |
|
1304 |
|
1305 filter.setValue(matchValue); |
|
1306 filter.setMatchFlags(QContactFilter::MatchEndsWith); |
|
1307 |
|
1308 request.setDefinitionRestrictions(details); |
|
1309 request.setFilter(filter); |
|
1310 |
|
1311 trackerEngine->startRequest(&request); |
|
1312 |
|
1313 for(int i = 0; i < 100; i++) { |
|
1314 usleep(100000); |
|
1315 QCoreApplication::processEvents(); |
|
1316 if (request.isFinished()) |
|
1317 break; |
|
1318 } |
|
1319 QVERIFY(request.isFinished()); |
|
1320 QVERIFY(!slot.contacts.isEmpty()); |
|
1321 |
|
1322 bool containsMatchingId = false; |
|
1323 bool containsNonMatchingId = false; |
|
1324 foreach(const QContact &contact, slot.contacts) { |
|
1325 if (contact.localId() == nonMatchingContact.localId()) |
|
1326 containsNonMatchingId = true; |
|
1327 if (contact.localId() == matchingContact.localId()) |
|
1328 containsMatchingId = true; |
|
1329 bool containsPhone = false; |
|
1330 foreach(const QContactDetail &detail, contact.details(QContactPhoneNumber::DefinitionName)) { |
|
1331 if (detail.value(QContactPhoneNumber::FieldNumber).endsWith(matchValue.right(matchCount))) { |
|
1332 containsPhone = true; |
|
1333 break; |
|
1334 } |
|
1335 } |
|
1336 QVERIFY(containsPhone); |
|
1337 } |
|
1338 QVERIFY(containsMatchingId); |
|
1339 QVERIFY(!containsNonMatchingId); |
|
1340 |
|
1341 // now verify with short number |
|
1342 filter.setValue("54321"); |
|
1343 filter.setMatchFlags(QContactFilter::MatchEndsWith); |
|
1344 |
|
1345 request.setDefinitionRestrictions(details); |
|
1346 request.setFilter(filter); |
|
1347 |
|
1348 trackerEngine->startRequest(&request); |
|
1349 |
|
1350 for(int i = 0; i < 100; i++) { |
|
1351 usleep(100000); |
|
1352 QCoreApplication::processEvents(); |
|
1353 if (request.isFinished()) |
|
1354 break; |
|
1355 } |
|
1356 QVERIFY(request.isFinished()); |
|
1357 QVERIFY(!slot.contacts.isEmpty()); |
|
1358 bool containsShort = false; |
|
1359 foreach(const QContact &contact, slot.contacts) { |
|
1360 if (contact.localId() == matchingContactWithShorterNumber.localId()) |
|
1361 containsShort = true; |
|
1362 } |
|
1363 QVERIFY(containsShort); |
|
1364 } |
|
1365 settings.setValue("phoneNumberMatchDigitCount", restoreValue); |
|
1366 } |
|
1367 |
|
1368 void ut_qtcontacts_trackerplugin::testFilterTwoNameFields() |
|
1369 { |
|
1370 // init test |
|
1371 QMap<QContactLocalId, QContactName> names; |
|
1372 for (int i = 0; i < 3; i++) { |
|
1373 QContact c; |
|
1374 QContactName name; |
|
1375 name.setFirstName(QUuid::createUuid().toString() + QString::number(i)); |
|
1376 name.setLastName(QUuid::createUuid().toString() + QString::number(i)); |
|
1377 c.saveDetail(&name); |
|
1378 QContactAvatar avatar; |
|
1379 avatar.setAvatar(QUuid::createUuid().toString()); |
|
1380 c.saveDetail(&avatar); |
|
1381 QVERIFY(trackerEngine->saveContact(&c, error)); |
|
1382 names.insert(c.localId(), name); |
|
1383 QCOMPARE(error, QContactManager::NoError); |
|
1384 addedContacts.append(c.localId()); |
|
1385 } |
|
1386 |
|
1387 // Init filter |
|
1388 QContactLocalId searchId = names.keys().at(1); |
|
1389 QString searchFirst = names.value(searchId).firstName(); |
|
1390 QString searchLast = names.value(searchId).lastName(); |
|
1391 QContactUnionFilter ufilter; |
|
1392 QContactDetailFilter filterFirst; |
|
1393 filterFirst.setDetailDefinitionName(QContactName::DefinitionName, QContactName::FieldFirst); |
|
1394 filterFirst.setMatchFlags(QContactFilter::MatchExactly); |
|
1395 filterFirst.setValue(searchFirst); |
|
1396 QContactDetailFilter filterLast; |
|
1397 filterLast.setDetailDefinitionName(QContactName::DefinitionName, QContactName::FieldLast); |
|
1398 filterLast.setMatchFlags(QContactFilter::MatchExactly); |
|
1399 filterLast.setValue(searchLast); |
|
1400 ufilter.setFilters(QList<QContactFilter>() << filterFirst << filterLast); |
|
1401 |
|
1402 // Init request |
|
1403 QContactFetchRequest request; |
|
1404 request.setFilter(ufilter); |
|
1405 trackerEngine->startRequest(&request); |
|
1406 trackerEngine->waitForRequestFinished(&request, 10000); |
|
1407 |
|
1408 |
|
1409 // Test fetch result |
|
1410 QCOMPARE(request.contacts().count(), 1); |
|
1411 QCOMPARE(request.contacts().at(0).localId(), searchId); |
|
1412 QCOMPARE(request.contacts().at(0).detail<QContactName>().firstName(), searchFirst); |
|
1413 QCOMPARE(request.contacts().at(0).detail<QContactName>().lastName(), searchLast); |
|
1414 } |
|
1415 |
|
1416 void ut_qtcontacts_trackerplugin::testTrackerUriToUniqueId() |
|
1417 { |
|
1418 QString uri = "contact:1234567"; |
|
1419 QContactLocalId id = url2UniqueId( uri ); |
|
1420 QCOMPARE( (int)id, 1234567 ); |
|
1421 } |
|
1422 |
|
1423 void ut_qtcontacts_trackerplugin::testQRelationshipAndMergingContacts() |
|
1424 { |
|
1425 QContact firstContact; |
|
1426 QContactName name; |
|
1427 name.setFirstName("FirstMeta"); |
|
1428 firstContact.saveDetail(&name); |
|
1429 QVERIFY(trackerEngine->saveContact(&firstContact, error)); |
|
1430 |
|
1431 QList<QContactLocalId> secondIds; |
|
1432 QStringList names(QStringList()<<"SecondMeta"<<"ThirdMeta"); |
|
1433 foreach (QString firstname, names) |
|
1434 { |
|
1435 QContact secondContact; |
|
1436 QContactName name1; |
|
1437 name1.setFirstName(firstname); |
|
1438 secondContact.saveDetail(&name1); |
|
1439 QVERIFY(trackerEngine->saveContact(&secondContact, error)); |
|
1440 secondIds<<secondContact.id().localId(); |
|
1441 QContactRelationship rel; |
|
1442 rel.setRelationshipType(QContactRelationship::Is); |
|
1443 rel.setFirst(firstContact.id()); |
|
1444 rel.setSecond(secondContact.id()); |
|
1445 QContactRelationshipSaveRequest req; |
|
1446 req.setRelationships(QList<QContactRelationship>()<<rel); |
|
1447 QVERIFY(trackerEngine->startRequest(&req)); |
|
1448 trackerEngine->waitForRequestFinished(&req, 10000); |
|
1449 // if it takes more, then something is wrong |
|
1450 QVERIFY(req.isFinished()); |
|
1451 QVERIFY(QContactManager::NoError == req.error()); |
|
1452 } |
|
1453 |
|
1454 // once they are merged - that's it - no contacts or relationship track exists |
|
1455 foreach( QContactLocalId mergedId, secondIds) |
|
1456 { |
|
1457 QContact second = contact(mergedId, QStringList()<<QContactName::DefinitionName); |
|
1458 QVERIFY(second.localId() == 0); // as not existing |
|
1459 } |
|
1460 QVERIFY(contact(firstContact.localId(), QStringList()<<QContactName::DefinitionName).localId() != 0); |
|
1461 // TODO check that values from secondids are merged to firstcontact |
|
1462 |
|
1463 } |
|
1464 |
|
1465 void ut_qtcontacts_trackerplugin::insertContact(const QString& URI, QContactLocalId uid, QString imId, QString imStatus, QString accountPath, QString protocol ) |
|
1466 { |
|
1467 QProcess inserter; |
|
1468 QStringList args; |
|
1469 args << URI << QString::number(uid) << imId << accountPath << imStatus << "In Helsinki" << protocol << "Some" << "Guy"; |
|
1470 inserter.start( PATH_TO_SPARQL_TESTS+"/insertTpContact.sparql", args ); |
|
1471 inserter.waitForFinished(); |
|
1472 } |
|
1473 |
|
1474 void ut_qtcontacts_trackerplugin::updateIMContactStatus(const QString& uri, QString imStatus) |
|
1475 { |
|
1476 QProcess inserter; |
|
1477 QStringList args; |
|
1478 args << uri << imStatus; |
|
1479 inserter.start( PATH_TO_SPARQL_TESTS+"/updateTpStatus.sparql", args ); |
|
1480 inserter.waitForFinished(); |
|
1481 } |
|
1482 |
|
1483 void ut_qtcontacts_trackerplugin::testMergeTwoOnlineContacts() |
|
1484 { |
|
1485 QList<QContact> contacts; |
|
1486 for( int i = 0; i < 3; i++ ) |
|
1487 { |
|
1488 unsigned int contactid = 555+i; |
|
1489 insertContact(QString("contact:") + QString::number(contactid), |
|
1490 contactid, QString::number(contactid)+ "@ovi.com", "nco:presence-status-available", QString("/org/freedesktop/fake/account/%1").arg(contactid),"ovi.com"); |
|
1491 QContact c = contact(contactid, QStringList()<<QContactOnlineAccount::DefinitionName); |
|
1492 contacts << c; |
|
1493 } |
|
1494 QContactRelationship rel; |
|
1495 rel.setFirst(contacts.at(0).id()); |
|
1496 rel.setSecond(contacts.at(1).id()); |
|
1497 QContactRelationship rel2; |
|
1498 rel2.setFirst(contacts.at(0).id()); |
|
1499 rel2.setSecond(contacts.at(2).id()); |
|
1500 |
|
1501 QContactRelationshipSaveRequest req; |
|
1502 //TODO adding rel2 to the following causes segfault |
|
1503 req.setRelationships(QList<QContactRelationship>()<<rel); |
|
1504 QVERIFY(trackerEngine->startRequest(&req)); |
|
1505 trackerEngine->waitForRequestFinished(&req, 1000); |
|
1506 QVERIFY(req.isFinished()); |
|
1507 QVERIFY(QContactManager::NoError == req.error()); |
|
1508 |
|
1509 QList<QContactOnlineAccount> onlineAccounts = contacts.at(0).details<QContactOnlineAccount>(); |
|
1510 qDebug() << onlineAccounts.size(); |
|
1511 QEXPECT_FAIL("", "Do net yet support merging multiple im contacts", Continue); |
|
1512 QVERIFY(onlineAccounts.size() == 2); |
|
1513 } |
|
1514 |
|
1515 void ut_qtcontacts_trackerplugin::testIMContactsAndMetacontactMasterPresence() |
|
1516 { |
|
1517 if( !QFileInfo(PATH_TO_SPARQL_TESTS).exists() ) |
|
1518 { |
|
1519 qWarning()<<Q_FUNC_INFO<<"is disabled - test scripts are not installed"; |
|
1520 return; |
|
1521 } |
|
1522 QList<unsigned int> idstomerge; |
|
1523 QContactLocalId masterContactId; // using one master contact later for additional testing |
|
1524 for( int i = 0; i < 2; i++ ) |
|
1525 { |
|
1526 unsigned int contactid = 999998+i; |
|
1527 idstomerge << contactid; |
|
1528 insertContact(QString("contact:") + QString::number(999998+i), |
|
1529 contactid, QString::number(999998 + i)+ "@ovi.com", "nco:presence-status-available", QString("/org/freedesktop/fake/account/%1").arg(999998+i),"ovi.com"); |
|
1530 QContact c = contact(contactid, QStringList()<<QContactOnlineAccount::DefinitionName); |
|
1531 QVERIFY(c.localId() == contactid); |
|
1532 QVERIFY(c.detail<QContactOnlineAccount>().serviceProvider() == "ovi.com"); |
|
1533 QContact firstContact; |
|
1534 QContactName name; |
|
1535 name.setFirstName("FirstMetaWithIM"+QString::number(contactid)); |
|
1536 firstContact.saveDetail(&name); |
|
1537 QVERIFY(trackerEngine->saveContact(&firstContact, error)); |
|
1538 |
|
1539 // save metarelationship |
|
1540 QContactRelationship rel; |
|
1541 rel.setRelationshipType(QContactRelationship::Is); |
|
1542 rel.setFirst(firstContact.id()); |
|
1543 masterContactId = firstContact.localId(); |
|
1544 rel.setSecond(c.id()); |
|
1545 QContactRelationshipSaveRequest req; |
|
1546 req.setRelationships(QList<QContactRelationship>()<<rel); |
|
1547 QVERIFY(trackerEngine->startRequest(&req)); |
|
1548 trackerEngine->waitForRequestFinished(&req, 1000); |
|
1549 QVERIFY(req.isFinished()); |
|
1550 QVERIFY(QContactManager::NoError == req.error()); |
|
1551 } |
|
1552 |
|
1553 // expected behavior - is that master contact contains all details aggregated |
|
1554 { |
|
1555 QList<QContact> cons = contacts(QList<QContactLocalId> () |
|
1556 << masterContactId << 999999, QStringList() |
|
1557 << QContactOnlineAccount::DefinitionName); |
|
1558 QVERIFY(cons.size() == 1); |
|
1559 QVERIFY(cons[0].id().localId() == masterContactId); |
|
1560 |
|
1561 bool containDetail = false; |
|
1562 foreach(QContactOnlineAccount det, cons[0].details<QContactOnlineAccount>()) |
|
1563 { |
|
1564 if (det.value("Account") == "999999@ovi.com" // deprecated, going to account URI |
|
1565 || det.accountUri() == "999999@ovi.com") |
|
1566 { |
|
1567 QVERIFY(det.presence() == QContactOnlineAccount::PresenceAvailable); |
|
1568 containDetail = true; |
|
1569 } |
|
1570 } |
|
1571 QVERIFY(containDetail); |
|
1572 } |
|
1573 //now update presence to IM Address and check it in contact (TODO and if signal is emitted) |
|
1574 updateIMContactStatus("telepathy:/org/freedesktop/fake/account/999999/999999@ovi.com", "nco:presence-status-offline"); |
|
1575 { |
|
1576 QList<QContact> cons = contacts(QList<QContactLocalId> () |
|
1577 << masterContactId << 999999, QStringList() |
|
1578 << QContactOnlineAccount::DefinitionName); |
|
1579 QVERIFY(cons.size() == 1); |
|
1580 QVERIFY(cons[0].id().localId() == masterContactId); |
|
1581 |
|
1582 bool containDetail = false; |
|
1583 foreach(QContactOnlineAccount det, cons[0].details<QContactOnlineAccount>()) |
|
1584 { |
|
1585 if (det.value("Account") == "999999@ovi.com" // deprecated, going to account URI |
|
1586 || det.accountUri() == "999999@ovi.com") |
|
1587 { |
|
1588 QVERIFY(det.presence() == QContactOnlineAccount::PresenceOffline); |
|
1589 containDetail = true; |
|
1590 } |
|
1591 } |
|
1592 QVERIFY(containDetail); |
|
1593 } |
|
1594 |
|
1595 // load contact should load also all merged content from other contacts (that dont exis anymore) |
|
1596 { |
|
1597 QList<QContact> cons = contacts(QList<QContactLocalId> () |
|
1598 << masterContactId, QStringList() |
|
1599 << QContactOnlineAccount::DefinitionName); |
|
1600 QVERIFY(cons.size() == 1); |
|
1601 QVERIFY(cons[0].id().localId() == masterContactId); |
|
1602 |
|
1603 bool containDetail = false; |
|
1604 foreach(QContactOnlineAccount det, cons[0].details<QContactOnlineAccount>()) |
|
1605 { |
|
1606 if (det.value("Account") == "999999@ovi.com" // deprecated, going to account URI |
|
1607 || det.accountUri() == "999999@ovi.com") |
|
1608 { |
|
1609 QVERIFY(det.presence() == QContactOnlineAccount::PresenceOffline); |
|
1610 containDetail = true; |
|
1611 } |
|
1612 } |
|
1613 QVERIFY(containDetail); |
|
1614 } |
|
1615 |
|
1616 // remove them |
|
1617 QVERIFY2(trackerEngine->removeContact(masterContactId, error), "Removing a contact failed"); |
|
1618 |
|
1619 foreach(unsigned int id, idstomerge) |
|
1620 { |
|
1621 QVERIFY2(!trackerEngine->removeContact(id, error), "Merged contact doesn't exist and removing it shoudl fail"); |
|
1622 } |
|
1623 } |
|
1624 |
|
1625 void ut_qtcontacts_trackerplugin::testIMContactsFilterring() |
|
1626 { |
|
1627 QList<unsigned int> idstoremove; |
|
1628 QList<QContactLocalId> idsToRetrieveThroughFilter; |
|
1629 for( int i = 0; i < 3; i++ ) |
|
1630 { |
|
1631 unsigned int contactid = qHash(QString("/org/freedesktop/fake/account/") + QString::number(999995+i) + "@ovi.com"); |
|
1632 idstoremove << contactid; |
|
1633 insertContact(QString("telepathy:/org/freedesktop/fake/account/") + QString::number(999995+i) + "@ovi.com", |
|
1634 contactid, QString::number(999995 + i)+ "@ovi.com", "nco:presence-status-available", |
|
1635 QString("/org/freedesktop/fake/account/%1").arg(i/2), QString("ovi%1.com").arg(i/2)); |
|
1636 if(!i/2) |
|
1637 idsToRetrieveThroughFilter << contactid; |
|
1638 } |
|
1639 |
|
1640 { |
|
1641 // now filter by service provider ovi0.com needs to return 2 contacts, 999995 & 999996 |
|
1642 QList<QContactLocalId> ids(idsToRetrieveThroughFilter); |
|
1643 |
|
1644 QContactFetchRequest request; |
|
1645 QContactDetailFilter filter; |
|
1646 filter.setDetailDefinitionName(QContactOnlineAccount::DefinitionName, QContactOnlineAccount::FieldServiceProvider); |
|
1647 |
|
1648 Slots slot; |
|
1649 QObject::connect(&request, SIGNAL(resultsAvailable()), |
|
1650 &slot, SLOT(resultsAvailable())); |
|
1651 filter.setValue(QString("ovi0.com")); |
|
1652 filter.setMatchFlags(QContactFilter::MatchExactly); |
|
1653 |
|
1654 request.setDefinitionRestrictions(QStringList()<<QContactOnlineAccount::DefinitionName); |
|
1655 request.setFilter(filter); |
|
1656 |
|
1657 trackerEngine->startRequest(&request); |
|
1658 |
|
1659 for(int i = 0; i < 100; i++) |
|
1660 { |
|
1661 usleep(100000); |
|
1662 QCoreApplication::processEvents(); |
|
1663 if(request.isFinished() ) |
|
1664 break; |
|
1665 } |
|
1666 |
|
1667 // if it takes more, then something is wrong |
|
1668 QVERIFY(request.isFinished()); |
|
1669 QVERIFY(!request.contacts().isEmpty()); |
|
1670 |
|
1671 QVERIFY(request.contacts().size() >= 2); |
|
1672 foreach(const QContact &contact, request.contacts()) |
|
1673 { |
|
1674 //qDebug() << contact.localId()<< "acc"<<contact.detail<QContactOnlineAccount>().serviceProvider(); |
|
1675 QVERIFY(contact.detail<QContactOnlineAccount>().serviceProvider() == "ovi0.com"); |
|
1676 ids.removeOne(contact.localId()); |
|
1677 } |
|
1678 QVERIFY(ids.isEmpty()); |
|
1679 } |
|
1680 |
|
1681 // now account path filter |
|
1682 { |
|
1683 // now filter by account path 999995 & 999996 |
|
1684 QList<QContactLocalId> ids(idsToRetrieveThroughFilter); |
|
1685 |
|
1686 QContactFetchRequest request; |
|
1687 QContactDetailFilter filter; |
|
1688 filter.setDetailDefinitionName(QContactOnlineAccount::DefinitionName, "AccountPath"); |
|
1689 |
|
1690 Slots slot; |
|
1691 QObject::connect(&request, SIGNAL(resultsAvailable()), |
|
1692 &slot, SLOT(resultsAvailable())); |
|
1693 // see insertTpContact |
|
1694 filter.setValue(QString("/org/freedesktop/fake/account/0")); |
|
1695 filter.setMatchFlags(QContactFilter::MatchExactly); |
|
1696 |
|
1697 request.setDefinitionRestrictions(QStringList()<<QContactOnlineAccount::DefinitionName); |
|
1698 request.setFilter(filter); |
|
1699 |
|
1700 trackerEngine->startRequest(&request); |
|
1701 |
|
1702 for(int i = 0; i < 100; i++) |
|
1703 { |
|
1704 usleep(100000); |
|
1705 QCoreApplication::processEvents(); |
|
1706 if(request.isFinished() ) |
|
1707 break; |
|
1708 } |
|
1709 |
|
1710 // if it takes more, then something is wrong |
|
1711 QVERIFY(request.isFinished()); |
|
1712 QVERIFY(!request.contacts().isEmpty()); |
|
1713 |
|
1714 QVERIFY(request.contacts().size() >= 2); |
|
1715 foreach(const QContact &contact, request.contacts()) |
|
1716 { |
|
1717 QVERIFY(contact.detail<QContactOnlineAccount>().serviceProvider() == "ovi0.com"); |
|
1718 ids.removeOne(contact.localId()); |
|
1719 } |
|
1720 QVERIFY(ids.isEmpty()); |
|
1721 } |
|
1722 |
|
1723 |
|
1724 // remove them |
|
1725 foreach(unsigned int id, idstoremove) |
|
1726 { |
|
1727 QVERIFY2(trackerEngine->removeContact(id, error), "Removing a contact failed"); |
|
1728 } |
|
1729 |
|
1730 } |
|
1731 |
|
1732 void ut_qtcontacts_trackerplugin::testContactsWithoutMeContact() { |
|
1733 QContact c; |
|
1734 QContactName name; |
|
1735 name.setFirstName("Totally"); |
|
1736 name.setLastName("Unique"); |
|
1737 c.saveDetail(&name); |
|
1738 trackerEngine->saveContact(&c, error); |
|
1739 QContactLocalId id = c.localId(); // Store ID for later removal. |
|
1740 |
|
1741 // Prepare the filter for the request - we fetch only the one contact saved above. |
|
1742 QList<QContactLocalId> ids; |
|
1743 ids << id; |
|
1744 QContactLocalIdFilter filter; |
|
1745 filter.setIds(ids); |
|
1746 |
|
1747 // Prepare the requst - give filter to it and specify which fields to fetch. We fetch only the name. |
|
1748 QStringList details; |
|
1749 details << QContactName::DefinitionName; |
|
1750 |
|
1751 QContactLocalIdFetchRequest nameFetchRequest; |
|
1752 nameFetchRequest.setFilter(filter); |
|
1753 |
|
1754 // Start the request and wait for it to finish. |
|
1755 trackerEngine->startRequest(&nameFetchRequest); |
|
1756 trackerEngine->waitForRequestFinished(&nameFetchRequest, 1000); |
|
1757 |
|
1758 // Requst finished. Test that only one contact is removed. |
|
1759 QList<QContactLocalId> contacts = nameFetchRequest.ids(); |
|
1760 QVERIFY2(contacts.count() < 2, "We expected to get only one contact. Got more."); |
|
1761 QVERIFY2(contacts.count() != 0, "We expected to get one contact. Got none."); |
|
1762 QVERIFY2(contacts.first() == id, "Did not get the requested contact back."); |
|
1763 |
|
1764 // Cleaning up. |
|
1765 trackerEngine->removeContact(id, error); |
|
1766 |
|
1767 } |
|
1768 |
|
1769 void ut_qtcontacts_trackerplugin::testDefinitionNames() |
|
1770 { |
|
1771 QContactManager *cm(ContactManager::instance()); |
|
1772 QMap<QString, QContactDetailDefinition> defs(cm->detailDefinitions()); |
|
1773 |
|
1774 foreach(QString key, defs.keys()) { |
|
1775 QCOMPARE(defs[key].name(), key); |
|
1776 } |
|
1777 } |
|
1778 |
|
1779 |
|
1780 /*************************** Helper functions for unit tests ***************'*/ |
|
1781 |
|
1782 QContact ut_qtcontacts_trackerplugin::contact(QContactLocalId id, QStringList details) |
|
1783 { |
|
1784 QContactManager::Error error; |
|
1785 return trackerEngine->contact_impl(id, details, error); |
|
1786 } |
|
1787 |
|
1788 QList<QContact> ut_qtcontacts_trackerplugin::contacts(QList<QContactLocalId> ids, QStringList details) |
|
1789 { |
|
1790 QContactFetchRequest request; |
|
1791 QContactLocalIdFilter filter; |
|
1792 filter.setIds(ids); |
|
1793 request.setFilter(filter); |
|
1794 |
|
1795 request.setDefinitionRestrictions(details); |
|
1796 |
|
1797 trackerEngine->startRequest(&request); |
|
1798 trackerEngine->waitForRequestFinished(&request, 1000); |
|
1799 |
|
1800 return request.contacts(); |
|
1801 } |
|
1802 |
|
1803 void Slots::idResultsAvailable() |
|
1804 { |
|
1805 QContactLocalIdFetchRequest* self = qobject_cast<QContactLocalIdFetchRequest*>(sender()); |
|
1806 ids << self->ids(); |
|
1807 } |
|
1808 |
|
1809 void Slots::resultsAvailable() |
|
1810 { |
|
1811 QContactFetchRequest* self = qobject_cast<QContactFetchRequest*>(sender()); |
|
1812 contacts = self->contacts(); |
|
1813 QList<QContactLocalId> idsFromAllContactReq; |
|
1814 foreach( QContact contact, contacts) |
|
1815 { |
|
1816 idsFromAllContactReq << contact.localId(); |
|
1817 } |
|
1818 } |
|
1819 |
|
1820 QTEST_MAIN(ut_qtcontacts_trackerplugin) |
|