summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAaron Kennedy <aaron.kennedy@nokia.com>2009-11-05 07:03:08 (GMT)
committerAaron Kennedy <aaron.kennedy@nokia.com>2009-11-05 07:03:08 (GMT)
commit8b40fac7e97a629ddaf1c0f6de2ebb504f004060 (patch)
treecdf3f3cc75f456980c3e06c2335ad74d6f4fa04f
parente1f01000ec127dd5a949eabf9b436f649597bf01 (diff)
downloadQt-8b40fac7e97a629ddaf1c0f6de2ebb504f004060.zip
Qt-8b40fac7e97a629ddaf1c0f6de2ebb504f004060.tar.gz
Qt-8b40fac7e97a629ddaf1c0f6de2ebb504f004060.tar.bz2
value types autotest
-rw-r--r--src/declarative/qml/qmlvaluetype_p.h20
-rw-r--r--tests/auto/declarative/valuetypes/testtypes.cpp4
-rw-r--r--tests/auto/declarative/valuetypes/testtypes.h40
-rw-r--r--tests/auto/declarative/valuetypes/tst_valuetypes.cpp207
4 files changed, 244 insertions, 27 deletions
diff --git a/src/declarative/qml/qmlvaluetype_p.h b/src/declarative/qml/qmlvaluetype_p.h
index 0af3813..e7566f9 100644
--- a/src/declarative/qml/qmlvaluetype_p.h
+++ b/src/declarative/qml/qmlvaluetype_p.h
@@ -62,7 +62,7 @@
QT_BEGIN_NAMESPACE
-class QmlValueType : public QObject
+class Q_AUTOTEST_EXPORT QmlValueType : public QObject
{
Q_OBJECT
public:
@@ -73,7 +73,7 @@ public:
virtual void setValue(QVariant) = 0;
};
-class QmlValueTypeFactory
+class Q_AUTOTEST_EXPORT QmlValueTypeFactory
{
public:
QmlValueTypeFactory();
@@ -84,7 +84,7 @@ public:
QmlValueType *operator[](int idx) const { return valueTypes[idx]; }
};
-class QmlPointFValueType : public QmlValueType
+class Q_AUTOTEST_EXPORT QmlPointFValueType : public QmlValueType
{
Q_PROPERTY(qreal x READ x WRITE setX)
Q_PROPERTY(qreal y READ y WRITE setY)
@@ -106,7 +106,7 @@ private:
QPointF point;
};
-class QmlPointValueType : public QmlValueType
+class Q_AUTOTEST_EXPORT QmlPointValueType : public QmlValueType
{
Q_PROPERTY(int x READ x WRITE setX)
Q_PROPERTY(int y READ y WRITE setY)
@@ -128,7 +128,7 @@ private:
QPoint point;
};
-class QmlSizeFValueType : public QmlValueType
+class Q_AUTOTEST_EXPORT QmlSizeFValueType : public QmlValueType
{
Q_PROPERTY(qreal width READ width WRITE setWidth)
Q_PROPERTY(qreal height READ height WRITE setHeight)
@@ -150,7 +150,7 @@ private:
QSizeF size;
};
-class QmlSizeValueType : public QmlValueType
+class Q_AUTOTEST_EXPORT QmlSizeValueType : public QmlValueType
{
Q_PROPERTY(int width READ width WRITE setWidth)
Q_PROPERTY(int height READ height WRITE setHeight)
@@ -172,7 +172,7 @@ private:
QSize size;
};
-class QmlRectFValueType : public QmlValueType
+class Q_AUTOTEST_EXPORT QmlRectFValueType : public QmlValueType
{
Q_PROPERTY(qreal x READ x WRITE setX)
Q_PROPERTY(qreal y READ y WRITE setY)
@@ -201,7 +201,7 @@ private:
QRectF rect;
};
-class QmlRectValueType : public QmlValueType
+class Q_AUTOTEST_EXPORT QmlRectValueType : public QmlValueType
{
Q_PROPERTY(int x READ x WRITE setX)
Q_PROPERTY(int y READ y WRITE setY)
@@ -230,7 +230,7 @@ private:
QRect rect;
};
-class QmlVector3DValueType : public QmlValueType
+class Q_AUTOTEST_EXPORT QmlVector3DValueType : public QmlValueType
{
Q_PROPERTY(qreal x READ x WRITE setX)
Q_PROPERTY(qreal y READ y WRITE setY)
@@ -255,7 +255,7 @@ private:
QVector3D vector;
};
-class QmlFontValueType : public QmlValueType
+class Q_AUTOTEST_EXPORT QmlFontValueType : public QmlValueType
{
Q_OBJECT
Q_ENUMS(FontWeight)
diff --git a/tests/auto/declarative/valuetypes/testtypes.cpp b/tests/auto/declarative/valuetypes/testtypes.cpp
index d57afaf..565eb1c 100644
--- a/tests/auto/declarative/valuetypes/testtypes.cpp
+++ b/tests/auto/declarative/valuetypes/testtypes.cpp
@@ -40,4 +40,6 @@
****************************************************************************/
#include "testtypes.h"
-QML_DEFINE_TYPE(Test, 1, 0, 0, MyTypeObject, MyTypeObject);
+QML_DEFINE_TYPE(Test, 1, 0, MyTypeObject, MyTypeObject);
+QML_DEFINE_TYPE(Test, 1, 0, MyConstantValueSource, MyConstantValueSource);
+QML_DEFINE_TYPE(Test, 1, 0, MyOffsetValueInterceptor, MyOffsetValueInterceptor);
diff --git a/tests/auto/declarative/valuetypes/testtypes.h b/tests/auto/declarative/valuetypes/testtypes.h
index cc98d7c..67e32f5 100644
--- a/tests/auto/declarative/valuetypes/testtypes.h
+++ b/tests/auto/declarative/valuetypes/testtypes.h
@@ -51,6 +51,7 @@
#include <QVector3D>
#include <QFont>
#include <qml.h>
+#include <QmlPropertyValueSource>
class MyTypeObject : public QObject
{
@@ -90,39 +91,62 @@ public:
QPoint m_point;
QPoint point() const { return m_point; }
- void setPoint(const QPoint &v) { m_point = v; }
+ void setPoint(const QPoint &v) { m_point = v; emit changed(); }
QPointF m_pointf;
QPointF pointf() const { return m_pointf; }
- void setPointf(const QPointF &v) { m_pointf = v; }
+ void setPointf(const QPointF &v) { m_pointf = v; emit changed(); }
QSize m_size;
QSize size() const { return m_size; }
- void setSize(const QSize &v) { m_size = v; }
+ void setSize(const QSize &v) { m_size = v; emit changed(); }
QSizeF m_sizef;
QSizeF sizef() const { return m_sizef; }
- void setSizef(const QSizeF &v) { m_sizef = v; }
+ void setSizef(const QSizeF &v) { m_sizef = v; emit changed(); }
QRect m_rect;
QRect rect() const { return m_rect; }
- void setRect(const QRect &v) { m_rect = v; }
+ void setRect(const QRect &v) { m_rect = v; emit changed(); }
QRectF m_rectf;
QRectF rectf() const { return m_rectf; }
- void setRectf(const QRectF &v) { m_rectf = v; }
+ void setRectf(const QRectF &v) { m_rectf = v; emit changed(); }
QVector3D m_vector;
QVector3D vector() const { return m_vector; }
- void setVector(const QVector3D &v) { m_vector = v; }
+ void setVector(const QVector3D &v) { m_vector = v; emit changed(); }
QFont m_font;
QFont font() const { return m_font; }
- void setFont(const QFont &v) { m_font = v; }
+ void setFont(const QFont &v) { m_font = v; emit changed(); }
+
+ void emitRunScript() { emit runScript(); }
signals:
void changed();
+ void runScript();
};
QML_DECLARE_TYPE(MyTypeObject);
+class MyConstantValueSource : public QObject, public QmlPropertyValueSource
+{
+ Q_OBJECT
+public:
+ virtual void setTarget(const QmlMetaProperty &p) { p.write(3345); }
+};
+QML_DECLARE_TYPE(MyConstantValueSource);
+
+class MyOffsetValueInterceptor : public QObject, public QmlPropertyValueInterceptor
+{
+ Q_OBJECT
+public:
+ virtual void setTarget(const QmlMetaProperty &p) { prop = p; }
+ virtual void write(const QVariant &value) { prop.write(value.toInt() + 13, QmlMetaProperty::BypassInterceptor); }
+
+private:
+ QmlMetaProperty prop;
+};
+QML_DECLARE_TYPE(MyOffsetValueInterceptor);
+
#endif // TESTTYPES_H
diff --git a/tests/auto/declarative/valuetypes/tst_valuetypes.cpp b/tests/auto/declarative/valuetypes/tst_valuetypes.cpp
index a338e47..d09bdf5 100644
--- a/tests/auto/declarative/valuetypes/tst_valuetypes.cpp
+++ b/tests/auto/declarative/valuetypes/tst_valuetypes.cpp
@@ -43,6 +43,7 @@
#include <QmlEngine>
#include <QmlComponent>
#include <QDebug>
+#include <private/qmlvaluetype_p.h>
#include "testtypes.h"
class tst_valuetypes : public QObject
@@ -61,14 +62,16 @@ private slots:
void vector3d();
void font();
- // ###
- // Test binding assignment
- // Test static assignment
- // Test JS assignment
- // Test "font.x: blah; font: blah2;" conflict
- // Test constant binding removal
- // Test value sources
- // Test behaviours
+ void bindingAssignment();
+ void bindingRead();
+ void staticAssignment();
+ void scriptAccess();
+ void autoBindingRemoval();
+ void valueSources();
+ void valueInterceptors();
+ void bindingConflict();
+ void cppClasses();
+
private:
QmlEngine engine;
};
@@ -312,6 +315,8 @@ void tst_valuetypes::font()
QVERIFY(object != 0);
QCOMPARE(object->font().pixelSize(), 10);
+
+ delete object;
}
// Test pixelSize and pointSize
@@ -322,9 +327,195 @@ void tst_valuetypes::font()
QVERIFY(object != 0);
QCOMPARE(object->font().pixelSize(), 10);
+
+ delete object;
}
}
+// Test bindings can write to value types
+void tst_valuetypes::bindingAssignment()
+{
+ QmlComponent component(&engine, TEST_FILE("bindingAssignment.qml"));
+ MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
+ QVERIFY(object != 0);
+
+ QCOMPARE(object->rect().x(), 10);
+
+ object->setProperty("value", QVariant(92));
+
+ QCOMPARE(object->rect().x(), 92);
+
+ delete object;
+}
+
+// Test bindings can read from value types
+void tst_valuetypes::bindingRead()
+{
+ QmlComponent component(&engine, TEST_FILE("bindingRead.qml"));
+ MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
+ QVERIFY(object != 0);
+
+ QCOMPARE(object->property("value").toInt(), 2);
+
+ object->setRect(QRect(19, 3, 88, 2));
+
+ QCOMPARE(object->property("value").toInt(), 19);
+
+ delete object;
+}
+
+// Test static values can assign to value types
+void tst_valuetypes::staticAssignment()
+{
+ QmlComponent component(&engine, TEST_FILE("staticAssignment.qml"));
+ MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
+ QVERIFY(object != 0);
+
+ QCOMPARE(object->rect().x(), 9);
+
+ delete object;
+}
+
+// Test scripts can read/write value types
+void tst_valuetypes::scriptAccess()
+{
+ QmlComponent component(&engine, TEST_FILE("scriptAccess.qml"));
+ MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
+ QVERIFY(object != 0);
+
+ QCOMPARE(object->property("valuePre").toInt(), 2);
+ QCOMPARE(object->rect().x(), 19);
+ QCOMPARE(object->property("valuePost").toInt(), 19);
+
+ delete object;
+}
+
+// Test that assigning a constant from script removes any binding
+void tst_valuetypes::autoBindingRemoval()
+{
+ {
+ QmlComponent component(&engine, TEST_FILE("autoBindingRemoval.qml"));
+ MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
+ QVERIFY(object != 0);
+
+ QCOMPARE(object->rect().x(), 10);
+
+ object->setProperty("value", QVariant(13));
+
+ QCOMPARE(object->rect().x(), 13);
+
+ object->emitRunScript();
+
+ QCOMPARE(object->rect().x(), 42);
+
+ object->setProperty("value", QVariant(92));
+
+ QCOMPARE(object->rect().x(), 42);
+
+ delete object;
+ }
+
+ {
+ QmlComponent component(&engine, TEST_FILE("autoBindingRemoval.2.qml"));
+ MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
+ QVERIFY(object != 0);
+
+ QCOMPARE(object->rect().x(), 10);
+
+ object->setProperty("value", QVariant(13));
+
+ QCOMPARE(object->rect().x(), 13);
+
+ object->emitRunScript();
+
+ QCOMPARE(object->rect(), QRect(10, 10, 10, 10));
+
+ object->setProperty("value", QVariant(92));
+
+ QCOMPARE(object->rect(), QRect(10, 10, 10, 10));
+
+ delete object;
+ }
+
+ {
+ QmlComponent component(&engine, TEST_FILE("autoBindingRemoval.3.qml"));
+ MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
+ QVERIFY(object != 0);
+
+ object->setProperty("value", QVariant(QRect(9, 22, 33, 44)));
+
+ QCOMPARE(object->rect(), QRect(9, 22, 33, 44));
+
+ object->emitRunScript();
+
+ QCOMPARE(object->rect(), QRect(44, 22, 33, 44));
+
+ object->setProperty("value", QVariant(QRect(19, 3, 4, 8)));
+
+ QCOMPARE(object->rect(), QRect(44, 22, 33, 44));
+
+ delete object;
+ }
+
+}
+
+// Test that property value sources assign to value types
+void tst_valuetypes::valueSources()
+{
+ QmlComponent component(&engine, TEST_FILE("valueSources.qml"));
+ MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
+ QVERIFY(object != 0);
+
+ QCOMPARE(object->rect().x(), 3345);
+
+ delete object;
+}
+
+// Test that property value interceptors can be applied to value types
+void tst_valuetypes::valueInterceptors()
+{
+ QmlComponent component(&engine, TEST_FILE("valueInterceptors.qml"));
+ MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
+ QVERIFY(object != 0);
+
+ QCOMPARE(object->rect().x(), 26);
+
+ object->setProperty("value", 99);
+
+ QCOMPARE(object->rect().x(), 112);
+
+ delete object;
+}
+
+// Test that you can't assign a binding to the "root" value type, and a sub-property
+void tst_valuetypes::bindingConflict()
+{
+ QmlComponent component(&engine, TEST_FILE("bindingConflict.qml"));
+ QCOMPARE(component.isError(), true);
+}
+
+#define CPP_TEST(type, v) \
+{ \
+ type *t = new type; \
+ QVariant value(v); \
+ t->setValue(value); \
+ QCOMPARE(t->value(), value); \
+ delete t; \
+}
+
+// Test that the value type classes can be used manually
+void tst_valuetypes::cppClasses()
+{
+ CPP_TEST(QmlPointValueType, QPoint(19, 33));
+ CPP_TEST(QmlPointFValueType, QPointF(33.6, -23));
+ CPP_TEST(QmlSizeValueType, QSize(-100, 18));
+ CPP_TEST(QmlSizeFValueType, QSizeF(-100.7, 18.2));
+ CPP_TEST(QmlRectValueType, QRect(13, 39, 10928, 88));
+ CPP_TEST(QmlRectFValueType, QRectF(88.2, -90.1, 103.2, 118));
+ CPP_TEST(QmlVector3DValueType, QVector3D(18.2, 19.7, 1002));
+ CPP_TEST(QmlFontValueType, QFont("Helvetica"));
+
+}
QTEST_MAIN(tst_valuetypes)
#include "tst_valuetypes.moc"