|
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 /* -*- C++ -*- |
|
42 */ |
|
43 #include <qcoreapplication.h> |
|
44 #include <qmetatype.h> |
|
45 #include <QtTest/QtTest> |
|
46 #include <QtCore/qvariant.h> |
|
47 #include <QtDBus/QtDBus> |
|
48 |
|
49 #include "../qdbusmarshall/common.h" |
|
50 |
|
51 Q_DECLARE_METATYPE(QVariantList) |
|
52 |
|
53 #define TEST_INTERFACE_NAME "com.trolltech.QtDBus.MyObject" |
|
54 #define TEST_SIGNAL_NAME "somethingHappened" |
|
55 |
|
56 class MyObject: public QObject |
|
57 { |
|
58 Q_OBJECT |
|
59 Q_CLASSINFO("D-Bus Interface", "com.trolltech.QtDBus.MyObject") |
|
60 Q_CLASSINFO("D-Bus Introspection", "" |
|
61 " <interface name=\"com.trolltech.QtDBus.MyObject\" >\n" |
|
62 " <property access=\"readwrite\" type=\"i\" name=\"prop1\" />\n" |
|
63 " <property name=\"complexProp\" type=\"ai\" access=\"readwrite\">\n" |
|
64 " <annotation name=\"com.trolltech.QtDBus.QtTypeName\" value=\"QList<int>\"/>\n" |
|
65 " </property>\n" |
|
66 " <signal name=\"somethingHappened\" >\n" |
|
67 " <arg direction=\"out\" type=\"s\" />\n" |
|
68 " </signal>\n" |
|
69 " <method name=\"ping\" >\n" |
|
70 " <arg direction=\"in\" type=\"v\" name=\"ping\" />\n" |
|
71 " <arg direction=\"out\" type=\"v\" name=\"ping\" />\n" |
|
72 " </method>\n" |
|
73 " <method name=\"ping\" >\n" |
|
74 " <arg direction=\"in\" type=\"v\" name=\"ping1\" />\n" |
|
75 " <arg direction=\"in\" type=\"v\" name=\"ping2\" />\n" |
|
76 " <arg direction=\"out\" type=\"v\" name=\"pong1\" />\n" |
|
77 " <arg direction=\"out\" type=\"v\" name=\"pong2\" />\n" |
|
78 " </method>\n" |
|
79 " <method name=\"ping\" >\n" |
|
80 " <arg direction=\"in\" type=\"ai\" name=\"ping\" />\n" |
|
81 " <arg direction=\"out\" type=\"ai\" name=\"ping\" />\n" |
|
82 " <annotation name=\"com.trolltech.QtDBus.QtTypeName.In0\" value=\"QList<int>\"/>\n" |
|
83 " <annotation name=\"com.trolltech.QtDBus.QtTypeName.Out0\" value=\"QList<int>\"/>\n" |
|
84 " </method>\n" |
|
85 " </interface>\n" |
|
86 "") |
|
87 Q_PROPERTY(int prop1 READ prop1 WRITE setProp1) |
|
88 Q_PROPERTY(QList<int> complexProp READ complexProp WRITE setComplexProp) |
|
89 |
|
90 public: |
|
91 static int callCount; |
|
92 static QVariantList callArgs; |
|
93 MyObject() |
|
94 { |
|
95 QObject *subObject = new QObject(this); |
|
96 subObject->setObjectName("subObject"); |
|
97 } |
|
98 |
|
99 int m_prop1; |
|
100 int prop1() const |
|
101 { |
|
102 ++callCount; |
|
103 return m_prop1; |
|
104 } |
|
105 void setProp1(int value) |
|
106 { |
|
107 ++callCount; |
|
108 m_prop1 = value; |
|
109 } |
|
110 |
|
111 QList<int> m_complexProp; |
|
112 QList<int> complexProp() const |
|
113 { |
|
114 ++callCount; |
|
115 return m_complexProp; |
|
116 } |
|
117 void setComplexProp(const QList<int> &value) |
|
118 { |
|
119 ++callCount; |
|
120 m_complexProp = value; |
|
121 } |
|
122 |
|
123 public slots: |
|
124 |
|
125 void ping(QDBusMessage msg) |
|
126 { |
|
127 QDBusConnection sender = QDBusConnection::sender(); |
|
128 if (!sender.isConnected()) |
|
129 exit(1); |
|
130 |
|
131 ++callCount; |
|
132 callArgs = msg.arguments(); |
|
133 |
|
134 msg.setDelayedReply(true); |
|
135 if (!sender.send(msg.createReply(callArgs))) |
|
136 exit(1); |
|
137 } |
|
138 }; |
|
139 int MyObject::callCount = 0; |
|
140 QVariantList MyObject::callArgs; |
|
141 |
|
142 class Spy: public QObject |
|
143 { |
|
144 Q_OBJECT |
|
145 public: |
|
146 QString received; |
|
147 int count; |
|
148 |
|
149 Spy() : count(0) |
|
150 { } |
|
151 |
|
152 public slots: |
|
153 void spySlot(const QString& arg) |
|
154 { |
|
155 received = arg; |
|
156 ++count; |
|
157 } |
|
158 }; |
|
159 |
|
160 // helper function |
|
161 void emitSignal(const QString &interface, const QString &name, const QString &arg) |
|
162 { |
|
163 QDBusMessage msg = QDBusMessage::createSignal("/", interface, name); |
|
164 msg << arg; |
|
165 QDBusConnection::sessionBus().send(msg); |
|
166 |
|
167 QTest::qWait(1000); |
|
168 } |
|
169 |
|
170 class tst_QDBusInterface: public QObject |
|
171 { |
|
172 Q_OBJECT |
|
173 MyObject obj; |
|
174 private slots: |
|
175 void initTestCase(); |
|
176 |
|
177 void notConnected(); |
|
178 void notValid(); |
|
179 void invalidAfterServiceOwnerChanged(); |
|
180 void introspect(); |
|
181 void callMethod(); |
|
182 void invokeMethod(); |
|
183 void invokeMethodWithReturn(); |
|
184 void invokeMethodWithMultiReturn(); |
|
185 void invokeMethodWithComplexReturn(); |
|
186 |
|
187 void signal(); |
|
188 |
|
189 void propertyRead(); |
|
190 void propertyWrite(); |
|
191 void complexPropertyRead(); |
|
192 void complexPropertyWrite(); |
|
193 }; |
|
194 |
|
195 void tst_QDBusInterface::initTestCase() |
|
196 { |
|
197 QDBusConnection con = QDBusConnection::sessionBus(); |
|
198 QVERIFY(con.isConnected()); |
|
199 QTest::qWait(500); |
|
200 |
|
201 con.registerObject("/", &obj, QDBusConnection::ExportAllProperties |
|
202 | QDBusConnection::ExportAllSlots |
|
203 | QDBusConnection::ExportChildObjects); |
|
204 } |
|
205 |
|
206 void tst_QDBusInterface::notConnected() |
|
207 { |
|
208 QDBusConnection connection(""); |
|
209 QVERIFY(!connection.isConnected()); |
|
210 |
|
211 QDBusInterface interface("org.freedesktop.DBus", "/", "org.freedesktop.DBus", |
|
212 connection); |
|
213 |
|
214 QVERIFY(!interface.isValid()); |
|
215 } |
|
216 |
|
217 void tst_QDBusInterface::notValid() |
|
218 { |
|
219 QDBusConnection connection(""); |
|
220 QVERIFY(!connection.isConnected()); |
|
221 |
|
222 QDBusInterface interface("com.example.Test", QString(), "org.example.Test", |
|
223 connection); |
|
224 |
|
225 QVERIFY(!interface.isValid()); |
|
226 } |
|
227 |
|
228 void tst_QDBusInterface::invalidAfterServiceOwnerChanged() |
|
229 { |
|
230 QDBusConnection conn = QDBusConnection::sessionBus(); |
|
231 QDBusConnectionInterface *connIface = conn.interface(); |
|
232 |
|
233 QDBusInterface validInterface(conn.baseService(), "/"); |
|
234 QVERIFY(validInterface.isValid()); |
|
235 QDBusInterface invalidInterface("com.example.Test", "/"); |
|
236 QVERIFY(!invalidInterface.isValid()); |
|
237 |
|
238 QVERIFY(connIface->registerService("com.example.Test") == QDBusConnectionInterface::ServiceRegistered); |
|
239 |
|
240 QSignalSpy serviceOwnerChangedSpy(connIface, SIGNAL(serviceOwnerChanged(QString, QString, QString))); |
|
241 |
|
242 QEventLoop loop; |
|
243 QObject::connect(connIface, SIGNAL(serviceOwnerChanged(QString, QString, QString)), |
|
244 &loop, SLOT(quit())); |
|
245 loop.exec(); |
|
246 |
|
247 // at least once, but other services might have changed while running the test, too. |
|
248 QVERIFY(serviceOwnerChangedSpy.count() >= 1); |
|
249 bool foundOurService = false; |
|
250 for (int i = 0; i < serviceOwnerChangedSpy.count(); ++i) { |
|
251 QList<QVariant> args = serviceOwnerChangedSpy.at(i); |
|
252 QString name = args[0].toString(); |
|
253 QString oldOwner = args[1].toString(); |
|
254 QString newOwner = args[2].toString(); |
|
255 if (name == QLatin1String("com.example.Test")) { |
|
256 if (newOwner == conn.baseService()) { |
|
257 foundOurService = true; |
|
258 break; |
|
259 } |
|
260 } |
|
261 } |
|
262 QVERIFY(foundOurService); |
|
263 |
|
264 QVERIFY(!invalidInterface.isValid()); |
|
265 } |
|
266 |
|
267 void tst_QDBusInterface::introspect() |
|
268 { |
|
269 QDBusConnection con = QDBusConnection::sessionBus(); |
|
270 QDBusInterface iface(QDBusConnection::sessionBus().baseService(), QLatin1String("/"), |
|
271 TEST_INTERFACE_NAME); |
|
272 |
|
273 const QMetaObject *mo = iface.metaObject(); |
|
274 |
|
275 QCOMPARE(mo->methodCount() - mo->methodOffset(), 4); |
|
276 QVERIFY(mo->indexOfSignal(TEST_SIGNAL_NAME "(QString)") != -1); |
|
277 |
|
278 QCOMPARE(mo->propertyCount() - mo->propertyOffset(), 2); |
|
279 QVERIFY(mo->indexOfProperty("prop1") != -1); |
|
280 QVERIFY(mo->indexOfProperty("complexProp") != -1); |
|
281 } |
|
282 |
|
283 void tst_QDBusInterface::callMethod() |
|
284 { |
|
285 QDBusConnection con = QDBusConnection::sessionBus(); |
|
286 QDBusInterface iface(QDBusConnection::sessionBus().baseService(), QLatin1String("/"), |
|
287 TEST_INTERFACE_NAME); |
|
288 |
|
289 MyObject::callCount = 0; |
|
290 QDBusMessage reply = iface.call("ping", qVariantFromValue(QDBusVariant("foo"))); |
|
291 QCOMPARE(MyObject::callCount, 1); |
|
292 QCOMPARE(reply.type(), QDBusMessage::ReplyMessage); |
|
293 |
|
294 // verify what the callee received |
|
295 QCOMPARE(MyObject::callArgs.count(), 1); |
|
296 QVariant v = MyObject::callArgs.at(0); |
|
297 QDBusVariant dv = qdbus_cast<QDBusVariant>(v); |
|
298 QCOMPARE(dv.variant().type(), QVariant::String); |
|
299 QCOMPARE(dv.variant().toString(), QString("foo")); |
|
300 |
|
301 // verify reply |
|
302 QCOMPARE(reply.arguments().count(), 1); |
|
303 v = reply.arguments().at(0); |
|
304 dv = qdbus_cast<QDBusVariant>(v); |
|
305 QCOMPARE(dv.variant().type(), QVariant::String); |
|
306 QCOMPARE(dv.variant().toString(), QString("foo")); |
|
307 } |
|
308 |
|
309 void tst_QDBusInterface::invokeMethod() |
|
310 { |
|
311 QDBusConnection con = QDBusConnection::sessionBus(); |
|
312 QDBusInterface iface(QDBusConnection::sessionBus().baseService(), QLatin1String("/"), |
|
313 TEST_INTERFACE_NAME); |
|
314 |
|
315 // make the call without a return type |
|
316 MyObject::callCount = 0; |
|
317 QDBusVariant arg("foo"); |
|
318 QVERIFY(QMetaObject::invokeMethod(&iface, "ping", Q_ARG(QDBusVariant, arg))); |
|
319 QCOMPARE(MyObject::callCount, 1); |
|
320 |
|
321 // verify what the callee received |
|
322 QCOMPARE(MyObject::callArgs.count(), 1); |
|
323 QVariant v = MyObject::callArgs.at(0); |
|
324 QDBusVariant dv = qdbus_cast<QDBusVariant>(v); |
|
325 QCOMPARE(dv.variant().type(), QVariant::String); |
|
326 QCOMPARE(dv.variant().toString(), QString("foo")); |
|
327 } |
|
328 |
|
329 void tst_QDBusInterface::invokeMethodWithReturn() |
|
330 { |
|
331 QDBusConnection con = QDBusConnection::sessionBus(); |
|
332 QDBusInterface iface(QDBusConnection::sessionBus().baseService(), QLatin1String("/"), |
|
333 TEST_INTERFACE_NAME); |
|
334 |
|
335 // make the call without a return type |
|
336 MyObject::callCount = 0; |
|
337 QDBusVariant arg("foo"); |
|
338 QDBusVariant retArg; |
|
339 QVERIFY(QMetaObject::invokeMethod(&iface, "ping", Q_RETURN_ARG(QDBusVariant, retArg), Q_ARG(QDBusVariant, arg))); |
|
340 QCOMPARE(MyObject::callCount, 1); |
|
341 |
|
342 // verify what the callee received |
|
343 QCOMPARE(MyObject::callArgs.count(), 1); |
|
344 QVariant v = MyObject::callArgs.at(0); |
|
345 QDBusVariant dv = qdbus_cast<QDBusVariant>(v); |
|
346 QCOMPARE(dv.variant().type(), QVariant::String); |
|
347 QCOMPARE(dv.variant().toString(), arg.variant().toString()); |
|
348 |
|
349 // verify that we got the reply as expected |
|
350 QCOMPARE(retArg.variant(), arg.variant()); |
|
351 } |
|
352 |
|
353 void tst_QDBusInterface::invokeMethodWithMultiReturn() |
|
354 { |
|
355 QDBusConnection con = QDBusConnection::sessionBus(); |
|
356 QDBusInterface iface(QDBusConnection::sessionBus().baseService(), QLatin1String("/"), |
|
357 TEST_INTERFACE_NAME); |
|
358 |
|
359 // make the call without a return type |
|
360 MyObject::callCount = 0; |
|
361 QDBusVariant arg("foo"), arg2("bar"); |
|
362 QDBusVariant retArg, retArg2; |
|
363 QVERIFY(QMetaObject::invokeMethod(&iface, "ping", |
|
364 Q_RETURN_ARG(QDBusVariant, retArg), |
|
365 Q_ARG(QDBusVariant, arg), |
|
366 Q_ARG(QDBusVariant, arg2), |
|
367 Q_ARG(QDBusVariant&, retArg2))); |
|
368 QCOMPARE(MyObject::callCount, 1); |
|
369 |
|
370 // verify what the callee received |
|
371 QCOMPARE(MyObject::callArgs.count(), 2); |
|
372 QVariant v = MyObject::callArgs.at(0); |
|
373 QDBusVariant dv = qdbus_cast<QDBusVariant>(v); |
|
374 QCOMPARE(dv.variant().type(), QVariant::String); |
|
375 QCOMPARE(dv.variant().toString(), arg.variant().toString()); |
|
376 |
|
377 v = MyObject::callArgs.at(1); |
|
378 dv = qdbus_cast<QDBusVariant>(v); |
|
379 QCOMPARE(dv.variant().type(), QVariant::String); |
|
380 QCOMPARE(dv.variant().toString(), arg2.variant().toString()); |
|
381 |
|
382 // verify that we got the replies as expected |
|
383 QCOMPARE(retArg.variant(), arg.variant()); |
|
384 QCOMPARE(retArg2.variant(), arg2.variant()); |
|
385 } |
|
386 |
|
387 void tst_QDBusInterface::invokeMethodWithComplexReturn() |
|
388 { |
|
389 QDBusConnection con = QDBusConnection::sessionBus(); |
|
390 QDBusInterface iface(QDBusConnection::sessionBus().baseService(), QLatin1String("/"), |
|
391 TEST_INTERFACE_NAME); |
|
392 |
|
393 // make the call without a return type |
|
394 MyObject::callCount = 0; |
|
395 QList<int> arg = QList<int>() << 42 << -47; |
|
396 QList<int> retArg; |
|
397 QVERIFY(QMetaObject::invokeMethod(&iface, "ping", Q_RETURN_ARG(QList<int>, retArg), Q_ARG(QList<int>, arg))); |
|
398 QCOMPARE(MyObject::callCount, 1); |
|
399 |
|
400 // verify what the callee received |
|
401 QCOMPARE(MyObject::callArgs.count(), 1); |
|
402 QVariant v = MyObject::callArgs.at(0); |
|
403 QCOMPARE(v.userType(), qMetaTypeId<QDBusArgument>()); |
|
404 QCOMPARE(qdbus_cast<QList<int> >(v), arg); |
|
405 |
|
406 // verify that we got the reply as expected |
|
407 QCOMPARE(retArg, arg); |
|
408 } |
|
409 |
|
410 void tst_QDBusInterface::signal() |
|
411 { |
|
412 QDBusConnection con = QDBusConnection::sessionBus(); |
|
413 QDBusInterface iface(QDBusConnection::sessionBus().baseService(), QLatin1String("/"), |
|
414 TEST_INTERFACE_NAME); |
|
415 |
|
416 QString arg = "So long and thanks for all the fish"; |
|
417 { |
|
418 Spy spy; |
|
419 spy.connect(&iface, SIGNAL(somethingHappened(QString)), SLOT(spySlot(QString))); |
|
420 |
|
421 emitSignal(TEST_INTERFACE_NAME, TEST_SIGNAL_NAME, arg); |
|
422 QCOMPARE(spy.count, 1); |
|
423 QCOMPARE(spy.received, arg); |
|
424 } |
|
425 |
|
426 QDBusInterface iface2(QDBusConnection::sessionBus().baseService(), QLatin1String("/"), |
|
427 TEST_INTERFACE_NAME); |
|
428 { |
|
429 Spy spy; |
|
430 spy.connect(&iface, SIGNAL(somethingHappened(QString)), SLOT(spySlot(QString))); |
|
431 spy.connect(&iface2, SIGNAL(somethingHappened(QString)), SLOT(spySlot(QString))); |
|
432 |
|
433 emitSignal(TEST_INTERFACE_NAME, TEST_SIGNAL_NAME, arg); |
|
434 QCOMPARE(spy.count, 2); |
|
435 QCOMPARE(spy.received, arg); |
|
436 } |
|
437 |
|
438 { |
|
439 Spy spy, spy2; |
|
440 spy.connect(&iface, SIGNAL(somethingHappened(QString)), SLOT(spySlot(QString))); |
|
441 spy2.connect(&iface2, SIGNAL(somethingHappened(QString)), SLOT(spySlot(QString))); |
|
442 |
|
443 emitSignal(TEST_INTERFACE_NAME, TEST_SIGNAL_NAME, arg); |
|
444 QCOMPARE(spy.count, 1); |
|
445 QCOMPARE(spy.received, arg); |
|
446 QCOMPARE(spy2.count, 1); |
|
447 QCOMPARE(spy2.received, arg); |
|
448 } |
|
449 } |
|
450 |
|
451 void tst_QDBusInterface::propertyRead() |
|
452 { |
|
453 QDBusConnection con = QDBusConnection::sessionBus(); |
|
454 QDBusInterface iface(QDBusConnection::sessionBus().baseService(), QLatin1String("/"), |
|
455 TEST_INTERFACE_NAME); |
|
456 |
|
457 int arg = obj.m_prop1 = 42; |
|
458 MyObject::callCount = 0; |
|
459 |
|
460 QVariant v = iface.property("prop1"); |
|
461 QVERIFY(v.isValid()); |
|
462 QCOMPARE(v.userType(), int(QVariant::Int)); |
|
463 QCOMPARE(v.toInt(), arg); |
|
464 QCOMPARE(MyObject::callCount, 1); |
|
465 } |
|
466 |
|
467 void tst_QDBusInterface::propertyWrite() |
|
468 { |
|
469 QDBusConnection con = QDBusConnection::sessionBus(); |
|
470 QDBusInterface iface(QDBusConnection::sessionBus().baseService(), QLatin1String("/"), |
|
471 TEST_INTERFACE_NAME); |
|
472 |
|
473 int arg = 42; |
|
474 obj.m_prop1 = 0; |
|
475 MyObject::callCount = 0; |
|
476 |
|
477 QVERIFY(iface.setProperty("prop1", arg)); |
|
478 QCOMPARE(MyObject::callCount, 1); |
|
479 QCOMPARE(obj.m_prop1, arg); |
|
480 } |
|
481 |
|
482 void tst_QDBusInterface::complexPropertyRead() |
|
483 { |
|
484 QDBusConnection con = QDBusConnection::sessionBus(); |
|
485 QDBusInterface iface(QDBusConnection::sessionBus().baseService(), QLatin1String("/"), |
|
486 TEST_INTERFACE_NAME); |
|
487 |
|
488 QList<int> arg = obj.m_complexProp = QList<int>() << 42 << -47; |
|
489 MyObject::callCount = 0; |
|
490 |
|
491 QVariant v = iface.property("complexProp"); |
|
492 QVERIFY(v.isValid()); |
|
493 QCOMPARE(v.userType(), qMetaTypeId<QList<int> >()); |
|
494 QCOMPARE(v.value<QList<int> >(), arg); |
|
495 QCOMPARE(MyObject::callCount, 1); |
|
496 } |
|
497 |
|
498 void tst_QDBusInterface::complexPropertyWrite() |
|
499 { |
|
500 QDBusConnection con = QDBusConnection::sessionBus(); |
|
501 QDBusInterface iface(QDBusConnection::sessionBus().baseService(), QLatin1String("/"), |
|
502 TEST_INTERFACE_NAME); |
|
503 |
|
504 QList<int> arg = QList<int>() << -47 << 42; |
|
505 obj.m_complexProp.clear(); |
|
506 MyObject::callCount = 0; |
|
507 |
|
508 QVERIFY(iface.setProperty("complexProp", qVariantFromValue(arg))); |
|
509 QCOMPARE(MyObject::callCount, 1); |
|
510 QCOMPARE(obj.m_complexProp, arg); |
|
511 } |
|
512 |
|
513 QTEST_MAIN(tst_QDBusInterface) |
|
514 |
|
515 #include "tst_qdbusinterface.moc" |
|
516 |