ExtensionSystem: Collect test objects centrally

Change-Id: I60cd128f0514116ef85c3f7fb040cf13ea82be66
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
hjk
2024-01-19 10:44:11 +01:00
parent 7ff6e555e3
commit 3f4dc07aea
4 changed files with 19 additions and 31 deletions

View File

@@ -3,6 +3,8 @@
#include "iplugin.h" #include "iplugin.h"
#include "pluginmanager_p.h"
#include <utils/algorithm.h> #include <utils/algorithm.h>
/*! /*!
@@ -159,32 +161,16 @@
*/ */
namespace ExtensionSystem { namespace ExtensionSystem {
namespace Internal {
class IPluginPrivate
{
public:
QList<TestCreator> testCreators;
};
} // Internal
/*! /*!
\internal \internal
*/ */
IPlugin::IPlugin() IPlugin::IPlugin() = default;
: d(new Internal::IPluginPrivate())
{
}
/*! /*!
\internal \internal
*/ */
IPlugin::~IPlugin() IPlugin::~IPlugin() = default;
{
delete d;
d = nullptr;
}
bool IPlugin::initialize(const QStringList &arguments, QString *errorString) bool IPlugin::initialize(const QStringList &arguments, QString *errorString)
{ {
@@ -208,15 +194,8 @@ bool IPlugin::initialize(const QStringList &arguments, QString *errorString)
void IPlugin::addTestCreator(const TestCreator &creator) void IPlugin::addTestCreator(const TestCreator &creator)
{ {
d->testCreators.append(creator); Internal::PluginManagerPrivate::addTestCreator(this, creator);
} }
/*!
\deprecated [10.0] Use \c addTest() instead.
*/
QVector<QObject *> IPlugin::createTestObjects() const
{
return Utils::transform(d->testCreators, &TestCreator::operator());
}
} // ExtensionSystem } // ExtensionSystem

View File

@@ -37,9 +37,6 @@ public:
const QStringList & /* arguments */) { return nullptr; } const QStringList & /* arguments */) { return nullptr; }
// Deprecated in 10.0, use addTest()
virtual QVector<QObject *> createTestObjects() const;
template <typename Test, typename ...Args> template <typename Test, typename ...Args>
void addTest(Args && ...args) { addTestCreator([args...] { return new Test(args...); }); } void addTest(Args && ...args) { addTestCreator([args...] { return new Test(args...); }); }
void addTestCreator(const TestCreator &creator); void addTestCreator(const TestCreator &creator);
@@ -51,7 +48,7 @@ signals:
void asynchronousShutdownFinished(); void asynchronousShutdownFinished();
private: private:
Internal::IPluginPrivate *d; Internal::IPluginPrivate *d = nullptr; // For potential extension. Currently unused.
}; };
} // namespace ExtensionSystem } // namespace ExtensionSystem

View File

@@ -1079,6 +1079,15 @@ void PluginManagerPrivate::checkForDuplicatePlugins()
} }
} }
static QHash<IPlugin *, QList<TestCreator>> g_testCreators;
void PluginManagerPrivate::addTestCreator(IPlugin *plugin, const TestCreator &testCreator)
{
#ifdef WITH_TESTS
g_testCreators[plugin].append(testCreator);
#endif
}
#ifdef WITH_TESTS #ifdef WITH_TESTS
using TestPlan = QMap<QObject *, QStringList>; // Object -> selected test functions using TestPlan = QMap<QObject *, QStringList>; // Object -> selected test functions
@@ -1289,7 +1298,8 @@ void PluginManagerPrivate::startTests()
if (!plugin) if (!plugin)
continue; // plugin not loaded continue; // plugin not loaded
const QVector<QObject *> testObjects = plugin->createTestObjects(); const QList<TestCreator> testCreators = g_testCreators[plugin];
const QVector<QObject *> testObjects = Utils::transform(testCreators, &TestCreator::operator());
const QScopeGuard cleanup([&] { qDeleteAll(testObjects); }); const QScopeGuard cleanup([&] { qDeleteAll(testObjects); });
const bool hasDuplicateTestObjects = testObjects.size() const bool hasDuplicateTestObjects = testObjects.size()

View File

@@ -123,6 +123,8 @@ public:
static PluginSpec *createSpec(); static PluginSpec *createSpec();
static PluginSpecPrivate *privateSpec(PluginSpec *spec); static PluginSpecPrivate *privateSpec(PluginSpec *spec);
static void addTestCreator(IPlugin *plugin, const std::function<QObject *()> &testCreator);
mutable QReadWriteLock m_lock; mutable QReadWriteLock m_lock;
bool m_isInitializationDone = false; bool m_isInitializationDone = false;