|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 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_fetch.h" |
|
43 |
|
44 #include <QContactManager> |
|
45 #include <QContactName> |
|
46 #include <QContactFetchRequest> |
|
47 #include <QContactUrl> |
|
48 |
|
49 #include <QtTracker/tracker.h> |
|
50 |
|
51 typedef QSet<QString> QStringSet; |
|
52 |
|
53 ut_qtcontacts_fetch::ut_qtcontacts_fetch() |
|
54 { |
|
55 mUuid = QUuid::createUuid(); |
|
56 mFirstName = "ut_qtcontacts_fetch_firstname_" + mUuid; |
|
57 mLastName = "ut_qtcontacts_fetch_lastname_" + mUuid; |
|
58 mWebPage = "ut_qtcontacts_fetch_url_" + mUuid; |
|
59 |
|
60 mNameFilter.setDetailDefinitionName(QContactName::DefinitionName, QContactName::FieldFirst); |
|
61 mNameFilter.setMatchFlags(QContactFilter::MatchExactly); |
|
62 mNameFilter.setValue(mFirstName); |
|
63 } |
|
64 |
|
65 void ut_qtcontacts_fetch::setupTestContact(QContact &contact) |
|
66 { |
|
67 QContactName name; |
|
68 name.setFirstName(mFirstName); |
|
69 name.setLastName(mLastName); |
|
70 QVERIFY(contact.saveDetail(&name)); |
|
71 |
|
72 QContactUrl url; |
|
73 url.setUrl(mWebPage); |
|
74 QVERIFY(contact.saveDetail(&url)); |
|
75 } |
|
76 |
|
77 void ut_qtcontacts_fetch::checkDatabaseEmpty() |
|
78 { |
|
79 // try to fetch our testing contact |
|
80 QContactFetchRequest request; |
|
81 request.setManager(mContactManager); |
|
82 request.setFilter(mNameFilter); |
|
83 QVERIFY(request.start()); |
|
84 |
|
85 // wait for the request to finish |
|
86 waitForRequest(request); |
|
87 CHECK_CURRENT_TEST_FAILED; |
|
88 |
|
89 // dump unexpected contacts |
|
90 foreach(const QContact &c, request.contacts()) { |
|
91 qWarning() << "unexpected contact" << c.localId(); |
|
92 foreach(const QContactDetail &d, c.details()) |
|
93 qWarning() << " " << d.definitionName() << d.variantValues(); |
|
94 } |
|
95 |
|
96 // verify that there really is no test contact yet |
|
97 QVERIFY(request.isFinished()); |
|
98 QVERIFY(request.contacts().isEmpty()); |
|
99 } |
|
100 |
|
101 void ut_qtcontacts_fetch::testSaveContact() |
|
102 { |
|
103 // check that we start with a clean database |
|
104 checkDatabaseEmpty(); |
|
105 CHECK_CURRENT_TEST_FAILED; |
|
106 |
|
107 // create a named contact and save it |
|
108 QContact contact; |
|
109 |
|
110 setupTestContact(contact); |
|
111 CHECK_CURRENT_TEST_FAILED; |
|
112 |
|
113 saveContact(contact); |
|
114 CHECK_CURRENT_TEST_FAILED; |
|
115 } |
|
116 |
|
117 void ut_qtcontacts_fetch::testSaveContactCopy() |
|
118 { |
|
119 // check that we start with a clean database |
|
120 checkDatabaseEmpty(); |
|
121 CHECK_CURRENT_TEST_FAILED; |
|
122 |
|
123 // create a named contact |
|
124 QContact contact; |
|
125 |
|
126 setupTestContact(contact); |
|
127 CHECK_CURRENT_TEST_FAILED; |
|
128 |
|
129 // add copy of the contact to database |
|
130 QContact copy(contact); |
|
131 |
|
132 saveContact(copy); |
|
133 CHECK_CURRENT_TEST_FAILED; |
|
134 } |
|
135 |
|
136 void ut_qtcontacts_fetch::testFetchSavedContact() |
|
137 { |
|
138 // check that we start with a clean database |
|
139 checkDatabaseEmpty(); |
|
140 CHECK_CURRENT_TEST_FAILED; |
|
141 |
|
142 // create a named contact and save it |
|
143 QContact savedContact; |
|
144 |
|
145 setupTestContact(savedContact); |
|
146 CHECK_CURRENT_TEST_FAILED; |
|
147 |
|
148 saveContact(savedContact); |
|
149 CHECK_CURRENT_TEST_FAILED; |
|
150 mLocalIds.clear(); |
|
151 |
|
152 // fetch the saved contact |
|
153 QContact fetchedContact; |
|
154 fetchContact(mNameFilter, fetchedContact); |
|
155 CHECK_CURRENT_TEST_FAILED; |
|
156 |
|
157 QVERIFY(0 != fetchedContact.localId()); |
|
158 |
|
159 // check that the fetched contact has the expected properties |
|
160 QList<QContactDetail> details(fetchedContact.details(QContactName::DefinitionName)); |
|
161 |
|
162 QCOMPARE(details.count(), 1); |
|
163 QCOMPARE(details[0].value(QContactName::FieldFirstName), mFirstName); |
|
164 QCOMPARE(details[0].value(QContactName::FieldLastName), mLastName); |
|
165 |
|
166 details = fetchedContact.details(QContactUrl::DefinitionName); |
|
167 |
|
168 QEXPECT_FAIL("", "cannot restore URL field right now", Continue); |
|
169 |
|
170 QCOMPARE(details.count(), 1); |
|
171 QCOMPARE(details[0].value(QContactUrl::FieldUrl), mWebPage); |
|
172 } |
|
173 |
|
174 QTEST_MAIN(ut_qtcontacts_fetch) |