1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
/* -*- C++ -*-
*
* Copyright (C) 2006 Trolltech AS. All rights reserved.
* Author: Thiago Macieira <thiago.macieira@trolltech.com>
*
* Licensed under the Academic Free License version 2.1
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include <qcoreapplication.h>
#include <qmetatype.h>
#include <QtTest/QtTest>
#include <dbus/qdbus.h>
#include <QtCore/qvariant.h>
#include "common.h"
Q_DECLARE_METATYPE(QVariantList)
#define TEST_INTERFACE_NAME "com.trolltech.QtDBus.MyObject"
#define TEST_SIGNAL_NAME "somethingHappened"
const char introspectionData[] =
"<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n"
"\"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n"
"<node>"
"<interface name=\"org.freedesktop.DBus.Introspectable\">"
"<method name=\"Introspect\">"
"<arg name=\"data\" direction=\"out\" type=\"s\"/>"
"</method>"
"</interface>"
"<interface name=\"" TEST_INTERFACE_NAME "\">"
"<method name=\"ping\">"
"<arg name=\"ping\" direction=\"in\" type=\"v\"/>"
"<arg name=\"pong\" direction=\"out\" type=\"v\"/>"
"</method>"
"<method name=\"ping\">"
"<arg name=\"ping1\" direction=\"in\" type=\"v\"/>"
"<arg name=\"ping2\" direction=\"in\" type=\"v\"/>"
"<arg name=\"pong1\" direction=\"out\" type=\"v\"/>"
"<arg name=\"pong2\" direction=\"out\" type=\"v\"/>"
"</method>"
"<signal name=\"" TEST_SIGNAL_NAME "\">"
"<arg type=\"s\"/>"
"</signal>"
"<property name=\"prop1\" access=\"readwrite\" type=\"i\" />"
"</interface>"
"<node name=\"subObject\"/>"
"</node>";
class IntrospectionAdaptor: public QDBusAbstractAdaptor
{
Q_OBJECT
Q_CLASSINFO("D-Bus Interface", "org.freedesktop.DBus.Introspectable")
public:
IntrospectionAdaptor(QObject *parent)
: QDBusAbstractAdaptor(parent)
{ }
public slots:
void Introspect(const QDBusMessage &msg)
{
QDBusMessage reply = QDBusMessage::methodReply(msg);
reply << ::introspectionData;
if (!msg.connection().send(reply))
exit(1);
}
};
class MyObject: public QObject
{
Q_OBJECT
public:
MyObject()
{
new IntrospectionAdaptor(this);
}
public slots:
void ping(const QDBusMessage &msg)
{
QDBusMessage reply = QDBusMessage::methodReply(msg);
reply << static_cast<QList<QVariant> >(msg);
if (!msg.connection().send(reply))
exit(1);
}
};
class Spy: public QObject
{
Q_OBJECT
public:
QString received;
int count;
Spy() : count(0)
{ }
public slots:
void spySlot(const QString& arg)
{
received = arg;
++count;
}
};
// helper function
void emitSignal(const QString &interface, const QString &name, const QString &arg)
{
QDBusMessage msg = QDBusMessage::signal("/", interface, name);
msg << arg;
QDBus::sessionBus().send(msg);
QTest::qWait(200);
}
class tst_QDBusInterface: public QObject
{
Q_OBJECT
MyObject obj;
private slots:
void initTestCase();
void call_data();
void call();
void introspect();
void signal();
};
void tst_QDBusInterface::initTestCase()
{
QDBusConnection &con = QDBus::sessionBus();
QVERIFY(con.isConnected());
con.registerObject("/", &obj, QDBusConnection::ExportAdaptors | QDBusConnection::ExportSlots);
}
void tst_QDBusInterface::call_data()
{
QTest::addColumn<QString>("method");
QTest::addColumn<QVariantList>("input");
QTest::addColumn<QVariantList>("output");
QVariantList input;
QTest::newRow("empty") << "ping" << input << input;
input << qVariantFromValue(1);
QTest::newRow("int") << "ping" << input << input;
QTest::newRow("int-int") << "ping.i" << input << input;
QTest::newRow("int-int16") << "ping.n" << input << (QVariantList() << qVariantFromValue(short(1)));
// try doing some conversions
QVariantList output;
output << qVariantFromValue(1U);
QTest::newRow("int-uint") << "ping.u" << input << output;
#if QT_VERSION >= 0x040200
output.clear();
output << qVariantFromValue(ushort(1));
QTest::newRow("int-uint16") << "ping.q" << input << output;
#endif
QTest::newRow("int-int64") << "ping.x" << input << (QVariantList() << qVariantFromValue(Q_INT64_C(1)));
QTest::newRow("int-uint64") << "ping.t" << input << (QVariantList() << qVariantFromValue(Q_UINT64_C(1)));
QTest::newRow("int-double") << "ping.d" << input << (QVariantList() << qVariantFromValue(1.0));
output.clear();
output << QString("1");
QTest::newRow("int-string") << "ping.s" << input << output;
// try from string now
input = output;
QTest::newRow("string") << "ping" << input << output;
QTest::newRow("string-string") << "ping.s" << input << output;
output.clear();
output << qVariantFromValue(1);
QTest::newRow("string-int") << "ping.i" << input << output;
#if QT_VERSION >= 0x040200
output.clear();
output << qVariantFromValue(short(1));
QTest::newRow("string-int16") << "ping.n" << input << input;
#endif
output.clear();
output << qVariantFromValue(1U);
QTest::newRow("string-uint") << "ping.u" << input << output;
#if QT_VERSION >= 0x040200
output.clear();
output << qVariantFromValue(ushort(1));
QTest::newRow("string-uint16") << "ping.q" << input << output;
#endif
QTest::newRow("string-int64") << "ping.x" << input << (QVariantList() << qVariantFromValue(1LL));
QTest::newRow("string-uint64") << "ping.t" << input << (QVariantList() << qVariantFromValue(1ULL));
QTest::newRow("string-double") << "ping.d" << input << (QVariantList() << qVariantFromValue(1.0));
// two args (must be strings!)
input.clear();
input << QString("Hello") << QString("World");
output = input;
QTest::newRow("two-strings") << "ping" << input << output;
QTest::newRow("two-strings") << "ping.ss" << input << output;
// this should drop one of the arguments
output.removeLast();
QTest::newRow("last-dropped") << "ping.s" << input << output;
}
void tst_QDBusInterface::call()
{
QDBusConnection &con = QDBus::sessionBus();
QDBusInterface *iface = con.findInterface(con.baseService(), QLatin1String("/"),
TEST_INTERFACE_NAME);
QFETCH(QString, method);
QFETCH(QVariantList, input);
QFETCH(QVariantList, output);
QDBusMessage reply;
// try first callWithArgs:
reply = iface->callWithArgs(method, input);
QCOMPARE(reply.type(), QDBusMessage::ReplyMessage);
if (!output.isEmpty()) {
QCOMPARE(reply.count(), output.count());
QVERIFY(compare(reply, output));
}
// try the template methods
if (input.isEmpty())
reply = iface->call(method);
else if (input.count() == 1)
switch (input.at(0).type())
{
case QVariant::Int:
reply = iface->call(method, input.at(0).toInt());
break;
case QVariant::UInt:
reply = iface->call(method, input.at(0).toUInt());
break;
case QVariant::String:
reply = iface->call(method, input.at(0).toString());
break;
default:
QFAIL("Unknown type. Please update the test case");
break;
}
else
reply = iface->call(method, input.at(0).toString(), input.at(1).toString());
QCOMPARE(reply.type(), QDBusMessage::ReplyMessage);
if (!output.isEmpty()) {
QCOMPARE(reply.count(), output.count());
QVERIFY(compare(reply, output));
}
}
void tst_QDBusInterface::introspect()
{
QDBusConnection &con = QDBus::sessionBus();
QDBusInterface *iface = con.findInterface(QDBus::sessionBus().baseService(), QLatin1String("/"),
TEST_INTERFACE_NAME);
const QMetaObject *mo = iface->metaObject();
qDebug("Improve to a better testcase of QDBusMetaObject");
QCOMPARE(mo->methodCount() - mo->methodOffset(), 3);
QVERIFY(mo->indexOfSignal(TEST_SIGNAL_NAME "(QString)") != -1);
QCOMPARE(mo->propertyCount() - mo->propertyOffset(), 1);
QVERIFY(mo->indexOfProperty("prop1") != -1);
iface->deleteLater();
}
void tst_QDBusInterface::signal()
{
QDBusConnection &con = QDBus::sessionBus();
QDBusInterface *iface = con.findInterface(con.baseService(), QLatin1String("/"),
TEST_INTERFACE_NAME);
QString arg = "So long and thanks for all the fish";
{
Spy spy;
spy.connect(iface, SIGNAL(somethingHappened(QString)), SLOT(spySlot(QString)));
emitSignal(TEST_INTERFACE_NAME, TEST_SIGNAL_NAME, arg);
QCOMPARE(spy.count, 1);
QCOMPARE(spy.received, arg);
}
iface->deleteLater();
}
QTEST_MAIN(tst_QDBusInterface)
#include "tst_qdbusinterface.moc"
|