on d43eea3 help sign in
unity8/tests/plugins/Unity/Launcher/launchermodeltest.cpp
539 lines 21 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 <QDomDocument>
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) { }
42 QString appId() const { return m_appId; }
43 QString name() const { return "mock"; }
44 QString comment() const { return "this is a mock"; }
45 QUrl icon() const { return QUrl(); }
46 ApplicationInfoInterface::Stage stage() const { return ApplicationInfoInterface::MainStage; }
47 ApplicationInfoInterface::State state() const { return ApplicationInfoInterface::Running; }
48 bool focused() const { return m_focused; }
49 QString splashTitle() const override { return QString(); }
50 QUrl splashImage() const override { return QUrl(); }
51 bool splashShowHeader() const override { return true; }
52 QColor splashColor() const override { return QColor(0,0,0,0); }
53 QColor splashColorHeader() const override { return QColor(0,0,0,0); }
54 QColor splashColorFooter() const override { return QColor(0,0,0,0); }
55 Qt::ScreenOrientations supportedOrientations() const override { return Qt::PortraitOrientation; }
56 bool rotatesWindowContents() const override { return false; }
58 // Methods used for mocking (not in the interface)
59 void setFocused(bool focused) { m_focused = focused; Q_EMIT focusedChanged(focused); }
60private:
61 QString m_appId;
62 bool m_focused;
63};
65// This is a mock, specifically to test the LauncherModel
66class MockAppManager: public unity::shell::application::ApplicationManagerInterface
68 Q_OBJECT
69public:
70 MockAppManager(QObject *parent = 0): ApplicationManagerInterface(parent) {}
71 int rowCount(const QModelIndex &) const { return m_list.count(); }
72 QVariant data(const QModelIndex &, int ) const { return QVariant(); }
73 QString focusedApplicationId() const {
74 Q_FOREACH(MockApp *app, m_list) {
75 if (app->focused()) return app->appId();
76 }
77 return QString();
78 }
79 unity::shell::application::ApplicationInfoInterface *get(int index) const { return m_list.at(index); }
80 unity::shell::application::ApplicationInfoInterface *findApplication(const QString &appId) const {
81 Q_FOREACH(MockApp* app, m_list) {
82 if (app->appId() == appId) {
83 return app;
84 }
85 }
86 return nullptr;
87 }
88 unity::shell::application::ApplicationInfoInterface *startApplication(const QString &, const QStringList &) { return nullptr; }
89 bool stopApplication(const QString &appId) {
90 Q_FOREACH(MockApp* app, m_list) {
91 if (app->appId() == appId) {
92 removeApplication(m_list.indexOf(app));
93 return true;
94 }
95 }
96 return false;
97 }
98 bool focusApplication(const QString &appId) {
99 Q_FOREACH(MockApp* app, m_list) {
100 app->setFocused(app->appId() == appId);
101 }
102 Q_EMIT focusedApplicationIdChanged();
103 return true;
104 }
106 void unfocusCurrentApplication() { }
108 void addApplication(MockApp *app) {
109 beginInsertRows(QModelIndex(), count(), count());
110 m_list.append(app);
111 endInsertRows();
112 }
113 void removeApplication(int index) {
114 beginRemoveRows(QModelIndex(), index, index);
115 m_list.takeAt(index)->deleteLater();
116 endRemoveRows();
117 }
118 bool requestFocusApplication(const QString &appId) { Q_UNUSED(appId); return true; }
119 bool suspended() const { return false; }
120 void setSuspended(bool) {}
121 bool forceDashActive() const { return false; }
122 void setForceDashActive(bool) {}
124private:
125 QList<MockApp*> m_list;
126};
128class LauncherModelTest : public QObject
130 Q_OBJECT
132private:
133 LauncherModel *launcherModel;
134 MockAppManager *appManager;
136 QList<QVariantMap> getASConfig() {
137 AccountsServiceDBusAdaptor *as = launcherModel->m_asAdapter->m_accounts;
138 return as->getUserProperty("", "", "LauncherItems").value<QList<QVariantMap>>();
139 }
141private Q_SLOTS:
143 void initTestCase() {
144 launcherModel = new LauncherModel(this);
145 QCoreApplication::processEvents(); // to let the model register on DBus
146 QCOMPARE(launcherModel->rowCount(QModelIndex()), 0);
148 appManager = new MockAppManager(this);
149 launcherModel->setApplicationManager(appManager);
150 }
152 // Adding 2 apps to the mock appmanager. Both should appear in the launcher.
153 void init() {
154 appManager->addApplication(new MockApp("abs-icon"));
155 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
157 appManager->addApplication(new MockApp("no-icon"));
158 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
160 launcherModel->m_settings->setStoredApplications(QStringList());
161 }
163 // Removing apps from appmanager and launcher as pinned ones would stick
164 void cleanup() {
165 while (appManager->count() > 0) {
166 appManager->removeApplication(0);
167 }
168 while (launcherModel->rowCount(QModelIndex()) > 0) {
169 launcherModel->requestRemove(launcherModel->get(0)->appId());
170 }
171 }
173 void testMove() {
174 QCOMPARE(launcherModel->get(0)->pinned(), false);
175 QCOMPARE(launcherModel->get(1)->pinned(), false);
177 LauncherItemInterface *item0BeforeMove = launcherModel->get(0);
178 LauncherItemInterface *item1BeforeMove = launcherModel->get(1);
179 launcherModel->move(1, 0);
181 QCOMPARE(item0BeforeMove, launcherModel->get(1));
182 QCOMPARE(item1BeforeMove, launcherModel->get(0));
184 // moved item must be pinned now
185 QCOMPARE(item0BeforeMove->pinned(), false);
186 QCOMPARE(item1BeforeMove->pinned(), true);
187 }
189 void testPinning() {
190 QSignalSpy spy(launcherModel, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)));
191 QCOMPARE(launcherModel->get(0)->pinned(), false);
192 QCOMPARE(launcherModel->get(1)->pinned(), false);
193 launcherModel->pin(launcherModel->get(0)->appId());
194 QCOMPARE(launcherModel->get(0)->pinned(), true);
195 QCOMPARE(launcherModel->get(1)->pinned(), false);
196 QCOMPARE(spy.count() > 0, true);
197 QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
199 // App should be pinned now
200 spy.clear();
201 launcherModel->requestRemove(launcherModel->get(0)->appId());
202 QCOMPARE(launcherModel->get(0)->pinned(), false);
203 QCOMPARE(launcherModel->get(1)->pinned(), false);
204 QCOMPARE(spy.count(), 1);
205 QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
207 // Now that the app is unpinned, nothing should change and the signal should not be emitted
208 spy.clear();
209 launcherModel->requestRemove(launcherModel->get(0)->appId());
210 QCOMPARE(launcherModel->get(0)->pinned(), false);
211 QCOMPARE(launcherModel->get(1)->pinned(), false);
212 QCOMPARE(spy.count(), 0);
213 }
215 void testRemove_data() {
217 QTest::addColumn<bool>("pinned");
218 QTest::addColumn<bool>("running");
220 QTest::newRow("non-pinned, running") << false << true;
221 QTest::newRow("pinned, running") << true << true;
222 QTest::newRow("pinned, non-running") << true << false;
223 }
225 void testRemove() {
226 QFETCH(bool, pinned);
227 QFETCH(bool, running);
229 // In the beginning we always have two items
230 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
232 // pin one if required
233 if (pinned) {
234 launcherModel->pin("abs-icon");
235 }
237 // stop it if required
238 if (!running) {
239 appManager->stopApplication("abs-icon");
240 }
242 // Now remove it
243 launcherModel->requestRemove("abs-icon");
245 if (running) {
246 // both apps are running, both apps must still be here
247 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
249 // Item must be unpinned now
250 int index = launcherModel->findApplication("abs-icon");
251 QCOMPARE(launcherModel->get(index)->pinned(), false);
253 } else if (pinned) {
254 // Item 1 must go away, item 0 is here to stay
255 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
256 }
258 // done our checks. now stop the app if was still running
259 if (running) {
260 appManager->stopApplication("abs-icon");
261 }
263 // It needs to go away in any case now
264 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
265 }
267 void testQuickListPinningRemoving() {
268 // we start with 2 unpinned items
269 QCOMPARE(launcherModel->get(0)->pinned(), false);
270 QCOMPARE(launcherModel->get(1)->pinned(), false);
271 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
273 // find the Pin item in the quicklist
274 QuickListModel *model = qobject_cast<QuickListModel*>(launcherModel->get(0)->quickList());
275 int pinActionIndex = -1;
276 for (int i = 0; i < model->rowCount(QModelIndex()); ++i) {
277 if (model->get(i).actionId() == "pin_item") {
278 pinActionIndex = i;
279 break;
280 }
281 }
282 QVERIFY(pinActionIndex >= 0);
284 // trigger pin item quicklist action => Item must be pinned now.
285 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
286 QCOMPARE(launcherModel->get(0)->pinned(), true);
287 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
289 // quicklist needs to transform to remove item. trigger it and check it item goes away
290 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
291 QCOMPARE(launcherModel->get(0)->pinned(), false);
293 // still needs to be here as the app is still here
294 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
295 // close the app
296 appManager->removeApplication(0);
297 // Now it needs to go away
298 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
299 }
301 void testApplicationFocused() {
302 // all apps unfocused at beginning...
303 QCOMPARE(launcherModel->get(0)->focused(), false);
304 QCOMPARE(launcherModel->get(1)->focused(), false);
306 appManager->focusApplication("abs-icon");
307 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
308 QCOMPARE(launcherModel->get(0)->focused(), true);
309 QCOMPARE(launcherModel->get(1)->focused(), false);
311 appManager->focusApplication("no-icon");
312 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
313 QCOMPARE(launcherModel->get(0)->focused(), false);
314 QCOMPARE(launcherModel->get(1)->focused(), true);
315 }
317 void testClosingApps() {
318 // At the start there are 2 items. Let's pin one.
319 launcherModel->pin("abs-icon");
320 while (appManager->count() > 0) {
321 appManager->removeApplication(0);
322 }
323 // The pinned one needs to stay, the other needs to disappear
324 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
325 QCOMPARE(launcherModel->get(0)->appId(), QLatin1String("abs-icon"));
326 }
328 void testGetUrlForAppId() {
329 QCOMPARE(launcherModel->getUrlForAppId(QString()), QString());
330 QCOMPARE(launcherModel->getUrlForAppId(""), QString());
331 QCOMPARE(launcherModel->getUrlForAppId("no-name"), QString("application:///no-name.desktop"));
332 QCOMPARE(launcherModel->getUrlForAppId("com.test.good"), QString("application:///com.test.good.desktop"));
333 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application"), QString("appid://com.test.good/application/current-user-version"));
334 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application_1.2.3"), QString("appid://com.test.good/application/current-user-version"));
335 }
337 void testIntrospection() {
338 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "org.freedesktop.DBus.Introspectable");
339 QDBusReply<QString> reply = interface.call("Introspect");
340 QStringList nodes = extractNodes(reply.value());
341 QCOMPARE(nodes.count(), launcherModel->rowCount());
343 appManager->addApplication(new MockApp("foobar"));
344 reply = interface.call("Introspect");
345 nodes = extractNodes(reply.value());
346 QCOMPARE(nodes.contains("foobar"), true);
348 appManager->removeApplication(2);
349 reply = interface.call("Introspect");
350 nodes = extractNodes(reply.value());
351 QCOMPARE(nodes.contains("foobar"), false);
352 }
354 QStringList extractNodes(const QString &introspectionXml) {
355 QXmlStreamReader introspectReply(introspectionXml);
357 QStringList ret;
358 while (!introspectReply.atEnd() && !introspectReply.hasError()) {
359 QXmlStreamReader::TokenType token = introspectReply.readNext();
361 if (token == QXmlStreamReader::StartElement) {
362 if (introspectReply.name() == "node" && introspectReply.attributes().count() > 0) {
363 ret << introspectReply.attributes().value("name").toString();
364 }
365 }
366 }
367 return ret;
368 }
370 void testCountEmblems() {
371 // Call GetAll on abs-icon
372 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
373 QDBusReply<QVariantMap> reply = interface.call("GetAll");
374 QVariantMap map = reply.value();
376 // Make sure GetAll returns a map with count and countVisible props
377 QCOMPARE(map.contains("count"), true);
378 QCOMPARE(map.contains("countVisible"), true);
380 // Make sure count is intitilized to 0 and non-visible
381 QCOMPARE(map.value("count").toInt(), 0);
382 QCOMPARE(map.value("countVisible").toBool(), false);
384 // Now make it visible and set it to 55 through D-Bus
385 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", QVariant::fromValue(QDBusVariant(55)));
386 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(true)));
388 // Fetch it again using GetAll
389 reply = interface.call("GetAll");
390 map = reply.value();
392 // Make sure values have changed on the D-Bus interface
393 QCOMPARE(map.value("count").toInt(), 55);
394 QCOMPARE(map.value("countVisible").toBool(), true);
396 // Now the item on the upper side of the API
397 int index = launcherModel->findApplication("abs-icon");
398 QCOMPARE(index >= 0, true);
400 // And make sure values have changed there as well
401 QCOMPARE(launcherModel->get(index)->countVisible(), true);
402 QCOMPARE(launcherModel->get(index)->count(), 55);
403 }
405 void testRefreshAfterDeletedDesktopFiles_data() {
406 QTest::addColumn<bool>("deleted");
407 QTest::newRow("have .desktop files") << false;
408 QTest::newRow("deleted .desktop files") << true;
409 }
411 void testRefreshAfterDeletedDesktopFiles() {
412 QFETCH(bool, deleted);
414 // pin both apps
415 launcherModel->pin("abs-icon");
416 launcherModel->pin("no-icon");
417 // close both apps
418 appManager->removeApplication(0);
419 appManager->removeApplication(0);
421 // "delete" the .desktop files
422 QString oldCurrent = QDir::currentPath();
423 if (deleted) {
424 // In testing mode, the launcher searches the current dir for the sample .desktop file
425 // We can make that fail by changing the current dir
426 QDir::setCurrent("..");
427 }
429 // Call refresh
430 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "com.canonical.Unity.Launcher");
431 QDBusReply<void> reply = interface.call("Refresh");
433 // Make sure the call to Refresh returned without error.
434 QCOMPARE(reply.isValid(), true);
436 QCOMPARE(launcherModel->rowCount(), deleted ? 0 : 2);
438 // Restoring current dir
439 QDir::setCurrent(oldCurrent);
440 }
442 void testSettings() {
443 GSettings *settings = launcherModel->m_settings;
444 QSignalSpy spy(launcherModel, SIGNAL(hint()));
446 // Nothing pinned at startup
447 QCOMPARE(settings->storedApplications().count(), 0);
449 // pin both apps
450 launcherModel->pin("abs-icon");
451 launcherModel->pin("no-icon");
452 QCOMPARE(spy.count(), 0);
454 // Now settings should have 2 apps
455 QCOMPARE(settings->storedApplications().count(), 2);
457 // close both apps
458 appManager->removeApplication(0);
459 appManager->removeApplication(0);
461 // Now settings should have 2 apps
462 QCOMPARE(settings->storedApplications().count(), 2);
464 // Now remove 1 app through the backend, make sure one is still there
465 settings->simulateDConfChanged(QStringList() << "abs-icon");
466 QCOMPARE(settings->storedApplications().count(), 1);
467 QCOMPARE(spy.count(), 1);
469 // Check if it disappeared from the frontend too
470 QCOMPARE(launcherModel->rowCount(), 1);
472 // Add them back but in reverse order
473 settings->simulateDConfChanged(QStringList() << "no-icon" << "abs-icon");
474 QCOMPARE(launcherModel->rowCount(), 2);
475 QCOMPARE(launcherModel->get(0)->appId(), QString("no-icon"));
476 QCOMPARE(launcherModel->get(1)->appId(), QString("abs-icon"));
477 QCOMPARE(spy.count(), 2);
478 }
480 void testAddSyncsToAS() {
481 // Make sure launcher and AS are in sync when we start the test
482 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
484 int oldCount = launcherModel->rowCount();
485 appManager->addApplication(new MockApp("rel-icon"));
486 QCOMPARE(launcherModel->rowCount(), oldCount + 1);
487 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
488 }
490 void testRemoveSyncsToAS() {
491 // Make sure launcher and AS are in sync when we start the test
492 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
494 int oldCount = launcherModel->rowCount();
495 appManager->stopApplication("abs-icon");
496 QCOMPARE(launcherModel->rowCount(), oldCount - 1);
497 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
498 }
500 void testMoveSyncsToAS() {
501 // Make sure launcher and AS are in sync when we start the test
502 QCOMPARE(launcherModel->rowCount(), getASConfig().count());
504 for (int i = 0; i < launcherModel->rowCount(); i++) {
505 QString launcherAppId = launcherModel->get(i)->appId();
506 QString asAppId = getASConfig().at(i).value("id").toString();
507 QCOMPARE(launcherAppId, asAppId);
508 }
510 launcherModel->move(0, 1);
512 for (int i = 0; i < launcherModel->rowCount(); i++) {
513 QString launcherAppId = launcherModel->get(i)->appId();
514 QString asAppId = getASConfig().at(i).value("id").toString();
515 QCOMPARE(launcherAppId, asAppId);
516 }
517 }
519 void testCountChangeSyncsToAS() {
520 // Find the index of the abs-icon app
521 int index = launcherModel->findApplication("abs-icon");
523 // Make sure it's invisible and 0 at the beginning
524 QCOMPARE(getASConfig().at(index).value("countVisible").toBool(), false);
525 QCOMPARE(getASConfig().at(index).value("count").toInt(), 0);
527 // Change the count of the abs-icon app through D-Bus
528 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
529 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", QVariant::fromValue(QDBusVariant(55)));
530 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", QVariant::fromValue(QDBusVariant(true)));
532 // Make sure it changed to visible and 55
533 QCOMPARE(getASConfig().at(index).value("countVisible").toBool(), true);
534 QCOMPARE(getASConfig().at(index).value("count").toInt(), 55);
535 }
536};
538QTEST_GUILESS_MAIN(LauncherModelTest)
539#include "launchermodeltest.moc"