File indexing completed on 2024-05-05 05:00:10
0001 /* 0002 SPDX-FileCopyrightText: 2009 David Faure <faure@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0005 */ 0006 0007 #include <kdesktopfile.h> 0008 #include <kconfiggroup.h> 0009 #include <module_manager.h> 0010 #include <QTest> 0011 #include <QStandardPaths> 0012 #include <KSharedConfig> 0013 0014 class ModuleManagerTest : public QObject 0015 { 0016 Q_OBJECT 0017 0018 private Q_SLOTS: 0019 void initTestCase(); 0020 void cleanupTestCase(); 0021 void testListModules(); 0022 void testAddLocalModule(); 0023 void testRenameGlobalModule(); 0024 void testRemoveLocalModule(); 0025 void testRemoveGlobalModule(); 0026 void testReAddGlobalModule(); 0027 void testRollback(); 0028 void testAvailablePlugins(); 0029 0030 private: 0031 ModuleManager *m_moduleManager; 0032 ModuleManager *m_moduleManager2; 0033 QString m_profile; 0034 QString m_profile2; 0035 KConfigGroup *m_configGroup; 0036 KConfigGroup *m_configGroup2; 0037 0038 int m_realModules; 0039 QString m_globalDir; 0040 }; 0041 0042 0043 QTEST_GUILESS_MAIN(ModuleManagerTest) 0044 0045 void ModuleManagerTest::initTestCase() 0046 { 0047 QStandardPaths::setTestModeEnabled(true); 0048 0049 const QString configFile = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QLatin1Char('/') + "konqsidebartngrc"; 0050 QFile::remove(configFile); 0051 KSharedConfig::Ptr config = KSharedConfig::openConfig("konqsidebartngrc"); 0052 0053 m_profile = "test_profile"; 0054 m_configGroup = new KConfigGroup(config, m_profile); 0055 m_moduleManager = new ModuleManager(m_configGroup); 0056 0057 m_profile2 = "other_profile"; 0058 m_configGroup2 = new KConfigGroup(config, m_profile2); 0059 m_moduleManager2 = new ModuleManager(m_configGroup2); 0060 0061 m_realModules = m_moduleManager->modules().count(); 0062 //m_realModules = 0; // because of our modified "global dir", the real ones are not visible anymore. 0063 0064 // This is needed because, in ModuleManager::modules(): 0065 // "We only list the most-global dir. Other levels use AddedModules." 0066 m_configGroup->writeEntry("AddedModules", QStringList() << "testModule.desktop"); 0067 m_configGroup2->writeEntry("AddedModules", QStringList() << "testModule.desktop"); 0068 0069 // Create a "global" dir for the (fake) pre-installed modules, 0070 // which isn't really global of course, but we can register it as such... 0071 m_globalDir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1Char('/') + "konqsidebartng/entries/"; 0072 QVERIFY(QDir().mkpath(m_globalDir)); 0073 QFile::remove(m_globalDir + "testModule.desktop"); 0074 0075 // Create a fake pre-installed plugin there. 0076 KDesktopFile testModule(m_globalDir + "testModule.desktop"); 0077 KConfigGroup scf = testModule.desktopGroup(); 0078 scf.writeEntry("Type", "Link"); 0079 scf.writePathEntry("URL", "http://www.kde.org"); 0080 scf.writeEntry("Icon", "internet-web-browser"); 0081 scf.writeEntry("Name", QString::fromLatin1("SideBar Test Plugin")); 0082 scf.writeEntry("X-KDE-KonqSidebarModule", "konqsidebar_web"); 0083 scf.sync(); 0084 QVERIFY(QFile::exists(m_globalDir + "testModule.desktop")); 0085 } 0086 0087 void ModuleManagerTest::cleanupTestCase() 0088 { 0089 delete m_moduleManager; 0090 delete m_moduleManager2; 0091 delete m_configGroup; 0092 delete m_configGroup2; 0093 QFile::remove(m_globalDir + "testModule.desktop"); 0094 QFile::remove(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1Char('/') + "konqsidebartng/entries/testModule.desktop"); 0095 } 0096 0097 // This test checks the initial state of things. 0098 // It should remain first. It's called again after the "rollback to defaults". 0099 void ModuleManagerTest::testListModules() 0100 { 0101 const QStringList modules = m_moduleManager->modules(); 0102 qDebug() << modules; 0103 QCOMPARE(modules.count(), m_realModules + 1); 0104 QVERIFY(modules.contains("testModule.desktop")); 0105 QVERIFY(m_moduleManager->moduleDataPath("testModule.desktop").endsWith("/testModule.desktop")); 0106 const KDesktopFile df(QStandardPaths::GenericDataLocation, m_moduleManager->moduleDataPath("testModule.desktop")); 0107 QCOMPARE(df.readName(), QString::fromLatin1("SideBar Test Plugin")); 0108 0109 const QStringList modules2 = m_moduleManager2->modules(); 0110 QCOMPARE(modules2.count(), m_realModules + 1); 0111 QVERIFY(modules2.contains("testModule.desktop")); 0112 } 0113 0114 void ModuleManagerTest::testAddLocalModule() 0115 { 0116 const QString expectedFileName = "local.desktop"; 0117 QString fileName = "local%1.desktop"; 0118 const QString path = m_moduleManager->addModuleFromTemplate(fileName); 0119 QCOMPARE(fileName, expectedFileName); 0120 QVERIFY(path.endsWith(fileName)); 0121 KDesktopFile testModule(path); 0122 KConfigGroup scf = testModule.desktopGroup(); 0123 scf.writeEntry("Type", "Link"); 0124 scf.writePathEntry("URL", "/tmp"); 0125 scf.writeEntry("Icon", "home"); 0126 scf.sync(); 0127 m_moduleManager->moduleAdded(fileName); 0128 QVERIFY(QFile::exists(path)); 0129 0130 const QStringList modules = m_moduleManager->modules(); 0131 if (modules.count() != m_realModules + 2) { 0132 qDebug() << modules; 0133 } 0134 QCOMPARE(modules.count(), m_realModules + 2); 0135 QVERIFY(modules.contains("testModule.desktop")); 0136 QVERIFY(modules.contains(fileName)); 0137 0138 // Check that this didn't affect the other profile 0139 const QStringList modules2 = m_moduleManager2->modules(); 0140 QCOMPARE(modules2.count(), m_realModules + 1); 0141 QVERIFY(modules2.contains("testModule.desktop")); 0142 0143 fileName = "local%1.desktop"; 0144 const QString secondPath = m_moduleManager->addModuleFromTemplate(fileName); 0145 QCOMPARE(fileName, QString("local1.desktop")); 0146 QVERIFY(secondPath.endsWith("local1.desktop")); 0147 } 0148 0149 void ModuleManagerTest::testRenameGlobalModule() 0150 { 0151 m_moduleManager->setModuleName("testModule.desktop", "new name"); 0152 const QStringList modules = m_moduleManager->modules(); 0153 if (modules.count() != m_realModules + 2) { 0154 qDebug() << modules; 0155 } 0156 QCOMPARE(modules.count(), m_realModules + 2); 0157 QVERIFY(modules.contains("testModule.desktop")); 0158 // A local copy was made 0159 const QString localCopy = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1Char('/') + "konqsidebartng/entries/testModule.desktop"; 0160 qDebug() << localCopy; 0161 QVERIFY(QFile(localCopy).exists()); 0162 // We didn't lose the icon (e.g. due to lack of merging) 0163 // Well, this code does the merging ;) 0164 const QString icon = KDesktopFile(QStandardPaths::GenericDataLocation, "konqsidebartng/entries/testModule.desktop").readIcon(); 0165 QVERIFY(!icon.isEmpty()); 0166 } 0167 0168 0169 void ModuleManagerTest::testRemoveLocalModule() 0170 { 0171 m_moduleManager->removeModule("local.desktop"); 0172 QCOMPARE(m_moduleManager->modules().count(), m_realModules + 1); 0173 QCOMPARE(m_moduleManager2->modules().count(), m_realModules + 1); 0174 } 0175 0176 void ModuleManagerTest::testRemoveGlobalModule() 0177 { 0178 m_moduleManager->removeModule("testModule.desktop"); 0179 QCOMPARE(m_moduleManager->modules().count(), m_realModules + 0); 0180 QCOMPARE(m_moduleManager2->modules().count(), m_realModules + 1); // not affected. 0181 } 0182 0183 void ModuleManagerTest::testReAddGlobalModule() 0184 { 0185 m_moduleManager->moduleAdded("testModule.desktop"); 0186 // It should re-appear once, not twice [was: 1 because global desktop file and 1 because in AddedModules] 0187 QCOMPARE(m_moduleManager->modules().count(), m_realModules + 1); 0188 QCOMPARE(m_moduleManager2->modules().count(), m_realModules + 1); // not affected. 0189 } 0190 0191 void ModuleManagerTest::testRollback() 0192 { 0193 m_moduleManager->rollbackToDefault(); 0194 0195 // FIXME: This will fail because testModule.desktop will have been removed 0196 // by testRemoveGlobalModule() above. 0197 //testListModules(); 0198 } 0199 0200 void ModuleManagerTest::testAvailablePlugins() 0201 { 0202 QVector<KPluginMetaData> availablePlugins = m_moduleManager->availablePlugins(); 0203 QVERIFY(availablePlugins.count() >= 2); 0204 QStringList libs; 0205 for (const KPluginMetaData &service : availablePlugins) { 0206 libs.append(service.pluginId()); 0207 } 0208 qDebug() << libs; 0209 QVERIFY(libs.contains("konqsidebar_tree")); 0210 // konqsidebar_web is not built because it depends on KHTML 0211 //QVERIFY(libs.contains("konqsidebar_web")); 0212 } 0213 0214 #include "modulemanagertest.moc"