File indexing completed on 2024-11-10 05:02:43

0001 /*
0002     SPDX-FileCopyrightText: 2022 Fushan Wen <qydwhotmail@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include <QDir>
0008 #include <QSignalSpy>
0009 #include <QTest>
0010 #include <QThreadPool>
0011 
0012 #include "commontestdata.h"
0013 #include "finder/imagefinder.h"
0014 
0015 class ImageFinderTest : public QObject
0016 {
0017     Q_OBJECT
0018 
0019 private Q_SLOTS:
0020     void initTestCase();
0021     void cleanupTestCase();
0022     void testImageFinderCanFindImages();
0023 
0024 private:
0025     QDir m_dataDir;
0026 };
0027 
0028 void ImageFinderTest::initTestCase()
0029 {
0030     m_dataDir = QDir(QFINDTESTDATA("testdata/default"));
0031     QVERIFY(!m_dataDir.isEmpty());
0032     renameBizarreFile(m_dataDir);
0033 }
0034 
0035 void ImageFinderTest::cleanupTestCase()
0036 {
0037     restoreBizarreFile(m_dataDir);
0038 }
0039 
0040 void ImageFinderTest::testImageFinderCanFindImages()
0041 {
0042     ImageFinder *finder = new ImageFinder({m_dataDir.absolutePath(), m_dataDir.absoluteFilePath(QStringLiteral("thispathdoesnotexist.jpg"))});
0043     QSignalSpy spy(finder, &ImageFinder::imageFound);
0044 
0045     QThreadPool::globalInstance()->start(finder);
0046 
0047     spy.wait(10 * 1000);
0048     QCOMPARE(spy.count(), 1);
0049 
0050     /**
0051      * Expected result:
0052      *
0053      * - wallpaper.jpg.jpg is found.
0054      * - "# BUG454692 file name with hash char.png" is found.
0055      * - symlinkshouldnotbefoundbythefinder.jpg is ignored.
0056      * - screenshot.png is ignored.
0057      * - All images in package/contents/images/ are ignored.
0058      *
0059      * So the total number of images found by ImageFinder is 2.
0060      */
0061     const auto paths = spy.takeFirst().at(0).toStringList();
0062 
0063     qInfo() << "Found images:" << paths;
0064     QCOMPARE(paths.size(), ImageBackendTestData::defaultImageCount);
0065     QTRY_COMPARE(paths.count(m_dataDir.absoluteFilePath(ImageBackendTestData::defaultImageFileName1)), 1);
0066     QTRY_COMPARE(paths.count(m_dataDir.absoluteFilePath(ImageBackendTestData::defaultImageFileName2)), 1);
0067     QCOMPARE(paths.count(m_dataDir.absoluteFilePath(ImageBackendTestData::defaultImageFileName3)), 1);
0068     QCOMPARE(paths.count(m_dataDir.absoluteFilePath(ImageBackendTestData::defaultImageFileName4)), 1);
0069     QCOMPARE(paths.count(m_dataDir.absoluteFilePath(ImageBackendTestData::defaultImageFileName5)), 1);
0070     QCOMPARE(paths.count(m_dataDir.absoluteFilePath(ImageBackendTestData::defaultHiddenImageFileName)), 0);
0071 }
0072 
0073 QTEST_MAIN(ImageFinderTest)
0074 
0075 #include "test_imagefinder.moc"