summaryrefslogtreecommitdiffstats
path: root/tests/manual/wasm/qstdweb/qwasmcompositor_main.cpp
blob: e1a9cf604d6c5a66300395f46537005a47d25945 (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
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include <QtCore/QEvent>
#include <QtCore/QObject>
#include <QtGui/qwindow.h>
#include <QtGui/qguiapplication.h>
#include <QtGui/qoffscreensurface.h>
#include <QtGui/qpa/qwindowsysteminterface.h>
#include <QtGui/rhi/qrhi.h>

#include <qtwasmtestlib.h>

#include <emscripten.h>
#include <emscripten/val.h>

#include <functional>
#include <memory>
#include <vector>

namespace tst_qwasmcompositor_internal {
namespace {
class Window : public QWindow
{
    Q_OBJECT
public:
    Window();
    ~Window() override { qDebug() << "dtor Window"; }

    void keyPressEvent(QKeyEvent *) final;

signals:
    void exposed();
    void keyEventReceived();
    void initFailed();

protected:
private:
    void init();

    void exposeEvent(QExposeEvent *) override;
    bool m_exposedOnce = false;

    std::unique_ptr<QOffscreenSurface> m_fallbackSurface;
    std::unique_ptr<QRhi> m_rhi;
};

Window::Window()
{
    setSurfaceType(OpenGLSurface);
}

void Window::exposeEvent(QExposeEvent *)
{
    if (isExposed() && !m_exposedOnce) {
        m_exposedOnce = true;
        init();
        emit exposed();
    }
}

void Window::keyPressEvent(QKeyEvent *)
{
    emit keyEventReceived();
}

void Window::init()
{
    QRhi::Flags rhiFlags = QRhi::EnableDebugMarkers;

    m_fallbackSurface.reset(QRhiGles2InitParams::newFallbackSurface());
    QRhiGles2InitParams params;
    params.fallbackSurface = m_fallbackSurface.get();
    params.window = this;

    // Double init of RHI causes the OpenGL context to be destroyed, which causes a bug with input.
    m_rhi.reset(QRhi::create(QRhi::OpenGLES2, &params, rhiFlags));
    m_rhi.reset(QRhi::create(QRhi::OpenGLES2, &params, rhiFlags));

    if (!m_rhi)
        emit initFailed();
}

} // namespace
} // namespace tst_qwasmcompositor_internal

using namespace emscripten;

class QWasmCompositorTest : public QObject
{
    Q_OBJECT

public:
    QWasmCompositorTest() : m_window(val::global("window")), m_testSupport(val::object())
    {
        m_window.set("testSupport", m_testSupport);
        m_testSupport.set("qtSetContainerElements",
                          emscripten::val::module_property("qtSetContainerElements"));
    }

    ~QWasmCompositorTest() noexcept
    {
        qDebug() << this << "In dtor";
        for (auto it = m_cleanup.rbegin(); it != m_cleanup.rend(); ++it)
            (*it)();
        m_window.set("testSupport", emscripten::val::undefined());
    }

private:
    void init()
    {
        EM_ASM({
            testSupport.screenElement = document.createElement("div");
            testSupport.screenElement.id = "test-canvas-qwasmcompositor";
            document.body.appendChild(testSupport.screenElement);
        });
        m_cleanup.emplace_back([]() mutable {
            EM_ASM({
                testSupport.qtSetContainerElements([]);
                testSupport.screenElement.parentElement.removeChild(testSupport.screenElement);
            });
        });

        EM_ASM({ testSupport.qtSetContainerElements([testSupport.screenElement]); });
    }

    template<class T>
    T *Own(T *plainPtr)
    {
        m_cleanup.emplace_back([plainPtr]() mutable { delete plainPtr; });
        return plainPtr;
    }

    val m_window;
    val m_testSupport;

    std::vector<std::function<void()>> m_cleanup;

private slots:
    void testReceivingKeyboardEventsAfterOpenGLContextReset();
};

void QWasmCompositorTest::testReceivingKeyboardEventsAfterOpenGLContextReset()
{
    init();

    using Window = tst_qwasmcompositor_internal::Window;
    Window *window = Own(new Window);
    window->show();
    window->requestActivate();

    QWindowSystemInterface::flushWindowSystemEvents();

    QObject::connect(window, &Window::keyEventReceived, []() { QWASMSUCCESS(); });
    QObject::connect(window, &Window::initFailed,
                     []() { QWASMFAIL("Cannot initialize test window"); });
    QObject::connect(window, &Window::exposed, []() {
        EM_ASM({
            testSupport.screenElement.shadowRoot.querySelector('.qt-window')
                    .dispatchEvent(new KeyboardEvent('keydown', { key : 'a' }));
        });
    });
}

int main(int argc, char **argv)
{
    auto testObject = std::make_shared<QWasmCompositorTest>();
    QtWasmTest::initTestCase<QGuiApplication>(argc, argv, testObject);
    return 0;
}

#include "qwasmcompositor_main.moc"