diff --git a/src/qml/qml/qqmlloggingcategory.cpp b/src/qml/qml/qqmlloggingcategory.cpp index b59a26e17e..98dfe62f0c 100644 --- a/src/qml/qml/qqmlloggingcategory.cpp +++ b/src/qml/qml/qqmlloggingcategory.cpp @@ -129,7 +129,7 @@ void QQmlLoggingCategory::componentComplete() { m_initialized = true; if (m_name.isNull()) { - qmlWarning(this) << QLatin1String("Declaring the name of the LoggingCategory is mandatory and cannot be changed later !"); + qmlWarning(this) << QLatin1String("Declaring the name of a LoggingCategory is mandatory and cannot be changed later"); } else { QScopedPointer category(new QLoggingCategory(m_name.constData(), QtMsgType(m_defaultLogLevel))); m_category.swap(category); @@ -138,23 +138,30 @@ void QQmlLoggingCategory::componentComplete() void QQmlLoggingCategory::setDefaultLogLevel(DefaultLogLevel defaultLogLevel) { + if (m_defaultLogLevel == defaultLogLevel) + return; + if (m_initialized) { - qmlWarning(this) << QLatin1String("The defaultLogLevel of a LoggingCategory cannot be changed after the Item is created"); + qmlWarning(this) << QLatin1String("The defaultLogLevel of a LoggingCategory cannot be changed after the component is completed"); return; } m_defaultLogLevel = defaultLogLevel; } - void QQmlLoggingCategory::setName(const QString &name) { + const QByteArray newName = name.toUtf8(); + + if (m_name == newName) + return; + if (m_initialized) { - qmlWarning(this) << QLatin1String("The name of a LoggingCategory cannot be changed after the Item is created"); + qmlWarning(this) << QLatin1String("The name of a LoggingCategory cannot be changed after the component is completed"); return; } - m_name = name.toUtf8(); + m_name = newName; } #include "moc_qqmlloggingcategory_p.cpp" diff --git a/tests/auto/qml/qqmlconsole/data/assert.qml b/tests/auto/qml/qqmlconsole/data/assert.qml index dd580e2a72..9687437c48 100644 --- a/tests/auto/qml/qqmlconsole/data/assert.qml +++ b/tests/auto/qml/qqmlconsole/data/assert.qml @@ -29,21 +29,22 @@ import QtQuick 2.0 QtObject { - property int q:1 + property int q: 1 + function assertFail() { - console.assert(0, "This will fail too") + console.assert(0, "This will fail too"); } Component.onCompleted: { - var x = 12; + const x = 12; console.assert(x == 12, "This will pass"); try { - console.assert(x < 12, "This will fail"); + console.assert(x < 12, "This will fail"); } catch (e) { console.log(e); } - console.assert("x < 12", "This will pass too") + console.assert("x < 12", "This will pass too"); assertFail(); - console.assert(1) + console.assert(1); } } diff --git a/tests/auto/qml/qqmlconsole/data/categorized_logging.qml b/tests/auto/qml/qqmlconsole/data/categorized_logging.qml index d593f0dfa1..6d471e7f80 100644 --- a/tests/auto/qml/qqmlconsole/data/categorized_logging.qml +++ b/tests/auto/qml/qqmlconsole/data/categorized_logging.qml @@ -40,7 +40,7 @@ import QtQuick 2.12 Item { - id:root + id: root LoggingCategory { id: testCategory @@ -69,8 +69,11 @@ Item { console.warn(testCategoryStartingFromWarning, "console.warn"); console.error(testCategoryStartingFromWarning, "console.error"); - testCategory.name = "qt.test2"; - testCategory.defaultLogLevel = LoggingCategory.Debug; + testCategory.name = "qt.test"; // should be silent + testCategoryStartingFromWarning.name = "qt.test.other"; // should issue a warning + + testCategory.defaultLogLevel = LoggingCategory.Debug; // should be silent + testCategoryStartingFromWarning.defaultLogLevel = LoggingCategory.Debug; // should issue a warning console.error(emptyCategory, "console.error"); } diff --git a/tests/auto/qml/qqmlconsole/data/exception.qml b/tests/auto/qml/qqmlconsole/data/exception.qml index 63afd18828..b9b83525e8 100644 --- a/tests/auto/qml/qqmlconsole/data/exception.qml +++ b/tests/auto/qml/qqmlconsole/data/exception.qml @@ -30,12 +30,12 @@ import QtQuick 2.0 QtObject { function exceptionFail() { - console.exception("Exception 2") + console.exception("Exception 2"); } Component.onCompleted: { try { - console.exception("Exception 1") + console.exception("Exception 1"); } catch (e) { console.log(e); } diff --git a/tests/auto/qml/qqmlconsole/data/logging.qml b/tests/auto/qml/qqmlconsole/data/logging.qml index f5eaeb442a..ac3884bc8e 100644 --- a/tests/auto/qml/qqmlconsole/data/logging.qml +++ b/tests/auto/qml/qqmlconsole/data/logging.qml @@ -30,7 +30,8 @@ import QtQuick 2.0 QtObject { - id:root + id: root + required property var customObject required property var stringListProperty @@ -49,14 +50,14 @@ QtObject { consoleCount(); consoleCount(); - var a = [1, 2]; - var b = {a: "hello", d: 1 }; - b.toString = function() { return JSON.stringify(b) } - var c - var d = 12; - var e = function() { return 5;}; - var f = true; - var g = {toString: function() { throw new Error('toString'); }}; + const a = [1, 2]; + const b = { a: "hello", d: 1 }; + b.toString = function() { return JSON.stringify(b); } + let c; + const d = 12; + const e = function() { return 5; }; + const f = true; + const g = { toString: function() { throw new Error('toString'); } }; console.log(a); console.log(b); @@ -79,6 +80,6 @@ QtObject { return; } - throw ("console.log(exception) should have raised an exception"); + throw "console.log(exception) should have raised an exception"; } } diff --git a/tests/auto/qml/qqmlconsole/tst_qqmlconsole.cpp b/tests/auto/qml/qqmlconsole/tst_qqmlconsole.cpp index 469d2fca98..019be5f16a 100644 --- a/tests/auto/qml/qqmlconsole/tst_qqmlconsole.cpp +++ b/tests/auto/qml/qqmlconsole/tst_qqmlconsole.cpp @@ -133,18 +133,27 @@ void tst_qqmlconsole::categorized_logging() QVERIFY(messageHandler.messages().contains("qt.test.warning: console.error")); QString emptyCategory = "default: " + QString::fromLatin1("%1:%2:%3: ").arg(testUrl.toString()).arg(56).arg(5) + - "QML LoggingCategory: Declaring the name of the LoggingCategory is mandatory and cannot be changed later !"; + "QML LoggingCategory: Declaring the name of a 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"; + + QString notChangedCategory = "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 component is completed"; + QVERIFY(!messageHandler.messages().contains(notChangedCategory)); + QString changedCategory = "default: " + QString::fromLatin1("%1:%2:%3: ").arg(testUrl.toString()).arg(50).arg(5) + + "QML LoggingCategory: The name of a LoggingCategory cannot be changed after the component is completed"; QVERIFY(messageHandler.messages().contains(changedCategory)); - 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"; + + QString notChangedDefaultLogLevel = "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 component is completed"; + QVERIFY(!messageHandler.messages().contains(notChangedDefaultLogLevel)); + QString changedDefaultLogLevel = "default: " + QString::fromLatin1("%1:%2:%3: ").arg(testUrl.toString()).arg(50).arg(5) + + "QML LoggingCategory: The defaultLogLevel of a LoggingCategory cannot be changed after the component is completed"; QVERIFY(messageHandler.messages().contains(changedDefaultLogLevel)); - QString useEmptyCategory = "default: " + QString::fromLatin1("%1:%2: ").arg(testUrl.toString()).arg(75) + + + QString useEmptyCategory = "default: " + QString::fromLatin1("%1:%2: ").arg(testUrl.toString()).arg(78) + "Error: A QmlLoggingCatgory was provided without a valid name"; QVERIFY(messageHandler.messages().contains(useEmptyCategory)); @@ -190,13 +199,13 @@ void tst_qqmlconsole::testAssert() QString assert1 = "This will fail\n" + QString::fromLatin1("expression for onCompleted (%1:%2)") .arg(testUrl.toString()) - .arg(41); + .arg(42); QString assert2 = "This will fail too\n" - + QString::fromLatin1("assertFail (%1:%2)\n").arg(testUrl.toString()).arg(34) + + QString::fromLatin1("assertFail (%1:%2)\n").arg(testUrl.toString()).arg(35) + QString::fromLatin1("expression for onCompleted (%1:%2)") .arg(testUrl.toString()) - .arg(46); + .arg(47); QTest::ignoreMessage(QtCriticalMsg, qPrintable(assert1)); QTest::ignoreMessage(QtCriticalMsg, qPrintable(assert2));