summaryrefslogtreecommitdiffstats
path: root/tests/auto/client/datadevicev1/tst_datadevicev1.cpp
blob: d4dfa2da046ff39e9723ee5490402776607343ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// Copyright (C) 2018 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "mockcompositor.h"

#include <QtOpenGL/QOpenGLWindow>
#include <QtGui/QRasterWindow>
#include <QtGui/QClipboard>
#include <QtGui/QDrag>

using namespace MockCompositor;

constexpr int dataDeviceVersion = 3;

class DataDeviceCompositor : public DefaultCompositor {
public:
    explicit DataDeviceCompositor()
    {
        exec([this] {
            m_config.autoConfigure = true;
            add<DataDeviceManager>(dataDeviceVersion);
        });
    }
    DataDevice *dataDevice() { return get<DataDeviceManager>()->deviceFor(get<Seat>()); }
};

class tst_datadevicev1 : public QObject, private DataDeviceCompositor
{
    Q_OBJECT
private slots:
    void cleanup() { QTRY_VERIFY2(isClean(), qPrintable(dirtyMessage())); }
    void initTestCase();
    void pasteAscii();
    void pasteUtf8();
    void destroysPreviousSelection();
    void destroysSelectionWithSurface();
    void destroysSelectionOnLeave();
    void dragWithoutFocus();
};

void tst_datadevicev1::initTestCase()
{
    QCOMPOSITOR_TRY_VERIFY(pointer());
    QCOMPOSITOR_TRY_VERIFY(keyboard());

    QCOMPOSITOR_TRY_VERIFY(dataDevice());
    QCOMPOSITOR_TRY_VERIFY(dataDevice()->resourceMap().contains(client()));
    QCOMPOSITOR_TRY_COMPARE(dataDevice()->resourceMap().value(client())->version(), dataDeviceVersion);
}

void tst_datadevicev1::pasteAscii()
{
    class Window : public QRasterWindow {
    public:
        void mousePressEvent(QMouseEvent *) override { m_text = QGuiApplication::clipboard()->text(); }
        QString m_text;
    };

    Window window;
    window.resize(64, 64);
    window.show();

    QCOMPOSITOR_TRY_VERIFY(xdgSurface() && xdgSurface()->m_committedConfigureSerial);
    exec([&] {
        auto *client = xdgSurface()->resource()->client();
        auto *offer = dataDevice()->sendDataOffer(client, {"text/plain"});
        connect(offer, &DataOffer::receive, [](QString mimeType, int fd) {
            QFile file;
            file.open(fd, QIODevice::WriteOnly, QFile::FileHandleFlag::AutoCloseHandle);
            QCOMPARE(mimeType, "text/plain");
            file.write(QByteArray("normal ascii"));
            file.close();
        });
        dataDevice()->sendSelection(offer);

        auto *surface = xdgSurface()->m_surface;
        keyboard()->sendEnter(surface); // Need to set keyboard focus according to protocol

        pointer()->sendEnter(surface, {32, 32});
        pointer()->sendFrame(client);
        pointer()->sendButton(client, BTN_LEFT, 1);
        pointer()->sendFrame(client);
        pointer()->sendButton(client, BTN_LEFT, 0);
        pointer()->sendFrame(client);
    });
    QTRY_COMPARE(window.m_text, "normal ascii");
}

void tst_datadevicev1::pasteUtf8()
{
    class Window : public QRasterWindow {
    public:
        void mousePressEvent(QMouseEvent *) override { m_text = QGuiApplication::clipboard()->text(); }
        QString m_text;
    };

    Window window;
    window.resize(64, 64);
    window.show();

    QCOMPOSITOR_TRY_VERIFY(xdgSurface() && xdgSurface()->m_committedConfigureSerial);
    exec([&] {
        auto *client = xdgSurface()->resource()->client();
        auto *offer = dataDevice()->sendDataOffer(client, {"text/plain", "text/plain;charset=utf-8"});
        connect(offer, &DataOffer::receive, [](QString mimeType, int fd) {
            QFile file;
            file.open(fd, QIODevice::WriteOnly, QFile::FileHandleFlag::AutoCloseHandle);
            QCOMPARE(mimeType, "text/plain;charset=utf-8");
            file.write(QByteArray("face with tears of joy: 😂"));
            file.close();
        });
        dataDevice()->sendSelection(offer);

        auto *surface = xdgSurface()->m_surface;
        keyboard()->sendEnter(surface); // Need to set keyboard focus according to protocol

        pointer()->sendEnter(surface, {32, 32});
        pointer()->sendFrame(client);
        pointer()->sendButton(client, BTN_LEFT, 1);
        pointer()->sendFrame(client);
        pointer()->sendButton(client, BTN_LEFT, 0);
        pointer()->sendFrame(client);
    });
    QTRY_COMPARE(window.m_text, "face with tears of joy: 😂");
}

