2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2016-01-15 14:55:33 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2016-01-15 14:55:33 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2016-01-15 14:55:33 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2010-12-17 16:01:08 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
#include <extensionsystem/pluginmanager.h>
|
|
|
|
|
#include <extensionsystem/pluginspec.h>
|
|
|
|
|
#include <extensionsystem/iplugin.h>
|
|
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QtTest>
|
2008-12-02 14:09:21 +01:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QObject>
|
2008-12-02 14:09:21 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
using namespace ExtensionSystem;
|
|
|
|
|
|
|
|
|
|
class SignalReceiver;
|
|
|
|
|
|
|
|
|
|
class tst_PluginManager : public QObject
|
|
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
|
|
private slots:
|
|
|
|
|
void init();
|
|
|
|
|
void cleanup();
|
|
|
|
|
void addRemoveObjects();
|
|
|
|
|
void getObject();
|
|
|
|
|
void getObjects();
|
|
|
|
|
void circularPlugins();
|
|
|
|
|
void correctPlugins1();
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
PluginManager *m_pm;
|
2010-11-08 14:08:41 +01:00
|
|
|
QSignalSpy *m_objectAdded;
|
|
|
|
|
QSignalSpy *m_aboutToRemoveObject;
|
|
|
|
|
QSignalSpy *m_pluginsChanged;
|
2008-12-02 12:01:29 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class MyClass1 : public QObject
|
|
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class MyClass2 : public QObject
|
|
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class MyClass11 : public MyClass1
|
|
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
};
|
|
|
|
|
|
2013-06-06 13:09:21 +02:00
|
|
|
static QString pluginFolder(const QLatin1String &folder)
|
|
|
|
|
{
|
|
|
|
|
return QLatin1String(PLUGINMANAGER_TESTS_DIR) + QLatin1String("/") + folder;
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
void tst_PluginManager::init()
|
|
|
|
|
{
|
2014-08-26 17:29:38 +02:00
|
|
|
QLoggingCategory::setFilterRules(QLatin1String("qtc.*.debug=false"));
|
2008-12-02 12:01:29 +01:00
|
|
|
m_pm = new PluginManager;
|
2014-08-26 17:29:38 +02:00
|
|
|
m_pm->setPluginIID(QLatin1String("plugin"));
|
2010-11-08 14:08:41 +01:00
|
|
|
m_objectAdded = new QSignalSpy(m_pm, SIGNAL(objectAdded(QObject*)));
|
|
|
|
|
m_aboutToRemoveObject = new QSignalSpy(m_pm, SIGNAL(aboutToRemoveObject(QObject*)));
|
|
|
|
|
m_pluginsChanged = new QSignalSpy(m_pm, SIGNAL(pluginsChanged()));
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void tst_PluginManager::cleanup()
|
|
|
|
|
{
|
2013-10-29 09:41:56 +01:00
|
|
|
m_pm->shutdown();
|
2008-12-02 12:01:29 +01:00
|
|
|
delete m_pm;
|
2010-11-08 14:08:41 +01:00
|
|
|
delete m_objectAdded;
|
|
|
|
|
delete m_aboutToRemoveObject;
|
|
|
|
|
delete m_pluginsChanged;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void tst_PluginManager::addRemoveObjects()
|
|
|
|
|
{
|
|
|
|
|
QObject *object1 = new QObject;
|
|
|
|
|
QObject *object2 = new QObject;
|
|
|
|
|
QCOMPARE(m_pm->allObjects().size(), 0);
|
|
|
|
|
m_pm->addObject(object1);
|
2010-11-08 14:08:41 +01:00
|
|
|
QCOMPARE(m_objectAdded->count(), 1);
|
|
|
|
|
QCOMPARE(m_objectAdded->at(0).first().value<QObject *>(), object1);
|
|
|
|
|
QCOMPARE(m_aboutToRemoveObject->count(), 0);
|
2008-12-02 12:01:29 +01:00
|
|
|
QVERIFY(m_pm->allObjects().contains(object1));
|
|
|
|
|
QVERIFY(!m_pm->allObjects().contains(object2));
|
|
|
|
|
QCOMPARE(m_pm->allObjects().size(), 1);
|
|
|
|
|
m_pm->addObject(object2);
|
2010-11-08 14:08:41 +01:00
|
|
|
QCOMPARE(m_objectAdded->count(), 2);
|
|
|
|
|
QCOMPARE(m_objectAdded->at(1).first().value<QObject *>(), object2);
|
|
|
|
|
QCOMPARE(m_aboutToRemoveObject->count(), 0);
|
2008-12-02 12:01:29 +01:00
|
|
|
QVERIFY(m_pm->allObjects().contains(object1));
|
|
|
|
|
QVERIFY(m_pm->allObjects().contains(object2));
|
|
|
|
|
QCOMPARE(m_pm->allObjects().size(), 2);
|
|
|
|
|
m_pm->removeObject(object1);
|
2010-11-08 14:08:41 +01:00
|
|
|
QCOMPARE(m_objectAdded->count(), 2);
|
|
|
|
|
QCOMPARE(m_aboutToRemoveObject->count(), 1);
|
|
|
|
|
QCOMPARE(m_aboutToRemoveObject->at(0).first().value<QObject *>(), object1);
|
2008-12-02 12:01:29 +01:00
|
|
|
QVERIFY(!m_pm->allObjects().contains(object1));
|
|
|
|
|
QVERIFY(m_pm->allObjects().contains(object2));
|
|
|
|
|
QCOMPARE(m_pm->allObjects().size(), 1);
|
|
|
|
|
m_pm->removeObject(object2);
|
2010-11-08 14:08:41 +01:00
|
|
|
QCOMPARE(m_objectAdded->count(), 2);
|
|
|
|
|
QCOMPARE(m_aboutToRemoveObject->count(), 2);
|
|
|
|
|
QCOMPARE(m_aboutToRemoveObject->at(1).first().value<QObject *>(), object2);
|
2008-12-02 12:01:29 +01:00
|
|
|
QVERIFY(!m_pm->allObjects().contains(object1));
|
|
|
|
|
QVERIFY(!m_pm->allObjects().contains(object2));
|
|
|
|
|
QCOMPARE(m_pm->allObjects().size(), 0);
|
|
|
|
|
delete object1;
|
|
|
|
|
delete object2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void tst_PluginManager::getObject()
|
|
|
|
|
{
|
|
|
|
|
MyClass2 *object2 = new MyClass2;
|
|
|
|
|
MyClass11 *object11 = new MyClass11;
|
2014-05-06 18:33:10 +02:00
|
|
|
MyClass2 *object2b = new MyClass2;
|
|
|
|
|
const QString objectName = QLatin1String("OBJECTNAME");
|
|
|
|
|
object2b->setObjectName(objectName);
|
2008-12-02 12:01:29 +01:00
|
|
|
m_pm->addObject(object2);
|
|
|
|
|
QCOMPARE(m_pm->getObject<MyClass11>(), (MyClass11*)0);
|
|
|
|
|
QCOMPARE(m_pm->getObject<MyClass1>(), (MyClass1*)0);
|
|
|
|
|
QCOMPARE(m_pm->getObject<MyClass2>(), object2);
|
|
|
|
|
m_pm->addObject(object11);
|
|
|
|
|
QCOMPARE(m_pm->getObject<MyClass11>(), object11);
|
|
|
|
|
QCOMPARE(m_pm->getObject<MyClass1>(), qobject_cast<MyClass1*>(object11));
|
|
|
|
|
QCOMPARE(m_pm->getObject<MyClass2>(), object2);
|
2014-05-06 18:33:10 +02:00
|
|
|
QCOMPARE(m_pm->getObjectByName(objectName), (QObject*)0);
|
|
|
|
|
m_pm->addObject(object2b);
|
|
|
|
|
QCOMPARE(m_pm->getObjectByName(objectName), object2b);
|
|
|
|
|
QCOMPARE(m_pm->getObject<MyClass2>(
|
|
|
|
|
[&objectName](MyClass2 *obj) { return obj->objectName() == objectName;}), object2b);
|
2008-12-02 12:01:29 +01:00
|
|
|
m_pm->removeObject(object2);
|
|
|
|
|
m_pm->removeObject(object11);
|
2014-05-06 18:33:10 +02:00
|
|
|
m_pm->removeObject(object2b);
|
2008-12-02 12:01:29 +01:00
|
|
|
delete object2;
|
|
|
|
|
delete object11;
|
2014-05-06 18:33:10 +02:00
|
|
|
delete object2b;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void tst_PluginManager::getObjects()
|
|
|
|
|
{
|
|
|
|
|
MyClass1 *object1 = new MyClass1;
|
|
|
|
|
MyClass2 *object2 = new MyClass2;
|
|
|
|
|
MyClass11 *object11 = new MyClass11;
|
|
|
|
|
m_pm->addObject(object2);
|
|
|
|
|
QCOMPARE(m_pm->getObjects<MyClass11>(), QList<MyClass11*>());
|
|
|
|
|
QCOMPARE(m_pm->getObjects<MyClass1>(), QList<MyClass1*>());
|
|
|
|
|
QCOMPARE(m_pm->getObjects<MyClass2>(), QList<MyClass2*>() << object2);
|
|
|
|
|
QCOMPARE(m_pm->allObjects(), QList<QObject*>() << object2);
|
|
|
|
|
m_pm->addObject(object11);
|
|
|
|
|
QCOMPARE(m_pm->getObjects<MyClass11>(), QList<MyClass11*>() << object11);
|
|
|
|
|
QCOMPARE(m_pm->getObjects<MyClass1>(), QList<MyClass1*>() << object11);
|
|
|
|
|
QCOMPARE(m_pm->getObjects<MyClass2>(), QList<MyClass2*>() << object2);
|
|
|
|
|
QCOMPARE(m_pm->allObjects(), QList<QObject*>() << object2 << object11);
|
|
|
|
|
m_pm->addObject(object1);
|
|
|
|
|
QCOMPARE(m_pm->getObjects<MyClass11>(), QList<MyClass11*>() << object11);
|
|
|
|
|
QCOMPARE(m_pm->getObjects<MyClass1>(), QList<MyClass1*>() << object11 << object1);
|
|
|
|
|
QCOMPARE(m_pm->getObjects<MyClass2>(), QList<MyClass2*>() << object2);
|
|
|
|
|
QCOMPARE(m_pm->allObjects(), QList<QObject*>() << object2 << object11 << object1);
|
2014-05-06 18:33:10 +02:00
|
|
|
|
|
|
|
|
QCOMPARE(m_pm->getObjects<MyClass1>(
|
|
|
|
|
[](MyClass1 *o){
|
|
|
|
|
return !qobject_cast<MyClass11 *>(o);} ),
|
|
|
|
|
QList<MyClass1 *>() << object1);
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
m_pm->removeObject(object2);
|
|
|
|
|
m_pm->removeObject(object11);
|
|
|
|
|
m_pm->removeObject(object1);
|
|
|
|
|
delete object1;
|
|
|
|
|
delete object2;
|
|
|
|
|
delete object11;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void tst_PluginManager::circularPlugins()
|
|
|
|
|
{
|
2013-06-06 13:09:21 +02:00
|
|
|
m_pm->setPluginPaths(QStringList() << pluginFolder(QLatin1String("circularplugins")));
|
2008-12-02 12:01:29 +01:00
|
|
|
m_pm->loadPlugins();
|
2013-09-05 12:27:48 +02:00
|
|
|
QList<PluginSpec *> plugins = m_pm->plugins();
|
|
|
|
|
QCOMPARE(plugins.count(), 3);
|
|
|
|
|
foreach (PluginSpec *spec, plugins) {
|
2008-12-02 12:01:29 +01:00
|
|
|
if (spec->name() == "plugin1") {
|
|
|
|
|
QVERIFY(spec->hasError());
|
|
|
|
|
QCOMPARE(spec->state(), PluginSpec::Resolved);
|
|
|
|
|
QCOMPARE(spec->plugin(), (IPlugin*)0);
|
|
|
|
|
} else if (spec->name() == "plugin2") {
|
2013-09-05 12:29:55 +02:00
|
|
|
QVERIFY2(!spec->hasError(), qPrintable(spec->errorString()));
|
2008-12-02 12:01:29 +01:00
|
|
|
QCOMPARE(spec->state(), PluginSpec::Running);
|
|
|
|
|
} else if (spec->name() == "plugin3") {
|
|
|
|
|
QVERIFY(spec->hasError());
|
|
|
|
|
QCOMPARE(spec->state(), PluginSpec::Resolved);
|
|
|
|
|
QCOMPARE(spec->plugin(), (IPlugin*)0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void tst_PluginManager::correctPlugins1()
|
|
|
|
|
{
|
2013-06-06 13:09:21 +02:00
|
|
|
m_pm->setPluginPaths(QStringList() << pluginFolder(QLatin1String("correctplugins1")));
|
2008-12-02 12:01:29 +01:00
|
|
|
m_pm->loadPlugins();
|
2014-08-26 17:29:38 +02:00
|
|
|
bool specError = false;
|
|
|
|
|
bool runError = false;
|
2008-12-02 12:01:29 +01:00
|
|
|
foreach (PluginSpec *spec, m_pm->plugins()) {
|
2014-08-26 17:29:38 +02:00
|
|
|
if (spec->hasError()) {
|
|
|
|
|
qDebug() << spec->filePath();
|
2008-12-02 12:01:29 +01:00
|
|
|
qDebug() << spec->errorString();
|
2014-08-26 17:29:38 +02:00
|
|
|
}
|
|
|
|
|
specError = specError || spec->hasError();
|
|
|
|
|
runError = runError || (spec->state() != PluginSpec::Running);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2014-08-26 17:29:38 +02:00
|
|
|
QVERIFY(!specError);
|
|
|
|
|
QVERIFY(!runError);
|
2008-12-02 12:01:29 +01:00
|
|
|
bool plugin1running = false;
|
|
|
|
|
bool plugin2running = false;
|
|
|
|
|
bool plugin3running = false;
|
|
|
|
|
foreach (QObject *obj, m_pm->allObjects()) {
|
|
|
|
|
if (obj->objectName() == "MyPlugin1_running")
|
|
|
|
|
plugin1running = true;
|
|
|
|
|
else if (obj->objectName() == "MyPlugin2_running")
|
|
|
|
|
plugin2running = true;
|
|
|
|
|
else if (obj->objectName() == "MyPlugin3_running")
|
|
|
|
|
plugin3running = true;
|
|
|
|
|
}
|
|
|
|
|
QVERIFY(plugin1running);
|
|
|
|
|
QVERIFY(plugin2running);
|
|
|
|
|
QVERIFY(plugin3running);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QTEST_MAIN(tst_PluginManager)
|
2008-12-02 14:09:21 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "tst_pluginmanager.moc"
|
|
|
|
|
|