File indexing completed on 2024-05-19 05:42:14

0001 // ct_lvtplg_pluginmanager.t.cpp                                     -*-C++-*-
0002 
0003 /*
0004 // Copyright 2023 Codethink Ltd <codethink@codethink.co.uk>
0005 // SPDX-License-Identifier: Apache-2.0
0006 //
0007 // Licensed under the Apache License, Version 2.0 (the "License");
0008 // you may not use this file except in compliance with the License.
0009 // You may obtain a copy of the License at
0010 //
0011 //     http://www.apache.org/licenses/LICENSE-2.0
0012 //
0013 // Unless required by applicable law or agreed to in writing, software
0014 // distributed under the License is distributed on an "AS IS" BASIS,
0015 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0016 // See the License for the specific language governing permissions and
0017 // limitations under the License.
0018 */
0019 
0020 #include <ct_lvtplg_pluginmanager.h>
0021 
0022 #include <QDir>
0023 #include <string>
0024 
0025 #include <catch2-local-includes.h>
0026 #include <test-project-paths.h>
0027 
0028 using namespace Codethink::lvtplg;
0029 
0030 auto constexpr CPP_TEST_PLUGIN_ID = "cppTestPlugin";
0031 auto constexpr PY_TEST_PLUGIN_ID = "pyTestPlugin";
0032 
0033 TEST_CASE("Plugin manager")
0034 {
0035     auto const pluginsPath = std::string{TEST_PLG_PATH};
0036 
0037     auto pm = PluginManager{};
0038     pm.loadPlugins({QString::fromStdString(pluginsPath)});
0039 
0040     // Make sure all plugins are enabled for this test
0041     pm.getPluginById(CPP_TEST_PLUGIN_ID)->get().setEnabled(true);
0042     pm.getPluginById(PY_TEST_PLUGIN_ID)->get().setEnabled(true);
0043 
0044     REQUIRE(pm.getPluginData(CPP_TEST_PLUGIN_ID) == nullptr);
0045     REQUIRE(pm.getPluginData(PY_TEST_PLUGIN_ID) == nullptr);
0046     pm.callHooksSetupPlugin();
0047     REQUIRE(pm.getPluginData(CPP_TEST_PLUGIN_ID) != nullptr);
0048     REQUIRE(pm.getPluginData(PY_TEST_PLUGIN_ID) != nullptr);
0049     REQUIRE(pm.getPluginData("some_other_id") == nullptr);
0050     pm.callHooksTeardownPlugin();
0051     REQUIRE(pm.getPluginData(CPP_TEST_PLUGIN_ID) == nullptr);
0052     REQUIRE(pm.getPluginData(PY_TEST_PLUGIN_ID) == nullptr);
0053 
0054     const QString basicPythonPluginPath =
0055         QString::fromStdString(pluginsPath) + QDir::separator() + QStringLiteral("basicpythonplugin");
0056 
0057     // Make sure it doesn't crashes reloading the plugin.'
0058     pm.reloadPlugin(basicPythonPluginPath);
0059 
0060     // Check workflow with disabled plugin
0061     pm.getPluginById(CPP_TEST_PLUGIN_ID)->get().setEnabled(false);
0062     pm.getPluginById(PY_TEST_PLUGIN_ID)->get().setEnabled(true);
0063     pm.callHooksSetupPlugin();
0064     REQUIRE(pm.getPluginData(CPP_TEST_PLUGIN_ID) == nullptr);
0065     REQUIRE(pm.getPluginData(PY_TEST_PLUGIN_ID) != nullptr);
0066     pm.callHooksTeardownPlugin();
0067     REQUIRE(pm.getPluginData(PY_TEST_PLUGIN_ID) == nullptr);
0068 
0069     pm.getPluginById(CPP_TEST_PLUGIN_ID)->get().setEnabled(true);
0070     pm.getPluginById(PY_TEST_PLUGIN_ID)->get().setEnabled(true);
0071     pm.callHooksSetupPlugin();
0072     REQUIRE(pm.getPluginData(CPP_TEST_PLUGIN_ID) != nullptr);
0073     REQUIRE(pm.getPluginData(PY_TEST_PLUGIN_ID) != nullptr);
0074     pm.callHooksTeardownPlugin();
0075     REQUIRE(pm.getPluginData(CPP_TEST_PLUGIN_ID) == nullptr);
0076     REQUIRE(pm.getPluginData(PY_TEST_PLUGIN_ID) == nullptr);
0077 }
0078 
0079 TEST_CASE("Unload plugin")
0080 {
0081     auto const pluginsPath = std::string{TEST_PLG_PATH};
0082     auto pm = PluginManager{};
0083     pm.loadPlugins({QString::fromStdString(pluginsPath)});
0084 
0085     // TODO: Unload C++ plugins
0086     // pm.getPluginById(CPP_TEST_PLUGIN_ID)->get().setEnabled(true);
0087     pm.getPluginById(PY_TEST_PLUGIN_ID)->get().setEnabled(true);
0088     pm.callHooksSetupPlugin();
0089     REQUIRE(pm.getPluginData(PY_TEST_PLUGIN_ID) != nullptr);
0090 
0091     const QString basicPythonPluginPath =
0092         QString::fromStdString(pluginsPath) + QDir::separator() + QStringLiteral("basicpythonplugin");
0093     pm.removePlugin(basicPythonPluginPath);
0094     REQUIRE(pm.getPluginData(PY_TEST_PLUGIN_ID) == nullptr);
0095 }