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/PreviewJob> 0012 0013 #include "../finder/mediametadatafinder.h" 0014 #include "../model/imagelistmodel.h" 0015 #include "commontestdata.h" 0016 #include "config-KExiv2.h" 0017 0018 class ImageListModelTest : 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 testImageListModelData(); 0029 void testImageListModelIndexOf(); 0030 void testImageListModelLoad(); 0031 void testImageListModelAddBackground(); 0032 void testImageListModelRemoveBackground(); 0033 void testImageListModelRemoveLocalBackground(); 0034 0035 private: 0036 QPointer<ImageListModel> m_model = nullptr; 0037 QPointer<QSignalSpy> m_countSpy = nullptr; 0038 QPointer<QSignalSpy> m_dataSpy = nullptr; 0039 0040 QDir m_dataDir; 0041 QDir m_alternateDir; 0042 QStringList m_wallpaperPaths; 0043 QString m_dummyWallpaperPath; 0044 QProperty<QSize> m_targetSize; 0045 QProperty<bool> m_usedInConfig{true}; 0046 }; 0047 0048 void ImageListModelTest::initTestCase() 0049 { 0050 qRegisterMetaType<MediaMetadata>(); 0051 0052 m_dataDir = QDir(QFINDTESTDATA("testdata/default")); 0053 m_alternateDir = QDir(QFINDTESTDATA("testdata/alternate")); 0054 QVERIFY(!m_dataDir.isEmpty()); 0055 QVERIFY(!m_alternateDir.isEmpty()); 0056 renameBizarreFile(m_dataDir); 0057 0058 m_wallpaperPaths << m_dataDir.absoluteFilePath(ImageBackendTestData::defaultImageFileName1); 0059 m_wallpaperPaths << m_dataDir.absoluteFilePath(ImageBackendTestData::defaultImageFileName2); 0060 m_wallpaperPaths << m_dataDir.absoluteFilePath(ImageBackendTestData::defaultImageFileName3); 0061 m_wallpaperPaths << m_dataDir.absoluteFilePath(ImageBackendTestData::defaultImageFileName4); 0062 m_wallpaperPaths << m_dataDir.absoluteFilePath(ImageBackendTestData::defaultImageFileName5); 0063 m_dummyWallpaperPath = m_alternateDir.absoluteFilePath(ImageBackendTestData::alternateImageFileName1); 0064 0065 m_targetSize = QSize(1920, 1080); 0066 0067 QStandardPaths::setTestModeEnabled(true); 0068 } 0069 0070 void ImageListModelTest::init() 0071 { 0072 m_model = new ImageListModel(QBindable<QSize>(&m_targetSize), QBindable<bool>(&m_usedInConfig), this); 0073 m_countSpy = new QSignalSpy(m_model, &ImageListModel::countChanged); 0074 m_dataSpy = new QSignalSpy(m_model, &ImageListModel::dataChanged); 0075 0076 // Test loading data 0077 m_model->load({m_dataDir.absolutePath()}); 0078 m_countSpy->wait(10 * 1000); 0079 QCOMPARE(m_countSpy->size(), 1); 0080 m_countSpy->clear(); 0081 QCOMPARE(m_model->rowCount(), ImageBackendTestData::defaultImageCount); 0082 } 0083 0084 void ImageListModelTest::cleanup() 0085 { 0086 m_model->deleteLater(); 0087 m_countSpy->deleteLater(); 0088 m_dataSpy->deleteLater(); 0089 } 0090 0091 void ImageListModelTest::cleanupTestCase() 0092 { 0093 const QString standardPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/wallpapers/"); 0094 0095 QDir(standardPath).removeRecursively(); 0096 0097 restoreBizarreFile(m_dataDir); 0098 } 0099 0100 void ImageListModelTest::testImageListModelData() 0101 { 0102 const int row = m_model->indexOf(m_wallpaperPaths.at(0)); 0103 QPersistentModelIndex idx = m_model->index(row, 0); 0104 QVERIFY(idx.isValid()); 0105 0106 // Should return the complete base name for wallpaper.jpg.jpg 0107 QCOMPARE(idx.data(Qt::DisplayRole).toString(), QStringLiteral("wallpaper.jpg")); 0108 m_dataSpy->wait(); 0109 #if HAVE_KExiv2 0110 m_dataSpy->wait(); 0111 m_dataSpy->wait(); 0112 QCOMPARE(m_dataSpy->size(), 3); 0113 QCOMPARE(m_dataSpy->takeFirst().at(2).value<QList<int>>().at(0), Qt::DisplayRole); 0114 QCOMPARE(idx.data(Qt::DisplayRole).toString(), QStringLiteral("DocumentName")); 0115 QCOMPARE(m_dataSpy->takeFirst().at(2).value<QList<int>>().at(0), ImageRoles::AuthorRole); 0116 #endif 0117 QCOMPARE(m_dataSpy->size(), 1); 0118 QCOMPARE(m_dataSpy->takeFirst().at(2).value<QList<int>>().at(0), ImageRoles::ResolutionRole); 0119 0120 QCOMPARE(idx.data(ImageRoles::ScreenshotRole), QVariant()); // Not cached yet 0121 const QStringList availablePlugins = KIO::PreviewJob::availablePlugins(); 0122 if (availablePlugins.contains(QLatin1String("imagethumbnail"))) { 0123 m_dataSpy->wait(); 0124 QCOMPARE(m_dataSpy->size(), 1); 0125 QCOMPARE(m_dataSpy->takeFirst().at(2).value<QList<int>>().at(0), ImageRoles::ScreenshotRole); 0126 QVERIFY(!idx.data(ImageRoles::ScreenshotRole).value<QPixmap>().isNull()); 0127 } 0128 0129 #if HAVE_KExiv2 0130 QCOMPARE(idx.data(ImageRoles::AuthorRole).toString(), QStringLiteral("KDE Community")); 0131 #else 0132 QCOMPARE(idx.data(ImageRoles::AuthorRole).toString(), QString()); 0133 #endif 0134 0135 QCOMPARE(idx.data(ImageRoles::ResolutionRole).toString(), QStringLiteral("15x16")); 0136 0137 QCOMPARE(idx.data(ImageRoles::PathRole).toUrl(), QUrl::fromLocalFile(m_wallpaperPaths.at(0))); 0138 QCOMPARE(idx.data(ImageRoles::PackageNameRole).toString(), m_wallpaperPaths.at(0)); 0139 0140 QCOMPARE(idx.data(ImageRoles::RemovableRole).toBool(), false); 0141 QCOMPARE(idx.data(ImageRoles::PendingDeletionRole).toBool(), false); 0142 } 0143 0144 void ImageListModelTest::testImageListModelIndexOf() 0145 { 0146 QTRY_VERIFY(m_model->indexOf(m_wallpaperPaths.at(0)) >= 0); 0147 QTRY_VERIFY(m_model->indexOf(m_wallpaperPaths.at(1)) >= 0); 0148 QTRY_VERIFY(m_model->indexOf(m_wallpaperPaths.at(2)) >= 0); 0149 QVERIFY(m_model->indexOf(m_wallpaperPaths.at(3)) >= 0); 0150 QVERIFY(m_model->indexOf(m_wallpaperPaths.at(4)) >= 0); 0151 QTRY_VERIFY(m_model->indexOf(QUrl::fromLocalFile(m_wallpaperPaths.at(0)).toString()) >= 0); 0152 QCOMPARE(m_model->indexOf(m_dataDir.absoluteFilePath(QStringLiteral(".wallpaper.jpg"))), -1); 0153 } 0154 0155 void ImageListModelTest::testImageListModelLoad() 0156 { 0157 m_model->load({m_alternateDir.absolutePath()}); 0158 m_countSpy->wait(10 * 1000); 0159 QCOMPARE(m_countSpy->size(), 1); 0160 m_countSpy->clear(); 0161 QCOMPARE(m_model->rowCount(), ImageBackendTestData::alternateImageCount); 0162 QCOMPARE(m_model->index(0, 0).data(Qt::DisplayRole), QStringLiteral("dummy")); 0163 } 0164 0165 void ImageListModelTest::testImageListModelAddBackground() 0166 { 0167 // Case 1: add a valid image 0168 QStringList results = m_model->addBackground(m_dummyWallpaperPath); 0169 QCOMPARE(m_countSpy->size(), 1); 0170 m_countSpy->clear(); 0171 QCOMPARE(results.size(), 1); 0172 QCOMPARE(results.at(0), m_dummyWallpaperPath); 0173 QCOMPARE(m_model->rowCount(), ImageBackendTestData::defaultImageCount + 1); 0174 0175 QPersistentModelIndex idx = m_model->index(0, 0); // This is the newly added item. 0176 QVERIFY(idx.isValid()); 0177 0178 QCOMPARE(idx.data(Qt::DisplayRole).toString(), QStringLiteral("dummy")); 0179 QCOMPARE(idx.data(ImageRoles::RemovableRole).toBool(), true); 0180 0181 // Case 2: add an existing image 0182 results = m_model->addBackground(m_dummyWallpaperPath); 0183 QCOMPARE(results.size(), 0); 0184 QCOMPARE(m_countSpy->size(), 0); 0185 0186 // Case 3: add an unexsting image 0187 results = m_model->addBackground(m_alternateDir.absoluteFilePath(QStringLiteral("thisimagedoesnotexist.jpg"))); 0188 QCOMPARE(results.size(), 0); 0189 QCOMPARE(m_countSpy->size(), 0); 0190 0191 // Case 4: add a hidden image 0192 QVERIFY(QFile::exists(m_dataDir.absoluteFilePath(QStringLiteral(".wallpaper.jpg")))); 0193 results = m_model->addBackground(m_dataDir.absoluteFilePath(QStringLiteral(".wallpaper.jpg"))); 0194 QCOMPARE(results.size(), 0); 0195 QCOMPARE(m_countSpy->size(), 0); 0196 0197 // Case 5: path is empty 0198 results = m_model->addBackground(QString()); 0199 QCOMPARE(results.size(), 0); 0200 QCOMPARE(m_countSpy->size(), 0); 0201 0202 // Case 6: add a non-image file 0203 results = m_model->addBackground(m_alternateDir.absoluteFilePath(QStringLiteral("thisisnotanimage.txt"))); 0204 QCOMPARE(results.size(), 0); 0205 QCOMPARE(m_countSpy->size(), 0); 0206 0207 // Test PendingDeletionRole 0208 QVERIFY(m_model->setData(idx, true, ImageRoles::PendingDeletionRole)); 0209 QCOMPARE(m_dataSpy->size(), 1); 0210 QCOMPARE(m_dataSpy->takeFirst().at(2).value<QList<int>>().at(0), ImageRoles::PendingDeletionRole); 0211 QCOMPARE(idx.data(ImageRoles::PendingDeletionRole).toBool(), true); 0212 0213 // Case 8: Add a package when usedInConfig: false 0214 results = m_model->removeBackground(m_dummyWallpaperPath); 0215 QCOMPARE(m_countSpy->size(), 1); 0216 m_countSpy->clear(); 0217 QCOMPARE(results.size(), 1); 0218 0219 m_usedInConfig = false; 0220 0221 results = m_model->addBackground(m_dummyWallpaperPath); 0222 idx = m_model->index(m_model->rowCount() - 1, 0); // This is the newly added item. 0223 QVERIFY(idx.isValid()); 0224 QCOMPARE(idx.data(Qt::DisplayRole).toString(), QStringLiteral("dummy")); 0225 QCOMPARE(idx.data(ImageRoles::RemovableRole).toBool(), true); 0226 0227 m_usedInConfig = true; 0228 } 0229 0230 void ImageListModelTest::testImageListModelRemoveBackground() 0231 { 0232 m_model->addBackground(m_dummyWallpaperPath); 0233 m_countSpy->clear(); 0234 QCOMPARE(m_model->m_removableWallpapers.size(), 1); 0235 QCOMPARE(m_model->m_removableWallpapers.at(0), m_dummyWallpaperPath); 0236 0237 QStringList results; 0238 0239 // Case 1: path is empty 0240 results = m_model->removeBackground(QString()); 0241 QCOMPARE(m_countSpy->size(), 0); 0242 QCOMPARE(results.size(), 0); 0243 QCOMPARE(m_model->m_removableWallpapers.size(), 1); 0244 0245 // Case 2: remove a not added image 0246 results = m_model->removeBackground(m_alternateDir.absoluteFilePath(QStringLiteral("thisimagedoesnotexist.jpg"))); 0247 QCOMPARE(m_countSpy->size(), 0); 0248 QCOMPARE(results.size(), 0); 0249 QCOMPARE(m_model->m_removableWallpapers.size(), 1); 0250 0251 // Case 3: remove an existing image 0252 results = m_model->removeBackground(m_dummyWallpaperPath); 0253 QCOMPARE(m_countSpy->size(), 1); 0254 m_countSpy->clear(); 0255 QCOMPARE(results.size(), 1); 0256 QCOMPARE(results.at(0), m_dummyWallpaperPath); 0257 QCOMPARE(m_model->rowCount(), ImageBackendTestData::defaultImageCount); 0258 QCOMPARE(m_model->m_removableWallpapers.size(), 0); 0259 } 0260 0261 void ImageListModelTest::testImageListModelRemoveLocalBackground() 0262 { 0263 const QString standardPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/wallpapers/"); 0264 QFile imageFile(m_wallpaperPaths.at(0)); 0265 0266 QVERIFY(QDir(standardPath).mkpath(standardPath)); 0267 QVERIFY(imageFile.copy(standardPath + QStringLiteral("wallpaper.jpg.jpg"))); 0268 0269 m_model->load({standardPath}); 0270 QVERIFY(m_countSpy->wait(10 * 1000)); 0271 m_countSpy->clear(); 0272 0273 QPersistentModelIndex idx = m_model->index(0, 0); 0274 QCOMPARE(idx.data(Qt::DisplayRole).toString(), QStringLiteral("wallpaper.jpg")); 0275 m_dataSpy->wait(); 0276 #if HAVE_KExiv2 0277 m_dataSpy->wait(); 0278 m_dataSpy->wait(); 0279 #endif 0280 m_dataSpy->clear(); 0281 QCOMPARE(idx.data(ImageRoles::RemovableRole).toBool(), true); 0282 0283 m_model->removeBackground(standardPath + QStringLiteral("wallpaper.jpg.jpg")); 0284 QCOMPARE(m_countSpy->size(), 1); 0285 m_countSpy->clear(); 0286 0287 // The local wallpaper should be deleted. 0288 QVERIFY(!QFile::exists(standardPath + QStringLiteral("wallpaper.jpg.jpg"))); 0289 QVERIFY(QDir(standardPath).rmdir(standardPath)); 0290 } 0291 0292 QTEST_MAIN(ImageListModelTest) 0293 0294 #include "test_imagelistmodel.moc"