2022-05-13 13:12:05 +00:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2011-11-18 15:41:08 +00:00
|
|
|
#include <qtest.h>
|
|
|
|
#include <QDebug>
|
2012-02-16 04:43:03 +00:00
|
|
|
#include <QQmlEngine>
|
|
|
|
#include <QQmlComponent>
|
2018-04-18 07:38:39 +00:00
|
|
|
#include <QQmlContext>
|
2013-10-23 13:47:30 +00:00
|
|
|
#include <QLoggingCategory>
|
2021-08-06 10:27:35 +00:00
|
|
|
#include <QtQuickTestUtils/private/qmlutils_p.h>
|
2011-11-18 15:41:08 +00:00
|
|
|
|
2012-02-16 04:43:03 +00:00
|
|
|
class tst_qqmlconsole : public QQmlDataTest
|
2011-11-18 15:41:08 +00:00
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
2021-08-06 10:27:35 +00:00
|
|
|
tst_qqmlconsole() : QQmlDataTest(QT_QMLTEST_DATADIR) {}
|
2011-11-18 15:41:08 +00:00
|
|
|
|
|
|
|
private slots:
|
2012-01-16 12:35:07 +00:00
|
|
|
void logging();
|
2016-05-02 15:29:40 +00:00
|
|
|
void categorized_logging();
|
2012-01-16 12:35:07 +00:00
|
|
|
void tracing();
|
|
|
|
void profiling();
|
2015-08-07 15:25:45 +00:00
|
|
|
void testAssert();
|
2012-01-17 13:38:06 +00:00
|
|
|
void exception();
|
2011-11-18 15:41:08 +00:00
|
|
|
|
|
|
|
private:
|
2012-02-16 04:43:03 +00:00
|
|
|
QQmlEngine engine;
|
2011-11-18 15:41:08 +00:00
|
|
|
};
|
|
|
|
|
2018-11-06 13:55:47 +00:00
|
|
|
struct CustomObject {};
|
|
|
|
|
|
|
|
QDebug operator<<(QDebug dbg, const CustomObject &)
|
|
|
|
{
|
|
|
|
return dbg << "MY OBJECT";
|
|
|
|
}
|
|
|
|
|
|
|
|
Q_DECLARE_METATYPE(CustomObject)
|
|
|
|
|
2012-02-16 04:43:03 +00:00
|
|
|
void tst_qqmlconsole::logging()
|
2011-11-18 15:41:08 +00:00
|
|
|
{
|
2012-01-16 12:35:07 +00:00
|
|
|
QUrl testUrl = testFileUrl("logging.qml");
|
|
|
|
|
2013-10-23 13:47:30 +00:00
|
|
|
QLoggingCategory loggingCategory("qml");
|
|
|
|
QVERIFY(loggingCategory.isDebugEnabled());
|
|
|
|
QVERIFY(loggingCategory.isWarningEnabled());
|
|
|
|
QVERIFY(loggingCategory.isCriticalEnabled());
|
|
|
|
|
2012-01-16 12:35:07 +00:00
|
|
|
QTest::ignoreMessage(QtDebugMsg, "console.debug");
|
|
|
|
QTest::ignoreMessage(QtDebugMsg, "console.log");
|
2014-12-12 10:10:45 +00:00
|
|
|
QTest::ignoreMessage(QtInfoMsg, "console.info");
|
2012-01-16 12:35:07 +00:00
|
|
|
QTest::ignoreMessage(QtWarningMsg, "console.warn");
|
|
|
|
QTest::ignoreMessage(QtCriticalMsg, "console.error");
|
|
|
|
|
2012-01-16 14:40:27 +00:00
|
|
|
QTest::ignoreMessage(QtDebugMsg, "console.count: 1");
|
|
|
|
QTest::ignoreMessage(QtDebugMsg, ": 1");
|
|
|
|
QTest::ignoreMessage(QtDebugMsg, "console.count: 2");
|
|
|
|
QTest::ignoreMessage(QtDebugMsg, ": 2");
|
|
|
|
|
2012-01-16 12:35:07 +00:00
|
|
|
QTest::ignoreMessage(QtDebugMsg, "[1,2]");
|
2012-06-05 11:34:40 +00:00
|
|
|
QTest::ignoreMessage(QtDebugMsg, "{\"a\":\"hello\",\"d\":1}");
|
2012-01-16 12:35:07 +00:00
|
|
|
QTest::ignoreMessage(QtDebugMsg, "undefined");
|
|
|
|
QTest::ignoreMessage(QtDebugMsg, "12");
|
2018-07-31 07:43:43 +00:00
|
|
|
QTest::ignoreMessage(QtDebugMsg, "function e() { [native code] }");
|
2012-01-16 12:35:07 +00:00
|
|
|
QTest::ignoreMessage(QtDebugMsg, "true");
|
2012-06-05 11:34:40 +00:00
|
|
|
// Printing QML object prints out the class/type of QML object with the memory address
|
|
|
|
// QTest::ignoreMessage(QtDebugMsg, "QtObject_QML_0(0xABCD..)");
|
2013-06-17 12:27:06 +00:00
|
|
|
// QTest::ignoreMessage(QtDebugMsg, "[object Object]");
|
2012-06-05 11:34:40 +00:00
|
|
|
QTest::ignoreMessage(QtDebugMsg, "1 pong! [object Object]");
|
|
|
|
QTest::ignoreMessage(QtDebugMsg, "1 [ping,pong] [object Object] 2");
|
2018-04-18 07:38:39 +00:00
|
|
|
QTest::ignoreMessage(QtDebugMsg, "[Hello,World]");
|
2018-11-06 13:55:47 +00:00
|
|
|
QTest::ignoreMessage(QtDebugMsg, "QVariant(CustomObject, MY OBJECT)");
|
2019-01-03 13:33:30 +00:00
|
|
|
QTest::ignoreMessage(QtDebugMsg, "[[1,2,3,[2,2,2,2],4],[5,6,7,8]]");
|
2018-04-18 07:38:39 +00:00
|
|
|
|
|
|
|
QStringList stringList; stringList << QStringLiteral("Hello") << QStringLiteral("World");
|
2012-01-16 12:35:07 +00:00
|
|
|
|
2018-11-06 13:55:47 +00:00
|
|
|
CustomObject customObject;
|
|
|
|
|
2012-02-16 04:43:03 +00:00
|
|
|
QQmlComponent component(&engine, testUrl);
|
2019-10-04 09:44:31 +00:00
|
|
|
QScopedPointer<QObject> object(component.createWithInitialProperties({
|
|
|
|
{"customObject", QVariant::fromValue(customObject)},
|
|
|
|
{"stringListProperty", stringList}
|
|
|
|
}));
|
2018-02-21 09:41:54 +00:00
|
|
|
QVERIFY(object != nullptr);
|
2011-11-18 15:41:08 +00:00
|
|
|
}
|
|
|
|
|
2016-05-02 15:29:40 +00:00
|
|
|
void tst_qqmlconsole::categorized_logging()
|
|
|
|
{
|
|
|
|
QUrl testUrl = testFileUrl("categorized_logging.qml");
|
|
|
|
QQmlTestMessageHandler messageHandler;
|
|
|
|
messageHandler.setIncludeCategoriesEnabled(true);
|
|
|
|
|
|
|
|
QLoggingCategory testCategory("qt.test");
|
|
|
|
testCategory.setEnabled(QtDebugMsg, true);
|
|
|
|
QVERIFY(testCategory.isDebugEnabled());
|
|
|
|
QVERIFY(testCategory.isWarningEnabled());
|
|
|
|
QVERIFY(testCategory.isCriticalEnabled());
|
|
|
|
|
|
|
|
QQmlComponent component(&engine, testUrl);
|
|
|
|
QObject *object = component.create();
|
2018-02-21 09:41:54 +00:00
|
|
|
QVERIFY2(object != nullptr, component.errorString().toUtf8());
|
2016-05-02 15:29:40 +00:00
|
|
|
|
|
|
|
QVERIFY(messageHandler.messages().contains("qt.test: console.info"));
|
|
|
|
QVERIFY(messageHandler.messages().contains("qt.test: console.warn"));
|
|
|
|
QVERIFY(messageHandler.messages().contains("qt.test: console.error"));
|
2018-03-22 10:25:59 +00:00
|
|
|
QVERIFY(!messageHandler.messages().contains("qt.test.warning: console.debug"));
|
|
|
|
QVERIFY(!messageHandler.messages().contains("qt.test.warning: console.log"));
|
|
|
|
QVERIFY(!messageHandler.messages().contains("qt.test.warning: console.info"));
|
|
|
|
QVERIFY(messageHandler.messages().contains("qt.test.warning: console.warn"));
|
|
|
|
QVERIFY(messageHandler.messages().contains("qt.test.warning: console.error"));
|
2016-05-02 15:29:40 +00:00
|
|
|
|
2022-05-13 13:12:05 +00:00
|
|
|
QString emptyCategory = "default: " + QString::fromLatin1("%1:%2:%3: ").arg(testUrl.toString()).arg(20).arg(5) +
|
2021-10-22 14:13:15 +00:00
|
|
|
"QML LoggingCategory: Declaring the name of a LoggingCategory is mandatory and cannot be changed later";
|
2016-05-02 15:29:40 +00:00
|
|
|
QVERIFY(messageHandler.messages().contains(emptyCategory));
|
|
|
|
|
2021-10-22 14:13:15 +00:00
|
|
|
|
2022-05-13 13:12:05 +00:00
|
|
|
QString notChangedCategory = "default: " + QString::fromLatin1("%1:%2:%3: ").arg(testUrl.toString()).arg(9).arg(5) +
|
2021-10-22 14:13:15 +00:00
|
|
|
"QML LoggingCategory: The name of a LoggingCategory cannot be changed after the component is completed";
|
|
|
|
QVERIFY(!messageHandler.messages().contains(notChangedCategory));
|
2022-05-13 13:12:05 +00:00
|
|
|
QString changedCategory = "default: " + QString::fromLatin1("%1:%2:%3: ").arg(testUrl.toString()).arg(14).arg(5) +
|
2021-10-22 14:13:15 +00:00
|
|
|
"QML LoggingCategory: The name of a LoggingCategory cannot be changed after the component is completed";
|
2016-05-02 15:29:40 +00:00
|
|
|
QVERIFY(messageHandler.messages().contains(changedCategory));
|
|
|
|
|
2021-10-22 14:13:15 +00:00
|
|
|
|
2022-05-13 13:12:05 +00:00
|
|
|
QString notChangedDefaultLogLevel = "default: " + QString::fromLatin1("%1:%2:%3: ").arg(testUrl.toString()).arg(9).arg(5) +
|
2021-10-22 14:13:15 +00:00
|
|
|
"QML LoggingCategory: The defaultLogLevel of a LoggingCategory cannot be changed after the component is completed";
|
|
|
|
QVERIFY(!messageHandler.messages().contains(notChangedDefaultLogLevel));
|
2022-05-13 13:12:05 +00:00
|
|
|
QString changedDefaultLogLevel = "default: " + QString::fromLatin1("%1:%2:%3: ").arg(testUrl.toString()).arg(14).arg(5) +
|
2021-10-22 14:13:15 +00:00
|
|
|
"QML LoggingCategory: The defaultLogLevel of a LoggingCategory cannot be changed after the component is completed";
|
2018-03-22 10:25:59 +00:00
|
|
|
QVERIFY(messageHandler.messages().contains(changedDefaultLogLevel));
|
|
|
|
|
2021-10-22 14:13:15 +00:00
|
|
|
|
2022-05-13 13:12:05 +00:00
|
|
|
QString useEmptyCategory = "default: " + QString::fromLatin1("%1:%2: ").arg(testUrl.toString()).arg(42) +
|
2016-05-02 15:29:40 +00:00
|
|
|
"Error: A QmlLoggingCatgory was provided without a valid name";
|
|
|
|
QVERIFY(messageHandler.messages().contains(useEmptyCategory));
|
|
|
|
|
|
|
|
delete object;
|
|
|
|
}
|
|
|
|
|
2012-02-16 04:43:03 +00:00
|
|
|
void tst_qqmlconsole::tracing()
|
2011-11-18 15:41:08 +00:00
|
|
|
{
|
2012-01-16 12:35:07 +00:00
|
|
|
QUrl testUrl = testFileUrl("tracing.qml");
|
|
|
|
|
2022-05-13 13:12:05 +00:00
|
|
|
QString traceText = QString::fromLatin1("tracing (%1:%2)\n").arg(testUrl.toString()).arg(12)
|
2021-02-25 09:37:51 +00:00
|
|
|
+ QString::fromLatin1("expression for onCompleted (%1:%2)")
|
|
|
|
.arg(testUrl.toString())
|
2022-05-13 13:12:05 +00:00
|
|
|
.arg(16);
|
2012-02-13 13:35:21 +00:00
|
|
|
|
|
|
|
QTest::ignoreMessage(QtDebugMsg, qPrintable(traceText));
|
2011-11-18 15:41:08 +00:00
|
|
|
|
2012-02-16 04:43:03 +00:00
|
|
|
QQmlComponent component(&engine, testUrl);
|
2011-11-18 15:41:08 +00:00
|
|
|
QObject *object = component.create();
|
2018-02-21 09:41:54 +00:00
|
|
|
QVERIFY(object != nullptr);
|
2011-11-18 15:41:08 +00:00
|
|
|
delete object;
|
|
|
|
}
|
|
|
|
|
2012-02-16 04:43:03 +00:00
|
|
|
void tst_qqmlconsole::profiling()
|
2012-01-16 12:35:07 +00:00
|
|
|
{
|
|
|
|
QUrl testUrl = testFileUrl("profiling.qml");
|
|
|
|
|
|
|
|
// profiling()
|
2014-01-15 11:30:36 +00:00
|
|
|
QTest::ignoreMessage(QtWarningMsg, "Cannot start profiling because debug service is disabled. Start with -qmljsdebugger=port:XXXXX.");
|
2014-01-23 16:18:05 +00:00
|
|
|
QTest::ignoreMessage(QtWarningMsg, "Ignoring console.profileEnd(): the debug service is disabled.");
|
2012-01-16 12:35:07 +00:00
|
|
|
|
2012-02-16 04:43:03 +00:00
|
|
|
QQmlComponent component(&engine, testUrl);
|
2012-01-16 12:35:07 +00:00
|
|
|
QObject *object = component.create();
|
2018-02-21 09:41:54 +00:00
|
|
|
QVERIFY(object != nullptr);
|
2012-01-16 12:35:07 +00:00
|
|
|
delete object;
|
|
|
|
}
|
|
|
|
|
2015-08-07 15:25:45 +00:00
|
|
|
void tst_qqmlconsole::testAssert()
|
2012-01-17 11:37:07 +00:00
|
|
|
{
|
|
|
|
QUrl testUrl = testFileUrl("assert.qml");
|
|
|
|
|
|
|
|
// assert()
|
2021-02-25 09:37:51 +00:00
|
|
|
QString assert1 = "This will fail\n"
|
|
|
|
+ QString::fromLatin1("expression for onCompleted (%1:%2)")
|
|
|
|
.arg(testUrl.toString())
|
2022-05-13 13:12:05 +00:00
|
|
|
.arg(17);
|
2012-02-13 13:35:21 +00:00
|
|
|
|
2021-02-25 09:37:51 +00:00
|
|
|
QString assert2 = "This will fail too\n"
|
2022-05-13 13:12:05 +00:00
|
|
|
+ QString::fromLatin1("assertFail (%1:%2)\n").arg(testUrl.toString()).arg(10)
|
2021-02-25 09:37:51 +00:00
|
|
|
+ QString::fromLatin1("expression for onCompleted (%1:%2)")
|
|
|
|
.arg(testUrl.toString())
|
2022-05-13 13:12:05 +00:00
|
|
|
.arg(22);
|
2012-02-13 13:35:21 +00:00
|
|
|
|
|
|
|
QTest::ignoreMessage(QtCriticalMsg, qPrintable(assert1));
|
|
|
|
QTest::ignoreMessage(QtCriticalMsg, qPrintable(assert2));
|
2012-01-17 11:37:07 +00:00
|
|
|
|
2012-02-16 04:43:03 +00:00
|
|
|
QQmlComponent component(&engine, testUrl);
|
2012-01-17 11:37:07 +00:00
|
|
|
QObject *object = component.create();
|
2018-02-21 09:41:54 +00:00
|
|
|
QVERIFY(object != nullptr);
|
2012-01-17 11:37:07 +00:00
|
|
|
delete object;
|
|
|
|
}
|
2012-01-16 12:35:07 +00:00
|
|
|
|
2012-02-16 04:43:03 +00:00
|
|
|
void tst_qqmlconsole::exception()
|
2012-01-17 13:38:06 +00:00
|
|
|
{
|
|
|
|
QUrl testUrl = testFileUrl("exception.qml");
|
|
|
|
|
|
|
|
// exception()
|
2021-02-25 09:37:51 +00:00
|
|
|
QString exception1 = "Exception 1\n"
|
|
|
|
+ QString::fromLatin1("expression for onCompleted (%1:%2)")
|
|
|
|
.arg(testUrl.toString())
|
2022-05-13 13:12:05 +00:00
|
|
|
.arg(13);
|
2021-02-25 09:37:51 +00:00
|
|
|
|
|
|
|
QString exception2 = "Exception 2\n"
|
2022-05-13 13:12:05 +00:00
|
|
|
+ QString::fromLatin1("exceptionFail (%1:%2)\n").arg(testUrl.toString()).arg(8)
|
2021-02-25 09:37:51 +00:00
|
|
|
+ QString::fromLatin1("expression for onCompleted (%1:%2)")
|
|
|
|
.arg(testUrl.toString())
|
2022-05-13 13:12:05 +00:00
|
|
|
.arg(18);
|
2012-02-13 13:35:21 +00:00
|
|
|
|
|
|
|
QTest::ignoreMessage(QtCriticalMsg, qPrintable(exception1));
|
|
|
|
QTest::ignoreMessage(QtCriticalMsg, qPrintable(exception2));
|
2012-01-17 13:38:06 +00:00
|
|
|
|
2012-02-16 04:43:03 +00:00
|
|
|
QQmlComponent component(&engine, testUrl);
|
2012-01-17 13:38:06 +00:00
|
|
|
QObject *object = component.create();
|
2018-02-21 09:41:54 +00:00
|
|
|
QVERIFY(object != nullptr);
|
2012-01-17 13:38:06 +00:00
|
|
|
delete object;
|
|
|
|
}
|
|
|
|
|
2012-02-16 04:43:03 +00:00
|
|
|
QTEST_MAIN(tst_qqmlconsole)
|
2011-11-18 15:41:08 +00:00
|
|
|
|
2012-02-16 04:43:03 +00:00
|
|
|
#include "tst_qqmlconsole.moc"
|