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 "pluginmanager_p.h"
#include <utils/algorithm.h>
/*!
@@ -159,32 +161,16 @@
*/
namespace ExtensionSystem {
namespace Internal {
class IPluginPrivate
{
public:
QList<TestCreator> testCreators;
};
} // Internal
/*!
\internal
*/
IPlugin::IPlugin()
: d(new Internal::IPluginPrivate())
{
}
IPlugin::IPlugin() = default;
/*!
\internal
*/
IPlugin::~IPlugin()
{
delete d;
d = nullptr;
}
IPlugin::~IPlugin() = default;
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)
{
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

View File

@@ -37,9 +37,6 @@ public:
const QStringList & /* arguments */) { return nullptr; }
// Deprecated in 10.0, use addTest()
virtual QVector<QObject *> createTestObjects() const;
template <typename Test, typename ...Args>
void addTest(Args && ...args) { addTestCreator([args...] { return new Test(args...); }); }
void addTestCreator(const TestCreator &creator);
@@ -51,7 +48,7 @@ signals:
void asynchronousShutdownFinished();
private:
Internal::IPluginPrivate *d;
Internal::IPluginPrivate *d = nullptr; // For potential extension. Currently unused.
};
} // 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
using TestPlan = QMap<QObject *, QStringList>; // Object -> selected test functions
@@ -1289,7 +1298,8 @@ void PluginManagerPrivate::startTests()
if (!plugin)
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 bool hasDuplicateTestObjects = testObjects.size()

View File

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