|
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 test suite of the Qt Toolkit. |
|
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 #include <QtCore/QObject> |
|
42 #include <QtCore/QVariant> |
|
43 #include <QtCore/QList> |
|
44 #include <QtCore/QVector> |
|
45 #include <QtTest/QtTest> |
|
46 #ifndef QT_NO_DBUS |
|
47 #include <QtDBus> |
|
48 |
|
49 #define TEST_INTERFACE_NAME "com.trolltech.QtDBus.MyObject" |
|
50 |
|
51 class MyObject : public QDBusAbstractAdaptor |
|
52 { |
|
53 Q_OBJECT |
|
54 Q_CLASSINFO("D-Bus Interface", "com.trolltech.QtDBus.MyObject") |
|
55 |
|
56 public: |
|
57 MyObject(QObject* parent =0) |
|
58 : QDBusAbstractAdaptor(parent) |
|
59 {} |
|
60 |
|
61 public slots: |
|
62 QStringList returnFoo() const |
|
63 { return QStringList() << QString::fromLatin1("foo"); } |
|
64 |
|
65 void returnError(const QDBusMessage &msg) const |
|
66 { |
|
67 msg.setDelayedReply(true); |
|
68 QDBusConnection::sessionBus().send(msg.createErrorReply("dbuspendingcall_error", "")); |
|
69 } |
|
70 }; |
|
71 |
|
72 class tst_QDBusPendingCall: public QObject |
|
73 { |
|
74 Q_OBJECT |
|
75 |
|
76 public: |
|
77 tst_QDBusPendingCall(); |
|
78 |
|
79 public Q_SLOTS: |
|
80 void callback(const QStringList &list); |
|
81 void errorCallback(const QDBusError &error); |
|
82 void finished(QDBusPendingCallWatcher *call); |
|
83 void makeCall(); |
|
84 |
|
85 private Q_SLOTS: |
|
86 void initTestCase(); |
|
87 void waitForFinished(); |
|
88 void waitForFinished_error(); |
|
89 // void setReplyCallback(); |
|
90 void watcher(); |
|
91 void watcher_error(); |
|
92 void watcher_waitForFinished(); |
|
93 void watcher_waitForFinished_alreadyFinished(); |
|
94 void watcher_waitForFinished_alreadyFinished_eventLoop(); |
|
95 void watcher_waitForFinished_error(); |
|
96 void callInsideWaitForFinished(); |
|
97 |
|
98 void callWithCallback_localLoop(); |
|
99 void callWithCallback_localLoop_errorReply(); |
|
100 |
|
101 private: |
|
102 QDBusPendingCall sendMessage(); |
|
103 QDBusPendingCall sendError(); |
|
104 |
|
105 QDBusConnection conn; |
|
106 |
|
107 enum { CallbackCalled, ErrorCallbackCalled, FinishCalled, MakeCallCalled }; |
|
108 int slotCalled; |
|
109 int callCount; |
|
110 QStringList callbackArgument; |
|
111 QDBusError errorArgument; |
|
112 QDBusPendingCallWatcher *watchArgument; |
|
113 MyObject *obj; |
|
114 }; |
|
115 |
|
116 tst_QDBusPendingCall::tst_QDBusPendingCall() |
|
117 : conn(QDBusConnection::sessionBus()) |
|
118 , obj(new MyObject(this)) |
|
119 { |
|
120 } |
|
121 |
|
122 void tst_QDBusPendingCall::finished(QDBusPendingCallWatcher *call) |
|
123 { |
|
124 slotCalled = FinishCalled; |
|
125 ++callCount; |
|
126 watchArgument = call; |
|
127 QTestEventLoop::instance().exitLoop(); |
|
128 } |
|
129 |
|
130 void tst_QDBusPendingCall::callback(const QStringList &list) |
|
131 { |
|
132 slotCalled = CallbackCalled; |
|
133 ++callCount; |
|
134 callbackArgument = list; |
|
135 QTestEventLoop::instance().exitLoop(); |
|
136 } |
|
137 |
|
138 void tst_QDBusPendingCall::errorCallback(const QDBusError &error) |
|
139 { |
|
140 slotCalled = ErrorCallbackCalled; |
|
141 ++callCount; |
|
142 errorArgument = error; |
|
143 QTestEventLoop::instance().exitLoop(); |
|
144 } |
|
145 |
|
146 void tst_QDBusPendingCall::makeCall() |
|
147 { |
|
148 // make an external call to D-Bus to make sure we haven't left any locks |
|
149 slotCalled = MakeCallCalled; |
|
150 ++callCount; |
|
151 |
|
152 sendMessage().waitForFinished(); |
|
153 } |
|
154 |
|
155 void tst_QDBusPendingCall::initTestCase() |
|
156 { |
|
157 QVERIFY(conn.isConnected()); |
|
158 QVERIFY(conn.registerObject("/", this)); |
|
159 } |
|
160 |
|
161 QDBusPendingCall tst_QDBusPendingCall::sendMessage() |
|
162 { |
|
163 QDBusMessage msg = QDBusMessage::createMethodCall("org.freedesktop.DBus", |
|
164 "/", |
|
165 "org.freedesktop.DBus", |
|
166 "ListNames"); |
|
167 return conn.asyncCall(msg); |
|
168 } |
|
169 |
|
170 QDBusPendingCall tst_QDBusPendingCall::sendError() |
|
171 { |
|
172 QDBusMessage msg = QDBusMessage::createMethodCall("org.freedesktop.DBus", |
|
173 "/", |
|
174 "org.freedesktop.DBus", |
|
175 "ThisNameWontExist"); |
|
176 return conn.asyncCall(msg); |
|
177 } |
|
178 |
|
179 void tst_QDBusPendingCall::waitForFinished() |
|
180 { |
|
181 QDBusPendingCall ac = sendMessage(); |
|
182 QVERIFY(!ac.isFinished()); |
|
183 QVERIFY(!ac.isError()); |
|
184 QVERIFY(ac.reply().type() == QDBusMessage::InvalidMessage); |
|
185 |
|
186 ac.waitForFinished(); |
|
187 QVERIFY(ac.isFinished()); |
|
188 QVERIFY(!ac.isError()); |
|
189 |
|
190 const QDBusMessage reply = ac.reply(); |
|
191 QVERIFY(reply.type() == QDBusMessage::ReplyMessage); |
|
192 QCOMPARE(reply.signature(), QString("as")); |
|
193 |
|
194 const QVariantList args = ac.reply().arguments(); |
|
195 QCOMPARE(args.count(), 1); |
|
196 |
|
197 const QVariant &arg = args.at(0); |
|
198 QCOMPARE(arg.type(), QVariant::StringList); |
|
199 QVERIFY(arg.toStringList().contains(conn.baseService())); |
|
200 } |
|
201 |
|
202 void tst_QDBusPendingCall::waitForFinished_error() |
|
203 { |
|
204 QDBusPendingCall ac = sendError(); |
|
205 QVERIFY(!ac.isFinished()); |
|
206 QVERIFY(!ac.isError()); |
|
207 QVERIFY(ac.reply().type() == QDBusMessage::InvalidMessage); |
|
208 |
|
209 ac.waitForFinished(); |
|
210 QVERIFY(ac.isFinished()); |
|
211 QVERIFY(ac.isError()); |
|
212 |
|
213 QDBusError error = ac.error(); |
|
214 QVERIFY(error.isValid()); |
|
215 QCOMPARE(error.name(), QString("org.freedesktop.DBus.Error.UnknownMethod")); |
|
216 QCOMPARE(error.type(), QDBusError::UnknownMethod); |
|
217 } |
|
218 |
|
219 void tst_QDBusPendingCall::callWithCallback_localLoop() |
|
220 { |
|
221 // Verify that a callback actually gets called when the call is dispatched locally. |
|
222 QDBusInterface iface(QDBusConnection::sessionBus().baseService(), QLatin1String("/"), |
|
223 TEST_INTERFACE_NAME); |
|
224 QVERIFY(iface.isValid()); |
|
225 |
|
226 QVERIFY(iface.callWithCallback("returnFoo", QVariantList(), this, SLOT(callback(QStringList)))); |
|
227 |
|
228 // May be called synchronously or asynchronously... |
|
229 if (callbackArgument != (QStringList() << QString::fromLatin1("foo"))) { |
|
230 QTestEventLoop::instance().enterLoop(2); |
|
231 QVERIFY(!QTestEventLoop::instance().timeout()); |
|
232 } |
|
233 |
|
234 QCOMPARE(callbackArgument, QStringList() << QString::fromLatin1("foo")); |
|
235 } |
|
236 |
|
237 void tst_QDBusPendingCall::callWithCallback_localLoop_errorReply() |
|
238 { |
|
239 // Verify that an error callback actually gets called when the call is |
|
240 // dispatched locally and the called method returns an error |
|
241 |
|
242 QDBusInterface iface(QDBusConnection::sessionBus().baseService(), QLatin1String("/"), |
|
243 TEST_INTERFACE_NAME); |
|
244 QVERIFY(iface.isValid()); |
|
245 |
|
246 callbackArgument.clear(); |
|
247 |
|
248 QVERIFY(iface.callWithCallback("returnError", QVariantList(), this, |
|
249 SLOT(callback(QStringList)), SLOT(errorCallback(QDBusError)))); |
|
250 |
|
251 // May be called synchronously or asynchronously... |
|
252 if (errorArgument.name() != "dbuspendingcall_error") { |
|
253 QTestEventLoop::instance().enterLoop(2); |
|
254 QVERIFY(!QTestEventLoop::instance().timeout()); |
|
255 } |
|
256 |
|
257 QCOMPARE(errorArgument.name(), QString::fromLatin1("dbuspendingcall_error")); |
|
258 QVERIFY(callbackArgument.isEmpty()); |
|
259 } |
|
260 |
|
261 #if 0 |
|
262 // This function was removed from the public API |
|
263 void tst_QDBusPendingCall::setReplyCallback() |
|
264 { |
|
265 QDBusPendingCall ac = sendMessage(); |
|
266 QVERIFY(!ac.isFinished()); |
|
267 QVERIFY(!ac.isError()); |
|
268 QVERIFY(ac.reply().type() == QDBusMessage::InvalidMessage); |
|
269 |
|
270 callCount = 0; |
|
271 callbackArgument.clear(); |
|
272 QVERIFY(ac.setReplyCallback(this, SLOT(callback(const QStringList &)))); |
|
273 QVERIFY(callCount == 0); |
|
274 QVERIFY(callbackArgument.isEmpty()); |
|
275 |
|
276 QTestEventLoop::instance().enterLoop(2); |
|
277 QVERIFY(!QTestEventLoop::instance().timeout()); |
|
278 |
|
279 QVERIFY(ac.isFinished()); |
|
280 QVERIFY(!ac.isError()); |
|
281 |
|
282 QCOMPARE(callCount, 1); |
|
283 QCOMPARE(slotCalled, (int)CallbackCalled); |
|
284 QVERIFY(!callbackArgument.isEmpty()); |
|
285 QVERIFY(callbackArgument.contains(conn.baseService())); |
|
286 |
|
287 const QVariantList args = ac.reply().arguments(); |
|
288 QVERIFY(!args.isEmpty()); |
|
289 QCOMPARE(args.at(0).toStringList(), callbackArgument); |
|
290 } |
|
291 #endif |
|
292 |
|
293 void tst_QDBusPendingCall::watcher() |
|
294 { |
|
295 QDBusPendingCall ac = sendMessage(); |
|
296 QVERIFY(!ac.isFinished()); |
|
297 QVERIFY(!ac.isError()); |
|
298 QVERIFY(ac.reply().type() == QDBusMessage::InvalidMessage); |
|
299 |
|
300 callCount = 0; |
|
301 watchArgument = 0; |
|
302 |
|
303 QDBusPendingCallWatcher watch(ac); |
|
304 connect(&watch, SIGNAL(finished(QDBusPendingCallWatcher*)), |
|
305 SLOT(finished(QDBusPendingCallWatcher*))); |
|
306 |
|
307 QTestEventLoop::instance().enterLoop(2); |
|
308 QVERIFY(!QTestEventLoop::instance().timeout()); |
|
309 |
|
310 QVERIFY(ac.isFinished()); |
|
311 QVERIFY(!ac.isError()); |
|
312 |
|
313 QCOMPARE(callCount, 1); |
|
314 QCOMPARE(slotCalled, (int)FinishCalled); |
|
315 QCOMPARE(watchArgument, &watch); |
|
316 QVERIFY(!watch.isError()); |
|
317 |
|
318 const QVariantList args2 = ac.reply().arguments(); |
|
319 QVERIFY(!args2.isEmpty()); |
|
320 QVERIFY(args2.at(0).toStringList().contains(conn.baseService())); |
|
321 } |
|
322 |
|
323 void tst_QDBusPendingCall::watcher_error() |
|
324 { |
|
325 QDBusPendingCall ac = sendError(); |
|
326 QVERIFY(!ac.isFinished()); |
|
327 QVERIFY(!ac.isError()); |
|
328 QVERIFY(ac.reply().type() == QDBusMessage::InvalidMessage); |
|
329 |
|
330 callCount = 0; |
|
331 watchArgument = 0; |
|
332 |
|
333 QDBusPendingCallWatcher watch(ac); |
|
334 connect(&watch, SIGNAL(finished(QDBusPendingCallWatcher*)), |
|
335 SLOT(finished(QDBusPendingCallWatcher*))); |
|
336 |
|
337 QTestEventLoop::instance().enterLoop(2); |
|
338 QVERIFY(!QTestEventLoop::instance().timeout()); |
|
339 |
|
340 QVERIFY(ac.isFinished()); |
|
341 QVERIFY(ac.isError()); |
|
342 |
|
343 QCOMPARE(callCount, 1); |
|
344 QCOMPARE(slotCalled, (int)FinishCalled); |
|
345 QCOMPARE(watchArgument, &watch); |
|
346 |
|
347 QVERIFY(watch.isError()); |
|
348 QVERIFY(watch.error().isValid()); |
|
349 } |
|
350 |
|
351 void tst_QDBusPendingCall::watcher_waitForFinished() |
|
352 { |
|
353 QDBusPendingCall ac = sendMessage(); |
|
354 QVERIFY(!ac.isFinished()); |
|
355 QVERIFY(!ac.isError()); |
|
356 QVERIFY(ac.reply().type() == QDBusMessage::InvalidMessage); |
|
357 |
|
358 callCount = 0; |
|
359 watchArgument = 0; |
|
360 |
|
361 QDBusPendingCallWatcher watch(ac); |
|
362 connect(&watch, SIGNAL(finished(QDBusPendingCallWatcher*)), |
|
363 SLOT(finished(QDBusPendingCallWatcher*))); |
|
364 |
|
365 watch.waitForFinished(); |
|
366 |
|
367 QVERIFY(ac.isFinished()); |
|
368 QVERIFY(!ac.isError()); |
|
369 |
|
370 QCOMPARE(callCount, 1); |
|
371 QCOMPARE(slotCalled, (int)FinishCalled); |
|
372 QCOMPARE(watchArgument, &watch); |
|
373 QVERIFY(!watch.isError()); |
|
374 |
|
375 const QVariantList args2 = ac.reply().arguments(); |
|
376 QVERIFY(!args2.isEmpty()); |
|
377 QVERIFY(args2.at(0).toStringList().contains(conn.baseService())); |
|
378 } |
|
379 |
|
380 void tst_QDBusPendingCall::watcher_waitForFinished_alreadyFinished() |
|
381 { |
|
382 QDBusPendingCall ac = sendMessage(); |
|
383 QVERIFY(!ac.isFinished()); |
|
384 QVERIFY(!ac.isError()); |
|
385 QVERIFY(ac.reply().type() == QDBusMessage::InvalidMessage); |
|
386 |
|
387 ac.waitForFinished(); |
|
388 QVERIFY(ac.isFinished()); |
|
389 QVERIFY(!ac.isError()); |
|
390 |
|
391 callCount = 0; |
|
392 watchArgument = 0; |
|
393 |
|
394 // create a watcher on an already-finished reply |
|
395 QDBusPendingCallWatcher watch(ac); |
|
396 connect(&watch, SIGNAL(finished(QDBusPendingCallWatcher*)), |
|
397 SLOT(finished(QDBusPendingCallWatcher*))); |
|
398 |
|
399 watch.waitForFinished(); |
|
400 |
|
401 QVERIFY(ac.isFinished()); |
|
402 QVERIFY(!ac.isError()); |
|
403 |
|
404 QCOMPARE(callCount, 1); |
|
405 QCOMPARE(slotCalled, (int)FinishCalled); |
|
406 QCOMPARE(watchArgument, &watch); |
|
407 QVERIFY(!watch.isError()); |
|
408 |
|
409 const QVariantList args2 = ac.reply().arguments(); |
|
410 QVERIFY(!args2.isEmpty()); |
|
411 QVERIFY(args2.at(0).toStringList().contains(conn.baseService())); |
|
412 } |
|
413 |
|
414 void tst_QDBusPendingCall::watcher_waitForFinished_alreadyFinished_eventLoop() |
|
415 { |
|
416 QDBusPendingCall ac = sendMessage(); |
|
417 QVERIFY(!ac.isFinished()); |
|
418 QVERIFY(!ac.isError()); |
|
419 QVERIFY(ac.reply().type() == QDBusMessage::InvalidMessage); |
|
420 |
|
421 ac.waitForFinished(); |
|
422 QVERIFY(ac.isFinished()); |
|
423 QVERIFY(!ac.isError()); |
|
424 |
|
425 callCount = 0; |
|
426 watchArgument = 0; |
|
427 |
|
428 // create a watcher on an already-finished reply |
|
429 QDBusPendingCallWatcher watch(ac); |
|
430 connect(&watch, SIGNAL(finished(QDBusPendingCallWatcher*)), |
|
431 SLOT(finished(QDBusPendingCallWatcher*))); |
|
432 connect(&watch, SIGNAL(finished(QDBusPendingCallWatcher*)), |
|
433 &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
434 |
|
435 QTestEventLoop::instance().enterLoop(1); |
|
436 QVERIFY(!QTestEventLoop::instance().timeout()); |
|
437 |
|
438 QVERIFY(ac.isFinished()); |
|
439 QVERIFY(!ac.isError()); |
|
440 |
|
441 QCOMPARE(callCount, 1); |
|
442 QCOMPARE(slotCalled, (int)FinishCalled); |
|
443 QCOMPARE(watchArgument, &watch); |
|
444 QVERIFY(!watch.isError()); |
|
445 |
|
446 const QVariantList args2 = ac.reply().arguments(); |
|
447 QVERIFY(!args2.isEmpty()); |
|
448 QVERIFY(args2.at(0).toStringList().contains(conn.baseService())); |
|
449 } |
|
450 |
|
451 void tst_QDBusPendingCall::watcher_waitForFinished_error() |
|
452 { |
|
453 QDBusPendingCall ac = sendError(); |
|
454 QVERIFY(!ac.isFinished()); |
|
455 QVERIFY(!ac.isError()); |
|
456 QVERIFY(ac.reply().type() == QDBusMessage::InvalidMessage); |
|
457 |
|
458 callCount = 0; |
|
459 watchArgument = 0; |
|
460 |
|
461 QDBusPendingCallWatcher watch(ac); |
|
462 connect(&watch, SIGNAL(finished(QDBusPendingCallWatcher*)), |
|
463 SLOT(finished(QDBusPendingCallWatcher*))); |
|
464 |
|
465 watch.waitForFinished(); |
|
466 |
|
467 QVERIFY(ac.isFinished()); |
|
468 QVERIFY(ac.isError()); |
|
469 |
|
470 QCOMPARE(callCount, 1); |
|
471 QCOMPARE(slotCalled, (int)FinishCalled); |
|
472 QCOMPARE(watchArgument, &watch); |
|
473 |
|
474 QVERIFY(watch.isError()); |
|
475 QVERIFY(watch.error().isValid()); |
|
476 } |
|
477 |
|
478 void tst_QDBusPendingCall::callInsideWaitForFinished() |
|
479 { |
|
480 QDBusPendingCall ac = sendMessage(); |
|
481 QDBusPendingCallWatcher watch(ac); |
|
482 |
|
483 callCount = 0; |
|
484 |
|
485 connect(&watch, SIGNAL(finished(QDBusPendingCallWatcher*)), |
|
486 SLOT(makeCall())); |
|
487 |
|
488 watch.waitForFinished(); |
|
489 |
|
490 QCOMPARE(callCount, 1); |
|
491 QCOMPARE(slotCalled, (int)MakeCallCalled); |
|
492 QVERIFY(!watch.isError()); |
|
493 |
|
494 const QVariantList args2 = ac.reply().arguments(); |
|
495 QVERIFY(!args2.isEmpty()); |
|
496 QVERIFY(args2.at(0).toStringList().contains(conn.baseService())); |
|
497 } |
|
498 |
|
499 QTEST_MAIN(tst_QDBusPendingCall) |
|
500 #include "tst_qdbuspendingcall.moc" |
|
501 #else |
|
502 QTEST_NOOP_MAIN |
|
503 #endif |