1 /*************************************************************************************
2  *  Copyright 2016 by Sebastian Kügler <sebas@kde.org>                               *
3  *                                                                                   *
4  *  This library is free software; you can redistribute it and/or                    *
5  *  modify it under the terms of the GNU Lesser General Public                       *
6  *  License as published by the Free Software Foundation; either                     *
7  *  version 2.1 of the License, or (at your option) any later version.               *
8  *                                                                                   *
9  *  This library is distributed in the hope that it will be useful,                  *
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of                   *
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                *
12  *  Lesser General Public License for more details.                                  *
13  *                                                                                   *
14  *  You should have received a copy of the GNU Lesser General Public                 *
15  *  License along with this library; if not, write to the Free Software              *
16  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA       *
17  *************************************************************************************/
18 #include <QCoreApplication>
19 #include <QObject>
20 #include <QSignalSpy>
21 #include <QtTest>
22 
23 #include <KWayland/Client/connection_thread.h>
24 #include <KWayland/Client/dpms.h>
25 #include <KWayland/Client/registry.h>
26 
27 #include "waylandtestserver.h"
28 
29 static const QString s_socketName = QStringLiteral("disman-test-wayland-backend-0");
30 
31 Q_LOGGING_CATEGORY(DISMAN, "disman")
32 
33 using namespace KWayland::Client;
34 
35 class TestDpmsClient : public QObject
36 {
37     Q_OBJECT
38 
39 public:
40     explicit TestDpmsClient(QObject* parent = nullptr);
41 
42 Q_SIGNALS:
43     void dpmsAnnounced();
44 
45 private Q_SLOTS:
46 
47     void initTestCase();
48     void cleanupTestCase();
49     void testDpmsConnect();
50 
51 private:
52     ConnectionThread* m_connection;
53     QThread* m_thread;
54     Registry* m_registry;
55 
56     Disman::WaylandTestServer* m_server;
57 };
58 
TestDpmsClient(QObject * parent)59 TestDpmsClient::TestDpmsClient(QObject* parent)
60     : QObject(parent)
61     , m_server(nullptr)
62 {
63     setenv("WAYLAND_DISPLAY", s_socketName.toLocal8Bit().constData(), true);
64     m_server = new Disman::WaylandTestServer(this);
65     m_server->start();
66 }
67 
initTestCase()68 void TestDpmsClient::initTestCase()
69 {
70     // setup connection
71     m_connection = new KWayland::Client::ConnectionThread;
72     m_connection->setSocketName(s_socketName);
73     QSignalSpy connectedSpy(m_connection, SIGNAL(connected()));
74     m_connection->setSocketName(s_socketName);
75 
76     m_thread = new QThread(this);
77     m_connection->moveToThread(m_thread);
78     m_thread->start();
79 
80     m_connection->initConnection();
81     QVERIFY(connectedSpy.wait());
82 
83     QSignalSpy dpmsSpy(this, &TestDpmsClient::dpmsAnnounced);
84 
85     m_connection->initConnection();
86     QVERIFY(connectedSpy.wait(100));
87 
88     m_registry = new KWayland::Client::Registry;
89     m_registry->create(m_connection);
90     QObject::connect(m_registry, &Registry::interfacesAnnounced, this, [this] {
91         const bool hasDpms = m_registry->hasInterface(Registry::Interface::Dpms);
92         if (hasDpms) {
93             qDebug() << QStringLiteral("Compositor provides a DpmsManager");
94         } else {
95             qDebug() << QStringLiteral("Compositor does not provid a DpmsManager");
96         }
97         emit this->dpmsAnnounced();
98     });
99     m_registry->setup();
100 
101     QVERIFY(dpmsSpy.wait(100));
102 }
103 
cleanupTestCase()104 void TestDpmsClient::cleanupTestCase()
105 {
106     m_thread->exit();
107     m_thread->wait();
108     delete m_registry;
109     delete m_thread;
110     delete m_connection;
111 }
112 
testDpmsConnect()113 void TestDpmsClient::testDpmsConnect()
114 {
115     QVERIFY(m_registry->isValid());
116 }
117 
118 QTEST_GUILESS_MAIN(TestDpmsClient)
119 
120 #include "testkwaylanddpms.moc"
121