author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Thu, 22 Apr 2010 16:15:11 +0300 | |
branch | RCL_3 |
changeset 14 | 8c4229025c0b |
parent 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
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 <QtCore/QObject> |
|
42 |
#include <QtCore/QVariant> |
|
43 |
#include <QtCore/QList> |
|
44 |
#include <QtCore/QVector> |
|
45 |
#include <QtTest/QtTest> |
|
46 |
#include <QtDBus> |
|
47 |
||
48 |
Q_DECLARE_METATYPE(QVariant) |
|
49 |
Q_DECLARE_METATYPE(QList<int>) |
|
50 |
Q_DECLARE_METATYPE(QVector<int>) |
|
51 |
||
52 |
class tst_QDBusLocalCalls: public QObject |
|
53 |
{ |
|
54 |
Q_OBJECT |
|
55 |
Q_CLASSINFO("D-Bus Interface", "local.tst_QDBusLocalCalls") |
|
56 |
||
57 |
QDBusConnection conn; |
|
58 |
public: |
|
59 |
tst_QDBusLocalCalls(); |
|
60 |
||
61 |
public Q_SLOTS: |
|
62 |
Q_SCRIPTABLE int echo(int value) |
|
63 |
{ return value; } |
|
64 |
||
65 |
Q_SCRIPTABLE QString echo(const QString &value) |
|
66 |
{ return value; } |
|
67 |
||
68 |
Q_SCRIPTABLE QDBusVariant echo(const QDBusVariant &value) |
|
69 |
{ return value; } |
|
70 |
||
71 |
Q_SCRIPTABLE QVector<int> echo(const QVector<int> &value) |
|
72 |
{ return value; } |
|
73 |
||
74 |
Q_SCRIPTABLE QString echo2(const QStringList &list, QString &out) |
|
75 |
{ out = list[1]; return list[0]; } |
|
76 |
||
77 |
Q_SCRIPTABLE void delayed(const QDBusMessage &msg) |
|
78 |
{ msg.setDelayedReply(true); } |
|
79 |
||
80 |
protected Q_SLOTS: |
|
81 |
void replyReceived(QDBusPendingCallWatcher *watcher); |
|
82 |
||
83 |
private Q_SLOTS: |
|
84 |
void initTestCase(); |
|
85 |
void makeInvalidCalls(); |
|
86 |
void makeCalls_data(); |
|
87 |
void makeCalls(); |
|
88 |
void makeCallsVariant_data(); |
|
89 |
void makeCallsVariant(); |
|
90 |
void makeCallsTwoRets(); |
|
91 |
void makeCallsComplex(); |
|
92 |
void makeDelayedCalls(); |
|
93 |
void asyncReplySignal(); |
|
94 |
||
95 |
private: |
|
96 |
QVariantList asyncReplyArgs; |
|
97 |
QDBusMessage doCall(const QDBusMessage &call); |
|
98 |
}; |
|
99 |
||
100 |
tst_QDBusLocalCalls::tst_QDBusLocalCalls() |
|
101 |
: conn(QDBusConnection::sessionBus()) |
|
102 |
{ |
|
103 |
} |
|
104 |
||
105 |
QDBusMessage tst_QDBusLocalCalls::doCall(const QDBusMessage &call) |
|
106 |
{ |
|
107 |
QFETCH_GLOBAL(bool, useAsync); |
|
108 |
if (useAsync) { |
|
109 |
QDBusPendingCall ac = conn.asyncCall(call); |
|
110 |
ac.waitForFinished(); |
|
111 |
return ac.reply(); |
|
112 |
} else { |
|
113 |
return conn.call(call); |
|
114 |
} |
|
115 |
} |
|
116 |
||
117 |
void tst_QDBusLocalCalls::replyReceived(QDBusPendingCallWatcher *watcher) |
|
118 |
{ |
|
119 |
asyncReplyArgs = watcher->reply().arguments(); |
|
120 |
QTestEventLoop::instance().exitLoop(); |
|
121 |
} |
|
122 |
||
123 |
void tst_QDBusLocalCalls::initTestCase() |
|
124 |
{ |
|
125 |
QVERIFY(conn.isConnected()); |
|
126 |
QVERIFY(conn.registerObject("/", this, QDBusConnection::ExportScriptableSlots)); |
|
127 |
||
128 |
QTest::addColumn<bool>("useAsync"); |
|
129 |
QTest::newRow("sync") << false; |
|
130 |
QTest::newRow("async") << true; |
|
131 |
} |
|
132 |
||
133 |
void tst_QDBusLocalCalls::makeCalls_data() |
|
134 |
{ |
|
135 |
QTest::addColumn<QVariant>("value"); |
|
136 |
QTest::newRow("int") << QVariant(42); |
|
137 |
QTest::newRow("string") << QVariant("Hello, world"); |
|
138 |
} |
|
139 |
||
140 |
void tst_QDBusLocalCalls::makeCallsVariant_data() |
|
141 |
{ |
|
142 |
makeCalls_data(); |
|
143 |
} |
|
144 |
||
145 |
void tst_QDBusLocalCalls::makeInvalidCalls() |
|
146 |
{ |
|
147 |
{ |
|
148 |
QDBusMessage callMsg = QDBusMessage::createMethodCall(conn.baseService(), |
|
149 |
"/", QString(), "echo"); |
|
150 |
QDBusMessage replyMsg = doCall(callMsg); |
|
151 |
QCOMPARE(int(replyMsg.type()), int(QDBusMessage::ErrorMessage)); |
|
152 |
||
153 |
QDBusError error(replyMsg); |
|
154 |
QCOMPARE(int(error.type()), int(QDBusError::UnknownMethod)); |
|
155 |
} |
|
156 |
||
157 |
{ |
|
158 |
QDBusMessage callMsg = QDBusMessage::createMethodCall(conn.baseService(), |
|
159 |
"/no_object", QString(), "echo"); |
|
160 |
QDBusMessage replyMsg = doCall(callMsg); |
|
161 |
QCOMPARE(int(replyMsg.type()), int(QDBusMessage::ErrorMessage)); |
|
162 |
||
163 |
QDBusError error(replyMsg); |
|
164 |
QCOMPARE(int(error.type()), int(QDBusError::UnknownObject)); |
|
165 |
} |
|
166 |
} |
|
167 |
||
168 |
void tst_QDBusLocalCalls::makeCalls() |
|
169 |
{ |
|
170 |
QFETCH(QVariant, value); |
|
171 |
QDBusMessage callMsg = QDBusMessage::createMethodCall(conn.baseService(), |
|
172 |
"/", QString(), "echo"); |
|
173 |
callMsg << value; |
|
174 |
QDBusMessage replyMsg = doCall(callMsg); |
|
175 |
||
176 |
QCOMPARE(int(replyMsg.type()), int(QDBusMessage::ReplyMessage)); |
|
177 |
||
178 |
QVariantList replyArgs = replyMsg.arguments(); |
|
179 |
QCOMPARE(replyArgs.count(), 1); |
|
180 |
QCOMPARE(replyArgs.at(0), value); |
|
181 |
} |
|
182 |
||
183 |
void tst_QDBusLocalCalls::makeCallsVariant() |
|
184 |
{ |
|
185 |
QFETCH(QVariant, value); |
|
186 |
QDBusMessage callMsg = QDBusMessage::createMethodCall(conn.baseService(), |
|
187 |
"/", QString(), "echo"); |
|
188 |
callMsg << qVariantFromValue(QDBusVariant(value)); |
|
189 |
QDBusMessage replyMsg = doCall(callMsg); |
|
190 |
||
191 |
QCOMPARE(int(replyMsg.type()), int(QDBusMessage::ReplyMessage)); |
|
192 |
||
193 |
QVariantList replyArgs = replyMsg.arguments(); |
|
194 |
QCOMPARE(replyArgs.count(), 1); |
|
195 |
||
196 |
const QVariant &reply = replyArgs.at(0); |
|
197 |
QCOMPARE(reply.userType(), qMetaTypeId<QDBusVariant>()); |
|
198 |
QCOMPARE(qvariant_cast<QDBusVariant>(reply).variant(), value); |
|
199 |
} |
|
200 |
||
201 |
void tst_QDBusLocalCalls::makeCallsTwoRets() |
|
202 |
{ |
|
203 |
QDBusMessage callMsg = QDBusMessage::createMethodCall(conn.baseService(), |
|
204 |
"/", QString(), "echo2"); |
|
205 |
callMsg << (QStringList() << "One" << "Two"); |
|
206 |
QDBusMessage replyMsg = doCall(callMsg); |
|
207 |
||
208 |
QCOMPARE(int(replyMsg.type()), int(QDBusMessage::ReplyMessage)); |
|
209 |
||
210 |
QVariantList replyArgs = replyMsg.arguments(); |
|
211 |
QCOMPARE(replyArgs.count(), 2); |
|
212 |
QCOMPARE(replyArgs.at(0).toString(), QString::fromLatin1("One")); |
|
213 |
QCOMPARE(replyArgs.at(1).toString(), QString::fromLatin1("Two")); |
|
214 |
} |
|
215 |
||
216 |
void tst_QDBusLocalCalls::makeCallsComplex() |
|
217 |
{ |
|
218 |
qDBusRegisterMetaType<QList<int> >(); |
|
219 |
qDBusRegisterMetaType<QVector<int> >(); |
|
220 |
||
221 |
QList<int> value; |
|
222 |
value << 1 << -42 << 47; |
|
223 |
QDBusMessage callMsg = QDBusMessage::createMethodCall(conn.baseService(), |
|
224 |
"/", QString(), "echo"); |
|
225 |
callMsg << qVariantFromValue(value); |
|
226 |
QDBusMessage replyMsg = doCall(callMsg); |
|
227 |
||
228 |
QCOMPARE(int(replyMsg.type()), int(QDBusMessage::ReplyMessage)); |
|
229 |
||
230 |
QVariantList replyArgs = replyMsg.arguments(); |
|
231 |
QCOMPARE(replyArgs.count(), 1); |
|
232 |
const QVariant &reply = replyArgs.at(0); |
|
233 |
QCOMPARE(reply.userType(), qMetaTypeId<QDBusArgument>()); |
|
234 |
QCOMPARE(qdbus_cast<QList<int> >(reply), value); |
|
235 |
} |
|
236 |
||
237 |
void tst_QDBusLocalCalls::makeDelayedCalls() |
|
238 |
{ |
|
239 |
QDBusMessage callMsg = QDBusMessage::createMethodCall(conn.baseService(), |
|
240 |
"/", QString(), "delayed"); |
|
241 |
QTest::ignoreMessage(QtWarningMsg, "QDBusConnection: cannot call local method 'delayed' at object / (with signature '') on blocking mode"); |
|
242 |
QDBusMessage replyMsg = doCall(callMsg); |
|
243 |
QCOMPARE(int(replyMsg.type()), int(QDBusMessage::ErrorMessage)); |
|
244 |
||
245 |
QDBusError error(replyMsg); |
|
246 |
QCOMPARE(int(error.type()), int(QDBusError::InternalError)); |
|
247 |
} |
|
248 |
||
249 |
void tst_QDBusLocalCalls::asyncReplySignal() |
|
250 |
{ |
|
251 |
QFETCH_GLOBAL(bool, useAsync); |
|
252 |
if (!useAsync) |
|
253 |
return; // this test only works in async mode |
|
254 |
||
255 |
QDBusMessage callMsg = QDBusMessage::createMethodCall(conn.baseService(), |
|
256 |
"/", QString(), "echo"); |
|
257 |
callMsg << "Hello World"; |
|
258 |
QDBusPendingCall ac = conn.asyncCall(callMsg); |
|
259 |
if (ac.isFinished()) |
|
260 |
QSKIP("Test ignored: the local-loop async call is already finished", SkipAll); |
|
261 |
||
262 |
QDBusPendingCallWatcher watch(ac); |
|
263 |
connect(&watch, SIGNAL(finished(QDBusPendingCallWatcher*)), |
|
264 |
SLOT(replyReceived(QDBusPendingCallWatcher*))); |
|
265 |
||
266 |
QTestEventLoop::instance().enterLoop(2); |
|
267 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
268 |
||
269 |
QVERIFY(ac.isFinished()); |
|
270 |
QVERIFY(!ac.isError()); |
|
271 |
||
272 |
QVERIFY(!asyncReplyArgs.isEmpty()); |
|
273 |
QCOMPARE(asyncReplyArgs.at(0).toString(), QString("Hello World")); |
|
274 |
} |
|
275 |
||
276 |
QTEST_MAIN(tst_QDBusLocalCalls) |
|
277 |
#include "tst_qdbuslocalcalls.moc" |