File indexing completed on 2024-04-21 16:17:28

0001 /*
0002  *  SPDX-FileCopyrightText: 2016 Sebastian Kügler <sebas@kde.org>
0003  *
0004  *  SPDX-License-Identifier: LGPL-2.1-or-later
0005  */
0006 
0007 #include <QCoreApplication>
0008 #include <QObject>
0009 #include <QSignalSpy>
0010 #include <QtTest>
0011 
0012 #include <KWayland/Client/connection_thread.h>
0013 #include <KWayland/Client/dpms.h>
0014 #include <KWayland/Client/registry.h>
0015 
0016 #include "waylandtestserver.h"
0017 
0018 static const QString s_socketName = QStringLiteral("libkscreen-test-wayland-backend-0");
0019 // static const QString s_socketName = QStringLiteral("wayland-0");
0020 
0021 Q_LOGGING_CATEGORY(KSCREEN, "kscreen")
0022 
0023 using namespace KWayland::Client;
0024 
0025 class TestDpmsClient : public QObject
0026 {
0027     Q_OBJECT
0028 
0029 public:
0030     explicit TestDpmsClient(QObject *parent = nullptr);
0031 
0032 Q_SIGNALS:
0033     void dpmsAnnounced();
0034 
0035 private Q_SLOTS:
0036 
0037     void initTestCase();
0038     void cleanupTestCase();
0039     void testDpmsConnect();
0040 
0041 private:
0042     ConnectionThread *m_connection;
0043     QThread *m_thread;
0044     Registry *m_registry;
0045 
0046     KScreen::WaylandTestServer *m_server;
0047 };
0048 
0049 TestDpmsClient::TestDpmsClient(QObject *parent)
0050     : QObject(parent)
0051     , m_server(nullptr)
0052 {
0053     setenv("WAYLAND_DISPLAY", s_socketName.toLocal8Bit().constData(), true);
0054     m_server = new KScreen::WaylandTestServer(this);
0055     m_server->start();
0056 }
0057 
0058 void TestDpmsClient::initTestCase()
0059 {
0060     // setup connection
0061     m_connection = new KWayland::Client::ConnectionThread;
0062     m_connection->setSocketName(s_socketName);
0063     QSignalSpy connectedSpy(m_connection, SIGNAL(connected()));
0064     m_connection->setSocketName(s_socketName);
0065 
0066     m_thread = new QThread(this);
0067     m_connection->moveToThread(m_thread);
0068     m_thread->start();
0069 
0070     m_connection->initConnection();
0071     QVERIFY(connectedSpy.wait());
0072 
0073     QSignalSpy dpmsSpy(this, &TestDpmsClient::dpmsAnnounced);
0074 
0075     m_connection->initConnection();
0076     QVERIFY(connectedSpy.wait(100));
0077 
0078     m_registry = new KWayland::Client::Registry;
0079     m_registry->create(m_connection);
0080     QObject::connect(m_registry, &Registry::interfacesAnnounced, this, [this] {
0081         const bool hasDpms = m_registry->hasInterface(Registry::Interface::Dpms);
0082         if (hasDpms) {
0083             qDebug() << QStringLiteral("Compositor provides a DpmsManager");
0084         } else {
0085             qDebug() << QStringLiteral("Compositor does not provid a DpmsManager");
0086         }
0087         Q_EMIT  this->dpmsAnnounced();
0088     });
0089     m_registry->setup();
0090 
0091     QVERIFY(dpmsSpy.wait(100));
0092 }
0093 
0094 void TestDpmsClient::cleanupTestCase()
0095 {
0096     m_thread->exit();
0097     m_thread->wait();
0098     delete m_registry;
0099     delete m_thread;
0100     delete m_connection;
0101 }
0102 
0103 void TestDpmsClient::testDpmsConnect()
0104 {
0105     QVERIFY(m_registry->isValid());
0106 }
0107 
0108 QTEST_GUILESS_MAIN(TestDpmsClient)
0109 
0110 #include "testkwaylanddpms.moc"