File indexing completed on 2024-10-13 03:42:25

0001 /*
0002     SPDX-FileCopyrightText: 2014 Alejandro Fiestas Olivares <afiestas@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #ifndef SOLID_QTEST_DBUS_H
0008 #define SOLID_QTEST_DBUS_H
0009 
0010 #include <QDebug>
0011 #include <QProcess>
0012 #include <QTest>
0013 #include <stdlib.h>
0014 
0015 // clang-format off
0016 #define QTEST_GUILESS_MAIN_SYSTEM_DBUS(TestObject) \
0017 int main(int argc, char *argv[]) \
0018 { \
0019     QProcess dbus; \
0020     dbus.start("dbus-launch", QStringList()); \
0021     dbus.waitForFinished(10000);    \
0022     QByteArray session = dbus.readLine(); \
0023     if (session.isEmpty()) { \
0024         qFatal("Couldn't execute new dbus session"); \
0025     } \
0026     int pos = session.indexOf('='); \
0027     setenv("DBUS_SYSTEM_BUS_ADDRESS", session.right(session.size() - pos - 1).trimmed(), 1); \
0028     qDebug() << session.right(session.size() - pos - 1).trimmed(); \
0029     session = dbus.readLine(); \
0030     pos = session.indexOf('='); \
0031     QByteArray pid = session.right(session.size() - pos - 1).trimmed(); \
0032     QCoreApplication app( argc, argv ); \
0033     app.setApplicationName( QLatin1String("qttest") ); \
0034     TestObject tc; \
0035     int result = QTest::qExec( &tc, argc, argv ); \
0036     dbus.start("kill", QStringList() << "-9" << pid); \
0037     dbus.waitForFinished(); \
0038     return result; \
0039 }
0040 #endif //SOLID_QTEST_DBUS_H