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
|
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtCore/QObject>
#include <QtCore/QVariant>
#include <QtCore/QList>
#include <QtCore/QVector>
#include <QtTest/QtTest>
#include <QtDBus>
Q_DECLARE_METATYPE(QVariant)
Q_DECLARE_METATYPE(QList<int>)
Q_DECLARE_METATYPE(QVector<int>)
class tst_QDBusLocalCalls: public QObject
{
Q_OBJECT
Q_CLASSINFO("D-Bus Interface", "local.tst_QDBusLocalCalls")
QDBusConnection conn;
public:
tst_QDBusLocalCalls();
public Q_SLOTS:
Q_SCRIPTABLE int echo(int value)
{ return value; }
Q_SCRIPTABLE QString echo(const QString &value)
{ return value; }
Q_SCRIPTABLE QDBusVariant echo(const QDBusVariant &value)
{ return value; }
Q_SCRIPTABLE QVector<int> echo(const QVector<int> &value)
{ return value; }
Q_SCRIPTABLE QString echo2(const QStringList &list, QString &out)
{ out = list[1]; return list[0]; }
Q_SCRIPTABLE void delayed(const QDBusMessage &msg)
{ msg.setDelayedReply(true); }
protected Q_SLOTS:
void replyReceived(QDBusPendingCallWatcher *watcher);
private Q_SLOTS:
void initTestCase();
void makeInvalidCalls();
void makeCalls_data();
void makeCalls();
void makeCallsVariant_data();
void makeCallsVariant();
void makeCallsTwoRets();
void makeCallsComplex();
void makeDelayedCalls();
void asyncReplySignal();
private:
QVariantList asyncReplyArgs;
QDBusMessage doCall(const QDBusMessage &call);
};
tst_QDBusLocalCalls::tst_QDBusLocalCalls()
: conn(QDBusConnection::sessionBus())
{
}
QDBusMessage tst_QDBusLocalCalls::doCall(const QDBusMessage &call)
{
QFETCH_GLOBAL(bool, useAsync);
if (useAsync) {
QDBusPendingCall ac = conn.asyncCall(call);
ac.waitForFinished();
return ac.reply();
} else {
return conn.call(call);
}
}
void tst_QDBusLocalCalls::replyReceived(QDBusPendingCallWatcher *watcher)
{
asyncReplyArgs = watcher->reply().arguments();
QTestEventLoop::instance().exitLoop();
}
void tst_QDBusLocalCalls::initTestCase()
{
QVERIFY(conn.isConnected());
QVERIFY(conn.registerObject("/", this, QDBusConnection::ExportScriptableSlots));
QTest::addColumn<bool>("useAsync");
QTest::newRow("sync") << false;
QTest::newRow("async") << true;
}
void tst_QDBusLocalCalls::makeCalls_data()
{
QTest::addColumn<QVariant>("value");
QTest::newRow("int") << QVariant(42);
QTest::newRow("string") << QVariant("Hello, world");
}
void tst_QDBusLocalCalls::makeCallsVariant_data()
{
makeCalls_data();
}
void tst_QDBusLocalCalls::makeInvalidCalls()
{
{
QDBusMessage callMsg = QDBusMessage::createMethodCall(conn.baseService(),
"/", QString(), "echo");
QDBusMessage replyMsg = doCall(callMsg);
QCOMPARE(int(replyMsg.type()), int(QDBusMessage::ErrorMessage));
QDBusError error(replyMsg);
QCOMPARE(int(error.type()), int(QDBusError::UnknownMethod));
}
{
QDBusMessage callMsg = QDBusMessage::createMethodCall(conn.baseService(),
"/no_object", QString(), "echo");
QDBusMessage replyMsg = doCall(callMsg);
QCOMPARE(int(replyMsg.type()), int(QDBusMessage::ErrorMessage));
QDBusError error(replyMsg);
QCOMPARE(int(error.type()), int(QDBusError::UnknownObject));
}
}
void tst_QDBusLocalCalls::makeCalls()
{
QFETCH(QVariant, value);
QDBusMessage callMsg = QDBusMessage::createMethodCall(conn.baseService(),
"/", QString(), "echo");
callMsg << value;
QDBusMessage replyMsg = doCall(callMsg);
QCOMPARE(int(replyMsg.type()), int(QDBusMessage::ReplyMessage));
QVariantList replyArgs = replyMsg.arguments();
QCOMPARE(replyArgs.count(), 1);
QCOMPARE(replyArgs.at(0), value);
}
void tst_QDBusLocalCalls::makeCallsVariant()
{
QFETCH(QVariant, value);
QDBusMessage callMsg = QDBusMessage::createMethodCall(conn.baseService(),
"/", QString(), "echo");
callMsg << qVariantFromValue(QDBusVariant(value));
QDBusMessage replyMsg = doCall(callMsg);
QCOMPARE(int(replyMsg.type()), int(QDBusMessage::ReplyMessage));
QVariantList replyArgs = replyMsg.arguments();
QCOMPARE(replyArgs.count(), 1);
const QVariant &reply = replyArgs.at(0);
QCOMPARE(reply.userType(), qMetaTypeId<QDBusVariant>());
QCOMPARE(qvariant_cast<QDBusVariant>(reply).variant(), value);
}
void tst_QDBusLocalCalls::makeCallsTwoRets()
{
QDBusMessage callMsg = QDBusMessage::createMethodCall(conn.baseService(),
"/", QString(), "echo2");
callMsg << (QStringList() << "One" << "Two");
QDBusMessage replyMsg = doCall(callMsg);
QCOMPARE(int(replyMsg.type()), int(QDBusMessage::ReplyMessage));
QVariantList replyArgs = replyMsg.arguments();
QCOMPARE(replyArgs.count(), 2);
QCOMPARE(replyArgs.at(0).toString(), QString::fromLatin1("One"));
QCOMPARE(replyArgs.at(1).toString(), QString::fromLatin1("Two"));
}
void tst_QDBusLocalCalls::makeCallsComplex()
{
qDBusRegisterMetaType<QList<int> >();
qDBusRegisterMetaType<QVector<int> >();
QList<int> value;
value << 1 << -42 << 47;
QDBusMessage callMsg = QDBusMessage::createMethodCall(conn.baseService(),
"/", QString(), "echo");
callMsg << qVariantFromValue(value);
QDBusMessage replyMsg = doCall(callMsg);
QCOMPARE(int(replyMsg.type()), int(QDBusMessage::ReplyMessage));
QVariantList replyArgs = replyMsg.arguments();
QCOMPARE(replyArgs.count(), 1);
const QVariant &reply = replyArgs.at(0);
QCOMPARE(reply.userType(), qMetaTypeId<QDBusArgument>());
QCOMPARE(qdbus_cast<QList<int> >(reply), value);
}
void tst_QDBusLocalCalls::makeDelayedCalls()
{
QDBusMessage callMsg = QDBusMessage::createMethodCall(conn.baseService(),
"/", QString(), "delayed");
QTest::ignoreMessage(QtWarningMsg, "QDBusConnection: cannot call local method 'delayed' at object / (with signature '') on blocking mode");
QDBusMessage replyMsg = doCall(callMsg);
QCOMPARE(int(replyMsg.type()), int(QDBusMessage::ErrorMessage));
QDBusError error(replyMsg);
QCOMPARE(int(error.type()), int(QDBusError::InternalError));
}
void tst_QDBusLocalCalls::asyncReplySignal()
{
QFETCH_GLOBAL(bool, useAsync);
if (!useAsync)
return; // this test only works in async mode
QDBusMessage callMsg = QDBusMessage::createMethodCall(conn.baseService(),
"/", QString(), "echo");
callMsg << "Hello World";
QDBusPendingCall ac = conn.asyncCall(callMsg);
if (ac.isFinished())
QSKIP("Test ignored: the local-loop async call is already finished", SkipAll);
QDBusPendingCallWatcher watch(ac);
connect(&watch, SIGNAL(finished(QDBusPendingCallWatcher*)),
SLOT(replyReceived(QDBusPendingCallWatcher*)));
QTestEventLoop::instance().enterLoop(2);
QVERIFY(!QTestEventLoop::instance().timeout());
QVERIFY(ac.isFinished());
QVERIFY(!ac.isError());
QVERIFY(!asyncReplyArgs.isEmpty());
QCOMPARE(asyncReplyArgs.at(0).toString(), QString("Hello World"));
}
QTEST_MAIN(tst_QDBusLocalCalls)
#include "tst_qdbuslocalcalls.moc"
|