dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
tst_pluginmanager.cpp
Go to the documentation of this file.
1 
13 /*
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful, but
20  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  * for more details.
23  *
24  * You should have received a copy of the GNU General Public License along
25  * with this program; if not, see <http://www.gnu.org/licenses/>
26  */
27 
31 
32 #include <QtTest/QtTest>
33 
34 #include <QtCore/QObject>
35 
36 using namespace ExtensionSystem;
37 
38 class SignalReceiver;
39 
40 class tst_PluginManager : public QObject
41 {
42  Q_OBJECT
43 
44 private slots:
45  void init();
46  void cleanup();
47  void addRemoveObjects();
48  void getObject();
49  void getObjects();
50  void plugins();
51  void circularPlugins();
52  void correctPlugins1();
53 
54 private:
55  PluginManager *m_pm;
56  SignalReceiver *m_sr;
57 };
58 
59 class SignalReceiver : public QObject
60 {
61  Q_OBJECT
62 
63 public:
65  objectAddedCount(0),
66  aboutToRemoveObjectCount(0),
67  pluginsChangedCount(0),
68  objectAddedObj(0),
69  aboutToRemoveObjectObj(0)
70  { }
74  QObject *objectAddedObj;
76 public slots:
77  void objectAdded(QObject *obj) { objectAddedCount++; objectAddedObj = obj; }
78  void aboutToRemoveObject(QObject *obj) { aboutToRemoveObjectCount++; aboutToRemoveObjectObj = obj; }
79  void pluginsChanged() { pluginsChangedCount++; }
80 };
81 
82 class MyClass1 : public QObject
83 {
84  Q_OBJECT
85 };
86 
87 class MyClass2 : public QObject
88 {
89  Q_OBJECT
90 };
91 
92 class MyClass11 : public MyClass1
93 {
94  Q_OBJECT
95 };
96 
97 void tst_PluginManager::init()
98 {
99  m_pm = new PluginManager;
100  m_sr = new SignalReceiver;
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()));
104 }
105 
106 void tst_PluginManager::cleanup()
107 {
108  delete m_pm;
109  delete m_sr;
110 }
111 
112 void tst_PluginManager::addRemoveObjects()
113 {
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);
145  delete object1;
146  delete object2;
147 }
148 
149 void tst_PluginManager::getObject()
150 {
151  MyClass2 *object2 = new MyClass2;
152  MyClass11 *object11 = new MyClass11;
153  m_pm->addObject(object2);
154  QCOMPARE(m_pm->getObject<MyClass11>(), (MyClass11*)0);
155  QCOMPARE(m_pm->getObject<MyClass1>(), (MyClass1*)0);
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);
163  delete object2;
164  delete object11;
165 }
166 
167 void tst_PluginManager::getObjects()
168 {
169  MyClass1 *object1 = new MyClass1;
170  MyClass2 *object2 = new MyClass2;
171  MyClass11 *object11 = new MyClass11;
172  m_pm->addObject(object2);
173  QCOMPARE(m_pm->getObjects<MyClass11>(), QList<MyClass11*>());
174  QCOMPARE(m_pm->getObjects<MyClass1>(), QList<MyClass1*>());
175  QCOMPARE(m_pm->getObjects<MyClass2>(), QList<MyClass2*>() << object2);
176  QCOMPARE(m_pm->allObjects(), QList<QObject*>() << object2);
177  m_pm->addObject(object11);
178  QCOMPARE(m_pm->getObjects<MyClass11>(), QList<MyClass11*>() << object11);
179  QCOMPARE(m_pm->getObjects<MyClass1>(), QList<MyClass1*>() << object11);
180  QCOMPARE(m_pm->getObjects<MyClass2>(), QList<MyClass2*>() << object2);
181  QCOMPARE(m_pm->allObjects(), QList<QObject*>() << object2 << object11);
182  m_pm->addObject(object1);
183  QCOMPARE(m_pm->getObjects<MyClass11>(), QList<MyClass11*>() << object11);
184  QCOMPARE(m_pm->getObjects<MyClass1>(), QList<MyClass1*>() << object11 << object1);
185  QCOMPARE(m_pm->getObjects<MyClass2>(), QList<MyClass2*>() << object2);
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);
190  delete object1;
191  delete object2;
192  delete object11;
193 }
194 
195 void tst_PluginManager::plugins()
196 {
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") {
202  bool found = false;
203  foreach (PluginSpec *spec, plugins) {
204  if (spec->name() == expected) {
205  found = true;
206  break;
207  }
208  }
209  QVERIFY2(found, QString("plugin '%1' not found").arg(expected).toLocal8Bit().constData());
210  }
211 }
212 
213 void tst_PluginManager::circularPlugins()
214 {
215  m_pm->setPluginPaths(QStringList() << "circularplugins");
216  m_pm->loadPlugins();
217  foreach (PluginSpec *spec, m_pm->plugins()) {
218  if (spec->name() == "plugin1") {
219  QVERIFY(spec->hasError());
220  QCOMPARE(spec->state(), PluginSpec::Resolved);
221  QCOMPARE(spec->plugin(), (IPlugin*)0);
222  } else if (spec->name() == "plugin2") {
223  QVERIFY(!spec->hasError());
224  QCOMPARE(spec->state(), PluginSpec::Running);
225  } else if (spec->name() == "plugin3") {
226  QVERIFY(spec->hasError());
227  QCOMPARE(spec->state(), PluginSpec::Resolved);
228  QCOMPARE(spec->plugin(), (IPlugin*)0);
229  }
230  }
231 }
232 
233 void tst_PluginManager::correctPlugins1()
234 {
235  m_pm->setFileExtension("spec");
236  m_pm->setPluginPaths(QStringList() << "correctplugins1");
237  m_pm->loadPlugins();
238  foreach (PluginSpec *spec, m_pm->plugins()) {
239  if (spec->hasError())
240  qDebug() << spec->errorString();
241  QVERIFY(!spec->hasError());
242  QCOMPARE(spec->state(), PluginSpec::Running);
243  }
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;
254  }
255  QVERIFY(plugin1running);
256  QVERIFY(plugin2running);
257  QVERIFY(plugin3running);
258 }
259 
260 QTEST_MAIN(tst_PluginManager)
261 
262 #include "tst_pluginmanager.moc"
263 
QObject * objectAddedObj
void aboutToRemoveObject(QObject *obj)
Core plugin system that manages the plugins, their life cycle and their registered objects...
Definition: pluginmanager.h:53
IPlugin * plugin() const
Definition: pluginspec.cpp:341
QString errorString() const
Definition: pluginspec.cpp:319
QObject * aboutToRemoveObjectObj
void objectAdded(QObject *obj)
Contains the information of the plugins xml description file and information about the plugin's curre...
Definition: pluginspec.h:63
Definition: icore.h:39
Base class for all plugins.
Definition: iplugin.h:45