1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: http://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL3$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see http://www.qt.io/terms-conditions. For further
15** information use the contact form at http://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPLv3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or later as published by the Free
28** Software Foundation and appearing in the file LICENSE.GPL included in
29** the packaging of this file. Please review the following information to
30** ensure the GNU General Public License version 2.0 requirements will be
31** met: http://www.gnu.org/licenses/gpl-2.0.html.
32**
33** $QT_END_LICENSE$
34**
35****************************************************************************/
36
37#include <QtTest>
38#include <QtQuick>
39#include <QtCore/private/qhooks_p.h>
40#include <iostream>
41#include "../../auto/shared/visualtestutil.h"
42
43using namespace QQuickVisualTestUtil;
44
45static int qt_verbose = qgetenv(varName: "VERBOSE").toInt() != 0;
46
47Q_GLOBAL_STATIC(QObjectList, qt_qobjects)
48
49extern "C" Q_DECL_EXPORT void qt_addQObject(QObject *object)
50{
51 qt_qobjects->append(t: object);
52}
53
54extern "C" Q_DECL_EXPORT void qt_removeQObject(QObject *object)
55{
56 qt_qobjects->removeAll(t: object);
57}
58
59class tst_ObjectCount : public QObject
60{
61 Q_OBJECT
62
63private slots:
64 void init();
65 void cleanup();
66
67 void qobjects();
68 void qobjects_data();
69
70 void qquickitems();
71 void qquickitems_data();
72
73private:
74 QQmlEngine engine;
75};
76
77void tst_ObjectCount::init()
78{
79 qtHookData[QHooks::AddQObject] = reinterpret_cast<quintptr>(&qt_addQObject);
80 qtHookData[QHooks::RemoveQObject] = reinterpret_cast<quintptr>(&qt_removeQObject);
81
82 // warmup
83 QQmlComponent component(&engine);
84 component.setData("import QtQuick 2.0; import QtQuick.Controls 2.1; Item { Button {} }", baseUrl: QUrl());
85 delete component.create();
86}
87
88void tst_ObjectCount::cleanup()
89{
90 qtHookData[QHooks::AddQObject] = 0;
91 qtHookData[QHooks::RemoveQObject] = 0;
92}
93
94static void initTestRows(QQmlEngine *engine)
95{
96 addTestRowForEachControl(engine, sourcePath: "controls", targetPath: "QtQuick/Controls.2");
97 addTestRowForEachControl(engine, sourcePath: "controls/fusion", targetPath: "QtQuick/Controls.2/Fusion", skiplist: QStringList() << "ButtonPanel" << "CheckIndicator" << "RadioIndicator" << "SliderGroove" << "SliderHandle" << "SwitchIndicator");
98 addTestRowForEachControl(engine, sourcePath: "controls/imagine", targetPath: "QtQuick/Controls.2/Imagine");
99 addTestRowForEachControl(engine, sourcePath: "controls/material", targetPath: "QtQuick/Controls.2/Material", skiplist: QStringList() << "Ripple" << "SliderHandle" << "CheckIndicator" << "RadioIndicator" << "SwitchIndicator" << "BoxShadow" << "ElevationEffect" << "CursorDelegate");
100 addTestRowForEachControl(engine, sourcePath: "controls/universal", targetPath: "QtQuick/Controls.2/Universal", skiplist: QStringList() << "CheckIndicator" << "RadioIndicator" << "SwitchIndicator");
101}
102
103template <typename T>
104static void doBenchmark(QQmlEngine *engine, const QUrl &url)
105{
106 QQmlComponent component(engine);
107
108 qt_qobjects->clear();
109
110 component.loadUrl(url);
111 QScopedPointer<QObject> object(component.create());
112 QVERIFY2(object.data(), qPrintable(component.errorString()));
113
114 QObjectList objects;
115 for (QObject *object : qAsConst(t&: *qt_qobjects())) {
116 if (qobject_cast<T *>(object))
117 objects += object;
118 }
119
120 if (qt_verbose) {
121 for (QObject *object : objects)
122 qInfo() << "\t" << object;
123 }
124
125 QTest::setBenchmarkResult(result: objects.count(), metric: QTest::Events);
126}
127
128void tst_ObjectCount::qobjects()
129{
130 QFETCH(QUrl, url);
131 doBenchmark<QObject>(engine: &engine, url);
132}
133
134void tst_ObjectCount::qobjects_data()
135{
136 QTest::addColumn<QUrl>(name: "url");
137 initTestRows(engine: &engine);
138}
139
140void tst_ObjectCount::qquickitems()
141{
142 QFETCH(QUrl, url);
143 doBenchmark<QQuickItem>(engine: &engine, url);
144}
145
146void tst_ObjectCount::qquickitems_data()
147{
148 QTest::addColumn<QUrl>(name: "url");
149 initTestRows(engine: &engine);
150}
151
152QTEST_MAIN(tst_ObjectCount)
153
154#include "tst_objectcount.moc"
155

source code of qtquickcontrols2/tests/benchmarks/objectcount/tst_objectcount.cpp