summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPiotr Wierciński <piotr.wiercinski@qt.io>2023-11-22 16:10:04 +0100
committerPiotr Wierciński <piotr.wiercinski@qt.io>2023-12-19 17:05:55 +0000
commita03d0a33b574677be1c9040e706591c5a1f8ec69 (patch)
tree2ddedf0aec2e1ed0a97c3b8882cea48843e7a229
parent2643210bd6e07aa1d8ae78c109592bdc2545b074 (diff)
wasm tests: Add test for making network request
Add test for making network request via Emscripten Fetch API on main and background thread. Task-number: QTBUG-118225 Change-Id: I7c3923493818e75a5c16e4e5f553a69cb1438c44 Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Morten Johan Sørvig <morten.sorvig@qt.io> Reviewed-by: Piotr Wierciński <piotr.wiercinski@qt.io>
-rw-r--r--tests/auto/wasm/CMakeLists.txt12
-rw-r--r--tests/auto/wasm/tst_fetchapi.cpp85
2 files changed, 97 insertions, 0 deletions
diff --git a/tests/auto/wasm/CMakeLists.txt b/tests/auto/wasm/CMakeLists.txt
index 124ef86633..5036678bf6 100644
--- a/tests/auto/wasm/CMakeLists.txt
+++ b/tests/auto/wasm/CMakeLists.txt
@@ -11,6 +11,18 @@ if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT)
find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
endif()
+qt_internal_add_test(tst_fetchapi
+ SOURCES
+ tst_fetchapi.cpp
+ DEFINES
+ QT_NO_FOREACH
+ LIBRARIES
+ Qt::GuiPrivate
+ Qt::Network
+ PUBLIC_LIBRARIES
+ Qt::Core
+)
+
qt_internal_add_test(tst_localfileapi
SOURCES
tst_localfileapi.cpp
diff --git a/tests/auto/wasm/tst_fetchapi.cpp b/tests/auto/wasm/tst_fetchapi.cpp
new file mode 100644
index 0000000000..7bbb8104d7
--- /dev/null
+++ b/tests/auto/wasm/tst_fetchapi.cpp
@@ -0,0 +1,85 @@
+// Copyright (C) 2023 The Qt Company Ltd.
+// Copyright (C) 2016 Intel Corporation.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+
+#include <QTest>
+#include <QEventLoop>
+#include <QNetworkReply>
+#include <QNetworkRequest>
+#include <QScopedPointer>
+
+#include <memory>
+
+namespace {
+
+const QUrl URL = QUrl("http://localhost:6931/test_batch.html");
+
+class BackgroundThread : public QThread
+{
+ Q_OBJECT
+ void run() override
+ {
+ manager = std::make_unique<QNetworkAccessManager>();
+ eventLoop = std::make_unique<QEventLoop>();
+ reply = manager->get(request);
+ QObject::connect(reply, &QNetworkReply::finished, this, &BackgroundThread::requestFinished);
+ eventLoop->exec();
+ }
+
+ void requestFinished()
+ {
+ eventLoop->quit();
+ }
+
+public:
+ QNetworkReply *reply{ nullptr };
+
+private:
+ std::unique_ptr<QNetworkAccessManager> manager;
+ std::unique_ptr<QEventLoop> eventLoop;
+ QNetworkRequest request{ URL };
+};
+
+} // namespace
+
+class tst_FetchApi : public QObject
+{
+ Q_OBJECT
+public:
+ tst_FetchApi() = default;
+
+private Q_SLOTS:
+ void sendRequestOnMainThread();
+ void sendRequestOnBackgroundThread();
+};
+
+void tst_FetchApi::sendRequestOnMainThread()
+{
+ QNetworkAccessManager manager;
+ QNetworkRequest request(URL);
+ QNetworkReply *reply = manager.get(request);
+
+ QEventLoop loop;
+ QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
+ loop.exec();
+
+ QVERIFY(reply != nullptr);
+ QVERIFY(reply->error() == QNetworkReply::NoError);
+}
+
+void tst_FetchApi::sendRequestOnBackgroundThread()
+{
+ QEventLoop mainEventLoop;
+ BackgroundThread *backgroundThread = new BackgroundThread();
+ connect(backgroundThread, &BackgroundThread::finished, &mainEventLoop, &QEventLoop::quit);
+ connect(backgroundThread, &BackgroundThread::finished, backgroundThread, &QObject::deleteLater);
+ backgroundThread->start();
+ mainEventLoop.exec();
+
+ QVERIFY(backgroundThread != nullptr);
+ QVERIFY(backgroundThread->reply != nullptr);
+ QVERIFY(backgroundThread->reply->error() == QNetworkReply::NoError);
+}
+
+QTEST_MAIN(tst_FetchApi);
+#include "tst_fetchapi.moc"