diff options
author | Andrei Golubev <andrei.golubev@qt.io> | 2021-02-25 15:46:20 +0100 |
---|---|---|
committer | Andrei Golubev <andrei.golubev@qt.io> | 2021-03-02 11:55:27 +0100 |
commit | 595dd75e3a6c74468d1752ebf3b6f1edc6a44882 (patch) | |
tree | 9efc8797fd85a5c0da6957d9f38c9eec549177c1 | |
parent | 4ebb4630e23fc6e0475ea7b3e4c10cec3cfb1ea9 (diff) |
Add "property var p: function() {}" test to tst_qmlcompiler_manual
This case is apparently very similar to "onSignal: function() {}", but
the behavior is completely different: instead of running the nested
function, we should only return it without evaluation. Add this as an
explicit test case to make sure QQmlEngine machinery behaves correctly
Change-Id: If426a934d4e5aa714f62cc437a75f8d16292a759
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
-rw-r--r-- | tests/auto/qml/qmlcompiler_manual/data/propertyReturningFunction.qml | 8 | ||||
-rw-r--r-- | tests/auto/qml/qmlcompiler_manual/tst_qmlcompiler_manual.cpp | 56 |
2 files changed, 64 insertions, 0 deletions
diff --git a/tests/auto/qml/qmlcompiler_manual/data/propertyReturningFunction.qml b/tests/auto/qml/qmlcompiler_manual/data/propertyReturningFunction.qml new file mode 100644 index 0000000000..a8472c3332 --- /dev/null +++ b/tests/auto/qml/qmlcompiler_manual/data/propertyReturningFunction.qml @@ -0,0 +1,8 @@ +import QtQml 2.0 + +QtObject { + property int counter: 0 + property var f: function() { + counter++; + } +} diff --git a/tests/auto/qml/qmlcompiler_manual/tst_qmlcompiler_manual.cpp b/tests/auto/qml/qmlcompiler_manual/tst_qmlcompiler_manual.cpp index 46efad05de..40b16b2b8d 100644 --- a/tests/auto/qml/qmlcompiler_manual/tst_qmlcompiler_manual.cpp +++ b/tests/auto/qml/qmlcompiler_manual/tst_qmlcompiler_manual.cpp @@ -53,6 +53,7 @@ private slots: void changingBindings(); void propertyAlias(); void propertyChangeHandler(); + void propertyReturningFunction(); private: void signalHandlers_impl(const QUrl &url); @@ -92,6 +93,8 @@ static constexpr int PROPERTY_ALIAS_GET_ALIAS_VALUE = 7; static constexpr int PROPERTY_CHANGE_HANDLER_P_BINDING = 0; static constexpr int PROPERTY_CHANGE_HANDLER_ON_P_CHANGED = 1; + +static constexpr int PROPERTY_RETURNING_FUNCTION_F_BINDING = 0; }; // test utility function for type erasure. the "real" code would be @@ -754,6 +757,59 @@ void tst_qmlcompiler_manual::propertyChangeHandler() QCOMPARE(created.property("watcher").toInt(), 96); } +class ANON_propertyReturningFunction : public QObject +{ + Q_OBJECT + QML_ANONYMOUS + Q_PROPERTY(int counter READ getCounter WRITE setCounter) + Q_PROPERTY(QVariant f READ getF WRITE setF BINDABLE bindableF) + +public: + // test workaround: the url is resolved by the test base class, so use + // member variable to store the resolved url used as argument in engine + // evaluation of runtime functions + QUrl url; + + ANON_propertyReturningFunction(QObject *parent = nullptr) : QObject(parent) + { + QPropertyBinding<QVariant> ANON_propertyReturningFunction_f_binding( + [&]() { + QQmlEnginePrivate *e = QQmlEnginePrivate::get(qmlEngine(this)); + const auto index = FunctionIndices::PROPERTY_RETURNING_FUNCTION_F_BINDING; + return qjsvalue_cast<QVariant>(e->executeRuntimeFunction(url, index, this)); + }, + QT_PROPERTY_DEFAULT_BINDING_LOCATION); + bindableF().setBinding(ANON_propertyReturningFunction_f_binding); + } + + int getCounter() { return counter.value(); } + QVariant getF() { return f.value(); } + + void setCounter(int counter_) { counter.setValue(counter_); } + void setF(QVariant f_) { f.setValue(f_); } + + QBindable<QVariant> bindableF() { return QBindable<QVariant>(&f); } + + QProperty<int> counter; + QProperty<QVariant> f; +}; + +void tst_qmlcompiler_manual::propertyReturningFunction() +{ + ANON_propertyReturningFunction created; + created.url = testFileUrl("propertyReturningFunction.qml"); // workaround + QQmlEngine e; + e.setContextForObject(&created, e.rootContext()); + + QCOMPARE(created.getCounter(), 0); + QVariant function = created.getF(); + Q_UNUSED(function); // ignored as it can't be used currently + QCOMPARE(created.getCounter(), 0); + + created.property("f"); + QCOMPARE(created.getCounter(), 0); +} + QTEST_MAIN(tst_qmlcompiler_manual) #include "tst_qmlcompiler_manual.moc" |