2 * Copyright 2013-2014 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 { 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); }
63// This is a mock, specifically to test the LauncherModel
64class MockAppManager: public unity::shell::application::ApplicationManagerInterface
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();
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) {
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));
96 bool focusApplication(const QString &appId) {
97 Q_FOREACH(MockApp* app, m_list) {
98 app->setFocused(app->appId() == appId);
100 Q_EMIT focusedApplicationIdChanged();
104 void unfocusCurrentApplication() { }
106 void addApplication(MockApp *app) {
107 beginInsertRows(QModelIndex(), count(), count());
111 void removeApplication(int index) {
112 beginRemoveRows(QModelIndex(), index, index);
113 m_list.takeAt(index)->deleteLater();
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) {}
123 QList<MockApp*> m_list;
126class LauncherModelTest : public QObject
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>>();
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);
150 // Adding 2 apps to the mock appmanager. Both should appear in the launcher.
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());
161 // Removing apps from appmanager and launcher as pinned ones would stick
163 while (appManager->count() > 0) {
164 appManager->removeApplication(0);
166 while (launcherModel->rowCount(QModelIndex()) > 0) {
167 launcherModel->requestRemove(launcherModel->get(0)->appId());
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);
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
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
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);
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;
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
232 launcherModel->pin("abs-icon");
235 // stop it if required
237 appManager->stopApplication("abs-icon");
241 launcherModel->requestRemove("abs-icon");
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);
252 // Item 1 must go away, item 0 is here to stay
253 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
256 // done our checks. now stop the app if was still running
258 appManager->stopApplication("abs-icon");
261 // It needs to go away in any case now
262 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
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") {
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);
294 appManager->removeApplication(0);
295 // Now it needs to go away
296 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
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);
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);
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"));
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"));
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);
352 QStringList extractNodes(const QString &introspectionXml) {
353 QXmlStreamReader introspectReply(introspectionXml);
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();
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");
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);
403 void testRefreshAfterDeletedDesktopFiles_data() {
404 QTest::addColumn<bool>("deleted");
405 QTest::newRow("have .desktop files") << false;
406 QTest::newRow("deleted .desktop files") << true;
409 void testRefreshAfterDeletedDesktopFiles() {
410 QFETCH(bool, deleted);
413 launcherModel->pin("abs-icon");
414 launcherModel->pin("no-icon");
416 appManager->removeApplication(0);
417 appManager->removeApplication(0);
419 // "delete" the .desktop files
420 QString oldCurrent = QDir::currentPath();
422 // In testing mode, the launcher searches the current dir for the sample .desktop file
423 // We can make that fail by changing the current dir
424 QDir::setCurrent("..");
428 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "com.canonical.Unity.Launcher");
429 QDBusReply<void> reply = interface.call("Refresh");
431 // Make sure the call to Refresh returned without error.
432 QCOMPARE(reply.isValid(), true);
434 QCOMPARE(launcherModel->rowCount(), deleted ? 0 : 2);
436 // Restoring current dir
437 QDir::setCurrent(oldCurrent);
440 void testSettings() {
441 GSettings *settings = launcherModel->m_settings;
442 QSignalSpy spy(launcherModel, SIGNAL(hint()));
444 // Nothing pinned at startup
445 QCOMPARE(settings->storedApplications().count(), 0);
448 launcherModel->pin("abs-icon");
449 launcherModel->pin("no-icon");
450 QCOMPARE(spy.count(), 0);
452 // Now settings should have 2 apps
453 QCOMPARE(settings->storedApplications().count(), 2);
456 appManager->removeApplication(0);
457 appManager->removeApplication(0);
459 // Now settings should have 2 apps
460 QCOMPARE(settings->storedApplications().count(), 2);
462 // Now remove 1 app through the backend, make sure one is still there
463 settings->simulateDConfChanged(QStringList() << "abs-icon");
464 QCOMPARE(settings->storedApplications().count(), 1);
465 QCOMPARE(spy.count(), 1);
467 // Check if it disappeared from the frontend too
468 QCOMPARE(launcherModel->rowCount(), 1);
470 // Add them back but in reverse order
471 settings->simulateDConfChanged(QStringList() << "no-icon" << "abs-icon");
472 QCOMPARE(launcherModel->rowCount(), 2);
473 QCOMPARE(launcherModel->get(0)->appId(), QString("no-icon"));
474 QCOMPARE(launcherModel->get(1)->appId(), QString("abs-icon"));
475 QCOMPARE(spy.count(), 2);
478 void testAddSyncsToAS() {
479 // Make sure launcher and AS are in sync when we start the test
480 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
482 int oldCount = launcherModel->rowCount();
483 appManager->addApplication(new MockApp("rel-icon"));
484 QCOMPARE(launcherModel->rowCount(), oldCount + 1);
485 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
488 void testRemoveSyncsToAS() {
489 // Make sure launcher and AS are in sync when we start the test
490 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
492 int oldCount = launcherModel->rowCount();
493 appManager->stopApplication("abs-icon");
494 QCOMPARE(launcherModel->rowCount(), oldCount - 1);
495 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
498 void testMoveSyncsToAS() {
499 // Make sure launcher and AS are in sync when we start the test
500 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
502 for (int i = 0; i < launcherModel->rowCount(); i++) {
503 QString launcherAppId = launcherModel->get(i)->appId();
504 QString asAppId = getASConfig().at(i).value("id").toString();
505 QCOMPARE(launcherAppId, asAppId);
508 launcherModel->move(0, 1);
510 for (int i = 0; i < launcherModel->rowCount(); i++) {
511 QString launcherAppId = launcherModel->get(i)->appId();
512 QString asAppId = getASConfig().at(i).value("id").toString();
513 QCOMPARE(launcherAppId, asAppId);
517 void testCountChangeSyncsToAS() {
518 // Find the index of the abs-icon app
519 int index = launcherModel->findApplication("abs-icon");
521 // Make sure it's invisible and 0 at the beginning
522 QCOMPARE(getASConfig().at(index).value("countVisible").toBool(), false);
523 QCOMPARE(getASConfig().at(index).value("count").toInt(), 0);
525 // Change the count of the abs-icon app through D-Bus
526 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
527 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", QVariant::fromValue(QDBusVariant(55)));
528 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(true)));
530 // Make sure it changed to visible and 55
531 QCOMPARE(getASConfig().at(index).value("countVisible").toBool(), true);
532 QCOMPARE(getASConfig().at(index).value("count").toInt(), 55);
536QTEST_GUILESS_MAIN(LauncherModelTest)
537#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