diff options
Diffstat (limited to 'tests/auto/quick/doc/how-tos/how-to-cpp-enum-js')
5 files changed, 144 insertions, 0 deletions
diff --git a/tests/auto/quick/doc/how-tos/how-to-cpp-enum-js/CMakeLists.txt b/tests/auto/quick/doc/how-tos/how-to-cpp-enum-js/CMakeLists.txt new file mode 100644 index 0000000000..9030a81c93 --- /dev/null +++ b/tests/auto/quick/doc/how-tos/how-to-cpp-enum-js/CMakeLists.txt @@ -0,0 +1,19 @@ +# Copyright (C) 2023 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +qt_internal_add_test(tst_how-to-cpp-enum-js + SOURCES + backend.cpp + backend.h + tst_how-to-cpp-enum-js.cpp + LIBRARIES + Qt::Core + Qt::Qml +) + +qt_add_resources(tst_how-to-cpp-enum-js "js" + PREFIX + / + FILES + script.mjs +) diff --git a/tests/auto/quick/doc/how-tos/how-to-cpp-enum-js/backend.cpp b/tests/auto/quick/doc/how-tos/how-to-cpp-enum-js/backend.cpp new file mode 100644 index 0000000000..9062dbad3f --- /dev/null +++ b/tests/auto/quick/doc/how-tos/how-to-cpp-enum-js/backend.cpp @@ -0,0 +1,36 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +//! [file] +#include "backend.h" + +Backend::Backend(QJSEngine *engine) : + mEngine(engine) +{ +} + +bool Backend::load() +{ + // Do some loading here... + + const QJSValue module = mEngine->importModule(":/script.mjs"); + if (module.isError()) { + qWarning() << "Error loading script.mjs:" << module.toString(); + return false; + } + + const QJSValue function = module.property("backendStatusUpdate"); + if (!function.isCallable()) { + qWarning() << "backendStatusUpdate script function is not callable!"; + return false; + } + + const QJSValue functionResult = function.call(QJSValueList() << Loaded); + if (functionResult.isError()) { + qWarning() << "backendStatusUpdate script function had errors:" << functionResult.toString(); + return false; + } + + return true; +} +//! [file] diff --git a/tests/auto/quick/doc/how-tos/how-to-cpp-enum-js/backend.h b/tests/auto/quick/doc/how-tos/how-to-cpp-enum-js/backend.h new file mode 100644 index 0000000000..7dbc7149e1 --- /dev/null +++ b/tests/auto/quick/doc/how-tos/how-to-cpp-enum-js/backend.h @@ -0,0 +1,29 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +//! [file] +#include <QObject> +#include <QJSEngine> + +class Backend : public QObject +{ + Q_OBJECT + +public: + Backend(QJSEngine *engine); + + enum Status { + Unknown, + Error, + Loading, + Loaded + }; + + Q_ENUM(Status) + + bool load(); + +private: + QJSEngine *mEngine = nullptr; +}; +//! [file] diff --git a/tests/auto/quick/doc/how-tos/how-to-cpp-enum-js/script.mjs b/tests/auto/quick/doc/how-tos/how-to-cpp-enum-js/script.mjs new file mode 100644 index 0000000000..de1e49a168 --- /dev/null +++ b/tests/auto/quick/doc/how-tos/how-to-cpp-enum-js/script.mjs @@ -0,0 +1,13 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +//! [file] +export function backendStatusUpdate(backendStatus) { + if (backendStatus === Backend.Error) { + console.warn("Error!") + return + } + + console.log("Backend loaded successfully") +} +//! [file] diff --git a/tests/auto/quick/doc/how-tos/how-to-cpp-enum-js/tst_how-to-cpp-enum-js.cpp b/tests/auto/quick/doc/how-tos/how-to-cpp-enum-js/tst_how-to-cpp-enum-js.cpp new file mode 100644 index 0000000000..ab78784675 --- /dev/null +++ b/tests/auto/quick/doc/how-tos/how-to-cpp-enum-js/tst_how-to-cpp-enum-js.cpp @@ -0,0 +1,47 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 + +#include <QtCore/qregularexpression.h> +#include <QtTest/QtTest> +#include <QtQml/qjsengine.h> + +#include "backend.h" + +QT_BEGIN_NAMESPACE + +class tst_HowToCppEnumJs : public QObject +{ + Q_OBJECT + +public: + tst_HowToCppEnumJs(); + +private slots: + void example(); +}; + +tst_HowToCppEnumJs::tst_HowToCppEnumJs() +{ +} + +void tst_HowToCppEnumJs::example() +{ + QTest::failOnWarning(QRegularExpression(QStringLiteral(".?"))); + + QJSEngine engine; + engine.installExtensions(QJSEngine::AllExtensions); + + QJSValue backendJsMetaObject = engine.newQMetaObject(&Backend::staticMetaObject); + engine.globalObject().setProperty("Backend", backendJsMetaObject); + + QTest::ignoreMessage(QtDebugMsg, "Backend loaded successfully"); + Backend backend(&engine); + const bool loaded = backend.load(); + QVERIFY(loaded); +} + +QT_END_NAMESPACE + +QTEST_MAIN(tst_HowToCppEnumJs) + +#include "tst_how-to-cpp-enum-js.moc" |
