on 1fb8ffc help sign in
unity8/tests/plugins/Unity/Launcher/launchermodeltest.cpp
691 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; }
63 // Methods used for mocking (not in the interface)
64 void setFocused(bool focused) { m_focused = focused; Q_EMIT focusedChanged(focused); }
65private:
66 QString m_appId;
67 bool m_focused;
68};
70// This is a mock, specifically to test the LauncherModel
71class MockAppManager: public unity::shell::application::ApplicationManagerInterface
73 Q_OBJECT
74public:
75 MockAppManager(QObject *parent = 0): ApplicationManagerInterface(parent) {}
76 ~MockAppManager() {}
77 int rowCount(const QModelIndex &) const override { return m_list.count(); }
78 QVariant data(const QModelIndex &, int ) const override { return QVariant(); }
79 QString focusedApplicationId() const override {
80 Q_FOREACH(MockApp *app, m_list) {
81 if (app->focused()) return app->appId();
82 }
83 return QString();
84 }
85 unity::shell::application::ApplicationInfoInterface *get(int index) const override { return m_list.at(index); }
86 unity::shell::application::ApplicationInfoInterface *findApplication(const QString &appId) const override {
87 Q_FOREACH(MockApp* app, m_list) {
88 if (app->appId() == appId) {
89 return app;
90 }
91 }
92 return nullptr;
93 }
94 unity::shell::application::ApplicationInfoInterface *startApplication(const QString &, const QStringList &) override { return nullptr; }
95 bool stopApplication(const QString &appId) override {
96 Q_FOREACH(MockApp* app, m_list) {
97 if (app->appId() == appId) {
98 removeApplication(m_list.indexOf(app));
99 return true;
100 }
101 }
102 return false;
103 }
104 bool focusApplication(const QString &appId) override {
105 Q_FOREACH(MockApp* app, m_list) {
106 app->setFocused(app->appId() == appId);
107 }
108 Q_EMIT focusedApplicationIdChanged();
109 return true;
110 }
112 void unfocusCurrentApplication() override { }
114 void addApplication(MockApp *app) {
115 beginInsertRows(QModelIndex(), count(), count());
116 m_list.append(app);
117 endInsertRows();
118 }
119 void removeApplication(int index) {
120 beginRemoveRows(QModelIndex(), index, index);
121 m_list.takeAt(index)->deleteLater();
122 endRemoveRows();
123 }
124 bool requestFocusApplication(const QString &appId) override { Q_UNUSED(appId); return true; }
126private:
127 QList<MockApp*> m_list;
128};
130class LauncherModelTest : public QObject
132 Q_OBJECT
134private:
135 LauncherModel *launcherModel;
136 MockAppManager *appManager;
138 QList<QVariantMap> getASConfig() {
139 AccountsServiceDBusAdaptor *as = launcherModel->m_asAdapter->m_accounts;
140 QDBusReply<QVariant> reply = as->getUserPropertyAsync(QString(qgetenv("USER")),
141 "com.canonical.unity.AccountsService",
142 "LauncherItems");
143 return qdbus_cast<QList<QVariantMap>>(reply.value().value<QDBusArgument>());
144 }
146private Q_SLOTS:
148 void initTestCase() {
149 qDBusRegisterMetaType<QList<QVariantMap>>();
151 launcherModel = new LauncherModel(this);
152 QCoreApplication::processEvents(); // to let the model register on DBus
153 QCOMPARE(launcherModel->rowCount(QModelIndex()), 0);
155 appManager = new MockAppManager(this);
156 launcherModel->setApplicationManager(appManager);
157 }
159 // Adding 2 apps to the mock appmanager. Both should appear in the launcher.
160 void init() {
161 QDBusInterface accountsInterface(QStringLiteral("org.freedesktop.Accounts"),
162 QStringLiteral("/org/freedesktop/Accounts"),
163 QStringLiteral("org.freedesktop.Accounts"));
164 QDBusReply<bool> addReply = accountsInterface.call(QStringLiteral("AddUser"),
165 QString(qgetenv("USER")));
166 QVERIFY(addReply.isValid());
167 QCOMPARE(addReply.value(), true);
169 appManager->addApplication(new MockApp("abs-icon"));
170 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
172 appManager->addApplication(new MockApp("no-icon"));
173 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
175 launcherModel->m_settings->setStoredApplications(QStringList());
176 }
178 // Removing apps from appmanager and launcher as pinned ones would stick
179 void cleanup() {
180 while (appManager->count() > 0) {
181 appManager->removeApplication(0);
182 }
183 while (launcherModel->rowCount(QModelIndex()) > 0) {
184 launcherModel->requestRemove(launcherModel->get(0)->appId());
185 }
187 QDBusInterface accountsInterface(QStringLiteral("org.freedesktop.Accounts"),
188 QStringLiteral("/org/freedesktop/Accounts"),
189 QStringLiteral("org.freedesktop.Accounts"));
190 QDBusReply<bool> removeReply = accountsInterface.call(QStringLiteral("RemoveUser"),
191 QString(qgetenv("USER")));
192 QVERIFY(removeReply.isValid());
193 QCOMPARE(removeReply.value(), true);
194 }
196 void testMove() {
197 QCOMPARE(launcherModel->get(0)->pinned(), false);
198 QCOMPARE(launcherModel->get(1)->pinned(), false);
200 LauncherItemInterface *item0BeforeMove = launcherModel->get(0);
201 LauncherItemInterface *item1BeforeMove = launcherModel->get(1);
202 launcherModel->move(1, 0);
204 QCOMPARE(item0BeforeMove, launcherModel->get(1));
205 QCOMPARE(item1BeforeMove, launcherModel->get(0));
207 // moved item must be pinned now
208 QCOMPARE(item0BeforeMove->pinned(), false);
209 QCOMPARE(item1BeforeMove->pinned(), true);
210 }
212 void testPinning() {
213 QSignalSpy spy(launcherModel, &LauncherModel::dataChanged);
214 QCOMPARE(launcherModel->get(0)->pinned(), false);
215 QCOMPARE(launcherModel->get(1)->pinned(), false);
216 launcherModel->pin(launcherModel->get(0)->appId());
217 QCOMPARE(launcherModel->get(0)->pinned(), true);
218 QCOMPARE(launcherModel->get(1)->pinned(), false);
219 QCOMPARE(spy.count() > 0, true);
220 QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
222 // App should be pinned now
223 spy.clear();
224 launcherModel->requestRemove(launcherModel->get(0)->appId());
225 QCOMPARE(launcherModel->get(0)->pinned(), false);
226 QCOMPARE(launcherModel->get(1)->pinned(), false);
227 QCOMPARE(spy.count(), 1);
228 QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
230 // Now that the app is unpinned, nothing should change and the signal should not be emitted
231 spy.clear();
232 launcherModel->requestRemove(launcherModel->get(0)->appId());
233 QCOMPARE(launcherModel->get(0)->pinned(), false);
234 QCOMPARE(launcherModel->get(1)->pinned(), false);
235 QCOMPARE(spy.count(), 0);
236 }
238 void testRemove_data() {
240 QTest::addColumn<bool>("pinned");
241 QTest::addColumn<bool>("running");
243 QTest::newRow("non-pinned, running") << false << true;
244 QTest::newRow("pinned, running") << true << true;
245 QTest::newRow("pinned, non-running") << true << false;
246 }
248 void testRemove() {
249 QFETCH(bool, pinned);
250 QFETCH(bool, running);
252 // In the beginning we always have two items
253 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
255 // pin one if required
256 if (pinned) {
257 launcherModel->pin("abs-icon");
258 }
260 // stop it if required
261 if (!running) {
262 appManager->stopApplication("abs-icon");
263 }
265 // Now remove it
266 launcherModel->requestRemove("abs-icon");
268 if (running) {
269 // both apps are running, both apps must still be here
270 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
272 // Item must be unpinned now
273 int index = launcherModel->findApplication("abs-icon");
274 QCOMPARE(launcherModel->get(index)->pinned(), false);
276 } else if (pinned) {
277 // Item 1 must go away, item 0 is here to stay
278 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
279 }
281 // done our checks. now stop the app if was still running
282 if (running) {
283 appManager->stopApplication("abs-icon");
284 }
286 // It needs to go away in any case now
287 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
288 }
290 void testQuickListPinningRemoving() {
291 // we start with 2 unpinned items
292 QCOMPARE(launcherModel->get(0)->pinned(), false);
293 QCOMPARE(launcherModel->get(1)->pinned(), false);
294 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
296 // find the Pin item in the quicklist
297 QuickListModel *model = qobject_cast<QuickListModel*>(launcherModel->get(0)->quickList());
298 int pinActionIndex = -1;
299 for (int i = 0; i < model->rowCount(QModelIndex()); ++i) {
300 if (model->get(i).actionId() == "pin_item") {
301 pinActionIndex = i;
302 break;
303 }
304 }
305 QVERIFY(pinActionIndex >= 0);
307 // trigger pin item quicklist action => Item must be pinned now.
308 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
309 QCOMPARE(launcherModel->get(0)->pinned(), true);
310 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
312 // quicklist needs to transform to remove item. trigger it and check it item goes away
313 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
314 QCOMPARE(launcherModel->get(0)->pinned(), false);
316 // still needs to be here as the app is still here
317 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
318 // close the app
319 appManager->removeApplication(0);
320 // Now it needs to go away
321 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
322 }
324 void testApplicationRunning() {
325 launcherModel->pin("abs-icon");
326 launcherModel->pin("no-icon");
328 QCOMPARE(launcherModel->get(0)->running(), true);
329 QCOMPARE(launcherModel->get(1)->running(), true);
331 appManager->stopApplication("abs-icon");
332 QCOMPARE(launcherModel->get(0)->running(), false);
333 QCOMPARE(launcherModel->get(1)->running(), true);
335 appManager->stopApplication("no-icon");
336 QCOMPARE(launcherModel->get(0)->running(), false);
337 QCOMPARE(launcherModel->get(1)->running(), false);
338 }
340 void testApplicationFocused() {
341 // all apps unfocused at beginning...
342 QCOMPARE(launcherModel->get(0)->focused(), false);
343 QCOMPARE(launcherModel->get(1)->focused(), false);
345 appManager->focusApplication("abs-icon");
346 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
347 QCOMPARE(launcherModel->get(0)->focused(), true);
348 QCOMPARE(launcherModel->get(1)->focused(), false);
350 appManager->focusApplication("no-icon");
351 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
352 QCOMPARE(launcherModel->get(0)->focused(), false);
353 QCOMPARE(launcherModel->get(1)->focused(), true);
354 }
356 void testClosingApps() {
357 // At the start there are 2 items. Let's pin one.
358 launcherModel->pin("abs-icon");
359 while (appManager->count() > 0) {
360 appManager->removeApplication(0);
361 }
362 // The pinned one needs to stay, the other needs to disappear
363 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
364 QCOMPARE(launcherModel->get(0)->appId(), QLatin1String("abs-icon"));
365 }
367 void testQuitMenuItem() {
368 // we have 2 apps running, both should have the Quit action in its quick list
369 QCOMPARE(launcherModel->rowCount(), 2);
371 // stop the second one keeping it pinned so that it doesn't go away
372 launcherModel->pin("no-icon");
373 appManager->stopApplication("no-icon");
375 // find the first Quit item, should be there
376 QuickListModel *model = qobject_cast<QuickListModel*>(launcherModel->get(0)->quickList());
377 int quitActionIndex = -1;
378 for (int i = 0; i < model->rowCount(); ++i) {
379 if (model->get(i).actionId() == "stop_item") {
380 quitActionIndex = i;
381 break;
382 }
383 }
384 QVERIFY(quitActionIndex >= 0);
386 // find the second Quit item, should NOT be there, the app is stopped
387 QuickListModel *model2 = qobject_cast<QuickListModel*>(launcherModel->get(1)->quickList());
388 int quitActionIndex2 = -1;
389 for (int i = 0; i < model2->rowCount(); ++i) {
390 if (model2->get(i).actionId() == "stop_item") {
391 quitActionIndex2 = i;
392 break;
393 }
394 }
395 QVERIFY(quitActionIndex2 == -1);
397 // trigger the first quit item quicklist action
398 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), quitActionIndex);
399 // first app should be gone...
400 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
401 // ... the second app (now at index 0) should still be there, pinned and stopped
402 QCOMPARE(launcherModel->get(0)->appId(), QStringLiteral("no-icon"));
403 QCOMPARE(launcherModel->get(0)->pinned(), true);
404 QCOMPARE(launcherModel->get(0)->running(), false);
405 }
407 void testGetUrlForAppId() {
408 QCOMPARE(launcherModel->getUrlForAppId(QString()), QString());
409 QCOMPARE(launcherModel->getUrlForAppId(""), QString());
410 QCOMPARE(launcherModel->getUrlForAppId("no-name"), QString("application:///no-name.desktop"));
411 QCOMPARE(launcherModel->getUrlForAppId("com.test.good"), QString("application:///com.test.good.desktop"));
412 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application"), QString("appid://com.test.good/application/current-user-version"));
413 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application_1.2.3"), QString("appid://com.test.good/application/current-user-version"));
414 }
416 void testIntrospection() {
417 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "org.freedesktop.DBus.Introspectable");
418 QDBusReply<QString> reply = interface.call("Introspect");
419 QStringList nodes = extractNodes(reply.value());
420 QCOMPARE(nodes.count(), launcherModel->rowCount());
422 appManager->addApplication(new MockApp("foobar"));
423 reply = interface.call("Introspect");
424 nodes = extractNodes(reply.value());
425 QCOMPARE(nodes.contains("foobar"), true);
427 appManager->removeApplication(2);
428 reply = interface.call("Introspect");
429 nodes = extractNodes(reply.value());
430 QCOMPARE(nodes.contains("foobar"), false);
431 }
433 QStringList extractNodes(const QString &introspectionXml) {
434 QXmlStreamReader introspectReply(introspectionXml);
436 QStringList ret;
437 while (!introspectReply.atEnd() && !introspectReply.hasError()) {
438 QXmlStreamReader::TokenType token = introspectReply.readNext();
440 if (token == QXmlStreamReader::StartElement) {
441 if (introspectReply.name() == "node" && introspectReply.attributes().count() > 0) {
442 ret << introspectReply.attributes().value("name").toString();
443 }
444 }
445 }
446 return ret;
447 }
449 void testCountEmblems() {
450 QSignalSpy spy(launcherModel, &LauncherModel::dataChanged);
452 // Call GetAll on abs-icon
453 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
454 QDBusReply<QVariantMap> reply = interface.call("GetAll");
455 QVariantMap map = reply.value();
457 // Check that the alerting-status is still false, and the item on the upper side of the API
458 int index = launcherModel->findApplication("abs-icon");
459 QCOMPARE(index >= 0, true);
460 QVERIFY(launcherModel->get(index)->alerting() == false);
462 // Make sure GetAll returns a map with count and countVisible props
463 QCOMPARE(map.contains("count"), true);
464 QCOMPARE(map.contains("countVisible"), true);
466 // Make sure count is intitilized to 0 and non-visible
467 QCOMPARE(map.value("count").toInt(), 0);
468 QCOMPARE(map.value("countVisible").toBool(), false);
470 // Now make it visible and set it to 55 through D-Bus
471 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", QVariant::fromValue(QDBusVariant(55)));
472 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(true)));
474 // Fetch it again using GetAll
475 reply = interface.call("GetAll");
476 map = reply.value();
478 // Make sure values have changed on the D-Bus interface
479 QCOMPARE(map.value("count").toInt(), 55);
480 QCOMPARE(map.value("countVisible").toBool(), true);
482 // Finally check, that the change to "count" implicitly also set the alerting-state to true
483 QVERIFY(launcherModel->get(index)->alerting() == true);
484 }
486 void testCountEmblemAddsRemovesItem_data() {
487 QTest::addColumn<bool>("isPinned");
488 QTest::addColumn<bool>("isRunning");
489 QTest::addColumn<bool>("startWhenVisible");
490 QTest::newRow("not pinned, not running") << false << false << false;
491 QTest::newRow("pinned, not running") << true << false << false;
492 QTest::newRow("not pinned, not running, starting from notification") << false << false << true;
493 QTest::newRow("pinned, not running, starting from notification") << true << false << true;
494 QTest::newRow("not pinned, running") << false << true << false;
495 QTest::newRow("pinned, running") << true << true << false;
496 }
498 void testCountEmblemAddsRemovesItem() {
499 QFETCH(bool, isPinned);
500 QFETCH(bool, isRunning);
501 QFETCH(bool, startWhenVisible);
503 // Make sure item is here as expected after init() and that count is not visible
504 int index = launcherModel->findApplication("abs-icon");
505 QCOMPARE(index == -1, false);
506 QCOMPARE(launcherModel->get(index)->countVisible(), false);
508 // Pin if we need to
509 if (isPinned) {
510 launcherModel->pin("abs-icon");
511 }
512 QCOMPARE(launcherModel->get(0)->pinned(), isPinned);
514 // Stop it if we need to
515 if (!isRunning) {
516 appManager->stopApplication("abs-icon");
517 }
518 QCOMPARE(launcherModel->findApplication("abs-icon") >= 0, isRunning || isPinned);
521 // set the count emblem to visible
522 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
523 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", QVariant::fromValue(QDBusVariant(55)));
524 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(true)));
526 // Make sure item is here and that count is visible
527 index = launcherModel->findApplication("abs-icon");
528 QCOMPARE(index == -1, false);
529 QCOMPARE(launcherModel->get(index)->countVisible(), true);
531 if (!isRunning && startWhenVisible) {
532 appManager->addApplication(new MockApp("abs-icon"));
533 }
535 // Hide count emblem again
536 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(false)));
538 // Make sure item is shown/hidden as expected
539 index = launcherModel->findApplication("abs-icon");
540 QCOMPARE(index == -1, !isRunning && !isPinned && !startWhenVisible);
541 }
543 void testAlert() {
544 // Check that the alerting-status is still false
545 int index = launcherModel->findApplication("abs-icon");
546 QCOMPARE(index >= 0, true);
547 QVERIFY(launcherModel->get(index)->alerting() == false);
549 // Call Alert() on "abs-icon"
550 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "com.canonical.Unity.Launcher.Item");
551 interface.call("Alert");
553 // Check that the alerting-status is now true
554 QVERIFY(launcherModel->get(index)->alerting() == true);
555 }
557 void testRefreshAfterDeletedDesktopFiles_data() {
558 QTest::addColumn<bool>("deleted");
559 QTest::newRow("have .desktop files") << false;
560 QTest::newRow("deleted .desktop files") << true;
561 }
563 void testRefreshAfterDeletedDesktopFiles() {
564 QFETCH(bool, deleted);
566 // pin both apps
567 launcherModel->pin("abs-icon");
568 launcherModel->pin("no-icon");
569 // close both apps
570 appManager->removeApplication(0);
571 appManager->removeApplication(0);
573 // "delete" the .desktop files
574 QString oldCurrent = QDir::currentPath();
575 if (deleted) {
576 // In testing mode, the launcher searches the current dir for the sample .desktop file
577 // We can make that fail by changing the current dir
578 QDir::setCurrent("..");
579 }
581 // Call refresh
582 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "com.canonical.Unity.Launcher");
583 QDBusReply<void> reply = interface.call("Refresh");
585 // Make sure the call to Refresh returned without error.
586 QCOMPARE(reply.isValid(), true);
588 QCOMPARE(launcherModel->rowCount(), deleted ? 0 : 2);
590 // Restoring current dir
591 QDir::setCurrent(oldCurrent);
592 }
594 void testSettings() {
595 GSettings *settings = launcherModel->m_settings;
596 QSignalSpy spy(launcherModel, &LauncherModel::hint);
598 // Nothing pinned at startup
599 QCOMPARE(settings->storedApplications().count(), 0);
601 // pin both apps
602 launcherModel->pin("abs-icon");
603 launcherModel->pin("no-icon");
604 QCOMPARE(spy.count(), 0);
606 // Now settings should have 2 apps
607 QCOMPARE(settings->storedApplications().count(), 2);
609 // close both apps
610 appManager->removeApplication(0);
611 appManager->removeApplication(0);
613 // Now settings should have 2 apps
614 QCOMPARE(settings->storedApplications().count(), 2);
616 // Now remove 1 app through the backend, make sure one is still there
617 settings->simulateDConfChanged(QStringList() << "abs-icon");
618 QCOMPARE(settings->storedApplications().count(), 1);
619 QCOMPARE(spy.count(), 1);
621 // Check if it disappeared from the frontend too
622 QCOMPARE(launcherModel->rowCount(), 1);
624 // Add them back but in reverse order
625 settings->simulateDConfChanged(QStringList() << "no-icon" << "abs-icon");
626 QCOMPARE(launcherModel->rowCount(), 2);
627 QCOMPARE(launcherModel->get(0)->appId(), QString("no-icon"));
628 QCOMPARE(launcherModel->get(1)->appId(), QString("abs-icon"));
629 QCOMPARE(spy.count(), 2);
630 }
632 void testAddSyncsToAS() {
633 // Make sure launcher and AS are in sync when we start the test
634 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
636 int oldCount = launcherModel->rowCount();
637 appManager->addApplication(new MockApp("rel-icon"));
638 QCOMPARE(launcherModel->rowCount(), oldCount + 1);
639 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
640 }
642 void testRemoveSyncsToAS() {
643 // Make sure launcher and AS are in sync when we start the test
644 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
646 int oldCount = launcherModel->rowCount();
647 appManager->stopApplication("abs-icon");
648 QCOMPARE(launcherModel->rowCount(), oldCount - 1);
649 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
650 }
652 void testMoveSyncsToAS() {
653 // Make sure launcher and AS are in sync when we start the test
654 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
656 for (int i = 0; i < launcherModel->rowCount(); i++) {
657 QString launcherAppId = launcherModel->get(i)->appId();
658 QString asAppId = getASConfig().at(i).value("id").toString();
659 QCOMPARE(launcherAppId, asAppId);
660 }
662 launcherModel->move(0, 1);
664 for (int i = 0; i < launcherModel->rowCount(); i++) {
665 QString launcherAppId = launcherModel->get(i)->appId();
666 QString asAppId = getASConfig().at(i).value("id").toString();
667 QCOMPARE(launcherAppId, asAppId);
668 }
669 }
671 void testCountChangeSyncsToAS() {
672 // Find the index of the abs-icon app
673 int index = launcherModel->findApplication("abs-icon");
675 // Make sure it's invisible and 0 at the beginning
676 QCOMPARE(getASConfig().at(index).value("countVisible").toBool(), false);
677 QCOMPARE(getASConfig().at(index).value("count").toInt(), 0);
679 // Change the count of the abs-icon app through D-Bus
680 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
681 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", QVariant::fromValue(QDBusVariant(55)));
682 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(true)));
684 // Make sure it changed to visible and 55
685 QCOMPARE(getASConfig().at(index).value("countVisible").toBool(), true);
686 QCOMPARE(getASConfig().at(index).value("count").toInt(), 55);
687 }
688};
690QTEST_GUILESS_MAIN(LauncherModelTest)
691#include "launchermodeltest.moc"