1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 Intel Corporation. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the test suite of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | |
29 | #include <QtDBus> |
30 | #include <QtTest> |
31 | |
32 | #ifdef Q_OS_WIN |
33 | # include <process.h> |
34 | # define getpid _getpid |
35 | #else |
36 | # include <sys/types.h> |
37 | # include <unistd.h> |
38 | #endif |
39 | |
40 | class tst_QDBusConnection_Delayed : public QObject |
41 | { |
42 | Q_OBJECT |
43 | private slots: |
44 | void delayedMessages(); |
45 | }; |
46 | |
47 | class Foo : public QObject |
48 | { |
49 | Q_OBJECT |
50 | Q_CLASSINFO("D-Bus Interface" , "org.qtproject.tst_qdbusconnection_delayed.Foo" ) |
51 | public slots: |
52 | int bar() { return 42; } |
53 | }; |
54 | |
55 | static bool executedOnce = false; |
56 | |
57 | void tst_QDBusConnection_Delayed::delayedMessages() |
58 | { |
59 | if (executedOnce) |
60 | QSKIP("This test can only be executed once" ); |
61 | executedOnce = true; |
62 | |
63 | int argc = 1; |
64 | char *argv[] = { const_cast<char *>("tst_qdbusconnection_delayed" ), 0 }; |
65 | QCoreApplication app(argc, argv); |
66 | |
67 | QDBusConnection session = QDBusConnection::sessionBus(); |
68 | QVERIFY(session.isConnected()); |
69 | QVERIFY(!session.baseService().isEmpty()); |
70 | |
71 | QDBusConnection other = QDBusConnection::connectToBus(type: QDBusConnection::SessionBus, name: "other" ); |
72 | QVERIFY(other.isConnected()); |
73 | QVERIFY(!other.baseService().isEmpty()); |
74 | |
75 | // make a method call: those should work even if delivery is disabled |
76 | QVERIFY(session.interface()->isServiceRegistered(other.baseService())); |
77 | |
78 | // acquire a name in the main session bus connection: the effect is immediate |
79 | QString name = "org.qtproject.tst_qdbusconnection_delayed-" + |
80 | QString::number(getpid()); |
81 | QVERIFY(session.registerService(name)); |
82 | QVERIFY(other.interface()->isServiceRegistered(name)); |
83 | |
84 | // make an asynchronous call to a yet-unregistered object |
85 | QDBusPendingCallWatcher pending(other.asyncCall(message: QDBusMessage::createMethodCall(destination: name, path: "/foo" , interface: QString(), method: "bar" ))); |
86 | |
87 | // sleep the main thread without running the event loop; |
88 | // the call must not be delivered |
89 | QTest::qSleep(ms: 1000); |
90 | QVERIFY(!pending.isFinished()); |
91 | |
92 | // now register the object |
93 | Foo foo; |
94 | session.registerObject(path: "/foo" , object: &foo, options: QDBusConnection::ExportAllSlots); |
95 | |
96 | connect(sender: &pending, signal: &QDBusPendingCallWatcher::finished, |
97 | receiver: &QTestEventLoop::instance(), slot: &QTestEventLoop::exitLoop); |
98 | QTestEventLoop::instance().enterLoop(secs: 2); |
99 | QVERIFY(!QTestEventLoop::instance().timeout()); |
100 | QVERIFY(pending.isFinished()); |
101 | QVERIFY2(!pending.isError(), pending.error().name().toLatin1()); |
102 | QVERIFY(!pending.reply().arguments().isEmpty()); |
103 | QCOMPARE(pending.reply().arguments().at(0), QVariant(42)); |
104 | } |
105 | |
106 | QTEST_APPLESS_MAIN(tst_QDBusConnection_Delayed) |
107 | |
108 | #include "tst_qdbusconnection_delayed.moc" |
109 | |