void tst_datadevicev1::destroysPreviousSelection()
{
    QRasterWindow window;
    window.resize(64, 64);
    window.show();
    QCOMPOSITOR_TRY_VERIFY(xdgSurface() && xdgSurface()->m_committedConfigureSerial);

    // When the client receives a selection event, it is required to destroy the previous offer
    exec([&] {
        QCOMPARE(dataDevice()->m_sentSelectionOffers.size(), 0);
        auto *offer = dataDevice()->sendDataOffer(client(), {"text/plain"});
        dataDevice()->sendSelection(offer);
        auto *surface = xdgSurface()->m_surface;
        keyboard()->sendEnter(surface); // Need to set keyboard focus according to protocol
        QCOMPARE(dataDevice()->m_sentSelectionOffers.size(), 1);
    });

    exec([&] {
        auto *offer = dataDevice()->sendDataOffer(client(), {"text/plain"});
        dataDevice()->sendSelection(offer);
        QCOMPARE(dataDevice()->m_sentSelectionOffers.size(), 2);
    });

    // Verify the first offer gets destroyed
    QCOMPOSITOR_TRY_COMPARE(dataDevice()->m_sentSelectionOffers.size(), 1);

    exec([&] {
        auto *offer = dataDevice()->sendDataOffer(client(), {"text/plain"});
        dataDevice()->sendSelection(offer);
        auto *surface = xdgSurface()->m_surface;
        keyboard()->sendLeave(surface);
    });

    // Clients are required to destroy their offer when losing keyboard focus
    QCOMPOSITOR_TRY_COMPARE(dataDevice()->m_sentSelectionOffers.size(), 0);
}

void tst_datadevicev1::destroysSelectionWithSurface()
{
    auto *window = new QRasterWindow;
    window->resize(64, 64);
    window->show();

    QCOMPOSITOR_TRY_VERIFY(xdgSurface() && xdgSurface()->m_committedConfigureSerial);

    // When the client receives a selection event, it is required to destroy the previous offer
    exec([&] {
        QCOMPARE(dataDevice()->m_sentSelectionOffers.size(), 0);
        auto *offer = dataDevice()->sendDataOffer(client(), {"text/plain"});
        dataDevice()->sendSelection(offer);
        auto *surface = xdgSurface()->m_surface;
        keyboard()->sendEnter(surface); // Need to set keyboard focus according to protocol
        QCOMPARE(dataDevice()->m_sentSelectionOffers.size(), 1);
    });

    // Ping to make sure we receive the wl_keyboard enter and leave events, before destroying the
    // surface. Otherwise, the client will receive enter and leave events with a destroyed (null)
    // surface, which is not what we are trying to test for here.
    xdgPingAndWaitForPong();
    window->destroy();

    QCOMPOSITOR_TRY_COMPARE(dataDevice()->m_sentSelectionOffers.size(), 0);
}

void tst_datadevicev1::destroysSelectionOnLeave()
{
    QRasterWindow window;
    window.resize(64, 64);
    window.show();
    QCOMPOSITOR_TRY_VERIFY(xdgSurface() && xdgSurface()->m_committedConfigureSerial);

    exec([&] {
        auto *offer = dataDevice()->sendDataOffer(client(), {"text/plain"});
        dataDevice()->sendSelection(offer);

        auto *surface = xdgSurface()->m_surface;
        keyboard()->sendEnter(surface); // Need to set keyboard focus according to protocol
    });

    QTRY_VERIFY(QGuiApplication::clipboard()->mimeData(QClipboard::Clipboard));
    QTRY_VERIFY(QGuiApplication::clipboard()->mimeData(QClipboard::Clipboard)->hasText());

    QSignalSpy dataChangedSpy(QGuiApplication::clipboard(), &QClipboard::dataChanged);

    exec([&] {
        auto *surface = xdgSurface()->m_surface;
        keyboard()->sendLeave(surface);
    });

    QTRY_COMPARE(dataChangedSpy.count(), 1);
    QVERIFY(!QGuiApplication::clipboard()->mimeData(QClipboard::Clipboard)->hasText());
}

// The application should not crash if it attempts to start a drag operation
// when it doesn't have input focus (QTBUG-76368)
void tst_datadevicev1::dragWithoutFocus()
{
    QRasterWindow window;
    window.resize(64, 64);
    window.show();
    QCOMPOSITOR_TRY_VERIFY(xdgSurface() && xdgSurface()->m_committedConfigureSerial);

    auto *mimeData = new QMimeData;
    const QByteArray data("testData");
    mimeData->setData("text/plain", data);
    QDrag drag(&window);
    drag.setMimeData(mimeData);
    drag.exec();
}

QCOMPOSITOR_TEST_MAIN(tst_datadevicev1)
#include "tst_datadevicev1.moc"