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

0001 /*
0002     SPDX-FileCopyrightText: 2013 Mark Gaiser <markg85@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-only
0005 */
0006 
0007 #include "listdirtest.h"
0008 #include <kio/listjob.h>
0009 
0010 #include <QDir>
0011 #include <QFile>
0012 #include <QSignalSpy>
0013 #include <QTemporaryDir>
0014 #include <QTest>
0015 
0016 QTEST_MAIN(ListDirTest)
0017 
0018 void ListDirTest::numFilesTestCase_data()
0019 {
0020     QTest::addColumn<int>("numOfFiles");
0021     QTest::newRow("10 files") << 10;
0022     QTest::newRow("100 files") << 100;
0023     QTest::newRow("1000 files") << 1000;
0024 }
0025 
0026 void ListDirTest::numFilesTestCase()
0027 {
0028     QFETCH(int, numOfFiles);
0029 
0030     QTemporaryDir tempDir;
0031     QVERIFY(tempDir.isValid());
0032 
0033     createEmptyTestFiles(numOfFiles, tempDir.path());
0034 
0035     /*QBENCHMARK*/ {
0036         m_receivedEntryCount = -2; // We start at -2 for . and .. slotResult will just increment this value
0037         KIO::ListJob *job = KIO::listDir(QUrl::fromLocalFile(tempDir.path()), KIO::HideProgressInfo);
0038         job->setUiDelegate(nullptr);
0039         connect(job, &KIO::ListJob::entries, this, &ListDirTest::slotEntries);
0040 
0041         QSignalSpy spy(job, &KJob::result);
0042         QVERIFY(spy.wait(100000));
0043         QCOMPARE(job->error(), 0); // no error
0044     }
0045     QCOMPARE(m_receivedEntryCount, numOfFiles);
0046 }
0047 
0048 void ListDirTest::slotEntries(KIO::Job *, const KIO::UDSEntryList &entries)
0049 {
0050     m_receivedEntryCount += entries.count();
0051 }
0052 
0053 void ListDirTest::createEmptyTestFiles(int numOfFilesToCreate, const QString &path)
0054 {
0055     for (int i = 0; i < numOfFilesToCreate; i++) {
0056         const QString filename = path + QDir::separator() + QString::number(i) + ".txt";
0057         QFile file(filename);
0058         QVERIFY(file.open(QIODevice::WriteOnly));
0059     }
0060 
0061     QCOMPARE(QDir(path).entryList(QDir::Files).count(), numOfFilesToCreate);
0062 }
0063 
0064 #include "moc_listdirtest.cpp"