diff options
Diffstat (limited to 'tests/auto/gui/itemmodels')
7 files changed, 242 insertions, 71 deletions
diff --git a/tests/auto/gui/itemmodels/CMakeLists.txt b/tests/auto/gui/itemmodels/CMakeLists.txt index d61eac78c4..4c25418ef1 100644 --- a/tests/auto/gui/itemmodels/CMakeLists.txt +++ b/tests/auto/gui/itemmodels/CMakeLists.txt @@ -1,8 +1,6 @@ # Copyright (C) 2022 The Qt Company Ltd. # SPDX-License-Identifier: BSD-3-Clause -# Generated from itemmodels.pro. - add_subdirectory(qstandarditem) if(TARGET Qt::Widgets) add_subdirectory(qstandarditemmodel) diff --git a/tests/auto/gui/itemmodels/qfilesystemmodel/CMakeLists.txt b/tests/auto/gui/itemmodels/qfilesystemmodel/CMakeLists.txt index 8c4a056d82..85fb4fe2e1 100644 --- a/tests/auto/gui/itemmodels/qfilesystemmodel/CMakeLists.txt +++ b/tests/auto/gui/itemmodels/qfilesystemmodel/CMakeLists.txt @@ -1,12 +1,16 @@ # Copyright (C) 2022 The Qt Company Ltd. # SPDX-License-Identifier: BSD-3-Clause -# Generated from qfilesystemmodel.pro. - ##################################################################### ## tst_qfilesystemmodel Test: ##################################################################### +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qfilesystemmodel LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + qt_internal_add_test(tst_qfilesystemmodel SOURCES tst_qfilesystemmodel.cpp @@ -17,12 +21,3 @@ qt_internal_add_test(tst_qfilesystemmodel Qt::WidgetsPrivate Qt::TestPrivate ) - -## Scopes: -##################################################################### - -#### Keys ignored in scope 2:.:.:qfilesystemmodel.pro:WIN32: -# testcase.timeout = "900" - -#### Keys ignored in scope 3:.:.:qfilesystemmodel.pro:MACOS: -# testcase.timeout = "900" diff --git a/tests/auto/gui/itemmodels/qfilesystemmodel/tst_qfilesystemmodel.cpp b/tests/auto/gui/itemmodels/qfilesystemmodel/tst_qfilesystemmodel.cpp index 3802f8afc6..4ba3ae11de 100644 --- a/tests/auto/gui/itemmodels/qfilesystemmodel/tst_qfilesystemmodel.cpp +++ b/tests/auto/gui/itemmodels/qfilesystemmodel/tst_qfilesystemmodel.cpp @@ -1,5 +1,5 @@ // Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QTest> @@ -29,6 +29,7 @@ #include <algorithm> using namespace Qt::StringLiterals; +using namespace std::chrono; #define WAITTIME 1000 @@ -64,6 +65,7 @@ private slots: void rootPath(); void readOnly(); void iconProvider(); + void nullIconProvider(); void rowCount(); @@ -79,6 +81,8 @@ private slots: void filters_data(); void filters(); + void showFilesOnly(); + void nameFilters(); void setData_data(); @@ -113,8 +117,7 @@ protected: bool createFiles(QFileSystemModel *model, const QString &test_path, const QStringList &initial_files, int existingFileCount = 0, const QStringList &initial_dirs = QStringList()); - QModelIndex prepareTestModelRoot(QFileSystemModel *model, const QString &test_path, - QSignalSpy **spy2 = nullptr, QSignalSpy **spy3 = nullptr); + QModelIndex prepareTestModelRoot(QFileSystemModel *model, const QString &test_path); private: QString flatDirTestPath; @@ -308,6 +311,19 @@ void tst_QFileSystemModel::iconProvider() QCOMPARE(myModel->fileIcon(myModel->index(QDir::homePath())).pixmap(50, 50), mb); } +void tst_QFileSystemModel::nullIconProvider() +{ + QFileSystemModel model; + QAbstractItemModelTester tester(&model); + tester.setUseFetchMore(false); + QVERIFY(model.iconProvider()); + // No crash when setIconProvider(nullptr) is used + model.setIconProvider(nullptr); + const auto documentPaths = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation); + QVERIFY(!documentPaths.isEmpty()); + model.setRootPath(documentPaths.constFirst()); +} + bool tst_QFileSystemModel::createFiles(QFileSystemModel *model, const QString &test_path, const QStringList &initial_files, int existingFileCount, const QStringList &initial_dirs) @@ -365,17 +381,12 @@ bool tst_QFileSystemModel::createFiles(QFileSystemModel *model, const QString &t return true; } -QModelIndex tst_QFileSystemModel::prepareTestModelRoot(QFileSystemModel *model, const QString &test_path, - QSignalSpy **spy2, QSignalSpy **spy3) +QModelIndex tst_QFileSystemModel::prepareTestModelRoot(QFileSystemModel *model, + const QString &test_path) { if (model->rowCount(model->index(test_path)) != 0) return QModelIndex(); - if (spy2) - *spy2 = new QSignalSpy(model, &QFileSystemModel::rowsInserted); - if (spy3) - *spy3 = new QSignalSpy(model, &QFileSystemModel::rowsAboutToBeInserted); - QStringList files = { "b", "d", "f", "h", "j", ".a", ".c", ".e", ".g" }; if (!createFiles(model, test_path, files)) @@ -395,16 +406,16 @@ QModelIndex tst_QFileSystemModel::prepareTestModelRoot(QFileSystemModel *model, void tst_QFileSystemModel::rowCount() { - QSignalSpy *spy2 = nullptr; - QSignalSpy *spy3 = nullptr; QScopedPointer<QFileSystemModel> model(new QFileSystemModel); QAbstractItemModelTester tester(model.get()); + QSignalSpy rowsInsertedSpy(model.get(), &QFileSystemModel::rowsInserted); + QSignalSpy rowsAboutToBeInsertedSpy(model.get(), &QFileSystemModel::rowsAboutToBeInserted); tester.setUseFetchMore(false); - QModelIndex root = prepareTestModelRoot(model.data(), flatDirTestPath, &spy2, &spy3); + QModelIndex root = prepareTestModelRoot(model.data(), flatDirTestPath); QVERIFY(root.isValid()); - QVERIFY(spy2 && spy2->size() > 0); - QVERIFY(spy3 && spy3->size() > 0); + QCOMPARE_GT(rowsInsertedSpy.size(), 0); + QCOMPARE_GT(rowsAboutToBeInsertedSpy.size(), 0); } void tst_QFileSystemModel::rowsInserted_data() @@ -675,6 +686,39 @@ void tst_QFileSystemModel::filters() #endif } +void tst_QFileSystemModel::showFilesOnly() +{ + QString tmp = flatDirTestPath; + QFileSystemModel model; + QAbstractItemModelTester tester(&model); + tester.setUseFetchMore(false); + QVERIFY(createFiles(&model, tmp, QStringList())); + const QStringList files{u"a"_s, u"b"_s, u"c"_s}; + const auto subdir = u"sub_directory"_s; + QVERIFY(createFiles(&model, tmp, files, 0, {subdir})); + + // The model changes asynchronously when we run the event loop in the QTRY_... + // macros, so the root index returned by an earlier call to setRootPath might + // become invalid. Make sure we use a fresh one for each iteration. + + // QTBUG-74471 + // WHAT: setting the root path of the model to a dir with some files and a subdir + QTRY_COMPARE(model.rowCount(model.setRootPath(tmp)), files.size() + 1); + + // Change the model to only show files + model.setFilter(QDir::Files); + QTRY_COMPARE(model.rowCount(model.setRootPath(tmp)), files.size()); + + // WHEN: setting the root path to a subdir + QModelIndex subIndex = model.setRootPath(tmp + u'/' + subdir); + QTRY_COMPARE(model.rowCount(subIndex), 0); + + // THEN: setting the root path to the previous (parent) dir, the model should + // still only show files. + // Doubling the default timeout (5s) as this test to fails on macos on the CI + QTRY_COMPARE_WITH_TIMEOUT(model.rowCount(model.setRootPath(tmp)), files.size(), 10s); +} + void tst_QFileSystemModel::nameFilters() { QStringList list; @@ -761,6 +805,7 @@ void tst_QFileSystemModel::setData() QCOMPARE(spy.size(), 1); QList<QVariant> arguments = spy.takeFirst(); QCOMPARE(model->data(idx, QFileSystemModel::FileNameRole).toString(), newFileName); + QCOMPARE(model->data(idx, QFileSystemModel::FileInfoRole).value<QFileInfo>().fileName(), newFileName); QCOMPARE(model->fileInfo(idx).filePath(), tmp + '/' + newFileName); QCOMPARE(model->index(arguments.at(0).toString()), model->index(tmp)); QCOMPARE(arguments.at(1).toString(), oldFileName); @@ -827,13 +872,13 @@ void tst_QFileSystemModel::sort() //Create a file that will be at the end when sorting by name (For Mac, the default) //but if we sort by size descending it will be the first QFile tempFile(dirPath + "/plop2.txt"); - tempFile.open(QIODevice::WriteOnly | QIODevice::Text); + QVERIFY(tempFile.open(QIODevice::WriteOnly | QIODevice::Text)); QTextStream out(&tempFile); out << "The magic number is: " << 49 << "\n"; tempFile.close(); QFile tempFile2(dirPath + "/plop.txt"); - tempFile2.open(QIODevice::WriteOnly | QIODevice::Text); + QVERIFY(tempFile2.open(QIODevice::WriteOnly | QIODevice::Text)); QTextStream out2(&tempFile2); out2 << "The magic number is : " << 49 << " but i write some stuff in the file \n"; tempFile2.close(); @@ -1028,11 +1073,12 @@ void tst_QFileSystemModel::drives() QFileSystemModel model; model.setRootPath(path); model.fetchMore(QModelIndex()); - QFileInfoList drives = QDir::drives(); + const QFileInfoList drives = QDir::drives(); int driveCount = 0; - foreach(const QFileInfo& driveRoot, drives) + for (const QFileInfo& driveRoot : drives) { if (driveRoot.exists()) driveCount++; + } QTRY_COMPARE(model.rowCount(), driveCount); } diff --git a/tests/auto/gui/itemmodels/qstandarditem/CMakeLists.txt b/tests/auto/gui/itemmodels/qstandarditem/CMakeLists.txt index 3a402b51b1..db29eaaf94 100644 --- a/tests/auto/gui/itemmodels/qstandarditem/CMakeLists.txt +++ b/tests/auto/gui/itemmodels/qstandarditem/CMakeLists.txt @@ -1,12 +1,16 @@ # Copyright (C) 2022 The Qt Company Ltd. # SPDX-License-Identifier: BSD-3-Clause -# Generated from qstandarditem.pro. - ##################################################################### ## tst_qstandarditem Test: ##################################################################### +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qstandarditem LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + qt_internal_add_test(tst_qstandarditem SOURCES tst_qstandarditem.cpp diff --git a/tests/auto/gui/itemmodels/qstandarditem/tst_qstandarditem.cpp b/tests/auto/gui/itemmodels/qstandarditem/tst_qstandarditem.cpp index 6f42c45821..093367c759 100644 --- a/tests/auto/gui/itemmodels/qstandarditem/tst_qstandarditem.cpp +++ b/tests/auto/gui/itemmodels/qstandarditem/tst_qstandarditem.cpp @@ -1,5 +1,5 @@ // Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QTest> @@ -112,13 +112,17 @@ void tst_QStandardItem::getSetData() item.setToolTip(toolTip); QCOMPARE(item.toolTip(), toolTip); +#ifndef QT_NO_STATUSTIP QString statusTip = QLatin1String("statusTip ") + iS; item.setStatusTip(statusTip); QCOMPARE(item.statusTip(), statusTip); +#endif +#if QT_CONFIG(whatsthis) QString whatsThis = QLatin1String("whatsThis ") + iS; item.setWhatsThis(whatsThis); QCOMPARE(item.whatsThis(), whatsThis); +#endif QSize sizeHint(64*i, 48*i); item.setSizeHint(sizeHint); @@ -157,8 +161,12 @@ void tst_QStandardItem::getSetData() QCOMPARE(item.text(), text); QCOMPARE(item.icon(), icon); QCOMPARE(item.toolTip(), toolTip); +#ifndef QT_NO_STATUSTIP QCOMPARE(item.statusTip(), statusTip); +#endif +#if QT_CONFIG(whatsthis) QCOMPARE(item.whatsThis(), whatsThis); +#endif QCOMPARE(item.sizeHint(), sizeHint); QCOMPARE(item.font(), font); QCOMPARE(item.textAlignment(), textAlignment); @@ -171,8 +179,12 @@ void tst_QStandardItem::getSetData() QCOMPARE(qvariant_cast<QString>(item.data(Qt::DisplayRole)), text); QCOMPARE(qvariant_cast<QIcon>(item.data(Qt::DecorationRole)), icon); QCOMPARE(qvariant_cast<QString>(item.data(Qt::ToolTipRole)), toolTip); +#ifndef QT_NO_STATUSTIP QCOMPARE(qvariant_cast<QString>(item.data(Qt::StatusTipRole)), statusTip); +#endif +#if QT_CONFIG(whatsthis) QCOMPARE(qvariant_cast<QString>(item.data(Qt::WhatsThisRole)), whatsThis); +#endif QCOMPARE(qvariant_cast<QSize>(item.data(Qt::SizeHintRole)), sizeHint); QCOMPARE(qvariant_cast<QFont>(item.data(Qt::FontRole)), font); QCOMPARE(qvariant_cast<int>(item.data(Qt::TextAlignmentRole)), int(textAlignment)); @@ -844,7 +856,9 @@ void tst_QStandardItem::streamItem() item.setText(QLatin1String("text")); item.setToolTip(QLatin1String("toolTip")); item.setStatusTip(QLatin1String("statusTip")); +#if QT_CONFIG(whatsthis) item.setWhatsThis(QLatin1String("whatsThis")); +#endif item.setSizeHint(QSize(64, 48)); item.setFont(QFont()); item.setTextAlignment(Qt::AlignLeft|Qt::AlignVCenter); @@ -866,7 +880,9 @@ void tst_QStandardItem::streamItem() QCOMPARE(streamedItem.text(), item.text()); QCOMPARE(streamedItem.toolTip(), item.toolTip()); QCOMPARE(streamedItem.statusTip(), item.statusTip()); +#if QT_CONFIG(whatsthis) QCOMPARE(streamedItem.whatsThis(), item.whatsThis()); +#endif QCOMPARE(streamedItem.sizeHint(), item.sizeHint()); QCOMPARE(streamedItem.font(), item.font()); QCOMPARE(streamedItem.textAlignment(), item.textAlignment()); @@ -905,7 +921,9 @@ void tst_QStandardItem::clone() item.setText(QLatin1String("text")); item.setToolTip(QLatin1String("toolTip")); item.setStatusTip(QLatin1String("statusTip")); +#if QT_CONFIG(whatsthis) item.setWhatsThis(QLatin1String("whatsThis")); +#endif item.setSizeHint(QSize(64, 48)); item.setFont(QFont()); item.setTextAlignment(Qt::AlignLeft|Qt::AlignVCenter); @@ -920,7 +938,9 @@ void tst_QStandardItem::clone() QCOMPARE(clone->text(), item.text()); QCOMPARE(clone->toolTip(), item.toolTip()); QCOMPARE(clone->statusTip(), item.statusTip()); +#if QT_CONFIG(whatsthis) QCOMPARE(clone->whatsThis(), item.whatsThis()); +#endif QCOMPARE(clone->sizeHint(), item.sizeHint()); QCOMPARE(clone->font(), item.font()); QCOMPARE(clone->textAlignment(), item.textAlignment()); @@ -1006,6 +1026,32 @@ public: using QStandardItem::clone; using QStandardItem::emitDataChanged; + + void setData(const QVariant &value, int role) override + { + switch (role) { + case Qt::DisplayRole: + QStandardItem::setData(value, role); + break; + default: + // setFlags() uses "UserRole - 1" to store the flags, which is an + // implementation detail not exposed in the docs. + QStandardItem::setData(value, role); + break; + } + } + + QVariant data(int role) const override + { + switch (role) { + case Qt::DisplayRole: + return QStandardItem::data(role); + default: + // flags() uses "UserRole - 1" to get the flags, which is an implementation + // detail not exposed in the docs. + return QStandardItem::data(role); + } + } }; Q_DECLARE_METATYPE(QStandardItem*) @@ -1041,6 +1087,12 @@ void tst_QStandardItem::subclassing() QCOMPARE(item->child(0), child2); QCOMPARE(item->child(1), child0); QCOMPARE(item->child(2), child1); + + item->setFlags(Qt::ItemFlags{0}); + QCOMPARE(item->flags(), Qt::ItemFlags{0}); + + item->setFlags(Qt::ItemFlags{Qt::ItemIsEditable | Qt::ItemIsSelectable}); + QCOMPARE(item->flags(), Qt::ItemFlags{Qt::ItemIsEditable | Qt::ItemIsSelectable}); } void tst_QStandardItem::lessThan() diff --git a/tests/auto/gui/itemmodels/qstandarditemmodel/CMakeLists.txt b/tests/auto/gui/itemmodels/qstandarditemmodel/CMakeLists.txt index f5cb020384..31ae25d008 100644 --- a/tests/auto/gui/itemmodels/qstandarditemmodel/CMakeLists.txt +++ b/tests/auto/gui/itemmodels/qstandarditemmodel/CMakeLists.txt @@ -1,12 +1,16 @@ # Copyright (C) 2022 The Qt Company Ltd. # SPDX-License-Identifier: BSD-3-Clause -# Generated from qstandarditemmodel.pro. - ##################################################################### ## tst_qstandarditemmodel Test: ##################################################################### +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qstandarditemmodel LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + qt_internal_add_test(tst_qstandarditemmodel SOURCES tst_qstandarditemmodel.cpp @@ -16,4 +20,5 @@ qt_internal_add_test(tst_qstandarditemmodel Qt::GuiPrivate Qt::Widgets Qt::WidgetsPrivate + Qt::TestPrivate ) diff --git a/tests/auto/gui/itemmodels/qstandarditemmodel/tst_qstandarditemmodel.cpp b/tests/auto/gui/itemmodels/qstandarditemmodel/tst_qstandarditemmodel.cpp index 07fc074577..ef2fd83d4c 100644 --- a/tests/auto/gui/itemmodels/qstandarditemmodel/tst_qstandarditemmodel.cpp +++ b/tests/auto/gui/itemmodels/qstandarditemmodel/tst_qstandarditemmodel.cpp @@ -1,16 +1,22 @@ // Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QTest> #include <QStandardItemModel> #include <QTreeView> +#include <QMap> #include <QSignalSpy> #include <QAbstractItemModelTester> #include <private/qabstractitemmodel_p.h> +#include <private/qpropertytesthelper_p.h> #include <private/qtreeview_p.h> +#include <algorithm> + +using namespace Qt::StringLiterals; + class tst_QStandardItemModel : public QObject { Q_OBJECT @@ -87,6 +93,7 @@ private slots: void indexFromItem(); void itemFromIndex(); void getSetItemPrototype(); + void getSetItemData_data(); void getSetItemData(); void setHeaderLabels_data(); void setHeaderLabels(); @@ -113,6 +120,7 @@ private slots: void taskQTBUG_45114_setItemData(); void setItemPersistentIndex(); void signalsOnTakeItem(); + void takeChild(); void createPersistentOnLayoutAboutToBeChanged(); private: QStandardItemModel *m_model = nullptr; @@ -720,7 +728,7 @@ void tst_QStandardItemModel::data() const QMap<int, QVariant> itmData = m_model->itemData(m_model->index(0, 0)); QCOMPARE(itmData.value(Qt::DisplayRole), QLatin1String("initialitem")); QCOMPARE(itmData.value(Qt::ToolTipRole), QLatin1String("tooltip")); - QVERIFY(!itmData.contains(Qt::UserRole - 1)); + QVERIFY(!itmData.contains(Qt::UserRole - 1)); // Qt::UserRole - 1 is used to store flags QVERIFY(m_model->itemData(QModelIndex()).isEmpty()); } @@ -896,6 +904,9 @@ void tst_QStandardItemModel::sortRoleBindings() sortRoleObserver.setBinding([&] { return model.sortRole(); }); model.setSortRole(Qt::EditRole); QCOMPARE(sortRoleObserver, Qt::EditRole); + + QTestPrivate::testReadWritePropertyBasics(model, static_cast<int>(Qt::DisplayRole), + static_cast<int>(Qt::EditRole), "sortRole"); } void tst_QStandardItemModel::findItems() @@ -1025,33 +1036,49 @@ void tst_QStandardItemModel::getSetItemPrototype() QCOMPARE(model.itemPrototype(), nullptr); } +using RoleMap = QMap<int, QVariant>; +using RoleList = QList<int>; + +static RoleMap getSetItemDataRoleMap(int textRole) +{ + return {{textRole, "text"_L1}, + {Qt::StatusTipRole, "statusTip"_L1}, + {Qt::ToolTipRole, "toolTip"_L1}, + {Qt::WhatsThisRole, "whatsThis"_L1}, + {Qt::SizeHintRole, QSize{64, 48}}, + {Qt::FontRole, QFont{}}, + {Qt::TextAlignmentRole, int(Qt::AlignLeft|Qt::AlignVCenter)}, + {Qt::BackgroundRole, QColor(Qt::blue)}, + {Qt::ForegroundRole, QColor(Qt::green)}, + {Qt::CheckStateRole, int(Qt::PartiallyChecked)}, + {Qt::AccessibleTextRole, "accessibleText"_L1}, + {Qt::AccessibleDescriptionRole, "accessibleDescription"_L1}}; +} + +void tst_QStandardItemModel::getSetItemData_data() +{ + QTest::addColumn<RoleMap>("itemData"); + QTest::addColumn<RoleMap>("expectedItemData"); + QTest::addColumn<RoleList>("expectedRoles"); + + // QTBUG-112326: verify that text data set using Qt::EditRole is mapped to + // Qt::DisplayRole and both roles are in the changed signal + const RoleMap expectedItemData = getSetItemDataRoleMap(Qt::DisplayRole); + RoleList expectedRoles = expectedItemData.keys() << Qt::EditRole; + std::sort(expectedRoles.begin(), expectedRoles.end()); + + QTest::newRow("DisplayRole") << expectedItemData + << expectedItemData << expectedRoles; + + QTest::newRow("EditRole") << getSetItemDataRoleMap(Qt::EditRole) + << expectedItemData << expectedRoles; +} + void tst_QStandardItemModel::getSetItemData() { - QMap<int, QVariant> roles; - QLatin1String text("text"); - roles.insert(Qt::DisplayRole, text); - QLatin1String statusTip("statusTip"); - roles.insert(Qt::StatusTipRole, statusTip); - QLatin1String toolTip("toolTip"); - roles.insert(Qt::ToolTipRole, toolTip); - QLatin1String whatsThis("whatsThis"); - roles.insert(Qt::WhatsThisRole, whatsThis); - QSize sizeHint(64, 48); - roles.insert(Qt::SizeHintRole, sizeHint); - QFont font; - roles.insert(Qt::FontRole, font); - Qt::Alignment textAlignment(Qt::AlignLeft|Qt::AlignVCenter); - roles.insert(Qt::TextAlignmentRole, int(textAlignment)); - QColor backgroundColor(Qt::blue); - roles.insert(Qt::BackgroundRole, backgroundColor); - QColor textColor(Qt::green); - roles.insert(Qt::ForegroundRole, textColor); - Qt::CheckState checkState(Qt::PartiallyChecked); - roles.insert(Qt::CheckStateRole, int(checkState)); - QLatin1String accessibleText("accessibleText"); - roles.insert(Qt::AccessibleTextRole, accessibleText); - QLatin1String accessibleDescription("accessibleDescription"); - roles.insert(Qt::AccessibleDescriptionRole, accessibleDescription); + QFETCH(RoleMap, itemData); + QFETCH(RoleMap, expectedItemData); + QFETCH(RoleList, expectedRoles); QStandardItemModel model; model.insertRows(0, 1); @@ -1060,11 +1087,17 @@ void tst_QStandardItemModel::getSetItemData() QSignalSpy modelDataChangedSpy( &model, &QStandardItemModel::dataChanged); - QVERIFY(model.setItemData(idx, roles)); + QVERIFY(model.setItemData(idx, itemData)); QCOMPARE(modelDataChangedSpy.size(), 1); - QVERIFY(model.setItemData(idx, roles)); + const QVariantList &args = modelDataChangedSpy.constFirst(); + QCOMPARE(args.size(), 3); + auto roleList = args.at(2).value<QList<int> >(); + std::sort(roleList.begin(), roleList.end()); + QCOMPARE(roleList, expectedRoles); + + QVERIFY(model.setItemData(idx, itemData)); QCOMPARE(modelDataChangedSpy.size(), 1); //it was already changed once - QCOMPARE(model.itemData(idx), roles); + QCOMPARE(model.itemData(idx), expectedItemData); } void tst_QStandardItemModel::setHeaderLabels_data() @@ -1581,15 +1614,21 @@ void tst_QStandardItemModel::removeRowsAndColumns() QList<QStandardItem *> row_taken = model.takeRow(6); QCOMPARE(row_taken.size(), col_list.size()); - for (int c = 0; c < col_list.size(); c++) - QCOMPARE(row_taken[c]->text() , row_list[6] + QLatin1Char('x') + col_list[c]); + for (qsizetype c = 0; c < row_taken.size(); c++) { + auto item = row_taken.at(c); + QCOMPARE(item->text() , row_list[6] + QLatin1Char('x') + col_list[c]); + delete item; + } row_list.remove(6); VERIFY_MODEL QList<QStandardItem *> col_taken = model.takeColumn(10); QCOMPARE(col_taken.size(), row_list.size()); - for (int r = 0; r < row_list.size(); r++) - QCOMPARE(col_taken[r]->text() , row_list[r] + QLatin1Char('x') + col_list[10]); + for (qsizetype r = 0; r < col_taken.size(); r++) { + auto item = col_taken.at(r); + QCOMPARE(item->text() , row_list[r] + QLatin1Char('x') + col_list[10]); + delete item; + } col_list.remove(10); VERIFY_MODEL } @@ -1759,6 +1798,7 @@ void tst_QStandardItemModel::signalsOnTakeItem() // QTBUG-89145 QCOMPARE(takenItem->model(), nullptr); QCOMPARE(takenItem->child(0, 0)->model(), nullptr); QCOMPARE(m.index(1, 0).data(), QVariant()); + delete takenItem; } void tst_QStandardItemModel::createPersistentOnLayoutAboutToBeChanged() // QTBUG-93466 @@ -1796,5 +1836,36 @@ void tst_QStandardItemModel::createPersistentOnLayoutAboutToBeChanged() // QTBUG QCOMPARE(layoutChangedSpy.size(), 1); } +void tst_QStandardItemModel::takeChild() // QTBUG-117900 +{ + { + // with model + QStandardItemModel model1; + QStandardItemModel model2; + QStandardItem base1("base1"); + model1.setItem(0, 0, &base1); + QStandardItem base2("base2"); + model2.setItem(0, 0, &base2); + auto item = new QStandardItem("item1"); + item->appendRow(new QStandardItem("child")); + base1.appendRow(item); + base2.appendRow(base1.takeChild(0, 0)); + QCOMPARE(base1.child(0, 0), nullptr); + QCOMPARE(base2.child(0, 0), item); + } + { + // without model + QStandardItem base1("base1"); + QStandardItem base2("base2"); + auto item = new QStandardItem("item1"); + item->appendRow(new QStandardItem("child")); + base1.appendRow(item); + base2.appendRow(base1.takeChild(0, 0)); + QCOMPARE(base1.child(0, 0), nullptr); + QCOMPARE(base2.child(0, 0), item); + } +} + + QTEST_MAIN(tst_QStandardItemModel) #include "tst_qstandarditemmodel.moc" |