on xenial help sign in
unit tests for IndicatorsClient
a4713ffNick Dedekind
plugins/IndicatorsClient/CMakeLists.txt+6−1
@@ -62,10 +62,15 @@ set(IndicatorsClientQM_RESOURCES
62 indicatorsclient.qrc
62 indicatorsclient.qrc
63)
63)
64
64
65−add_library(IndicatorsClientQml MODULE
65+add_library(IndicatorsClientQml SHARED
66 ${IndicatorsClientQML_RCC}
66 ${IndicatorsClientQML_RCC}
67 ${IndicatorsClientQML_SOURCES}
67 ${IndicatorsClientQML_SOURCES}
68)
68)
69+# Because this is an internal support library, we want
70+# to expose all symbols in it. Consider changing this
71+# either to a static library or just using the
72+# files directly in targets.
73+set_target_properties(IndicatorsClientQml PROPERTIES COMPILE_FLAGS -fvisibility=default)
69
74
70target_link_libraries(IndicatorsClientQml
75target_link_libraries(IndicatorsClientQml
71 ${Qt5Core_LIBRARIES}
76 ${Qt5Core_LIBRARIES}
plugins/IndicatorsClient/indicatorsfactory.cpp+14−8
@@ -29,11 +29,11 @@
29
29
30IndicatorsFactory::IndicatorsFactory()
30IndicatorsFactory::IndicatorsFactory()
31{
31{
32− registerItem<IndicatorClientDateTime>("indicator-datetime");
32+ registerIndicator<IndicatorClientDateTime>("indicator-datetime");
33− registerItem<IndicatorClientMessaging>("indicator-messaging");
33+ registerIndicator<IndicatorClientMessaging>("indicator-messaging");
34− registerItem<IndicatorClientNetwork>("indicator-network");
34+ registerIndicator<IndicatorClientNetwork>("indicator-network");
35− registerItem<IndicatorClientPower>("indicator-power");
35+ registerIndicator<IndicatorClientPower>("indicator-power");
36− registerItem<IndicatorClientSound>("indicator-sound");
36+ registerIndicator<IndicatorClientSound>("indicator-sound");
37}
37}
38
38
39IndicatorsFactory::~IndicatorsFactory()
39IndicatorsFactory::~IndicatorsFactory()
@@ -41,10 +41,10 @@ IndicatorsFactory::~IndicatorsFactory()
41 qDeleteAll(m_factoryItems);
41 qDeleteAll(m_factoryItems);
42}
42}
43
43
44−template<class TYPE>
44+IndicatorsFactory& IndicatorsFactory::instance()
45−void IndicatorsFactory::registerItem(const QString& indicator)
46{
45{
47− m_factoryItems[indicator] = new IndicatorFactoryItemTyped<TYPE>();
46+ static IndicatorsFactory factory;
47+ return factory;
48}
48}
49
49
50IndicatorClientInterface::Ptr IndicatorsFactory::create(const QString& indicator, QObject* parent)
50IndicatorClientInterface::Ptr IndicatorsFactory::create(const QString& indicator, QObject* parent)
@@ -52,4 +52,10 @@ IndicatorClientInterface::Ptr IndicatorsFactory::create(const QString& indicator
52 if (!m_factoryItems.contains(indicator))
52 if (!m_factoryItems.contains(indicator))
53 return IndicatorClientInterface::Ptr();
53 return IndicatorClientInterface::Ptr();
54 return m_factoryItems[indicator]->create(parent);
54 return m_factoryItems[indicator]->create(parent);
55+}
56+
57+
58+bool IndicatorsFactory::isRegistered(const QString& indicator)
59+{
60+ return m_factoryItems.contains(indicator);
55}
61}
plugins/IndicatorsClient/indicatorsfactory.h+18−3
@@ -41,18 +41,33 @@ public:
41 IndicatorClientInterface::Ptr create(QObject* parent = 0) { return std::make_shared<TYPE>(parent); }
41 IndicatorClientInterface::Ptr create(QObject* parent = 0) { return std::make_shared<TYPE>(parent); }
42};
42};
43
43
44−class IndicatorsFactory
44+class IndicatorsFactory : public QObject
45{
45{
46+ Q_OBJECT
46public:
47public:
47− IndicatorsFactory();
48 virtual ~IndicatorsFactory();
48 virtual ~IndicatorsFactory();
49
49
50 IndicatorClientInterface::Ptr create(const QString& indicator, QObject* parent = 0);
50 IndicatorClientInterface::Ptr create(const QString& indicator, QObject* parent = 0);
51
51
52 template<class TYPE>
52 template<class TYPE>
53+ void registerIndicator(const QString& indicator)
54+ {
55+ if (m_factoryItems.contains(indicator))
56+ return;
57+ m_factoryItems[indicator] = new IndicatorFactoryItemTyped<TYPE>();
58+ Q_EMIT registered(indicator);
59+ }
60+
61+ bool isRegistered(const QString& indicator);
62+
63+ static IndicatorsFactory& instance();
64+
65+Q_SIGNALS:
53− void registerItem(const QString& indicator);
66+ void registered(const QString& indicator);
54
67
55private:
68private:
69+ IndicatorsFactory();
70+
56 QHash<QString, IndicatorFactoryItem*> m_factoryItems;
71 QHash<QString, IndicatorFactoryItem*> m_factoryItems;
57};
72};
58
73
plugins/IndicatorsClient/indicatorsmanager.cpp+38−7
@@ -43,7 +43,7 @@ public:
43
43
44IndicatorsManager::IndicatorsManager(QObject* parent)
44IndicatorsManager::IndicatorsManager(QObject* parent)
45: QObject(parent)
45: QObject(parent)
46−, m_factory(new IndicatorsFactory())
46+, m_loaded(false)
47{
47{
48}
48}
49
49
@@ -74,6 +74,7 @@ void IndicatorsManager::load()
74
74
75 QObject::connect(m_fsWatcher.data(), SIGNAL(directoryChanged(const QString&)), this, SLOT(onDirectoryChanged(const QString&)));
75 QObject::connect(m_fsWatcher.data(), SIGNAL(directoryChanged(const QString&)), this, SLOT(onDirectoryChanged(const QString&)));
76 QObject::connect(m_fsWatcher.data(), SIGNAL(fileChanged(const QString&)), this, SLOT(onFileChanged(const QString&)));
76 QObject::connect(m_fsWatcher.data(), SIGNAL(fileChanged(const QString&)), this, SLOT(onFileChanged(const QString&)));
77+ setLoaded(true);
77}
78}
78
79
79void IndicatorsManager::onDirectoryChanged(const QString& direcory)
80void IndicatorsManager::onDirectoryChanged(const QString& direcory)
@@ -114,6 +115,13 @@ void IndicatorsManager::load(const QFileInfo& file_info)
114 indicator_settings.beginGroup("Indicator Service");
115 indicator_settings.beginGroup("Indicator Service");
115 QString name = indicator_settings.value("Name").toString();
116 QString name = indicator_settings.value("Name").toString();
116
117
118+ // don't bother loading if it's not registered.
119+ if (!IndicatorsFactory::instance().isRegistered(name))
120+ {
121+ qWarning() << Q_FUNC_INFO << "Invalid plugin name: " << name << " - not registered in factory.";
122+ return;
123+ }
124+
117 auto iter = m_indicatorsData.find(name);
125 auto iter = m_indicatorsData.find(name);
118 if (iter != m_indicatorsData.end())
126 if (iter != m_indicatorsData.end())
119 {
127 {
@@ -147,7 +155,7 @@ void IndicatorsManager::load(const QFileInfo& file_info)
147 file_info != currentData->m_fileInfo)
155 file_info != currentData->m_fileInfo)
148 {
156 {
149 currentData->m_fileInfo = file_info;
157 currentData->m_fileInfo = file_info;
150− Q_EMIT loaded(name);
158+ Q_EMIT indicatorLoaded(name);
151 }
159 }
152 }
160 }
153 else
161 else
@@ -155,7 +163,7 @@ void IndicatorsManager::load(const QFileInfo& file_info)
155 IndicatorData* data = new IndicatorData(name, file_info);
163 IndicatorData* data = new IndicatorData(name, file_info);
156 data->m_verified = true;
164 data->m_verified = true;
157 m_indicatorsData[name]= data;
165 m_indicatorsData[name]= data;
158− Q_EMIT loaded(name);
166+ Q_EMIT indicatorLoaded(name);
159 }
167 }
160}
168}
161
169
@@ -165,11 +173,18 @@ void IndicatorsManager::unload()
165 while(iter.hasNext())
173 while(iter.hasNext())
166 {
174 {
167 iter.next();
175 iter.next();
168− Q_EMIT aboutToBeUnloaded(iter.key());
176+ IndicatorData* data = iter.value();
177+ if (data->m_obj)
178+ {
179+ data->m_obj->shutdown();
180+ }
181+ Q_EMIT indicatorAboutToBeUnloaded(iter.key());
169 }
182 }
170
183
171 qDeleteAll(m_indicatorsData);
184 qDeleteAll(m_indicatorsData);
172 m_indicatorsData.clear();
185 m_indicatorsData.clear();
186+
187+ setLoaded(false);
173}
188}
174
189
175void IndicatorsManager::unload(const QFileInfo& file)
190void IndicatorsManager::unload(const QFileInfo& file)
@@ -184,13 +199,24 @@ void IndicatorsManager::unload(const QFileInfo& file)
184 if (!data->m_verified)
199 if (!data->m_verified)
185 {
200 {
186 QString name = data->m_name;
201 QString name = data->m_name;
187− Q_EMIT aboutToBeUnloaded(name);
202+ Q_EMIT indicatorAboutToBeUnloaded(name);
188
203
189 delete data;
204 delete data;
190 iter.remove();
205 iter.remove();
191 }
206 }
192 }
207 }
193 }
208 }
209+
210+ setLoaded(m_indicatorsData.size() > 0);
211+}
212+
213+void IndicatorsManager::setLoaded(bool loaded)
214+{
215+ if (loaded != m_loaded)
216+ {
217+ m_loaded = loaded;
218+ Q_EMIT loadedChanged(m_loaded);
219+ }
194}
220}
195
221
196void IndicatorsManager::startVerify(const QString& path)
222void IndicatorsManager::startVerify(const QString& path)
@@ -217,7 +243,7 @@ void IndicatorsManager::endVerify(const QString& path)
217 if (!data->m_verified)
243 if (!data->m_verified)
218 {
244 {
219 QString name = data->m_name;
245 QString name = data->m_name;
220− Q_EMIT aboutToBeUnloaded(name);
246+ Q_EMIT indicatorAboutToBeUnloaded(name);
221
247
222 delete data;
248 delete data;
223 iter.remove();
249 iter.remove();
@@ -237,7 +263,7 @@ IndicatorClientInterface::Ptr IndicatorsManager::indicator(const QString& indica
237 if (data->m_obj)
263 if (data->m_obj)
238 return data->m_obj;
264 return data->m_obj;
239
265
240− IndicatorClientInterface::Ptr plugin = m_factory->create(indicator, this);
266+ IndicatorClientInterface::Ptr plugin = IndicatorsFactory::instance().create(indicator, this);
241 if (plugin)
267 if (plugin)
242 {
268 {
243 data->m_obj = plugin;
269 data->m_obj = plugin;
@@ -256,3 +282,8 @@ QList<IndicatorClientInterface::Ptr> IndicatorsManager::indicators()
256 }
282 }
257 return list;
283 return list;
258}
284}
285+
286+bool IndicatorsManager::isLoaded() const
287+{
288+ return m_loaded;
289+}
plugins/IndicatorsClient/indicatorsmanager.h+10−5
@@ -29,11 +29,10 @@
29
29
30#include "indicatorclientinterface.h"
30#include "indicatorclientinterface.h"
31
31
32−class IndicatorsFactory;
33−
34class IndicatorsManager : public QObject
32class IndicatorsManager : public QObject
35{
33{
36 Q_OBJECT
34 Q_OBJECT
35+ Q_PROPERTY(bool loaded READ isLoaded NOTIFY loadedChanged)
37public:
36public:
38 explicit IndicatorsManager(QObject* parent = 0);
37 explicit IndicatorsManager(QObject* parent = 0);
39 ~IndicatorsManager();
38 ~IndicatorsManager();
@@ -45,9 +44,13 @@ public:
45
44
46 QList<IndicatorClientInterface::Ptr> indicators();
45 QList<IndicatorClientInterface::Ptr> indicators();
47
46
47+ bool isLoaded() const;
48+
48Q_SIGNALS:
49Q_SIGNALS:
50+ void loadedChanged(bool);
51+
49− void loaded(const QString& indicator);
52+ void indicatorLoaded(const QString& indicator);
50− void aboutToBeUnloaded(const QString& indicator);
53+ void indicatorAboutToBeUnloaded(const QString& indicator);
51
54
52private Q_SLOTS:
55private Q_SLOTS:
53 void onDirectoryChanged(const QString& direcory);
56 void onDirectoryChanged(const QString& direcory);
@@ -61,10 +64,12 @@ private:
61 void startVerify(const QString& path);
64 void startVerify(const QString& path);
62 void endVerify(const QString& path);
65 void endVerify(const QString& path);
63
66
67+ void setLoaded(bool);
68+
64 class IndicatorData;
69 class IndicatorData;
65 QHash<QString, IndicatorData*> m_indicatorsData;
70 QHash<QString, IndicatorData*> m_indicatorsData;
66 QSharedPointer<QFileSystemWatcher> m_fsWatcher;
71 QSharedPointer<QFileSystemWatcher> m_fsWatcher;
67− IndicatorsFactory* m_factory;
72+ bool m_loaded;
68};
73};
69
74
70#endif // INDICATORS_MANAGER_H
75#endif // INDICATORS_MANAGER_H
plugins/IndicatorsClient/indicatorsmodel.cpp+4−4
@@ -58,8 +58,8 @@ IndicatorsModel::IndicatorsModel(QObject *parent)
58 : QAbstractListModel(parent)
58 : QAbstractListModel(parent)
59{
59{
60 m_manager = new IndicatorsManager(this);
60 m_manager = new IndicatorsManager(this);
61− QObject::connect(m_manager, SIGNAL(loaded(const QString&)), this, SLOT(onIndicatorLoaded(const QString&)));
61+ QObject::connect(m_manager, SIGNAL(indicatorLoaded(const QString&)), this, SLOT(onIndicatorLoaded(const QString&)));
62− QObject::connect(m_manager, SIGNAL(aboutToBeUnloaded(const QString&)), this, SLOT(onIndicatorAboutToBeUnloaded(const QString&)));
62+ QObject::connect(m_manager, SIGNAL(indicatorAboutToBeUnloaded(const QString&)), this, SLOT(onIndicatorAboutToBeUnloaded(const QString&)));
63 m_widgetsMap = new WidgetsMap;
63 m_widgetsMap = new WidgetsMap;
64
64
65 QObject::connect(this, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SIGNAL(countChanged()));
65 QObject::connect(this, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SIGNAL(countChanged()));
@@ -156,7 +156,7 @@ void IndicatorsModel::onIndicatorLoaded(const QString& indicator)
156 QObject* obj = dynamic_cast<QObject*>(plugin.get());
156 QObject* obj = dynamic_cast<QObject*>(plugin.get());
157 if (obj)
157 if (obj)
158 {
158 {
159− QObject::connect(obj, SIGNAL(idChanged(const QString&)), this, SLOT(onIdChanged()));
159+ QObject::connect(obj, SIGNAL(identifierChanged(const QString&)), this, SLOT(onIdentifierChanged()));
160 QObject::connect(obj, SIGNAL(iconChanged(const QUrl&)), this, SLOT(onIconChanged()));
160 QObject::connect(obj, SIGNAL(iconChanged(const QUrl&)), this, SLOT(onIconChanged()));
161 QObject::connect(obj, SIGNAL(titleChanged(const QString&)), this, SLOT(onTitleChanged()));
161 QObject::connect(obj, SIGNAL(titleChanged(const QString&)), this, SLOT(onTitleChanged()));
162 QObject::connect(obj, SIGNAL(labelChanged(const QString&)), this, SLOT(onLabelChanged()));
162 QObject::connect(obj, SIGNAL(labelChanged(const QString&)), this, SLOT(onLabelChanged()));
@@ -192,7 +192,7 @@ void IndicatorsModel::onIndicatorAboutToBeUnloaded(const QString& indicator)
192}
192}
193
193
194/*! \internal */
194/*! \internal */
195−void IndicatorsModel::onIdChanged()
195+void IndicatorsModel::onIdentifierChanged()
196{
196{
197 notifyDataChanged(QObject::sender(), Identifier);
197 notifyDataChanged(QObject::sender(), Identifier);
198}
198}
plugins/IndicatorsClient/indicatorsmodel.h+1−1
@@ -69,7 +69,7 @@ Q_SIGNALS:
69 void countChanged();
69 void countChanged();
70
70
71private Q_SLOTS:
71private Q_SLOTS:
72− void onIdChanged();
72+ void onIdentifierChanged();
73 void onIconChanged();
73 void onIconChanged();
74 void onTitleChanged();
74 void onTitleChanged();
75 void onLabelChanged();
75 void onLabelChanged();
tests/data/unity/indicators/indicator-fake1.indicatornew file+4
@@ -0,0 +1,4 @@
1+[Indicator Service]
2+Name=indicator-fake1
3+DBusName=com.canonical.indicator.fake1
4+ObjectPath=/com/canonical/indicator/fake1
tests/data/unity/indicators/indicator-fake2.indicatornew file+4
@@ -0,0 +1,4 @@
1+[Indicator Service]
2+Name=indicator-fake2
3+DBusName=com.canonical.indicator.fake2
4+ObjectPath=/com/canonical/indicator/fake2
tests/data/unity/indicators/indicator-fake3.indicatornew file+4
@@ -0,0 +1,4 @@
1+[Indicator Service]
2+Name=indicator-fake3
3+DBusName=com.canonical.indicator.fake3
4+ObjectPath=/com/canonical/indicator/fake3
tests/plugins/CMakeLists.txt+1
@@ -1,2 +1,3 @@
1+add_subdirectory(IndicatorsClient)
1add_subdirectory(Utils)
2add_subdirectory(Utils)
2add_subdirectory(Ubuntu)
3add_subdirectory(Ubuntu)
tests/plugins/IndicatorsClient/CMakeLists.txtnew file+28
@@ -0,0 +1,28 @@
1+include_directories(
2+ ${CMAKE_CURRENT_BINARY_DIR}
3+ ${CMAKE_CURRENT_SOURCE_DIR}/../../../plugins/IndicatorsClient
4+ ${CMAKE_CURRENT_SOURCE_DIR}/../../../plugins/IndicatorsClient/indicators/common
5+ )
6+
7+macro(run_tests)
8+ set(_test_list "")
9+ foreach(_test ${ARGN})
10+ set(testCommand ${CMAKE_CURRENT_BINARY_DIR}/${_test}Exec
11+ -o ${CMAKE_BINARY_DIR}/${_test}.xml,xunitxml
12+ -o -,txt)
13+ add_test(NAME ${_test} COMMAND ${testCommand})
14+ add_custom_target(${_test} ${testCommand})
15+ add_executable(${_test}Exec ${_test}.cpp fakeindicator.h
16+ )
17+ qt5_use_modules(${_test}Exec Test Core Qml)
18+ target_link_libraries(${_test}Exec
19+ IndicatorsClientQml
20+ )
21+ set(_test_list "${_test_list};${_test}")
22+ endforeach()
23+endmacro(run_tests)
24+
25+run_tests(
26+ indicatorsmanagertest
27+ indicatorsmodeltest
28+ )
tests/plugins/IndicatorsClient/fakeindicator.hnew file+71
@@ -0,0 +1,71 @@
1+/*
2+ * Copyright 2013 Canonical Ltd.
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU Lesser General Public License as published by
6+ * the Free Software Foundation; version 3.
7+ *
8+ * This program is distributed in the hope that it will be useful,
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU Lesser General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU Lesser General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ *
16+ * Authors:
17+ * Renato Araujo Oliveira Filho <renato@canonical.com>
18+ * Nick Dedekind <nick.dedekind@canonical.com>
19+ */
20+
21+#ifndef FAKE_INDICATORCLIENT_H
22+#define FAKE_INDICATORCLIENT_H
23+
24+#include "indicatorclient_common.h"
25+
26+class FakeIndicatorClient : public IndicatorClientCommon
27+{
28+ Q_OBJECT
29+ Q_PROPERTY(int initializedCount READ initializedCount)
30+public:
31+ FakeIndicatorClient(QObject *parent=0)
32+ : IndicatorClientCommon(parent)
33+ , m_initializedCount(0)
34+ {
35+ }
36+ void init(const QSettings& settings)
37+ {
38+ IndicatorClientCommon::init(settings);
39+ m_initializedCount++;
40+ }
41+ int initializedCount() const { return m_initializedCount; }
42+ void shutdown() { m_initializedCount--; }
43+
44+ IndicatorClientInterface::WidgetsMap widgets()
45+ {
46+ WidgetsMap w;
47+ w.insert("com.canonical.button", QUrl("Button.qml"));
48+ w.insert("com.canonical.label", QUrl("Label.qml"));
49+ return w;
50+ }
51+
52+ private:
53+ int m_initializedCount;
54+};
55+
56+
57+#define FAKE_INDICATOR(number, title, icon) \
58+ class FakeIndicatorClient##number : public FakeIndicatorClient \
59+ { \
60+ public: \
61+ FakeIndicatorClient##number(QObject *parent=0) \
62+ : FakeIndicatorClient(parent) \
63+ { \
64+ setTitle(title); \
65+ setPriority(number); \
66+ setIcon(QUrl(icon)); \
67+ } \
68+ };
69+
70+
71+#endif
tests/plugins/IndicatorsClient/indicatorsmanagertest.cppnew file+152
@@ -0,0 +1,152 @@
1+/*
2+ * Copyright 2012 Canonical Ltd.
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU Lesser General Public License as published by
6+ * the Free Software Foundation; version 3.
7+ *
8+ * This program is distributed in the hope that it will be useful,
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU Lesser General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU Lesser General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ *
16+ * Authors:
17+ * Renato Araujo Oliveira Filho <renato@canonical.com>
18+ */
19+
20+#include "fakeindicator.h"
21+#include "indicatorsmanager.h"
22+#include "indicatorsfactory.h"
23+#include "paths.h"
24+
25+#include <QtTest>
26+#include <QDebug>
27+
28+FAKE_INDICATOR(1, "Fake Title 1", "fake-indicator-1")
29+FAKE_INDICATOR(2, "Fake Title 2", "fake-indicator-2")
30+
31+class IndicatorsManagerTest : public QObject
32+{
33+ Q_OBJECT
34+private Q_SLOTS:
35+
36+ void initTestCase() {
37+ setenv("XDG_DATA_DIRS", (sourceDirectory() + "/tests/data").toLatin1().data(), 1);
38+
39+ IndicatorsFactory::instance().registerIndicator<FakeIndicatorClient1>("indicator-fake1");
40+ IndicatorsFactory::instance().registerIndicator<FakeIndicatorClient2>("indicator-fake2");
41+ }
42+
43+ /*
44+ * Test if all plugins is loaded correct
45+ */
46+ void testLoad()
47+ {
48+
49+ IndicatorsManager manager;
50+
51+ QVERIFY(!manager.isLoaded());
52+ QCOMPARE(manager.indicators().count(), 0);
53+
54+ manager.load();
55+
56+ QVERIFY(manager.isLoaded());
57+ QCOMPARE(manager.indicators().count(), 2);
58+
59+ manager.unload();
60+
61+ QVERIFY(!manager.isLoaded());
62+ QCOMPARE(manager.indicators().count(), 0);
63+ }
64+
65+ /*
66+ * Test the creation & initialising of the indicator data
67+ */
68+ void testPluginInterface()
69+ {
70+ IndicatorsManager manager;
71+ manager.load();
72+
73+ IndicatorClientInterface::Ptr indicator = manager.indicator("indicator-fake1");
74+ QVERIFY(indicator.get());
75+
76+ QCOMPARE(indicator->identifier(), QString("indicator-fake1"));
77+ QCOMPARE(indicator->icon(), QUrl("image://gicon/fake-indicator-1"));
78+ QCOMPARE(indicator->title(), QString("Fake Title 1"));
79+ QCOMPARE(indicator->label(), QString());
80+ QCOMPARE(indicator->description(), QString());
81+ QCOMPARE(indicator->priority(), 1);
82+
83+ IndicatorClientInterface::WidgetsMap widgets = indicator->widgets();
84+ QCOMPARE(widgets.count(), 2);
85+ QCOMPARE(widgets["com.canonical.button"], QUrl("Button.qml"));
86+ QCOMPARE(widgets["com.canonical.label"], QUrl("Label.qml"));
87+
88+ // Check that the initial properties have been set.
89+ IndicatorClientInterface::PropertiesMap props = indicator->initialProperties();
90+ QCOMPARE(props.count(), 4);
91+ QCOMPARE(props["title"].toString(), QString("Fake Title 1"));
92+ QCOMPARE(props["busType"].toInt(), 1);
93+ QCOMPARE(props["busName"].toString(), QString("com.canonical.indicator.fake1"));
94+ QCOMPARE(props["objectPath"].toString(), QString("/com/canonical/indicator/fake1"));
95+ }
96+
97+ /*
98+ * Test if a new plugin object is create for each different plugin
99+ */
100+ void testPluginInstance()
101+ {
102+ IndicatorsManager manager;
103+ manager.load();
104+
105+ IndicatorClientInterface::Ptr i0 = manager.indicator("indicator-fake1");
106+ IndicatorClientInterface::Ptr i1 = manager.indicator("indicator-fake1");
107+ QVERIFY(i0?true:false);
108+ QVERIFY(i1?true:false);
109+ QCOMPARE(i0, i1);
110+
111+ IndicatorClientInterface::Ptr i2 = manager.indicator("indicator-fake2");
112+ QVERIFY(i2?true:false);
113+ QVERIFY(i2 != i1);
114+ QVERIFY(i2 != i1);
115+ }
116+
117+ /*
118+ * Test if a new plugin init function is called only once
119+ */
120+ void testPluginInitAndShutdown()
121+ {
122+ IndicatorsManager manager;
123+ manager.load();
124+
125+ QPointer<QObject> pi0;
126+ QPointer<QObject> pi1;
127+ {
128+ IndicatorClientInterface::Ptr i0 = manager.indicator("indicator-fake1");
129+ pi0 = dynamic_cast<QObject*>(i0.get());
130+ QCOMPARE(pi0->property("initializedCount").toInt(), 1);
131+
132+ IndicatorClientInterface::Ptr i1 = manager.indicator("indicator-fake1");
133+ pi1 = dynamic_cast<QObject*>(i1.get());
134+ QCOMPARE(pi0->property("initializedCount").toInt(), 1);
135+ QCOMPARE(pi1->property("initializedCount").toInt(), 1);
136+
137+ manager.unload();
138+ // still alive while we have the shared pointers.
139+ QVERIFY(!pi0.isNull());
140+ QVERIFY(!pi1.isNull());
141+
142+ }
143+
144+ // no more smart pointers. should have been released.
145+ QVERIFY(pi0.isNull());
146+ QVERIFY(pi1.isNull());
147+ }
148+};
149+
150+QTEST_MAIN(IndicatorsManagerTest)
151+#include "indicatorsmanagertest.moc"
152+
tests/plugins/IndicatorsClient/indicatorsmodeltest.cppnew file+96
@@ -0,0 +1,96 @@
1+/*
2+ * Copyright 2012 Canonical Ltd.
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU Lesser General Public License as published by
6+ * the Free Software Foundation; version 3.
7+ *
8+ * This program is distributed in the hope that it will be useful,
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU Lesser General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU Lesser General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ *
16+ * Authors:
17+ * Renato Araujo Oliveira Filho <renato@canonical.com>
18+ */
19+
20+#include "fakeindicator.h"
21+#include "indicatorsmodel.h"
22+#include "indicatorsfactory.h"
23+#include "paths.h"
24+
25+#include <QtTest>
26+#include <QDebug>
27+
28+FAKE_INDICATOR(1, "Fake Title 1", "fake-indicator-1")
29+FAKE_INDICATOR(2, "Fake Title 2", "fake-indicator-2")
30+
31+class IndicatorsModelTest : public QObject
32+{
33+ Q_OBJECT
34+private Q_SLOTS:
35+
36+ void initTestCase() {
37+ setenv("XDG_DATA_DIRS", (sourceDirectory() + "/tests/data").toLatin1().data(), 1);
38+
39+ IndicatorsFactory::instance().registerIndicator<FakeIndicatorClient1>("indicator-fake1");
40+ IndicatorsFactory::instance().registerIndicator<FakeIndicatorClient2>("indicator-fake2");
41+ }
42+
43+ /*
44+ * Test if all plugins is loaded correct
45+ */
46+ void testLoad()
47+ {
48+ IndicatorsModel model;
49+
50+ QCOMPARE(model.property("count").toInt(), 0);
51+
52+ model.load();
53+
54+ QCOMPARE(model.property("count").toInt(), 2);
55+
56+ model.unload();
57+
58+ QCOMPARE(model.property("count").toInt(), 0);
59+
60+ }
61+
62+ /*
63+ * Test if the plugin data was loaded correct
64+ */
65+ void testDataOrder()
66+ {
67+ IndicatorsModel model;
68+ model.load();
69+
70+ QVariantMap data = model.get(0);
71+ QCOMPARE(data["title"].toString(), QString("Fake Title 1"));
72+ QCOMPARE(data["label"].toString(), QString());
73+ QCOMPARE(data["iconSource"].value<QUrl>(), QUrl("image://gicon/fake-indicator-1"));
74+ QCOMPARE(data["description"].toString(), QString());
75+ // There is no QML context
76+ QVERIFY(!data["component"].value<QObject*>());
77+ QCOMPARE(data["isValid"].toBool(), true);
78+
79+ data = model.get(1);
80+ QCOMPARE(data["title"].toString(), QString("Fake Title 2"));
81+ QCOMPARE(data["label"].toString(), QString());
82+ QCOMPARE(data["iconSource"].value<QUrl>(), QUrl("image://gicon/fake-indicator-2"));
83+ QCOMPARE(data["description"].toString(), QString());
84+ // There is no QML context
85+ QVERIFY(!data["component"].value<QObject*>());
86+ QCOMPARE(data["isValid"].toBool(), true);
87+
88+ data = model.get(3);
89+ QVERIFY(data.isEmpty());
90+ }
91+};
92+
93+QTEST_MAIN(IndicatorsModelTest)
94+
95+#include "indicatorsmodeltest.moc"
96+