diff options
Diffstat (limited to 'tests/auto/declarative/qmlparser/tst_qmlparser.cpp')
-rw-r--r-- | tests/auto/declarative/qmlparser/tst_qmlparser.cpp | 206 |
1 files changed, 199 insertions, 7 deletions
diff --git a/tests/auto/declarative/qmlparser/tst_qmlparser.cpp b/tests/auto/declarative/qmlparser/tst_qmlparser.cpp index 7b6d7aa..b27d5d5 100644 --- a/tests/auto/declarative/qmlparser/tst_qmlparser.cpp +++ b/tests/auto/declarative/qmlparser/tst_qmlparser.cpp @@ -1,6 +1,7 @@ #include <qtest.h> #include <QtDeclarative/qmlengine.h> #include <QtDeclarative/qmlcomponent.h> +#include <QtDeclarative/qmlpropertyvaluesource.h> #include <QtCore/qfile.h> #include <QtCore/qdebug.h> @@ -15,7 +16,7 @@ Q_DECLARE_INTERFACE(MyInterface, "com.trolltech.Qt.Test.MyInterface"); QML_DECLARE_INTERFACE(MyInterface); QML_DEFINE_INTERFACE(MyInterface); -class MyQmlObject : public QObject, public MyInterface +class MyQmlObject : public QObject, public MyInterface, public QmlParserStatus { Q_OBJECT Q_PROPERTY(int value READ value WRITE setValue) @@ -24,7 +25,7 @@ class MyQmlObject : public QObject, public MyInterface Q_PROPERTY(QRect rect READ rect WRITE setRect) Q_PROPERTY(QMatrix matrix READ matrix WRITE setMatrix) //assumed to be unsupported by QML Q_PROPERTY(MyInterface *interface READ interface WRITE setInterface) - Q_INTERFACES(MyInterface) + Q_INTERFACES(MyInterface QmlParserStatus) public: MyQmlObject() : m_value(-1), m_interface(0) {} @@ -45,6 +46,11 @@ public: MyInterface *interface() const { return m_interface; } void setInterface(MyInterface *iface) { m_interface = iface; } + static QObject *qmlAttachedProperties(QObject *other) { + MyQmlObject *rv = new MyQmlObject; + rv->setParent(other); + return rv; + } Q_CLASSINFO("DefaultMethod", "basicSlot()"); public slots: @@ -68,6 +74,9 @@ class MyTypeObject : public QObject Q_ENUMS(MyEnum) Q_FLAGS(MyFlags) + Q_PROPERTY(QString id READ id WRITE setId); + Q_PROPERTY(QObject *objectProperty READ objectProperty WRITE setObjectProperty); + Q_PROPERTY(QmlComponent *componentProperty READ componentProperty WRITE setComponentProperty); Q_PROPERTY(MyFlags flagProperty READ flagProperty WRITE setFlagProperty); Q_PROPERTY(MyEnum enumProperty READ enumProperty WRITE setEnumProperty); Q_PROPERTY(QString stringProperty READ stringProperty WRITE setStringProperty); @@ -89,6 +98,33 @@ class MyTypeObject : public QObject Q_PROPERTY(QVariant variantProperty READ variantProperty WRITE setVariantProperty); public: + MyTypeObject() + : objectPropertyValue(0), componentPropertyValue(0) {} + + QString idValue; + QString id() const { + return idValue; + } + void setId(const QString &v) { + idValue = v; + } + + QObject *objectPropertyValue; + QObject *objectProperty() const { + return objectPropertyValue; + } + void setObjectProperty(QObject *v) { + objectPropertyValue = v; + } + + QmlComponent *componentPropertyValue; + QmlComponent *componentProperty() const { + return componentPropertyValue; + } + void setComponentProperty(QmlComponent *v) { + componentPropertyValue = v; + } + enum MyFlag { FlagVal1 = 0x01, FlagVal2 = 0x02, FlagVal3 = 0x04 }; Q_DECLARE_FLAGS(MyFlags, MyFlag) MyFlags flagPropertyValue; @@ -273,6 +309,22 @@ private: QML_DECLARE_TYPE(MyContainer); QML_DEFINE_TYPE(MyContainer,MyContainer); +class MyPropertyValueSource : public QmlPropertyValueSource +{ + Q_OBJECT +public: + MyPropertyValueSource() + : QmlPropertyValueSource(0) {} + + QmlMetaProperty prop; + virtual void setTarget(const QmlMetaProperty &p) + { + prop = p; + } +}; +QML_DECLARE_TYPE(MyPropertyValueSource); +QML_DEFINE_TYPE(MyPropertyValueSource,MyPropertyValueSource); + class MyDotPropertyObject : public QObject { Q_OBJECT @@ -334,9 +386,19 @@ private slots: void assignObjectToSignal(); void assignQmlComponent(); void assignBasicTypes(); + void assignTypeExtremes(); void customParserTypes(); void rootAsQmlComponent(); void inlineQmlComponents(); + void idProperty(); + void assignSignal(); + void dynamicProperties(); + void dynamicSignalsAndSlots(); + void simpleBindings(); + void autoComponentCreation(); + void propertyValueSource(); + void attachedProperties(); + void dynamicObjects(); // regression tests for crashes void crash1(); @@ -385,7 +447,6 @@ void tst_qmlparser::errors_data() QTest::newRow("nonExistantProperty.5") << "nonexistantProperty.5.txt" << "nonexistantProperty.5.errors.txt" << false; QTest::newRow("nonExistantProperty.6") << "nonexistantProperty.6.txt" << "nonexistantProperty.6.errors.txt" << true; - QTest::newRow("wrongType (string for int)") << "wrongType.1.txt" << "wrongType.1.errors.txt" << false; QTest::newRow("wrongType (int for bool)") << "wrongType.2.txt" << "wrongType.2.errors.txt" << false; QTest::newRow("wrongType (bad rect)") << "wrongType.3.txt" << "wrongType.3.errors.txt" << false; @@ -402,19 +463,27 @@ void tst_qmlparser::errors_data() QTest::newRow("wrongType (number string for int)") << "wrongType.13.txt" << "wrongType.13.errors.txt" << false; QTest::newRow("wrongType (int for string)") << "wrongType.14.txt" << "wrongType.14.errors.txt" << false; - QTest::newRow("nonExistantProperty.1") << "readOnly.1.txt" << "readOnly.1.errors.txt" << false; - QTest::newRow("nonExistantProperty.2") << "readOnly.2.txt" << "readOnly.2.errors.txt" << true; + QTest::newRow("readOnly.1") << "readOnly.1.txt" << "readOnly.1.errors.txt" << false; + QTest::newRow("readOnly.2") << "readOnly.2.txt" << "readOnly.2.errors.txt" << true; + QTest::newRow("listAssignment.1") << "listAssignment.1.txt" << "listAssignment.1.errors.txt" << false; + QTest::newRow("listAssignment.2") << "listAssignment.2.txt" << "listAssignment.2.errors.txt" << false; + QTest::newRow("listAssignment.3") << "listAssignment.3.txt" << "listAssignment.3.errors.txt" << false; + + QTest::newRow("invalidID.1") << "invalidID.txt" << "invalidID.errors.txt" << false; + QTest::newRow("invalidID.2") << "invalidID.2.txt" << "invalidID.2.errors.txt" << false; + QTest::newRow("invalidID.3") << "invalidID.3.txt" << "invalidID.3.errors.txt" << false; + QTest::newRow("invalidID.4") << "invalidID.4.txt" << "invalidID.4.errors.txt" << false; QTest::newRow("unsupportedProperty") << "unsupportedProperty.txt" << "unsupportedProperty.errors.txt" << true; QTest::newRow("nullDotProperty") << "nullDotProperty.txt" << "nullDotProperty.errors.txt" << true; QTest::newRow("fakeDotProperty") << "fakeDotProperty.txt" << "fakeDotProperty.errors.txt" << true; QTest::newRow("duplicateIDs") << "duplicateIDs.txt" << "duplicateIDs.errors.txt" << false; - QTest::newRow("invalidID.1") << "invalidID.txt" << "invalidID.errors.txt" << false; - QTest::newRow("invalidID.2") << "invalidID.2.txt" << "invalidID.2.errors.txt" << false; QTest::newRow("unregisteredObject") << "unregisteredObject.txt" << "unregisteredObject.errors.txt" << false; QTest::newRow("empty") << "empty.txt" << "empty.errors.txt" << false; QTest::newRow("missingObject") << "missingObject.txt" << "missingObject.errors.txt" << false; + QTest::newRow("failingComponent") << "failingComponent.txt" << "failingComponent.errors.txt" << true; + QTest::newRow("missingSignal") << "missingSignal.txt" << "missingSignal.errors.txt" << true; } void tst_qmlparser::errors() @@ -525,6 +594,20 @@ void tst_qmlparser::assignBasicTypes() QCOMPARE(object->rectFProperty(), QRectF((float)1000.1, (float)-10.9, (float)400, (float)90.99)); QCOMPARE(object->boolProperty(), true); QCOMPARE(object->variantProperty(), QVariant("Hello World!")); + QVERIFY(object->objectProperty() != 0); + MyTypeObject *child = qobject_cast<MyTypeObject *>(object->objectProperty()); + QVERIFY(child != 0); + QCOMPARE(child->intProperty(), 8); +} + +// Test edge case type assignments +void tst_qmlparser::assignTypeExtremes() +{ + QmlComponent component(&engine, TEST_FILE("assignTypeExtremes.txt")); + MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create()); + QVERIFY(object != 0); + QCOMPARE(object->uintProperty(), 0xEE6B2800); + QCOMPARE(object->intProperty(), -0x77359400); } // Tests that custom parser tyeps can be instantiated @@ -560,6 +643,115 @@ void tst_qmlparser::inlineQmlComponents() QCOMPARE(compObject->value(), 11); } +// Tests that types that have an id property have it set +void tst_qmlparser::idProperty() +{ + QmlComponent component(&engine, TEST_FILE("idProperty.txt")); + MyContainer *object = qobject_cast<MyContainer *>(component.create()); + QVERIFY(object != 0); + QCOMPARE(object->children()->count(), 1); + MyTypeObject *child = + qobject_cast<MyTypeObject *>(object->children()->at(0)); + QVERIFY(child != 0); + QCOMPARE(child->id(), QString("MyObjectId")); + QCOMPARE(object->property("object"), QVariant::fromValue((QObject *)child)); +} + +// Tests that signals can be assigned to +void tst_qmlparser::assignSignal() +{ + QmlComponent component(&engine, TEST_FILE("assignSignal.txt")); + MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create()); + QVERIFY(object != 0); + QTest::ignoreMessage(QtWarningMsg, "MyQmlObject::basicSlot"); + emit object->basicSignal(); +} + +// Tests the creation and assignment of dynamic properties +void tst_qmlparser::dynamicProperties() +{ + QmlComponent component(&engine, TEST_FILE("dynamicProperties.txt")); + QObject *object = component.create(); + QVERIFY(object != 0); + QCOMPARE(object->property("intProperty"), QVariant(10)); + QCOMPARE(object->property("boolProperty"), QVariant(false)); + QCOMPARE(object->property("doubleProperty"), QVariant((float)-10.1)); + QCOMPARE(object->property("realProperty"), QVariant((float)-19.9)); + QCOMPARE(object->property("stringProperty"), QVariant("Hello World!")); + QCOMPARE(object->property("colorProperty"), QVariant(QColor("red"))); + QCOMPARE(object->property("dateProperty"), QVariant(QDate(1945, 9, 2))); + QCOMPARE(object->property("varProperty"), QVariant("Hello World!")); + QCOMPARE(object->property("variantProperty"), QVariant(12)); +} + +// Tests the declaration of dynamic signals and slots +void tst_qmlparser::dynamicSignalsAndSlots() +{ + QmlComponent component(&engine, TEST_FILE("dynamicSignalsAndSlots.txt")); + QObject *object = component.create(); + QVERIFY(object != 0); + QVERIFY(object->metaObject()->indexOfMethod("signal1()") != -1); + QVERIFY(object->metaObject()->indexOfMethod("signal2()") != -1); + QVERIFY(object->metaObject()->indexOfMethod("slot1()") != -1); + QVERIFY(object->metaObject()->indexOfMethod("slot2()") != -1); +} + +void tst_qmlparser::simpleBindings() +{ + QmlComponent component(&engine, TEST_FILE("simpleBindings.txt")); + QObject *object = component.create(); + QVERIFY(object != 0); + QCOMPARE(object->property("value1"), QVariant(10)); + QCOMPARE(object->property("value2"), QVariant(10)); + QCOMPARE(object->property("value3"), QVariant(21)); + QCOMPARE(object->property("value4"), QVariant(10)); + QCOMPARE(object->property("objectProperty"), QVariant::fromValue(object)); +} + +void tst_qmlparser::autoComponentCreation() +{ + QmlComponent component(&engine, TEST_FILE("autoComponentCreation.txt")); + MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create()); + QVERIFY(object != 0); + QVERIFY(object->componentProperty() != 0); + MyTypeObject *child = qobject_cast<MyTypeObject *>(object->componentProperty()->create()); + QVERIFY(child != 0); + QCOMPARE(child->realProperty(), qreal(9)); +} + +void tst_qmlparser::propertyValueSource() +{ + QmlComponent component(&engine, TEST_FILE("propertyValueSource.txt")); + MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create()); + QVERIFY(object != 0); + QList<QmlPropertyValueSource *> valueSources = + object->findChildren<QmlPropertyValueSource *>(); + QCOMPARE(valueSources.count(), 1); + MyPropertyValueSource *valueSource = + qobject_cast<MyPropertyValueSource *>(valueSources.at(0)); + QVERIFY(valueSource != 0); + QCOMPARE(valueSource->prop.object(), object); + QCOMPARE(valueSource->prop.name(), QString(QLatin1String("intProperty"))); +} + +void tst_qmlparser::attachedProperties() +{ + QmlComponent component(&engine, TEST_FILE("attachedProperties.txt")); + QObject *object = component.create(); + QVERIFY(object != 0); + QObject *attached = qmlAttachedPropertiesObject<MyQmlObject>(object); + QVERIFY(attached != 0); + QCOMPARE(attached->property("value"), QVariant(10)); +} + +// Tests non-static object properties +void tst_qmlparser::dynamicObjects() +{ + QmlComponent component(&engine, TEST_FILE("dynamicObject.1.txt")); + QObject *object = component.create(); + QVERIFY(object != 0); +} + void tst_qmlparser::crash1() { QmlComponent component(&engine, "Component {}"); |