author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Thu, 02 Sep 2010 21:20:32 +0300 | |
changeset 34 | a33bf25e6f72 |
parent 25 | e24348a560a6 |
child 37 | 758a864f9613 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
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 |
/* -*- 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 |
||
25
e24348a560a6
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
160 |
class DerivedFromQDBusInterface: public QDBusInterface |
e24348a560a6
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
161 |
{ |
e24348a560a6
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
162 |
Q_OBJECT |
e24348a560a6
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
163 |
public: |
e24348a560a6
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
164 |
DerivedFromQDBusInterface() |
e24348a560a6
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
165 |
: QDBusInterface("com.example.Test", "/") |
e24348a560a6
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
166 |
{} |
e24348a560a6
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
167 |
|
e24348a560a6
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
168 |
public slots: |
e24348a560a6
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
169 |
void method() {} |
e24348a560a6
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
170 |
}; |
e24348a560a6
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
171 |
|
0 | 172 |
// helper function |
173 |
void emitSignal(const QString &interface, const QString &name, const QString &arg) |
|
174 |
{ |
|
175 |
QDBusMessage msg = QDBusMessage::createSignal("/", interface, name); |
|
176 |
msg << arg; |
|
177 |
QDBusConnection::sessionBus().send(msg); |
|
178 |
||
179 |
QTest::qWait(1000); |
|
180 |
} |
|
181 |
||
182 |
class tst_QDBusInterface: public QObject |
|
183 |
{ |
|
184 |
Q_OBJECT |
|
185 |
MyObject obj; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
186 |
public slots: |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
187 |
void testServiceOwnerChanged(const QString &service) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
188 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
189 |
if (service == "com.example.Test") |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
190 |
QTestEventLoop::instance().exitLoop(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
191 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
192 |
|
0 | 193 |
private slots: |
194 |
void initTestCase(); |
|
195 |
||
196 |
void notConnected(); |
|
197 |
void notValid(); |
|
25
e24348a560a6
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
198 |
void notValidDerived(); |
0 | 199 |
void invalidAfterServiceOwnerChanged(); |
200 |
void introspect(); |
|
201 |
void callMethod(); |
|
202 |
void invokeMethod(); |
|
203 |
void invokeMethodWithReturn(); |
|
204 |
void invokeMethodWithMultiReturn(); |
|
205 |
void invokeMethodWithComplexReturn(); |
|
206 |
||
207 |
void signal(); |
|
208 |
||
209 |
void propertyRead(); |
|
210 |
void propertyWrite(); |
|
211 |
void complexPropertyRead(); |
|
212 |
void complexPropertyWrite(); |
|
213 |
}; |
|
214 |
||
215 |
void tst_QDBusInterface::initTestCase() |
|
216 |
{ |
|
217 |
QDBusConnection con = QDBusConnection::sessionBus(); |
|
218 |
QVERIFY(con.isConnected()); |
|
219 |
QTest::qWait(500); |
|
220 |
||
221 |
con.registerObject("/", &obj, QDBusConnection::ExportAllProperties |
|
222 |
| QDBusConnection::ExportAllSlots |
|
223 |
| QDBusConnection::ExportChildObjects); |
|
224 |
} |
|
225 |
||
226 |
void tst_QDBusInterface::notConnected() |
|
227 |
{ |
|
228 |
QDBusConnection connection(""); |
|
229 |
QVERIFY(!connection.isConnected()); |
|
230 |
||
231 |
QDBusInterface interface("org.freedesktop.DBus", "/", "org.freedesktop.DBus", |
|
232 |
connection); |
|
233 |
||
234 |
QVERIFY(!interface.isValid()); |
|
25
e24348a560a6
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
235 |
QVERIFY(!QMetaObject::invokeMethod(&interface, "ListNames", Qt::DirectConnection)); |
0 | 236 |
} |
237 |
||
238 |
void tst_QDBusInterface::notValid() |
|
239 |
{ |
|
240 |
QDBusConnection connection(""); |
|
241 |
QVERIFY(!connection.isConnected()); |
|
242 |
||
243 |
QDBusInterface interface("com.example.Test", QString(), "org.example.Test", |
|
244 |
connection); |
|
245 |
||
246 |
QVERIFY(!interface.isValid()); |
|
25
e24348a560a6
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
247 |
QVERIFY(!QMetaObject::invokeMethod(&interface, "ListNames", Qt::DirectConnection)); |
e24348a560a6
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
248 |
} |
e24348a560a6
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
249 |
|
e24348a560a6
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
250 |
void tst_QDBusInterface::notValidDerived() |
e24348a560a6
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
251 |
{ |
e24348a560a6
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
252 |
DerivedFromQDBusInterface c; |
e24348a560a6
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
253 |
QVERIFY(!c.isValid()); |
e24348a560a6
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
254 |
QMetaObject::invokeMethod(&c, "method", Qt::DirectConnection); |
0 | 255 |
} |
256 |
||
257 |
void tst_QDBusInterface::invalidAfterServiceOwnerChanged() |
|
258 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
259 |
// this test is technically the same as tst_QDBusAbstractInterface::followSignal |
0 | 260 |
QDBusConnection conn = QDBusConnection::sessionBus(); |
261 |
QDBusConnectionInterface *connIface = conn.interface(); |
|
262 |
||
263 |
QDBusInterface validInterface(conn.baseService(), "/"); |
|
264 |
QVERIFY(validInterface.isValid()); |
|
265 |
QDBusInterface invalidInterface("com.example.Test", "/"); |
|
266 |
QVERIFY(!invalidInterface.isValid()); |
|
267 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
268 |
QTestEventLoop::instance().connect(connIface, SIGNAL(serviceOwnerChanged(QString, QString, QString)), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
269 |
SLOT(exitLoop())); |
0 | 270 |
QVERIFY(connIface->registerService("com.example.Test") == QDBusConnectionInterface::ServiceRegistered); |
271 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
272 |
QTestEventLoop::instance().enterLoop(5); |
0 | 273 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
274 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
275 |
QVERIFY(invalidInterface.isValid()); |
0 | 276 |
} |
277 |
||
278 |
void tst_QDBusInterface::introspect() |
|
279 |
{ |
|
280 |
QDBusConnection con = QDBusConnection::sessionBus(); |
|
281 |
QDBusInterface iface(QDBusConnection::sessionBus().baseService(), QLatin1String("/"), |
|
282 |
TEST_INTERFACE_NAME); |
|
283 |
||
284 |
const QMetaObject *mo = iface.metaObject(); |
|
285 |
||
286 |
QCOMPARE(mo->methodCount() - mo->methodOffset(), 4); |
|
287 |
QVERIFY(mo->indexOfSignal(TEST_SIGNAL_NAME "(QString)") != -1); |
|
288 |
||
289 |
QCOMPARE(mo->propertyCount() - mo->propertyOffset(), 2); |
|
290 |
QVERIFY(mo->indexOfProperty("prop1") != -1); |
|
291 |
QVERIFY(mo->indexOfProperty("complexProp") != -1); |
|
292 |
} |
|
293 |
||
294 |
void tst_QDBusInterface::callMethod() |
|
295 |
{ |
|
296 |
QDBusConnection con = QDBusConnection::sessionBus(); |
|
297 |
QDBusInterface iface(QDBusConnection::sessionBus().baseService(), QLatin1String("/"), |
|
298 |
TEST_INTERFACE_NAME); |
|
299 |
||
300 |
MyObject::callCount = 0; |
|
301 |
QDBusMessage reply = iface.call("ping", qVariantFromValue(QDBusVariant("foo"))); |
|
302 |
QCOMPARE(MyObject::callCount, 1); |
|
303 |
QCOMPARE(reply.type(), QDBusMessage::ReplyMessage); |
|
304 |
||
305 |
// verify what the callee received |
|
306 |
QCOMPARE(MyObject::callArgs.count(), 1); |
|
307 |
QVariant v = MyObject::callArgs.at(0); |
|
308 |
QDBusVariant dv = qdbus_cast<QDBusVariant>(v); |
|
309 |
QCOMPARE(dv.variant().type(), QVariant::String); |
|
310 |
QCOMPARE(dv.variant().toString(), QString("foo")); |
|
311 |
||
312 |
// verify reply |
|
313 |
QCOMPARE(reply.arguments().count(), 1); |
|
314 |
v = reply.arguments().at(0); |
|
315 |
dv = qdbus_cast<QDBusVariant>(v); |
|
316 |
QCOMPARE(dv.variant().type(), QVariant::String); |
|
317 |
QCOMPARE(dv.variant().toString(), QString("foo")); |
|
318 |
} |
|
319 |
||
320 |
void tst_QDBusInterface::invokeMethod() |
|
321 |
{ |
|
322 |
QDBusConnection con = QDBusConnection::sessionBus(); |
|
323 |
QDBusInterface iface(QDBusConnection::sessionBus().baseService(), QLatin1String("/"), |
|
324 |
TEST_INTERFACE_NAME); |
|
325 |
||
326 |
// make the call without a return type |
|
327 |
MyObject::callCount = 0; |
|
328 |
QDBusVariant arg("foo"); |
|
329 |
QVERIFY(QMetaObject::invokeMethod(&iface, "ping", Q_ARG(QDBusVariant, arg))); |
|
330 |
QCOMPARE(MyObject::callCount, 1); |
|
331 |
||
332 |
// verify what the callee received |
|
333 |
QCOMPARE(MyObject::callArgs.count(), 1); |
|
334 |
QVariant v = MyObject::callArgs.at(0); |
|
335 |
QDBusVariant dv = qdbus_cast<QDBusVariant>(v); |
|
336 |
QCOMPARE(dv.variant().type(), QVariant::String); |
|
337 |
QCOMPARE(dv.variant().toString(), QString("foo")); |
|
338 |
} |
|
339 |
||
340 |
void tst_QDBusInterface::invokeMethodWithReturn() |
|
341 |
{ |
|
342 |
QDBusConnection con = QDBusConnection::sessionBus(); |
|
343 |
QDBusInterface iface(QDBusConnection::sessionBus().baseService(), QLatin1String("/"), |
|
344 |
TEST_INTERFACE_NAME); |
|
345 |
||
346 |
// make the call without a return type |
|
347 |
MyObject::callCount = 0; |
|
348 |
QDBusVariant arg("foo"); |
|
349 |
QDBusVariant retArg; |
|
350 |
QVERIFY(QMetaObject::invokeMethod(&iface, "ping", Q_RETURN_ARG(QDBusVariant, retArg), Q_ARG(QDBusVariant, arg))); |
|
351 |
QCOMPARE(MyObject::callCount, 1); |
|
352 |
||
353 |
// verify what the callee received |
|
354 |
QCOMPARE(MyObject::callArgs.count(), 1); |
|
355 |
QVariant v = MyObject::callArgs.at(0); |
|
356 |
QDBusVariant dv = qdbus_cast<QDBusVariant>(v); |
|
357 |
QCOMPARE(dv.variant().type(), QVariant::String); |
|
358 |
QCOMPARE(dv.variant().toString(), arg.variant().toString()); |
|
359 |
||
360 |
// verify that we got the reply as expected |
|
361 |
QCOMPARE(retArg.variant(), arg.variant()); |
|
362 |
} |
|
363 |
||
364 |
void tst_QDBusInterface::invokeMethodWithMultiReturn() |
|
365 |
{ |
|
366 |
QDBusConnection con = QDBusConnection::sessionBus(); |
|
367 |
QDBusInterface iface(QDBusConnection::sessionBus().baseService(), QLatin1String("/"), |
|
368 |
TEST_INTERFACE_NAME); |
|
369 |
||
370 |
// make the call without a return type |
|
371 |
MyObject::callCount = 0; |
|
372 |
QDBusVariant arg("foo"), arg2("bar"); |
|
373 |
QDBusVariant retArg, retArg2; |
|
374 |
QVERIFY(QMetaObject::invokeMethod(&iface, "ping", |
|
375 |
Q_RETURN_ARG(QDBusVariant, retArg), |
|
376 |
Q_ARG(QDBusVariant, arg), |
|
377 |
Q_ARG(QDBusVariant, arg2), |
|
378 |
Q_ARG(QDBusVariant&, retArg2))); |
|
379 |
QCOMPARE(MyObject::callCount, 1); |
|
380 |
||
381 |
// verify what the callee received |
|
382 |
QCOMPARE(MyObject::callArgs.count(), 2); |
|
383 |
QVariant v = MyObject::callArgs.at(0); |
|
384 |
QDBusVariant dv = qdbus_cast<QDBusVariant>(v); |
|
385 |
QCOMPARE(dv.variant().type(), QVariant::String); |
|
386 |
QCOMPARE(dv.variant().toString(), arg.variant().toString()); |
|
387 |
||
388 |
v = MyObject::callArgs.at(1); |
|
389 |
dv = qdbus_cast<QDBusVariant>(v); |
|
390 |
QCOMPARE(dv.variant().type(), QVariant::String); |
|
391 |
QCOMPARE(dv.variant().toString(), arg2.variant().toString()); |
|
392 |
||
393 |
// verify that we got the replies as expected |
|
394 |
QCOMPARE(retArg.variant(), arg.variant()); |
|
395 |
QCOMPARE(retArg2.variant(), arg2.variant()); |
|
396 |
} |
|
397 |
||
398 |
void tst_QDBusInterface::invokeMethodWithComplexReturn() |
|
399 |
{ |
|
400 |
QDBusConnection con = QDBusConnection::sessionBus(); |
|
401 |
QDBusInterface iface(QDBusConnection::sessionBus().baseService(), QLatin1String("/"), |
|
402 |
TEST_INTERFACE_NAME); |
|
403 |
||
404 |
// make the call without a return type |
|
405 |
MyObject::callCount = 0; |
|
406 |
QList<int> arg = QList<int>() << 42 << -47; |
|
407 |
QList<int> retArg; |
|
408 |
QVERIFY(QMetaObject::invokeMethod(&iface, "ping", Q_RETURN_ARG(QList<int>, retArg), Q_ARG(QList<int>, arg))); |
|
409 |
QCOMPARE(MyObject::callCount, 1); |
|
410 |
||
411 |
// verify what the callee received |
|
412 |
QCOMPARE(MyObject::callArgs.count(), 1); |
|
413 |
QVariant v = MyObject::callArgs.at(0); |
|
414 |
QCOMPARE(v.userType(), qMetaTypeId<QDBusArgument>()); |
|
415 |
QCOMPARE(qdbus_cast<QList<int> >(v), arg); |
|
416 |
||
417 |
// verify that we got the reply as expected |
|
418 |
QCOMPARE(retArg, arg); |
|
419 |
} |
|
420 |
||
421 |
void tst_QDBusInterface::signal() |
|
422 |
{ |
|
423 |
QDBusConnection con = QDBusConnection::sessionBus(); |
|
424 |
QDBusInterface iface(QDBusConnection::sessionBus().baseService(), QLatin1String("/"), |
|
425 |
TEST_INTERFACE_NAME); |
|
426 |
||
427 |
QString arg = "So long and thanks for all the fish"; |
|
428 |
{ |
|
429 |
Spy spy; |
|
430 |
spy.connect(&iface, SIGNAL(somethingHappened(QString)), SLOT(spySlot(QString))); |
|
431 |
||
432 |
emitSignal(TEST_INTERFACE_NAME, TEST_SIGNAL_NAME, arg); |
|
433 |
QCOMPARE(spy.count, 1); |
|
434 |
QCOMPARE(spy.received, arg); |
|
435 |
} |
|
436 |
||
437 |
QDBusInterface iface2(QDBusConnection::sessionBus().baseService(), QLatin1String("/"), |
|
438 |
TEST_INTERFACE_NAME); |
|
439 |
{ |
|
440 |
Spy spy; |
|
441 |
spy.connect(&iface, SIGNAL(somethingHappened(QString)), SLOT(spySlot(QString))); |
|
442 |
spy.connect(&iface2, SIGNAL(somethingHappened(QString)), SLOT(spySlot(QString))); |
|
443 |
||
444 |
emitSignal(TEST_INTERFACE_NAME, TEST_SIGNAL_NAME, arg); |
|
445 |
QCOMPARE(spy.count, 2); |
|
446 |
QCOMPARE(spy.received, arg); |
|
447 |
} |
|
448 |
||
449 |
{ |
|
450 |
Spy spy, spy2; |
|
451 |
spy.connect(&iface, SIGNAL(somethingHappened(QString)), SLOT(spySlot(QString))); |
|
452 |
spy2.connect(&iface2, SIGNAL(somethingHappened(QString)), SLOT(spySlot(QString))); |
|
453 |
||
454 |
emitSignal(TEST_INTERFACE_NAME, TEST_SIGNAL_NAME, arg); |
|
455 |
QCOMPARE(spy.count, 1); |
|
456 |
QCOMPARE(spy.received, arg); |
|
457 |
QCOMPARE(spy2.count, 1); |
|
458 |
QCOMPARE(spy2.received, arg); |
|
459 |
} |
|
460 |
} |
|
461 |
||
462 |
void tst_QDBusInterface::propertyRead() |
|
463 |
{ |
|
464 |
QDBusConnection con = QDBusConnection::sessionBus(); |
|
465 |
QDBusInterface iface(QDBusConnection::sessionBus().baseService(), QLatin1String("/"), |
|
466 |
TEST_INTERFACE_NAME); |
|
467 |
||
468 |
int arg = obj.m_prop1 = 42; |
|
469 |
MyObject::callCount = 0; |
|
470 |
||
471 |
QVariant v = iface.property("prop1"); |
|
472 |
QVERIFY(v.isValid()); |
|
473 |
QCOMPARE(v.userType(), int(QVariant::Int)); |
|
474 |
QCOMPARE(v.toInt(), arg); |
|
475 |
QCOMPARE(MyObject::callCount, 1); |
|
476 |
} |
|
477 |
||
478 |
void tst_QDBusInterface::propertyWrite() |
|
479 |
{ |
|
480 |
QDBusConnection con = QDBusConnection::sessionBus(); |
|
481 |
QDBusInterface iface(QDBusConnection::sessionBus().baseService(), QLatin1String("/"), |
|
482 |
TEST_INTERFACE_NAME); |
|
483 |
||
484 |
int arg = 42; |
|
485 |
obj.m_prop1 = 0; |
|
486 |
MyObject::callCount = 0; |
|
487 |
||
488 |
QVERIFY(iface.setProperty("prop1", arg)); |
|
489 |
QCOMPARE(MyObject::callCount, 1); |
|
490 |
QCOMPARE(obj.m_prop1, arg); |
|
491 |
} |
|
492 |
||
493 |
void tst_QDBusInterface::complexPropertyRead() |
|
494 |
{ |
|
495 |
QDBusConnection con = QDBusConnection::sessionBus(); |
|
496 |
QDBusInterface iface(QDBusConnection::sessionBus().baseService(), QLatin1String("/"), |
|
497 |
TEST_INTERFACE_NAME); |
|
498 |
||
499 |
QList<int> arg = obj.m_complexProp = QList<int>() << 42 << -47; |
|
500 |
MyObject::callCount = 0; |
|
501 |
||
502 |
QVariant v = iface.property("complexProp"); |
|
503 |
QVERIFY(v.isValid()); |
|
504 |
QCOMPARE(v.userType(), qMetaTypeId<QList<int> >()); |
|
505 |
QCOMPARE(v.value<QList<int> >(), arg); |
|
506 |
QCOMPARE(MyObject::callCount, 1); |
|
507 |
} |
|
508 |
||
509 |
void tst_QDBusInterface::complexPropertyWrite() |
|
510 |
{ |
|
511 |
QDBusConnection con = QDBusConnection::sessionBus(); |
|
512 |
QDBusInterface iface(QDBusConnection::sessionBus().baseService(), QLatin1String("/"), |
|
513 |
TEST_INTERFACE_NAME); |
|
514 |
||
515 |
QList<int> arg = QList<int>() << -47 << 42; |
|
516 |
obj.m_complexProp.clear(); |
|
517 |
MyObject::callCount = 0; |
|
518 |
||
519 |
QVERIFY(iface.setProperty("complexProp", qVariantFromValue(arg))); |
|
520 |
QCOMPARE(MyObject::callCount, 1); |
|
521 |
QCOMPARE(obj.m_complexProp, arg); |
|
522 |
} |
|
523 |
||
524 |
QTEST_MAIN(tst_QDBusInterface) |
|
525 |
||
526 |
#include "tst_qdbusinterface.moc" |
|
527 |