on 04a414e help sign in
unity8/tests/plugins/Unity/Launcher/launchermodeltest.cpp
693 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// This is a mock, specifically to test the LauncherModel
38class MockApp: public unity::shell::application::ApplicationInfoInterface
40 Q_OBJECT
41public:
42 MockApp(const QString &appId, QObject *parent = 0): ApplicationInfoInterface(appId, parent), m_appId(appId), m_focused(false) { }
44 RequestedState requestedState() const override { return RequestedRunning; }
45 void setRequestedState(RequestedState) override {}
46 QString appId() const override { return m_appId; }
47 QString name() const override { return "mock"; }
48 QString comment() const override { return "this is a mock"; }
49 QUrl icon() const override { return QUrl(); }
50 ApplicationInfoInterface::Stage stage() const override { return ApplicationInfoInterface::MainStage; }
51 ApplicationInfoInterface::State state() const override { return ApplicationInfoInterface::Running; }
52 bool focused() const override { return m_focused; }
53 QString splashTitle() const override { return QString(); }
54 QUrl splashImage() const override { return QUrl(); }
55 bool splashShowHeader() const override { return true; }
56 QColor splashColor() const override { return QColor(0,0,0,0); }
57 QColor splashColorHeader() const override { return QColor(0,0,0,0); }
58 QColor splashColorFooter() const override { return QColor(0,0,0,0); }
59 Qt::ScreenOrientations supportedOrientations() const override { return Qt::PortraitOrientation; }
60 bool rotatesWindowContents() const override { return false; }
61 bool isTouchApp() const override { return true; }
62 bool exemptFromLifecycle() const override { return false; }
63 void setExemptFromLifecycle(bool) override {}
65 // Methods used for mocking (not in the interface)
66 void setFocused(bool focused) { m_focused = focused; Q_EMIT focusedChanged(focused); }
67private:
68 QString m_appId;
69 bool m_focused;
70};
72// This is a mock, specifically to test the LauncherModel
73class MockAppManager: public unity::shell::application::ApplicationManagerInterface
75 Q_OBJECT
76public:
77 MockAppManager(QObject *parent = 0): ApplicationManagerInterface(parent) {}
78 ~MockAppManager() {}
79 int rowCount(const QModelIndex &) const override { return m_list.count(); }
80 QVariant data(const QModelIndex &, int ) const override { return QVariant(); }
81 QString focusedApplicationId() const override {
82 Q_FOREACH(MockApp *app, m_list) {
83 if (app->focused()) return app->appId();
84 }
85 return QString();
86 }
87 unity::shell::application::ApplicationInfoInterface *get(int index) const override { return m_list.at(index); }
88 unity::shell::application::ApplicationInfoInterface *findApplication(const QString &appId) const override {
89 Q_FOREACH(MockApp* app, m_list) {
90 if (app->appId() == appId) {
91 return app;
92 }
93 }
94 return nullptr;
95 }
96 unity::shell::application::ApplicationInfoInterface *startApplication(const QString &, const QStringList &) override { return nullptr; }
97 bool stopApplication(const QString &appId) override {
98 Q_FOREACH(MockApp* app, m_list) {
99 if (app->appId() == appId) {
100 removeApplication(m_list.indexOf(app));
101 return true;
102 }
103 }
104 return false;
105 }
106 bool focusApplication(const QString &appId) override {
107 Q_FOREACH(MockApp* app, m_list) {
108 app->setFocused(app->appId() == appId);
109 }
110 Q_EMIT focusedApplicationIdChanged();
111 return true;
112 }
114 void unfocusCurrentApplication() override { }
116 void addApplication(MockApp *app) {
117 beginInsertRows(QModelIndex(), count(), count());
118 m_list.append(app);
119 endInsertRows();
120 }
121 void removeApplication(int index) {
122 beginRemoveRows(QModelIndex(), index, index);
123 m_list.takeAt(index)->deleteLater();
124 endRemoveRows();
125 }
126 bool requestFocusApplication(const QString &appId) override { Q_UNUSED(appId); return true; }
128private:
129 QList<MockApp*> m_list;
130};
132class LauncherModelTest : public QObject
134 Q_OBJECT
136private:
137 LauncherModel *launcherModel;
138 MockAppManager *appManager;
140 QList<QVariantMap> getASConfig() {
141 AccountsServiceDBusAdaptor *as = launcherModel->m_asAdapter->m_accounts;
142 QDBusReply<QVariant> reply = as->getUserPropertyAsync(QString(qgetenv("USER")),
143 "com.canonical.unity.AccountsService",
144 "LauncherItems");
145 return qdbus_cast<QList<QVariantMap>>(reply.value().value<QDBusArgument>());
146 }
148private Q_SLOTS:
150 void initTestCase() {
151 qDBusRegisterMetaType<QList<QVariantMap>>();
153 launcherModel = new LauncherModel(this);
154 QCoreApplication::processEvents(); // to let the model register on DBus
155 QCOMPARE(launcherModel->rowCount(QModelIndex()), 0);
157 appManager = new MockAppManager(this);
158 launcherModel->setApplicationManager(appManager);
159 }
161 // Adding 2 apps to the mock appmanager. Both should appear in the launcher.
162 void init() {
163 QDBusInterface accountsInterface(QStringLiteral("org.freedesktop.Accounts"),
164 QStringLiteral("/org/freedesktop/Accounts"),
165 QStringLiteral("org.freedesktop.Accounts"));
166 QDBusReply<bool> addReply = accountsInterface.call(QStringLiteral("AddUser"),
167 QString(qgetenv("USER")));
168 QVERIFY(addReply.isValid());
169 QCOMPARE(addReply.value(), true);
171 appManager->addApplication(new MockApp("abs-icon"));
172 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
174 appManager->addApplication(new MockApp("no-icon"));
175 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
177 launcherModel->m_settings->setStoredApplications(QStringList());
178 }
180 // Removing apps from appmanager and launcher as pinned ones would stick
181 void cleanup() {
182 while (appManager->count() > 0) {
183 appManager->removeApplication(0);
184 }
185 while (launcherModel->rowCount(QModelIndex()) > 0) {
186 launcherModel->requestRemove(launcherModel->get(0)->appId());
187 }
189 QDBusInterface accountsInterface(QStringLiteral("org.freedesktop.Accounts"),
190 QStringLiteral("/org/freedesktop/Accounts"),
191 QStringLiteral("org.freedesktop.Accounts"));
192 QDBusReply<bool> removeReply = accountsInterface.call(QStringLiteral("RemoveUser"),
193 QString(qgetenv("USER")));
194 QVERIFY(removeReply.isValid());
195 QCOMPARE(removeReply.value(), true);
196 }
198 void testMove() {
199 QCOMPARE(launcherModel->get(0)->pinned(), false);
200 QCOMPARE(launcherModel->get(1)->pinned(), false);
202 LauncherItemInterface *item0BeforeMove = launcherModel->get(0);
203 LauncherItemInterface *item1BeforeMove = launcherModel->get(1);
204 launcherModel->move(1, 0);
206 QCOMPARE(item0BeforeMove, launcherModel->get(1));
207 QCOMPARE(item1BeforeMove, launcherModel->get(0));
209 // moved item must be pinned now
210 QCOMPARE(item0BeforeMove->pinned(), false);
211 QCOMPARE(item1BeforeMove->pinned(), true);
212 }
214 void testPinning() {
215 QSignalSpy spy(launcherModel, &LauncherModel::dataChanged);
216 QCOMPARE(launcherModel->get(0)->pinned(), false);
217 QCOMPARE(launcherModel->get(1)->pinned(), false);
218 launcherModel->pin(launcherModel->get(0)->appId());
219 QCOMPARE(launcherModel->get(0)->pinned(), true);
220 QCOMPARE(launcherModel->get(1)->pinned(), false);
221 QCOMPARE(spy.count() > 0, true);
222 QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
224 // App should be pinned now
225 spy.clear();
226 launcherModel->requestRemove(launcherModel->get(0)->appId());
227 QCOMPARE(launcherModel->get(0)->pinned(), false);
228 QCOMPARE(launcherModel->get(1)->pinned(), false);
229 QCOMPARE(spy.count(), 1);
230 QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
232 // Now that the app is unpinned, nothing should change and the signal should not be emitted
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(), 0);
238 }
240 void testRemove_data() {
242 QTest::addColumn<bool>("pinned");
243 QTest::addColumn<bool>("running");
245 QTest::newRow("non-pinned, running") << false << true;
246 QTest::newRow("pinned, running") << true << true;
247 QTest::newRow("pinned, non-running") << true << false;
248 }
250 void testRemove() {
251 QFETCH(bool, pinned);
252 QFETCH(bool, running);
254 // In the beginning we always have two items
255 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
257 // pin one if required
258 if (pinned) {
259 launcherModel->pin("abs-icon");
260 }
262 // stop it if required
263 if (!running) {
264 appManager->stopApplication("abs-icon");
265 }
267 // Now remove it
268 launcherModel->requestRemove("abs-icon");
270 if (running) {
271 // both apps are running, both apps must still be here
272 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
274 // Item must be unpinned now
275 int index = launcherModel->findApplication("abs-icon");
276 QCOMPARE(launcherModel->get(index)->pinned(), false);
278 } else if (pinned) {
279 // Item 1 must go away, item 0 is here to stay
280 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
281 }
283 // done our checks. now stop the app if was still running
284 if (running) {
285 appManager->stopApplication("abs-icon");
286 }
288 // It needs to go away in any case now
289 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
290 }
292 void testQuickListPinningRemoving() {
293 // we start with 2 unpinned items
294 QCOMPARE(launcherModel->get(0)->pinned(), false);
295 QCOMPARE(launcherModel->get(1)->pinned(), false);
296 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
298 // find the Pin item in the quicklist
299 QuickListModel *model = qobject_cast<QuickListModel*>(launcherModel->get(0)->quickList());
300 int pinActionIndex = -1;
301 for (int i = 0; i < model->rowCount(QModelIndex()); ++i) {
302 if (model->get(i).actionId() == "pin_item") {
303 pinActionIndex = i;
304 break;
305 }
306 }
307 QVERIFY(pinActionIndex >= 0);
309 // trigger pin item quicklist action => Item must be pinned now.
310 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
311 QCOMPARE(launcherModel->get(0)->pinned(), true);
312 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
314 // quicklist needs to transform to remove item. trigger it and check it item goes away
315 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
316 QCOMPARE(launcherModel->get(0)->pinned(), false);
318 // still needs to be here as the app is still here
319 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
320 // close the app
321 appManager->removeApplication(0);
322 // Now it needs to go away
323 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
324 }
326 void testApplicationRunning() {
327 launcherModel->pin("abs-icon");
328 launcherModel->pin("no-icon");
330 QCOMPARE(launcherModel->get(0)->running(), true);
331 QCOMPARE(launcherModel->get(1)->running(), true);
333 appManager->stopApplication("abs-icon");
334 QCOMPARE(launcherModel->get(0)->running(), false);
335 QCOMPARE(launcherModel->get(1)->running(), true);
337 appManager->stopApplication("no-icon");
338 QCOMPARE(launcherModel->get(0)->running(), false);
339 QCOMPARE(launcherModel->get(1)->running(), false);
340 }
342 void testApplicationFocused() {
343 // all apps unfocused at beginning...
344 QCOMPARE(launcherModel->get(0)->focused(), false);
345 QCOMPARE(launcherModel->get(1)->focused(), false);
347 appManager->focusApplication("abs-icon");
348 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
349 QCOMPARE(launcherModel->get(0)->focused(), true);
350 QCOMPARE(launcherModel->get(1)->focused(), false);
352 appManager->focusApplication("no-icon");
353 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
354 QCOMPARE(launcherModel->get(0)->focused(), false);
355 QCOMPARE(launcherModel->get(1)->focused(), true);
356 }
358 void testClosingApps() {
359 // At the start there are 2 items. Let's pin one.
360 launcherModel->pin("abs-icon");
361 while (appManager->count() > 0) {
362 appManager->removeApplication(0);
363 }
364 // The pinned one needs to stay, the other needs to disappear
365 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
366 QCOMPARE(launcherModel->get(0)->appId(), QLatin1String("abs-icon"));
367 }
369 void testQuitMenuItem() {
370 // we have 2 apps running, both should have the Quit action in its quick list
371 QCOMPARE(launcherModel->rowCount(), 2);
373 // stop the second one keeping it pinned so that it doesn't go away
374 launcherModel->pin("no-icon");
375 appManager->stopApplication("no-icon");
377 // find the first Quit item, should be there
378 QuickListModel *model = qobject_cast<QuickListModel*>(launcherModel->get(0)->quickList());
379 int quitActionIndex = -1;
380 for (int i = 0; i < model->rowCount(); ++i) {
381 if (model->get(i).actionId() == "stop_item") {
382 quitActionIndex = i;
383 break;
384 }
385 }
386 QVERIFY(quitActionIndex >= 0);
388 // find the second Quit item, should NOT be there, the app is stopped
389 QuickListModel *model2 = qobject_cast<QuickListModel*>(launcherModel->get(1)->quickList());
390 int quitActionIndex2 = -1;
391 for (int i = 0; i < model2->rowCount(); ++i) {
392 if (model2->get(i).actionId() == "stop_item") {
393 quitActionIndex2 = i;
394 break;
395 }
396 }
397 QVERIFY(quitActionIndex2 == -1);
399 // trigger the first quit item quicklist action
400 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), quitActionIndex);
401 // first app should be gone...
402 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
403 // ... the second app (now at index 0) should still be there, pinned and stopped
404 QCOMPARE(launcherModel->get(0)->appId(), QStringLiteral("no-icon"));
405 QCOMPARE(launcherModel->get(0)->pinned(), true);
406 QCOMPARE(launcherModel->get(0)->running(), false);
407 }
409 void testGetUrlForAppId() {
410 QCOMPARE(launcherModel->getUrlForAppId(QString()), QString());
411 QCOMPARE(launcherModel->getUrlForAppId(""), QString());
412 QCOMPARE(launcherModel->getUrlForAppId("no-name"), QString("application:///no-name.desktop"));
413 QCOMPARE(launcherModel->getUrlForAppId("com.test.good"), QString("application:///com.test.good.desktop"));
414 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application"), QString("appid://com.test.good/application/current-user-version"));
415 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application_1.2.3"), QString("appid://com.test.good/application/current-user-version"));
416 }
418 void testIntrospection() {
419 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "org.freedesktop.DBus.Introspectable");
420 QDBusReply<QString> reply = interface.call("Introspect");
421 QStringList nodes = extractNodes(reply.value());
422 QCOMPARE(nodes.count(), launcherModel->rowCount());
424 appManager->addApplication(new MockApp("foobar"));
425 reply = interface.call("Introspect");
426 nodes = extractNodes(reply.value());
427 QCOMPARE(nodes.contains("foobar"), true);
429 appManager->removeApplication(2);
430 reply = interface.call("Introspect");
431 nodes = extractNodes(reply.value());
432 QCOMPARE(nodes.contains("foobar"), false);
433 }
435 QStringList extractNodes(const QString &introspectionXml) {
436 QXmlStreamReader introspectReply(introspectionXml);
438 QStringList ret;
439 while (!introspectReply.atEnd() && !introspectReply.hasError()) {
440 QXmlStreamReader::TokenType token = introspectReply.readNext();
442 if (token == QXmlStreamReader::StartElement) {
443 if (introspectReply.name() == "node" && introspectReply.attributes().count() > 0) {
444 ret << introspectReply.attributes().value("name").toString();
445 }
446 }
447 }
448 return ret;
449 }
451 void testCountEmblems() {
452 QSignalSpy spy(launcherModel, &LauncherModel::dataChanged);
454 // Call GetAll on abs-icon
455 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
456 QDBusReply<QVariantMap> reply = interface.call("GetAll");
457 QVariantMap map = reply.value();
459 // Check that the alerting-status is still false, and the item on the upper side of the API
460 int index = launcherModel->findApplication("abs-icon");
461 QCOMPARE(index >= 0, true);
462 QVERIFY(launcherModel->get(index)->alerting() == false);
464 // Make sure GetAll returns a map with count and countVisible props
465 QCOMPARE(map.contains("count"), true);
466 QCOMPARE(map.contains("countVisible"), true);
468 // Make sure count is intitilized to 0 and non-visible
469 QCOMPARE(map.value("count").toInt(), 0);
470 QCOMPARE(map.value("countVisible").toBool(), false);
472 // Now make it visible and set it to 55 through D-Bus
473 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", QVariant::fromValue(QDBusVariant(55)));
474 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(true)));
476 // Fetch it again using GetAll
477 reply = interface.call("GetAll");
478 map = reply.value();
480 // Make sure values have changed on the D-Bus interface
481 QCOMPARE(map.value("count").toInt(), 55);
482 QCOMPARE(map.value("countVisible").toBool(), true);
484 // Finally check, that the change to "count" implicitly also set the alerting-state to true
485 QVERIFY(launcherModel->get(index)->alerting() == true);
486 }
488 void testCountEmblemAddsRemovesItem_data() {
489 QTest::addColumn<bool>("isPinned");
490 QTest::addColumn<bool>("isRunning");
491 QTest::addColumn<bool>("startWhenVisible");
492 QTest::newRow("not pinned, not running") << false << false << false;
493 QTest::newRow("pinned, not running") << true << false << false;
494 QTest::newRow("not pinned, not running, starting from notification") << false << false << true;
495 QTest::newRow("pinned, not running, starting from notification") << true << false << true;
496 QTest::newRow("not pinned, running") << false << true << false;
497 QTest::newRow("pinned, running") << true << true << false;
498 }
500 void testCountEmblemAddsRemovesItem() {
501 QFETCH(bool, isPinned);
502 QFETCH(bool, isRunning);
503 QFETCH(bool, startWhenVisible);
505 // Make sure item is here as expected after init() and that count is not visible
506 int index = launcherModel->findApplication("abs-icon");
507 QCOMPARE(index == -1, false);
508 QCOMPARE(launcherModel->get(index)->countVisible(), false);
510 // Pin if we need to
511 if (isPinned) {
512 launcherModel->pin("abs-icon");
513 }
514 QCOMPARE(launcherModel->get(0)->pinned(), isPinned);
516 // Stop it if we need to
517 if (!isRunning) {
518 appManager->stopApplication("abs-icon");
519 }
520 QCOMPARE(launcherModel->findApplication("abs-icon") >= 0, isRunning || isPinned);
523 // set the count emblem to visible
524 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
525 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", QVariant::fromValue(QDBusVariant(55)));
526 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(true)));
528 // Make sure item is here and that count is visible
529 index = launcherModel->findApplication("abs-icon");
530 QCOMPARE(index == -1, false);
531 QCOMPARE(launcherModel->get(index)->countVisible(), true);
533 if (!isRunning && startWhenVisible) {
534 appManager->addApplication(new MockApp("abs-icon"));
535 }
537 // Hide count emblem again
538 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(false)));
540 // Make sure item is shown/hidden as expected
541 index = launcherModel->findApplication("abs-icon");
542 QCOMPARE(index == -1, !isRunning && !isPinned && !startWhenVisible);
543 }
545 void testAlert() {
546 // Check that the alerting-status is still false
547 int index = launcherModel->findApplication("abs-icon");
548 QCOMPARE(index >= 0, true);
549 QVERIFY(launcherModel->get(index)->alerting() == false);
551 // Call Alert() on "abs-icon"
552 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "com.canonical.Unity.Launcher.Item");
553 interface.call("Alert");
555 // Check that the alerting-status is now true
556 QVERIFY(launcherModel->get(index)->alerting() == true);
557 }
559 void testRefreshAfterDeletedDesktopFiles_data() {
560 QTest::addColumn<bool>("deleted");
561 QTest::newRow("have .desktop files") << false;
562 QTest::newRow("deleted .desktop files") << true;
563 }
565 void testRefreshAfterDeletedDesktopFiles() {
566 QFETCH(bool, deleted);
568 // pin both apps
569 launcherModel->pin("abs-icon");
570 launcherModel->pin("no-icon");
571 // close both apps
572 appManager->removeApplication(0);
573 appManager->removeApplication(0);
575 // "delete" the .desktop files
576 QString oldCurrent = QDir::currentPath();
577 if (deleted) {
578 // In testing mode, the launcher searches the current dir for the sample .desktop file
579 // We can make that fail by changing the current dir
580 QDir::setCurrent("..");
581 }
583 // Call refresh
584 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "com.canonical.Unity.Launcher");
585 QDBusReply<void> reply = interface.call("Refresh");
587 // Make sure the call to Refresh returned without error.
588 QCOMPARE(reply.isValid(), true);
590 QCOMPARE(launcherModel->rowCount(), deleted ? 0 : 2);
592 // Restoring current dir
593 QDir::setCurrent(oldCurrent);
594 }
596 void testSettings() {
597 GSettings *settings = launcherModel->m_settings;
598 QSignalSpy spy(launcherModel, &LauncherModel::hint);
600 // Nothing pinned at startup
601 QCOMPARE(settings->storedApplications().count(), 0);
603 // pin both apps
604 launcherModel->pin("abs-icon");
605 launcherModel->pin("no-icon");
606 QCOMPARE(spy.count(), 0);
608 // Now settings should have 2 apps
609 QCOMPARE(settings->storedApplications().count(), 2);
611 // close both apps
612 appManager->removeApplication(0);
613 appManager->removeApplication(0);
615 // Now settings should have 2 apps
616 QCOMPARE(settings->storedApplications().count(), 2);
618 // Now remove 1 app through the backend, make sure one is still there
619 settings->simulateDConfChanged(QStringList() << "abs-icon");
620 QCOMPARE(settings->storedApplications().count(), 1);
621 QCOMPARE(spy.count(), 1);
623 // Check if it disappeared from the frontend too
624 QCOMPARE(launcherModel->rowCount(), 1);
626 // Add them back but in reverse order
627 settings->simulateDConfChanged(QStringList() << "no-icon" << "abs-icon");
628 QCOMPARE(launcherModel->rowCount(), 2);
629 QCOMPARE(launcherModel->get(0)->appId(), QString("no-icon"));
630 QCOMPARE(launcherModel->get(1)->appId(), QString("abs-icon"));
631 QCOMPARE(spy.count(), 2);
632 }
634 void testAddSyncsToAS() {
635 // Make sure launcher and AS are in sync when we start the test
636 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
638 int oldCount = launcherModel->rowCount();
639 appManager->addApplication(new MockApp("rel-icon"));
640 QCOMPARE(launcherModel->rowCount(), oldCount + 1);
641 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
642 }
644 void testRemoveSyncsToAS() {
645 // Make sure launcher and AS are in sync when we start the test
646 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
648 int oldCount = launcherModel->rowCount();
649 appManager->stopApplication("abs-icon");
650 QCOMPARE(launcherModel->rowCount(), oldCount - 1);
651 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
652 }
654 void testMoveSyncsToAS() {
655 // Make sure launcher and AS are in sync when we start the test
656 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
658 for (int i = 0; i < launcherModel->rowCount(); i++) {
659 QString launcherAppId = launcherModel->get(i)->appId();
660 QString asAppId = getASConfig().at(i).value("id").toString();
661 QCOMPARE(launcherAppId, asAppId);
662 }
664 launcherModel->move(0, 1);
666 for (int i = 0; i < launcherModel->rowCount(); i++) {
667 QString launcherAppId = launcherModel->get(i)->appId();
668 QString asAppId = getASConfig().at(i).value("id").toString();
669 QCOMPARE(launcherAppId, asAppId);
670 }
671 }
673 void testCountChangeSyncsToAS() {
674 // Find the index of the abs-icon app
675 int index = launcherModel->findApplication("abs-icon");
677 // Make sure it's invisible and 0 at the beginning
678 QCOMPARE(getASConfig().at(index).value("countVisible").toBool(), false);
679 QCOMPARE(getASConfig().at(index).value("count").toInt(), 0);
681 // Change the count of the abs-icon app through D-Bus
682 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
683 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", QVariant::fromValue(QDBusVariant(55)));
684 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(true)));
686 // Make sure it changed to visible and 55
687 QCOMPARE(getASConfig().at(index).value("countVisible").toBool(), true);
688 QCOMPARE(getASConfig().at(index).value("count").toInt(), 55);
689 }
690};
692QTEST_GUILESS_MAIN(LauncherModelTest)
693#include "launchermodeltest.moc"