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

0001 /*
0002     SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@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/blur.h"
0011 #include "wayland/compositor.h"
0012 #include "wayland/display.h"
0013 
0014 #include "KWayland/Client/blur.h"
0015 #include "KWayland/Client/compositor.h"
0016 #include "KWayland/Client/connection_thread.h"
0017 #include "KWayland/Client/event_queue.h"
0018 #include "KWayland/Client/region.h"
0019 #include "KWayland/Client/registry.h"
0020 #include "KWayland/Client/surface.h"
0021 
0022 class TestBlur : public QObject
0023 {
0024     Q_OBJECT
0025 public:
0026     explicit TestBlur(QObject *parent = nullptr);
0027 private Q_SLOTS:
0028     void init();
0029     void cleanup();
0030 
0031     void testCreate();
0032     void testSurfaceDestroy();
0033 
0034 private:
0035     KWin::Display *m_display;
0036     KWin::CompositorInterface *m_compositorInterface;
0037     KWin::BlurManagerInterface *m_blurManagerInterface;
0038     KWayland::Client::ConnectionThread *m_connection;
0039     KWayland::Client::Compositor *m_compositor;
0040     KWayland::Client::BlurManager *m_blurManager;
0041     KWayland::Client::EventQueue *m_queue;
0042     QThread *m_thread;
0043 };
0044 
0045 static const QString s_socketName = QStringLiteral("kwayland-test-wayland-blur-0");
0046 
0047 TestBlur::TestBlur(QObject *parent)
0048     : QObject(parent)
0049     , m_display(nullptr)
0050     , m_compositorInterface(nullptr)
0051     , m_connection(nullptr)
0052     , m_compositor(nullptr)
0053     , m_queue(nullptr)
0054     , m_thread(nullptr)
0055 {
0056 }
0057 
0058 void TestBlur::init()
0059 {
0060     using namespace KWin;
0061     delete m_display;
0062     m_display = new KWin::Display(this);
0063     m_display->addSocketName(s_socketName);
0064     m_display->start();
0065     QVERIFY(m_display->isRunning());
0066 
0067     // setup connection
0068     m_connection = new KWayland::Client::ConnectionThread;
0069     QSignalSpy connectedSpy(m_connection, &KWayland::Client::ConnectionThread::connected);
0070     m_connection->setSocketName(s_socketName);
0071 
0072     m_thread = new QThread(this);
0073     m_connection->moveToThread(m_thread);
0074     m_thread->start();
0075 
0076     m_connection->initConnection();
0077     QVERIFY(connectedSpy.wait());
0078 
0079     m_queue = new KWayland::Client::EventQueue(this);
0080     QVERIFY(!m_queue->isValid());
0081     m_queue->setup(m_connection);
0082     QVERIFY(m_queue->isValid());
0083 
0084     KWayland::Client::Registry registry;
0085     QSignalSpy compositorSpy(&registry, &KWayland::Client::Registry::compositorAnnounced);
0086 
0087     QSignalSpy blurSpy(&registry, &KWayland::Client::Registry::blurAnnounced);
0088 
0089     QVERIFY(!registry.eventQueue());
0090     registry.setEventQueue(m_queue);
0091     QCOMPARE(registry.eventQueue(), m_queue);
0092     registry.create(m_connection->display());
0093     QVERIFY(registry.isValid());
0094     registry.setup();
0095 
0096     m_compositorInterface = new CompositorInterface(m_display, m_display);
0097     QVERIFY(compositorSpy.wait());
0098     m_compositor = registry.createCompositor(compositorSpy.first().first().value<quint32>(), compositorSpy.first().last().value<quint32>(), this);
0099 
0100     m_blurManagerInterface = new BlurManagerInterface(m_display, m_display);
0101     QVERIFY(blurSpy.wait());
0102     m_blurManager = registry.createBlurManager(blurSpy.first().first().value<quint32>(), blurSpy.first().last().value<quint32>(), this);
0103 }
0104 
0105 void TestBlur::cleanup()
0106 {
0107 #define CLEANUP(variable)   \
0108     if (variable) {         \
0109         delete variable;    \
0110         variable = nullptr; \
0111     }
0112     CLEANUP(m_compositor)
0113     CLEANUP(m_blurManager)
0114     CLEANUP(m_queue)
0115     if (m_connection) {
0116         m_connection->deleteLater();
0117         m_connection = nullptr;
0118     }
0119     if (m_thread) {
0120         m_thread->quit();
0121         m_thread->wait();
0122         delete m_thread;
0123         m_thread = nullptr;
0124     }
0125     CLEANUP(m_display)
0126 #undef CLEANUP
0127 
0128     // these are the children of the display
0129     m_compositorInterface = nullptr;
0130     m_blurManagerInterface = nullptr;
0131 }
0132 
0133 void TestBlur::testCreate()
0134 {
0135     QSignalSpy serverSurfaceCreated(m_compositorInterface, &KWin::CompositorInterface::surfaceCreated);
0136 
0137     std::unique_ptr<KWayland::Client::Surface> surface(m_compositor->createSurface());
0138     QVERIFY(serverSurfaceCreated.wait());
0139 
0140     auto serverSurface = serverSurfaceCreated.first().first().value<KWin::SurfaceInterface *>();
0141     QSignalSpy blurChanged(serverSurface, &KWin::SurfaceInterface::blurChanged);
0142 
0143     auto blur = m_blurManager->createBlur(surface.get(), surface.get());
0144     blur->setRegion(m_compositor->createRegion(QRegion(0, 0, 10, 20), nullptr));
0145     blur->commit();
0146     surface->commit(KWayland::Client::Surface::CommitFlag::None);
0147 
0148     QVERIFY(blurChanged.wait());
0149     QCOMPARE(serverSurface->blur()->region(), QRegion(0, 0, 10, 20));
0150 
0151     // and destroy
0152     QSignalSpy destroyedSpy(serverSurface->blur(), &QObject::destroyed);
0153     delete blur;
0154     QVERIFY(destroyedSpy.wait());
0155 }
0156 
0157 void TestBlur::testSurfaceDestroy()
0158 {
0159     QSignalSpy serverSurfaceCreated(m_compositorInterface, &KWin::CompositorInterface::surfaceCreated);
0160 
0161     std::unique_ptr<KWayland::Client::Surface> surface(m_compositor->createSurface());
0162     QVERIFY(serverSurfaceCreated.wait());
0163 
0164     auto serverSurface = serverSurfaceCreated.first().first().value<KWin::SurfaceInterface *>();
0165     QSignalSpy blurChanged(serverSurface, &KWin::SurfaceInterface::blurChanged);
0166 
0167     std::unique_ptr<KWayland::Client::Blur> blur(m_blurManager->createBlur(surface.get()));
0168     blur->setRegion(m_compositor->createRegion(QRegion(0, 0, 10, 20), nullptr));
0169     blur->commit();
0170     surface->commit(KWayland::Client::Surface::CommitFlag::None);
0171 
0172     QVERIFY(blurChanged.wait());
0173     QCOMPARE(serverSurface->blur()->region(), QRegion(0, 0, 10, 20));
0174 
0175     // destroy the parent surface
0176     QSignalSpy surfaceDestroyedSpy(serverSurface, &QObject::destroyed);
0177     QSignalSpy blurDestroyedSpy(serverSurface->blur(), &QObject::destroyed);
0178     surface.reset();
0179     QVERIFY(surfaceDestroyedSpy.wait());
0180     QVERIFY(blurDestroyedSpy.isEmpty());
0181     // destroy the blur
0182     blur.reset();
0183     QVERIFY(blurDestroyedSpy.wait());
0184 }
0185 
0186 QTEST_GUILESS_MAIN(TestBlur)
0187 #include "test_wayland_blur.moc"