Initial import from the monolithic Qt.
[profile/ivi/qtdeclarative.git] / tests / benchmarks / declarative / creation / tst_creation.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 <QDeclarativeEngine>
44 #include <QDeclarativeComponent>
45 #include <private/qdeclarativemetatype_p.h>
46 #include <QDebug>
47 #include <QGraphicsScene>
48 #include <QGraphicsItem>
49 #include <QDeclarativeItem>
50 #include <QDeclarativeContext>
51 #include <private/qdeclarativetextinput_p.h>
52 #include <private/qobject_p.h>
53
54 #ifdef Q_OS_SYMBIAN
55 // In Symbian OS test data is located in applications private dir
56 #define SRCDIR "."
57 #endif
58
59 class tst_creation : public QObject
60 {
61     Q_OBJECT
62 public:
63     tst_creation();
64
65 private slots:
66     void qobject_cpp();
67     void qobject_qml();
68     void qobject_qmltype();
69     void qobject_alloc();
70
71     void qobject_10flat_qml();
72     void qobject_10flat_cpp();
73
74     void qobject_10tree_qml();
75     void qobject_10tree_cpp();
76
77     void itemtree_notree_cpp();
78     void itemtree_objtree_cpp();
79     void itemtree_cpp();
80     void itemtree_data_cpp();
81     void itemtree_qml();
82     void itemtree_scene_cpp();
83
84     void elements_data();
85     void elements();
86
87 private:
88     QDeclarativeEngine engine;
89 };
90
91 class TestType : public QObject
92 {
93 Q_OBJECT
94 Q_PROPERTY(QDeclarativeListProperty<QObject> resources READ resources)
95 Q_CLASSINFO("DefaultProperty", "resources")
96 public:
97     TestType(QObject *parent = 0)
98     : QObject(parent) {}
99
100     QDeclarativeListProperty<QObject> resources() {
101         return QDeclarativeListProperty<QObject>(this, 0, resources_append);
102     }
103
104     static void resources_append(QDeclarativeListProperty<QObject> *p, QObject *o) {
105         o->setParent(p->object);
106     }
107 };
108
109 tst_creation::tst_creation()
110 {
111     qmlRegisterType<TestType>("Qt.test", 1, 0, "TestType");
112
113     //get rid of initialization effects
114     QDeclarativeTextInput te;
115 }
116
117 inline QUrl TEST_FILE(const QString &filename)
118 {
119     return QUrl::fromLocalFile(QLatin1String(SRCDIR) + QLatin1String("/data/") + filename);
120 }
121
122 void tst_creation::qobject_cpp()
123 {
124     QBENCHMARK {
125         QObject *obj = new QObject;
126         delete obj;
127     }
128 }
129
130 void tst_creation::qobject_qml()
131 {
132     QDeclarativeComponent component(&engine);
133     component.setData("import QtQuick 1.0\nQtObject {}", QUrl());
134     QObject *obj = component.create();
135     delete obj;
136
137     QBENCHMARK {
138         QObject *obj = component.create();
139         delete obj;
140     }
141 }
142
143 void tst_creation::qobject_10flat_qml()
144 {
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();
148     delete obj;
149
150     QBENCHMARK {
151         QObject *obj = component.create();
152         delete obj;
153     }
154 }
155
156 void tst_creation::qobject_10flat_cpp()
157 {
158     QBENCHMARK {
159         QObject *item = new TestType;
160         new TestType(item);
161         new TestType(item);
162         new TestType(item);
163         new TestType(item);
164         new TestType(item);
165         new TestType(item);
166         new TestType(item);
167         new TestType(item);
168         new TestType(item);
169         new TestType(item);
170         delete item;
171     }
172 }
173
174 void tst_creation::qobject_10tree_qml()
175 {
176     QDeclarativeComponent component(&engine);
177     component.setData("import Qt.test 1.0\nTestType { TestType{ TestType { TestType{ TestType{ TestType{ TestType{ TestType{ TestType{ TestType{ TestType{ } } } } } } } } } } }", QUrl());
178
179     QObject *obj = component.create();
180     delete obj;
181
182     QBENCHMARK {
183         QObject *obj = component.create();
184         delete obj;
185     }
186 }
187
188 void tst_creation::qobject_10tree_cpp()
189 {
190     QBENCHMARK {
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);
203         delete root;
204     }
205 }
206
207 void tst_creation::qobject_qmltype()
208 {
209     QDeclarativeType *t = QDeclarativeMetaType::qmlType("Qt/QtObject", 4, 7);
210
211     QBENCHMARK {
212         QObject *obj = t->create();
213         delete obj;
214     }
215 }
216
217 struct QObjectFakeData {
218     char data[sizeof(QObjectPrivate)];
219 };
220
221 struct QObjectFake {
222     QObjectFake();
223     virtual ~QObjectFake();
224 private:
225     QObjectFakeData *d;
226 };
227
228 QObjectFake::QObjectFake()
229 {
230     d = new QObjectFakeData;
231 }
232
233 QObjectFake::~QObjectFake()
234 {
235     delete d;
236 }
237
238 void tst_creation::qobject_alloc()
239 {
240     QBENCHMARK {
241         QObjectFake *obj = new QObjectFake;
242         delete obj;
243     }
244 }
245
246 struct QDeclarativeGraphics_Derived : public QObject
247 {
248     void setParent_noEvent(QObject *parent) {
249         bool sce = d_ptr->sendChildEvents;
250         d_ptr->sendChildEvents = false;
251         setParent(parent);
252         d_ptr->sendChildEvents = sce;
253     }
254 };
255
256 inline void QDeclarativeGraphics_setParent_noEvent(QObject *object, QObject *parent)
257 {
258     static_cast<QDeclarativeGraphics_Derived *>(object)->setParent_noEvent(parent);
259 }
260
261 void tst_creation::itemtree_notree_cpp()
262 {
263     QBENCHMARK {
264         QDeclarativeItem *item = new QDeclarativeItem;
265         for (int i = 0; i < 30; ++i) {
266             QDeclarativeItem *child = new QDeclarativeItem;
267         }
268         delete item;
269     }
270 }
271
272 void tst_creation::itemtree_objtree_cpp()
273 {
274     QBENCHMARK {
275         QDeclarativeItem *item = new QDeclarativeItem;
276         for (int i = 0; i < 30; ++i) {
277             QDeclarativeItem *child = new QDeclarativeItem;
278             QDeclarativeGraphics_setParent_noEvent(child,item);
279         }
280         delete item;
281     }
282 }
283
284 void tst_creation::itemtree_cpp()
285 {
286     QBENCHMARK {
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);
292         }
293         delete item;
294     }
295 }
296
297 void tst_creation::itemtree_data_cpp()
298 {
299     QBENCHMARK {
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");
305             ref.append(child);
306         }
307         delete item;
308     }
309 }
310
311 void tst_creation::itemtree_qml()
312 {
313     QDeclarativeComponent component(&engine, TEST_FILE("item.qml"));
314     QObject *obj = component.create();
315     delete obj;
316
317     QBENCHMARK {
318         QObject *obj = component.create();
319         delete obj;
320     }
321 }
322
323 void tst_creation::itemtree_scene_cpp()
324 {
325     QGraphicsScene scene;
326     QDeclarativeItem *root = new QDeclarativeItem;
327     scene.addItem(root);
328     QBENCHMARK {
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);
334         }
335         item->setParentItem(root);
336         delete item;
337     }
338     delete root;
339 }
340
341 void tst_creation::elements_data()
342 {
343     QTest::addColumn<QByteArray>("type");
344
345     QList<QByteArray> types = QDeclarativeMetaType::qmlTypeNames();
346     foreach (QByteArray type, types)
347         QTest::newRow(type.constData()) << type;
348 }
349
350 void tst_creation::elements()
351 {
352     QFETCH(QByteArray, type);
353     QDeclarativeType *t = QDeclarativeMetaType::qmlType(type, 4, 7);
354     if (!t || !t->isCreatable())
355         QSKIP("Non-creatable type", SkipSingle);
356
357     QBENCHMARK {
358         QObject *obj = t->create();
359         delete obj;
360     }
361 }
362
363 QTEST_MAIN(tst_creation)
364
365 #include "tst_creation.moc"