1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Copyright (C) 2016 Intel Corporation. |
5 | ** Contact: https://www.qt.io/licensing/ |
6 | ** |
7 | ** This file is part of the test suite of the Qt Toolkit. |
8 | ** |
9 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
10 | ** Commercial License Usage |
11 | ** Licensees holding valid commercial Qt licenses may use this file in |
12 | ** accordance with the commercial license agreement provided with the |
13 | ** Software or, alternatively, in accordance with the terms contained in |
14 | ** a written agreement between you and The Qt Company. For licensing terms |
15 | ** and conditions see https://www.qt.io/terms-conditions. For further |
16 | ** information use the contact form at https://www.qt.io/contact-us. |
17 | ** |
18 | ** GNU General Public License Usage |
19 | ** Alternatively, this file may be used under the terms of the GNU |
20 | ** General Public License version 3 as published by the Free Software |
21 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
22 | ** included in the packaging of this file. Please review the following |
23 | ** information to ensure the GNU General Public License requirements will |
24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
25 | ** |
26 | ** $QT_END_LICENSE$ |
27 | ** |
28 | ****************************************************************************/ |
29 | |
30 | #ifndef TST_QDBUSCONNECTION_H |
31 | #define TST_QDBUSCONNECTION_H |
32 | |
33 | #include <QObject> |
34 | #include <QtDBus/QtDBus> |
35 | #include <QtTest/QtTest> |
36 | |
37 | class BaseObject: public QObject |
38 | { |
39 | Q_OBJECT |
40 | Q_CLASSINFO("D-Bus Interface" , "local.BaseObject" ) |
41 | public: |
42 | BaseObject(QObject *parent = 0) : QObject(parent) { } |
43 | public slots: |
44 | void anotherMethod() { } |
45 | }; |
46 | |
47 | class MyObject: public BaseObject |
48 | { |
49 | Q_OBJECT |
50 | public slots: |
51 | void method(const QDBusMessage &msg); |
52 | |
53 | public: |
54 | static QString path; |
55 | int callCount; |
56 | MyObject(QObject *parent = 0) : BaseObject(parent), callCount(0) {} |
57 | }; |
58 | |
59 | class MyObjectWithoutInterface: public QObject |
60 | { |
61 | Q_OBJECT |
62 | public slots: |
63 | void method(const QDBusMessage &msg); |
64 | |
65 | public: |
66 | static QString path; |
67 | static QString interface; |
68 | int callCount; |
69 | MyObjectWithoutInterface(QObject *parent = 0) : QObject(parent), callCount(0) {} |
70 | }; |
71 | |
72 | class SignalReceiver : public QObject |
73 | { |
74 | Q_OBJECT |
75 | public: |
76 | QString argumentReceived; |
77 | int signalsReceived; |
78 | SignalReceiver() : signalsReceived(0) {} |
79 | |
80 | public slots: |
81 | void oneSlot(const QString &arg) { ++signalsReceived; argumentReceived = arg;} |
82 | void oneSlot() { ++signalsReceived; } |
83 | void exitLoop() { ++signalsReceived; QTestEventLoop::instance().exitLoop(); } |
84 | void secondCallWithCallback(); |
85 | }; |
86 | |
87 | class tst_QDBusConnection: public QObject |
88 | { |
89 | Q_OBJECT |
90 | |
91 | public: |
92 | static int hookCallCount; |
93 | tst_QDBusConnection(); |
94 | |
95 | public slots: |
96 | void init(); |
97 | void cleanup(); |
98 | |
99 | private slots: |
100 | void noConnection(); |
101 | void connectToBus(); |
102 | void connectToPeer(); |
103 | void connect(); |
104 | void send(); |
105 | void sendWithGui(); |
106 | void sendAsync(); |
107 | void sendSignal(); |
108 | void sendSignalToName(); |
109 | void sendSignalToOtherName(); |
110 | |
111 | void registerObject_data(); |
112 | void registerObject(); |
113 | void registerObjectWithInterface_data(); |
114 | void registerObjectWithInterface(); |
115 | void registerObjectPeer_data(); |
116 | void registerObjectPeer(); |
117 | void registerObject2(); |
118 | void registerObjectPeer2(); |
119 | |
120 | void registerQObjectChildren(); |
121 | void registerQObjectChildrenPeer(); |
122 | |
123 | void callSelf(); |
124 | void callSelfByAnotherName_data(); |
125 | void callSelfByAnotherName(); |
126 | void multipleInterfacesInQObject(); |
127 | |
128 | void connectSignal(); |
129 | void slotsWithLessParameters(); |
130 | void nestedCallWithCallback(); |
131 | |
132 | void serviceRegistrationRaceCondition(); |
133 | |
134 | void registerVirtualObject(); |
135 | void callVirtualObject(); |
136 | void callVirtualObjectLocal(); |
137 | void pendingCallWhenDisconnected(); |
138 | |
139 | public: |
140 | QString serviceName() const { return "org.qtproject.Qt.Autotests.QDBusConnection" ; } |
141 | bool callMethod(const QDBusConnection &conn, const QString &path); |
142 | bool callMethod(const QDBusConnection &conn, const QString &path, const QString &interface); |
143 | bool callMethodPeer(const QDBusConnection &conn, const QString &path); |
144 | }; |
145 | |
146 | class QDBusSpy: public QObject |
147 | { |
148 | Q_OBJECT |
149 | public slots: |
150 | void handlePing(const QString &str) { args.clear(); args << str; } |
151 | void asyncReply(const QDBusMessage &msg) { args = msg.arguments(); } |
152 | |
153 | public: |
154 | QList<QVariant> args; |
155 | }; |
156 | |
157 | class MyServer : public QDBusServer |
158 | { |
159 | Q_OBJECT |
160 | public: |
161 | MyServer(QString path) : m_path(path), m_connections() |
162 | { |
163 | connect(asender: this, SIGNAL(newConnection(QDBusConnection)), SLOT(handleConnection(QDBusConnection))); |
164 | } |
165 | |
166 | bool registerObject(const QDBusConnection& c) |
167 | { |
168 | QDBusConnection conn(c); |
169 | if (!conn.registerObject(path: m_path, object: &m_obj, options: QDBusConnection::ExportAllSlots)) |
170 | return false; |
171 | if (!(conn.objectRegisteredAt(path: m_path) == &m_obj)) |
172 | return false; |
173 | return true; |
174 | } |
175 | |
176 | bool registerObject() |
177 | { |
178 | Q_FOREACH (const QString &name, m_connections) { |
179 | if (!registerObject(c: QDBusConnection(name))) |
180 | return false; |
181 | } |
182 | return true; |
183 | } |
184 | |
185 | void unregisterObject() |
186 | { |
187 | Q_FOREACH (const QString &name, m_connections) { |
188 | QDBusConnection c(name); |
189 | c.unregisterObject(path: m_path); |
190 | } |
191 | } |
192 | |
193 | public slots: |
194 | void handleConnection(const QDBusConnection& c) |
195 | { |
196 | m_connections << c.name(); |
197 | QVERIFY(isConnected()); |
198 | QVERIFY(c.isConnected()); |
199 | QVERIFY(registerObject(c)); |
200 | QTestEventLoop::instance().exitLoop(); |
201 | } |
202 | |
203 | private: |
204 | MyObject m_obj; |
205 | QString m_path; |
206 | QStringList m_connections; |
207 | }; |
208 | |
209 | class MyServer2 : public QDBusServer |
210 | { |
211 | Q_OBJECT |
212 | public: |
213 | MyServer2() : m_conn("none" ) |
214 | { |
215 | connect(asender: this, SIGNAL(newConnection(QDBusConnection)), SLOT(handleConnection(QDBusConnection))); |
216 | } |
217 | |
218 | QDBusConnection connection() |
219 | { |
220 | return m_conn; |
221 | } |
222 | |
223 | public slots: |
224 | void handleConnection(const QDBusConnection& c) |
225 | { |
226 | m_conn = c; |
227 | QVERIFY(isConnected()); |
228 | QVERIFY(m_conn.isConnected()); |
229 | QTestEventLoop::instance().exitLoop(); |
230 | } |
231 | |
232 | private: |
233 | MyObject m_obj; |
234 | QDBusConnection m_conn; |
235 | }; |
236 | |
237 | class TestObject : public QObject |
238 | { |
239 | Q_OBJECT |
240 | public: |
241 | TestObject(QObject *parent = 0) : QObject(parent) {} |
242 | ~TestObject() {} |
243 | |
244 | QString func; |
245 | |
246 | public slots: |
247 | void test0() { func = "test0" ; } |
248 | void test1(int i) { func = "test1 " + QString::number(i); } |
249 | int test2() { func = "test2" ; return 43; } |
250 | int test3(int i) { func = "test2" ; return i + 1; } |
251 | }; |
252 | |
253 | class RaceConditionSignalWaiter : public QObject |
254 | { |
255 | Q_OBJECT |
256 | public: |
257 | int count; |
258 | RaceConditionSignalWaiter() : count (0) {} |
259 | virtual ~RaceConditionSignalWaiter() {} |
260 | |
261 | public slots: |
262 | void countUp() { ++count; emit done(); } |
263 | signals: |
264 | void done(); |
265 | }; |
266 | |
267 | class VirtualObject: public QDBusVirtualObject |
268 | { |
269 | Q_OBJECT |
270 | public: |
271 | VirtualObject() :success(true) {} |
272 | |
273 | QString introspect(const QString & /* path */) const |
274 | { |
275 | return QString(); |
276 | } |
277 | |
278 | bool handleMessage(const QDBusMessage &message, const QDBusConnection &connection) { |
279 | ++callCount; |
280 | lastMessage = message; |
281 | |
282 | if (success) { |
283 | QDBusMessage reply = message.createReply(arguments: replyArguments); |
284 | connection.send(message: reply); |
285 | } |
286 | emit messageReceived(message); |
287 | return success; |
288 | } |
289 | signals: |
290 | void messageReceived(const QDBusMessage &message) const; |
291 | |
292 | public: |
293 | mutable QDBusMessage lastMessage; |
294 | QVariantList replyArguments; |
295 | mutable int callCount; |
296 | bool success; |
297 | }; |
298 | |
299 | |
300 | #endif // TST_QDBUSCONNECTION_H |
301 | |
302 | |