1 /****************************************************************************
3 ** Copyright (C) 2011 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 ** 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
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
47 #include <QTextLength>
52 #include <QQuaternion>
53 #include <qdeclarative.h>
55 #include <private/qdeclarativemetatype_p.h>
58 // In Symbian OS test data is located in applications private dir
62 class tst_qdeclarativemetatype : public QObject
66 tst_qdeclarativemetatype() {}
73 void qmlParserStatusCast();
74 void qmlPropertyValueSourceCast();
75 void qmlPropertyValueInterceptorCast();
82 class TestType : public QObject
85 Q_PROPERTY(int foo READ foo)
87 Q_CLASSINFO("DefaultProperty", "foo")
89 int foo() { return 0; }
91 QML_DECLARE_TYPE(TestType);
93 class ParserStatusTestType : public QObject, public QDeclarativeParserStatus
97 void componentComplete(){}
98 Q_CLASSINFO("DefaultProperty", "foo") // Missing default property
99 Q_INTERFACES(QDeclarativeParserStatus)
101 QML_DECLARE_TYPE(ParserStatusTestType);
103 class ValueSourceTestType : public QObject, public QDeclarativePropertyValueSource
106 Q_INTERFACES(QDeclarativePropertyValueSource)
108 virtual void setTarget(const QDeclarativeProperty &) {}
110 QML_DECLARE_TYPE(ValueSourceTestType);
112 class ValueInterceptorTestType : public QObject, public QDeclarativePropertyValueInterceptor
115 Q_INTERFACES(QDeclarativePropertyValueInterceptor)
117 virtual void setTarget(const QDeclarativeProperty &) {}
118 virtual void write(const QVariant &) {}
120 QML_DECLARE_TYPE(ValueInterceptorTestType);
123 #define COPY_TEST(cpptype, metatype, value, defaultvalue) \
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)); \
133 #define QT_COPY_TEST(type, value) \
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)); \
142 void tst_qdeclarativemetatype::initTestCase()
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");
150 void tst_qdeclarativemetatype::copy()
152 QVERIFY(QDeclarativeMetaType::copy(QMetaType::Void, 0, 0));
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);
161 QT_COPY_TEST(QChar, QChar('a'));
163 QVariantMap variantMap;
164 variantMap.insert("Hello World!", QVariant(10));
165 QT_COPY_TEST(QVariantMap, variantMap);
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)"));
190 QVariantHash variantHash;
191 variantHash.insert("Hello World!", QVariant(19));
192 QT_COPY_TEST(QVariantHash, variantHash);
195 QT_COPY_TEST(QColorGroup, QColorGroup(Qt::red, Qt::red, Qt::red, Qt::red, Qt::red, Qt::red, Qt::red));
198 QT_COPY_TEST(QFont, QFont("Helvetica", 1024));
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());
208 QT_COPY_TEST(QBrush, QBrush(Qt::blue));
209 QT_COPY_TEST(QColor, QColor("lightsteelblue"));
210 QT_COPY_TEST(QPalette, QPalette(Qt::green));
213 QPixmap icon(100, 100);
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());
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());
231 QT_COPY_TEST(QPolygon, QPolygon(QRect(100, 100, 200, 103)));
232 QT_COPY_TEST(QRegion, QRegion(QRect(0, 10, 99, 87)));
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());
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());
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));
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);
275 COPY_TEST(QObject *, QObjectStar, &objectValue, 0);
276 COPY_TEST(QWidget *, QWidgetStar, &widgetValue, 0);
277 COPY_TEST(qreal, QReal, 10.5, 0);
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));
289 TestType t; QVariant tv = QVariant::fromValue(&t);
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));
299 void tst_qdeclarativemetatype::qmlParserStatusCast()
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);
307 QVERIFY(QDeclarativeMetaType::qmlType(qMetaTypeId<ParserStatusTestType *>()) != 0);
308 int cast = QDeclarativeMetaType::qmlType(qMetaTypeId<ParserStatusTestType *>())->parserStatusCast();
312 ParserStatusTestType t;
313 QVERIFY(reinterpret_cast<char *>((QObject *)&t) != reinterpret_cast<char *>((QDeclarativeParserStatus *)&t));
315 QDeclarativeParserStatus *status = reinterpret_cast<QDeclarativeParserStatus *>(reinterpret_cast<char *>((QObject *)&t) + cast);
316 QCOMPARE(status, (QDeclarativeParserStatus*)&t);
319 void tst_qdeclarativemetatype::qmlPropertyValueSourceCast()
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);
327 QVERIFY(QDeclarativeMetaType::qmlType(qMetaTypeId<ValueSourceTestType *>()) != 0);
328 int cast = QDeclarativeMetaType::qmlType(qMetaTypeId<ValueSourceTestType *>())->propertyValueSourceCast();
332 ValueSourceTestType t;
333 QVERIFY(reinterpret_cast<char *>((QObject *)&t) != reinterpret_cast<char *>((QDeclarativePropertyValueSource *)&t));
335 QDeclarativePropertyValueSource *source = reinterpret_cast<QDeclarativePropertyValueSource *>(reinterpret_cast<char *>((QObject *)&t) + cast);
336 QCOMPARE(source, (QDeclarativePropertyValueSource*)&t);
339 void tst_qdeclarativemetatype::qmlPropertyValueInterceptorCast()
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);
347 QVERIFY(QDeclarativeMetaType::qmlType(qMetaTypeId<ValueInterceptorTestType *>()) != 0);
348 int cast = QDeclarativeMetaType::qmlType(qMetaTypeId<ValueInterceptorTestType *>())->propertyValueInterceptorCast();
352 ValueInterceptorTestType t;
353 QVERIFY(reinterpret_cast<char *>((QObject *)&t) != reinterpret_cast<char *>((QDeclarativePropertyValueInterceptor *)&t));
355 QDeclarativePropertyValueInterceptor *interceptor = reinterpret_cast<QDeclarativePropertyValueInterceptor *>(reinterpret_cast<char *>((QObject *)&t) + cast);
356 QCOMPARE(interceptor, (QDeclarativePropertyValueInterceptor*)&t);
359 void tst_qdeclarativemetatype::isList()
361 QCOMPARE(QDeclarativeMetaType::isList(QVariant::Invalid), false);
362 QCOMPARE(QDeclarativeMetaType::isList(QVariant::Int), false);
364 QDeclarativeListProperty<TestType> list;
366 QCOMPARE(QDeclarativeMetaType::isList(qMetaTypeId<QDeclarativeListProperty<TestType> >()), true);
369 void tst_qdeclarativemetatype::defaultObject()
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"));
377 ParserStatusTestType p;
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"));
385 QTEST_MAIN(tst_qdeclarativemetatype)
387 #include "tst_qdeclarativemetatype.moc"