File indexing completed on 2024-06-23 10:37:16

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2017 Chinmoy Ranjan Pradhan <chinmoyrp65@gmail.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 
0008 #include <QSignalSpy>
0009 #include <QTest>
0010 
0011 #include <KIO/BatchRenameJob>
0012 
0013 #include "kiotesthelper.h"
0014 
0015 class BatchRenameJobTest : public QObject
0016 {
0017     Q_OBJECT
0018 
0019 private:
0020     void createTestFiles(const QStringList &fileList)
0021     {
0022         for (const QString &filename : fileList) {
0023             createTestFile(m_homeDir + filename);
0024         }
0025     }
0026 
0027     bool checkFileExistence(const QStringList &fileList)
0028     {
0029         for (const QString &filename : fileList) {
0030             const QString filePath = m_homeDir + filename;
0031             if (!QFile::exists(filePath)) {
0032                 return false;
0033             }
0034         }
0035         return true;
0036     }
0037 
0038     QList<QUrl> createUrlList(const QStringList &fileList)
0039     {
0040         QList<QUrl> srcList;
0041         srcList.reserve(fileList.count());
0042         for (const QString &filename : fileList) {
0043             const QString filePath = m_homeDir + filename;
0044             srcList.append(QUrl::fromLocalFile(filePath));
0045         }
0046         return srcList;
0047     }
0048 
0049 private Q_SLOTS:
0050     void initTestCase()
0051     {
0052         QStandardPaths::setTestModeEnabled(true);
0053 
0054         cleanupTestCase();
0055 
0056         // Create temporary home directory
0057         m_homeDir = homeTmpDir();
0058     }
0059 
0060     void cleanupTestCase()
0061     {
0062         QDir(homeTmpDir()).removeRecursively();
0063     }
0064 
0065     void batchRenameJobTest_data()
0066     {
0067         QTest::addColumn<QStringList>("oldFilenames");
0068         QTest::addColumn<QString>("baseName");
0069         QTest::addColumn<int>("index");
0070         QTest::addColumn<QChar>("indexPlaceholder");
0071         QTest::addColumn<QStringList>("newFilenames");
0072 
0073         /* clang-format off */
0074         QTest::newRow("different-extensions-single-placeholder") << (QStringList{"old_file_without_extension", "old_file.txt", "old_file.zip"})
0075                                                                  << "#-new_name"
0076                                                                  << 1
0077                                                                  << QChar('#')
0078                                                                  << QStringList{"1-new_name", "2-new_name.txt", "3-new_name.zip"};
0079 
0080         QTest::newRow("same-extensions-placeholder-sequence") << (QStringList{"first_source.cpp", "second_source.cpp", "third_source.java"})
0081                                                               << "new_source###"
0082                                                               << 8
0083                                                               << QChar('#')
0084                                                               << QStringList{"new_source008.cpp", "new_source009.cpp", "new_source010.java"};
0085 
0086         QTest::newRow("different-extensions-invalid-placeholder") << (QStringList{"audio.mp3", "video.mp4", "movie.mkv"})
0087                                                                   << "me#d#ia"
0088                                                                   << 0
0089                                                                   << QChar('#')
0090                                                                   << QStringList{"me#d#ia.mp3", "me#d#ia.mp4", "me#d#ia.mkv"};
0091 
0092         QTest::newRow("same-extensions-invalid-placeholder") << (QStringList{"random_headerfile.h", "another_headerfile.h", "random_sourcefile.c"})
0093                                                              << "##file#"
0094                                                              << 4
0095                                                              << QChar('#')
0096                                                              << QStringList{"##file#4.h", "##file#5.h", "##file#6.c"};
0097         /* clang-format on */
0098     }
0099 
0100     void batchRenameJobTest()
0101     {
0102         QFETCH(QStringList, oldFilenames);
0103         QFETCH(QString, baseName);
0104         QFETCH(int, index);
0105         QFETCH(QChar, indexPlaceholder);
0106         QFETCH(QStringList, newFilenames);
0107         createTestFiles(oldFilenames);
0108         QVERIFY(checkFileExistence(oldFilenames));
0109         KIO::BatchRenameJob *job = KIO::batchRename(createUrlList(oldFilenames), baseName, index, indexPlaceholder);
0110         job->setUiDelegate(nullptr);
0111         QSignalSpy spy(job, &KIO::BatchRenameJob::fileRenamed);
0112         QVERIFY2(job->exec(), qPrintable(job->errorString()));
0113         QCOMPARE(spy.count(), oldFilenames.count());
0114         QCOMPARE(job->processedAmount(KJob::Items), oldFilenames.count());
0115         QCOMPARE(job->totalAmount(KJob::Items), oldFilenames.count());
0116         QVERIFY(!checkFileExistence(oldFilenames));
0117         QVERIFY(checkFileExistence(newFilenames));
0118     }
0119 
0120 private:
0121     QString m_homeDir;
0122 };
0123 
0124 QTEST_MAIN(BatchRenameJobTest)
0125 
0126 #include "batchrenamejobtest.moc"