@@ -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+