on a8aed54 help sign in
unity8/tests/plugins/Unity/Launcher/launchermodeltest.cpp
472 lines 18 kB history raw
1/*
2 * Copyright 2013 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"
29#include <QtTest>
30#include <QDBusInterface>
31#include <QDBusReply>
32#include <QDomDocument>
34// This is a mock, specifically to test the LauncherModel
35class MockApp: public unity::shell::application::ApplicationInfoInterface
37 Q_OBJECT
38public:
39 MockApp(const QString &appId, QObject *parent = 0): ApplicationInfoInterface(appId, parent), m_appId(appId), m_focused(false) { }
40 QString appId() const { return m_appId; }
41 QString name() const { return "mock"; }
42 QString comment() const { return "this is a mock"; }
43 QUrl icon() const { return QUrl(); }
44 ApplicationInfoInterface::Stage stage() const { return ApplicationInfoInterface::MainStage; }
45 ApplicationInfoInterface::State state() const { return ApplicationInfoInterface::Running; }
46 bool focused() const { return m_focused; }
47 QString splashTitle() const override { return QString(); }
48 QUrl splashImage() const override { return QUrl(); }
49 bool splashShowHeader() const override { return true; }
50 QColor splashColor() const override { return QColor(0,0,0,0); }
51 QColor splashColorHeader() const override { return QColor(0,0,0,0); }
52 QColor splashColorFooter() const override { return QColor(0,0,0,0); }
54 // Methods used for mocking (not in the interface)
55 void setFocused(bool focused) { m_focused = focused; Q_EMIT focusedChanged(focused); }
56private:
57 QString m_appId;
58 bool m_focused;
59};
61// This is a mock, specifically to test the LauncherModel
62class MockAppManager: public unity::shell::application::ApplicationManagerInterface
64 Q_OBJECT
65public:
66 MockAppManager(QObject *parent = 0): ApplicationManagerInterface(parent) {}
67 int rowCount(const QModelIndex &) const { return m_list.count(); }
68 QVariant data(const QModelIndex &, int ) const { return QVariant(); }
69 QString focusedApplicationId() const {
70 Q_FOREACH(MockApp *app, m_list) {
71 if (app->focused()) return app->appId();
72 }
73 return QString();
74 }
75 unity::shell::application::ApplicationInfoInterface *get(int index) const { return m_list.at(index); }
76 unity::shell::application::ApplicationInfoInterface *findApplication(const QString &appId) const {
77 Q_FOREACH(MockApp* app, m_list) {
78 if (app->appId() == appId) {
79 return app;
80 }
81 }
82 return nullptr;
83 }
84 unity::shell::application::ApplicationInfoInterface *startApplication(const QString &, const QStringList &) { return nullptr; }
85 bool stopApplication(const QString &appId) {
86 Q_FOREACH(MockApp* app, m_list) {
87 if (app->appId() == appId) {
88 removeApplication(m_list.indexOf(app));
89 return true;
90 }
91 }
92 return false;
93 }
94 bool focusApplication(const QString &appId) {
95 Q_FOREACH(MockApp* app, m_list) {
96 app->setFocused(app->appId() == appId);
97 }
98 Q_EMIT focusedApplicationIdChanged();
99 return true;
100 }
102 void unfocusCurrentApplication() { }
104 void addApplication(MockApp *app) {
105 beginInsertRows(QModelIndex(), count(), count());
106 m_list.append(app);
107 endInsertRows();
108 }
109 void removeApplication(int index) {
110 beginRemoveRows(QModelIndex(), index, index);
111 m_list.takeAt(index)->deleteLater();
112 endRemoveRows();
113 }
114 bool requestFocusApplication(const QString &appId) { Q_UNUSED(appId); return true; }
115 bool suspended() const { return false; }
116 void setSuspended(bool) {}
117 bool forceDashActive() const { return false; }
118 void setForceDashActive(bool) {}
120private:
121 QList<MockApp*> m_list;
122};
124class LauncherModelTest : public QObject
126 Q_OBJECT
128private:
129 LauncherModel *launcherModel;
130 MockAppManager *appManager;
132private Q_SLOTS:
134 void initTestCase() {
135 launcherModel = new LauncherModel(this);
136 QCOMPARE(launcherModel->rowCount(QModelIndex()), 0);
138 appManager = new MockAppManager(this);
139 launcherModel->setApplicationManager(appManager);
140 }
142 // Adding 2 apps to the mock appmanager. Both should appear in the launcher.
143 void init() {
144 appManager->addApplication(new MockApp("abs-icon"));
145 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
147 appManager->addApplication(new MockApp("no-icon"));
148 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
150 launcherModel->m_settings->setStoredApplications(QStringList());
151 }
153 // Removing apps from appmanager and launcher as pinned ones would stick
154 void cleanup() {
155 while (appManager->count() > 0) {
156 appManager->removeApplication(0);
157 }
158 while (launcherModel->rowCount(QModelIndex()) > 0) {
159 launcherModel->requestRemove(launcherModel->get(0)->appId());
160 }
161 }
163 void testMove() {
164 QCOMPARE(launcherModel->get(0)->pinned(), false);
165 QCOMPARE(launcherModel->get(1)->pinned(), false);
167 LauncherItemInterface *item0BeforeMove = launcherModel->get(0);
168 LauncherItemInterface *item1BeforeMove = launcherModel->get(1);
169 launcherModel->move(1, 0);
171 QCOMPARE(item0BeforeMove, launcherModel->get(1));
172 QCOMPARE(item1BeforeMove, launcherModel->get(0));
174 // moved item must be pinned now
175 QCOMPARE(item0BeforeMove->pinned(), false);
176 QCOMPARE(item1BeforeMove->pinned(), true);
177 }
179 void testPinning() {
180 QSignalSpy spy(launcherModel, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)));
181 QCOMPARE(launcherModel->get(0)->pinned(), false);
182 QCOMPARE(launcherModel->get(1)->pinned(), false);
183 launcherModel->pin(launcherModel->get(0)->appId());
184 QCOMPARE(launcherModel->get(0)->pinned(), true);
185 QCOMPARE(launcherModel->get(1)->pinned(), false);
186 QCOMPARE(spy.count() > 0, true);
187 QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
189 // App should be pinned now
190 spy.clear();
191 launcherModel->requestRemove(launcherModel->get(0)->appId());
192 QCOMPARE(launcherModel->get(0)->pinned(), false);
193 QCOMPARE(launcherModel->get(1)->pinned(), false);
194 QCOMPARE(spy.count(), 1);
195 QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
197 // Now that the app is unpinned, nothing should change and the signal should not be emitted
198 spy.clear();
199 launcherModel->requestRemove(launcherModel->get(0)->appId());
200 QCOMPARE(launcherModel->get(0)->pinned(), false);
201 QCOMPARE(launcherModel->get(1)->pinned(), false);
202 QCOMPARE(spy.count(), 0);
203 }
205 void testRemove_data() {
207 QTest::addColumn<bool>("pinned");
208 QTest::addColumn<bool>("running");
210 QTest::newRow("non-pinned, running") << false << true;
211 QTest::newRow("pinned, running") << true << true;
212 QTest::newRow("pinned, non-running") << true << false;
213 }
215 void testRemove() {
216 QFETCH(bool, pinned);
217 QFETCH(bool, running);
219 // In the beginning we always have two items
220 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
222 // pin one if required
223 if (pinned) {
224 launcherModel->pin("abs-icon");
225 }
227 // stop it if required
228 if (!running) {
229 appManager->stopApplication("abs-icon");
230 }
232 // Now remove it
233 launcherModel->requestRemove("abs-icon");
235 if (running) {
236 // both apps are running, both apps must still be here
237 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
239 // Item must be unpinned now
240 int index = launcherModel->findApplication("abs-icon");
241 QCOMPARE(launcherModel->get(index)->pinned(), false);
243 } else if (pinned) {
244 // Item 1 must go away, item 0 is here to stay
245 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
246 }
248 // done our checks. now stop the app if was still running
249 if (running) {
250 appManager->stopApplication("abs-icon");
251 }
253 // It needs to go away in any case now
254 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
255 }
257 void testQuickListPinningRemoving() {
258 // we start with 2 unpinned items
259 QCOMPARE(launcherModel->get(0)->pinned(), false);
260 QCOMPARE(launcherModel->get(1)->pinned(), false);
261 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
263 // find the Pin item in the quicklist
264 QuickListModel *model = qobject_cast<QuickListModel*>(launcherModel->get(0)->quickList());
265 int pinActionIndex = -1;
266 for (int i = 0; i < model->rowCount(QModelIndex()); ++i) {
267 if (model->get(i).actionId() == "pin_item") {
268 pinActionIndex = i;
269 break;
270 }
271 }
272 QVERIFY(pinActionIndex >= 0);
274 // trigger pin item quicklist action => Item must be pinned now.
275 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
276 QCOMPARE(launcherModel->get(0)->pinned(), true);
277 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
279 // quicklist needs to transform to remove item. trigger it and check it item goes away
280 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
281 QCOMPARE(launcherModel->get(0)->pinned(), false);
283 // still needs to be here as the app is still here
284 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
285 // close the app
286 appManager->removeApplication(0);
287 // Now it needs to go away
288 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
289 }
291 void testApplicationFocused() {
292 // all apps unfocused at beginning...
293 QCOMPARE(launcherModel->get(0)->focused(), false);
294 QCOMPARE(launcherModel->get(1)->focused(), false);
296 appManager->focusApplication("abs-icon");
297 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
298 QCOMPARE(launcherModel->get(0)->focused(), true);
299 QCOMPARE(launcherModel->get(1)->focused(), false);
301 appManager->focusApplication("no-icon");
302 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
303 QCOMPARE(launcherModel->get(0)->focused(), false);
304 QCOMPARE(launcherModel->get(1)->focused(), true);
305 }
307 void testClosingApps() {
308 // At the start there are 2 items. Let's pin one.
309 launcherModel->pin("abs-icon");
310 while (appManager->count() > 0) {
311 appManager->removeApplication(0);
312 }
313 // The pinned one needs to stay, the other needs to disappear
314 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
315 QCOMPARE(launcherModel->get(0)->appId(), QLatin1String("abs-icon"));
316 }
318 void testGetUrlForAppId() {
319 QCOMPARE(launcherModel->getUrlForAppId(QString()), QString());
320 QCOMPARE(launcherModel->getUrlForAppId(""), QString());
321 QCOMPARE(launcherModel->getUrlForAppId("no-name"), QString("application:///no-name.desktop"));
322 QCOMPARE(launcherModel->getUrlForAppId("com.test.good"), QString("application:///com.test.good.desktop"));
323 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application"), QString("appid://com.test.good/application/current-user-version"));
324 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application_1.2.3"), QString("appid://com.test.good/application/current-user-version"));
325 }
327 void testIntrospection() {
328 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "org.freedesktop.DBus.Introspectable");
329 QDBusReply<QString> reply = interface.call("Introspect");
330 QStringList nodes = extractNodes(reply.value());
331 QCOMPARE(nodes.count(), launcherModel->rowCount());
333 appManager->addApplication(new MockApp("foobar"));
334 reply = interface.call("Introspect");
335 nodes = extractNodes(reply.value());
336 QCOMPARE(nodes.contains("foobar"), true);
338 appManager->removeApplication(2);
339 reply = interface.call("Introspect");
340 nodes = extractNodes(reply.value());
341 QCOMPARE(nodes.contains("foobar"), false);
342 }
344 QStringList extractNodes(const QString &introspectionXml) {
345 QXmlStreamReader introspectReply(introspectionXml);
347 QStringList ret;
348 while (!introspectReply.atEnd() && !introspectReply.hasError()) {
349 QXmlStreamReader::TokenType token = introspectReply.readNext();
351 if (token == QXmlStreamReader::StartElement) {
352 if (introspectReply.name() == "node" && introspectReply.attributes().count() > 0) {
353 ret << introspectReply.attributes().value("name").toString();
354 }
355 }
356 }
357 return ret;
358 }
360 void testCountEmblems() {
361 // Call GetAll on abs-icon
362 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
363 QDBusReply<QVariantMap> reply = interface.call("GetAll");
364 QVariantMap map = reply.value();
366 // Make sure GetAll returns a map with count and countVisible props
367 QCOMPARE(map.contains("count"), true);
368 QCOMPARE(map.contains("countVisible"), true);
370 // Make sure count is intitilized to 0 and non-visible
371 QCOMPARE(map.value("count").toInt(), 0);
372 QCOMPARE(map.value("countVisible").toBool(), false);
374 // Now make it visible and set it to 55 through D-Bus
375 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", 55);
376 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", true);
378 // Fetch it again using GetAll
379 reply = interface.call("GetAll");
380 map = reply.value();
382 // Make sure values have changed on the D-Bus interface
383 QCOMPARE(map.value("count").toInt(), 55);
384 QCOMPARE(map.value("countVisible").toBool(), true);
386 // Now the item on the upper side of the API
387 int index = launcherModel->findApplication("abs-icon");
388 QCOMPARE(index >= 0, true);
390 // And make sure values have changed there as well
391 QCOMPARE(launcherModel->get(index)->countVisible(), true);
392 QCOMPARE(launcherModel->get(index)->count(), 55);
393 }
395 void testRefreshAfterDeletedDesktopFiles_data() {
396 QTest::addColumn<bool>("deleted");
397 QTest::newRow("have .desktop files") << false;
398 QTest::newRow("deleted .desktop files") << true;
399 }
401 void testRefreshAfterDeletedDesktopFiles() {
402 QFETCH(bool, deleted);
404 // pin both apps
405 launcherModel->pin("abs-icon");
406 launcherModel->pin("no-icon");
407 // close both apps
408 appManager->removeApplication(0);
409 appManager->removeApplication(0);
411 // "delete" the .desktop files
412 QString oldCurrent = QDir::currentPath();
413 if (deleted) {
414 // In testing mode, the launcher searches the current dir for the sample .desktop file
415 // We can make that fail by changing the current dir
416 QDir::setCurrent("..");
417 }
419 // Call refresh
420 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "com.canonical.Unity.Launcher");
421 QDBusReply<void> reply = interface.call("Refresh");
423 // Make sure the call to Refresh returned without error.
424 QCOMPARE(reply.isValid(), true);
426 QCOMPARE(launcherModel->rowCount(), deleted ? 0 : 2);
428 // Restoring current dir
429 QDir::setCurrent(oldCurrent);
430 }
432 void testSettings() {
433 GSettings *settings = launcherModel->m_settings;
434 QSignalSpy spy(launcherModel, SIGNAL(hint()));
436 // Nothing pinned at startup
437 QCOMPARE(settings->storedApplications().count(), 0);
439 // pin both apps
440 launcherModel->pin("abs-icon");
441 launcherModel->pin("no-icon");
442 QCOMPARE(spy.count(), 0);
444 // Now settings should have 2 apps
445 QCOMPARE(settings->storedApplications().count(), 2);
447 // close both apps
448 appManager->removeApplication(0);
449 appManager->removeApplication(0);
451 // Now settings should have 2 apps
452 QCOMPARE(settings->storedApplications().count(), 2);
454 // Now remove 1 app through the backend, make sure one is still there
455 settings->simulateDConfChanged(QStringList() << "abs-icon");
456 QCOMPARE(settings->storedApplications().count(), 1);
457 QCOMPARE(spy.count(), 1);
459 // Check if it disappeared from the frontend too
460 QCOMPARE(launcherModel->rowCount(), 1);
462 // Add them back but in reverse order
463 settings->simulateDConfChanged(QStringList() << "no-icon" << "abs-icon");
464 QCOMPARE(launcherModel->rowCount(), 2);
465 QCOMPARE(launcherModel->get(0)->appId(), QString("no-icon"));
466 QCOMPARE(launcherModel->get(1)->appId(), QString("abs-icon"));
467 QCOMPARE(spy.count(), 2);
468 }
469};
471QTEST_GUILESS_MAIN(LauncherModelTest)
472#include "launchermodeltest.moc"