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) { }
43 RequestedState requestedState() const override { return RequestedRunning; }
44 void setRequestedState(RequestedState) override {}
45 QString appId() const override { return m_appId; }
46 QString name() const override { return "mock"; }
47 QString comment() const override { return "this is a mock"; }
48 QUrl icon() const override { return QUrl(); }
49 ApplicationInfoInterface::Stage stage() const override { return ApplicationInfoInterface::MainStage; }
50 ApplicationInfoInterface::State state() const override { return ApplicationInfoInterface::Running; }
51 bool focused() const override { return m_focused; }
52 QString splashTitle() const override { return QString(); }
53 QUrl splashImage() const override { return QUrl(); }
54 bool splashShowHeader() const override { return true; }
55 QColor splashColor() const override { return QColor(0,0,0,0); }
56 QColor splashColorHeader() const override { return QColor(0,0,0,0); }
57 QColor splashColorFooter() const override { return QColor(0,0,0,0); }
58 Qt::ScreenOrientations supportedOrientations() const override { return Qt::PortraitOrientation; }
59 bool rotatesWindowContents() const override { return false; }
61 // Methods used for mocking (not in the interface)
62 void setFocused(bool focused) { m_focused = focused; Q_EMIT focusedChanged(focused); }
68// This is a mock, specifically to test the LauncherModel
69class MockAppManager: public unity::shell::application::ApplicationManagerInterface
73 MockAppManager(QObject *parent = 0): ApplicationManagerInterface(parent) {}
74 int rowCount(const QModelIndex &) const override { return m_list.count(); }
75 QVariant data(const QModelIndex &, int ) const override { return QVariant(); }
76 QString focusedApplicationId() const override {
77 Q_FOREACH(MockApp *app, m_list) {
78 if (app->focused()) return app->appId();
82 unity::shell::application::ApplicationInfoInterface *get(int index) const override { return m_list.at(index); }
83 unity::shell::application::ApplicationInfoInterface *findApplication(const QString &appId) const override {
84 Q_FOREACH(MockApp* app, m_list) {
85 if (app->appId() == appId) {
91 unity::shell::application::ApplicationInfoInterface *startApplication(const QString &, const QStringList &) override { return nullptr; }
92 bool stopApplication(const QString &appId) override {
93 Q_FOREACH(MockApp* app, m_list) {
94 if (app->appId() == appId) {
95 removeApplication(m_list.indexOf(app));
101 bool focusApplication(const QString &appId) override {
102 Q_FOREACH(MockApp* app, m_list) {
103 app->setFocused(app->appId() == appId);
105 Q_EMIT focusedApplicationIdChanged();
109 void unfocusCurrentApplication() override { }
111 void addApplication(MockApp *app) {
112 beginInsertRows(QModelIndex(), count(), count());
116 void removeApplication(int index) {
117 beginRemoveRows(QModelIndex(), index, index);
118 m_list.takeAt(index)->deleteLater();
121 bool requestFocusApplication(const QString &appId) override { Q_UNUSED(appId); return true; }
124 QList<MockApp*> m_list;
127class LauncherModelTest : public QObject
132 LauncherModel *launcherModel;
133 MockAppManager *appManager;
135 QList<QVariantMap> getASConfig() {
136 AccountsServiceDBusAdaptor *as = launcherModel->m_asAdapter->m_accounts;
137 return as->getUserProperty("", "", "LauncherItems").value<QList<QVariantMap>>();
142 void initTestCase() {
143 launcherModel = new LauncherModel(this);
144 QCoreApplication::processEvents(); // to let the model register on DBus
145 QCOMPARE(launcherModel->rowCount(QModelIndex()), 0);
147 appManager = new MockAppManager(this);
148 launcherModel->setApplicationManager(appManager);
151 // Adding 2 apps to the mock appmanager. Both should appear in the launcher.
153 appManager->addApplication(new MockApp("abs-icon"));
154 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
156 appManager->addApplication(new MockApp("no-icon"));
157 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
159 launcherModel->m_settings->setStoredApplications(QStringList());
162 // Removing apps from appmanager and launcher as pinned ones would stick
164 while (appManager->count() > 0) {
165 appManager->removeApplication(0);
167 while (launcherModel->rowCount(QModelIndex()) > 0) {
168 launcherModel->requestRemove(launcherModel->get(0)->appId());
173 QCOMPARE(launcherModel->get(0)->pinned(), false);
174 QCOMPARE(launcherModel->get(1)->pinned(), false);
176 LauncherItemInterface *item0BeforeMove = launcherModel->get(0);
177 LauncherItemInterface *item1BeforeMove = launcherModel->get(1);
178 launcherModel->move(1, 0);
180 QCOMPARE(item0BeforeMove, launcherModel->get(1));
181 QCOMPARE(item1BeforeMove, launcherModel->get(0));
183 // moved item must be pinned now
184 QCOMPARE(item0BeforeMove->pinned(), false);
185 QCOMPARE(item1BeforeMove->pinned(), true);
189 QSignalSpy spy(launcherModel, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)));
190 QCOMPARE(launcherModel->get(0)->pinned(), false);
191 QCOMPARE(launcherModel->get(1)->pinned(), false);
192 launcherModel->pin(launcherModel->get(0)->appId());
193 QCOMPARE(launcherModel->get(0)->pinned(), true);
194 QCOMPARE(launcherModel->get(1)->pinned(), false);
195 QCOMPARE(spy.count() > 0, true);
196 QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
198 // App should be pinned now
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(), 1);
204 QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
206 // Now that the app is unpinned, nothing should change and the signal should not be emitted
208 launcherModel->requestRemove(launcherModel->get(0)->appId());
209 QCOMPARE(launcherModel->get(0)->pinned(), false);
210 QCOMPARE(launcherModel->get(1)->pinned(), false);
211 QCOMPARE(spy.count(), 0);
214 void testRemove_data() {
216 QTest::addColumn<bool>("pinned");
217 QTest::addColumn<bool>("running");
219 QTest::newRow("non-pinned, running") << false << true;
220 QTest::newRow("pinned, running") << true << true;
221 QTest::newRow("pinned, non-running") << true << false;
225 QFETCH(bool, pinned);
226 QFETCH(bool, running);
228 // In the beginning we always have two items
229 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
231 // pin one if required
233 launcherModel->pin("abs-icon");
236 // stop it if required
238 appManager->stopApplication("abs-icon");
242 launcherModel->requestRemove("abs-icon");
245 // both apps are running, both apps must still be here
246 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
248 // Item must be unpinned now
249 int index = launcherModel->findApplication("abs-icon");
250 QCOMPARE(launcherModel->get(index)->pinned(), false);
253 // Item 1 must go away, item 0 is here to stay
254 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
257 // done our checks. now stop the app if was still running
259 appManager->stopApplication("abs-icon");
262 // It needs to go away in any case now
263 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
266 void testQuickListPinningRemoving() {
267 // we start with 2 unpinned items
268 QCOMPARE(launcherModel->get(0)->pinned(), false);
269 QCOMPARE(launcherModel->get(1)->pinned(), false);
270 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
272 // find the Pin item in the quicklist
273 QuickListModel *model = qobject_cast<QuickListModel*>(launcherModel->get(0)->quickList());
274 int pinActionIndex = -1;
275 for (int i = 0; i < model->rowCount(QModelIndex()); ++i) {
276 if (model->get(i).actionId() == "pin_item") {
281 QVERIFY(pinActionIndex >= 0);
283 // trigger pin item quicklist action => Item must be pinned now.
284 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
285 QCOMPARE(launcherModel->get(0)->pinned(), true);
286 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
288 // quicklist needs to transform to remove item. trigger it and check it item goes away
289 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
290 QCOMPARE(launcherModel->get(0)->pinned(), false);
292 // still needs to be here as the app is still here
293 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
295 appManager->removeApplication(0);
296 // Now it needs to go away
297 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
300 void testApplicationFocused() {
301 // all apps unfocused at beginning...
302 QCOMPARE(launcherModel->get(0)->focused(), false);
303 QCOMPARE(launcherModel->get(1)->focused(), false);
305 appManager->focusApplication("abs-icon");
306 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
307 QCOMPARE(launcherModel->get(0)->focused(), true);
308 QCOMPARE(launcherModel->get(1)->focused(), false);
310 appManager->focusApplication("no-icon");
311 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
312 QCOMPARE(launcherModel->get(0)->focused(), false);
313 QCOMPARE(launcherModel->get(1)->focused(), true);
316 void testClosingApps() {
317 // At the start there are 2 items. Let's pin one.
318 launcherModel->pin("abs-icon");
319 while (appManager->count() > 0) {
320 appManager->removeApplication(0);
322 // The pinned one needs to stay, the other needs to disappear
323 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
324 QCOMPARE(launcherModel->get(0)->appId(), QLatin1String("abs-icon"));
327 void testGetUrlForAppId() {
328 QCOMPARE(launcherModel->getUrlForAppId(QString()), QString());
329 QCOMPARE(launcherModel->getUrlForAppId(""), QString());
330 QCOMPARE(launcherModel->getUrlForAppId("no-name"), QString("application:///no-name.desktop"));
331 QCOMPARE(launcherModel->getUrlForAppId("com.test.good"), QString("application:///com.test.good.desktop"));
332 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application"), QString("appid://com.test.good/application/current-user-version"));
333 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application_1.2.3"), QString("appid://com.test.good/application/current-user-version"));
336 void testIntrospection() {
337 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "org.freedesktop.DBus.Introspectable");
338 QDBusReply<QString> reply = interface.call("Introspect");
339 QStringList nodes = extractNodes(reply.value());
340 QCOMPARE(nodes.count(), launcherModel->rowCount());
342 appManager->addApplication(new MockApp("foobar"));
343 reply = interface.call("Introspect");
344 nodes = extractNodes(reply.value());
345 QCOMPARE(nodes.contains("foobar"), true);
347 appManager->removeApplication(2);
348 reply = interface.call("Introspect");
349 nodes = extractNodes(reply.value());
350 QCOMPARE(nodes.contains("foobar"), false);
353 QStringList extractNodes(const QString &introspectionXml) {
354 QXmlStreamReader introspectReply(introspectionXml);
357 while (!introspectReply.atEnd() && !introspectReply.hasError()) {
358 QXmlStreamReader::TokenType token = introspectReply.readNext();
360 if (token == QXmlStreamReader::StartElement) {
361 if (introspectReply.name() == "node" && introspectReply.attributes().count() > 0) {
362 ret << introspectReply.attributes().value("name").toString();
369 void testCountEmblems() {
370 // Call GetAll on abs-icon
371 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
372 QDBusReply<QVariantMap> reply = interface.call("GetAll");
373 QVariantMap map = reply.value();
375 // Make sure GetAll returns a map with count and countVisible props
376 QCOMPARE(map.contains("count"), true);
377 QCOMPARE(map.contains("countVisible"), true);
379 // Make sure count is intitilized to 0 and non-visible
380 QCOMPARE(map.value("count").toInt(), 0);
381 QCOMPARE(map.value("countVisible").toBool(), false);
383 // Now make it visible and set it to 55 through D-Bus
384 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", QVariant::fromValue(QDBusVariant(55)));
385 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(true)));
387 // Fetch it again using GetAll
388 reply = interface.call("GetAll");
391 // Make sure values have changed on the D-Bus interface
392 QCOMPARE(map.value("count").toInt(), 55);
393 QCOMPARE(map.value("countVisible").toBool(), true);
395 // Now the item on the upper side of the API
396 int index = launcherModel->findApplication("abs-icon");
397 QCOMPARE(index >= 0, true);
399 // And make sure values have changed there as well
400 QCOMPARE(launcherModel->get(index)->countVisible(), true);
401 QCOMPARE(launcherModel->get(index)->count(), 55);
404 void testCountEmblemAddsRemovesItem_data() {
405 QTest::addColumn<bool>("isPinned");
406 QTest::addColumn<bool>("isRunning");
407 QTest::addColumn<bool>("startWhenVisible");
408 QTest::newRow("not pinned, not running") << false << false << false;
409 QTest::newRow("pinned, not running") << true << false << false;
410 QTest::newRow("not pinned, not running, starting from notification") << false << false << true;
411 QTest::newRow("pinned, not running, starting from notification") << true << false << true;
412 QTest::newRow("not pinned, running") << false << true << false;
413 QTest::newRow("pinned, running") << true << true << false;
416 void testCountEmblemAddsRemovesItem() {
417 QFETCH(bool, isPinned);
418 QFETCH(bool, isRunning);
419 QFETCH(bool, startWhenVisible);
421 // Make sure item is here as expected after init() and that count is not visible
422 int index = launcherModel->findApplication("abs-icon");
423 QCOMPARE(index == -1, false);
424 QCOMPARE(launcherModel->get(index)->countVisible(), false);
428 launcherModel->pin("abs-icon");
430 QCOMPARE(launcherModel->get(0)->pinned(), isPinned);
432 // Stop it if we need to
434 appManager->stopApplication("abs-icon");
436 QCOMPARE(launcherModel->findApplication("abs-icon") >= 0, isRunning || isPinned);
439 // set the count emblem to visible
440 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
441 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", QVariant::fromValue(QDBusVariant(55)));
442 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(true)));
444 // Make sure item is here and that count is visible
445 index = launcherModel->findApplication("abs-icon");
446 QCOMPARE(index == -1, false);
447 QCOMPARE(launcherModel->get(index)->countVisible(), true);
449 if (!isRunning && startWhenVisible) {
450 appManager->addApplication(new MockApp("abs-icon"));
453 // Hide count emblem again
454 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(false)));
456 // Make sure item is shown/hidden as expected
457 index = launcherModel->findApplication("abs-icon");
458 QCOMPARE(index == -1, !isRunning && !isPinned && !startWhenVisible);
461 void testRefreshAfterDeletedDesktopFiles_data() {
462 QTest::addColumn<bool>("deleted");
463 QTest::newRow("have .desktop files") << false;
464 QTest::newRow("deleted .desktop files") << true;
467 void testRefreshAfterDeletedDesktopFiles() {
468 QFETCH(bool, deleted);
471 launcherModel->pin("abs-icon");
472 launcherModel->pin("no-icon");
474 appManager->removeApplication(0);
475 appManager->removeApplication(0);
477 // "delete" the .desktop files
478 QString oldCurrent = QDir::currentPath();
480 // In testing mode, the launcher searches the current dir for the sample .desktop file
481 // We can make that fail by changing the current dir
482 QDir::setCurrent("..");
486 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "com.canonical.Unity.Launcher");
487 QDBusReply<void> reply = interface.call("Refresh");
489 // Make sure the call to Refresh returned without error.
490 QCOMPARE(reply.isValid(), true);
492 QCOMPARE(launcherModel->rowCount(), deleted ? 0 : 2);
494 // Restoring current dir
495 QDir::setCurrent(oldCurrent);
498 void testSettings() {
499 GSettings *settings = launcherModel->m_settings;
500 QSignalSpy spy(launcherModel, SIGNAL(hint()));
502 // Nothing pinned at startup
503 QCOMPARE(settings->storedApplications().count(), 0);
506 launcherModel->pin("abs-icon");
507 launcherModel->pin("no-icon");
508 QCOMPARE(spy.count(), 0);
510 // Now settings should have 2 apps
511 QCOMPARE(settings->storedApplications().count(), 2);
514 appManager->removeApplication(0);
515 appManager->removeApplication(0);
517 // Now settings should have 2 apps
518 QCOMPARE(settings->storedApplications().count(), 2);
520 // Now remove 1 app through the backend, make sure one is still there
521 settings->simulateDConfChanged(QStringList() << "abs-icon");
522 QCOMPARE(settings->storedApplications().count(), 1);
523 QCOMPARE(spy.count(), 1);
525 // Check if it disappeared from the frontend too
526 QCOMPARE(launcherModel->rowCount(), 1);
528 // Add them back but in reverse order
529 settings->simulateDConfChanged(QStringList() << "no-icon" << "abs-icon");
530 QCOMPARE(launcherModel->rowCount(), 2);
531 QCOMPARE(launcherModel->get(0)->appId(), QString("no-icon"));
532 QCOMPARE(launcherModel->get(1)->appId(), QString("abs-icon"));
533 QCOMPARE(spy.count(), 2);
536 void testAddSyncsToAS() {
537 // Make sure launcher and AS are in sync when we start the test
538 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
540 int oldCount = launcherModel->rowCount();
541 appManager->addApplication(new MockApp("rel-icon"));
542 QCOMPARE(launcherModel->rowCount(), oldCount + 1);
543 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
546 void testRemoveSyncsToAS() {
547 // Make sure launcher and AS are in sync when we start the test
548 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
550 int oldCount = launcherModel->rowCount();
551 appManager->stopApplication("abs-icon");
552 QCOMPARE(launcherModel->rowCount(), oldCount - 1);
553 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
556 void testMoveSyncsToAS() {
557 // Make sure launcher and AS are in sync when we start the test
558 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
560 for (int i = 0; i < launcherModel->rowCount(); i++) {
561 QString launcherAppId = launcherModel->get(i)->appId();
562 QString asAppId = getASConfig().at(i).value("id").toString();
563 QCOMPARE(launcherAppId, asAppId);
566 launcherModel->move(0, 1);
568 for (int i = 0; i < launcherModel->rowCount(); i++) {
569 QString launcherAppId = launcherModel->get(i)->appId();
570 QString asAppId = getASConfig().at(i).value("id").toString();
571 QCOMPARE(launcherAppId, asAppId);
575 void testCountChangeSyncsToAS() {
576 // Find the index of the abs-icon app
577 int index = launcherModel->findApplication("abs-icon");
579 // Make sure it's invisible and 0 at the beginning
580 QCOMPARE(getASConfig().at(index).value("countVisible").toBool(), false);
581 QCOMPARE(getASConfig().at(index).value("count").toInt(), 0);
583 // Change the count of the abs-icon app through D-Bus
584 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
585 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", QVariant::fromValue(QDBusVariant(55)));
586 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(true)));
588 // Make sure it changed to visible and 55
589 QCOMPARE(getASConfig().at(index).value("countVisible").toBool(), true);
590 QCOMPARE(getASConfig().at(index).value("count").toInt(), 55);
594QTEST_GUILESS_MAIN(LauncherModelTest)
595#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