[utilities/kdebugsettings] src/core/autotests: Add autotests
Laurent Montel <[email protected]>
| Newsgroups | gmane.comp.kde.cvs |
|---|---|
| Message-ID | <[email protected]> |
Git commit 20d8eec3e9b461dbafb039ca350de29c706a4cd1 by Laurent Montel. Committed on 18/07/2026 at 19:54. Pushed by mlaurent into branch 'master'. Add autotests M +1 -0 src/core/autotests/CMakeLists.txt A +75 -0 src/core/autotests/kdeapplicationloggingcategorymodeltest.cpp [License: LGPL(v2.0+)] A +21 -0 src/core/autotests/kdeapplicationloggingcategorymodeltest.h [License: LGPL(v2.0+)] https://invent.kde.org/utilities/kdebugsettings/-/commit/20d8eec3e9b461dbafb039ca350de29c706a4cd1 diff --git a/src/core/autotests/CMakeLists.txt b/src/core/autotests/CMakeLists.txt index f7f82d35..48eca20f 100644 --- a/src/core/autotests/CMakeLists.txt +++ b/src/core/autotests/CMakeLists.txt @@ -28,3 +28,4 @@ add_kdebugsettingscore_unittest(saverulesjobtest.cpp) add_kdebugsettingscore_unittest(changedebugmodejobtest.cpp) add_kdebugsettingscore_unittest(categorytypemodeltest.cpp) add_kdebugsettingscore_unittest(kdebugsettingscommandlineparsertest.cpp) +add_kdebugsettingscore_unittest(kdeapplicationloggingcategorymodeltest.cpp) diff --git a/src/core/autotests/kdeapplicationloggingcategorymodeltest.cpp b/src/core/autotests/kdeapplicationloggingcategorymodeltest.cpp new file mode 100644 index 00000000..fb4e738a --- /dev/null +++ b/src/core/autotests/kdeapplicationloggingcategorymodeltest.cpp @@ -0,0 +1,75 @@ +/* + SPDX-FileCopyrightText: 2026 Laurent Montel <[email protected]> + + SPDX-License-Identifier: LGPL-2.0-or-later +*/ + +#include "kdeapplicationloggingcategorymodeltest.h" +#include "model/kdeapplicationloggingcategorymodel.h" + +#include <QTest> + +namespace +{ +constexpr int qmlRole(KDEApplicationLoggingCategoryModel::CategoryRoles role) +{ + return static_cast<int>(Qt::UserRole) + static_cast<int>(role) + 1; +} +} + +QTEST_GUILESS_MAIN(KDEApplicationLoggingCategoryModelTest) + +KDEApplicationLoggingCategoryModelTest::KDEApplicationLoggingCategoryModelTest(QObject *parent) + : QObject{parent} +{ +} + +void KDEApplicationLoggingCategoryModelTest::shouldExposeUserRolesForQml() +{ + const KDEApplicationLoggingCategoryModel model; + const auto roles = model.roleNames(); + + QVERIFY(!roles.contains(Qt::DisplayRole)); + QVERIFY(!roles.contains(KDEApplicationLoggingCategoryModel::DescriptionRole)); + + QCOMPARE(roles.value(qmlRole(KDEApplicationLoggingCategoryModel::DescriptionRole)), QByteArray("description")); + QCOMPARE(roles.value(qmlRole(KDEApplicationLoggingCategoryModel::LoggingTypeRole)), QByteArray("loggingType")); + QCOMPARE(roles.value(qmlRole(KDEApplicationLoggingCategoryModel::CategoryRole)), QByteArray("category")); + QCOMPARE(roles.value(qmlRole(KDEApplicationLoggingCategoryModel::LoggingTypeStrRole)), QByteArray("loggingTypeStr")); +} + +void KDEApplicationLoggingCategoryModelTest::shouldReturnExpectedDataForWidgetsAndQml() +{ + KDEApplicationLoggingCategoryModel model; + + LoggingCategory cat(QStringLiteral("desc"), QStringLiteral("org.kde.test"), LoggingCategory::Warning, QStringLiteral("ident"), true); + cat.defaultSeverityType = LoggingCategory::Info; + model.setLoggingCategories({cat}); + + QCOMPARE(model.rowCount(), 1); + QCOMPARE(model.columnCount(), static_cast<int>(KDEApplicationLoggingCategoryModel::LastColumn) + 1); + + const QModelIndex descriptionIndex = model.index(0, KDEApplicationLoggingCategoryModel::DescriptionRole); + const QModelIndex typeIndex = model.index(0, KDEApplicationLoggingCategoryModel::LoggingTypeRole); + const QModelIndex categoryIndex = model.index(0, KDEApplicationLoggingCategoryModel::CategoryRole); + const QModelIndex typeStrIndex = model.index(0, KDEApplicationLoggingCategoryModel::LoggingTypeStrRole); + + QCOMPARE(model.data(descriptionIndex, Qt::DisplayRole).toString(), QStringLiteral("desc")); + QCOMPARE(model.data(typeIndex, Qt::DisplayRole).value<LoggingCategory::LoggingType>(), LoggingCategory::Warning); + QCOMPARE(model.data(categoryIndex, Qt::DisplayRole).value<LoggingCategory>(), cat); + QCOMPARE(model.data(typeStrIndex, Qt::DisplayRole).toString(), + model.data(descriptionIndex, qmlRole(KDEApplicationLoggingCategoryModel::LoggingTypeStrRole)).toString()); + + // QML uses role-based lookup on any column index. + QCOMPARE(model.data(descriptionIndex, qmlRole(KDEApplicationLoggingCategoryModel::DescriptionRole)).toString(), QStringLiteral("desc")); + QCOMPARE(model.data(descriptionIndex, qmlRole(KDEApplicationLoggingCategoryModel::LoggingTypeRole)).value<LoggingCategory::LoggingType>(), + LoggingCategory::Warning); + QCOMPARE(model.data(descriptionIndex, qmlRole(KDEApplicationLoggingCategoryModel::CategoryRole)).value<LoggingCategory>(), cat); + QCOMPARE(model.data(descriptionIndex, qmlRole(KDEApplicationLoggingCategoryModel::LoggingTypeStrRole)).toString(), + model.data(typeStrIndex, Qt::DisplayRole).toString()); + + // Role value 0 is Qt::DisplayRole, not the QML "description" role. + QCOMPARE(model.data(descriptionIndex, KDEApplicationLoggingCategoryModel::DescriptionRole).toString(), QStringLiteral("desc")); +} + +#include "moc_kdeapplicationloggingcategorymodeltest.cpp" diff --git a/src/core/autotests/kdeapplicationloggingcategorymodeltest.h b/src/core/autotests/kdeapplicationloggingcategorymodeltest.h new file mode 100644 index 00000000..6b159320 --- /dev/null +++ b/src/core/autotests/kdeapplicationloggingcategorymodeltest.h @@ -0,0 +1,21 @@ +/* + SPDX-FileCopyrightText: 2026 Laurent Montel <[email protected]> + + SPDX-License-Identifier: LGPL-2.0-or-later +*/ + +#pragma once + +#include <QObject> + +class KDEApplicationLoggingCategoryModelTest : public QObject +{ + Q_OBJECT +public: + explicit KDEApplicationLoggingCategoryModelTest(QObject *parent = nullptr); + ~KDEApplicationLoggingCategoryModelTest() override = default; + +private Q_SLOTS: + void shouldExposeUserRolesForQml(); + void shouldReturnExpectedDataForWidgetsAndQml(); +};