3961d7433a00d36194d2ff4e99cbca60181828e2
[profile/ivi/qtdeclarative.git] / tests / benchmarks / declarative / creation / tst_creation.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
6 **
7 ** This file is part of the test suite of the Qt Toolkit.
8 **
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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
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 <QtQuick1/private/qdeclarativetextinput_p.h>
52 #include <private/qobject_p.h>
53
54 class tst_creation : public QObject
55 {
56     Q_OBJECT
57 public:
58     tst_creation();
59
60 private slots:
61     void qobject_cpp();
62     void qobject_qml();
63     void qobject_qmltype();
64     void qobject_alloc();
65
66     void qobject_10flat_qml();
67     void qobject_10flat_cpp();
68
69     void qobject_10tree_qml();
70     void qobject_10tree_cpp();
71
72     void itemtree_notree_cpp();
73     void itemtree_objtree_cpp();
74     void itemtree_cpp();
75     void itemtree_data_cpp();
76     void itemtree_qml();
77     void itemtree_scene_cpp();
78
79     void elements_data();
80     void elements();
81
82 private:
83     QDeclarativeEngine engine;
84 };
85
86 class TestType : public QObject
87 {
88 Q_OBJECT
89 Q_PROPERTY(QDeclarativeListProperty<QObject> resources READ resources)
90 Q_CLASSINFO("DefaultProperty", "resources")
91 public:
92     TestType(QObject *parent = 0)
93     : QObject(parent) {}
94
95     QDeclarativeListProperty<QObject> resources() {
96         return QDeclarativeListProperty<QObject>(this, 0, resources_append);
97     }
98
99     static void resources_append(QDeclarativeListProperty<QObject> *p, QObject *o) {
100         o->setParent(p->object);
101     }
102 };
103
104 tst_creation::tst_creation()
105 {
106     qmlRegisterType<TestType>("Qt.test", 1, 0, "TestType");
107
108     //get rid of initialization effects
109     QDeclarative1TextInput te;
110 }
111
112 inline QUrl TEST_FILE(const QString &filename)
113 {
114     return QUrl::fromLocalFile(QLatin1String(SRCDIR) + QLatin1String("/data/") + filename);
115 }
116
117 void tst_creation::qobject_cpp()
118 {
119     QBENCHMARK {
120         QObject *obj = new QObject;
121         delete obj;
122     }
123 }
124
125 void tst_creation::qobject_qml()
126 {
127     QDeclarativeComponent component(&engine);
128     component.setData("import QtQuick 1.0\nQtObject {}", QUrl());
129     QObject *obj = component.create();
130     delete obj;
131
132     QBENCHMARK {
133         QObject *obj = component.create();
134         delete obj;
135     }
136 }
137
138 void tst_creation::qobject_10flat_qml()
139 {
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();
143     delete obj;
144
145     QBENCHMARK {
146         QObject *obj = component.create();
147         delete obj;
148     }
149 }
150
151 void tst_creation::qobject_10flat_cpp()
152 {
153     QBENCHMARK {
154         QObject *item = new TestType;
155         new TestType(item);
156         new TestType(item);
157         new TestType(item);
158         new TestType(item);
159         new TestType(item);
160         new TestType(item);
161         new TestType(item);
162         new TestType(item);
163         new TestType(item);
164         new TestType(item);
165         delete item;
166     }
167 }
168
169 void tst_creation::qobject_10tree_qml()
170 {
171     QDeclarativeComponent component(&engine);
172     component.setData("import Qt.test 1.0\nTestType { TestType{ TestType { TestType{ TestType{ TestType{ TestType{ TestType{ TestType{ TestType{ TestType{ } } } } } } } } } } }", QUrl());
173
174     QObject *obj = component.create();
175     delete obj;
176
177     QBENCHMARK {
178         QObject *obj = component.create();
179         delete obj;
180     }
181 }
182
183 void tst_creation::qobject_10tree_cpp()
184 {
185     QBENCHMARK {
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);
198         delete root;
199     }
200 }
201
202 void tst_creation::qobject_qmltype()
203 {
204     QDeclarativeType *t = QDeclarativeMetaType::qmlType("QtQuick/QtObject", 2, 0);
205
206     QBENCHMARK {
207         QObject *obj = t->create();
208         delete obj;
209     }
210 }
211
212 struct QObjectFakeData {
213     char data[sizeof(QObjectPrivate)];
214 };
215
216 struct QObjectFake {
217     QObjectFake();
218     virtual ~QObjectFake();
219 private:
220     QObjectFakeData *d;
221 };
222
223 QObjectFake::QObjectFake()
224 {
225     d = new QObjectFakeData;
226 }
227
228 QObjectFake::~QObjectFake()
229 {
230     delete d;
231 }
232
233 void tst_creation::qobject_alloc()
234 {
235     QBENCHMARK {
236         QObjectFake *obj = new QObjectFake;
237         delete obj;
238     }
239 }
240
241 struct QDeclarativeGraphics_Derived : public QObject
242 {
243     void setParent_noEvent(QObject *parent) {
244         bool sce = d_ptr->sendChildEvents;
245         d_ptr->sendChildEvents = false;
246         setParent(parent);
247         d_ptr->sendChildEvents = sce;
248     }
249 };
250
251 inline void QDeclarativeGraphics_setParent_noEvent(QObject *object, QObject *parent)
252 {
253     static_cast<QDeclarativeGraphics_Derived *>(object)->setParent_noEvent(parent);
254 }
255
256 void tst_creation::itemtree_notree_cpp()
257 {
258     QBENCHMARK {
259         QDeclarativeItem *item = new QDeclarativeItem;
260         for (int i = 0; i < 30; ++i) {
261             QDeclarativeItem *child = new QDeclarativeItem;
262             Q_UNUSED(child);
263         }
264         delete item;
265     }
266 }
267
268 void tst_creation::itemtree_objtree_cpp()
269 {
270     QBENCHMARK {
271         QDeclarativeItem *item = new QDeclarativeItem;
272         for (int i = 0; i < 30; ++i) {
273             QDeclarativeItem *child = new QDeclarativeItem;
274             QDeclarativeGraphics_setParent_noEvent(child,item);
275         }
276         delete item;
277     }
278 }
279
280 void tst_creation::itemtree_cpp()
281 {
282     QBENCHMARK {
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);
288         }
289         delete item;
290     }
291 }
292
293 void tst_creation::itemtree_data_cpp()
294 {
295     QBENCHMARK {
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");
301             ref.append(child);
302         }
303         delete item;
304     }
305 }
306
307 void tst_creation::itemtree_qml()
308 {
309     QDeclarativeComponent component(&engine, TEST_FILE("item.qml"));
310     QObject *obj = component.create();
311     delete obj;
312
313     QBENCHMARK {
314         QObject *obj = component.create();
315         delete obj;
316     }
317 }
318
319 void tst_creation::itemtree_scene_cpp()
320 {
321     QGraphicsScene scene;
322     QDeclarativeItem *root = new QDeclarativeItem;
323     scene.addItem(root);
324     QBENCHMARK {
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);
330         }
331         item->setParentItem(root);
332         delete item;
333     }
334     delete root;
335 }
336
337 void tst_creation::elements_data()
338 {
339     QTest::addColumn<QString>("type");
340
341     QList<QString> types = QDeclarativeMetaType::qmlTypeNames();
342     foreach (QString type, types)
343         QTest::newRow(type.toLatin1()) << type;
344 }
345
346 void tst_creation::elements()
347 {
348     QFETCH(QString, type);
349     QDeclarativeType *t = QDeclarativeMetaType::qmlType(type, 2, 0);
350     if (!t || !t->isCreatable())
351         QSKIP("Non-creatable type");
352
353     QBENCHMARK {
354         QObject *obj = t->create();
355         delete obj;
356     }
357 }
358
359 QTEST_MAIN(tst_creation)
360
361 #include "tst_creation.moc"