KWin
Loading...
Searching...
No Matches
plasmawindow_test.cpp
Go to the documentation of this file.
1/*
2 KWin - the KDE window manager
3 This file is part of the KDE project.
4
5 SPDX-FileCopyrightText: 2016 Martin Gräßlin <mgraesslin@kde.org>
6
7 SPDX-License-Identifier: GPL-2.0-or-later
8*/
9#include "kwin_wayland_test.h"
10
11#include "core/output.h"
12#include "pointer_input.h"
13#include "wayland/seat.h"
14#include "wayland_server.h"
15#include "workspace.h"
16#include "x11window.h"
17
18#include <KWayland/Client/compositor.h>
19#include <KWayland/Client/plasmawindowmanagement.h>
20#include <KWayland/Client/surface.h>
21// screenlocker
22#if KWIN_BUILD_SCREENLOCKER
23#include <KScreenLocker/KsldApp>
24#endif
25
26#include <QPainter>
27#include <QRasterWindow>
28
29#include <netwm.h>
30#include <xcb/xcb_icccm.h>
31
32namespace KWin
33{
34
35static const QString s_socketName = QStringLiteral("wayland_test_kwin_plasma-window-0");
36
37class PlasmaWindowTest : public QObject
38{
39 Q_OBJECT
40private Q_SLOTS:
41 void initTestCase();
42 void init();
43 void cleanup();
44 void testCreateDestroyX11PlasmaWindow();
45 void testInternalWindowNoPlasmaWindow();
46 void testPopupWindowNoPlasmaWindow();
47 void testLockScreenNoPlasmaWindow();
48 void testDestroyedButNotUnmapped();
49
50private:
51 KWayland::Client::PlasmaWindowManagement *m_windowManagement = nullptr;
52 KWayland::Client::Compositor *m_compositor = nullptr;
53};
54
55void PlasmaWindowTest::initTestCase()
56{
57 qRegisterMetaType<KWin::Window *>();
58 QSignalSpy applicationStartedSpy(kwinApp(), &Application::started);
59 QVERIFY(waylandServer()->init(s_socketName));
61 QRect(0, 0, 1280, 1024),
62 QRect(1280, 0, 1280, 1024),
63 });
64
65 kwinApp()->start();
66 QVERIFY(applicationStartedSpy.wait());
67 const auto outputs = workspace()->outputs();
68 QCOMPARE(outputs.count(), 2);
69 QCOMPARE(outputs[0]->geometry(), QRect(0, 0, 1280, 1024));
70 QCOMPARE(outputs[1]->geometry(), QRect(1280, 0, 1280, 1024));
71 setenv("QT_QPA_PLATFORM", "wayland", true);
72 setenv("QMLSCENE_DEVICE", "softwarecontext", true);
73}
74
75void PlasmaWindowTest::init()
76{
78 m_windowManagement = Test::waylandWindowManagement();
79 m_compositor = Test::waylandCompositor();
80
81 workspace()->setActiveOutput(QPoint(640, 512));
82 input()->pointer()->warp(QPoint(640, 512));
83}
84
85void PlasmaWindowTest::cleanup()
86{
88}
89
90void PlasmaWindowTest::testCreateDestroyX11PlasmaWindow()
91{
92 // this test verifies that a PlasmaWindow gets unmapped on Client side when an X11 window is destroyed
93 QSignalSpy plasmaWindowCreatedSpy(m_windowManagement, &KWayland::Client::PlasmaWindowManagement::windowCreated);
94
95 // create an xcb window
97 QVERIFY(!xcb_connection_has_error(c.get()));
98 const QRect windowGeometry(0, 0, 100, 200);
99 xcb_window_t windowId = xcb_generate_id(c.get());
100 xcb_create_window(c.get(), XCB_COPY_FROM_PARENT, windowId, rootWindow(),
101 windowGeometry.x(),
102 windowGeometry.y(),
103 windowGeometry.width(),
104 windowGeometry.height(),
105 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCB_COPY_FROM_PARENT, 0, nullptr);
106 xcb_size_hints_t hints;
107 memset(&hints, 0, sizeof(hints));
108 xcb_icccm_size_hints_set_position(&hints, 1, windowGeometry.x(), windowGeometry.y());
109 xcb_icccm_size_hints_set_size(&hints, 1, windowGeometry.width(), windowGeometry.height());
110 xcb_icccm_set_wm_normal_hints(c.get(), windowId, &hints);
111 xcb_map_window(c.get(), windowId);
112 xcb_flush(c.get());
113
114 // we should get a window for it
115 QSignalSpy windowCreatedSpy(workspace(), &Workspace::windowAdded);
116 QVERIFY(windowCreatedSpy.wait());
117 X11Window *window = windowCreatedSpy.first().first().value<X11Window *>();
118 QVERIFY(window);
119 QCOMPARE(window->window(), windowId);
120 QVERIFY(window->isDecorated());
121 QVERIFY(window->isActive());
122 // verify that it gets the keyboard focus
123 if (!window->surface()) {
124 // we don't have a surface yet, so focused keyboard surface if set is not ours
125 QVERIFY(!waylandServer()->seat()->focusedKeyboardSurface());
126 QVERIFY(Test::waitForWaylandSurface(window));
127 }
128 QCOMPARE(waylandServer()->seat()->focusedKeyboardSurface(), window->surface());
129
130 // now that should also give it to us on client side
131 QVERIFY(plasmaWindowCreatedSpy.wait());
132 QCOMPARE(plasmaWindowCreatedSpy.count(), 1);
133 QCOMPARE(m_windowManagement->windows().count(), 1);
134 auto pw = m_windowManagement->windows().first();
135 QCOMPARE(pw->geometry(), window->frameGeometry());
136 QSignalSpy geometryChangedSpy(pw, &KWayland::Client::PlasmaWindow::geometryChanged);
137
138 QSignalSpy unmappedSpy(m_windowManagement->windows().first(), &KWayland::Client::PlasmaWindow::unmapped);
139 QSignalSpy destroyedSpy(m_windowManagement->windows().first(), &QObject::destroyed);
140
141 // now shade the window
142 const QRectF geoBeforeShade = window->frameGeometry();
143 QVERIFY(geoBeforeShade.isValid());
144 QVERIFY(!geoBeforeShade.isEmpty());
146 QVERIFY(window->isShade());
147 QVERIFY(window->frameGeometry() != geoBeforeShade);
148 QVERIFY(geometryChangedSpy.wait());
149 QCOMPARE(pw->geometry(), window->frameGeometry());
150 // and unshade again
152 QVERIFY(!window->isShade());
153 QCOMPARE(window->frameGeometry(), geoBeforeShade);
154 QVERIFY(geometryChangedSpy.wait());
155 QCOMPARE(pw->geometry(), geoBeforeShade);
156
157 // and destroy the window again
158 xcb_unmap_window(c.get(), windowId);
159 xcb_flush(c.get());
160
161 QSignalSpy windowClosedSpy(window, &X11Window::closed);
162 QVERIFY(windowClosedSpy.wait());
163 xcb_destroy_window(c.get(), windowId);
164 c.reset();
165
166 QVERIFY(unmappedSpy.wait());
167 QCOMPARE(unmappedSpy.count(), 1);
168
169 QVERIFY(destroyedSpy.wait());
170}
171
172class HelperWindow : public QRasterWindow
173{
174 Q_OBJECT
175public:
177 ~HelperWindow() override;
178
179protected:
180 void paintEvent(QPaintEvent *event) override;
181};
182
184 : QRasterWindow(nullptr)
185{
186}
187
189
190void HelperWindow::paintEvent(QPaintEvent *event)
191{
192 QPainter p(this);
193 p.fillRect(0, 0, width(), height(), Qt::red);
194}
195
196void PlasmaWindowTest::testInternalWindowNoPlasmaWindow()
197{
198 // this test verifies that an internal window is not added as a PlasmaWindow
199 QSignalSpy plasmaWindowCreatedSpy(m_windowManagement, &KWayland::Client::PlasmaWindowManagement::windowCreated);
200 HelperWindow win;
201 win.setGeometry(0, 0, 100, 100);
202 win.show();
203
204 QVERIFY(!plasmaWindowCreatedSpy.wait(100));
205}
206
207void PlasmaWindowTest::testPopupWindowNoPlasmaWindow()
208{
209 // this test verifies that a popup window is not added as a PlasmaWindow
210 QSignalSpy plasmaWindowCreatedSpy(m_windowManagement, &KWayland::Client::PlasmaWindowManagement::windowCreated);
211
212 // first create the parent window
213 std::unique_ptr<KWayland::Client::Surface> parentSurface(Test::createSurface());
214 std::unique_ptr<Test::XdgToplevel> parentShellSurface(Test::createXdgToplevelSurface(parentSurface.get()));
215 Window *parentClient = Test::renderAndWaitForShown(parentSurface.get(), QSize(100, 50), Qt::blue);
216 QVERIFY(parentClient);
217 QVERIFY(plasmaWindowCreatedSpy.wait());
218 QCOMPARE(plasmaWindowCreatedSpy.count(), 1);
219
220 // now let's create a popup window for it
221 std::unique_ptr<Test::XdgPositioner> positioner(Test::createXdgPositioner());
222 positioner->set_size(10, 10);
223 positioner->set_anchor_rect(0, 0, 10, 10);
224 positioner->set_anchor(Test::XdgPositioner::anchor_bottom_right);
225 positioner->set_gravity(Test::XdgPositioner::gravity_bottom_right);
226 std::unique_ptr<KWayland::Client::Surface> popupSurface(Test::createSurface());
227 std::unique_ptr<Test::XdgPopup> popupShellSurface(Test::createXdgPopupSurface(popupSurface.get(), parentShellSurface->xdgSurface(), positioner.get()));
228 Window *popupWindow = Test::renderAndWaitForShown(popupSurface.get(), QSize(10, 10), Qt::blue);
229 QVERIFY(popupWindow);
230 QVERIFY(!plasmaWindowCreatedSpy.wait(100));
231 QCOMPARE(plasmaWindowCreatedSpy.count(), 1);
232
233 // let's destroy the windows
234 popupShellSurface.reset();
235 QVERIFY(Test::waitForWindowClosed(popupWindow));
236 parentShellSurface.reset();
237 QVERIFY(Test::waitForWindowClosed(parentClient));
238}
239
240void PlasmaWindowTest::testLockScreenNoPlasmaWindow()
241{
242#if KWIN_BUILD_SCREENLOCKER
243 // this test verifies that lock screen windows are not exposed to PlasmaWindow
244 QSignalSpy plasmaWindowCreatedSpy(m_windowManagement, &KWayland::Client::PlasmaWindowManagement::windowCreated);
245
246 // this time we use a QSignalSpy on XdgShellClient as it'a a little bit more complex setup
247 QSignalSpy windowAddedSpy(workspace(), &Workspace::windowAdded);
248 // lock
249 ScreenLocker::KSldApp::self()->lock(ScreenLocker::EstablishLock::Immediate);
250 QVERIFY(windowAddedSpy.wait());
251 QVERIFY(windowAddedSpy.first().first().value<Window *>()->isLockScreen());
252 // should not be sent to the window
253 QVERIFY(plasmaWindowCreatedSpy.isEmpty());
254 QVERIFY(!plasmaWindowCreatedSpy.wait(100));
255
256 // fake unlock
257 QSignalSpy lockStateChangedSpy(ScreenLocker::KSldApp::self(), &ScreenLocker::KSldApp::lockStateChanged);
258 const auto children = ScreenLocker::KSldApp::self()->children();
259 for (auto it = children.begin(); it != children.end(); ++it) {
260 if (qstrcmp((*it)->metaObject()->className(), "LogindIntegration") != 0) {
261 continue;
262 }
263 QMetaObject::invokeMethod(*it, "requestUnlock");
264 break;
265 }
266 QVERIFY(lockStateChangedSpy.wait());
267 QVERIFY(!waylandServer()->isScreenLocked());
268#else
269 QSKIP("KWin was built without lockscreen support");
270#endif
271}
272
273void PlasmaWindowTest::testDestroyedButNotUnmapped()
274{
275 // this test verifies that also when a ShellSurface gets destroyed without a prior unmap
276 // the PlasmaWindow gets destroyed on Client side
277 QSignalSpy plasmaWindowCreatedSpy(m_windowManagement, &KWayland::Client::PlasmaWindowManagement::windowCreated);
278
279 // first create the parent window
280 std::unique_ptr<KWayland::Client::Surface> parentSurface(Test::createSurface());
281 std::unique_ptr<Test::XdgToplevel> parentShellSurface(Test::createXdgToplevelSurface(parentSurface.get()));
282 // map that window
283 Test::render(parentSurface.get(), QSize(100, 50), Qt::blue);
284 // this should create a plasma window
285 QVERIFY(plasmaWindowCreatedSpy.wait());
286 QCOMPARE(plasmaWindowCreatedSpy.count(), 1);
287 auto window = plasmaWindowCreatedSpy.first().first().value<KWayland::Client::PlasmaWindow *>();
288 QVERIFY(window);
289 QSignalSpy destroyedSpy(window, &QObject::destroyed);
290
291 // now destroy without an unmap
292 parentShellSurface.reset();
293 parentSurface.reset();
294 QVERIFY(destroyedSpy.wait());
295}
296
297}
298
300#include "plasmawindow_test.moc"
void paintEvent(QPaintEvent *event) override
~HelperWindow() override=default
~HelperWindow() override
bool event(QEvent *event) override
PointerInputRedirection * pointer() const
Definition input.h:220
void warp(const QPointF &pos)
void windowAdded(KWin::Window *)
QList< Output * > outputs() const
Definition workspace.h:762
void setActiveOutput(Output *output)
#define WAYLANDTEST_MAIN(TestObject)
XdgPositioner * createXdgPositioner()
Window * renderAndWaitForShown(KWayland::Client::Surface *surface, const QSize &size, const QColor &color, const QImage::Format &format=QImage::Format_ARGB32, int timeout=5000)
XdgPopup * createXdgPopupSurface(KWayland::Client::Surface *surface, XdgSurface *parentSurface, XdgPositioner *positioner, CreationSetup configureMode=CreationSetup::CreateAndConfigure, QObject *parent=nullptr)
void destroyWaylandConnection()
void setOutputConfig(const QList< QRect > &geometries)
KWayland::Client::Seat * seat
bool setupWaylandConnection(AdditionalWaylandInterfaces flags=AdditionalWaylandInterfaces())
KWayland::Client::PlasmaWindowManagement * waylandWindowManagement()
KWayland::Client::Compositor * waylandCompositor()
void render(KWayland::Client::Surface *surface, const QSize &size, const QColor &color, const QImage::Format &format=QImage::Format_ARGB32_Premultiplied)
bool waitForWaylandSurface(Window *window)
XcbConnectionPtr createX11Connection()
std::unique_ptr< KWayland::Client::Surface > createSurface()
XdgToplevel * createXdgToplevelSurface(KWayland::Client::Surface *surface, QObject *parent=nullptr)
std::unique_ptr< xcb_connection_t, XcbConnectionDeleter > XcbConnectionPtr
bool waitForWindowClosed(Window *window)
KWIN_EXPORT xcb_window_t rootWindow()
Definition xcb.h:24
WaylandServer * waylandServer()
Workspace * workspace()
Definition workspace.h:830
InputRedirection * input()
Definition input.h:549