1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
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 https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include <QtTest>
30#include <QtQuick>
31#include <QtCore/private/qhooks_p.h>
32#include <iostream>
33
34static int qt_verbose = qgetenv(varName: "VERBOSE").toInt() != 0;
35
36Q_GLOBAL_STATIC(QObjectList, qt_qobjects)
37
38extern "C" Q_DECL_EXPORT void qt_addQObject(QObject *object)
39{
40 qt_qobjects->append(t: object);
41}
42
43extern "C" Q_DECL_EXPORT void qt_removeQObject(QObject *object)
44{
45 qt_qobjects->removeAll(t: object);
46}
47
48class tst_ObjectCount : public QObject
49{
50 Q_OBJECT
51
52private slots:
53 void init();
54 void cleanup();
55
56 void controls();
57 void controls_data();
58
59private:
60 QQmlEngine engine;
61};
62
63void tst_ObjectCount::init()
64{
65 qtHookData[QHooks::AddQObject] = reinterpret_cast<quintptr>(&qt_addQObject);
66 qtHookData[QHooks::RemoveQObject] = reinterpret_cast<quintptr>(&qt_removeQObject);
67
68 // warmup
69 QQmlComponent component(&engine);
70 component.setData("import QtQuick 2.0; import QtQuick.Controls 1.3; Button {}", baseUrl: QUrl());
71 delete component.create();
72}
73
74void tst_ObjectCount::cleanup()
75{
76 qtHookData[QHooks::AddQObject] = 0;
77 qtHookData[QHooks::RemoveQObject] = 0;
78}
79
80static void printItems(const QList<QQuickItem *> &items)
81{
82 std::cout << "RESULT tst_ObjectCount::" << QTest::currentTestFunction() << "():\"" << QTest::currentDataTag() << "\":" << std::endl;
83 std::cout << " QQuickItems: " << items.count() << " (total of QObjects: " << qt_qobjects->count() << ")" << std::endl;
84
85 if (qt_verbose) {
86 for (QObject *object : qAsConst(t&: *qt_qobjects))
87 qInfo() << "\t" << object;
88 }
89}
90
91void tst_ObjectCount::controls()
92{
93 QFETCH(QByteArray, data);
94
95 QQmlComponent component(&engine);
96
97 qt_qobjects->clear();
98
99 component.setData(data, baseUrl: QUrl());
100 QScopedPointer<QObject> object(component.create());
101 QVERIFY2(object.data(), qPrintable(component.errorString()));
102
103 QList<QQuickItem *> items;
104 for (QObject *object : qAsConst(t&: *qt_qobjects)) {
105 QQuickItem *item = qobject_cast<QQuickItem *>(object);
106 if (item)
107 items += item;
108 }
109 printItems(items);
110}
111
112void tst_ObjectCount::controls_data()
113{
114 QTest::addColumn<QByteArray>(name: "data");
115
116 QTest::newRow(dataTag: "ApplicationWindow")
117 << QByteArray("import QtQuick.Controls 1.3; ApplicationWindow { }");
118 QTest::newRow(dataTag: "BusyIndicator")
119 << QByteArray("import QtQuick.Controls 1.3; BusyIndicator { }");
120 QTest::newRow(dataTag: "Button")
121 << QByteArray("import QtQuick.Controls 1.3; Button { }");
122 QTest::newRow(dataTag: "CheckBox")
123 << QByteArray("import QtQuick.Controls 1.3; CheckBox { }");
124 QTest::newRow(dataTag: "Dial")
125 << QByteArray("import QtQuick.Extras 1.3; Dial { }");
126 QTest::newRow(dataTag: "GroupBox")
127 << QByteArray("import QtQuick.Controls 1.3; GroupBox { }");
128 QTest::newRow(dataTag: "Label")
129 << QByteArray("import QtQuick.Controls 1.3; Label { }");
130 QTest::newRow(dataTag: "ProgressBar")
131 << QByteArray("import QtQuick.Controls 1.3; ProgressBar { }");
132 QTest::newRow(dataTag: "RadioButton")
133 << QByteArray("import QtQuick.Controls 1.3; RadioButton { }");
134 QTest::newRow(dataTag: "ScrollView")
135 << QByteArray("import QtQuick.Controls 1.3; ScrollView { }");
136 QTest::newRow(dataTag: "Slider")
137 << QByteArray("import QtQuick.Controls 1.3; Slider { }");
138 QTest::newRow(dataTag: "SpinBox")
139 << QByteArray("import QtQuick.Controls 1.3; SpinBox { }");
140 QTest::newRow(dataTag: "StackView")
141 << QByteArray("import QtQuick.Controls 1.3; StackView { }");
142 QTest::newRow(dataTag: "Switch")
143 << QByteArray("import QtQuick.Controls 1.3; Switch { }");
144 QTest::newRow(dataTag: "TabView")
145 << QByteArray("import QtQuick.Controls 1.3; TabView { }");
146 QTest::newRow(dataTag: "TextArea")
147 << QByteArray("import QtQuick.Controls 1.3; TextArea { }");
148 QTest::newRow(dataTag: "TextField")
149 << QByteArray("import QtQuick.Controls 1.3; TextField { }");
150 QTest::newRow(dataTag: "ToolBar")
151 << QByteArray("import QtQuick.Controls 1.3; ToolBar { }");
152 QTest::newRow(dataTag: "ToolButton")
153 << QByteArray("import QtQuick.Controls 1.3; ToolButton { }");
154 QTest::newRow(dataTag: "Tumbler")
155 << QByteArray("import QtQuick.Extras 1.3; Tumbler { }");
156}
157
158QTEST_MAIN(tst_ObjectCount)
159
160#include "tst_objectcount.moc"
161

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