on 72c4cda help sign in
unity8/tests/plugins/Unity/Launcher/launchermodeltest.cpp
385 lines 15 kB history raw
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 * Michael Zanetti <michael.zanetti@canonical.com>
18 */
20// unity-api
21#include <unity/shell/launcher/LauncherModelInterface.h>
22#include <unity/shell/application/ApplicationInfoInterface.h>
24#include "launcheritem.h"
25#include "launchermodel.h"
26#include "dbusinterface.h"
28#include <QtTest>
29#include <QDBusInterface>
30#include <QDBusReply>
31#include <QDomDocument>
33// This is a mock, specifically to test the LauncherModel
34class MockApp: public unity::shell::application::ApplicationInfoInterface
36 Q_OBJECT
37public:
38 MockApp(const QString &appId, QObject *parent = 0): ApplicationInfoInterface(appId, parent), m_appId(appId), m_focused(false) { }
39 QString appId() const { return m_appId; }
40 QString name() const { return "mock"; }
41 QString comment() const { return "this is a mock"; }
42 QUrl icon() const { return QUrl(); }
43 ApplicationInfoInterface::Stage stage() const { return ApplicationInfoInterface::MainStage; }
44 ApplicationInfoInterface::State state() const { return ApplicationInfoInterface::Running; }
45 bool focused() const { return m_focused; }
46 QString splashTitle() const override { return QString(); }
47 QUrl splashImage() const override { return QUrl(); }
48 bool splashShowHeader() const override { return true; }
49 QColor splashColor() const override { return QColor(0,0,0,0); }
50 QColor splashColorHeader() const override { return QColor(0,0,0,0); }
51 QColor splashColorFooter() const override { return QColor(0,0,0,0); }
53 // Methods used for mocking (not in the interface)
54 void setFocused(bool focused) { m_focused = focused; Q_EMIT focusedChanged(focused); }
55private:
56 QString m_appId;
57 bool m_focused;
58};
60// This is a mock, specifically to test the LauncherModel
61class MockAppManager: public unity::shell::application::ApplicationManagerInterface
63 Q_OBJECT
64public:
65 MockAppManager(QObject *parent = 0): ApplicationManagerInterface(parent) {}
66 int rowCount(const QModelIndex &) const { return m_list.count(); }
67 QVariant data(const QModelIndex &, int ) const { return QVariant(); }
68 QString focusedApplicationId() const {
69 Q_FOREACH(MockApp *app, m_list) {
70 if (app->focused()) return app->appId();
71 }
72 return QString();
73 }
74 unity::shell::application::ApplicationInfoInterface *get(int index) const { return m_list.at(index); }
75 unity::shell::application::ApplicationInfoInterface *findApplication(const QString &appId) const {
76 Q_FOREACH(MockApp* app, m_list) {
77 if (app->appId() == appId) {
78 return app;
79 }
80 }
81 return nullptr;
82 }
83 unity::shell::application::ApplicationInfoInterface *startApplication(const QString &, const QStringList &) { return nullptr; }
84 bool stopApplication(const QString &) { return false; }
85 bool focusApplication(const QString &appId) {
86 Q_FOREACH(MockApp* app, m_list) {
87 app->setFocused(app->appId() == appId);
88 }
89 Q_EMIT focusedApplicationIdChanged();
90 return true;
91 }
93 void unfocusCurrentApplication() { }
95 void addApplication(MockApp *app) {
96 beginInsertRows(QModelIndex(), count(), count());
97 m_list.append(app);
98 endInsertRows();
99 }
100 void removeApplication(int index) {
101 beginRemoveRows(QModelIndex(), index, index);
102 m_list.takeAt(index)->deleteLater();
103 endRemoveRows();
104 }
105 bool requestFocusApplication(const QString &appId) { Q_UNUSED(appId); return true; }
106 bool suspended() const { return false; }
107 void setSuspended(bool) {}
108 bool forceDashActive() const { return false; }
109 void setForceDashActive(bool) {}
111private:
112 QList<MockApp*> m_list;
113};
115class LauncherModelTest : public QObject
117 Q_OBJECT
119private:
120 LauncherModel *launcherModel;
121 MockAppManager *appManager;
123private Q_SLOTS:
125 void initTestCase() {
126 launcherModel = new LauncherModel(this);
127 QCoreApplication::processEvents(); // to let the model register on DBus
128 QCOMPARE(launcherModel->rowCount(QModelIndex()), 0);
130 appManager = new MockAppManager(this);
131 launcherModel->setApplicationManager(appManager);
132 }
134 // Adding 2 apps to the mock appmanager. Both should appear in the launcher.
135 void init() {
136 appManager->addApplication(new MockApp("abs-icon"));
137 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
139 appManager->addApplication(new MockApp("no-icon"));
140 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
141 }
143 // Removing apps from appmanager and launcher as pinned ones would stick
144 void cleanup() {
145 while (appManager->count() > 0) {
146 appManager->removeApplication(0);
147 }
148 while (launcherModel->rowCount(QModelIndex()) > 0) {
149 launcherModel->requestRemove(launcherModel->get(0)->appId());
150 }
151 }
153 void testMove() {
154 QCOMPARE(launcherModel->get(0)->pinned(), false);
155 QCOMPARE(launcherModel->get(1)->pinned(), false);
157 LauncherItemInterface *item0BeforeMove = launcherModel->get(0);
158 LauncherItemInterface *item1BeforeMove = launcherModel->get(1);
159 launcherModel->move(1, 0);
161 QCOMPARE(item0BeforeMove, launcherModel->get(1));
162 QCOMPARE(item1BeforeMove, launcherModel->get(0));
164 // moved item must be pinned now
165 QCOMPARE(item0BeforeMove->pinned(), false);
166 QCOMPARE(item1BeforeMove->pinned(), true);
167 }
169 void testPinning() {
170 QSignalSpy spy(launcherModel, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)));
171 QCOMPARE(launcherModel->get(0)->pinned(), false);
172 QCOMPARE(launcherModel->get(1)->pinned(), false);
173 launcherModel->pin(launcherModel->get(0)->appId());
174 QCOMPARE(launcherModel->get(0)->pinned(), true);
175 QCOMPARE(launcherModel->get(1)->pinned(), false);
176 QCOMPARE(spy.count(), 1);
177 QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
179 launcherModel->requestRemove(launcherModel->get(0)->appId());
180 QCOMPARE(launcherModel->get(0)->pinned(), false);
181 QCOMPARE(launcherModel->get(1)->pinned(), false);
182 QCOMPARE(spy.count(), 2);
183 QCOMPARE(spy.at(1).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
184 }
186 void testRemove_data() {
188 QTest::addColumn<bool>("pinned");
189 QTest::addColumn<bool>("running");
191 QTest::newRow("non-pinned, running") << false << true;
192 QTest::newRow("pinned, running") << true << false;
193 QTest::newRow("pinned, non-running") << true << false;
194 }
196 void testRemove() {
197 QFETCH(bool, pinned);
198 QFETCH(bool, running);
200 // In the beginning we always have two items
201 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
203 // pin one if required
204 if (pinned) {
205 launcherModel->pin(launcherModel->get(1)->appId());
206 }
208 // stop it if required
209 if (!running) {
210 appManager->removeApplication(1);
211 }
213 // Now remove it
214 launcherModel->requestRemove(launcherModel->get(1)->appId());
216 if (running) {
217 // both apps are running, both apps must still be here
218 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
220 // Item must be unpinned now
221 QCOMPARE(launcherModel->get(1)->pinned(), false);
223 } else if (pinned) {
224 // Item 1 must go away, item 0 is here to stay
225 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
226 }
228 // done our checks. now stop the app if was still running
229 if (running) {
230 appManager->removeApplication(1);
231 }
233 // It needs to go away in any case now
234 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
235 }
237 void testQuickListPinningRemoving() {
238 // we start with 2 unpinned items
239 QCOMPARE(launcherModel->get(0)->pinned(), false);
240 QCOMPARE(launcherModel->get(1)->pinned(), false);
241 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
243 // find the Pin item in the quicklist
244 QuickListModel *model = qobject_cast<QuickListModel*>(launcherModel->get(0)->quickList());
245 int pinActionIndex = -1;
246 for (int i = 0; i < model->rowCount(QModelIndex()); ++i) {
247 if (model->get(i).actionId() == "pin_item") {
248 pinActionIndex = i;
249 break;
250 }
251 }
252 QVERIFY(pinActionIndex >= 0);
254 // trigger pin item quicklist action => Item must be pinned now.
255 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
256 QCOMPARE(launcherModel->get(0)->pinned(), true);
257 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
259 // quicklist needs to transform to remove item. trigger it and check it item goes away
260 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
261 QCOMPARE(launcherModel->get(0)->pinned(), false);
263 // still needs to be here as the app is still here
264 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
265 // close the app
266 appManager->removeApplication(0);
267 // Now it needs to go away
268 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
269 }
271 void testApplicationFocused() {
272 // all apps unfocused at beginning...
273 QCOMPARE(launcherModel->get(0)->focused(), false);
274 QCOMPARE(launcherModel->get(1)->focused(), false);
276 appManager->focusApplication("abs-icon");
277 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
278 QCOMPARE(launcherModel->get(0)->focused(), true);
279 QCOMPARE(launcherModel->get(1)->focused(), false);
281 appManager->focusApplication("no-icon");
282 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
283 QCOMPARE(launcherModel->get(0)->focused(), false);
284 QCOMPARE(launcherModel->get(1)->focused(), true);
285 }
287 void testClosingApps() {
288 // At the start there are 2 items. Let's pin one.
289 launcherModel->pin("abs-icon");
290 while (appManager->count() > 0) {
291 appManager->removeApplication(0);
292 }
293 // The pinned one needs to stay, the other needs to disappear
294 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
295 QCOMPARE(launcherModel->get(0)->appId(), QLatin1String("abs-icon"));
296 }
298 void testGetUrlForAppId() {
299 QCOMPARE(launcherModel->getUrlForAppId(QString()), QString());
300 QCOMPARE(launcherModel->getUrlForAppId(""), QString());
301 QCOMPARE(launcherModel->getUrlForAppId("no-name"), QString("application:///no-name.desktop"));
302 QCOMPARE(launcherModel->getUrlForAppId("com.test.good"), QString("application:///com.test.good.desktop"));
303 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application"), QString("appid://com.test.good/application/current-user-version"));
304 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application_1.2.3"), QString("appid://com.test.good/application/current-user-version"));
305 }
307 void testIntrospection() {
308 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "org.freedesktop.DBus.Introspectable");
309 QDBusReply<QString> reply = interface.call("Introspect");
310 QStringList nodes = extractNodes(reply.value());
311 QCOMPARE(nodes.count(), launcherModel->rowCount());
313 appManager->addApplication(new MockApp("foobar"));
314 reply = interface.call("Introspect");
315 nodes = extractNodes(reply.value());
316 QCOMPARE(nodes.contains("foobar"), true);
318 appManager->removeApplication(2);
319 reply = interface.call("Introspect");
320 nodes = extractNodes(reply.value());
321 QCOMPARE(nodes.contains("foobar"), false);
322 }
324 QStringList extractNodes(const QString &introspectionXml) {
325 QXmlStreamReader introspectReply(introspectionXml);
327 QStringList ret;
328 while (!introspectReply.atEnd() && !introspectReply.hasError()) {
329 QXmlStreamReader::TokenType token = introspectReply.readNext();
331 if (token == QXmlStreamReader::StartElement) {
332 if (introspectReply.name() == "node" && introspectReply.attributes().count() > 0) {
333 ret << introspectReply.attributes().value("name").toString();
334 }
335 }
336 }
337 return ret;
338 }
340 void testCountEmblems() {
341 // Call GetAll on abs-icon
342 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
343 QDBusReply<QVariantMap> reply = interface.call("GetAll");
344 QVariantMap map = reply.value();
346 // Make sure GetAll returns a map with count and countVisible props
347 QCOMPARE(map.contains("count"), true);
348 QCOMPARE(map.contains("countVisible"), true);
350 // Make sure count is intitilized to 0 and non-visible
351 QCOMPARE(map.value("count").toInt(), 0);
352 QCOMPARE(map.value("countVisible").toBool(), false);
354 // Now make it visible and set it to 55 through D-Bus
355 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", 55);
356 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", true);
358 // Fetch it again using GetAll
359 reply = interface.call("GetAll");
360 map = reply.value();
362 // Make sure values have changed on the D-Bus interface
363 QCOMPARE(map.value("count").toInt(), 55);
364 QCOMPARE(map.value("countVisible").toBool(), true);
366 // Now the item on the upper side of the API
367 int index = launcherModel->findApplication("abs-icon");
368 QCOMPARE(index >= 0, true);
370 // And make sure values have changed there as well
371 QCOMPARE(launcherModel->get(index)->countVisible(), true);
372 QCOMPARE(launcherModel->get(index)->count(), 55);
373 }
375 void testRefresh() {
376 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "com.canonical.Unity.Launcher");
377 QDBusReply<void> reply = interface.call("Refresh");
379 // Make sure the call to Refresh returned without error.
380 QCOMPARE(reply.isValid(), true);
381 }
382};
384QTEST_GUILESS_MAIN(LauncherModelTest)
385#include "launchermodeltest.moc"