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_add_async.h" |
|
43 |
|
44 #include <QContactManager> |
|
45 #include <QContactName> |
|
46 #include <QContactDetailFilter> |
|
47 #include <QContactPhoneNumber> |
|
48 #include <QtTest/QtTest> |
|
49 #include <QDebug> |
|
50 |
|
51 // Note that we try to avoid using any names that might already be in the database: |
|
52 const char* TESTNAME_FIRST = "ut_qtcontacts_add_firstname"; |
|
53 const char* TESTNAME_LAST = "ut_qtcontacts_add_firstlast"; |
|
54 |
|
55 ut_qtcontacts_add::ut_qtcontacts_add() |
|
56 : getExistingContactFinishedCallback(0), |
|
57 waiting(false) |
|
58 { |
|
59 } |
|
60 |
|
61 ut_qtcontacts_add::~ut_qtcontacts_add() |
|
62 { |
|
63 |
|
64 } |
|
65 |
|
66 void ut_qtcontacts_add::initTestCase() |
|
67 { |
|
68 } |
|
69 |
|
70 void ut_qtcontacts_add::cleanupTestCase() |
|
71 { |
|
72 } |
|
73 |
|
74 void ut_qtcontacts_add::init() |
|
75 { |
|
76 } |
|
77 |
|
78 void ut_qtcontacts_add::cleanup() |
|
79 { |
|
80 waiting = false; |
|
81 } |
|
82 |
|
83 bool ut_qtcontacts_add::waitForStop() |
|
84 { |
|
85 waiting = true; |
|
86 |
|
87 const int max_secs = 100000; |
|
88 |
|
89 // wait for signal |
|
90 int i = 0; |
|
91 while(waiting && i++ < max_secs) { |
|
92 // Allow the mainloop to run: |
|
93 QTest::qWait(10); |
|
94 } |
|
95 |
|
96 return !waiting; |
|
97 } |
|
98 |
|
99 QContactManager* ut_qtcontacts_add::getContactManager() |
|
100 { |
|
101 static QContactManager manager("tracker"); |
|
102 return &manager; |
|
103 } |
|
104 |
|
105 void ut_qtcontacts_add::onContactFetchRequestProgress() |
|
106 { |
|
107 //qDebug() << "onContactFetchRequestProgress"; |
|
108 if (!contactFetchRequest.isFinished()) |
|
109 return; |
|
110 |
|
111 //Store the contact so the callback can use it. |
|
112 if(!(contactFetchRequest.contacts().isEmpty())) { |
|
113 contact = contactFetchRequest.contacts()[0]; |
|
114 QVERIFY(contact.localId() != 0); |
|
115 contactFetchRequest.cancel(); //Stop any more slot calls. |
|
116 } |
|
117 |
|
118 //qDebug() << "debug: fetched localId=" << contact.localId(); |
|
119 |
|
120 //Avoid more slot calls, though this is unlikely because it has finished. |
|
121 contactFetchRequest.cancel(); |
|
122 |
|
123 //Call the callback method that was specified to getExistingContact(): |
|
124 if(getExistingContactFinishedCallback) { |
|
125 FinishedCallbackFunc func = getExistingContactFinishedCallback; |
|
126 getExistingContactFinishedCallback = 0; |
|
127 (this->*func)(); |
|
128 } |
|
129 } |
|
130 |
|
131 |
|
132 void ut_qtcontacts_add::getExistingContact(FinishedCallbackFunc finishedCallback) |
|
133 { |
|
134 QContactManager* manager = getContactManager(); |
|
135 Q_ASSERT(manager); |
|
136 |
|
137 // Stop pending fetch requests |
|
138 if (contactFetchRequest.isActive()) |
|
139 contactFetchRequest.cancel(); |
|
140 |
|
141 // Initialize the result. |
|
142 contact = QContact(); |
|
143 |
|
144 //TODO: How can we AND on both the first and last name? |
|
145 getExistingContactFinishedCallback = finishedCallback; //Call this when the contact has been retrieved. |
|
146 connect(&contactFetchRequest, SIGNAL(resultsAvailable()), |
|
147 SLOT(onContactFetchRequestProgress())); |
|
148 |
|
149 QContactDetailFilter nameFilter; |
|
150 nameFilter.setDetailDefinitionName(QContactName::DefinitionName, QContactName::FieldFirst); |
|
151 nameFilter.setValue(QLatin1String(TESTNAME_FIRST)); |
|
152 nameFilter.setMatchFlags(QContactFilter::MatchExactly); |
|
153 contactFetchRequest.setManager(manager); |
|
154 contactFetchRequest.setFilter(nameFilter); |
|
155 |
|
156 //qDebug() << "debug: start request"; |
|
157 contactFetchRequest.start(); |
|
158 } |
|
159 |
|
160 //This is our actual test function: |
|
161 void ut_qtcontacts_add::ut_testAddContact() |
|
162 { |
|
163 //qDebug() << "debug: ut_testAddContact"; |
|
164 //Make sure that the contact is not already in the database. |
|
165 getExistingContact(&ut_qtcontacts_add::onContactFoundThenRemoveAndTest); |
|
166 |
|
167 //Block (allowing the mainloop to run) until we have finished. |
|
168 waitForStop(); |
|
169 } |
|
170 |
|
171 void ut_qtcontacts_add::onContactFoundThenRemoveAndTest() |
|
172 { |
|
173 //qDebug() << "debug: Removing the existing contact, if it exists."; |
|
174 QContactManager* manager = getContactManager(); |
|
175 Q_ASSERT(manager); |
|
176 |
|
177 //TODO: Find and use an async API that tells us when it has finished. |
|
178 manager->removeContact(contact.localId()); |
|
179 |
|
180 // Run the Qt UI's main loop, showing the editor while it is running: |
|
181 QTimer::singleShot(2000, this, SLOT(onTimeoutAddContact())); |
|
182 } |
|
183 |
|
184 void ut_qtcontacts_add::onTimeoutAddContact() |
|
185 { |
|
186 //qDebug() << "debug: Trying to add contact."; |
|
187 |
|
188 // Offer a UI to edit a prefilled contact. |
|
189 QContactName name; |
|
190 name.setFirstName(QLatin1String(TESTNAME_FIRST)); |
|
191 name.setLastName(QLatin1String(TESTNAME_LAST)); |
|
192 //TODO: Find and use an async API that tells us when it has finished. |
|
193 contact.saveDetail(&name); |
|
194 //const bool saved = contact.saveDetail(&name); |
|
195 //Q_ASSERT(saved); //This won't necessarily be useful because our implementation doesn't support sync methods. |
|
196 |
|
197 //Save the contact. |
|
198 //But note that our QContactManager backend does not set localId when returning. |
|
199 QContactManager* manager = getContactManager(); |
|
200 Q_ASSERT(manager); |
|
201 |
|
202 |
|
203 manager->saveContact(&contact); |
|
204 //This works too: |
|
205 //QContact copy(contact); |
|
206 //manager->saveContact(©); |
|
207 |
|
208 //Check that it was really saved: |
|
209 //qDebug() << "debug: checking that the contact was saved."; |
|
210 getExistingContact(&ut_qtcontacts_add::onContactFoundThenCheck); |
|
211 } |
|
212 |
|
213 void ut_qtcontacts_add::onContactFoundThenCheck() |
|
214 { |
|
215 //Check that it was really saved: |
|
216 // The ContactManager::saveContact() documentation suggests that localeId=0 is for non-saved contacts. |
|
217 //QEXPECT_FAIL("", "QContactManager::saveContact() saves the contact (see it by running the contacts UI), but returns false and doesn't set error(). Find out why.", Continue); |
|
218 QVERIFY(contact.localId() != 0); |
|
219 |
|
220 //Check that the correct details were saved: |
|
221 const QContactName name = contact.detail<QContactName>(); |
|
222 QVERIFY(name.firstName() == QLatin1String(TESTNAME_FIRST)); |
|
223 QVERIFY(name.lastName() == QLatin1String(TESTNAME_LAST)); |
|
224 |
|
225 //Try to restore original conditions: |
|
226 getExistingContact(&ut_qtcontacts_add::onContactFoundThenRemoveAndStop); |
|
227 } |
|
228 |
|
229 void ut_qtcontacts_add::onContactFoundThenRemoveAndStop() |
|
230 { |
|
231 //qDebug() << "debug: ut_qtcontacts_add::onContactFoundThenRemoveAndStop"; |
|
232 QContactManager* manager = getContactManager(); |
|
233 Q_ASSERT(manager); |
|
234 manager->removeContact(contact.localId()); |
|
235 |
|
236 // Allow the actual test function to return: |
|
237 waiting = false; |
|
238 } |
|
239 |
|
240 void ut_qtcontacts_add::checkSubTypes() |
|
241 { |
|
242 QContact contact; |
|
243 |
|
244 QContactManager* manager = getContactManager(); |
|
245 |
|
246 QContactPhoneNumber phone; |
|
247 |
|
248 phone.setSubTypes(QContactPhoneNumber::SubTypeMobile); |
|
249 phone.setContexts(QContactPhoneNumber::ContextHome); |
|
250 phone.setNumber("12345"); |
|
251 |
|
252 contact.saveDetail(&phone); |
|
253 manager->saveContact(&contact); |
|
254 |
|
255 phone.setSubTypes(QContactPhoneNumber::SubTypeFacsimile); |
|
256 |
|
257 contact.saveDetail(&phone); |
|
258 manager->saveContact(&contact); |
|
259 |
|
260 } |
|
261 |
|
262 QTEST_MAIN(ut_qtcontacts_add) |
|