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 ****************************************************************************/
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 <QtQuick1/private/qdeclarativetextinput_p.h>
52 #include <private/qobject_p.h>
54 class tst_creation : public QObject
63 void qobject_qmltype();
66 void qobject_10flat_qml();
67 void qobject_10flat_cpp();
69 void qobject_10tree_qml();
70 void qobject_10tree_cpp();
72 void itemtree_notree_cpp();
73 void itemtree_objtree_cpp();
75 void itemtree_data_cpp();
77 void itemtree_scene_cpp();
83 QDeclarativeEngine engine;
86 class TestType : public QObject
89 Q_PROPERTY(QDeclarativeListProperty<QObject> resources READ resources)
90 Q_CLASSINFO("DefaultProperty", "resources")
92 TestType(QObject *parent = 0)
95 QDeclarativeListProperty<QObject> resources() {
96 return QDeclarativeListProperty<QObject>(this, 0, resources_append);
99 static void resources_append(QDeclarativeListProperty<QObject> *p, QObject *o) {
100 o->setParent(p->object);
104 tst_creation::tst_creation()
106 qmlRegisterType<TestType>("Qt.test", 1, 0, "TestType");
108 //get rid of initialization effects
109 QDeclarative1TextInput te;
112 inline QUrl TEST_FILE(const QString &filename)
114 return QUrl::fromLocalFile(QLatin1String(SRCDIR) + QLatin1String("/data/") + filename);
117 void tst_creation::qobject_cpp()
120 QObject *obj = new QObject;
125 void tst_creation::qobject_qml()
127 QDeclarativeComponent component(&engine);
128 component.setData("import QtQuick 1.0\nQtObject {}", QUrl());
129 QObject *obj = component.create();
133 QObject *obj = component.create();
138 void tst_creation::qobject_10flat_qml()
140 QDeclarativeComponent component(&engine);
141 component.setData("import Qt.test 1.0\nTestType { resources: [ TestType{},TestType{},TestType{},TestType{},TestType{},TestType{},TestType{},TestType{},TestType{},TestType{} ] }", QUrl());
142 QObject *obj = component.create();
146 QObject *obj = component.create();
151 void tst_creation::qobject_10flat_cpp()
154 QObject *item = new TestType;
169 void tst_creation::qobject_10tree_qml()
171 QDeclarativeComponent component(&engine);
172 component.setData("import Qt.test 1.0\nTestType { TestType{ TestType { TestType{ TestType{ TestType{ TestType{ TestType{ TestType{ TestType{ TestType{ } } } } } } } } } } }", QUrl());
174 QObject *obj = component.create();
178 QObject *obj = component.create();
183 void tst_creation::qobject_10tree_cpp()
186 QObject *item = new TestType;
187 QObject *root = item;
188 item = new TestType(item);
189 item = new TestType(item);
190 item = new TestType(item);
191 item = new TestType(item);
192 item = new TestType(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);
202 void tst_creation::qobject_qmltype()
204 QDeclarativeType *t = QDeclarativeMetaType::qmlType("QtQuick/QtObject", 2, 0);
207 QObject *obj = t->create();
212 struct QObjectFakeData {
213 char data[sizeof(QObjectPrivate)];
218 virtual ~QObjectFake();
223 QObjectFake::QObjectFake()
225 d = new QObjectFakeData;
228 QObjectFake::~QObjectFake()
233 void tst_creation::qobject_alloc()
236 QObjectFake *obj = new QObjectFake;
241 struct QDeclarativeGraphics_Derived : public QObject
243 void setParent_noEvent(QObject *parent) {
244 bool sce = d_ptr->sendChildEvents;
245 d_ptr->sendChildEvents = false;
247 d_ptr->sendChildEvents = sce;
251 inline void QDeclarativeGraphics_setParent_noEvent(QObject *object, QObject *parent)
253 static_cast<QDeclarativeGraphics_Derived *>(object)->setParent_noEvent(parent);
256 void tst_creation::itemtree_notree_cpp()
259 QDeclarativeItem *item = new QDeclarativeItem;
260 for (int i = 0; i < 30; ++i) {
261 QDeclarativeItem *child = new QDeclarativeItem;
268 void tst_creation::itemtree_objtree_cpp()
271 QDeclarativeItem *item = new QDeclarativeItem;
272 for (int i = 0; i < 30; ++i) {
273 QDeclarativeItem *child = new QDeclarativeItem;
274 QDeclarativeGraphics_setParent_noEvent(child,item);
280 void tst_creation::itemtree_cpp()
283 QDeclarativeItem *item = new QDeclarativeItem;
284 for (int i = 0; i < 30; ++i) {
285 QDeclarativeItem *child = new QDeclarativeItem;
286 QDeclarativeGraphics_setParent_noEvent(child,item);
287 child->setParentItem(item);
293 void tst_creation::itemtree_data_cpp()
296 QDeclarativeItem *item = new QDeclarativeItem;
297 for (int i = 0; i < 30; ++i) {
298 QDeclarativeItem *child = new QDeclarativeItem;
299 QDeclarativeGraphics_setParent_noEvent(child,item);
300 QDeclarativeListReference ref(item, "data");
307 void tst_creation::itemtree_qml()
309 QDeclarativeComponent component(&engine, TEST_FILE("item.qml"));
310 QObject *obj = component.create();
314 QObject *obj = component.create();
319 void tst_creation::itemtree_scene_cpp()
321 QGraphicsScene scene;
322 QDeclarativeItem *root = new QDeclarativeItem;
325 QDeclarativeItem *item = new QDeclarativeItem;
326 for (int i = 0; i < 30; ++i) {
327 QDeclarativeItem *child = new QDeclarativeItem;
328 QDeclarativeGraphics_setParent_noEvent(child,item);
329 child->setParentItem(item);
331 item->setParentItem(root);
337 void tst_creation::elements_data()
339 QTest::addColumn<QString>("type");
341 QList<QString> types = QDeclarativeMetaType::qmlTypeNames();
342 foreach (QString type, types)
343 QTest::newRow(type.toLatin1()) << type;
346 void tst_creation::elements()
348 QFETCH(QString, type);
349 QDeclarativeType *t = QDeclarativeMetaType::qmlType(type, 2, 0);
350 if (!t || !t->isCreatable())
351 QSKIP("Non-creatable type");
354 QObject *obj = t->create();
359 QTEST_MAIN(tst_creation)
361 #include "tst_creation.moc"