on 4e3068c help sign in
unity8/tests/plugins/Unity/Launcher/launchermodeltest.cpp
698 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>
39// This is a mock, specifically to test the LauncherModel
40class MockApp: public unity::shell::application::ApplicationInfoInterface
42 Q_OBJECT
43public:
44 MockApp(const QString &appId, QObject *parent = 0): ApplicationInfoInterface(appId, parent), m_appId(appId), m_focused(false) { }
46 RequestedState requestedState() const override { return RequestedRunning; }
47 void setRequestedState(RequestedState) override {}
48 QString appId() const override { return m_appId; }
49 QString name() const override { return "mock"; }
50 QString comment() const override { return "this is a mock"; }
51 QUrl icon() const override { return QUrl(); }
52 ApplicationInfoInterface::Stage stage() const override { return ApplicationInfoInterface::MainStage; }
53 void setStage(ApplicationInfoInterface::Stage) override {}
54 ApplicationInfoInterface::State state() const override { return ApplicationInfoInterface::Running; }
55 bool focused() const override { return m_focused; }
56 QString splashTitle() const override { return QString(); }
57 QUrl splashImage() const override { return QUrl(); }
58 bool splashShowHeader() const override { return true; }
59 QColor splashColor() const override { return QColor(0,0,0,0); }
60 QColor splashColorHeader() const override { return QColor(0,0,0,0); }
61 QColor splashColorFooter() const override { return QColor(0,0,0,0); }
62 Qt::ScreenOrientations supportedOrientations() const override { return Qt::PortraitOrientation; }
63 bool rotatesWindowContents() const override { return false; }
64 bool isTouchApp() const override { return true; }
65 bool exemptFromLifecycle() const override { return false; }
66 void setExemptFromLifecycle(bool) override {}
67 QSize initialSurfaceSize() const override { return QSize(); }
68 void setInitialSurfaceSize(const QSize &) override {}
69 MirSurfaceListInterface* surfaceList() override { return nullptr; }
70 MirSurfaceListInterface* promptSurfaceList() 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 QDBusInterface accountsInterface(QStringLiteral("org.freedesktop.Accounts"),
169 QStringLiteral("/org/freedesktop/Accounts"),
170 QStringLiteral("org.freedesktop.Accounts"));
171 QDBusReply<bool> addReply = accountsInterface.call(QStringLiteral("AddUser"),
172 QString::fromUtf8(g_get_user_name()));
173 QVERIFY(addReply.isValid());
174 QCOMPARE(addReply.value(), true);
176 appManager->addApplication(new MockApp("abs-icon"));
177 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
179 appManager->addApplication(new MockApp("no-icon"));
180 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
182 launcherModel->m_settings->setStoredApplications(QStringList());
183 }
185 // Removing apps from appmanager and launcher as pinned ones would stick
186 void cleanup() {
187 while (appManager->count() > 0) {
188 appManager->removeApplication(0);
189 }
190 while (launcherModel->rowCount(QModelIndex()) > 0) {
191 launcherModel->requestRemove(launcherModel->get(0)->appId());
192 }
194 QDBusInterface accountsInterface(QStringLiteral("org.freedesktop.Accounts"),
195 QStringLiteral("/org/freedesktop/Accounts"),
196 QStringLiteral("org.freedesktop.Accounts"));
197 QDBusReply<bool> removeReply = accountsInterface.call(QStringLiteral("RemoveUser"),
198 QString::fromUtf8(g_get_user_name()));
199 QVERIFY(removeReply.isValid());
200 QCOMPARE(removeReply.value(), true);
201 }
203 void testMove() {
204 QCOMPARE(launcherModel->get(0)->pinned(), false);
205 QCOMPARE(launcherModel->get(1)->pinned(), false);
207 LauncherItemInterface *item0BeforeMove = launcherModel->get(0);
208 LauncherItemInterface *item1BeforeMove = launcherModel->get(1);
209 launcherModel->move(1, 0);
211 QCOMPARE(item0BeforeMove, launcherModel->get(1));
212 QCOMPARE(item1BeforeMove, launcherModel->get(0));
214 // moved item must be pinned now
215 QCOMPARE(item0BeforeMove->pinned(), false);
216 QCOMPARE(item1BeforeMove->pinned(), true);
217 }
219 void testPinning() {
220 QSignalSpy spy(launcherModel, &LauncherModel::dataChanged);
221 QCOMPARE(launcherModel->get(0)->pinned(), false);
222 QCOMPARE(launcherModel->get(1)->pinned(), false);
223 launcherModel->pin(launcherModel->get(0)->appId());
224 QCOMPARE(launcherModel->get(0)->pinned(), true);
225 QCOMPARE(launcherModel->get(1)->pinned(), false);
226 QCOMPARE(spy.count() > 0, true);
227 QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
229 // App should be pinned now
230 spy.clear();
231 launcherModel->requestRemove(launcherModel->get(0)->appId());
232 QCOMPARE(launcherModel->get(0)->pinned(), false);
233 QCOMPARE(launcherModel->get(1)->pinned(), false);
234 QCOMPARE(spy.count(), 1);
235 QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
237 // Now that the app is unpinned, nothing should change and the signal should not be emitted
238 spy.clear();
239 launcherModel->requestRemove(launcherModel->get(0)->appId());
240 QCOMPARE(launcherModel->get(0)->pinned(), false);
241 QCOMPARE(launcherModel->get(1)->pinned(), false);
242 QCOMPARE(spy.count(), 0);
243 }
245 void testRemove_data() {
247 QTest::addColumn<bool>("pinned");
248 QTest::addColumn<bool>("running");
250 QTest::newRow("non-pinned, running") << false << true;
251 QTest::newRow("pinned, running") << true << true;
252 QTest::newRow("pinned, non-running") << true << false;
253 }
255 void testRemove() {
256 QFETCH(bool, pinned);
257 QFETCH(bool, running);
259 // In the beginning we always have two items
260 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
262 // pin one if required
263 if (pinned) {
264 launcherModel->pin("abs-icon");
265 }
267 // stop it if required
268 if (!running) {
269 appManager->stopApplication("abs-icon");
270 }
272 // Now remove it
273 launcherModel->requestRemove("abs-icon");
275 if (running) {
276 // both apps are running, both apps must still be here
277 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
279 // Item must be unpinned now
280 int index = launcherModel->findApplication("abs-icon");
281 QCOMPARE(launcherModel->get(index)->pinned(), false);
283 } else if (pinned) {
284 // Item 1 must go away, item 0 is here to stay
285 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
286 }
288 // done our checks. now stop the app if was still running
289 if (running) {
290 appManager->stopApplication("abs-icon");
291 }
293 // It needs to go away in any case now
294 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
295 }
297 void testQuickListPinningRemoving() {
298 // we start with 2 unpinned items
299 QCOMPARE(launcherModel->get(0)->pinned(), false);
300 QCOMPARE(launcherModel->get(1)->pinned(), false);
301 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
303 // find the Pin item in the quicklist
304 QuickListModel *model = qobject_cast<QuickListModel*>(launcherModel->get(0)->quickList());
305 int pinActionIndex = -1;
306 for (int i = 0; i < model->rowCount(QModelIndex()); ++i) {
307 if (model->get(i).actionId() == "pin_item") {
308 pinActionIndex = i;
309 break;
310 }
311 }
312 QVERIFY(pinActionIndex >= 0);
314 // trigger pin item quicklist action => Item must be pinned now.
315 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
316 QCOMPARE(launcherModel->get(0)->pinned(), true);
317 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
319 // quicklist needs to transform to remove item. trigger it and check it item goes away
320 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
321 QCOMPARE(launcherModel->get(0)->pinned(), false);
323 // still needs to be here as the app is still here
324 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
325 // close the app
326 appManager->removeApplication(0);
327 // Now it needs to go away
328 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
329 }
331 void testApplicationRunning() {
332 launcherModel->pin("abs-icon");
333 launcherModel->pin("no-icon");
335 QCOMPARE(launcherModel->get(0)->running(), true);
336 QCOMPARE(launcherModel->get(1)->running(), true);
338 appManager->stopApplication("abs-icon");
339 QCOMPARE(launcherModel->get(0)->running(), false);
340 QCOMPARE(launcherModel->get(1)->running(), true);
342 appManager->stopApplication("no-icon");
343 QCOMPARE(launcherModel->get(0)->running(), false);
344 QCOMPARE(launcherModel->get(1)->running(), false);
345 }
347 void testApplicationFocused() {
348 // all apps unfocused at beginning...
349 QCOMPARE(launcherModel->get(0)->focused(), false);
350 QCOMPARE(launcherModel->get(1)->focused(), false);
352 appManager->focusApplication("abs-icon");
353 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
354 QCOMPARE(launcherModel->get(0)->focused(), true);
355 QCOMPARE(launcherModel->get(1)->focused(), false);
357 appManager->focusApplication("no-icon");
358 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
359 QCOMPARE(launcherModel->get(0)->focused(), false);
360 QCOMPARE(launcherModel->get(1)->focused(), true);
361 }
363 void testClosingApps() {
364 // At the start there are 2 items. Let's pin one.
365 launcherModel->pin("abs-icon");
366 while (appManager->count() > 0) {
367 appManager->removeApplication(0);
368 }
369 // The pinned one needs to stay, the other needs to disappear
370 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
371 QCOMPARE(launcherModel->get(0)->appId(), QLatin1String("abs-icon"));
372 }
374 void testQuitMenuItem() {
375 // we have 2 apps running, both should have the Quit action in its quick list
376 QCOMPARE(launcherModel->rowCount(), 2);
378 // stop the second one keeping it pinned so that it doesn't go away
379 launcherModel->pin("no-icon");
380 appManager->stopApplication("no-icon");
382 // find the first Quit item, should be there
383 QuickListModel *model = qobject_cast<QuickListModel*>(launcherModel->get(0)->quickList());
384 int quitActionIndex = -1;
385 for (int i = 0; i < model->rowCount(); ++i) {
386 if (model->get(i).actionId() == "stop_item") {
387 quitActionIndex = i;
388 break;
389 }
390 }
391 QVERIFY(quitActionIndex >= 0);
393 // find the second Quit item, should NOT be there, the app is stopped
394 QuickListModel *model2 = qobject_cast<QuickListModel*>(launcherModel->get(1)->quickList());
395 int quitActionIndex2 = -1;
396 for (int i = 0; i < model2->rowCount(); ++i) {
397 if (model2->get(i).actionId() == "stop_item") {
398 quitActionIndex2 = i;
399 break;
400 }
401 }
402 QVERIFY(quitActionIndex2 == -1);
404 // trigger the first quit item quicklist action
405 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), quitActionIndex);
406 // first app should be gone...
407 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
408 // ... the second app (now at index 0) should still be there, pinned and stopped
409 QCOMPARE(launcherModel->get(0)->appId(), QStringLiteral("no-icon"));
410 QCOMPARE(launcherModel->get(0)->pinned(), true);
411 QCOMPARE(launcherModel->get(0)->running(), false);
412 }
414 void testGetUrlForAppId() {
415 QCOMPARE(launcherModel->getUrlForAppId(QString()), QString());
416 QCOMPARE(launcherModel->getUrlForAppId(""), QString());
417 QCOMPARE(launcherModel->getUrlForAppId("no-name"), QString("application:///no-name.desktop"));
418 QCOMPARE(launcherModel->getUrlForAppId("com.test.good"), QString("application:///com.test.good.desktop"));
419 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application"), QString("appid://com.test.good/application/current-user-version"));
420 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application_1.2.3"), QString("appid://com.test.good/application/current-user-version"));
421 }
423 void testIntrospection() {
424 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "org.freedesktop.DBus.Introspectable");
425 QDBusReply<QString> reply = interface.call("Introspect");
426 QStringList nodes = extractNodes(reply.value());
427 QCOMPARE(nodes.count(), launcherModel->rowCount());
429 appManager->addApplication(new MockApp("foobar"));
430 reply = interface.call("Introspect");
431 nodes = extractNodes(reply.value());
432 QCOMPARE(nodes.contains("foobar"), true);
434 appManager->removeApplication(2);
435 reply = interface.call("Introspect");
436 nodes = extractNodes(reply.value());
437 QCOMPARE(nodes.contains("foobar"), false);
438 }
440 QStringList extractNodes(const QString &introspectionXml) {
441 QXmlStreamReader introspectReply(introspectionXml);
443 QStringList ret;
444 while (!introspectReply.atEnd() && !introspectReply.hasError()) {
445 QXmlStreamReader::TokenType token = introspectReply.readNext();
447 if (token == QXmlStreamReader::StartElement) {
448 if (introspectReply.name() == "node" && introspectReply.attributes().count() > 0) {
449 ret << introspectReply.attributes().value("name").toString();
450 }
451 }
452 }
453 return ret;
454 }
456 void testCountEmblems() {
457 QSignalSpy spy(launcherModel, &LauncherModel::dataChanged);
459 // Call GetAll on abs-icon
460 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
461 QDBusReply<QVariantMap> reply = interface.call("GetAll");
462 QVariantMap map = reply.value();
464 // Check that the alerting-status is still false, and the item on the upper side of the API
465 int index = launcherModel->findApplication("abs-icon");
466 QCOMPARE(index >= 0, true);
467 QVERIFY(launcherModel->get(index)->alerting() == false);
469 // Make sure GetAll returns a map with count and countVisible props
470 QCOMPARE(map.contains("count"), true);
471 QCOMPARE(map.contains("countVisible"), true);
473 // Make sure count is intitilized to 0 and non-visible
474 QCOMPARE(map.value("count").toInt(), 0);
475 QCOMPARE(map.value("countVisible").toBool(), false);
477 // Now make it visible and set it to 55 through D-Bus
478 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", QVariant::fromValue(QDBusVariant(55)));
479 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(true)));
481 // Fetch it again using GetAll
482 reply = interface.call("GetAll");
483 map = reply.value();
485 // Make sure values have changed on the D-Bus interface
486 QCOMPARE(map.value("count").toInt(), 55);
487 QCOMPARE(map.value("countVisible").toBool(), true);
489 // Finally check, that the change to "count" implicitly also set the alerting-state to true
490 QVERIFY(launcherModel->get(index)->alerting() == true);
491 }
493 void testCountEmblemAddsRemovesItem_data() {
494 QTest::addColumn<bool>("isPinned");
495 QTest::addColumn<bool>("isRunning");
496 QTest::addColumn<bool>("startWhenVisible");
497 QTest::newRow("not pinned, not running") << false << false << false;
498 QTest::newRow("pinned, not running") << true << false << false;
499 QTest::newRow("not pinned, not running, starting from notification") << false << false << true;
500 QTest::newRow("pinned, not running, starting from notification") << true << false << true;
501 QTest::newRow("not pinned, running") << false << true << false;
502 QTest::newRow("pinned, running") << true << true << false;
503 }
505 void testCountEmblemAddsRemovesItem() {
506 QFETCH(bool, isPinned);
507 QFETCH(bool, isRunning);
508 QFETCH(bool, startWhenVisible);
510 // Make sure item is here as expected after init() and that count is not visible
511 int index = launcherModel->findApplication("abs-icon");
512 QCOMPARE(index == -1, false);
513 QCOMPARE(launcherModel->get(index)->countVisible(), false);
515 // Pin if we need to
516 if (isPinned) {
517 launcherModel->pin("abs-icon");
518 }
519 QCOMPARE(launcherModel->get(0)->pinned(), isPinned);
521 // Stop it if we need to
522 if (!isRunning) {
523 appManager->stopApplication("abs-icon");
524 }
525 QCOMPARE(launcherModel->findApplication("abs-icon") >= 0, isRunning || isPinned);
528 // set the count emblem to visible
529 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
530 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", QVariant::fromValue(QDBusVariant(55)));
531 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(true)));
533 // Make sure item is here and that count is visible
534 index = launcherModel->findApplication("abs-icon");
535 QCOMPARE(index == -1, false);
536 QCOMPARE(launcherModel->get(index)->countVisible(), true);
538 if (!isRunning && startWhenVisible) {
539 appManager->addApplication(new MockApp("abs-icon"));
540 }
542 // Hide count emblem again
543 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(false)));
545 // Make sure item is shown/hidden as expected
546 index = launcherModel->findApplication("abs-icon");
547 QCOMPARE(index == -1, !isRunning && !isPinned && !startWhenVisible);
548 }
550 void testAlert() {
551 // Check that the alerting-status is still false
552 int index = launcherModel->findApplication("abs-icon");
553 QCOMPARE(index >= 0, true);
554 QVERIFY(launcherModel->get(index)->alerting() == false);
556 // Call Alert() on "abs-icon"
557 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "com.canonical.Unity.Launcher.Item");
558 interface.call("Alert");
560 // Check that the alerting-status is now true
561 QVERIFY(launcherModel->get(index)->alerting() == true);
562 }
564 void testRefreshAfterDeletedDesktopFiles_data() {
565 QTest::addColumn<bool>("deleted");
566 QTest::newRow("have .desktop files") << false;
567 QTest::newRow("deleted .desktop files") << true;
568 }
570 void testRefreshAfterDeletedDesktopFiles() {
571 QFETCH(bool, deleted);
573 // pin both apps
574 launcherModel->pin("abs-icon");
575 launcherModel->pin("no-icon");
576 // close both apps
577 appManager->removeApplication(0);
578 appManager->removeApplication(0);
580 // "delete" the .desktop files
581 QString oldCurrent = QDir::currentPath();
582 if (deleted) {
583 // In testing mode, the launcher searches the current dir for the sample .desktop file
584 // We can make that fail by changing the current dir
585 QDir::setCurrent("..");
586 }
588 // Call refresh
589 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "com.canonical.Unity.Launcher");
590 QDBusReply<void> reply = interface.call("Refresh");
592 // Make sure the call to Refresh returned without error.
593 QCOMPARE(reply.isValid(), true);
595 QCOMPARE(launcherModel->rowCount(), deleted ? 0 : 2);
597 // Restoring current dir
598 QDir::setCurrent(oldCurrent);
599 }
601 void testSettings() {
602 GSettings *settings = launcherModel->m_settings;
603 QSignalSpy spy(launcherModel, &LauncherModel::hint);
605 // Nothing pinned at startup
606 QCOMPARE(settings->storedApplications().count(), 0);
608 // pin both apps
609 launcherModel->pin("abs-icon");
610 launcherModel->pin("no-icon");
611 QCOMPARE(spy.count(), 0);
613 // Now settings should have 2 apps
614 QCOMPARE(settings->storedApplications().count(), 2);
616 // close both apps
617 appManager->removeApplication(0);
618 appManager->removeApplication(0);
620 // Now settings should have 2 apps
621 QCOMPARE(settings->storedApplications().count(), 2);
623 // Now remove 1 app through the backend, make sure one is still there
624 settings->simulateDConfChanged(QStringList() << "abs-icon");
625 QCOMPARE(settings->storedApplications().count(), 1);
626 QCOMPARE(spy.count(), 1);
628 // Check if it disappeared from the frontend too
629 QCOMPARE(launcherModel->rowCount(), 1);
631 // Add them back but in reverse order
632 settings->simulateDConfChanged(QStringList() << "no-icon" << "abs-icon");
633 QCOMPARE(launcherModel->rowCount(), 2);
634 QCOMPARE(launcherModel->get(0)->appId(), QString("no-icon"));
635 QCOMPARE(launcherModel->get(1)->appId(), QString("abs-icon"));
636 QCOMPARE(spy.count(), 2);
637 }
639 void testAddSyncsToAS() {
640 // Make sure launcher and AS are in sync when we start the test
641 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
643 int oldCount = launcherModel->rowCount();
644 appManager->addApplication(new MockApp("rel-icon"));
645 QCOMPARE(launcherModel->rowCount(), oldCount + 1);
646 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
647 }
649 void testRemoveSyncsToAS() {
650 // Make sure launcher and AS are in sync when we start the test
651 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
653 int oldCount = launcherModel->rowCount();
654 appManager->stopApplication("abs-icon");
655 QCOMPARE(launcherModel->rowCount(), oldCount - 1);
656 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
657 }
659 void testMoveSyncsToAS() {
660 // Make sure launcher and AS are in sync when we start the test
661 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
663 for (int i = 0; i < launcherModel->rowCount(); i++) {
664 QString launcherAppId = launcherModel->get(i)->appId();
665 QString asAppId = getASConfig().at(i).value("id").toString();
666 QCOMPARE(launcherAppId, asAppId);
667 }
669 launcherModel->move(0, 1);
671 for (int i = 0; i < launcherModel->rowCount(); i++) {
672 QString launcherAppId = launcherModel->get(i)->appId();
673 QString asAppId = getASConfig().at(i).value("id").toString();
674 QCOMPARE(launcherAppId, asAppId);
675 }
676 }
678 void testCountChangeSyncsToAS() {
679 // Find the index of the abs-icon app
680 int index = launcherModel->findApplication("abs-icon");
682 // Make sure it's invisible and 0 at the beginning
683 QCOMPARE(getASConfig().at(index).value("countVisible").toBool(), false);
684 QCOMPARE(getASConfig().at(index).value("count").toInt(), 0);
686 // Change the count of the abs-icon app through D-Bus
687 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
688 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", QVariant::fromValue(QDBusVariant(55)));
689 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(true)));
691 // Make sure it changed to visible and 55
692 QCOMPARE(getASConfig().at(index).value("countVisible").toBool(), true);
693 QCOMPARE(getASConfig().at(index).value("count").toInt(), 55);
694 }
695};
697QTEST_GUILESS_MAIN(LauncherModelTest)
698#include "launchermodeltest.moc"