author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 04 Oct 2010 01:19:32 +0300 | |
changeset 37 | 758a864f9613 |
parent 18 | 2f34d5167611 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 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 <QtTest> |
|
42 |
#include <QtDBus> |
|
43 |
#include <QtCore/QVarLengthArray> |
|
44 |
#include <QtCore/QThread> |
|
45 |
#include <QtCore/QObject> |
|
46 |
#include <QtCore/QSemaphore> |
|
47 |
#include <QtCore/QMutex> |
|
48 |
#include <QtCore/QWaitCondition> |
|
49 |
#include <QtCore/QMap> |
|
50 |
||
51 |
class Thread : public QThread |
|
52 |
{ |
|
53 |
Q_OBJECT |
|
54 |
static int counter; |
|
55 |
public: |
|
56 |
Thread(bool automatic = true); |
|
57 |
void run(); |
|
58 |
||
59 |
using QThread::exec; |
|
60 |
}; |
|
61 |
int Thread::counter; |
|
62 |
||
63 |
class tst_QDBusThreading : public QObject |
|
64 |
{ |
|
65 |
Q_OBJECT |
|
66 |
static tst_QDBusThreading *_self; |
|
67 |
QAtomicInt threadJoinCount; |
|
68 |
QSemaphore threadJoin; |
|
69 |
public: |
|
70 |
QSemaphore sem1, sem2; |
|
71 |
volatile bool success; |
|
72 |
QEventLoop *loop; |
|
37
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
73 |
enum FunctionSpy { |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
74 |
NoMethod = 0, |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
75 |
Adaptor_method, |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
76 |
Object_method |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
77 |
} functionSpy; |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
78 |
|
0 | 79 |
QThread *threadSpy; |
80 |
int signalSpy; |
|
81 |
||
82 |
tst_QDBusThreading(); |
|
83 |
static inline tst_QDBusThreading *self() { return _self; } |
|
84 |
||
85 |
void joinThreads(); |
|
86 |
bool waitForSignal(QObject *obj, const char *signal, int delay = 1); |
|
87 |
||
88 |
public Q_SLOTS: |
|
89 |
void cleanup(); |
|
90 |
void signalSpySlot() { ++signalSpy; } |
|
91 |
void threadStarted() { threadJoinCount.ref(); } |
|
92 |
void threadFinished() { threadJoin.release(); } |
|
93 |
||
94 |
void dyingThread_thread(); |
|
95 |
void lastInstanceInOtherThread_thread(); |
|
96 |
void concurrentCreation_thread(); |
|
97 |
void disconnectAnothersConnection_thread(); |
|
98 |
void accessMainsConnection_thread(); |
|
99 |
void accessOthersConnection_thread(); |
|
100 |
void registerObjectInOtherThread_thread(); |
|
101 |
void registerAdaptorInOtherThread_thread(); |
|
102 |
void callbackInMainThread_thread(); |
|
103 |
void callbackInAuxThread_thread(); |
|
104 |
void callbackInAnotherAuxThread_thread(); |
|
105 |
||
106 |
private Q_SLOTS: |
|
107 |
void initTestCase(); |
|
108 |
void dyingThread(); |
|
109 |
void lastInstanceInOtherThread(); |
|
110 |
void concurrentCreation(); |
|
111 |
void disconnectAnothersConnection(); |
|
112 |
void accessMainsConnection(); |
|
113 |
void accessOthersConnection(); |
|
114 |
void registerObjectInOtherThread(); |
|
115 |
void registerAdaptorInOtherThread(); |
|
116 |
void callbackInMainThread(); |
|
117 |
void callbackInAuxThread(); |
|
118 |
void callbackInAnotherAuxThread(); |
|
119 |
}; |
|
120 |
tst_QDBusThreading *tst_QDBusThreading::_self; |
|
121 |
||
122 |
class Adaptor : public QDBusAbstractAdaptor |
|
123 |
{ |
|
124 |
Q_OBJECT |
|
125 |
Q_CLASSINFO("D-Bus Interface", "local.Adaptor") |
|
126 |
public: |
|
127 |
Adaptor(QObject *parent) |
|
128 |
: QDBusAbstractAdaptor(parent) |
|
129 |
{ |
|
130 |
} |
|
131 |
||
132 |
public Q_SLOTS: |
|
133 |
void method() |
|
134 |
{ |
|
37
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
135 |
tst_QDBusThreading::self()->functionSpy = tst_QDBusThreading::Adaptor_method; |
0 | 136 |
tst_QDBusThreading::self()->threadSpy = QThread::currentThread(); |
137 |
emit signal(); |
|
138 |
} |
|
139 |
||
140 |
Q_SIGNALS: |
|
141 |
void signal(); |
|
142 |
}; |
|
143 |
||
144 |
class Object : public QObject |
|
145 |
{ |
|
146 |
Q_OBJECT |
|
147 |
Q_CLASSINFO("D-Bus Interface", "local.Object") |
|
148 |
public: |
|
149 |
Object(bool useAdaptor) |
|
150 |
{ |
|
151 |
if (useAdaptor) |
|
152 |
new Adaptor(this); |
|
153 |
} |
|
154 |
||
155 |
~Object() |
|
156 |
{ |
|
157 |
QMetaObject::invokeMethod(QThread::currentThread(), "quit", Qt::QueuedConnection); |
|
158 |
} |
|
159 |
||
160 |
public Q_SLOTS: |
|
161 |
void method() |
|
162 |
{ |
|
37
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
163 |
tst_QDBusThreading::self()->functionSpy = tst_QDBusThreading::Object_method; |
0 | 164 |
tst_QDBusThreading::self()->threadSpy = QThread::currentThread(); |
165 |
emit signal(); |
|
166 |
deleteLater(); |
|
167 |
} |
|
168 |
||
169 |
Q_SIGNALS: |
|
170 |
void signal(); |
|
171 |
}; |
|
172 |
||
173 |
#if 0 |
|
174 |
typedef void (*qdbusThreadDebugFunc)(int, int, QDBusConnectionPrivate *); |
|
175 |
QDBUS_EXPORT void qdbusDefaultThreadDebug(int, int, QDBusConnectionPrivate *); |
|
176 |
extern QDBUS_EXPORT qdbusThreadDebugFunc qdbusThreadDebug; |
|
177 |
||
178 |
static void threadDebug(int action, int condition, QDBusConnectionPrivate *p) |
|
179 |
{ |
|
180 |
qdbusDefaultThreadDebug(action, condition, p); |
|
181 |
} |
|
182 |
#endif |
|
183 |
||
184 |
Thread::Thread(bool automatic) |
|
185 |
{ |
|
186 |
setObjectName(QString::fromLatin1("Aux thread %1").arg(++counter)); |
|
187 |
connect(this, SIGNAL(started()), tst_QDBusThreading::self(), SLOT(threadStarted())); |
|
188 |
connect(this, SIGNAL(finished()), tst_QDBusThreading::self(), SLOT(threadFinished()), |
|
189 |
Qt::DirectConnection); |
|
190 |
connect(this, SIGNAL(finished()), this, SLOT(deleteLater()), Qt::DirectConnection); |
|
191 |
if (automatic) |
|
192 |
start(); |
|
193 |
} |
|
194 |
||
195 |
void Thread::run() |
|
196 |
{ |
|
197 |
QVarLengthArray<char, 56> name; |
|
198 |
name.append(QTest::currentTestFunction(), qstrlen(QTest::currentTestFunction())); |
|
199 |
name.append("_thread", sizeof "_thread"); |
|
200 |
QMetaObject::invokeMethod(tst_QDBusThreading::self(), name.constData(), Qt::DirectConnection); |
|
201 |
} |
|
202 |
||
203 |
static const char myConnectionName[] = "connection"; |
|
204 |
||
205 |
tst_QDBusThreading::tst_QDBusThreading() |
|
37
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
206 |
: loop(0), functionSpy(NoMethod), threadSpy(0) |
0 | 207 |
{ |
208 |
_self = this; |
|
209 |
QCoreApplication::instance()->thread()->setObjectName("Main thread"); |
|
210 |
} |
|
211 |
||
212 |
void tst_QDBusThreading::joinThreads() |
|
213 |
{ |
|
214 |
threadJoin.acquire(threadJoinCount); |
|
215 |
threadJoinCount = 0; |
|
216 |
} |
|
217 |
||
218 |
bool tst_QDBusThreading::waitForSignal(QObject *obj, const char *signal, int delay) |
|
219 |
{ |
|
220 |
QObject::connect(obj, signal, &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
221 |
QPointer<QObject> safe = obj; |
|
222 |
||
223 |
QTestEventLoop::instance().enterLoop(delay); |
|
224 |
if (!safe.isNull()) |
|
225 |
QObject::disconnect(safe, signal, &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
226 |
return QTestEventLoop::instance().timeout(); |
|
227 |
} |
|
228 |
||
229 |
void tst_QDBusThreading::cleanup() |
|
230 |
{ |
|
231 |
joinThreads(); |
|
232 |
||
233 |
if (sem1.available()) |
|
234 |
sem1.acquire(sem1.available()); |
|
235 |
if (sem2.available()) |
|
236 |
sem2.acquire(sem2.available()); |
|
237 |
||
238 |
if (QDBusConnection(myConnectionName).isConnected()) |
|
239 |
QDBusConnection::disconnectFromBus(myConnectionName); |
|
240 |
||
241 |
delete loop; |
|
242 |
loop = 0; |
|
243 |
||
244 |
QTest::qWait(500); |
|
245 |
} |
|
246 |
||
247 |
void tst_QDBusThreading::initTestCase() |
|
248 |
{ |
|
249 |
} |
|
250 |
||
251 |
void tst_QDBusThreading::dyingThread_thread() |
|
252 |
{ |
|
253 |
QDBusConnection::connectToBus(QDBusConnection::SessionBus, myConnectionName); |
|
254 |
} |
|
255 |
||
256 |
void tst_QDBusThreading::dyingThread() |
|
257 |
{ |
|
258 |
Thread *th = new Thread(false); |
|
259 |
QTestEventLoop::instance().connect(th, SIGNAL(destroyed(QObject*)), SLOT(exitLoop())); |
|
260 |
th->start(); |
|
261 |
||
262 |
QTestEventLoop::instance().enterLoop(10); |
|
263 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
264 |
||
265 |
QDBusConnection con(myConnectionName); |
|
266 |
QDBusConnection::disconnectFromBus(myConnectionName); |
|
267 |
||
268 |
QVERIFY(con.isConnected()); |
|
269 |
QDBusReply<QStringList> reply = con.interface()->registeredServiceNames(); |
|
270 |
QVERIFY(reply.isValid()); |
|
271 |
QVERIFY(!reply.value().isEmpty()); |
|
272 |
QVERIFY(reply.value().contains(con.baseService())); |
|
273 |
||
274 |
con.interface()->callWithCallback("ListNames", QVariantList(), |
|
275 |
&QTestEventLoop::instance(), SLOT(exitLoop())); |
|
276 |
||
277 |
QTestEventLoop::instance().enterLoop(1); |
|
278 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
279 |
} |
|
280 |
||
281 |
void tst_QDBusThreading::lastInstanceInOtherThread_thread() |
|
282 |
{ |
|
283 |
QDBusConnection con(myConnectionName); |
|
284 |
QVERIFY(con.isConnected()); |
|
285 |
||
286 |
QDBusConnection::disconnectFromBus(myConnectionName); |
|
287 |
||
288 |
// con is being destroyed in the wrong thread |
|
289 |
} |
|
290 |
||
291 |
void tst_QDBusThreading::lastInstanceInOtherThread() |
|
292 |
{ |
|
293 |
Thread *th = new Thread(false); |
|
294 |
// create the connection: |
|
295 |
QDBusConnection::connectToBus(QDBusConnection::SessionBus, myConnectionName); |
|
296 |
||
297 |
th->start(); |
|
298 |
th->wait(); |
|
299 |
} |
|
300 |
||
301 |
void tst_QDBusThreading::concurrentCreation_thread() |
|
302 |
{ |
|
303 |
sem1.acquire(); |
|
304 |
QDBusConnection con = QDBusConnection::connectToBus(QDBusConnection::SessionBus, |
|
305 |
myConnectionName); |
|
306 |
sem2.release(); |
|
307 |
} |
|
308 |
||
309 |
void tst_QDBusThreading::concurrentCreation() |
|
310 |
{ |
|
311 |
Thread *th = new Thread; |
|
312 |
||
313 |
{ |
|
314 |
sem1.release(); |
|
315 |
QDBusConnection con = QDBusConnection::connectToBus(QDBusConnection::SessionBus, |
|
316 |
myConnectionName); |
|
317 |
QVERIFY(con.isConnected()); |
|
318 |
sem2.acquire(); |
|
319 |
} |
|
320 |
waitForSignal(th, SIGNAL(finished())); |
|
321 |
QDBusConnection::disconnectFromBus(myConnectionName); |
|
322 |
||
323 |
QVERIFY(!QDBusConnection(myConnectionName).isConnected()); |
|
324 |
} |
|
325 |
||
326 |
void tst_QDBusThreading::disconnectAnothersConnection_thread() |
|
327 |
{ |
|
328 |
QDBusConnection con = QDBusConnection::connectToBus(QDBusConnection::SessionBus, |
|
329 |
myConnectionName); |
|
330 |
sem2.release(); |
|
331 |
} |
|
332 |
||
333 |
void tst_QDBusThreading::disconnectAnothersConnection() |
|
334 |
{ |
|
335 |
new Thread; |
|
336 |
sem2.acquire(); |
|
337 |
||
338 |
QVERIFY(QDBusConnection(myConnectionName).isConnected()); |
|
339 |
QDBusConnection::disconnectFromBus(myConnectionName); |
|
340 |
} |
|
341 |
||
342 |
void tst_QDBusThreading::accessMainsConnection_thread() |
|
343 |
{ |
|
344 |
sem1.acquire(); |
|
345 |
QDBusConnection con = QDBusConnection::sessionBus(); |
|
346 |
con.interface()->registeredServiceNames(); |
|
347 |
sem2.release(); |
|
348 |
} |
|
349 |
||
350 |
void tst_QDBusThreading::accessMainsConnection() |
|
351 |
{ |
|
352 |
QVERIFY(QDBusConnection::sessionBus().isConnected()); |
|
353 |
||
354 |
new Thread; |
|
355 |
sem1.release(); |
|
356 |
sem2.acquire(); |
|
357 |
}; |
|
358 |
||
359 |
void tst_QDBusThreading::accessOthersConnection_thread() |
|
360 |
{ |
|
361 |
QDBusConnection::connectToBus(QDBusConnection::SessionBus, myConnectionName); |
|
362 |
sem2.release(); |
|
363 |
||
364 |
// wait for main thread to be done |
|
365 |
sem1.acquire(); |
|
366 |
QDBusConnection::disconnectFromBus(myConnectionName); |
|
367 |
sem2.release(); |
|
368 |
} |
|
369 |
||
370 |
void tst_QDBusThreading::accessOthersConnection() |
|
371 |
{ |
|
372 |
new Thread; |
|
373 |
||
374 |
// wait for the connection to be created |
|
375 |
sem2.acquire(); |
|
376 |
||
377 |
{ |
|
378 |
QDBusConnection con(myConnectionName); |
|
379 |
QVERIFY(con.isConnected()); |
|
380 |
QVERIFY(con.baseService() != QDBusConnection::sessionBus().baseService()); |
|
381 |
||
382 |
QDBusReply<QStringList> reply = con.interface()->registeredServiceNames(); |
|
383 |
if (!reply.isValid()) |
|
384 |
qDebug() << reply.error().name() << reply.error().message(); |
|
385 |
QVERIFY(reply.isValid()); |
|
386 |
QVERIFY(!reply.value().isEmpty()); |
|
387 |
QVERIFY(reply.value().contains(con.baseService())); |
|
388 |
QVERIFY(reply.value().contains(QDBusConnection::sessionBus().baseService())); |
|
389 |
} |
|
390 |
||
391 |
// tell it to destroy: |
|
392 |
sem1.release(); |
|
393 |
sem2.acquire(); |
|
394 |
||
395 |
QDBusConnection con(myConnectionName); |
|
396 |
QVERIFY(!con.isConnected()); |
|
397 |
} |
|
398 |
||
399 |
void tst_QDBusThreading::registerObjectInOtherThread_thread() |
|
400 |
{ |
|
401 |
{ |
|
402 |
Object *obj = new Object(false); |
|
403 |
QDBusConnection::sessionBus().registerObject("/", obj, QDBusConnection::ExportAllSlots | QDBusConnection::ExportAllSignals); |
|
404 |
||
405 |
sem2.release(); |
|
406 |
static_cast<Thread *>(QThread::currentThread())->exec(); |
|
407 |
} |
|
408 |
||
409 |
sem2.release(); |
|
410 |
} |
|
411 |
||
412 |
void tst_QDBusThreading::registerObjectInOtherThread() |
|
413 |
{ |
|
414 |
QVERIFY(QDBusConnection::sessionBus().isConnected()); |
|
415 |
QThread *th = new Thread; |
|
416 |
sem2.acquire(); |
|
417 |
||
418 |
signalSpy = 0; |
|
419 |
||
420 |
QDBusInterface iface(QDBusConnection::sessionBus().baseService(), "/", "local.Object"); |
|
421 |
QVERIFY(iface.isValid()); |
|
422 |
||
423 |
connect(&iface, SIGNAL(signal()), SLOT(signalSpySlot())); |
|
424 |
||
425 |
QTest::qWait(100); |
|
426 |
QCOMPARE(signalSpy, 0); |
|
427 |
||
37
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
428 |
functionSpy = NoMethod; |
0 | 429 |
threadSpy = 0; |
430 |
QDBusReply<void> reply = iface.call("method"); |
|
431 |
QVERIFY(reply.isValid()); |
|
37
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
432 |
QCOMPARE(functionSpy, Object_method); |
0 | 433 |
QCOMPARE(threadSpy, th); |
434 |
||
435 |
QTest::qWait(100); |
|
436 |
QCOMPARE(signalSpy, 1); |
|
437 |
||
438 |
sem2.acquire(); // the object is gone |
|
37
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
439 |
functionSpy = NoMethod; |
0 | 440 |
threadSpy = 0; |
441 |
reply = iface.call("method"); |
|
442 |
QVERIFY(!reply.isValid()); |
|
37
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
443 |
QCOMPARE(functionSpy, NoMethod); |
0 | 444 |
QCOMPARE(threadSpy, (QThread*)0); |
445 |
} |
|
446 |
||
447 |
void tst_QDBusThreading::registerAdaptorInOtherThread_thread() |
|
448 |
{ |
|
449 |
{ |
|
450 |
Object *obj = new Object(true); |
|
451 |
QDBusConnection::sessionBus().registerObject("/", obj, QDBusConnection::ExportAdaptors | |
|
452 |
QDBusConnection::ExportAllSlots | QDBusConnection::ExportAllSignals); |
|
453 |
||
454 |
sem2.release(); |
|
455 |
static_cast<Thread *>(QThread::currentThread())->exec(); |
|
456 |
} |
|
457 |
||
458 |
sem2.release(); |
|
459 |
} |
|
460 |
||
461 |
void tst_QDBusThreading::registerAdaptorInOtherThread() |
|
462 |
{ |
|
463 |
QVERIFY(QDBusConnection::sessionBus().isConnected()); |
|
464 |
QThread *th = new Thread; |
|
465 |
sem2.acquire(); |
|
466 |
||
467 |
QDBusInterface object(QDBusConnection::sessionBus().baseService(), "/", "local.Object"); |
|
468 |
QDBusInterface adaptor(QDBusConnection::sessionBus().baseService(), "/", "local.Adaptor"); |
|
469 |
QVERIFY(object.isValid()); |
|
470 |
QVERIFY(adaptor.isValid()); |
|
471 |
||
472 |
signalSpy = 0; |
|
473 |
connect(&adaptor, SIGNAL(signal()), SLOT(signalSpySlot())); |
|
474 |
QCOMPARE(signalSpy, 0); |
|
475 |
||
37
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
476 |
functionSpy = NoMethod; |
0 | 477 |
threadSpy = 0; |
478 |
QDBusReply<void> reply = adaptor.call("method"); |
|
479 |
QVERIFY(reply.isValid()); |
|
37
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
480 |
QCOMPARE(functionSpy, Adaptor_method); |
0 | 481 |
QCOMPARE(threadSpy, th); |
482 |
||
483 |
QTest::qWait(100); |
|
484 |
QCOMPARE(signalSpy, 1); |
|
485 |
||
37
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
486 |
functionSpy = NoMethod; |
0 | 487 |
threadSpy = 0; |
488 |
reply = object.call("method"); |
|
489 |
QVERIFY(reply.isValid()); |
|
37
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
490 |
QCOMPARE(functionSpy, Object_method); |
0 | 491 |
QCOMPARE(threadSpy, th); |
492 |
||
493 |
QTest::qWait(100); |
|
494 |
QCOMPARE(signalSpy, 1); |
|
495 |
||
496 |
sem2.acquire(); // the object is gone |
|
37
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
497 |
functionSpy = NoMethod; |
0 | 498 |
threadSpy = 0; |
499 |
reply = adaptor.call("method"); |
|
500 |
QVERIFY(!reply.isValid()); |
|
37
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
501 |
QCOMPARE(functionSpy, NoMethod); |
0 | 502 |
QCOMPARE(threadSpy, (QThread*)0); |
503 |
reply = object.call("method"); |
|
504 |
QVERIFY(!reply.isValid()); |
|
37
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
505 |
QCOMPARE(functionSpy, NoMethod); |
0 | 506 |
QCOMPARE(threadSpy, (QThread*)0); |
507 |
} |
|
508 |
||
509 |
void tst_QDBusThreading::callbackInMainThread_thread() |
|
510 |
{ |
|
511 |
QDBusConnection::connectToBus(QDBusConnection::SessionBus, myConnectionName); |
|
512 |
sem2.release(); |
|
513 |
||
514 |
static_cast<Thread *>(QThread::currentThread())->exec(); |
|
515 |
QDBusConnection::disconnectFromBus(myConnectionName); |
|
516 |
} |
|
517 |
||
518 |
void tst_QDBusThreading::callbackInMainThread() |
|
519 |
{ |
|
520 |
Thread *th = new Thread; |
|
521 |
||
522 |
// wait for it to be connected |
|
523 |
sem2.acquire(); |
|
524 |
||
525 |
QDBusConnection con(myConnectionName); |
|
526 |
con.interface()->callWithCallback("ListNames", QVariantList(), |
|
527 |
&QTestEventLoop::instance(), SLOT(exitLoop())); |
|
528 |
QTestEventLoop::instance().enterLoop(10); |
|
529 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
530 |
||
531 |
QMetaObject::invokeMethod(th, "quit"); |
|
532 |
waitForSignal(th, SIGNAL(finished())); |
|
533 |
} |
|
534 |
||
535 |
void tst_QDBusThreading::callbackInAuxThread_thread() |
|
536 |
{ |
|
537 |
QDBusConnection con(QDBusConnection::sessionBus()); |
|
538 |
QTestEventLoop ownLoop; |
|
539 |
con.interface()->callWithCallback("ListNames", QVariantList(), |
|
540 |
&ownLoop, SLOT(exitLoop())); |
|
541 |
ownLoop.enterLoop(10); |
|
542 |
loop->exit(ownLoop.timeout() ? 1 : 0); |
|
543 |
} |
|
544 |
||
545 |
void tst_QDBusThreading::callbackInAuxThread() |
|
546 |
{ |
|
547 |
QVERIFY(QDBusConnection::sessionBus().isConnected()); |
|
548 |
||
549 |
loop = new QEventLoop; |
|
550 |
||
551 |
new Thread; |
|
552 |
QCOMPARE(loop->exec(), 0); |
|
553 |
} |
|
554 |
||
555 |
void tst_QDBusThreading::callbackInAnotherAuxThread_thread() |
|
556 |
{ |
|
557 |
sem1.acquire(); |
|
558 |
if (!loop) { |
|
559 |
// first thread |
|
560 |
// create the connection and just wait |
|
561 |
QDBusConnection con = QDBusConnection::connectToBus(QDBusConnection::SessionBus, myConnectionName); |
|
562 |
loop = new QEventLoop; |
|
563 |
||
564 |
// tell the main thread we have created the loop and connection |
|
565 |
sem2.release(); |
|
566 |
||
567 |
// wait for the main thread to connect its signal |
|
568 |
sem1.acquire(); |
|
569 |
success = loop->exec() == 0; |
|
570 |
sem2.release(); |
|
571 |
||
572 |
// clean up |
|
573 |
QDBusConnection::disconnectFromBus(myConnectionName); |
|
574 |
} else { |
|
575 |
// second thread |
|
576 |
// try waiting for a message |
|
577 |
QDBusConnection con(myConnectionName); |
|
578 |
QTestEventLoop ownLoop; |
|
579 |
con.interface()->callWithCallback("ListNames", QVariantList(), |
|
580 |
&ownLoop, SLOT(exitLoop())); |
|
581 |
ownLoop.enterLoop(1); |
|
582 |
loop->exit(ownLoop.timeout() ? 1 : 0); |
|
583 |
} |
|
584 |
} |
|
585 |
||
586 |
void tst_QDBusThreading::callbackInAnotherAuxThread() |
|
587 |
{ |
|
588 |
// create first thread |
|
589 |
success = false; |
|
590 |
new Thread; |
|
591 |
||
592 |
// wait for the event loop |
|
593 |
sem1.release(); |
|
594 |
sem2.acquire(); |
|
595 |
Q_ASSERT(loop); |
|
596 |
||
597 |
// create the second thread |
|
598 |
new Thread; |
|
599 |
sem1.release(2); |
|
600 |
||
601 |
// wait for loop thread to finish executing: |
|
602 |
sem2.acquire(); |
|
603 |
||
604 |
QVERIFY(success); |
|
605 |
} |
|
606 |
||
607 |
// Next tests: |
|
608 |
// - unexport an object at the moment the call is being delivered |
|
609 |
// - delete an object at the moment the call is being delivered |
|
610 |
// - keep a global-static QDBusConnection for a thread-created connection |
|
611 |
||
612 |
QTEST_MAIN(tst_QDBusThreading) |
|
613 |
#include "tst_qdbusthreading.moc" |