on 09f8113 help sign in
unity8/tests/plugins/Unity/Launcher/launchermodeltest.cpp
696 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 void setStage(ApplicationInfoInterface::Stage) override {}
52 ApplicationInfoInterface::State state() const override { return ApplicationInfoInterface::Running; }
53 bool focused() const override { return m_focused; }
54 QString splashTitle() const override { return QString(); }
55 QUrl splashImage() const override { return QUrl(); }
56 bool splashShowHeader() const override { return true; }
57 QColor splashColor() const override { return QColor(0,0,0,0); }
58 QColor splashColorHeader() const override { return QColor(0,0,0,0); }
59 QColor splashColorFooter() const override { return QColor(0,0,0,0); }
60 Qt::ScreenOrientations supportedOrientations() const override { return Qt::PortraitOrientation; }
61 bool rotatesWindowContents() const override { return false; }
62 bool isTouchApp() const override { return true; }
63 bool exemptFromLifecycle() const override { return false; }
64 void setExemptFromLifecycle(bool) override {}
65 QSize initialSurfaceSize() const override { return QSize(); }
66 void setInitialSurfaceSize(const QSize &) override {}
68 // Methods used for mocking (not in the interface)
69 void setFocused(bool focused) { m_focused = focused; Q_EMIT focusedChanged(focused); }
70private:
71 QString m_appId;
72 bool m_focused;
73};
75// This is a mock, specifically to test the LauncherModel
76class MockAppManager: public unity::shell::application::ApplicationManagerInterface
78 Q_OBJECT
79public:
80 MockAppManager(QObject *parent = 0): ApplicationManagerInterface(parent) {}
81 ~MockAppManager() {}
82 int rowCount(const QModelIndex &) const override { return m_list.count(); }
83 QVariant data(const QModelIndex &, int ) const override { return QVariant(); }
84 QString focusedApplicationId() const override {
85 Q_FOREACH(MockApp *app, m_list) {
86 if (app->focused()) return app->appId();
87 }
88 return QString();
89 }
90 unity::shell::application::ApplicationInfoInterface *get(int index) const override { return m_list.at(index); }
91 unity::shell::application::ApplicationInfoInterface *findApplication(const QString &appId) const override {
92 Q_FOREACH(MockApp* app, m_list) {
93 if (app->appId() == appId) {
94 return app;
95 }
96 }
97 return nullptr;
98 }
99 unity::shell::application::ApplicationInfoInterface *startApplication(const QString &, const QStringList &) override { return nullptr; }
100 bool stopApplication(const QString &appId) override {
101 Q_FOREACH(MockApp* app, m_list) {
102 if (app->appId() == appId) {
103 removeApplication(m_list.indexOf(app));
104 return true;
105 }
106 }
107 return false;
108 }
109 bool focusApplication(const QString &appId) override {
110 Q_FOREACH(MockApp* app, m_list) {
111 app->setFocused(app->appId() == appId);
112 }
113 Q_EMIT focusedApplicationIdChanged();
114 return true;
115 }
117 void unfocusCurrentApplication() override { }
119 void addApplication(MockApp *app) {
120 beginInsertRows(QModelIndex(), count(), count());
121 m_list.append(app);
122 endInsertRows();
123 }
124 void removeApplication(int index) {
125 beginRemoveRows(QModelIndex(), index, index);
126 m_list.takeAt(index)->deleteLater();
127 endRemoveRows();
128 }
129 bool requestFocusApplication(const QString &appId) override { Q_UNUSED(appId); return true; }
131private:
132 QList<MockApp*> m_list;
133};
135class LauncherModelTest : public QObject
137 Q_OBJECT
139private:
140 LauncherModel *launcherModel;
141 MockAppManager *appManager;
143 QList<QVariantMap> getASConfig() {
144 AccountsServiceDBusAdaptor *as = launcherModel->m_asAdapter->m_accounts;
145 QDBusReply<QVariant> reply = as->getUserPropertyAsync(QString(qgetenv("USER")),
146 "com.canonical.unity.AccountsService",
147 "LauncherItems");
148 return qdbus_cast<QList<QVariantMap>>(reply.value().value<QDBusArgument>());
149 }
151private Q_SLOTS:
153 void initTestCase() {
154 qDBusRegisterMetaType<QList<QVariantMap>>();
156 launcherModel = new LauncherModel(this);
157 QCoreApplication::processEvents(); // to let the model register on DBus
158 QCOMPARE(launcherModel->rowCount(QModelIndex()), 0);
160 appManager = new MockAppManager(this);
161 launcherModel->setApplicationManager(appManager);
162 }
164 // Adding 2 apps to the mock appmanager. Both should appear in the launcher.
165 void init() {
166 QDBusInterface accountsInterface(QStringLiteral("org.freedesktop.Accounts"),
167 QStringLiteral("/org/freedesktop/Accounts"),
168 QStringLiteral("org.freedesktop.Accounts"));
169 QDBusReply<bool> addReply = accountsInterface.call(QStringLiteral("AddUser"),
170 QString(qgetenv("USER")));
171 QVERIFY(addReply.isValid());
172 QCOMPARE(addReply.value(), true);
174 appManager->addApplication(new MockApp("abs-icon"));
175 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
177 appManager->addApplication(new MockApp("no-icon"));
178 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
180 launcherModel->m_settings->setStoredApplications(QStringList());
181 }
183 // Removing apps from appmanager and launcher as pinned ones would stick
184 void cleanup() {
185 while (appManager->count() > 0) {
186 appManager->removeApplication(0);
187 }
188 while (launcherModel->rowCount(QModelIndex()) > 0) {
189 launcherModel->requestRemove(launcherModel->get(0)->appId());
190 }
192 QDBusInterface accountsInterface(QStringLiteral("org.freedesktop.Accounts"),
193 QStringLiteral("/org/freedesktop/Accounts"),
194 QStringLiteral("org.freedesktop.Accounts"));
195 QDBusReply<bool> removeReply = accountsInterface.call(QStringLiteral("RemoveUser"),
196 QString(qgetenv("USER")));
197 QVERIFY(removeReply.isValid());
198 QCOMPARE(removeReply.value(), true);
199 }
201 void testMove() {
202 QCOMPARE(launcherModel->get(0)->pinned(), false);
203 QCOMPARE(launcherModel->get(1)->pinned(), false);
205 LauncherItemInterface *item0BeforeMove = launcherModel->get(0);
206 LauncherItemInterface *item1BeforeMove = launcherModel->get(1);
207 launcherModel->move(1, 0);
209 QCOMPARE(item0BeforeMove, launcherModel->get(1));
210 QCOMPARE(item1BeforeMove, launcherModel->get(0));
212 // moved item must be pinned now
213 QCOMPARE(item0BeforeMove->pinned(), false);
214 QCOMPARE(item1BeforeMove->pinned(), true);
215 }
217 void testPinning() {
218 QSignalSpy spy(launcherModel, &LauncherModel::dataChanged);
219 QCOMPARE(launcherModel->get(0)->pinned(), false);
220 QCOMPARE(launcherModel->get(1)->pinned(), false);
221 launcherModel->pin(launcherModel->get(0)->appId());
222 QCOMPARE(launcherModel->get(0)->pinned(), true);
223 QCOMPARE(launcherModel->get(1)->pinned(), false);
224 QCOMPARE(spy.count() > 0, true);
225 QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
227 // App should be pinned now
228 spy.clear();
229 launcherModel->requestRemove(launcherModel->get(0)->appId());
230 QCOMPARE(launcherModel->get(0)->pinned(), false);
231 QCOMPARE(launcherModel->get(1)->pinned(), false);
232 QCOMPARE(spy.count(), 1);
233 QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
235 // Now that the app is unpinned, nothing should change and the signal should not be emitted
236 spy.clear();
237 launcherModel->requestRemove(launcherModel->get(0)->appId());
238 QCOMPARE(launcherModel->get(0)->pinned(), false);
239 QCOMPARE(launcherModel->get(1)->pinned(), false);
240 QCOMPARE(spy.count(), 0);
241 }
243 void testRemove_data() {
245 QTest::addColumn<bool>("pinned");
246 QTest::addColumn<bool>("running");
248 QTest::newRow("non-pinned, running") << false << true;
249 QTest::newRow("pinned, running") << true << true;
250 QTest::newRow("pinned, non-running") << true << false;
251 }
253 void testRemove() {
254 QFETCH(bool, pinned);
255 QFETCH(bool, running);
257 // In the beginning we always have two items
258 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
260 // pin one if required
261 if (pinned) {
262 launcherModel->pin("abs-icon");
263 }
265 // stop it if required
266 if (!running) {
267 appManager->stopApplication("abs-icon");
268 }
270 // Now remove it
271 launcherModel->requestRemove("abs-icon");
273 if (running) {
274 // both apps are running, both apps must still be here
275 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
277 // Item must be unpinned now
278 int index = launcherModel->findApplication("abs-icon");
279 QCOMPARE(launcherModel->get(index)->pinned(), false);
281 } else if (pinned) {
282 // Item 1 must go away, item 0 is here to stay
283 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
284 }
286 // done our checks. now stop the app if was still running
287 if (running) {
288 appManager->stopApplication("abs-icon");
289 }
291 // It needs to go away in any case now
292 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
293 }
295 void testQuickListPinningRemoving() {
296 // we start with 2 unpinned items
297 QCOMPARE(launcherModel->get(0)->pinned(), false);
298 QCOMPARE(launcherModel->get(1)->pinned(), false);
299 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
301 // find the Pin item in the quicklist
302 QuickListModel *model = qobject_cast<QuickListModel*>(launcherModel->get(0)->quickList());
303 int pinActionIndex = -1;
304 for (int i = 0; i < model->rowCount(QModelIndex()); ++i) {
305 if (model->get(i).actionId() == "pin_item") {
306 pinActionIndex = i;
307 break;
308 }
309 }
310 QVERIFY(pinActionIndex >= 0);
312 // trigger pin item quicklist action => Item must be pinned now.
313 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
314 QCOMPARE(launcherModel->get(0)->pinned(), true);
315 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
317 // quicklist needs to transform to remove item. trigger it and check it item goes away
318 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
319 QCOMPARE(launcherModel->get(0)->pinned(), false);
321 // still needs to be here as the app is still here
322 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
323 // close the app
324 appManager->removeApplication(0);
325 // Now it needs to go away
326 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
327 }
329 void testApplicationRunning() {
330 launcherModel->pin("abs-icon");
331 launcherModel->pin("no-icon");
333 QCOMPARE(launcherModel->get(0)->running(), true);
334 QCOMPARE(launcherModel->get(1)->running(), true);
336 appManager->stopApplication("abs-icon");
337 QCOMPARE(launcherModel->get(0)->running(), false);
338 QCOMPARE(launcherModel->get(1)->running(), true);
340 appManager->stopApplication("no-icon");
341 QCOMPARE(launcherModel->get(0)->running(), false);
342 QCOMPARE(launcherModel->get(1)->running(), false);
343 }
345 void testApplicationFocused() {
346 // all apps unfocused at beginning...
347 QCOMPARE(launcherModel->get(0)->focused(), false);
348 QCOMPARE(launcherModel->get(1)->focused(), false);
350 appManager->focusApplication("abs-icon");
351 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
352 QCOMPARE(launcherModel->get(0)->focused(), true);
353 QCOMPARE(launcherModel->get(1)->focused(), false);
355 appManager->focusApplication("no-icon");
356 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
357 QCOMPARE(launcherModel->get(0)->focused(), false);
358 QCOMPARE(launcherModel->get(1)->focused(), true);
359 }
361 void testClosingApps() {
362 // At the start there are 2 items. Let's pin one.
363 launcherModel->pin("abs-icon");
364 while (appManager->count() > 0) {
365 appManager->removeApplication(0);
366 }
367 // The pinned one needs to stay, the other needs to disappear
368 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
369 QCOMPARE(launcherModel->get(0)->appId(), QLatin1String("abs-icon"));
370 }
372 void testQuitMenuItem() {
373 // we have 2 apps running, both should have the Quit action in its quick list
374 QCOMPARE(launcherModel->rowCount(), 2);
376 // stop the second one keeping it pinned so that it doesn't go away
377 launcherModel->pin("no-icon");
378 appManager->stopApplication("no-icon");
380 // find the first Quit item, should be there
381 QuickListModel *model = qobject_cast<QuickListModel*>(launcherModel->get(0)->quickList());
382 int quitActionIndex = -1;
383 for (int i = 0; i < model->rowCount(); ++i) {
384 if (model->get(i).actionId() == "stop_item") {
385 quitActionIndex = i;
386 break;
387 }
388 }
389 QVERIFY(quitActionIndex >= 0);
391 // find the second Quit item, should NOT be there, the app is stopped
392 QuickListModel *model2 = qobject_cast<QuickListModel*>(launcherModel->get(1)->quickList());
393 int quitActionIndex2 = -1;
394 for (int i = 0; i < model2->rowCount(); ++i) {
395 if (model2->get(i).actionId() == "stop_item") {
396 quitActionIndex2 = i;
397 break;
398 }
399 }
400 QVERIFY(quitActionIndex2 == -1);
402 // trigger the first quit item quicklist action
403 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), quitActionIndex);
404 // first app should be gone...
405 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
406 // ... the second app (now at index 0) should still be there, pinned and stopped
407 QCOMPARE(launcherModel->get(0)->appId(), QStringLiteral("no-icon"));
408 QCOMPARE(launcherModel->get(0)->pinned(), true);
409 QCOMPARE(launcherModel->get(0)->running(), false);
410 }
412 void testGetUrlForAppId() {
413 QCOMPARE(launcherModel->getUrlForAppId(QString()), QString());
414 QCOMPARE(launcherModel->getUrlForAppId(""), QString());
415 QCOMPARE(launcherModel->getUrlForAppId("no-name"), QString("application:///no-name.desktop"));
416 QCOMPARE(launcherModel->getUrlForAppId("com.test.good"), QString("application:///com.test.good.desktop"));
417 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application"), QString("appid://com.test.good/application/current-user-version"));
418 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application_1.2.3"), QString("appid://com.test.good/application/current-user-version"));
419 }
421 void testIntrospection() {
422 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "org.freedesktop.DBus.Introspectable");
423 QDBusReply<QString> reply = interface.call("Introspect");
424 QStringList nodes = extractNodes(reply.value());
425 QCOMPARE(nodes.count(), launcherModel->rowCount());
427 appManager->addApplication(new MockApp("foobar"));
428 reply = interface.call("Introspect");
429 nodes = extractNodes(reply.value());
430 QCOMPARE(nodes.contains("foobar"), true);
432 appManager->removeApplication(2);
433 reply = interface.call("Introspect");
434 nodes = extractNodes(reply.value());
435 QCOMPARE(nodes.contains("foobar"), false);
436 }
438 QStringList extractNodes(const QString &introspectionXml) {
439 QXmlStreamReader introspectReply(introspectionXml);
441 QStringList ret;
442 while (!introspectReply.atEnd() && !introspectReply.hasError()) {
443 QXmlStreamReader::TokenType token = introspectReply.readNext();
445 if (token == QXmlStreamReader::StartElement) {
446 if (introspectReply.name() == "node" && introspectReply.attributes().count() > 0) {
447 ret << introspectReply.attributes().value("name").toString();
448 }
449 }
450 }
451 return ret;
452 }
454 void testCountEmblems() {
455 QSignalSpy spy(launcherModel, &LauncherModel::dataChanged);
457 // Call GetAll on abs-icon
458 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
459 QDBusReply<QVariantMap> reply = interface.call("GetAll");
460 QVariantMap map = reply.value();
462 // Check that the alerting-status is still false, and the item on the upper side of the API
463 int index = launcherModel->findApplication("abs-icon");
464 QCOMPARE(index >= 0, true);
465 QVERIFY(launcherModel->get(index)->alerting() == false);
467 // Make sure GetAll returns a map with count and countVisible props
468 QCOMPARE(map.contains("count"), true);
469 QCOMPARE(map.contains("countVisible"), true);
471 // Make sure count is intitilized to 0 and non-visible
472 QCOMPARE(map.value("count").toInt(), 0);
473 QCOMPARE(map.value("countVisible").toBool(), false);
475 // Now make it visible and set it to 55 through D-Bus
476 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", QVariant::fromValue(QDBusVariant(55)));
477 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(true)));
479 // Fetch it again using GetAll
480 reply = interface.call("GetAll");
481 map = reply.value();
483 // Make sure values have changed on the D-Bus interface
484 QCOMPARE(map.value("count").toInt(), 55);
485 QCOMPARE(map.value("countVisible").toBool(), true);
487 // Finally check, that the change to "count" implicitly also set the alerting-state to true
488 QVERIFY(launcherModel->get(index)->alerting() == true);
489 }
491 void testCountEmblemAddsRemovesItem_data() {
492 QTest::addColumn<bool>("isPinned");
493 QTest::addColumn<bool>("isRunning");
494 QTest::addColumn<bool>("startWhenVisible");
495 QTest::newRow("not pinned, not running") << false << false << false;
496 QTest::newRow("pinned, not running") << true << false << false;
497 QTest::newRow("not pinned, not running, starting from notification") << false << false << true;
498 QTest::newRow("pinned, not running, starting from notification") << true << false << true;
499 QTest::newRow("not pinned, running") << false << true << false;
500 QTest::newRow("pinned, running") << true << true << false;
501 }
503 void testCountEmblemAddsRemovesItem() {
504 QFETCH(bool, isPinned);
505 QFETCH(bool, isRunning);
506 QFETCH(bool, startWhenVisible);
508 // Make sure item is here as expected after init() and that count is not visible
509 int index = launcherModel->findApplication("abs-icon");
510 QCOMPARE(index == -1, false);
511 QCOMPARE(launcherModel->get(index)->countVisible(), false);
513 // Pin if we need to
514 if (isPinned) {
515 launcherModel->pin("abs-icon");
516 }
517 QCOMPARE(launcherModel->get(0)->pinned(), isPinned);
519 // Stop it if we need to
520 if (!isRunning) {
521 appManager->stopApplication("abs-icon");
522 }
523 QCOMPARE(launcherModel->findApplication("abs-icon") >= 0, isRunning || isPinned);
526 // set the count emblem to visible
527 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
528 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", QVariant::fromValue(QDBusVariant(55)));
529 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(true)));
531 // Make sure item is here and that count is visible
532 index = launcherModel->findApplication("abs-icon");
533 QCOMPARE(index == -1, false);
534 QCOMPARE(launcherModel->get(index)->countVisible(), true);
536 if (!isRunning && startWhenVisible) {
537 appManager->addApplication(new MockApp("abs-icon"));
538 }
540 // Hide count emblem again
541 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(false)));
543 // Make sure item is shown/hidden as expected
544 index = launcherModel->findApplication("abs-icon");
545 QCOMPARE(index == -1, !isRunning && !isPinned && !startWhenVisible);
546 }
548 void testAlert() {
549 // Check that the alerting-status is still false
550 int index = launcherModel->findApplication("abs-icon");
551 QCOMPARE(index >= 0, true);
552 QVERIFY(launcherModel->get(index)->alerting() == false);
554 // Call Alert() on "abs-icon"
555 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "com.canonical.Unity.Launcher.Item");
556 interface.call("Alert");
558 // Check that the alerting-status is now true
559 QVERIFY(launcherModel->get(index)->alerting() == true);
560 }
562 void testRefreshAfterDeletedDesktopFiles_data() {
563 QTest::addColumn<bool>("deleted");
564 QTest::newRow("have .desktop files") << false;
565 QTest::newRow("deleted .desktop files") << true;
566 }
568 void testRefreshAfterDeletedDesktopFiles() {
569 QFETCH(bool, deleted);
571 // pin both apps
572 launcherModel->pin("abs-icon");
573 launcherModel->pin("no-icon");
574 // close both apps
575 appManager->removeApplication(0);
576 appManager->removeApplication(0);
578 // "delete" the .desktop files
579 QString oldCurrent = QDir::currentPath();
580 if (deleted) {
581 // In testing mode, the launcher searches the current dir for the sample .desktop file
582 // We can make that fail by changing the current dir
583 QDir::setCurrent("..");
584 }
586 // Call refresh
587 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "com.canonical.Unity.Launcher");
588 QDBusReply<void> reply = interface.call("Refresh");
590 // Make sure the call to Refresh returned without error.
591 QCOMPARE(reply.isValid(), true);
593 QCOMPARE(launcherModel->rowCount(), deleted ? 0 : 2);
595 // Restoring current dir
596 QDir::setCurrent(oldCurrent);
597 }
599 void testSettings() {
600 GSettings *settings = launcherModel->m_settings;
601 QSignalSpy spy(launcherModel, &LauncherModel::hint);
603 // Nothing pinned at startup
604 QCOMPARE(settings->storedApplications().count(), 0);
606 // pin both apps
607 launcherModel->pin("abs-icon");
608 launcherModel->pin("no-icon");
609 QCOMPARE(spy.count(), 0);
611 // Now settings should have 2 apps
612 QCOMPARE(settings->storedApplications().count(), 2);
614 // close both apps
615 appManager->removeApplication(0);
616 appManager->removeApplication(0);
618 // Now settings should have 2 apps
619 QCOMPARE(settings->storedApplications().count(), 2);
621 // Now remove 1 app through the backend, make sure one is still there
622 settings->simulateDConfChanged(QStringList() << "abs-icon");
623 QCOMPARE(settings->storedApplications().count(), 1);
624 QCOMPARE(spy.count(), 1);
626 // Check if it disappeared from the frontend too
627 QCOMPARE(launcherModel->rowCount(), 1);
629 // Add them back but in reverse order
630 settings->simulateDConfChanged(QStringList() << "no-icon" << "abs-icon");
631 QCOMPARE(launcherModel->rowCount(), 2);
632 QCOMPARE(launcherModel->get(0)->appId(), QString("no-icon"));
633 QCOMPARE(launcherModel->get(1)->appId(), QString("abs-icon"));
634 QCOMPARE(spy.count(), 2);
635 }
637 void testAddSyncsToAS() {
638 // Make sure launcher and AS are in sync when we start the test
639 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
641 int oldCount = launcherModel->rowCount();
642 appManager->addApplication(new MockApp("rel-icon"));
643 QCOMPARE(launcherModel->rowCount(), oldCount + 1);
644 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
645 }
647 void testRemoveSyncsToAS() {
648 // Make sure launcher and AS are in sync when we start the test
649 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
651 int oldCount = launcherModel->rowCount();
652 appManager->stopApplication("abs-icon");
653 QCOMPARE(launcherModel->rowCount(), oldCount - 1);
654 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
655 }
657 void testMoveSyncsToAS() {
658 // Make sure launcher and AS are in sync when we start the test
659 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
661 for (int i = 0; i < launcherModel->rowCount(); i++) {
662 QString launcherAppId = launcherModel->get(i)->appId();
663 QString asAppId = getASConfig().at(i).value("id").toString();
664 QCOMPARE(launcherAppId, asAppId);
665 }
667 launcherModel->move(0, 1);
669 for (int i = 0; i < launcherModel->rowCount(); i++) {
670 QString launcherAppId = launcherModel->get(i)->appId();
671 QString asAppId = getASConfig().at(i).value("id").toString();
672 QCOMPARE(launcherAppId, asAppId);
673 }
674 }
676 void testCountChangeSyncsToAS() {
677 // Find the index of the abs-icon app
678 int index = launcherModel->findApplication("abs-icon");
680 // Make sure it's invisible and 0 at the beginning
681 QCOMPARE(getASConfig().at(index).value("countVisible").toBool(), false);
682 QCOMPARE(getASConfig().at(index).value("count").toInt(), 0);
684 // Change the count of the abs-icon app through D-Bus
685 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
686 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", QVariant::fromValue(QDBusVariant(55)));
687 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(true)));
689 // Make sure it changed to visible and 55
690 QCOMPARE(getASConfig().at(index).value("countVisible").toBool(), true);
691 QCOMPARE(getASConfig().at(index).value("count").toInt(), 55);
692 }
693};
695QTEST_GUILESS_MAIN(LauncherModelTest)
696#include "launchermodeltest.moc"