on e0ca4e4 help sign in
unity8/tests/plugins/Unity/Launcher/launchermodeltest.cpp
473 lines 18 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"
27#include "gsettings.h"
29#include <QtTest>
30#include <QDBusInterface>
31#include <QDBusReply>
32#include <QDomDocument>
34// This is a mock, specifically to test the LauncherModel
35class MockApp: public unity::shell::application::ApplicationInfoInterface
37 Q_OBJECT
38public:
39 MockApp(const QString &appId, QObject *parent = 0): ApplicationInfoInterface(appId, parent), m_appId(appId), m_focused(false) { }
40 QString appId() const { return m_appId; }
41 QString name() const { return "mock"; }
42 QString comment() const { return "this is a mock"; }
43 QUrl icon() const { return QUrl(); }
44 ApplicationInfoInterface::Stage stage() const { return ApplicationInfoInterface::MainStage; }
45 ApplicationInfoInterface::State state() const { return ApplicationInfoInterface::Running; }
46 bool focused() const { return m_focused; }
47 QString splashTitle() const override { return QString(); }
48 QUrl splashImage() const override { return QUrl(); }
49 bool splashShowHeader() const override { return true; }
50 QColor splashColor() const override { return QColor(0,0,0,0); }
51 QColor splashColorHeader() const override { return QColor(0,0,0,0); }
52 QColor splashColorFooter() const override { return QColor(0,0,0,0); }
54 // Methods used for mocking (not in the interface)
55 void setFocused(bool focused) { m_focused = focused; Q_EMIT focusedChanged(focused); }
56private:
57 QString m_appId;
58 bool m_focused;
59};
61// This is a mock, specifically to test the LauncherModel
62class MockAppManager: public unity::shell::application::ApplicationManagerInterface
64 Q_OBJECT
65public:
66 MockAppManager(QObject *parent = 0): ApplicationManagerInterface(parent) {}
67 int rowCount(const QModelIndex &) const { return m_list.count(); }
68 QVariant data(const QModelIndex &, int ) const { return QVariant(); }
69 QString focusedApplicationId() const {
70 Q_FOREACH(MockApp *app, m_list) {
71 if (app->focused()) return app->appId();
72 }
73 return QString();
74 }
75 unity::shell::application::ApplicationInfoInterface *get(int index) const { return m_list.at(index); }
76 unity::shell::application::ApplicationInfoInterface *findApplication(const QString &appId) const {
77 Q_FOREACH(MockApp* app, m_list) {
78 if (app->appId() == appId) {
79 return app;
80 }
81 }
82 return nullptr;
83 }
84 unity::shell::application::ApplicationInfoInterface *startApplication(const QString &, const QStringList &) { return nullptr; }
85 bool stopApplication(const QString &appId) {
86 Q_FOREACH(MockApp* app, m_list) {
87 if (app->appId() == appId) {
88 removeApplication(m_list.indexOf(app));
89 return true;
90 }
91 }
92 return false;
93 }
94 bool focusApplication(const QString &appId) {
95 Q_FOREACH(MockApp* app, m_list) {
96 app->setFocused(app->appId() == appId);
97 }
98 Q_EMIT focusedApplicationIdChanged();
99 return true;
100 }
102 void unfocusCurrentApplication() { }
104 void addApplication(MockApp *app) {
105 beginInsertRows(QModelIndex(), count(), count());
106 m_list.append(app);
107 endInsertRows();
108 }
109 void removeApplication(int index) {
110 beginRemoveRows(QModelIndex(), index, index);
111 m_list.takeAt(index)->deleteLater();
112 endRemoveRows();
113 }
114 bool requestFocusApplication(const QString &appId) { Q_UNUSED(appId); return true; }
115 bool suspended() const { return false; }
116 void setSuspended(bool) {}
117 bool forceDashActive() const { return false; }
118 void setForceDashActive(bool) {}
120private:
121 QList<MockApp*> m_list;
122};
124class LauncherModelTest : public QObject
126 Q_OBJECT
128private:
129 LauncherModel *launcherModel;
130 MockAppManager *appManager;
132private Q_SLOTS:
134 void initTestCase() {
135 launcherModel = new LauncherModel(this);
136 QCoreApplication::processEvents(); // to let the model register on DBus
137 QCOMPARE(launcherModel->rowCount(QModelIndex()), 0);
139 appManager = new MockAppManager(this);
140 launcherModel->setApplicationManager(appManager);
141 }
143 // Adding 2 apps to the mock appmanager. Both should appear in the launcher.
144 void init() {
145 appManager->addApplication(new MockApp("abs-icon"));
146 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
148 appManager->addApplication(new MockApp("no-icon"));
149 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
151 launcherModel->m_settings->setStoredApplications(QStringList());
152 }
154 // Removing apps from appmanager and launcher as pinned ones would stick
155 void cleanup() {
156 while (appManager->count() > 0) {
157 appManager->removeApplication(0);
158 }
159 while (launcherModel->rowCount(QModelIndex()) > 0) {
160 launcherModel->requestRemove(launcherModel->get(0)->appId());
161 }
162 }
164 void testMove() {
165 QCOMPARE(launcherModel->get(0)->pinned(), false);
166 QCOMPARE(launcherModel->get(1)->pinned(), false);
168 LauncherItemInterface *item0BeforeMove = launcherModel->get(0);
169 LauncherItemInterface *item1BeforeMove = launcherModel->get(1);
170 launcherModel->move(1, 0);
172 QCOMPARE(item0BeforeMove, launcherModel->get(1));
173 QCOMPARE(item1BeforeMove, launcherModel->get(0));
175 // moved item must be pinned now
176 QCOMPARE(item0BeforeMove->pinned(), false);
177 QCOMPARE(item1BeforeMove->pinned(), true);
178 }
180 void testPinning() {
181 QSignalSpy spy(launcherModel, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)));
182 QCOMPARE(launcherModel->get(0)->pinned(), false);
183 QCOMPARE(launcherModel->get(1)->pinned(), false);
184 launcherModel->pin(launcherModel->get(0)->appId());
185 QCOMPARE(launcherModel->get(0)->pinned(), true);
186 QCOMPARE(launcherModel->get(1)->pinned(), false);
187 QCOMPARE(spy.count() > 0, true);
188 QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
190 // App should be pinned now
191 spy.clear();
192 launcherModel->requestRemove(launcherModel->get(0)->appId());
193 QCOMPARE(launcherModel->get(0)->pinned(), false);
194 QCOMPARE(launcherModel->get(1)->pinned(), false);
195 QCOMPARE(spy.count(), 1);
196 QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
198 // Now that the app is unpinned, nothing should change and the signal should not be emitted
199 spy.clear();
200 launcherModel->requestRemove(launcherModel->get(0)->appId());
201 QCOMPARE(launcherModel->get(0)->pinned(), false);
202 QCOMPARE(launcherModel->get(1)->pinned(), false);
203 QCOMPARE(spy.count(), 0);
204 }
206 void testRemove_data() {
208 QTest::addColumn<bool>("pinned");
209 QTest::addColumn<bool>("running");
211 QTest::newRow("non-pinned, running") << false << true;
212 QTest::newRow("pinned, running") << true << true;
213 QTest::newRow("pinned, non-running") << true << false;
214 }
216 void testRemove() {
217 QFETCH(bool, pinned);
218 QFETCH(bool, running);
220 // In the beginning we always have two items
221 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
223 // pin one if required
224 if (pinned) {
225 launcherModel->pin("abs-icon");
226 }
228 // stop it if required
229 if (!running) {
230 appManager->stopApplication("abs-icon");
231 }
233 // Now remove it
234 launcherModel->requestRemove("abs-icon");
236 if (running) {
237 // both apps are running, both apps must still be here
238 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
240 // Item must be unpinned now
241 int index = launcherModel->findApplication("abs-icon");
242 QCOMPARE(launcherModel->get(index)->pinned(), false);
244 } else if (pinned) {
245 // Item 1 must go away, item 0 is here to stay
246 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
247 }
249 // done our checks. now stop the app if was still running
250 if (running) {
251 appManager->stopApplication("abs-icon");
252 }
254 // It needs to go away in any case now
255 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
256 }
258 void testQuickListPinningRemoving() {
259 // we start with 2 unpinned items
260 QCOMPARE(launcherModel->get(0)->pinned(), false);
261 QCOMPARE(launcherModel->get(1)->pinned(), false);
262 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
264 // find the Pin item in the quicklist
265 QuickListModel *model = qobject_cast<QuickListModel*>(launcherModel->get(0)->quickList());
266 int pinActionIndex = -1;
267 for (int i = 0; i < model->rowCount(QModelIndex()); ++i) {
268 if (model->get(i).actionId() == "pin_item") {
269 pinActionIndex = i;
270 break;
271 }
272 }
273 QVERIFY(pinActionIndex >= 0);
275 // trigger pin item quicklist action => Item must be pinned now.
276 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
277 QCOMPARE(launcherModel->get(0)->pinned(), true);
278 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
280 // quicklist needs to transform to remove item. trigger it and check it item goes away
281 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
282 QCOMPARE(launcherModel->get(0)->pinned(), false);
284 // still needs to be here as the app is still here
285 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
286 // close the app
287 appManager->removeApplication(0);
288 // Now it needs to go away
289 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
290 }
292 void testApplicationFocused() {
293 // all apps unfocused at beginning...
294 QCOMPARE(launcherModel->get(0)->focused(), false);
295 QCOMPARE(launcherModel->get(1)->focused(), false);
297 appManager->focusApplication("abs-icon");
298 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
299 QCOMPARE(launcherModel->get(0)->focused(), true);
300 QCOMPARE(launcherModel->get(1)->focused(), false);
302 appManager->focusApplication("no-icon");
303 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
304 QCOMPARE(launcherModel->get(0)->focused(), false);
305 QCOMPARE(launcherModel->get(1)->focused(), true);
306 }
308 void testClosingApps() {
309 // At the start there are 2 items. Let's pin one.
310 launcherModel->pin("abs-icon");
311 while (appManager->count() > 0) {
312 appManager->removeApplication(0);
313 }
314 // The pinned one needs to stay, the other needs to disappear
315 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
316 QCOMPARE(launcherModel->get(0)->appId(), QLatin1String("abs-icon"));
317 }
319 void testGetUrlForAppId() {
320 QCOMPARE(launcherModel->getUrlForAppId(QString()), QString());
321 QCOMPARE(launcherModel->getUrlForAppId(""), QString());
322 QCOMPARE(launcherModel->getUrlForAppId("no-name"), QString("application:///no-name.desktop"));
323 QCOMPARE(launcherModel->getUrlForAppId("com.test.good"), QString("application:///com.test.good.desktop"));
324 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application"), QString("appid://com.test.good/application/current-user-version"));
325 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application_1.2.3"), QString("appid://com.test.good/application/current-user-version"));
326 }
328 void testIntrospection() {
329 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "org.freedesktop.DBus.Introspectable");
330 QDBusReply<QString> reply = interface.call("Introspect");
331 QStringList nodes = extractNodes(reply.value());
332 QCOMPARE(nodes.count(), launcherModel->rowCount());
334 appManager->addApplication(new MockApp("foobar"));
335 reply = interface.call("Introspect");
336 nodes = extractNodes(reply.value());
337 QCOMPARE(nodes.contains("foobar"), true);
339 appManager->removeApplication(2);
340 reply = interface.call("Introspect");
341 nodes = extractNodes(reply.value());
342 QCOMPARE(nodes.contains("foobar"), false);
343 }
345 QStringList extractNodes(const QString &introspectionXml) {
346 QXmlStreamReader introspectReply(introspectionXml);
348 QStringList ret;
349 while (!introspectReply.atEnd() && !introspectReply.hasError()) {
350 QXmlStreamReader::TokenType token = introspectReply.readNext();
352 if (token == QXmlStreamReader::StartElement) {
353 if (introspectReply.name() == "node" && introspectReply.attributes().count() > 0) {
354 ret << introspectReply.attributes().value("name").toString();
355 }
356 }
357 }
358 return ret;
359 }
361 void testCountEmblems() {
362 // Call GetAll on abs-icon
363 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
364 QDBusReply<QVariantMap> reply = interface.call("GetAll");
365 QVariantMap map = reply.value();
367 // Make sure GetAll returns a map with count and countVisible props
368 QCOMPARE(map.contains("count"), true);
369 QCOMPARE(map.contains("countVisible"), true);
371 // Make sure count is intitilized to 0 and non-visible
372 QCOMPARE(map.value("count").toInt(), 0);
373 QCOMPARE(map.value("countVisible").toBool(), false);
375 // Now make it visible and set it to 55 through D-Bus
376 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", QVariant::fromValue(QDBusVariant(55)));
377 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(true)));
379 // Fetch it again using GetAll
380 reply = interface.call("GetAll");
381 map = reply.value();
383 // Make sure values have changed on the D-Bus interface
384 QCOMPARE(map.value("count").toInt(), 55);
385 QCOMPARE(map.value("countVisible").toBool(), true);
387 // Now the item on the upper side of the API
388 int index = launcherModel->findApplication("abs-icon");
389 QCOMPARE(index >= 0, true);
391 // And make sure values have changed there as well
392 QCOMPARE(launcherModel->get(index)->countVisible(), true);
393 QCOMPARE(launcherModel->get(index)->count(), 55);
394 }
396 void testRefreshAfterDeletedDesktopFiles_data() {
397 QTest::addColumn<bool>("deleted");
398 QTest::newRow("have .desktop files") << false;
399 QTest::newRow("deleted .desktop files") << true;
400 }
402 void testRefreshAfterDeletedDesktopFiles() {
403 QFETCH(bool, deleted);
405 // pin both apps
406 launcherModel->pin("abs-icon");
407 launcherModel->pin("no-icon");
408 // close both apps
409 appManager->removeApplication(0);
410 appManager->removeApplication(0);
412 // "delete" the .desktop files
413 QString oldCurrent = QDir::currentPath();
414 if (deleted) {
415 // In testing mode, the launcher searches the current dir for the sample .desktop file
416 // We can make that fail by changing the current dir
417 QDir::setCurrent("..");
418 }
420 // Call refresh
421 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "com.canonical.Unity.Launcher");
422 QDBusReply<void> reply = interface.call("Refresh");
424 // Make sure the call to Refresh returned without error.
425 QCOMPARE(reply.isValid(), true);
427 QCOMPARE(launcherModel->rowCount(), deleted ? 0 : 2);
429 // Restoring current dir
430 QDir::setCurrent(oldCurrent);
431 }
433 void testSettings() {
434 GSettings *settings = launcherModel->m_settings;
435 QSignalSpy spy(launcherModel, SIGNAL(hint()));
437 // Nothing pinned at startup
438 QCOMPARE(settings->storedApplications().count(), 0);
440 // pin both apps
441 launcherModel->pin("abs-icon");
442 launcherModel->pin("no-icon");
443 QCOMPARE(spy.count(), 0);
445 // Now settings should have 2 apps
446 QCOMPARE(settings->storedApplications().count(), 2);
448 // close both apps
449 appManager->removeApplication(0);
450 appManager->removeApplication(0);
452 // Now settings should have 2 apps
453 QCOMPARE(settings->storedApplications().count(), 2);
455 // Now remove 1 app through the backend, make sure one is still there
456 settings->simulateDConfChanged(QStringList() << "abs-icon");
457 QCOMPARE(settings->storedApplications().count(), 1);
458 QCOMPARE(spy.count(), 1);
460 // Check if it disappeared from the frontend too
461 QCOMPARE(launcherModel->rowCount(), 1);
463 // Add them back but in reverse order
464 settings->simulateDConfChanged(QStringList() << "no-icon" << "abs-icon");
465 QCOMPARE(launcherModel->rowCount(), 2);
466 QCOMPARE(launcherModel->get(0)->appId(), QString("no-icon"));
467 QCOMPARE(launcherModel->get(1)->appId(), QString("abs-icon"));
468 QCOMPARE(spy.count(), 2);
469 }
470};
472QTEST_GUILESS_MAIN(LauncherModelTest)
473#include "launchermodeltest.moc"