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