aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorMaximilian Goldstein <max.goldstein@qt.io>2021-05-05 12:02:07 +0200
committerMaximilian Goldstein <max.goldstein@qt.io>2021-05-06 17:13:51 +0200
commit0fef848468fdf2fb38670a40c593a2b8f20830a6 (patch)
treeb52e5b18484b40cd06682d5e722b146b1dbf0b87 /tests
parent3a8c0fd5cb9bfec439c306593bce19f434c871ce (diff)
qv4engine: Fix enums getting turned into objects when passed in lists
Previously passing a QList of a registered enum would result in an array of objects instead of the array of numbers which usually represent enum values in QML. You now get an array of numbers as you would expect. [ChangeLog][QtQml][Important Behavior Changes] QJSEngine::toScriptValue() used to return a QVariant containing an enum, now it returns the enum directly. If you still wish to use valueOf() on the resulting value use QJSEngine::toManagedValue() instead. [ChangeLog][QtQml][Important Behavior Changes] A QList containing enums will now result in an array of numbers instead of an array of objects. Fixes: QTBUG-85861 Change-Id: I5c28f4489dfd02d8256aa818e27b1dd6b7d3113d Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io> Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/qml/qjsengine/tst_qjsengine.cpp4
-rw-r--r--tests/auto/qml/qqmllanguage/testtypes.h11
-rw-r--r--tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp29
3 files changed, 42 insertions, 2 deletions
diff --git a/tests/auto/qml/qjsengine/tst_qjsengine.cpp b/tests/auto/qml/qjsengine/tst_qjsengine.cpp
index 81671176cc..1cc48bd73b 100644
--- a/tests/auto/qml/qjsengine/tst_qjsengine.cpp
+++ b/tests/auto/qml/qjsengine/tst_qjsengine.cpp
@@ -692,8 +692,8 @@ void tst_QJSEngine::newVariant_valueOfEnum()
{
QJSEngine eng;
{
- QJSValue object = eng.toScriptValue(QVariant::fromValue(Qt::ControlModifier));
- QJSValue value = object.property("valueOf").callWithInstance(object);
+ QJSManagedValue object = eng.toManagedValue(QVariant::fromValue(Qt::ControlModifier));
+ QJSValue value = object.property("valueOf").callWithInstance(object.toJSValue());
QVERIFY(value.isNumber());
QCOMPARE(value.toInt(), static_cast<qint32>(Qt::ControlModifier));
}
diff --git a/tests/auto/qml/qqmllanguage/testtypes.h b/tests/auto/qml/qqmllanguage/testtypes.h
index ecf8b2ede2..25950acb74 100644
--- a/tests/auto/qml/qqmllanguage/testtypes.h
+++ b/tests/auto/qml/qqmllanguage/testtypes.h
@@ -1714,6 +1714,17 @@ signals:
void signal(QJSValue value);
};
+class EnumList : public QObject
+{
+ Q_OBJECT
+ QML_ELEMENT
+public:
+ enum Enum { Alpha, Beta, Gamma };
+ Q_ENUM(Enum)
+
+ Q_INVOKABLE QList<EnumList::Enum> list() const { return { Alpha, Beta, Gamma }; }
+};
+
void registerTypes();
#endif // TESTTYPES_H
diff --git a/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp b/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp
index 09b0e46a51..8988039259 100644
--- a/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp
+++ b/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp
@@ -358,6 +358,8 @@ private slots:
void hangOnWarning();
+ void listEnumConversion();
+
private:
QQmlEngine engine;
QStringList defaultImportPathList;
@@ -6297,6 +6299,33 @@ void tst_qqmllanguage::hangOnWarning()
QVERIFY(object != nullptr);
}
+void tst_qqmllanguage::listEnumConversion()
+{
+ QQmlEngine e;
+ QQmlComponent c(&engine);
+ c.setData(R"(
+import QtQml 2.0
+import StaticTest 1.0
+QtObject {
+ property EnumList enumList: EnumList {}
+ property var list: enumList.list()
+ property bool resultAlpha: EnumList.Alpha === list[0]
+ property bool resultBeta: EnumList.Beta === list[1]
+ property bool resultGamma: EnumList.Gamma === list[2]
+ property var resultEnumType: EnumList.Alpha
+ property var resultEnumListType: list[0]
+})",
+ QUrl());
+ QVERIFY2(c.isReady(), qPrintable(c.errorString()));
+
+ QScopedPointer<QObject> o(c.create());
+ QCOMPARE(o->property("resultAlpha").toBool(), true);
+ QCOMPARE(o->property("resultBeta").toBool(), true);
+ QCOMPARE(o->property("resultGamma").toBool(), true);
+ QCOMPARE(o->property("resultEnumType").metaType(), QMetaType(QMetaType::Int));
+ QCOMPARE(o->property("resultEnumListType").metaType(), QMetaType(QMetaType::Int));
+}
+
QTEST_MAIN(tst_qqmllanguage)
#include "tst_qqmllanguage.moc"