on f3eb40d help sign in
unity8/tests/plugins/Unity/Launcher/launchermodeltest.cpp
757 lines 32 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>
35#include <paths.h>
37namespace unityapi = unity::shell::application;
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::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() const override { return nullptr; }
68 MirSurfaceListInterface* promptSurfaceList() const override { return nullptr; }
69 int surfaceCount() const override { return m_surfaceCount; }
70 void setSurfaceCount(int count) { m_surfaceCount = count; Q_EMIT surfaceCountChanged(count); }
71 void close() override {}
73 // Methods used for mocking (not in the interface)
74 void setFocused(bool focused) { m_focused = focused; Q_EMIT focusedChanged(focused); }
75private:
76 QString m_appId;
77 bool m_focused;
78 int m_surfaceCount = 0;
79};
81// This is a mock, specifically to test the LauncherModel
82class MockAppManager: public unity::shell::application::ApplicationManagerInterface
84 Q_OBJECT
85public:
86 MockAppManager(QObject *parent = 0): ApplicationManagerInterface(parent) {}
87 ~MockAppManager() {}
88 int rowCount(const QModelIndex &) const override { return m_list.count(); }
89 QVariant data(const QModelIndex &, int ) const override { return QVariant(); }
90 QString focusedApplicationId() const override {
91 Q_FOREACH(MockApp *app, m_list) {
92 if (app->focused()) return app->appId();
93 }
94 return QString();
95 }
96 unity::shell::application::ApplicationInfoInterface *get(int index) const override { return m_list.at(index); }
97 unity::shell::application::ApplicationInfoInterface *findApplication(const QString &appId) const override {
98 Q_FOREACH(MockApp* app, m_list) {
99 if (app->appId() == appId) {
100 return app;
101 }
102 }
103 return nullptr;
104 }
105 unityapi::ApplicationInfoInterface *findApplicationWithSurface(unityapi::MirSurfaceInterface* /*surface*/) const override {
106 return nullptr;
107 }
108 unity::shell::application::ApplicationInfoInterface *startApplication(const QString &, const QStringList &) override { return nullptr; }
109 bool stopApplication(const QString &appId) override {
110 Q_FOREACH(MockApp* app, m_list) {
111 if (app->appId() == appId) {
112 removeApplication(m_list.indexOf(app));
113 return true;
114 }
115 }
116 return false;
117 }
118 bool focusApplication(const QString &appId) {
119 Q_FOREACH(MockApp* app, m_list) {
120 app->setFocused(app->appId() == appId);
121 }
122 Q_EMIT focusedApplicationIdChanged();
123 return true;
124 }
126 void addApplication(MockApp *app) {
127 beginInsertRows(QModelIndex(), count(), count());
128 m_list.append(app);
129 endInsertRows();
130 }
131 void removeApplication(int index) {
132 beginRemoveRows(QModelIndex(), index, index);
133 m_list.takeAt(index)->deleteLater();
134 endRemoveRows();
135 }
136 bool requestFocusApplication(const QString &appId) override { Q_UNUSED(appId); return true; }
138private:
139 QList<MockApp*> m_list;
140};
142class LauncherModelTest : public QObject
144 Q_OBJECT
146private:
147 LauncherModel *launcherModel;
148 MockAppManager *appManager;
149 QTemporaryDir tmpDir;
151 QList<QVariantMap> getASConfig() {
152 AccountsServiceDBusAdaptor *as = launcherModel->m_asAdapter->m_accounts;
153 QDBusReply<QVariant> reply = as->getUserPropertyAsync(QString::fromUtf8(g_get_user_name()),
154 "com.canonical.unity.AccountsService",
155 "LauncherItems");
156 return qdbus_cast<QList<QVariantMap>>(reply.value().value<QDBusArgument>());
157 }
159 // Link our app data from a tempdir & tell glib/UAL to look there.
160 // We do this because we want to be able to delete the applications dir
161 // during testing, but that dir may be read-only (installed on system).
162 void setUpAppDir() {
163 QFile appDir(qgetenv("APPDIR"));
164 appDir.link(tmpDir.path() + "/applications");
166 qputenv("XDG_DATA_HOME", tmpDir.path().toUtf8());
167 }
169private Q_SLOTS:
171 void initTestCase() {
172 qDBusRegisterMetaType<QList<QVariantMap>>();
174 setUpAppDir();
176 launcherModel = new LauncherModel(this);
177 QCoreApplication::processEvents(); // to let the model register on DBus
178 QCOMPARE(launcherModel->rowCount(QModelIndex()), 0);
180 appManager = new MockAppManager(this);
181 launcherModel->setApplicationManager(appManager);
182 }
184 // Adding 2 apps to the mock appmanager. Both should appear in the launcher.
185 void init() {
186 // Switching to tmpDir makes manipulating our appdir symlink easier.
187 QDir::setCurrent(tmpDir.path());
189 QDBusInterface accountsInterface(QStringLiteral("org.freedesktop.Accounts"),
190 QStringLiteral("/org/freedesktop/Accounts"),
191 QStringLiteral("org.freedesktop.Accounts"));
192 QDBusReply<bool> addReply = accountsInterface.call(QStringLiteral("AddUser"),
193 QString::fromUtf8(g_get_user_name()));
194 QVERIFY(addReply.isValid());
195 QCOMPARE(addReply.value(), true);
197 appManager->addApplication(new MockApp("abs-icon"));
198 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
200 appManager->addApplication(new MockApp("rel-icon"));
201 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
203 launcherModel->m_settings->setStoredApplications(QStringList());
204 }
206 // Removing apps from appmanager and launcher as pinned ones would stick
207 void cleanup() {
208 while (appManager->count() > 0) {
209 appManager->removeApplication(0);
210 }
211 while (launcherModel->rowCount(QModelIndex()) > 0) {
212 launcherModel->requestRemove(launcherModel->get(0)->appId());
213 }
215 QDBusInterface accountsInterface(QStringLiteral("org.freedesktop.Accounts"),
216 QStringLiteral("/org/freedesktop/Accounts"),
217 QStringLiteral("org.freedesktop.Accounts"));
218 QDBusReply<bool> removeReply = accountsInterface.call(QStringLiteral("RemoveUser"),
219 QString::fromUtf8(g_get_user_name()));
220 QVERIFY(removeReply.isValid());
221 QCOMPARE(removeReply.value(), true);
223 // Some tests move the directory, so lets move it back if so.
224 // But this will usually fail.
225 QFile::rename("applications.old", "applications");
226 }
228 void testMove() {
229 QCOMPARE(launcherModel->get(0)->pinned(), false);
230 QCOMPARE(launcherModel->get(1)->pinned(), false);
232 LauncherItemInterface *item0BeforeMove = launcherModel->get(0);
233 LauncherItemInterface *item1BeforeMove = launcherModel->get(1);
234 launcherModel->move(1, 0);
236 QCOMPARE(item0BeforeMove, launcherModel->get(1));
237 QCOMPARE(item1BeforeMove, launcherModel->get(0));
239 // moved item must be pinned now
240 QCOMPARE(item0BeforeMove->pinned(), false);
241 QCOMPARE(item1BeforeMove->pinned(), true);
242 }
244 void testPinning() {
245 QSignalSpy spy(launcherModel, &LauncherModel::dataChanged);
246 QCOMPARE(launcherModel->get(0)->pinned(), false);
247 QCOMPARE(launcherModel->get(1)->pinned(), false);
248 launcherModel->pin(launcherModel->get(0)->appId());
249 QCOMPARE(launcherModel->get(0)->pinned(), true);
250 QCOMPARE(launcherModel->get(1)->pinned(), false);
251 QCOMPARE(spy.count() > 0, true);
252 QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
254 // App should be pinned now
255 spy.clear();
256 launcherModel->requestRemove(launcherModel->get(0)->appId());
257 QCOMPARE(launcherModel->get(0)->pinned(), false);
258 QCOMPARE(launcherModel->get(1)->pinned(), false);
259 QCOMPARE(spy.count(), 1);
260 QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
262 // Now that the app is unpinned, nothing should change and the signal should not be emitted
263 spy.clear();
264 launcherModel->requestRemove(launcherModel->get(0)->appId());
265 QCOMPARE(launcherModel->get(0)->pinned(), false);
266 QCOMPARE(launcherModel->get(1)->pinned(), false);
267 QCOMPARE(spy.count(), 0);
268 }
270 void testRemove_data() {
272 QTest::addColumn<bool>("pinned");
273 QTest::addColumn<bool>("running");
275 QTest::newRow("non-pinned, running") << false << true;
276 QTest::newRow("pinned, running") << true << true;
277 QTest::newRow("pinned, non-running") << true << false;
278 }
280 void testRemove() {
281 QFETCH(bool, pinned);
282 QFETCH(bool, running);
284 // In the beginning we always have two items
285 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
287 // pin one if required
288 if (pinned) {
289 launcherModel->pin("abs-icon");
290 }
292 // stop it if required
293 if (!running) {
294 appManager->stopApplication("abs-icon");
295 }
297 // Now remove it
298 launcherModel->requestRemove("abs-icon");
300 if (running) {
301 // both apps are running, both apps must still be here
302 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
304 // Item must be unpinned now
305 int index = launcherModel->findApplication("abs-icon");
306 QCOMPARE(launcherModel->get(index)->pinned(), false);
308 } else if (pinned) {
309 // Item 1 must go away, item 0 is here to stay
310 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
311 }
313 // done our checks. now stop the app if was still running
314 if (running) {
315 appManager->stopApplication("abs-icon");
316 }
318 // It needs to go away in any case now
319 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
320 }
322 void testQuickListPinningRemoving() {
323 // we start with 2 unpinned items
324 QCOMPARE(launcherModel->get(0)->pinned(), false);
325 QCOMPARE(launcherModel->get(1)->pinned(), false);
326 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
328 // find the Pin item in the quicklist
329 QuickListModel *model = qobject_cast<QuickListModel*>(launcherModel->get(0)->quickList());
330 int pinActionIndex = -1;
331 for (int i = 0; i < model->rowCount(QModelIndex()); ++i) {
332 if (model->get(i).actionId() == "pin_item") {
333 pinActionIndex = i;
334 break;
335 }
336 }
337 QVERIFY(pinActionIndex >= 0);
339 // trigger pin item quicklist action => Item must be pinned now.
340 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
341 QCOMPARE(launcherModel->get(0)->pinned(), true);
342 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
344 // quicklist needs to transform to remove item. trigger it and check it item goes away
345 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
346 QCOMPARE(launcherModel->get(0)->pinned(), false);
348 // still needs to be here as the app is still here
349 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
350 // close the app
351 appManager->removeApplication(0);
352 // Now it needs to go away
353 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
354 }
356 void testApplicationRunning() {
357 launcherModel->pin("abs-icon");
358 launcherModel->pin("rel-icon");
360 QCOMPARE(launcherModel->get(0)->running(), true);
361 QCOMPARE(launcherModel->get(1)->running(), true);
363 appManager->stopApplication("abs-icon");
364 QCOMPARE(launcherModel->get(0)->running(), false);
365 QCOMPARE(launcherModel->get(1)->running(), true);
367 appManager->stopApplication("rel-icon");
368 QCOMPARE(launcherModel->get(0)->running(), false);
369 QCOMPARE(launcherModel->get(1)->running(), false);
370 }
372 void testApplicationFocused() {
373 // all apps unfocused at beginning...
374 QCOMPARE(launcherModel->get(0)->focused(), false);
375 QCOMPARE(launcherModel->get(1)->focused(), false);
377 appManager->focusApplication("abs-icon");
378 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
379 QCOMPARE(launcherModel->get(0)->focused(), true);
380 QCOMPARE(launcherModel->get(1)->focused(), false);
382 appManager->focusApplication("rel-icon");
383 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
384 QCOMPARE(launcherModel->get(0)->focused(), false);
385 QCOMPARE(launcherModel->get(1)->focused(), true);
386 }
388 void testClosingApps() {
389 // At the start there are 2 items. Let's pin one.
390 launcherModel->pin("abs-icon");
391 while (appManager->count() > 0) {
392 appManager->removeApplication(0);
393 }
394 // The pinned one needs to stay, the other needs to disappear
395 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
396 QCOMPARE(launcherModel->get(0)->appId(), QLatin1String("abs-icon"));
397 }
399 void testQuitMenuItem() {
400 // we have 2 apps running, both should have the Quit action in its quick list
401 QCOMPARE(launcherModel->rowCount(), 2);
403 // stop the second one keeping it pinned so that it doesn't go away
404 launcherModel->pin("rel-icon");
405 appManager->stopApplication("rel-icon");
407 // find the first Quit item, should be there
408 QuickListModel *model = qobject_cast<QuickListModel*>(launcherModel->get(0)->quickList());
409 int quitActionIndex = -1;
410 for (int i = 0; i < model->rowCount(); ++i) {
411 if (model->get(i).actionId() == "stop_item") {
412 quitActionIndex = i;
413 break;
414 }
415 }
416 QVERIFY(quitActionIndex >= 0);
418 // find the second Quit item, should NOT be there, the app is stopped
419 QuickListModel *model2 = qobject_cast<QuickListModel*>(launcherModel->get(1)->quickList());
420 int quitActionIndex2 = -1;
421 for (int i = 0; i < model2->rowCount(); ++i) {
422 if (model2->get(i).actionId() == "stop_item") {
423 quitActionIndex2 = i;
424 break;
425 }
426 }
427 QVERIFY(quitActionIndex2 == -1);
429 // trigger the first quit item quicklist action
430 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), quitActionIndex);
431 // first app should be gone...
432 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
433 // ... the second app (now at index 0) should still be there, pinned and stopped
434 QCOMPARE(launcherModel->get(0)->appId(), QStringLiteral("rel-icon"));
435 QCOMPARE(launcherModel->get(0)->pinned(), true);
436 QCOMPARE(launcherModel->get(0)->running(), false);
437 }
439 void testGetUrlForAppId() {
440 QCOMPARE(launcherModel->getUrlForAppId(QString()), QString());
441 QCOMPARE(launcherModel->getUrlForAppId(""), QString());
442 QCOMPARE(launcherModel->getUrlForAppId("no-name"), QString("application:///no-name.desktop"));
443 QCOMPARE(launcherModel->getUrlForAppId("com.test.good"), QString("application:///com.test.good.desktop"));
444 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application"), QString("appid://com.test.good/application/current-user-version"));
445 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application_1.2.3"), QString("appid://com.test.good/application/current-user-version"));
446 }
448 void testIntrospection() {
449 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "org.freedesktop.DBus.Introspectable");
450 QDBusReply<QString> reply = interface.call("Introspect");
451 QStringList nodes = extractNodes(reply.value());
452 QCOMPARE(nodes.count(), launcherModel->rowCount());
454 appManager->addApplication(new MockApp("foobar"));
455 reply = interface.call("Introspect");
456 nodes = extractNodes(reply.value());
457 QCOMPARE(nodes.contains("foobar"), true);
459 appManager->removeApplication(2);
460 reply = interface.call("Introspect");
461 nodes = extractNodes(reply.value());
462 QCOMPARE(nodes.contains("foobar"), false);
463 }
465 QStringList extractNodes(const QString &introspectionXml) {
466 QXmlStreamReader introspectReply(introspectionXml);
468 QStringList ret;
469 while (!introspectReply.atEnd() && !introspectReply.hasError()) {
470 QXmlStreamReader::TokenType token = introspectReply.readNext();
472 if (token == QXmlStreamReader::StartElement) {
473 if (introspectReply.name() == "node" && introspectReply.attributes().count() > 0) {
474 ret << introspectReply.attributes().value("name").toString();
475 }
476 }
477 }
478 return ret;
479 }
481 void testCountEmblems() {
482 QSignalSpy spy(launcherModel, &LauncherModel::dataChanged);
484 // Call GetAll on abs-icon
485 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
486 QDBusReply<QVariantMap> reply = interface.call("GetAll");
487 QVariantMap map = reply.value();
489 // Check that the alerting-status is still false, and the item on the upper side of the API
490 int index = launcherModel->findApplication("abs-icon");
491 QCOMPARE(index >= 0, true);
492 QVERIFY(launcherModel->get(index)->alerting() == false);
494 // Make sure GetAll returns a map with count and countVisible props
495 QCOMPARE(map.contains("count"), true);
496 QCOMPARE(map.contains("countVisible"), true);
498 // Make sure count is intitilized to 0 and non-visible
499 QCOMPARE(map.value("count").toInt(), 0);
500 QCOMPARE(map.value("countVisible").toBool(), false);
502 // Now make it visible and set it to 55 through D-Bus
503 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", QVariant::fromValue(QDBusVariant(55)));
504 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(true)));
506 // Fetch it again using GetAll
507 reply = interface.call("GetAll");
508 map = reply.value();
510 // Make sure values have changed on the D-Bus interface
511 QCOMPARE(map.value("count").toInt(), 55);
512 QCOMPARE(map.value("countVisible").toBool(), true);
514 // Finally check, that the change to "count" implicitly also set the alerting-state to true
515 QVERIFY(launcherModel->get(index)->alerting() == true);
517 // Check if the launcher emitted the changed signals
518 QCOMPARE(spy.count(), 2);
520 QVariantList countEmissionArgs = spy.takeFirst();
521 QCOMPARE(countEmissionArgs.at(0).toModelIndex().row(), index);
522 QCOMPARE(countEmissionArgs.at(1).toModelIndex().row(), index);
523 QVector<int> roles = countEmissionArgs.at(2).value<QVector<int> >();
524 QCOMPARE(roles.first(), (int)LauncherModel::RoleCount);
526 QVariantList countVisibleEmissionArgs = spy.takeFirst();
527 QCOMPARE(countVisibleEmissionArgs.at(0).toModelIndex().row(), index);
528 QCOMPARE(countVisibleEmissionArgs.at(1).toModelIndex().row(), index);
529 roles = countVisibleEmissionArgs.at(2).value<QVector<int> >();
530 QVERIFY(roles.contains(LauncherModel::RoleCountVisible));
531 QVERIFY(roles.contains(LauncherModel::RoleAlerting));
533 // Check if the values match
534 QCOMPARE(launcherModel->get(index)->countVisible(), true);
535 QCOMPARE(launcherModel->get(index)->count(), 55);
536 QCOMPARE(launcherModel->get(index)->alerting(), true);
538 // Focus the app, make sure the alert gets cleared
539 appManager->focusApplication("abs-icon");
540 QVERIFY(launcherModel->get(index)->alerting() == false);
541 }
543 void testCountEmblemAddsRemovesItem_data() {
544 QTest::addColumn<bool>("isPinned");
545 QTest::addColumn<bool>("isRunning");
546 QTest::addColumn<bool>("startWhenVisible");
547 QTest::newRow("not pinned, not running") << false << false << false;
548 QTest::newRow("pinned, not running") << true << false << false;
549 QTest::newRow("not pinned, not running, starting from notification") << false << false << true;
550 QTest::newRow("pinned, not running, starting from notification") << true << false << true;
551 QTest::newRow("not pinned, running") << false << true << false;
552 QTest::newRow("pinned, running") << true << true << false;
553 }
555 void testCountEmblemAddsRemovesItem() {
556 QFETCH(bool, isPinned);
557 QFETCH(bool, isRunning);
558 QFETCH(bool, startWhenVisible);
560 // Make sure item is here as expected after init() and that count is not visible
561 int index = launcherModel->findApplication("abs-icon");
562 QCOMPARE(index == -1, false);
563 QCOMPARE(launcherModel->get(index)->countVisible(), false);
565 // Pin if we need to
566 if (isPinned) {
567 launcherModel->pin("abs-icon");
568 }
569 QCOMPARE(launcherModel->get(0)->pinned(), isPinned);
571 // Stop it if we need to
572 if (!isRunning) {
573 appManager->stopApplication("abs-icon");
574 }
575 QCOMPARE(launcherModel->findApplication("abs-icon") >= 0, isRunning || isPinned);
578 // set the count emblem to visible
579 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
580 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", QVariant::fromValue(QDBusVariant(55)));
581 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(true)));
583 // Make sure item is here and that count is visible
584 index = launcherModel->findApplication("abs-icon");
585 QCOMPARE(index == -1, false);
586 QCOMPARE(launcherModel->get(index)->countVisible(), true);
588 if (!isRunning && startWhenVisible) {
589 appManager->addApplication(new MockApp("abs-icon"));
590 }
592 // Hide count emblem again
593 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(false)));
595 // Make sure item is shown/hidden as expected
596 index = launcherModel->findApplication("abs-icon");
597 QCOMPARE(index == -1, !isRunning && !isPinned && !startWhenVisible);
598 }
600 void testAlert() {
601 // Check that the alerting-status is still false
602 int index = launcherModel->findApplication("abs-icon");
603 QCOMPARE(index >= 0, true);
604 QVERIFY(launcherModel->get(index)->alerting() == false);
606 // Call Alert() on "abs-icon"
607 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "com.canonical.Unity.Launcher.Item");
608 interface.call("Alert");
610 // Check that the alerting-status is now true
611 QVERIFY(launcherModel->get(index)->alerting() == true);
613 // Focus the app, make sure the alert gets cleared
614 appManager->focusApplication("abs-icon");
615 QVERIFY(launcherModel->get(index)->alerting() == false);
616 }
618 void testRefreshAfterDeletedDesktopFiles_data() {
619 QTest::addColumn<bool>("deleted");
620 QTest::newRow("have .desktop files") << false;
621 QTest::newRow("deleted .desktop files") << true;
622 }
624 void testRefreshAfterDeletedDesktopFiles() {
625 QFETCH(bool, deleted);
627 // pin both apps
628 launcherModel->pin("abs-icon");
629 launcherModel->pin("rel-icon");
630 // close both apps
631 appManager->removeApplication(0);
632 appManager->removeApplication(0);
634 // "delete" the .desktop files
635 if (deleted) {
636 // In testing mode, the launcher searches the current dir for the sample .desktop file
637 // We can make that fail by moving the applications dir
638 QFile::rename("applications", "applications.old");
639 }
641 // Call refresh
642 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "com.canonical.Unity.Launcher");
643 QDBusReply<void> reply = interface.call("Refresh");
645 // Make sure the call to Refresh returned without error.
646 QCOMPARE(reply.isValid(), true);
648 QCOMPARE(launcherModel->rowCount(), deleted ? 0 : 2);
649 }
651 void testSettings() {
652 GSettings *settings = launcherModel->m_settings;
653 QSignalSpy spy(launcherModel, &LauncherModel::hint);
655 // Nothing pinned at startup
656 QCOMPARE(settings->storedApplications().count(), 0);
658 // pin both apps
659 launcherModel->pin("abs-icon");
660 launcherModel->pin("rel-icon");
661 QCOMPARE(spy.count(), 0);
663 // Now settings should have 2 apps
664 QCOMPARE(settings->storedApplications().count(), 2);
666 // close both apps
667 appManager->removeApplication(0);
668 appManager->removeApplication(0);
670 // Now settings should have 2 apps
671 QCOMPARE(settings->storedApplications().count(), 2);
673 // Now remove 1 app through the backend, make sure one is still there
674 settings->simulateDConfChanged(QStringList() << "abs-icon");
675 QCOMPARE(settings->storedApplications().count(), 1);
676 QCOMPARE(spy.count(), 1);
678 // Check if it disappeared from the frontend too
679 QCOMPARE(launcherModel->rowCount(), 1);
681 // Add them back but in reverse order
682 settings->simulateDConfChanged(QStringList() << "rel-icon" << "abs-icon");
683 QCOMPARE(launcherModel->rowCount(), 2);
684 QCOMPARE(launcherModel->get(0)->appId(), QString("rel-icon"));
685 QCOMPARE(launcherModel->get(1)->appId(), QString("abs-icon"));
686 QCOMPARE(spy.count(), 2);
687 }
689 void testAddSyncsToAS() {
690 // Make sure launcher and AS are in sync when we start the test
691 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
693 int oldCount = launcherModel->rowCount();
694 appManager->addApplication(new MockApp("click-icon"));
695 QCOMPARE(launcherModel->rowCount(), oldCount + 1);
696 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
697 }
699 void testRemoveSyncsToAS() {
700 // Make sure launcher and AS are in sync when we start the test
701 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
703 int oldCount = launcherModel->rowCount();
704 appManager->stopApplication("abs-icon");
705 QCOMPARE(launcherModel->rowCount(), oldCount - 1);
706 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
707 }
709 void testMoveSyncsToAS() {
710 // Make sure launcher and AS are in sync when we start the test
711 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
713 for (int i = 0; i < launcherModel->rowCount(); i++) {
714 QString launcherAppId = launcherModel->get(i)->appId();
715 QString asAppId = getASConfig().at(i).value("id").toString();
716 QCOMPARE(launcherAppId, asAppId);
717 }
719 launcherModel->move(0, 1);
721 for (int i = 0; i < launcherModel->rowCount(); i++) {
722 QString launcherAppId = launcherModel->get(i)->appId();
723 QString asAppId = getASConfig().at(i).value("id").toString();
724 QCOMPARE(launcherAppId, asAppId);
725 }
726 }
728 void testCountChangeSyncsToAS() {
729 // Find the index of the abs-icon app
730 int index = launcherModel->findApplication("abs-icon");
732 // Make sure it's invisible and 0 at the beginning
733 QCOMPARE(getASConfig().at(index).value("countVisible").toBool(), false);
734 QCOMPARE(getASConfig().at(index).value("count").toInt(), 0);
736 // Change the count of the abs-icon app through D-Bus
737 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
738 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", QVariant::fromValue(QDBusVariant(55)));
739 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(true)));
741 // Make sure it changed to visible and 55
742 QCOMPARE(getASConfig().at(index).value("countVisible").toBool(), true);
743 QCOMPARE(getASConfig().at(index).value("count").toInt(), 55);
744 }
746 void testSurfaceCountUpdates() {
747 QString appId = launcherModel->get(0)->appId();
749 QCOMPARE(launcherModel->get(0)->surfaceCount(), 0);
750 MockApp *app = qobject_cast<MockApp*>(appManager->findApplication(appId));
751 app->setSurfaceCount(1);
752 QCOMPARE(launcherModel->get(0)->surfaceCount(), 1);
753 }
754};
756QTEST_GUILESS_MAIN(LauncherModelTest)
757#include "launchermodeltest.moc"