on 53bcb44 help sign in
unity8/tests/plugins/Unity/Launcher/launchermodeltest.cpp
594 lines 24 kB history raw
1/*
2 * Copyright 2013-2014 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"
28#include "asadapter.h"
29#include "AccountsServiceDBusAdaptor.h"
31#include <QtTest>
32#include <QDBusInterface>
33#include <QDBusReply>
34#include <QDomDocument>
36// This is a mock, specifically to test the LauncherModel
37class MockApp: public unity::shell::application::ApplicationInfoInterface
39 Q_OBJECT
40public:
41 MockApp(const QString &appId, QObject *parent = 0): ApplicationInfoInterface(appId, parent), m_appId(appId), m_focused(false) { }
42 QString appId() const { return m_appId; }
43 QString name() const { return "mock"; }
44 QString comment() const { return "this is a mock"; }
45 QUrl icon() const { return QUrl(); }
46 ApplicationInfoInterface::Stage stage() const { return ApplicationInfoInterface::MainStage; }
47 ApplicationInfoInterface::State state() const { return ApplicationInfoInterface::Running; }
48 bool focused() const { return m_focused; }
49 QString splashTitle() const override { return QString(); }
50 QUrl splashImage() const override { return QUrl(); }
51 bool splashShowHeader() const override { return true; }
52 QColor splashColor() const override { return QColor(0,0,0,0); }
53 QColor splashColorHeader() const override { return QColor(0,0,0,0); }
54 QColor splashColorFooter() const override { return QColor(0,0,0,0); }
56 // Methods used for mocking (not in the interface)
57 void setFocused(bool focused) { m_focused = focused; Q_EMIT focusedChanged(focused); }
58private:
59 QString m_appId;
60 bool m_focused;
61};
63// This is a mock, specifically to test the LauncherModel
64class MockAppManager: public unity::shell::application::ApplicationManagerInterface
66 Q_OBJECT
67public:
68 MockAppManager(QObject *parent = 0): ApplicationManagerInterface(parent) {}
69 int rowCount(const QModelIndex &) const { return m_list.count(); }
70 QVariant data(const QModelIndex &, int ) const { return QVariant(); }
71 QString focusedApplicationId() const {
72 Q_FOREACH(MockApp *app, m_list) {
73 if (app->focused()) return app->appId();
74 }
75 return QString();
76 }
77 unity::shell::application::ApplicationInfoInterface *get(int index) const { return m_list.at(index); }
78 unity::shell::application::ApplicationInfoInterface *findApplication(const QString &appId) const {
79 Q_FOREACH(MockApp* app, m_list) {
80 if (app->appId() == appId) {
81 return app;
82 }
83 }
84 return nullptr;
85 }
86 unity::shell::application::ApplicationInfoInterface *startApplication(const QString &, const QStringList &) { return nullptr; }
87 bool stopApplication(const QString &appId) {
88 Q_FOREACH(MockApp* app, m_list) {
89 if (app->appId() == appId) {
90 removeApplication(m_list.indexOf(app));
91 return true;
92 }
93 }
94 return false;
95 }
96 bool focusApplication(const QString &appId) {
97 Q_FOREACH(MockApp* app, m_list) {
98 app->setFocused(app->appId() == appId);
99 }
100 Q_EMIT focusedApplicationIdChanged();
101 return true;
102 }
104 void unfocusCurrentApplication() { }
106 void addApplication(MockApp *app) {
107 beginInsertRows(QModelIndex(), count(), count());
108 m_list.append(app);
109 endInsertRows();
110 }
111 void removeApplication(int index) {
112 beginRemoveRows(QModelIndex(), index, index);
113 m_list.takeAt(index)->deleteLater();
114 endRemoveRows();
115 }
116 bool requestFocusApplication(const QString &appId) { Q_UNUSED(appId); return true; }
117 bool suspended() const { return false; }
118 void setSuspended(bool) {}
119 bool forceDashActive() const { return false; }
120 void setForceDashActive(bool) {}
122private:
123 QList<MockApp*> m_list;
124};
126class LauncherModelTest : public QObject
128 Q_OBJECT
130private:
131 LauncherModel *launcherModel;
132 MockAppManager *appManager;
134 QList<QVariantMap> getASConfig() {
135 AccountsServiceDBusAdaptor *as = launcherModel->m_asAdapter->m_accounts;
136 return as->getUserProperty("", "", "LauncherItems").value<QList<QVariantMap>>();
137 }
139private Q_SLOTS:
141 void initTestCase() {
142 launcherModel = new LauncherModel(this);
143 QCoreApplication::processEvents(); // to let the model register on DBus
144 QCOMPARE(launcherModel->rowCount(QModelIndex()), 0);
146 appManager = new MockAppManager(this);
147 launcherModel->setApplicationManager(appManager);
148 }
150 // Adding 2 apps to the mock appmanager. Both should appear in the launcher.
151 void init() {
152 appManager->addApplication(new MockApp("abs-icon"));
153 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
155 appManager->addApplication(new MockApp("no-icon"));
156 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
158 launcherModel->m_settings->setStoredApplications(QStringList());
159 }
161 // Removing apps from appmanager and launcher as pinned ones would stick
162 void cleanup() {
163 while (appManager->count() > 0) {
164 appManager->removeApplication(0);
165 }
166 while (launcherModel->rowCount(QModelIndex()) > 0) {
167 launcherModel->requestRemove(launcherModel->get(0)->appId());
168 }
169 }
171 void testMove() {
172 QCOMPARE(launcherModel->get(0)->pinned(), false);
173 QCOMPARE(launcherModel->get(1)->pinned(), false);
175 LauncherItemInterface *item0BeforeMove = launcherModel->get(0);
176 LauncherItemInterface *item1BeforeMove = launcherModel->get(1);
177 launcherModel->move(1, 0);
179 QCOMPARE(item0BeforeMove, launcherModel->get(1));
180 QCOMPARE(item1BeforeMove, launcherModel->get(0));
182 // moved item must be pinned now
183 QCOMPARE(item0BeforeMove->pinned(), false);
184 QCOMPARE(item1BeforeMove->pinned(), true);
185 }
187 void testPinning() {
188 QSignalSpy spy(launcherModel, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)));
189 QCOMPARE(launcherModel->get(0)->pinned(), false);
190 QCOMPARE(launcherModel->get(1)->pinned(), false);
191 launcherModel->pin(launcherModel->get(0)->appId());
192 QCOMPARE(launcherModel->get(0)->pinned(), true);
193 QCOMPARE(launcherModel->get(1)->pinned(), false);
194 QCOMPARE(spy.count() > 0, true);
195 QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
197 // App should be pinned now
198 spy.clear();
199 launcherModel->requestRemove(launcherModel->get(0)->appId());
200 QCOMPARE(launcherModel->get(0)->pinned(), false);
201 QCOMPARE(launcherModel->get(1)->pinned(), false);
202 QCOMPARE(spy.count(), 1);
203 QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
205 // Now that the app is unpinned, nothing should change and the signal should not be emitted
206 spy.clear();
207 launcherModel->requestRemove(launcherModel->get(0)->appId());
208 QCOMPARE(launcherModel->get(0)->pinned(), false);
209 QCOMPARE(launcherModel->get(1)->pinned(), false);
210 QCOMPARE(spy.count(), 0);
211 }
213 void testRemove_data() {
215 QTest::addColumn<bool>("pinned");
216 QTest::addColumn<bool>("running");
218 QTest::newRow("non-pinned, running") << false << true;
219 QTest::newRow("pinned, running") << true << true;
220 QTest::newRow("pinned, non-running") << true << false;
221 }
223 void testRemove() {
224 QFETCH(bool, pinned);
225 QFETCH(bool, running);
227 // In the beginning we always have two items
228 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
230 // pin one if required
231 if (pinned) {
232 launcherModel->pin("abs-icon");
233 }
235 // stop it if required
236 if (!running) {
237 appManager->stopApplication("abs-icon");
238 }
240 // Now remove it
241 launcherModel->requestRemove("abs-icon");
243 if (running) {
244 // both apps are running, both apps must still be here
245 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
247 // Item must be unpinned now
248 int index = launcherModel->findApplication("abs-icon");
249 QCOMPARE(launcherModel->get(index)->pinned(), false);
251 } else if (pinned) {
252 // Item 1 must go away, item 0 is here to stay
253 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
254 }
256 // done our checks. now stop the app if was still running
257 if (running) {
258 appManager->stopApplication("abs-icon");
259 }
261 // It needs to go away in any case now
262 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
263 }
265 void testQuickListPinningRemoving() {
266 // we start with 2 unpinned items
267 QCOMPARE(launcherModel->get(0)->pinned(), false);
268 QCOMPARE(launcherModel->get(1)->pinned(), false);
269 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
271 // find the Pin item in the quicklist
272 QuickListModel *model = qobject_cast<QuickListModel*>(launcherModel->get(0)->quickList());
273 int pinActionIndex = -1;
274 for (int i = 0; i < model->rowCount(QModelIndex()); ++i) {
275 if (model->get(i).actionId() == "pin_item") {
276 pinActionIndex = i;
277 break;
278 }
279 }
280 QVERIFY(pinActionIndex >= 0);
282 // trigger pin item quicklist action => Item must be pinned now.
283 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
284 QCOMPARE(launcherModel->get(0)->pinned(), true);
285 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
287 // quicklist needs to transform to remove item. trigger it and check it item goes away
288 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
289 QCOMPARE(launcherModel->get(0)->pinned(), false);
291 // still needs to be here as the app is still here
292 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
293 // close the app
294 appManager->removeApplication(0);
295 // Now it needs to go away
296 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
297 }
299 void testApplicationFocused() {
300 // all apps unfocused at beginning...
301 QCOMPARE(launcherModel->get(0)->focused(), false);
302 QCOMPARE(launcherModel->get(1)->focused(), false);
304 appManager->focusApplication("abs-icon");
305 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
306 QCOMPARE(launcherModel->get(0)->focused(), true);
307 QCOMPARE(launcherModel->get(1)->focused(), false);
309 appManager->focusApplication("no-icon");
310 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
311 QCOMPARE(launcherModel->get(0)->focused(), false);
312 QCOMPARE(launcherModel->get(1)->focused(), true);
313 }
315 void testClosingApps() {
316 // At the start there are 2 items. Let's pin one.
317 launcherModel->pin("abs-icon");
318 while (appManager->count() > 0) {
319 appManager->removeApplication(0);
320 }
321 // The pinned one needs to stay, the other needs to disappear
322 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
323 QCOMPARE(launcherModel->get(0)->appId(), QLatin1String("abs-icon"));
324 }
326 void testGetUrlForAppId() {
327 QCOMPARE(launcherModel->getUrlForAppId(QString()), QString());
328 QCOMPARE(launcherModel->getUrlForAppId(""), QString());
329 QCOMPARE(launcherModel->getUrlForAppId("no-name"), QString("application:///no-name.desktop"));
330 QCOMPARE(launcherModel->getUrlForAppId("com.test.good"), QString("application:///com.test.good.desktop"));
331 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application"), QString("appid://com.test.good/application/current-user-version"));
332 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application_1.2.3"), QString("appid://com.test.good/application/current-user-version"));
333 }
335 void testIntrospection() {
336 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "org.freedesktop.DBus.Introspectable");
337 QDBusReply<QString> reply = interface.call("Introspect");
338 QStringList nodes = extractNodes(reply.value());
339 QCOMPARE(nodes.count(), launcherModel->rowCount());
341 appManager->addApplication(new MockApp("foobar"));
342 reply = interface.call("Introspect");
343 nodes = extractNodes(reply.value());
344 QCOMPARE(nodes.contains("foobar"), true);
346 appManager->removeApplication(2);
347 reply = interface.call("Introspect");
348 nodes = extractNodes(reply.value());
349 QCOMPARE(nodes.contains("foobar"), false);
350 }
352 QStringList extractNodes(const QString &introspectionXml) {
353 QXmlStreamReader introspectReply(introspectionXml);
355 QStringList ret;
356 while (!introspectReply.atEnd() && !introspectReply.hasError()) {
357 QXmlStreamReader::TokenType token = introspectReply.readNext();
359 if (token == QXmlStreamReader::StartElement) {
360 if (introspectReply.name() == "node" && introspectReply.attributes().count() > 0) {
361 ret << introspectReply.attributes().value("name").toString();
362 }
363 }
364 }
365 return ret;
366 }
368 void testCountEmblems() {
369 // Call GetAll on abs-icon
370 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
371 QDBusReply<QVariantMap> reply = interface.call("GetAll");
372 QVariantMap map = reply.value();
374 // Make sure GetAll returns a map with count and countVisible props
375 QCOMPARE(map.contains("count"), true);
376 QCOMPARE(map.contains("countVisible"), true);
378 // Make sure count is intitilized to 0 and non-visible
379 QCOMPARE(map.value("count").toInt(), 0);
380 QCOMPARE(map.value("countVisible").toBool(), false);
382 // Now make it visible and set it to 55 through D-Bus
383 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", QVariant::fromValue(QDBusVariant(55)));
384 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(true)));
386 // Fetch it again using GetAll
387 reply = interface.call("GetAll");
388 map = reply.value();
390 // Make sure values have changed on the D-Bus interface
391 QCOMPARE(map.value("count").toInt(), 55);
392 QCOMPARE(map.value("countVisible").toBool(), true);
394 // Now the item on the upper side of the API
395 int index = launcherModel->findApplication("abs-icon");
396 QCOMPARE(index >= 0, true);
398 // And make sure values have changed there as well
399 QCOMPARE(launcherModel->get(index)->countVisible(), true);
400 QCOMPARE(launcherModel->get(index)->count(), 55);
401 }
403 void testCountEmblemAddsRemovesItem_data() {
404 QTest::addColumn<bool>("isPinned");
405 QTest::addColumn<bool>("isRunning");
406 QTest::addColumn<bool>("startWhenVisible");
407 QTest::newRow("not pinned, not running") << false << false << false;
408 QTest::newRow("pinned, not running") << true << false << false;
409 QTest::newRow("not pinned, not running, starting from notification") << false << false << true;
410 QTest::newRow("pinned, not running, starting from notification") << true << false << true;
411 QTest::newRow("not pinned, running") << false << true << false;
412 QTest::newRow("pinned, running") << true << true << false;
413 }
415 void testCountEmblemAddsRemovesItem() {
416 QFETCH(bool, isPinned);
417 QFETCH(bool, isRunning);
418 QFETCH(bool, startWhenVisible);
420 // Make sure item is here as expected after init() and that count is not visible
421 int index = launcherModel->findApplication("abs-icon");
422 QCOMPARE(index == -1, false);
423 QCOMPARE(launcherModel->get(index)->countVisible(), false);
425 // Pin if we need to
426 if (isPinned) {
427 launcherModel->pin("abs-icon");
428 }
429 QCOMPARE(launcherModel->get(0)->pinned(), isPinned);
431 // Stop it if we need to
432 if (!isRunning) {
433 appManager->stopApplication("abs-icon");
434 }
435 QCOMPARE(launcherModel->findApplication("abs-icon") >= 0, isRunning || isPinned);
438 // set the count emblem to visible
439 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
440 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", QVariant::fromValue(QDBusVariant(55)));
441 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(true)));
443 // Make sure item is here and that count is visible
444 index = launcherModel->findApplication("abs-icon");
445 QCOMPARE(index == -1, false);
446 QCOMPARE(launcherModel->get(index)->countVisible(), true);
448 if (!isRunning && startWhenVisible) {
449 appManager->addApplication(new MockApp("abs-icon"));
450 }
452 // Hide count emblem again
453 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(false)));
455 // Make sure item is shown/hidden as expected
456 index = launcherModel->findApplication("abs-icon");
457 QCOMPARE(index == -1, !isRunning && !isPinned && !startWhenVisible);
458 }
460 void testRefreshAfterDeletedDesktopFiles_data() {
461 QTest::addColumn<bool>("deleted");
462 QTest::newRow("have .desktop files") << false;
463 QTest::newRow("deleted .desktop files") << true;
464 }
466 void testRefreshAfterDeletedDesktopFiles() {
467 QFETCH(bool, deleted);
469 // pin both apps
470 launcherModel->pin("abs-icon");
471 launcherModel->pin("no-icon");
472 // close both apps
473 appManager->removeApplication(0);
474 appManager->removeApplication(0);
476 // "delete" the .desktop files
477 QString oldCurrent = QDir::currentPath();
478 if (deleted) {
479 // In testing mode, the launcher searches the current dir for the sample .desktop file
480 // We can make that fail by changing the current dir
481 QDir::setCurrent("..");
482 }
484 // Call refresh
485 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "com.canonical.Unity.Launcher");
486 QDBusReply<void> reply = interface.call("Refresh");
488 // Make sure the call to Refresh returned without error.
489 QCOMPARE(reply.isValid(), true);
491 QCOMPARE(launcherModel->rowCount(), deleted ? 0 : 2);
493 // Restoring current dir
494 QDir::setCurrent(oldCurrent);
495 }
497 void testSettings() {
498 GSettings *settings = launcherModel->m_settings;
499 QSignalSpy spy(launcherModel, SIGNAL(hint()));
501 // Nothing pinned at startup
502 QCOMPARE(settings->storedApplications().count(), 0);
504 // pin both apps
505 launcherModel->pin("abs-icon");
506 launcherModel->pin("no-icon");
507 QCOMPARE(spy.count(), 0);
509 // Now settings should have 2 apps
510 QCOMPARE(settings->storedApplications().count(), 2);
512 // close both apps
513 appManager->removeApplication(0);
514 appManager->removeApplication(0);
516 // Now settings should have 2 apps
517 QCOMPARE(settings->storedApplications().count(), 2);
519 // Now remove 1 app through the backend, make sure one is still there
520 settings->simulateDConfChanged(QStringList() << "abs-icon");
521 QCOMPARE(settings->storedApplications().count(), 1);
522 QCOMPARE(spy.count(), 1);
524 // Check if it disappeared from the frontend too
525 QCOMPARE(launcherModel->rowCount(), 1);
527 // Add them back but in reverse order
528 settings->simulateDConfChanged(QStringList() << "no-icon" << "abs-icon");
529 QCOMPARE(launcherModel->rowCount(), 2);
530 QCOMPARE(launcherModel->get(0)->appId(), QString("no-icon"));
531 QCOMPARE(launcherModel->get(1)->appId(), QString("abs-icon"));
532 QCOMPARE(spy.count(), 2);
533 }
535 void testAddSyncsToAS() {
536 // Make sure launcher and AS are in sync when we start the test
537 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
539 int oldCount = launcherModel->rowCount();
540 appManager->addApplication(new MockApp("rel-icon"));
541 QCOMPARE(launcherModel->rowCount(), oldCount + 1);
542 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
543 }
545 void testRemoveSyncsToAS() {
546 // Make sure launcher and AS are in sync when we start the test
547 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
549 int oldCount = launcherModel->rowCount();
550 appManager->stopApplication("abs-icon");
551 QCOMPARE(launcherModel->rowCount(), oldCount - 1);
552 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
553 }
555 void testMoveSyncsToAS() {
556 // Make sure launcher and AS are in sync when we start the test
557 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
559 for (int i = 0; i < launcherModel->rowCount(); i++) {
560 QString launcherAppId = launcherModel->get(i)->appId();
561 QString asAppId = getASConfig().at(i).value("id").toString();
562 QCOMPARE(launcherAppId, asAppId);
563 }
565 launcherModel->move(0, 1);
567 for (int i = 0; i < launcherModel->rowCount(); i++) {
568 QString launcherAppId = launcherModel->get(i)->appId();
569 QString asAppId = getASConfig().at(i).value("id").toString();
570 QCOMPARE(launcherAppId, asAppId);
571 }
572 }
574 void testCountChangeSyncsToAS() {
575 // Find the index of the abs-icon app
576 int index = launcherModel->findApplication("abs-icon");
578 // Make sure it's invisible and 0 at the beginning
579 QCOMPARE(getASConfig().at(index).value("countVisible").toBool(), false);
580 QCOMPARE(getASConfig().at(index).value("count").toInt(), 0);
582 // Change the count of the abs-icon app through D-Bus
583 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
584 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", QVariant::fromValue(QDBusVariant(55)));
585 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(true)));
587 // Make sure it changed to visible and 55
588 QCOMPARE(getASConfig().at(index).value("countVisible").toBool(), true);
589 QCOMPARE(getASConfig().at(index).value("count").toInt(), 55);
590 }
591};
593QTEST_GUILESS_MAIN(LauncherModelTest)
594#include "launchermodeltest.moc"