32 #include <QtTest/QtTest>
34 #include <QtCore/QObject>
36 using namespace ExtensionSystem;
47 void addRemoveObjects();
51 void circularPlugins();
52 void correctPlugins1();
66 aboutToRemoveObjectCount(0),
67 pluginsChangedCount(0),
69 aboutToRemoveObjectObj(0)
77 void objectAdded(QObject *obj) { objectAddedCount++; objectAddedObj = obj; }
97 void tst_PluginManager::init()
101 connect(m_pm, SIGNAL(objectAdded(QObject*)), m_sr, SLOT(objectAdded(QObject*)));
102 connect(m_pm, SIGNAL(aboutToRemoveObject(QObject*)), m_sr, SLOT(aboutToRemoveObject(QObject*)));
103 connect(m_pm, SIGNAL(pluginsChanged()), m_sr, SLOT(pluginsChanged()));
106 void tst_PluginManager::cleanup()
112 void tst_PluginManager::addRemoveObjects()
114 QObject *object1 =
new QObject;
115 QObject *object2 =
new QObject;
116 QCOMPARE(m_pm->allObjects().size(), 0);
117 m_pm->addObject(object1);
118 QCOMPARE(m_sr->objectAddedCount, 1);
119 QCOMPARE(m_sr->objectAddedObj, object1);
120 QCOMPARE(m_sr->aboutToRemoveObjectCount, 0);
121 QVERIFY(m_pm->allObjects().contains(object1));
122 QVERIFY(!m_pm->allObjects().contains(object2));
123 QCOMPARE(m_pm->allObjects().size(), 1);
124 m_pm->addObject(object2);
125 QCOMPARE(m_sr->objectAddedCount, 2);
126 QCOMPARE(m_sr->objectAddedObj, object2);
127 QCOMPARE(m_sr->aboutToRemoveObjectCount, 0);
128 QVERIFY(m_pm->allObjects().contains(object1));
129 QVERIFY(m_pm->allObjects().contains(object2));
130 QCOMPARE(m_pm->allObjects().size(), 2);
131 m_pm->removeObject(object1);
132 QCOMPARE(m_sr->objectAddedCount, 2);
133 QCOMPARE(m_sr->aboutToRemoveObjectCount, 1);
134 QCOMPARE(m_sr->aboutToRemoveObjectObj, object1);
135 QVERIFY(!m_pm->allObjects().contains(object1));
136 QVERIFY(m_pm->allObjects().contains(object2));
137 QCOMPARE(m_pm->allObjects().size(), 1);
138 m_pm->removeObject(object2);
139 QCOMPARE(m_sr->objectAddedCount, 2);
140 QCOMPARE(m_sr->aboutToRemoveObjectCount, 2);
141 QCOMPARE(m_sr->aboutToRemoveObjectObj, object2);
142 QVERIFY(!m_pm->allObjects().contains(object1));
143 QVERIFY(!m_pm->allObjects().contains(object2));
144 QCOMPARE(m_pm->allObjects().size(), 0);
149 void tst_PluginManager::getObject()
153 m_pm->addObject(object2);
156 QCOMPARE(m_pm->getObject<
MyClass2>(), object2);
157 m_pm->addObject(object11);
158 QCOMPARE(m_pm->getObject<
MyClass11>(), object11);
159 QCOMPARE(m_pm->getObject<
MyClass1>(), qobject_cast<MyClass1*>(object11));
160 QCOMPARE(m_pm->getObject<
MyClass2>(), object2);
161 m_pm->removeObject(object2);
162 m_pm->removeObject(object11);
167 void tst_PluginManager::getObjects()
172 m_pm->addObject(object2);
177 m_pm->addObject(object11);
181 QCOMPARE(m_pm->allObjects(),
QList<QObject*>() << object2 << object11);
182 m_pm->addObject(object1);
186 QCOMPARE(m_pm->allObjects(),
QList<QObject*>() << object2 << object11 << object1);
187 m_pm->removeObject(object2);
188 m_pm->removeObject(object11);
189 m_pm->removeObject(object1);
195 void tst_PluginManager::plugins()
197 m_pm->setPluginPaths(QStringList() <<
"plugins");
198 QCOMPARE(m_sr->pluginsChangedCount, 1);
199 QSet<PluginSpec *> plugins = m_pm->plugins();
200 QCOMPARE(plugins.count(), 3);
201 foreach (
const QString &expected, QStringList() <<
"helloworld" <<
"MyPlugin" <<
"dummyPlugin") {
204 if (spec->
name() == expected) {
209 QVERIFY2(found, QString(
"plugin '%1' not found").arg(expected).toLocal8Bit().constData());
213 void tst_PluginManager::circularPlugins()
215 m_pm->setPluginPaths(QStringList() <<
"circularplugins");
218 if (spec->
name() ==
"plugin1") {
222 }
else if (spec->
name() ==
"plugin2") {
225 }
else if (spec->
name() ==
"plugin3") {
233 void tst_PluginManager::correctPlugins1()
235 m_pm->setFileExtension(
"spec");
236 m_pm->setPluginPaths(QStringList() <<
"correctplugins1");
244 bool plugin1running =
false;
245 bool plugin2running =
false;
246 bool plugin3running =
false;
247 foreach (QObject *obj, m_pm->allObjects()) {
248 if (obj->objectName() ==
"MyPlugin1_running")
249 plugin1running =
true;
250 else if (obj->objectName() ==
"MyPlugin2_running")
251 plugin2running =
true;
252 else if (obj->objectName() ==
"MyPlugin3_running")
253 plugin3running =
true;
255 QVERIFY(plugin1running);
256 QVERIFY(plugin2running);
257 QVERIFY(plugin3running);
262 #include "tst_pluginmanager.moc"
void aboutToRemoveObject(QObject *obj)
Core plugin system that manages the plugins, their life cycle and their registered objects...
int aboutToRemoveObjectCount
QString errorString() const
QObject * aboutToRemoveObjectObj
void objectAdded(QObject *obj)
Contains the information of the plugins xml description file and information about the plugin's curre...
Base class for all plugins.