2 * Copyright 2013-2015 Canonical Ltd.
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.
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.
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/>.
17 * Michael Zanetti <michael.zanetti@canonical.com>
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"
29#include "AccountsServiceDBusAdaptor.h"
32#include <QDBusInterface>
34#include <QDomDocument>
36// This is a mock, specifically to test the LauncherModel
37class MockApp: public unity::shell::application::ApplicationInfoInterface
41 MockApp(const QString &appId, QObject *parent = 0): ApplicationInfoInterface(appId, parent), m_appId(appId), m_focused(false) { }
42 QString appId() const override { return m_appId; }
43 QString name() const override { return "mock"; }
44 QString comment() const override { return "this is a mock"; }
45 QUrl icon() const override { return QUrl(); }
46 ApplicationInfoInterface::Stage stage() const override { return ApplicationInfoInterface::MainStage; }
47 ApplicationInfoInterface::State state() const override { return ApplicationInfoInterface::Running; }
48 bool focused() const override { 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); }
55 Qt::ScreenOrientations supportedOrientations() const override { return Qt::PortraitOrientation; }
56 bool rotatesWindowContents() const override { return false; }
58 // Methods used for mocking (not in the interface)
59 void setFocused(bool focused) { m_focused = focused; Q_EMIT focusedChanged(focused); }
65// This is a mock, specifically to test the LauncherModel
66class MockAppManager: public unity::shell::application::ApplicationManagerInterface
70 MockAppManager(QObject *parent = 0): ApplicationManagerInterface(parent) {}
71 int rowCount(const QModelIndex &) const override { return m_list.count(); }
72 QVariant data(const QModelIndex &, int ) const override { return QVariant(); }
73 QString focusedApplicationId() const override {
74 Q_FOREACH(MockApp *app, m_list) {
75 if (app->focused()) return app->appId();
79 unity::shell::application::ApplicationInfoInterface *get(int index) const override { return m_list.at(index); }
80 unity::shell::application::ApplicationInfoInterface *findApplication(const QString &appId) const override {
81 Q_FOREACH(MockApp* app, m_list) {
82 if (app->appId() == appId) {
88 unity::shell::application::ApplicationInfoInterface *startApplication(const QString &, const QStringList &) override { return nullptr; }
89 bool stopApplication(const QString &appId) override {
90 Q_FOREACH(MockApp* app, m_list) {
91 if (app->appId() == appId) {
92 removeApplication(m_list.indexOf(app));
98 bool focusApplication(const QString &appId) override {
99 Q_FOREACH(MockApp* app, m_list) {
100 app->setFocused(app->appId() == appId);
102 Q_EMIT focusedApplicationIdChanged();
106 void unfocusCurrentApplication() override { }
108 void addApplication(MockApp *app) {
109 beginInsertRows(QModelIndex(), count(), count());
113 void removeApplication(int index) {
114 beginRemoveRows(QModelIndex(), index, index);
115 m_list.takeAt(index)->deleteLater();
118 bool requestFocusApplication(const QString &appId) override { Q_UNUSED(appId); return true; }
119 bool suspended() const override { return false; }
120 void setSuspended(bool) override {}
121 bool forceDashActive() const override { return false; }
122 void setForceDashActive(bool) override {}
125 QList<MockApp*> m_list;
128class LauncherModelTest : public QObject
133 LauncherModel *launcherModel;
134 MockAppManager *appManager;
136 QList<QVariantMap> getASConfig() {
137 AccountsServiceDBusAdaptor *as = launcherModel->m_asAdapter->m_accounts;
138 return as->getUserProperty("", "", "LauncherItems").value<QList<QVariantMap>>();
143 void initTestCase() {
144 launcherModel = new LauncherModel(this);
145 QCoreApplication::processEvents(); // to let the model register on DBus
146 QCOMPARE(launcherModel->rowCount(QModelIndex()), 0);
148 appManager = new MockAppManager(this);
149 launcherModel->setApplicationManager(appManager);
152 // Adding 2 apps to the mock appmanager. Both should appear in the launcher.
154 appManager->addApplication(new MockApp("abs-icon"));
155 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
157 appManager->addApplication(new MockApp("no-icon"));
158 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
160 launcherModel->m_settings->setStoredApplications(QStringList());
163 // Removing apps from appmanager and launcher as pinned ones would stick
165 while (appManager->count() > 0) {
166 appManager->removeApplication(0);
168 while (launcherModel->rowCount(QModelIndex()) > 0) {
169 launcherModel->requestRemove(launcherModel->get(0)->appId());
174 QCOMPARE(launcherModel->get(0)->pinned(), false);
175 QCOMPARE(launcherModel->get(1)->pinned(), false);
177 LauncherItemInterface *item0BeforeMove = launcherModel->get(0);
178 LauncherItemInterface *item1BeforeMove = launcherModel->get(1);
179 launcherModel->move(1, 0);
181 QCOMPARE(item0BeforeMove, launcherModel->get(1));
182 QCOMPARE(item1BeforeMove, launcherModel->get(0));
184 // moved item must be pinned now
185 QCOMPARE(item0BeforeMove->pinned(), false);
186 QCOMPARE(item1BeforeMove->pinned(), true);
190 QSignalSpy spy(launcherModel, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)));
191 QCOMPARE(launcherModel->get(0)->pinned(), false);
192 QCOMPARE(launcherModel->get(1)->pinned(), false);
193 launcherModel->pin(launcherModel->get(0)->appId());
194 QCOMPARE(launcherModel->get(0)->pinned(), true);
195 QCOMPARE(launcherModel->get(1)->pinned(), false);
196 QCOMPARE(spy.count() > 0, true);
197 QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
199 // App should be pinned now
201 launcherModel->requestRemove(launcherModel->get(0)->appId());
202 QCOMPARE(launcherModel->get(0)->pinned(), false);
203 QCOMPARE(launcherModel->get(1)->pinned(), false);
204 QCOMPARE(spy.count(), 1);
205 QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
207 // Now that the app is unpinned, nothing should change and the signal should not be emitted
209 launcherModel->requestRemove(launcherModel->get(0)->appId());
210 QCOMPARE(launcherModel->get(0)->pinned(), false);
211 QCOMPARE(launcherModel->get(1)->pinned(), false);
212 QCOMPARE(spy.count(), 0);
215 void testRemove_data() {
217 QTest::addColumn<bool>("pinned");
218 QTest::addColumn<bool>("running");
220 QTest::newRow("non-pinned, running") << false << true;
221 QTest::newRow("pinned, running") << true << true;
222 QTest::newRow("pinned, non-running") << true << false;
226 QFETCH(bool, pinned);
227 QFETCH(bool, running);
229 // In the beginning we always have two items
230 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
232 // pin one if required
234 launcherModel->pin("abs-icon");
237 // stop it if required
239 appManager->stopApplication("abs-icon");
243 launcherModel->requestRemove("abs-icon");
246 // both apps are running, both apps must still be here
247 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
249 // Item must be unpinned now
250 int index = launcherModel->findApplication("abs-icon");
251 QCOMPARE(launcherModel->get(index)->pinned(), false);
254 // Item 1 must go away, item 0 is here to stay
255 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
258 // done our checks. now stop the app if was still running
260 appManager->stopApplication("abs-icon");
263 // It needs to go away in any case now
264 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
267 void testQuickListPinningRemoving() {
268 // we start with 2 unpinned items
269 QCOMPARE(launcherModel->get(0)->pinned(), false);
270 QCOMPARE(launcherModel->get(1)->pinned(), false);
271 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
273 // find the Pin item in the quicklist
274 QuickListModel *model = qobject_cast<QuickListModel*>(launcherModel->get(0)->quickList());
275 int pinActionIndex = -1;
276 for (int i = 0; i < model->rowCount(QModelIndex()); ++i) {
277 if (model->get(i).actionId() == "pin_item") {
282 QVERIFY(pinActionIndex >= 0);
284 // trigger pin item quicklist action => Item must be pinned now.
285 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
286 QCOMPARE(launcherModel->get(0)->pinned(), true);
287 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
289 // quicklist needs to transform to remove item. trigger it and check it item goes away
290 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
291 QCOMPARE(launcherModel->get(0)->pinned(), false);
293 // still needs to be here as the app is still here
294 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
296 appManager->removeApplication(0);
297 // Now it needs to go away
298 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
301 void testApplicationFocused() {
302 // all apps unfocused at beginning...
303 QCOMPARE(launcherModel->get(0)->focused(), false);
304 QCOMPARE(launcherModel->get(1)->focused(), false);
306 appManager->focusApplication("abs-icon");
307 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
308 QCOMPARE(launcherModel->get(0)->focused(), true);
309 QCOMPARE(launcherModel->get(1)->focused(), false);
311 appManager->focusApplication("no-icon");
312 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
313 QCOMPARE(launcherModel->get(0)->focused(), false);
314 QCOMPARE(launcherModel->get(1)->focused(), true);
317 void testClosingApps() {
318 // At the start there are 2 items. Let's pin one.
319 launcherModel->pin("abs-icon");
320 while (appManager->count() > 0) {
321 appManager->removeApplication(0);
323 // The pinned one needs to stay, the other needs to disappear
324 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
325 QCOMPARE(launcherModel->get(0)->appId(), QLatin1String("abs-icon"));
328 void testGetUrlForAppId() {
329 QCOMPARE(launcherModel->getUrlForAppId(QString()), QString());
330 QCOMPARE(launcherModel->getUrlForAppId(""), QString());
331 QCOMPARE(launcherModel->getUrlForAppId("no-name"), QString("application:///no-name.desktop"));
332 QCOMPARE(launcherModel->getUrlForAppId("com.test.good"), QString("application:///com.test.good.desktop"));
333 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application"), QString("appid://com.test.good/application/current-user-version"));
334 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application_1.2.3"), QString("appid://com.test.good/application/current-user-version"));
337 void testIntrospection() {
338 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "org.freedesktop.DBus.Introspectable");
339 QDBusReply<QString> reply = interface.call("Introspect");
340 QStringList nodes = extractNodes(reply.value());
341 QCOMPARE(nodes.count(), launcherModel->rowCount());
343 appManager->addApplication(new MockApp("foobar"));
344 reply = interface.call("Introspect");
345 nodes = extractNodes(reply.value());
346 QCOMPARE(nodes.contains("foobar"), true);
348 appManager->removeApplication(2);
349 reply = interface.call("Introspect");
350 nodes = extractNodes(reply.value());
351 QCOMPARE(nodes.contains("foobar"), false);
354 QStringList extractNodes(const QString &introspectionXml) {
355 QXmlStreamReader introspectReply(introspectionXml);
358 while (!introspectReply.atEnd() && !introspectReply.hasError()) {
359 QXmlStreamReader::TokenType token = introspectReply.readNext();
361 if (token == QXmlStreamReader::StartElement) {
362 if (introspectReply.name() == "node" && introspectReply.attributes().count() > 0) {
363 ret << introspectReply.attributes().value("name").toString();
370 void testCountEmblems() {
371 QSignalSpy spy(launcherModel, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)));
373 // Call GetAll on abs-icon
374 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
375 QDBusReply<QVariantMap> reply = interface.call("GetAll");
376 QVariantMap map = reply.value();
378 // Check that the alerting-status is still false, and the item on the upper side of the API
379 int index = launcherModel->findApplication("abs-icon");
380 QCOMPARE(index >= 0, true);
381 QVERIFY(launcherModel->get(index)->alerting() == false);
383 // Make sure GetAll returns a map with count and countVisible props
384 QCOMPARE(map.contains("count"), true);
385 QCOMPARE(map.contains("countVisible"), true);
387 // Make sure count is intitilized to 0 and non-visible
388 QCOMPARE(map.value("count").toInt(), 0);
389 QCOMPARE(map.value("countVisible").toBool(), false);
391 // Now make it visible and set it to 55 through D-Bus
392 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", QVariant::fromValue(QDBusVariant(55)));
393 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(true)));
395 // Fetch it again using GetAll
396 reply = interface.call("GetAll");
399 // Make sure values have changed on the D-Bus interface
400 QCOMPARE(map.value("count").toInt(), 55);
401 QCOMPARE(map.value("countVisible").toBool(), true);
403 // Finally check, that the change to "count" implicitly also set the alerting-state to true
404 QVERIFY(launcherModel->get(index)->alerting() == true);
407 void testCountEmblemAddsRemovesItem_data() {
408 QTest::addColumn<bool>("isPinned");
409 QTest::addColumn<bool>("isRunning");
410 QTest::addColumn<bool>("startWhenVisible");
411 QTest::newRow("not pinned, not running") << false << false << false;
412 QTest::newRow("pinned, not running") << true << false << false;
413 QTest::newRow("not pinned, not running, starting from notification") << false << false << true;
414 QTest::newRow("pinned, not running, starting from notification") << true << false << true;
415 QTest::newRow("not pinned, running") << false << true << false;
416 QTest::newRow("pinned, running") << true << true << false;
419 void testCountEmblemAddsRemovesItem() {
420 QFETCH(bool, isPinned);
421 QFETCH(bool, isRunning);
422 QFETCH(bool, startWhenVisible);
424 // Make sure item is here as expected after init() and that count is not visible
425 int index = launcherModel->findApplication("abs-icon");
426 QCOMPARE(index == -1, false);
427 QCOMPARE(launcherModel->get(index)->countVisible(), false);
431 launcherModel->pin("abs-icon");
433 QCOMPARE(launcherModel->get(0)->pinned(), isPinned);
435 // Stop it if we need to
437 appManager->stopApplication("abs-icon");
439 QCOMPARE(launcherModel->findApplication("abs-icon") >= 0, isRunning || isPinned);
442 // set the count emblem to visible
443 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
444 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", QVariant::fromValue(QDBusVariant(55)));
445 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(true)));
447 // Make sure item is here and that count is visible
448 index = launcherModel->findApplication("abs-icon");
449 QCOMPARE(index == -1, false);
450 QCOMPARE(launcherModel->get(index)->countVisible(), true);
452 if (!isRunning && startWhenVisible) {
453 appManager->addApplication(new MockApp("abs-icon"));
456 // Hide count emblem again
457 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(false)));
459 // Make sure item is shown/hidden as expected
460 index = launcherModel->findApplication("abs-icon");
461 QCOMPARE(index == -1, !isRunning && !isPinned && !startWhenVisible);
464 void testRefreshAfterDeletedDesktopFiles_data() {
465 QTest::addColumn<bool>("deleted");
466 QTest::newRow("have .desktop files") << false;
467 QTest::newRow("deleted .desktop files") << true;
470 void testRefreshAfterDeletedDesktopFiles() {
471 QFETCH(bool, deleted);
474 launcherModel->pin("abs-icon");
475 launcherModel->pin("no-icon");
477 appManager->removeApplication(0);
478 appManager->removeApplication(0);
480 // "delete" the .desktop files
481 QString oldCurrent = QDir::currentPath();
483 // In testing mode, the launcher searches the current dir for the sample .desktop file
484 // We can make that fail by changing the current dir
485 QDir::setCurrent("..");
489 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "com.canonical.Unity.Launcher");
490 QDBusReply<void> reply = interface.call("Refresh");
492 // Make sure the call to Refresh returned without error.
493 QCOMPARE(reply.isValid(), true);
495 QCOMPARE(launcherModel->rowCount(), deleted ? 0 : 2);
497 // Restoring current dir
498 QDir::setCurrent(oldCurrent);
501 void testSettings() {
502 GSettings *settings = launcherModel->m_settings;
503 QSignalSpy spy(launcherModel, SIGNAL(hint()));
505 // Nothing pinned at startup
506 QCOMPARE(settings->storedApplications().count(), 0);
509 launcherModel->pin("abs-icon");
510 launcherModel->pin("no-icon");
511 QCOMPARE(spy.count(), 0);
513 // Now settings should have 2 apps
514 QCOMPARE(settings->storedApplications().count(), 2);
517 appManager->removeApplication(0);
518 appManager->removeApplication(0);
520 // Now settings should have 2 apps
521 QCOMPARE(settings->storedApplications().count(), 2);
523 // Now remove 1 app through the backend, make sure one is still there
524 settings->simulateDConfChanged(QStringList() << "abs-icon");
525 QCOMPARE(settings->storedApplications().count(), 1);
526 QCOMPARE(spy.count(), 1);
528 // Check if it disappeared from the frontend too
529 QCOMPARE(launcherModel->rowCount(), 1);
531 // Add them back but in reverse order
532 settings->simulateDConfChanged(QStringList() << "no-icon" << "abs-icon");
533 QCOMPARE(launcherModel->rowCount(), 2);
534 QCOMPARE(launcherModel->get(0)->appId(), QString("no-icon"));
535 QCOMPARE(launcherModel->get(1)->appId(), QString("abs-icon"));
536 QCOMPARE(spy.count(), 2);
539 void testAddSyncsToAS() {
540 // Make sure launcher and AS are in sync when we start the test
541 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
543 int oldCount = launcherModel->rowCount();
544 appManager->addApplication(new MockApp("rel-icon"));
545 QCOMPARE(launcherModel->rowCount(), oldCount + 1);
546 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
549 void testRemoveSyncsToAS() {
550 // Make sure launcher and AS are in sync when we start the test
551 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
553 int oldCount = launcherModel->rowCount();
554 appManager->stopApplication("abs-icon");
555 QCOMPARE(launcherModel->rowCount(), oldCount - 1);
556 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
559 void testMoveSyncsToAS() {
560 // Make sure launcher and AS are in sync when we start the test
561 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
563 for (int i = 0; i < launcherModel->rowCount(); i++) {
564 QString launcherAppId = launcherModel->get(i)->appId();
565 QString asAppId = getASConfig().at(i).value("id").toString();
566 QCOMPARE(launcherAppId, asAppId);
569 launcherModel->move(0, 1);
571 for (int i = 0; i < launcherModel->rowCount(); i++) {
572 QString launcherAppId = launcherModel->get(i)->appId();
573 QString asAppId = getASConfig().at(i).value("id").toString();
574 QCOMPARE(launcherAppId, asAppId);
578 void testCountChangeSyncsToAS() {
579 // Find the index of the abs-icon app
580 int index = launcherModel->findApplication("abs-icon");
582 // Make sure it's invisible and 0 at the beginning
583 QCOMPARE(getASConfig().at(index).value("countVisible").toBool(), false);
584 QCOMPARE(getASConfig().at(index).value("count").toInt(), 0);
586 // Change the count of the abs-icon app through D-Bus
587 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
588 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", QVariant::fromValue(QDBusVariant(55)));
589 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(true)));
591 // Make sure it changed to visible and 55
592 QCOMPARE(getASConfig().at(index).value("countVisible").toBool(), true);
593 QCOMPARE(getASConfig().at(index).value("count").toInt(), 55);
597QTEST_GUILESS_MAIN(LauncherModelTest)
598#include "launchermodeltest.moc"
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list