diff --git a/autotests/wayland_client/CMakeLists.txt b/autotests/wayland_client/CMakeLists.txt index 708d92fc20..130cd4eaa5 100644 --- a/autotests/wayland_client/CMakeLists.txt +++ b/autotests/wayland_client/CMakeLists.txt @@ -77,3 +77,21 @@ add_dependencies(testWaylandShell wayland-client-fullscreen-shell) target_link_libraries( testWaylandShell Qt5::Test Qt5::Gui Wayland::Client) add_test(kwin-testWaylandShell testWaylandShell) ecm_mark_as_test(testWaylandShell) + +######################################################## +# Test WaylandSurface +######################################################## +set( testWaylandSurface_SRCS + test_wayland_surface.cpp + ${KWIN_SOURCE_DIR}/wayland_client/compositor.cpp + ${KWIN_SOURCE_DIR}/wayland_client/connection_thread.cpp + ${KWIN_SOURCE_DIR}/wayland_client/registry.cpp + ${KWIN_SOURCE_DIR}/wayland_client/fullscreen_shell.cpp + ${KWIN_SOURCE_DIR}/wayland_client/surface.cpp + ${CMAKE_BINARY_DIR}/wayland_protocols/wayland-client-fullscreen-shell.c + ) +add_executable(testWaylandSurface ${testWaylandSurface_SRCS}) +add_dependencies(testWaylandSurface wayland-client-fullscreen-shell) +target_link_libraries( testWaylandSurface Qt5::Test Qt5::Gui Wayland::Client) +add_test(kwin-testWaylandSurface testWaylandSurface) +ecm_mark_as_test(testWaylandSurface) diff --git a/autotests/wayland_client/test_wayland_surface.cpp b/autotests/wayland_client/test_wayland_surface.cpp new file mode 100644 index 0000000000..a2936feb3c --- /dev/null +++ b/autotests/wayland_client/test_wayland_surface.cpp @@ -0,0 +1,142 @@ +/******************************************************************** +KWin - the KDE window manager +This file is part of the KDE project. + +Copyright (C) 2014 Martin Gräßlin + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*********************************************************************/ +// Qt +#include +// KWin +#include "../../wayland_client/compositor.h" +#include "../../wayland_client/connection_thread.h" +#include "../../wayland_client/surface.h" +#include "../../wayland_client/registry.h" +// Wayland +#include + +class TestWaylandSurface : public QObject +{ + Q_OBJECT +public: + explicit TestWaylandSurface(QObject *parent = nullptr); +private Q_SLOTS: + void init(); + void cleanup(); + + void testStaticAccessor(); + +private: + QProcess *m_westonProcess; +}; + +static const QString s_socketName = QStringLiteral("kwin-test-wayland-surface-0"); + +TestWaylandSurface::TestWaylandSurface(QObject *parent) + : QObject(parent) + , m_westonProcess(nullptr) +{ +} + +void TestWaylandSurface::init() +{ + QVERIFY(!m_westonProcess); + // starts weston + m_westonProcess = new QProcess(this); + m_westonProcess->setProgram(QStringLiteral("weston")); + + m_westonProcess->setArguments(QStringList({QStringLiteral("--socket=%1").arg(s_socketName), + QStringLiteral("--use-pixman")})); + m_westonProcess->start(); + QVERIFY(m_westonProcess->waitForStarted()); + + // wait for the socket to appear + QDir runtimeDir(qgetenv("XDG_RUNTIME_DIR")); + if (runtimeDir.exists(s_socketName)) { + return; + } + QFileSystemWatcher *socketWatcher = new QFileSystemWatcher(QStringList({runtimeDir.absolutePath()}), this); + QSignalSpy socketSpy(socketWatcher, SIGNAL(directoryChanged(QString))); + + // limit to maximum of 10 waits + for (int i = 0; i < 10; ++i) { + QVERIFY(socketSpy.wait()); + if (runtimeDir.exists(s_socketName)) { + delete socketWatcher; + return; + } + } +} + +void TestWaylandSurface::cleanup() +{ + // terminates weston + m_westonProcess->terminate(); + QVERIFY(m_westonProcess->waitForFinished()); + delete m_westonProcess; + m_westonProcess = nullptr; +} + +void TestWaylandSurface::testStaticAccessor() +{ + if (m_westonProcess->state() != QProcess::Running) { + QSKIP("This test requires a running wayland server"); + } + KWin::Wayland::ConnectionThread connection; + QSignalSpy connectedSpy(&connection, SIGNAL(connected())); + connection.setSocketName(s_socketName); + connection.initConnection(); + QVERIFY(connectedSpy.wait()); + + KWin::Wayland::Registry registry; + QSignalSpy compositorSpy(®istry, SIGNAL(compositorAnnounced(quint32,quint32))); + registry.create(connection.display()); + QVERIFY(registry.isValid()); + registry.setup(); + wl_display_flush(connection.display()); + QVERIFY(compositorSpy.wait()); + + KWin::Wayland::Compositor compositor; + compositor.setup(registry.bindCompositor(compositorSpy.first().first().value(), compositorSpy.first().last().value())); + QVERIFY(compositor.isValid()); + + QVERIFY(KWin::Wayland::Surface::all().isEmpty()); + KWin::Wayland::Surface *s1 = compositor.createSurface(); + QCOMPARE(KWin::Wayland::Surface::all().count(), 1); + QCOMPARE(KWin::Wayland::Surface::all().first(), s1); + QCOMPARE(KWin::Wayland::Surface::get(*s1), s1); + + // add another surface + KWin::Wayland::Surface *s2 = compositor.createSurface(); + QCOMPARE(KWin::Wayland::Surface::all().count(), 2); + QCOMPARE(KWin::Wayland::Surface::all().first(), s1); + QCOMPARE(KWin::Wayland::Surface::all().last(), s2); + QCOMPARE(KWin::Wayland::Surface::get(*s1), s1); + QCOMPARE(KWin::Wayland::Surface::get(*s2), s2); + + // delete s2 again + delete s2; + QCOMPARE(KWin::Wayland::Surface::all().count(), 1); + QCOMPARE(KWin::Wayland::Surface::all().first(), s1); + QCOMPARE(KWin::Wayland::Surface::get(*s1), s1); + + // and finally delete the last one + delete s1; + QVERIFY(KWin::Wayland::Surface::all().isEmpty()); + QVERIFY(!KWin::Wayland::Surface::get(nullptr)); +} + +QTEST_MAIN(TestWaylandSurface) +#include "test_wayland_surface.moc" diff --git a/wayland_client/surface.cpp b/wayland_client/surface.cpp index 28c1178ca9..e190f8feb5 100644 --- a/wayland_client/surface.cpp +++ b/wayland_client/surface.cpp @@ -27,15 +27,19 @@ namespace KWin namespace Wayland { +QList Surface::s_surfaces = QList(); + Surface::Surface(QObject *parent) : QObject(parent) , m_surface(nullptr) , m_frameCallbackInstalled(false) { + s_surfaces << this; } Surface::~Surface() { + s_surfaces.removeAll(this); release(); } @@ -121,5 +125,23 @@ void Surface::attachBuffer(wl_buffer *buffer, const QPoint &offset) wl_surface_attach(m_surface, buffer, offset.x(), offset.y()); } +Surface *Surface::get(wl_surface *native) +{ + auto it = std::find_if(s_surfaces.constBegin(), s_surfaces.constEnd(), + [native](Surface *s) { + return s->m_surface == native; + } + ); + if (it != s_surfaces.constEnd()) { + return *(it); + } + return nullptr; +} + +const QList< Surface* > &Surface::all() +{ + return s_surfaces; +} + } } diff --git a/wayland_client/surface.h b/wayland_client/surface.h index ecd93ad812..ff2fab5eb1 100644 --- a/wayland_client/surface.h +++ b/wayland_client/surface.h @@ -62,12 +62,16 @@ public: static void frameCallback(void *data, wl_callback *callback, uint32_t time); + static const QList &all(); + static Surface *get(wl_surface *native); + Q_SIGNALS: void frameRendered(); private: void handleFrameCallback(); static const wl_callback_listener s_listener; + static QList s_surfaces; wl_surface *m_surface; bool m_frameCallbackInstalled; };