b4daebd63b06ac36bea15428931ace1c3321d818
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / qdeclarativemetatype / tst_qdeclarativemetatype.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
6 **
7 ** This file is part of the test suite of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <qtest.h>
43 #include <qdeclarative.h>
44
45 #include <private/qdeclarativemetatype_p.h>
46 #include <private/qdeclarativepropertyvalueinterceptor_p.h>
47
48 class tst_qdeclarativemetatype : public QObject
49 {
50     Q_OBJECT
51 public:
52     tst_qdeclarativemetatype() {}
53
54 private slots:
55     void initTestCase();
56
57     void qmlParserStatusCast();
58     void qmlPropertyValueSourceCast();
59     void qmlPropertyValueInterceptorCast();
60
61     void isList();
62
63     void defaultObject();
64 };
65
66 class TestType : public QObject
67 {
68     Q_OBJECT
69     Q_PROPERTY(int foo READ foo)
70
71     Q_CLASSINFO("DefaultProperty", "foo")
72 public:
73     int foo() { return 0; }
74 };
75 QML_DECLARE_TYPE(TestType);
76
77 class ParserStatusTestType : public QObject, public QDeclarativeParserStatus
78 {
79     Q_OBJECT
80     void classBegin(){}
81     void componentComplete(){}
82     Q_CLASSINFO("DefaultProperty", "foo") // Missing default property
83     Q_INTERFACES(QDeclarativeParserStatus)
84 };
85 QML_DECLARE_TYPE(ParserStatusTestType);
86
87 class ValueSourceTestType : public QObject, public QDeclarativePropertyValueSource
88 {
89     Q_OBJECT
90     Q_INTERFACES(QDeclarativePropertyValueSource)
91 public:
92     virtual void setTarget(const QDeclarativeProperty &) {}
93 };
94 QML_DECLARE_TYPE(ValueSourceTestType);
95
96 class ValueInterceptorTestType : public QObject, public QDeclarativePropertyValueInterceptor
97 {
98     Q_OBJECT
99     Q_INTERFACES(QDeclarativePropertyValueInterceptor)
100 public:
101     virtual void setTarget(const QDeclarativeProperty &) {}
102     virtual void write(const QVariant &) {}
103 };
104 QML_DECLARE_TYPE(ValueInterceptorTestType);
105
106 void tst_qdeclarativemetatype::initTestCase()
107 {
108     qmlRegisterType<TestType>("Test", 1, 0, "TestType");
109     qmlRegisterType<ParserStatusTestType>("Test", 1, 0, "ParserStatusTestType");
110     qmlRegisterType<ValueSourceTestType>("Test", 1, 0, "ValueSourceTestType");
111     qmlRegisterType<ValueInterceptorTestType>("Test", 1, 0, "ValueInterceptorTestType");
112 }
113
114 void tst_qdeclarativemetatype::qmlParserStatusCast()
115 {
116     QVERIFY(QDeclarativeMetaType::qmlType(QVariant::Int) == 0);
117     QVERIFY(QDeclarativeMetaType::qmlType(qMetaTypeId<TestType *>()) != 0);
118     QCOMPARE(QDeclarativeMetaType::qmlType(qMetaTypeId<TestType *>())->parserStatusCast(), -1);
119     QVERIFY(QDeclarativeMetaType::qmlType(qMetaTypeId<ValueSourceTestType *>()) != 0);
120     QCOMPARE(QDeclarativeMetaType::qmlType(qMetaTypeId<ValueSourceTestType *>())->parserStatusCast(), -1);
121             
122     QVERIFY(QDeclarativeMetaType::qmlType(qMetaTypeId<ParserStatusTestType *>()) != 0);
123     int cast = QDeclarativeMetaType::qmlType(qMetaTypeId<ParserStatusTestType *>())->parserStatusCast();
124     QVERIFY(cast != -1);
125     QVERIFY(cast != 0);
126
127     ParserStatusTestType t;
128     QVERIFY(reinterpret_cast<char *>((QObject *)&t) != reinterpret_cast<char *>((QDeclarativeParserStatus *)&t));
129
130     QDeclarativeParserStatus *status = reinterpret_cast<QDeclarativeParserStatus *>(reinterpret_cast<char *>((QObject *)&t) + cast);
131     QCOMPARE(status, (QDeclarativeParserStatus*)&t);
132 }
133
134 void tst_qdeclarativemetatype::qmlPropertyValueSourceCast()
135 {
136     QVERIFY(QDeclarativeMetaType::qmlType(QVariant::Int) == 0);
137     QVERIFY(QDeclarativeMetaType::qmlType(qMetaTypeId<TestType *>()) != 0);
138     QCOMPARE(QDeclarativeMetaType::qmlType(qMetaTypeId<TestType *>())->propertyValueSourceCast(), -1);
139     QVERIFY(QDeclarativeMetaType::qmlType(qMetaTypeId<ParserStatusTestType *>()) != 0);
140     QCOMPARE(QDeclarativeMetaType::qmlType(qMetaTypeId<ParserStatusTestType *>())->propertyValueSourceCast(), -1);
141             
142     QVERIFY(QDeclarativeMetaType::qmlType(qMetaTypeId<ValueSourceTestType *>()) != 0);
143     int cast = QDeclarativeMetaType::qmlType(qMetaTypeId<ValueSourceTestType *>())->propertyValueSourceCast();
144     QVERIFY(cast != -1);
145     QVERIFY(cast != 0);
146
147     ValueSourceTestType t;
148     QVERIFY(reinterpret_cast<char *>((QObject *)&t) != reinterpret_cast<char *>((QDeclarativePropertyValueSource *)&t));
149
150     QDeclarativePropertyValueSource *source = reinterpret_cast<QDeclarativePropertyValueSource *>(reinterpret_cast<char *>((QObject *)&t) + cast);
151     QCOMPARE(source, (QDeclarativePropertyValueSource*)&t);
152 }
153
154 void tst_qdeclarativemetatype::qmlPropertyValueInterceptorCast()
155 {
156     QVERIFY(QDeclarativeMetaType::qmlType(QVariant::Int) == 0);
157     QVERIFY(QDeclarativeMetaType::qmlType(qMetaTypeId<TestType *>()) != 0);
158     QCOMPARE(QDeclarativeMetaType::qmlType(qMetaTypeId<TestType *>())->propertyValueInterceptorCast(), -1);
159     QVERIFY(QDeclarativeMetaType::qmlType(qMetaTypeId<ParserStatusTestType *>()) != 0);
160     QCOMPARE(QDeclarativeMetaType::qmlType(qMetaTypeId<ParserStatusTestType *>())->propertyValueInterceptorCast(), -1);
161             
162     QVERIFY(QDeclarativeMetaType::qmlType(qMetaTypeId<ValueInterceptorTestType *>()) != 0);
163     int cast = QDeclarativeMetaType::qmlType(qMetaTypeId<ValueInterceptorTestType *>())->propertyValueInterceptorCast();
164     QVERIFY(cast != -1);
165     QVERIFY(cast != 0);
166
167     ValueInterceptorTestType t;
168     QVERIFY(reinterpret_cast<char *>((QObject *)&t) != reinterpret_cast<char *>((QDeclarativePropertyValueInterceptor *)&t));
169
170     QDeclarativePropertyValueInterceptor *interceptor = reinterpret_cast<QDeclarativePropertyValueInterceptor *>(reinterpret_cast<char *>((QObject *)&t) + cast);
171     QCOMPARE(interceptor, (QDeclarativePropertyValueInterceptor*)&t);
172 }
173
174 void tst_qdeclarativemetatype::isList()
175 {
176     QCOMPARE(QDeclarativeMetaType::isList(QVariant::Invalid), false);
177     QCOMPARE(QDeclarativeMetaType::isList(QVariant::Int), false);
178
179     QDeclarativeListProperty<TestType> list;
180
181     QCOMPARE(QDeclarativeMetaType::isList(qMetaTypeId<QDeclarativeListProperty<TestType> >()), true);
182 }
183
184 void tst_qdeclarativemetatype::defaultObject()
185 {
186     QVERIFY(QDeclarativeMetaType::defaultProperty(&QObject::staticMetaObject).name() == 0);
187     QVERIFY(QDeclarativeMetaType::defaultProperty(&ParserStatusTestType::staticMetaObject).name() == 0);
188     QCOMPARE(QString(QDeclarativeMetaType::defaultProperty(&TestType::staticMetaObject).name()), QString("foo"));
189
190     QObject o;
191     TestType t;
192     ParserStatusTestType p;
193
194     QVERIFY(QDeclarativeMetaType::defaultProperty((QObject *)0).name() == 0);
195     QVERIFY(QDeclarativeMetaType::defaultProperty(&o).name() == 0);
196     QVERIFY(QDeclarativeMetaType::defaultProperty(&p).name() == 0);
197     QCOMPARE(QString(QDeclarativeMetaType::defaultProperty(&t).name()), QString("foo"));
198 }
199
200 QTEST_MAIN(tst_qdeclarativemetatype)
201
202 #include "tst_qdeclarativemetatype.moc"