summaryrefslogtreecommitdiff
path: root/tests/auto/client/reconnect/tst_reconnect.cpp
blob: 2b6c7558fd66c80c73882c49b0367fd288589a26 (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
// Copyright (C) 2023 David Edmundson <davidedmundson@kde.org>
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "mockcompositor.h"

#include <QBackingStore>
#include <QPainter>
#include <QScreen>
#include <QWindow>
#include <QMimeData>
#include <QPixmap>
#include <QDrag>
#include <QWindow>
#if QT_CONFIG(opengl)
#include <QOpenGLWindow>
#endif
#include <QRasterWindow>

#include <QtTest/QtTest>
#include <QtWaylandClient/private/qwaylandintegration_p.h>
#include <QtGui/private/qguiapplication_p.h>

#include "wl-socket.h"

using namespace MockCompositor;

class TestWindow : public QRasterWindow
{
public:
    TestWindow()
    {
    }

    void focusInEvent(QFocusEvent *) override
    {
        ++focusInEventCount;
    }

    void focusOutEvent(QFocusEvent *) override
    {
        ++focusOutEventCount;
    }

    void keyPressEvent(QKeyEvent *event) override
    {
        ++keyPressEventCount;
        keyCode = event->nativeScanCode();
    }

    void keyReleaseEvent(QKeyEvent *event) override
    {
        ++keyReleaseEventCount;
        keyCode = event->nativeScanCode();
    }

    void mousePressEvent(QMouseEvent *event) override
    {
        ++mousePressEventCount;
        mousePressPos = event->position().toPoint();
    }

    void mouseReleaseEvent(QMouseEvent *) override
    {
        ++mouseReleaseEventCount;
    }

    void touchEvent(QTouchEvent *event) override
    {
        Q_UNUSED(event);
        ++touchEventCount;
    }

    QPoint frameOffset() const { return QPoint(frameMargins().left(), frameMargins().top()); }

    int focusInEventCount = 0;
    int focusOutEventCount = 0;
    int keyPressEventCount = 0;
    int keyReleaseEventCount = 0;
    int mousePressEventCount = 0;
    int mouseReleaseEventCount = 0;
    int touchEventCount = 0;

    uint keyCode = 0;
    QPoint mousePressPos;
};

class tst_WaylandReconnect : public QObject
{
    Q_OBJECT
public:
    tst_WaylandReconnect();
    void triggerReconnect();

    template<typename function_type, typename... arg_types>
    auto exec(function_type func, arg_types&&... args) -> decltype(func())
    {
        return m_comp->exec(func, std::forward<arg_types>(args)...);
    }

private Q_SLOTS:
//core
    void cleanup() { QTRY_VERIFY2(m_comp->isClean(), qPrintable(m_comp->dirtyMessage())); }
    void basicWindow();
    void screens();

//input
    void keyFocus();

private:
    void configureWindow();
    QScopedPointer<DefaultCompositor> m_comp;
    wl_socket *m_socket;
};

tst_WaylandReconnect::tst_WaylandReconnect()
{
    m_socket = wl_socket_create();
    QVERIFY(m_socket);
    const int socketFd = wl_socket_get_fd(m_socket);
    const QByteArray socketName = wl_socket_get_display_name(m_socket);
    qputenv("WAYLAND_DISPLAY", socketName);

    m_comp.reset(new DefaultCompositor(CoreCompositor::Default, dup(socketFd)));
}

void tst_WaylandReconnect::triggerReconnect()
{
    const int socketFd = wl_socket_get_fd(m_socket);
    m_comp.reset(new DefaultCompositor(CoreCompositor::Default, dup(socketFd)));
    QTest::qWait(50); //we need to spin the main loop to actually reconnect
}

void tst_WaylandReconnect::basicWindow()
{
    QRasterWindow window;
    window.resize(64, 48);
    window.show();
    QCOMPOSITOR_TRY_VERIFY(m_comp->xdgToplevel());

    triggerReconnect();

    QCOMPOSITOR_TRY_VERIFY(m_comp->xdgToplevel());
}

void tst_WaylandReconnect::screens()
{
    QRasterWindow window;
    window.resize(64, 48);
    window.show();
    QCOMPOSITOR_TRY_VERIFY(m_comp->xdgToplevel());

    auto originalScreens = QGuiApplication::screens();

    QSignalSpy screenRemovedSpy(qGuiApp, &QGuiApplication::screenRemoved);
    QVERIFY(screenRemovedSpy.isValid());

    triggerReconnect();

    // All screens plus temporary placeholder screen removed
    QCOMPARE(screenRemovedSpy.count(), originalScreens.count() + 1);
    for (const auto &screen : std::as_const(screenRemovedSpy)) {
        originalScreens.removeOne(screen[0].value<QScreen *>());
    }
    QVERIFY(originalScreens.isEmpty());
    QVERIFY(window.screen());
    QVERIFY(QGuiApplication::screens().contains(window.screen()));
}

void tst_WaylandReconnect::keyFocus()
{
    TestWindow window;
    window.resize(64, 48);
    window.show();

    configureWindow();
    QTRY_VERIFY(window.isExposed());
    exec([=] {
        m_comp->keyboard()->sendEnter(m_comp->surface());
    });
    QTRY_COMPARE(window.focusInEventCount, 1);

    uint keyCode = 80;
    QCOMPARE(window.keyPressEventCount, 0);
    exec([=] {
        m_comp->keyboard()->sendKey(m_comp->client(), keyCode - 8, Keyboard::key_state_pressed);
    });
    QTRY_COMPARE(window.keyPressEventCount, 1);
    QCOMPARE(QGuiApplication::focusWindow(), &window);

    triggerReconnect();
    configureWindow();

    // on reconnect our knowledge of focus is reset to a clean slate
    QCOMPARE(QGuiApplication::focusWindow(), nullptr);
    QTRY_COMPARE(window.focusOutEventCount, 1);

    // fake the user explicitly focussing this window afterwards
    exec([=] {
        m_comp->keyboard()->sendEnter(m_comp->surface());
    });
    exec([=] {
        m_comp->keyboard()->sendKey(m_comp->client(), keyCode - 8, Keyboard::key_state_pressed);
    });
    QTRY_COMPARE(window.focusInEventCount, 2);
    QTRY_COMPARE(window.keyPressEventCount, 2);
}


void tst_WaylandReconnect::configureWindow()
{
    QCOMPOSITOR_TRY_VERIFY(m_comp->xdgToplevel());
    m_comp->exec([=] {
        m_comp->xdgToplevel()->sendConfigure({0, 0}, {});
        const uint serial = m_comp->nextSerial(); // Let the window decide the size
        m_comp->xdgSurface()->sendConfigure(serial);
    });
}

int main(int argc, char **argv)
{
    // Note when debugging that a failing reconnect will exit this
    // test rather than fail. Making sure it finishes is important!

    QTemporaryDir tmpRuntimeDir;
    setenv("QT_QPA_PLATFORM", "wayland", 1); // force QGuiApplication to use wayland plugin
    setenv("QT_WAYLAND_RECONNECT", "1", 1);
    setenv("XDG_CURRENT_DESKTOP", "qtwaylandtests", 1);

    tst_WaylandReconnect tc;
    QGuiApplication app(argc, argv);
    QTEST_SET_MAIN_SOURCE_PATH
    return QTest::qExec(&tc, argc, argv);
}

#include "tst_reconnect.moc"