From 5ce62bd74ca84abf30b31fd376df370caba403dc Mon Sep 17 00:00:00 2001 From: Fushan Wen Date: Sat, 9 Apr 2022 19:37:31 +0800 Subject: [PATCH] wallpapers/image: Add autotest for PackageFinder Test PackageFinder can find packages and choose a correct size. --- .../image/plugin/autotests/CMakeLists.txt | 4 + .../plugin/autotests/test_packagefinder.cpp | 103 ++++++++++++++++++ .../the folder is intended to leave blank.txt | 1 + .../default/brokenpackage/metadata.desktop | 7 ++ 4 files changed, 115 insertions(+) create mode 100644 wallpapers/image/plugin/autotests/test_packagefinder.cpp create mode 100644 wallpapers/image/plugin/autotests/testdata/default/brokenpackage/contents/images/the folder is intended to leave blank.txt create mode 100644 wallpapers/image/plugin/autotests/testdata/default/brokenpackage/metadata.desktop diff --git a/wallpapers/image/plugin/autotests/CMakeLists.txt b/wallpapers/image/plugin/autotests/CMakeLists.txt index 0f355ab6c..9a7e125e6 100644 --- a/wallpapers/image/plugin/autotests/CMakeLists.txt +++ b/wallpapers/image/plugin/autotests/CMakeLists.txt @@ -20,3 +20,7 @@ ecm_add_test(test_imagesizefinder.cpp TEST_NAME testimagesizefinder # ImageFinder test ecm_add_test(test_imagefinder.cpp TEST_NAME testimagefinder LINK_LIBRARIES Qt::Test plasma_wallpaper_imageplugin_static) + +# PackageFinder test +ecm_add_test(test_packagefinder.cpp TEST_NAME testpackageimagefinder + LINK_LIBRARIES Qt::Test plasma_wallpaper_imageplugin_static) diff --git a/wallpapers/image/plugin/autotests/test_packagefinder.cpp b/wallpapers/image/plugin/autotests/test_packagefinder.cpp new file mode 100644 index 000000000..a7cbc635e --- /dev/null +++ b/wallpapers/image/plugin/autotests/test_packagefinder.cpp @@ -0,0 +1,103 @@ +/* + SPDX-FileCopyrightText: 2016 Antonio Larrosa + SPDX-FileCopyrightText: 2022 Fushan Wen + + SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include +#include + +#include + +#include "finder/packagefinder.h" + +class PackageFinderTest : public QObject +{ + Q_OBJECT + +private Q_SLOTS: + void initTestCase(); + + void testFindPreferredSizeInPackage_data(); + void testFindPreferredSizeInPackage(); + void testPackageFinderCanFindPackages(); + +private: + QDir m_dataDir; +}; + +void PackageFinderTest::initTestCase() +{ + m_dataDir = QDir(QFINDTESTDATA("testdata/default")); + QVERIFY(!m_dataDir.isEmpty()); +} + +void PackageFinderTest::testFindPreferredSizeInPackage_data() +{ + // The list of possible screen resolutions to test and the appropriate images that should be chosen + QTest::addColumn("resolution"); + QTest::addColumn("expected"); + QTest::newRow("1280x1024") << QSize(1280, 1024) << QStringLiteral("1280x1024"); + QTest::newRow("1350x1080") << QSize(1350, 1080) << QStringLiteral("1280x1024"); + QTest::newRow("1440x1080") << QSize(1440, 1080) << QStringLiteral("1600x1200"); + QTest::newRow("1600x1200") << QSize(1600, 1200) << QStringLiteral("1600x1200"); + QTest::newRow("1920x1080") << QSize(1920, 1080) << QStringLiteral("1920x1080"); + QTest::newRow("1920x1200") << QSize(1920, 1200) << QStringLiteral("1920x1200"); + QTest::newRow("3840x2400") << QSize(3840, 2400) << QStringLiteral("3200x2000"); + QTest::newRow("4096x2160") << QSize(4096, 2160) << QStringLiteral("3840x2160"); + QTest::newRow("3840x2160") << QSize(3840, 2160) << QStringLiteral("3840x2160"); + QTest::newRow("3200x1800") << QSize(3200, 1800) << QStringLiteral("3200x1800"); + QTest::newRow("2048x1080") << QSize(2048, 1080) << QStringLiteral("1920x1080"); + QTest::newRow("1680x1050") << QSize(1680, 1050) << QStringLiteral("1680x1050"); + QTest::newRow("1400x1050") << QSize(1400, 1050) << QStringLiteral("1600x1200"); + QTest::newRow("1440x900") << QSize(1440, 900) << QStringLiteral("1440x900"); + QTest::newRow("1280x960") << QSize(1280, 960) << QStringLiteral("1600x1200"); + QTest::newRow("1280x854") << QSize(1280, 854) << QStringLiteral("1280x800"); + QTest::newRow("1280x800") << QSize(1280, 800) << QStringLiteral("1280x800"); + QTest::newRow("1280x720") << QSize(1280, 720) << QStringLiteral("1366x768"); + QTest::newRow("1152x768") << QSize(1152, 768) << QStringLiteral("1280x800"); + QTest::newRow("1024x768") << QSize(1024, 768) << QStringLiteral("1024x768"); + QTest::newRow("800x600") << QSize(800, 600) << QStringLiteral("1024x768"); + QTest::newRow("848x480") << QSize(848, 480) << QStringLiteral("1366x768"); + QTest::newRow("720x480") << QSize(720, 480) << QStringLiteral("1280x800"); + QTest::newRow("640x480") << QSize(640, 480) << QStringLiteral("1024x768"); + QTest::newRow("1366x768") << QSize(1366, 768) << QStringLiteral("1366x768"); + QTest::newRow("1600x814") << QSize(1600, 814) << QStringLiteral("1920x1080"); +} + +void PackageFinderTest::testFindPreferredSizeInPackage() +{ + QFETCH(QSize, resolution); + QFETCH(QString, expected); + + KPackage::Package package = KPackage::PackageLoader::self()->loadPackage(QStringLiteral("Wallpaper/Images")); + package.setPath(m_dataDir.absoluteFilePath(QStringLiteral("package"))); + + QVERIFY(package.isValid()); + QVERIFY(package.metadata().isValid()); + + PackageFinder::findPreferredImageInPackage(package, resolution); + + QVERIFY(package.filePath("preferred").contains(expected)); +} + +void PackageFinderTest::testPackageFinderCanFindPackages() +{ + PackageFinder *finder = new PackageFinder({m_dataDir.absolutePath(), m_dataDir.absoluteFilePath(QStringLiteral("package"))}, QSize(1920, 1080)); + QSignalSpy spy(finder, &PackageFinder::packageFound); + + QThreadPool::globalInstance()->start(finder); + + spy.wait(10 * 1000); + QCOMPARE(spy.size(), 1); + + const auto items = spy.takeFirst().at(0).value>(); + // Total 2 packages in the directory, but one package is broken and should not be added to the list. + QCOMPARE(items.size(), 1); + QCOMPARE(items.at(0).filePath("preferred"), m_dataDir.absoluteFilePath(QStringLiteral("package/contents/images/1920x1080.jpg"))); +} + +QTEST_MAIN(PackageFinderTest) + +#include "test_packagefinder.moc" diff --git a/wallpapers/image/plugin/autotests/testdata/default/brokenpackage/contents/images/the folder is intended to leave blank.txt b/wallpapers/image/plugin/autotests/testdata/default/brokenpackage/contents/images/the folder is intended to leave blank.txt new file mode 100644 index 000000000..8d1c8b69c --- /dev/null +++ b/wallpapers/image/plugin/autotests/testdata/default/brokenpackage/contents/images/the folder is intended to leave blank.txt @@ -0,0 +1 @@ + diff --git a/wallpapers/image/plugin/autotests/testdata/default/brokenpackage/metadata.desktop b/wallpapers/image/plugin/autotests/testdata/default/brokenpackage/metadata.desktop new file mode 100644 index 000000000..b2ebfaadc --- /dev/null +++ b/wallpapers/image/plugin/autotests/testdata/default/brokenpackage/metadata.desktop @@ -0,0 +1,7 @@ +[Desktop Entry] +Name=Broken Wallpaper (For test purpose, don't translate!) + +X-KDE-PluginInfo-Name=Next +X-KDE-PluginInfo-Author=Ken Vermette +X-KDE-PluginInfo-Email=Ken Vermette +X-KDE-PluginInfo-License=CC-BY-SA-4.0