1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2015 The Qt Company Ltd. |
4 | ** Contact: http://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the test suite of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL21$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see http://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at http://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 2.1 or version 3 as published by the Free |
20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and |
21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the |
22 | ** following information to ensure the GNU Lesser General Public License |
23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and |
24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
25 | ** |
26 | ** As a special exception, The Qt Company gives you certain additional |
27 | ** rights. These rights are described in The Qt Company LGPL Exception |
28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
29 | ** |
30 | ** $QT_END_LICENSE$ |
31 | ** |
32 | ****************************************************************************/ |
33 | |
34 | #include <QtTest/QtTest> |
35 | #include <QDateTime> |
36 | #include <QtContacts> |
37 | |
38 | #include "qcontactmanagerdataholder.h" |
39 | |
40 | QTCONTACTS_USE_NAMESPACE |
41 | |
42 | // to get QFETCH to work with the template expression... |
43 | typedef QMap<QString,QString> tst_QContactManagerDetails_QStringMap; |
44 | Q_DECLARE_METATYPE(tst_QContactManagerDetails_QStringMap) |
45 | |
46 | |
47 | class tst_QContactManagerDetails : public QObject |
48 | { |
49 | Q_OBJECT |
50 | |
51 | public: |
52 | tst_QContactManagerDetails(); |
53 | virtual ~tst_QContactManagerDetails(); |
54 | |
55 | private: |
56 | void addManagers(); |
57 | |
58 | template <typename T> |
59 | void removeDetail( QContact& contact ) |
60 | { |
61 | T detail = contact.detail<T>(); |
62 | contact.removeDetail( detail: &detail ); |
63 | } |
64 | |
65 | bool saveAndLoadContact( QContactManager *cm, QContact &original, QContact &loaded ); |
66 | void saveAndVerifyContact( QContactManager *cm, QContact &original ); |
67 | |
68 | QScopedPointer<QContactManagerDataHolder> managerDataHolder; |
69 | |
70 | private slots: |
71 | void initTestCase(); |
72 | void cleanupTestCase(); |
73 | |
74 | void testAddress(); |
75 | void testAddress_data() {addManagers();} |
76 | void testAnniversary(); |
77 | void testAnniversary_data() {addManagers();} |
78 | void testAvatar(); |
79 | void testAvatar_data() {addManagers();} |
80 | void testBirthday(); |
81 | void testBirthday_data() {addManagers();} |
82 | void testDisplayLabel(); |
83 | void testDisplayLabel_data() {addManagers();} |
84 | void testEmailAddress(); |
85 | void testEmailAddress_data() {addManagers();} |
86 | void testEmptyExtendedDetail(); |
87 | void testEmptyExtendedDetail_data() {addManagers();} |
88 | void testExtendedDetail(); |
89 | void testExtendedDetail_data() {addManagers();} |
90 | void testGuid(); |
91 | void testGuid_data() {addManagers();} |
92 | void testName(); |
93 | void testName_data() {addManagers();} |
94 | void testNickName(); |
95 | void testNickName_data() {addManagers();} |
96 | void testOrganisation(); |
97 | void testOrganisation_data() {addManagers();} |
98 | void testOnlineAccount(); |
99 | void testOnlineAccount_data() {addManagers();} |
100 | void testPhoneNumber(); |
101 | void testPhoneNumber_data() {addManagers();} |
102 | void testSyncTarget(); |
103 | void testSyncTarget_data() {addManagers();} |
104 | void testUrl(); |
105 | void testUrl_data() {addManagers();} |
106 | void testRingtone(); |
107 | void testRingtone_data() {addManagers();} |
108 | |
109 | private slots: |
110 | |
111 | }; |
112 | |
113 | tst_QContactManagerDetails::tst_QContactManagerDetails() |
114 | { |
115 | } |
116 | |
117 | tst_QContactManagerDetails::~tst_QContactManagerDetails() |
118 | { |
119 | } |
120 | |
121 | void tst_QContactManagerDetails::addManagers() |
122 | { |
123 | QTest::addColumn<QString>(name: "uri" ); |
124 | |
125 | QStringList managers = QContactManager::availableManagers(); |
126 | |
127 | /* Known one that will not pass */ |
128 | managers.removeAll(t: "invalid" ); |
129 | managers.removeAll(t: "testdummy" ); |
130 | managers.removeAll(t: "teststaticdummy" ); |
131 | managers.removeAll(t: "maliciousplugin" ); |
132 | |
133 | foreach(QString mgr, managers) { |
134 | QMap<QString, QString> params; |
135 | QTest::newRow(dataTag: QString("mgr='%1'" ).arg(a: mgr).toLatin1().constData()) << QContactManager::buildUri(managerName: mgr, params); |
136 | } |
137 | } |
138 | |
139 | void tst_QContactManagerDetails::initTestCase() |
140 | { |
141 | managerDataHolder.reset(other: new QContactManagerDataHolder()); |
142 | } |
143 | |
144 | void tst_QContactManagerDetails::cleanupTestCase() |
145 | { |
146 | managerDataHolder.reset(other: 0); |
147 | } |
148 | |
149 | bool tst_QContactManagerDetails::saveAndLoadContact( QContactManager *cm, QContact &original, QContact &loaded ) |
150 | { |
151 | // Save contact |
152 | if( cm->saveContact(contact: &original) == false ) |
153 | return false; |
154 | |
155 | // Check the id |
156 | if (original.id().isNull()) |
157 | return false; |
158 | |
159 | // Load same contact from database |
160 | loaded = cm->contact(contactId: original.id()); |
161 | if( cm->error() ) |
162 | return false; |
163 | |
164 | // Ignore some details which are not relevant and will mess |
165 | // up direct comparison between two contacts. |
166 | removeDetail<QContactGuid>(contact&: original); |
167 | removeDetail<QContactGuid>(contact&: loaded); |
168 | removeDetail<QContactTimestamp>(contact&: original); |
169 | removeDetail<QContactTimestamp>(contact&: loaded); |
170 | removeDetail<QContactVersion>(contact&: loaded); |
171 | |
172 | return true; |
173 | } |
174 | |
175 | void tst_QContactManagerDetails::saveAndVerifyContact( QContactManager *cm, QContact &original ) |
176 | { |
177 | QContact loaded; |
178 | QVERIFY( saveAndLoadContact( cm, original, loaded ) ); |
179 | if (original != loaded) { |
180 | qDebug() << "expected: " << original; |
181 | qDebug() << "loaded: " << loaded; |
182 | QCOMPARE( loaded.details().count(), original.details().count() ); |
183 | QCOMPARE( loaded, original ); |
184 | } |
185 | } |
186 | |
187 | void tst_QContactManagerDetails::testAddress() |
188 | { |
189 | QFETCH(QString, uri); |
190 | QScopedPointer<QContactManager> cm(QContactManager::fromUri(uri)); |
191 | |
192 | QContact c; |
193 | |
194 | QContactAddress a; |
195 | a.setStreet("street" ); |
196 | a.setLocality("locality" ); |
197 | a.setRegion("region" ); |
198 | a.setPostcode("postcode" ); |
199 | a.setCountry("country" ); |
200 | a.setPostOfficeBox("POBox" ); |
201 | c.saveDetail( detail: &a ); |
202 | |
203 | saveAndVerifyContact( cm: cm.data(), original&: c ); |
204 | QContact c1; |
205 | |
206 | QContactAddress a1; |
207 | a1.setStreet("street1" ); |
208 | a1.setLocality("locality1" ); |
209 | a1.setRegion("region1" ); |
210 | a1.setPostcode("postcode1" ); |
211 | a1.setCountry("country1" ); |
212 | a1.setPostOfficeBox("POBox1" ); |
213 | c1.saveDetail( detail: &a1 ); |
214 | |
215 | QContactAddress a2; |
216 | a2.setStreet("street2" ); |
217 | a2.setLocality("locality2" ); |
218 | a2.setRegion("region2" ); |
219 | a2.setPostcode("postcode2" ); |
220 | a2.setCountry("country2" ); |
221 | a2.setPostOfficeBox("POBox2" ); |
222 | c1.saveDetail( detail: &a2 ); |
223 | |
224 | QContact c2; |
225 | saveAndLoadContact( cm: cm.data(), original&: c1, loaded&: c2 ); |
226 | |
227 | QList<QContactAddress> addresses = c2.details<QContactAddress>(); |
228 | QCOMPARE(addresses.count(), 2); |
229 | QCOMPARE(addresses[0], a1); |
230 | QCOMPARE(addresses[1], a2); |
231 | |
232 | QContact c3; |
233 | |
234 | // General address |
235 | QContactAddress a3; |
236 | a3.setStreet("street1" ); |
237 | c3.saveDetail( detail: &a3 ); |
238 | |
239 | // home address |
240 | QContactAddress a4; |
241 | a4.setStreet("street2" ); |
242 | a4.setContexts( QContactDetail::ContextHome ); |
243 | c3.saveDetail( detail: &a4 ); |
244 | |
245 | // work address |
246 | QContactAddress a5; |
247 | a5.setStreet("street3" ); |
248 | a5.setContexts( QContactDetail::ContextWork ); |
249 | c3.saveDetail( detail: &a5 ); |
250 | |
251 | saveAndVerifyContact( cm: cm.data(), original&: c3 ); |
252 | } |
253 | |
254 | void tst_QContactManagerDetails::testAnniversary() |
255 | { |
256 | QFETCH(QString, uri); |
257 | QScopedPointer<QContactManager> cm(QContactManager::fromUri(uri)); |
258 | QContact c; |
259 | QContactAnniversary a; |
260 | |
261 | a.setOriginalDate( QDate(2009,9,9) ); |
262 | c.saveDetail( detail: &a ); |
263 | saveAndVerifyContact( cm: cm.data(), original&: c ); |
264 | } |
265 | |
266 | void tst_QContactManagerDetails::testAvatar() |
267 | { |
268 | QFETCH(QString, uri); |
269 | QScopedPointer<QContactManager> cm(QContactManager::fromUri(uri)); |
270 | |
271 | QContact c; |
272 | |
273 | QContactAvatar a; |
274 | a.setImageUrl( QUrl::fromLocalFile(localfile: "c:\\foo\\bar.jpeg" ) ); |
275 | c.saveDetail( detail: &a ); |
276 | |
277 | saveAndVerifyContact( cm: cm.data(), original&: c ); |
278 | } |
279 | |
280 | void tst_QContactManagerDetails::testBirthday() |
281 | { |
282 | QFETCH(QString, uri); |
283 | QScopedPointer<QContactManager> cm(QContactManager::fromUri(uri)); |
284 | |
285 | QContact c; |
286 | |
287 | QContactBirthday b; |
288 | b.setDateTime( QDateTime(QDate(2001,1,1)) ); |
289 | c.saveDetail( detail: &b ); |
290 | |
291 | saveAndVerifyContact( cm: cm.data(), original&: c ); |
292 | } |
293 | |
294 | void tst_QContactManagerDetails::testEmailAddress() |
295 | { |
296 | QFETCH(QString, uri); |
297 | QScopedPointer<QContactManager> cm(QContactManager::fromUri(uri)); |
298 | |
299 | QContact c; |
300 | |
301 | QContactEmailAddress e; |
302 | e.setEmailAddress( "foo@bar.com" ); |
303 | c.saveDetail( detail: &e ); |
304 | |
305 | saveAndVerifyContact( cm: cm.data(), original&: c ); |
306 | } |
307 | |
308 | void tst_QContactManagerDetails::testName() |
309 | { |
310 | QFETCH(QString, uri); |
311 | QScopedPointer<QContactManager> cm(QContactManager::fromUri(uri)); |
312 | QContact c; |
313 | |
314 | QContactName n; |
315 | n.setPrefix( "prefix" ); |
316 | n.setFirstName( "first" ); |
317 | n.setMiddleName( "middle" ); |
318 | n.setLastName( "last" ); |
319 | n.setSuffix( "suffix" ); |
320 | c.saveDetail( detail: &n ); |
321 | saveAndVerifyContact( cm: cm.data(), original&: c ); |
322 | } |
323 | |
324 | void tst_QContactManagerDetails::testDisplayLabel() |
325 | { |
326 | QFETCH(QString, uri); |
327 | QScopedPointer<QContactManager> cm(QContactManager::fromUri(uri)); |
328 | |
329 | QContact originalContact; |
330 | |
331 | QContactDisplayLabel dl1; |
332 | dl1.setLabel("displayLabel1" ); |
333 | originalContact.saveDetail(detail: &dl1); |
334 | |
335 | saveAndVerifyContact(cm: cm.data(), original&: originalContact); |
336 | |
337 | QContact loadedContact; |
338 | QVERIFY(saveAndLoadContact(cm.data(), originalContact, loadedContact)); |
339 | QCOMPARE(loadedContact.details().count(), originalContact.details().count()); |
340 | QCOMPARE(loadedContact, originalContact); |
341 | } |
342 | |
343 | void tst_QContactManagerDetails::testNickName() |
344 | { |
345 | QFETCH(QString, uri); |
346 | QScopedPointer<QContactManager> cm(QContactManager::fromUri(uri)); |
347 | |
348 | QContact originalContact; |
349 | |
350 | QContactNickname n1; |
351 | n1.setNickname("nickname1" ); |
352 | originalContact.saveDetail(detail: &n1); |
353 | |
354 | saveAndVerifyContact(cm: cm.data(), original&: originalContact); |
355 | |
356 | QContactNickname n2; |
357 | n2.setNickname("nickname2" ); |
358 | originalContact.saveDetail(detail: &n2); |
359 | |
360 | QContact loadedContact; |
361 | QVERIFY(saveAndLoadContact(cm.data(), originalContact, loadedContact)); |
362 | QCOMPARE(loadedContact.details().count(), originalContact.details().count()); |
363 | QCOMPARE(loadedContact, originalContact); |
364 | } |
365 | |
366 | void tst_QContactManagerDetails::testOrganisation() |
367 | { |
368 | QFETCH(QString, uri); |
369 | QScopedPointer<QContactManager> cm(QContactManager::fromUri(uri)); |
370 | |
371 | QContact c; |
372 | |
373 | QContactOrganization o; |
374 | o.setName( "Foreign legion" ); |
375 | o.setTitle( "Bicycle mechanic" ); |
376 | c.saveDetail( detail: &o ); |
377 | |
378 | saveAndVerifyContact( cm: cm.data(), original&: c ); |
379 | } |
380 | |
381 | void tst_QContactManagerDetails::testOnlineAccount() |
382 | { |
383 | QFETCH(QString, uri); |
384 | QScopedPointer<QContactManager> cm(QContactManager::fromUri(uri)); |
385 | QContact c; |
386 | |
387 | QContactOnlineAccount o; |
388 | o.setAccountUri( "john@example.com" ); |
389 | o.setProtocol(QContactOnlineAccount::ProtocolJabber); |
390 | c.saveDetail( detail: &o ); |
391 | saveAndVerifyContact( cm: cm.data(), original&: c ); |
392 | } |
393 | |
394 | void tst_QContactManagerDetails::testPhoneNumber() |
395 | { |
396 | QFETCH(QString, uri); |
397 | QScopedPointer<QContactManager> cm(QContactManager::fromUri(uri)); |
398 | QContact c; |
399 | |
400 | // General landline number |
401 | QContactPhoneNumber n2; |
402 | n2.setNumber( "2" ); |
403 | n2.setSubTypes( QList<int>() << QContactPhoneNumber::SubTypeLandline ); |
404 | c.saveDetail( detail: &n2 ); |
405 | |
406 | saveAndVerifyContact( cm: cm.data(), original&: c ); |
407 | |
408 | QContact c2; |
409 | |
410 | // home mobile number |
411 | QContactPhoneNumber n1; |
412 | n1.setNumber( "1" ); |
413 | n1.setSubTypes( QList<int>() << QContactPhoneNumber::SubTypeMobile ); |
414 | n1.setContexts( QContactDetail::ContextHome ); |
415 | c2.saveDetail( detail: &n1 ); |
416 | |
417 | // work landline number |
418 | QContactPhoneNumber n3; |
419 | n3.setNumber( "3" ); |
420 | n3.setSubTypes( QList<int>() << QContactPhoneNumber::SubTypeLandline ); |
421 | n3.setContexts( QContactDetail::ContextWork ); |
422 | c2.saveDetail( detail: &n3 ); |
423 | |
424 | saveAndVerifyContact( cm: cm.data(), original&: c2 ); |
425 | } |
426 | |
427 | void tst_QContactManagerDetails::testSyncTarget() |
428 | { |
429 | QFETCH(QString, uri); |
430 | QScopedPointer<QContactManager> cm(QContactManager::fromUri(uri)); |
431 | QContact c; |
432 | |
433 | QContactSyncTarget syncTarget; |
434 | syncTarget.setSyncTarget("TestSyncTarget" ); |
435 | c.saveDetail( detail: &syncTarget ); |
436 | saveAndVerifyContact( cm: cm.data(), original&: c ); |
437 | } |
438 | |
439 | void tst_QContactManagerDetails::testUrl() |
440 | { |
441 | QFETCH(QString, uri); |
442 | QScopedPointer<QContactManager> cm(QContactManager::fromUri(uri)); |
443 | |
444 | QContact originalContact; |
445 | |
446 | QContactUrl u; |
447 | u.setUrl("http://failblog.org" ); |
448 | u.setSubType(QContactUrl::SubTypeHomePage); |
449 | originalContact.saveDetail(detail: &u); |
450 | |
451 | QContact loadedContact; |
452 | QVERIFY(saveAndLoadContact(cm.data(), originalContact, loadedContact)); |
453 | QCOMPARE( loadedContact.details().count(), originalContact.details().count() ); |
454 | QCOMPARE( loadedContact, originalContact ); |
455 | } |
456 | |
457 | void tst_QContactManagerDetails::testRingtone() |
458 | { |
459 | QFETCH(QString, uri); |
460 | QScopedPointer<QContactManager> cm(QContactManager::fromUri(uri)); |
461 | |
462 | QUrl rtUrl(QUrl::fromLocalFile(localfile: "/home/user/sample.wav" )); |
463 | |
464 | QContact c; |
465 | QContactRingtone rt; |
466 | rt.setAudioRingtoneUrl(rtUrl); |
467 | c.saveDetail(detail: &rt); |
468 | |
469 | saveAndVerifyContact( cm: cm.data(), original&: c ); |
470 | } |
471 | |
472 | void tst_QContactManagerDetails::testEmptyExtendedDetail() |
473 | { |
474 | QFETCH(QString, uri); |
475 | QScopedPointer<QContactManager> cm(QContactManager::fromUri(uri)); |
476 | |
477 | QContact c; |
478 | |
479 | // Adding an empty extended detail |
480 | QContactExtendedDetail emptyDetail; |
481 | QVERIFY(c.saveDetail(&emptyDetail)); |
482 | saveAndVerifyContact(cm: cm.data(), original&: c); |
483 | } |
484 | |
485 | void tst_QContactManagerDetails::testExtendedDetail() |
486 | { |
487 | QFETCH(QString, uri); |
488 | QScopedPointer<QContactManager> cm(QContactManager::fromUri(uri)); |
489 | |
490 | QContact c; |
491 | |
492 | QContactExtendedDetail extendedDetail1, extendedDetail2; |
493 | extendedDetail1.setName("testExtDetailVariantList" ); |
494 | extendedDetail1.setData(QVariantList() << QString("QString in the QVariantlist detail." ) |
495 | << QVariant(1) |
496 | << QString("Another QString in the QVariantlist detail." ) |
497 | << 2); |
498 | QVERIFY(c.saveDetail(&extendedDetail1)); |
499 | saveAndVerifyContact(cm: cm.data(), original&: c); |
500 | |
501 | // Adding two extended details to same contact |
502 | QContact c2; |
503 | extendedDetail2.setName("testExtDetailInt" ); |
504 | extendedDetail2.setData(2); |
505 | QVERIFY (c2.saveDetail(&extendedDetail2)); |
506 | QVERIFY (c2.saveDetail(&extendedDetail1)); |
507 | saveAndVerifyContact(cm: cm.data(), original&: c2); |
508 | |
509 | // Adding same details but in different order |
510 | QContact c3; |
511 | QVERIFY (c3.saveDetail(&extendedDetail1)); |
512 | QVERIFY (c3.saveDetail(&extendedDetail2)); |
513 | saveAndVerifyContact(cm: cm.data(), original&: c3); |
514 | } |
515 | |
516 | void tst_QContactManagerDetails::testGuid() |
517 | { |
518 | QFETCH(QString, uri); |
519 | QScopedPointer<QContactManager> cm(QContactManager::fromUri(uri)); |
520 | QContact c; |
521 | |
522 | QContactGuid guid; |
523 | guid.setGuid("TestGUID" ); |
524 | c.saveDetail( detail: &guid ); |
525 | saveAndVerifyContact( cm: cm.data(), original&: c ); |
526 | } |
527 | |
528 | QTEST_MAIN(tst_QContactManagerDetails) |
529 | #include "tst_qcontactmanagerdetails.moc" |
530 | |