1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the test suite of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
43 #include <qdeclarative.h>
45 #include <private/qdeclarativemetatype_p.h>
46 #include <private/qdeclarativepropertyvalueinterceptor_p.h>
48 class tst_qdeclarativemetatype : public QObject
52 tst_qdeclarativemetatype() {}
57 void qmlParserStatusCast();
58 void qmlPropertyValueSourceCast();
59 void qmlPropertyValueInterceptorCast();
66 class TestType : public QObject
69 Q_PROPERTY(int foo READ foo)
71 Q_CLASSINFO("DefaultProperty", "foo")
73 int foo() { return 0; }
75 QML_DECLARE_TYPE(TestType);
77 class ParserStatusTestType : public QObject, public QDeclarativeParserStatus
81 void componentComplete(){}
82 Q_CLASSINFO("DefaultProperty", "foo") // Missing default property
83 Q_INTERFACES(QDeclarativeParserStatus)
85 QML_DECLARE_TYPE(ParserStatusTestType);
87 class ValueSourceTestType : public QObject, public QDeclarativePropertyValueSource
90 Q_INTERFACES(QDeclarativePropertyValueSource)
92 virtual void setTarget(const QDeclarativeProperty &) {}
94 QML_DECLARE_TYPE(ValueSourceTestType);
96 class ValueInterceptorTestType : public QObject, public QDeclarativePropertyValueInterceptor
99 Q_INTERFACES(QDeclarativePropertyValueInterceptor)
101 virtual void setTarget(const QDeclarativeProperty &) {}
102 virtual void write(const QVariant &) {}
104 QML_DECLARE_TYPE(ValueInterceptorTestType);
106 void tst_qdeclarativemetatype::initTestCase()
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");
114 void tst_qdeclarativemetatype::qmlParserStatusCast()
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);
122 QVERIFY(QDeclarativeMetaType::qmlType(qMetaTypeId<ParserStatusTestType *>()) != 0);
123 int cast = QDeclarativeMetaType::qmlType(qMetaTypeId<ParserStatusTestType *>())->parserStatusCast();
127 ParserStatusTestType t;
128 QVERIFY(reinterpret_cast<char *>((QObject *)&t) != reinterpret_cast<char *>((QDeclarativeParserStatus *)&t));
130 QDeclarativeParserStatus *status = reinterpret_cast<QDeclarativeParserStatus *>(reinterpret_cast<char *>((QObject *)&t) + cast);
131 QCOMPARE(status, (QDeclarativeParserStatus*)&t);
134 void tst_qdeclarativemetatype::qmlPropertyValueSourceCast()
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);
142 QVERIFY(QDeclarativeMetaType::qmlType(qMetaTypeId<ValueSourceTestType *>()) != 0);
143 int cast = QDeclarativeMetaType::qmlType(qMetaTypeId<ValueSourceTestType *>())->propertyValueSourceCast();
147 ValueSourceTestType t;
148 QVERIFY(reinterpret_cast<char *>((QObject *)&t) != reinterpret_cast<char *>((QDeclarativePropertyValueSource *)&t));
150 QDeclarativePropertyValueSource *source = reinterpret_cast<QDeclarativePropertyValueSource *>(reinterpret_cast<char *>((QObject *)&t) + cast);
151 QCOMPARE(source, (QDeclarativePropertyValueSource*)&t);
154 void tst_qdeclarativemetatype::qmlPropertyValueInterceptorCast()
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);
162 QVERIFY(QDeclarativeMetaType::qmlType(qMetaTypeId<ValueInterceptorTestType *>()) != 0);
163 int cast = QDeclarativeMetaType::qmlType(qMetaTypeId<ValueInterceptorTestType *>())->propertyValueInterceptorCast();
167 ValueInterceptorTestType t;
168 QVERIFY(reinterpret_cast<char *>((QObject *)&t) != reinterpret_cast<char *>((QDeclarativePropertyValueInterceptor *)&t));
170 QDeclarativePropertyValueInterceptor *interceptor = reinterpret_cast<QDeclarativePropertyValueInterceptor *>(reinterpret_cast<char *>((QObject *)&t) + cast);
171 QCOMPARE(interceptor, (QDeclarativePropertyValueInterceptor*)&t);
174 void tst_qdeclarativemetatype::isList()
176 QCOMPARE(QDeclarativeMetaType::isList(QVariant::Invalid), false);
177 QCOMPARE(QDeclarativeMetaType::isList(QVariant::Int), false);
179 QDeclarativeListProperty<TestType> list;
181 QCOMPARE(QDeclarativeMetaType::isList(qMetaTypeId<QDeclarativeListProperty<TestType> >()), true);
184 void tst_qdeclarativemetatype::defaultObject()
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"));
192 ParserStatusTestType p;
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"));
200 QTEST_MAIN(tst_qdeclarativemetatype)
202 #include "tst_qdeclarativemetatype.moc"