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

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/compositor.h"
0011 #include "wayland/display.h"
0012 #include "wayland/slide.h"
0013 
0014 #include "KWayland/Client/compositor.h"
0015 #include "KWayland/Client/connection_thread.h"
0016 #include "KWayland/Client/event_queue.h"
0017 #include "KWayland/Client/region.h"
0018 #include "KWayland/Client/registry.h"
0019 #include "KWayland/Client/slide.h"
0020 #include "KWayland/Client/surface.h"
0021 
0022 class TestSlide : public QObject
0023 {
0024     Q_OBJECT
0025 public:
0026     explicit TestSlide(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::SlideManagerInterface *m_slideManagerInterface;
0038     KWayland::Client::ConnectionThread *m_connection;
0039     KWayland::Client::Compositor *m_compositor;
0040     KWayland::Client::SlideManager *m_slideManager;
0041     KWayland::Client::EventQueue *m_queue;
0042     QThread *m_thread;
0043 };
0044 
0045 static const QString s_socketName = QStringLiteral("kwayland-test-wayland-slide-0");
0046 
0047 TestSlide::TestSlide(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 TestSlide::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 slideSpy(&registry, &KWayland::Client::Registry::slideAnnounced);
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_slideManagerInterface = new SlideManagerInterface(m_display, m_display);
0101 
0102     QVERIFY(slideSpy.wait());
0103     m_slideManager = registry.createSlideManager(slideSpy.first().first().value<quint32>(), slideSpy.first().last().value<quint32>(), this);
0104 }
0105 
0106 void TestSlide::cleanup()
0107 {
0108 #define CLEANUP(variable)   \
0109     if (variable) {         \
0110         delete variable;    \
0111         variable = nullptr; \
0112     }
0113     CLEANUP(m_compositor)
0114     CLEANUP(m_slideManager)
0115     CLEANUP(m_queue)
0116     if (m_connection) {
0117         m_connection->deleteLater();
0118         m_connection = nullptr;
0119     }
0120     if (m_thread) {
0121         m_thread->quit();
0122         m_thread->wait();
0123         delete m_thread;
0124         m_thread = nullptr;
0125     }
0126     CLEANUP(m_display)
0127 #undef CLEANUP
0128     // these are the children of the display
0129     m_compositorInterface = nullptr;
0130     m_slideManagerInterface = nullptr;
0131 }
0132 
0133 void TestSlide::testCreate()
0134 {
0135     QSignalSpy serverSurfaceCreated(m_compositorInterface, &KWin::CompositorInterface::surfaceCreated);
0136     std::unique_ptr<KWayland::Client::Surface> surface(m_compositor->createSurface());
0137     QVERIFY(serverSurfaceCreated.wait());
0138 
0139     auto serverSurface = serverSurfaceCreated.first().first().value<KWin::SurfaceInterface *>();
0140     QSignalSpy slideChanged(serverSurface, &KWin::SurfaceInterface::slideOnShowHideChanged);
0141 
0142     auto slide = m_slideManager->createSlide(surface.get(), surface.get());
0143     slide->setLocation(KWayland::Client::Slide::Location::Top);
0144     slide->setOffset(15);
0145     slide->commit();
0146     surface->commit(KWayland::Client::Surface::CommitFlag::None);
0147 
0148     QVERIFY(slideChanged.wait());
0149     QCOMPARE(serverSurface->slideOnShowHide()->location(), KWin::SlideInterface::Location::Top);
0150     QCOMPARE(serverSurface->slideOnShowHide()->offset(), 15);
0151 
0152     // and destroy
0153     QSignalSpy destroyedSpy(serverSurface->slideOnShowHide(), &QObject::destroyed);
0154     delete slide;
0155     QVERIFY(destroyedSpy.wait());
0156 }
0157 
0158 void TestSlide::testSurfaceDestroy()
0159 {
0160     using namespace KWin;
0161     QSignalSpy serverSurfaceCreated(m_compositorInterface, &CompositorInterface::surfaceCreated);
0162     std::unique_ptr<KWayland::Client::Surface> surface(m_compositor->createSurface());
0163     QVERIFY(serverSurfaceCreated.wait());
0164 
0165     auto serverSurface = serverSurfaceCreated.first().first().value<SurfaceInterface *>();
0166     QSignalSpy slideChanged(serverSurface, &SurfaceInterface::slideOnShowHideChanged);
0167 
0168     std::unique_ptr<KWayland::Client::Slide> slide(m_slideManager->createSlide(surface.get()));
0169     slide->commit();
0170     surface->commit(KWayland::Client::Surface::CommitFlag::None);
0171     QVERIFY(slideChanged.wait());
0172     auto serverSlide = serverSurface->slideOnShowHide();
0173     QVERIFY(serverSlide);
0174 
0175     // destroy the parent surface
0176     QSignalSpy surfaceDestroyedSpy(serverSurface, &QObject::destroyed);
0177     QSignalSpy slideDestroyedSpy(serverSlide, &QObject::destroyed);
0178     surface.reset();
0179     QVERIFY(surfaceDestroyedSpy.wait());
0180     QVERIFY(slideDestroyedSpy.isEmpty());
0181     // destroy the slide
0182     slide.reset();
0183     QVERIFY(slideDestroyedSpy.wait());
0184 }
0185 
0186 QTEST_GUILESS_MAIN(TestSlide)
0187 #include "test_wayland_slide.moc"