Initial import from the monolithic Qt.
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / qdeclarativemetatype / tst_qdeclarativemetatype.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the test suite of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <qtest.h>
43 #include <QLocale>
44 #include <QPixmap>
45 #include <QBitmap>
46 #include <QPen>
47 #include <QTextLength>
48 #include <QMatrix4x4>
49 #include <QVector2D>
50 #include <QVector3D>
51 #include <QVector4D>
52 #include <QQuaternion>
53 #include <qdeclarative.h>
54
55 #include <private/qdeclarativemetatype_p.h>
56
57 #ifdef Q_OS_SYMBIAN
58 // In Symbian OS test data is located in applications private dir
59 #define SRCDIR "."
60 #endif
61
62 class tst_qdeclarativemetatype : public QObject
63 {
64     Q_OBJECT
65 public:
66     tst_qdeclarativemetatype() {}
67
68 private slots:
69     void initTestCase();
70
71     void copy();
72
73     void qmlParserStatusCast();
74     void qmlPropertyValueSourceCast();
75     void qmlPropertyValueInterceptorCast();
76
77     void isList();
78
79     void defaultObject();
80 };
81
82 class TestType : public QObject
83 {
84     Q_OBJECT
85     Q_PROPERTY(int foo READ foo)
86
87     Q_CLASSINFO("DefaultProperty", "foo")
88 public:
89     int foo() { return 0; }
90 };
91 QML_DECLARE_TYPE(TestType);
92
93 class ParserStatusTestType : public QObject, public QDeclarativeParserStatus
94 {
95     Q_OBJECT
96     void classBegin(){}
97     void componentComplete(){}
98     Q_CLASSINFO("DefaultProperty", "foo") // Missing default property
99     Q_INTERFACES(QDeclarativeParserStatus)
100 };
101 QML_DECLARE_TYPE(ParserStatusTestType);
102
103 class ValueSourceTestType : public QObject, public QDeclarativePropertyValueSource
104 {
105     Q_OBJECT
106     Q_INTERFACES(QDeclarativePropertyValueSource)
107 public:
108     virtual void setTarget(const QDeclarativeProperty &) {}
109 };
110 QML_DECLARE_TYPE(ValueSourceTestType);
111
112 class ValueInterceptorTestType : public QObject, public QDeclarativePropertyValueInterceptor
113 {
114     Q_OBJECT
115     Q_INTERFACES(QDeclarativePropertyValueInterceptor)
116 public:
117     virtual void setTarget(const QDeclarativeProperty &) {}
118     virtual void write(const QVariant &) {}
119 };
120 QML_DECLARE_TYPE(ValueInterceptorTestType);
121
122
123 #define COPY_TEST(cpptype, metatype, value, defaultvalue) \
124 { \
125     cpptype v = (value); cpptype v2 = (value); \
126     QVERIFY(QDeclarativeMetaType::copy(QMetaType:: metatype, &v, 0)); \
127     QCOMPARE((cpptype)(v),(cpptype)(defaultvalue)); \
128     QVERIFY(v == (defaultvalue)); \
129     QVERIFY(QDeclarativeMetaType::copy(QMetaType:: metatype, &v, &v2)); \
130     QVERIFY(v == (value)); \
131 }
132
133 #define QT_COPY_TEST(type, value) \
134 { \
135     type v = (value); type v2 = (value); \
136     QVERIFY(QDeclarativeMetaType::copy(QMetaType:: type, &v, 0)); \
137     QVERIFY(v == (type ())); \
138     QVERIFY(QDeclarativeMetaType::copy(QMetaType:: type, &v, &v2)); \
139     QVERIFY(v == (value)); \
140 }
141
142 void tst_qdeclarativemetatype::initTestCase()
143 {
144     qmlRegisterType<TestType>("Test", 1, 0, "TestType");
145     qmlRegisterType<ParserStatusTestType>("Test", 1, 0, "ParserStatusTestType");
146     qmlRegisterType<ValueSourceTestType>("Test", 1, 0, "ValueSourceTestType");
147     qmlRegisterType<ValueInterceptorTestType>("Test", 1, 0, "ValueInterceptorTestType");
148 }
149
150 void tst_qdeclarativemetatype::copy()
151 {
152     QVERIFY(QDeclarativeMetaType::copy(QMetaType::Void, 0, 0));
153
154     COPY_TEST(bool, Bool, true, false);
155     COPY_TEST(int, Int, 10, 0);
156     COPY_TEST(unsigned int, UInt, 10, 0);
157     COPY_TEST(long long, LongLong, 10, 0);
158     COPY_TEST(unsigned long long, ULongLong, 10, 0);
159     COPY_TEST(double, Double, 19.2, 0);
160
161     QT_COPY_TEST(QChar, QChar('a'));
162
163     QVariantMap variantMap;
164     variantMap.insert("Hello World!", QVariant(10));
165     QT_COPY_TEST(QVariantMap, variantMap);
166
167     QT_COPY_TEST(QVariantList, QVariantList() << QVariant(19.2));
168     QT_COPY_TEST(QString, QString("QML Rocks!"));
169     QT_COPY_TEST(QStringList, QStringList() << "QML" << "Rocks");
170     QT_COPY_TEST(QByteArray, QByteArray("0x1102DDD"));
171     QT_COPY_TEST(QBitArray, QBitArray(102, true));
172     QDate cd = QDate::currentDate();
173     QT_COPY_TEST(QDate, cd);
174     QTime ct = QTime::currentTime();
175     QT_COPY_TEST(QTime, ct);
176     QDateTime cdt = QDateTime::currentDateTime();
177     QT_COPY_TEST(QDateTime, cdt);
178     QT_COPY_TEST(QUrl, QUrl("http://www.nokia.com"));
179     QT_COPY_TEST(QLocale, QLocale(QLocale::English, QLocale::Australia));
180     QT_COPY_TEST(QRect, QRect(-10, 10, 102, 99));
181     QT_COPY_TEST(QRectF, QRectF(-10.2, 1.2, 102, 99.6));
182     QT_COPY_TEST(QSize, QSize(100, 2));
183     QT_COPY_TEST(QSizeF, QSizeF(20.2, -100234.2)); 
184     QT_COPY_TEST(QLine, QLine(0, 0, 100, 100));
185     QT_COPY_TEST(QLineF, QLineF(-10.2, 0, 103, 1));
186     QT_COPY_TEST(QPoint, QPoint(-1912, 1613));
187     QT_COPY_TEST(QPointF, QPointF(-908.1, 1612));
188     QT_COPY_TEST(QRegExp, QRegExp("(\\d+)(?:\\s*)(cm|inch)"));
189
190     QVariantHash variantHash;
191     variantHash.insert("Hello World!", QVariant(19));
192     QT_COPY_TEST(QVariantHash, variantHash);
193
194 #ifdef QT3_SUPPORT
195     QT_COPY_TEST(QColorGroup, QColorGroup(Qt::red, Qt::red, Qt::red, Qt::red, Qt::red, Qt::red, Qt::red));
196 #endif
197
198     QT_COPY_TEST(QFont, QFont("Helvetica", 1024));
199
200     {
201         QPixmap v = QPixmap(100, 100); QPixmap v2 = QPixmap(100, 100);
202         QVERIFY(QDeclarativeMetaType::copy(QMetaType::QPixmap, &v, 0)); 
203         QVERIFY(v.size() == QPixmap().size());
204         QVERIFY(QDeclarativeMetaType::copy(QMetaType::QPixmap , &v, &v2)); 
205         QVERIFY(v.size() == QPixmap(100,100).size());
206     }
207
208     QT_COPY_TEST(QBrush, QBrush(Qt::blue));
209     QT_COPY_TEST(QColor, QColor("lightsteelblue"));
210     QT_COPY_TEST(QPalette, QPalette(Qt::green));
211     
212     {
213         QPixmap icon(100, 100);
214
215         QIcon v = QIcon(icon); QIcon v2 = QIcon(icon);
216         QVERIFY(QDeclarativeMetaType::copy(QMetaType::QIcon, &v, 0)); 
217         QVERIFY(v.isNull() == QIcon().isNull());
218         QVERIFY(QDeclarativeMetaType::copy(QMetaType::QIcon , &v, &v2)); 
219         QVERIFY(v.isNull() == QIcon(icon).isNull());
220     }
221
222     {
223         QImage v = QImage(100, 100, QImage::Format_RGB32); 
224         QImage v2 = QImage(100, 100, QImage::Format_RGB32);
225         QVERIFY(QDeclarativeMetaType::copy(QMetaType::QImage, &v, 0)); 
226         QVERIFY(v.size() == QImage().size());
227         QVERIFY(QDeclarativeMetaType::copy(QMetaType::QImage , &v, &v2)); 
228         QVERIFY(v.size() == QImage(100,100, QImage::Format_RGB32).size());
229     }
230
231     QT_COPY_TEST(QPolygon, QPolygon(QRect(100, 100, 200, 103)));
232     QT_COPY_TEST(QRegion, QRegion(QRect(0, 10, 99, 87)));
233
234     {
235         QBitmap v = QBitmap(100, 100); QBitmap v2 = QBitmap(100, 100);
236         QVERIFY(QDeclarativeMetaType::copy(QMetaType::QBitmap, &v, 0)); 
237         QVERIFY(v.size() == QBitmap().size());
238         QVERIFY(QDeclarativeMetaType::copy(QMetaType::QBitmap , &v, &v2)); 
239         QVERIFY(v.size() == QBitmap(100,100).size());
240     }
241
242     {
243         QCursor v = QCursor(Qt::SizeFDiagCursor); QCursor v2 = QCursor(Qt::SizeFDiagCursor);
244         QVERIFY(QDeclarativeMetaType::copy(QMetaType::QCursor, &v, 0)); 
245         QVERIFY(v.shape() == QCursor().shape());
246         QVERIFY(QDeclarativeMetaType::copy(QMetaType::QCursor , &v, &v2)); 
247         QVERIFY(v.shape() == QCursor(Qt::SizeFDiagCursor).shape());
248     }
249
250     QT_COPY_TEST(QSizePolicy, QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Maximum));
251     QT_COPY_TEST(QKeySequence, QKeySequence("Ctrl+O"));
252     QT_COPY_TEST(QPen, QPen(Qt::red));
253     QT_COPY_TEST(QTextLength, QTextLength(QTextLength::FixedLength, 10.2));
254     QT_COPY_TEST(QTextFormat, QTextFormat(QTextFormat::ListFormat));
255     QT_COPY_TEST(QMatrix, QMatrix().translate(10, 10));
256     QT_COPY_TEST(QTransform, QTransform().translate(10, 10));
257     QT_COPY_TEST(QMatrix4x4, QMatrix4x4(1,0,2,3,0,1,0,0,9,0,1,0,0,0,10,1));
258     QT_COPY_TEST(QVector2D, QVector2D(10.2, 1));
259     QT_COPY_TEST(QVector3D, QVector3D(10.2, 1, -2));
260     QT_COPY_TEST(QVector4D, QVector4D(10.2, 1, -2, 1.2));
261     QT_COPY_TEST(QQuaternion, QQuaternion(1.0, 10.2, 1, -2));
262
263     int voidValue;
264     COPY_TEST(void *, VoidStar, (void *)&voidValue, (void *)0);
265     COPY_TEST(long, Long, 10, 0);
266     COPY_TEST(short, Short, 10, 0);
267     COPY_TEST(char, Char, 'a', 0);
268     COPY_TEST(unsigned long, ULong, 10, 0);
269     COPY_TEST(unsigned short, UShort, 10, 0);
270     COPY_TEST(unsigned char, UChar, 'a', 0);
271     COPY_TEST(float, Float, 10.5, 0);
272
273     QObject objectValue;
274     QWidget widgetValue;
275     COPY_TEST(QObject *, QObjectStar, &objectValue, 0);
276     COPY_TEST(QWidget *, QWidgetStar, &widgetValue, 0);
277     COPY_TEST(qreal, QReal, 10.5, 0);
278
279     {
280         QVariant tv = QVariant::fromValue(QVariant(10));
281         QVariant v(tv); QVariant v2(tv);
282         QVERIFY(QDeclarativeMetaType::copy(qMetaTypeId<QVariant>(), &v, 0)); 
283         QVERIFY(v == QVariant());
284         QVERIFY(QDeclarativeMetaType::copy(qMetaTypeId<QVariant>(), &v, &v2)); 
285         QVERIFY(v == tv);
286     }
287
288     {
289         TestType t;  QVariant tv = QVariant::fromValue(&t);
290
291         QVariant v(tv); QVariant v2(tv);
292         QVERIFY(QDeclarativeMetaType::copy(qMetaTypeId<TestType *>(), &v, 0)); 
293         QVERIFY(v == QVariant::fromValue((TestType *)0));
294         QVERIFY(QDeclarativeMetaType::copy(qMetaTypeId<TestType *>(), &v, &v2)); 
295         QVERIFY(v == tv);
296     }
297 }
298
299 void tst_qdeclarativemetatype::qmlParserStatusCast()
300 {
301     QVERIFY(QDeclarativeMetaType::qmlType(QVariant::Int) == 0);
302     QVERIFY(QDeclarativeMetaType::qmlType(qMetaTypeId<TestType *>()) != 0);
303     QCOMPARE(QDeclarativeMetaType::qmlType(qMetaTypeId<TestType *>())->parserStatusCast(), -1);
304     QVERIFY(QDeclarativeMetaType::qmlType(qMetaTypeId<ValueSourceTestType *>()) != 0);
305     QCOMPARE(QDeclarativeMetaType::qmlType(qMetaTypeId<ValueSourceTestType *>())->parserStatusCast(), -1);
306             
307     QVERIFY(QDeclarativeMetaType::qmlType(qMetaTypeId<ParserStatusTestType *>()) != 0);
308     int cast = QDeclarativeMetaType::qmlType(qMetaTypeId<ParserStatusTestType *>())->parserStatusCast();
309     QVERIFY(cast != -1);
310     QVERIFY(cast != 0);
311
312     ParserStatusTestType t;
313     QVERIFY(reinterpret_cast<char *>((QObject *)&t) != reinterpret_cast<char *>((QDeclarativeParserStatus *)&t));
314
315     QDeclarativeParserStatus *status = reinterpret_cast<QDeclarativeParserStatus *>(reinterpret_cast<char *>((QObject *)&t) + cast);
316     QCOMPARE(status, (QDeclarativeParserStatus*)&t);
317 }
318
319 void tst_qdeclarativemetatype::qmlPropertyValueSourceCast()
320 {
321     QVERIFY(QDeclarativeMetaType::qmlType(QVariant::Int) == 0);
322     QVERIFY(QDeclarativeMetaType::qmlType(qMetaTypeId<TestType *>()) != 0);
323     QCOMPARE(QDeclarativeMetaType::qmlType(qMetaTypeId<TestType *>())->propertyValueSourceCast(), -1);
324     QVERIFY(QDeclarativeMetaType::qmlType(qMetaTypeId<ParserStatusTestType *>()) != 0);
325     QCOMPARE(QDeclarativeMetaType::qmlType(qMetaTypeId<ParserStatusTestType *>())->propertyValueSourceCast(), -1);
326             
327     QVERIFY(QDeclarativeMetaType::qmlType(qMetaTypeId<ValueSourceTestType *>()) != 0);
328     int cast = QDeclarativeMetaType::qmlType(qMetaTypeId<ValueSourceTestType *>())->propertyValueSourceCast();
329     QVERIFY(cast != -1);
330     QVERIFY(cast != 0);
331
332     ValueSourceTestType t;
333     QVERIFY(reinterpret_cast<char *>((QObject *)&t) != reinterpret_cast<char *>((QDeclarativePropertyValueSource *)&t));
334
335     QDeclarativePropertyValueSource *source = reinterpret_cast<QDeclarativePropertyValueSource *>(reinterpret_cast<char *>((QObject *)&t) + cast);
336     QCOMPARE(source, (QDeclarativePropertyValueSource*)&t);
337 }
338
339 void tst_qdeclarativemetatype::qmlPropertyValueInterceptorCast()
340 {
341     QVERIFY(QDeclarativeMetaType::qmlType(QVariant::Int) == 0);
342     QVERIFY(QDeclarativeMetaType::qmlType(qMetaTypeId<TestType *>()) != 0);
343     QCOMPARE(QDeclarativeMetaType::qmlType(qMetaTypeId<TestType *>())->propertyValueInterceptorCast(), -1);
344     QVERIFY(QDeclarativeMetaType::qmlType(qMetaTypeId<ParserStatusTestType *>()) != 0);
345     QCOMPARE(QDeclarativeMetaType::qmlType(qMetaTypeId<ParserStatusTestType *>())->propertyValueInterceptorCast(), -1);
346             
347     QVERIFY(QDeclarativeMetaType::qmlType(qMetaTypeId<ValueInterceptorTestType *>()) != 0);
348     int cast = QDeclarativeMetaType::qmlType(qMetaTypeId<ValueInterceptorTestType *>())->propertyValueInterceptorCast();
349     QVERIFY(cast != -1);
350     QVERIFY(cast != 0);
351
352     ValueInterceptorTestType t;
353     QVERIFY(reinterpret_cast<char *>((QObject *)&t) != reinterpret_cast<char *>((QDeclarativePropertyValueInterceptor *)&t));
354
355     QDeclarativePropertyValueInterceptor *interceptor = reinterpret_cast<QDeclarativePropertyValueInterceptor *>(reinterpret_cast<char *>((QObject *)&t) + cast);
356     QCOMPARE(interceptor, (QDeclarativePropertyValueInterceptor*)&t);
357 }
358
359 void tst_qdeclarativemetatype::isList()
360 {
361     QCOMPARE(QDeclarativeMetaType::isList(QVariant::Invalid), false);
362     QCOMPARE(QDeclarativeMetaType::isList(QVariant::Int), false);
363
364     QDeclarativeListProperty<TestType> list;
365
366     QCOMPARE(QDeclarativeMetaType::isList(qMetaTypeId<QDeclarativeListProperty<TestType> >()), true);
367 }
368
369 void tst_qdeclarativemetatype::defaultObject()
370 {
371     QVERIFY(QDeclarativeMetaType::defaultProperty(&QObject::staticMetaObject).name() == 0);
372     QVERIFY(QDeclarativeMetaType::defaultProperty(&ParserStatusTestType::staticMetaObject).name() == 0);
373     QCOMPARE(QString(QDeclarativeMetaType::defaultProperty(&TestType::staticMetaObject).name()), QString("foo"));
374
375     QObject o;
376     TestType t;
377     ParserStatusTestType p;
378
379     QVERIFY(QDeclarativeMetaType::defaultProperty((QObject *)0).name() == 0);
380     QVERIFY(QDeclarativeMetaType::defaultProperty(&o).name() == 0);
381     QVERIFY(QDeclarativeMetaType::defaultProperty(&p).name() == 0);
382     QCOMPARE(QString(QDeclarativeMetaType::defaultProperty(&t).name()), QString("foo"));
383 }
384
385 QTEST_MAIN(tst_qdeclarativemetatype)
386
387 #include "tst_qdeclarativemetatype.moc"