From 0901d4290f6e7f0d1650b14fc085ecde7fb595ff Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Mon, 15 Jan 2018 15:32:45 +0100 Subject: QFileSystemModel/Win: Fix file system operations failing due to watchers File system operations like renaming/removing may fail on Windows when file system watchers are present. Add functions to QFileSystemModelPrivate to temporarily remove the watchers prior to such operations and to restore them in case of failure. Use them for rename/remove (within a feature check for QFileSystemWatcher and Q_OS_WIN). Task-number: QTBUG-65683 Change-Id: I90142901892fbf9b1e1206a3397a95ffd3c8f010 Reviewed-by: Edward Welbourne Reviewed-by: Shawn Rutledge --- .../qfilesystemmodel/tst_qfilesystemmodel.cpp | 39 ++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'tests/auto/widgets/dialogs') diff --git a/tests/auto/widgets/dialogs/qfilesystemmodel/tst_qfilesystemmodel.cpp b/tests/auto/widgets/dialogs/qfilesystemmodel/tst_qfilesystemmodel.cpp index 71efe1d59a..40a7d56432 100644 --- a/tests/auto/widgets/dialogs/qfilesystemmodel/tst_qfilesystemmodel.cpp +++ b/tests/auto/widgets/dialogs/qfilesystemmodel/tst_qfilesystemmodel.cpp @@ -102,6 +102,7 @@ private slots: void mkdir(); void deleteFile(); + void deleteDirectory(); void caseSensitivity(); @@ -884,6 +885,44 @@ void tst_QFileSystemModel::deleteFile() QVERIFY(!newFile.exists()); } +void tst_QFileSystemModel::deleteDirectory() +{ + // QTBUG-65683: Verify that directories can be removed recursively despite + // file system watchers being active on them or their sub-directories (Windows). + // Create a temporary directory, a nested directory and expand a treeview + // to show them to ensure watcher creation. Then delete the directory. + QTemporaryDir dirToBeDeleted(flatDirTestPath + QStringLiteral("/deleteDirectory-XXXXXX")); + QVERIFY(dirToBeDeleted.isValid()); + const QString dirToBeDeletedPath = dirToBeDeleted.path(); + const QString nestedTestDir = QStringLiteral("test"); + QVERIFY(QDir(dirToBeDeletedPath).mkpath(nestedTestDir)); + const QString nestedTestDirPath = dirToBeDeletedPath + QLatin1Char('/') + nestedTestDir; + QFile testFile(nestedTestDirPath + QStringLiteral("/test.txt")); + QVERIFY(testFile.open(QIODevice::WriteOnly | QIODevice::Text)); + testFile.write("Hello\n"); + testFile.close(); + + QFileSystemModel model; + const QModelIndex rootIndex = model.setRootPath(flatDirTestPath); + QTreeView treeView; + treeView.setWindowTitle(QTest::currentTestFunction()); + treeView.setModel(&model); + treeView.setRootIndex(rootIndex); + + const QModelIndex dirToBeDeletedPathIndex = model.index(dirToBeDeletedPath); + QVERIFY(dirToBeDeletedPathIndex.isValid()); + treeView.setExpanded(dirToBeDeletedPathIndex, true); + const QModelIndex nestedTestDirIndex = model.index(nestedTestDirPath); + QVERIFY(nestedTestDirIndex.isValid()); + treeView.setExpanded(nestedTestDirIndex, true); + + treeView.show(); + QVERIFY(QTest::qWaitForWindowExposed(&treeView)); + + QVERIFY(model.remove(dirToBeDeletedPathIndex)); + dirToBeDeleted.setAutoRemove(false); +} + static QString flipCase(QString s) { for (int i = 0, size = s.size(); i < size; ++i) { -- cgit v1.2.3