File indexing completed on 2024-05-12 04:38:21

0001 /*
0002     SPDX-FileCopyrightText: 2017 Friedrich W. H. Kossebau <kossebau@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "test_pluginenabling.h"
0008 
0009 #include "testfilepaths.h"
0010 
0011 #include <QSignalSpy>
0012 #include <QTest>
0013 
0014 #include <KConfigGroup>
0015 
0016 #include <tests/autotestshell.h>
0017 #include <tests/testcore.h>
0018 #include <isession.h>
0019 
0020 #include "../core.h"
0021 #include "../plugincontroller.h"
0022 
0023 using namespace KDevelop;
0024 
0025 void TestPluginEnabling::initTestCase()
0026 {
0027     qApp->addLibraryPath(QStringLiteral(TEST_PLUGIN_DIR));
0028 }
0029 
0030 void TestPluginEnabling::loadPlugin(const QString& pluginId, bool shouldBeEnabled)
0031 {
0032     // check config storage
0033     KConfigGroup grp = Core::self()->activeSession()->config()->group(QStringLiteral("Plugins"));
0034 
0035     const QString pluginEnabledKey = pluginId + QLatin1String("Enabled");
0036     // logic in kdevelop
0037     const bool enabled = grp.hasKey(pluginEnabledKey) ? grp.readEntry(pluginEnabledKey, true) : false;
0038 
0039     QCOMPARE(enabled, shouldBeEnabled);
0040 
0041     // check plugin loading
0042     QCOMPARE((m_pluginCtrl->loadPlugin(pluginId) != nullptr), shouldBeEnabled);
0043     QCOMPARE((m_pluginCtrl->plugin(pluginId) != nullptr), shouldBeEnabled);
0044 
0045     if (shouldBeEnabled) {
0046         m_pluginCtrl->unloadPlugin(pluginId);
0047         QVERIFY(!m_pluginCtrl->plugin(pluginId));
0048     }
0049 
0050     // switch enabled state
0051     const bool shouldNowBeEnabled = !shouldBeEnabled;
0052     grp.writeEntry(pluginEnabledKey, shouldNowBeEnabled);
0053 
0054     // check plugin loading again
0055     QCOMPARE((m_pluginCtrl->loadPlugin(pluginId) != nullptr), shouldNowBeEnabled);
0056     QCOMPARE((m_pluginCtrl->plugin(pluginId) != nullptr), shouldNowBeEnabled);
0057 
0058     if (shouldNowBeEnabled) {
0059         m_pluginCtrl->unloadPlugin(pluginId);
0060         QVERIFY(!m_pluginCtrl->plugin(pluginId));
0061     }
0062 
0063 }
0064 
0065 void TestPluginEnabling::loadPluginCustomDefaults_data()
0066 {
0067     QTest::addColumn<QString>("pluginId");
0068     QTest::addColumn<bool>("shouldBeEnabled");
0069 
0070     QTest::newRow("test_globaldefault") << "test_globaldefault" << false;
0071     QTest::newRow("test_globalnondefault") << "test_globalnondefault" << true;
0072     QTest::newRow("test_projectdefault") << "test_projectdefault" << false;
0073     QTest::newRow("test_projectnondefault") << "test_projectnondefault" << true;
0074 }
0075 
0076 void TestPluginEnabling::loadPluginCustomDefaults()
0077 {
0078     QFETCH(QString, pluginId);
0079     QFETCH(bool, shouldBeEnabled);
0080 
0081     AutoTestShell::init({
0082         // set those as default which would not be by own metadata
0083         // and do not set those which otherwise would, so both
0084         QStringLiteral("test_globalnondefault"),
0085         QStringLiteral("test_projectnondefault")
0086     });
0087     // TODO: somehow currently the clean-up of the previous session is not yet done
0088     // on the next data item test run, so the session with the name is still locked
0089     // so we work-around that for now by using a custom session name per session
0090     // TODO: consider adding a new bool temporarySession = true to TestCore::initialize()
0091     TestCore::initialize(Core::NoUi, QStringLiteral("test_pluginenabling_custom_")+pluginId);
0092     TestCore::self()->activeSession()->setTemporary(true);
0093     m_pluginCtrl = Core::self()->pluginControllerInternal();
0094 
0095     loadPlugin(pluginId, shouldBeEnabled);
0096 
0097     TestCore::shutdown();
0098 }
0099 
0100 void TestPluginEnabling::loadPluginNormalDefaults_data()
0101 {
0102     QTest::addColumn<QString>("pluginId");
0103     QTest::addColumn<bool>("shouldBeEnabled");
0104 
0105     QTest::newRow("test_globaldefault") << "test_globaldefault" << true;
0106     QTest::newRow("test_globalnondefault") << "test_globalnondefault" << false;
0107     QTest::newRow("test_projectdefault") << "test_projectdefault" << true;
0108     QTest::newRow("test_projectnondefault") << "test_projectnondefault" << false;
0109 }
0110 
0111 void TestPluginEnabling::loadPluginNormalDefaults()
0112 {
0113     QFETCH(QString, pluginId);
0114     QFETCH(bool, shouldBeEnabled);
0115 
0116     AutoTestShell::init();
0117     // see TODO in loadPluginCustomDefaults()
0118     TestCore::initialize(Core::NoUi, QStringLiteral("test_pluginenabling_normal_")+pluginId);
0119     TestCore::self()->activeSession()->setTemporary(true);
0120     m_pluginCtrl = Core::self()->pluginControllerInternal();
0121 
0122     // check plugin metadata
0123     const auto pluginInfo  = m_pluginCtrl->infoForPluginId(pluginId);
0124     // logic in kdevelop different from  KPluginMetaData::isEnabledByDefault(), here defaults to true
0125     const QJsonValue enabledByDefaultValue = pluginInfo.rawData()["KPlugin"].toObject()["EnabledByDefault"];
0126     const bool enabledByDefault = (enabledByDefaultValue.isNull() || enabledByDefaultValue.toBool());
0127     QCOMPARE(enabledByDefault, shouldBeEnabled);
0128 
0129     loadPlugin(pluginId, shouldBeEnabled);
0130 
0131     TestCore::shutdown();
0132 }
0133 
0134 QTEST_MAIN(TestPluginEnabling)
0135 
0136 #include "moc_test_pluginenabling.cpp"