File indexing completed on 2024-11-10 04:56:21

0001 /*
0002     SPDX-FileCopyrightText: 2018 David Edmundson <davidedmundson@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 // Qt
0007 #include <QSignalSpy>
0008 #include <QTest>
0009 // KWin
0010 #include "wayland/display.h"
0011 #include "wayland/output.h"
0012 #include "wayland/xdgoutput_v1.h"
0013 
0014 #include "KWayland/Client/connection_thread.h"
0015 #include "KWayland/Client/event_queue.h"
0016 #include "KWayland/Client/output.h"
0017 #include "KWayland/Client/registry.h"
0018 #include "KWayland/Client/xdgoutput.h"
0019 
0020 #include "../../../tests/fakeoutput.h"
0021 
0022 class TestXdgOutput : public QObject
0023 {
0024     Q_OBJECT
0025 public:
0026     explicit TestXdgOutput(QObject *parent = nullptr);
0027 private Q_SLOTS:
0028     void init();
0029     void cleanup();
0030     void testChanges();
0031 
0032 private:
0033     KWin::Display *m_display;
0034     std::unique_ptr<FakeOutput> m_outputHandle;
0035     KWin::OutputInterface *m_serverOutput;
0036     KWin::XdgOutputManagerV1Interface *m_serverXdgOutputManager;
0037     KWayland::Client::ConnectionThread *m_connection;
0038     KWayland::Client::EventQueue *m_queue;
0039     QThread *m_thread;
0040 };
0041 
0042 static const QString s_socketName = QStringLiteral("kwin-test-xdg-output-0");
0043 
0044 TestXdgOutput::TestXdgOutput(QObject *parent)
0045     : QObject(parent)
0046     , m_display(nullptr)
0047     , m_serverOutput(nullptr)
0048     , m_connection(nullptr)
0049     , m_thread(nullptr)
0050 {
0051 }
0052 
0053 void TestXdgOutput::init()
0054 {
0055     using namespace KWin;
0056     delete m_display;
0057     m_display = new KWin::Display(this);
0058     m_display->addSocketName(s_socketName);
0059     m_display->start();
0060     QVERIFY(m_display->isRunning());
0061 
0062     m_outputHandle = std::make_unique<FakeOutput>();
0063     m_outputHandle->setMode(QSize(1920, 1080), 60000);
0064     m_outputHandle->moveTo(QPoint(11, 12)); // not a sensible value for one monitor, but works for this test
0065     m_outputHandle->setScale(1.5);
0066     m_outputHandle->setName("testName");
0067     m_outputHandle->setManufacturer("foo");
0068     m_outputHandle->setModel("bar");
0069 
0070     m_serverOutput = new OutputInterface(m_display, m_outputHandle.get(), this);
0071 
0072     m_serverXdgOutputManager = new XdgOutputManagerV1Interface(m_display, this);
0073     m_serverXdgOutputManager->offer(m_serverOutput);
0074 
0075     // setup connection
0076     m_connection = new KWayland::Client::ConnectionThread;
0077     QSignalSpy connectedSpy(m_connection, &KWayland::Client::ConnectionThread::connected);
0078     m_connection->setSocketName(s_socketName);
0079 
0080     m_thread = new QThread(this);
0081     m_connection->moveToThread(m_thread);
0082     m_thread->start();
0083 
0084     m_connection->initConnection();
0085     QVERIFY(connectedSpy.wait());
0086 
0087     m_queue = new KWayland::Client::EventQueue(this);
0088     QVERIFY(!m_queue->isValid());
0089     m_queue->setup(m_connection);
0090     QVERIFY(m_queue->isValid());
0091 }
0092 
0093 void TestXdgOutput::cleanup()
0094 {
0095     if (m_queue) {
0096         delete m_queue;
0097         m_queue = nullptr;
0098     }
0099     if (m_thread) {
0100         m_thread->quit();
0101         m_thread->wait();
0102         delete m_thread;
0103         m_thread = nullptr;
0104     }
0105     delete m_connection;
0106     m_connection = nullptr;
0107 
0108     delete m_serverOutput;
0109     m_serverOutput = nullptr;
0110     m_outputHandle.reset();
0111 
0112     delete m_display;
0113     m_display = nullptr;
0114 }
0115 
0116 void TestXdgOutput::testChanges()
0117 {
0118     // verify the server modes
0119     using namespace KWin;
0120     KWayland::Client::Registry registry;
0121     QSignalSpy announced(&registry, &KWayland::Client::Registry::outputAnnounced);
0122     QSignalSpy xdgOutputAnnounced(&registry, &KWayland::Client::Registry::xdgOutputAnnounced);
0123 
0124     registry.setEventQueue(m_queue);
0125     registry.create(m_connection->display());
0126     QVERIFY(registry.isValid());
0127     registry.setup();
0128     QVERIFY(announced.wait());
0129     if (xdgOutputAnnounced.count() != 1) {
0130         QVERIFY(xdgOutputAnnounced.wait());
0131     }
0132 
0133     KWayland::Client::Output output;
0134     QSignalSpy outputChanged(&output, &KWayland::Client::Output::changed);
0135 
0136     output.setup(registry.bindOutput(announced.first().first().value<quint32>(), announced.first().last().value<quint32>()));
0137     QVERIFY(outputChanged.wait());
0138 
0139     std::unique_ptr<KWayland::Client::XdgOutputManager> xdgOutputManager(
0140         registry.createXdgOutputManager(xdgOutputAnnounced.first().first().value<quint32>(), xdgOutputAnnounced.first().last().value<quint32>(), this));
0141 
0142     std::unique_ptr<KWayland::Client::XdgOutput> xdgOutput(xdgOutputManager->getXdgOutput(&output, this));
0143     QSignalSpy xdgOutputChanged(xdgOutput.get(), &KWayland::Client::XdgOutput::changed);
0144 
0145     // check details are sent on client bind
0146     QVERIFY(xdgOutputChanged.wait());
0147     xdgOutputChanged.clear();
0148     QCOMPARE(xdgOutput->logicalPosition(), QPoint(11, 12));
0149     QCOMPARE(xdgOutput->logicalSize(), QSize(1280, 720));
0150     QCOMPARE(xdgOutput->name(), "testName");
0151     QCOMPARE(xdgOutput->description(), "foo bar");
0152 
0153     // change the logical position
0154     m_outputHandle->moveTo(QPoint(1000, 2000));
0155     QVERIFY(xdgOutputChanged.wait());
0156     QCOMPARE(xdgOutputChanged.count(), 1);
0157     QCOMPARE(xdgOutput->logicalPosition(), QPoint(1000, 2000));
0158     QEXPECT_FAIL("", "KWayland::Client::XdgOutput incorrectly handles partial updates", Continue);
0159     QCOMPARE(xdgOutput->logicalSize(), QSize(1280, 720));
0160 
0161     // change the logical size
0162     m_outputHandle->setScale(2);
0163     QVERIFY(xdgOutputChanged.wait());
0164     QCOMPARE(xdgOutputChanged.count(), 2);
0165     QEXPECT_FAIL("", "KWayland::Client::XdgOutput incorrectly handles partial updates", Continue);
0166     QCOMPARE(xdgOutput->logicalPosition(), QPoint(1000, 2000));
0167     QCOMPARE(xdgOutput->logicalSize(), QSize(960, 540));
0168 }
0169 
0170 QTEST_GUILESS_MAIN(TestXdgOutput)
0171 #include "test_xdg_output.moc"