File: tst_native_qpa.cpp

package info (click to toggle)
kddockwidgets 2.4.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,412 kB
  • sloc: cpp: 50,019; ansic: 765; python: 239; xml: 61; makefile: 14; sh: 7
file content (302 lines) | stat: -rw-r--r-- 9,185 bytes parent folder | download
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
  This file is part of KDDockWidgets.

  SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
  Author: Sérgio Martins <sergio.martins@kdab.com>

  SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only

  Contact KDAB at <info@kdab.com> for commercial licensing options.
*/
#define NOMINMAX

#include "Config.h"
#include "utils.h"
#include "qtcommon/Window_p.h"
#include "core/MainWindow.h"
#include "core/DockWidget.h"
#include "core/Platform.h"

#include <QTimer>
#include <QtTest/QTest>


#ifdef KDDW_HAS_SPDLOG
#include "fatal_logger.h"
#endif

using namespace KDDockWidgets;
using namespace KDDockWidgets::Core;
using namespace KDDockWidgets::Tests;

/// Tests that should run in the native QPAs (windows, cocoa, xcb) instead of offscreen
/// To test functionality that usually is window manager dependent

class TestNativeQPA : public QObject
{
    Q_OBJECT
public Q_SLOTS:
    void initTestCase();
    void cleanupTestCase();

private Q_SLOTS:
    void tst_restoreNormalFromMaximized();
    void tst_restoreMaximizedFromNormal();
    void tst_restoreMaximizedFromMaximized();
};

void TestNativeQPA::initTestCase()
{
    KDDockWidgets::Core::Platform::instance()->installMessageHandler();
}

void TestNativeQPA::cleanupTestCase()
{
    KDDockWidgets::Core::Platform::instance()->uninstallMessageHandler();
}

namespace {
// QWindow::windowStateChange() is not reliable, since we're only interested
// in the spontaneous events (async), as those reflect the window manager state
class MyEventFilter : public QObject
{
    Q_OBJECT
public:
    bool eventFilter(QObject *obj, QEvent *event) override
    {
        if (event->type() == QEvent::WindowStateChange) {
            if (event->spontaneous()) {
                QWindow *window = static_cast<QWindow *>(obj);
                qDebug() << "WindowStateChange:" << int(window->windowState());
                m_lastState = window->windowState();
                Q_EMIT stateChanged(window->windowState());
            }
        } else if (event->type() == QEvent::Resize) {
            auto rev = static_cast<QResizeEvent *>(event);
            qDebug() << "Resize event. old=" << rev->oldSize() << "; new=" << rev->size();
        } else if (event->type() == QEvent::Move) {
            auto mev = static_cast<QMoveEvent *>(event);
            qDebug() << "Move event. old=" << mev->oldPos() << "; new=" << mev->pos();
        }

        return false;
    }

    bool waitForState(Qt::WindowState state)
    {
        if (m_lastState == state)
            return true;

        bool result = false;
        QEventLoop loop;
        QTimer::singleShot(5000, &loop, [&loop] { loop.quit(); });
        connect(this, &MyEventFilter::stateChanged, &loop, [&](auto s) {
            if (state == s) {
                result = true;
                loop.quit();
            }
        });

        loop.exec();
        return result;
    }

Q_SIGNALS:
    void stateChanged(Qt::WindowState);

public:
    int m_lastState = -1;
};
}

void TestNativeQPA::tst_restoreNormalFromMaximized()
{
#ifdef Q_OS_MACOS
    if (Platform::instance()->isQtQuick())
        return;
#endif

    // Saves the window state while in normal state, then restores after the window is maximized
    // the window should become unmaximized.

    auto m = createMainWindow(Size(500, 500), MainWindowOption_None, "m1", false);

    auto windowptr = m->view()->window();
    auto window = static_cast<QtCommon::Window *>(windowptr.get());
    QWindow *qtwindow = window->qtWindow();
    MyEventFilter filter;
    qtwindow->installEventFilter(&filter);

    m->show();
    QVERIFY(m->isVisible());

    LayoutSaver saver;
    const QByteArray saved = saver.serializeLayout();

    m->view()->showMaximized();
    QVERIFY(filter.waitForState(Qt::WindowMaximized));
    QVERIFY(saver.restoreLayout(saved));
    QVERIFY(filter.waitForState(Qt::WindowNoState));
}

