diff --git a/wallpapers/image/plugin/autotests/CMakeLists.txt b/wallpapers/image/plugin/autotests/CMakeLists.txt index 6faf2953c..c0ba1c600 100644 --- a/wallpapers/image/plugin/autotests/CMakeLists.txt +++ b/wallpapers/image/plugin/autotests/CMakeLists.txt @@ -12,3 +12,7 @@ ecm_add_test(test_imagefinder.cpp TEST_NAME testimagefinder # PackageFinder test ecm_add_test(test_packagefinder.cpp TEST_NAME testpackageimagefinder LINK_LIBRARIES Qt::Test plasma_wallpaper_imageplugin_static) + +# ImageListModel test +ecm_add_test(test_imagelistmodel.cpp TEST_NAME testimagelistmodel + LINK_LIBRARIES Qt::Test plasma_wallpaper_imageplugin_static) diff --git a/wallpapers/image/plugin/autotests/test_imagelistmodel.cpp b/wallpapers/image/plugin/autotests/test_imagelistmodel.cpp new file mode 100644 index 000000000..a0749a68e --- /dev/null +++ b/wallpapers/image/plugin/autotests/test_imagelistmodel.cpp @@ -0,0 +1,240 @@ +/* + SPDX-FileCopyrightText: 2022 Fushan Wen + + SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include + +#include + +#include "../model/imagelistmodel.h" + +class ImageListModelTest : public QObject +{ + Q_OBJECT + +private Q_SLOTS: + void initTestCase(); + void init(); + void cleanup(); + void cleanupTestCase(); + + void testImageListModelData(); + void testImageListModelIndexOf(); + void testImageListModelLoad(); + void testImageListModelAddBackground(); + void testImageListModelRemoveBackground(); + void testImageListModelRemoveLocalBackground(); + +private: + QPointer m_model = nullptr; + QPointer m_countSpy = nullptr; + QPointer m_dataSpy = nullptr; + + QDir m_dataDir; + QDir m_alternateDir; + QString m_wallpaperPath; + QString m_dummyWallpaperPath; + QSize m_targetSize; +}; + +void ImageListModelTest::initTestCase() +{ + m_dataDir = QDir(QFINDTESTDATA("testdata/default")); + m_alternateDir = QDir(QFINDTESTDATA("testdata/alternate")); + QVERIFY(!m_dataDir.isEmpty()); + QVERIFY(!m_alternateDir.isEmpty()); + + m_wallpaperPath = m_dataDir.absoluteFilePath(QStringLiteral("wallpaper.jpg.jpg")); + m_dummyWallpaperPath = m_alternateDir.absoluteFilePath(QStringLiteral("dummy.jpg")); + + m_targetSize = QSize(1920, 1080); + + QStandardPaths::setTestModeEnabled(true); +} + +void ImageListModelTest::init() +{ + m_model = new ImageListModel(m_targetSize, this); + m_countSpy = new QSignalSpy(m_model, &ImageListModel::countChanged); + m_dataSpy = new QSignalSpy(m_model, &ImageListModel::dataChanged); + + // Test loading data + m_model->load({m_dataDir.absolutePath()}); + m_countSpy->wait(10 * 1000); + QCOMPARE(m_countSpy->size(), 1); + m_countSpy->clear(); + QCOMPARE(m_model->rowCount(), 1); +} + +void ImageListModelTest::cleanup() +{ + m_model->deleteLater(); + m_countSpy->deleteLater(); + m_dataSpy->deleteLater(); +} + +void ImageListModelTest::cleanupTestCase() +{ + const QString standardPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/wallpapers/"); + + QDir(standardPath).removeRecursively(); +} + +void ImageListModelTest::testImageListModelData() +{ + QPersistentModelIndex idx = m_model->index(0, 0); + QVERIFY(idx.isValid()); + + // Should return the complete base name for wallpaper.jpg.jpg + QCOMPARE(idx.data(Qt::DisplayRole).toString(), QStringLiteral("wallpaper.jpg")); + + QCOMPARE(idx.data(ImageRoles::ScreenshotRole), QVariant()); // Not cached yet + if (!KIO::PreviewJob::availablePlugins().empty()) { + m_dataSpy->wait(); + QCOMPARE(m_dataSpy->size(), 1); + QCOMPARE(m_dataSpy->takeFirst().at(2).value>().at(0), ImageRoles::ScreenshotRole); + QVERIFY(!idx.data(ImageRoles::ScreenshotRole).value().isNull()); + } + + QCOMPARE(idx.data(ImageRoles::AuthorRole).toString(), QString()); + + QCOMPARE(idx.data(ImageRoles::ResolutionRole).toString(), QString()); + m_dataSpy->wait(); + QCOMPARE(m_dataSpy->size(), 1); + QCOMPARE(m_dataSpy->takeFirst().at(2).value>().at(0), ImageRoles::ResolutionRole); + QCOMPARE(idx.data(ImageRoles::ResolutionRole).toString(), QStringLiteral("15x16")); + + QCOMPARE(idx.data(ImageRoles::PathRole).toUrl(), QUrl::fromLocalFile(m_wallpaperPath)); + QCOMPARE(idx.data(ImageRoles::PackageNameRole).toString(), m_wallpaperPath); + + QCOMPARE(idx.data(ImageRoles::RemovableRole).toBool(), false); + QCOMPARE(idx.data(ImageRoles::PendingDeletionRole).toBool(), false); +} + +void ImageListModelTest::testImageListModelIndexOf() +{ + QCOMPARE(m_model->indexOf(m_wallpaperPath), 0); + QCOMPARE(m_model->indexOf(QUrl::fromLocalFile(m_wallpaperPath).toString()), 0); + QCOMPARE(m_model->indexOf(m_dataDir.absoluteFilePath(QStringLiteral(".wallpaper.jpg"))), -1); +} + +void ImageListModelTest::testImageListModelLoad() +{ + m_model->load({m_alternateDir.absolutePath()}); + m_countSpy->wait(10 * 1000); + QCOMPARE(m_countSpy->size(), 1); + m_countSpy->clear(); + QCOMPARE(m_model->rowCount(), 1); + QCOMPARE(m_model->index(0, 0).data(Qt::DisplayRole), QStringLiteral("dummy")); +} + +void ImageListModelTest::testImageListModelAddBackground() +{ + // Case 1: add a valid image + QStringList results = m_model->addBackground(m_dummyWallpaperPath); + QCOMPARE(m_countSpy->size(), 1); + m_countSpy->clear(); + QCOMPARE(results.size(), 1); + QCOMPARE(results.at(0), m_dummyWallpaperPath); + QCOMPARE(m_model->rowCount(), 2); + + QPersistentModelIndex idx = m_model->index(0, 0); // This is the newly added item. + QVERIFY(idx.isValid()); + + QCOMPARE(idx.data(Qt::DisplayRole).toString(), QStringLiteral("dummy")); + QCOMPARE(idx.data(ImageRoles::RemovableRole).toBool(), true); + + // Case 2: add an existing image + results = m_model->addBackground(m_dummyWallpaperPath); + QCOMPARE(results.size(), 0); + QCOMPARE(m_countSpy->size(), 0); + + // Case 3: add an unexsting image + results = m_model->addBackground(m_alternateDir.absoluteFilePath(QStringLiteral("thisimagedoesnotexist.jpg"))); + QCOMPARE(results.size(), 0); + QCOMPARE(m_countSpy->size(), 0); + + // Case 4: add a hidden image + QVERIFY(QFile::exists(m_dataDir.absoluteFilePath(QStringLiteral(".wallpaper.jpg")))); + results = m_model->addBackground(m_dataDir.absoluteFilePath(QStringLiteral(".wallpaper.jpg"))); + QCOMPARE(results.size(), 0); + QCOMPARE(m_countSpy->size(), 0); + + // Case 5: path is empty + results = m_model->addBackground(QString()); + QCOMPARE(results.size(), 0); + QCOMPARE(m_countSpy->size(), 0); + + // Case 6: add a non-image file + results = m_model->addBackground(m_alternateDir.absoluteFilePath(QStringLiteral("thisisnotanimage.txt"))); + QCOMPARE(results.size(), 0); + QCOMPARE(m_countSpy->size(), 0); + + // Test PendingDeletionRole + QVERIFY(m_model->setData(idx, true, ImageRoles::PendingDeletionRole)); + QCOMPARE(m_dataSpy->size(), 1); + QCOMPARE(m_dataSpy->takeFirst().at(2).value>().at(0), ImageRoles::PendingDeletionRole); + QCOMPARE(idx.data(ImageRoles::PendingDeletionRole).toBool(), true); +} + +void ImageListModelTest::testImageListModelRemoveBackground() +{ + m_model->addBackground(m_dummyWallpaperPath); + m_countSpy->clear(); + QCOMPARE(m_model->m_removableWallpapers.size(), 1); + QCOMPARE(m_model->m_removableWallpapers.at(0), m_dummyWallpaperPath); + + QStringList results; + + // Case 1: path is empty + results = m_model->removeBackground(QString()); + QCOMPARE(m_countSpy->size(), 0); + QCOMPARE(results.size(), 0); + QCOMPARE(m_model->m_removableWallpapers.size(), 1); + + // Case 2: remove a not added image + results = m_model->removeBackground(m_alternateDir.absoluteFilePath(QStringLiteral("thisimagedoesnotexist.jpg"))); + QCOMPARE(m_countSpy->size(), 0); + QCOMPARE(results.size(), 0); + QCOMPARE(m_model->m_removableWallpapers.size(), 1); + + // Case 3: remove an existing image + results = m_model->removeBackground(m_dummyWallpaperPath); + QCOMPARE(m_countSpy->size(), 1); + m_countSpy->clear(); + QCOMPARE(results.size(), 1); + QCOMPARE(results.at(0), m_dummyWallpaperPath); + QCOMPARE(m_model->rowCount(), 1); + QCOMPARE(m_model->m_removableWallpapers.size(), 0); +} + +void ImageListModelTest::testImageListModelRemoveLocalBackground() +{ + const QString standardPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/wallpapers/"); + QFile imageFile(m_wallpaperPath); + + QVERIFY(QDir(standardPath).mkpath(standardPath)); + QVERIFY(imageFile.copy(standardPath + QStringLiteral("wallpaper.jpg.jpg"))); + + m_model->load({standardPath}); + QVERIFY(m_countSpy->wait(10 * 1000)); + m_countSpy->clear(); + + QPersistentModelIndex idx = m_model->index(0, 0); + QCOMPARE(idx.data(Qt::DisplayRole).toString(), QStringLiteral("wallpaper.jpg")); + QCOMPARE(idx.data(ImageRoles::RemovableRole).toBool(), true); + + m_model->removeBackground(standardPath + QStringLiteral("wallpaper.jpg.jpg")); + QCOMPARE(m_countSpy->size(), 1); + m_countSpy->clear(); + + // The local wallpaper should be deleted. + QVERIFY(!QFile::exists(standardPath + QStringLiteral("wallpaper.jpg.jpg"))); + QVERIFY(QDir(standardPath).rmdir(standardPath)); +} + +QTEST_MAIN(ImageListModelTest) + +#include "test_imagelistmodel.moc" diff --git a/wallpapers/image/plugin/autotests/testdata/alternate/dummy.jpg b/wallpapers/image/plugin/autotests/testdata/alternate/dummy.jpg new file mode 100644 index 000000000..b8217aeba Binary files /dev/null and b/wallpapers/image/plugin/autotests/testdata/alternate/dummy.jpg differ diff --git a/wallpapers/image/plugin/autotests/testdata/alternate/dummy/contents/images/1280x1024.jpg b/wallpapers/image/plugin/autotests/testdata/alternate/dummy/contents/images/1280x1024.jpg new file mode 100644 index 000000000..b8217aeba Binary files /dev/null and b/wallpapers/image/plugin/autotests/testdata/alternate/dummy/contents/images/1280x1024.jpg differ diff --git a/wallpapers/image/plugin/autotests/testdata/alternate/dummy/metadata.desktop b/wallpapers/image/plugin/autotests/testdata/alternate/dummy/metadata.desktop new file mode 100644 index 000000000..833479832 --- /dev/null +++ b/wallpapers/image/plugin/autotests/testdata/alternate/dummy/metadata.desktop @@ -0,0 +1,9 @@ +[Desktop Entry] +Name=Dummy wallpaper (For test purpose, don't translate!) + +X-KDE-PluginInfo-Name=Azul-openSUSE +X-KDE-PluginInfo-Author=openSUSE Artwork Team +X-KDE-PluginInfo-Email=opensuse-artwork@opensuse.org +X-KDE-PluginInfo-License=GPLv2 + + diff --git a/wallpapers/image/plugin/autotests/testdata/alternate/thisisnotanimage.txt b/wallpapers/image/plugin/autotests/testdata/alternate/thisisnotanimage.txt new file mode 100644 index 000000000..8d1c8b69c --- /dev/null +++ b/wallpapers/image/plugin/autotests/testdata/alternate/thisisnotanimage.txt @@ -0,0 +1 @@ +