From 2f35653a3058840e042ed92aba8f04d0abdf7d61 Mon Sep 17 00:00:00 2001 From: Andreas Buhr Date: Wed, 16 Feb 2022 10:45:46 +0100 Subject: Use QAbstractItemModelTester or QFileSystemModel This patch enables usage of QAbstractItemModelTester on QFileSystemModel. QAbstractItemModelTester called fetchMore() on all items. QFileSystemModel represents the whole file system. This led to very long test runs. To avoid this, this patch introduces a new feature in QAbstractItemModelTester, namely to disable calling of fetchMore(). Change-Id: Ie5d2e22fa4c143be7c080d9f79632cd2cbe07aac Reviewed-by: David Faure --- src/testlib/qabstractitemmodeltester.cpp | 24 ++++++++++++--- src/testlib/qabstractitemmodeltester.h | 1 + .../qfilesystemmodel/tst_qfilesystemmodel.cpp | 35 ++++++++++++++++++++++ 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/src/testlib/qabstractitemmodeltester.cpp b/src/testlib/qabstractitemmodeltester.cpp index 39db02831a..e7aa8979d5 100644 --- a/src/testlib/qabstractitemmodeltester.cpp +++ b/src/testlib/qabstractitemmodeltester.cpp @@ -91,6 +91,7 @@ private: QStack insert; QStack remove; + bool useFetchMore = true; bool fetchingMore; enum class ChangeInFlight { @@ -315,6 +316,19 @@ QAbstractItemModelTester::FailureReportingMode QAbstractItemModelTester::failure return d->failureReportingMode; } +/*! + If \a value is true, enables dynamic population of the + tested model, which is the default. + If \a value is false, it disables it. + + \since 6.4 +*/ +void QAbstractItemModelTester::setUseFetchMore(bool value) +{ + Q_D(QAbstractItemModelTester); + d->useFetchMore = value; +} + bool QAbstractItemModelTester::verify(bool statement, const char *statementStr, const char *description, const char *file, int line) { Q_D(QAbstractItemModelTester); @@ -349,9 +363,11 @@ void QAbstractItemModelTesterPrivate::nonDestructiveBasicTest() MODELTESTER_VERIFY(!model->buddy(QModelIndex()).isValid()); model->canFetchMore(QModelIndex()); MODELTESTER_VERIFY(model->columnCount(QModelIndex()) >= 0); - fetchingMore = true; - model->fetchMore(QModelIndex()); - fetchingMore = false; + if (useFetchMore) { + fetchingMore = true; + model->fetchMore(QModelIndex()); + fetchingMore = false; + } Qt::ItemFlags flags = model->flags(QModelIndex()); MODELTESTER_VERIFY(flags == Qt::ItemIsDropEnabled || flags == 0); model->hasChildren(QModelIndex()); @@ -529,7 +545,7 @@ void QAbstractItemModelTesterPrivate::checkChildren(const QModelIndex &parent, i p = p.parent(); // For models that are dynamically populated - if (model->canFetchMore(parent)) { + if (model->canFetchMore(parent) && useFetchMore) { fetchingMore = true; model->fetchMore(parent); fetchingMore = false; diff --git a/src/testlib/qabstractitemmodeltester.h b/src/testlib/qabstractitemmodeltester.h index dc58685e5e..c4d94be261 100644 --- a/src/testlib/qabstractitemmodeltester.h +++ b/src/testlib/qabstractitemmodeltester.h @@ -47,6 +47,7 @@ public: QAbstractItemModel *model() const; FailureReportingMode failureReportingMode() const; + void setUseFetchMore(bool value); private: friend inline bool QTestPrivate::testDataGuiRoles(QAbstractItemModelTester *tester); diff --git a/tests/auto/gui/itemmodels/qfilesystemmodel/tst_qfilesystemmodel.cpp b/tests/auto/gui/itemmodels/qfilesystemmodel/tst_qfilesystemmodel.cpp index 4c94b17df2..ec724c284f 100644 --- a/tests/auto/gui/itemmodels/qfilesystemmodel/tst_qfilesystemmodel.cpp +++ b/tests/auto/gui/itemmodels/qfilesystemmodel/tst_qfilesystemmodel.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #if defined(Q_OS_WIN) # include // for SetFileAttributes #endif @@ -149,6 +150,8 @@ void tst_QFileSystemModel::indexPath() { #if !defined(Q_OS_WIN) QScopedPointer model(new QFileSystemModel); + QAbstractItemModelTester tester(model.get()); + tester.setUseFetchMore(false); int depth = QDir::currentPath().count('/'); model->setRootPath(QDir::currentPath()); QString backPath; @@ -163,6 +166,8 @@ void tst_QFileSystemModel::indexPath() void tst_QFileSystemModel::rootPath() { QScopedPointer model(new QFileSystemModel); + QAbstractItemModelTester tester(model.get()); + tester.setUseFetchMore(false); QCOMPARE(model->rootPath(), QString(QDir().path())); QSignalSpy rootChanged(model.data(), &QFileSystemModel::rootPathChanged); @@ -227,6 +232,8 @@ void tst_QFileSystemModel::rootPath() void tst_QFileSystemModel::readOnly() { QScopedPointer 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())); @@ -280,6 +287,8 @@ private: void tst_QFileSystemModel::iconProvider() { QScopedPointer model(new QFileSystemModel); + QAbstractItemModelTester tester(model.get()); + tester.setUseFetchMore(false); QVERIFY(model->iconProvider()); QScopedPointer provider(new QFileIconProvider); model->setIconProvider(provider.data()); @@ -389,6 +398,8 @@ void tst_QFileSystemModel::rowCount() QSignalSpy *spy2 = nullptr; QSignalSpy *spy3 = nullptr; QScopedPointer model(new QFileSystemModel); + QAbstractItemModelTester tester(model.get()); + tester.setUseFetchMore(false); QModelIndex root = prepareTestModelRoot(model.data(), flatDirTestPath, &spy2, &spy3); QVERIFY(root.isValid()); @@ -417,6 +428,8 @@ void tst_QFileSystemModel::rowsInserted() { const QString tmp = flatDirTestPath; QScopedPointer model(new QFileSystemModel); + QAbstractItemModelTester tester(model.get()); + tester.setUseFetchMore(false); QModelIndex root = prepareTestModelRoot(model.data(), tmp); QVERIFY(root.isValid()); @@ -471,6 +484,8 @@ void tst_QFileSystemModel::rowsRemoved() { const QString tmp = flatDirTestPath; QScopedPointer model(new QFileSystemModel); + QAbstractItemModelTester tester(model.get()); + tester.setUseFetchMore(false); QModelIndex root = prepareTestModelRoot(model.data(), tmp); QVERIFY(root.isValid()); @@ -533,6 +548,8 @@ void tst_QFileSystemModel::dataChanged() const QString tmp = flatDirTestPath; QScopedPointer model(new QFileSystemModel); + QAbstractItemModelTester tester(model.get()); + tester.setUseFetchMore(false); QModelIndex root = prepareTestModelRoot(model.data(), tmp); QVERIFY(root.isValid()); @@ -593,6 +610,8 @@ void tst_QFileSystemModel::filters() { QString tmp = flatDirTestPath; QScopedPointer model(new QFileSystemModel); + QAbstractItemModelTester tester(model.get()); + tester.setUseFetchMore(false); QVERIFY(createFiles(model.data(), tmp, QStringList())); QModelIndex root = model->setRootPath(tmp); QFETCH(QStringList, files); @@ -661,6 +680,8 @@ void tst_QFileSystemModel::nameFilters() QStringList list; list << "a" << "b" << "c"; QScopedPointer model(new QFileSystemModel); + QAbstractItemModelTester tester(model.get()); + tester.setUseFetchMore(false); model->setNameFilters(list); model->setNameFilterDisables(false); QCOMPARE(model->nameFilters(), list); @@ -706,6 +727,8 @@ void tst_QFileSystemModel::setData_data() void tst_QFileSystemModel::setData() { QScopedPointer model(new QFileSystemModel); + QAbstractItemModelTester tester(model.get()); + tester.setUseFetchMore(false); QSignalSpy spy(model.data(), &QFileSystemModel::fileRenamed); QFETCH(QString, subdirName); QFETCH(QStringList, files); @@ -758,6 +781,8 @@ void tst_QFileSystemModel::sortPersistentIndex() file.close(); QTRY_VERIFY(QDir(flatDirTestPath).entryInfoList().contains(fileInfo)); QScopedPointer model(new QFileSystemModel); + QAbstractItemModelTester tester(model.get()); + tester.setUseFetchMore(false); QModelIndex root = model->setRootPath(flatDirTestPath); QTRY_VERIFY(model->rowCount(root) > 0); @@ -864,6 +889,8 @@ void tst_QFileSystemModel::mkdir() QString tmp = flatDirTestPath; QString newFolderPath = QDir::toNativeSeparators(tmp + '/' + "NewFoldermkdirtest4"); QScopedPointer model(new QFileSystemModel); + QAbstractItemModelTester tester(model.get()); + tester.setUseFetchMore(false); QModelIndex tmpDir = model->index(tmp); QVERIFY(tmpDir.isValid()); QDir bestatic(newFolderPath); @@ -899,6 +926,8 @@ void tst_QFileSystemModel::deleteFile() } newFile.close(); QScopedPointer model(new QFileSystemModel); + QAbstractItemModelTester tester(model.get()); + tester.setUseFetchMore(false); QModelIndex idx = model->index(newFilePath); QVERIFY(idx.isValid()); QVERIFY(model->remove(idx)); @@ -961,6 +990,8 @@ void tst_QFileSystemModel::caseSensitivity() QStringList files; files << "a" << "c" << "C"; QScopedPointer model(new QFileSystemModel); + QAbstractItemModelTester tester(model.get()); + tester.setUseFetchMore(false); QVERIFY(createFiles(model.data(), tmp, files)); QModelIndex root = model->index(tmp); QStringList paths; @@ -1026,6 +1057,8 @@ void tst_QFileSystemModel::dirsBeforeFiles() } QScopedPointer 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); @@ -1100,6 +1133,8 @@ void tst_QFileSystemModel::permissions() // checks QTBUG-20503 const QString tmp = flatDirTestPath; const QString file = tmp + QLatin1String("/f"); QScopedPointer model(new QFileSystemModel); + QAbstractItemModelTester tester(model.get()); + tester.setUseFetchMore(false); QVERIFY(createFiles(model.data(), tmp, QStringList{QLatin1String("f")})); QVERIFY(QFile::setPermissions(file, permissions)); -- cgit v1.2.3