2022-05-13 13:12:05 +00:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
2024-02-22 14:51:16 +00:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2025-06-02 14:15:47 +00:00
|
|
|
#include <private/qmlutils_p.h>
|
|
|
|
#include <private/qqmlengine_p.h>
|
|
|
|
#include <private/qquickworkerscript_p.h>
|
|
|
|
|
|
|
|
#include <QtTest/qtest.h>
|
|
|
|
|
|
|
|
#include <QtQml/qjsengine.h>
|
2012-02-16 04:43:03 +00:00
|
|
|
#include <QtQml/qqmlcomponent.h>
|
|
|
|
#include <QtQml/qqmlengine.h>
|
2025-06-02 14:15:47 +00:00
|
|
|
#include <QtQml/qqmlnetworkaccessmanagerfactory.h>
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2025-06-02 14:15:47 +00:00
|
|
|
#include <QtCore/qdebug.h>
|
|
|
|
#include <QtCore/qdir.h>
|
|
|
|
#include <QtCore/qfileinfo.h>
|
|
|
|
#include <QtCore/qregularexpression.h>
|
|
|
|
#include <QtCore/qtimer.h>
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-02-16 04:43:03 +00:00
|
|
|
class tst_QQuickWorkerScript : public QQmlDataTest
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
2021-08-06 10:27:35 +00:00
|
|
|
tst_QQuickWorkerScript() : QQmlDataTest(QT_QMLTEST_DATADIR) {}
|
2011-04-27 10:05:43 +00:00
|
|
|
private slots:
|
|
|
|
void source();
|
2019-12-06 09:56:22 +00:00
|
|
|
void ready();
|
2011-04-27 10:05:43 +00:00
|
|
|
void messaging();
|
|
|
|
void messaging_data();
|
|
|
|
void messaging_sendQObjectList();
|
|
|
|
void messaging_sendJsObject();
|
2011-11-04 04:38:38 +00:00
|
|
|
void messaging_sendExternalObject();
|
2011-04-27 10:05:43 +00:00
|
|
|
void script_with_pragma();
|
|
|
|
void script_included();
|
|
|
|
void scriptError_onLoad();
|
|
|
|
void scriptError_onCall();
|
2014-02-17 15:35:20 +00:00
|
|
|
void script_function();
|
|
|
|
void script_var();
|
2011-06-07 06:34:04 +00:00
|
|
|
void stressDispose();
|
2020-01-03 09:32:01 +00:00
|
|
|
void xmlHttpRequest();
|
2025-06-02 14:15:47 +00:00
|
|
|
void remoteScript();
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
private:
|
2012-02-16 04:43:03 +00:00
|
|
|
void waitForEchoMessage(QQuickWorkerScript *worker) {
|
2011-04-27 10:05:43 +00:00
|
|
|
QEventLoop loop;
|
|
|
|
QVERIFY(connect(worker, SIGNAL(done()), &loop, SLOT(quit())));
|
|
|
|
QTimer timer;
|
|
|
|
timer.setSingleShot(true);
|
|
|
|
connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
|
|
|
|
timer.start(10000);
|
|
|
|
loop.exec();
|
|
|
|
QVERIFY(timer.isActive());
|
|
|
|
}
|
|
|
|
|
2012-02-16 04:43:03 +00:00
|
|
|
QQmlEngine m_engine;
|
2011-04-27 10:05:43 +00:00
|
|
|
};
|
|
|
|
|
2012-02-16 04:43:03 +00:00
|
|
|
void tst_QQuickWorkerScript::source()
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2012-02-16 04:43:03 +00:00
|
|
|
QQmlComponent component(&m_engine, testFileUrl("worker.qml"));
|
2018-08-17 14:53:05 +00:00
|
|
|
QScopedPointer<QQuickWorkerScript>worker(qobject_cast<QQuickWorkerScript*>(component.create()));
|
2018-02-21 09:41:54 +00:00
|
|
|
QVERIFY(worker != nullptr);
|
2011-04-27 10:05:43 +00:00
|
|
|
const QMetaObject *mo = worker->metaObject();
|
|
|
|
|
|
|
|
QVariant value(100);
|
2018-08-17 14:53:05 +00:00
|
|
|
QVERIFY(QMetaObject::invokeMethod(worker.data(), "testSend", Q_ARG(QVariant, value)));
|
|
|
|
waitForEchoMessage(worker.data());
|
|
|
|
QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker.data()).value<QVariant>(), value);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2011-12-21 08:06:26 +00:00
|
|
|
QUrl source = testFileUrl("script_fixed_return.js");
|
2011-04-27 10:05:43 +00:00
|
|
|
worker->setSource(source);
|
|
|
|
QCOMPARE(worker->source(), source);
|
2018-08-17 14:53:05 +00:00
|
|
|
QVERIFY(QMetaObject::invokeMethod(worker.data(), "testSend", Q_ARG(QVariant, value)));
|
|
|
|
waitForEchoMessage(worker.data());
|
2019-04-05 07:42:17 +00:00
|
|
|
QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker.data()).value<QVariant>(), QVariant::fromValue(QString("Hello_World")));
|
2018-08-17 14:53:05 +00:00
|
|
|
|
|
|
|
source = testFileUrl("script_module.mjs");
|
|
|
|
worker->setSource(source);
|
|
|
|
QCOMPARE(worker->source(), source);
|
|
|
|
QVERIFY(QMetaObject::invokeMethod(worker.data(), "testSend", Q_ARG(QVariant, value)));
|
|
|
|
waitForEchoMessage(worker.data());
|
2019-04-05 07:42:17 +00:00
|
|
|
QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker.data()).value<QVariant>(), QVariant::fromValue(QString("Hello from the module")));
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
qApp->processEvents();
|
|
|
|
}
|
|
|
|
|
2019-12-06 09:56:22 +00:00
|
|
|
void tst_QQuickWorkerScript::ready()
|
|
|
|
{
|
|
|
|
QQmlComponent component(&m_engine, testFileUrl("worker.qml"));
|
|
|
|
QScopedPointer<QQuickWorkerScript>worker(qobject_cast<QQuickWorkerScript*>(component.create()));
|
|
|
|
QVERIFY(worker != nullptr);
|
|
|
|
|
|
|
|
const QMetaObject *mo = worker->metaObject();
|
|
|
|
|
2019-12-11 15:39:42 +00:00
|
|
|
QTRY_VERIFY(worker->ready());
|
2019-12-06 09:56:22 +00:00
|
|
|
|
|
|
|
QVariant readyChangedCalled = mo->property(mo->indexOfProperty("readyChangedCalled")).read(worker.data()).value<QVariant>();
|
|
|
|
|
|
|
|
QVERIFY(!readyChangedCalled.isNull());
|
|
|
|
QVERIFY(readyChangedCalled.toBool());
|
|
|
|
}
|
|
|
|
|
2012-02-16 04:43:03 +00:00
|
|
|
void tst_QQuickWorkerScript::messaging()
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
QFETCH(QVariant, value);
|
|
|
|
|
2012-02-16 04:43:03 +00:00
|
|
|
QQmlComponent component(&m_engine, testFileUrl("worker.qml"));
|
2023-08-17 12:25:10 +00:00
|
|
|
std::unique_ptr<QQuickWorkerScript> worker { qobject_cast<QQuickWorkerScript*>(component.create()) };
|
|
|
|
QVERIFY(worker);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2023-08-17 12:25:10 +00:00
|
|
|
QVERIFY(QMetaObject::invokeMethod(worker.get(), "testSend", Q_ARG(QVariant, value)));
|
|
|
|
waitForEchoMessage(worker.get());
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
const QMetaObject *mo = worker->metaObject();
|
2023-08-17 12:25:10 +00:00
|
|
|
QVariant response = mo->property(mo->indexOfProperty("response")).read(worker.get()).value<QVariant>();
|
2014-09-10 15:13:10 +00:00
|
|
|
if (response.userType() == qMetaTypeId<QJSValue>())
|
|
|
|
response = response.value<QJSValue>().toVariant();
|
2019-02-13 14:54:14 +00:00
|
|
|
|
2020-04-02 07:57:13 +00:00
|
|
|
QCOMPARE(response, value);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
qApp->processEvents();
|
|
|
|
}
|
|
|
|
|
2012-02-16 04:43:03 +00:00
|
|
|
void tst_QQuickWorkerScript::messaging_data()
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
QTest::addColumn<QVariant>("value");
|
|
|
|
|
|
|
|
QTest::newRow("invalid") << QVariant();
|
2019-04-05 07:42:17 +00:00
|
|
|
QTest::newRow("bool") << QVariant::fromValue(true);
|
|
|
|
QTest::newRow("int") << QVariant::fromValue(1001);
|
|
|
|
QTest::newRow("real") << QVariant::fromValue(10334.375);
|
|
|
|
QTest::newRow("string") << QVariant::fromValue(QString("More cheeeese, Gromit!"));
|
|
|
|
QTest::newRow("variant list") << QVariant::fromValue((QVariantList() << "a" << "b" << "c"));
|
|
|
|
QTest::newRow("date time") << QVariant::fromValue(QDateTime::currentDateTime());
|
|
|
|
QTest::newRow("regularexpression") << QVariant::fromValue(QRegularExpression(
|
2019-02-13 14:54:14 +00:00
|
|
|
"^\\d\\d?$", QRegularExpression::CaseInsensitiveOption));
|
2019-12-13 12:43:28 +00:00
|
|
|
QTest::newRow("url") << QVariant::fromValue(QUrl("http://example.com/foo/bar"));
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2012-02-16 04:43:03 +00:00
|
|
|
void tst_QQuickWorkerScript::messaging_sendQObjectList()
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2013-01-10 22:07:37 +00:00
|
|
|
// Not allowed to send QObjects other than QQmlListModelWorkerAgent
|
2011-04-27 10:05:43 +00:00
|
|
|
// instances. If objects are sent in a list, they will be sent as 'undefined'
|
|
|
|
// js values.
|
|
|
|
|
2012-02-16 04:43:03 +00:00
|
|
|
QQmlComponent component(&m_engine, testFileUrl("worker.qml"));
|
2023-08-17 12:25:10 +00:00
|
|
|
std::unique_ptr<QQuickWorkerScript> worker { qobject_cast<QQuickWorkerScript*>(component.create()) };
|
|
|
|
QVERIFY(worker);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QVariantList objects;
|
|
|
|
for (int i=0; i<3; i++)
|
2019-04-05 07:42:17 +00:00
|
|
|
objects << QVariant::fromValue(new QObject(this));
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2023-08-17 12:25:10 +00:00
|
|
|
QVERIFY(QMetaObject::invokeMethod(worker.get(), "testSend", Q_ARG(QVariant, QVariant::fromValue(objects))));
|
|
|
|
waitForEchoMessage(worker.get());
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
const QMetaObject *mo = worker->metaObject();
|
2023-08-17 12:25:10 +00:00
|
|
|
QVariantList result = mo->property(mo->indexOfProperty("response")).read(worker.get()).value<QVariantList>();
|
2011-04-27 10:05:43 +00:00
|
|
|
QCOMPARE(result, (QVariantList() << QVariant() << QVariant() << QVariant()));
|
|
|
|
|
|
|
|
qApp->processEvents();
|
|
|
|
}
|
|
|
|
|
2012-02-16 04:43:03 +00:00
|
|
|
void tst_QQuickWorkerScript::messaging_sendJsObject()
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2012-02-16 04:43:03 +00:00
|
|
|
QQmlComponent component(&m_engine, testFileUrl("worker.qml"));
|
2023-08-17 12:25:10 +00:00
|
|
|
std::unique_ptr<QQuickWorkerScript> worker { qobject_cast<QQuickWorkerScript*>(component.create()) };
|
|
|
|
QVERIFY(worker);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
// Properties are in alphabetical order to enable string-based comparison after
|
|
|
|
// QVariant roundtrip, since the properties will be stored in a QVariantMap.
|
|
|
|
QString jsObject = "{'haste': 1125, 'name': 'zyz', 'spell power': 3101}";
|
|
|
|
|
2011-05-11 07:20:40 +00:00
|
|
|
QVariantMap map;
|
|
|
|
map.insert("haste", 1125);
|
|
|
|
map.insert("name", "zyz");
|
|
|
|
map.insert("spell power", 3101);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2023-08-17 12:25:10 +00:00
|
|
|
QVERIFY(QMetaObject::invokeMethod(worker.get(), "testSend", Q_ARG(QVariant, QVariant::fromValue(map))));
|
|
|
|
waitForEchoMessage(worker.get());
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2019-04-05 07:42:17 +00:00
|
|
|
QVariant result = QVariant::fromValue(false);
|
2023-08-17 12:25:10 +00:00
|
|
|
QVERIFY(QMetaObject::invokeMethod(worker.get(), "compareLiteralResponse", Qt::DirectConnection,
|
2011-04-27 10:05:43 +00:00
|
|
|
Q_RETURN_ARG(QVariant, result), Q_ARG(QVariant, jsObject)));
|
|
|
|
QVERIFY(result.toBool());
|
|
|
|
|
|
|
|
qApp->processEvents();
|
|
|
|
}
|
|
|
|
|
2012-02-16 04:43:03 +00:00
|
|
|
void tst_QQuickWorkerScript::messaging_sendExternalObject()
|
2011-11-04 04:38:38 +00:00
|
|
|
{
|
2012-02-16 04:43:03 +00:00
|
|
|
QQmlComponent component(&m_engine, testFileUrl("externalObjectWorker.qml"));
|
2023-08-17 12:25:10 +00:00
|
|
|
std::unique_ptr<QObject> obj { component.create() };
|
|
|
|
QVERIFY(obj.get());
|
|
|
|
QMetaObject::invokeMethod(obj.get(), "testExternalObject");
|
2011-11-04 04:38:38 +00:00
|
|
|
QTest::qWait(100); // shouldn't crash.
|
|
|
|
}
|
|
|
|
|
2012-02-16 04:43:03 +00:00
|
|
|
void tst_QQuickWorkerScript::script_with_pragma()
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
QVariant value(100);
|
|
|
|
|
2012-02-16 04:43:03 +00:00
|
|
|
QQmlComponent component(&m_engine, testFileUrl("worker_pragma.qml"));
|
2023-08-17 12:25:10 +00:00
|
|
|
std::unique_ptr<QQuickWorkerScript> worker { qobject_cast<QQuickWorkerScript*>(component.create()) };
|
|
|
|
QVERIFY(worker);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2023-08-17 12:25:10 +00:00
|
|
|
QVERIFY(QMetaObject::invokeMethod(worker.get(), "testSend", Q_ARG(QVariant, value)));
|
|
|
|
waitForEchoMessage(worker.get());
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
const QMetaObject *mo = worker->metaObject();
|
2023-08-17 12:25:10 +00:00
|
|
|
QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker.get()).value<QVariant>(), value);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
qApp->processEvents();
|
|
|
|
}
|
|
|
|
|
2012-02-16 04:43:03 +00:00
|
|
|
void tst_QQuickWorkerScript::script_included()
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2012-02-16 04:43:03 +00:00
|
|
|
QQmlComponent component(&m_engine, testFileUrl("worker_include.qml"));
|
2023-08-17 12:25:10 +00:00
|
|
|
std::unique_ptr<QQuickWorkerScript> worker { qobject_cast<QQuickWorkerScript*>(component.create()) };
|
|
|
|
QVERIFY(worker);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QString value("Hello");
|
|
|
|
|
2023-08-17 12:25:10 +00:00
|
|
|
QVERIFY(QMetaObject::invokeMethod(worker.get(), "testSend", Q_ARG(QVariant, value)));
|
|
|
|
waitForEchoMessage(worker.get());
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
const QMetaObject *mo = worker->metaObject();
|
2023-08-17 12:25:10 +00:00
|
|
|
QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker.get()).toString(), value + " World");
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
qApp->processEvents();
|
|
|
|
}
|
|
|
|
|
2012-02-16 04:43:03 +00:00
|
|
|
static QString qquickworkerscript_lastWarning;
|
2012-09-13 12:35:42 +00:00
|
|
|
static void qquickworkerscript_warningsHandler(QtMsgType type, const QMessageLogContext &, const QString &msg)
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
if (type == QtWarningMsg)
|
2012-09-13 12:35:42 +00:00
|
|
|
qquickworkerscript_lastWarning = msg;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2012-02-16 04:43:03 +00:00
|
|
|
void tst_QQuickWorkerScript::scriptError_onLoad()
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2012-02-16 04:43:03 +00:00
|
|
|
QQmlComponent component(&m_engine, testFileUrl("worker_error_onLoad.qml"));
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-09-13 12:35:42 +00:00
|
|
|
QtMessageHandler previousMsgHandler = qInstallMessageHandler(qquickworkerscript_warningsHandler);
|
2023-08-17 12:25:10 +00:00
|
|
|
std::unique_ptr<QQuickWorkerScript> worker { qobject_cast<QQuickWorkerScript*>(component.create()) };
|
|
|
|
QVERIFY(worker);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-02-16 04:43:03 +00:00
|
|
|
QTRY_COMPARE(qquickworkerscript_lastWarning,
|
2017-05-15 07:56:05 +00:00
|
|
|
testFileUrl("script_error_onLoad.js").toString() + QLatin1String(":3:10: SyntaxError: Expected token `,'"));
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-09-13 12:35:42 +00:00
|
|
|
qInstallMessageHandler(previousMsgHandler);
|
2011-04-27 10:05:43 +00:00
|
|
|
qApp->processEvents();
|
|
|
|
}
|
|
|
|
|
2012-02-16 04:43:03 +00:00
|
|
|
void tst_QQuickWorkerScript::scriptError_onCall()
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2012-02-16 04:43:03 +00:00
|
|
|
QQmlComponent component(&m_engine, testFileUrl("worker_error_onCall.qml"));
|
2023-08-17 12:25:10 +00:00
|
|
|
std::unique_ptr<QQuickWorkerScript> worker { qobject_cast<QQuickWorkerScript*>(component.create()) };
|
|
|
|
QVERIFY(worker);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-09-13 12:35:42 +00:00
|
|
|
QtMessageHandler previousMsgHandler = qInstallMessageHandler(qquickworkerscript_warningsHandler);
|
2011-04-27 10:05:43 +00:00
|
|
|
QVariant value;
|
2023-08-17 12:25:10 +00:00
|
|
|
QVERIFY(QMetaObject::invokeMethod(worker.get(), "testSend", Q_ARG(QVariant, value)));
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-02-16 04:43:03 +00:00
|
|
|
QTRY_COMPARE(qquickworkerscript_lastWarning,
|
2012-03-14 11:51:22 +00:00
|
|
|
testFileUrl("script_error_onCall.js").toString() + QLatin1String(":4: ReferenceError: getData is not defined"));
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-09-13 12:35:42 +00:00
|
|
|
qInstallMessageHandler(previousMsgHandler);
|
2011-04-27 10:05:43 +00:00
|
|
|
qApp->processEvents();
|
|
|
|
}
|
|
|
|
|
2014-02-17 15:35:20 +00:00
|
|
|
void tst_QQuickWorkerScript::script_function()
|
|
|
|
{
|
|
|
|
QQmlComponent component(&m_engine, testFileUrl("worker_function.qml"));
|
2023-08-17 12:25:10 +00:00
|
|
|
std::unique_ptr<QQuickWorkerScript> worker { qobject_cast<QQuickWorkerScript*>(component.create()) };
|
|
|
|
QVERIFY(worker);
|
2014-02-17 15:35:20 +00:00
|
|
|
|
|
|
|
QString value("Hello");
|
|
|
|
|
2023-08-17 12:25:10 +00:00
|
|
|
QVERIFY(QMetaObject::invokeMethod(worker.get(), "testSend", Q_ARG(QVariant, value)));
|
|
|
|
waitForEchoMessage(worker.get());
|
2014-02-17 15:35:20 +00:00
|
|
|
|
|
|
|
const QMetaObject *mo = worker->metaObject();
|
2023-08-17 12:25:10 +00:00
|
|
|
QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker.get()).toString(), value + " World");
|
2014-02-17 15:35:20 +00:00
|
|
|
|
|
|
|
qApp->processEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QQuickWorkerScript::script_var()
|
|
|
|
{
|
|
|
|
QQmlComponent component(&m_engine, testFileUrl("worker_var.qml"));
|
2023-08-17 12:25:10 +00:00
|
|
|
std::unique_ptr<QQuickWorkerScript> worker { qobject_cast<QQuickWorkerScript*>(component.create()) };
|
|
|
|
QVERIFY(worker);
|
2014-02-17 15:35:20 +00:00
|
|
|
|
|
|
|
QString value("Hello");
|
|
|
|
|
2023-08-17 12:25:10 +00:00
|
|
|
QVERIFY(QMetaObject::invokeMethod(worker.get(), "testSend", Q_ARG(QVariant, value)));
|
|
|
|
waitForEchoMessage(worker.get());
|
2014-02-17 15:35:20 +00:00
|
|
|
|
|
|
|
const QMetaObject *mo = worker->metaObject();
|
2023-08-17 12:25:10 +00:00
|
|
|
QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker.get()).toString(), value + " World");
|
2014-02-17 15:35:20 +00:00
|
|
|
|
|
|
|
qApp->processEvents();
|
|
|
|
}
|
|
|
|
|
2011-06-07 06:34:04 +00:00
|
|
|
// Rapidly create and destroy worker scripts to test resources are being disposed
|
|
|
|
// in the correct isolate
|
2012-02-16 04:43:03 +00:00
|
|
|
void tst_QQuickWorkerScript::stressDispose()
|
2011-06-07 06:34:04 +00:00
|
|
|
{
|
|
|
|
for (int ii = 0; ii < 100; ++ii) {
|
2012-02-16 04:43:03 +00:00
|
|
|
QQmlEngine engine;
|
|
|
|
QQmlComponent component(&engine, testFileUrl("stressDispose.qml"));
|
2023-08-17 12:25:10 +00:00
|
|
|
std::unique_ptr<QObject> o { component.create() };
|
|
|
|
QVERIFY(o.get());
|
2011-06-07 06:34:04 +00:00
|
|
|
}
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2020-01-03 09:32:01 +00:00
|
|
|
void tst_QQuickWorkerScript::xmlHttpRequest()
|
|
|
|
{
|
|
|
|
QQmlComponent component(&m_engine, testFileUrl("xmlHttpRequest.qml"));
|
|
|
|
QScopedPointer<QObject> root{component.create()}; // should not crash
|
|
|
|
QVERIFY(root);
|
|
|
|
}
|
|
|
|
|
2025-06-02 14:15:47 +00:00
|
|
|
|
|
|
|
class DebugStrippingNAM : public QNetworkAccessManager
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DebugStrippingNAM(const QString &replacementScheme, QObject *parent = nullptr)
|
|
|
|
: QNetworkAccessManager(parent)
|
|
|
|
, replacementScheme(replacementScheme)
|
|
|
|
{}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
QNetworkReply *createRequest(
|
|
|
|
Operation op, const QNetworkRequest &request, QIODevice *outgoingData) final
|
|
|
|
{
|
|
|
|
QUrl url = request.url();
|
|
|
|
if (request.url().scheme() != "debug")
|
|
|
|
return QNetworkAccessManager::createRequest(op, request, outgoingData);
|
|
|
|
|
|
|
|
QNetworkRequest rewritten = request;
|
|
|
|
url.setScheme(replacementScheme);
|
|
|
|
rewritten.setUrl(url);
|
|
|
|
return QNetworkAccessManager::createRequest(op, rewritten, outgoingData);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
QString replacementScheme;
|
|
|
|
};
|
|
|
|
|
|
|
|
class DebugStrippingNAMF : public QQmlNetworkAccessManagerFactory
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DebugStrippingNAMF(const QString &replacementScheme)
|
|
|
|
: replacementScheme(replacementScheme)
|
|
|
|
{}
|
|
|
|
|
|
|
|
QNetworkAccessManager *create(QObject *parent) final
|
|
|
|
{
|
|
|
|
return new DebugStrippingNAM(replacementScheme, parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
QString replacementScheme;
|
|
|
|
};
|
|
|
|
|
|
|
|
void tst_QQuickWorkerScript::remoteScript()
|
|
|
|
{
|
|
|
|
QUrl url = testFileUrl("remoteScript.qml");
|
|
|
|
DebugStrippingNAMF factory(url.scheme());
|
|
|
|
|
|
|
|
QQmlEngine engine;
|
|
|
|
engine.setNetworkAccessManagerFactory(&factory);
|
|
|
|
|
|
|
|
url.setScheme("debug");
|
|
|
|
QQmlComponent c(&engine, url);
|
|
|
|
QTRY_VERIFY2(c.isReady(), qPrintable(c.errorString()));
|
|
|
|
|
|
|
|
QTest::ignoreMessage(QtDebugMsg, "received");
|
|
|
|
QScopedPointer<QObject> o(c.create());
|
|
|
|
QVERIFY(!o.isNull());
|
|
|
|
|
|
|
|
QTRY_VERIFY(o->property("ready").toBool());
|
|
|
|
QTRY_COMPARE(o->objectName(), "remote script running");
|
|
|
|
}
|
|
|
|
|
2012-02-16 04:43:03 +00:00
|
|
|
QTEST_MAIN(tst_QQuickWorkerScript)
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-02-16 04:43:03 +00:00
|
|
|
#include "tst_qquickworkerscript.moc"
|