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

0001 /*
0002     SPDX-FileCopyrightText: 2017 David Edmundson <davidedmundson@kde.org>
0003     SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 // Qt
0008 #include <QSignalSpy>
0009 #include <QTest>
0010 // KWin
0011 #include "wayland/compositor.h"
0012 #include "wayland/display.h"
0013 #include "wayland/server_decoration_palette.h"
0014 
0015 #include "qwayland-server-decoration-palette.h"
0016 
0017 #include "KWayland/Client/compositor.h"
0018 #include "KWayland/Client/connection_thread.h"
0019 #include "KWayland/Client/event_queue.h"
0020 #include "KWayland/Client/registry.h"
0021 #include "KWayland/Client/surface.h"
0022 
0023 class ServerSideDecorationPaletteManager : public QtWayland::org_kde_kwin_server_decoration_palette_manager
0024 {
0025 };
0026 
0027 class ServerSideDecorationPalette : public QObject, public QtWayland::org_kde_kwin_server_decoration_palette
0028 {
0029     Q_OBJECT
0030 
0031 public:
0032     ~ServerSideDecorationPalette()
0033     {
0034         release();
0035     }
0036 };
0037 
0038 class TestServerSideDecorationPalette : public QObject
0039 {
0040     Q_OBJECT
0041 public:
0042     explicit TestServerSideDecorationPalette(QObject *parent = nullptr);
0043 private Q_SLOTS:
0044     void init();
0045     void cleanup();
0046 
0047     void testCreateAndSet();
0048 
0049 private:
0050     KWin::Display *m_display;
0051     KWin::CompositorInterface *m_compositorInterface;
0052     KWin::ServerSideDecorationPaletteManagerInterface *m_paletteManagerInterface;
0053     KWayland::Client::ConnectionThread *m_connection;
0054     KWayland::Client::Compositor *m_compositor;
0055     ServerSideDecorationPaletteManager *m_paletteManager;
0056     KWayland::Client::EventQueue *m_queue;
0057     QThread *m_thread;
0058 };
0059 
0060 static const QString s_socketName = QStringLiteral("kwayland-test-wayland-decopalette-0");
0061 
0062 TestServerSideDecorationPalette::TestServerSideDecorationPalette(QObject *parent)
0063     : QObject(parent)
0064     , m_display(nullptr)
0065     , m_compositorInterface(nullptr)
0066     , m_connection(nullptr)
0067     , m_compositor(nullptr)
0068     , m_queue(nullptr)
0069     , m_thread(nullptr)
0070 {
0071 }
0072 
0073 void TestServerSideDecorationPalette::init()
0074 {
0075     using namespace KWin;
0076     delete m_display;
0077     m_display = new KWin::Display(this);
0078     m_display->addSocketName(s_socketName);
0079     m_display->start();
0080     QVERIFY(m_display->isRunning());
0081 
0082     // setup connection
0083     m_connection = new KWayland::Client::ConnectionThread;
0084     QSignalSpy connectedSpy(m_connection, &KWayland::Client::ConnectionThread::connected);
0085     m_connection->setSocketName(s_socketName);
0086 
0087     m_thread = new QThread(this);
0088     m_connection->moveToThread(m_thread);
0089     m_thread->start();
0090 
0091     m_connection->initConnection();
0092     QVERIFY(connectedSpy.wait());
0093 
0094     m_queue = new KWayland::Client::EventQueue(this);
0095     QVERIFY(!m_queue->isValid());
0096     m_queue->setup(m_connection);
0097     QVERIFY(m_queue->isValid());
0098 
0099     m_compositorInterface = new CompositorInterface(m_display, m_display);
0100     m_paletteManagerInterface = new ServerSideDecorationPaletteManagerInterface(m_display, m_display);
0101 
0102     KWayland::Client::Registry registry;
0103 
0104     connect(&registry, &KWayland::Client::Registry::interfaceAnnounced, this, [this, &registry](const QByteArray &interfaceName, quint32 name, quint32 version) {
0105         if (interfaceName == org_kde_kwin_server_decoration_palette_manager_interface.name) {
0106             m_paletteManager = new ServerSideDecorationPaletteManager();
0107             m_paletteManager->init(registry.registry(), name, version);
0108         }
0109     });
0110 
0111     QSignalSpy interfacesAnnouncedSpy(&registry, &KWayland::Client::Registry::interfacesAnnounced);
0112     QSignalSpy compositorSpy(&registry, &KWayland::Client::Registry::compositorAnnounced);
0113 
0114     QVERIFY(!registry.eventQueue());
0115     registry.setEventQueue(m_queue);
0116     QCOMPARE(registry.eventQueue(), m_queue);
0117     registry.create(m_connection->display());
0118     QVERIFY(registry.isValid());
0119     registry.setup();
0120     QVERIFY(interfacesAnnouncedSpy.wait());
0121 
0122     m_compositor = registry.createCompositor(compositorSpy.first().first().value<quint32>(), compositorSpy.first().last().value<quint32>(), this);
0123 
0124     QVERIFY(m_compositor);
0125     QVERIFY(m_paletteManager);
0126 }
0127 
0128 void TestServerSideDecorationPalette::cleanup()
0129 {
0130 #define CLEANUP(variable)   \
0131     if (variable) {         \
0132         delete variable;    \
0133         variable = nullptr; \
0134     }
0135     CLEANUP(m_compositor)
0136     CLEANUP(m_paletteManager)
0137     CLEANUP(m_queue)
0138     if (m_connection) {
0139         m_connection->deleteLater();
0140         m_connection = nullptr;
0141     }
0142     if (m_thread) {
0143         m_thread->quit();
0144         m_thread->wait();
0145         delete m_thread;
0146         m_thread = nullptr;
0147     }
0148     CLEANUP(m_compositorInterface)
0149     CLEANUP(m_paletteManagerInterface)
0150     CLEANUP(m_display)
0151 #undef CLEANUP
0152 }
0153 
0154 void TestServerSideDecorationPalette::testCreateAndSet()
0155 {
0156     QSignalSpy serverSurfaceCreated(m_compositorInterface, &KWin::CompositorInterface::surfaceCreated);
0157 
0158     std::unique_ptr<KWayland::Client::Surface> surface(m_compositor->createSurface());
0159     QVERIFY(serverSurfaceCreated.wait());
0160 
0161     auto serverSurface = serverSurfaceCreated.first().first().value<KWin::SurfaceInterface *>();
0162     QSignalSpy paletteCreatedSpy(m_paletteManagerInterface, &KWin::ServerSideDecorationPaletteManagerInterface::paletteCreated);
0163 
0164     QVERIFY(!m_paletteManagerInterface->paletteForSurface(serverSurface));
0165 
0166     auto palette = std::make_unique<ServerSideDecorationPalette>();
0167     palette->init(m_paletteManager->create(*surface.get()));
0168     QVERIFY(paletteCreatedSpy.wait());
0169     auto paletteInterface = paletteCreatedSpy.first().first().value<KWin::ServerSideDecorationPaletteInterface *>();
0170     QCOMPARE(m_paletteManagerInterface->paletteForSurface(serverSurface), paletteInterface);
0171 
0172     QCOMPARE(paletteInterface->palette(), QString());
0173 
0174     QSignalSpy changedSpy(paletteInterface, &KWin::ServerSideDecorationPaletteInterface::paletteChanged);
0175     palette->set_palette(QStringLiteral("foobar"));
0176     QVERIFY(changedSpy.wait());
0177     QCOMPARE(paletteInterface->palette(), QString("foobar"));
0178 
0179     // and destroy
0180     QSignalSpy destroyedSpy(paletteInterface, &QObject::destroyed);
0181     palette.reset();
0182     QVERIFY(destroyedSpy.wait());
0183     QVERIFY(!m_paletteManagerInterface->paletteForSurface(serverSurface));
0184 }
0185 
0186 QTEST_GUILESS_MAIN(TestServerSideDecorationPalette)
0187 #include "test_server_side_decoration_palette.moc"