diff options
author | Michael Brasser <mbrasser@ford.com> | 2017-04-13 17:10:45 -0500 |
---|---|---|
committer | Michael Brasser <michael.brasser@live.com> | 2017-05-03 11:38:21 +0000 |
commit | aae18aa05719c4b750905f467f42002f4cea1516 (patch) | |
tree | 167e75ae3aa157b47fccd75266ba40aa0325b63a /tests/auto/qml/qqmllanguage | |
parent | f7656d06c0d2095a8d8dd3b930e48dc98996634f (diff) |
C++11 scoped enum support for QML
[ChangeLog][QtQml] Support C++11 scoped enums in QML. These can now be
accessed as <TypeName>.<EnumName>.<EnumValue>
Change-Id: I29bd3f16e980f3e6f1b2390b5a8e9e8e999952a3
Task-number: QTBUG-54961
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'tests/auto/qml/qqmllanguage')
7 files changed, 76 insertions, 0 deletions
diff --git a/tests/auto/qml/qqmllanguage/data/cppnamespace.qml b/tests/auto/qml/qqmllanguage/data/cppnamespace.qml index efedf2b14a..48f7eb6715 100644 --- a/tests/auto/qml/qqmllanguage/data/cppnamespace.qml +++ b/tests/auto/qml/qqmllanguage/data/cppnamespace.qml @@ -2,4 +2,5 @@ import Test 1.0 MyNamespacedType { myEnum: MyNamespace.Key5 + property int intProperty: MyNamespace.MyOtherNSEnum.OtherKey2 } diff --git a/tests/auto/qml/qqmllanguage/data/registeredCompositeTypeWithEnum.qml b/tests/auto/qml/qqmllanguage/data/registeredCompositeTypeWithEnum.qml index 5f8c11e5f6..b6a07693f2 100644 --- a/tests/auto/qml/qqmllanguage/data/registeredCompositeTypeWithEnum.qml +++ b/tests/auto/qml/qqmllanguage/data/registeredCompositeTypeWithEnum.qml @@ -3,4 +3,5 @@ import Test 1.0 RegisteredCompositeTypeWithEnum { property int enumValue0: RegisteredCompositeTypeWithEnum.EnumValue0 property int enumValue42: RegisteredCompositeTypeWithEnum.EnumValue42 + property int enumValue15: RegisteredCompositeTypeWithEnum.ScopedCompositeEnum.EnumValue15 } diff --git a/tests/auto/qml/qqmllanguage/data/scopedEnum.qml b/tests/auto/qml/qqmllanguage/data/scopedEnum.qml new file mode 100644 index 0000000000..7f4177af76 --- /dev/null +++ b/tests/auto/qml/qqmllanguage/data/scopedEnum.qml @@ -0,0 +1,21 @@ +import QtQuick 2.0 +import Test 1.0 + +MyTypeObject { + id: obj + scopedEnum: MyTypeObject.MyScopedEnum.ScopedVal1 + intProperty: MyTypeObject.MyScopedEnum.ScopedVal2 + property int listValue: myModel.get(0).myData + property int noScope: MyTypeObject.ScopedVal1 + + function assignNewValue() { + scopedEnum = MyTypeObject.MyScopedEnum.ScopedVal2 + noScope = MyTypeObject.ScopedVal2 + } + + property ListModel myModel: ListModel { + ListElement { + myData: MyTypeObject.MyScopedEnum.ScopedVal3 + } + } +} diff --git a/tests/auto/qml/qqmllanguage/data/scopedEnumList.errors.txt b/tests/auto/qml/qqmllanguage/data/scopedEnumList.errors.txt new file mode 100644 index 0000000000..67576dfd8d --- /dev/null +++ b/tests/auto/qml/qqmllanguage/data/scopedEnumList.errors.txt @@ -0,0 +1 @@ +7:13:ListElement: cannot use script for property value diff --git a/tests/auto/qml/qqmllanguage/data/scopedEnumList.qml b/tests/auto/qml/qqmllanguage/data/scopedEnumList.qml new file mode 100644 index 0000000000..8655139683 --- /dev/null +++ b/tests/auto/qml/qqmllanguage/data/scopedEnumList.qml @@ -0,0 +1,10 @@ +import QtQuick 2.0 +import Test 1.0 + +MyTypeObject { + property ListModel myModel: ListModel { + ListElement { + myData: MyTypeObject.MyScopedEnum + } + } +} diff --git a/tests/auto/qml/qqmllanguage/testtypes.h b/tests/auto/qml/qqmllanguage/testtypes.h index 7d7a8ac6d3..e4a76b4324 100644 --- a/tests/auto/qml/qqmllanguage/testtypes.h +++ b/tests/auto/qml/qqmllanguage/testtypes.h @@ -223,6 +223,7 @@ class MyTypeObject : public QObject Q_PROPERTY(Qt::TextFormat qtEnumProperty READ qtEnumProperty WRITE setQtEnumProperty NOTIFY qtEnumPropertyChanged) Q_PROPERTY(MyMirroredEnum mirroredEnumProperty READ mirroredEnumProperty WRITE setMirroredEnumProperty NOTIFY mirroredEnumPropertyChanged) Q_PROPERTY(MyEnumContainer::RelatedEnum relatedEnumProperty READ relatedEnumProperty WRITE setRelatedEnumProperty) + Q_PROPERTY(MyScopedEnum scopedEnum READ scopedEnum WRITE setScopedEnum) Q_PROPERTY(QString stringProperty READ stringProperty WRITE setStringProperty NOTIFY stringPropertyChanged) Q_PROPERTY(QByteArray byteArrayProperty READ byteArrayProperty WRITE setByteArrayProperty NOTIFY byteArrayPropertyChanged) Q_PROPERTY(uint uintProperty READ uintProperty WRITE setUintProperty NOTIFY uintPropertyChanged) @@ -339,6 +340,14 @@ public: relatedEnumPropertyValue = v; } + enum class MyScopedEnum : int { ScopedVal1, ScopedVal2, ScopedVal3 }; + Q_ENUM(MyScopedEnum) + MyScopedEnum scopedEnumPropertyValue; + MyScopedEnum scopedEnum() const { return scopedEnumPropertyValue; } + void setScopedEnum(MyScopedEnum v) { + scopedEnumPropertyValue = v; + } + QString stringPropertyValue; QString stringProperty() const { return stringPropertyValue; @@ -738,6 +747,13 @@ namespace MyNamespace { }; Q_ENUM_NS(MyNSEnum); + enum class MyOtherNSEnum { + OtherKey1 = 1, + OtherKey2 + }; + Q_ENUM_NS(MyOtherNSEnum); + + class MyNamespacedType : public QObject { Q_OBJECT @@ -1171,9 +1187,11 @@ class MyCompositeBaseType : public QObject { Q_OBJECT Q_ENUMS(CompositeEnum) + Q_ENUMS(ScopedCompositeEnum) public: enum CompositeEnum { EnumValue0, EnumValue42 = 42 }; + enum class ScopedCompositeEnum : int { EnumValue15 = 15 }; static QObject *qmlAttachedProperties(QObject *parent) { return new QObject(parent); } }; diff --git a/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp b/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp index e67fa18309..002c27c49d 100644 --- a/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp +++ b/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp @@ -208,6 +208,7 @@ private slots: void lowercaseEnumRuntime(); void lowercaseEnumCompileTime_data(); void lowercaseEnumCompileTime(); + void scopedEnum(); void literals_data(); void literals(); @@ -541,6 +542,8 @@ void tst_qqmllanguage::errors_data() QTest::newRow("singularProperty") << "singularProperty.qml" << "singularProperty.errors.txt" << false; QTest::newRow("singularProperty.2") << "singularProperty.2.qml" << "singularProperty.2.errors.txt" << false; + QTest::newRow("scopedEnumList") << "scopedEnumList.qml" << "scopedEnumList.errors.txt" << false; + const QString expectedError = isCaseSensitiveFileSystem(dataDirectory()) ? QStringLiteral("incorrectCase.errors.sensitive.txt") : QStringLiteral("incorrectCase.errors.insensitive.txt"); @@ -1601,6 +1604,9 @@ void tst_qqmllanguage::cppnamespace() VERIFY_ERRORS(0); QObject *object = component.create(); QVERIFY(object != 0); + + QCOMPARE(object->property("intProperty").toInt(), (int)MyNamespace::MyOtherNSEnum::OtherKey2); + delete object; } @@ -3501,6 +3507,7 @@ void tst_qqmllanguage::registeredCompositeTypeWithEnum() QCOMPARE(o->property("enumValue0").toInt(), static_cast<int>(MyCompositeBaseType::EnumValue0)); QCOMPARE(o->property("enumValue42").toInt(), static_cast<int>(MyCompositeBaseType::EnumValue42)); + QCOMPARE(o->property("enumValue15").toInt(), static_cast<int>(MyCompositeBaseType::ScopedCompositeEnum::EnumValue15)); delete o; } @@ -3681,6 +3688,23 @@ void tst_qqmllanguage::lowercaseEnumCompileTime() VERIFY_ERRORS(qPrintable(errorFile)); } +void tst_qqmllanguage::scopedEnum() +{ + QQmlComponent component(&engine, testFileUrl("scopedEnum.qml")); + + MyTypeObject *o = qobject_cast<MyTypeObject *>(component.create()); + QVERIFY(o != 0); + + QCOMPARE(o->scopedEnum(), MyTypeObject::MyScopedEnum::ScopedVal1); + QCOMPARE(o->intProperty(), (int)MyTypeObject::MyScopedEnum::ScopedVal2); + QCOMPARE(o->property("listValue").toInt(), (int)MyTypeObject::MyScopedEnum::ScopedVal3); + QCOMPARE(o->property("noScope").toInt(), (int)MyTypeObject::MyScopedEnum::ScopedVal1); + + QMetaObject::invokeMethod(o, "assignNewValue"); + QCOMPARE(o->scopedEnum(), MyTypeObject::MyScopedEnum::ScopedVal2); + QCOMPARE(o->property("noScope").toInt(), (int)MyTypeObject::MyScopedEnum::ScopedVal2); +} + void tst_qqmllanguage::literals_data() { QTest::addColumn<QString>("property"); |