on 0cfbca3 help sign in
unity8/tests/plugins/Unity/Launcher/launchermodeltest.cpp
697 lines 29 kB history raw
1/*
2 * Copyright 2013-2015 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Authors:
17 * Michael Zanetti <michael.zanetti@canonical.com>
18 */
20// unity-api
21#include <unity/shell/launcher/LauncherModelInterface.h>
22#include <unity/shell/application/ApplicationInfoInterface.h>
24#include "launcheritem.h"
25#include "launchermodel.h"
26#include "dbusinterface.h"
27#include "gsettings.h"
28#include "asadapter.h"
29#include "AccountsServiceDBusAdaptor.h"
31#include <QtTest>
32#include <QDBusInterface>
33#include <QDBusReply>
34#include <QDBusMetaType>
35#include <QDomDocument>
37#include <glib.h>
38#include <paths.h>
40// This is a mock, specifically to test the LauncherModel
41class MockApp: public unity::shell::application::ApplicationInfoInterface
43 Q_OBJECT
44public:
45 MockApp(const QString &appId, QObject *parent = 0): ApplicationInfoInterface(appId, parent), m_appId(appId), m_focused(false) { }
47 RequestedState requestedState() const override { return RequestedRunning; }
48 void setRequestedState(RequestedState) override {}
49 QString appId() const override { return m_appId; }
50 QString name() const override { return "mock"; }
51 QString comment() const override { return "this is a mock"; }
52 QUrl icon() const override { return QUrl(); }
53 ApplicationInfoInterface::Stage stage() const override { return ApplicationInfoInterface::MainStage; }
54 void setStage(ApplicationInfoInterface::Stage) override {}
55 ApplicationInfoInterface::State state() const override { return ApplicationInfoInterface::Running; }
56 bool focused() const override { return m_focused; }
57 QString splashTitle() const override { return QString(); }
58 QUrl splashImage() const override { return QUrl(); }
59 bool splashShowHeader() const override { return true; }
60 QColor splashColor() const override { return QColor(0,0,0,0); }
61 QColor splashColorHeader() const override { return QColor(0,0,0,0); }
62 QColor splashColorFooter() const override { return QColor(0,0,0,0); }
63 Qt::ScreenOrientations supportedOrientations() const override { return Qt::PortraitOrientation; }
64 bool rotatesWindowContents() const override { return false; }
65 bool isTouchApp() const override { return true; }
66 bool exemptFromLifecycle() const override { return false; }
67 void setExemptFromLifecycle(bool) override {}
68 QSize initialSurfaceSize() const override { return QSize(); }
69 void setInitialSurfaceSize(const QSize &) override {}
70 MirSurfaceListInterface* surfaceList() override { return nullptr; }
72 // Methods used for mocking (not in the interface)
73 void setFocused(bool focused) { m_focused = focused; Q_EMIT focusedChanged(focused); }
74private:
75 QString m_appId;
76 bool m_focused;
77};
79// This is a mock, specifically to test the LauncherModel
80class MockAppManager: public unity::shell::application::ApplicationManagerInterface
82 Q_OBJECT
83public:
84 MockAppManager(QObject *parent = 0): ApplicationManagerInterface(parent) {}
85 ~MockAppManager() {}
86 int rowCount(const QModelIndex &) const override { return m_list.count(); }
87 QVariant data(const QModelIndex &, int ) const override { return QVariant(); }
88 QString focusedApplicationId() const override {
89 Q_FOREACH(MockApp *app, m_list) {
90 if (app->focused()) return app->appId();
91 }
92 return QString();
93 }
94 unity::shell::application::ApplicationInfoInterface *get(int index) const override { return m_list.at(index); }
95 unity::shell::application::ApplicationInfoInterface *findApplication(const QString &appId) const override {
96 Q_FOREACH(MockApp* app, m_list) {
97 if (app->appId() == appId) {
98 return app;
99 }
100 }
101 return nullptr;
102 }
103 unity::shell::application::ApplicationInfoInterface *startApplication(const QString &, const QStringList &) override { return nullptr; }
104 bool stopApplication(const QString &appId) override {
105 Q_FOREACH(MockApp* app, m_list) {
106 if (app->appId() == appId) {
107 removeApplication(m_list.indexOf(app));
108 return true;
109 }
110 }
111 return false;
112 }
113 bool focusApplication(const QString &appId) {
114 Q_FOREACH(MockApp* app, m_list) {
115 app->setFocused(app->appId() == appId);
116 }
117 Q_EMIT focusedApplicationIdChanged();
118 return true;
119 }
121 void addApplication(MockApp *app) {
122 beginInsertRows(QModelIndex(), count(), count());
123 m_list.append(app);
124 endInsertRows();
125 }
126 void removeApplication(int index) {
127 beginRemoveRows(QModelIndex(), index, index);
128 m_list.takeAt(index)->deleteLater();
129 endRemoveRows();
130 }
131 bool requestFocusApplication(const QString &appId) override { Q_UNUSED(appId); return true; }
133private:
134 QList<MockApp*> m_list;
135};
137class LauncherModelTest : public QObject
139 Q_OBJECT
141private:
142 LauncherModel *launcherModel;
143 MockAppManager *appManager;
145 QList<QVariantMap> getASConfig() {
146 AccountsServiceDBusAdaptor *as = launcherModel->m_asAdapter->m_accounts;
147 QDBusReply<QVariant> reply = as->getUserPropertyAsync(QString::fromUtf8(g_get_user_name()),
148 "com.canonical.unity.AccountsService",
149 "LauncherItems");
150 return qdbus_cast<QList<QVariantMap>>(reply.value().value<QDBusArgument>());
151 }
153private Q_SLOTS:
155 void initTestCase() {
156 qDBusRegisterMetaType<QList<QVariantMap>>();
158 launcherModel = new LauncherModel(this);
159 QCoreApplication::processEvents(); // to let the model register on DBus
160 QCOMPARE(launcherModel->rowCount(QModelIndex()), 0);
162 appManager = new MockAppManager(this);
163 launcherModel->setApplicationManager(appManager);
164 }
166 // Adding 2 apps to the mock appmanager. Both should appear in the launcher.
167 void init() {
168 // Launcher will look in current dir in testing mode for data
169 QDir::setCurrent(testDataDir() + "/" TEST_DIR);
171 QDBusInterface accountsInterface(QStringLiteral("org.freedesktop.Accounts"),
172 QStringLiteral("/org/freedesktop/Accounts"),
173 QStringLiteral("org.freedesktop.Accounts"));
174 QDBusReply<bool> addReply = accountsInterface.call(QStringLiteral("AddUser"),
175 QString::fromUtf8(g_get_user_name()));
176 QVERIFY(addReply.isValid());
177 QCOMPARE(addReply.value(), true);
179 appManager->addApplication(new MockApp("abs-icon"));
180 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
182 appManager->addApplication(new MockApp("no-icon"));
183 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
185 launcherModel->m_settings->setStoredApplications(QStringList());
186 }
188 // Removing apps from appmanager and launcher as pinned ones would stick
189 void cleanup() {
190 while (appManager->count() > 0) {
191 appManager->removeApplication(0);
192 }
193 while (launcherModel->rowCount(QModelIndex()) > 0) {
194 launcherModel->requestRemove(launcherModel->get(0)->appId());
195 }
197 QDBusInterface accountsInterface(QStringLiteral("org.freedesktop.Accounts"),
198 QStringLiteral("/org/freedesktop/Accounts"),
199 QStringLiteral("org.freedesktop.Accounts"));
200 QDBusReply<bool> removeReply = accountsInterface.call(QStringLiteral("RemoveUser"),
201 QString::fromUtf8(g_get_user_name()));
202 QVERIFY(removeReply.isValid());
203 QCOMPARE(removeReply.value(), true);
204 }
206 void testMove() {
207 QCOMPARE(launcherModel->get(0)->pinned(), false);
208 QCOMPARE(launcherModel->get(1)->pinned(), false);
210 LauncherItemInterface *item0BeforeMove = launcherModel->get(0);
211 LauncherItemInterface *item1BeforeMove = launcherModel->get(1);
212 launcherModel->move(1, 0);
214 QCOMPARE(item0BeforeMove, launcherModel->get(1));
215 QCOMPARE(item1BeforeMove, launcherModel->get(0));
217 // moved item must be pinned now
218 QCOMPARE(item0BeforeMove->pinned(), false);
219 QCOMPARE(item1BeforeMove->pinned(), true);
220 }
222 void testPinning() {
223 QSignalSpy spy(launcherModel, &LauncherModel::dataChanged);
224 QCOMPARE(launcherModel->get(0)->pinned(), false);
225 QCOMPARE(launcherModel->get(1)->pinned(), false);
226 launcherModel->pin(launcherModel->get(0)->appId());
227 QCOMPARE(launcherModel->get(0)->pinned(), true);
228 QCOMPARE(launcherModel->get(1)->pinned(), false);
229 QCOMPARE(spy.count() > 0, true);
230 QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
232 // App should be pinned now
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(), 1);
238 QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
240 // Now that the app is unpinned, nothing should change and the signal should not be emitted
241 spy.clear();
242 launcherModel->requestRemove(launcherModel->get(0)->appId());
243 QCOMPARE(launcherModel->get(0)->pinned(), false);
244 QCOMPARE(launcherModel->get(1)->pinned(), false);
245 QCOMPARE(spy.count(), 0);
246 }
248 void testRemove_data() {
250 QTest::addColumn<bool>("pinned");
251 QTest::addColumn<bool>("running");
253 QTest::newRow("non-pinned, running") << false << true;
254 QTest::newRow("pinned, running") << true << true;
255 QTest::newRow("pinned, non-running") << true << false;
256 }
258 void testRemove() {
259 QFETCH(bool, pinned);
260 QFETCH(bool, running);
262 // In the beginning we always have two items
263 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
265 // pin one if required
266 if (pinned) {
267 launcherModel->pin("abs-icon");
268 }
270 // stop it if required
271 if (!running) {
272 appManager->stopApplication("abs-icon");
273 }
275 // Now remove it
276 launcherModel->requestRemove("abs-icon");
278 if (running) {
279 // both apps are running, both apps must still be here
280 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
282 // Item must be unpinned now
283 int index = launcherModel->findApplication("abs-icon");
284 QCOMPARE(launcherModel->get(index)->pinned(), false);
286 } else if (pinned) {
287 // Item 1 must go away, item 0 is here to stay
288 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
289 }
291 // done our checks. now stop the app if was still running
292 if (running) {
293 appManager->stopApplication("abs-icon");
294 }
296 // It needs to go away in any case now
297 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
298 }
300 void testQuickListPinningRemoving() {
301 // we start with 2 unpinned items
302 QCOMPARE(launcherModel->get(0)->pinned(), false);
303 QCOMPARE(launcherModel->get(1)->pinned(), false);
304 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
306 // find the Pin item in the quicklist
307 QuickListModel *model = qobject_cast<QuickListModel*>(launcherModel->get(0)->quickList());
308 int pinActionIndex = -1;
309 for (int i = 0; i < model->rowCount(QModelIndex()); ++i) {
310 if (model->get(i).actionId() == "pin_item") {
311 pinActionIndex = i;
312 break;
313 }
314 }
315 QVERIFY(pinActionIndex >= 0);
317 // trigger pin item quicklist action => Item must be pinned now.
318 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
319 QCOMPARE(launcherModel->get(0)->pinned(), true);
320 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
322 // quicklist needs to transform to remove item. trigger it and check it item goes away
323 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
324 QCOMPARE(launcherModel->get(0)->pinned(), false);
326 // still needs to be here as the app is still here
327 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
328 // close the app
329 appManager->removeApplication(0);
330 // Now it needs to go away
331 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
332 }
334 void testApplicationRunning() {
335 launcherModel->pin("abs-icon");
336 launcherModel->pin("no-icon");
338 QCOMPARE(launcherModel->get(0)->running(), true);
339 QCOMPARE(launcherModel->get(1)->running(), true);
341 appManager->stopApplication("abs-icon");
342 QCOMPARE(launcherModel->get(0)->running(), false);
343 QCOMPARE(launcherModel->get(1)->running(), true);
345 appManager->stopApplication("no-icon");
346 QCOMPARE(launcherModel->get(0)->running(), false);
347 QCOMPARE(launcherModel->get(1)->running(), false);
348 }
350 void testApplicationFocused() {
351 // all apps unfocused at beginning...
352 QCOMPARE(launcherModel->get(0)->focused(), false);
353 QCOMPARE(launcherModel->get(1)->focused(), false);
355 appManager->focusApplication("abs-icon");
356 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
357 QCOMPARE(launcherModel->get(0)->focused(), true);
358 QCOMPARE(launcherModel->get(1)->focused(), false);
360 appManager->focusApplication("no-icon");
361 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
362 QCOMPARE(launcherModel->get(0)->focused(), false);
363 QCOMPARE(launcherModel->get(1)->focused(), true);
364 }
366 void testClosingApps() {
367 // At the start there are 2 items. Let's pin one.
368 launcherModel->pin("abs-icon");
369 while (appManager->count() > 0) {
370 appManager->removeApplication(0);
371 }
372 // The pinned one needs to stay, the other needs to disappear
373 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
374 QCOMPARE(launcherModel->get(0)->appId(), QLatin1String("abs-icon"));
375 }
377 void testQuitMenuItem() {
378 // we have 2 apps running, both should have the Quit action in its quick list
379 QCOMPARE(launcherModel->rowCount(), 2);
381 // stop the second one keeping it pinned so that it doesn't go away
382 launcherModel->pin("no-icon");
383 appManager->stopApplication("no-icon");
385 // find the first Quit item, should be there
386 QuickListModel *model = qobject_cast<QuickListModel*>(launcherModel->get(0)->quickList());
387 int quitActionIndex = -1;
388 for (int i = 0; i < model->rowCount(); ++i) {
389 if (model->get(i).actionId() == "stop_item") {
390 quitActionIndex = i;
391 break;
392 }
393 }
394 QVERIFY(quitActionIndex >= 0);
396 // find the second Quit item, should NOT be there, the app is stopped
397 QuickListModel *model2 = qobject_cast<QuickListModel*>(launcherModel->get(1)->quickList());
398 int quitActionIndex2 = -1;
399 for (int i = 0; i < model2->rowCount(); ++i) {
400 if (model2->get(i).actionId() == "stop_item") {
401 quitActionIndex2 = i;
402 break;
403 }
404 }
405 QVERIFY(quitActionIndex2 == -1);
407 // trigger the first quit item quicklist action
408 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), quitActionIndex);
409 // first app should be gone...
410 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
411 // ... the second app (now at index 0) should still be there, pinned and stopped
412 QCOMPARE(launcherModel->get(0)->appId(), QStringLiteral("no-icon"));
413 QCOMPARE(launcherModel->get(0)->pinned(), true);
414 QCOMPARE(launcherModel->get(0)->running(), false);
415 }
417 void testGetUrlForAppId() {
418 QCOMPARE(launcherModel->getUrlForAppId(QString()), QString());
419 QCOMPARE(launcherModel->getUrlForAppId(""), QString());
420 QCOMPARE(launcherModel->getUrlForAppId("no-name"), QString("application:///no-name.desktop"));
421 QCOMPARE(launcherModel->getUrlForAppId("com.test.good"), QString("application:///com.test.good.desktop"));
422 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application"), QString("appid://com.test.good/application/current-user-version"));
423 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application_1.2.3"), QString("appid://com.test.good/application/current-user-version"));
424 }
426 void testIntrospection() {
427 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "org.freedesktop.DBus.Introspectable");
428 QDBusReply<QString> reply = interface.call("Introspect");
429 QStringList nodes = extractNodes(reply.value());
430 QCOMPARE(nodes.count(), launcherModel->rowCount());
432 appManager->addApplication(new MockApp("foobar"));
433 reply = interface.call("Introspect");
434 nodes = extractNodes(reply.value());
435 QCOMPARE(nodes.contains("foobar"), true);
437 appManager->removeApplication(2);
438 reply = interface.call("Introspect");
439 nodes = extractNodes(reply.value());
440 QCOMPARE(nodes.contains("foobar"), false);
441 }
443 QStringList extractNodes(const QString &introspectionXml) {
444 QXmlStreamReader introspectReply(introspectionXml);
446 QStringList ret;
447 while (!introspectReply.atEnd() && !introspectReply.hasError()) {
448 QXmlStreamReader::TokenType token = introspectReply.readNext();
450 if (token == QXmlStreamReader::StartElement) {
451 if (introspectReply.name() == "node" && introspectReply.attributes().count() > 0) {
452 ret << introspectReply.attributes().value("name").toString();
453 }
454 }
455 }
456 return ret;
457 }
459 void testCountEmblems() {
460 QSignalSpy spy(launcherModel, &LauncherModel::dataChanged);
462 // Call GetAll on abs-icon
463 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
464 QDBusReply<QVariantMap> reply = interface.call("GetAll");
465 QVariantMap map = reply.value();
467 // Check that the alerting-status is still false, and the item on the upper side of the API
468 int index = launcherModel->findApplication("abs-icon");
469 QCOMPARE(index >= 0, true);
470 QVERIFY(launcherModel->get(index)->alerting() == false);
472 // Make sure GetAll returns a map with count and countVisible props
473 QCOMPARE(map.contains("count"), true);
474 QCOMPARE(map.contains("countVisible"), true);
476 // Make sure count is intitilized to 0 and non-visible
477 QCOMPARE(map.value("count").toInt(), 0);
478 QCOMPARE(map.value("countVisible").toBool(), false);
480 // Now make it visible and set it to 55 through D-Bus
481 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", QVariant::fromValue(QDBusVariant(55)));
482 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(true)));
484 // Fetch it again using GetAll
485 reply = interface.call("GetAll");
486 map = reply.value();
488 // Make sure values have changed on the D-Bus interface
489 QCOMPARE(map.value("count").toInt(), 55);
490 QCOMPARE(map.value("countVisible").toBool(), true);
492 // Finally check, that the change to "count" implicitly also set the alerting-state to true
493 QVERIFY(launcherModel->get(index)->alerting() == true);
494 }
496 void testCountEmblemAddsRemovesItem_data() {
497 QTest::addColumn<bool>("isPinned");
498 QTest::addColumn<bool>("isRunning");
499 QTest::addColumn<bool>("startWhenVisible");
500 QTest::newRow("not pinned, not running") << false << false << false;
501 QTest::newRow("pinned, not running") << true << false << false;
502 QTest::newRow("not pinned, not running, starting from notification") << false << false << true;
503 QTest::newRow("pinned, not running, starting from notification") << true << false << true;
504 QTest::newRow("not pinned, running") << false << true << false;
505 QTest::newRow("pinned, running") << true << true << false;
506 }
508 void testCountEmblemAddsRemovesItem() {
509 QFETCH(bool, isPinned);
510 QFETCH(bool, isRunning);
511 QFETCH(bool, startWhenVisible);
513 // Make sure item is here as expected after init() and that count is not visible
514 int index = launcherModel->findApplication("abs-icon");
515 QCOMPARE(index == -1, false);
516 QCOMPARE(launcherModel->get(index)->countVisible(), false);
518 // Pin if we need to
519 if (isPinned) {
520 launcherModel->pin("abs-icon");
521 }
522 QCOMPARE(launcherModel->get(0)->pinned(), isPinned);
524 // Stop it if we need to
525 if (!isRunning) {
526 appManager->stopApplication("abs-icon");
527 }
528 QCOMPARE(launcherModel->findApplication("abs-icon") >= 0, isRunning || isPinned);
531 // set the count emblem to visible
532 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
533 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", QVariant::fromValue(QDBusVariant(55)));
534 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(true)));
536 // Make sure item is here and that count is visible
537 index = launcherModel->findApplication("abs-icon");
538 QCOMPARE(index == -1, false);
539 QCOMPARE(launcherModel->get(index)->countVisible(), true);
541 if (!isRunning && startWhenVisible) {
542 appManager->addApplication(new MockApp("abs-icon"));
543 }
545 // Hide count emblem again
546 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(false)));
548 // Make sure item is shown/hidden as expected
549 index = launcherModel->findApplication("abs-icon");
550 QCOMPARE(index == -1, !isRunning && !isPinned && !startWhenVisible);
551 }
553 void testAlert() {
554 // Check that the alerting-status is still false
555 int index = launcherModel->findApplication("abs-icon");
556 QCOMPARE(index >= 0, true);
557 QVERIFY(launcherModel->get(index)->alerting() == false);
559 // Call Alert() on "abs-icon"
560 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "com.canonical.Unity.Launcher.Item");
561 interface.call("Alert");
563 // Check that the alerting-status is now true
564 QVERIFY(launcherModel->get(index)->alerting() == true);
565 }
567 void testRefreshAfterDeletedDesktopFiles_data() {
568 QTest::addColumn<bool>("deleted");
569 QTest::newRow("have .desktop files") << false;
570 QTest::newRow("deleted .desktop files") << true;
571 }
573 void testRefreshAfterDeletedDesktopFiles() {
574 QFETCH(bool, deleted);
576 // pin both apps
577 launcherModel->pin("abs-icon");
578 launcherModel->pin("no-icon");
579 // close both apps
580 appManager->removeApplication(0);
581 appManager->removeApplication(0);
583 // "delete" the .desktop files
584 if (deleted) {
585 // In testing mode, the launcher searches the current dir for the sample .desktop file
586 // We can make that fail by changing the current dir
587 QDir::setCurrent("..");
588 }
590 // Call refresh
591 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "com.canonical.Unity.Launcher");
592 QDBusReply<void> reply = interface.call("Refresh");
594 // Make sure the call to Refresh returned without error.
595 QCOMPARE(reply.isValid(), true);
597 QCOMPARE(launcherModel->rowCount(), deleted ? 0 : 2);
598 }
600 void testSettings() {
601 GSettings *settings = launcherModel->m_settings;
602 QSignalSpy spy(launcherModel, &LauncherModel::hint);
604 // Nothing pinned at startup
605 QCOMPARE(settings->storedApplications().count(), 0);
607 // pin both apps
608 launcherModel->pin("abs-icon");
609 launcherModel->pin("no-icon");
610 QCOMPARE(spy.count(), 0);
612 // Now settings should have 2 apps
613 QCOMPARE(settings->storedApplications().count(), 2);
615 // close both apps
616 appManager->removeApplication(0);
617 appManager->removeApplication(0);
619 // Now settings should have 2 apps
620 QCOMPARE(settings->storedApplications().count(), 2);
622 // Now remove 1 app through the backend, make sure one is still there
623 settings->simulateDConfChanged(QStringList() << "abs-icon");
624 QCOMPARE(settings->storedApplications().count(), 1);
625 QCOMPARE(spy.count(), 1);
627 // Check if it disappeared from the frontend too
628 QCOMPARE(launcherModel->rowCount(), 1);
630 // Add them back but in reverse order
631 settings->simulateDConfChanged(QStringList() << "no-icon" << "abs-icon");
632 QCOMPARE(launcherModel->rowCount(), 2);
633 QCOMPARE(launcherModel->get(0)->appId(), QString("no-icon"));
634 QCOMPARE(launcherModel->get(1)->appId(), QString("abs-icon"));
635 QCOMPARE(spy.count(), 2);
636 }
638 void testAddSyncsToAS() {
639 // Make sure launcher and AS are in sync when we start the test
640 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
642 int oldCount = launcherModel->rowCount();
643 appManager->addApplication(new MockApp("rel-icon"));
644 QCOMPARE(launcherModel->rowCount(), oldCount + 1);
645 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
646 }
648 void testRemoveSyncsToAS() {
649 // Make sure launcher and AS are in sync when we start the test
650 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
652 int oldCount = launcherModel->rowCount();
653 appManager->stopApplication("abs-icon");
654 QCOMPARE(launcherModel->rowCount(), oldCount - 1);
655 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
656 }
658 void testMoveSyncsToAS() {
659 // Make sure launcher and AS are in sync when we start the test
660 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
662 for (int i = 0; i < launcherModel->rowCount(); i++) {
663 QString launcherAppId = launcherModel->get(i)->appId();
664 QString asAppId = getASConfig().at(i).value("id").toString();
665 QCOMPARE(launcherAppId, asAppId);
666 }
668 launcherModel->move(0, 1);
670 for (int i = 0; i < launcherModel->rowCount(); i++) {
671 QString launcherAppId = launcherModel->get(i)->appId();
672 QString asAppId = getASConfig().at(i).value("id").toString();
673 QCOMPARE(launcherAppId, asAppId);
674 }
675 }
677 void testCountChangeSyncsToAS() {
678 // Find the index of the abs-icon app
679 int index = launcherModel->findApplication("abs-icon");
681 // Make sure it's invisible and 0 at the beginning
682 QCOMPARE(getASConfig().at(index).value("countVisible").toBool(), false);
683 QCOMPARE(getASConfig().at(index).value("count").toInt(), 0);
685 // Change the count of the abs-icon app through D-Bus
686 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
687 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", QVariant::fromValue(QDBusVariant(55)));
688 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(true)));
690 // Make sure it changed to visible and 55
691 QCOMPARE(getASConfig().at(index).value("countVisible").toBool(), true);
692 QCOMPARE(getASConfig().at(index).value("count").toInt(), 55);
693 }
694};
696QTEST_GUILESS_MAIN(LauncherModelTest)
697#include "launchermodeltest.moc"