File indexing completed on 2024-10-06 12:17:08

0001 /*
0002     SPDX-FileCopyrightText: 2014 Alex Merry <alex.merry@kde.org>
0003     SPDX-FileCopyrightText: 2021 Alexander Lohnau <alexander.lohnau@gmx.de>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 
0008 #include <QTest>
0009 
0010 #include <QPluginLoader>
0011 #include <kpluginfactory.h>
0012 #include <kpluginloader.h>
0013 
0014 // We do not have QWidgets as a dependency, this is a simple placeholder for the type to be fully qualified
0015 class QWidget : public QObject
0016 {
0017 };
0018 
0019 class KPluginFactoryTest : public QObject
0020 {
0021     Q_OBJECT
0022 
0023 private Q_SLOTS:
0024     void testCreate()
0025     {
0026         KPluginFactory::Result<KPluginFactory> factoryResult = KPluginFactory::loadFactory(KPluginMetaData(QStringLiteral("multiplugin")));
0027         auto factory = factoryResult.plugin;
0028         QVERIFY(factory);
0029         QVariantList args;
0030         args << QStringLiteral("Some") << QStringLiteral("args") << 5;
0031 
0032         QObject *obj = factory->create<QObject>(this, args);
0033         QVERIFY(obj);
0034         QCOMPARE(obj->objectName(), QString::fromLatin1("MultiPlugin1"));
0035 
0036         QObject *obj2 = factory->create<QObject>(this, args);
0037         QVERIFY(obj2);
0038         QCOMPARE(obj2->objectName(), QString::fromLatin1("MultiPlugin1"));
0039         QVERIFY(obj != obj2);
0040         delete obj;
0041         delete obj2;
0042 
0043         // Try creating a part without keyword/args
0044         QWidget parentWidget;
0045         QObject *partTest = factory->create<QObject>(&parentWidget, this);
0046         QVERIFY(partTest);
0047         delete partTest;
0048 
0049 #if KCOREADDONS_BUILD_DEPRECATED_SINCE(5, 89)
0050         obj = factory->create<QObject>(QStringLiteral("secondary"), this, args);
0051         QVERIFY(obj);
0052         QCOMPARE(obj->objectName(), QString::fromLatin1("MultiPlugin2"));
0053 
0054         obj2 = factory->create<QObject>(QStringLiteral("secondary"), this, args);
0055         QVERIFY(obj2);
0056         QCOMPARE(obj2->objectName(), QString::fromLatin1("MultiPlugin2"));
0057         QVERIFY(obj != obj2);
0058         delete obj;
0059         delete obj2;
0060 #endif
0061     }
0062 
0063     void testPluginWithoutMetaData()
0064     {
0065         KPluginFactory::Result<KPluginFactory> factoryResult = KPluginFactory::loadFactory(KPluginMetaData(QStringLiteral("namespace/pluginwithoutmetadata")));
0066         QVERIFY(factoryResult);
0067         auto plugin = factoryResult.plugin->create<QObject>();
0068         QVERIFY(plugin);
0069         QCOMPARE(plugin->metaObject()->className(), "PluginWithoutMetaData");
0070         delete plugin;
0071     }
0072 
0073     void testResultingCMakeMacroPlugin()
0074     {
0075         KPluginMetaData data(QStringLiteral("namespace/jsonplugin_cmake_macro"));
0076         QVERIFY(data.isValid());
0077 
0078         auto instance = QPluginLoader(data.fileName()).instance();
0079         QVERIFY(instance);
0080         QCOMPARE(instance->metaObject()->className(), "jsonplugin_cmake_macro_factory");
0081     }
0082     void testCreateUsingUtilityMethods()
0083     {
0084         auto result = KPluginFactory::instantiatePlugin<QObject>(KPluginMetaData(QStringLiteral("jsonplugin")), nullptr, QVariantList());
0085         QVERIFY(result.plugin);
0086         QCOMPARE(result.plugin->metaObject()->className(), "JsonPlugin");
0087         QVERIFY(result.errorString.isEmpty());
0088         QCOMPARE(result.errorReason, KPluginFactory::NO_PLUGIN_ERROR);
0089         delete result.plugin;
0090     }
0091 
0092     void testCreateUsingUtilityMethodsErrorHandling()
0093     {
0094         {
0095             auto result = KPluginFactory::instantiatePlugin<QObject>(KPluginMetaData(QFINDTESTDATA("jsonplugin.json")), nullptr, QVariantList());
0096             QVERIFY(!result.plugin);
0097             QCOMPARE(result.errorReason, KPluginFactory::INVALID_PLUGIN);
0098         }
0099         {
0100             // it is a valid plugin, but does not contain a KPluginFactory
0101             QVERIFY(QPluginLoader(QStringLiteral("qtplugin")).instance());
0102             auto result = KPluginFactory::instantiatePlugin<QObject>(KPluginMetaData(QStringLiteral("qtplugin")), nullptr, QVariantList());
0103             QVERIFY(!result.plugin);
0104             // But does not contain a valid plugin factory
0105             QCOMPARE(result.errorReason, KPluginFactory::INVALID_FACTORY);
0106         }
0107         {
0108             // it is a QObject, but not a KPluginFactoryTest instance
0109             auto result = KPluginFactory::instantiatePlugin<KPluginFactoryTest>(KPluginMetaData(QStringLiteral("jsonplugin")), nullptr, QVariantList());
0110             QVERIFY(!result.plugin);
0111             QCOMPARE(result.errorReason, KPluginFactory::INVALID_KPLUGINFACTORY_INSTANTIATION);
0112             QVERIFY(result.errorText.contains("KPluginFactoryTest"));
0113         }
0114     }
0115 
0116     void testStaticPlugins()
0117     {
0118         const auto plugins = KPluginMetaData::findPlugins(QStringLiteral("staticnamespace"));
0119         QCOMPARE(plugins.count(), 1);
0120 
0121         auto result = KPluginFactory::instantiatePlugin<QObject>(plugins.first());
0122         QVERIFY(result);
0123         delete result.plugin;
0124     }
0125 
0126     void testNonExistingPlugin()
0127     {
0128         KPluginMetaData data(QStringLiteral("does/not/exist"));
0129         QVERIFY(!data.isValid());
0130         const auto res = KPluginFactory::instantiatePlugin<QObject>(data);
0131         QVERIFY(!res);
0132         QCOMPARE(res.errorReason, KPluginFactory::INVALID_PLUGIN);
0133         QCOMPARE(res.errorText, QStringLiteral("Could not find plugin does/not/exist"));
0134     }
0135 };
0136 
0137 QTEST_MAIN(KPluginFactoryTest)
0138 
0139 #include "kpluginfactorytest.moc"