diff options
Diffstat (limited to 'tests/auto/gui/itemmodels')
8 files changed, 376 insertions, 241 deletions
diff --git a/tests/auto/gui/itemmodels/CMakeLists.txt b/tests/auto/gui/itemmodels/CMakeLists.txt index 69b6cb0e22..4c25418ef1 100644 --- a/tests/auto/gui/itemmodels/CMakeLists.txt +++ b/tests/auto/gui/itemmodels/CMakeLists.txt @@ -1,4 +1,5 @@ -# Generated from itemmodels.pro. +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause add_subdirectory(qstandarditem) if(TARGET Qt::Widgets) diff --git a/tests/auto/gui/itemmodels/qfilesystemmodel/BLACKLIST b/tests/auto/gui/itemmodels/qfilesystemmodel/BLACKLIST index ae8a64dc1f..4119afce84 100644 --- a/tests/auto/gui/itemmodels/qfilesystemmodel/BLACKLIST +++ b/tests/auto/gui/itemmodels/qfilesystemmodel/BLACKLIST @@ -3,6 +3,3 @@ ubuntu b2qt [specialFiles] b2qt -# QTBUG-87427 -[specialFiles] -android diff --git a/tests/auto/gui/itemmodels/qfilesystemmodel/CMakeLists.txt b/tests/auto/gui/itemmodels/qfilesystemmodel/CMakeLists.txt index 31fd8ca32d..85fb4fe2e1 100644 --- a/tests/auto/gui/itemmodels/qfilesystemmodel/CMakeLists.txt +++ b/tests/auto/gui/itemmodels/qfilesystemmodel/CMakeLists.txt @@ -1,25 +1,23 @@ -# Generated from qfilesystemmodel.pro. +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause ##################################################################### ## 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 - PUBLIC_LIBRARIES + LIBRARIES Qt::CorePrivate Qt::Gui Qt::Widgets 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 b6f6328acd..8ef0b6272a 100644 --- a/tests/auto/gui/itemmodels/qfilesystemmodel/tst_qfilesystemmodel.cpp +++ b/tests/auto/gui/itemmodels/qfilesystemmodel/tst_qfilesystemmodel.cpp @@ -1,30 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** 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 -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** 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. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QTest> @@ -45,6 +20,7 @@ #include <QStyle> #include <QtGlobal> #include <QTemporaryDir> +#include <QAbstractItemModelTester> #if defined(Q_OS_WIN) # include <qt_windows.h> // for SetFileAttributes #endif @@ -52,6 +28,9 @@ #include <algorithm> +using namespace Qt::StringLiterals; +using namespace std::chrono; + #define WAITTIME 1000 // Will try to wait for the condition while allowing event processing @@ -86,6 +65,7 @@ private slots: void rootPath(); void readOnly(); void iconProvider(); + void nullIconProvider(); void rowCount(); @@ -101,6 +81,8 @@ private slots: void filters_data(); void filters(); + void showFilesOnly(); + void nameFilters(); void setData_data(); @@ -172,6 +154,8 @@ void tst_QFileSystemModel::indexPath() { #if !defined(Q_OS_WIN) QScopedPointer<QFileSystemModel> model(new QFileSystemModel); + QAbstractItemModelTester tester(model.get()); + tester.setUseFetchMore(false); int depth = QDir::currentPath().count('/'); model->setRootPath(QDir::currentPath()); QString backPath; @@ -186,12 +170,14 @@ void tst_QFileSystemModel::indexPath() void tst_QFileSystemModel::rootPath() { QScopedPointer<QFileSystemModel> model(new QFileSystemModel); + QAbstractItemModelTester tester(model.get()); + tester.setUseFetchMore(false); QCOMPARE(model->rootPath(), QString(QDir().path())); QSignalSpy rootChanged(model.data(), &QFileSystemModel::rootPathChanged); QModelIndex root = model->setRootPath(model->rootPath()); root = model->setRootPath("this directory shouldn't exist"); - QCOMPARE(rootChanged.count(), 0); + QCOMPARE(rootChanged.size(), 0); QString oldRootPath = model->rootPath(); const QStringList documentPaths = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation); @@ -208,48 +194,50 @@ void tst_QFileSystemModel::rootPath() QTRY_VERIFY(model->rowCount(root) >= 0); QCOMPARE(model->rootPath(), QString(documentPath)); - QCOMPARE(rootChanged.count(), oldRootPath == model->rootPath() ? 0 : 1); + QCOMPARE(rootChanged.size(), oldRootPath == model->rootPath() ? 0 : 1); QCOMPARE(model->rootDirectory().absolutePath(), documentPath); model->setRootPath(QDir::rootPath()); - int oldCount = rootChanged.count(); + int oldCount = rootChanged.size(); oldRootPath = model->rootPath(); root = model->setRootPath(documentPath + QLatin1String("/.")); QTRY_VERIFY(model->rowCount(root) >= 0); QCOMPARE(model->rootPath(), documentPath); - QCOMPARE(rootChanged.count(), oldRootPath == model->rootPath() ? oldCount : oldCount + 1); + QCOMPARE(rootChanged.size(), oldRootPath == model->rootPath() ? oldCount : oldCount + 1); QCOMPARE(model->rootDirectory().absolutePath(), documentPath); QDir newdir = documentPath; if (newdir.cdUp()) { - oldCount = rootChanged.count(); + oldCount = rootChanged.size(); oldRootPath = model->rootPath(); root = model->setRootPath(documentPath + QLatin1String("/..")); QTRY_VERIFY(model->rowCount(root) >= 0); QCOMPARE(model->rootPath(), newdir.path()); - QCOMPARE(rootChanged.count(), oldCount + 1); + QCOMPARE(rootChanged.size(), oldCount + 1); QCOMPARE(model->rootDirectory().absolutePath(), newdir.path()); } #ifdef Q_OS_WIN // check case insensitive root node on windows, tests QTBUG-71701 - QModelIndex index = model->setRootPath(uR"(\\localhost\c$)"_qs); + QModelIndex index = model->setRootPath(uR"(\\localhost\c$)"_s); QVERIFY(index.isValid()); - QCOMPARE(model->rootPath(), u"//localhost/c$"_qs); + QCOMPARE(model->rootPath(), u"//localhost/c$"_s); - index = model->setRootPath(uR"(\\localhost\C$)"_qs); + index = model->setRootPath(uR"(\\localhost\C$)"_s); QVERIFY(index.isValid()); - QCOMPARE(model->rootPath(), u"//localhost/C$"_qs); + QCOMPARE(model->rootPath(), u"//localhost/C$"_s); - index = model->setRootPath(uR"(\\LOCALHOST\C$)"_qs); + index = model->setRootPath(uR"(\\LOCALHOST\C$)"_s); QVERIFY(index.isValid()); - QCOMPARE(model->rootPath(), u"//LOCALHOST/C$"_qs); + QCOMPARE(model->rootPath(), u"//LOCALHOST/C$"_s); #endif } void tst_QFileSystemModel::readOnly() { QScopedPointer<QFileSystemModel> model(new QFileSystemModel); + QAbstractItemModelTester tester(model.get()); + tester.setUseFetchMore(false); QCOMPARE(model->isReadOnly(), true); QTemporaryFile file(flatDirTestPath + QStringLiteral("/XXXXXX.dat")); QVERIFY2(file.open(), qPrintable(file.errorString())); @@ -261,10 +249,14 @@ void tst_QFileSystemModel::readOnly() QModelIndex root = model->setRootPath(flatDirTestPath); QTRY_VERIFY(model->rowCount(root) > 0); + + // ItemIsEditable should change, ItemNeverHasChildren should not change QVERIFY(!(model->flags(model->index(fileName)) & Qt::ItemIsEditable)); + QVERIFY(model->flags(model->index(fileName)) & Qt::ItemNeverHasChildren); model->setReadOnly(false); QCOMPARE(model->isReadOnly(), false); QVERIFY(model->flags(model->index(fileName)) & Qt::ItemIsEditable); + QVERIFY(model->flags(model->index(fileName)) & Qt::ItemNeverHasChildren); } class CustomFileIconProvider : public QFileIconProvider @@ -299,6 +291,8 @@ private: void tst_QFileSystemModel::iconProvider() { QScopedPointer<QFileSystemModel> model(new QFileSystemModel); + QAbstractItemModelTester tester(model.get()); + tester.setUseFetchMore(false); QVERIFY(model->iconProvider()); QScopedPointer<QFileIconProvider> provider(new QFileIconProvider); model->setIconProvider(provider.data()); @@ -318,6 +312,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) @@ -408,11 +415,13 @@ void tst_QFileSystemModel::rowCount() QSignalSpy *spy2 = nullptr; QSignalSpy *spy3 = nullptr; QScopedPointer<QFileSystemModel> model(new QFileSystemModel); + QAbstractItemModelTester tester(model.get()); + tester.setUseFetchMore(false); QModelIndex root = prepareTestModelRoot(model.data(), flatDirTestPath, &spy2, &spy3); QVERIFY(root.isValid()); - QVERIFY(spy2 && spy2->count() > 0); - QVERIFY(spy3 && spy3->count() > 0); + QVERIFY(spy2 && spy2->size() > 0); + QVERIFY(spy3 && spy3->size() > 0); } void tst_QFileSystemModel::rowsInserted_data() @@ -436,6 +445,8 @@ void tst_QFileSystemModel::rowsInserted() { const QString tmp = flatDirTestPath; QScopedPointer<QFileSystemModel> model(new QFileSystemModel); + QAbstractItemModelTester tester(model.get()); + tester.setUseFetchMore(false); QModelIndex root = prepareTestModelRoot(model.data(), tmp); QVERIFY(root.isValid()); @@ -452,7 +463,7 @@ void tst_QFileSystemModel::rowsInserted() QVERIFY(createFiles(model.data(), tmp, files, 5)); QTRY_COMPARE(model->rowCount(root), oldCount + count); int totalRowsInserted = 0; - for (int i = 0; i < spy0.count(); ++i) { + for (int i = 0; i < spy0.size(); ++i) { int start = spy0[i].value(1).toInt(); int end = spy0[i].value(2).toInt(); totalRowsInserted += end - start + 1; @@ -461,24 +472,24 @@ void tst_QFileSystemModel::rowsInserted() const QString expected = ascending == Qt::AscendingOrder ? QStringLiteral("j") : QStringLiteral("b"); QTRY_COMPARE(lastEntry(root), expected); - if (spy0.count() > 0) { + if (spy0.size() > 0) { if (count == 0) - QCOMPARE(spy0.count(), 0); + QCOMPARE(spy0.size(), 0); else - QVERIFY(spy0.count() >= 1); + QVERIFY(spy0.size() >= 1); } - if (count == 0) QCOMPARE(spy1.count(), 0); else QVERIFY(spy1.count() >= 1); + if (count == 0) QCOMPARE(spy1.size(), 0); else QVERIFY(spy1.size() >= 1); QVERIFY(createFiles(model.data(), tmp, QStringList(".hidden_file"), 5 + count)); if (count != 0) - QTRY_VERIFY(spy0.count() >= 1); + QTRY_VERIFY(spy0.size() >= 1); else - QTRY_COMPARE(spy0.count(), 0); + QTRY_COMPARE(spy0.size(), 0); if (count != 0) - QTRY_VERIFY(spy1.count() >= 1); + QTRY_VERIFY(spy1.size() >= 1); else - QTRY_COMPARE(spy1.count(), 0); + QTRY_COMPARE(spy1.size(), 0); } void tst_QFileSystemModel::rowsRemoved_data() @@ -490,6 +501,8 @@ void tst_QFileSystemModel::rowsRemoved() { const QString tmp = flatDirTestPath; QScopedPointer<QFileSystemModel> model(new QFileSystemModel); + QAbstractItemModelTester tester(model.get()); + tester.setUseFetchMore(false); QModelIndex root = prepareTestModelRoot(model.data(), tmp); QVERIFY(root.isValid()); @@ -507,14 +520,14 @@ void tst_QFileSystemModel::rowsRemoved() } for (int i = 0 ; i < 10; ++i) { if (count != 0) { - if (i == 10 || spy0.count() != 0) { - QVERIFY(spy0.count() >= 1); - QVERIFY(spy1.count() >= 1); + if (i == 10 || spy0.size() != 0) { + QVERIFY(spy0.size() >= 1); + QVERIFY(spy1.size() >= 1); } } else { - if (i == 10 || spy0.count() == 0) { - QCOMPARE(spy0.count(), 0); - QCOMPARE(spy1.count(), 0); + if (i == 10 || spy0.size() == 0) { + QCOMPARE(spy0.size(), 0); + QCOMPARE(spy1.size(), 0); } } QStringList lst; @@ -533,11 +546,11 @@ void tst_QFileSystemModel::rowsRemoved() QVERIFY(QFile::remove(tmp + QLatin1String("/.c"))); if (count != 0) { - QVERIFY(spy0.count() >= 1); - QVERIFY(spy1.count() >= 1); + QVERIFY(spy0.size() >= 1); + QVERIFY(spy1.size() >= 1); } else { - QCOMPARE(spy0.count(), 0); - QCOMPARE(spy1.count(), 0); + QCOMPARE(spy0.size(), 0); + QCOMPARE(spy1.size(), 0); } } @@ -552,6 +565,8 @@ void tst_QFileSystemModel::dataChanged() const QString tmp = flatDirTestPath; QScopedPointer<QFileSystemModel> model(new QFileSystemModel); + QAbstractItemModelTester tester(model.get()); + tester.setUseFetchMore(false); QModelIndex root = prepareTestModelRoot(model.data(), tmp); QVERIFY(root.isValid()); @@ -567,7 +582,7 @@ void tst_QFileSystemModel::dataChanged() QTest::qWait(WAITTIME); - if (count != 0) QVERIFY(spy.count() >= 1); else QCOMPARE(spy.count(), 0); + if (count != 0) QVERIFY(spy.size() >= 1); else QCOMPARE(spy.size(), 0); } void tst_QFileSystemModel::filters_data() @@ -612,6 +627,8 @@ void tst_QFileSystemModel::filters() { QString tmp = flatDirTestPath; QScopedPointer<QFileSystemModel> model(new QFileSystemModel); + QAbstractItemModelTester tester(model.get()); + tester.setUseFetchMore(false); QVERIFY(createFiles(model.data(), tmp, QStringList())); QModelIndex root = model->setRootPath(tmp); QFETCH(QStringList, files); @@ -620,7 +637,7 @@ void tst_QFileSystemModel::filters() QFETCH(QStringList, nameFilters); QFETCH(int, rowCount); - if (nameFilters.count() > 0) + if (nameFilters.size() > 0) model->setNameFilters(nameFilters); model->setNameFilterDisables(false); model->setFilter(dirFilters); @@ -632,12 +649,12 @@ void tst_QFileSystemModel::filters() QDir xFactor(tmp); QStringList dirEntries; - if (nameFilters.count() > 0) + if (nameFilters.size() > 0) dirEntries = xFactor.entryList(nameFilters, dirFilters); else dirEntries = xFactor.entryList(dirFilters); - QCOMPARE(dirEntries.count(), rowCount); + QCOMPARE(dirEntries.size(), rowCount); QStringList modelEntries; @@ -649,7 +666,7 @@ void tst_QFileSystemModel::filters() QCOMPARE(dirEntries, modelEntries); #ifdef Q_OS_LINUX - if (files.count() >= 3 && rowCount >= 3 && rowCount != 5) { + if (files.size() >= 3 && rowCount >= 3 && rowCount != 5) { QString fileName1 = (tmp + '/' + files.at(0)); QString fileName2 = (tmp + '/' + files.at(1)); QString fileName3 = (tmp + '/' + files.at(2)); @@ -675,11 +692,46 @@ 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; list << "a" << "b" << "c"; QScopedPointer<QFileSystemModel> model(new QFileSystemModel); + QAbstractItemModelTester tester(model.get()); + tester.setUseFetchMore(false); model->setNameFilters(list); model->setNameFilterDisables(false); QCOMPARE(model->nameFilters(), list); @@ -725,6 +777,8 @@ void tst_QFileSystemModel::setData_data() void tst_QFileSystemModel::setData() { QScopedPointer<QFileSystemModel> model(new QFileSystemModel); + QAbstractItemModelTester tester(model.get()); + tester.setUseFetchMore(false); QSignalSpy spy(model.data(), &QFileSystemModel::fileRenamed); QFETCH(QString, subdirName); QFETCH(QStringList, files); @@ -744,7 +798,7 @@ void tst_QFileSystemModel::setData() tmpIdx = model->index(tmp); model->fetchMore(tmpIdx); } - QTRY_COMPARE(model->rowCount(tmpIdx), files.count()); + QTRY_COMPARE(model->rowCount(tmpIdx), files.size()); QModelIndex idx = model->index(tmp + '/' + oldFileName); QCOMPARE(idx.isValid(), true); @@ -754,16 +808,17 @@ void tst_QFileSystemModel::setData() QCOMPARE(model->setData(idx, newFileName), success); model->setReadOnly(true); if (success) { - QCOMPARE(spy.count(), 1); + 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); QCOMPARE(arguments.at(2).toString(), newFileName); QCOMPARE(QFile::rename(tmp + '/' + newFileName, tmp + '/' + oldFileName), true); } - QTRY_COMPARE(model->rowCount(tmpIdx), files.count()); + QTRY_COMPARE(model->rowCount(tmpIdx), files.size()); // cleanup if (!subdirName.isEmpty()) QVERIFY(QDir(tmp).removeRecursively()); @@ -777,6 +832,8 @@ void tst_QFileSystemModel::sortPersistentIndex() file.close(); QTRY_VERIFY(QDir(flatDirTestPath).entryInfoList().contains(fileInfo)); QScopedPointer<QFileSystemModel> model(new QFileSystemModel); + QAbstractItemModelTester tester(model.get()); + tester.setUseFetchMore(false); QModelIndex root = model->setRootPath(flatDirTestPath); QTRY_VERIFY(model->rowCount(root) > 0); @@ -821,13 +878,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(); @@ -856,7 +913,7 @@ void tst_QFileSystemModel::sort() expectedOrder << tempFile2.fileName() << tempFile.fileName() << dirPath + QChar('/') + ".." << dirPath + QChar('/') + "."; if (fileDialogMode) { - QTRY_COMPARE(myModel->rowCount(parent), expectedOrder.count()); + QTRY_COMPARE(myModel->rowCount(parent), expectedOrder.size()); // File dialog Mode means sub trees are not sorted, only the current root. // There's no way we can check that the sub tree is "not sorted"; just check if it // has the same contents of the expected list @@ -883,6 +940,8 @@ void tst_QFileSystemModel::mkdir() QString tmp = flatDirTestPath; QString newFolderPath = QDir::toNativeSeparators(tmp + '/' + "NewFoldermkdirtest4"); QScopedPointer<QFileSystemModel> model(new QFileSystemModel); + QAbstractItemModelTester tester(model.get()); + tester.setUseFetchMore(false); QModelIndex tmpDir = model->index(tmp); QVERIFY(tmpDir.isValid()); QDir bestatic(newFolderPath); @@ -918,6 +977,8 @@ void tst_QFileSystemModel::deleteFile() } newFile.close(); QScopedPointer<QFileSystemModel> model(new QFileSystemModel); + QAbstractItemModelTester tester(model.get()); + tester.setUseFetchMore(false); QModelIndex idx = model->index(newFilePath); QVERIFY(idx.isValid()); QVERIFY(model->remove(idx)); @@ -980,12 +1041,14 @@ void tst_QFileSystemModel::caseSensitivity() QStringList files; files << "a" << "c" << "C"; QScopedPointer<QFileSystemModel> model(new QFileSystemModel); + QAbstractItemModelTester tester(model.get()); + tester.setUseFetchMore(false); QVERIFY(createFiles(model.data(), tmp, files)); QModelIndex root = model->index(tmp); QStringList paths; QModelIndexList indexes; QCOMPARE(model->rowCount(root), 0); - for (int i = 0; i < files.count(); ++i) { + for (int i = 0; i < files.size(); ++i) { const QString path = tmp + '/' + files.at(i); const QModelIndex index = model->index(path); QVERIFY(index.isValid()); @@ -995,7 +1058,7 @@ void tst_QFileSystemModel::caseSensitivity() if (!QFileSystemEngine::isCaseSensitive()) { // QTBUG-31103, QTBUG-64147: Verify that files can be accessed by paths with fLipPeD case. - for (int i = 0; i < paths.count(); ++i) { + for (int i = 0; i < paths.size(); ++i) { const QModelIndex flippedCaseIndex = model->index(flipCase(paths.at(i))); QCOMPARE(indexes.at(i), flippedCaseIndex); } @@ -1016,11 +1079,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); } @@ -1045,6 +1109,8 @@ void tst_QFileSystemModel::dirsBeforeFiles() } QScopedPointer<QFileSystemModel> model(new QFileSystemModel); + QAbstractItemModelTester tester(model.get()); + tester.setUseFetchMore(false); QModelIndex root = model->setRootPath(dir.absolutePath()); // Wait for model to be notified by the file system watcher QTRY_COMPARE(model->rowCount(root), 2 * itemCount); @@ -1119,6 +1185,8 @@ void tst_QFileSystemModel::permissions() // checks QTBUG-20503 const QString tmp = flatDirTestPath; const QString file = tmp + QLatin1String("/f"); QScopedPointer<QFileSystemModel> model(new QFileSystemModel); + QAbstractItemModelTester tester(model.get()); + tester.setUseFetchMore(false); QVERIFY(createFiles(model.data(), tmp, QStringList{QLatin1String("f")})); QVERIFY(QFile::setPermissions(file, permissions)); @@ -1183,6 +1251,9 @@ void tst_QFileSystemModel::specialFiles() #ifndef Q_OS_UNIX QSKIP("Not implemented"); #endif +#ifdef Q_OS_ANDROID + QSKIP("Android does not allow access to root filesystem"); +#endif QFileSystemModel model; diff --git a/tests/auto/gui/itemmodels/qstandarditem/CMakeLists.txt b/tests/auto/gui/itemmodels/qstandarditem/CMakeLists.txt index e5446d596a..db29eaaf94 100644 --- a/tests/auto/gui/itemmodels/qstandarditem/CMakeLists.txt +++ b/tests/auto/gui/itemmodels/qstandarditem/CMakeLists.txt @@ -1,12 +1,19 @@ -# Generated from qstandarditem.pro. +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause ##################################################################### ## 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 - PUBLIC_LIBRARIES + LIBRARIES Qt::Gui ) diff --git a/tests/auto/gui/itemmodels/qstandarditem/tst_qstandarditem.cpp b/tests/auto/gui/itemmodels/qstandarditem/tst_qstandarditem.cpp index 61683b20a6..70fb2b39d3 100644 --- a/tests/auto/gui/itemmodels/qstandarditem/tst_qstandarditem.cpp +++ b/tests/auto/gui/itemmodels/qstandarditem/tst_qstandarditem.cpp @@ -1,30 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** 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 -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** 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. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QTest> @@ -782,10 +757,10 @@ void tst_QStandardItem::takeColumn() QList<QStandardItem *> taken = item.takeColumn(column); if (expectSuccess) { - QCOMPARE(taken.count(), item.rowCount()); + QCOMPARE(taken.size(), item.rowCount()); QCOMPARE(item.columnCount(), columns - 1); int index = column; - for (int i = 0; i < taken.count(); ++i) { + for (int i = 0; i < taken.size(); ++i) { QCOMPARE(taken.at(i), originalChildren.takeAt(index)); index += item.columnCount(); } @@ -843,10 +818,10 @@ void tst_QStandardItem::takeRow() QList<QStandardItem *> taken = item.takeRow(row); if (expectSuccess) { - QCOMPARE(taken.count(), item.columnCount()); + QCOMPARE(taken.size(), item.columnCount()); QCOMPARE(item.rowCount(), rows - 1); int index = row * columns; - for (int i = 0; i < taken.count(); ++i) { + for (int i = 0; i < taken.size(); ++i) { QCOMPARE(taken.at(i), originalChildren.takeAt(index)); } index = 0; @@ -1009,8 +984,8 @@ void tst_QStandardItem::sortChildren() QCOMPARE(two->child(1)->text(), QLatin1String("e")); QCOMPARE(two->child(2)->text(), QLatin1String("f")); - QCOMPARE(layoutAboutToBeChangedSpy.count(), (x == 0) ? 0 : 3); - QCOMPARE(layoutChangedSpy.count(), (x == 0) ? 0 : 3); + QCOMPARE(layoutAboutToBeChangedSpy.size(), (x == 0) ? 0 : 3); + QCOMPARE(layoutChangedSpy.size(), (x == 0) ? 0 : 3); if (x == 0) delete item; @@ -1026,11 +1001,37 @@ public: int type() const override { return QStandardItem::UserType + 1; } bool operator<(const QStandardItem &other) const override { - return text().length() < other.text().length(); + return text().size() < other.text().size(); } 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*) @@ -1052,8 +1053,8 @@ void tst_QStandardItem::subclassing() QSignalSpy itemChangedSpy(&model, &QStandardItemModel::itemChanged); item->emitDataChanged(); - QCOMPARE(itemChangedSpy.count(), 1); - QCOMPARE(itemChangedSpy.at(0).count(), 1); + QCOMPARE(itemChangedSpy.size(), 1); + QCOMPARE(itemChangedSpy.at(0).size(), 1); QCOMPARE(qvariant_cast<QStandardItem*>(itemChangedSpy.at(0).at(0)), item); CustomItem *child0 = new CustomItem("cc"); @@ -1066,6 +1067,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 49cbcbfa88..31ae25d008 100644 --- a/tests/auto/gui/itemmodels/qstandarditemmodel/CMakeLists.txt +++ b/tests/auto/gui/itemmodels/qstandarditemmodel/CMakeLists.txt @@ -1,16 +1,24 @@ -# Generated from qstandarditemmodel.pro. +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause ##################################################################### ## 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 - PUBLIC_LIBRARIES + LIBRARIES Qt::CorePrivate Qt::Gui 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 7758a5d5cc..ef2fd83d4c 100644 --- a/tests/auto/gui/itemmodels/qstandarditemmodel/tst_qstandarditemmodel.cpp +++ b/tests/auto/gui/itemmodels/qstandarditemmodel/tst_qstandarditemmodel.cpp @@ -1,41 +1,22 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** 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 -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** 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. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// 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 @@ -112,6 +93,7 @@ private slots: void indexFromItem(); void itemFromIndex(); void getSetItemPrototype(); + void getSetItemData_data(); void getSetItemData(); void setHeaderLabels_data(); void setHeaderLabels(); @@ -138,6 +120,7 @@ private slots: void taskQTBUG_45114_setItemData(); void setItemPersistentIndex(); void signalsOnTakeItem(); + void takeChild(); void createPersistentOnLayoutAboutToBeChanged(); private: QStandardItemModel *m_model = nullptr; @@ -473,16 +456,16 @@ void tst_QStandardItemModel::setHeaderData() for (int i = 0; i < count; ++i) { QString customString = QString("custom") + QString::number(i); QCOMPARE(m_model->setHeaderData(i, orient, customString), true); - QCOMPARE(headerDataChangedSpy.count(), 1); - QCOMPARE(dataChangedSpy.count(), 0); + QCOMPARE(headerDataChangedSpy.size(), 1); + QCOMPARE(dataChangedSpy.size(), 0); QVariantList args = headerDataChangedSpy.takeFirst(); QCOMPARE(qvariant_cast<Qt::Orientation>(args.at(0)), orient); QCOMPARE(args.at(1).toInt(), i); QCOMPARE(args.at(2).toInt(), i); QCOMPARE(m_model->headerData(i, orient).toString(), customString); QCOMPARE(m_model->setHeaderData(i, orient, customString), true); - QCOMPARE(headerDataChangedSpy.count(), 0); - QCOMPARE(dataChangedSpy.count(), 0); + QCOMPARE(headerDataChangedSpy.size(), 0); + QCOMPARE(dataChangedSpy.size(), 0); } //check read from invalid sections @@ -745,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()); } @@ -786,9 +769,9 @@ void tst_QStandardItemModel::clear() model.clear(); - QCOMPARE(modelResetSpy.count(), 1); - QCOMPARE(layoutChangedSpy.count(), 0); - QCOMPARE(rowsRemovedSpy.count(), 0); + QCOMPARE(modelResetSpy.size(), 1); + QCOMPARE(layoutChangedSpy.size(), 0); + QCOMPARE(rowsRemovedSpy.size(), 0); QCOMPARE(model.index(0, 0), QModelIndex()); QCOMPARE(model.columnCount(), 0); QCOMPARE(model.rowCount(), 0); @@ -830,8 +813,8 @@ void tst_QStandardItemModel::sort() QFETCH(QStringList, expected); // prepare model QStandardItemModel model; - QVERIFY(model.insertRows(0, initial.count(), QModelIndex())); - QCOMPARE(model.rowCount(QModelIndex()), initial.count()); + QVERIFY(model.insertRows(0, initial.size(), QModelIndex())); + QCOMPARE(model.rowCount(QModelIndex()), initial.size()); model.insertColumns(0, 1, QModelIndex()); QCOMPARE(model.columnCount(QModelIndex()), 1); for (int row = 0; row < model.rowCount(QModelIndex()); ++row) { @@ -847,8 +830,8 @@ void tst_QStandardItemModel::sort() // sort model.sort(0, sortOrder); - QCOMPARE(layoutAboutToBeChangedSpy.count(), 1); - QCOMPARE(layoutChangedSpy.count(), 1); + QCOMPARE(layoutAboutToBeChangedSpy.size(), 1); + QCOMPARE(layoutChangedSpy.size(), 1); // make sure the model is sorted for (int row = 0; row < model.rowCount(QModelIndex()); ++row) { @@ -892,7 +875,7 @@ void tst_QStandardItemModel::sortRole() QFETCH(QVariantList, expectedData); QStandardItemModel model; - for (int i = 0; i < initialText.count(); ++i) { + for (int i = 0; i < initialText.size(); ++i) { QStandardItem *item = new QStandardItem; item->setText(initialText.at(i)); item->setData(initialData.at(i), Qt::UserRole); @@ -900,7 +883,7 @@ void tst_QStandardItemModel::sortRole() } model.setSortRole(sortRole); model.sort(0, sortOrder); - for (int i = 0; i < expectedText.count(); ++i) { + for (int i = 0; i < expectedText.size(); ++i) { QStandardItem *item = model.item(i); QCOMPARE(item->text(), expectedText.at(i)); QCOMPARE(item->data(Qt::UserRole), expectedData.at(i)); @@ -921,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() @@ -931,15 +917,15 @@ void tst_QStandardItemModel::findItems() model.item(1)->appendRow(new QStandardItem(QLatin1String("foo"))); QList<QStandardItem*> matches; matches = model.findItems(QLatin1String("foo"), Qt::MatchExactly|Qt::MatchRecursive, 0); - QCOMPARE(matches.count(), 2); + QCOMPARE(matches.size(), 2); matches = model.findItems(QLatin1String("foo"), Qt::MatchExactly, 0); - QCOMPARE(matches.count(), 1); + QCOMPARE(matches.size(), 1); matches = model.findItems(QLatin1String("food"), Qt::MatchExactly|Qt::MatchRecursive, 0); - QCOMPARE(matches.count(), 0); + QCOMPARE(matches.size(), 0); matches = model.findItems(QLatin1String("foo"), Qt::MatchExactly|Qt::MatchRecursive, -1); - QCOMPARE(matches.count(), 0); + QCOMPARE(matches.size(), 0); matches = model.findItems(QLatin1String("foo"), Qt::MatchExactly|Qt::MatchRecursive, 1); - QCOMPARE(matches.count(), 0); + QCOMPARE(matches.size(), 0); } void tst_QStandardItemModel::getSetHeaderItem() @@ -1050,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); @@ -1085,11 +1087,17 @@ void tst_QStandardItemModel::getSetItemData() QSignalSpy modelDataChangedSpy( &model, &QStandardItemModel::dataChanged); - QVERIFY(model.setItemData(idx, roles)); - QCOMPARE(modelDataChangedSpy.count(), 1); - QVERIFY(model.setItemData(idx, roles)); - QCOMPARE(modelDataChangedSpy.count(), 1); //it was already changed once - QCOMPARE(model.itemData(idx), roles); + QVERIFY(model.setItemData(idx, itemData)); + QCOMPARE(modelDataChangedSpy.size(), 1); + 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), expectedItemData); } void tst_QStandardItemModel::setHeaderLabels_data() @@ -1152,12 +1160,12 @@ void tst_QStandardItemModel::setHeaderLabels() model.setHorizontalHeaderLabels(labels); else model.setVerticalHeaderLabels(labels); - for (int i = 0; i < expectedLabels.count(); ++i) + for (int i = 0; i < expectedLabels.size(); ++i) QCOMPARE(model.headerData(i, orientation).toString(), expectedLabels.at(i)); - QCOMPARE(columnsInsertedSpy.count(), - (orientation == Qt::Vertical) ? 0 : labels.count() > columns); - QCOMPARE(rowsInsertedSpy.count(), - (orientation == Qt::Horizontal) ? 0 : labels.count() > rows); + QCOMPARE(columnsInsertedSpy.size(), + (orientation == Qt::Vertical) ? 0 : labels.size() > columns); + QCOMPARE(rowsInsertedSpy.size(), + (orientation == Qt::Horizontal) ? 0 : labels.size() > rows); } void tst_QStandardItemModel::itemDataChanged() @@ -1168,8 +1176,8 @@ void tst_QStandardItemModel::itemDataChanged() QSignalSpy itemChangedSpy(&model, &QStandardItemModel::itemChanged); model.setItem(0, &item); - QCOMPARE(dataChangedSpy.count(), 1); - QCOMPARE(itemChangedSpy.count(), 1); + QCOMPARE(dataChangedSpy.size(), 1); + QCOMPARE(itemChangedSpy.size(), 1); QModelIndex index = model.indexFromItem(&item); QList<QVariant> args; args = dataChangedSpy.takeFirst(); @@ -1179,8 +1187,8 @@ void tst_QStandardItemModel::itemDataChanged() QCOMPARE(qvariant_cast<QStandardItem*>(args.at(0)), &item); item.setData(QLatin1String("foo"), Qt::DisplayRole); - QCOMPARE(dataChangedSpy.count(), 1); - QCOMPARE(itemChangedSpy.count(), 1); + QCOMPARE(dataChangedSpy.size(), 1); + QCOMPARE(itemChangedSpy.size(), 1); args = dataChangedSpy.takeFirst(); QCOMPARE(qvariant_cast<QModelIndex>(args.at(0)), index); QCOMPARE(qvariant_cast<QModelIndex>(args.at(1)), index); @@ -1188,12 +1196,12 @@ void tst_QStandardItemModel::itemDataChanged() QCOMPARE(qvariant_cast<QStandardItem*>(args.at(0)), &item); item.setData(item.data(Qt::DisplayRole), Qt::DisplayRole); - QCOMPARE(dataChangedSpy.count(), 0); - QCOMPARE(itemChangedSpy.count(), 0); + QCOMPARE(dataChangedSpy.size(), 0); + QCOMPARE(itemChangedSpy.size(), 0); item.setFlags(Qt::ItemIsEnabled); - QCOMPARE(dataChangedSpy.count(), 1); - QCOMPARE(itemChangedSpy.count(), 1); + QCOMPARE(dataChangedSpy.size(), 1); + QCOMPARE(itemChangedSpy.size(), 1); args = dataChangedSpy.takeFirst(); QCOMPARE(qvariant_cast<QModelIndex>(args.at(0)), index); QCOMPARE(qvariant_cast<QModelIndex>(args.at(1)), index); @@ -1201,8 +1209,8 @@ void tst_QStandardItemModel::itemDataChanged() QCOMPARE(qvariant_cast<QStandardItem*>(args.at(0)), &item); item.setFlags(item.flags()); - QCOMPARE(dataChangedSpy.count(), 0); - QCOMPARE(itemChangedSpy.count(), 0); + QCOMPARE(dataChangedSpy.size(), 0); + QCOMPARE(itemChangedSpy.size(), 0); } void tst_QStandardItemModel::takeHeaderItem() @@ -1330,7 +1338,7 @@ void tst_QStandardItemModel::setNullChild() QSignalSpy spy(&model, &QAbstractItemModel::dataChanged); item->setChild(0, nullptr); QCOMPARE(item->child(0), nullptr); - QCOMPARE(spy.count(), 1); + QCOMPARE(spy.size(), 1); } void tst_QStandardItemModel::deleteChild() @@ -1342,7 +1350,7 @@ void tst_QStandardItemModel::deleteChild() QSignalSpy spy(&model, &QAbstractItemModel::dataChanged); delete item->child(0); QCOMPARE(item->child(0), nullptr); - QCOMPARE(spy.count(), 1); + QCOMPARE(spy.size(), 1); } void tst_QStandardItemModel::rootItemFlags() @@ -1583,8 +1591,8 @@ void tst_QStandardItemModel::removeRowsAndColumns() QStringList row_list = QString("1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20").split(','); QStringList col_list = row_list; QStandardItemModel model; - for (int c = 0; c < col_list.count(); c++) - for (int r = 0; r < row_list.count(); r++) + for (int c = 0; c < col_list.size(); c++) + for (int r = 0; r < row_list.size(); r++) model.setItem(r, c, new QStandardItem(row_list[r] + QLatin1Char('x') + col_list[c])); VERIFY_MODEL @@ -1605,16 +1613,22 @@ void tst_QStandardItemModel::removeRowsAndColumns() VERIFY_MODEL QList<QStandardItem *> row_taken = model.takeRow(6); - QCOMPARE(row_taken.count(), col_list.count()); - for (int c = 0; c < col_list.count(); c++) - QCOMPARE(row_taken[c]->text() , row_list[6] + QLatin1Char('x') + col_list[c]); + QCOMPARE(row_taken.size(), col_list.size()); + 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.count(), row_list.count()); - for (int r = 0; r < row_list.count(); r++) - QCOMPARE(col_taken[r]->text() , row_list[r] + QLatin1Char('x') + col_list[10]); + QCOMPARE(col_taken.size(), row_list.size()); + 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 } @@ -1630,8 +1644,8 @@ void tst_QStandardItemModel::itemRoleNames() QStringList row_list = QString("1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20").split(','); QStringList col_list = row_list; QStandardItemModel model; - for (int c = 0; c < col_list.count(); c++) - for (int r = 0; r < row_list.count(); r++) + for (int c = 0; c < col_list.size(); c++) + for (int r = 0; r < row_list.size(); r++) model.setItem(r, c, new QStandardItem(row_list[r] + QLatin1Char('x') + col_list[c])); VERIFY_MODEL @@ -1671,7 +1685,7 @@ void tst_QStandardItemModel::taskQTBUG_45114_setItemData() QModelIndex index = item->index(); QCOMPARE(model.itemData(index).size(), 3); - QCOMPARE(spy.count(), 0); + QCOMPARE(spy.size(), 0); QMap<int, QVariant> roles; @@ -1679,21 +1693,21 @@ void tst_QStandardItemModel::taskQTBUG_45114_setItemData() roles.insert(Qt::UserRole + 2, 2); model.setItemData(index, roles); - QCOMPARE(spy.count(), 0); + QCOMPARE(spy.size(), 0); roles.insert(Qt::UserRole + 1, 1); roles.insert(Qt::UserRole + 2, 2); roles.insert(Qt::UserRole + 3, QVariant()); model.setItemData(index, roles); - QCOMPARE(spy.count(), 0); + QCOMPARE(spy.size(), 0); roles.clear(); roles.insert(Qt::UserRole + 1, 10); roles.insert(Qt::UserRole + 3, 12); model.setItemData(index, roles); - QCOMPARE(spy.count(), 1); + QCOMPARE(spy.size(), 1); QMap<int, QVariant> itemRoles = model.itemData(index); QCOMPARE(itemRoles.size(), 4); @@ -1705,13 +1719,13 @@ void tst_QStandardItemModel::taskQTBUG_45114_setItemData() roles.insert(Qt::UserRole + 3, 1); model.setItemData(index, roles); - QCOMPARE(spy.count(), 2); + QCOMPARE(spy.size(), 2); roles.clear(); roles.insert(Qt::UserRole + 3, QVariant()); model.setItemData(index, roles); - QCOMPARE(spy.count(), 3); + QCOMPARE(spy.size(), 3); itemRoles = model.itemData(index); QCOMPARE(itemRoles.size(), 3); @@ -1764,13 +1778,13 @@ void tst_QStandardItemModel::signalsOnTakeItem() // QTBUG-89145 QSignalSpy dataChangedSpy(&m, &QAbstractItemModel::dataChanged); QStandardItem *const takenItem = m.takeItem(1, 0); for (auto &&spy : removeSpies) { - QCOMPARE(spy->count(), 1); + QCOMPARE(spy->size(), 1); const auto spyArgs = spy->takeFirst(); QCOMPARE(spyArgs.at(0).value<QModelIndex>(), parentIndex); QCOMPARE(spyArgs.at(1).toInt(), 0); QCOMPARE(spyArgs.at(2).toInt(), 1); } - QCOMPARE(dataChangedSpy.count(), 1); + QCOMPARE(dataChangedSpy.size(), 1); const auto dataChangedSpyArgs = dataChangedSpy.takeFirst(); QCOMPARE(dataChangedSpyArgs.at(0).value<QModelIndex>(), m.index(1, 0)); QCOMPARE(dataChangedSpyArgs.at(1).value<QModelIndex>(), m.index(1, 0)); @@ -1784,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 @@ -1821,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" |