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 #include "abstract_client.h"
11 #include "abstract_output.h"
12 #include "platform.h"
13 #include "cursor.h"
14 #include "screens.h"
15 #include "virtualdesktops.h"
16 #include "wayland_server.h"
17 #include "workspace.h"
18 #include <KWayland/Client/connection_thread.h>
19 #include <KWayland/Client/compositor.h>
20 #include <KWayland/Client/event_queue.h>
21 #include <KWayland/Client/plasmashell.h>
22 #include <KWayland/Client/registry.h>
23 #include <KWayland/Client/shm_pool.h>
24 #include <KWayland/Client/surface.h>
25 
26 using namespace KWin;
27 using namespace KWayland::Client;
28 
29 Q_DECLARE_METATYPE(KWin::Layer)
30 
31 static const QString s_socketName = QStringLiteral("wayland_test_kwin_plasma_surface-0");
32 
33 class PlasmaSurfaceTest : public QObject
34 {
35     Q_OBJECT
36 private Q_SLOTS:
37     void initTestCase();
38     void init();
39     void cleanup();
40 
41     void testRoleOnAllDesktops_data();
42     void testRoleOnAllDesktops();
43     void testAcceptsFocus_data();
44     void testAcceptsFocus();
45 
46     void testPanelWindowsCanCover_data();
47     void testPanelWindowsCanCover();
48     void testOSDPlacement();
49     void testOSDPlacementManualPosition();
50     void testPanelTypeHasStrut_data();
51     void testPanelTypeHasStrut();
52     void testPanelActivate_data();
53     void testPanelActivate();
54 
55 private:
56     KWayland::Client::Compositor *m_compositor = nullptr;
57     PlasmaShell *m_plasmaShell = nullptr;
58 };
59 
initTestCase()60 void PlasmaSurfaceTest::initTestCase()
61 {
62     qRegisterMetaType<KWin::AbstractClient *>();
63     QSignalSpy applicationStartedSpy(kwinApp(), &Application::started);
64     QVERIFY(applicationStartedSpy.isValid());
65     kwinApp()->platform()->setInitialWindowSize(QSize(1280, 1024));
66     QVERIFY(waylandServer()->init(s_socketName));
67     kwinApp()->start();
68     QVERIFY(applicationStartedSpy.wait());
69 }
70 
init()71 void PlasmaSurfaceTest::init()
72 {
73     QVERIFY(Test::setupWaylandConnection(Test::AdditionalWaylandInterface::PlasmaShell));
74     m_compositor = Test::waylandCompositor();
75     m_plasmaShell = Test::waylandPlasmaShell();
76 
77     KWin::Cursors::self()->mouse()->setPos(640, 512);
78 }
79 
cleanup()80 void PlasmaSurfaceTest::cleanup()
81 {
82     Test::destroyWaylandConnection();
83 }
84 
testRoleOnAllDesktops_data()85 void PlasmaSurfaceTest::testRoleOnAllDesktops_data()
86 {
87     QTest::addColumn<PlasmaShellSurface::Role>("role");
88     QTest::addColumn<bool>("expectedOnAllDesktops");
89 
90     QTest::newRow("Desktop") << PlasmaShellSurface::Role::Desktop << true;
91     QTest::newRow("Panel") << PlasmaShellSurface::Role::Panel << true;
92     QTest::newRow("OSD") << PlasmaShellSurface::Role::OnScreenDisplay << true;
93     QTest::newRow("Normal") << PlasmaShellSurface::Role::Normal << false;
94     QTest::newRow("Notification") << PlasmaShellSurface::Role::Notification << true;
95     QTest::newRow("ToolTip") << PlasmaShellSurface::Role::ToolTip << true;
96     QTest::newRow("CriticalNotification") << PlasmaShellSurface::Role::CriticalNotification << true;
97 }
98 
testRoleOnAllDesktops()99 void PlasmaSurfaceTest::testRoleOnAllDesktops()
100 {
101     // this test verifies that a XdgShellClient is set on all desktops when the role changes
102     QScopedPointer<KWayland::Client::Surface> surface(Test::createSurface());
103     QVERIFY(!surface.isNull());
104     QScopedPointer<Test::XdgToplevel> shellSurface(Test::createXdgToplevelSurface(surface.data()));
105     QVERIFY(!shellSurface.isNull());
106     QScopedPointer<PlasmaShellSurface> plasmaSurface(m_plasmaShell->createSurface(surface.data()));
107     QVERIFY(!plasmaSurface.isNull());
108 
109     // now render to map the window
110     AbstractClient *c = Test::renderAndWaitForShown(surface.data(), QSize(100, 50), Qt::blue);
111     QVERIFY(c);
112     QCOMPARE(workspace()->activeClient(), c);
113 
114     // currently the role is not yet set, so the window should not be on all desktops
115     QCOMPARE(c->isOnAllDesktops(), false);
116 
117     // now let's try to change that
118     QSignalSpy onAllDesktopsSpy(c, &AbstractClient::desktopChanged);
119     QVERIFY(onAllDesktopsSpy.isValid());
120     QFETCH(PlasmaShellSurface::Role, role);
121     plasmaSurface->setRole(role);
122     QFETCH(bool, expectedOnAllDesktops);
123     QCOMPARE(onAllDesktopsSpy.wait(), expectedOnAllDesktops);
124     QCOMPARE(c->isOnAllDesktops(), expectedOnAllDesktops);
125 
126     // let's create a second window where we init a little bit different
127     // first creating the PlasmaSurface then the Shell Surface
128     QScopedPointer<KWayland::Client::Surface> surface2(Test::createSurface());
129     QVERIFY(!surface2.isNull());
130     QScopedPointer<PlasmaShellSurface> plasmaSurface2(m_plasmaShell->createSurface(surface2.data()));
131     QVERIFY(!plasmaSurface2.isNull());
132     plasmaSurface2->setRole(role);
133     QScopedPointer<Test::XdgToplevel> shellSurface2(Test::createXdgToplevelSurface(surface2.data()));
134     QVERIFY(!shellSurface2.isNull());
135     auto c2 = Test::renderAndWaitForShown(surface2.data(), QSize(100, 50), Qt::blue);
136     QVERIFY(c2);
137     QVERIFY(c != c2);
138 
139     QCOMPARE(c2->isOnAllDesktops(), expectedOnAllDesktops);
140 }
141 
testAcceptsFocus_data()142 void PlasmaSurfaceTest::testAcceptsFocus_data()
143 {
144     QTest::addColumn<PlasmaShellSurface::Role>("role");
145     QTest::addColumn<bool>("wantsInput");
146     QTest::addColumn<bool>("active");
147 
148     QTest::newRow("Desktop") << PlasmaShellSurface::Role::Desktop << true << true;
149     QTest::newRow("Panel") << PlasmaShellSurface::Role::Panel << true << false;
150     QTest::newRow("OSD") << PlasmaShellSurface::Role::OnScreenDisplay << false << false;
151     QTest::newRow("Normal") << PlasmaShellSurface::Role::Normal << true << true;
152     QTest::newRow("Notification") << PlasmaShellSurface::Role::Notification << false << false;
153     QTest::newRow("ToolTip") << PlasmaShellSurface::Role::ToolTip << false << false;
154     QTest::newRow("CriticalNotification") << PlasmaShellSurface::Role::CriticalNotification << false << false;
155 }
156 
testAcceptsFocus()157 void PlasmaSurfaceTest::testAcceptsFocus()
158 {
159     // this test verifies that some surface roles don't get focus
160     QScopedPointer<KWayland::Client::Surface> surface(Test::createSurface());
161     QVERIFY(!surface.isNull());
162     QScopedPointer<Test::XdgToplevel> shellSurface(Test::createXdgToplevelSurface(surface.data()));
163     QVERIFY(!shellSurface.isNull());
164     QScopedPointer<PlasmaShellSurface> plasmaSurface(m_plasmaShell->createSurface(surface.data()));
165     QVERIFY(!plasmaSurface.isNull());
166     QFETCH(PlasmaShellSurface::Role, role);
167     plasmaSurface->setRole(role);
168 
169     // now render to map the window
170     auto c = Test::renderAndWaitForShown(surface.data(), QSize(100, 50), Qt::blue);
171 
172     QVERIFY(c);
173     QTEST(c->wantsInput(), "wantsInput");
174     QTEST(c->isActive(), "active");
175 }
176 
testOSDPlacement()177 void PlasmaSurfaceTest::testOSDPlacement()
178 {
179     QScopedPointer<KWayland::Client::Surface> surface(Test::createSurface());
180     QVERIFY(!surface.isNull());
181     QScopedPointer<Test::XdgToplevel> shellSurface(Test::createXdgToplevelSurface(surface.data()));
182     QVERIFY(!shellSurface.isNull());
183     QScopedPointer<PlasmaShellSurface> plasmaSurface(m_plasmaShell->createSurface(surface.data()));
184     QVERIFY(!plasmaSurface.isNull());
185     plasmaSurface->setRole(PlasmaShellSurface::Role::OnScreenDisplay);
186 
187     // now render and map the window
188     auto c = Test::renderAndWaitForShown(surface.data(), QSize(100, 50), Qt::blue);
189 
190     QVERIFY(c);
191     QCOMPARE(c->windowType(), NET::OnScreenDisplay);
192     QVERIFY(c->isOnScreenDisplay());
193     QCOMPARE(c->frameGeometry(), QRect(1280 / 2 - 100 / 2, 2 * 1024 / 3 - 50 / 2, 100, 50));
194 
195     // change the screen size
196     QSignalSpy screensChangedSpy(screens(), &Screens::changed);
197     QVERIFY(screensChangedSpy.isValid());
198     const QVector<QRect> geometries{QRect(0, 0, 1280, 1024), QRect(1280, 0, 1280, 1024)};
199     QMetaObject::invokeMethod(kwinApp()->platform(), "setVirtualOutputs",
200                               Qt::DirectConnection,
201                               Q_ARG(int, 2),
202                               Q_ARG(QVector<QRect>, geometries));
203     QVERIFY(screensChangedSpy.wait());
204     QCOMPARE(screensChangedSpy.count(), 2);
205     const auto outputs = kwinApp()->platform()->enabledOutputs();
206     QCOMPARE(outputs.count(), 2);
207     QCOMPARE(outputs[0]->geometry(), geometries[0]);
208     QCOMPARE(outputs[1]->geometry(), geometries[1]);
209 
210     QCOMPARE(c->frameGeometry(), QRect(1280 / 2 - 100 / 2, 2 * 1024 / 3 - 50 / 2, 100, 50));
211 
212     // change size of window
213     QSignalSpy frameGeometryChangedSpy(c, &AbstractClient::frameGeometryChanged);
214     QVERIFY(frameGeometryChangedSpy.isValid());
215     Test::render(surface.data(), QSize(200, 100), Qt::red);
216     QVERIFY(frameGeometryChangedSpy.wait());
217     QCOMPARE(c->frameGeometry(), QRect(1280 / 2 - 200 / 2, 2 * 1024 / 3 - 100 / 2, 200, 100));
218 }
219 
testOSDPlacementManualPosition()220 void PlasmaSurfaceTest::testOSDPlacementManualPosition()
221 {
222     QScopedPointer<KWayland::Client::Surface> surface(Test::createSurface());
223     QVERIFY(!surface.isNull());
224     QScopedPointer<PlasmaShellSurface> plasmaSurface(m_plasmaShell->createSurface(surface.data()));
225     QVERIFY(!plasmaSurface.isNull());
226     plasmaSurface->setRole(PlasmaShellSurface::Role::OnScreenDisplay);
227 
228     plasmaSurface->setPosition(QPoint(50, 70));
229 
230     QScopedPointer<Test::XdgToplevel> shellSurface(Test::createXdgToplevelSurface(surface.data()));
231     QVERIFY(!shellSurface.isNull());
232 
233     // now render and map the window
234     auto c = Test::renderAndWaitForShown(surface.data(), QSize(100, 50), Qt::blue);
235 
236     QVERIFY(c);
237     QVERIFY(!c->isPlaceable());
238     QCOMPARE(c->windowType(), NET::OnScreenDisplay);
239     QVERIFY(c->isOnScreenDisplay());
240     QCOMPARE(c->frameGeometry(), QRect(50, 70, 100, 50));
241 }
242 
243 
testPanelTypeHasStrut_data()244 void PlasmaSurfaceTest::testPanelTypeHasStrut_data()
245 {
246     QTest::addColumn<PlasmaShellSurface::PanelBehavior>("panelBehavior");
247     QTest::addColumn<bool>("expectedStrut");
248     QTest::addColumn<QRect>("expectedMaxArea");
249     QTest::addColumn<KWin::Layer>("expectedLayer");
250 
251     QTest::newRow("always visible - xdgWmBase") << PlasmaShellSurface::PanelBehavior::AlwaysVisible << true << QRect(0, 50, 1280, 974) << KWin::DockLayer;
252     QTest::newRow("autohide - xdgWmBase") << PlasmaShellSurface::PanelBehavior::AutoHide << false << QRect(0, 0, 1280, 1024) << KWin::AboveLayer;
253     QTest::newRow("windows can cover - xdgWmBase") << PlasmaShellSurface::PanelBehavior::WindowsCanCover << false << QRect(0, 0, 1280, 1024) << KWin::NormalLayer;
254     QTest::newRow("windows go below - xdgWmBase") << PlasmaShellSurface::PanelBehavior::WindowsGoBelow << false << QRect(0, 0, 1280, 1024) << KWin::AboveLayer;
255 }
256 
testPanelTypeHasStrut()257 void PlasmaSurfaceTest::testPanelTypeHasStrut()
258 {
259     QScopedPointer<KWayland::Client::Surface> surface(Test::createSurface());
260     QVERIFY(!surface.isNull());
261     QScopedPointer<QObject> shellSurface(Test::createXdgToplevelSurface(surface.data()));
262     QVERIFY(!shellSurface.isNull());
263     QScopedPointer<PlasmaShellSurface> plasmaSurface(m_plasmaShell->createSurface(surface.data()));
264     QVERIFY(!plasmaSurface.isNull());
265     plasmaSurface->setRole(PlasmaShellSurface::Role::Panel);
266     plasmaSurface->setPosition(QPoint(0, 0));
267     QFETCH(PlasmaShellSurface::PanelBehavior, panelBehavior);
268     plasmaSurface->setPanelBehavior(panelBehavior);
269 
270     // now render and map the window
271     auto c = Test::renderAndWaitForShown(surface.data(), QSize(100, 50), Qt::blue);
272 
273     // the panel is on the first output and the current desktop
274     AbstractOutput *output = kwinApp()->platform()->enabledOutputs().constFirst();
275     VirtualDesktop *desktop = VirtualDesktopManager::self()->currentDesktop();
276 
277     QVERIFY(c);
278     QCOMPARE(c->windowType(), NET::Dock);
279     QVERIFY(c->isDock());
280     QCOMPARE(c->frameGeometry(), QRect(0, 0, 100, 50));
281     QTEST(c->hasStrut(), "expectedStrut");
282     QTEST(workspace()->clientArea(MaximizeArea, output, desktop), "expectedMaxArea");
283     QTEST(c->layer(), "expectedLayer");
284 }
285 
testPanelWindowsCanCover_data()286 void PlasmaSurfaceTest::testPanelWindowsCanCover_data()
287 {
288     QTest::addColumn<QRect>("panelGeometry");
289     QTest::addColumn<QRect>("windowGeometry");
290     QTest::addColumn<QPoint>("triggerPoint");
291 
292     QTest::newRow("top-full-edge") << QRect(0, 0, 1280, 30) << QRect(0, 0, 200, 300) << QPoint(100, 0);
293     QTest::newRow("top-left-edge") << QRect(0, 0, 1000, 30) << QRect(0, 0, 200, 300) << QPoint(100, 0);
294     QTest::newRow("top-right-edge") << QRect(280, 0, 1000, 30) << QRect(1000, 0, 200, 300) << QPoint(1000, 0);
295     QTest::newRow("bottom-full-edge") << QRect(0, 994, 1280, 30) << QRect(0, 724, 200, 300) << QPoint(100, 1023);
296     QTest::newRow("bottom-left-edge") << QRect(0, 994, 1000, 30) << QRect(0, 724, 200, 300) << QPoint(100, 1023);
297     QTest::newRow("bottom-right-edge") << QRect(280, 994, 1000, 30) << QRect(1000, 724, 200, 300) << QPoint(1000, 1023);
298     QTest::newRow("left-full-edge") << QRect(0, 0, 30, 1024) << QRect(0, 0, 200, 300) << QPoint(0, 100);
299     QTest::newRow("left-top-edge") << QRect(0, 0, 30, 800) << QRect(0, 0, 200, 300) << QPoint(0, 100);
300     QTest::newRow("left-bottom-edge") << QRect(0, 200, 30, 824) << QRect(0, 0, 200, 300) << QPoint(0, 250);
301     QTest::newRow("right-full-edge") << QRect(1250, 0, 30, 1024) << QRect(1080, 0, 200, 300) << QPoint(1279, 100);
302     QTest::newRow("right-top-edge") << QRect(1250, 0, 30, 800) << QRect(1080, 0, 200, 300) << QPoint(1279, 100);
303     QTest::newRow("right-bottom-edge") << QRect(1250, 200, 30, 824) << QRect(1080, 0, 200, 300) << QPoint(1279, 250);
304 }
305 
testPanelWindowsCanCover()306 void PlasmaSurfaceTest::testPanelWindowsCanCover()
307 {
308     // this test verifies the behavior of a panel with windows can cover
309     // triggering the screen edge should raise the panel.
310     QScopedPointer<KWayland::Client::Surface> surface(Test::createSurface());
311     QVERIFY(!surface.isNull());
312     QScopedPointer<Test::XdgToplevel> shellSurface(Test::createXdgToplevelSurface(surface.data()));
313     QVERIFY(!shellSurface.isNull());
314     QScopedPointer<PlasmaShellSurface> plasmaSurface(m_plasmaShell->createSurface(surface.data()));
315     QVERIFY(!plasmaSurface.isNull());
316     plasmaSurface->setRole(PlasmaShellSurface::Role::Panel);
317     QFETCH(QRect, panelGeometry);
318     plasmaSurface->setPosition(panelGeometry.topLeft());
319     plasmaSurface->setPanelBehavior(PlasmaShellSurface::PanelBehavior::WindowsCanCover);
320 
321     // now render and map the window
322     auto panel = Test::renderAndWaitForShown(surface.data(), panelGeometry.size(), Qt::blue);
323 
324     // the panel is on the first output and the current desktop
325     AbstractOutput *output = kwinApp()->platform()->enabledOutputs().constFirst();
326     VirtualDesktop *desktop = VirtualDesktopManager::self()->currentDesktop();
327 
328     QVERIFY(panel);
329     QCOMPARE(panel->windowType(), NET::Dock);
330     QVERIFY(panel->isDock());
331     QCOMPARE(panel->frameGeometry(), panelGeometry);
332     QCOMPARE(panel->hasStrut(), false);
333     QCOMPARE(workspace()->clientArea(MaximizeArea, output, desktop), QRect(0, 0, 1280, 1024));
334     QCOMPARE(panel->layer(), KWin::NormalLayer);
335 
336     // create a Window
337     QScopedPointer<KWayland::Client::Surface> surface2(Test::createSurface());
338     QVERIFY(!surface2.isNull());
339     QScopedPointer<Test::XdgToplevel> shellSurface2(Test::createXdgToplevelSurface(surface2.data()));
340     QVERIFY(!shellSurface2.isNull());
341 
342     QFETCH(QRect, windowGeometry);
343     auto c = Test::renderAndWaitForShown(surface2.data(), windowGeometry.size(), Qt::red);
344 
345     QVERIFY(c);
346     QCOMPARE(c->windowType(), NET::Normal);
347     QVERIFY(c->isActive());
348     QCOMPARE(c->layer(), KWin::NormalLayer);
349     c->move(windowGeometry.topLeft());
350     QCOMPARE(c->frameGeometry(), windowGeometry);
351 
352     auto stackingOrder = workspace()->stackingOrder();
353     QCOMPARE(stackingOrder.count(), 2);
354     QCOMPARE(stackingOrder.first(), panel);
355     QCOMPARE(stackingOrder.last(), c);
356 
357     QSignalSpy stackingOrderChangedSpy(workspace(), &Workspace::stackingOrderChanged);
358     QVERIFY(stackingOrderChangedSpy.isValid());
359     // trigger screenedge
360     QFETCH(QPoint, triggerPoint);
361     KWin::Cursors::self()->mouse()->setPos(triggerPoint);
362     QVERIFY(stackingOrderChangedSpy.wait());
363     QCOMPARE(stackingOrderChangedSpy.count(), 1);
364     stackingOrder = workspace()->stackingOrder();
365     QCOMPARE(stackingOrder.count(), 2);
366     QCOMPARE(stackingOrder.first(), c);
367     QCOMPARE(stackingOrder.last(), panel);
368 }
369 
testPanelActivate_data()370 void PlasmaSurfaceTest::testPanelActivate_data()
371 {
372     QTest::addColumn<bool>("wantsFocus");
373     QTest::addColumn<bool>("active");
374 
375     QTest::newRow("no focus") << false << false;
376     QTest::newRow("focus") << true << true;
377 }
378 
testPanelActivate()379 void PlasmaSurfaceTest::testPanelActivate()
380 {
381     QScopedPointer<KWayland::Client::Surface> surface(Test::createSurface());
382     QVERIFY(!surface.isNull());
383     QScopedPointer<Test::XdgToplevel> shellSurface(Test::createXdgToplevelSurface(surface.data()));
384     QVERIFY(!shellSurface.isNull());
385     QScopedPointer<PlasmaShellSurface> plasmaSurface(m_plasmaShell->createSurface(surface.data()));
386     QVERIFY(!plasmaSurface.isNull());
387     plasmaSurface->setRole(PlasmaShellSurface::Role::Panel);
388     QFETCH(bool, wantsFocus);
389     plasmaSurface->setPanelTakesFocus(wantsFocus);
390 
391     auto panel = Test::renderAndWaitForShown(surface.data(), QSize(100, 200), Qt::blue);
392 
393     QVERIFY(panel);
394     QCOMPARE(panel->windowType(), NET::Dock);
395     QVERIFY(panel->isDock());
396     QFETCH(bool, active);
397     QCOMPARE(panel->dockWantsInput(), active);
398     QCOMPARE(panel->isActive(), active);
399 }
400 
401 WAYLANDTEST_MAIN(PlasmaSurfaceTest)
402 #include "plasma_surface_test.moc"
403