summaryrefslogtreecommitdiffstats
path: root/tests/auto/declarative/qmlmetaproperty/tst_qmlmetaproperty.cpp
blob: 65d061e2aea7f05c19a5e6f5f69e908a5e2a115b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <qtest.h>
#include <QtDeclarative/qmlengine.h>
#include <QtDeclarative/qmlcomponent.h>
#include <QtDeclarative/qmlmetaproperty.h>
#include <QtGui/QLineEdit>

class MyQmlObject : public QObject
{
    Q_OBJECT
public:
    MyQmlObject() {}
};

QML_DECLARE_TYPE(MyQmlObject);
QML_DEFINE_TYPE(Test,1,0,0,MyQmlObject,MyQmlObject);

class MyContainer : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QList<MyQmlObject*>* children READ children)
    Q_PROPERTY(QmlList<MyQmlObject*>* qmlChildren READ qmlChildren)
public:
    MyContainer() {}

    QList<MyQmlObject*> *children() { return &m_children; }
    QmlConcreteList<MyQmlObject *> *qmlChildren() { return &m_qmlChildren; }

private:
    QList<MyQmlObject*> m_children;
    QmlConcreteList<MyQmlObject *> m_qmlChildren;
};

QML_DECLARE_TYPE(MyContainer);
QML_DEFINE_TYPE(Test,1,0,0,MyContainer,MyContainer);

class tst_QmlMetaProperty : public QObject
{
    Q_OBJECT
public:
    tst_QmlMetaProperty() {}

private slots:
    void writeObjectToList();
    void writeListToList();
    void writeObjectToQmlList();

    //writeToReadOnly();

private:
    QmlEngine engine;
};

void tst_QmlMetaProperty::writeObjectToList()
{
    QmlComponent containerComponent(&engine, "import Test 1.0\nMyContainer { children: MyQmlObject {} }", QUrl());
    MyContainer *container = qobject_cast<MyContainer*>(containerComponent.create());
    QVERIFY(container != 0);
    QVERIFY(container->children()->size() == 1);

    MyQmlObject *object = new MyQmlObject;
    QmlMetaProperty prop(container, "children");
    prop.write(qVariantFromValue(object));
    QCOMPARE(container->children()->size(), 2);
    QCOMPARE(container->children()->at(1), object);
}

Q_DECLARE_METATYPE(QList<QObject *>);
void tst_QmlMetaProperty::writeListToList()
{
    QmlComponent containerComponent(&engine, "import Test 1.0\nMyContainer { children: MyQmlObject {} }", QUrl());
    MyContainer *container = qobject_cast<MyContainer*>(containerComponent.create());
    QVERIFY(container != 0);
    QVERIFY(container->children()->size() == 1);

    QList<QObject*> objList;
    objList << new MyQmlObject() << new MyQmlObject() << new MyQmlObject() << new MyQmlObject();
    QmlMetaProperty prop(container, "children");
    prop.write(qVariantFromValue(objList));
    QCOMPARE(container->children()->size(), 4);

    //XXX need to try this with read/write prop (for read-only it correctly doesn't write)
    /*QList<MyQmlObject*> typedObjList;
    typedObjList << new MyQmlObject();
    prop.write(qVariantFromValue(&typedObjList));
    QCOMPARE(container->children()->size(), 1);*/
}

void tst_QmlMetaProperty::writeObjectToQmlList()
{
    QmlComponent containerComponent(&engine, "import Test 1.0\nMyContainer { qmlChildren: MyQmlObject {} }", QUrl());
    MyContainer *container = qobject_cast<MyContainer*>(containerComponent.create());
    QVERIFY(container != 0);
    QVERIFY(container->qmlChildren()->size() == 1);

    MyQmlObject *object = new MyQmlObject;
    QmlMetaProperty prop(container, "qmlChildren");
    prop.write(qVariantFromValue(object));
    QCOMPARE(container->qmlChildren()->size(), 2);
    QCOMPARE(container->qmlChildren()->at(1), object);
}

QTEST_MAIN(tst_QmlMetaProperty)

#include "tst_qmlmetaproperty.moc"