on 2ba4e6c help sign in
unity8/tests/plugins/Unity/Launcher/launchermodeltest.cpp
697 lines 29 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() override { return nullptr; }
71 // Methods used for mocking (not in the interface)
72 void setFocused(bool focused) { m_focused = focused; Q_EMIT focusedChanged(focused); }
73private:
74 QString m_appId;
75 bool m_focused;
76};
78// This is a mock, specifically to test the LauncherModel
79class MockAppManager: public unity::shell::application::ApplicationManagerInterface
81 Q_OBJECT
82public:
83 MockAppManager(QObject *parent = 0): ApplicationManagerInterface(parent) {}
84 ~MockAppManager() {}
85 int rowCount(const QModelIndex &) const override { return m_list.count(); }
86 QVariant data(const QModelIndex &, int ) const override { return QVariant(); }
87 QString focusedApplicationId() const override {
88 Q_FOREACH(MockApp *app, m_list) {
89 if (app->focused()) return app->appId();
90 }
91 return QString();
92 }
93 unity::shell::application::ApplicationInfoInterface *get(int index) const override { return m_list.at(index); }
94 unity::shell::application::ApplicationInfoInterface *findApplication(const QString &appId) const override {
95 Q_FOREACH(MockApp* app, m_list) {
96 if (app->appId() == appId) {
97 return app;
98 }
99 }
100 return nullptr;
101 }
102 unity::shell::application::ApplicationInfoInterface *startApplication(const QString &, const QStringList &) override { return nullptr; }
103 bool stopApplication(const QString &appId) override {
104 Q_FOREACH(MockApp* app, m_list) {
105 if (app->appId() == appId) {
106 removeApplication(m_list.indexOf(app));
107 return true;
108 }
109 }
110 return false;
111 }
112 bool focusApplication(const QString &appId) {
113 Q_FOREACH(MockApp* app, m_list) {
114 app->setFocused(app->appId() == appId);
115 }
116 Q_EMIT focusedApplicationIdChanged();
117 return true;
118 }
120 void addApplication(MockApp *app) {
121 beginInsertRows(QModelIndex(), count(), count());
122 m_list.append(app);
123 endInsertRows();
124 }
125 void removeApplication(int index) {
126 beginRemoveRows(QModelIndex(), index, index);
127 m_list.takeAt(index)->deleteLater();
128 endRemoveRows();
129 }
130 bool requestFocusApplication(const QString &appId) override { Q_UNUSED(appId); return true; }
132private:
133 QList<MockApp*> m_list;
134};
136class LauncherModelTest : public QObject
138 Q_OBJECT
140private:
141 LauncherModel *launcherModel;
142 MockAppManager *appManager;
144 QList<QVariantMap> getASConfig() {
145 AccountsServiceDBusAdaptor *as = launcherModel->m_asAdapter->m_accounts;
146 QDBusReply<QVariant> reply = as->getUserPropertyAsync(QString::fromUtf8(g_get_user_name()),
147 "com.canonical.unity.AccountsService",
148 "LauncherItems");
149 return qdbus_cast<QList<QVariantMap>>(reply.value().value<QDBusArgument>());
150 }
152private Q_SLOTS:
154 void initTestCase() {
155 qDBusRegisterMetaType<QList<QVariantMap>>();
157 launcherModel = new LauncherModel(this);
158 QCoreApplication::processEvents(); // to let the model register on DBus
159 QCOMPARE(launcherModel->rowCount(QModelIndex()), 0);
161 appManager = new MockAppManager(this);
162 launcherModel->setApplicationManager(appManager);
163 }
165 // Adding 2 apps to the mock appmanager. Both should appear in the launcher.
166 void init() {
167 QDBusInterface accountsInterface(QStringLiteral("org.freedesktop.Accounts"),
168 QStringLiteral("/org/freedesktop/Accounts"),
169 QStringLiteral("org.freedesktop.Accounts"));
170 QDBusReply<bool> addReply = accountsInterface.call(QStringLiteral("AddUser"),
171 QString::fromUtf8(g_get_user_name()));
172 QVERIFY(addReply.isValid());
173 QCOMPARE(addReply.value(), true);
175 appManager->addApplication(new MockApp("abs-icon"));
176 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
178 appManager->addApplication(new MockApp("no-icon"));
179 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
181 launcherModel->m_settings->setStoredApplications(QStringList());
182 }
184 // Removing apps from appmanager and launcher as pinned ones would stick
185 void cleanup() {
186 while (appManager->count() > 0) {
187 appManager->removeApplication(0);
188 }
189 while (launcherModel->rowCount(QModelIndex()) > 0) {
190 launcherModel->requestRemove(launcherModel->get(0)->appId());
191 }
193 QDBusInterface accountsInterface(QStringLiteral("org.freedesktop.Accounts"),
194 QStringLiteral("/org/freedesktop/Accounts"),
195 QStringLiteral("org.freedesktop.Accounts"));
196 QDBusReply<bool> removeReply = accountsInterface.call(QStringLiteral("RemoveUser"),
197 QString::fromUtf8(g_get_user_name()));
198 QVERIFY(removeReply.isValid());
199 QCOMPARE(removeReply.value(), true);
200 }
202 void testMove() {
203 QCOMPARE(launcherModel->get(0)->pinned(), false);
204 QCOMPARE(launcherModel->get(1)->pinned(), false);
206 LauncherItemInterface *item0BeforeMove = launcherModel->get(0);
207 LauncherItemInterface *item1BeforeMove = launcherModel->get(1);
208 launcherModel->move(1, 0);
210 QCOMPARE(item0BeforeMove, launcherModel->get(1));
211 QCOMPARE(item1BeforeMove, launcherModel->get(0));
213 // moved item must be pinned now
214 QCOMPARE(item0BeforeMove->pinned(), false);
215 QCOMPARE(item1BeforeMove->pinned(), true);
216 }
218 void testPinning() {
219 QSignalSpy spy(launcherModel, &LauncherModel::dataChanged);
220 QCOMPARE(launcherModel->get(0)->pinned(), false);
221 QCOMPARE(launcherModel->get(1)->pinned(), false);
222 launcherModel->pin(launcherModel->get(0)->appId());
223 QCOMPARE(launcherModel->get(0)->pinned(), true);
224 QCOMPARE(launcherModel->get(1)->pinned(), false);
225 QCOMPARE(spy.count() > 0, true);
226 QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
228 // App should be pinned now
229 spy.clear();
230 launcherModel->requestRemove(launcherModel->get(0)->appId());
231 QCOMPARE(launcherModel->get(0)->pinned(), false);
232 QCOMPARE(launcherModel->get(1)->pinned(), false);
233 QCOMPARE(spy.count(), 1);
234 QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
236 // Now that the app is unpinned, nothing should change and the signal should not be emitted
237 spy.clear();
238 launcherModel->requestRemove(launcherModel->get(0)->appId());
239 QCOMPARE(launcherModel->get(0)->pinned(), false);
240 QCOMPARE(launcherModel->get(1)->pinned(), false);
241 QCOMPARE(spy.count(), 0);
242 }
244 void testRemove_data() {
246 QTest::addColumn<bool>("pinned");
247 QTest::addColumn<bool>("running");
249 QTest::newRow("non-pinned, running") << false << true;
250 QTest::newRow("pinned, running") << true << true;
251 QTest::newRow("pinned, non-running") << true << false;
252 }
254 void testRemove() {
255 QFETCH(bool, pinned);
256 QFETCH(bool, running);
258 // In the beginning we always have two items
259 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
261 // pin one if required
262 if (pinned) {
263 launcherModel->pin("abs-icon");
264 }
266 // stop it if required
267 if (!running) {
268 appManager->stopApplication("abs-icon");
269 }
271 // Now remove it
272 launcherModel->requestRemove("abs-icon");
274 if (running) {
275 // both apps are running, both apps must still be here
276 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
278 // Item must be unpinned now
279 int index = launcherModel->findApplication("abs-icon");
280 QCOMPARE(launcherModel->get(index)->pinned(), false);
282 } else if (pinned) {
283 // Item 1 must go away, item 0 is here to stay
284 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
285 }
287 // done our checks. now stop the app if was still running
288 if (running) {
289 appManager->stopApplication("abs-icon");
290 }
292 // It needs to go away in any case now
293 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
294 }
296 void testQuickListPinningRemoving() {
297 // we start with 2 unpinned items
298 QCOMPARE(launcherModel->get(0)->pinned(), false);
299 QCOMPARE(launcherModel->get(1)->pinned(), false);
300 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
302 // find the Pin item in the quicklist
303 QuickListModel *model = qobject_cast<QuickListModel*>(launcherModel->get(0)->quickList());
304 int pinActionIndex = -1;
305 for (int i = 0; i < model->rowCount(QModelIndex()); ++i) {
306 if (model->get(i).actionId() == "pin_item") {
307 pinActionIndex = i;
308 break;
309 }
310 }
311 QVERIFY(pinActionIndex >= 0);
313 // trigger pin item quicklist action => Item must be pinned now.
314 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
315 QCOMPARE(launcherModel->get(0)->pinned(), true);
316 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
318 // quicklist needs to transform to remove item. trigger it and check it item goes away
319 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
320 QCOMPARE(launcherModel->get(0)->pinned(), false);
322 // still needs to be here as the app is still here
323 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
324 // close the app
325 appManager->removeApplication(0);
326 // Now it needs to go away
327 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
328 }
330 void testApplicationRunning() {
331 launcherModel->pin("abs-icon");
332 launcherModel->pin("no-icon");
334 QCOMPARE(launcherModel->get(0)->running(), true);
335 QCOMPARE(launcherModel->get(1)->running(), true);
337 appManager->stopApplication("abs-icon");
338 QCOMPARE(launcherModel->get(0)->running(), false);
339 QCOMPARE(launcherModel->get(1)->running(), true);
341 appManager->stopApplication("no-icon");
342 QCOMPARE(launcherModel->get(0)->running(), false);
343 QCOMPARE(launcherModel->get(1)->running(), false);
344 }
346 void testApplicationFocused() {
347 // all apps unfocused at beginning...
348 QCOMPARE(launcherModel->get(0)->focused(), false);
349 QCOMPARE(launcherModel->get(1)->focused(), false);
351 appManager->focusApplication("abs-icon");
352 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
353 QCOMPARE(launcherModel->get(0)->focused(), true);
354 QCOMPARE(launcherModel->get(1)->focused(), false);
356 appManager->focusApplication("no-icon");
357 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
358 QCOMPARE(launcherModel->get(0)->focused(), false);
359 QCOMPARE(launcherModel->get(1)->focused(), true);
360 }
362 void testClosingApps() {
363 // At the start there are 2 items. Let's pin one.
364 launcherModel->pin("abs-icon");
365 while (appManager->count() > 0) {
366 appManager->removeApplication(0);
367 }
368 // The pinned one needs to stay, the other needs to disappear
369 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
370 QCOMPARE(launcherModel->get(0)->appId(), QLatin1String("abs-icon"));
371 }
373 void testQuitMenuItem() {
374 // we have 2 apps running, both should have the Quit action in its quick list
375 QCOMPARE(launcherModel->rowCount(), 2);
377 // stop the second one keeping it pinned so that it doesn't go away
378 launcherModel->pin("no-icon");
379 appManager->stopApplication("no-icon");
381 // find the first Quit item, should be there
382 QuickListModel *model = qobject_cast<QuickListModel*>(launcherModel->get(0)->quickList());
383 int quitActionIndex = -1;
384 for (int i = 0; i < model->rowCount(); ++i) {
385 if (model->get(i).actionId() == "stop_item") {
386 quitActionIndex = i;
387 break;
388 }
389 }
390 QVERIFY(quitActionIndex >= 0);
392 // find the second Quit item, should NOT be there, the app is stopped
393 QuickListModel *model2 = qobject_cast<QuickListModel*>(launcherModel->get(1)->quickList());
394 int quitActionIndex2 = -1;
395 for (int i = 0; i < model2->rowCount(); ++i) {
396 if (model2->get(i).actionId() == "stop_item") {
397 quitActionIndex2 = i;
398 break;
399 }
400 }
401 QVERIFY(quitActionIndex2 == -1);
403 // trigger the first quit item quicklist action
404 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), quitActionIndex);
405 // first app should be gone...
406 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
407 // ... the second app (now at index 0) should still be there, pinned and stopped
408 QCOMPARE(launcherModel->get(0)->appId(), QStringLiteral("no-icon"));
409 QCOMPARE(launcherModel->get(0)->pinned(), true);
410 QCOMPARE(launcherModel->get(0)->running(), false);
411 }
413 void testGetUrlForAppId() {
414 QCOMPARE(launcherModel->getUrlForAppId(QString()), QString());
415 QCOMPARE(launcherModel->getUrlForAppId(""), QString());
416 QCOMPARE(launcherModel->getUrlForAppId("no-name"), QString("application:///no-name.desktop"));
417 QCOMPARE(launcherModel->getUrlForAppId("com.test.good"), QString("application:///com.test.good.desktop"));
418 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application"), QString("appid://com.test.good/application/current-user-version"));
419 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application_1.2.3"), QString("appid://com.test.good/application/current-user-version"));
420 }
422 void testIntrospection() {
423 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "org.freedesktop.DBus.Introspectable");
424 QDBusReply<QString> reply = interface.call("Introspect");
425 QStringList nodes = extractNodes(reply.value());
426 QCOMPARE(nodes.count(), launcherModel->rowCount());
428 appManager->addApplication(new MockApp("foobar"));
429 reply = interface.call("Introspect");
430 nodes = extractNodes(reply.value());
431 QCOMPARE(nodes.contains("foobar"), true);
433 appManager->removeApplication(2);
434 reply = interface.call("Introspect");
435 nodes = extractNodes(reply.value());
436 QCOMPARE(nodes.contains("foobar"), false);
437 }
439 QStringList extractNodes(const QString &introspectionXml) {
440 QXmlStreamReader introspectReply(introspectionXml);
442 QStringList ret;
443 while (!introspectReply.atEnd() && !introspectReply.hasError()) {
444 QXmlStreamReader::TokenType token = introspectReply.readNext();
446 if (token == QXmlStreamReader::StartElement) {
447 if (introspectReply.name() == "node" && introspectReply.attributes().count() > 0) {
448 ret << introspectReply.attributes().value("name").toString();
449 }
450 }
451 }
452 return ret;
453 }
455 void testCountEmblems() {
456 QSignalSpy spy(launcherModel, &LauncherModel::dataChanged);
458 // Call GetAll on abs-icon
459 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
460 QDBusReply<QVariantMap> reply = interface.call("GetAll");
461 QVariantMap map = reply.value();
463 // Check that the alerting-status is still false, and the item on the upper side of the API
464 int index = launcherModel->findApplication("abs-icon");
465 QCOMPARE(index >= 0, true);
466 QVERIFY(launcherModel->get(index)->alerting() == false);
468 // Make sure GetAll returns a map with count and countVisible props
469 QCOMPARE(map.contains("count"), true);
470 QCOMPARE(map.contains("countVisible"), true);
472 // Make sure count is intitilized to 0 and non-visible
473 QCOMPARE(map.value("count").toInt(), 0);
474 QCOMPARE(map.value("countVisible").toBool(), false);
476 // Now make it visible and set it to 55 through D-Bus
477 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", QVariant::fromValue(QDBusVariant(55)));
478 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(true)));
480 // Fetch it again using GetAll
481 reply = interface.call("GetAll");
482 map = reply.value();
484 // Make sure values have changed on the D-Bus interface
485 QCOMPARE(map.value("count").toInt(), 55);
486 QCOMPARE(map.value("countVisible").toBool(), true);
488 // Finally check, that the change to "count" implicitly also set the alerting-state to true
489 QVERIFY(launcherModel->get(index)->alerting() == true);
490 }
492 void testCountEmblemAddsRemovesItem_data() {
493 QTest::addColumn<bool>("isPinned");
494 QTest::addColumn<bool>("isRunning");
495 QTest::addColumn<bool>("startWhenVisible");
496 QTest::newRow("not pinned, not running") << false << false << false;
497 QTest::newRow("pinned, not running") << true << false << false;
498 QTest::newRow("not pinned, not running, starting from notification") << false << false << true;
499 QTest::newRow("pinned, not running, starting from notification") << true << false << true;
500 QTest::newRow("not pinned, running") << false << true << false;
501 QTest::newRow("pinned, running") << true << true << false;
502 }
504 void testCountEmblemAddsRemovesItem() {
505 QFETCH(bool, isPinned);
506 QFETCH(bool, isRunning);
507 QFETCH(bool, startWhenVisible);
509 // Make sure item is here as expected after init() and that count is not visible
510 int index = launcherModel->findApplication("abs-icon");
511 QCOMPARE(index == -1, false);
512 QCOMPARE(launcherModel->get(index)->countVisible(), false);
514 // Pin if we need to
515 if (isPinned) {
516 launcherModel->pin("abs-icon");
517 }
518 QCOMPARE(launcherModel->get(0)->pinned(), isPinned);
520 // Stop it if we need to
521 if (!isRunning) {
522 appManager->stopApplication("abs-icon");
523 }
524 QCOMPARE(launcherModel->findApplication("abs-icon") >= 0, isRunning || isPinned);
527 // set the count emblem to visible
528 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
529 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", QVariant::fromValue(QDBusVariant(55)));
530 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(true)));
532 // Make sure item is here and that count is visible
533 index = launcherModel->findApplication("abs-icon");
534 QCOMPARE(index == -1, false);
535 QCOMPARE(launcherModel->get(index)->countVisible(), true);
537 if (!isRunning && startWhenVisible) {
538 appManager->addApplication(new MockApp("abs-icon"));
539 }
541 // Hide count emblem again
542 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(false)));
544 // Make sure item is shown/hidden as expected
545 index = launcherModel->findApplication("abs-icon");
546 QCOMPARE(index == -1, !isRunning && !isPinned && !startWhenVisible);
547 }
549 void testAlert() {
550 // Check that the alerting-status is still false
551 int index = launcherModel->findApplication("abs-icon");
552 QCOMPARE(index >= 0, true);
553 QVERIFY(launcherModel->get(index)->alerting() == false);
555 // Call Alert() on "abs-icon"
556 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "com.canonical.Unity.Launcher.Item");
557 interface.call("Alert");
559 // Check that the alerting-status is now true
560 QVERIFY(launcherModel->get(index)->alerting() == true);
561 }
563 void testRefreshAfterDeletedDesktopFiles_data() {
564 QTest::addColumn<bool>("deleted");
565 QTest::newRow("have .desktop files") << false;
566 QTest::newRow("deleted .desktop files") << true;
567 }
569 void testRefreshAfterDeletedDesktopFiles() {
570 QFETCH(bool, deleted);
572 // pin both apps
573 launcherModel->pin("abs-icon");
574 launcherModel->pin("no-icon");
575 // close both apps
576 appManager->removeApplication(0);
577 appManager->removeApplication(0);
579 // "delete" the .desktop files
580 QString oldCurrent = QDir::currentPath();
581 if (deleted) {
582 // In testing mode, the launcher searches the current dir for the sample .desktop file
583 // We can make that fail by changing the current dir
584 QDir::setCurrent("..");
585 }
587 // Call refresh
588 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "com.canonical.Unity.Launcher");
589 QDBusReply<void> reply = interface.call("Refresh");
591 // Make sure the call to Refresh returned without error.
592 QCOMPARE(reply.isValid(), true);
594 QCOMPARE(launcherModel->rowCount(), deleted ? 0 : 2);
596 // Restoring current dir
597 QDir::setCurrent(oldCurrent);
598 }
600 void testSettings() {
601 GSettings *settings = launcherModel->m_settings;
602 QSignalSpy spy(launcherModel, &LauncherModel::hint);
604 // Nothing pinned at startup
605 QCOMPARE(settings->storedApplications().count(), 0);
607 // pin both apps
608 launcherModel->pin("abs-icon");
609 launcherModel->pin("no-icon");
610 QCOMPARE(spy.count(), 0);
612 // Now settings should have 2 apps
613 QCOMPARE(settings->storedApplications().count(), 2);
615 // close both apps
616 appManager->removeApplication(0);
617 appManager->removeApplication(0);
619 // Now settings should have 2 apps
620 QCOMPARE(settings->storedApplications().count(), 2);
622 // Now remove 1 app through the backend, make sure one is still there
623 settings->simulateDConfChanged(QStringList() << "abs-icon");
624 QCOMPARE(settings->storedApplications().count(), 1);
625 QCOMPARE(spy.count(), 1);
627 // Check if it disappeared from the frontend too
628 QCOMPARE(launcherModel->rowCount(), 1);
630 // Add them back but in reverse order
631 settings->simulateDConfChanged(QStringList() << "no-icon" << "abs-icon");
632 QCOMPARE(launcherModel->rowCount(), 2);
633 QCOMPARE(launcherModel->get(0)->appId(), QString("no-icon"));
634 QCOMPARE(launcherModel->get(1)->appId(), QString("abs-icon"));
635 QCOMPARE(spy.count(), 2);
636 }
638 void testAddSyncsToAS() {
639 // Make sure launcher and AS are in sync when we start the test
640 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
642 int oldCount = launcherModel->rowCount();
643 appManager->addApplication(new MockApp("rel-icon"));
644 QCOMPARE(launcherModel->rowCount(), oldCount + 1);
645 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
646 }
648 void testRemoveSyncsToAS() {
649 // Make sure launcher and AS are in sync when we start the test
650 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
652 int oldCount = launcherModel->rowCount();
653 appManager->stopApplication("abs-icon");
654 QCOMPARE(launcherModel->rowCount(), oldCount - 1);
655 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
656 }
658 void testMoveSyncsToAS() {
659 // Make sure launcher and AS are in sync when we start the test
660 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
662 for (int i = 0; i < launcherModel->rowCount(); i++) {
663 QString launcherAppId = launcherModel->get(i)->appId();
664 QString asAppId = getASConfig().at(i).value("id").toString();
665 QCOMPARE(launcherAppId, asAppId);
666 }
668 launcherModel->move(0, 1);
670 for (int i = 0; i < launcherModel->rowCount(); i++) {
671 QString launcherAppId = launcherModel->get(i)->appId();
672 QString asAppId = getASConfig().at(i).value("id").toString();
673 QCOMPARE(launcherAppId, asAppId);
674 }
675 }
677 void testCountChangeSyncsToAS() {
678 // Find the index of the abs-icon app
679 int index = launcherModel->findApplication("abs-icon");
681 // Make sure it's invisible and 0 at the beginning
682 QCOMPARE(getASConfig().at(index).value("countVisible").toBool(), false);
683 QCOMPARE(getASConfig().at(index).value("count").toInt(), 0);
685 // Change the count of the abs-icon app through D-Bus
686 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
687 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", QVariant::fromValue(QDBusVariant(55)));
688 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(true)));
690 // Make sure it changed to visible and 55
691 QCOMPARE(getASConfig().at(index).value("countVisible").toBool(), true);
692 QCOMPARE(getASConfig().at(index).value("count").toInt(), 55);
693 }
694};
696QTEST_GUILESS_MAIN(LauncherModelTest)
697#include "launchermodeltest.moc"