void TestNativeQPA::tst_restoreMaximizedFromNormal()
{
    if (qGuiApp->platformName() == QLatin1String("offscreen")) {
        // offscreen: calling showMaximized() on an hidden widget, puts it at pos=2,2 instead of 0,0
        // Ignore this QPA. This file is for testing native QPAs only. offscreen is nice to have
        // if it beahaves well only.
        return;
    }

#ifdef Q_OS_MACOS
    if (Platform::instance()->isQtQuick())
        return;
#endif

    // whitelist some macOS warning
    SetExpectedWarning warn("invalid window content view size");

    // Saves the window state while in maximized state, then restores after the window is shown normal
    // the window should become maximized again.
    // qDebug() << qGuiApp->primaryScreen()->geometry();
    const QSize initialSize(500, 500);
    auto m = createMainWindow(initialSize, MainWindowOption_None, "m1", false);

    auto windowptr = m->view()->window();
    auto window = static_cast<QtCommon::Window *>(windowptr.get());
    QWindow *qtwindow = window->qtWindow();
    MyEventFilter filter;
    qtwindow->installEventFilter(&filter);

    m->view()->showMaximized();
    QVERIFY(filter.waitForState(Qt::WindowMaximized));

    int count = 0;
    // Qt annoyingly sends us 2 or 3 resize events before the fully maximized one, even when
    // already having state==Qt::WindowMaximized. Probably depends on platform.
    // Consume all resize events until window gets big.
    while (m->geometry().size().width() < 700) {
        QVERIFY(Platform::instance()->tests_waitForResize(m->view()));
        count++;
        QVERIFY(count < 5);
    }

    const auto expectedMaximizedGeometry = m->geometry();
    QVERIFY(initialSize != expectedMaximizedGeometry.size());

    LayoutSaver saver;
    const QByteArray saved = saver.serializeLayout();

    m->view()->showNormal();
    QVERIFY(filter.waitForState(Qt::WindowNoState));

    QVERIFY(saver.restoreLayout(saved));
    QVERIFY(filter.waitForState(Qt::WindowMaximized));
    QVERIFY(m->isVisible());

    /// Catch more resizes:
    QTest::qWait(1000);

    QCOMPARE(m->geometry(), expectedMaximizedGeometry);
}

void TestNativeQPA::tst_restoreMaximizedFromMaximized()
{
    if (qGuiApp->platformName() == QLatin1String("offscreen")) {
        // offscreen: calling showMaximized() on an hidden widget, puts it at pos=2,2 instead of 0,0
        // Ignore this QPA. This file is for testing native QPAs only. offscreen is nice to have
        // if it beahaves well only.
        return;
    }

#ifdef Q_OS_MACOS
    if (Platform::instance()->isQtQuick())
        return;
#endif

    // whitelist some macOS warning
    SetExpectedWarning warn("invalid window content view size");

    // Saves the window state while in maximized state, then restores after the window is shown normal
    // the window should become maximized again.
    // qDebug() << qGuiApp->primaryScreen()->geometry();
    const QSize initialSize(500, 500);
    auto m = createMainWindow(initialSize, MainWindowOption_None, "m1", false);

    auto windowptr = m->view()->window();
    auto window = static_cast<QtCommon::Window *>(windowptr.get());
    QWindow *qtwindow = window->qtWindow();
    MyEventFilter filter;
    qtwindow->installEventFilter(&filter);

    m->view()->showMaximized();
    QVERIFY(filter.waitForState(Qt::WindowMaximized));

    int count = 0;
    // Qt annoyingly sends us 2 or 3 resize events before the fully maximized one, even when
    // already having state==Qt::WindowMaximized. Probably depends on platform.
    // Consume all resize events until window gets big.
    while (m->geometry().size().width() < 700) {
        QVERIFY(Platform::instance()->tests_waitForResize(m->view()));
        count++;
        QVERIFY(count < 5);
    }

    const auto expectedMaximizedGeometry = m->geometry();
    QVERIFY(initialSize != expectedMaximizedGeometry.size());

    LayoutSaver saver;
    const QByteArray saved = saver.serializeLayout();

    QTest::qWait(1000);

    QVERIFY(saver.restoreLayout(saved));
    QVERIFY(filter.waitForState(Qt::WindowMaximized));

    // Catch more resizes:
    QTest::qWait(1000);

#if QT_VERSION > QT_VERSION_CHECK(6, 0, 0)
#ifdef Q_OS_LINUX
    if (Platform::instance()->isQtWidgets()) {
        // buggy on Linux, Qt6, QtWidgets. The window is visually maximixed but geometry is wrong
        return;
    }
#endif
#endif



    QCOMPARE(m->geometry(), expectedMaximizedGeometry);
}

int main(int argc, char *argv[])
{
#ifdef Q_OS_LINUX
    if (!qEnvironmentVariableIsSet("DISPLAY")) {
        // Don't fail if we don't have X11. GitHub CI will use xvfb
        return 0;
    }
#endif

#ifdef KDDW_HAS_SPDLOG
    FatalLogger::create();
#endif

    int exitCode = 0;
    for (FrontendType type : Platform::frontendTypes()) {
        KDDockWidgets::Core::Platform::tests_initPlatform(argc, argv, type, /*defaultToOffscreenQPA=*/false);
        qDebug() << "\nTesting platform" << type << "on" << qGuiApp->platformName() << "\n";

        TestNativeQPA test;

        const int code = QTest::qExec(&test, argc, argv);
        if (code != 0)
            exitCode = 1;
        KDDockWidgets::Core::Platform::tests_deinitPlatform();
    }

    return exitCode;
}

#include <tst_native_qpa.moc>