Debian Codegraph @

tests/auto/wayland/datadevicev1/tst_datadevicev1.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 <QtGui/QRasterWindow>
7 #include <QtGui/QClipboard>
8 #include <QtGui/QDrag>
9
10 using namespace MockCompositor;
11
12 constexpr int dataDeviceVersion = 3;
13
14 class DataDeviceCompositor : public DefaultCompositor {
15 public:
16 explicit DataDeviceCompositor()
17 {
18 exec([this] {
19 m_config.autoConfigure = true;
20 add<DataDeviceManager>(dataDeviceVersion);
21 });
22 }
23 DataDevice *dataDevice() { return get<DataDeviceManager>()->deviceFor(get<Seat>()); }
24 };
25
26 class tst_datadevicev1 : public QObject, private DataDeviceCompositor
27 {
28 Q_OBJECT
29 private slots:
30 void cleanup() { QTRY_VERIFY2(isClean(), qPrintable(dirtyMessage())); }
31 void initTestCase();
32 void pasteAscii();
33 void pasteUtf8();
34 void pasteMozUrl();
35 void pasteSingleUtf8MozUrl();
36 void destroysPreviousSelection();
37 void destroysSelectionWithSurface();
38 void destroysSelectionOnLeave();
39 void dragWithoutFocus();
40 };
41
42 void tst_datadevicev1::initTestCase()
43 {
44 QCOMPOSITOR_TRY_VERIFY(pointer());
45 QCOMPOSITOR_TRY_VERIFY(keyboard());
46
47 QCOMPOSITOR_TRY_VERIFY(dataDevice());
48 QCOMPOSITOR_TRY_VERIFY(dataDevice()->resourceMap().contains(client()));
49 QCOMPOSITOR_TRY_COMPARE(dataDevice()->resourceMap().value(client())->version(), dataDeviceVersion);
50 }
51
52 void tst_datadevicev1::pasteAscii()
53 {
54 class Window : public QRasterWindow {
55 public:
56 void mousePressEvent(QMouseEvent *) override { m_text = QGuiApplication::clipboard()->text(); }
57 QString m_text;
58 };
59
60 Window window;
61 window.resize(64, 64);
62 window.show();
63
64 QCOMPOSITOR_TRY_VERIFY(xdgSurface() && xdgSurface()->m_committedConfigureSerial);
65 exec([&] {
66 auto *client = xdgSurface()->resource()->client();
67 auto *offer = dataDevice()->sendDataOffer(client, {"text/plain"});
68 connect(offer, &DataOffer::receive, offer, [](QString mimeType, int fd) {
69 QFile file;
70 QVERIFY(file.open(fd, QIODevice::WriteOnly, QFile::FileHandleFlag::AutoCloseHandle));
71 QCOMPARE(mimeType, "text/plain");
72 file.write(QByteArray("normal ascii"));
73 file.close();
74 }, Qt::DirectConnection);
75 dataDevice()->sendSelection(offer);
76
77 auto *surface = xdgSurface()->m_surface;
78 keyboard()->sendEnter(surface); // Need to set keyboard focus according to protocol
79
80 pointer()->sendEnter(surface, {32, 32});
81 pointer()->sendFrame(client);
82 pointer()->sendButton(client, BTN_LEFT, 1);
83 pointer()->sendFrame(client);
84 pointer()->sendButton(client, BTN_LEFT, 0);
85 pointer()->sendFrame(client);
86 });
87 QTRY_COMPARE(window.m_text, "normal ascii");
88 }
89
90 void tst_datadevicev1::pasteUtf8()
91 {
92 class Window : public QRasterWindow {
93 public:
94 void mousePressEvent(QMouseEvent *) override { m_text = QGuiApplication::clipboard()->text(); }
95 QString m_text;
96 };
97
98 Window window;
99 window.resize(64, 64);
100 window.show();
101
102 QCOMPOSITOR_TRY_VERIFY(xdgSurface() && xdgSurface()->m_committedConfigureSerial);
103 exec([&] {
104 auto *client = xdgSurface()->resource()->client();
105 auto *offer = dataDevice()->sendDataOffer(client, {"text/plain", "text/plain;charset=utf-8"});
106 connect(offer, &DataOffer::receive, offer, [](QString mimeType, int fd) {
107 QFile file;
108 QVERIFY(file.open(fd, QIODevice::WriteOnly, QFile::FileHandleFlag::AutoCloseHandle));
109 QCOMPARE(mimeType, "text/plain;charset=utf-8");
110 file.write(QByteArray("face with tears of joy: 😂"));
111 file.close();
112 }, Qt::DirectConnection);
113 dataDevice()->sendSelection(offer);
114
115 auto *surface = xdgSurface()->m_surface;
116 keyboard()->sendEnter(surface); // Need to set keyboard focus according to protocol
117
118 pointer()->sendEnter(surface, {32, 32});
119 pointer()->sendFrame(client);
120 pointer()->sendButton(client, BTN_LEFT, 1);
121 pointer()->sendFrame(client);
122 pointer()->sendButton(client, BTN_LEFT, 0);
123 pointer()->sendFrame(client);
124 });
125 QTRY_COMPARE(window.m_text, "face with tears of joy: 😂");
126 }
127
128 void tst_datadevicev1::pasteMozUrl()
129 {
130 class Window : public QRasterWindow {
131 public:
132 void mousePressEvent(QMouseEvent *) override { m_urls = QGuiApplication::clipboard()->mimeData()->urls(); }
133 QList<QUrl> m_urls;
134 };
135
136 Window window;
137 window.resize(64, 64);
138 window.show();
139
140 QCOMPOSITOR_TRY_VERIFY(xdgSurface() && xdgSurface()->m_committedConfigureSerial);
141 exec([&] {
142 auto *client = xdgSurface()->resource()->client();
143 auto *offer = dataDevice()->sendDataOffer(client, {"text/x-moz-url"});
144 connect(offer, &DataOffer::receive, offer, [](QString mimeType, int fd) {
145 QFile file;
146 QVERIFY(file.open(fd, QIODevice::WriteOnly, QFile::FileHandleFlag::AutoCloseHandle));
147 QCOMPARE(mimeType, "text/x-moz-url");
148 const QString content("https://www.qt.io/\nQt\nhttps://www.example.com/\nExample Website");
149 // Need UTF-16.
150 file.write(reinterpret_cast<const char *>(content.data()), content.size() * 2);
151 file.close();
152 }, Qt::DirectConnection);
153 dataDevice()->sendSelection(offer);
154
155 auto *surface = xdgSurface()->m_surface;
156 keyboard()->sendEnter(surface); // Need to set keyboard focus according to protocol
157
158 pointer()->sendEnter(surface, {32, 32});
159 pointer()->sendFrame(client);
160 pointer()->sendButton(client, BTN_LEFT, 1);
161 pointer()->sendFrame(client);
162 pointer()->sendButton(client, BTN_LEFT, 0);
163 pointer()->sendFrame(client);
164 });
165
166 QTRY_COMPARE(window.m_urls.count(), 2);
167 QCOMPARE(window.m_urls.at(0), QUrl("https://www.qt.io/"));
168 QCOMPARE(window.m_urls.at(1), QUrl("https://www.example.com/"));
169 }
170
171 void tst_datadevicev1::pasteSingleUtf8MozUrl()
172 {
173 class Window : public QRasterWindow {
174 public:
175 void mousePressEvent(QMouseEvent *) override { m_urls = QGuiApplication::clipboard()->mimeData()->urls(); }
176 QList<QUrl> m_urls;
177 };
178
179 Window window;
180 window.resize(64, 64);
181 window.show();
182
183 QCOMPOSITOR_TRY_VERIFY(xdgSurface() && xdgSurface()->m_committedConfigureSerial);
184 exec([&] {
185 auto *client = xdgSurface()->resource()->client();
186 auto *offer = dataDevice()->sendDataOffer(client, {"text/x-moz-url"});
187 connect(offer, &DataOffer::receive, offer, [](QString mimeType, int fd) {
188 QFile file;
189 QVERIFY(file.open(fd, QIODevice::WriteOnly, QFile::FileHandleFlag::AutoCloseHandle));
190 QCOMPARE(mimeType, "text/x-moz-url");
191 const QString content("https://www.qt.io/");
192 file.write(content.toUtf8());
193 file.close();
194 }, Qt::DirectConnection);
195 dataDevice()->sendSelection(offer);
196
197 auto *surface = xdgSurface()->m_surface;
198 keyboard()->sendEnter(surface); // Need to set keyboard focus according to protocol
199
200 pointer()->sendEnter(surface, {32, 32});
201 pointer()->sendFrame(client);
202 pointer()->sendButton(client, BTN_LEFT, 1);
203 pointer()->sendFrame(client);
204 pointer()->sendButton(client, BTN_LEFT, 0);
205 pointer()->sendFrame(client);
206 });
207
208 QTRY_COMPARE(window.m_urls.count(), 1);
209 QCOMPARE(window.m_urls.at(0), QUrl("https://www.qt.io/"));
210 }
211
212 void tst_datadevicev1::destroysPreviousSelection()
213 {
214 QRasterWindow window;
215 window.resize(64, 64);
216 window.show();
217 QCOMPOSITOR_TRY_VERIFY(xdgSurface() && xdgSurface()->m_committedConfigureSerial);
218
219 // When the client receives a selection event, it is required to destroy the previous offer
220 exec([&] {
221 QCOMPARE(dataDevice()->m_sentSelectionOffers.size(), 0);
222 auto *offer = dataDevice()->sendDataOffer(client(), {"text/plain"});
223 dataDevice()->sendSelection(offer);
224 auto *surface = xdgSurface()->m_surface;
225 keyboard()->sendEnter(surface); // Need to set keyboard focus according to protocol
226 QCOMPARE(dataDevice()->m_sentSelectionOffers.size(), 1);
227 });
228
229 exec([&] {
230 auto *offer = dataDevice()->sendDataOffer(client(), {"text/plain"});
231 dataDevice()->sendSelection(offer);
232 QCOMPARE(dataDevice()->m_sentSelectionOffers.size(), 2);
233 });
234
235 // Verify the first offer gets destroyed
236 QCOMPOSITOR_TRY_COMPARE(dataDevice()->m_sentSelectionOffers.size(), 1);
237
238 exec([&] {
239 auto *offer = dataDevice()->sendDataOffer(client(), {"text/plain"});
240 dataDevice()->sendSelection(offer);
241 auto *surface = xdgSurface()->m_surface;
242 keyboard()->sendLeave(surface);
243 });
244
245 // Clients are required to destroy their offer when losing keyboard focus
246 QCOMPOSITOR_TRY_COMPARE(dataDevice()->m_sentSelectionOffers.size(), 0);
247 }
248
249 void tst_datadevicev1::destroysSelectionWithSurface()
250 {
251 auto *window = new QRasterWindow;
252 window->resize(64, 64);
253 window->show();
254
255 QCOMPOSITOR_TRY_VERIFY(xdgSurface() && xdgSurface()->m_committedConfigureSerial);
256
257 // When the client receives a selection event, it is required to destroy the previous offer
258 exec([&] {
259 QCOMPARE(dataDevice()->m_sentSelectionOffers.size(), 0);
260 auto *offer = dataDevice()->sendDataOffer(client(), {"text/plain"});
261 dataDevice()->sendSelection(offer);
262 auto *surface = xdgSurface()->m_surface;
263 keyboard()->sendEnter(surface); // Need to set keyboard focus according to protocol
264 QCOMPARE(dataDevice()->m_sentSelectionOffers.size(), 1);
265 });
266
267 // Ping to make sure we receive the wl_keyboard enter and leave events, before destroying the
268 // surface. Otherwise, the client will receive enter and leave events with a destroyed (null)
269 // surface, which is not what we are trying to test for here.
270 xdgPingAndWaitForPong();
271 window->destroy();
272
273 QCOMPOSITOR_TRY_COMPARE(dataDevice()->m_sentSelectionOffers.size(), 0);
274 }
275
276 void tst_datadevicev1::destroysSelectionOnLeave()
277 {
278 QRasterWindow window;
279 window.resize(64, 64);
280 window.show();
281 QCOMPOSITOR_TRY_VERIFY(xdgSurface() && xdgSurface()->m_committedConfigureSerial);
282
283 exec([&] {
284 auto *offer = dataDevice()->sendDataOffer(client(), {"text/plain"});
285 dataDevice()->sendSelection(offer);
286
287 auto *surface = xdgSurface()->m_surface;
288 keyboard()->sendEnter(surface); // Need to set keyboard focus according to protocol
289 });
290
291 QTRY_VERIFY(QGuiApplication::clipboard()->mimeData(QClipboard::Clipboard));
292 QTRY_VERIFY(QGuiApplication::clipboard()->mimeData(QClipboard::Clipboard)->hasText());
293
294 QSignalSpy dataChangedSpy(QGuiApplication::clipboard(), &QClipboard::dataChanged);
295
296 exec([&] {
297 auto *surface = xdgSurface()->m_surface;
298 keyboard()->sendLeave(surface);
299 });
300
301 QTRY_COMPARE(dataChangedSpy.size(), 1);
302 QVERIFY(!QGuiApplication::clipboard()->mimeData(QClipboard::Clipboard)->hasText());
303 }
304
305 // The application should not crash if it attempts to start a drag operation
306 // when it doesn't have input focus (QTBUG-76368)
307 void tst_datadevicev1::dragWithoutFocus()
308 {
309 QRasterWindow window;
310 window.resize(64, 64);
311 window.show();
312 QCOMPOSITOR_TRY_VERIFY(xdgSurface() && xdgSurface()->m_committedConfigureSerial);
313
314 auto *mimeData = new QMimeData;
315 const QByteArray data("testData");
316 mimeData->setData("text/plain", data);
317 QDrag drag(&window);
318 drag.setMimeData(mimeData);
319 drag.exec();
320 }
321
322 QCOMPOSITOR_TEST_MAIN(tst_datadevicev1)
323 #include "tst_datadevicev1.moc"