1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
45 #include <private/qqmlmetatype_p.h>
46 #include <private/qqmlpropertyvalueinterceptor_p.h>
47 #include <private/qhashedstring_p.h>
49 class tst_qqmlmetatype : public QObject
58 void qmlParserStatusCast();
59 void qmlPropertyValueSourceCast();
60 void qmlPropertyValueInterceptorCast();
62 void invalidQmlTypeName();
69 class TestType : public QObject
72 Q_PROPERTY(int foo READ foo)
74 Q_CLASSINFO("DefaultProperty", "foo")
76 int foo() { return 0; }
78 QML_DECLARE_TYPE(TestType);
80 class ParserStatusTestType : public QObject, public QQmlParserStatus
84 void componentComplete(){}
85 Q_CLASSINFO("DefaultProperty", "foo") // Missing default property
86 Q_INTERFACES(QQmlParserStatus)
88 QML_DECLARE_TYPE(ParserStatusTestType);
90 class ValueSourceTestType : public QObject, public QQmlPropertyValueSource
93 Q_INTERFACES(QQmlPropertyValueSource)
95 virtual void setTarget(const QQmlProperty &) {}
97 QML_DECLARE_TYPE(ValueSourceTestType);
99 class ValueInterceptorTestType : public QObject, public QQmlPropertyValueInterceptor
102 Q_INTERFACES(QQmlPropertyValueInterceptor)
104 virtual void setTarget(const QQmlProperty &) {}
105 virtual void write(const QVariant &) {}
107 QML_DECLARE_TYPE(ValueInterceptorTestType);
109 void tst_qqmlmetatype::initTestCase()
111 qmlRegisterType<TestType>("Test", 1, 0, "TestType");
112 qmlRegisterType<ParserStatusTestType>("Test", 1, 0, "ParserStatusTestType");
113 qmlRegisterType<ValueSourceTestType>("Test", 1, 0, "ValueSourceTestType");
114 qmlRegisterType<ValueInterceptorTestType>("Test", 1, 0, "ValueInterceptorTestType");
117 void tst_qqmlmetatype::qmlParserStatusCast()
119 QVERIFY(QQmlMetaType::qmlType(QVariant::Int) == 0);
120 QVERIFY(QQmlMetaType::qmlType(qMetaTypeId<TestType *>()) != 0);
121 QCOMPARE(QQmlMetaType::qmlType(qMetaTypeId<TestType *>())->parserStatusCast(), -1);
122 QVERIFY(QQmlMetaType::qmlType(qMetaTypeId<ValueSourceTestType *>()) != 0);
123 QCOMPARE(QQmlMetaType::qmlType(qMetaTypeId<ValueSourceTestType *>())->parserStatusCast(), -1);
125 QVERIFY(QQmlMetaType::qmlType(qMetaTypeId<ParserStatusTestType *>()) != 0);
126 int cast = QQmlMetaType::qmlType(qMetaTypeId<ParserStatusTestType *>())->parserStatusCast();
130 ParserStatusTestType t;
131 QVERIFY(reinterpret_cast<char *>((QObject *)&t) != reinterpret_cast<char *>((QQmlParserStatus *)&t));
133 QQmlParserStatus *status = reinterpret_cast<QQmlParserStatus *>(reinterpret_cast<char *>((QObject *)&t) + cast);
134 QCOMPARE(status, (QQmlParserStatus*)&t);
137 void tst_qqmlmetatype::qmlPropertyValueSourceCast()
139 QVERIFY(QQmlMetaType::qmlType(QVariant::Int) == 0);
140 QVERIFY(QQmlMetaType::qmlType(qMetaTypeId<TestType *>()) != 0);
141 QCOMPARE(QQmlMetaType::qmlType(qMetaTypeId<TestType *>())->propertyValueSourceCast(), -1);
142 QVERIFY(QQmlMetaType::qmlType(qMetaTypeId<ParserStatusTestType *>()) != 0);
143 QCOMPARE(QQmlMetaType::qmlType(qMetaTypeId<ParserStatusTestType *>())->propertyValueSourceCast(), -1);
145 QVERIFY(QQmlMetaType::qmlType(qMetaTypeId<ValueSourceTestType *>()) != 0);
146 int cast = QQmlMetaType::qmlType(qMetaTypeId<ValueSourceTestType *>())->propertyValueSourceCast();
150 ValueSourceTestType t;
151 QVERIFY(reinterpret_cast<char *>((QObject *)&t) != reinterpret_cast<char *>((QQmlPropertyValueSource *)&t));
153 QQmlPropertyValueSource *source = reinterpret_cast<QQmlPropertyValueSource *>(reinterpret_cast<char *>((QObject *)&t) + cast);
154 QCOMPARE(source, (QQmlPropertyValueSource*)&t);
157 void tst_qqmlmetatype::qmlPropertyValueInterceptorCast()
159 QVERIFY(QQmlMetaType::qmlType(QVariant::Int) == 0);
160 QVERIFY(QQmlMetaType::qmlType(qMetaTypeId<TestType *>()) != 0);
161 QCOMPARE(QQmlMetaType::qmlType(qMetaTypeId<TestType *>())->propertyValueInterceptorCast(), -1);
162 QVERIFY(QQmlMetaType::qmlType(qMetaTypeId<ParserStatusTestType *>()) != 0);
163 QCOMPARE(QQmlMetaType::qmlType(qMetaTypeId<ParserStatusTestType *>())->propertyValueInterceptorCast(), -1);
165 QVERIFY(QQmlMetaType::qmlType(qMetaTypeId<ValueInterceptorTestType *>()) != 0);
166 int cast = QQmlMetaType::qmlType(qMetaTypeId<ValueInterceptorTestType *>())->propertyValueInterceptorCast();
170 ValueInterceptorTestType t;
171 QVERIFY(reinterpret_cast<char *>((QObject *)&t) != reinterpret_cast<char *>((QQmlPropertyValueInterceptor *)&t));
173 QQmlPropertyValueInterceptor *interceptor = reinterpret_cast<QQmlPropertyValueInterceptor *>(reinterpret_cast<char *>((QObject *)&t) + cast);
174 QCOMPARE(interceptor, (QQmlPropertyValueInterceptor*)&t);
177 void tst_qqmlmetatype::qmlType()
179 QQmlType *type = QQmlMetaType::qmlType(QString("ParserStatusTestType"), QString("Test"), 1, 0);
181 QVERIFY(type->module() == QLatin1String("Test"));
182 QVERIFY(type->elementName() == QLatin1String("ParserStatusTestType"));
183 QCOMPARE(type->qmlTypeName(), QLatin1String("Test/ParserStatusTestType"));
185 type = QQmlMetaType::qmlType("Test/ParserStatusTestType", 1, 0);
187 QVERIFY(type->module() == QLatin1String("Test"));
188 QVERIFY(type->elementName() == QLatin1String("ParserStatusTestType"));
189 QCOMPARE(type->qmlTypeName(), QLatin1String("Test/ParserStatusTestType"));
192 void tst_qqmlmetatype::invalidQmlTypeName()
194 QStringList currFailures = QQmlMetaType::typeRegistrationFailures();
195 QCOMPARE(qmlRegisterType<TestType>("TestNamespace", 1, 0, "Test$Type"), -1); // should fail due to invalid QML type name.
196 QStringList nowFailures = QQmlMetaType::typeRegistrationFailures();
198 foreach (const QString &f, currFailures)
199 nowFailures.removeOne(f);
201 QCOMPARE(nowFailures.size(), 1);
202 QCOMPARE(nowFailures.at(0), QStringLiteral("Invalid QML element name \"Test$Type\""));
205 void tst_qqmlmetatype::isList()
207 QCOMPARE(QQmlMetaType::isList(QVariant::Invalid), false);
208 QCOMPARE(QQmlMetaType::isList(QVariant::Int), false);
210 QQmlListProperty<TestType> list;
212 QCOMPARE(QQmlMetaType::isList(qMetaTypeId<QQmlListProperty<TestType> >()), true);
215 void tst_qqmlmetatype::defaultObject()
217 QVERIFY(QQmlMetaType::defaultProperty(&QObject::staticMetaObject).name() == 0);
218 QVERIFY(QQmlMetaType::defaultProperty(&ParserStatusTestType::staticMetaObject).name() == 0);
219 QCOMPARE(QString(QQmlMetaType::defaultProperty(&TestType::staticMetaObject).name()), QString("foo"));
223 ParserStatusTestType p;
225 QVERIFY(QQmlMetaType::defaultProperty((QObject *)0).name() == 0);
226 QVERIFY(QQmlMetaType::defaultProperty(&o).name() == 0);
227 QVERIFY(QQmlMetaType::defaultProperty(&p).name() == 0);
228 QCOMPARE(QString(QQmlMetaType::defaultProperty(&t).name()), QString("foo"));
231 QTEST_MAIN(tst_qqmlmetatype)
233 #include "tst_qqmlmetatype.moc"