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
|
// SPDX-FileCopyrightText: 2021 Daniel Vrátil <dvratil@kde.org>
//
// SPDX-License-Identifier: MIT
#include <QCoreApplication>
#include <QObject>
#include <QProcess>
#include <QTimer>
#include <QString>
#include <iostream>
class DBusServer : public QObject {
Q_OBJECT
public:
inline static const QString serviceName = QStringLiteral("cz.dvratil.qcorodbustest");
inline static const QString interfaceName = QStringLiteral("cz.dvratil.qcorodbustest");
inline static const QString objectPath = QStringLiteral("/");
explicit DBusServer();
void run();
public Q_SLOTS:
void foo();
QString ping(const QString &ping);
void blockFor(int seconds);
QString blockAndReturn(int seconds);
QString blockAndReturnMultipleArguments(int seconds, bool &out);
void quit();
private:
QTimer mSuicideTimer;
};
// We must run the DBus server into its own process due to QTBUG-92107 (asyncCall blocks if the
// remote service is registered in the same process (even if it lives in a different thread)
#define DBUS_TEST_MAIN(TestClass) \
bool startDBusServer(QProcess &process) { \
process.start(QStringLiteral(TESTDBUSSERVER_EXECUTABLE), QStringList{}); \
if (!process.waitForStarted()) { \
std::cerr << "Failed to start testdbusserver" << std::endl; \
return false; \
} \
return true; \
} \
\
bool stopDBusServer(QProcess &process) { \
process.waitForFinished(); \
if (process.exitCode() != 0) { \
std::cerr << "testdbuserver terminated with exit code " << process.exitCode() \
<< std::endl; \
return false; \
} \
return true; \
} \
\
int main(int argc, char **argv) { \
QCoreApplication app(argc, argv); \
QProcess dbusServer; \
if (!startDBusServer(dbusServer)) \
return 1; \
TestClass testClass; \
QTEST_SET_MAIN_SOURCE_PATH \
const int result = QTest::qExec(&testClass, argc, argv); \
if (!stopDBusServer(dbusServer)) \
return 1; \
return result; \
}
|