Debian Codegraph @

tests/auto/wayland/primaryselectionv1/tst_primaryselectionv1.cpp

raw
1 // Copyright (C) 2018 The Qt Company Ltd.
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4 #include "mockcompositor.h"
5
6 #include <qwayland-server-wp-primary-selection-unstable-v1.h>
7
8 #include <QtGui/QRasterWindow>
9 #include <QtGui/QClipboard>
10 #include <QtCore/private/qcore_unix_p.h>
11
12 #include <fcntl.h>
13
14 using namespace MockCompositor;
15
16 constexpr int primarySelectionVersion = 1; // protocol VERSION, not the name suffix (_v1)
17
18 class PrimarySelectionDeviceV1;
19 class PrimarySelectionDeviceManagerV1;
20
21 class PrimarySelectionOfferV1 : public QObject, public QtWaylandServer::zwp_primary_selection_offer_v1
22 {
23 Q_OBJECT
24 public:
25 explicit PrimarySelectionOfferV1(PrimarySelectionDeviceV1 *device, wl_client *client, int version)
26 : zwp_primary_selection_offer_v1(client, 0, version)
27 , m_device(device)
28 {}
29 void send_offer() = delete;
30 void sendOffer(const QString &offer)
31 {
32 zwp_primary_selection_offer_v1::send_offer(offer);
33 m_mimeTypes << offer;
34 }
35
36 PrimarySelectionDeviceV1 *m_device = nullptr;
37 QStringList m_mimeTypes;
38
39 signals:
40 void receive(QString mimeType, int fd);
41
42 protected:
43 void zwp_primary_selection_offer_v1_destroy_resource(Resource *resource) override
44 {
45 Q_UNUSED(resource);
46 delete this;
47 }
48
49 void zwp_primary_selection_offer_v1_receive(Resource *resource, const QString &mime_type, int32_t fd) override
50 {
51 Q_UNUSED(resource);
52 QTRY_VERIFY(m_mimeTypes.contains(mime_type));
53 emit receive(mime_type, fd);
54 }
55
56 void zwp_primary_selection_offer_v1_destroy(Resource *resource) override;
57 };
58
59 class PrimarySelectionSourceV1 : public QObject, public QtWaylandServer::zwp_primary_selection_source_v1
60 {
61 Q_OBJECT
62 public:
63 explicit PrimarySelectionSourceV1(wl_client *client, int id, int version)
64 : zwp_primary_selection_source_v1(client, id, version)
65 {
66 }
67 QStringList m_offers;
68 protected:
69 void zwp_primary_selection_source_v1_destroy_resource(Resource *resource) override
70 {
71 Q_UNUSED(resource);
72 delete this;
73 }
74 void zwp_primary_selection_source_v1_offer(Resource *resource, const QString &mime_type) override
75 {
76 Q_UNUSED(resource);
77 m_offers << mime_type;
78 }
79 void zwp_primary_selection_source_v1_destroy(Resource *resource) override
80 {
81 wl_resource_destroy(resource->handle);
82 }
83 };
84
85 class PrimarySelectionDeviceV1 : public QObject, public QtWaylandServer::zwp_primary_selection_device_v1
86 {
87 Q_OBJECT
88 public:
89 explicit PrimarySelectionDeviceV1(PrimarySelectionDeviceManagerV1 *manager, Seat *seat)
90 : m_manager(manager)
91 , m_seat(seat)
92 {}
93
94 void send_data_offer(::wl_resource *resource) = delete;
95
96 PrimarySelectionOfferV1 *sendDataOffer(::wl_client *client, const QStringList &mimeTypes = {});
97
98 PrimarySelectionOfferV1 *sendDataOffer(const QStringList &mimeTypes = {}) // creates a new offer for the focused surface and sends it
99 {
100 Q_ASSERT(m_seat->m_capabilities & Seat::capability_keyboard);
101 Q_ASSERT(m_seat->m_keyboard->m_enteredSurface);
102 auto *client = m_seat->m_keyboard->m_enteredSurface->resource()->client();
103 return sendDataOffer(client, mimeTypes);
104 }
105
106 void send_selection(::wl_resource *resource) = delete;
107 void sendSelection(PrimarySelectionOfferV1 *offer)
108 {
109 auto *client = offer->resource()->client();
110 for (auto *resource : resourceMap().values(client))
111 zwp_primary_selection_device_v1::send_selection(resource->handle, offer->resource()->handle);
112 m_sentSelectionOffers << offer;
113 }
114
115 PrimarySelectionDeviceManagerV1 *m_manager = nullptr;
116 Seat *m_seat = nullptr;
117 QList<PrimarySelectionOfferV1 *> m_sentSelectionOffers;
118 PrimarySelectionSourceV1 *m_selectionSource = nullptr;
119 uint m_serial = 0;
120
121 protected:
122 void zwp_primary_selection_device_v1_set_selection(Resource *resource, ::wl_resource *source, uint32_t serial) override
123 {
124 Q_UNUSED(resource);
125 m_selectionSource = fromResource<PrimarySelectionSourceV1>(source);
126 m_serial = serial;
127 }
128 void zwp_primary_selection_device_v1_destroy(Resource *resource) override
129 {
130 wl_resource_destroy(resource->handle);
131 }
132 };
133
134 class PrimarySelectionDeviceManagerV1 : public Global, public QtWaylandServer::zwp_primary_selection_device_manager_v1
135 {
136 Q_OBJECT
137 public:
138 explicit PrimarySelectionDeviceManagerV1(CoreCompositor *compositor, int version = 1)
139 : QtWaylandServer::zwp_primary_selection_device_manager_v1(compositor->m_display, version)
140 , m_version(version)
141 {}
142 ~PrimarySelectionDeviceManagerV1() override
143 {
144 qDeleteAll(m_devices);
145 }
146 bool isClean() override
147 {
148 for (auto *device : std::as_const(m_devices)) {
149 // The client should not leak selection offers, i.e. if this fails, there is a missing
150 // zwp_primary_selection_offer_v1.destroy request
151 if (!device->m_sentSelectionOffers.empty())
152 return false;
153 }
154 return true;
155 }
156
157 PrimarySelectionDeviceV1 *deviceFor(Seat *seat)
158 {
159 Q_ASSERT(seat);
160 if (auto *device = m_devices.value(seat, nullptr))
161 return device;
162
163 auto *device = new PrimarySelectionDeviceV1(this, seat);
164 m_devices[seat] = device;
165 return device;
166 }
167
168 int m_version = 1; // TODO: Remove on libwayland upgrade
169 QMap<Seat *, PrimarySelectionDeviceV1 *> m_devices;
170 QList<PrimarySelectionSourceV1 *> m_sources;
171 protected:
172 void zwp_primary_selection_device_manager_v1_destroy(Resource *resource) override
173 {
174 // The protocol doesn't say whether managed objects should be destroyed as well,
175 // so leave them alone, they'll be cleaned up in the destructor anyway
176 wl_resource_destroy(resource->handle);
177 }
178
179 void zwp_primary_selection_device_manager_v1_create_source(Resource *resource, uint32_t id) override
180 {
181 int version = m_version;
182 m_sources << new PrimarySelectionSourceV1(resource->client(), id, version);
183 }
184 void zwp_primary_selection_device_manager_v1_get_device(Resource *resource, uint32_t id, ::wl_resource *seatResource) override
185 {
186 auto *seat = fromResource<Seat>(seatResource);
187 QVERIFY(seat);
188 auto *device = deviceFor(seat);
189 device->add(resource->client(), id, resource->version());
190 }
191 };
192
193 PrimarySelectionOfferV1 *PrimarySelectionDeviceV1::sendDataOffer(wl_client *client, const QStringList &mimeTypes)
194 {
195 Q_ASSERT(client);
196 auto *offer = new PrimarySelectionOfferV1(this, client, m_manager->m_version);
197 for (auto *resource : resourceMap().values(client))
198 zwp_primary_selection_device_v1::send_data_offer(resource->handle, offer->resource()->handle);
199 for (const auto &mimeType : mimeTypes)
200 offer->sendOffer(mimeType);
201 return offer;
202 }
203
204 void PrimarySelectionOfferV1::zwp_primary_selection_offer_v1_destroy(QtWaylandServer::zwp_primary_selection_offer_v1::Resource *resource)
205 {
206 bool removed = m_device->m_sentSelectionOffers.removeOne(this);
207 QVERIFY(removed);
208 wl_resource_destroy(resource->handle);
209 }
210
211 class PrimarySelectionCompositor : public DefaultCompositor {
212 public:
213 explicit PrimarySelectionCompositor()
214 {
215 exec([this] {
216 m_config.autoConfigure = true;
217 add<PrimarySelectionDeviceManagerV1>(primarySelectionVersion);
218 });
219 }
220 PrimarySelectionDeviceV1 *primarySelectionDevice(int i = 0) {
221 return get<PrimarySelectionDeviceManagerV1>()->deviceFor(get<Seat>(i));
222 }
223 };
224
225 class tst_primaryselectionv1 : public QObject, private PrimarySelectionCompositor
226 {
227 Q_OBJECT
228 private slots:
229 void cleanup() { QTRY_VERIFY2(isClean(), qPrintable(dirtyMessage())); }
230 void initTestCase();
231 void bindsToManager();
232 void createsPrimaryDevice();
233 void createsPrimaryDeviceForNewSeats();
234 void pasteAscii();
235 void pasteUtf8();
236 void destroysPreviousSelection();
237 void destroysSelectionOnLeave();
238 void copy();
239 };
240
241 void tst_primaryselectionv1::initTestCase()
242 {
243 QCOMPOSITOR_TRY_VERIFY(pointer());
244 QCOMPOSITOR_TRY_VERIFY(keyboard());
245 }
246
247 void tst_primaryselectionv1::bindsToManager()
248 {
249 QCOMPOSITOR_TRY_COMPARE(get<PrimarySelectionDeviceManagerV1>()->resourceMap().size(), 1);
250 QCOMPOSITOR_TRY_COMPARE(get<PrimarySelectionDeviceManagerV1>()->resourceMap().first()->version(), primarySelectionVersion);
251 }
252
253 void tst_primaryselectionv1::createsPrimaryDevice()
254 {
255 QCOMPOSITOR_TRY_VERIFY(primarySelectionDevice());
256 QCOMPOSITOR_TRY_VERIFY(primarySelectionDevice()->resourceMap().contains(client()));
257 QCOMPOSITOR_TRY_COMPARE(primarySelectionDevice()->resourceMap().value(client())->version(), primarySelectionVersion);
258 QTRY_VERIFY(QGuiApplication::clipboard()->supportsSelection());
259 }
260
261 void tst_primaryselectionv1::createsPrimaryDeviceForNewSeats()
262 {
263 exec([&] { add<Seat>(); });
264 QCOMPOSITOR_TRY_VERIFY(primarySelectionDevice(1));
265 }
266
267 void tst_primaryselectionv1::pasteAscii()
268 {
269 class Window : public QRasterWindow {
270 public:
271 void mousePressEvent(QMouseEvent *event) override
272 {
273 Q_UNUSED(event);
274 auto *mimeData = QGuiApplication::clipboard()->mimeData(QClipboard::Selection);
275 m_formats = mimeData->formats();
276 m_text = QGuiApplication::clipboard()->text(QClipboard::Selection);
277 }
278 QStringList m_formats;
279 QString m_text;
280 };
281
282 Window window;
283 window.resize(64, 64);
284 window.show();
285
286 QCOMPOSITOR_TRY_VERIFY(xdgSurface() && xdgSurface()->m_committedConfigureSerial);
287 exec([&] {
288 auto *surface = xdgSurface()->m_surface;
289 keyboard()->sendEnter(surface); // Need to set keyboard focus according to protocol
290
291 auto *device = primarySelectionDevice();
292 auto *offer = device->sendDataOffer({"text/plain"});
293 connect(offer, &PrimarySelectionOfferV1::receive, offer, [](QString mimeType, int fd) {
294 QFile file;
295 QVERIFY(file.open(fd, QIODevice::WriteOnly, QFile::FileHandleFlag::AutoCloseHandle));
296 QCOMPARE(mimeType, "text/plain");
297 file.write(QByteArray("normal ascii"));
298 file.close();
299 }, Qt::DirectConnection);
300 device->sendSelection(offer);
301
302 pointer()->sendEnter(surface, {32, 32});
303 pointer()->sendFrame(client());
304 pointer()->sendButton(client(), BTN_MIDDLE, 1);
305 pointer()->sendFrame(client());
306 pointer()->sendButton(client(), BTN_MIDDLE, 0);
307 pointer()->sendFrame(client());
308 });
309 QTRY_COMPARE(window.m_formats, QStringList{"text/plain"});
310 QTRY_COMPARE(window.m_text, "normal ascii");
311 }
312
313 void tst_primaryselectionv1::pasteUtf8()
314 {
315 class Window : public QRasterWindow {
316 public:
317 void mousePressEvent(QMouseEvent *event) override
318 {
319 Q_UNUSED(event);
320 auto *mimeData = QGuiApplication::clipboard()->mimeData(QClipboard::Selection);
321 m_formats = mimeData->formats();
322 m_text = QGuiApplication::clipboard()->text(QClipboard::Selection);
323 }
324 QStringList m_formats;
325 QString m_text;
326 };
327
328 Window window;
329 window.resize(64, 64);
330 window.show();
331
332 QCOMPOSITOR_TRY_VERIFY(xdgSurface() && xdgSurface()->m_committedConfigureSerial);
333 exec([&] {
334 auto *surface = xdgSurface()->m_surface;
335 keyboard()->sendEnter(surface); // Need to set keyboard focus according to protocol
336
337 auto *device = primarySelectionDevice();
338 auto *offer = device->sendDataOffer({"text/plain", "text/plain;charset=utf-8"});
339 connect(offer, &PrimarySelectionOfferV1::receive, offer, [](QString mimeType, int fd) {
340 QFile file;
341 QVERIFY(file.open(fd, QIODevice::WriteOnly, QFile::FileHandleFlag::AutoCloseHandle));
342 QCOMPARE(mimeType, "text/plain;charset=utf-8");
343 file.write(QByteArray("face with tears of joy: 😂"));
344 file.close();
345 }, Qt::DirectConnection);
346 device->sendSelection(offer);
347
348 pointer()->sendEnter(surface, {32, 32});
349 pointer()->sendFrame(client());
350 pointer()->sendButton(client(), BTN_MIDDLE, 1);
351 pointer()->sendFrame(client());
352 pointer()->sendButton(client(), BTN_MIDDLE, 0);
353 pointer()->sendFrame(client());
354 });
355 QTRY_COMPARE(window.m_formats, QStringList({"text/plain"}));
356 QTRY_COMPARE(window.m_text, "face with tears of joy: 😂");
357 }
358
359 void tst_primaryselectionv1::destroysPreviousSelection()
360 {
361 QRasterWindow window;
362 window.resize(64, 64);
363 window.show();
364 QCOMPOSITOR_TRY_VERIFY(xdgSurface() && xdgSurface()->m_committedConfigureSerial);
365
366 // When the client receives a selection event, it is required to destroy the previous offer
367 exec([&] {
368 auto *surface = xdgSurface()->m_surface;
369 keyboard()->sendEnter(surface); // Need to set keyboard focus according to protocol
370
371 auto *offer = primarySelectionDevice()->sendDataOffer({"text/plain"});
372 primarySelectionDevice()->sendSelection(offer);
373 });
374
375 exec([&] {
376 auto *offer = primarySelectionDevice()->sendDataOffer({"text/plain"});
377 primarySelectionDevice()->sendSelection(offer);
378 QCOMPARE(primarySelectionDevice()->m_sentSelectionOffers.size(), 2);
379 });
380
381 // Verify the first offer gets destroyed
382 QCOMPOSITOR_TRY_COMPARE(primarySelectionDevice()->m_sentSelectionOffers.size(), 1);
383 }
384
385 void tst_primaryselectionv1::destroysSelectionOnLeave()
386 {
387 QRasterWindow window;
388 window.resize(64, 64);
389 window.show();
390 QCOMPOSITOR_TRY_VERIFY(xdgSurface() && xdgSurface()->m_committedConfigureSerial);
391
392 exec([&] {
393 auto *surface = xdgSurface()->m_surface;
394 keyboard()->sendEnter(surface); // Need to set keyboard focus according to protocol
395
396 auto *offer = primarySelectionDevice()->sendDataOffer({"text/plain"});
397 primarySelectionDevice()->sendSelection(offer);
398 });
399
400 QTRY_VERIFY(QGuiApplication::clipboard()->mimeData(QClipboard::Selection));
401 QTRY_VERIFY(QGuiApplication::clipboard()->mimeData(QClipboard::Selection)->hasText());
402
403 QSignalSpy selectionChangedSpy(QGuiApplication::clipboard(), &QClipboard::selectionChanged);
404
405 exec([&] {
406 auto *surface = xdgSurface()->m_surface;
407 keyboard()->sendLeave(surface);
408 });
409
410 QTRY_COMPARE(selectionChangedSpy.size(), 1);
411 QVERIFY(!QGuiApplication::clipboard()->mimeData(QClipboard::Selection)->hasText());
412 }
413
414 void tst_primaryselectionv1::copy()
415 {
416 class Window : public QRasterWindow {
417 public:
418 void mousePressEvent(QMouseEvent *event) override
419 {
420 Q_UNUSED(event);
421 QGuiApplication::clipboard()->setText("face with tears of joy: 😂", QClipboard::Selection);
422 }
423 QStringList m_formats;
424 QString m_text;
425 };
426
427 Window window;
428 window.resize(64, 64);
429 window.show();
430
431 QCOMPOSITOR_TRY_VERIFY(xdgSurface() && xdgSurface()->m_committedConfigureSerial);
432 QList<uint> mouseSerials;
433 exec([&] {
434 auto *surface = xdgSurface()->m_surface;
435 keyboard()->sendEnter(surface); // Need to set keyboard focus according to protocol
436 pointer()->sendEnter(surface, {32, 32});
437 pointer()->sendFrame(client());
438 mouseSerials << pointer()->sendButton(client(), BTN_MIDDLE, 1);
439 pointer()->sendFrame(client());
440 mouseSerials << pointer()->sendButton(client(), BTN_MIDDLE, 0);
441 pointer()->sendFrame(client());
442 });
443 QCOMPOSITOR_TRY_VERIFY(primarySelectionDevice()->m_selectionSource);
444 QCOMPOSITOR_TRY_VERIFY(mouseSerials.contains(primarySelectionDevice()->m_serial));
445 QVERIFY(QGuiApplication::clipboard()->ownsSelection());
446 QByteArray pastedBuf;
447 exec([&](){
448 auto *source = primarySelectionDevice()->m_selectionSource;
449 QCOMPARE(source->m_offers, QStringList({"text/plain", "text/plain;charset=utf-8"}));
450 int fd[2];
451 if (pipe(fd) == -1)
452 QSKIP("Failed to create pipe");
453 fcntl(fd[0], F_SETFL, fcntl(fd[0], F_GETFL, 0) | O_NONBLOCK);
454 source->send_send("text/plain;charset=utf-8", fd[1]);
455 auto *notifier = new QSocketNotifier(fd[0], QSocketNotifier::Read, this);
456 connect(notifier, &QSocketNotifier::activated, this, [&](int fd) {
457 exec([&]{
458 static char buf[1024];
459 int n = QT_READ(fd, buf, sizeof buf);
460 if (n <= 0) {
461 delete notifier;
462 close(fd);
463 } else {
464 pastedBuf.append(buf, n);
465 }
466 });
467 }, Qt::DirectConnection);
468 });
469
470 QCOMPOSITOR_TRY_VERIFY(pastedBuf.size()); // this assumes we got everything in one read
471 auto pasted = QString::fromUtf8(pastedBuf);
472 QCOMPARE(pasted, "face with tears of joy: 😂");
473 }
474
475 QCOMPOSITOR_TEST_MAIN(tst_primaryselectionv1)
476 #include "tst_primaryselectionv1.moc"