KWin
Loading...
Searching...
No Matches
test_wayland_contrast.cpp
Go to the documentation of this file.
1/*
2 SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org>
3 SPDX-FileCopyrightText: 2015 Marco Martin <mart@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6*/
7// Qt
8#include <QSignalSpy>
9#include <QTest>
10// KWin
11#include "wayland/compositor.h"
12#include "wayland/contrast.h"
13#include "wayland/display.h"
14
15#include "KWayland/Client/compositor.h"
16#include "KWayland/Client/connection_thread.h"
17#include "KWayland/Client/contrast.h"
18#include "KWayland/Client/event_queue.h"
19#include "KWayland/Client/region.h"
20#include "KWayland/Client/registry.h"
21#include "KWayland/Client/surface.h"
22
23#include <wayland-util.h>
24
25class TestContrast : public QObject
26{
27 Q_OBJECT
28public:
29 explicit TestContrast(QObject *parent = nullptr);
30private Q_SLOTS:
31 void init();
32 void cleanup();
33
34 void testCreate();
35 void testSurfaceDestroy();
36
37private:
38 KWin::Display *m_display;
39 KWin::CompositorInterface *m_compositorInterface;
40 KWin::ContrastManagerInterface *m_contrastManagerInterface;
41 KWayland::Client::ConnectionThread *m_connection;
42 KWayland::Client::Compositor *m_compositor;
43 KWayland::Client::ContrastManager *m_contrastManager;
44 KWayland::Client::EventQueue *m_queue;
45 QThread *m_thread;
46};
47
48static const QString s_socketName = QStringLiteral("kwayland-test-wayland-contrast-0");
49
51 : QObject(parent)
52 , m_display(nullptr)
53 , m_compositorInterface(nullptr)
54 , m_connection(nullptr)
55 , m_compositor(nullptr)
56 , m_queue(nullptr)
57 , m_thread(nullptr)
58{
59}
60
61void TestContrast::init()
62{
63 using namespace KWin;
64 delete m_display;
65 m_display = new KWin::Display(this);
66 m_display->addSocketName(s_socketName);
67 m_display->start();
68 QVERIFY(m_display->isRunning());
69
70 // setup connection
71 m_connection = new KWayland::Client::ConnectionThread;
72 QSignalSpy connectedSpy(m_connection, &KWayland::Client::ConnectionThread::connected);
73 m_connection->setSocketName(s_socketName);
74
75 m_thread = new QThread(this);
76 m_connection->moveToThread(m_thread);
77 m_thread->start();
78
79 m_connection->initConnection();
80 QVERIFY(connectedSpy.wait());
81
82 m_queue = new KWayland::Client::EventQueue(this);
83 QVERIFY(!m_queue->isValid());
84 m_queue->setup(m_connection);
85 QVERIFY(m_queue->isValid());
86
87 KWayland::Client::Registry registry;
88 QSignalSpy compositorSpy(&registry, &KWayland::Client::Registry::compositorAnnounced);
89
90 QSignalSpy contrastSpy(&registry, &KWayland::Client::Registry::contrastAnnounced);
91
92 QVERIFY(!registry.eventQueue());
93 registry.setEventQueue(m_queue);
94 QCOMPARE(registry.eventQueue(), m_queue);
95 registry.create(m_connection->display());
96 QVERIFY(registry.isValid());
97 registry.setup();
98
99 m_compositorInterface = new CompositorInterface(m_display, m_display);
100 QVERIFY(compositorSpy.wait());
101 m_compositor = registry.createCompositor(compositorSpy.first().first().value<quint32>(), compositorSpy.first().last().value<quint32>(), this);
102
103 m_contrastManagerInterface = new ContrastManagerInterface(m_display, m_display);
104
105 QVERIFY(contrastSpy.wait());
106 m_contrastManager = registry.createContrastManager(contrastSpy.first().first().value<quint32>(), contrastSpy.first().last().value<quint32>(), this);
107}
108
109void TestContrast::cleanup()
110{
111#define CLEANUP(variable) \
112 if (variable) { \
113 delete variable; \
114 variable = nullptr; \
115 }
116 CLEANUP(m_compositor)
117 CLEANUP(m_contrastManager)
118 CLEANUP(m_queue)
119 if (m_connection) {
120 m_connection->deleteLater();
121 m_connection = nullptr;
122 }
123 if (m_thread) {
124 m_thread->quit();
125 m_thread->wait();
126 delete m_thread;
127 m_thread = nullptr;
128 }
129 CLEANUP(m_display)
130#undef CLEANUP
131
132 // these are the children of the display
133 m_compositorInterface = nullptr;
134 m_contrastManagerInterface = nullptr;
135}
136
137void TestContrast::testCreate()
138{
139 QSignalSpy serverSurfaceCreated(m_compositorInterface, &KWin::CompositorInterface::surfaceCreated);
140
141 std::unique_ptr<KWayland::Client::Surface> surface(m_compositor->createSurface());
142 QVERIFY(serverSurfaceCreated.wait());
143
144 auto serverSurface = serverSurfaceCreated.first().first().value<KWin::SurfaceInterface *>();
145 QSignalSpy contrastChanged(serverSurface, &KWin::SurfaceInterface::contrastChanged);
146
147 auto contrast = m_contrastManager->createContrast(surface.get(), surface.get());
148 contrast->setRegion(m_compositor->createRegion(QRegion(0, 0, 10, 20), nullptr));
149
150 contrast->setContrast(0.2);
151 contrast->setIntensity(2.0);
152 contrast->setSaturation(1.7);
153
154 contrast->commit();
155 surface->commit(KWayland::Client::Surface::CommitFlag::None);
156
157 QVERIFY(contrastChanged.wait());
158 QCOMPARE(serverSurface->contrast()->region(), QRegion(0, 0, 10, 20));
159 QCOMPARE(wl_fixed_from_double(serverSurface->contrast()->contrast()), wl_fixed_from_double(0.2));
160 QCOMPARE(wl_fixed_from_double(serverSurface->contrast()->intensity()), wl_fixed_from_double(2.0));
161 QCOMPARE(wl_fixed_from_double(serverSurface->contrast()->saturation()), wl_fixed_from_double(1.7));
162
163 // and destroy
164 QSignalSpy destroyedSpy(serverSurface->contrast(), &QObject::destroyed);
165 delete contrast;
166 QVERIFY(destroyedSpy.wait());
167}
168
169void TestContrast::testSurfaceDestroy()
170{
171 QSignalSpy serverSurfaceCreated(m_compositorInterface, &KWin::CompositorInterface::surfaceCreated);
172
173 std::unique_ptr<KWayland::Client::Surface> surface(m_compositor->createSurface());
174 QVERIFY(serverSurfaceCreated.wait());
175
176 auto serverSurface = serverSurfaceCreated.first().first().value<KWin::SurfaceInterface *>();
177 QSignalSpy contrastChanged(serverSurface, &KWin::SurfaceInterface::contrastChanged);
178
179 std::unique_ptr<KWayland::Client::Contrast> contrast(m_contrastManager->createContrast(surface.get()));
180 contrast->setRegion(m_compositor->createRegion(QRegion(0, 0, 10, 20), nullptr));
181 contrast->commit();
182 surface->commit(KWayland::Client::Surface::CommitFlag::None);
183
184 QVERIFY(contrastChanged.wait());
185 QCOMPARE(serverSurface->contrast()->region(), QRegion(0, 0, 10, 20));
186
187 // destroy the parent surface
188 QSignalSpy surfaceDestroyedSpy(serverSurface, &QObject::destroyed);
189 QSignalSpy contrastDestroyedSpy(serverSurface->contrast(), &QObject::destroyed);
190 surface.reset();
191 QVERIFY(surfaceDestroyedSpy.wait());
192 QVERIFY(contrastDestroyedSpy.isEmpty());
193 // destroy the blur
194 contrast.reset();
195 QVERIFY(contrastDestroyedSpy.wait());
196}
197
198QTEST_GUILESS_MAIN(TestContrast)
199#include "test_wayland_contrast.moc"
void surfaceCreated(KWin::SurfaceInterface *surface)
Represents the Global for org_kde_kwin_contrast_manager interface.
Definition contrast.h:34
Class holding the Wayland server display loop.
Definition display.h:34
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
TestContrast(QObject *parent=nullptr)
#define CLEANUP(variable)