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

0001 /*
0002     SPDX-FileCopyrightText: 2016 Antonio Larrosa <larrosa@kde.org>
0003     SPDX-FileCopyrightText: 2022 Fushan Wen <qydwhotmail@gmail.com>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include <QDir>
0009 #include <QSignalSpy>
0010 #include <QTest>
0011 #include <QThreadPool>
0012 
0013 #include <KPackage/PackageLoader>
0014 
0015 #include "commontestdata.h"
0016 #include "finder/packagefinder.h"
0017 
0018 class PackageFinderTest : public QObject
0019 {
0020     Q_OBJECT
0021 
0022 private Q_SLOTS:
0023     void initTestCase();
0024 
0025     void testFindPreferredSizeInPackage_data();
0026     void testFindPreferredSizeInPackage();
0027     void testPackageFinderCanFindPackages();
0028 
0029 private:
0030     QDir m_dataDir;
0031 };
0032 
0033 void PackageFinderTest::initTestCase()
0034 {
0035     m_dataDir = QDir(QFINDTESTDATA("testdata/default"));
0036     QVERIFY(!m_dataDir.isEmpty());
0037 }
0038 
0039 void PackageFinderTest::testFindPreferredSizeInPackage_data()
0040 {
0041     // The list of possible screen resolutions to test and the appropriate images that should be chosen
0042     QTest::addColumn<QSize>("resolution");
0043     QTest::addColumn<QString>("expected");
0044     QTest::newRow("1280x1024") << QSize(1280, 1024) << QStringLiteral("1280x1024");
0045     QTest::newRow("1350x1080") << QSize(1350, 1080) << QStringLiteral("1280x1024");
0046     QTest::newRow("1440x1080") << QSize(1440, 1080) << QStringLiteral("1600x1200");
0047     QTest::newRow("1600x1200") << QSize(1600, 1200) << QStringLiteral("1600x1200");
0048     QTest::newRow("1920x1080") << QSize(1920, 1080) << QStringLiteral("1920x1080");
0049     QTest::newRow("1920x1200") << QSize(1920, 1200) << QStringLiteral("1920x1200");
0050     QTest::newRow("3840x2400") << QSize(3840, 2400) << QStringLiteral("3200x2000");
0051     QTest::newRow("4096x2160") << QSize(4096, 2160) << QStringLiteral("3840x2160");
0052     QTest::newRow("3840x2160") << QSize(3840, 2160) << QStringLiteral("3840x2160");
0053     QTest::newRow("3200x1800") << QSize(3200, 1800) << QStringLiteral("3200x1800");
0054     QTest::newRow("2048x1080") << QSize(2048, 1080) << QStringLiteral("1920x1080");
0055     QTest::newRow("1680x1050") << QSize(1680, 1050) << QStringLiteral("1680x1050");
0056     QTest::newRow("1400x1050") << QSize(1400, 1050) << QStringLiteral("1600x1200");
0057     QTest::newRow("1440x900") << QSize(1440, 900) << QStringLiteral("1440x900");
0058     QTest::newRow("1280x960") << QSize(1280, 960) << QStringLiteral("1600x1200");
0059     QTest::newRow("1280x854") << QSize(1280, 854) << QStringLiteral("1280x800");
0060     QTest::newRow("1280x800") << QSize(1280, 800) << QStringLiteral("1280x800");
0061     QTest::newRow("1280x720") << QSize(1280, 720) << QStringLiteral("1366x768");
0062     QTest::newRow("1152x768") << QSize(1152, 768) << QStringLiteral("1280x800");
0063     QTest::newRow("1024x768") << QSize(1024, 768) << QStringLiteral("1024x768");
0064     QTest::newRow("800x600") << QSize(800, 600) << QStringLiteral("1024x768");
0065     QTest::newRow("848x480") << QSize(848, 480) << QStringLiteral("1366x768");
0066     QTest::newRow("720x480") << QSize(720, 480) << QStringLiteral("1280x800");
0067     QTest::newRow("640x480") << QSize(640, 480) << QStringLiteral("1024x768");
0068     QTest::newRow("1366x768") << QSize(1366, 768) << QStringLiteral("1366x768");
0069     QTest::newRow("1600x814") << QSize(1600, 814) << QStringLiteral("1920x1080");
0070 }
0071 
0072 void PackageFinderTest::testFindPreferredSizeInPackage()
0073 {
0074     QFETCH(QSize, resolution);
0075     QFETCH(QString, expected);
0076 
0077     KPackage::Package package = KPackage::PackageLoader::self()->loadPackage(QStringLiteral("Wallpaper/Images"));
0078     package.setPath(m_dataDir.absoluteFilePath(ImageBackendTestData::defaultPackageFolderName2));
0079 
0080     QVERIFY(package.isValid());
0081     QVERIFY(package.metadata().isValid());
0082 
0083     PackageFinder::findPreferredImageInPackage(package, resolution);
0084 
0085     QVERIFY(package.filePath("preferred").contains(expected));
0086 }
0087 
0088 void PackageFinderTest::testPackageFinderCanFindPackages()
0089 {
0090     PackageFinder *finder =
0091         new PackageFinder({m_dataDir.absolutePath(), m_dataDir.absoluteFilePath(ImageBackendTestData::defaultPackageFolderName2)}, QSize(1920, 1080));
0092     QSignalSpy spy(finder, &PackageFinder::packageFound);
0093 
0094     QThreadPool::globalInstance()->start(finder);
0095 
0096     spy.wait(10 * 1000);
0097     QCOMPARE(spy.size(), 1);
0098 
0099     const auto items = spy.takeFirst().at(0).value<QList<KPackage::Package>>();
0100     // Total 3 packages in the directory, but one package is broken and should not be added to the list.
0101     QCOMPARE(items.size(), ImageBackendTestData::defaultPackageCount);
0102 
0103     // Folders are sorted by names
0104     // FEATURE207976-dark-wallpaper
0105     QCOMPARE(items.at(0).filePath("preferred"),
0106              m_dataDir.absoluteFilePath(QStringLiteral("%1/contents/images/1024x768.png").arg(ImageBackendTestData::defaultPackageFolderName1)));
0107     QCOMPARE(items.at(0).filePath("preferredDark"),
0108              m_dataDir.absoluteFilePath(QStringLiteral("%1/contents/images_dark/1920x1080.jpg").arg(ImageBackendTestData::defaultPackageFolderName1)));
0109     // package
0110     QCOMPARE(items.at(1).filePath("preferred"),
0111              m_dataDir.absoluteFilePath(QStringLiteral("%1/contents/images/1920x1080.jpg").arg(ImageBackendTestData::defaultPackageFolderName2)));
0112     QCOMPARE(items.at(1).filePath("preferredDark"), QString());
0113 }
0114 
0115 QTEST_MAIN(PackageFinderTest)
0116 
0117 #include "test_packagefinder.moc"