diff options
Diffstat (limited to 'tests/manual/wasm/qstdweb')
-rw-r--r-- | tests/manual/wasm/qstdweb/CMakeLists.txt | 54 | ||||
-rw-r--r-- | tests/manual/wasm/qstdweb/files_auto.html | 2 | ||||
-rw-r--r-- | tests/manual/wasm/qstdweb/files_main.cpp | 20 | ||||
-rw-r--r-- | tests/manual/wasm/qstdweb/iodevices_auto.html | 10 | ||||
-rw-r--r-- | tests/manual/wasm/qstdweb/iodevices_main.cpp | 103 | ||||
-rw-r--r-- | tests/manual/wasm/qstdweb/promise_auto.html | 2 | ||||
-rw-r--r-- | tests/manual/wasm/qstdweb/promise_main.cpp | 2 | ||||
-rw-r--r-- | tests/manual/wasm/qstdweb/qwasmcompositor_auto.html | 10 | ||||
-rw-r--r-- | tests/manual/wasm/qstdweb/qwasmcompositor_main.cpp | 172 |
9 files changed, 361 insertions, 14 deletions
diff --git a/tests/manual/wasm/qstdweb/CMakeLists.txt b/tests/manual/wasm/qstdweb/CMakeLists.txt index 093fe54663..39039c3910 100644 --- a/tests/manual/wasm/qstdweb/CMakeLists.txt +++ b/tests/manual/wasm/qstdweb/CMakeLists.txt @@ -1,3 +1,5 @@ +# Copyright (C) 2024 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause qt_internal_add_manual_test(promise_auto SOURCES promise_main.cpp @@ -25,7 +27,7 @@ qt_internal_add_manual_test(files_auto SOURCES files_main.cpp ../qtwasmtestlib/qtwasmtestlib.cpp - PUBLIC_LIBRARIES + LIBRARIES Qt::Core Qt::CorePrivate Qt::GuiPrivate @@ -44,3 +46,53 @@ add_custom_command( COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/../qtwasmtestlib/qtwasmtestlib.js ${CMAKE_CURRENT_BINARY_DIR}/qtwasmtestlib.js) + +qt_internal_add_manual_test(qwasmcompositor_auto + SOURCES + qwasmcompositor_main.cpp + ../qtwasmtestlib/qtwasmtestlib.cpp + LIBRARIES + Qt::Core + Qt::CorePrivate + Qt::GuiPrivate +) + +include_directories(../qtwasmtestlib/) + +add_custom_command( + TARGET qwasmcompositor_auto POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy + ${CMAKE_CURRENT_SOURCE_DIR}/qwasmcompositor_auto.html + ${CMAKE_CURRENT_BINARY_DIR}/qwasmcompositor_auto.html) + +add_custom_command( + TARGET qwasmcompositor_auto POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy + ${CMAKE_CURRENT_SOURCE_DIR}/../qtwasmtestlib/qtwasmtestlib.js + ${CMAKE_CURRENT_BINARY_DIR}/qtwasmtestlib.js) + +target_link_options(qwasmcompositor_auto PRIVATE -sASYNCIFY -Os) + +qt_internal_add_manual_test(iodevices_auto + SOURCES + iodevices_main.cpp + ../qtwasmtestlib/qtwasmtestlib.cpp + LIBRARIES + Qt::Core + Qt::CorePrivate + Qt::GuiPrivate +) + +add_custom_command( + TARGET iodevices_auto POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy + ${CMAKE_CURRENT_SOURCE_DIR}/iodevices_auto.html + ${CMAKE_CURRENT_BINARY_DIR}/iodevices_auto.html) + +add_custom_command( + TARGET iodevices_auto POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy + ${CMAKE_CURRENT_SOURCE_DIR}/../qtwasmtestlib/qtwasmtestlib.js + ${CMAKE_CURRENT_BINARY_DIR}/qtwasmtestlib.js) + +target_link_options(iodevices_auto PRIVATE -sASYNCIFY -Os) diff --git a/tests/manual/wasm/qstdweb/files_auto.html b/tests/manual/wasm/qstdweb/files_auto.html index 71e8088dfb..9027fdc660 100644 --- a/tests/manual/wasm/qstdweb/files_auto.html +++ b/tests/manual/wasm/qstdweb/files_auto.html @@ -6,7 +6,7 @@ <script type="text/javascript" src="files_auto.js"></script> <script> window.onload = () => { - runTestCase(document.getElementById("log")); + runTestCase(files_auto_entry, document.getElementById("log")); }; </script> <p>Running files auto test.</p> diff --git a/tests/manual/wasm/qstdweb/files_main.cpp b/tests/manual/wasm/qstdweb/files_main.cpp index 4dfae7e13b..45939feb10 100644 --- a/tests/manual/wasm/qstdweb/files_main.cpp +++ b/tests/manual/wasm/qstdweb/files_main.cpp @@ -1,5 +1,5 @@ // Copyright (C) 2022 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QtCore/QCoreApplication> #include <QtCore/QEvent> @@ -320,8 +320,8 @@ void FilesTest::selectOneFileWithFileDialog() QWASMSUCCESS(); }); - QWasmLocalFileAccess::openFile( - {QStringLiteral("*")}, fileSelectedCallback->get(), acceptFileCallback->get(), fileDataReadyCallback->get()); + QWasmLocalFileAccess::openFile("*", fileSelectedCallback->get(), acceptFileCallback->get(), + fileDataReadyCallback->get()); } void FilesTest::selectMultipleFilesWithFileDialog() @@ -377,9 +377,9 @@ void FilesTest::selectMultipleFilesWithFileDialog() } }); - QWasmLocalFileAccess::openFiles( - {QStringLiteral("*")}, QWasmLocalFileAccess::FileSelectMode::MultipleFiles, - fileSelectedCallback->get(), acceptFileCallback->get(), fileDataReadyCallback->get()); + QWasmLocalFileAccess::openFiles("*", QWasmLocalFileAccess::FileSelectMode::MultipleFiles, + fileSelectedCallback->get(), acceptFileCallback->get(), + fileDataReadyCallback->get()); } void FilesTest::cancelFileDialog() @@ -398,8 +398,8 @@ void FilesTest::cancelFileDialog() auto* acceptFileCallback = Own(new MockCallback<char*, uint64_t, const std::string&>()); auto* fileDataReadyCallback = Own(new MockCallback<void>()); - QWasmLocalFileAccess::openFile( - {QStringLiteral("*")}, fileSelectedCallback->get(), acceptFileCallback->get(), fileDataReadyCallback->get()); + QWasmLocalFileAccess::openFile("*", fileSelectedCallback->get(), acceptFileCallback->get(), + fileDataReadyCallback->get()); } void FilesTest::rejectFile() @@ -430,8 +430,8 @@ void FilesTest::rejectFile() return nullptr; }); - QWasmLocalFileAccess::openFile( - {QStringLiteral("*")}, fileSelectedCallback->get(), acceptFileCallback->get(), fileDataReadyCallback->get()); + QWasmLocalFileAccess::openFile("*", fileSelectedCallback->get(), acceptFileCallback->get(), + fileDataReadyCallback->get()); } void FilesTest::saveFileWithFileDialog() diff --git a/tests/manual/wasm/qstdweb/iodevices_auto.html b/tests/manual/wasm/qstdweb/iodevices_auto.html new file mode 100644 index 0000000000..7937b8a483 --- /dev/null +++ b/tests/manual/wasm/qstdweb/iodevices_auto.html @@ -0,0 +1,10 @@ +<!doctype html> +<script type="text/javascript" src="qtwasmtestlib.js"></script> +<script type="text/javascript" src="iodevices_auto.js"></script> +<script> + window.onload = () => { + runTestCase(iodevices_auto_entry, document.getElementById("log")); + }; +</script> +<p>Running qstdweb iodevices auto test.</p> +<div id="log"></div> diff --git a/tests/manual/wasm/qstdweb/iodevices_main.cpp b/tests/manual/wasm/qstdweb/iodevices_main.cpp new file mode 100644 index 0000000000..0dbdd0084e --- /dev/null +++ b/tests/manual/wasm/qstdweb/iodevices_main.cpp @@ -0,0 +1,103 @@ +// Copyright (C) 2022 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + +#include <QtCore/QtCore> +#include <QtCore/private/qstdweb_p.h> + +#include <qtwasmtestlib.h> + +#include "emscripten.h" + +using qstdweb::ArrayBuffer; +using qstdweb::Uint8Array; +using qstdweb::Blob; +using qstdweb::BlobIODevice; +using qstdweb::Uint8ArrayIODevice; + +class WasmIoDevicesTest: public QObject +{ + Q_OBJECT + +private slots: + void blobIODevice(); + void uint8ArrayIODevice(); +}; + +// Creates a test arraybuffer with byte values [0..size] % 256 * 2 +char testByteValue(int i) { return (i % 256) * 2; } +ArrayBuffer createTestArrayBuffer(int size) +{ + ArrayBuffer buffer(size); + Uint8Array array(buffer); + for (int i = 0; i < size; ++i) + array.val().set(i, testByteValue(i)); + return buffer; +} + +void WasmIoDevicesTest::blobIODevice() +{ + if (!qstdweb::canBlockCallingThread()) { + QtWasmTest::completeTestFunction(QtWasmTest::TestResult::Skip, "requires asyncify"); + return; + } + + // Create test buffer and BlobIODevice + const int bufferSize = 16; + BlobIODevice blobDevice(Blob::fromArrayBuffer(createTestArrayBuffer(bufferSize))); + + // Read back byte for byte from the device + QWASMVERIFY(blobDevice.open(QIODevice::ReadOnly)); + for (int i = 0; i < bufferSize; ++i) { + char byte; + blobDevice.seek(i); + blobDevice.read(&byte, 1); + QWASMCOMPARE(byte, testByteValue(i)); + } + + blobDevice.close(); + QWASMVERIFY(!blobDevice.open(QIODevice::WriteOnly)); + QWASMSUCCESS(); +} + +void WasmIoDevicesTest::uint8ArrayIODevice() +{ + // Create test buffer and Uint8ArrayIODevice + const int bufferSize = 1024; + Uint8Array array(createTestArrayBuffer(bufferSize)); + Uint8ArrayIODevice arrayDevice(array); + + // Read back byte for byte from the device + QWASMVERIFY(arrayDevice.open(QIODevice::ReadWrite)); + for (int i = 0; i < bufferSize; ++i) { + char byte; + arrayDevice.seek(i); + arrayDevice.read(&byte, 1); + QWASMCOMPARE(byte, testByteValue(i)); + } + + // Write a different set of bytes + QWASMCOMPARE(arrayDevice.seek(0), true); + for (int i = 0; i < bufferSize; ++i) { + char byte = testByteValue(i + 1); + arrayDevice.seek(i); + QWASMCOMPARE(arrayDevice.write(&byte, 1), 1); + } + + // Verify that the original array was updated + QByteArray copy = QByteArray::fromEcmaUint8Array(array.val()); + for (int i = 0; i < bufferSize; ++i) + QWASMCOMPARE(copy.at(i), testByteValue(i + 1)); + + arrayDevice.close(); + QWASMSUCCESS(); +} + +int main(int argc, char **argv) +{ + auto testObject = std::make_shared<WasmIoDevicesTest>(); + QtWasmTest::initTestCase<QCoreApplication>(argc, argv, testObject); + return 0; +} + +#include "iodevices_main.moc" + diff --git a/tests/manual/wasm/qstdweb/promise_auto.html b/tests/manual/wasm/qstdweb/promise_auto.html index 786145419f..94a8dbb88a 100644 --- a/tests/manual/wasm/qstdweb/promise_auto.html +++ b/tests/manual/wasm/qstdweb/promise_auto.html @@ -3,7 +3,7 @@ <script type="text/javascript" src="promise_auto.js"></script> <script> window.onload = () => { - runTestCase(document.getElementById("log")); + runTestCase(promise_auto_entry, document.getElementById("log")); }; </script> <p>Running promise auto test.</p> diff --git a/tests/manual/wasm/qstdweb/promise_main.cpp b/tests/manual/wasm/qstdweb/promise_main.cpp index 351f06c91d..c5f6f7f412 100644 --- a/tests/manual/wasm/qstdweb/promise_main.cpp +++ b/tests/manual/wasm/qstdweb/promise_main.cpp @@ -1,5 +1,5 @@ // Copyright (C) 2022 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QtCore/QCoreApplication> #include <QtCore/QEvent> diff --git a/tests/manual/wasm/qstdweb/qwasmcompositor_auto.html b/tests/manual/wasm/qstdweb/qwasmcompositor_auto.html new file mode 100644 index 0000000000..f33aab0b9c --- /dev/null +++ b/tests/manual/wasm/qstdweb/qwasmcompositor_auto.html @@ -0,0 +1,10 @@ +<!doctype html> +<script type="text/javascript" src="qtwasmtestlib.js"></script> +<script type="text/javascript" src="qwasmcompositor_auto.js"></script> +<script> + window.onload = () => { + runTestCase(qwasmcompositor_auto_entry, document.getElementById("log")); + }; +</script> +<p>Running files auto test.</p> +<div id="log"></div> diff --git a/tests/manual/wasm/qstdweb/qwasmcompositor_main.cpp b/tests/manual/wasm/qstdweb/qwasmcompositor_main.cpp new file mode 100644 index 0000000000..e1a9cf604d --- /dev/null +++ b/tests/manual/wasm/qstdweb/qwasmcompositor_main.cpp @@ -0,0 +1,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, ¶ms, rhiFlags)); + m_rhi.reset(QRhi::create(QRhi::OpenGLES2, ¶ms, 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" |