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 <QDebug> 0008 #include <QSignalSpy> 0009 #include <QStandardPaths> 0010 #include <QTest> 0011 0012 #include "../model/imageproxymodel.h" 0013 #include "../slidemodel.h" 0014 #include "commontestdata.h" 0015 0016 class SlideModelTest : public QObject 0017 { 0018 Q_OBJECT 0019 0020 private Q_SLOTS: 0021 void initTestCase(); 0022 void init(); 0023 void cleanup(); 0024 void cleanupTestCase(); 0025 0026 void testSlideModelData(); 0027 void testSlideModelIndexOf(); 0028 void testSlideModelAddDirs(); 0029 void testSlideModelRemoveDir(); 0030 void testSlideModelSetSlidePaths(); 0031 void testSlideModelSetUncheckedSlides(); 0032 0033 private: 0034 QPointer<SlideModel> m_model = nullptr; 0035 QPointer<QSignalSpy> m_doneSpy = nullptr; 0036 QPointer<QSignalSpy> m_dataSpy = nullptr; 0037 0038 QDir m_dataDir; 0039 QDir m_alternateDir; 0040 QStringList m_wallpaperPaths; 0041 QString m_dummyWallpaperPath; 0042 QStringList m_packagePaths; 0043 QString m_dummyPackagePath; 0044 QProperty<QSize> m_targetSize; 0045 QProperty<bool> m_usedInConfig{false}; 0046 }; 0047 0048 void SlideModelTest::initTestCase() 0049 { 0050 m_dataDir = QDir(QFINDTESTDATA("testdata/default")); 0051 m_alternateDir = QDir(QFINDTESTDATA("testdata/alternate")); 0052 QVERIFY(!m_dataDir.isEmpty()); 0053 QVERIFY(!m_alternateDir.isEmpty()); 0054 renameBizarreFile(m_dataDir); 0055 0056 m_wallpaperPaths << m_dataDir.absoluteFilePath(ImageBackendTestData::defaultImageFileName1); 0057 m_wallpaperPaths << m_dataDir.absoluteFilePath(ImageBackendTestData::defaultImageFileName2); 0058 m_wallpaperPaths << m_dataDir.absoluteFilePath(ImageBackendTestData::defaultImageFileName3); 0059 m_wallpaperPaths << m_dataDir.absoluteFilePath(ImageBackendTestData::defaultImageFileName4); 0060 m_wallpaperPaths << m_dataDir.absoluteFilePath(ImageBackendTestData::defaultImageFileName5); 0061 m_dummyWallpaperPath = m_alternateDir.absoluteFilePath(QStringLiteral("dummy.jpg")); 0062 0063 m_packagePaths << m_dataDir.absoluteFilePath(ImageBackendTestData::defaultPackageFolderName1); 0064 m_packagePaths << m_dataDir.absoluteFilePath(ImageBackendTestData::defaultPackageFolderName2); 0065 m_dummyPackagePath = m_alternateDir.absoluteFilePath(QStringLiteral("dummy")); 0066 0067 m_targetSize = QSize(1920, 1080); 0068 0069 QStandardPaths::setTestModeEnabled(true); 0070 } 0071 0072 void SlideModelTest::init() 0073 { 0074 m_model = new SlideModel(QBindable<QSize>(&m_targetSize), QBindable<bool>(&m_usedInConfig), this); 0075 m_doneSpy = new QSignalSpy(m_model, &SlideModel::done); 0076 m_dataSpy = new QSignalSpy(m_model, &SlideModel::dataChanged); 0077 0078 // Test loading data 0079 m_model->setSlidePaths({m_dataDir.absolutePath()}); 0080 0081 QVERIFY(m_doneSpy->wait(10 * 1000)); 0082 QCOMPARE(m_doneSpy->size(), 1); 0083 m_doneSpy->clear(); 0084 QCOMPARE(m_model->sourceModels().size(), 1); 0085 QCOMPARE(m_model->m_models.size(), 1); 0086 QCOMPARE(m_model->rowCount(), ImageBackendTestData::defaultTotalCount); 0087 0088 QVERIFY(m_model->m_models.contains(m_dataDir.absolutePath() + QDir::separator())); 0089 QCOMPARE(m_model->m_models.value(m_dataDir.absolutePath() + QDir::separator())->count(), ImageBackendTestData::defaultTotalCount); 0090 } 0091 0092 void SlideModelTest::cleanup() 0093 { 0094 m_model->deleteLater(); 0095 m_doneSpy->deleteLater(); 0096 m_dataSpy->deleteLater(); 0097 } 0098 0099 void SlideModelTest::cleanupTestCase() 0100 { 0101 const QString standardPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/wallpapers/"); 0102 0103 QDir(standardPath).removeRecursively(); 0104 0105 restoreBizarreFile(m_dataDir); 0106 } 0107 0108 void SlideModelTest::testSlideModelData() 0109 { 0110 const int row = m_model->indexOf(m_packagePaths.at(1)); 0111 QVERIFY(row >= 0); 0112 const QPersistentModelIndex idx = m_model->index(row, 0); 0113 0114 QCOMPARE(idx.data(ImageRoles::ToggleRole).toBool(), true); 0115 QCOMPARE(idx.data(ImageRoles::AuthorRole).toString(), QStringLiteral("Ken Vermette")); 0116 QCOMPARE(idx.data(ImageRoles::RemovableRole).toBool(), false); 0117 // Other roles are tested in ImageProxyModelTest 0118 } 0119 0120 void SlideModelTest::testSlideModelIndexOf() 0121 { 0122 QVERIFY(m_model->indexOf(m_wallpaperPaths.at(0)) >= 0); 0123 QVERIFY(m_model->indexOf(m_wallpaperPaths.at(1)) >= 0); 0124 QVERIFY(m_model->indexOf(m_wallpaperPaths.at(2)) >= 0); 0125 QVERIFY(m_model->indexOf(m_wallpaperPaths.at(3)) >= 0); 0126 QVERIFY(m_model->indexOf(m_wallpaperPaths.at(4)) >= 0); 0127 QVERIFY(m_model->indexOf(m_packagePaths.at(0)) >= 0); 0128 QVERIFY(m_model->indexOf(m_packagePaths.at(1)) >= 0); 0129 QVERIFY(m_model->indexOf(m_packagePaths.at(0) + QDir::separator()) >= 0); 0130 QVERIFY(m_model->indexOf(m_packagePaths.at(1) + QDir::separator()) >= 0); 0131 QCOMPARE(m_model->indexOf(m_dummyWallpaperPath), -1); 0132 QCOMPARE(m_model->indexOf(m_dummyPackagePath + QDir::separator()), -1); 0133 } 0134 0135 void SlideModelTest::testSlideModelAddDirs() 0136 { 0137 int count = m_model->rowCount(); 0138 0139 // Case 1: add an image file 0140 auto results = m_model->addDirs({m_wallpaperPaths.at(0)}); 0141 QCOMPARE(m_model->m_models.size(), 1); 0142 QCOMPARE(results.size(), 0); 0143 0144 // Case 2: add an existing folder 0145 results = m_model->addDirs({m_dataDir.absolutePath()}); 0146 QCOMPARE(m_model->m_models.size(), 1); 0147 QCOMPARE(results.size(), 0); 0148 0149 // Case 3: add a new folder 0150 results = m_model->addDirs({m_alternateDir.absolutePath()}); 0151 QCOMPARE(results.size(), 1); 0152 QCOMPARE(m_model->m_models.size(), 2); 0153 QVERIFY(m_doneSpy->wait()); 0154 QCOMPARE(m_doneSpy->size(), 1); 0155 m_doneSpy->clear(); 0156 QCOMPARE(m_model->rowCount(), count + ImageBackendTestData::alternateTotalCount); 0157 } 0158 0159 void SlideModelTest::testSlideModelRemoveDir() 0160 { 0161 // Case 1: remove a not added dir 0162 m_model->removeDir(m_alternateDir.absolutePath()); 0163 QCOMPARE(m_model->m_models.size(), 1); 0164 QCOMPARE(m_model->m_loaded, 1); 0165 QCOMPARE(m_model->rowCount(), ImageBackendTestData::defaultTotalCount); 0166 0167 // Case 2: remove an existing dir 0168 m_model->removeDir(m_dataDir.absolutePath()); 0169 QCOMPARE(m_model->m_models.size(), 0); 0170 QCOMPARE(m_model->m_loaded, 0); 0171 QCOMPARE(m_model->rowCount(), 0); 0172 } 0173 0174 void SlideModelTest::testSlideModelSetSlidePaths() 0175 { 0176 // Case 1: list is empty 0177 m_model->setSlidePaths({}); 0178 QCOMPARE(m_model->m_models.size(), 0); 0179 QCOMPARE(m_model->m_loaded, 0); 0180 0181 // Case 2: path is invalid 0182 m_model->setSlidePaths({QString()}); 0183 QCOMPARE(m_model->m_models.size(), 0); 0184 QCOMPARE(m_model->m_loaded, 0); 0185 0186 // Case 3: path is valid 0187 m_model->setSlidePaths({m_alternateDir.absolutePath()}); 0188 QCOMPARE(m_model->m_models.size(), 1); 0189 QVERIFY(m_doneSpy->wait(10 * 1000)); 0190 QCOMPARE(m_doneSpy->size(), 1); 0191 m_doneSpy->clear(); 0192 QCOMPARE(m_model->sourceModels().size(), 1); 0193 QCOMPARE(m_model->m_models.size(), 1); 0194 QCOMPARE(m_model->rowCount(), ImageBackendTestData::alternateTotalCount); 0195 } 0196 0197 void SlideModelTest::testSlideModelSetUncheckedSlides() 0198 { 0199 QPersistentModelIndex idx = m_model->index(0, 0); 0200 0201 QVERIFY(m_model->setData(idx, false, ImageRoles::ToggleRole)); 0202 QCOMPARE(idx.data(ImageRoles::ToggleRole), false); 0203 0204 QVERIFY(m_model->setData(idx, true, ImageRoles::ToggleRole)); 0205 QCOMPARE(idx.data(ImageRoles::ToggleRole), true); 0206 } 0207 0208 QTEST_MAIN(SlideModelTest) 0209 0210 #include "test_slidemodel.moc"