on ae9bc90 help sign in
unity8/tests/plugins/Unity/Launcher/launchermodeltest.cpp
831 lines 35 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>
20#include <unity/shell/application/MirSurfaceListInterface.h>
22#include "launcheritem.h"
23#include "launchermodel.h"
24#include "dbusinterface.h"
25#include "gsettings.h"
26#include "asadapter.h"
27#include "AccountsServiceDBusAdaptor.h"
29#include <QtTest>
30#include <QDBusInterface>
31#include <QDBusReply>
32#include <QDBusMetaType>
33#include <QDomDocument>
35#include <glib.h>
36#include <paths.h>
38namespace unityapi = unity::shell::application;
40// This is a mock, specifically to test the LauncherModel
41class MockSurface: public unity::shell::application::MirSurfaceInterface
43 Q_OBJECT
44public:
45 MockSurface(const QString &id, QObject* parent): unityapi::MirSurfaceInterface(parent), m_id(id) {}
46 Mir::Type type() const override { return Mir::NormalType;}
47 QString name() const override { return QStringLiteral("mock surface"); }
48 QString persistentId() const override { return m_id; }
49 QPoint position() const override { return QPoint(); }
50 QSize size() const override { return QSize(); }
51 void resize(const QSize &size) override { Q_UNUSED(size) }
52 void resize(int width, int height) override { Q_UNUSED(width); Q_UNUSED(height) }
53 Mir::State state() const override { return Mir::RestoredState; }
54 bool live() const override { return true; }
55 bool visible() const override { return true; }
56 Mir::OrientationAngle orientationAngle() const override { return Mir::Angle0; }
57 void setOrientationAngle(Mir::OrientationAngle angle) override { Q_UNUSED(angle); }
58 int minimumWidth() const override { return 0; }
59 int minimumHeight() const override { return 0; }
60 int maximumWidth() const override { return 0; }
61 int maximumHeight() const override { return 0; }
62 int widthIncrement() const override { return 0; }
63 int heightIncrement() const override { return 0; }
64 void setKeymap(const QString &) override {}
65 Mir::ShellChrome shellChrome() const override { return Mir::NormalChrome; }
66 QString keymap() const override { return QStringLiteral("de_DE"); }
67 bool focused() const override { return true; }
68 QRect inputBounds() const override { return QRect(); }
69 bool confinesMousePointer() const override { return false; }
70 QPoint requestedPosition() const override { return QPoint(); }
71 void setRequestedPosition(const QPoint &) override {}
72 MirSurfaceInterface* parentSurface() const override { return nullptr; }
73 MirSurfaceListInterface* childSurfaceList() const override { return nullptr; }
74 void close() override {}
75 void activate() override {}
76 void requestState(Mir::State) override {}
78private:
79 QString m_id;
80};
82class MockSurfaceList: public unity::shell::application::MirSurfaceListInterface
84 Q_OBJECT
85public:
86 MockSurfaceList(QObject *parent): unityapi::MirSurfaceListInterface(parent) {}
87 int rowCount(const QModelIndex & = QModelIndex()) const override { return m_list.count(); }
88 QVariant data(const QModelIndex &, int) const override { return QVariant(); }
89 Q_INVOKABLE MirSurfaceInterface* get(int index) override { return m_list.at(index); }
90 void append(MirSurfaceInterface* surface) {
91 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
92 m_list.append(surface);
93 endInsertRows();
94 }
95private:
96 QList<MirSurfaceInterface*> m_list;
98};
100class MockApp: public unity::shell::application::ApplicationInfoInterface
102 Q_OBJECT
103public:
104 MockApp(const QString &appId, QObject *parent = 0): ApplicationInfoInterface(appId, parent), m_appId(appId), m_focused(false) {
105 m_surfaces = new MockSurfaceList(this);
106 }
108 RequestedState requestedState() const override { return RequestedRunning; }
109 void setRequestedState(RequestedState) override {}
110 QString appId() const override { return m_appId; }
111 QString name() const override { return "mock"; }
112 QString comment() const override { return "this is a mock"; }
113 QUrl icon() const override { return QUrl(); }
114 ApplicationInfoInterface::State state() const override { return ApplicationInfoInterface::Running; }
115 bool focused() const override { return m_focused; }
116 QString splashTitle() const override { return QString(); }
117 QUrl splashImage() const override { return QUrl(); }
118 bool splashShowHeader() const override { return true; }
119 QColor splashColor() const override { return QColor(0,0,0,0); }
120 QColor splashColorHeader() const override { return QColor(0,0,0,0); }
121 QColor splashColorFooter() const override { return QColor(0,0,0,0); }
122 Qt::ScreenOrientations supportedOrientations() const override { return Qt::PortraitOrientation; }
123 bool rotatesWindowContents() const override { return false; }
124 bool isTouchApp() const override { return true; }
125 bool exemptFromLifecycle() const override { return false; }
126 void setExemptFromLifecycle(bool) override {}
127 QSize initialSurfaceSize() const override { return QSize(); }
128 void setInitialSurfaceSize(const QSize &) override {}
129 MirSurfaceListInterface* surfaceList() const override { return m_surfaces; }
130 MirSurfaceListInterface* promptSurfaceList() const override { return nullptr; }
131 int surfaceCount() const override { return m_surfaces->count(); }
132 void setSurfaces(MockSurfaceList* surfaces) { m_surfaces = surfaces; Q_EMIT surfaceCountChanged(m_surfaces->count()); }
133 void close() override {}
135 // Methods used for mocking (not in the interface)
136 void setFocused(bool focused) { m_focused = focused; Q_EMIT focusedChanged(focused); }
137private:
138 QString m_appId;
139 bool m_focused;
140 MockSurfaceList *m_surfaces;
141};
143// This is a mock, specifically to test the LauncherModel
144class MockAppManager: public unity::shell::application::ApplicationManagerInterface
146 Q_OBJECT
147public:
148 MockAppManager(QObject *parent = 0): ApplicationManagerInterface(parent) {}
149 ~MockAppManager() {}
150 int rowCount(const QModelIndex &) const override { return m_list.count(); }
151 QVariant data(const QModelIndex &, int ) const override { return QVariant(); }
152 QString focusedApplicationId() const override {
153 Q_FOREACH(MockApp *app, m_list) {
154 if (app->focused()) return app->appId();
155 }
156 return QString();
157 }
158 unity::shell::application::ApplicationInfoInterface *get(int index) const override { return m_list.at(index); }
159 unity::shell::application::ApplicationInfoInterface *findApplication(const QString &appId) const override {
160 Q_FOREACH(MockApp* app, m_list) {
161 if (app->appId() == appId) {
162 return app;
163 }
164 }
165 return nullptr;
166 }
167 unityapi::ApplicationInfoInterface *findApplicationWithSurface(unityapi::MirSurfaceInterface* /*surface*/) const override {
168 return nullptr;
169 }
170 unity::shell::application::ApplicationInfoInterface *startApplication(const QString &, const QStringList &) override { return nullptr; }
171 bool stopApplication(const QString &appId) override {
172 Q_FOREACH(MockApp* app, m_list) {
173 if (app->appId() == appId) {
174 removeApplication(m_list.indexOf(app));
175 return true;
176 }
177 }
178 return false;
179 }
180 bool focusApplication(const QString &appId) {
181 Q_FOREACH(MockApp* app, m_list) {
182 app->setFocused(app->appId() == appId);
183 }
184 Q_EMIT focusedApplicationIdChanged();
185 return true;
186 }
188 void addApplication(MockApp *app) {
189 beginInsertRows(QModelIndex(), count(), count());
190 m_list.append(app);
191 endInsertRows();
192 }
193 void removeApplication(int index) {
194 beginRemoveRows(QModelIndex(), index, index);
195 m_list.takeAt(index)->deleteLater();
196 endRemoveRows();
197 }
198 bool requestFocusApplication(const QString &appId) override { Q_UNUSED(appId); return true; }
200private:
201 QList<MockApp*> m_list;
202};
204class LauncherModelTest : public QObject
206 Q_OBJECT
208private:
209 LauncherModel *launcherModel;
210 MockAppManager *appManager;
211 QTemporaryDir tmpDir;
213 QList<QVariantMap> getASConfig() {
214 AccountsServiceDBusAdaptor *as = launcherModel->m_asAdapter->m_accounts;
215 QDBusReply<QVariant> reply = as->getUserPropertyAsync(QString::fromUtf8(g_get_user_name()),
216 "com.canonical.unity.AccountsService",
217 "LauncherItems");
218 return qdbus_cast<QList<QVariantMap>>(reply.value().value<QDBusArgument>());
219 }
221 // Link our app data from a tempdir & tell glib/UAL to look there.
222 // We do this because we want to be able to delete the applications dir
223 // during testing, but that dir may be read-only (installed on system).
224 void setUpAppDir() {
225 QFile appDir(qgetenv("APPDIR"));
226 appDir.link(tmpDir.path() + "/applications");
228 qputenv("XDG_DATA_HOME", tmpDir.path().toUtf8());
229 }
231private Q_SLOTS:
233 void initTestCase() {
234 qDBusRegisterMetaType<QList<QVariantMap>>();
236 setUpAppDir();
238 launcherModel = new LauncherModel(this);
239 QCoreApplication::processEvents(); // to let the model register on DBus
240 QCOMPARE(launcherModel->rowCount(QModelIndex()), 0);
242 appManager = new MockAppManager(this);
243 launcherModel->setApplicationManager(appManager);
244 }
246 // Adding 2 apps to the mock appmanager. Both should appear in the launcher.
247 void init() {
248 // Switching to tmpDir makes manipulating our appdir symlink easier.
249 QDir::setCurrent(tmpDir.path());
251 QDBusInterface accountsInterface(QStringLiteral("org.freedesktop.Accounts"),
252 QStringLiteral("/org/freedesktop/Accounts"),
253 QStringLiteral("org.freedesktop.Accounts"));
254 QDBusReply<bool> addReply = accountsInterface.call(QStringLiteral("AddUser"),
255 QString::fromUtf8(g_get_user_name()));
256 QVERIFY(addReply.isValid());
257 QCOMPARE(addReply.value(), true);
259 appManager->addApplication(new MockApp("abs-icon"));
260 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
262 appManager->addApplication(new MockApp("rel-icon"));
263 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
265 launcherModel->m_settings->setStoredApplications(QStringList());
266 }
268 // Removing apps from appmanager and launcher as pinned ones would stick
269 void cleanup() {
270 while (appManager->count() > 0) {
271 appManager->removeApplication(0);
272 }
273 while (launcherModel->rowCount(QModelIndex()) > 0) {
274 launcherModel->requestRemove(launcherModel->get(0)->appId());
275 }
277 QDBusInterface accountsInterface(QStringLiteral("org.freedesktop.Accounts"),
278 QStringLiteral("/org/freedesktop/Accounts"),
279 QStringLiteral("org.freedesktop.Accounts"));
280 QDBusReply<bool> removeReply = accountsInterface.call(QStringLiteral("RemoveUser"),
281 QString::fromUtf8(g_get_user_name()));
282 QVERIFY(removeReply.isValid());
283 QCOMPARE(removeReply.value(), true);
285 // Some tests move the directory, so lets move it back if so.
286 // But this will usually fail.
287 QFile::rename("applications.old", "applications");
288 }
290 void testMove() {
291 QCOMPARE(launcherModel->get(0)->pinned(), false);
292 QCOMPARE(launcherModel->get(1)->pinned(), false);
294 LauncherItemInterface *item0BeforeMove = launcherModel->get(0);
295 LauncherItemInterface *item1BeforeMove = launcherModel->get(1);
296 launcherModel->move(1, 0);
298 QCOMPARE(item0BeforeMove, launcherModel->get(1));
299 QCOMPARE(item1BeforeMove, launcherModel->get(0));
301 // moved item must be pinned now
302 QCOMPARE(item0BeforeMove->pinned(), false);
303 QCOMPARE(item1BeforeMove->pinned(), true);
304 }
306 void testPinning() {
307 QSignalSpy spy(launcherModel, &LauncherModel::dataChanged);
308 QCOMPARE(launcherModel->get(0)->pinned(), false);
309 QCOMPARE(launcherModel->get(1)->pinned(), false);
310 launcherModel->pin(launcherModel->get(0)->appId());
311 QCOMPARE(launcherModel->get(0)->pinned(), true);
312 QCOMPARE(launcherModel->get(1)->pinned(), false);
313 QCOMPARE(spy.count() > 0, true);
314 QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
316 // App should be pinned now
317 spy.clear();
318 launcherModel->requestRemove(launcherModel->get(0)->appId());
319 QCOMPARE(launcherModel->get(0)->pinned(), false);
320 QCOMPARE(launcherModel->get(1)->pinned(), false);
321 QCOMPARE(spy.count(), 1);
322 QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
324 // Now that the app is unpinned, nothing should change and the signal should not be emitted
325 spy.clear();
326 launcherModel->requestRemove(launcherModel->get(0)->appId());
327 QCOMPARE(launcherModel->get(0)->pinned(), false);
328 QCOMPARE(launcherModel->get(1)->pinned(), false);
329 QCOMPARE(spy.count(), 0);
330 }
332 void testRemove_data() {
334 QTest::addColumn<bool>("pinned");
335 QTest::addColumn<bool>("running");
337 QTest::newRow("non-pinned, running") << false << true;
338 QTest::newRow("pinned, running") << true << true;
339 QTest::newRow("pinned, non-running") << true << false;
340 }
342 void testRemove() {
343 QFETCH(bool, pinned);
344 QFETCH(bool, running);
346 // In the beginning we always have two items
347 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
349 // pin one if required
350 if (pinned) {
351 launcherModel->pin("abs-icon");
352 }
354 // stop it if required
355 if (!running) {
356 appManager->stopApplication("abs-icon");
357 }
359 // Now remove it
360 launcherModel->requestRemove("abs-icon");
362 if (running) {
363 // both apps are running, both apps must still be here
364 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
366 // Item must be unpinned now
367 int index = launcherModel->findApplication("abs-icon");
368 QCOMPARE(launcherModel->get(index)->pinned(), false);
370 } else if (pinned) {
371 // Item 1 must go away, item 0 is here to stay
372 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
373 }
375 // done our checks. now stop the app if was still running
376 if (running) {
377 appManager->stopApplication("abs-icon");
378 }
380 // It needs to go away in any case now
381 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
382 }
384 void testQuickListPinningRemoving() {
385 // we start with 2 unpinned items
386 QCOMPARE(launcherModel->get(0)->pinned(), false);
387 QCOMPARE(launcherModel->get(1)->pinned(), false);
388 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
390 // find the Pin item in the quicklist
391 QuickListModel *model = qobject_cast<QuickListModel*>(launcherModel->get(0)->quickList());
392 int pinActionIndex = -1;
393 for (int i = 0; i < model->rowCount(QModelIndex()); ++i) {
394 if (model->get(i).actionId() == "pin_item") {
395 pinActionIndex = i;
396 break;
397 }
398 }
399 QVERIFY(pinActionIndex >= 0);
401 // trigger pin item quicklist action => Item must be pinned now.
402 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
403 QCOMPARE(launcherModel->get(0)->pinned(), true);
404 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
406 // quicklist needs to transform to remove item. trigger it and check it item goes away
407 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
408 QCOMPARE(launcherModel->get(0)->pinned(), false);
410 // still needs to be here as the app is still here
411 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
412 // close the app
413 appManager->removeApplication(0);
414 // Now it needs to go away
415 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
416 }
418 void testApplicationRunning() {
419 launcherModel->pin("abs-icon");
420 launcherModel->pin("rel-icon");
422 QCOMPARE(launcherModel->get(0)->running(), true);
423 QCOMPARE(launcherModel->get(1)->running(), true);
425 appManager->stopApplication("abs-icon");
426 QCOMPARE(launcherModel->get(0)->running(), false);
427 QCOMPARE(launcherModel->get(1)->running(), true);
429 appManager->stopApplication("rel-icon");
430 QCOMPARE(launcherModel->get(0)->running(), false);
431 QCOMPARE(launcherModel->get(1)->running(), false);
432 }
434 void testApplicationFocused() {
435 // all apps unfocused at beginning...
436 QCOMPARE(launcherModel->get(0)->focused(), false);
437 QCOMPARE(launcherModel->get(1)->focused(), false);
439 appManager->focusApplication("abs-icon");
440 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
441 QCOMPARE(launcherModel->get(0)->focused(), true);
442 QCOMPARE(launcherModel->get(1)->focused(), false);
444 appManager->focusApplication("rel-icon");
445 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
446 QCOMPARE(launcherModel->get(0)->focused(), false);
447 QCOMPARE(launcherModel->get(1)->focused(), true);
448 }
450 void testClosingApps() {
451 // At the start there are 2 items. Let's pin one.
452 launcherModel->pin("abs-icon");
453 while (appManager->count() > 0) {
454 appManager->removeApplication(0);
455 }
456 // The pinned one needs to stay, the other needs to disappear
457 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
458 QCOMPARE(launcherModel->get(0)->appId(), QLatin1String("abs-icon"));
459 }
461 void testQuitMenuItem() {
462 // we have 2 apps running, both should have the Quit action in its quick list
463 QCOMPARE(launcherModel->rowCount(), 2);
465 // stop the second one keeping it pinned so that it doesn't go away
466 launcherModel->pin("rel-icon");
467 appManager->stopApplication("rel-icon");
469 // find the first Quit item, should be there
470 QuickListModel *model = qobject_cast<QuickListModel*>(launcherModel->get(0)->quickList());
471 int quitActionIndex = -1;
472 for (int i = 0; i < model->rowCount(); ++i) {
473 if (model->get(i).actionId() == "stop_item") {
474 quitActionIndex = i;
475 break;
476 }
477 }
478 QVERIFY(quitActionIndex >= 0);
480 // find the second Quit item, should NOT be there, the app is stopped
481 QuickListModel *model2 = qobject_cast<QuickListModel*>(launcherModel->get(1)->quickList());
482 int quitActionIndex2 = -1;
483 for (int i = 0; i < model2->rowCount(); ++i) {
484 if (model2->get(i).actionId() == "stop_item") {
485 quitActionIndex2 = i;
486 break;
487 }
488 }
489 QVERIFY(quitActionIndex2 == -1);
491 // trigger the first quit item quicklist action
492 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), quitActionIndex);
493 // first app should be gone...
494 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
495 // ... the second app (now at index 0) should still be there, pinned and stopped
496 QCOMPARE(launcherModel->get(0)->appId(), QStringLiteral("rel-icon"));
497 QCOMPARE(launcherModel->get(0)->pinned(), true);
498 QCOMPARE(launcherModel->get(0)->running(), false);
499 }
501 void testGetUrlForAppId() {
502 QCOMPARE(launcherModel->getUrlForAppId(QString()), QString());
503 QCOMPARE(launcherModel->getUrlForAppId(""), QString());
504 QCOMPARE(launcherModel->getUrlForAppId("no-name"), QString("application:///no-name.desktop"));
505 QCOMPARE(launcherModel->getUrlForAppId("com.test.good"), QString("application:///com.test.good.desktop"));
506 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application"), QString("appid://com.test.good/application/current-user-version"));
507 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application_1.2.3"), QString("appid://com.test.good/application/current-user-version"));
508 }
510 void testIntrospection() {
511 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "org.freedesktop.DBus.Introspectable");
512 QDBusReply<QString> reply = interface.call("Introspect");
513 QStringList nodes = extractNodes(reply.value());
514 QCOMPARE(nodes.count(), launcherModel->rowCount());
516 appManager->addApplication(new MockApp("foobar"));
517 reply = interface.call("Introspect");
518 nodes = extractNodes(reply.value());
519 QCOMPARE(nodes.contains("foobar"), true);
521 appManager->removeApplication(2);
522 reply = interface.call("Introspect");
523 nodes = extractNodes(reply.value());
524 QCOMPARE(nodes.contains("foobar"), false);
525 }
527 QStringList extractNodes(const QString &introspectionXml) {
528 QXmlStreamReader introspectReply(introspectionXml);
530 QStringList ret;
531 while (!introspectReply.atEnd() && !introspectReply.hasError()) {
532 QXmlStreamReader::TokenType token = introspectReply.readNext();
534 if (token == QXmlStreamReader::StartElement) {
535 if (introspectReply.name() == "node" && introspectReply.attributes().count() > 0) {
536 ret << introspectReply.attributes().value("name").toString();
537 }
538 }
539 }
540 return ret;
541 }
543 void testCountEmblems() {
544 QSignalSpy spy(launcherModel, &LauncherModel::dataChanged);
546 // Call GetAll on abs-icon
547 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
548 QDBusReply<QVariantMap> reply = interface.call("GetAll");
549 QVariantMap map = reply.value();
551 // Check that the alerting-status is still false, and the item on the upper side of the API
552 int index = launcherModel->findApplication("abs-icon");
553 QCOMPARE(index >= 0, true);
554 QVERIFY(launcherModel->get(index)->alerting() == false);
556 // Make sure GetAll returns a map with count and countVisible props
557 QCOMPARE(map.contains("count"), true);
558 QCOMPARE(map.contains("countVisible"), true);
560 // Make sure count is intitilized to 0 and non-visible
561 QCOMPARE(map.value("count").toInt(), 0);
562 QCOMPARE(map.value("countVisible").toBool(), false);
564 // Now make it visible and set it to 55 through D-Bus
565 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", QVariant::fromValue(QDBusVariant(55)));
566 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(true)));
568 // Fetch it again using GetAll
569 reply = interface.call("GetAll");
570 map = reply.value();
572 // Make sure values have changed on the D-Bus interface
573 QCOMPARE(map.value("count").toInt(), 55);
574 QCOMPARE(map.value("countVisible").toBool(), true);
576 // Finally check, that the change to "count" implicitly also set the alerting-state to true
577 QVERIFY(launcherModel->get(index)->alerting() == true);
579 // Check if the launcher emitted the changed signals
580 QCOMPARE(spy.count(), 2);
582 QVariantList countEmissionArgs = spy.takeFirst();
583 QCOMPARE(countEmissionArgs.at(0).toModelIndex().row(), index);
584 QCOMPARE(countEmissionArgs.at(1).toModelIndex().row(), index);
585 QVector<int> roles = countEmissionArgs.at(2).value<QVector<int> >();
586 QCOMPARE(roles.first(), (int)LauncherModel::RoleCount);
588 QVariantList countVisibleEmissionArgs = spy.takeFirst();
589 QCOMPARE(countVisibleEmissionArgs.at(0).toModelIndex().row(), index);
590 QCOMPARE(countVisibleEmissionArgs.at(1).toModelIndex().row(), index);
591 roles = countVisibleEmissionArgs.at(2).value<QVector<int> >();
592 QVERIFY(roles.contains(LauncherModel::RoleCountVisible));
593 QVERIFY(roles.contains(LauncherModel::RoleAlerting));
595 // Check if the values match
596 QCOMPARE(launcherModel->get(index)->countVisible(), true);
597 QCOMPARE(launcherModel->get(index)->count(), 55);
598 QCOMPARE(launcherModel->get(index)->alerting(), true);
600 // Focus the app, make sure the alert gets cleared
601 appManager->focusApplication("abs-icon");
602 QVERIFY(launcherModel->get(index)->alerting() == false);
603 }
605 void testCountEmblemAddsRemovesItem_data() {
606 QTest::addColumn<bool>("isPinned");
607 QTest::addColumn<bool>("isRunning");
608 QTest::addColumn<bool>("startWhenVisible");
609 QTest::newRow("not pinned, not running") << false << false << false;
610 QTest::newRow("pinned, not running") << true << false << false;
611 QTest::newRow("not pinned, not running, starting from notification") << false << false << true;
612 QTest::newRow("pinned, not running, starting from notification") << true << false << true;
613 QTest::newRow("not pinned, running") << false << true << false;
614 QTest::newRow("pinned, running") << true << true << false;
615 }
617 void testCountEmblemAddsRemovesItem() {
618 QFETCH(bool, isPinned);
619 QFETCH(bool, isRunning);
620 QFETCH(bool, startWhenVisible);
622 // Make sure item is here as expected after init() and that count is not visible
623 int index = launcherModel->findApplication("abs-icon");
624 QCOMPARE(index == -1, false);
625 QCOMPARE(launcherModel->get(index)->countVisible(), false);
627 // Pin if we need to
628 if (isPinned) {
629 launcherModel->pin("abs-icon");
630 }
631 QCOMPARE(launcherModel->get(0)->pinned(), isPinned);
633 // Stop it if we need to
634 if (!isRunning) {
635 appManager->stopApplication("abs-icon");
636 }
637 QCOMPARE(launcherModel->findApplication("abs-icon") >= 0, isRunning || isPinned);
640 // set the count emblem to visible
641 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
642 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", QVariant::fromValue(QDBusVariant(55)));
643 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(true)));
645 // Make sure item is here and that count is visible
646 index = launcherModel->findApplication("abs-icon");
647 QCOMPARE(index == -1, false);
648 QCOMPARE(launcherModel->get(index)->countVisible(), true);
650 if (!isRunning && startWhenVisible) {
651 appManager->addApplication(new MockApp("abs-icon"));
652 }
654 // Hide count emblem again
655 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(false)));
657 // Make sure item is shown/hidden as expected
658 index = launcherModel->findApplication("abs-icon");
659 QCOMPARE(index == -1, !isRunning && !isPinned && !startWhenVisible);
660 }
662 void testAlert() {
663 // Check that the alerting-status is still false
664 int index = launcherModel->findApplication("abs-icon");
665 QCOMPARE(index >= 0, true);
666 QVERIFY(launcherModel->get(index)->alerting() == false);
668 // Call Alert() on "abs-icon"
669 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "com.canonical.Unity.Launcher.Item");
670 interface.call("Alert");
672 // Check that the alerting-status is now true
673 QVERIFY(launcherModel->get(index)->alerting() == true);
675 // Focus the app, make sure the alert gets cleared
676 appManager->focusApplication("abs-icon");
677 QVERIFY(launcherModel->get(index)->alerting() == false);
678 }
680 void testRefreshAfterDeletedDesktopFiles_data() {
681 QTest::addColumn<bool>("deleted");
682 QTest::newRow("have .desktop files") << false;
683 QTest::newRow("deleted .desktop files") << true;
684 }
686 void testRefreshAfterDeletedDesktopFiles() {
687 QFETCH(bool, deleted);
689 // pin both apps
690 launcherModel->pin("abs-icon");
691 launcherModel->pin("rel-icon");
692 // close both apps
693 appManager->removeApplication(0);
694 appManager->removeApplication(0);
696 // "delete" the .desktop files
697 if (deleted) {
698 // In testing mode, the launcher searches the current dir for the sample .desktop file
699 // We can make that fail by moving the applications dir
700 QFile::rename("applications", "applications.old");
701 }
703 // Call refresh
704 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "com.canonical.Unity.Launcher");
705 QDBusReply<void> reply = interface.call("Refresh");
707 // Make sure the call to Refresh returned without error.
708 QCOMPARE(reply.isValid(), true);
710 QCOMPARE(launcherModel->rowCount(), deleted ? 0 : 2);
711 }
713 void testSettings() {
714 GSettings *settings = launcherModel->m_settings;
715 QSignalSpy spy(launcherModel, &LauncherModel::hint);
717 // Nothing pinned at startup
718 QCOMPARE(settings->storedApplications().count(), 0);
720 // pin both apps
721 launcherModel->pin("abs-icon");
722 launcherModel->pin("rel-icon");
723 QCOMPARE(spy.count(), 0);
725 // Now settings should have 2 apps
726 QCOMPARE(settings->storedApplications().count(), 2);
728 // close both apps
729 appManager->removeApplication(0);
730 appManager->removeApplication(0);
732 // Now settings should have 2 apps
733 QCOMPARE(settings->storedApplications().count(), 2);
735 // Now remove 1 app through the backend, make sure one is still there
736 settings->simulateDConfChanged(QStringList() << "abs-icon");
737 QCOMPARE(settings->storedApplications().count(), 1);
738 QCOMPARE(spy.count(), 1);
740 // Check if it disappeared from the frontend too
741 QCOMPARE(launcherModel->rowCount(), 1);
743 // Add them back but in reverse order
744 settings->simulateDConfChanged(QStringList() << "rel-icon" << "abs-icon");
745 QCOMPARE(launcherModel->rowCount(), 2);
746 QCOMPARE(launcherModel->get(0)->appId(), QString("rel-icon"));
747 QCOMPARE(launcherModel->get(1)->appId(), QString("abs-icon"));
748 QCOMPARE(spy.count(), 2);
749 }
751 void testAddSyncsToAS() {
752 // Make sure launcher and AS are in sync when we start the test
753 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
755 int oldCount = launcherModel->rowCount();
756 appManager->addApplication(new MockApp("click-icon"));
757 QCOMPARE(launcherModel->rowCount(), oldCount + 1);
758 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
759 }
761 void testRemoveSyncsToAS() {
762 // Make sure launcher and AS are in sync when we start the test
763 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
765 int oldCount = launcherModel->rowCount();
766 appManager->stopApplication("abs-icon");
767 QCOMPARE(launcherModel->rowCount(), oldCount - 1);
768 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
769 }
771 void testMoveSyncsToAS() {
772 // Make sure launcher and AS are in sync when we start the test
773 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
775 for (int i = 0; i < launcherModel->rowCount(); i++) {
776 QString launcherAppId = launcherModel->get(i)->appId();
777 QString asAppId = getASConfig().at(i).value("id").toString();
778 QCOMPARE(launcherAppId, asAppId);
779 }
781 launcherModel->move(0, 1);
783 for (int i = 0; i < launcherModel->rowCount(); i++) {
784 QString launcherAppId = launcherModel->get(i)->appId();
785 QString asAppId = getASConfig().at(i).value("id").toString();
786 QCOMPARE(launcherAppId, asAppId);
787 }
788 }
790 void testCountChangeSyncsToAS() {
791 // Find the index of the abs-icon app
792 int index = launcherModel->findApplication("abs-icon");
794 // Make sure it's invisible and 0 at the beginning
795 QCOMPARE(getASConfig().at(index).value("countVisible").toBool(), false);
796 QCOMPARE(getASConfig().at(index).value("count").toInt(), 0);
798 // Change the count of the abs-icon app through D-Bus
799 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
800 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", QVariant::fromValue(QDBusVariant(55)));
801 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(true)));
803 // Make sure it changed to visible and 55
804 QCOMPARE(getASConfig().at(index).value("countVisible").toBool(), true);
805 QCOMPARE(getASConfig().at(index).value("count").toInt(), 55);
806 }
808 void testSurfaceCountUpdates() {
809 QString appId = launcherModel->get(0)->appId();
811 QCOMPARE(launcherModel->get(0)->surfaceCount(), 0);
812 MockApp *app = qobject_cast<MockApp*>(appManager->findApplication(appId));
813 MockSurfaceList* surfaces = new MockSurfaceList(appManager);
814 surfaces->append(new MockSurface("foobar", surfaces));
815 app->setSurfaces(surfaces);
816 QCOMPARE(launcherModel->get(0)->surfaceCount(), 1);
818 // Make sure the new surface appears in the quicklist
819 QuickListModel *quickList = qobject_cast<QuickListModel*>(launcherModel->get(0)->quickList());
820 bool found = false;
821 for (int i = 0; i < quickList->rowCount(); ++i) {
822 if (quickList->get(i).actionId() == "surface_foobar") {
823 found = true;
824 }
825 }
826 QCOMPARE(found, true);
827 }
828};
830QTEST_GUILESS_MAIN(LauncherModelTest)
831#include "launchermodeltest.moc"