1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 Research In Motion. |
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 "../../shared/util.h" |
30 | #include <QtCore/QObject> |
31 | #include <QtQml/qqml.h> |
32 | #include <QtQml/QQmlEngine> |
33 | #include <QtQml/QQmlComponent> |
34 | #include <private/qhashedstring_p.h> |
35 | #include <private/qqmlmetatype_p.h> |
36 | |
37 | //Separate test, because if engine cleanup attempts fail they can easily break unrelated tests |
38 | class tst_qqmlenginecleanup : public QQmlDataTest |
39 | { |
40 | Q_OBJECT |
41 | public: |
42 | tst_qqmlenginecleanup() {} |
43 | |
44 | private slots: |
45 | void test_qmlClearTypeRegistrations(); |
46 | void test_valueTypeProviderModule(); // QTBUG-43004 |
47 | void test_customModuleCleanup(); |
48 | void test_qmlListCleared(); |
49 | }; |
50 | |
51 | // A wrapper around QQmlComponent to ensure the temporary reference counts |
52 | // on the type data as a result of the main thread <> loader thread communication |
53 | // are dropped. Regular Synchronous loading will leave us with an event posted |
54 | // to the gui thread and an extra refcount that will only be dropped after the |
55 | // event delivery. A plain sendPostedEvents() however is insufficient because |
56 | // we can't be sure that the event is posted after the constructor finished. |
57 | class CleanlyLoadingComponent : public QQmlComponent |
58 | { |
59 | public: |
60 | CleanlyLoadingComponent(QQmlEngine *engine, const QUrl &url) |
61 | : QQmlComponent(engine, url, QQmlComponent::Asynchronous) |
62 | { waitForLoad(); } |
63 | CleanlyLoadingComponent(QQmlEngine *engine, const QString &fileName) |
64 | : QQmlComponent(engine, fileName, QQmlComponent::Asynchronous) |
65 | { waitForLoad(); } |
66 | |
67 | void waitForLoad() |
68 | { |
69 | QTRY_VERIFY(status() == QQmlComponent::Ready || status() == QQmlComponent::Error); |
70 | } |
71 | }; |
72 | |
73 | void tst_qqmlenginecleanup::test_qmlClearTypeRegistrations() |
74 | { |
75 | //Test for preventing memory leaks is in tests/manual/qmltypememory |
76 | QQmlEngine* engine; |
77 | CleanlyLoadingComponent* component; |
78 | QUrl testFile = testFileUrl(fileName: "types.qml" ); |
79 | |
80 | const auto qmlTypeForTestType = []() { |
81 | return QQmlMetaType::qmlType(QStringLiteral("TestTypeCpp" ), QStringLiteral("Test" ), 2, 0); |
82 | }; |
83 | |
84 | QVERIFY(!qmlTypeForTestType().isValid()); |
85 | qmlRegisterType<QObject>(uri: "Test" , versionMajor: 2, versionMinor: 0, qmlName: "TestTypeCpp" ); |
86 | QVERIFY(qmlTypeForTestType().isValid()); |
87 | |
88 | engine = new QQmlEngine; |
89 | component = new CleanlyLoadingComponent(engine, testFile); |
90 | QVERIFY(component->isReady()); |
91 | |
92 | delete component; |
93 | delete engine; |
94 | |
95 | { |
96 | auto cppType = qmlTypeForTestType(); |
97 | |
98 | qmlClearTypeRegistrations(); |
99 | QVERIFY(!qmlTypeForTestType().isValid()); |
100 | |
101 | // cppType should hold the last ref, qmlClearTypeRegistration should have wiped |
102 | // all internal references. |
103 | QCOMPARE(QQmlType::refCount(cppType.priv()), 1); |
104 | } |
105 | |
106 | //2nd run verifies that types can reload after a qmlClearTypeRegistrations |
107 | qmlRegisterType<QObject>(uri: "Test" , versionMajor: 2, versionMinor: 0, qmlName: "TestTypeCpp" ); |
108 | QVERIFY(qmlTypeForTestType().isValid()); |
109 | engine = new QQmlEngine; |
110 | component = new CleanlyLoadingComponent(engine, testFile); |
111 | QVERIFY(component->isReady()); |
112 | |
113 | delete component; |
114 | delete engine; |
115 | qmlClearTypeRegistrations(); |
116 | QVERIFY(!qmlTypeForTestType().isValid()); |
117 | |
118 | //3nd run verifies that TestTypeCpp is no longer registered |
119 | engine = new QQmlEngine; |
120 | component = new CleanlyLoadingComponent(engine, testFile); |
121 | QVERIFY(component->isError()); |
122 | QCOMPARE(component->errorString(), |
123 | testFile.toString() +":33 module \"Test\" is not installed\n" ); |
124 | |
125 | delete component; |
126 | delete engine; |
127 | } |
128 | |
129 | static void cleanState(QQmlEngine **e) |
130 | { |
131 | delete *e; |
132 | qmlClearTypeRegistrations(); |
133 | *e = new QQmlEngine; |
134 | QCoreApplication::sendPostedEvents(receiver: nullptr, event_type: QEvent::DeferredDelete); |
135 | QCoreApplication::processEvents(); |
136 | } |
137 | |
138 | void tst_qqmlenginecleanup::test_valueTypeProviderModule() |
139 | { |
140 | // this test ensures that a module which installs a value type |
141 | // provider can be reinitialized after multiple calls to |
142 | // qmlClearTypeRegistrations() without causing cycles in the |
143 | // value type provider list. |
144 | QQmlEngine *e = nullptr; |
145 | QUrl testFile1 = testFileUrl(fileName: "testFile1.qml" ); |
146 | QUrl testFile2 = testFileUrl(fileName: "testFile2.qml" ); |
147 | bool noCycles = false; |
148 | for (int i = 0; i < 20; ++i) { |
149 | cleanState(e: &e); |
150 | QQmlComponent c(e, this); |
151 | c.loadUrl(url: i % 2 == 0 ? testFile1 : testFile2); // this will hang if cycles exist. |
152 | } |
153 | delete e; |
154 | e = nullptr; |
155 | noCycles = true; |
156 | QVERIFY(noCycles); |
157 | |
158 | // this test ensures that no crashes occur due to using |
159 | // a dangling QQmlType pointer in the type compiler |
160 | // which results from qmlClearTypeRegistrations() |
161 | QUrl testFile3 = testFileUrl(fileName: "testFile3.qml" ); |
162 | bool noDangling = false; |
163 | for (int i = 0; i < 20; ++i) { |
164 | cleanState(e: &e); |
165 | QQmlComponent c(e, this); |
166 | c.loadUrl(url: i % 2 == 0 ? testFile1 : testFile3); // this will crash if dangling ptr exists. |
167 | } |
168 | delete e; |
169 | noDangling = true; |
170 | QVERIFY(noDangling); |
171 | } |
172 | |
173 | void tst_qqmlenginecleanup::test_customModuleCleanup() |
174 | { |
175 | for (int i = 0; i < 5; ++i) { |
176 | qmlClearTypeRegistrations(); |
177 | |
178 | QQmlEngine engine; |
179 | engine.addImportPath(QT_TESTCASE_BUILDDIR); |
180 | |
181 | QQmlComponent component(&engine); |
182 | component.setData("import CustomModule 1.0\nModuleType {}" , baseUrl: QUrl()); |
183 | QCOMPARE(component.status(), QQmlComponent::Ready); |
184 | |
185 | QScopedPointer<QObject> object(component.create()); |
186 | QVERIFY(!object.isNull()); |
187 | } |
188 | } |
189 | |
190 | void tst_qqmlenginecleanup::test_qmlListCleared() |
191 | { |
192 | { |
193 | QQmlEngine engine; |
194 | auto url = testFileUrl(fileName: "MyItem.qml" ); |
195 | QQmlComponent comp(&engine, url); |
196 | QScopedPointer<QObject> item {comp.create()}; |
197 | QCOMPARE(QQmlMetaType::qmlRegisteredListTypeCount(), 1); |
198 | } |
199 | QCOMPARE(QQmlMetaType::qmlRegisteredListTypeCount(), 0); |
200 | } |
201 | |
202 | QTEST_MAIN(tst_qqmlenginecleanup) |
203 | |
204 | #include "tst_qqmlenginecleanup.moc" |
205 | |