File indexing completed on 2024-04-14 15:37:31

0001 /*
0002  *  SPDX-FileCopyrightText: 2015 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 "backendmanager_p.h"
0013 #include "config.h"
0014 #include "configmonitor.h"
0015 #include "getconfigoperation.h"
0016 #include "output.h"
0017 #include "setconfigoperation.h"
0018 
0019 #include "waylandtestserver.h"
0020 
0021 Q_LOGGING_CATEGORY(KSCREEN_WAYLAND, "kscreen.kwayland")
0022 
0023 using namespace KScreen;
0024 
0025 class TestKWaylandConfig : public QObject
0026 {
0027     Q_OBJECT
0028 
0029 public:
0030     explicit TestKWaylandConfig(QObject *parent = nullptr);
0031 
0032 private Q_SLOTS:
0033     void initTestCase();
0034     void cleanupTestCase();
0035 
0036     void changeConfig();
0037     void testPositionChange();
0038     void testRotationChange();
0039     void testRotationChange_data();
0040     void testScaleChange();
0041     void testModeChange();
0042     void testApplyOnPending();
0043 
0044 private:
0045     WaylandTestServer *m_server;
0046 };
0047 
0048 TestKWaylandConfig::TestKWaylandConfig(QObject *parent)
0049     : QObject(parent)
0050     , m_server(nullptr)
0051 {
0052     qputenv("KSCREEN_LOGGING", "false");
0053 }
0054 
0055 void TestKWaylandConfig::initTestCase()
0056 {
0057     setenv("KSCREEN_BACKEND", "kwayland", 1);
0058     KScreen::BackendManager::instance()->shutdownBackend();
0059 
0060     // This is how KWayland will pick up the right socket,
0061     // and thus connect to our internal test server.
0062     setenv("WAYLAND_DISPLAY", s_socketName.toLocal8Bit().constData(), 1);
0063 
0064     m_server = new WaylandTestServer(this);
0065     m_server->start();
0066 }
0067 
0068 void TestKWaylandConfig::cleanupTestCase()
0069 {
0070     qDebug() << "Shutting down";
0071     KScreen::BackendManager::instance()->shutdownBackend();
0072     delete m_server;
0073 }
0074 
0075 void TestKWaylandConfig::changeConfig()
0076 {
0077     auto op = new GetConfigOperation();
0078     QVERIFY(op->exec());
0079     auto config = op->config();
0080     QVERIFY(config);
0081 
0082     // Prepare monitor & spy
0083     KScreen::ConfigMonitor *monitor = KScreen::ConfigMonitor::instance();
0084     monitor->addConfig(config);
0085     QSignalSpy configSpy(monitor, &KScreen::ConfigMonitor::configurationChanged);
0086 
0087     // The first output is currently disabled, let's try to enable it
0088     auto output = config->outputs().first();
0089     QVERIFY(output->isEnabled() == false);
0090     output->setEnabled(true);
0091     output->setCurrentModeId(QStringLiteral("76"));
0092 
0093     auto output2 = config->outputs()[2]; // is this id stable enough?
0094     output2->setPos(QPoint(4000, 1080));
0095     output2->setRotation(KScreen::Output::Left);
0096 
0097     QSignalSpy serverSpy(m_server, &WaylandTestServer::configChanged);
0098     auto sop = new SetConfigOperation(config, this);
0099     sop->exec(); // fire and forget...
0100 
0101     QVERIFY(configSpy.wait());
0102     // check if the server changed
0103     QCOMPARE(serverSpy.count(), 1);
0104 
0105     QCOMPARE(configSpy.count(), 1);
0106 
0107     monitor->removeConfig(config);
0108     m_server->showOutputs();
0109 }
0110 
0111 void TestKWaylandConfig::testPositionChange()
0112 {
0113     auto op = new GetConfigOperation();
0114     QVERIFY(op->exec());
0115     auto config = op->config();
0116     QVERIFY(config);
0117 
0118     // Prepare monitor & spy
0119     KScreen::ConfigMonitor *monitor = KScreen::ConfigMonitor::instance();
0120     monitor->addConfig(config);
0121     QSignalSpy configSpy(monitor, &KScreen::ConfigMonitor::configurationChanged);
0122 
0123     auto output = config->outputs()[2]; // is this id stable enough?
0124     auto new_pos = QPoint(3840, 1080);
0125     output->setPos(new_pos);
0126 
0127     QSignalSpy serverSpy(m_server, &WaylandTestServer::configChanged);
0128     auto sop = new SetConfigOperation(config, this);
0129     sop->exec(); // fire and forget...
0130 
0131     QVERIFY(configSpy.wait());
0132     // check if the server changed
0133     QCOMPARE(serverSpy.count(), 1);
0134 
0135     QCOMPARE(configSpy.count(), 1);
0136 }
0137 
0138 void TestKWaylandConfig::testRotationChange_data()
0139 {
0140     QTest::addColumn<KScreen::Output::Rotation>("rotation");
0141     QTest::newRow("left") << KScreen::Output::Left;
0142     QTest::newRow("inverted") << KScreen::Output::Inverted;
0143     QTest::newRow("right") << KScreen::Output::Right;
0144     QTest::newRow("none") << KScreen::Output::None;
0145 }
0146 
0147 void TestKWaylandConfig::testRotationChange()
0148 {
0149     QFETCH(KScreen::Output::Rotation, rotation);
0150 
0151     auto op = new GetConfigOperation();
0152     QVERIFY(op->exec());
0153     auto config = op->config();
0154     QVERIFY(config);
0155 
0156     // Prepare monitor & spy
0157     KScreen::ConfigMonitor *monitor = KScreen::ConfigMonitor::instance();
0158     monitor->addConfig(config);
0159     QSignalSpy configSpy(monitor, &KScreen::ConfigMonitor::configurationChanged);
0160 
0161     auto output = config->outputs().first(); // is this id stable enough?
0162     output->setRotation(rotation);
0163 
0164     QSignalSpy serverSpy(m_server, &WaylandTestServer::configChanged);
0165     auto sop = new SetConfigOperation(config, this);
0166     sop->exec(); // fire and forget...
0167 
0168     QVERIFY(configSpy.wait());
0169     // check if the server changed
0170     QCOMPARE(serverSpy.count(), 1);
0171 
0172     QCOMPARE(configSpy.count(), 1);
0173 
0174     // Get a new config, then compare the output with the expected new value
0175     auto newop = new GetConfigOperation();
0176     QVERIFY(newop->exec());
0177     auto newconfig = newop->config();
0178     QVERIFY(newconfig);
0179 
0180     auto newoutput = newconfig->outputs().first();
0181     QCOMPARE(newoutput->rotation(), rotation);
0182 }
0183 
0184 void TestKWaylandConfig::testScaleChange()
0185 {
0186     auto op = new GetConfigOperation();
0187     QVERIFY(op->exec());
0188     auto config = op->config();
0189     QVERIFY(config);
0190 
0191     auto op2 = new GetConfigOperation();
0192     QVERIFY(op2->exec());
0193     auto config2 = op2->config();
0194     QVERIFY(config2);
0195 
0196     // Prepare monitor & spy
0197     KScreen::ConfigMonitor *monitor = KScreen::ConfigMonitor::instance();
0198     monitor->addConfig(config);
0199     monitor->addConfig(config2);
0200     QSignalSpy configSpy(monitor, &KScreen::ConfigMonitor::configurationChanged);
0201     QSignalSpy configSpy2(monitor, &KScreen::ConfigMonitor::configurationChanged);
0202 
0203     auto output2 = config2->outputs()[2]; // is this id stable enough?
0204     QSignalSpy outputSpy(output2.data(), &KScreen::Output::scaleChanged);
0205     QCOMPARE(output2->scale(), 1.0);
0206 
0207     auto output = config->outputs()[2]; // is this id stable enough?
0208     output->setScale(2);
0209 
0210     QSignalSpy serverSpy(m_server, &WaylandTestServer::configChanged);
0211     auto sop = new SetConfigOperation(config, this);
0212     sop->exec(); // fire and forget...
0213 
0214     QVERIFY(configSpy.wait());
0215     // check if the server changed
0216     QCOMPARE(serverSpy.count(), 1);
0217 
0218     QCOMPARE(configSpy.count(), 1);
0219     QCOMPARE(outputSpy.count(), 1);
0220     QCOMPARE(configSpy2.count(), 1);
0221     QCOMPARE(output2->scale(), 2.0);
0222 }
0223 
0224 void TestKWaylandConfig::testModeChange()
0225 {
0226     auto op = new GetConfigOperation();
0227     QVERIFY(op->exec());
0228     auto config = op->config();
0229     QVERIFY(config);
0230 
0231     KScreen::ConfigMonitor *monitor = KScreen::ConfigMonitor::instance();
0232     monitor->addConfig(config);
0233     QSignalSpy configSpy(monitor, &KScreen::ConfigMonitor::configurationChanged);
0234 
0235     auto output = config->outputs()[1]; // is this id stable enough?
0236 
0237     QString new_mode = QStringLiteral("74");
0238     output->setCurrentModeId(new_mode);
0239 
0240     QSignalSpy serverSpy(m_server, &WaylandTestServer::configChanged);
0241     auto sop = new SetConfigOperation(config, this);
0242     sop->exec();
0243 
0244     QVERIFY(configSpy.wait());
0245     // check if the server changed
0246     QCOMPARE(serverSpy.count(), 1);
0247 
0248     QCOMPARE(configSpy.count(), 1);
0249 }
0250 
0251 void TestKWaylandConfig::testApplyOnPending()
0252 {
0253     auto op = new GetConfigOperation();
0254     QVERIFY(op->exec());
0255     auto config = op->config();
0256     QVERIFY(config);
0257 
0258     auto op2 = new GetConfigOperation();
0259     QVERIFY(op2->exec());
0260     auto config2 = op2->config();
0261     QVERIFY(config2);
0262 
0263     KScreen::ConfigMonitor *monitor = KScreen::ConfigMonitor::instance();
0264     monitor->addConfig(config);
0265     QSignalSpy configSpy(monitor, &KScreen::ConfigMonitor::configurationChanged);
0266 
0267     auto output = config->outputs()[1]; // is this id stable enough?
0268 
0269     QCOMPARE(output->scale(), 1.0);
0270     output->setScale(2);
0271 
0272     QSignalSpy serverSpy(m_server, &WaylandTestServer::configChanged);
0273     QSignalSpy serverReceivedSpy(m_server, &WaylandTestServer::configReceived);
0274 
0275     m_server->suspendChanges(true);
0276 
0277     new SetConfigOperation(config, this);
0278 
0279     /* Apply next config */
0280 
0281     auto output2 = config2->outputs()[2]; // is this id stable enough?
0282     QCOMPARE(output2->scale(), 2.0);
0283     output2->setScale(3);
0284 
0285     new SetConfigOperation(config2, this);
0286 
0287     QVERIFY(serverReceivedSpy.wait());
0288     QCOMPARE(serverReceivedSpy.count(), 1);
0289     m_server->suspendChanges(false);
0290 
0291     QVERIFY(configSpy.wait());
0292     // check if the server changed
0293     QCOMPARE(serverSpy.count(), 1);
0294     QCOMPARE(configSpy.count(), 1);
0295     QCOMPARE(output->scale(), 2.0);
0296     QCOMPARE(output2->scale(), 3.0);
0297 
0298     QVERIFY(configSpy.wait());
0299     // check if the server changed
0300     QCOMPARE(serverSpy.count(), 2);
0301     QCOMPARE(configSpy.count(), 2);
0302     QCOMPARE(output2->scale(), 3.0);
0303 }
0304 
0305 QTEST_GUILESS_MAIN(TestKWaylandConfig)
0306 
0307 #include "testkwaylandconfig.moc"