File indexing completed on 2024-06-16 10:04:56

0001 /*
0002     SPDX-FileCopyrightText: 2015 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-only
0005 */
0006 
0007 #include "deletejobtest.h"
0008 
0009 #include <QDir>
0010 #include <QFile>
0011 #include <QSignalSpy>
0012 #include <QTemporaryDir>
0013 #include <QTemporaryFile>
0014 #include <QTest>
0015 
0016 #include <kio/deletejob.h>
0017 
0018 QTEST_MAIN(DeleteJobTest)
0019 
0020 static constexpr int KJOB_NO_ERROR = static_cast<int>(KJob::NoError);
0021 
0022 void DeleteJobTest::deleteFileTestCase_data() const
0023 {
0024     QTest::addColumn<QString>("fileName");
0025 
0026     QTest::newRow("latin characters") << "testfile";
0027     QTest::newRow("german umlauts") << "testger\u00E4t";
0028     QTest::newRow("chinese characters") << "\u8A66";
0029 }
0030 
0031 void DeleteJobTest::deleteFileTestCase()
0032 {
0033     QFETCH(QString, fileName);
0034 
0035     QTemporaryFile tempFile;
0036     tempFile.setFileTemplate(fileName + QStringLiteral("XXXXXX"));
0037 
0038     QVERIFY(tempFile.open());
0039     const QString path = tempFile.fileName();
0040     tempFile.close();
0041     QVERIFY(QFile::exists(path));
0042 
0043     /*QBENCHMARK*/ {
0044         KIO::DeleteJob *job = KIO::del(QUrl::fromLocalFile(path), KIO::HideProgressInfo);
0045         job->setUiDelegate(nullptr);
0046 
0047         QSignalSpy spy(job, &KJob::result);
0048         QVERIFY(spy.isValid());
0049         QVERIFY(spy.wait(100000));
0050         QCOMPARE(job->error(), KJOB_NO_ERROR);
0051         QVERIFY(!tempFile.exists());
0052     }
0053 }
0054 
0055 void DeleteJobTest::deleteDirectoryTestCase_data() const
0056 {
0057     QTest::addColumn<QStringList>("fileNames");
0058 
0059     QTest::newRow("non-empty directory") << QStringList{QStringLiteral("1.txt")};
0060     QTest::newRow("empty directory") << QStringList();
0061 }
0062 
0063 void DeleteJobTest::deleteDirectoryTestCase()
0064 {
0065     QTemporaryDir tempDir;
0066     QVERIFY(tempDir.isValid());
0067 
0068     QFETCH(QStringList, fileNames);
0069 
0070     createEmptyTestFiles(fileNames, tempDir.path());
0071 
0072     /*QBENCHMARK*/ {
0073         KIO::DeleteJob *job = KIO::del(QUrl::fromLocalFile(tempDir.path()), KIO::HideProgressInfo);
0074         job->setUiDelegate(nullptr);
0075 
0076         QSignalSpy spy(job, &KJob::result);
0077         QVERIFY(spy.isValid());
0078         QVERIFY(spy.wait(100000));
0079         QCOMPARE(job->error(), KJOB_NO_ERROR);
0080         QVERIFY(!QDir(tempDir.path()).exists());
0081     }
0082 }
0083 
0084 void DeleteJobTest::createEmptyTestFiles(const QStringList &fileNames, const QString &path) const
0085 {
0086     QStringListIterator iterator(fileNames);
0087     while (iterator.hasNext()) {
0088         const QString filename = path + QDir::separator() + iterator.next();
0089         QFile file(filename);
0090         QVERIFY(file.open(QIODevice::WriteOnly));
0091     }
0092 
0093     QCOMPARE(QDir(path).entryList(QDir::Files).count(), fileNames.size());
0094 }
0095 
0096 #include "moc_deletejobtest.cpp"