KWin
Loading...
Searching...
No Matches
test_shadow.cpp
Go to the documentation of this file.
1/*
2 SPDX-FileCopyrightText: 2016 Martin Gräßlin <mgraesslin@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6// Qt
7#include <QSignalSpy>
8#include <QTest>
9// client
10#include "KWayland/Client/compositor.h"
11#include "KWayland/Client/connection_thread.h"
12#include "KWayland/Client/event_queue.h"
13#include "KWayland/Client/registry.h"
14#include "KWayland/Client/shadow.h"
15#include "KWayland/Client/shm_pool.h"
16#include "KWayland/Client/surface.h"
17// server
19#include "wayland/compositor.h"
20#include "wayland/display.h"
21#include "wayland/shadow.h"
22
23using namespace KWin;
24
25class ShadowTest : public QObject
26{
27 Q_OBJECT
28private Q_SLOTS:
29 void init();
30 void cleanup();
31
32 void testCreateShadow();
33 void testShadowElements();
34 void testSurfaceDestroy();
35
36private:
37 KWin::Display *m_display = nullptr;
38
39 KWayland::Client::ConnectionThread *m_connection = nullptr;
40 CompositorInterface *m_compositorInterface = nullptr;
41 ShadowManagerInterface *m_shadowInterface = nullptr;
42 QThread *m_thread = nullptr;
43 KWayland::Client::EventQueue *m_queue = nullptr;
44 KWayland::Client::ShmPool *m_shm = nullptr;
45 KWayland::Client::Compositor *m_compositor = nullptr;
46 KWayland::Client::ShadowManager *m_shadow = nullptr;
47};
48
49static const QString s_socketName = QStringLiteral("kwayland-test-shadow-0");
50
51void ShadowTest::init()
52{
53 delete m_display;
54 m_display = new KWin::Display(this);
55 m_display->addSocketName(s_socketName);
56 m_display->start();
57 QVERIFY(m_display->isRunning());
58 m_display->createShm();
59 m_compositorInterface = new CompositorInterface(m_display, m_display);
60 m_shadowInterface = new ShadowManagerInterface(m_display, m_display);
61
62 // setup connection
63 m_connection = new KWayland::Client::ConnectionThread;
64 QSignalSpy connectedSpy(m_connection, &KWayland::Client::ConnectionThread::connected);
65 m_connection->setSocketName(s_socketName);
66
67 m_thread = new QThread(this);
68 m_connection->moveToThread(m_thread);
69 m_thread->start();
70
71 m_connection->initConnection();
72 QVERIFY(connectedSpy.wait());
73
74 m_queue = new KWayland::Client::EventQueue(this);
75 m_queue->setup(m_connection);
76
77 KWayland::Client::Registry registry;
78 QSignalSpy interfacesAnnouncedSpy(&registry, &KWayland::Client::Registry::interfacesAnnounced);
79 registry.setEventQueue(m_queue);
80 registry.create(m_connection);
81 QVERIFY(registry.isValid());
82 registry.setup();
83 QVERIFY(interfacesAnnouncedSpy.wait());
84
85 m_shm = registry.createShmPool(registry.interface(KWayland::Client::Registry::Interface::Shm).name, registry.interface(KWayland::Client::Registry::Interface::Shm).version, this);
86 QVERIFY(m_shm->isValid());
87 m_compositor =
88 registry.createCompositor(registry.interface(KWayland::Client::Registry::Interface::Compositor).name, registry.interface(KWayland::Client::Registry::Interface::Compositor).version, this);
89 QVERIFY(m_compositor->isValid());
90 m_shadow =
91 registry.createShadowManager(registry.interface(KWayland::Client::Registry::Interface::Shadow).name, registry.interface(KWayland::Client::Registry::Interface::Shadow).version, this);
92 QVERIFY(m_shadow->isValid());
93}
94
95void ShadowTest::cleanup()
96{
97#define CLEANUP(variable) \
98 if (variable) { \
99 delete variable; \
100 variable = nullptr; \
101 }
102 CLEANUP(m_shm)
103 CLEANUP(m_compositor)
104 CLEANUP(m_shadow)
105 CLEANUP(m_queue)
106 if (m_connection) {
107 m_connection->deleteLater();
108 m_connection = nullptr;
109 }
110 if (m_thread) {
111 m_thread->quit();
112 m_thread->wait();
113 delete m_thread;
114 m_thread = nullptr;
115 }
116
117 CLEANUP(m_display)
118#undef CLEANUP
119
120 // these are the children of the display
121 m_compositorInterface = nullptr;
122 m_shadowInterface = nullptr;
123}
124
125void ShadowTest::testCreateShadow()
126{
127 // this test verifies the basic shadow behavior, create for surface, commit it, etc.
128 QSignalSpy surfaceCreatedSpy(m_compositorInterface, &CompositorInterface::surfaceCreated);
129 std::unique_ptr<KWayland::Client::Surface> surface(m_compositor->createSurface());
130 QVERIFY(surfaceCreatedSpy.wait());
131 auto serverSurface = surfaceCreatedSpy.first().first().value<SurfaceInterface *>();
132 QVERIFY(serverSurface);
133 // a surface without anything should not have a Shadow
134 QVERIFY(!serverSurface->shadow());
135 QSignalSpy shadowChangedSpy(serverSurface, &SurfaceInterface::shadowChanged);
136
137 // let's create a shadow for the Surface
138 std::unique_ptr<KWayland::Client::Shadow> shadow(m_shadow->createShadow(surface.get()));
139 // that should not have triggered the shadowChangedSpy)
140 QVERIFY(!shadowChangedSpy.wait(100));
141
142 // now let's commit the surface, that should trigger the shadow changed
143 surface->commit(KWayland::Client::Surface::CommitFlag::None);
144 QVERIFY(shadowChangedSpy.wait());
145 QCOMPARE(shadowChangedSpy.count(), 1);
146
147 // we didn't set anything on the shadow, so it should be all default values
148 auto serverShadow = serverSurface->shadow();
149 QVERIFY(serverShadow);
150 QCOMPARE(serverShadow->offset(), QMarginsF());
151 QVERIFY(!serverShadow->topLeft());
152 QVERIFY(!serverShadow->top());
153 QVERIFY(!serverShadow->topRight());
154 QVERIFY(!serverShadow->right());
155 QVERIFY(!serverShadow->bottomRight());
156 QVERIFY(!serverShadow->bottom());
157 QVERIFY(!serverShadow->bottomLeft());
158 QVERIFY(!serverShadow->left());
159
160 // now let's remove the shadow
161 m_shadow->removeShadow(surface.get());
162 // just removing should not remove it yet, surface needs to be committed
163 QVERIFY(!shadowChangedSpy.wait(100));
164 surface->commit(KWayland::Client::Surface::CommitFlag::None);
165 QVERIFY(shadowChangedSpy.wait());
166 QCOMPARE(shadowChangedSpy.count(), 2);
167 QVERIFY(!serverSurface->shadow());
168}
169
170static QImage bufferToImage(KWin::GraphicsBuffer *clientBuffer)
171{
172 if (clientBuffer) {
173 KWin::GraphicsBufferView view(clientBuffer);
174 if (QImage *image = view.image()) {
175 return image->copy();
176 }
177 }
178 return QImage();
179}
180
181void ShadowTest::testShadowElements()
182{
183 // this test verifies that all shadow elements are correctly passed to the server
184 // first create surface
185 QSignalSpy surfaceCreatedSpy(m_compositorInterface, &CompositorInterface::surfaceCreated);
186 std::unique_ptr<KWayland::Client::Surface> surface(m_compositor->createSurface());
187 QVERIFY(surfaceCreatedSpy.wait());
188 auto serverSurface = surfaceCreatedSpy.first().first().value<SurfaceInterface *>();
189 QVERIFY(serverSurface);
190 QSignalSpy shadowChangedSpy(serverSurface, &SurfaceInterface::shadowChanged);
191
192 // now create the shadow
193 std::unique_ptr<KWayland::Client::Shadow> shadow(m_shadow->createShadow(surface.get()));
194 QImage topLeftImage(QSize(10, 10), QImage::Format_ARGB32_Premultiplied);
195 topLeftImage.fill(Qt::white);
196 shadow->attachTopLeft(m_shm->createBuffer(topLeftImage));
197 QImage topImage(QSize(11, 11), QImage::Format_ARGB32_Premultiplied);
198 topImage.fill(Qt::black);
199 shadow->attachTop(m_shm->createBuffer(topImage));
200 QImage topRightImage(QSize(12, 12), QImage::Format_ARGB32_Premultiplied);
201 topRightImage.fill(Qt::red);
202 shadow->attachTopRight(m_shm->createBuffer(topRightImage));
203 QImage rightImage(QSize(13, 13), QImage::Format_ARGB32_Premultiplied);
204 rightImage.fill(Qt::darkRed);
205 shadow->attachRight(m_shm->createBuffer(rightImage));
206 QImage bottomRightImage(QSize(14, 14), QImage::Format_ARGB32_Premultiplied);
207 bottomRightImage.fill(Qt::green);
208 shadow->attachBottomRight(m_shm->createBuffer(bottomRightImage));
209 QImage bottomImage(QSize(15, 15), QImage::Format_ARGB32_Premultiplied);
210 bottomImage.fill(Qt::darkGreen);
211 shadow->attachBottom(m_shm->createBuffer(bottomImage));
212 QImage bottomLeftImage(QSize(16, 16), QImage::Format_ARGB32_Premultiplied);
213 bottomLeftImage.fill(Qt::blue);
214 shadow->attachBottomLeft(m_shm->createBuffer(bottomLeftImage));
215 QImage leftImage(QSize(17, 17), QImage::Format_ARGB32_Premultiplied);
216 leftImage.fill(Qt::darkBlue);
217 shadow->attachLeft(m_shm->createBuffer(leftImage));
218 shadow->setOffsets(QMarginsF(1, 2, 3, 4));
219 shadow->commit();
220 surface->commit(KWayland::Client::Surface::CommitFlag::None);
221
222 QVERIFY(shadowChangedSpy.wait());
223 auto serverShadow = serverSurface->shadow();
224 QVERIFY(serverShadow);
225 QCOMPARE(serverShadow->offset(), QMarginsF(1, 2, 3, 4));
226 QCOMPARE(bufferToImage(serverShadow->topLeft()), topLeftImage);
227 QCOMPARE(bufferToImage(serverShadow->top()), topImage);
228 QCOMPARE(bufferToImage(serverShadow->topRight()), topRightImage);
229 QCOMPARE(bufferToImage(serverShadow->right()), rightImage);
230 QCOMPARE(bufferToImage(serverShadow->bottomRight()), bottomRightImage);
231 QCOMPARE(bufferToImage(serverShadow->bottom()), bottomImage);
232 QCOMPARE(bufferToImage(serverShadow->bottomLeft()), bottomLeftImage);
233 QCOMPARE(bufferToImage(serverShadow->left()), leftImage);
234}
235
236void ShadowTest::testSurfaceDestroy()
237{
238 using namespace KWin;
239 QSignalSpy serverSurfaceCreated(m_compositorInterface, &CompositorInterface::surfaceCreated);
240
241 std::unique_ptr<KWayland::Client::Surface> surface(m_compositor->createSurface());
242 QVERIFY(serverSurfaceCreated.wait());
243 auto serverSurface = serverSurfaceCreated.first().first().value<SurfaceInterface *>();
244 QSignalSpy shadowChangedSpy(serverSurface, &SurfaceInterface::shadowChanged);
245
246 std::unique_ptr<KWayland::Client::Shadow> shadow(m_shadow->createShadow(surface.get()));
247 shadow->commit();
248 surface->commit(KWayland::Client::Surface::CommitFlag::None);
249 QVERIFY(shadowChangedSpy.wait());
250 auto serverShadow = serverSurface->shadow();
251 QVERIFY(serverShadow);
252
253 // destroy the parent surface
254 QSignalSpy surfaceDestroyedSpy(serverSurface, &QObject::destroyed);
255 QSignalSpy shadowDestroyedSpy(serverShadow, &QObject::destroyed);
256 surface.reset();
257 QVERIFY(surfaceDestroyedSpy.wait());
258 QVERIFY(shadowDestroyedSpy.isEmpty());
259 // destroy the shadow
260 shadow.reset();
261 QVERIFY(shadowDestroyedSpy.wait());
262 QCOMPARE(shadowDestroyedSpy.count(), 1);
263}
264
265QTEST_GUILESS_MAIN(ShadowTest)
266#include "test_shadow.moc"
void surfaceCreated(KWin::SurfaceInterface *surface)
Class holding the Wayland server display loop.
Definition display.h:34
void createShm()
Definition display.cpp:128
bool addSocketName(const QString &name=QString())
Definition display.cpp:68
bool isRunning() const
Definition display.cpp:144
bool start()
Definition display.cpp:92
Resource representing a wl_surface.
Definition surface.h:80
#define CLEANUP(variable)