EditableTreeModel: Add a test

Add a test running QAbstractItemModelTester on the model.

Change-Id: I40c141c7e754ca05234da611534bd65e456be2fb
Reviewed-by: Kai Köhne <kai.koehne@qt.io>
(cherry picked from commit 283cdcd3d5fad368c9df1bcae69cbfbf9ade623f)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Friedemann Kleint 2023-11-28 13:57:47 +01:00
parent a51ee84a61
commit cbb0dabc4c
3 changed files with 79 additions and 1 deletions

View File

@ -419,4 +419,19 @@
\target TreeModel::data \target TreeModel::data
\target TreeModel::setupModelData \target TreeModel::setupModelData
\section1 Testing the model
Correctly implementing an item model can be challenging. The class
\l QAbstractItemModelTester from the \l{Qt Test} module checks for model
consistency, like the model index creation and parent-child relationships.
You can test your model by just passing a model instance to the class
constructor, for instance as part of a Qt unit test:
\snippet itemviews/editabletreemodel/test.cpp 1
To create a test which can be run using the \c ctest executable,
\c add_test() is used:
\snippet itemviews/editabletreemodel/CMakeLists.txt 1
*/ */

View File

@ -10,7 +10,7 @@ endif()
set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/widgets/itemviews/editabletreemodel") set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/widgets/itemviews/editabletreemodel")
find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets) find_package(Qt6 REQUIRED COMPONENTS Core Gui Test Widgets)
qt_standard_project_setup() qt_standard_project_setup()
@ -49,3 +49,29 @@ install(TARGETS editabletreemodel
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
) )
#! [1]
# Unit Test
include(CTest)
qt_add_executable(editabletreemodel_tester
test.cpp
treeitem.cpp treeitem.h
treemodel.cpp treemodel.h)
target_link_libraries(editabletreemodel_tester PRIVATE
Qt6::Core
Qt6::Test
)
qt_add_resources(editabletreemodel_tester "editabletreemodel"
PREFIX
"/"
FILES
${editabletreemodel_resource_files}
)
add_test(NAME editabletreemodel_tester
COMMAND editabletreemodel_tester)
#! [1]

View File

@ -0,0 +1,37 @@
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "treemodel.h"
#include <QAbstractItemModelTester>
#include <QObject>
#include <QTest>
using namespace Qt::StringLiterals;
//! [1]
class TestEditableTreeModel : public QObject
{
Q_OBJECT
private slots:
void testTreeModel();
};
void TestEditableTreeModel::testTreeModel()
{
constexpr auto fileName = ":/default.txt"_L1;
QFile file(fileName);
QVERIFY2(file.open(QIODevice::ReadOnly | QIODevice::Text),
qPrintable(fileName + " cannot be opened: "_L1 + file.errorString()));
const QStringList headers{"column1"_L1, "column2"_L1};
TreeModel model(headers, QString::fromUtf8(file.readAll()));
QAbstractItemModelTester tester(&model);
}
QTEST_APPLESS_MAIN(TestEditableTreeModel)
#include "test.moc"
//! [1]