GCC Code Coverage Report


Directory: ./
File: autotests/client/keyboard_shortcuts_inhibitor.cpp
Date: 2024-01-22 17:25:27
Exec Total Coverage
Lines: 118 118 100.0%
Branches: 173 342 50.6%

Line Branch Exec Source
1 /****************************************************************************
2 Copyright 2020 Faveraux Adrien <ad1rie3@hotmail.fr>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) version 3, or any
8 later version accepted by the membership of KDE e.V. (or its
9 successor approved by the membership of KDE e.V.), which shall
10 act as a proxy defined in Section 6 of version 3 of the license.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library. If not, see <http://www.gnu.org/licenses/>.
19 ****************************************************************************/
20 #include <QtTest>
21
22 #include "../../src/client/compositor.h"
23 #include "../../src/client/connection_thread.h"
24 #include "../../src/client/event_queue.h"
25 #include "../../src/client/keyboard_shortcuts_inhibit.h"
26 #include "../../src/client/registry.h"
27 #include "../../src/client/seat.h"
28 #include "../../src/client/surface.h"
29
30 #include "../../server/compositor.h"
31 #include "../../server/display.h"
32 #include "../../server/keyboard_shortcuts_inhibit.h"
33 #include "../../server/seat.h"
34 #include "../../server/surface.h"
35
36 #include "../../tests/globals.h"
37
38 #include <wayland-keyboard-shortcuts-inhibit-client-protocol.h>
39
40 using namespace Wrapland::Client;
41 using namespace Wrapland::Server;
42
43 namespace Clt = Wrapland::Client;
44 namespace Srv = Wrapland::Server;
45
46 2 class TestKeyboardShortcutsInhibitor : public QObject
47 {
48 Q_OBJECT
49 private Q_SLOTS:
50 void init();
51 void cleanup();
52
53 void testKeyboardShortcuts();
54
55 private:
56 1 struct {
57 std::unique_ptr<Wrapland::Server::Display> display;
58 Wrapland::Server::globals globals;
59 } server;
60
61 QVector<Srv::Surface*> m_serverSurfaces;
62 QVector<Clt::Surface*> m_clientSurfaces;
63
64 1 ConnectionThread* m_connection = nullptr;
65 1 QThread* m_thread = nullptr;
66 1 EventQueue* m_queue = nullptr;
67 1 Clt::Seat* m_seat = nullptr;
68 Clt::Compositor* m_clientCompositor;
69 1 Clt::KeyboardShortcutsInhibitManagerV1* m_keyboard_shortcuts_inhibitor = nullptr;
70 };
71
72 constexpr auto socket_name{"wrapland-keyboard-shortcuts-inhibitor-test-0"};
73
74 1 void TestKeyboardShortcutsInhibitor::init()
75 {
76 1 qRegisterMetaType<Wrapland::Server::Surface*>();
77
78 1 server.display = std::make_unique<Wrapland::Server::Display>();
79
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 server.display->set_socket_name(socket_name);
80 1 server.display->start();
81
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 QVERIFY(server.display->running());
82
83 1 server.display->createShm();
84 1 auto server_seat
85 1 = server.globals.seats
86
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 .emplace_back(std::make_unique<Wrapland::Server::Seat>(server.display.get()))
87 1 .get();
88
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 server_seat->setName("seat0");
89 2 server.globals.keyboard_shortcuts_inhibit_manager_v1
90 2 = std::make_unique<Wrapland::Server::KeyboardShortcutsInhibitManagerV1>(
91 1 server.display.get());
92
93 // setup connection
94
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 m_connection = new Clt::ConnectionThread;
95 1 QSignalSpy connectedSpy(m_connection, &ConnectionThread::establishedChanged);
96
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 QVERIFY(connectedSpy.isValid());
97
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 m_connection->setSocketName(socket_name);
98
99
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 m_thread = new QThread(this);
100
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 m_connection->moveToThread(m_thread);
101
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 m_thread->start();
102
103
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 m_connection->establishConnection();
104
6/12
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 1 times.
1 QVERIFY(connectedSpy.count() || connectedSpy.wait());
105
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 QCOMPARE(connectedSpy.count(), 1);
106
107
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 m_queue = new EventQueue(this);
108
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 m_queue->setup(m_connection);
109
110
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 Registry registry;
111
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 QSignalSpy interfacesAnnouncedSpy(&registry, &Registry::interfacesAnnounced);
112
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 QSignalSpy compositorSpy(&registry, &Registry::compositorAnnounced);
113
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 QVERIFY(interfacesAnnouncedSpy.isValid());
114
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 registry.setEventQueue(m_queue);
115
116
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 registry.create(m_connection);
117
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 QVERIFY(registry.isValid());
118
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 registry.setup();
119
5/10
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 1 times.
1 QVERIFY(interfacesAnnouncedSpy.wait());
120
121 2 server.globals.compositor
122
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 = std::make_unique<Wrapland::Server::Compositor>(server.display.get());
123
124
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
2 connect(server.globals.compositor.get(),
125 &Srv::Compositor::surfaceCreated,
126 this,
127 4 [this](Srv::Surface* surface) { m_serverSurfaces += surface; });
128
129
5/10
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 1 times.
1 QVERIFY(compositorSpy.wait());
130
4/8
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
2 m_clientCompositor = registry.createCompositor(compositorSpy.first().first().value<quint32>(),
131
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 compositorSpy.first().last().value<quint32>(),
132 this);
133
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 QVERIFY(m_clientCompositor->isValid());
134
135
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 m_seat = registry.createSeat(registry.interface(Registry::Interface::Seat).name,
136
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 registry.interface(Registry::Interface::Seat).version,
137 this);
138
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 QVERIFY(m_seat->isValid());
139
140
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 m_keyboard_shortcuts_inhibitor = registry.createKeyboardShortcutsInhibitManagerV1(
141
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 registry.interface(Registry::Interface::KeyboardShortcutsInhibitManagerV1).name,
142
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 registry.interface(Registry::Interface::KeyboardShortcutsInhibitManagerV1).version,
143 this);
144
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 QVERIFY(m_keyboard_shortcuts_inhibitor->isValid());
145
146
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 QSignalSpy surfaceSpy(server.globals.compositor.get(), &Srv::Compositor::surfaceCreated);
147
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 for (int i = 0; i < 3; ++i) {
148
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 m_clientSurfaces += m_clientCompositor->createSurface(this);
149 3 }
150
4/8
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
2 QVERIFY(surfaceSpy.count() < 3 && surfaceSpy.wait(200));
151
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 QVERIFY(m_serverSurfaces.count() == 3);
152
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 QVERIFY(m_keyboard_shortcuts_inhibitor);
153
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 }
154
155 1 void TestKeyboardShortcutsInhibitor::cleanup()
156 {
157 #define CLEANUP(variable) \
158 if (variable) { \
159 delete variable; \
160 variable = nullptr; \
161 }
162
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 CLEANUP(m_keyboard_shortcuts_inhibitor)
163
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 CLEANUP(m_seat)
164
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 CLEANUP(m_queue)
165
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 CLEANUP(m_clientCompositor)
166
167
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 for (auto surface : m_clientSurfaces) {
168
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 delete surface;
169 }
170 1 m_clientSurfaces.clear();
171
172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (m_connection) {
173 1 m_connection->deleteLater();
174 1 m_connection = nullptr;
175 1 }
176
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (m_thread) {
177 1 m_thread->quit();
178 1 m_thread->wait();
179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 delete m_thread;
180 1 m_thread = nullptr;
181 1 }
182 #undef CLEANUP
183
184
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 server = {};
185 1 }
186
187 1 void TestKeyboardShortcutsInhibitor::testKeyboardShortcuts()
188 {
189 1 auto surface = m_clientSurfaces[0];
190
191 // Test
192 1 QSignalSpy inhibitorCreatedSpy(m_keyboard_shortcuts_inhibitor,
193 &Clt::KeyboardShortcutsInhibitManagerV1::inhibitorCreated);
194
195
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 auto inhibitorClient = m_keyboard_shortcuts_inhibitor->inhibitShortcuts(surface, m_seat);
196
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 QSignalSpy inhibitorActiveSpy(inhibitorClient,
197 &Clt::KeyboardShortcutsInhibitorV1::inhibitorActive);
198
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 QSignalSpy inhibitorInactiveSpy(inhibitorClient,
199 &Clt::KeyboardShortcutsInhibitorV1::inhibitorInactive);
200
201
6/12
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 1 times.
1 QVERIFY(inhibitorCreatedSpy.wait() || inhibitorCreatedSpy.count() == 1);
202
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 auto inhibitorServer = server.globals.keyboard_shortcuts_inhibit_manager_v1->findInhibitor(
203
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 m_serverSurfaces[0], server.globals.seats.back().get());
204
205 // Test deactivate
206
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 inhibitorServer->setActive(false);
207
6/12
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 1 times.
1 QVERIFY(inhibitorInactiveSpy.wait() || inhibitorInactiveSpy.count() == 1);
208
209 // Test activate
210
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 inhibitorServer->setActive(true);
211
6/12
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 1 times.
1 QVERIFY(inhibitorInactiveSpy.wait() || inhibitorInactiveSpy.count() == 1);
212
213 // Test creating for another surface
214 1 auto inhibitorClient2
215
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 = m_keyboard_shortcuts_inhibitor->inhibitShortcuts(m_clientSurfaces[1], m_seat);
216
6/12
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 1 times.
1 QVERIFY(inhibitorCreatedSpy.wait() || inhibitorCreatedSpy.count() == 2);
217
218 // Test destroy is working
219
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 delete inhibitorClient;
220
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 inhibitorClient = m_keyboard_shortcuts_inhibitor->inhibitShortcuts(surface, m_seat);
221
6/12
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 1 times.
1 QVERIFY(inhibitorCreatedSpy.wait() || inhibitorCreatedSpy.count() == 3);
222
223 // Test creating with same surface / seat (expect error)
224
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 QSignalSpy errorOccured(m_connection, &Wrapland::Client::ConnectionThread::establishedChanged);
225
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 QCOMPARE(m_connection->error(), 0);
226
227 1 auto inhibitorClient3
228
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 = m_keyboard_shortcuts_inhibitor->inhibitShortcuts(m_clientSurfaces[0], m_seat);
229
230
5/10
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 1 times.
1 QVERIFY(errorOccured.wait());
231
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 QCOMPARE(m_connection->error(), EPROTO);
232
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 QVERIFY(m_connection->hasProtocolError());
233
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
1 QCOMPARE(m_connection->protocolError(),
234 ZWP_KEYBOARD_SHORTCUTS_INHIBIT_MANAGER_V1_ERROR_ALREADY_INHIBITED);
235
236
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 delete inhibitorClient;
237
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 delete inhibitorClient2;
238
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 delete inhibitorClient3;
239
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 }
240
241
4/8
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
1 QTEST_GUILESS_MAIN(TestKeyboardShortcutsInhibitor)
242 #include "keyboard_shortcuts_inhibitor.moc"
243