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 | #include <qtest.h> |
29 | #include <QDebug> |
30 | #include <QQmlEngine> |
31 | #include <QQmlComponent> |
32 | #include <QQmlContext> |
33 | #include <QLoggingCategory> |
34 | #include "../../shared/util.h" |
35 | |
36 | class tst_qqmlconsole : public QQmlDataTest |
37 | { |
38 | Q_OBJECT |
39 | public: |
40 | tst_qqmlconsole() {} |
41 | |
42 | private slots: |
43 | void logging(); |
44 | void categorized_logging(); |
45 | void tracing(); |
46 | void profiling(); |
47 | void testAssert(); |
48 | void exception(); |
49 | |
50 | private: |
51 | QQmlEngine engine; |
52 | }; |
53 | |
54 | struct CustomObject {}; |
55 | |
56 | QDebug operator<<(QDebug dbg, const CustomObject &) |
57 | { |
58 | return dbg << "MY OBJECT" ; |
59 | } |
60 | |
61 | Q_DECLARE_METATYPE(CustomObject) |
62 | |
63 | void tst_qqmlconsole::logging() |
64 | { |
65 | QUrl testUrl = testFileUrl(fileName: "logging.qml" ); |
66 | |
67 | QLoggingCategory loggingCategory("qml" ); |
68 | QVERIFY(loggingCategory.isDebugEnabled()); |
69 | QVERIFY(loggingCategory.isWarningEnabled()); |
70 | QVERIFY(loggingCategory.isCriticalEnabled()); |
71 | |
72 | QTest::ignoreMessage(type: QtDebugMsg, message: "console.debug" ); |
73 | QTest::ignoreMessage(type: QtDebugMsg, message: "console.log" ); |
74 | QTest::ignoreMessage(type: QtInfoMsg, message: "console.info" ); |
75 | QTest::ignoreMessage(type: QtWarningMsg, message: "console.warn" ); |
76 | QTest::ignoreMessage(type: QtCriticalMsg, message: "console.error" ); |
77 | |
78 | QTest::ignoreMessage(type: QtDebugMsg, message: "console.count: 1" ); |
79 | QTest::ignoreMessage(type: QtDebugMsg, message: ": 1" ); |
80 | QTest::ignoreMessage(type: QtDebugMsg, message: "console.count: 2" ); |
81 | QTest::ignoreMessage(type: QtDebugMsg, message: ": 2" ); |
82 | |
83 | QTest::ignoreMessage(type: QtDebugMsg, message: "[1,2]" ); |
84 | QTest::ignoreMessage(type: QtDebugMsg, message: "{\"a\":\"hello\",\"d\":1}" ); |
85 | QTest::ignoreMessage(type: QtDebugMsg, message: "undefined" ); |
86 | QTest::ignoreMessage(type: QtDebugMsg, message: "12" ); |
87 | QTest::ignoreMessage(type: QtDebugMsg, message: "function e() { [native code] }" ); |
88 | QTest::ignoreMessage(type: QtDebugMsg, message: "true" ); |
89 | // Printing QML object prints out the class/type of QML object with the memory address |
90 | // QTest::ignoreMessage(QtDebugMsg, "QtObject_QML_0(0xABCD..)"); |
91 | // QTest::ignoreMessage(QtDebugMsg, "[object Object]"); |
92 | QTest::ignoreMessage(type: QtDebugMsg, message: "1 pong! [object Object]" ); |
93 | QTest::ignoreMessage(type: QtDebugMsg, message: "1 [ping,pong] [object Object] 2" ); |
94 | QTest::ignoreMessage(type: QtDebugMsg, message: "[Hello,World]" ); |
95 | QTest::ignoreMessage(type: QtDebugMsg, message: "QVariant(CustomObject, MY OBJECT)" ); |
96 | QTest::ignoreMessage(type: QtDebugMsg, message: "[[1,2,3,[2,2,2,2],4],[5,6,7,8]]" ); |
97 | |
98 | QStringList stringList; stringList << QStringLiteral("Hello" ) << QStringLiteral("World" ); |
99 | |
100 | CustomObject customObject; |
101 | QVERIFY(QMetaType::registerDebugStreamOperator<CustomObject>()); |
102 | |
103 | QQmlComponent component(&engine, testUrl); |
104 | QScopedPointer<QObject> object(component.createWithInitialProperties(initialProperties: { |
105 | {"customObject" , QVariant::fromValue(value: customObject)}, |
106 | {"stringListProperty" , stringList} |
107 | })); |
108 | QVERIFY(object != nullptr); |
109 | } |
110 | |
111 | void tst_qqmlconsole::categorized_logging() |
112 | { |
113 | QUrl testUrl = testFileUrl(fileName: "categorized_logging.qml" ); |
114 | QQmlTestMessageHandler messageHandler; |
115 | messageHandler.setIncludeCategoriesEnabled(true); |
116 | |
117 | QLoggingCategory testCategory("qt.test" ); |
118 | testCategory.setEnabled(type: QtDebugMsg, enable: true); |
119 | QVERIFY(testCategory.isDebugEnabled()); |
120 | QVERIFY(testCategory.isWarningEnabled()); |
121 | QVERIFY(testCategory.isCriticalEnabled()); |
122 | |
123 | QQmlComponent component(&engine, testUrl); |
124 | QObject *object = component.create(); |
125 | QVERIFY2(object != nullptr, component.errorString().toUtf8()); |
126 | |
127 | QVERIFY(messageHandler.messages().contains("qt.test: console.info" )); |
128 | QVERIFY(messageHandler.messages().contains("qt.test: console.warn" )); |
129 | QVERIFY(messageHandler.messages().contains("qt.test: console.error" )); |
130 | QVERIFY(!messageHandler.messages().contains("qt.test.warning: console.debug" )); |
131 | QVERIFY(!messageHandler.messages().contains("qt.test.warning: console.log" )); |
132 | QVERIFY(!messageHandler.messages().contains("qt.test.warning: console.info" )); |
133 | QVERIFY(messageHandler.messages().contains("qt.test.warning: console.warn" )); |
134 | QVERIFY(messageHandler.messages().contains("qt.test.warning: console.error" )); |
135 | |
136 | QString emptyCategory = "default: " + QString::fromLatin1(str: "%1:%2:%3: " ).arg(a: testUrl.toString()).arg(a: 56).arg(a: 5) + |
137 | "QML LoggingCategory: Declaring the name of a LoggingCategory is mandatory and cannot be changed later" ; |
138 | QVERIFY(messageHandler.messages().contains(emptyCategory)); |
139 | |
140 | |
141 | QString notChangedCategory = "default: " + QString::fromLatin1(str: "%1:%2:%3: " ).arg(a: testUrl.toString()).arg(a: 45).arg(a: 5) + |
142 | "QML LoggingCategory: The name of a LoggingCategory cannot be changed after the component is completed" ; |
143 | QVERIFY(!messageHandler.messages().contains(notChangedCategory)); |
144 | QString changedCategory = "default: " + QString::fromLatin1(str: "%1:%2:%3: " ).arg(a: testUrl.toString()).arg(a: 50).arg(a: 5) + |
145 | "QML LoggingCategory: The name of a LoggingCategory cannot be changed after the component is completed" ; |
146 | QVERIFY(messageHandler.messages().contains(changedCategory)); |
147 | |
148 | |
149 | QString notChangedDefaultLogLevel = "default: " + QString::fromLatin1(str: "%1:%2:%3: " ).arg(a: testUrl.toString()).arg(a: 45).arg(a: 5) + |
150 | "QML LoggingCategory: The defaultLogLevel of a LoggingCategory cannot be changed after the component is completed" ; |
151 | QVERIFY(!messageHandler.messages().contains(notChangedDefaultLogLevel)); |
152 | QString changedDefaultLogLevel = "default: " + QString::fromLatin1(str: "%1:%2:%3: " ).arg(a: testUrl.toString()).arg(a: 50).arg(a: 5) + |
153 | "QML LoggingCategory: The defaultLogLevel of a LoggingCategory cannot be changed after the component is completed" ; |
154 | QVERIFY(messageHandler.messages().contains(changedDefaultLogLevel)); |
155 | |
156 | |
157 | QString useEmptyCategory = "default: " + QString::fromLatin1(str: "%1:%2: " ).arg(a: testUrl.toString()).arg(a: 78) + |
158 | "Error: A QmlLoggingCatgory was provided without a valid name" ; |
159 | QVERIFY(messageHandler.messages().contains(useEmptyCategory)); |
160 | |
161 | delete object; |
162 | } |
163 | |
164 | void tst_qqmlconsole::tracing() |
165 | { |
166 | QUrl testUrl = testFileUrl(fileName: "tracing.qml" ); |
167 | |
168 | QString traceText = |
169 | QString::fromLatin1(str: "tracing (%1:%2)\n" ).arg(a: testUrl.toString()).arg(a: 37) + |
170 | QString::fromLatin1(str: "onCompleted (%1:%2)" ).arg(a: testUrl.toString()).arg(a: 41); |
171 | |
172 | QTest::ignoreMessage(type: QtDebugMsg, qPrintable(traceText)); |
173 | |
174 | QQmlComponent component(&engine, testUrl); |
175 | QObject *object = component.create(); |
176 | QVERIFY(object != nullptr); |
177 | delete object; |
178 | } |
179 | |
180 | void tst_qqmlconsole::profiling() |
181 | { |
182 | QUrl testUrl = testFileUrl(fileName: "profiling.qml" ); |
183 | |
184 | // profiling() |
185 | QTest::ignoreMessage(type: QtWarningMsg, message: "Cannot start profiling because debug service is disabled. Start with -qmljsdebugger=port:XXXXX." ); |
186 | QTest::ignoreMessage(type: QtWarningMsg, message: "Ignoring console.profileEnd(): the debug service is disabled." ); |
187 | |
188 | QQmlComponent component(&engine, testUrl); |
189 | QObject *object = component.create(); |
190 | QVERIFY(object != nullptr); |
191 | delete object; |
192 | } |
193 | |
194 | void tst_qqmlconsole::testAssert() |
195 | { |
196 | QUrl testUrl = testFileUrl(fileName: "assert.qml" ); |
197 | |
198 | // assert() |
199 | QString assert1 = "This will fail\n" + |
200 | QString::fromLatin1(str: "onCompleted (%1:%2)" ).arg(a: testUrl.toString()).arg(a: 42); |
201 | |
202 | QString assert2 = "This will fail too\n" + |
203 | QString::fromLatin1(str: "assertFail (%1:%2)\n" ).arg(a: testUrl.toString()).arg(a: 35) + |
204 | QString::fromLatin1(str: "onCompleted (%1:%2)" ).arg(a: testUrl.toString()).arg(a: 47); |
205 | |
206 | QTest::ignoreMessage(type: QtCriticalMsg, qPrintable(assert1)); |
207 | QTest::ignoreMessage(type: QtCriticalMsg, qPrintable(assert2)); |
208 | |
209 | QQmlComponent component(&engine, testUrl); |
210 | QObject *object = component.create(); |
211 | QVERIFY(object != nullptr); |
212 | delete object; |
213 | } |
214 | |
215 | void tst_qqmlconsole::exception() |
216 | { |
217 | QUrl testUrl = testFileUrl(fileName: "exception.qml" ); |
218 | |
219 | // exception() |
220 | QString exception1 = "Exception 1\n" + |
221 | QString::fromLatin1(str: "onCompleted (%1:%2)" ).arg(a: testUrl.toString()).arg(a: 38); |
222 | |
223 | QString exception2 = "Exception 2\n" + |
224 | QString::fromLatin1(str: "exceptionFail (%1:%2)\n" ).arg(a: testUrl.toString()).arg(a: 33) + |
225 | QString::fromLatin1(str: "onCompleted (%1:%2)" ).arg(a: testUrl.toString()).arg(a: 43); |
226 | |
227 | QTest::ignoreMessage(type: QtCriticalMsg, qPrintable(exception1)); |
228 | QTest::ignoreMessage(type: QtCriticalMsg, qPrintable(exception2)); |
229 | |
230 | QQmlComponent component(&engine, testUrl); |
231 | QObject *object = component.create(); |
232 | QVERIFY(object != nullptr); |
233 | delete object; |
234 | } |
235 | |
236 | QTEST_MAIN(tst_qqmlconsole) |
237 | |
238 | #include "tst_qqmlconsole.moc" |
239 | |