on b92dd91 help sign in
unity8/tests/plugins/Unity/Launcher/launchermodeltest.cpp
738 lines 31 kB history raw
1/*
2 * Copyright 2013-2016 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 */
17// unity-api
18#include <unity/shell/launcher/LauncherModelInterface.h>
19#include <unity/shell/application/ApplicationInfoInterface.h>
21#include "launcheritem.h"
22#include "launchermodel.h"
23#include "dbusinterface.h"
24#include "gsettings.h"
25#include "asadapter.h"
26#include "AccountsServiceDBusAdaptor.h"
28#include <QtTest>
29#include <QDBusInterface>
30#include <QDBusReply>
31#include <QDBusMetaType>
32#include <QDomDocument>
34#include <glib.h>
36// This is a mock, specifically to test the LauncherModel
37class MockApp: public unity::shell::application::ApplicationInfoInterface
39 Q_OBJECT
40public:
41 MockApp(const QString &appId, QObject *parent = 0): ApplicationInfoInterface(appId, parent), m_appId(appId), m_focused(false) { }
43 RequestedState requestedState() const override { return RequestedRunning; }
44 void setRequestedState(RequestedState) override {}
45 QString appId() const override { return m_appId; }
46 QString name() const override { return "mock"; }
47 QString comment() const override { return "this is a mock"; }
48 QUrl icon() const override { return QUrl(); }
49 ApplicationInfoInterface::State state() const override { return ApplicationInfoInterface::Running; }
50 bool focused() const override { return m_focused; }
51 QString splashTitle() const override { return QString(); }
52 QUrl splashImage() const override { return QUrl(); }
53 bool splashShowHeader() const override { return true; }
54 QColor splashColor() const override { return QColor(0,0,0,0); }
55 QColor splashColorHeader() const override { return QColor(0,0,0,0); }
56 QColor splashColorFooter() const override { return QColor(0,0,0,0); }
57 Qt::ScreenOrientations supportedOrientations() const override { return Qt::PortraitOrientation; }
58 bool rotatesWindowContents() const override { return false; }
59 bool isTouchApp() const override { return true; }
60 bool exemptFromLifecycle() const override { return false; }
61 void setExemptFromLifecycle(bool) override {}
62 QSize initialSurfaceSize() const override { return QSize(); }
63 void setInitialSurfaceSize(const QSize &) override {}
64 MirSurfaceListInterface* surfaceList() const override { return nullptr; }
65 MirSurfaceListInterface* promptSurfaceList() const override { return nullptr; }
66 int surfaceCount() const override { return m_surfaceCount; }
67 void setSurfaceCount(int count) { m_surfaceCount = count; Q_EMIT surfaceCountChanged(count); }
68 void close() override {}
70 // Methods used for mocking (not in the interface)
71 void setFocused(bool focused) { m_focused = focused; Q_EMIT focusedChanged(focused); }
72private:
73 QString m_appId;
74 bool m_focused;
75 int m_surfaceCount = 0;
76};
78// This is a mock, specifically to test the LauncherModel
79class MockAppManager: public unity::shell::application::ApplicationManagerInterface
81 Q_OBJECT
82public:
83 MockAppManager(QObject *parent = 0): ApplicationManagerInterface(parent) {}
84 ~MockAppManager() {}
85 int rowCount(const QModelIndex &) const override { return m_list.count(); }
86 QVariant data(const QModelIndex &, int ) const override { return QVariant(); }
87 QString focusedApplicationId() const override {
88 Q_FOREACH(MockApp *app, m_list) {
89 if (app->focused()) return app->appId();
90 }
91 return QString();
92 }
93 unity::shell::application::ApplicationInfoInterface *get(int index) const override { return m_list.at(index); }
94 unity::shell::application::ApplicationInfoInterface *findApplication(const QString &appId) const override {
95 Q_FOREACH(MockApp* app, m_list) {
96 if (app->appId() == appId) {
97 return app;
98 }
99 }
100 return nullptr;
101 }
102 unity::shell::application::ApplicationInfoInterface *findApplicationWithSurface(unity::shell::application::MirSurfaceInterface* surface) override {
103 return nullptr;
104 }
105 unity::shell::application::ApplicationInfoInterface *startApplication(const QString &, const QStringList &) override { return nullptr; }
106 bool stopApplication(const QString &appId) override {
107 Q_FOREACH(MockApp* app, m_list) {
108 if (app->appId() == appId) {
109 removeApplication(m_list.indexOf(app));
110 return true;
111 }
112 }
113 return false;
114 }
115 bool focusApplication(const QString &appId) {
116 Q_FOREACH(MockApp* app, m_list) {
117 app->setFocused(app->appId() == appId);
118 }
119 Q_EMIT focusedApplicationIdChanged();
120 return true;
121 }
123 void addApplication(MockApp *app) {
124 beginInsertRows(QModelIndex(), count(), count());
125 m_list.append(app);
126 endInsertRows();
127 }
128 void removeApplication(int index) {
129 beginRemoveRows(QModelIndex(), index, index);
130 m_list.takeAt(index)->deleteLater();
131 endRemoveRows();
132 }
133 bool requestFocusApplication(const QString &appId) override { Q_UNUSED(appId); return true; }
135private:
136 QList<MockApp*> m_list;
137};
139class LauncherModelTest : public QObject
141 Q_OBJECT
143private:
144 LauncherModel *launcherModel;
145 MockAppManager *appManager;
147 QList<QVariantMap> getASConfig() {
148 AccountsServiceDBusAdaptor *as = launcherModel->m_asAdapter->m_accounts;
149 QDBusReply<QVariant> reply = as->getUserPropertyAsync(QString::fromUtf8(g_get_user_name()),
150 "com.canonical.unity.AccountsService",
151 "LauncherItems");
152 return qdbus_cast<QList<QVariantMap>>(reply.value().value<QDBusArgument>());
153 }
155private Q_SLOTS:
157 void initTestCase() {
158 qDBusRegisterMetaType<QList<QVariantMap>>();
160 launcherModel = new LauncherModel(this);
161 QCoreApplication::processEvents(); // to let the model register on DBus
162 QCOMPARE(launcherModel->rowCount(QModelIndex()), 0);
164 appManager = new MockAppManager(this);
165 launcherModel->setApplicationManager(appManager);
166 }
168 // Adding 2 apps to the mock appmanager. Both should appear in the launcher.
169 void init() {
170 QDBusInterface accountsInterface(QStringLiteral("org.freedesktop.Accounts"),
171 QStringLiteral("/org/freedesktop/Accounts"),
172 QStringLiteral("org.freedesktop.Accounts"));
173 QDBusReply<bool> addReply = accountsInterface.call(QStringLiteral("AddUser"),
174 QString::fromUtf8(g_get_user_name()));
175 QVERIFY(addReply.isValid());
176 QCOMPARE(addReply.value(), true);
178 appManager->addApplication(new MockApp("abs-icon"));
179 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
181 appManager->addApplication(new MockApp("rel-icon"));
182 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
184 launcherModel->m_settings->setStoredApplications(QStringList());
185 }
187 // Removing apps from appmanager and launcher as pinned ones would stick
188 void cleanup() {
189 while (appManager->count() > 0) {
190 appManager->removeApplication(0);
191 }
192 while (launcherModel->rowCount(QModelIndex()) > 0) {
193 launcherModel->requestRemove(launcherModel->get(0)->appId());
194 }
196 QDBusInterface accountsInterface(QStringLiteral("org.freedesktop.Accounts"),
197 QStringLiteral("/org/freedesktop/Accounts"),
198 QStringLiteral("org.freedesktop.Accounts"));
199 QDBusReply<bool> removeReply = accountsInterface.call(QStringLiteral("RemoveUser"),
200 QString::fromUtf8(g_get_user_name()));
201 QVERIFY(removeReply.isValid());
202 QCOMPARE(removeReply.value(), true);
204 // Some tests move the directory, so lets move it back if so.
205 // But this will usually fail.
206 QFile::rename("applications.old", "applications");
207 }
209 void testMove() {
210 QCOMPARE(launcherModel->get(0)->pinned(), false);
211 QCOMPARE(launcherModel->get(1)->pinned(), false);
213 LauncherItemInterface *item0BeforeMove = launcherModel->get(0);
214 LauncherItemInterface *item1BeforeMove = launcherModel->get(1);
215 launcherModel->move(1, 0);
217 QCOMPARE(item0BeforeMove, launcherModel->get(1));
218 QCOMPARE(item1BeforeMove, launcherModel->get(0));
220 // moved item must be pinned now
221 QCOMPARE(item0BeforeMove->pinned(), false);
222 QCOMPARE(item1BeforeMove->pinned(), true);
223 }
225 void testPinning() {
226 QSignalSpy spy(launcherModel, &LauncherModel::dataChanged);
227 QCOMPARE(launcherModel->get(0)->pinned(), false);
228 QCOMPARE(launcherModel->get(1)->pinned(), false);
229 launcherModel->pin(launcherModel->get(0)->appId());
230 QCOMPARE(launcherModel->get(0)->pinned(), true);
231 QCOMPARE(launcherModel->get(1)->pinned(), false);
232 QCOMPARE(spy.count() > 0, true);
233 QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
235 // App should be pinned now
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(), 1);
241 QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
243 // Now that the app is unpinned, nothing should change and the signal should not be emitted
244 spy.clear();
245 launcherModel->requestRemove(launcherModel->get(0)->appId());
246 QCOMPARE(launcherModel->get(0)->pinned(), false);
247 QCOMPARE(launcherModel->get(1)->pinned(), false);
248 QCOMPARE(spy.count(), 0);
249 }
251 void testRemove_data() {
253 QTest::addColumn<bool>("pinned");
254 QTest::addColumn<bool>("running");
256 QTest::newRow("non-pinned, running") << false << true;
257 QTest::newRow("pinned, running") << true << true;
258 QTest::newRow("pinned, non-running") << true << false;
259 }
261 void testRemove() {
262 QFETCH(bool, pinned);
263 QFETCH(bool, running);
265 // In the beginning we always have two items
266 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
268 // pin one if required
269 if (pinned) {
270 launcherModel->pin("abs-icon");
271 }
273 // stop it if required
274 if (!running) {
275 appManager->stopApplication("abs-icon");
276 }
278 // Now remove it
279 launcherModel->requestRemove("abs-icon");
281 if (running) {
282 // both apps are running, both apps must still be here
283 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
285 // Item must be unpinned now
286 int index = launcherModel->findApplication("abs-icon");
287 QCOMPARE(launcherModel->get(index)->pinned(), false);
289 } else if (pinned) {
290 // Item 1 must go away, item 0 is here to stay
291 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
292 }
294 // done our checks. now stop the app if was still running
295 if (running) {
296 appManager->stopApplication("abs-icon");
297 }
299 // It needs to go away in any case now
300 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
301 }
303 void testQuickListPinningRemoving() {
304 // we start with 2 unpinned items
305 QCOMPARE(launcherModel->get(0)->pinned(), false);
306 QCOMPARE(launcherModel->get(1)->pinned(), false);
307 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
309 // find the Pin item in the quicklist
310 QuickListModel *model = qobject_cast<QuickListModel*>(launcherModel->get(0)->quickList());
311 int pinActionIndex = -1;
312 for (int i = 0; i < model->rowCount(QModelIndex()); ++i) {
313 if (model->get(i).actionId() == "pin_item") {
314 pinActionIndex = i;
315 break;
316 }
317 }
318 QVERIFY(pinActionIndex >= 0);
320 // trigger pin item quicklist action => Item must be pinned now.
321 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
322 QCOMPARE(launcherModel->get(0)->pinned(), true);
323 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
325 // quicklist needs to transform to remove item. trigger it and check it item goes away
326 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
327 QCOMPARE(launcherModel->get(0)->pinned(), false);
329 // still needs to be here as the app is still here
330 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
331 // close the app
332 appManager->removeApplication(0);
333 // Now it needs to go away
334 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
335 }
337 void testApplicationRunning() {
338 launcherModel->pin("abs-icon");
339 launcherModel->pin("rel-icon");
341 QCOMPARE(launcherModel->get(0)->running(), true);
342 QCOMPARE(launcherModel->get(1)->running(), true);
344 appManager->stopApplication("abs-icon");
345 QCOMPARE(launcherModel->get(0)->running(), false);
346 QCOMPARE(launcherModel->get(1)->running(), true);
348 appManager->stopApplication("rel-icon");
349 QCOMPARE(launcherModel->get(0)->running(), false);
350 QCOMPARE(launcherModel->get(1)->running(), false);
351 }
353 void testApplicationFocused() {
354 // all apps unfocused at beginning...
355 QCOMPARE(launcherModel->get(0)->focused(), false);
356 QCOMPARE(launcherModel->get(1)->focused(), false);
358 appManager->focusApplication("abs-icon");
359 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
360 QCOMPARE(launcherModel->get(0)->focused(), true);
361 QCOMPARE(launcherModel->get(1)->focused(), false);
363 appManager->focusApplication("rel-icon");
364 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
365 QCOMPARE(launcherModel->get(0)->focused(), false);
366 QCOMPARE(launcherModel->get(1)->focused(), true);
367 }
369 void testClosingApps() {
370 // At the start there are 2 items. Let's pin one.
371 launcherModel->pin("abs-icon");
372 while (appManager->count() > 0) {
373 appManager->removeApplication(0);
374 }
375 // The pinned one needs to stay, the other needs to disappear
376 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
377 QCOMPARE(launcherModel->get(0)->appId(), QLatin1String("abs-icon"));
378 }
380 void testQuitMenuItem() {
381 // we have 2 apps running, both should have the Quit action in its quick list
382 QCOMPARE(launcherModel->rowCount(), 2);
384 // stop the second one keeping it pinned so that it doesn't go away
385 launcherModel->pin("rel-icon");
386 appManager->stopApplication("rel-icon");
388 // find the first Quit item, should be there
389 QuickListModel *model = qobject_cast<QuickListModel*>(launcherModel->get(0)->quickList());
390 int quitActionIndex = -1;
391 for (int i = 0; i < model->rowCount(); ++i) {
392 if (model->get(i).actionId() == "stop_item") {
393 quitActionIndex = i;
394 break;
395 }
396 }
397 QVERIFY(quitActionIndex >= 0);
399 // find the second Quit item, should NOT be there, the app is stopped
400 QuickListModel *model2 = qobject_cast<QuickListModel*>(launcherModel->get(1)->quickList());
401 int quitActionIndex2 = -1;
402 for (int i = 0; i < model2->rowCount(); ++i) {
403 if (model2->get(i).actionId() == "stop_item") {
404 quitActionIndex2 = i;
405 break;
406 }
407 }
408 QVERIFY(quitActionIndex2 == -1);
410 // trigger the first quit item quicklist action
411 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), quitActionIndex);
412 // first app should be gone...
413 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
414 // ... the second app (now at index 0) should still be there, pinned and stopped
415 QCOMPARE(launcherModel->get(0)->appId(), QStringLiteral("rel-icon"));
416 QCOMPARE(launcherModel->get(0)->pinned(), true);
417 QCOMPARE(launcherModel->get(0)->running(), false);
418 }
420 void testGetUrlForAppId() {
421 QCOMPARE(launcherModel->getUrlForAppId(QString()), QString());
422 QCOMPARE(launcherModel->getUrlForAppId(""), QString());
423 QCOMPARE(launcherModel->getUrlForAppId("no-name"), QString("application:///no-name.desktop"));
424 QCOMPARE(launcherModel->getUrlForAppId("com.test.good"), QString("application:///com.test.good.desktop"));
425 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application"), QString("appid://com.test.good/application/current-user-version"));
426 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application_1.2.3"), QString("appid://com.test.good/application/current-user-version"));
427 }
429 void testIntrospection() {
430 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "org.freedesktop.DBus.Introspectable");
431 QDBusReply<QString> reply = interface.call("Introspect");
432 QStringList nodes = extractNodes(reply.value());
433 QCOMPARE(nodes.count(), launcherModel->rowCount());
435 appManager->addApplication(new MockApp("foobar"));
436 reply = interface.call("Introspect");
437 nodes = extractNodes(reply.value());
438 QCOMPARE(nodes.contains("foobar"), true);
440 appManager->removeApplication(2);
441 reply = interface.call("Introspect");
442 nodes = extractNodes(reply.value());
443 QCOMPARE(nodes.contains("foobar"), false);
444 }
446 QStringList extractNodes(const QString &introspectionXml) {
447 QXmlStreamReader introspectReply(introspectionXml);
449 QStringList ret;
450 while (!introspectReply.atEnd() && !introspectReply.hasError()) {
451 QXmlStreamReader::TokenType token = introspectReply.readNext();
453 if (token == QXmlStreamReader::StartElement) {
454 if (introspectReply.name() == "node" && introspectReply.attributes().count() > 0) {
455 ret << introspectReply.attributes().value("name").toString();
456 }
457 }
458 }
459 return ret;
460 }
462 void testCountEmblems() {
463 QSignalSpy spy(launcherModel, &LauncherModel::dataChanged);
465 // Call GetAll on abs-icon
466 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
467 QDBusReply<QVariantMap> reply = interface.call("GetAll");
468 QVariantMap map = reply.value();
470 // Check that the alerting-status is still false, and the item on the upper side of the API
471 int index = launcherModel->findApplication("abs-icon");
472 QCOMPARE(index >= 0, true);
473 QVERIFY(launcherModel->get(index)->alerting() == false);
475 // Make sure GetAll returns a map with count and countVisible props
476 QCOMPARE(map.contains("count"), true);
477 QCOMPARE(map.contains("countVisible"), true);
479 // Make sure count is intitilized to 0 and non-visible
480 QCOMPARE(map.value("count").toInt(), 0);
481 QCOMPARE(map.value("countVisible").toBool(), false);
483 // Now make it visible and set it to 55 through D-Bus
484 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", QVariant::fromValue(QDBusVariant(55)));
485 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(true)));
487 // Fetch it again using GetAll
488 reply = interface.call("GetAll");
489 map = reply.value();
491 // Make sure values have changed on the D-Bus interface
492 QCOMPARE(map.value("count").toInt(), 55);
493 QCOMPARE(map.value("countVisible").toBool(), true);
495 // Finally check, that the change to "count" implicitly also set the alerting-state to true
496 QVERIFY(launcherModel->get(index)->alerting() == true);
498 // Check if the launcher emitted the changed signals
499 QCOMPARE(spy.count(), 2);
501 QVariantList countEmissionArgs = spy.takeFirst();
502 QCOMPARE(countEmissionArgs.at(0).value<QModelIndex>().row(), index);
503 QCOMPARE(countEmissionArgs.at(1).value<QModelIndex>().row(), index);
504 QVector<int> roles = countEmissionArgs.at(2).value<QVector<int> >();
505 QCOMPARE(roles.first(), (int)LauncherModel::RoleCount);
507 QVariantList countVisibleEmissionArgs = spy.takeFirst();
508 QCOMPARE(countVisibleEmissionArgs.at(0).value<QModelIndex>().row(), index);
509 QCOMPARE(countVisibleEmissionArgs.at(1).value<QModelIndex>().row(), index);
510 roles = countVisibleEmissionArgs.at(2).value<QVector<int> >();
511 QVERIFY(roles.contains(LauncherModel::RoleCountVisible));
512 QVERIFY(roles.contains(LauncherModel::RoleAlerting));
514 // Check if the values match
515 QCOMPARE(launcherModel->get(index)->countVisible(), true);
516 QCOMPARE(launcherModel->get(index)->count(), 55);
517 QCOMPARE(launcherModel->get(index)->alerting(), true);
519 // Focus the app, make sure the alert gets cleared
520 appManager->focusApplication("abs-icon");
521 QVERIFY(launcherModel->get(index)->alerting() == false);
522 }
524 void testCountEmblemAddsRemovesItem_data() {
525 QTest::addColumn<bool>("isPinned");
526 QTest::addColumn<bool>("isRunning");
527 QTest::addColumn<bool>("startWhenVisible");
528 QTest::newRow("not pinned, not running") << false << false << false;
529 QTest::newRow("pinned, not running") << true << false << false;
530 QTest::newRow("not pinned, not running, starting from notification") << false << false << true;
531 QTest::newRow("pinned, not running, starting from notification") << true << false << true;
532 QTest::newRow("not pinned, running") << false << true << false;
533 QTest::newRow("pinned, running") << true << true << false;
534 }
536 void testCountEmblemAddsRemovesItem() {
537 QFETCH(bool, isPinned);
538 QFETCH(bool, isRunning);
539 QFETCH(bool, startWhenVisible);
541 // Make sure item is here as expected after init() and that count is not visible
542 int index = launcherModel->findApplication("abs-icon");
543 QCOMPARE(index == -1, false);
544 QCOMPARE(launcherModel->get(index)->countVisible(), false);
546 // Pin if we need to
547 if (isPinned) {
548 launcherModel->pin("abs-icon");
549 }
550 QCOMPARE(launcherModel->get(0)->pinned(), isPinned);
552 // Stop it if we need to
553 if (!isRunning) {
554 appManager->stopApplication("abs-icon");
555 }
556 QCOMPARE(launcherModel->findApplication("abs-icon") >= 0, isRunning || isPinned);
559 // set the count emblem to visible
560 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
561 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", QVariant::fromValue(QDBusVariant(55)));
562 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(true)));
564 // Make sure item is here and that count is visible
565 index = launcherModel->findApplication("abs-icon");
566 QCOMPARE(index == -1, false);
567 QCOMPARE(launcherModel->get(index)->countVisible(), true);
569 if (!isRunning && startWhenVisible) {
570 appManager->addApplication(new MockApp("abs-icon"));
571 }
573 // Hide count emblem again
574 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(false)));
576 // Make sure item is shown/hidden as expected
577 index = launcherModel->findApplication("abs-icon");
578 QCOMPARE(index == -1, !isRunning && !isPinned && !startWhenVisible);
579 }
581 void testAlert() {
582 // Check that the alerting-status is still false
583 int index = launcherModel->findApplication("abs-icon");
584 QCOMPARE(index >= 0, true);
585 QVERIFY(launcherModel->get(index)->alerting() == false);
587 // Call Alert() on "abs-icon"
588 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "com.canonical.Unity.Launcher.Item");
589 interface.call("Alert");
591 // Check that the alerting-status is now true
592 QVERIFY(launcherModel->get(index)->alerting() == true);
594 // Focus the app, make sure the alert gets cleared
595 appManager->focusApplication("abs-icon");
596 QVERIFY(launcherModel->get(index)->alerting() == false);
597 }
599 void testRefreshAfterDeletedDesktopFiles_data() {
600 QTest::addColumn<bool>("deleted");
601 QTest::newRow("have .desktop files") << false;
602 QTest::newRow("deleted .desktop files") << true;
603 }
605 void testRefreshAfterDeletedDesktopFiles() {
606 QFETCH(bool, deleted);
608 // pin both apps
609 launcherModel->pin("abs-icon");
610 launcherModel->pin("rel-icon");
611 // close both apps
612 appManager->removeApplication(0);
613 appManager->removeApplication(0);
615 // "delete" the .desktop files
616 if (deleted) {
617 // In testing mode, the launcher searches the current dir for the sample .desktop file
618 // We can make that fail by moving the applications dir
619 QFile::rename("applications", "applications.old");
620 }
622 // Call refresh
623 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "com.canonical.Unity.Launcher");
624 QDBusReply<void> reply = interface.call("Refresh");
626 // Make sure the call to Refresh returned without error.
627 QCOMPARE(reply.isValid(), true);
629 QCOMPARE(launcherModel->rowCount(), deleted ? 0 : 2);
630 }
632 void testSettings() {
633 GSettings *settings = launcherModel->m_settings;
634 QSignalSpy spy(launcherModel, &LauncherModel::hint);
636 // Nothing pinned at startup
637 QCOMPARE(settings->storedApplications().count(), 0);
639 // pin both apps
640 launcherModel->pin("abs-icon");
641 launcherModel->pin("rel-icon");
642 QCOMPARE(spy.count(), 0);
644 // Now settings should have 2 apps
645 QCOMPARE(settings->storedApplications().count(), 2);
647 // close both apps
648 appManager->removeApplication(0);
649 appManager->removeApplication(0);
651 // Now settings should have 2 apps
652 QCOMPARE(settings->storedApplications().count(), 2);
654 // Now remove 1 app through the backend, make sure one is still there
655 settings->simulateDConfChanged(QStringList() << "abs-icon");
656 QCOMPARE(settings->storedApplications().count(), 1);
657 QCOMPARE(spy.count(), 1);
659 // Check if it disappeared from the frontend too
660 QCOMPARE(launcherModel->rowCount(), 1);
662 // Add them back but in reverse order
663 settings->simulateDConfChanged(QStringList() << "rel-icon" << "abs-icon");
664 QCOMPARE(launcherModel->rowCount(), 2);
665 QCOMPARE(launcherModel->get(0)->appId(), QString("rel-icon"));
666 QCOMPARE(launcherModel->get(1)->appId(), QString("abs-icon"));
667 QCOMPARE(spy.count(), 2);
668 }
670 void testAddSyncsToAS() {
671 // Make sure launcher and AS are in sync when we start the test
672 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
674 int oldCount = launcherModel->rowCount();
675 appManager->addApplication(new MockApp("click-icon"));
676 QCOMPARE(launcherModel->rowCount(), oldCount + 1);
677 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
678 }
680 void testRemoveSyncsToAS() {
681 // Make sure launcher and AS are in sync when we start the test
682 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
684 int oldCount = launcherModel->rowCount();
685 appManager->stopApplication("abs-icon");
686 QCOMPARE(launcherModel->rowCount(), oldCount - 1);
687 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
688 }
690 void testMoveSyncsToAS() {
691 // Make sure launcher and AS are in sync when we start the test
692 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
694 for (int i = 0; i < launcherModel->rowCount(); i++) {
695 QString launcherAppId = launcherModel->get(i)->appId();
696 QString asAppId = getASConfig().at(i).value("id").toString();
697 QCOMPARE(launcherAppId, asAppId);
698 }
700 launcherModel->move(0, 1);
702 for (int i = 0; i < launcherModel->rowCount(); i++) {
703 QString launcherAppId = launcherModel->get(i)->appId();
704 QString asAppId = getASConfig().at(i).value("id").toString();
705 QCOMPARE(launcherAppId, asAppId);
706 }
707 }
709 void testCountChangeSyncsToAS() {
710 // Find the index of the abs-icon app
711 int index = launcherModel->findApplication("abs-icon");
713 // Make sure it's invisible and 0 at the beginning
714 QCOMPARE(getASConfig().at(index).value("countVisible").toBool(), false);
715 QCOMPARE(getASConfig().at(index).value("count").toInt(), 0);
717 // Change the count of the abs-icon app through D-Bus
718 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
719 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", QVariant::fromValue(QDBusVariant(55)));
720 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(true)));
722 // Make sure it changed to visible and 55
723 QCOMPARE(getASConfig().at(index).value("countVisible").toBool(), true);
724 QCOMPARE(getASConfig().at(index).value("count").toInt(), 55);
725 }
727 void testSurfaceCountUpdates() {
728 QString appId = launcherModel->get(0)->appId();
730 QCOMPARE(launcherModel->get(0)->surfaceCount(), 0);
731 MockApp *app = qobject_cast<MockApp*>(appManager->findApplication(appId));
732 app->setSurfaceCount(1);
733 QCOMPARE(launcherModel->get(0)->surfaceCount(), 1);
734 }
735};
737QTEST_GUILESS_MAIN(LauncherModelTest)
738#include "launchermodeltest.moc"