2011-11-18 15:41:08 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2016-01-19 11:23:05 +00:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2011-11-18 15:41:08 +00:00
|
|
|
**
|
|
|
|
** This file is part of the test suite of the Qt Toolkit.
|
|
|
|
**
|
2016-01-19 11:23:05 +00:00
|
|
|
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
|
2012-09-20 05:21:40 +00:00
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2015-01-28 11:55:39 +00:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
2016-01-19 11:23:05 +00:00
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2012-09-20 05:21:40 +00:00
|
|
|
**
|
2016-01-19 11:23:05 +00:00
|
|
|
** GNU General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2011-11-18 15:41:08 +00:00
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
#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
|
|
|
|
2018-03-22 10:25:59 +00:00
|
|
|
QString emptyCategory = "default: " + QString::fromLatin1("%1:%2:%3: ").arg(testUrl.toString()).arg(56).arg(5) +
|
2016-05-02 15:29:40 +00:00
|
|
|
"QML LoggingCategory: Declaring the name of the LoggingCategory is mandatory and cannot be changed later !";
|
|
|
|
QVERIFY(messageHandler.messages().contains(emptyCategory));
|
|
|
|
|
|
|
|
QString changedCategory = "default: " + QString::fromLatin1("%1:%2:%3: ").arg(testUrl.toString()).arg(45).arg(5) +
|
|
|
|
"QML LoggingCategory: The name of a LoggingCategory cannot be changed after the Item is created";
|
|
|
|
QVERIFY(messageHandler.messages().contains(changedCategory));
|
|
|
|
|
2018-03-22 10:25:59 +00:00
|
|
|
QString changedDefaultLogLevel = "default: " + QString::fromLatin1("%1:%2:%3: ").arg(testUrl.toString()).arg(45).arg(5) +
|
|
|
|
"QML LoggingCategory: The defaultLogLevel of a LoggingCategory cannot be changed after the Item is created";
|
|
|
|
QVERIFY(messageHandler.messages().contains(changedDefaultLogLevel));
|
|
|
|
|
|
|
|
QString useEmptyCategory = "default: " + QString::fromLatin1("%1:%2: ").arg(testUrl.toString()).arg(75) +
|
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");
|
|
|
|
|
2021-02-25 09:37:51 +00:00
|
|
|
QString traceText = QString::fromLatin1("tracing (%1:%2)\n").arg(testUrl.toString()).arg(37)
|
|
|
|
+ QString::fromLatin1("expression for onCompleted (%1:%2)")
|
|
|
|
.arg(testUrl.toString())
|
|
|
|
.arg(41);
|
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())
|
|
|
|
.arg(41);
|
2012-02-13 13:35:21 +00:00
|
|
|
|
2021-02-25 09:37:51 +00:00
|
|
|
QString assert2 = "This will fail too\n"
|
|
|
|
+ QString::fromLatin1("assertFail (%1:%2)\n").arg(testUrl.toString()).arg(34)
|
|
|
|
+ QString::fromLatin1("expression for onCompleted (%1:%2)")
|
|
|
|
.arg(testUrl.toString())
|
|
|
|
.arg(46);
|
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())
|
|
|
|
.arg(38);
|
|
|
|
|
|
|
|
QString exception2 = "Exception 2\n"
|
|
|
|
+ QString::fromLatin1("exceptionFail (%1:%2)\n").arg(testUrl.toString()).arg(33)
|
|
|
|
+ QString::fromLatin1("expression for onCompleted (%1:%2)")
|
|
|
|
.arg(testUrl.toString())
|
|
|
|
.arg(43);
|
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"
|