diff options
Diffstat (limited to 'tests/auto/gui/itemmodels/qfilesystemmodel/tst_qfilesystemmodel.cpp')
-rw-r--r-- | tests/auto/gui/itemmodels/qfilesystemmodel/tst_qfilesystemmodel.cpp | 138 |
1 files changed, 95 insertions, 43 deletions
diff --git a/tests/auto/gui/itemmodels/qfilesystemmodel/tst_qfilesystemmodel.cpp b/tests/auto/gui/itemmodels/qfilesystemmodel/tst_qfilesystemmodel.cpp index ec724c284f..8ef0b6272a 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(); @@ -173,7 +177,7 @@ void tst_QFileSystemModel::rootPath() 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); @@ -190,26 +194,26 @@ 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()); } @@ -308,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) @@ -403,8 +420,8 @@ void tst_QFileSystemModel::rowCount() 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() @@ -446,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; @@ -455,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() @@ -503,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; @@ -529,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); } } @@ -565,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() @@ -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,6 +692,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; @@ -748,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); @@ -758,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()); @@ -827,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(); @@ -862,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 @@ -997,7 +1048,7 @@ void tst_QFileSystemModel::caseSensitivity() 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()); @@ -1007,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); } @@ -1028,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); } |