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 <QSignalSpy> 0008 #include <QStandardPaths> 0009 #include <QTest> 0010 0011 #include <KIO/CopyJob> 0012 #include <KIO/PreviewJob> 0013 0014 #include "../finder/mediametadatafinder.h" 0015 #include "../model/packagelistmodel.h" 0016 #include "commontestdata.h" 0017 0018 class PackageListModelTest : public QObject 0019 { 0020 Q_OBJECT 0021 0022 private Q_SLOTS: 0023 void initTestCase(); 0024 void init(); 0025 void cleanup(); 0026 void cleanupTestCase(); 0027 0028 void testPackageListModelData(); 0029 void testPackageListModelDarkWallpaperPreview(); 0030 void testPackageListModelIndexOf(); 0031 void testPackageListModelLoad(); 0032 void testPackageListModelAddBackground(); 0033 void testPackageListModelRemoveBackground(); 0034 void testPackageListModelRemoveLocalBackground(); 0035 0036 private: 0037 QPointer<PackageListModel> m_model = nullptr; 0038 QPointer<QSignalSpy> m_countSpy = nullptr; 0039 QPointer<QSignalSpy> m_dataSpy = nullptr; 0040 0041 QDir m_dataDir; 0042 QDir m_alternateDir; 0043 QStringList m_packagePaths; 0044 QString m_dummyPackagePath; 0045 QProperty<QSize> m_targetSize; 0046 QProperty<bool> m_usedInConfig{true}; 0047 }; 0048 0049 void PackageListModelTest::initTestCase() 0050 { 0051 qRegisterMetaType<MediaMetadata>(); 0052 0053 m_dataDir = QDir(QFINDTESTDATA("testdata/default")); 0054 m_alternateDir = QDir(QFINDTESTDATA("testdata/alternate")); 0055 QVERIFY(!m_dataDir.isEmpty()); 0056 QVERIFY(!m_alternateDir.isEmpty()); 0057 0058 m_packagePaths << m_dataDir.absoluteFilePath(ImageBackendTestData::defaultPackageFolderName1); 0059 m_packagePaths << m_dataDir.absoluteFilePath(ImageBackendTestData::defaultPackageFolderName2); 0060 m_dummyPackagePath = m_alternateDir.absoluteFilePath(ImageBackendTestData::alternatePackageFolderName1); 0061 0062 m_targetSize = QSize(1920, 1080); 0063 0064 QStandardPaths::setTestModeEnabled(true); 0065 } 0066 0067 void PackageListModelTest::init() 0068 { 0069 m_model = new PackageListModel(QBindable<QSize>(&m_targetSize), QBindable<bool>(&m_usedInConfig), this); 0070 m_countSpy = new QSignalSpy(m_model, &PackageListModel::countChanged); 0071 m_dataSpy = new QSignalSpy(m_model, &PackageListModel::dataChanged); 0072 0073 // Test loading data 0074 m_model->load({m_dataDir.absolutePath()}); 0075 m_countSpy->wait(10 * 1000); 0076 QCOMPARE(m_countSpy->size(), 1); 0077 m_countSpy->clear(); 0078 QCOMPARE(m_model->rowCount(), ImageBackendTestData::defaultPackageCount); 0079 } 0080 0081 void PackageListModelTest::cleanup() 0082 { 0083 m_model->deleteLater(); 0084 m_countSpy->deleteLater(); 0085 m_dataSpy->deleteLater(); 0086 } 0087 0088 void PackageListModelTest::cleanupTestCase() 0089 { 0090 const QString standardPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/wallpapers/"); 0091 0092 QDir(standardPath).removeRecursively(); 0093 } 0094 0095 void PackageListModelTest::testPackageListModelData() 0096 { 0097 QPersistentModelIndex idx = m_model->index(m_model->indexOf(m_packagePaths.at(1)), 0); 0098 QVERIFY(idx.isValid()); 0099 0100 QCOMPARE(idx.data(Qt::DisplayRole).toString(), QStringLiteral("Honeywave (For test purpose, don't translate!)")); 0101 0102 QCOMPARE(idx.data(ImageRoles::ScreenshotRole), QVariant()); // Not cached yet 0103 const QStringList availablePlugins = KIO::PreviewJob::availablePlugins(); 0104 if (availablePlugins.contains(QLatin1String("imagethumbnail"))) { 0105 m_dataSpy->wait(); 0106 QCOMPARE(m_dataSpy->size(), 1); 0107 QCOMPARE(m_dataSpy->takeFirst().at(2).value<QList<int>>().at(0), ImageRoles::ScreenshotRole); 0108 QVERIFY(!idx.data(ImageRoles::ScreenshotRole).value<QPixmap>().isNull()); 0109 } 0110 0111 QCOMPARE(idx.data(ImageRoles::AuthorRole).toString(), QStringLiteral("Ken Vermette")); 0112 0113 QCOMPARE(idx.data(ImageRoles::ResolutionRole).toString(), QString()); 0114 m_dataSpy->wait(); 0115 QCOMPARE(m_dataSpy->size(), 1); 0116 QCOMPARE(m_dataSpy->takeFirst().at(2).value<QList<int>>().at(0), ImageRoles::ResolutionRole); 0117 QCOMPARE(idx.data(ImageRoles::ResolutionRole).toString(), QStringLiteral("1920x1080")); 0118 0119 QCOMPARE(idx.data(ImageRoles::PathRole).toUrl().toLocalFile(), 0120 m_packagePaths.at(1) + QDir::separator() + QStringLiteral("contents") + QDir::separator() + QStringLiteral("images") + QDir::separator() 0121 + QStringLiteral("1920x1080.jpg")); 0122 QCOMPARE(idx.data(ImageRoles::PackageNameRole).toString(), m_packagePaths.at(1) + QDir::separator()); 0123 0124 QCOMPARE(idx.data(ImageRoles::RemovableRole).toBool(), false); 0125 QCOMPARE(idx.data(ImageRoles::PendingDeletionRole).toBool(), false); 0126 } 0127 0128 void PackageListModelTest::testPackageListModelDarkWallpaperPreview() 0129 { 0130 QModelIndex idx = m_model->index(m_model->indexOf(m_packagePaths.at(0)), 0); 0131 QVERIFY(idx.isValid()); 0132 0133 QCOMPARE(idx.data(Qt::DisplayRole).toString(), QStringLiteral("Dark Wallpaper (For test purpose, don't translate!)")); 0134 0135 QCOMPARE(idx.data(ImageRoles::ScreenshotRole), QVariant()); // Not cached yet 0136 const QStringList availablePlugins = KIO::PreviewJob::availablePlugins(); 0137 if (availablePlugins.contains(QLatin1String("imagethumbnail"))) { 0138 m_dataSpy->wait(); 0139 QCOMPARE(m_dataSpy->size(), 1); 0140 QCOMPARE(m_dataSpy->takeFirst().at(2).value<QList<int>>().at(0), ImageRoles::ScreenshotRole); 0141 m_dataSpy->clear(); 0142 0143 const auto preview = idx.data(ImageRoles::ScreenshotRole).value<QPixmap>(); 0144 QVERIFY(!preview.isNull()); 0145 const auto previewImage = preview.toImage(); 0146 QCOMPARE(previewImage.pixelColor({0, 0}), Qt::red); 0147 QCOMPARE(previewImage.pixelColor({previewImage.width() / 2, 0}), Qt::black); 0148 } 0149 } 0150 0151 void PackageListModelTest::testPackageListModelIndexOf() 0152 { 0153 QCOMPARE(m_model->indexOf(m_packagePaths.at(0)), 0); 0154 QCOMPARE(m_model->indexOf(m_packagePaths.at(1)), 1); 0155 QCOMPARE(m_model->indexOf(QUrl::fromLocalFile(m_packagePaths.at(0)).toString()), 0); 0156 QCOMPARE(m_model->indexOf(QUrl::fromLocalFile(m_packagePaths.at(1)).toString()), 1); 0157 QCOMPARE(m_model->indexOf(m_dataDir.absoluteFilePath(QStringLiteral("brokenpackage"))), -1); 0158 } 0159 0160 void PackageListModelTest::testPackageListModelLoad() 0161 { 0162 m_model->load({m_alternateDir.absolutePath()}); 0163 m_countSpy->wait(10 * 1000); 0164 QCOMPARE(m_countSpy->size(), 1); 0165 m_countSpy->clear(); 0166 QCOMPARE(m_model->rowCount(), 1); 0167 QCOMPARE(m_model->index(0, 0).data(Qt::DisplayRole), QStringLiteral("Dummy wallpaper (For test purpose, don't translate!)")); 0168 } 0169 0170 void PackageListModelTest::testPackageListModelAddBackground() 0171 { 0172 // Case 1: add a valid package 0173 QStringList results = m_model->addBackground(m_dummyPackagePath); 0174 QCOMPARE(m_countSpy->size(), 1); 0175 m_countSpy->clear(); 0176 QCOMPARE(results.size(), 1); 0177 QCOMPARE(results.at(0), m_dummyPackagePath + QDir::separator()); 0178 QCOMPARE(m_model->rowCount(), ImageBackendTestData::defaultPackageCount + 1); 0179 0180 QPersistentModelIndex idx = m_model->index(0, 0); // This is the newly added item. 0181 QVERIFY(idx.isValid()); 0182 0183 QCOMPARE(idx.data(Qt::DisplayRole).toString(), QStringLiteral("Dummy wallpaper (For test purpose, don't translate!)")); 0184 QCOMPARE(idx.data(ImageRoles::RemovableRole).toBool(), true); 0185 0186 // Case 2: add an existing package 0187 results = m_model->addBackground(m_dummyPackagePath); 0188 QCOMPARE(results.size(), 0); 0189 QCOMPARE(m_countSpy->size(), 0); 0190 0191 // Case 3: add an image 0192 results = m_model->addBackground(m_alternateDir.absoluteFilePath(ImageBackendTestData::alternateImageFileName1)); 0193 QCOMPARE(results.size(), 0); 0194 QCOMPARE(m_countSpy->size(), 0); 0195 0196 // Case 4: path is empty 0197 results = m_model->addBackground(QString()); 0198 QCOMPARE(results.size(), 0); 0199 QCOMPARE(m_countSpy->size(), 0); 0200 0201 // Case 5: add a broken package 0202 results = m_model->addBackground(m_dataDir.absoluteFilePath(QStringLiteral("brokenpackage"))); 0203 QCOMPARE(results.size(), 0); 0204 QCOMPARE(m_countSpy->size(), 0); 0205 0206 // Test PendingDeletionRole 0207 QVERIFY(m_model->setData(idx, true, ImageRoles::PendingDeletionRole)); 0208 QCOMPARE(m_dataSpy->size(), 1); 0209 QCOMPARE(m_dataSpy->takeFirst().at(2).value<QList<int>>().at(0), ImageRoles::PendingDeletionRole); 0210 QCOMPARE(idx.data(ImageRoles::PendingDeletionRole).toBool(), true); 0211 0212 // Case 7: Add a package when usedInConfig: false 0213 results = m_model->removeBackground(m_dummyPackagePath); 0214 QCOMPARE(m_countSpy->size(), 1); 0215 m_countSpy->clear(); 0216 QCOMPARE(results.size(), 1); 0217 0218 m_usedInConfig = false; 0219 0220 results = m_model->addBackground(m_dummyPackagePath); 0221 idx = m_model->index(m_model->rowCount() - 1, 0); // This is the newly added item. 0222 QVERIFY(idx.isValid()); 0223 QCOMPARE(idx.data(Qt::DisplayRole).toString(), QStringLiteral("Dummy wallpaper (For test purpose, don't translate!)")); 0224 QCOMPARE(idx.data(ImageRoles::RemovableRole).toBool(), true); 0225 0226 m_usedInConfig = true; 0227 } 0228 0229 void PackageListModelTest::testPackageListModelRemoveBackground() 0230 { 0231 m_model->addBackground(m_dummyPackagePath); 0232 m_countSpy->clear(); 0233 QCOMPARE(m_model->m_removableWallpapers.size(), 1); 0234 QCOMPARE(m_model->m_removableWallpapers.at(0), m_dummyPackagePath + QDir::separator()); 0235 0236 QStringList results; 0237 0238 // Case 1: path is empty 0239 results = m_model->removeBackground(QString()); 0240 QCOMPARE(m_countSpy->size(), 0); 0241 QCOMPARE(results.size(), 0); 0242 QCOMPARE(m_model->m_removableWallpapers.size(), 1); 0243 0244 // Case 2: remove a not added package 0245 results = m_model->removeBackground(m_alternateDir.absoluteFilePath(QStringLiteral("thispackagedoesnotexist"))); 0246 QCOMPARE(m_countSpy->size(), 0); 0247 QCOMPARE(results.size(), 0); 0248 QCOMPARE(m_model->m_removableWallpapers.size(), 1); 0249 0250 // Case 3: remove an existing image 0251 results = m_model->removeBackground(m_dummyPackagePath); 0252 QCOMPARE(m_countSpy->size(), 1); 0253 m_countSpy->clear(); 0254 QCOMPARE(m_model->rowCount(), ImageBackendTestData::defaultPackageCount); 0255 QCOMPARE(results.size(), 1); 0256 QCOMPARE(results.at(0), m_dummyPackagePath + QDir::separator()); 0257 QCOMPARE(m_model->m_removableWallpapers.size(), 0); 0258 } 0259 0260 void PackageListModelTest::testPackageListModelRemoveLocalBackground() 0261 { 0262 const QString standardPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/wallpapers/"); 0263 0264 QVERIFY(QDir(standardPath).mkpath(standardPath)); 0265 KIO::CopyJob *job = KIO::copy(QUrl::fromLocalFile(m_dummyPackagePath), 0266 QUrl::fromLocalFile(standardPath + QStringLiteral("dummy") + QDir::separator()), 0267 KIO::HideProgressInfo | KIO::Overwrite); 0268 QVERIFY(job->exec()); 0269 0270 m_model->load({standardPath}); 0271 QVERIFY(m_countSpy->wait(10 * 1000)); 0272 m_countSpy->clear(); 0273 0274 QPersistentModelIndex idx = m_model->index(0, 0); 0275 QCOMPARE(idx.data(Qt::DisplayRole).toString(), QStringLiteral("Dummy wallpaper (For test purpose, don't translate!)")); 0276 QCOMPARE(idx.data(ImageRoles::RemovableRole).toBool(), true); 0277 0278 m_model->removeBackground(standardPath + QStringLiteral("dummy")); 0279 QCOMPARE(m_countSpy->size(), 1); 0280 m_countSpy->clear(); 0281 0282 // The local package should be deleted. 0283 QVERIFY(!QFileInfo::exists(standardPath + QStringLiteral("dummy"))); 0284 QVERIFY(QDir(standardPath).rmdir(standardPath)); 0285 } 0286 0287 QTEST_MAIN(PackageListModelTest) 0288 0289 #include "test_packagelistmodel.moc"