diff options
Diffstat (limited to 'tests/auto/corelib/io/qfile/tst_qfile.cpp')
-rw-r--r-- | tests/auto/corelib/io/qfile/tst_qfile.cpp | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/tests/auto/corelib/io/qfile/tst_qfile.cpp b/tests/auto/corelib/io/qfile/tst_qfile.cpp index 16a30af892..b065e1f10f 100644 --- a/tests/auto/corelib/io/qfile/tst_qfile.cpp +++ b/tests/auto/corelib/io/qfile/tst_qfile.cpp @@ -279,6 +279,9 @@ private slots: void reuseQFile(); + void moveToTrash_data(); + void moveToTrash(); + private: #ifdef BUILTIN_TESTDATA QSharedPointer<QTemporaryDir> m_dataDir; @@ -3672,5 +3675,145 @@ void tst_QFile::reuseQFile() } } +void tst_QFile::moveToTrash_data() +{ + QTest::addColumn<QString>("source"); + QTest::addColumn<bool>("create"); + QTest::addColumn<bool>("result"); + + if (QOperatingSystemVersion::current() <= QOperatingSystemVersion::Windows7) + QSKIP("Windows 7 insists on showing a confirmation dialog", SkipAll); + + // success cases + { + QTemporaryFile temp; + QVERIFY(temp.open()); + QTest::newRow("temporary file") << temp.fileName() << true << true; + } + { + QTemporaryDir tempDir; + tempDir.setAutoRemove(false); + QTest::newRow("temporary dir") + << tempDir.path() + QLatin1Char('/') + << true << true; + } + { + QTemporaryDir homeDir(QDir::homePath() + QLatin1String("/XXXXXX")); + homeDir.setAutoRemove(false); + QTemporaryFile homeFile(homeDir.path() + + QLatin1String("/tst_qfile-XXXXXX")); + homeFile.open(); + QTest::newRow("home file") + << homeFile.fileName() + << true << true; + + QTest::newRow("home dir") + << homeDir.path() + QLatin1Char('/') + << true << true; + } + QTest::newRow("relative") << QStringLiteral("tst_qfile_moveToTrash.tmp") << true << true; + + // failure cases + QTest::newRow("root") << QDir::rootPath() << false << false; + QTest::newRow("no-such-file") << QString::fromLatin1("no/such/file") << false << false; +} + +void tst_QFile::moveToTrash() +{ + QFETCH(QString, source); + QFETCH(bool, create); + QFETCH(bool, result); + + /* This test makes assumptions about the file system layout + which might be wrong - moveToTrash may fail if the file lives + on a file system that is different from the home file system, and + has no .Trash directory. + */ + const bool mayFail = QStorageInfo(source) != QStorageInfo(QDir::home()); + +#if defined(Q_OS_WINRT) + QSKIP("WinRT does not have a trash", SkipAll); +#endif + + auto ensureFile = [](const QString &source, bool create) { + if (QFileInfo::exists(source) || !create) + return; + if (source.endsWith(QLatin1Char('/'))) { + QDir::root().mkdir(source); + QFile file(source + QLatin1String("test")); + if (!file.open(QIODevice::WriteOnly)) + QSKIP("Couldn't create directory with file"); + } else { + QFile sourceFile(source); + QVERIFY2(sourceFile.open(QFile::WriteOnly | QFile::Text), qPrintable(sourceFile.errorString())); + sourceFile.close(); + } + }; + auto cleanupFile = [source, create]() { + if (!QFileInfo::exists(source) || !create) + return; + if (source.endsWith(QLatin1Char('/'))) { + QDir(source).removeRecursively(); + } else { + QFile sourceFile(source); + sourceFile.remove(); + } + }; + // non-static version + { + ensureFile(source, create); + QFile sourceFile(source); + const bool success = sourceFile.moveToTrash(); + + // tolerate moveToTrash failing + if (result && !success && mayFail) + result = false; + + if (result) { + // if any of the test fails, we still want to remove the file + auto onFailure = qScopeGuard(cleanupFile); + QVERIFY2(success, qPrintable(sourceFile.errorString())); + QCOMPARE(sourceFile.error(), QFile::NoError); + QVERIFY(source != sourceFile.fileName()); + if (!sourceFile.fileName().isEmpty()) { + QVERIFY2(sourceFile.exists(), qPrintable(sourceFile.fileName())); + // remove file/dir in trash as well, don't fill disk + if (source.endsWith(QLatin1Char('/'))) + QDir(sourceFile.fileName()).removeRecursively(); + else + sourceFile.remove(); + } + } else { + QVERIFY(!success); + QVERIFY(!sourceFile.errorString().isEmpty()); + QCOMPARE(source, sourceFile.fileName()); + } + } + + // don't retry + if (mayFail) + return; + + // static version + { + ensureFile(source, create); + QString pathInTrash; + const bool success = QFile::moveToTrash(source, &pathInTrash); + QCOMPARE(success, result); + if (result) { + auto onFailure = qScopeGuard(cleanupFile); + QVERIFY(source != pathInTrash); + if (!pathInTrash.isEmpty()) { + // remove file/dir in trash as well, don't fill disk + QVERIFY2(QFile::exists(pathInTrash), qPrintable(pathInTrash)); + if (source.endsWith(QLatin1Char('/'))) + QDir(pathInTrash).removeRecursively(); + else + QFile::remove(pathInTrash); + } + } + } +} + QTEST_MAIN(tst_QFile) #include "tst_qfile.moc" |