KWin
Loading...
Searching...
No Matches
test_error.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
10// server
11#include "wayland/compositor.h"
12#include "wayland/display.h"
13#include "wayland/plasmashell.h"
14
15// client
16#include "KWayland/Client/compositor.h"
17#include "KWayland/Client/connection_thread.h"
18#include "KWayland/Client/event_queue.h"
19#include "KWayland/Client/plasmashell.h"
20#include "KWayland/Client/registry.h"
21#include "KWayland/Client/surface.h"
22
23#include <wayland-client-protocol.h>
24
25#include <cerrno> // For EPROTO
26
27using namespace KWin;
28
29class ErrorTest : public QObject
30{
31 Q_OBJECT
32private Q_SLOTS:
33 void init();
34 void cleanup();
35
36 void testMultiplePlasmaShellSurfacesForSurface();
37
38private:
39 KWin::Display *m_display = nullptr;
40 CompositorInterface *m_ci = nullptr;
41 PlasmaShellInterface *m_psi = nullptr;
42 KWayland::Client::ConnectionThread *m_connection = nullptr;
43 QThread *m_thread = nullptr;
44 KWayland::Client::EventQueue *m_queue = nullptr;
45 KWayland::Client::Compositor *m_compositor = nullptr;
46 KWayland::Client::PlasmaShell *m_plasmaShell = nullptr;
47};
48
49static const QString s_socketName = QStringLiteral("kwayland-test-error-0");
50
51void ErrorTest::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_ci = new CompositorInterface(m_display, m_display);
60 m_psi = new PlasmaShellInterface(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_compositor =
86 registry.createCompositor(registry.interface(KWayland::Client::Registry::Interface::Compositor).name, registry.interface(KWayland::Client::Registry::Interface::Compositor).version, this);
87 QVERIFY(m_compositor);
88 m_plasmaShell = registry.createPlasmaShell(registry.interface(KWayland::Client::Registry::Interface::PlasmaShell).name,
89 registry.interface(KWayland::Client::Registry::Interface::PlasmaShell).version,
90 this);
91 QVERIFY(m_plasmaShell);
92}
93
94void ErrorTest::cleanup()
95{
96#define CLEANUP(variable) \
97 if (variable) { \
98 delete variable; \
99 variable = nullptr; \
100 }
101 CLEANUP(m_plasmaShell)
102 CLEANUP(m_compositor)
103 CLEANUP(m_queue)
104 if (m_connection) {
105 m_connection->deleteLater();
106 m_connection = nullptr;
107 }
108 if (m_thread) {
109 m_thread->quit();
110 m_thread->wait();
111 delete m_thread;
112 m_thread = nullptr;
113 }
114 CLEANUP(m_display)
115#undef CLEANUP
116 // these are the children of the display
117 m_psi = nullptr;
118 m_ci = nullptr;
119}
120
121void ErrorTest::testMultiplePlasmaShellSurfacesForSurface()
122{
123 // this test verifies that creating two ShellSurfaces for the same Surface triggers a protocol error
124 QSignalSpy errorSpy(m_connection, &KWayland::Client::ConnectionThread::errorOccurred);
125 // PlasmaShell is too smart and doesn't allow us to create a second PlasmaShellSurface
126 // thus we need to cheat by creating a surface manually
127 auto surface = wl_compositor_create_surface(*m_compositor);
128 std::unique_ptr<KWayland::Client::PlasmaShellSurface> shellSurface1(m_plasmaShell->createSurface(surface));
129 std::unique_ptr<KWayland::Client::PlasmaShellSurface> shellSurface2(m_plasmaShell->createSurface(surface));
130 QVERIFY(!m_connection->hasError());
131 QVERIFY(errorSpy.wait());
132 QVERIFY(m_connection->hasError());
133 QCOMPARE(m_connection->errorCode(), EPROTO);
134 wl_surface_destroy(surface);
135}
136
137QTEST_GUILESS_MAIN(ErrorTest)
138#include "test_error.moc"
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
Global for the org_kde_plasma_shell interface.
Definition plasmashell.h:37
#define CLEANUP(variable)