on 84adc8a help sign in
unity8/tests/plugins/Unity/Launcher/launchermodeltest.cpp
461 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 QCoreApplication::processEvents(); // to let the model register on DBus
137 QCOMPARE(launcherModel->rowCount(QModelIndex()), 0);
139 appManager = new MockAppManager(this);
140 launcherModel->setApplicationManager(appManager);
141 }
143 // Adding 2 apps to the mock appmanager. Both should appear in the launcher.
144 void init() {
145 appManager->addApplication(new MockApp("abs-icon"));
146 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
148 appManager->addApplication(new MockApp("no-icon"));
149 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
151 launcherModel->m_settings->setStoredApplications(QStringList());
152 }
154 // Removing apps from appmanager and launcher as pinned ones would stick
155 void cleanup() {
156 while (appManager->count() > 0) {
157 appManager->removeApplication(0);
158 }
159 while (launcherModel->rowCount(QModelIndex()) > 0) {
160 launcherModel->requestRemove(launcherModel->get(0)->appId());
161 }
162 }
164 void testMove() {
165 QCOMPARE(launcherModel->get(0)->pinned(), false);
166 QCOMPARE(launcherModel->get(1)->pinned(), false);
168 LauncherItemInterface *item0BeforeMove = launcherModel->get(0);
169 LauncherItemInterface *item1BeforeMove = launcherModel->get(1);
170 launcherModel->move(1, 0);
172 QCOMPARE(item0BeforeMove, launcherModel->get(1));
173 QCOMPARE(item1BeforeMove, launcherModel->get(0));
175 // moved item must be pinned now
176 QCOMPARE(item0BeforeMove->pinned(), false);
177 QCOMPARE(item1BeforeMove->pinned(), true);
178 }
180 void testPinning() {
181 QSignalSpy spy(launcherModel, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)));
182 QCOMPARE(launcherModel->get(0)->pinned(), false);
183 QCOMPARE(launcherModel->get(1)->pinned(), false);
184 launcherModel->pin(launcherModel->get(0)->appId());
185 QCOMPARE(launcherModel->get(0)->pinned(), true);
186 QCOMPARE(launcherModel->get(1)->pinned(), false);
187 QCOMPARE(spy.count() > 0, true);
188 QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
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);
196 }
198 void testRemove_data() {
200 QTest::addColumn<bool>("pinned");
201 QTest::addColumn<bool>("running");
203 QTest::newRow("non-pinned, running") << false << true;
204 QTest::newRow("pinned, running") << true << true;
205 QTest::newRow("pinned, non-running") << true << false;
206 }
208 void testRemove() {
209 QFETCH(bool, pinned);
210 QFETCH(bool, running);
212 // In the beginning we always have two items
213 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
215 // pin one if required
216 if (pinned) {
217 launcherModel->pin("abs-icon");
218 }
220 // stop it if required
221 if (!running) {
222 appManager->stopApplication("abs-icon");
223 }
225 // Now remove it
226 launcherModel->requestRemove("abs-icon");
228 if (running) {
229 // both apps are running, both apps must still be here
230 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
232 // Item must be unpinned now
233 int index = launcherModel->findApplication("abs-icon");
234 QCOMPARE(launcherModel->get(index)->pinned(), false);
236 } else if (pinned) {
237 // Item 1 must go away, item 0 is here to stay
238 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
239 }
241 // done our checks. now stop the app if was still running
242 if (running) {
243 appManager->stopApplication("abs-icon");
244 }
246 // It needs to go away in any case now
247 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
248 }
250 void testQuickListPinningRemoving() {
251 // we start with 2 unpinned items
252 QCOMPARE(launcherModel->get(0)->pinned(), false);
253 QCOMPARE(launcherModel->get(1)->pinned(), false);
254 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
256 // find the Pin item in the quicklist
257 QuickListModel *model = qobject_cast<QuickListModel*>(launcherModel->get(0)->quickList());
258 int pinActionIndex = -1;
259 for (int i = 0; i < model->rowCount(QModelIndex()); ++i) {
260 if (model->get(i).actionId() == "pin_item") {
261 pinActionIndex = i;
262 break;
263 }
264 }
265 QVERIFY(pinActionIndex >= 0);
267 // trigger pin item quicklist action => Item must be pinned now.
268 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
269 QCOMPARE(launcherModel->get(0)->pinned(), true);
270 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
272 // quicklist needs to transform to remove item. trigger it and check it item goes away
273 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
274 QCOMPARE(launcherModel->get(0)->pinned(), false);
276 // still needs to be here as the app is still here
277 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
278 // close the app
279 appManager->removeApplication(0);
280 // Now it needs to go away
281 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
282 }
284 void testApplicationFocused() {
285 // all apps unfocused at beginning...
286 QCOMPARE(launcherModel->get(0)->focused(), false);
287 QCOMPARE(launcherModel->get(1)->focused(), false);
289 appManager->focusApplication("abs-icon");
290 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
291 QCOMPARE(launcherModel->get(0)->focused(), true);
292 QCOMPARE(launcherModel->get(1)->focused(), false);
294 appManager->focusApplication("no-icon");
295 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
296 QCOMPARE(launcherModel->get(0)->focused(), false);
297 QCOMPARE(launcherModel->get(1)->focused(), true);
298 }
300 void testClosingApps() {
301 // At the start there are 2 items. Let's pin one.
302 launcherModel->pin("abs-icon");
303 while (appManager->count() > 0) {
304 appManager->removeApplication(0);
305 }
306 // The pinned one needs to stay, the other needs to disappear
307 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
308 QCOMPARE(launcherModel->get(0)->appId(), QLatin1String("abs-icon"));
309 }
311 void testGetUrlForAppId() {
312 QCOMPARE(launcherModel->getUrlForAppId(QString()), QString());
313 QCOMPARE(launcherModel->getUrlForAppId(""), QString());
314 QCOMPARE(launcherModel->getUrlForAppId("no-name"), QString("application:///no-name.desktop"));
315 QCOMPARE(launcherModel->getUrlForAppId("com.test.good"), QString("application:///com.test.good.desktop"));
316 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application"), QString("appid://com.test.good/application/current-user-version"));
317 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application_1.2.3"), QString("appid://com.test.good/application/current-user-version"));
318 }
320 void testIntrospection() {
321 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "org.freedesktop.DBus.Introspectable");
322 QDBusReply<QString> reply = interface.call("Introspect");
323 QStringList nodes = extractNodes(reply.value());
324 QCOMPARE(nodes.count(), launcherModel->rowCount());
326 appManager->addApplication(new MockApp("foobar"));
327 reply = interface.call("Introspect");
328 nodes = extractNodes(reply.value());
329 QCOMPARE(nodes.contains("foobar"), true);
331 appManager->removeApplication(2);
332 reply = interface.call("Introspect");
333 nodes = extractNodes(reply.value());
334 QCOMPARE(nodes.contains("foobar"), false);
335 }
337 QStringList extractNodes(const QString &introspectionXml) {
338 QXmlStreamReader introspectReply(introspectionXml);
340 QStringList ret;
341 while (!introspectReply.atEnd() && !introspectReply.hasError()) {
342 QXmlStreamReader::TokenType token = introspectReply.readNext();
344 if (token == QXmlStreamReader::StartElement) {
345 if (introspectReply.name() == "node" && introspectReply.attributes().count() > 0) {
346 ret << introspectReply.attributes().value("name").toString();
347 }
348 }
349 }
350 return ret;
351 }
353 void testCountEmblems() {
354 // Call GetAll on abs-icon
355 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
356 QDBusReply<QVariantMap> reply = interface.call("GetAll");
357 QVariantMap map = reply.value();
359 // Make sure GetAll returns a map with count and countVisible props
360 QCOMPARE(map.contains("count"), true);
361 QCOMPARE(map.contains("countVisible"), true);
363 // Make sure count is intitilized to 0 and non-visible
364 QCOMPARE(map.value("count").toInt(), 0);
365 QCOMPARE(map.value("countVisible").toBool(), false);
367 // Now make it visible and set it to 55 through D-Bus
368 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", 55);
369 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", true);
371 // Fetch it again using GetAll
372 reply = interface.call("GetAll");
373 map = reply.value();
375 // Make sure values have changed on the D-Bus interface
376 QCOMPARE(map.value("count").toInt(), 55);
377 QCOMPARE(map.value("countVisible").toBool(), true);
379 // Now the item on the upper side of the API
380 int index = launcherModel->findApplication("abs-icon");
381 QCOMPARE(index >= 0, true);
383 // And make sure values have changed there as well
384 QCOMPARE(launcherModel->get(index)->countVisible(), true);
385 QCOMPARE(launcherModel->get(index)->count(), 55);
386 }
388 void testRefreshAfterDeletedDesktopFiles_data() {
389 QTest::addColumn<bool>("deleted");
390 QTest::newRow("have .desktop files") << false;
391 QTest::newRow("deleted .desktop files") << true;
392 }
394 void testRefreshAfterDeletedDesktopFiles() {
395 QFETCH(bool, deleted);
397 // pin both apps
398 launcherModel->pin("abs-icon");
399 launcherModel->pin("no-icon");
400 // close both apps
401 appManager->removeApplication(0);
402 appManager->removeApplication(0);
404 // "delete" the .desktop files
405 QString oldCurrent = QDir::currentPath();
406 if (deleted) {
407 // In testing mode, the launcher searches the current dir for the sample .desktop file
408 // We can make that fail by changing the current dir
409 QDir::setCurrent("..");
410 }
412 // Call refresh
413 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "com.canonical.Unity.Launcher");
414 QDBusReply<void> reply = interface.call("Refresh");
416 // Make sure the call to Refresh returned without error.
417 QCOMPARE(reply.isValid(), true);
419 QCOMPARE(launcherModel->rowCount(), deleted ? 0 : 2);
421 // Restoring current dir
422 QDir::setCurrent(oldCurrent);
423 }
425 void testSettings() {
426 GSettings *settings = launcherModel->m_settings;
428 // Nothing pinned at startup
429 QCOMPARE(settings->storedApplications().count(), 0);
431 // pin both apps
432 launcherModel->pin("abs-icon");
433 launcherModel->pin("no-icon");
435 // Now settings should have 2 apps
436 QCOMPARE(settings->storedApplications().count(), 2);
438 // close both apps
439 appManager->removeApplication(0);
440 appManager->removeApplication(0);
442 // Now settings should have 2 apps
443 QCOMPARE(settings->storedApplications().count(), 2);
445 // Now remove 1 app through the backend, make sure one is still there
446 settings->simulateDConfChanged(QStringList() << "abs-icon");
447 QCOMPARE(settings->storedApplications().count(), 1);
449 // Check if it disappeared from the frontend too
450 QCOMPARE(launcherModel->rowCount(), 1);
452 // Add them back but in reverse order
453 settings->simulateDConfChanged(QStringList() << "no-icon" << "abs-icon");
454 QCOMPARE(launcherModel->rowCount(), 2);
455 QCOMPARE(launcherModel->get(0)->appId(), QString("no-icon"));
456 QCOMPARE(launcherModel->get(1)->appId(), QString("abs-icon"));
457 }
458};
460QTEST_GUILESS_MAIN(LauncherModelTest)
461#include "launchermodeltest.moc"