summaryrefslogtreecommitdiffstats
path: root/tests/plugins/platforms/webgl/tst_webgl.cpp
blob: 306d6fddce6d06765827a80f931067b8d156763f (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
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
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt WebGL module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QtTest/qtest.h>
#include <QtTest/qsignalspy.h>

#include <QtCore/qcoreapplication.h>
#include <QtCore/qlibraryinfo.h>
#include <QtCore/qjsonarray.h>
#include <QtCore/qjsondocument.h>
#include <QtCore/qjsonobject.h>
#include <QtCore/qobject.h>
#include <QtCore/qprocess.h>
#include <QtCore/qregularexpression.h>
#include <QtGui/qguiapplication.h>
#include <QtGui/qopengl.h>
#include <QtNetwork/qnetworkaccessmanager.h>
#include <QtNetwork/qnetworkreply.h>
#include <QtWebSockets/qwebsocket.h>

#include "parameters.h"

#include <memory>

#define PORT 8080
#define PORTSTRING QT_STRINGIFY(PORT)

class tst_WebGL : public QObject
{
    Q_OBJECT

    QNetworkAccessManager manager;
    QWebSocket webSocket;
    QStringList functions;
    QProcess process;

signals:
    void command(const QString &name, const QVariantList &parameters);
    void queryCommand(const QString &name, int id, const QVariantList &parameters);

public slots:
    void parseTextMessage(const QString &text);
    void parseBinaryMessage(const QByteArray &data);

private slots:
    void initTestCase();

    void init();
    void cleanup();

    void checkFunctionCount_data();
    void checkFunctionCount();

    void waitForSwapBuffers_data();
    void waitForSwapBuffers();
};

void tst_WebGL::parseTextMessage(const QString &text)
{
    const auto document = QJsonDocument::fromJson(text.toUtf8());
    if (document["type"].toString() == "connect") {
        const auto supportedFunctions = document["supportedFunctions"].toArray();
        functions.clear();
        for (const auto &function : supportedFunctions)
            functions.append(function.toString());
    } else if (document["type"] == "create_canvas") {
        const QJsonDocument defaultValuesMessage {
            QJsonObject {
                { QLatin1String("type"), QLatin1String("default_context_parameters") },
                { QString::number(GL_EXTENSIONS),
                            QLatin1String("GL_OES_element_index_uint "
                                          "GL_OES_standard_derivatives "
                                          "GL_OES_depth_texture GL_OES_packed_depth_stencil") },
                { QString::number(GL_BLEND), false },
                { QString::number(GL_DEPTH_TEST), false },
                { QString::number(GL_MAX_TEXTURE_SIZE), 512 },
                { QString::number(GL_MAX_VERTEX_ATTRIBS), 16},
                { QString::number(GL_RENDERER), "Test WebGL"},
                { QString::number(GL_SCISSOR_TEST), false },
                { QString::number(GL_STENCIL_TEST), false },
                { QString::number(GL_UNPACK_ALIGNMENT), 4 },
                { QString::number(GL_VENDOR), "Qt" },
                { QString::number(GL_VIEWPORT), QJsonArray{ 0, 0, 640, 480 } },
                { QLatin1String("name"), document["winId"] }
            },
        };
        webSocket.sendTextMessage(defaultValuesMessage.toJson());
    }
}

void tst_WebGL::parseBinaryMessage(const QByteArray &data)
{
    const QSet<QString> commandsNeedingResponse {
        QLatin1String("swapBuffers"),
        QLatin1String("checkFramebufferStatus"),
        QLatin1String("createProgram"),
        QLatin1String("createShader"),
        QLatin1String("genBuffers"),
        QLatin1String("genFramebuffers"),
        QLatin1String("genRenderbuffers"),
        QLatin1String("genTextures"),
        QLatin1String("getAttachedShaders"),
        QLatin1String("getAttribLocation"),
        QLatin1String("getBooleanv"),
        QLatin1String("getError"),
        QLatin1String("getFramebufferAttachmentParameteriv"),
        QLatin1String("getIntegerv"),
        QLatin1String("getParameter"),
        QLatin1String("getProgramInfoLog"),
        QLatin1String("getProgramiv"),
        QLatin1String("getRenderbufferParameteriv"),
        QLatin1String("getShaderiv"),
        QLatin1String("getShaderPrecisionFormat"),
        QLatin1String("getString"),
        QLatin1String("getTexParameterfv"),
        QLatin1String("getTexParameteriv"),
        QLatin1String("getUniformfv"),
        QLatin1String("getUniformLocation"),
        QLatin1String("getUniformiv"),
        QLatin1String("getVertexAttribfv"),
        QLatin1String("getVertexAttribiv"),
        QLatin1String("getShaderSource"),
        QLatin1String("getShaderInfoLog"),
        QLatin1String("isRenderbuffer")
    };

    quint32 offset = 0;
    QString function;
    int id = -1;
    QDataStream stream(data);
    {
        quint8 functionIndex;
        stream >> functionIndex;
        offset += sizeof(functionIndex);
        function = functions[functionIndex];
        if (commandsNeedingResponse.contains(function)) {
            stream >> id;
            offset += sizeof(id);
        }
    }
    const auto parameters = Parameters::read(data, stream, offset);
    {
        quint32 magic = 0;
        stream >> magic;
        offset += sizeof(magic);
        QCOMPARE(magic, 0xbaadf00d);
    }
    QCOMPARE(int(offset), data.size());
    if (id == -1)
        emit command(function, parameters);
    else
        emit queryCommand(function, id, parameters);
}

void tst_WebGL::initTestCase()
{
    connect(&webSocket, &QWebSocket::binaryMessageReceived, this, &tst_WebGL::parseBinaryMessage);
    connect(&webSocket, &QWebSocket::textMessageReceived, this, &tst_WebGL::parseTextMessage);
}

void tst_WebGL::init()
{
    QFETCH(QString, scene);
    const auto tryToConnect = [=](quint16 port = PORT) {
        QTcpSocket socket;
        socket.connectToHost("localhost", port);
        QTRY_LOOP_IMPL(socket.state() == QTcpSocket::ConnectedState ||
                       socket.state() == QTcpSocket::UnconnectedState, 1000, 50);
        return socket.state() == QTcpSocket::ConnectedState;
    };

    QVERIFY2(!tryToConnect(), "An application is listening on port " PORTSTRING);

    QString executableName = QLatin1String("qmlscene");
#if defined(Q_OS_WIN)
    executableName += QString::fromLatin1(".exe");
#endif

    process.setProcessChannelMode(QProcess::MergedChannels);
    process.setProgram(QLibraryInfo::location(QLibraryInfo::BinariesPath) + QChar('/')
                       + executableName);
    process.setArguments(QStringList { QDir::toNativeSeparators(scene) });
    process.setEnvironment(QProcess::systemEnvironment()
                           << "QT_QPA_PLATFORM=webgl:port=" PORTSTRING
                           << "QT_LOGGING_RULES=qt.qpa.webgl.*=true");
    process.start();
    process.waitForStarted();
    QVERIFY(process.isOpen());
    connect(&process, &QProcess::readyReadStandardOutput, [=]() {
        while (process.bytesAvailable())
            qDebug() << process.pid() << process.readLine();
    });
    QTRY_VERIFY(tryToConnect());
    const QJsonDocument connectMessage {
        QJsonObject {
            { QLatin1String("type"), QLatin1String("connect") },
            { QLatin1String("width"), 1920 },
            { QLatin1String("height"), 1080 },
            { QLatin1String("physicalWidth"), 531.3 },
            { QLatin1String("physicalHeight"), 298.9 }
        }
    };

    auto reply = manager.get(QNetworkRequest(QUrl("http://localhost:" PORTSTRING "/webqt.js")));
    QSignalSpy replyFinishedSpy(reply, &QNetworkReply::finished);
    QTRY_VERIFY(!replyFinishedSpy.isEmpty());
    reply->readLine();
    const auto portString = reply->readLine().trimmed();
    QVERIFY(portString.size());
    const QRegularExpression rx("var port = (\\d+);");
    const auto match = rx.match(portString);
    QVERIFY(!match.captured(1).isEmpty());
    QVERIFY(match.captured(1).toUInt() <= std::numeric_limits<quint16>::max());
    const auto websocketPort = quint16(match.captured(1).toUInt());

    QTRY_VERIFY(tryToConnect(websocketPort));
    QSignalSpy connected(&webSocket, &QWebSocket::connected);
    webSocket.open(QUrl(QString::fromLatin1("ws://localhost:%1").arg(websocketPort)));
    QTRY_VERIFY(!connected.isEmpty());
    webSocket.sendTextMessage(connectMessage.toJson());
    QVERIFY(webSocket.state() == QAbstractSocket::ConnectedState);
}

void tst_WebGL::cleanup()
{
    webSocket.close();

    process.kill();
    process.waitForFinished();
}

void tst_WebGL::checkFunctionCount_data()
{
    QTest::addColumn<QString>("scene"); // Fetched in tst_WebGL::init
    QTest::newRow("Basic scene") << QFINDTESTDATA("basic_scene.qml");
}

void tst_WebGL::checkFunctionCount()
{
    QCOMPARE(functions.size(), 147);
}

void tst_WebGL::waitForSwapBuffers_data()
{
    QTest::addColumn<QString>("scene"); // Fetched in tst_WebGL::init
    QTest::newRow("Basic scene") << QFINDTESTDATA("basic_scene.qml");
}

void tst_WebGL::waitForSwapBuffers()
{
    QSignalSpy spy(this, &tst_WebGL::queryCommand);
    QTRY_VERIFY(std::find_if(spy.cbegin(), spy.cend(), [](const QList<QVariant> &list) {
        // Our connect message changed the scene's size, forcing a swapBuffers() call.
        return list.first() == QLatin1String("swapBuffers");
    }) != spy.cend());
}

QTEST_MAIN(tst_WebGL)

#include "tst_webgl.moc"