on ebaf087 help sign in
unity8/tests/plugins/Unity/Launcher/launchermodeltest.cpp
718 lines 30 kB history raw
1/*
2 * Copyright 2013-2015 Canonical Ltd.
3 *
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.
7 *
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.
12 *
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/>.
15 *
16 * Authors:
17 * Michael Zanetti <michael.zanetti@canonical.com>
18 */
20// unity-api
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"
27#include "gsettings.h"
28#include "asadapter.h"
29#include "AccountsServiceDBusAdaptor.h"
31#include <QtTest>
32#include <QDBusInterface>
33#include <QDBusReply>
34#include <QDBusMetaType>
35#include <QDomDocument>
37#include <glib.h>
39// This is a mock, specifically to test the LauncherModel
40class MockApp: public unity::shell::application::ApplicationInfoInterface
42 Q_OBJECT
43public:
44 MockApp(const QString &appId, QObject *parent = 0): ApplicationInfoInterface(appId, parent), m_appId(appId), m_focused(false) { }
46 RequestedState requestedState() const override { return RequestedRunning; }
47 void setRequestedState(RequestedState) override {}
48 QString appId() const override { return m_appId; }
49 QString name() const override { return "mock"; }
50 QString comment() const override { return "this is a mock"; }
51 QUrl icon() const override { return QUrl(); }
52 ApplicationInfoInterface::Stage stage() const override { return ApplicationInfoInterface::MainStage; }
53 void setStage(ApplicationInfoInterface::Stage) override {}
54 ApplicationInfoInterface::State state() const override { return ApplicationInfoInterface::Running; }
55 bool focused() const override { return m_focused; }
56 QString splashTitle() const override { return QString(); }
57 QUrl splashImage() const override { return QUrl(); }
58 bool splashShowHeader() const override { return true; }
59 QColor splashColor() const override { return QColor(0,0,0,0); }
60 QColor splashColorHeader() const override { return QColor(0,0,0,0); }
61 QColor splashColorFooter() const override { return QColor(0,0,0,0); }
62 Qt::ScreenOrientations supportedOrientations() const override { return Qt::PortraitOrientation; }
63 bool rotatesWindowContents() const override { return false; }
64 bool isTouchApp() const override { return true; }
65 bool exemptFromLifecycle() const override { return false; }
66 void setExemptFromLifecycle(bool) override {}
67 QSize initialSurfaceSize() const override { return QSize(); }
68 void setInitialSurfaceSize(const QSize &) override {}
69 MirSurfaceListInterface* surfaceList() const override { return nullptr; }
70 MirSurfaceListInterface* promptSurfaceList() const override { return nullptr; }
71 int surfaceCount() const override { return m_surfaceCount; }
72 void setSurfaceCount(int count) { m_surfaceCount = count; Q_EMIT surfaceCountChanged(count); }
74 // Methods used for mocking (not in the interface)
75 void setFocused(bool focused) { m_focused = focused; Q_EMIT focusedChanged(focused); }
76private:
77 QString m_appId;
78 bool m_focused;
79 int m_surfaceCount = 0;
80};
82// This is a mock, specifically to test the LauncherModel
83class MockAppManager: public unity::shell::application::ApplicationManagerInterface
85 Q_OBJECT
86public:
87 MockAppManager(QObject *parent = 0): ApplicationManagerInterface(parent) {}
88 ~MockAppManager() {}
89 int rowCount(const QModelIndex &) const override { return m_list.count(); }
90 QVariant data(const QModelIndex &, int ) const override { return QVariant(); }
91 QString focusedApplicationId() const override {
92 Q_FOREACH(MockApp *app, m_list) {
93 if (app->focused()) return app->appId();
94 }
95 return QString();
96 }
97 unity::shell::application::ApplicationInfoInterface *get(int index) const override { return m_list.at(index); }
98 unity::shell::application::ApplicationInfoInterface *findApplication(const QString &appId) const override {
99 Q_FOREACH(MockApp* app, m_list) {
100 if (app->appId() == appId) {
101 return app;
102 }
103 }
104 return nullptr;
105 }
106 unity::shell::application::ApplicationInfoInterface *startApplication(const QString &, const QStringList &) override { return nullptr; }
107 bool stopApplication(const QString &appId) override {
108 Q_FOREACH(MockApp* app, m_list) {
109 if (app->appId() == appId) {
110 removeApplication(m_list.indexOf(app));
111 return true;
112 }
113 }
114 return false;
115 }
116 bool focusApplication(const QString &appId) {
117 Q_FOREACH(MockApp* app, m_list) {
118 app->setFocused(app->appId() == appId);
119 }
120 Q_EMIT focusedApplicationIdChanged();
121 return true;
122 }
124 void addApplication(MockApp *app) {
125 beginInsertRows(QModelIndex(), count(), count());
126 m_list.append(app);
127 endInsertRows();
128 }
129 void removeApplication(int index) {
130 beginRemoveRows(QModelIndex(), index, index);
131 m_list.takeAt(index)->deleteLater();
132 endRemoveRows();
133 }
134 bool requestFocusApplication(const QString &appId) override { Q_UNUSED(appId); return true; }
136private:
137 QList<MockApp*> m_list;
138};
140class LauncherModelTest : public QObject
142 Q_OBJECT
144private:
145 LauncherModel *launcherModel;
146 MockAppManager *appManager;
148 QList<QVariantMap> getASConfig() {
149 AccountsServiceDBusAdaptor *as = launcherModel->m_asAdapter->m_accounts;
150 QDBusReply<QVariant> reply = as->getUserPropertyAsync(QString::fromUtf8(g_get_user_name()),
151 "com.canonical.unity.AccountsService",
152 "LauncherItems");
153 return qdbus_cast<QList<QVariantMap>>(reply.value().value<QDBusArgument>());
154 }
156private Q_SLOTS:
158 void initTestCase() {
159 qDBusRegisterMetaType<QList<QVariantMap>>();
161 launcherModel = new LauncherModel(this);
162 QCoreApplication::processEvents(); // to let the model register on DBus
163 QCOMPARE(launcherModel->rowCount(QModelIndex()), 0);
165 appManager = new MockAppManager(this);
166 launcherModel->setApplicationManager(appManager);
167 }
169 // Adding 2 apps to the mock appmanager. Both should appear in the launcher.
170 void init() {
171 QDBusInterface accountsInterface(QStringLiteral("org.freedesktop.Accounts"),
172 QStringLiteral("/org/freedesktop/Accounts"),
173 QStringLiteral("org.freedesktop.Accounts"));
174 QDBusReply<bool> addReply = accountsInterface.call(QStringLiteral("AddUser"),
175 QString::fromUtf8(g_get_user_name()));
176 QVERIFY(addReply.isValid());
177 QCOMPARE(addReply.value(), true);
179 appManager->addApplication(new MockApp("abs-icon"));
180 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
182 appManager->addApplication(new MockApp("no-icon"));
183 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
185 launcherModel->m_settings->setStoredApplications(QStringList());
186 }
188 // Removing apps from appmanager and launcher as pinned ones would stick
189 void cleanup() {
190 while (appManager->count() > 0) {
191 appManager->removeApplication(0);
192 }
193 while (launcherModel->rowCount(QModelIndex()) > 0) {
194 launcherModel->requestRemove(launcherModel->get(0)->appId());
195 }
197 QDBusInterface accountsInterface(QStringLiteral("org.freedesktop.Accounts"),
198 QStringLiteral("/org/freedesktop/Accounts"),
199 QStringLiteral("org.freedesktop.Accounts"));
200 QDBusReply<bool> removeReply = accountsInterface.call(QStringLiteral("RemoveUser"),
201 QString::fromUtf8(g_get_user_name()));
202 QVERIFY(removeReply.isValid());
203 QCOMPARE(removeReply.value(), true);
204 }
206 void testMove() {
207 QCOMPARE(launcherModel->get(0)->pinned(), false);
208 QCOMPARE(launcherModel->get(1)->pinned(), false);
210 LauncherItemInterface *item0BeforeMove = launcherModel->get(0);
211 LauncherItemInterface *item1BeforeMove = launcherModel->get(1);
212 launcherModel->move(1, 0);
214 QCOMPARE(item0BeforeMove, launcherModel->get(1));
215 QCOMPARE(item1BeforeMove, launcherModel->get(0));
217 // moved item must be pinned now
218 QCOMPARE(item0BeforeMove->pinned(), false);
219 QCOMPARE(item1BeforeMove->pinned(), true);
220 }
222 void testPinning() {
223 QSignalSpy spy(launcherModel, &LauncherModel::dataChanged);
224 QCOMPARE(launcherModel->get(0)->pinned(), false);
225 QCOMPARE(launcherModel->get(1)->pinned(), false);
226 launcherModel->pin(launcherModel->get(0)->appId());
227 QCOMPARE(launcherModel->get(0)->pinned(), true);
228 QCOMPARE(launcherModel->get(1)->pinned(), false);
229 QCOMPARE(spy.count() > 0, true);
230 QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
232 // App should be pinned now
233 spy.clear();
234 launcherModel->requestRemove(launcherModel->get(0)->appId());
235 QCOMPARE(launcherModel->get(0)->pinned(), false);
236 QCOMPARE(launcherModel->get(1)->pinned(), false);
237 QCOMPARE(spy.count(), 1);
238 QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
240 // Now that the app is unpinned, nothing should change and the signal should not be emitted
241 spy.clear();
242 launcherModel->requestRemove(launcherModel->get(0)->appId());
243 QCOMPARE(launcherModel->get(0)->pinned(), false);
244 QCOMPARE(launcherModel->get(1)->pinned(), false);
245 QCOMPARE(spy.count(), 0);
246 }
248 void testRemove_data() {
250 QTest::addColumn<bool>("pinned");
251 QTest::addColumn<bool>("running");
253 QTest::newRow("non-pinned, running") << false << true;
254 QTest::newRow("pinned, running") << true << true;
255 QTest::newRow("pinned, non-running") << true << false;
256 }
258 void testRemove() {
259 QFETCH(bool, pinned);
260 QFETCH(bool, running);
262 // In the beginning we always have two items
263 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
265 // pin one if required
266 if (pinned) {
267 launcherModel->pin("abs-icon");
268 }
270 // stop it if required
271 if (!running) {
272 appManager->stopApplication("abs-icon");
273 }
275 // Now remove it
276 launcherModel->requestRemove("abs-icon");
278 if (running) {
279 // both apps are running, both apps must still be here
280 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
282 // Item must be unpinned now
283 int index = launcherModel->findApplication("abs-icon");
284 QCOMPARE(launcherModel->get(index)->pinned(), false);
286 } else if (pinned) {
287 // Item 1 must go away, item 0 is here to stay
288 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
289 }
291 // done our checks. now stop the app if was still running
292 if (running) {
293 appManager->stopApplication("abs-icon");
294 }
296 // It needs to go away in any case now
297 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
298 }
300 void testQuickListPinningRemoving() {
301 // we start with 2 unpinned items
302 QCOMPARE(launcherModel->get(0)->pinned(), false);
303 QCOMPARE(launcherModel->get(1)->pinned(), false);
304 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
306 // find the Pin item in the quicklist
307 QuickListModel *model = qobject_cast<QuickListModel*>(launcherModel->get(0)->quickList());
308 int pinActionIndex = -1;
309 for (int i = 0; i < model->rowCount(QModelIndex()); ++i) {
310 if (model->get(i).actionId() == "pin_item") {
311 pinActionIndex = i;
312 break;
313 }
314 }
315 QVERIFY(pinActionIndex >= 0);
317 // trigger pin item quicklist action => Item must be pinned now.
318 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
319 QCOMPARE(launcherModel->get(0)->pinned(), true);
320 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
322 // quicklist needs to transform to remove item. trigger it and check it item goes away
323 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
324 QCOMPARE(launcherModel->get(0)->pinned(), false);
326 // still needs to be here as the app is still here
327 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
328 // close the app
329 appManager->removeApplication(0);
330 // Now it needs to go away
331 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
332 }
334 void testApplicationRunning() {
335 launcherModel->pin("abs-icon");
336 launcherModel->pin("no-icon");
338 QCOMPARE(launcherModel->get(0)->running(), true);
339 QCOMPARE(launcherModel->get(1)->running(), true);
341 appManager->stopApplication("abs-icon");
342 QCOMPARE(launcherModel->get(0)->running(), false);
343 QCOMPARE(launcherModel->get(1)->running(), true);
345 appManager->stopApplication("no-icon");
346 QCOMPARE(launcherModel->get(0)->running(), false);
347 QCOMPARE(launcherModel->get(1)->running(), false);
348 }
350 void testApplicationFocused() {
351 // all apps unfocused at beginning...
352 QCOMPARE(launcherModel->get(0)->focused(), false);
353 QCOMPARE(launcherModel->get(1)->focused(), false);
355 appManager->focusApplication("abs-icon");
356 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
357 QCOMPARE(launcherModel->get(0)->focused(), true);
358 QCOMPARE(launcherModel->get(1)->focused(), false);
360 appManager->focusApplication("no-icon");
361 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
362 QCOMPARE(launcherModel->get(0)->focused(), false);
363 QCOMPARE(launcherModel->get(1)->focused(), true);
364 }
366 void testClosingApps() {
367 // At the start there are 2 items. Let's pin one.
368 launcherModel->pin("abs-icon");
369 while (appManager->count() > 0) {
370 appManager->removeApplication(0);
371 }
372 // The pinned one needs to stay, the other needs to disappear
373 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
374 QCOMPARE(launcherModel->get(0)->appId(), QLatin1String("abs-icon"));
375 }
377 void testQuitMenuItem() {
378 // we have 2 apps running, both should have the Quit action in its quick list
379 QCOMPARE(launcherModel->rowCount(), 2);
381 // stop the second one keeping it pinned so that it doesn't go away
382 launcherModel->pin("no-icon");
383 appManager->stopApplication("no-icon");
385 // find the first Quit item, should be there
386 QuickListModel *model = qobject_cast<QuickListModel*>(launcherModel->get(0)->quickList());
387 int quitActionIndex = -1;
388 for (int i = 0; i < model->rowCount(); ++i) {
389 if (model->get(i).actionId() == "stop_item") {
390 quitActionIndex = i;
391 break;
392 }
393 }
394 QVERIFY(quitActionIndex >= 0);
396 // find the second Quit item, should NOT be there, the app is stopped
397 QuickListModel *model2 = qobject_cast<QuickListModel*>(launcherModel->get(1)->quickList());
398 int quitActionIndex2 = -1;
399 for (int i = 0; i < model2->rowCount(); ++i) {
400 if (model2->get(i).actionId() == "stop_item") {
401 quitActionIndex2 = i;
402 break;
403 }
404 }
405 QVERIFY(quitActionIndex2 == -1);
407 // trigger the first quit item quicklist action
408 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), quitActionIndex);
409 // first app should be gone...
410 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
411 // ... the second app (now at index 0) should still be there, pinned and stopped
412 QCOMPARE(launcherModel->get(0)->appId(), QStringLiteral("no-icon"));
413 QCOMPARE(launcherModel->get(0)->pinned(), true);
414 QCOMPARE(launcherModel->get(0)->running(), false);
415 }
417 void testGetUrlForAppId() {
418 QCOMPARE(launcherModel->getUrlForAppId(QString()), QString());
419 QCOMPARE(launcherModel->getUrlForAppId(""), QString());
420 QCOMPARE(launcherModel->getUrlForAppId("no-name"), QString("application:///no-name.desktop"));
421 QCOMPARE(launcherModel->getUrlForAppId("com.test.good"), QString("application:///com.test.good.desktop"));
422 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application"), QString("appid://com.test.good/application/current-user-version"));
423 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application_1.2.3"), QString("appid://com.test.good/application/current-user-version"));
424 }
426 void testIntrospection() {
427 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "org.freedesktop.DBus.Introspectable");
428 QDBusReply<QString> reply = interface.call("Introspect");
429 QStringList nodes = extractNodes(reply.value());
430 QCOMPARE(nodes.count(), launcherModel->rowCount());
432 appManager->addApplication(new MockApp("foobar"));
433 reply = interface.call("Introspect");
434 nodes = extractNodes(reply.value());
435 QCOMPARE(nodes.contains("foobar"), true);
437 appManager->removeApplication(2);
438 reply = interface.call("Introspect");
439 nodes = extractNodes(reply.value());
440 QCOMPARE(nodes.contains("foobar"), false);
441 }
443 QStringList extractNodes(const QString &introspectionXml) {
444 QXmlStreamReader introspectReply(introspectionXml);
446 QStringList ret;
447 while (!introspectReply.atEnd() && !introspectReply.hasError()) {
448 QXmlStreamReader::TokenType token = introspectReply.readNext();
450 if (token == QXmlStreamReader::StartElement) {
451 if (introspectReply.name() == "node" && introspectReply.attributes().count() > 0) {
452 ret << introspectReply.attributes().value("name").toString();
453 }
454 }
455 }
456 return ret;
457 }
459 void testCountEmblems() {
460 QSignalSpy spy(launcherModel, &LauncherModel::dataChanged);
462 // Call GetAll on abs-icon
463 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
464 QDBusReply<QVariantMap> reply = interface.call("GetAll");
465 QVariantMap map = reply.value();
467 // Check that the alerting-status is still false, and the item on the upper side of the API
468 int index = launcherModel->findApplication("abs-icon");
469 QCOMPARE(index >= 0, true);
470 QVERIFY(launcherModel->get(index)->alerting() == false);
472 // Make sure GetAll returns a map with count and countVisible props
473 QCOMPARE(map.contains("count"), true);
474 QCOMPARE(map.contains("countVisible"), true);
476 // Make sure count is intitilized to 0 and non-visible
477 QCOMPARE(map.value("count").toInt(), 0);
478 QCOMPARE(map.value("countVisible").toBool(), false);
480 // Now make it visible and set it to 55 through D-Bus
481 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", QVariant::fromValue(QDBusVariant(55)));
482 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(true)));
484 // Fetch it again using GetAll
485 reply = interface.call("GetAll");
486 map = reply.value();
488 // Make sure values have changed on the D-Bus interface
489 QCOMPARE(map.value("count").toInt(), 55);
490 QCOMPARE(map.value("countVisible").toBool(), true);
492 // Finally check, that the change to "count" implicitly also set the alerting-state to true
493 QVERIFY(launcherModel->get(index)->alerting() == true);
495 // Focus the app, make sure the alert gets cleared
496 appManager->focusApplication("abs-icon");
497 QVERIFY(launcherModel->get(index)->alerting() == false);
498 }
500 void testCountEmblemAddsRemovesItem_data() {
501 QTest::addColumn<bool>("isPinned");
502 QTest::addColumn<bool>("isRunning");
503 QTest::addColumn<bool>("startWhenVisible");
504 QTest::newRow("not pinned, not running") << false << false << false;
505 QTest::newRow("pinned, not running") << true << false << false;
506 QTest::newRow("not pinned, not running, starting from notification") << false << false << true;
507 QTest::newRow("pinned, not running, starting from notification") << true << false << true;
508 QTest::newRow("not pinned, running") << false << true << false;
509 QTest::newRow("pinned, running") << true << true << false;
510 }
512 void testCountEmblemAddsRemovesItem() {
513 QFETCH(bool, isPinned);
514 QFETCH(bool, isRunning);
515 QFETCH(bool, startWhenVisible);
517 // Make sure item is here as expected after init() and that count is not visible
518 int index = launcherModel->findApplication("abs-icon");
519 QCOMPARE(index == -1, false);
520 QCOMPARE(launcherModel->get(index)->countVisible(), false);
522 // Pin if we need to
523 if (isPinned) {
524 launcherModel->pin("abs-icon");
525 }
526 QCOMPARE(launcherModel->get(0)->pinned(), isPinned);
528 // Stop it if we need to
529 if (!isRunning) {
530 appManager->stopApplication("abs-icon");
531 }
532 QCOMPARE(launcherModel->findApplication("abs-icon") >= 0, isRunning || isPinned);
535 // set the count emblem to visible
536 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
537 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", QVariant::fromValue(QDBusVariant(55)));
538 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(true)));
540 // Make sure item is here and that count is visible
541 index = launcherModel->findApplication("abs-icon");
542 QCOMPARE(index == -1, false);
543 QCOMPARE(launcherModel->get(index)->countVisible(), true);
545 if (!isRunning && startWhenVisible) {
546 appManager->addApplication(new MockApp("abs-icon"));
547 }
549 // Hide count emblem again
550 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(false)));
552 // Make sure item is shown/hidden as expected
553 index = launcherModel->findApplication("abs-icon");
554 QCOMPARE(index == -1, !isRunning && !isPinned && !startWhenVisible);
555 }
557 void testAlert() {
558 // Check that the alerting-status is still false
559 int index = launcherModel->findApplication("abs-icon");
560 QCOMPARE(index >= 0, true);
561 QVERIFY(launcherModel->get(index)->alerting() == false);
563 // Call Alert() on "abs-icon"
564 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "com.canonical.Unity.Launcher.Item");
565 interface.call("Alert");
567 // Check that the alerting-status is now true
568 QVERIFY(launcherModel->get(index)->alerting() == true);
570 // Focus the app, make sure the alert gets cleared
571 appManager->focusApplication("abs-icon");
572 QVERIFY(launcherModel->get(index)->alerting() == false);
573 }
575 void testRefreshAfterDeletedDesktopFiles_data() {
576 QTest::addColumn<bool>("deleted");
577 QTest::newRow("have .desktop files") << false;
578 QTest::newRow("deleted .desktop files") << true;
579 }
581 void testRefreshAfterDeletedDesktopFiles() {
582 QFETCH(bool, deleted);
584 // pin both apps
585 launcherModel->pin("abs-icon");
586 launcherModel->pin("no-icon");
587 // close both apps
588 appManager->removeApplication(0);
589 appManager->removeApplication(0);
591 // "delete" the .desktop files
592 QString oldCurrent = QDir::currentPath();
593 if (deleted) {
594 // In testing mode, the launcher searches the current dir for the sample .desktop file
595 // We can make that fail by changing the current dir
596 QDir::setCurrent("..");
597 }
599 // Call refresh
600 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "com.canonical.Unity.Launcher");
601 QDBusReply<void> reply = interface.call("Refresh");
603 // Make sure the call to Refresh returned without error.
604 QCOMPARE(reply.isValid(), true);
606 QCOMPARE(launcherModel->rowCount(), deleted ? 0 : 2);
608 // Restoring current dir
609 QDir::setCurrent(oldCurrent);
610 }
612 void testSettings() {
613 GSettings *settings = launcherModel->m_settings;
614 QSignalSpy spy(launcherModel, &LauncherModel::hint);
616 // Nothing pinned at startup
617 QCOMPARE(settings->storedApplications().count(), 0);
619 // pin both apps
620 launcherModel->pin("abs-icon");
621 launcherModel->pin("no-icon");
622 QCOMPARE(spy.count(), 0);
624 // Now settings should have 2 apps
625 QCOMPARE(settings->storedApplications().count(), 2);
627 // close both apps
628 appManager->removeApplication(0);
629 appManager->removeApplication(0);
631 // Now settings should have 2 apps
632 QCOMPARE(settings->storedApplications().count(), 2);
634 // Now remove 1 app through the backend, make sure one is still there
635 settings->simulateDConfChanged(QStringList() << "abs-icon");
636 QCOMPARE(settings->storedApplications().count(), 1);
637 QCOMPARE(spy.count(), 1);
639 // Check if it disappeared from the frontend too
640 QCOMPARE(launcherModel->rowCount(), 1);
642 // Add them back but in reverse order
643 settings->simulateDConfChanged(QStringList() << "no-icon" << "abs-icon");
644 QCOMPARE(launcherModel->rowCount(), 2);
645 QCOMPARE(launcherModel->get(0)->appId(), QString("no-icon"));
646 QCOMPARE(launcherModel->get(1)->appId(), QString("abs-icon"));
647 QCOMPARE(spy.count(), 2);
648 }
650 void testAddSyncsToAS() {
651 // Make sure launcher and AS are in sync when we start the test
652 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
654 int oldCount = launcherModel->rowCount();
655 appManager->addApplication(new MockApp("rel-icon"));
656 QCOMPARE(launcherModel->rowCount(), oldCount + 1);
657 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
658 }
660 void testRemoveSyncsToAS() {
661 // Make sure launcher and AS are in sync when we start the test
662 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
664 int oldCount = launcherModel->rowCount();
665 appManager->stopApplication("abs-icon");
666 QCOMPARE(launcherModel->rowCount(), oldCount - 1);
667 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
668 }
670 void testMoveSyncsToAS() {
671 // Make sure launcher and AS are in sync when we start the test
672 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
674 for (int i = 0; i < launcherModel->rowCount(); i++) {
675 QString launcherAppId = launcherModel->get(i)->appId();
676 QString asAppId = getASConfig().at(i).value("id").toString();
677 QCOMPARE(launcherAppId, asAppId);
678 }
680 launcherModel->move(0, 1);
682 for (int i = 0; i < launcherModel->rowCount(); i++) {
683 QString launcherAppId = launcherModel->get(i)->appId();
684 QString asAppId = getASConfig().at(i).value("id").toString();
685 QCOMPARE(launcherAppId, asAppId);
686 }
687 }
689 void testCountChangeSyncsToAS() {
690 // Find the index of the abs-icon app
691 int index = launcherModel->findApplication("abs-icon");
693 // Make sure it's invisible and 0 at the beginning
694 QCOMPARE(getASConfig().at(index).value("countVisible").toBool(), false);
695 QCOMPARE(getASConfig().at(index).value("count").toInt(), 0);
697 // Change the count of the abs-icon app through D-Bus
698 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
699 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", QVariant::fromValue(QDBusVariant(55)));
700 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(true)));
702 // Make sure it changed to visible and 55
703 QCOMPARE(getASConfig().at(index).value("countVisible").toBool(), true);
704 QCOMPARE(getASConfig().at(index).value("count").toInt(), 55);
705 }
707 void testSurfaceCountUpdates() {
708 QString appId = launcherModel->get(0)->appId();
710 QCOMPARE(launcherModel->get(0)->surfaceCount(), 0);
711 MockApp *app = qobject_cast<MockApp*>(appManager->findApplication(appId));
712 app->setSurfaceCount(1);
713 QCOMPARE(launcherModel->get(0)->surfaceCount(), 1);
714 }
715};
717QTEST_GUILESS_MAIN(LauncherModelTest)
718#include "launchermodeltest.moc"