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) {}
117 bool forceDashActive() const { return false; }
118 void setForceDashActive(bool) {}
121 QList<MockApp*> m_list;
124class LauncherModelTest : public QObject
129 LauncherModel *launcherModel;
130 MockAppManager *appManager;
134 void initTestCase() {
135 launcherModel = new LauncherModel(this);
136 QCOMPARE(launcherModel->rowCount(QModelIndex()), 0);
138 appManager = new MockAppManager(this);
139 launcherModel->setApplicationManager(appManager);
142 // Adding 2 apps to the mock appmanager. Both should appear in the launcher.
144 appManager->addApplication(new MockApp("abs-icon"));
145 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
147 appManager->addApplication(new MockApp("no-icon"));
148 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
150 launcherModel->m_settings->setStoredApplications(QStringList());
153 // Removing apps from appmanager and launcher as pinned ones would stick
155 while (appManager->count() > 0) {
156 appManager->removeApplication(0);
158 while (launcherModel->rowCount(QModelIndex()) > 0) {
159 launcherModel->requestRemove(launcherModel->get(0)->appId());
164 QCOMPARE(launcherModel->get(0)->pinned(), false);
165 QCOMPARE(launcherModel->get(1)->pinned(), false);
167 LauncherItemInterface *item0BeforeMove = launcherModel->get(0);
168 LauncherItemInterface *item1BeforeMove = launcherModel->get(1);
169 launcherModel->move(1, 0);
171 QCOMPARE(item0BeforeMove, launcherModel->get(1));
172 QCOMPARE(item1BeforeMove, launcherModel->get(0));
174 // moved item must be pinned now
175 QCOMPARE(item0BeforeMove->pinned(), false);
176 QCOMPARE(item1BeforeMove->pinned(), true);
180 QSignalSpy spy(launcherModel, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)));
181 QCOMPARE(launcherModel->get(0)->pinned(), false);
182 QCOMPARE(launcherModel->get(1)->pinned(), false);
183 launcherModel->pin(launcherModel->get(0)->appId());
184 QCOMPARE(launcherModel->get(0)->pinned(), true);
185 QCOMPARE(launcherModel->get(1)->pinned(), false);
186 QCOMPARE(spy.count() > 0, true);
187 QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
190 launcherModel->requestRemove(launcherModel->get(0)->appId());
191 QCOMPARE(launcherModel->get(0)->pinned(), false);
192 QCOMPARE(launcherModel->get(1)->pinned(), false);
193 QCOMPARE(spy.count(), 1);
194 QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
197 launcherModel->requestRemove(launcherModel->get(0)->appId());
198 QCOMPARE(launcherModel->get(0)->pinned(), false);
199 QCOMPARE(launcherModel->get(1)->pinned(), false);
200 QCOMPARE(spy.count(), 1);
201 QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
204 void testRemove_data() {
206 QTest::addColumn<bool>("pinned");
207 QTest::addColumn<bool>("running");
209 QTest::newRow("non-pinned, running") << false << true;
210 QTest::newRow("pinned, running") << true << true;
211 QTest::newRow("pinned, non-running") << true << false;
215 QFETCH(bool, pinned);
216 QFETCH(bool, running);
218 // In the beginning we always have two items
219 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
221 // pin one if required
223 launcherModel->pin("abs-icon");
226 // stop it if required
228 appManager->stopApplication("abs-icon");
232 launcherModel->requestRemove("abs-icon");
235 // both apps are running, both apps must still be here
236 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
238 // Item must be unpinned now
239 int index = launcherModel->findApplication("abs-icon");
240 QCOMPARE(launcherModel->get(index)->pinned(), false);
243 // Item 1 must go away, item 0 is here to stay
244 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
247 // done our checks. now stop the app if was still running
249 appManager->stopApplication("abs-icon");
252 // It needs to go away in any case now
253 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
256 void testQuickListPinningRemoving() {
257 // we start with 2 unpinned items
258 QCOMPARE(launcherModel->get(0)->pinned(), false);
259 QCOMPARE(launcherModel->get(1)->pinned(), false);
260 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
262 // find the Pin item in the quicklist
263 QuickListModel *model = qobject_cast<QuickListModel*>(launcherModel->get(0)->quickList());
264 int pinActionIndex = -1;
265 for (int i = 0; i < model->rowCount(QModelIndex()); ++i) {
266 if (model->get(i).actionId() == "pin_item") {
271 QVERIFY(pinActionIndex >= 0);
273 // trigger pin item quicklist action => Item must be pinned now.
274 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
275 QCOMPARE(launcherModel->get(0)->pinned(), true);
276 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
278 // quicklist needs to transform to remove item. trigger it and check it item goes away
279 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
280 QCOMPARE(launcherModel->get(0)->pinned(), false);
282 // still needs to be here as the app is still here
283 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
285 appManager->removeApplication(0);
286 // Now it needs to go away
287 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
290 void testApplicationFocused() {
291 // all apps unfocused at beginning...
292 QCOMPARE(launcherModel->get(0)->focused(), false);
293 QCOMPARE(launcherModel->get(1)->focused(), false);
295 appManager->focusApplication("abs-icon");
296 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
297 QCOMPARE(launcherModel->get(0)->focused(), true);
298 QCOMPARE(launcherModel->get(1)->focused(), false);
300 appManager->focusApplication("no-icon");
301 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
302 QCOMPARE(launcherModel->get(0)->focused(), false);
303 QCOMPARE(launcherModel->get(1)->focused(), true);
306 void testClosingApps() {
307 // At the start there are 2 items. Let's pin one.
308 launcherModel->pin("abs-icon");
309 while (appManager->count() > 0) {
310 appManager->removeApplication(0);
312 // The pinned one needs to stay, the other needs to disappear
313 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
314 QCOMPARE(launcherModel->get(0)->appId(), QLatin1String("abs-icon"));
317 void testGetUrlForAppId() {
318 QCOMPARE(launcherModel->getUrlForAppId(QString()), QString());
319 QCOMPARE(launcherModel->getUrlForAppId(""), QString());
320 QCOMPARE(launcherModel->getUrlForAppId("no-name"), QString("application:///no-name.desktop"));
321 QCOMPARE(launcherModel->getUrlForAppId("com.test.good"), QString("application:///com.test.good.desktop"));
322 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application"), QString("appid://com.test.good/application/current-user-version"));
323 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application_1.2.3"), QString("appid://com.test.good/application/current-user-version"));
326 void testIntrospection() {
327 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "org.freedesktop.DBus.Introspectable");
328 QDBusReply<QString> reply = interface.call("Introspect");
329 QStringList nodes = extractNodes(reply.value());
330 QCOMPARE(nodes.count(), launcherModel->rowCount());
332 appManager->addApplication(new MockApp("foobar"));
333 reply = interface.call("Introspect");
334 nodes = extractNodes(reply.value());
335 QCOMPARE(nodes.contains("foobar"), true);
337 appManager->removeApplication(2);
338 reply = interface.call("Introspect");
339 nodes = extractNodes(reply.value());
340 QCOMPARE(nodes.contains("foobar"), false);
343 QStringList extractNodes(const QString &introspectionXml) {
344 QXmlStreamReader introspectReply(introspectionXml);
347 while (!introspectReply.atEnd() && !introspectReply.hasError()) {
348 QXmlStreamReader::TokenType token = introspectReply.readNext();
350 if (token == QXmlStreamReader::StartElement) {
351 if (introspectReply.name() == "node" && introspectReply.attributes().count() > 0) {
352 ret << introspectReply.attributes().value("name").toString();
359 void testCountEmblems() {
360 // Call GetAll on abs-icon
361 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
362 QDBusReply<QVariantMap> reply = interface.call("GetAll");
363 QVariantMap map = reply.value();
365 // Make sure GetAll returns a map with count and countVisible props
366 QCOMPARE(map.contains("count"), true);
367 QCOMPARE(map.contains("countVisible"), true);
369 // Make sure count is intitilized to 0 and non-visible
370 QCOMPARE(map.value("count").toInt(), 0);
371 QCOMPARE(map.value("countVisible").toBool(), false);
373 // Now make it visible and set it to 55 through D-Bus
374 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", 55);
375 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", true);
377 // Fetch it again using GetAll
378 reply = interface.call("GetAll");
381 // Make sure values have changed on the D-Bus interface
382 QCOMPARE(map.value("count").toInt(), 55);
383 QCOMPARE(map.value("countVisible").toBool(), true);
385 // Now the item on the upper side of the API
386 int index = launcherModel->findApplication("abs-icon");
387 QCOMPARE(index >= 0, true);
389 // And make sure values have changed there as well
390 QCOMPARE(launcherModel->get(index)->countVisible(), true);
391 QCOMPARE(launcherModel->get(index)->count(), 55);
394 void testRefreshAfterDeletedDesktopFiles_data() {
395 QTest::addColumn<bool>("deleted");
396 QTest::newRow("have .desktop files") << false;
397 QTest::newRow("deleted .desktop files") << true;
400 void testRefreshAfterDeletedDesktopFiles() {
401 QFETCH(bool, deleted);
404 launcherModel->pin("abs-icon");
405 launcherModel->pin("no-icon");
407 appManager->removeApplication(0);
408 appManager->removeApplication(0);
410 // "delete" the .desktop files
411 QString oldCurrent = QDir::currentPath();
413 // In testing mode, the launcher searches the current dir for the sample .desktop file
414 // We can make that fail by changing the current dir
415 QDir::setCurrent("..");
419 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "com.canonical.Unity.Launcher");
420 QDBusReply<void> reply = interface.call("Refresh");
422 // Make sure the call to Refresh returned without error.
423 QCOMPARE(reply.isValid(), true);
425 QCOMPARE(launcherModel->rowCount(), deleted ? 0 : 2);
427 // Restoring current dir
428 QDir::setCurrent(oldCurrent);
431 void testSettings() {
432 GSettings *settings = launcherModel->m_settings;
433 QSignalSpy spy(launcherModel, SIGNAL(hintChanged()));
435 // Nothing pinned at startup
436 QCOMPARE(settings->storedApplications().count(), 0);
439 launcherModel->pin("abs-icon");
440 launcherModel->pin("no-icon");
441 QCOMPARE(spy.count(), 0);
443 // Now settings should have 2 apps
444 QCOMPARE(settings->storedApplications().count(), 2);
447 appManager->removeApplication(0);
448 appManager->removeApplication(0);
450 // Now settings should have 2 apps
451 QCOMPARE(settings->storedApplications().count(), 2);
453 // Now remove 1 app through the backend, make sure one is still there
454 settings->simulateDconfChange(QStringList() << "abs-icon");
455 QCOMPARE(settings->storedApplications().count(), 1);
456 QCOMPARE(spy.count(), 1);
458 // Check if it disappeared from the frontend too
459 QCOMPARE(launcherModel->rowCount(), 1);
461 // Add them back but in reverse order
462 settings->simulateDconfChange(QStringList() << "no-icon" << "abs-icon");
463 QCOMPARE(launcherModel->rowCount(), 2);
464 QCOMPARE(launcherModel->get(0)->appId(), QString("no-icon"));
465 QCOMPARE(launcherModel->get(1)->appId(), QString("abs-icon"));
466 QCOMPARE(spy.count(), 2);
470QTEST_GUILESS_MAIN(LauncherModelTest)
471#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