2 * Copyright 2013 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"
30#include <QDBusInterface>
32#include <QDomDocument>
34// This is a mock, specifically to test the LauncherModel
35class MockApp: public unity::shell::application::ApplicationInfoInterface
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); }
61// This is a mock, specifically to test the LauncherModel
62class MockAppManager: public unity::shell::application::ApplicationManagerInterface
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();
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) {
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));
94 bool focusApplication(const QString &appId) {
95 Q_FOREACH(MockApp* app, m_list) {
96 app->setFocused(app->appId() == appId);
98 Q_EMIT focusedApplicationIdChanged();
102 void unfocusCurrentApplication() { }
104 void addApplication(MockApp *app) {
105 beginInsertRows(QModelIndex(), count(), count());
109 void removeApplication(int index) {
110 beginRemoveRows(QModelIndex(), index, index);
111 m_list.takeAt(index)->deleteLater();
114 bool requestFocusApplication(const QString &appId) { Q_UNUSED(appId); return true; }
115 bool suspended() const { return false; }
116 void setSuspended(bool) {}
119 QList<MockApp*> m_list;
122class LauncherModelTest : public QObject
127 LauncherModel *launcherModel;
128 MockAppManager *appManager;
132 void initTestCase() {
133 launcherModel = new LauncherModel(this);
134 QCOMPARE(launcherModel->rowCount(QModelIndex()), 0);
136 appManager = new MockAppManager(this);
137 launcherModel->setApplicationManager(appManager);
140 // Adding 2 apps to the mock appmanager. Both should appear in the launcher.
142 appManager->addApplication(new MockApp("abs-icon"));
143 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
145 appManager->addApplication(new MockApp("no-icon"));
146 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
148 launcherModel->m_settings->setStoredApplications(QStringList());
151 // Removing apps from appmanager and launcher as pinned ones would stick
153 while (appManager->count() > 0) {
154 appManager->removeApplication(0);
156 while (launcherModel->rowCount(QModelIndex()) > 0) {
157 launcherModel->requestRemove(launcherModel->get(0)->appId());
162 QCOMPARE(launcherModel->get(0)->pinned(), false);
163 QCOMPARE(launcherModel->get(1)->pinned(), false);
165 LauncherItemInterface *item0BeforeMove = launcherModel->get(0);
166 LauncherItemInterface *item1BeforeMove = launcherModel->get(1);
167 launcherModel->move(1, 0);
169 QCOMPARE(item0BeforeMove, launcherModel->get(1));
170 QCOMPARE(item1BeforeMove, launcherModel->get(0));
172 // moved item must be pinned now
173 QCOMPARE(item0BeforeMove->pinned(), false);
174 QCOMPARE(item1BeforeMove->pinned(), true);
178 QSignalSpy spy(launcherModel, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)));
179 QCOMPARE(launcherModel->get(0)->pinned(), false);
180 QCOMPARE(launcherModel->get(1)->pinned(), false);
181 launcherModel->pin(launcherModel->get(0)->appId());
182 QCOMPARE(launcherModel->get(0)->pinned(), true);
183 QCOMPARE(launcherModel->get(1)->pinned(), false);
184 QCOMPARE(spy.count() > 0, true);
185 QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
188 launcherModel->requestRemove(launcherModel->get(0)->appId());
189 QCOMPARE(launcherModel->get(0)->pinned(), false);
190 QCOMPARE(launcherModel->get(1)->pinned(), false);
191 QCOMPARE(spy.count(), 1);
192 QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
195 void testRemove_data() {
197 QTest::addColumn<bool>("pinned");
198 QTest::addColumn<bool>("running");
200 QTest::newRow("non-pinned, running") << false << true;
201 QTest::newRow("pinned, running") << true << true;
202 QTest::newRow("pinned, non-running") << true << false;
206 QFETCH(bool, pinned);
207 QFETCH(bool, running);
209 // In the beginning we always have two items
210 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
212 // pin one if required
214 launcherModel->pin("abs-icon");
217 // stop it if required
219 appManager->stopApplication("abs-icon");
223 launcherModel->requestRemove("abs-icon");
226 // both apps are running, both apps must still be here
227 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
229 // Item must be unpinned now
230 int index = launcherModel->findApplication("abs-icon");
231 QCOMPARE(launcherModel->get(index)->pinned(), false);
234 // Item 1 must go away, item 0 is here to stay
235 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
238 // done our checks. now stop the app if was still running
240 appManager->stopApplication("abs-icon");
243 // It needs to go away in any case now
244 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
247 void testQuickListPinningRemoving() {
248 // we start with 2 unpinned items
249 QCOMPARE(launcherModel->get(0)->pinned(), false);
250 QCOMPARE(launcherModel->get(1)->pinned(), false);
251 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
253 // find the Pin item in the quicklist
254 QuickListModel *model = qobject_cast<QuickListModel*>(launcherModel->get(0)->quickList());
255 int pinActionIndex = -1;
256 for (int i = 0; i < model->rowCount(QModelIndex()); ++i) {
257 if (model->get(i).actionId() == "pin_item") {
262 QVERIFY(pinActionIndex >= 0);
264 // trigger pin item quicklist action => Item must be pinned now.
265 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
266 QCOMPARE(launcherModel->get(0)->pinned(), true);
267 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
269 // quicklist needs to transform to remove item. trigger it and check it item goes away
270 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
271 QCOMPARE(launcherModel->get(0)->pinned(), false);
273 // still needs to be here as the app is still here
274 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
276 appManager->removeApplication(0);
277 // Now it needs to go away
278 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
281 void testApplicationFocused() {
282 // all apps unfocused at beginning...
283 QCOMPARE(launcherModel->get(0)->focused(), false);
284 QCOMPARE(launcherModel->get(1)->focused(), false);
286 appManager->focusApplication("abs-icon");
287 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
288 QCOMPARE(launcherModel->get(0)->focused(), true);
289 QCOMPARE(launcherModel->get(1)->focused(), false);
291 appManager->focusApplication("no-icon");
292 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
293 QCOMPARE(launcherModel->get(0)->focused(), false);
294 QCOMPARE(launcherModel->get(1)->focused(), true);
297 void testClosingApps() {
298 // At the start there are 2 items. Let's pin one.
299 launcherModel->pin("abs-icon");
300 while (appManager->count() > 0) {
301 appManager->removeApplication(0);
303 // The pinned one needs to stay, the other needs to disappear
304 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
305 QCOMPARE(launcherModel->get(0)->appId(), QLatin1String("abs-icon"));
308 void testGetUrlForAppId() {
309 QCOMPARE(launcherModel->getUrlForAppId(QString()), QString());
310 QCOMPARE(launcherModel->getUrlForAppId(""), QString());
311 QCOMPARE(launcherModel->getUrlForAppId("no-name"), QString("application:///no-name.desktop"));
312 QCOMPARE(launcherModel->getUrlForAppId("com.test.good"), QString("application:///com.test.good.desktop"));
313 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application"), QString("appid://com.test.good/application/current-user-version"));
314 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application_1.2.3"), QString("appid://com.test.good/application/current-user-version"));
317 void testIntrospection() {
318 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "org.freedesktop.DBus.Introspectable");
319 QDBusReply<QString> reply = interface.call("Introspect");
320 QStringList nodes = extractNodes(reply.value());
321 QCOMPARE(nodes.count(), launcherModel->rowCount());
323 appManager->addApplication(new MockApp("foobar"));
324 reply = interface.call("Introspect");
325 nodes = extractNodes(reply.value());
326 QCOMPARE(nodes.contains("foobar"), true);
328 appManager->removeApplication(2);
329 reply = interface.call("Introspect");
330 nodes = extractNodes(reply.value());
331 QCOMPARE(nodes.contains("foobar"), false);
334 QStringList extractNodes(const QString &introspectionXml) {
335 QXmlStreamReader introspectReply(introspectionXml);
338 while (!introspectReply.atEnd() && !introspectReply.hasError()) {
339 QXmlStreamReader::TokenType token = introspectReply.readNext();
341 if (token == QXmlStreamReader::StartElement) {
342 if (introspectReply.name() == "node" && introspectReply.attributes().count() > 0) {
343 ret << introspectReply.attributes().value("name").toString();
350 void testCountEmblems() {
351 // Call GetAll on abs-icon
352 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
353 QDBusReply<QVariantMap> reply = interface.call("GetAll");
354 QVariantMap map = reply.value();
356 // Make sure GetAll returns a map with count and countVisible props
357 QCOMPARE(map.contains("count"), true);
358 QCOMPARE(map.contains("countVisible"), true);
360 // Make sure count is intitilized to 0 and non-visible
361 QCOMPARE(map.value("count").toInt(), 0);
362 QCOMPARE(map.value("countVisible").toBool(), false);
364 // Now make it visible and set it to 55 through D-Bus
365 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", 55);
366 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", true);
368 // Fetch it again using GetAll
369 reply = interface.call("GetAll");
372 // Make sure values have changed on the D-Bus interface
373 QCOMPARE(map.value("count").toInt(), 55);
374 QCOMPARE(map.value("countVisible").toBool(), true);
376 // Now the item on the upper side of the API
377 int index = launcherModel->findApplication("abs-icon");
378 QCOMPARE(index >= 0, true);
380 // And make sure values have changed there as well
381 QCOMPARE(launcherModel->get(index)->countVisible(), true);
382 QCOMPARE(launcherModel->get(index)->count(), 55);
385 void testRefreshAfterDeletedDesktopFiles_data() {
386 QTest::addColumn<bool>("deleted");
387 QTest::newRow("have .desktop files") << false;
388 QTest::newRow("deleted .desktop files") << true;
391 void testRefreshAfterDeletedDesktopFiles() {
392 QFETCH(bool, deleted);
395 launcherModel->pin("abs-icon");
396 launcherModel->pin("no-icon");
398 appManager->removeApplication(0);
399 appManager->removeApplication(0);
401 // "delete" the .desktop files
402 QString oldCurrent = QDir::currentPath();
404 // In testing mode, the launcher searches the current dir for the sample .desktop file
405 // We can make that fail by changing the current dir
406 QDir::setCurrent("..");
410 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "com.canonical.Unity.Launcher");
411 QDBusReply<void> reply = interface.call("Refresh");
413 // Make sure the call to Refresh returned without error.
414 QCOMPARE(reply.isValid(), true);
416 QCOMPARE(launcherModel->rowCount(), deleted ? 0 : 2);
418 // Restoring current dir
419 QDir::setCurrent(oldCurrent);
422 void testSettings() {
423 GSettings *settings = launcherModel->m_settings;
424 QSignalSpy spy(launcherModel, SIGNAL(hintChanged()));
426 // Nothing pinned at startup
427 QCOMPARE(settings->storedApplications().count(), 0);
430 launcherModel->pin("abs-icon");
431 launcherModel->pin("no-icon");
432 QCOMPARE(spy.count(), 0);
434 // Now settings should have 2 apps
435 QCOMPARE(settings->storedApplications().count(), 2);
438 appManager->removeApplication(0);
439 appManager->removeApplication(0);
441 // Now settings should have 2 apps
442 QCOMPARE(settings->storedApplications().count(), 2);
444 // Now remove 1 app through the backend, make sure one is still there
445 settings->simulateDconfChange(QStringList() << "abs-icon");
446 QCOMPARE(settings->storedApplications().count(), 1);
447 QCOMPARE(spy.count(), 1);
449 // Check if it disappeared from the frontend too
450 QCOMPARE(launcherModel->rowCount(), 1);
452 // Add them back but in reverse order
453 settings->simulateDconfChange(QStringList() << "no-icon" << "abs-icon");
454 QCOMPARE(launcherModel->rowCount(), 2);
455 QCOMPARE(launcherModel->get(0)->appId(), QString("no-icon"));
456 QCOMPARE(launcherModel->get(1)->appId(), QString("abs-icon"));
457 QCOMPARE(spy.count(), 2);
461QTEST_GUILESS_MAIN(LauncherModelTest)
462#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