QtBase  v6.3.1
tst_qdbusconnection.h
Go to the documentation of this file.
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 <QTest>
35 #include <QTestEventLoop>
36 #include <QDBusMessage>
37 #include <QDBusConnection>
38 #include <QDBusServer>
39 #include <QDBusVirtualObject>
40 
41 class BaseObject: public QObject
42 {
43  Q_OBJECT
44  Q_CLASSINFO("D-Bus Interface", "local.BaseObject")
45 public:
46  BaseObject(QObject *parent = nullptr) : QObject(parent) { }
47 public slots:
48  void anotherMethod() { }
49 };
50 
51 class MyObject: public BaseObject
52 {
53  Q_OBJECT
54 public slots:
55  void method(const QDBusMessage &msg);
56 
57 public:
58  static QString path;
59  int callCount;
61 };
62 
64 {
65  Q_OBJECT
66 public slots:
67  void method(const QDBusMessage &msg);
68 
69 public:
70  static QString path;
72  int callCount;
74 };
75 
76 class SignalReceiver : public QObject
77 {
78  Q_OBJECT
79 public:
83 
84 public slots:
86  void oneSlot() { ++signalsReceived; }
89 };
90 
92 {
93  Q_OBJECT
94 
95 public:
96  static int hookCallCount;
98 
99 public slots:
100  void init();
101  void cleanup();
102 
103 private slots:
104  void noConnection();
105  void connectToBus();
106  void connectToPeer();
107  void connect();
108  void send();
109  void sendWithGui();
110  void sendAsync();
111  void sendSignal();
112  void sendSignalToName();
113  void sendSignalToOtherName();
114 
115  void registerObject_data();
116  void registerObject();
117  void registerObjectWithInterface_data();
118  void registerObjectWithInterface();
119  void registerObjectPeer_data();
120  void registerObjectPeer();
121  void registerObject2();
122  void registerObjectPeer2();
123 
124  void registerQObjectChildren();
125  void registerQObjectChildrenPeer();
126 
127  void callSelf();
128  void callSelfByAnotherName_data();
129  void callSelfByAnotherName();
130  void multipleInterfacesInQObject();
131 
132  void connectSignal();
133  void slotsWithLessParameters();
134  void nestedCallWithCallback();
135 
136  void serviceRegistrationRaceCondition();
137 
138  void registerVirtualObject();
139  void callVirtualObject();
140  void callVirtualObjectLocal();
141  void pendingCallWhenDisconnected();
142 
143 public:
144  QString serviceName() const { return "org.qtproject.Qt.Autotests.QDBusConnection"; }
145  bool callMethod(const QDBusConnection &conn, const QString &path);
146  bool callMethod(const QDBusConnection &conn, const QString &path, const QString &interface);
147  bool callMethodPeer(const QDBusConnection &conn, const QString &path);
148 };
149 
150 class QDBusSpy: public QObject
151 {
152  Q_OBJECT
153 public slots:
154  void handlePing(const QString &str) { args.clear(); args << str; }
155  void asyncReply(const QDBusMessage &msg) { args = msg.arguments(); }
156 
157 public:
159 };
160 
161 class MyServer : public QDBusServer
162 {
163  Q_OBJECT
164 public:
165  MyServer(QString path) : m_path(path), m_connections()
166  {
167  connect(this, SIGNAL(newConnection(QDBusConnection)), SLOT(handleConnection(QDBusConnection)));
168  }
169 
171  {
172  QDBusConnection conn(c);
173  if (!conn.registerObject(m_path, &m_obj, QDBusConnection::ExportAllSlots))
174  return false;
175  if (!(conn.objectRegisteredAt(m_path) == &m_obj))
176  return false;
177  return true;
178  }
179 
181  {
182  Q_FOREACH (const QString &name, m_connections) {
184  return false;
185  }
186  return true;
187  }
188 
190  {
191  Q_FOREACH (const QString &name, m_connections) {
193  c.unregisterObject(m_path);
194  }
195  }
196 
197 public slots:
199  {
200  m_connections << c.name();
201  QVERIFY(isConnected());
202  QVERIFY(c.isConnected());
205  }
206 
207 private:
208  MyObject m_obj;
209  QString m_path;
210  QStringList m_connections;
211 };
212 
213 class MyServer2 : public QDBusServer
214 {
215  Q_OBJECT
216 public:
217  MyServer2() : m_conn("none")
218  {
220  }
221 
223  {
224  return m_conn;
225  }
226 
227 public slots:
229  {
230  m_conn = c;
231  QVERIFY(isConnected());
232  QVERIFY(m_conn.isConnected());
234  }
235 
236 private:
237  MyObject m_obj;
238  QDBusConnection m_conn;
239 };
240 
241 class TestObject : public QObject
242 {
243 Q_OBJECT
244 public:
247 
249 
250 public slots:
251  void test0() { func = "test0"; }
252  void test1(int i) { func = "test1 " + QString::number(i); }
253  int test2() { func = "test2"; return 43; }
254  int test3(int i) { func = "test2"; return i + 1; }
255 };
256 
258 {
259  Q_OBJECT
260 public:
261  int count;
264 
265 public slots:
266  void countUp() { ++count; emit done(); }
267 signals:
268  void done();
269 };
270 
272 {
273  Q_OBJECT
274 public:
276 
277  QString introspect(const QString & /* path */) const override
278  {
279  return QString();
280  }
281 
283  ++callCount;
285 
286  if (success) {
287  QDBusMessage reply = message.createReply(replyArguments);
288  connection.send(reply);
289  }
291  return success;
292  }
293 signals:
295 
296 public:
299  mutable int callCount;
300  bool success;
301 };
302 
303 
304 #endif // TST_QDBUSCONNECTION_H
305 
small capitals from c petite p scientific i
[1]
Definition: afcover.h:80
const char msg[]
Definition: arch.cpp:46
BaseObject(QObject *parent=nullptr)
[0]
Definition: myobject.h:58
static QString path
void method(const QDBusMessage &msg)
MyObject(QObject *parent=nullptr)
MyObjectWithoutInterface(QObject *parent=nullptr)
void method(const QDBusMessage &msg)
void handleConnection(const QDBusConnection &c)
QDBusConnection connection()
void unregisterObject()
void handleConnection(const QDBusConnection &c)
MyServer(QString path)
bool registerObject(const QDBusConnection &c)
bool registerObject()
The QDBusConnection class represents a connection to the D-Bus bus daemon.
bool isConnected() const
QObject * objectRegisteredAt(const QString &path) const
bool registerObject(const QString &path, QObject *object, RegisterOptions options=ExportAdaptors)
The QDBusMessage class represents one message sent or received over the D-Bus bus.
Definition: qdbusmessage.h:58
The QDBusServer class provides peer-to-peer communication between processes on the same computer.
Definition: qdbusserver.h:57
void newConnection(const QDBusConnection &connection)
bool isConnected() const
void handlePing(const QString &str)
void asyncReply(const QDBusMessage &msg)
QList< QVariant > args
The QDBusVirtualObject class is used to handle several DBus paths with one class.
void clear()
Definition: qlist.h:445
The QObject class is the base class of all Qt objects.
Definition: qobject.h:125
QObject * parent() const
Definition: qobject.h:409
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Definition: qobject.cpp:2772
The QString class provides a Unicode character string.
Definition: qstring.h:388
static QString number(int, int base=10)
Definition: qstring.cpp:7538
The QStringList class provides a list of strings.
static QTestEventLoop & instance()
void oneSlot(const QString &arg)
TestObject(QObject *parent=nullptr)
int test3(int i)
void test1(int i)
QVariantList replyArguments
QString introspect(const QString &) const override
bool handleMessage(const QDBusMessage &message, const QDBusConnection &connection) override
QDBusMessage lastMessage
void messageReceived(const QDBusMessage &message) const
QString serviceName() const
bool callMethodPeer(const QDBusConnection &conn, const QString &path)
bool callMethod(const QDBusConnection &conn, const QString &path)
QString str
[2]
#define true
Definition: ftrandom.c:51
#define QString()
Definition: parse-defines.h:51
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char * interface
DBusConnection * connection
#define SLOT(a)
Definition: qobjectdefs.h:87
#define SIGNAL(a)
Definition: qobjectdefs.h:88
GLenum GLuint GLenum GLsizei const GLchar * message
Definition: qopengl.h:270
GLenum GLenum GLsizei count
GLuint name
GLenum func
Definition: qopenglext.h:663
const GLubyte * c
Definition: qopenglext.h:12701
GLsizei const GLchar *const * path
Definition: qopenglext.h:4283
SSL_CTX int(*) void arg)
#define QVERIFY(statement)
Definition: qtestcase.h:64
#define Q_OBJECT
Definition: qtmetamacros.h:158
#define Q_CLASSINFO(name, value)
Definition: qtmetamacros.h:88
#define slots
Definition: qtmetamacros.h:76
#define signals
Definition: qtmetamacros.h:77
#define emit
Definition: qtmetamacros.h:85
QNetworkReply * reply