Refactor singleton type registration code
[profile/ivi/qtdeclarative.git] / tests / auto / qml / qqmlmetatype / tst_qqmlmetatype.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <qtest.h>
43 #include <qqml.h>
44
45 #include <private/qqmlmetatype_p.h>
46 #include <private/qqmlpropertyvalueinterceptor_p.h>
47 #include <private/qhashedstring_p.h>
48
49 class tst_qqmlmetatype : public QObject
50 {
51     Q_OBJECT
52 public:
53     tst_qqmlmetatype() {}
54
55 private slots:
56     void initTestCase();
57
58     void qmlParserStatusCast();
59     void qmlPropertyValueSourceCast();
60     void qmlPropertyValueInterceptorCast();
61     void qmlType();
62     void invalidQmlTypeName();
63
64     void isList();
65
66     void defaultObject();
67 };
68
69 class TestType : public QObject
70 {
71     Q_OBJECT
72     Q_PROPERTY(int foo READ foo)
73
74     Q_CLASSINFO("DefaultProperty", "foo")
75 public:
76     int foo() { return 0; }
77 };
78 QML_DECLARE_TYPE(TestType);
79
80 class ParserStatusTestType : public QObject, public QQmlParserStatus
81 {
82     Q_OBJECT
83     void classBegin(){}
84     void componentComplete(){}
85     Q_CLASSINFO("DefaultProperty", "foo") // Missing default property
86     Q_INTERFACES(QQmlParserStatus)
87 };
88 QML_DECLARE_TYPE(ParserStatusTestType);
89
90 class ValueSourceTestType : public QObject, public QQmlPropertyValueSource
91 {
92     Q_OBJECT
93     Q_INTERFACES(QQmlPropertyValueSource)
94 public:
95     virtual void setTarget(const QQmlProperty &) {}
96 };
97 QML_DECLARE_TYPE(ValueSourceTestType);
98
99 class ValueInterceptorTestType : public QObject, public QQmlPropertyValueInterceptor
100 {
101     Q_OBJECT
102     Q_INTERFACES(QQmlPropertyValueInterceptor)
103 public:
104     virtual void setTarget(const QQmlProperty &) {}
105     virtual void write(const QVariant &) {}
106 };
107 QML_DECLARE_TYPE(ValueInterceptorTestType);
108
109 void tst_qqmlmetatype::initTestCase()
110 {
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");
115 }
116
117 void tst_qqmlmetatype::qmlParserStatusCast()
118 {
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);
124             
125     QVERIFY(QQmlMetaType::qmlType(qMetaTypeId<ParserStatusTestType *>()) != 0);
126     int cast = QQmlMetaType::qmlType(qMetaTypeId<ParserStatusTestType *>())->parserStatusCast();
127     QVERIFY(cast != -1);
128     QVERIFY(cast != 0);
129
130     ParserStatusTestType t;
131     QVERIFY(reinterpret_cast<char *>((QObject *)&t) != reinterpret_cast<char *>((QQmlParserStatus *)&t));
132
133     QQmlParserStatus *status = reinterpret_cast<QQmlParserStatus *>(reinterpret_cast<char *>((QObject *)&t) + cast);
134     QCOMPARE(status, (QQmlParserStatus*)&t);
135 }
136
137 void tst_qqmlmetatype::qmlPropertyValueSourceCast()
138 {
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);
144             
145     QVERIFY(QQmlMetaType::qmlType(qMetaTypeId<ValueSourceTestType *>()) != 0);
146     int cast = QQmlMetaType::qmlType(qMetaTypeId<ValueSourceTestType *>())->propertyValueSourceCast();
147     QVERIFY(cast != -1);
148     QVERIFY(cast != 0);
149
150     ValueSourceTestType t;
151     QVERIFY(reinterpret_cast<char *>((QObject *)&t) != reinterpret_cast<char *>((QQmlPropertyValueSource *)&t));
152
153     QQmlPropertyValueSource *source = reinterpret_cast<QQmlPropertyValueSource *>(reinterpret_cast<char *>((QObject *)&t) + cast);
154     QCOMPARE(source, (QQmlPropertyValueSource*)&t);
155 }
156
157 void tst_qqmlmetatype::qmlPropertyValueInterceptorCast()
158 {
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);
164             
165     QVERIFY(QQmlMetaType::qmlType(qMetaTypeId<ValueInterceptorTestType *>()) != 0);
166     int cast = QQmlMetaType::qmlType(qMetaTypeId<ValueInterceptorTestType *>())->propertyValueInterceptorCast();
167     QVERIFY(cast != -1);
168     QVERIFY(cast != 0);
169
170     ValueInterceptorTestType t;
171     QVERIFY(reinterpret_cast<char *>((QObject *)&t) != reinterpret_cast<char *>((QQmlPropertyValueInterceptor *)&t));
172
173     QQmlPropertyValueInterceptor *interceptor = reinterpret_cast<QQmlPropertyValueInterceptor *>(reinterpret_cast<char *>((QObject *)&t) + cast);
174     QCOMPARE(interceptor, (QQmlPropertyValueInterceptor*)&t);
175 }
176
177 void tst_qqmlmetatype::qmlType()
178 {
179     QQmlType *type = QQmlMetaType::qmlType(QString("ParserStatusTestType"), QString("Test"), 1, 0);
180     QVERIFY(type);
181     QVERIFY(type->module() == QLatin1String("Test"));
182     QVERIFY(type->elementName() == QLatin1String("ParserStatusTestType"));
183     QCOMPARE(type->qmlTypeName(), QLatin1String("Test/ParserStatusTestType"));
184
185     type = QQmlMetaType::qmlType("Test/ParserStatusTestType", 1, 0);
186     QVERIFY(type);
187     QVERIFY(type->module() == QLatin1String("Test"));
188     QVERIFY(type->elementName() == QLatin1String("ParserStatusTestType"));
189     QCOMPARE(type->qmlTypeName(), QLatin1String("Test/ParserStatusTestType"));
190 }
191
192 void tst_qqmlmetatype::invalidQmlTypeName()
193 {
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();
197
198     foreach (const QString &f, currFailures)
199         nowFailures.removeOne(f);
200
201     QCOMPARE(nowFailures.size(), 1);
202     QCOMPARE(nowFailures.at(0), QStringLiteral("Invalid QML element name \"Test$Type\""));
203 }
204
205 void tst_qqmlmetatype::isList()
206 {
207     QCOMPARE(QQmlMetaType::isList(QVariant::Invalid), false);
208     QCOMPARE(QQmlMetaType::isList(QVariant::Int), false);
209
210     QQmlListProperty<TestType> list;
211
212     QCOMPARE(QQmlMetaType::isList(qMetaTypeId<QQmlListProperty<TestType> >()), true);
213 }
214
215 void tst_qqmlmetatype::defaultObject()
216 {
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"));
220
221     QObject o;
222     TestType t;
223     ParserStatusTestType p;
224
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"));
229 }
230
231 QTEST_MAIN(tst_qqmlmetatype)
232
233 #include "tst_qqmlmetatype.moc"