KWin
Loading...
Searching...
No Matches
test_wayland_appmenu.cpp
Go to the documentation of this file.
1/*
2 SPDX-FileCopyrightText: 2017 David Edmundson <davidedmundson@kde.org>
3 SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@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/appmenu.h"
12#include "wayland/compositor.h"
13#include "wayland/display.h"
14
15#include "KWayland/Client/appmenu.h"
16#include "KWayland/Client/compositor.h"
17#include "KWayland/Client/connection_thread.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
24
25class TestAppmenu : public QObject
26{
27 Q_OBJECT
28public:
29 explicit TestAppmenu(QObject *parent = nullptr);
30private Q_SLOTS:
31 void init();
32 void cleanup();
33
34 void testCreateAndSet();
35
36private:
37 KWin::Display *m_display;
38 KWin::CompositorInterface *m_compositorInterface;
39 KWin::AppMenuManagerInterface *m_appmenuManagerInterface;
40 KWayland::Client::ConnectionThread *m_connection;
41 KWayland::Client::Compositor *m_compositor;
42 KWayland::Client::AppMenuManager *m_appmenuManager;
43 KWayland::Client::EventQueue *m_queue;
44 QThread *m_thread;
45};
46
47static const QString s_socketName = QStringLiteral("kwayland-test-wayland-appmenu-0");
48
50 : QObject(parent)
51 , m_display(nullptr)
52 , m_compositorInterface(nullptr)
53 , m_connection(nullptr)
54 , m_compositor(nullptr)
55 , m_queue(nullptr)
56 , m_thread(nullptr)
57{
58}
59
60void TestAppmenu::init()
61{
62 using namespace KWin;
63 qRegisterMetaType<AppMenuInterface::InterfaceAddress>();
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 appmenuSpy(&registry, &KWayland::Client::Registry::appMenuAnnounced);
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_appmenuManagerInterface = new AppMenuManagerInterface(m_display, m_display);
104
105 QVERIFY(appmenuSpy.wait());
106 m_appmenuManager = registry.createAppMenuManager(appmenuSpy.first().first().value<quint32>(), appmenuSpy.first().last().value<quint32>(), this);
107}
108
109void TestAppmenu::cleanup()
110{
111#define CLEANUP(variable) \
112 if (variable) { \
113 delete variable; \
114 variable = nullptr; \
115 }
116 CLEANUP(m_compositor)
117 CLEANUP(m_appmenuManager)
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_compositorInterface)
130 CLEANUP(m_appmenuManagerInterface)
131 CLEANUP(m_display)
132#undef CLEANUP
133}
134
135void TestAppmenu::testCreateAndSet()
136{
137 QSignalSpy serverSurfaceCreated(m_compositorInterface, &KWin::CompositorInterface::surfaceCreated);
138
139 std::unique_ptr<KWayland::Client::Surface> surface(m_compositor->createSurface());
140 QVERIFY(serverSurfaceCreated.wait());
141
142 auto serverSurface = serverSurfaceCreated.first().first().value<KWin::SurfaceInterface *>();
143 QSignalSpy appMenuCreated(m_appmenuManagerInterface, &KWin::AppMenuManagerInterface::appMenuCreated);
144
145 QVERIFY(!m_appmenuManagerInterface->appMenuForSurface(serverSurface));
146
147 auto appmenu = m_appmenuManager->create(surface.get(), surface.get());
148 QVERIFY(appMenuCreated.wait());
149 auto appMenuInterface = appMenuCreated.first().first().value<KWin::AppMenuInterface *>();
150 QCOMPARE(m_appmenuManagerInterface->appMenuForSurface(serverSurface), appMenuInterface);
151
152 QCOMPARE(appMenuInterface->address().serviceName, QString());
153 QCOMPARE(appMenuInterface->address().objectPath, QString());
154
155 QSignalSpy appMenuChangedSpy(appMenuInterface, &KWin::AppMenuInterface::addressChanged);
156
157 appmenu->setAddress("net.somename", "/test/path");
158
159 QVERIFY(appMenuChangedSpy.wait());
160 QCOMPARE(appMenuInterface->address().serviceName, QString("net.somename"));
161 QCOMPARE(appMenuInterface->address().objectPath, QString("/test/path"));
162
163 // and destroy
164 QSignalSpy destroyedSpy(appMenuInterface, &QObject::destroyed);
165 delete appmenu;
166 QVERIFY(destroyedSpy.wait());
167 QVERIFY(!m_appmenuManagerInterface->appMenuForSurface(serverSurface));
168}
169
170QTEST_GUILESS_MAIN(TestAppmenu)
171#include "test_wayland_appmenu.moc"
void addressChanged(KWin::AppMenuInterface::InterfaceAddress)
AppMenuInterface * appMenuForSurface(SurfaceInterface *)
Definition appmenu.cpp:111
void appMenuCreated(KWin::AppMenuInterface *)
void surfaceCreated(KWin::SurfaceInterface *surface)
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
TestAppmenu(QObject *parent=nullptr)
Q_DECLARE_METATYPE(KWin::SwitchEvent::State)
#define CLEANUP(variable)