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 ** 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 <QDeclarativeEngine>
44 #include <QDeclarativeComponent>
45 #include <private/qdeclarativemetatype_p.h>
47 #include <QGraphicsScene>
48 #include <QGraphicsItem>
49 #include <QDeclarativeItem>
50 #include <QDeclarativeContext>
51 #include <private/qdeclarativetextinput_p.h>
52 #include <private/qobject_p.h>
55 // In Symbian OS test data is located in applications private dir
59 class tst_creation : public QObject
68 void qobject_qmltype();
71 void qobject_10flat_qml();
72 void qobject_10flat_cpp();
74 void qobject_10tree_qml();
75 void qobject_10tree_cpp();
77 void itemtree_notree_cpp();
78 void itemtree_objtree_cpp();
80 void itemtree_data_cpp();
82 void itemtree_scene_cpp();
88 QDeclarativeEngine engine;
91 class TestType : public QObject
94 Q_PROPERTY(QDeclarativeListProperty<QObject> resources READ resources)
95 Q_CLASSINFO("DefaultProperty", "resources")
97 TestType(QObject *parent = 0)
100 QDeclarativeListProperty<QObject> resources() {
101 return QDeclarativeListProperty<QObject>(this, 0, resources_append);
104 static void resources_append(QDeclarativeListProperty<QObject> *p, QObject *o) {
105 o->setParent(p->object);
109 tst_creation::tst_creation()
111 qmlRegisterType<TestType>("Qt.test", 1, 0, "TestType");
113 //get rid of initialization effects
114 QDeclarativeTextInput te;
117 inline QUrl TEST_FILE(const QString &filename)
119 return QUrl::fromLocalFile(QLatin1String(SRCDIR) + QLatin1String("/data/") + filename);
122 void tst_creation::qobject_cpp()
125 QObject *obj = new QObject;
130 void tst_creation::qobject_qml()
132 QDeclarativeComponent component(&engine);
133 component.setData("import QtQuick 1.0\nQtObject {}", QUrl());
134 QObject *obj = component.create();
138 QObject *obj = component.create();
143 void tst_creation::qobject_10flat_qml()
145 QDeclarativeComponent component(&engine);
146 component.setData("import Qt.test 1.0\nTestType { resources: [ TestType{},TestType{},TestType{},TestType{},TestType{},TestType{},TestType{},TestType{},TestType{},TestType{} ] }", QUrl());
147 QObject *obj = component.create();
151 QObject *obj = component.create();
156 void tst_creation::qobject_10flat_cpp()
159 QObject *item = new TestType;
174 void tst_creation::qobject_10tree_qml()
176 QDeclarativeComponent component(&engine);
177 component.setData("import Qt.test 1.0\nTestType { TestType{ TestType { TestType{ TestType{ TestType{ TestType{ TestType{ TestType{ TestType{ TestType{ } } } } } } } } } } }", QUrl());
179 QObject *obj = component.create();
183 QObject *obj = component.create();
188 void tst_creation::qobject_10tree_cpp()
191 QObject *item = new TestType;
192 QObject *root = item;
193 item = new TestType(item);
194 item = new TestType(item);
195 item = new TestType(item);
196 item = new TestType(item);
197 item = new TestType(item);
198 item = new TestType(item);
199 item = new TestType(item);
200 item = new TestType(item);
201 item = new TestType(item);
202 item = new TestType(item);
207 void tst_creation::qobject_qmltype()
209 QDeclarativeType *t = QDeclarativeMetaType::qmlType("Qt/QtObject", 4, 7);
212 QObject *obj = t->create();
217 struct QObjectFakeData {
218 char data[sizeof(QObjectPrivate)];
223 virtual ~QObjectFake();
228 QObjectFake::QObjectFake()
230 d = new QObjectFakeData;
233 QObjectFake::~QObjectFake()
238 void tst_creation::qobject_alloc()
241 QObjectFake *obj = new QObjectFake;
246 struct QDeclarativeGraphics_Derived : public QObject
248 void setParent_noEvent(QObject *parent) {
249 bool sce = d_ptr->sendChildEvents;
250 d_ptr->sendChildEvents = false;
252 d_ptr->sendChildEvents = sce;
256 inline void QDeclarativeGraphics_setParent_noEvent(QObject *object, QObject *parent)
258 static_cast<QDeclarativeGraphics_Derived *>(object)->setParent_noEvent(parent);
261 void tst_creation::itemtree_notree_cpp()
264 QDeclarativeItem *item = new QDeclarativeItem;
265 for (int i = 0; i < 30; ++i) {
266 QDeclarativeItem *child = new QDeclarativeItem;
272 void tst_creation::itemtree_objtree_cpp()
275 QDeclarativeItem *item = new QDeclarativeItem;
276 for (int i = 0; i < 30; ++i) {
277 QDeclarativeItem *child = new QDeclarativeItem;
278 QDeclarativeGraphics_setParent_noEvent(child,item);
284 void tst_creation::itemtree_cpp()
287 QDeclarativeItem *item = new QDeclarativeItem;
288 for (int i = 0; i < 30; ++i) {
289 QDeclarativeItem *child = new QDeclarativeItem;
290 QDeclarativeGraphics_setParent_noEvent(child,item);
291 child->setParentItem(item);
297 void tst_creation::itemtree_data_cpp()
300 QDeclarativeItem *item = new QDeclarativeItem;
301 for (int i = 0; i < 30; ++i) {
302 QDeclarativeItem *child = new QDeclarativeItem;
303 QDeclarativeGraphics_setParent_noEvent(child,item);
304 QDeclarativeListReference ref(item, "data");
311 void tst_creation::itemtree_qml()
313 QDeclarativeComponent component(&engine, TEST_FILE("item.qml"));
314 QObject *obj = component.create();
318 QObject *obj = component.create();
323 void tst_creation::itemtree_scene_cpp()
325 QGraphicsScene scene;
326 QDeclarativeItem *root = new QDeclarativeItem;
329 QDeclarativeItem *item = new QDeclarativeItem;
330 for (int i = 0; i < 30; ++i) {
331 QDeclarativeItem *child = new QDeclarativeItem;
332 QDeclarativeGraphics_setParent_noEvent(child,item);
333 child->setParentItem(item);
335 item->setParentItem(root);
341 void tst_creation::elements_data()
343 QTest::addColumn<QByteArray>("type");
345 QList<QByteArray> types = QDeclarativeMetaType::qmlTypeNames();
346 foreach (QByteArray type, types)
347 QTest::newRow(type.constData()) << type;
350 void tst_creation::elements()
352 QFETCH(QByteArray, type);
353 QDeclarativeType *t = QDeclarativeMetaType::qmlType(type, 4, 7);
354 if (!t || !t->isCreatable())
355 QSKIP("Non-creatable type", SkipSingle);
358 QObject *obj = t->create();
363 QTEST_MAIN(tst_creation)
365 #include "tst_creation.moc"