File indexing completed on 2024-04-14 03:56:53

0001 /*
0002     SPDX-FileCopyrightText: 2016 Martin Gräßlin <mgraesslin@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #include <QFileSystemWatcher>
0008 #include <QProcess>
0009 #include <QSignalSpy>
0010 #include <QStandardPaths>
0011 #include <QTest>
0012 
0013 class TestKWindowsystemPlatformWayland : public QObject
0014 {
0015     Q_OBJECT
0016 private Q_SLOTS:
0017     void initTestCase();
0018     void cleanupTestCase();
0019 
0020     void testWithHelper();
0021 
0022 private:
0023     std::unique_ptr<QProcess> m_westonProcess;
0024 };
0025 
0026 void TestKWindowsystemPlatformWayland::initTestCase()
0027 {
0028     const QString westonExec = QStandardPaths::findExecutable(QStringLiteral("weston"));
0029 
0030     // start Weston
0031     m_westonProcess.reset(new QProcess);
0032     m_westonProcess->setProgram(westonExec);
0033     m_westonProcess->setArguments(QStringList({QStringLiteral("--socket=kwindowsystem-platform-wayland-0"), QStringLiteral("--backend=headless-backend.so")}));
0034     m_westonProcess->start();
0035     if (!m_westonProcess->waitForStarted()) {
0036         m_westonProcess.reset();
0037         QSKIP("Weston could not be started");
0038     }
0039 
0040     // wait for the socket to appear
0041     QTest::qWait(500);
0042 
0043     QDir runtimeDir(qgetenv("XDG_RUNTIME_DIR"));
0044     if (runtimeDir.exists(QStringLiteral("kwindowsystem-platform-wayland-0"))) {
0045         // already there
0046         return;
0047     }
0048 
0049     std::unique_ptr<QFileSystemWatcher> socketWatcher(new QFileSystemWatcher(QStringList({runtimeDir.absolutePath()})));
0050     QSignalSpy socketSpy(socketWatcher.get(), &QFileSystemWatcher::directoryChanged);
0051     QVERIFY(socketSpy.isValid());
0052 
0053     // limit to max of 10 waits
0054     for (int i = 0; i < 10; i++) {
0055         QVERIFY(socketSpy.wait());
0056         if (runtimeDir.exists(QStringLiteral("kwindowsystem-platform-wayland-0"))) {
0057             return;
0058         }
0059     }
0060 }
0061 
0062 void TestKWindowsystemPlatformWayland::cleanupTestCase()
0063 {
0064     if (!m_westonProcess) {
0065         return;
0066     }
0067     m_westonProcess->terminate();
0068     QVERIFY(m_westonProcess->waitForFinished());
0069     m_westonProcess.reset();
0070 }
0071 
0072 void TestKWindowsystemPlatformWayland::testWithHelper()
0073 {
0074     // This test starts a helper binary on platform wayland
0075     // it executes the actual test and will return 0 on success, and an error value otherwise
0076     QString processName = QFINDTESTDATA("kwindowsystem_platform_wayland_helper");
0077     QVERIFY(!processName.isEmpty());
0078 
0079     QProcess helper;
0080     QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
0081     env.insert(QStringLiteral("WAYLAND_DISPLAY"), QStringLiteral("kwindowsystem-platform-wayland-0"));
0082     helper.setProgram(processName);
0083     helper.setProcessEnvironment(env);
0084     helper.start();
0085     QVERIFY(helper.waitForFinished());
0086     QCOMPARE(helper.exitCode(), 0);
0087 }
0088 
0089 QTEST_GUILESS_MAIN(TestKWindowsystemPlatformWayland)
0090 #include "kwindowsystem_platform_wayland_test.moc"